blob: b5c875d7132b2fcd4ca7ddc675103172073e7dd6 [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
Joe Perches5b579802018-08-21 21:57:33 -0700243my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700244if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700245 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700246 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700247 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700248}
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|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800349 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700350 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700351 __refconst|
352 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800353 __rcu|
354 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700355 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800356our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
357our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
358our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
359our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
360our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700361
Wolfram Sang52131292010-03-05 13:43:51 -0800362# Notes to $Attribute:
363# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700364our $Attribute = qr{
365 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700366 __percpu|
367 __nocast|
368 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200369 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700370 __packed__|
371 __packed2__|
372 __naked|
373 __maybe_unused|
374 __always_unused|
375 __noreturn|
376 __used|
377 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800378 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700379 __noclone|
380 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700381 __read_mostly|
382 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700383 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700384 ____cacheline_aligned|
385 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800386 ____cacheline_internodealigned_in_smp|
387 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700388 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700389our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700390our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700391our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
392our $Lval = qr{$Ident(?:$Member)*};
393
Joe Perches95e2c602013-07-03 15:05:20 -0700394our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
395our $Binary = qr{(?i)0b[01]+$Int_type?};
396our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
397our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700398our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800399our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800400our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
401our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
402our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800403our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700404our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800405our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700406our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700407our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700408our $Operators = qr{
409 <=|>=|==|!=|
410 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700411 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700412 }x;
413
Joe Perches91cb5192014-04-03 14:49:32 -0700414our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
415
Joe Perchesab7e23f2015-04-16 12:44:22 -0700416our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800417our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700418our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700419our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800420our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700421our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800422our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700423our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800424
Joe Perches15662b32011-10-31 17:13:12 -0700425our $NON_ASCII_UTF8 = qr{
426 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700427 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
428 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
429 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
430 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
431 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
432 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
433}x;
434
Joe Perches15662b32011-10-31 17:13:12 -0700435our $UTF8 = qr{
436 [\x09\x0A\x0D\x20-\x7E] # ASCII
437 | $NON_ASCII_UTF8
438}x;
439
Joe Perchese6176fa2015-06-25 15:02:49 -0700440our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800441our $typeOtherOSTypedefs = qr{(?x:
442 u_(?:char|short|int|long) | # bsd
443 u(?:nchar|short|int|long) # sysv
444)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700445our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700446 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700447 atomic_t
448)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700449our $typeTypedefs = qr{(?x:
450 $typeC99Typedefs\b|
451 $typeOtherOSTypedefs\b|
452 $typeKernelTypedefs\b
453)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700454
Joe Perches6d32f7a2015-11-06 16:31:37 -0800455our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
456
Joe Perches691e6692010-03-05 13:43:51 -0800457our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800458 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700459 (?:[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 -0800460 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700461 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700462 panic|
Joe Perches06668722013-11-12 15:10:07 -0800463 MODULE_[A-Z_]+|
464 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800465)};
466
Joe Perches20112472011-07-25 17:13:23 -0700467our $signature_tags = qr{(?xi:
468 Signed-off-by:|
469 Acked-by:|
470 Tested-by:|
471 Reviewed-by:|
472 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700473 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700474 To:|
475 Cc:
476)};
477
Joe Perches18130872014-08-06 16:11:22 -0700478our @typeListMisordered = (
479 qr{char\s+(?:un)?signed},
480 qr{int\s+(?:(?:un)?signed\s+)?short\s},
481 qr{int\s+short(?:\s+(?:un)?signed)},
482 qr{short\s+int(?:\s+(?:un)?signed)},
483 qr{(?:un)?signed\s+int\s+short},
484 qr{short\s+(?:un)?signed},
485 qr{long\s+int\s+(?:un)?signed},
486 qr{int\s+long\s+(?:un)?signed},
487 qr{long\s+(?:un)?signed\s+int},
488 qr{int\s+(?:un)?signed\s+long},
489 qr{int\s+(?:un)?signed},
490 qr{int\s+long\s+long\s+(?:un)?signed},
491 qr{long\s+long\s+int\s+(?:un)?signed},
492 qr{long\s+long\s+(?:un)?signed\s+int},
493 qr{long\s+long\s+(?:un)?signed},
494 qr{long\s+(?:un)?signed},
495);
496
Andy Whitcroft8905a672007-11-28 16:21:06 -0800497our @typeList = (
498 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700499 qr{(?:(?:un)?signed\s+)?char},
500 qr{(?:(?:un)?signed\s+)?short\s+int},
501 qr{(?:(?:un)?signed\s+)?short},
502 qr{(?:(?:un)?signed\s+)?int},
503 qr{(?:(?:un)?signed\s+)?long\s+int},
504 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
505 qr{(?:(?:un)?signed\s+)?long\s+long},
506 qr{(?:(?:un)?signed\s+)?long},
507 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800508 qr{float},
509 qr{double},
510 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800511 qr{struct\s+$Ident},
512 qr{union\s+$Ident},
513 qr{enum\s+$Ident},
514 qr{${Ident}_t},
515 qr{${Ident}_handler},
516 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700517 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800518);
Joe Perches938224b2016-01-20 14:59:15 -0800519
520our $C90_int_types = qr{(?x:
521 long\s+long\s+int\s+(?:un)?signed|
522 long\s+long\s+(?:un)?signed\s+int|
523 long\s+long\s+(?:un)?signed|
524 (?:(?:un)?signed\s+)?long\s+long\s+int|
525 (?:(?:un)?signed\s+)?long\s+long|
526 int\s+long\s+long\s+(?:un)?signed|
527 int\s+(?:(?:un)?signed\s+)?long\s+long|
528
529 long\s+int\s+(?:un)?signed|
530 long\s+(?:un)?signed\s+int|
531 long\s+(?:un)?signed|
532 (?:(?:un)?signed\s+)?long\s+int|
533 (?:(?:un)?signed\s+)?long|
534 int\s+long\s+(?:un)?signed|
535 int\s+(?:(?:un)?signed\s+)?long|
536
537 int\s+(?:un)?signed|
538 (?:(?:un)?signed\s+)?int
539)};
540
Alex Dowad485ff232015-06-25 15:02:52 -0700541our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700542our @typeListWithAttr = (
543 @typeList,
544 qr{struct\s+$InitAttribute\s+$Ident},
545 qr{union\s+$InitAttribute\s+$Ident},
546);
547
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700548our @modifierList = (
549 qr{fastcall},
550);
Alex Dowad485ff232015-06-25 15:02:52 -0700551our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800552
Joe Perches24358802014-04-03 14:49:13 -0700553our @mode_permission_funcs = (
554 ["module_param", 3],
555 ["module_param_(?:array|named|string)", 4],
556 ["module_param_array_named", 5],
557 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
558 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700559 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
560 ["IIO_DEV_ATTR_[A-Z_]+", 1],
561 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
562 ["SENSOR_TEMPLATE(?:_2|)", 3],
563 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700564);
565
Joe Perches515a2352014-04-03 14:49:24 -0700566#Create a search pattern for all these functions to speed up a loop below
567our $mode_perms_search = "";
568foreach my $entry (@mode_permission_funcs) {
569 $mode_perms_search .= '|' if ($mode_perms_search ne "");
570 $mode_perms_search .= $entry->[0];
571}
Joe Perches00180462018-02-06 15:38:55 -0800572$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700573
Joe Perchesb392c642015-04-16 12:44:16 -0700574our $mode_perms_world_writable = qr{
575 S_IWUGO |
576 S_IWOTH |
577 S_IRWXUGO |
578 S_IALLUGO |
579 0[0-7][0-7][2367]
580}x;
581
Joe Perchesf90774e2016-10-11 13:51:47 -0700582our %mode_permission_string_types = (
583 "S_IRWXU" => 0700,
584 "S_IRUSR" => 0400,
585 "S_IWUSR" => 0200,
586 "S_IXUSR" => 0100,
587 "S_IRWXG" => 0070,
588 "S_IRGRP" => 0040,
589 "S_IWGRP" => 0020,
590 "S_IXGRP" => 0010,
591 "S_IRWXO" => 0007,
592 "S_IROTH" => 0004,
593 "S_IWOTH" => 0002,
594 "S_IXOTH" => 0001,
595 "S_IRWXUGO" => 0777,
596 "S_IRUGO" => 0444,
597 "S_IWUGO" => 0222,
598 "S_IXUGO" => 0111,
599);
600
601#Create a search pattern for all these strings to speed up a loop below
602our $mode_perms_string_search = "";
603foreach my $entry (keys %mode_permission_string_types) {
604 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
605 $mode_perms_string_search .= $entry;
606}
Joe Perches00180462018-02-06 15:38:55 -0800607our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
608our $multi_mode_perms_string_search = qr{
609 ${single_mode_perms_string_search}
610 (?:\s*\|\s*${single_mode_perms_string_search})*
611}x;
612
613sub perms_to_octal {
614 my ($string) = @_;
615
616 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
617
618 my $val = "";
619 my $oval = "";
620 my $to = 0;
621 my $curpos = 0;
622 my $lastpos = 0;
623 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
624 $curpos = pos($string);
625 my $match = $2;
626 my $omatch = $1;
627 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
628 $lastpos = $curpos;
629 $to |= $mode_permission_string_types{$match};
630 $val .= '\s*\|\s*' if ($val ne "");
631 $val .= $match;
632 $oval .= $omatch;
633 }
634 $oval =~ s/^\s*\|\s*//;
635 $oval =~ s/\s*\|\s*$//;
636 return sprintf("%04o", $to);
637}
Joe Perchesf90774e2016-10-11 13:51:47 -0700638
Wolfram Sang7840a942010-08-09 17:20:57 -0700639our $allowed_asm_includes = qr{(?x:
640 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700641 memory|
642 time|
643 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700644)};
645# memory.h: ARM has a custom one
646
Kees Cook66b47b42014-10-13 15:51:57 -0700647# Load common spelling mistakes and build regular expression list.
648my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700649my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700650
Joe Perches36061e32014-12-10 15:51:43 -0800651if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800652 while (<$spelling>) {
653 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700654
Joe Perches36061e32014-12-10 15:51:43 -0800655 $line =~ s/\s*\n?$//g;
656 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700657
Joe Perches36061e32014-12-10 15:51:43 -0800658 next if ($line =~ m/^\s*#/);
659 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700660
Joe Perches36061e32014-12-10 15:51:43 -0800661 my ($suspect, $fix) = split(/\|\|/, $line);
662
Joe Perches36061e32014-12-10 15:51:43 -0800663 $spelling_fix{$suspect} = $fix;
664 }
665 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800666} else {
667 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700668}
Kees Cook66b47b42014-10-13 15:51:57 -0700669
Joe Perchesebfd7d62015-04-16 12:44:14 -0700670if ($codespell) {
671 if (open(my $spelling, '<', $codespellfile)) {
672 while (<$spelling>) {
673 my $line = $_;
674
675 $line =~ s/\s*\n?$//g;
676 $line =~ s/^\s*//g;
677
678 next if ($line =~ m/^\s*#/);
679 next if ($line =~ m/^\s*$/);
680 next if ($line =~ m/, disabled/i);
681
682 $line =~ s/,.*$//;
683
684 my ($suspect, $fix) = split(/->/, $line);
685
686 $spelling_fix{$suspect} = $fix;
687 }
688 close($spelling);
689 } else {
690 warn "No codespell typos will be found - file '$codespellfile': $!\n";
691 }
692}
693
694$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
695
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700696sub read_words {
697 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700698
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700699 if (open(my $words, '<', $file)) {
700 while (<$words>) {
701 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700702
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700703 $line =~ s/\s*\n?$//g;
704 $line =~ s/^\s*//g;
705
706 next if ($line =~ m/^\s*#/);
707 next if ($line =~ m/^\s*$/);
708 if ($line =~ /\s/) {
709 print("$file: '$line' invalid - ignored\n");
710 next;
711 }
712
713 $$wordsRef .= '|' if ($$wordsRef ne "");
714 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700715 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700716 close($file);
717 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700718 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700719
720 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700721}
722
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700723my $const_structs = "";
724read_words(\$const_structs, $conststructsfile)
725 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
726
727my $typeOtherTypedefs = "";
728if (length($typedefsfile)) {
729 read_words(\$typeOtherTypedefs, $typedefsfile)
730 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
731}
732$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
733
Andy Whitcroft8905a672007-11-28 16:21:06 -0800734sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700735 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
736 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700737 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700738 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700739 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700740 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700741 (?:$typeTypedefs\b)|
742 (?:${all}\b)
743 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800744 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700745 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800746 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800747 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700748 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700749 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800750 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700751 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800752 }x;
Joe Perches18130872014-08-06 16:11:22 -0700753 $NonptrTypeMisordered = qr{
754 (?:$Modifier\s+|const\s+)*
755 (?:
756 (?:${Misordered}\b)
757 )
758 (?:\s+$Modifier|\s+const)*
759 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700760 $NonptrTypeWithAttr = qr{
761 (?:$Modifier\s+|const\s+)*
762 (?:
763 (?:typeof|__typeof__)\s*\([^\)]*\)|
764 (?:$typeTypedefs\b)|
765 (?:${allWithAttr}\b)
766 )
767 (?:\s+$Modifier|\s+const)*
768 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800769 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700770 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700771 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700772 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800773 }x;
Joe Perches18130872014-08-06 16:11:22 -0700774 $TypeMisordered = qr{
775 $NonptrTypeMisordered
776 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
777 (?:\s+$Inline|\s+$Modifier)*
778 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700779 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700780 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800781}
782build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700783
Joe Perches7d2367a2011-07-25 17:13:22 -0700784our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700785
786# Using $balanced_parens, $LvalOrFunc, or $FuncArg
787# requires at least perl version v5.10.0
788# Any use must be runtime checked with $^V
789
790our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700791our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800792our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700793
Joe Perchesf8422302014-08-06 16:11:31 -0700794our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700795 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700796 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700797 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
798 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700799)};
800
Joe Perches7d2367a2011-07-25 17:13:22 -0700801sub deparenthesize {
802 my ($string) = @_;
803 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700804
805 while ($string =~ /^\s*\(.*\)\s*$/) {
806 $string =~ s@^\s*\(\s*@@;
807 $string =~ s@\s*\)\s*$@@;
808 }
809
Joe Perches7d2367a2011-07-25 17:13:22 -0700810 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700811
Joe Perches7d2367a2011-07-25 17:13:22 -0700812 return $string;
813}
814
Joe Perches34456862013-07-03 15:05:34 -0700815sub seed_camelcase_file {
816 my ($file) = @_;
817
818 return if (!(-f $file));
819
820 local $/;
821
822 open(my $include_file, '<', "$file")
823 or warn "$P: Can't read '$file' $!\n";
824 my $text = <$include_file>;
825 close($include_file);
826
827 my @lines = split('\n', $text);
828
829 foreach my $line (@lines) {
830 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
831 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
832 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800833 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
834 $camelcase{$1} = 1;
835 } 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 -0700836 $camelcase{$1} = 1;
837 }
838 }
839}
840
Joe Perches85b0ee12016-10-11 13:51:44 -0700841sub is_maintained_obsolete {
842 my ($filename) = @_;
843
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800844 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700845
Joe Perches0616efa2016-10-11 13:52:02 -0700846 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 -0700847
848 return $status =~ /obsolete/i;
849}
850
Joe Perches34456862013-07-03 15:05:34 -0700851my $camelcase_seeded = 0;
852sub seed_camelcase_includes {
853 return if ($camelcase_seeded);
854
855 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700856 my $camelcase_cache = "";
857 my @include_files = ();
858
859 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700860
Richard Genoud3645e322014-02-10 14:25:32 -0800861 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700862 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
863 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700864 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700865 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700866 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700867 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700868 @include_files = split('\n', $files);
869 foreach my $file (@include_files) {
870 my $date = POSIX::strftime("%Y%m%d%H%M",
871 localtime((stat $file)[9]));
872 $last_mod_date = $date if ($last_mod_date < $date);
873 }
874 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700875 }
Joe Perchesc707a812013-07-08 16:00:43 -0700876
877 if ($camelcase_cache ne "" && -f $camelcase_cache) {
878 open(my $camelcase_file, '<', "$camelcase_cache")
879 or warn "$P: Can't read '$camelcase_cache' $!\n";
880 while (<$camelcase_file>) {
881 chomp;
882 $camelcase{$_} = 1;
883 }
884 close($camelcase_file);
885
886 return;
887 }
888
Richard Genoud3645e322014-02-10 14:25:32 -0800889 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700890 $files = `git ls-files "include/*.h"`;
891 @include_files = split('\n', $files);
892 }
893
Joe Perches34456862013-07-03 15:05:34 -0700894 foreach my $file (@include_files) {
895 seed_camelcase_file($file);
896 }
Joe Perches351b2a12013-07-03 15:05:36 -0700897
Joe Perchesc707a812013-07-08 16:00:43 -0700898 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700899 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700900 open(my $camelcase_file, '>', "$camelcase_cache")
901 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700902 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
903 print $camelcase_file ("$_\n");
904 }
905 close($camelcase_file);
906 }
Joe Perches34456862013-07-03 15:05:34 -0700907}
908
Joe Perchesd311cd42014-08-06 16:10:57 -0700909sub git_commit_info {
910 my ($commit, $id, $desc) = @_;
911
912 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
913
914 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
915 $output =~ s/^\s*//gm;
916 my @lines = split("\n", $output);
917
Joe Perches0d7835f2015-02-13 14:38:35 -0800918 return ($id, $desc) if ($#lines < 0);
919
Joe Perchesd311cd42014-08-06 16:10:57 -0700920 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
921# Maybe one day convert this block of bash into something that returns
922# all matching commit ids, but it's very slow...
923#
924# echo "checking commits $1..."
925# git rev-list --remotes | grep -i "^$1" |
926# while read line ; do
927# git log --format='%H %s' -1 $line |
928# echo "commit $(cut -c 1-12,41-)"
929# done
930 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700931 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700932 } else {
933 $id = substr($lines[0], 0, 12);
934 $desc = substr($lines[0], 41);
935 }
936
937 return ($id, $desc);
938}
939
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700940$chk_signoff = 0 if ($file);
941
Andy Whitcroft00df3442007-06-08 13:47:06 -0700942my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800943my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700944my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700945my @fixed_inserted = ();
946my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700947my $fixlinenr = -1;
948
Du, Changbin4a593c32016-05-20 17:04:16 -0700949# If input is git commits, extract all commits from the commit expressions.
950# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
951die "$P: No git repository found\n" if ($git && !-e ".git");
952
953if ($git) {
954 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700955 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700956 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700957 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
958 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700959 } elsif ($commit_expr =~ m/\.\./) {
960 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700961 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700962 $git_range = "-1 $commit_expr";
963 }
964 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
965 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700966 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
967 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700968 my $sha1 = $1;
969 my $subject = $2;
970 unshift(@commits, $sha1);
971 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700972 }
973 }
974 die "$P: no git commits after extraction!\n" if (@commits == 0);
975 @ARGV = @commits;
976}
977
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800978my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700979for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800980 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700981 if ($git) {
982 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
983 die "$P: $filename: git format-patch failed - $!\n";
984 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800985 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700986 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800987 } elsif ($filename eq '-') {
988 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700989 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800990 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700991 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700992 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800993 if ($filename eq '-') {
994 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700995 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700996 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800997 } else {
998 $vname = $filename;
999 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001000 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001001 chomp;
1002 push(@rawlines, $_);
1003 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001004 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001005
1006 if ($#ARGV > 0 && $quiet == 0) {
1007 print '-' x length($vname) . "\n";
1008 print "$vname\n";
1009 print '-' x length($vname) . "\n";
1010 }
1011
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001012 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001013 $exit = 1;
1014 }
1015 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001016 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001017 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001018 @fixed_inserted = ();
1019 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001020 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001021 @modifierListFile = ();
1022 @typeListFile = ();
1023 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001024}
1025
Joe Perchesd8469f12015-06-25 15:03:00 -07001026if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001027 hash_show_words(\%use_type, "Used");
1028 hash_show_words(\%ignore_type, "Ignored");
1029
Joe Perches5b579802018-08-21 21:57:33 -07001030 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001031 print << "EOM"
1032
1033NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001034 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001035EOM
1036 }
1037 if ($exit) {
1038 print << "EOM"
1039
1040NOTE: If any of the errors are false positives, please report
1041 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1042EOM
1043 }
1044}
1045
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001046exit($exit);
1047
1048sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001049 my ($root) = @_;
1050
1051 my @tree_check = (
1052 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1053 "README", "Documentation", "arch", "include", "drivers",
1054 "fs", "init", "ipc", "kernel", "lib", "scripts",
1055 );
1056
1057 foreach my $check (@tree_check) {
1058 if (! -e $root . '/' . $check) {
1059 return 0;
1060 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001061 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001062 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001063}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001064
Joe Perches20112472011-07-25 17:13:23 -07001065sub parse_email {
1066 my ($formatted_email) = @_;
1067
1068 my $name = "";
1069 my $address = "";
1070 my $comment = "";
1071
1072 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1073 $name = $1;
1074 $address = $2;
1075 $comment = $3 if defined $3;
1076 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1077 $address = $1;
1078 $comment = $2 if defined $2;
1079 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1080 $address = $1;
1081 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001082 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001083 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001084 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001085 $name =~ s/^\"|\"$//g;
1086 # If there's a name left after stripping spaces and
1087 # leading quotes, and the address doesn't have both
1088 # leading and trailing angle brackets, the address
1089 # is invalid. ie:
1090 # "joe smith joe@smith.com" bad
1091 # "joe smith <joe@smith.com" bad
1092 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1093 $name = "";
1094 $address = "";
1095 $comment = "";
1096 }
1097 }
1098
Joe Perches3705ce52013-07-03 15:05:31 -07001099 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001100 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001101 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001102 $address =~ s/^\<|\>$//g;
1103
1104 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1105 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1106 $name = "\"$name\"";
1107 }
1108
1109 return ($name, $address, $comment);
1110}
1111
1112sub format_email {
1113 my ($name, $address) = @_;
1114
1115 my $formatted_email;
1116
Joe Perches3705ce52013-07-03 15:05:31 -07001117 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001118 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001119 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001120
1121 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1122 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1123 $name = "\"$name\"";
1124 }
1125
1126 if ("$name" eq "") {
1127 $formatted_email = "$address";
1128 } else {
1129 $formatted_email = "$name <$address>";
1130 }
1131
1132 return $formatted_email;
1133}
1134
Joe Perchesd311cd42014-08-06 16:10:57 -07001135sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001136 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001137
Joe Perchesbd474ca2014-08-06 16:11:10 -07001138 foreach my $path (split(/:/, $ENV{PATH})) {
1139 if (-e "$path/$bin") {
1140 return "$path/$bin";
1141 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001142 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001143
Joe Perchesbd474ca2014-08-06 16:11:10 -07001144 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001145}
1146
Joe Perches000d1cc12011-07-25 17:13:25 -07001147sub which_conf {
1148 my ($conf) = @_;
1149
1150 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1151 if (-e "$path/$conf") {
1152 return "$path/$conf";
1153 }
1154 }
1155
1156 return "";
1157}
1158
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001159sub expand_tabs {
1160 my ($str) = @_;
1161
1162 my $res = '';
1163 my $n = 0;
1164 for my $c (split(//, $str)) {
1165 if ($c eq "\t") {
1166 $res .= ' ';
1167 $n++;
1168 for (; ($n % 8) != 0; $n++) {
1169 $res .= ' ';
1170 }
1171 next;
1172 }
1173 $res .= $c;
1174 $n++;
1175 }
1176
1177 return $res;
1178}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001179sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001180 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001181 return $res;
1182}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001183
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001184sub line_stats {
1185 my ($line) = @_;
1186
1187 # Drop the diff line leader and expand tabs
1188 $line =~ s/^.//;
1189 $line = expand_tabs($line);
1190
1191 # Pick the indent from the front of the line.
1192 my ($white) = ($line =~ /^(\s*)/);
1193
1194 return (length($line), length($white));
1195}
1196
Andy Whitcroft773647a2008-03-28 14:15:58 -07001197my $sanitise_quote = '';
1198
1199sub sanitise_line_reset {
1200 my ($in_comment) = @_;
1201
1202 if ($in_comment) {
1203 $sanitise_quote = '*/';
1204 } else {
1205 $sanitise_quote = '';
1206 }
1207}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001208sub sanitise_line {
1209 my ($line) = @_;
1210
1211 my $res = '';
1212 my $l = '';
1213
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001214 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001215 my $off = 0;
1216 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001217
Andy Whitcroft773647a2008-03-28 14:15:58 -07001218 # Always copy over the diff marker.
1219 $res = substr($line, 0, 1);
1220
1221 for ($off = 1; $off < length($line); $off++) {
1222 $c = substr($line, $off, 1);
1223
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001224 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001225 # and end, all to $;.
1226 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1227 $sanitise_quote = '*/';
1228
1229 substr($res, $off, 2, "$;$;");
1230 $off++;
1231 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001232 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001233 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001234 $sanitise_quote = '';
1235 substr($res, $off, 2, "$;$;");
1236 $off++;
1237 next;
1238 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001239 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1240 $sanitise_quote = '//';
1241
1242 substr($res, $off, 2, $sanitise_quote);
1243 $off++;
1244 next;
1245 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001246
1247 # A \ in a string means ignore the next character.
1248 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1249 $c eq "\\") {
1250 substr($res, $off, 2, 'XX');
1251 $off++;
1252 next;
1253 }
1254 # Regular quotes.
1255 if ($c eq "'" || $c eq '"') {
1256 if ($sanitise_quote eq '') {
1257 $sanitise_quote = $c;
1258
1259 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001260 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001261 } elsif ($sanitise_quote eq $c) {
1262 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001263 }
1264 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001265
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001266 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001267 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1268 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001269 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1270 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001271 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1272 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001273 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001274 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001275 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001276 }
1277
Daniel Walker113f04a2009-09-21 17:04:35 -07001278 if ($sanitise_quote eq '//') {
1279 $sanitise_quote = '';
1280 }
1281
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001282 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001283 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001284 my $clean = 'X' x length($1);
1285 $res =~ s@\<.*\>@<$clean>@;
1286
1287 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001288 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001289 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001290 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001291 }
1292
Joe Perchesdadf6802016-08-02 14:04:33 -07001293 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1294 my $match = $1;
1295 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1296 }
1297
Andy Whitcroft00df3442007-06-08 13:47:06 -07001298 return $res;
1299}
1300
Joe Perchesa6962d72013-04-29 16:18:13 -07001301sub get_quoted_string {
1302 my ($line, $rawline) = @_;
1303
Joe Perches478b1792018-04-10 16:33:34 -07001304 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001305 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001306 return substr($rawline, $-[0], $+[0] - $-[0]);
1307}
1308
Andy Whitcroft8905a672007-11-28 16:21:06 -08001309sub ctx_statement_block {
1310 my ($linenr, $remain, $off) = @_;
1311 my $line = $linenr - 1;
1312 my $blk = '';
1313 my $soff = $off;
1314 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001315 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001316
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001317 my $loff = 0;
1318
Andy Whitcroft8905a672007-11-28 16:21:06 -08001319 my $type = '';
1320 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001321 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001322 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001323 my $c;
1324 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001325
1326 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001327 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001328 @stack = (['', 0]) if ($#stack == -1);
1329
Andy Whitcroft773647a2008-03-28 14:15:58 -07001330 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001331 # If we are about to drop off the end, pull in more
1332 # context.
1333 if ($off >= $len) {
1334 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001335 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001336 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001337 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001338 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001339 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001340 $len = length($blk);
1341 $line++;
1342 last;
1343 }
1344 # Bail if there is no further context.
1345 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001346 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001347 last;
1348 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001349 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1350 $level++;
1351 $type = '#';
1352 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001353 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001354 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001355 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001356 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001357
Andy Whitcroft773647a2008-03-28 14:15:58 -07001358 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001359
1360 # Handle nested #if/#else.
1361 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1362 push(@stack, [ $type, $level ]);
1363 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1364 ($type, $level) = @{$stack[$#stack - 1]};
1365 } elsif ($remainder =~ /^#\s*endif\b/) {
1366 ($type, $level) = @{pop(@stack)};
1367 }
1368
Andy Whitcroft8905a672007-11-28 16:21:06 -08001369 # Statement ends at the ';' or a close '}' at the
1370 # outermost level.
1371 if ($level == 0 && $c eq ';') {
1372 last;
1373 }
1374
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001375 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001376 if ($level == 0 && $coff_set == 0 &&
1377 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1378 $remainder =~ /^(else)(?:\s|{)/ &&
1379 $remainder !~ /^else\s+if\b/) {
1380 $coff = $off + length($1) - 1;
1381 $coff_set = 1;
1382 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1383 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001384 }
1385
Andy Whitcroft8905a672007-11-28 16:21:06 -08001386 if (($type eq '' || $type eq '(') && $c eq '(') {
1387 $level++;
1388 $type = '(';
1389 }
1390 if ($type eq '(' && $c eq ')') {
1391 $level--;
1392 $type = ($level != 0)? '(' : '';
1393
1394 if ($level == 0 && $coff < $soff) {
1395 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001396 $coff_set = 1;
1397 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001398 }
1399 }
1400 if (($type eq '' || $type eq '{') && $c eq '{') {
1401 $level++;
1402 $type = '{';
1403 }
1404 if ($type eq '{' && $c eq '}') {
1405 $level--;
1406 $type = ($level != 0)? '{' : '';
1407
1408 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001409 if (substr($blk, $off + 1, 1) eq ';') {
1410 $off++;
1411 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001412 last;
1413 }
1414 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001415 # Preprocessor commands end at the newline unless escaped.
1416 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1417 $level--;
1418 $type = '';
1419 $off++;
1420 last;
1421 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001422 $off++;
1423 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001424 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001425 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001426 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001427 $line++;
1428 $remain--;
1429 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001430
1431 my $statement = substr($blk, $soff, $off - $soff + 1);
1432 my $condition = substr($blk, $soff, $coff - $soff + 1);
1433
1434 #warn "STATEMENT<$statement>\n";
1435 #warn "CONDITION<$condition>\n";
1436
Andy Whitcroft773647a2008-03-28 14:15:58 -07001437 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001438
1439 return ($statement, $condition,
1440 $line, $remain + 1, $off - $loff + 1, $level);
1441}
1442
Andy Whitcroftcf655042008-03-04 14:28:20 -08001443sub statement_lines {
1444 my ($stmt) = @_;
1445
1446 # Strip the diff line prefixes and rip blank lines at start and end.
1447 $stmt =~ s/(^|\n)./$1/g;
1448 $stmt =~ s/^\s*//;
1449 $stmt =~ s/\s*$//;
1450
1451 my @stmt_lines = ($stmt =~ /\n/g);
1452
1453 return $#stmt_lines + 2;
1454}
1455
1456sub statement_rawlines {
1457 my ($stmt) = @_;
1458
1459 my @stmt_lines = ($stmt =~ /\n/g);
1460
1461 return $#stmt_lines + 2;
1462}
1463
1464sub statement_block_size {
1465 my ($stmt) = @_;
1466
1467 $stmt =~ s/(^|\n)./$1/g;
1468 $stmt =~ s/^\s*{//;
1469 $stmt =~ s/}\s*$//;
1470 $stmt =~ s/^\s*//;
1471 $stmt =~ s/\s*$//;
1472
1473 my @stmt_lines = ($stmt =~ /\n/g);
1474 my @stmt_statements = ($stmt =~ /;/g);
1475
1476 my $stmt_lines = $#stmt_lines + 2;
1477 my $stmt_statements = $#stmt_statements + 1;
1478
1479 if ($stmt_lines > $stmt_statements) {
1480 return $stmt_lines;
1481 } else {
1482 return $stmt_statements;
1483 }
1484}
1485
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001486sub ctx_statement_full {
1487 my ($linenr, $remain, $off) = @_;
1488 my ($statement, $condition, $level);
1489
1490 my (@chunks);
1491
Andy Whitcroftcf655042008-03-04 14:28:20 -08001492 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001493 ($statement, $condition, $linenr, $remain, $off, $level) =
1494 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001495 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001496 push(@chunks, [ $condition, $statement ]);
1497 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1498 return ($level, $linenr, @chunks);
1499 }
1500
1501 # Pull in the following conditional/block pairs and see if they
1502 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001503 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001504 ($statement, $condition, $linenr, $remain, $off, $level) =
1505 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001506 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001507 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001508 #print "C: push\n";
1509 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001510 }
1511
1512 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001513}
1514
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001515sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001516 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001517 my $line;
1518 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001519 my $blk = '';
1520 my @o;
1521 my @c;
1522 my @res = ();
1523
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001524 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001525 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001526 for ($line = $start; $remain > 0; $line++) {
1527 next if ($rawlines[$line] =~ /^-/);
1528 $remain--;
1529
1530 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001531
1532 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001533 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001534 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001535 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001536 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001537 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001538 $level = pop(@stack);
1539 }
1540
Andy Whitcroft01464f32010-10-26 14:23:19 -07001541 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001542 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1543 if ($off > 0) {
1544 $off--;
1545 next;
1546 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001547
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001548 if ($c eq $close && $level > 0) {
1549 $level--;
1550 last if ($level == 0);
1551 } elsif ($c eq $open) {
1552 $level++;
1553 }
1554 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001555
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001556 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001557 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001558 }
1559
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001560 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001561 }
1562
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001563 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001564}
1565sub ctx_block_outer {
1566 my ($linenr, $remain) = @_;
1567
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001568 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1569 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001570}
1571sub ctx_block {
1572 my ($linenr, $remain) = @_;
1573
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001574 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1575 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001576}
1577sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001578 my ($linenr, $remain, $off) = @_;
1579
1580 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1581 return @r;
1582}
1583sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001584 my ($linenr, $remain) = @_;
1585
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001586 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001587}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001588sub ctx_statement_level {
1589 my ($linenr, $remain, $off) = @_;
1590
1591 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1592}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001593
1594sub ctx_locate_comment {
1595 my ($first_line, $end_line) = @_;
1596
1597 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001598 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001599 return $current_comment if (defined $current_comment);
1600
1601 # Look through the context and try and figure out if there is a
1602 # comment.
1603 my $in_comment = 0;
1604 $current_comment = '';
1605 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001606 my $line = $rawlines[$linenr - 1];
1607 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001608 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1609 $in_comment = 1;
1610 }
1611 if ($line =~ m@/\*@) {
1612 $in_comment = 1;
1613 }
1614 if (!$in_comment && $current_comment ne '') {
1615 $current_comment = '';
1616 }
1617 $current_comment .= $line . "\n" if ($in_comment);
1618 if ($line =~ m@\*/@) {
1619 $in_comment = 0;
1620 }
1621 }
1622
1623 chomp($current_comment);
1624 return($current_comment);
1625}
1626sub ctx_has_comment {
1627 my ($first_line, $end_line) = @_;
1628 my $cmt = ctx_locate_comment($first_line, $end_line);
1629
Andy Whitcroft00df3442007-06-08 13:47:06 -07001630 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001631 ##print "CMMT: $cmt\n";
1632
1633 return ($cmt ne '');
1634}
1635
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001636sub raw_line {
1637 my ($linenr, $cnt) = @_;
1638
1639 my $offset = $linenr - 1;
1640 $cnt++;
1641
1642 my $line;
1643 while ($cnt) {
1644 $line = $rawlines[$offset++];
1645 next if (defined($line) && $line =~ /^-/);
1646 $cnt--;
1647 }
1648
1649 return $line;
1650}
1651
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001652sub get_stat_real {
1653 my ($linenr, $lc) = @_;
1654
1655 my $stat_real = raw_line($linenr, 0);
1656 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1657 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1658 }
1659
1660 return $stat_real;
1661}
1662
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001663sub get_stat_here {
1664 my ($linenr, $cnt, $here) = @_;
1665
1666 my $herectx = $here . "\n";
1667 for (my $n = 0; $n < $cnt; $n++) {
1668 $herectx .= raw_line($linenr, $n) . "\n";
1669 }
1670
1671 return $herectx;
1672}
1673
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001674sub cat_vet {
1675 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001676 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001677
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001678 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001679 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1680 $res .= $1;
1681 if ($2 ne '') {
1682 $coded = sprintf("^%c", unpack('C', $2) + 64);
1683 $res .= $coded;
1684 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001685 }
1686 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001687
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001688 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001689}
1690
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001691my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001692my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001693my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001694my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001695
1696sub annotate_reset {
1697 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001698 $av_pending = '_';
1699 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001700 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001701}
1702
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001703sub annotate_values {
1704 my ($stream, $type) = @_;
1705
1706 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001707 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001708 my $cur = $stream;
1709
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001710 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001711
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001712 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001713 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001714 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001715 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001716 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001717 print "WS($1)\n" if ($dbg_values > 1);
1718 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001719 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001720 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001721 }
1722
Florian Micklerc023e4732011-01-12 16:59:58 -08001723 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001724 print "CAST($1)\n" if ($dbg_values > 1);
1725 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001726 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001727
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001728 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001729 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001730 $type = 'T';
1731
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001732 } elsif ($cur =~ /^($Modifier)\s*/) {
1733 print "MODIFIER($1)\n" if ($dbg_values > 1);
1734 $type = 'T';
1735
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001736 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001737 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001738 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001739 push(@av_paren_type, $type);
1740 if ($2 ne '') {
1741 $av_pending = 'N';
1742 }
1743 $type = 'E';
1744
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001745 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001746 print "UNDEF($1)\n" if ($dbg_values > 1);
1747 $av_preprocessor = 1;
1748 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001749
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001750 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001751 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001752 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001753
1754 push(@av_paren_type, $type);
1755 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001756 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001757
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001758 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001759 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1760 $av_preprocessor = 1;
1761
1762 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1763
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001764 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001765
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001766 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001767 print "PRE_END($1)\n" if ($dbg_values > 1);
1768
1769 $av_preprocessor = 1;
1770
1771 # Assume all arms of the conditional end as this
1772 # one does, and continue as if the #endif was not here.
1773 pop(@av_paren_type);
1774 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001775 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001776
1777 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001778 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001779
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001780 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1781 print "ATTR($1)\n" if ($dbg_values > 1);
1782 $av_pending = $type;
1783 $type = 'N';
1784
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001785 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001786 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001787 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001788 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001789 }
1790 $type = 'N';
1791
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001792 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001793 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001794 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001795 $type = 'N';
1796
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001797 } elsif ($cur =~/^(case)/o) {
1798 print "CASE($1)\n" if ($dbg_values > 1);
1799 $av_pend_colon = 'C';
1800 $type = 'N';
1801
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001802 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001803 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001804 $type = 'N';
1805
1806 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001807 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001808 push(@av_paren_type, $av_pending);
1809 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001810 $type = 'N';
1811
1812 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001813 my $new_type = pop(@av_paren_type);
1814 if ($new_type ne '_') {
1815 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001816 print "PAREN('$1') -> $type\n"
1817 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001818 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001819 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001820 }
1821
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001822 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001823 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001824 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001825 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001826
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001827 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1828 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001829 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001830 } elsif ($type eq 'E') {
1831 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001832 }
1833 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1834 $type = 'V';
1835
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001836 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001837 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001838 $type = 'V';
1839
1840 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001841 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001842 $type = 'N';
1843
Andy Whitcroftcf655042008-03-04 14:28:20 -08001844 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001845 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001846 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001847 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001848
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001849 } elsif ($cur =~/^(,)/) {
1850 print "COMMA($1)\n" if ($dbg_values > 1);
1851 $type = 'C';
1852
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001853 } elsif ($cur =~ /^(\?)/o) {
1854 print "QUESTION($1)\n" if ($dbg_values > 1);
1855 $type = 'N';
1856
1857 } elsif ($cur =~ /^(:)/o) {
1858 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1859
1860 substr($var, length($res), 1, $av_pend_colon);
1861 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1862 $type = 'E';
1863 } else {
1864 $type = 'N';
1865 }
1866 $av_pend_colon = 'O';
1867
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001868 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001869 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001870 $type = 'N';
1871
Andy Whitcroft0d413862008-10-15 22:02:16 -07001872 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001873 my $variant;
1874
1875 print "OPV($1)\n" if ($dbg_values > 1);
1876 if ($type eq 'V') {
1877 $variant = 'B';
1878 } else {
1879 $variant = 'U';
1880 }
1881
1882 substr($var, length($res), 1, $variant);
1883 $type = 'N';
1884
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001885 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001886 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001887 if ($1 ne '++' && $1 ne '--') {
1888 $type = 'N';
1889 }
1890
1891 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001892 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001893 }
1894 if (defined $1) {
1895 $cur = substr($cur, length($1));
1896 $res .= $type x length($1);
1897 }
1898 }
1899
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001900 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001901}
1902
Andy Whitcroft8905a672007-11-28 16:21:06 -08001903sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001904 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001905 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001906 ^(?:
1907 $Modifier|
1908 $Storage|
1909 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001910 DEFINE_\S+
1911 )$|
1912 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001913 goto|
1914 return|
1915 case|
1916 else|
1917 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001918 do|
1919 \#|
1920 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001921 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001922 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001923 )}x;
1924 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1925 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001926 # Check for modifiers.
1927 $possible =~ s/\s*$Storage\s*//g;
1928 $possible =~ s/\s*$Sparse\s*//g;
1929 if ($possible =~ /^\s*$/) {
1930
1931 } elsif ($possible =~ /\s/) {
1932 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001933 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001934 if ($modifier !~ $notPermitted) {
1935 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001936 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001937 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001938 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001939
1940 } else {
1941 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001942 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001943 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001944 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001945 } else {
1946 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001947 }
1948}
1949
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001950my $prefix = '';
1951
Joe Perches000d1cc12011-07-25 17:13:25 -07001952sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001953 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001954
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001955 $type =~ tr/[a-z]/[A-Z]/;
1956
Joe Perchescbec18a2014-04-03 14:49:19 -07001957 return defined $use_type{$type} if (scalar keys %use_type > 0);
1958
1959 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001960}
1961
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001962sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001963 my ($level, $type, $msg) = @_;
1964
1965 if (!show_type($type) ||
1966 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001967 return 0;
1968 }
Joe Perches57230292015-06-25 15:03:03 -07001969 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07001970 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07001971 if ($level eq 'ERROR') {
1972 $output .= RED;
1973 } elsif ($level eq 'WARNING') {
1974 $output .= YELLOW;
1975 } else {
1976 $output .= GREEN;
1977 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001978 }
Joe Perches57230292015-06-25 15:03:03 -07001979 $output .= $prefix . $level . ':';
1980 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07001981 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001982 $output .= "$type:";
1983 }
John Brooks737c0762017-07-10 15:52:24 -07001984 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001985 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001986
1987 if ($showfile) {
1988 my @lines = split("\n", $output, -1);
1989 splice(@lines, 1, 1);
1990 $output = join("\n", @lines);
1991 }
Joe Perches57230292015-06-25 15:03:03 -07001992 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001993
Joe Perches57230292015-06-25 15:03:03 -07001994 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001995
1996 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001997}
Joe Perchescbec18a2014-04-03 14:49:19 -07001998
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001999sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002000 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002001}
Joe Perches000d1cc12011-07-25 17:13:25 -07002002
Joe Perchesd752fcc2014-08-06 16:11:05 -07002003sub fixup_current_range {
2004 my ($lineRef, $offset, $length) = @_;
2005
2006 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2007 my $o = $1;
2008 my $l = $2;
2009 my $no = $o + $offset;
2010 my $nl = $l + $length;
2011 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2012 }
2013}
2014
2015sub fix_inserted_deleted_lines {
2016 my ($linesRef, $insertedRef, $deletedRef) = @_;
2017
2018 my $range_last_linenr = 0;
2019 my $delta_offset = 0;
2020
2021 my $old_linenr = 0;
2022 my $new_linenr = 0;
2023
2024 my $next_insert = 0;
2025 my $next_delete = 0;
2026
2027 my @lines = ();
2028
2029 my $inserted = @{$insertedRef}[$next_insert++];
2030 my $deleted = @{$deletedRef}[$next_delete++];
2031
2032 foreach my $old_line (@{$linesRef}) {
2033 my $save_line = 1;
2034 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002035 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002036 $delta_offset = 0;
2037 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2038 $range_last_linenr = $new_linenr;
2039 fixup_current_range(\$line, $delta_offset, 0);
2040 }
2041
2042 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2043 $deleted = @{$deletedRef}[$next_delete++];
2044 $save_line = 0;
2045 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2046 }
2047
2048 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2049 push(@lines, ${$inserted}{'LINE'});
2050 $inserted = @{$insertedRef}[$next_insert++];
2051 $new_linenr++;
2052 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2053 }
2054
2055 if ($save_line) {
2056 push(@lines, $line);
2057 $new_linenr++;
2058 }
2059
2060 $old_linenr++;
2061 }
2062
2063 return @lines;
2064}
2065
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002066sub fix_insert_line {
2067 my ($linenr, $line) = @_;
2068
2069 my $inserted = {
2070 LINENR => $linenr,
2071 LINE => $line,
2072 };
2073 push(@fixed_inserted, $inserted);
2074}
2075
2076sub fix_delete_line {
2077 my ($linenr, $line) = @_;
2078
2079 my $deleted = {
2080 LINENR => $linenr,
2081 LINE => $line,
2082 };
2083
2084 push(@fixed_deleted, $deleted);
2085}
2086
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002087sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002088 my ($type, $msg) = @_;
2089
2090 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002091 our $clean = 0;
2092 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002093 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002094 }
Joe Perches3705ce52013-07-03 15:05:31 -07002095 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002096}
2097sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002098 my ($type, $msg) = @_;
2099
2100 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002101 our $clean = 0;
2102 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002103 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002104 }
Joe Perches3705ce52013-07-03 15:05:31 -07002105 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002106}
2107sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002108 my ($type, $msg) = @_;
2109
2110 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002111 our $clean = 0;
2112 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002113 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002114 }
Joe Perches3705ce52013-07-03 15:05:31 -07002115 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002116}
2117
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002118sub check_absolute_file {
2119 my ($absolute, $herecurr) = @_;
2120 my $file = $absolute;
2121
2122 ##print "absolute<$absolute>\n";
2123
2124 # See if any suffix of this path is a path within the tree.
2125 while ($file =~ s@^[^/]*/@@) {
2126 if (-f "$root/$file") {
2127 ##print "file<$file>\n";
2128 last;
2129 }
2130 }
2131 if (! -f _) {
2132 return 0;
2133 }
2134
2135 # It is, so see if the prefix is acceptable.
2136 my $prefix = $absolute;
2137 substr($prefix, -length($file)) = '';
2138
2139 ##print "prefix<$prefix>\n";
2140 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002141 WARN("USE_RELATIVE_PATH",
2142 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002143 }
2144}
2145
Joe Perches3705ce52013-07-03 15:05:31 -07002146sub trim {
2147 my ($string) = @_;
2148
Joe Perchesb34c6482013-09-11 14:24:01 -07002149 $string =~ s/^\s+|\s+$//g;
2150
2151 return $string;
2152}
2153
2154sub ltrim {
2155 my ($string) = @_;
2156
2157 $string =~ s/^\s+//;
2158
2159 return $string;
2160}
2161
2162sub rtrim {
2163 my ($string) = @_;
2164
2165 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002166
2167 return $string;
2168}
2169
Joe Perches52ea8502013-11-12 15:10:09 -08002170sub string_find_replace {
2171 my ($string, $find, $replace) = @_;
2172
2173 $string =~ s/$find/$replace/g;
2174
2175 return $string;
2176}
2177
Joe Perches3705ce52013-07-03 15:05:31 -07002178sub tabify {
2179 my ($leading) = @_;
2180
2181 my $source_indent = 8;
2182 my $max_spaces_before_tab = $source_indent - 1;
2183 my $spaces_to_tab = " " x $source_indent;
2184
2185 #convert leading spaces to tabs
2186 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2187 #Remove spaces before a tab
2188 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2189
2190 return "$leading";
2191}
2192
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002193sub pos_last_openparen {
2194 my ($line) = @_;
2195
2196 my $pos = 0;
2197
2198 my $opens = $line =~ tr/\(/\(/;
2199 my $closes = $line =~ tr/\)/\)/;
2200
2201 my $last_openparen = 0;
2202
2203 if (($opens == 0) || ($closes >= $opens)) {
2204 return -1;
2205 }
2206
2207 my $len = length($line);
2208
2209 for ($pos = 0; $pos < $len; $pos++) {
2210 my $string = substr($line, $pos);
2211 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2212 $pos += length($1) - 1;
2213 } elsif (substr($line, $pos, 1) eq '(') {
2214 $last_openparen = $pos;
2215 } elsif (index($string, '(') == -1) {
2216 last;
2217 }
2218 }
2219
Joe Perches91cb5192014-04-03 14:49:32 -07002220 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002221}
2222
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002223sub process {
2224 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002225
2226 my $linenr=0;
2227 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002228 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002229 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002230 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002231
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002232 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002233 my $indent;
2234 my $previndent=0;
2235 my $stashindent=0;
2236
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002237 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002238 my $signoff = 0;
2239 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002240 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002241 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002242 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches77cb8542017-02-24 15:01:28 -08002243 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002244 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002245 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002246 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002247 my $non_utf8_charset = 0;
2248
Joe Perches365dd4e2014-08-06 16:10:42 -07002249 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002250 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002251
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002252 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002253 our $cnt_lines = 0;
2254 our $cnt_error = 0;
2255 our $cnt_warn = 0;
2256 our $cnt_chk = 0;
2257
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002258 # Trace the real file/line as we go.
2259 my $realfile = '';
2260 my $realline = 0;
2261 my $realcnt = 0;
2262 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002263 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002264 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002265 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002266 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002267 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002268
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002269 my $prev_values = 'E';
2270
2271 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002272 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002273 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002274 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002275 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002276
Joe Perches7e51f192013-09-11 14:23:57 -07002277 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002278
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002279 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002280 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002281 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002282 my @setup_docs = ();
2283 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002284
Joe Perchesd8b07712013-11-12 15:10:06 -08002285 my $camelcase_file_seeded = 0;
2286
Rob Herring9f3a8992018-04-10 16:33:13 -07002287 my $checklicenseline = 1;
2288
Andy Whitcroft773647a2008-03-28 14:15:58 -07002289 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002290 my $line;
2291 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002292 $linenr++;
2293 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002294
Joe Perches3705ce52013-07-03 15:05:31 -07002295 push(@fixed, $rawline) if ($fix);
2296
Andy Whitcroft773647a2008-03-28 14:15:58 -07002297 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002298 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002299 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002300 $setup_docs = 1;
2301 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002302 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002303 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002304 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002305 $realline=$1-1;
2306 if (defined $2) {
2307 $realcnt=$3+1;
2308 } else {
2309 $realcnt=1+1;
2310 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002311 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002312
2313 # Guestimate if this is a continuing comment. Run
2314 # the context looking for a comment "edge". If this
2315 # edge is a close comment then we must be in a comment
2316 # at context start.
2317 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002318 my $cnt = $realcnt;
2319 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2320 next if (defined $rawlines[$ln - 1] &&
2321 $rawlines[$ln - 1] =~ /^-/);
2322 $cnt--;
2323 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002324 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002325 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2326 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2327 ($edge) = $1;
2328 last;
2329 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002330 }
2331 if (defined $edge && $edge eq '*/') {
2332 $in_comment = 1;
2333 }
2334
2335 # Guestimate if this is a continuing comment. If this
2336 # is the start of a diff block and this line starts
2337 # ' *' then it is very likely a comment.
2338 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002339 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002340 {
2341 $in_comment = 1;
2342 }
2343
2344 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2345 sanitise_line_reset($in_comment);
2346
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002347 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002348 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002349 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002350 $line = sanitise_line($rawline);
2351 }
2352 push(@lines, $line);
2353
2354 if ($realcnt > 1) {
2355 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2356 } else {
2357 $realcnt = 0;
2358 }
2359
2360 #print "==>$rawline\n";
2361 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002362
2363 if ($setup_docs && $line =~ /^\+/) {
2364 push(@setup_docs, $line);
2365 }
2366 }
2367
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002368 $prefix = '';
2369
Andy Whitcroft773647a2008-03-28 14:15:58 -07002370 $realcnt = 0;
2371 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002372 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002373 foreach my $line (@lines) {
2374 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002375 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002376 my $sline = $line; #copy of $line
2377 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002378
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002379 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002380
Joe Perches12c253a2018-06-07 17:10:58 -07002381# check if it's a mode change, rename or start of a patch
2382 if (!$in_commit_log &&
2383 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2384 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2385 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2386 $is_patch = 1;
2387 }
2388
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002389#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002390 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002391 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2392 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002393 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002394 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002395 $realline=$1-1;
2396 if (defined $2) {
2397 $realcnt=$3+1;
2398 } else {
2399 $realcnt=1+1;
2400 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002401 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002402 $prev_values = 'E';
2403
Andy Whitcroft773647a2008-03-28 14:15:58 -07002404 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002405 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002406 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002407 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002408 if ($context =~ /\b(\w+)\s*\(/) {
2409 $context_function = $1;
2410 } else {
2411 undef $context_function;
2412 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002413 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002414
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002415# track the line number as we move through the hunk, note that
2416# new versions of GNU diff omit the leading space on completely
2417# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002418 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002419 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002420 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002421
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002422 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002423 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002424
2425 # Track the previous line.
2426 ($prevline, $stashline) = ($stashline, $line);
2427 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002428 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2429
Andy Whitcroft773647a2008-03-28 14:15:58 -07002430 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002431
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002432 } elsif ($realcnt == 1) {
2433 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002434 }
2435
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002436 my $hunk_line = ($realcnt != 0);
2437
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002438 $here = "#$linenr: " if (!$file);
2439 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002440
Joe Perches2ac73b42014-06-04 16:12:05 -07002441 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002442 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002443 if ($line =~ /^diff --git.*?(\S+)$/) {
2444 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002445 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002446 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002447 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002448 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002449 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002450 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002451 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002452
2453 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002454 if (!$file && $tree && $p1_prefix ne '' &&
2455 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002456 WARN("PATCH_PREFIX",
2457 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002458 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002459
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002460 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002461 ERROR("MODIFIED_INCLUDE_ASM",
2462 "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 -07002463 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002464 $found_file = 1;
2465 }
2466
Joe Perches34d88152015-06-25 15:03:05 -07002467#make up the handle for any error we report on this line
2468 if ($showfile) {
2469 $prefix = "$realfile:$realline: "
2470 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002471 if ($file) {
2472 $prefix = "$filename:$realline: ";
2473 } else {
2474 $prefix = "$filename:$linenr: ";
2475 }
Joe Perches34d88152015-06-25 15:03:05 -07002476 }
2477
Joe Perches2ac73b42014-06-04 16:12:05 -07002478 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002479 if (is_maintained_obsolete($realfile)) {
2480 WARN("OBSOLETE",
2481 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2482 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002483 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002484 $check = 1;
2485 } else {
2486 $check = $check_orig;
2487 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002488 $checklicenseline = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002489 next;
2490 }
2491
Randy Dunlap389834b2007-06-08 13:47:03 -07002492 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002493
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002494 my $hereline = "$here\n$rawline\n";
2495 my $herecurr = "$here\n$rawline\n";
2496 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002497
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002498 $cnt_lines++ if ($realcnt != 0);
2499
Joe Perchese518e9a2015-06-25 15:03:27 -07002500# Check if the commit log has what seems like a diff which can confuse patch
2501 if ($in_commit_log && !$commit_log_has_diff &&
2502 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2503 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2504 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2505 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2506 ERROR("DIFF_IN_COMMIT_MSG",
2507 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2508 $commit_log_has_diff = 1;
2509 }
2510
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002511# Check for incorrect file permissions
2512 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2513 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002514 if ($realfile !~ m@scripts/@ &&
2515 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002516 ERROR("EXECUTE_PERMISSIONS",
2517 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002518 }
2519 }
2520
Joe Perches20112472011-07-25 17:13:23 -07002521# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002522 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002523 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002524 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002525 }
2526
Joe Perchese0d975b2014-12-10 15:51:49 -08002527# Check if MAINTAINERS is being updated. If so, there's probably no need to
2528# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2529 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2530 $reported_maintainer_file = 1;
2531 }
2532
Joe Perches20112472011-07-25 17:13:23 -07002533# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002534 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002535 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002536 my $space_before = $1;
2537 my $sign_off = $2;
2538 my $space_after = $3;
2539 my $email = $4;
2540 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2541
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002542 if ($sign_off !~ /$signature_tags/) {
2543 WARN("BAD_SIGN_OFF",
2544 "Non-standard signature: $sign_off\n" . $herecurr);
2545 }
Joe Perches20112472011-07-25 17:13:23 -07002546 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002547 if (WARN("BAD_SIGN_OFF",
2548 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2549 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002550 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002551 "$ucfirst_sign_off $email";
2552 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002553 }
Joe Perches20112472011-07-25 17:13:23 -07002554 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002555 if (WARN("BAD_SIGN_OFF",
2556 "'$ucfirst_sign_off' is the preferred signature form\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 }
2561
Joe Perches20112472011-07-25 17:13:23 -07002562 }
2563 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002564 if (WARN("BAD_SIGN_OFF",
2565 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2566 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002567 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002568 "$ucfirst_sign_off $email";
2569 }
Joe Perches20112472011-07-25 17:13:23 -07002570 }
2571
2572 my ($email_name, $email_address, $comment) = parse_email($email);
2573 my $suggested_email = format_email(($email_name, $email_address));
2574 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002575 ERROR("BAD_SIGN_OFF",
2576 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002577 } else {
2578 my $dequoted = $suggested_email;
2579 $dequoted =~ s/^"//;
2580 $dequoted =~ s/" </ </;
2581 # Don't force email to have quotes
2582 # Allow just an angle bracketed address
2583 if ("$dequoted$comment" ne $email &&
2584 "<$email_address>$comment" ne $email &&
2585 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002586 WARN("BAD_SIGN_OFF",
2587 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002588 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002589 }
Joe Perches7e51f192013-09-11 14:23:57 -07002590
2591# Check for duplicate signatures
2592 my $sig_nospace = $line;
2593 $sig_nospace =~ s/\s//g;
2594 $sig_nospace = lc($sig_nospace);
2595 if (defined $signatures{$sig_nospace}) {
2596 WARN("BAD_SIGN_OFF",
2597 "Duplicate signature\n" . $herecurr);
2598 } else {
2599 $signatures{$sig_nospace} = 1;
2600 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002601 }
2602
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002603# Check email subject for common tools that don't need to be mentioned
2604 if ($in_header_lines &&
2605 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2606 WARN("EMAIL_SUBJECT",
2607 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2608 }
2609
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002610# Check for unwanted Gerrit info
2611 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2612 ERROR("GERRIT_CHANGE_ID",
2613 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2614 }
2615
Joe Perches369c8dd2015-11-06 16:31:34 -08002616# Check if the commit log is in a possible stack dump
2617 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2618 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2619 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2620 # timestamp
2621 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2622 # stack dump address
2623 $commit_log_possible_stack_dump = 1;
2624 }
2625
Joe Perches2a076f42015-04-16 12:44:28 -07002626# Check for line lengths > 75 in commit log, warn once
2627 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002628 length($line) > 75 &&
2629 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2630 # file delta changes
2631 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2632 # filename then :
2633 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2634 # A Fixes: or Link: line
2635 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002636 WARN("COMMIT_LOG_LONG_LINE",
2637 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2638 $commit_log_long_line = 1;
2639 }
2640
Joe Perchesbf4daf12015-09-09 15:37:50 -07002641# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002642 if ($in_commit_log && $commit_log_possible_stack_dump &&
2643 $line =~ /^\s*$/) {
2644 $commit_log_possible_stack_dump = 0;
2645 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002646
Joe Perches0d7835f2015-02-13 14:38:35 -08002647# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002648 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002649 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002650 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002651 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002652 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002653 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2654 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002655 my $init_char = "c";
2656 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002657 my $short = 1;
2658 my $long = 0;
2659 my $case = 1;
2660 my $space = 1;
2661 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002662 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002663 my $id = '0123456789ab';
2664 my $orig_desc = "commit description";
2665 my $description = "";
2666
Joe Perchesfe043ea2015-09-09 15:37:25 -07002667 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2668 $init_char = $1;
2669 $orig_commit = lc($2);
2670 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2671 $orig_commit = lc($1);
2672 }
2673
Joe Perches0d7835f2015-02-13 14:38:35 -08002674 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2675 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2676 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2677 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2678 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2679 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002680 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002681 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2682 defined $rawlines[$linenr] &&
2683 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2684 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002685 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002686 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2687 defined $rawlines[$linenr] &&
2688 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2689 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2690 $orig_desc = $1;
2691 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2692 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002693 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002694 }
2695
2696 ($id, $description) = git_commit_info($orig_commit,
2697 $id, $orig_desc);
2698
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002699 if (defined($id) &&
2700 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002701 ERROR("GIT_COMMIT_ID",
2702 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2703 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002704 }
2705
Joe Perches13f19372014-08-06 16:10:59 -07002706# Check for added, moved or deleted files
2707 if (!$reported_maintainer_file && !$in_commit_log &&
2708 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2709 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2710 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2711 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002712 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002713 $reported_maintainer_file = 1;
2714 WARN("FILE_PATH_CHANGES",
2715 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2716 }
2717
Andy Whitcroft00df3442007-06-08 13:47:06 -07002718# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002719 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002720 ERROR("CORRUPTED_PATCH",
2721 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002722 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002723 }
2724
2725# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2726 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002727 $rawline !~ m/^$UTF8*$/) {
2728 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2729
2730 my $blank = copy_spacing($rawline);
2731 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2732 my $hereptr = "$hereline$ptr\n";
2733
Joe Perches34d99212011-07-25 17:13:26 -07002734 CHK("INVALID_UTF8",
2735 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002736 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002737
Joe Perches15662b32011-10-31 17:13:12 -07002738# Check if it's the start of a commit log
2739# (not a header line and we haven't seen the patch filename)
2740 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002741 !($rawline =~ /^\s+(?:\S|$)/ ||
2742 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002743 $in_header_lines = 0;
2744 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002745 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002746 }
2747
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002748# Check if there is UTF-8 in a commit log when a mail header has explicitly
2749# declined it, i.e defined some charset where it is missing.
2750 if ($in_header_lines &&
2751 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2752 $1 !~ /utf-8/i) {
2753 $non_utf8_charset = 1;
2754 }
2755
2756 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002757 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002758 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002759 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2760 }
2761
Joe Perchesd6430f72016-12-12 16:46:28 -08002762# Check for absolute kernel paths in commit message
2763 if ($tree && $in_commit_log) {
2764 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2765 my $file = $1;
2766
2767 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2768 check_absolute_file($1, $herecurr)) {
2769 #
2770 } else {
2771 check_absolute_file($file, $herecurr);
2772 }
2773 }
2774 }
2775
Kees Cook66b47b42014-10-13 15:51:57 -07002776# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002777 if (defined($misspellings) &&
2778 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002779 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002780 my $typo = $1;
2781 my $typo_fix = $spelling_fix{lc($typo)};
2782 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2783 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002784 my $msg_level = \&WARN;
2785 $msg_level = \&CHK if ($file);
2786 if (&{$msg_level}("TYPO_SPELLING",
2787 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002788 $fix) {
2789 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2790 }
2791 }
2792 }
2793
Andy Whitcroft306708542008-10-15 22:02:28 -07002794# ignore non-hunk lines and lines being removed
2795 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002796
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002797#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002798 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002799 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002800 if (ERROR("DOS_LINE_ENDINGS",
2801 "DOS line endings\n" . $herevet) &&
2802 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002803 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002804 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002805 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2806 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002807 if (ERROR("TRAILING_WHITESPACE",
2808 "trailing whitespace\n" . $herevet) &&
2809 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002810 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002811 }
2812
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002813 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002814 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002815
Josh Triplett4783f892013-11-12 15:10:12 -08002816# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002817 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002818 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002819 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2820 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002821 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002822 my $msg_level = \&ERROR;
2823 $msg_level = \&CHK if ($file);
2824 &{$msg_level}("FSF_MAILING_ADDRESS",
2825 "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 -08002826 }
2827
Andi Kleen33549572010-05-24 14:33:29 -07002828# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002829# Only applies when adding the entry originally, after that we do not have
2830# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002831 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002832 # 'choice' is usually the last thing on the line (though
2833 # Kconfig supports named choices), so use a word boundary
2834 # (\b) rather than a whitespace character (\s)
2835 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002836 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002837 my $cnt = $realcnt;
2838 my $ln = $linenr + 1;
2839 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002840 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002841 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002842 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002843 $f = $lines[$ln - 1];
2844 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2845 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002846
2847 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002848 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002849
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01002850 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002851 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01002852 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2853 if ($lines[$ln - 1] =~ "---help---") {
2854 WARN("CONFIG_DESCRIPTION",
2855 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2856 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002857 $length = -1;
2858 }
2859
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002860 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002861 $f =~ s/#.*//;
2862 $f =~ s/^\s+//;
2863 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01002864
2865 # This only checks context lines in the patch
2866 # and so hopefully shouldn't trigger false
2867 # positives, even though some of these are
2868 # common words in help texts
2869 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2870 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002871 $is_end = 1;
2872 last;
2873 }
Andi Kleen33549572010-05-24 14:33:29 -07002874 $length++;
2875 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002876 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2877 WARN("CONFIG_DESCRIPTION",
2878 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2879 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002880 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002881 }
2882
Joe Perches628f91a2017-07-10 15:52:07 -07002883# check for MAINTAINERS entries that don't have the right form
2884 if ($realfile =~ /^MAINTAINERS$/ &&
2885 $rawline =~ /^\+[A-Z]:/ &&
2886 $rawline !~ /^\+[A-Z]:\t\S/) {
2887 if (WARN("MAINTAINERS_STYLE",
2888 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2889 $fix) {
2890 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2891 }
2892 }
2893
Christoph Jaeger327953e2015-02-13 14:38:29 -08002894# discourage the use of boolean for type definition attributes of Kconfig options
2895 if ($realfile =~ /Kconfig/ &&
2896 $line =~ /^\+\s*\bboolean\b/) {
2897 WARN("CONFIG_TYPE_BOOLEAN",
2898 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2899 }
2900
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002901 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2902 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2903 my $flag = $1;
2904 my $replacement = {
2905 'EXTRA_AFLAGS' => 'asflags-y',
2906 'EXTRA_CFLAGS' => 'ccflags-y',
2907 'EXTRA_CPPFLAGS' => 'cppflags-y',
2908 'EXTRA_LDFLAGS' => 'ldflags-y',
2909 };
2910
2911 WARN("DEPRECATED_VARIABLE",
2912 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2913 }
2914
Rob Herringbff5da42014-01-23 15:54:51 -08002915# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002916 if (defined $root &&
2917 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2918 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2919
Rob Herringbff5da42014-01-23 15:54:51 -08002920 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2921
Florian Vaussardcc933192014-04-03 14:49:27 -07002922 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2923 my $vp_file = $dt_path . "vendor-prefixes.txt";
2924
Rob Herringbff5da42014-01-23 15:54:51 -08002925 foreach my $compat (@compats) {
2926 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002927 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2928 my $compat3 = $compat;
2929 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2930 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002931 if ( $? >> 8 ) {
2932 WARN("UNDOCUMENTED_DT_STRING",
2933 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2934 }
2935
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002936 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2937 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002938 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002939 if ( $? >> 8 ) {
2940 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002941 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002942 }
2943 }
2944 }
2945
Rob Herring9f3a8992018-04-10 16:33:13 -07002946# check for using SPDX license tag at beginning of files
2947 if ($realline == $checklicenseline) {
2948 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
2949 $checklicenseline = 2;
2950 } elsif ($rawline =~ /^\+/) {
2951 my $comment = "";
2952 if ($realfile =~ /\.(h|s|S)$/) {
2953 $comment = '/*';
2954 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
2955 $comment = '//';
2956 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
2957 $comment = '#';
2958 } elsif ($realfile =~ /\.rst$/) {
2959 $comment = '..';
2960 }
2961
2962 if ($comment !~ /^$/ &&
2963 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
2964 WARN("SPDX_LICENSE_TAG",
2965 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
2966 }
2967 }
2968 }
2969
Andy Whitcroft5368df202008-10-15 22:02:27 -07002970# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08002971 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002972
Joe Perches47e0c882015-06-25 15:02:57 -07002973# line length limit (with some exclusions)
2974#
2975# There are a few types of lines that may extend beyond $max_line_length:
2976# logging functions like pr_info that end in a string
2977# lines with a single string
2978# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08002979# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07002980#
2981# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07002982# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07002983# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2984# LONG_LINE all other lines longer than $max_line_length
2985#
2986# if LONG_LINE is ignored, the other 2 types are also ignored
2987#
2988
Joe Perchesb4749e92015-07-17 16:24:01 -07002989 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07002990 my $msg_type = "LONG_LINE";
2991
2992 # Check the allowed long line types first
2993
2994 # logging functions that end in a string that starts
2995 # before $max_line_length
2996 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2997 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2998 $msg_type = "";
2999
3000 # lines with only strings (w/ possible termination)
3001 # #defines with only strings
3002 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3003 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3004 $msg_type = "";
3005
Joe Perchescc147502017-11-17 15:28:44 -08003006 # More special cases
3007 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3008 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003009 $msg_type = "";
3010
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003011 # URL ($rawline is used in case the URL is in a comment)
3012 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3013 $msg_type = "";
3014
Joe Perches47e0c882015-06-25 15:02:57 -07003015 # Otherwise set the alternate message types
3016
3017 # a comment starts before $max_line_length
3018 } elsif ($line =~ /($;[\s$;]*)$/ &&
3019 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3020 $msg_type = "LONG_LINE_COMMENT"
3021
3022 # a quoted string starts before $max_line_length
3023 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3024 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3025 $msg_type = "LONG_LINE_STRING"
3026 }
3027
3028 if ($msg_type ne "" &&
3029 (show_type("LONG_LINE") || show_type($msg_type))) {
3030 WARN($msg_type,
3031 "line over $max_line_length characters\n" . $herecurr);
3032 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003033 }
3034
Andy Whitcroft8905a672007-11-28 16:21:06 -08003035# check for adding lines without a newline.
3036 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003037 WARN("MISSING_EOF_NEWLINE",
3038 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003039 }
3040
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003041# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003042 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003043
3044# at the beginning of a line any tabs must come first and anything
3045# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003046 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3047 $rawline =~ /^\+\s* \s*/) {
3048 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003049 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003050 if (ERROR("CODE_INDENT",
3051 "code indent should use tabs where possible\n" . $herevet) &&
3052 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003053 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003054 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003055 }
3056
Alberto Panizzo08e44362010-03-05 13:43:54 -08003057# check for space before tabs.
3058 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3059 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003060 if (WARN("SPACE_BEFORE_TAB",
3061 "please, no space before tabs\n" . $herevet) &&
3062 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003063 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003064 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003065 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003066 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003067 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003068 }
3069
Joe Perches6a487212018-04-10 16:34:04 -07003070# check for assignments on the start of a line
3071 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3072 CHK("ASSIGNMENT_CONTINUATIONS",
3073 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3074 }
3075
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003076# check for && or || at the start of a line
3077 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3078 CHK("LOGICAL_CONTINUATIONS",
3079 "Logical continuations should be on the previous line\n" . $hereprev);
3080 }
3081
Joe Perchesa91e8992016-05-20 17:04:05 -07003082# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003083 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003084 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003085 my $indent = length($1);
3086 if ($indent % 8) {
3087 if (WARN("TABSTOP",
3088 "Statements should start on a tabstop\n" . $herecurr) &&
3089 $fix) {
3090 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3091 }
3092 }
3093 }
3094
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003095# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003096 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003097 $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 -07003098 $prevline =~ /^\+(\t*)(.*)$/;
3099 my $oldindent = $1;
3100 my $rest = $2;
3101
3102 my $pos = pos_last_openparen($rest);
3103 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003104 $line =~ /^(\+| )([ \t]*)/;
3105 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003106
3107 my $goodtabindent = $oldindent .
3108 "\t" x ($pos / 8) .
3109 " " x ($pos % 8);
3110 my $goodspaceindent = $oldindent . " " x $pos;
3111
3112 if ($newindent ne $goodtabindent &&
3113 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003114
3115 if (CHK("PARENTHESIS_ALIGNMENT",
3116 "Alignment should match open parenthesis\n" . $hereprev) &&
3117 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003118 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003119 s/^\+[ \t]*/\+$goodtabindent/;
3120 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003121 }
3122 }
3123 }
3124
Joe Perches6ab3a972015-04-16 12:44:05 -07003125# check for space after cast like "(int) foo" or "(struct foo) bar"
3126# avoid checking a few false positives:
3127# "sizeof(<type>)" or "__alignof__(<type>)"
3128# function pointer declarations like "(*foo)(int) = bar;"
3129# structure definitions like "(struct foo) { 0 };"
3130# multiline macros that define functions
3131# known attributes or the __attribute__ keyword
3132 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3133 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003134 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003135 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003136 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003137 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003138 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003139 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003140 }
3141
Joe Perches86406b12015-09-09 15:37:41 -07003142# Block comment styles
3143# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003144 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003145 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003146 $rawline =~ /^\+[ \t]*\*/ &&
3147 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003148 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3149 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3150 }
3151
Joe Perches86406b12015-09-09 15:37:41 -07003152# Block comments use * on subsequent lines
3153 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3154 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003155 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003156 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003157 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003158 WARN("BLOCK_COMMENT_STYLE",
3159 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003160 }
3161
Joe Perches86406b12015-09-09 15:37:41 -07003162# Block comments use */ on trailing lines
3163 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003164 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3165 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3166 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003167 WARN("BLOCK_COMMENT_STYLE",
3168 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003169 }
3170
Joe Perches08eb9b82016-10-11 13:51:50 -07003171# Block comment * alignment
3172 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003173 $line =~ /^\+[ \t]*$;/ && #leading comment
3174 $rawline =~ /^\+[ \t]*\*/ && #leading *
3175 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003176 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003177 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3178 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003179 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003180 if (defined($1)) {
3181 $oldindent = expand_tabs($1);
3182 } else {
3183 $prevrawline =~ m@^\+(.*/?)\*@;
3184 $oldindent = expand_tabs($1);
3185 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003186 $rawline =~ m@^\+([ \t]*)\*@;
3187 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003188 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003189 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003190 WARN("BLOCK_COMMENT_STYLE",
3191 "Block comments should align the * on each line\n" . $hereprev);
3192 }
3193 }
3194
Joe Perches7f619192014-08-06 16:10:39 -07003195# check for missing blank lines after struct/union declarations
3196# with exceptions for various attributes and macros
3197 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3198 $line =~ /^\+/ &&
3199 !($line =~ /^\+\s*$/ ||
3200 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3201 $line =~ /^\+\s*MODULE_/i ||
3202 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3203 $line =~ /^\+[a-z_]*init/ ||
3204 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3205 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003206 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003207 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003208 if (CHK("LINE_SPACING",
3209 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3210 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003211 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003212 }
Joe Perches7f619192014-08-06 16:10:39 -07003213 }
3214
Joe Perches365dd4e2014-08-06 16:10:42 -07003215# check for multiple consecutive blank lines
3216 if ($prevline =~ /^[\+ ]\s*$/ &&
3217 $line =~ /^\+\s*$/ &&
3218 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003219 if (CHK("LINE_SPACING",
3220 "Please don't use multiple blank lines\n" . $hereprev) &&
3221 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003222 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003223 }
3224
Joe Perches365dd4e2014-08-06 16:10:42 -07003225 $last_blank_line = $linenr;
3226 }
3227
Joe Perches3b617e32014-04-03 14:49:28 -07003228# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003229 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3230 # actual declarations
3231 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003232 # function pointer declarations
3233 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003234 # foo bar; where foo is some local typedef or #define
3235 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3236 # known declaration macros
3237 $prevline =~ /^\+\s+$declaration_macros/) &&
3238 # for "else if" which can look like "$Ident $Ident"
3239 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3240 # other possible extensions of declaration lines
3241 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3242 # not starting a section or a macro "\" extended line
3243 $prevline =~ /(?:\{\s*|\\)$/) &&
3244 # looks like a declaration
3245 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003246 # function pointer declarations
3247 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003248 # foo bar; where foo is some local typedef or #define
3249 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3250 # known declaration macros
3251 $sline =~ /^\+\s+$declaration_macros/ ||
3252 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003253 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003254 # start or end of block or continuation of declaration
3255 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3256 # bitfield continuation
3257 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3258 # other possible extensions of declaration lines
3259 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3260 # indentation of previous and current line are the same
3261 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003262 if (WARN("LINE_SPACING",
3263 "Missing a blank line after declarations\n" . $hereprev) &&
3264 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003265 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003266 }
Joe Perches3b617e32014-04-03 14:49:28 -07003267 }
3268
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003269# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003270# Exceptions:
3271# 1) within comments
3272# 2) indented preprocessor commands
3273# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003274 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003275 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003276 if (WARN("LEADING_SPACE",
3277 "please, no spaces at the start of a line\n" . $herevet) &&
3278 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003279 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003280 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003281 }
3282
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003283# check we are in a valid C source file if not then ignore this hunk
3284 next if ($realfile !~ /\.(h|c)$/);
3285
Joe Perches5751a242017-11-17 15:28:52 -08003286# check for unusual line ending [ or (
3287 if ($line =~ /^\+.*([\[\(])\s*$/) {
3288 CHK("OPEN_ENDED_LINE",
3289 "Lines should not end with a '$1'\n" . $herecurr);
3290 }
3291
Joe Perches4dbed762017-05-08 15:55:39 -07003292# check if this appears to be the start function declaration, save the name
3293 if ($sline =~ /^\+\{\s*$/ &&
3294 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3295 $context_function = $1;
3296 }
3297
3298# check if this appears to be the end of function declaration
3299 if ($sline =~ /^\+\}\s*$/) {
3300 undef $context_function;
3301 }
3302
Joe Perches032a4c02014-08-06 16:10:29 -07003303# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003304# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003305# if the previous line is a break or return and is indented 1 tab more...
3306 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3307 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003308 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3309 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3310 defined $lines[$linenr] &&
3311 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003312 WARN("UNNECESSARY_ELSE",
3313 "else is not generally useful after a break or return\n" . $hereprev);
3314 }
3315 }
3316
Joe Perchesc00df192014-08-06 16:11:01 -07003317# check indentation of a line with a break;
3318# if the previous line is a goto or return and is indented the same # of tabs
3319 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3320 my $tabs = $1;
3321 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3322 WARN("UNNECESSARY_BREAK",
3323 "break is not useful after a goto or return\n" . $hereprev);
3324 }
3325 }
3326
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003327# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003328 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003329 WARN("CVS_KEYWORD",
3330 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003331 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003332
Joe Perches56e77d72013-02-21 16:44:14 -08003333# check for old HOTPLUG __dev<foo> section markings
3334 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3335 WARN("HOTPLUG_SECTION",
3336 "Using $1 is unnecessary\n" . $herecurr);
3337 }
3338
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003339# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003340 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3341 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003342#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003343 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003344 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003345 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003346 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003347 $stat =~ s/\n./\n /g;
3348 $cond =~ s/\n./\n /g;
3349
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003350#print "linenr<$linenr> <$stat>\n";
3351 # If this statement has no statement boundaries within
3352 # it there is no point in retrying a statement scan
3353 # until we hit end of it.
3354 my $frag = $stat; $frag =~ s/;+\s*$//;
3355 if ($frag !~ /(?:{|;)/) {
3356#print "skip<$line_nr_next>\n";
3357 $suppress_statement = $line_nr_next;
3358 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003359
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003360 # Find the real next line.
3361 $realline_next = $line_nr_next;
3362 if (defined $realline_next &&
3363 (!defined $lines[$realline_next - 1] ||
3364 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3365 $realline_next++;
3366 }
3367
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003368 my $s = $stat;
3369 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003370
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003371 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003372 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003373
3374 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003375 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003376
Andy Whitcroft463f2862009-09-21 17:04:34 -07003377 } elsif ($s =~ /^.\s*else\b/s) {
3378
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003379 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003380 } 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 -07003381 my $type = $1;
3382 $type =~ s/\s+/ /g;
3383 possible($type, "A:" . $s);
3384
Andy Whitcroft8905a672007-11-28 16:21:06 -08003385 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003386 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003387 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003388 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003389
3390 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003391 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003392 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003393 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003394
3395 # Check for any sort of function declaration.
3396 # int foo(something bar, other baz);
3397 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003398 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 -08003399 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003400
Andy Whitcroftcf655042008-03-04 14:28:20 -08003401 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003402 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003403 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003404
Andy Whitcroft8905a672007-11-28 16:21:06 -08003405 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003406 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003407
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003408 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003409 }
3410 }
3411 }
3412
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003413 }
3414
Andy Whitcroft653d4872007-06-23 17:16:34 -07003415#
3416# Checks which may be anchored in the context.
3417#
3418
3419# Check for switch () and associated case and default
3420# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003421 if ($line=~/\bswitch\s*\(.*\)/) {
3422 my $err = '';
3423 my $sep = '';
3424 my @ctx = ctx_block_outer($linenr, $realcnt);
3425 shift(@ctx);
3426 for my $ctx (@ctx) {
3427 my ($clen, $cindent) = line_stats($ctx);
3428 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3429 $indent != $cindent) {
3430 $err .= "$sep$ctx\n";
3431 $sep = '';
3432 } else {
3433 $sep = "[...]\n";
3434 }
3435 }
3436 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003437 ERROR("SWITCH_CASE_INDENT_LEVEL",
3438 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003439 }
3440 }
3441
3442# if/while/etc brace do not go on next line, unless defining a do while loop,
3443# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003444 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 -07003445 my $pre_ctx = "$1$2";
3446
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003447 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003448
3449 if ($line =~ /^\+\t{6,}/) {
3450 WARN("DEEP_INDENTATION",
3451 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3452 }
3453
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003454 my $ctx_cnt = $realcnt - $#ctx - 1;
3455 my $ctx = join("\n", @ctx);
3456
Andy Whitcroft548596d2008-07-23 21:29:01 -07003457 my $ctx_ln = $linenr;
3458 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003459
Andy Whitcroft548596d2008-07-23 21:29:01 -07003460 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3461 defined $lines[$ctx_ln - 1] &&
3462 $lines[$ctx_ln - 1] =~ /^-/)) {
3463 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3464 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003465 $ctx_ln++;
3466 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003467
Andy Whitcroft53210162008-07-23 21:29:03 -07003468 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3469 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003470
Joe Perchesd752fcc2014-08-06 16:11:05 -07003471 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003472 ERROR("OPEN_BRACE",
3473 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003474 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003475 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003476 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3477 $ctx =~ /\)\s*\;\s*$/ &&
3478 defined $lines[$ctx_ln - 1])
3479 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003480 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3481 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003482 WARN("TRAILING_SEMICOLON",
3483 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003484 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003485 }
3486 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003487 }
3488
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003489# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003490 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 -08003491 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3492 ctx_statement_block($linenr, $realcnt, 0)
3493 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003494 my ($s, $c) = ($stat, $cond);
3495
3496 substr($s, 0, length($c), '');
3497
Joe Perches9f5af482015-09-09 15:37:30 -07003498 # remove inline comments
3499 $s =~ s/$;/ /g;
3500 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003501
3502 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003503 my @newlines = ($c =~ /\n/gs);
3504 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003505
Joe Perches9f5af482015-09-09 15:37:30 -07003506 # Make sure we remove the line prefixes as we have
3507 # none on the first line, and are going to readd them
3508 # where necessary.
3509 $s =~ s/\n./\n/gs;
3510 while ($s =~ /\n\s+\\\n/) {
3511 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3512 }
3513
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003514 # We want to check the first line inside the block
3515 # starting at the end of the conditional, so remove:
3516 # 1) any blank line termination
3517 # 2) any opening brace { on end of the line
3518 # 3) any do (...) {
3519 my $continuation = 0;
3520 my $check = 0;
3521 $s =~ s/^.*\bdo\b//;
3522 $s =~ s/^\s*{//;
3523 if ($s =~ s/^\s*\\//) {
3524 $continuation = 1;
3525 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003526 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003527 $check = 1;
3528 $cond_lines++;
3529 }
3530
3531 # Also ignore a loop construct at the end of a
3532 # preprocessor statement.
3533 if (($prevline =~ /^.\s*#\s*define\s/ ||
3534 $prevline =~ /\\\s*$/) && $continuation == 0) {
3535 $check = 0;
3536 }
3537
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003538 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003539 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003540 while ($cond_ptr != $cond_lines) {
3541 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003542
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003543 # If we see an #else/#elif then the code
3544 # is not linear.
3545 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3546 $check = 0;
3547 }
3548
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003549 # Ignore:
3550 # 1) blank lines, they should be at 0,
3551 # 2) preprocessor lines, and
3552 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003553 if ($continuation ||
3554 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003555 $s =~ /^\s*#\s*?/ ||
3556 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003557 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003558 if ($s =~ s/^.*?\n//) {
3559 $cond_lines++;
3560 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003561 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003562 }
3563
3564 my (undef, $sindent) = line_stats("+" . $s);
3565 my $stat_real = raw_line($linenr, $cond_lines);
3566
3567 # Check if either of these lines are modified, else
3568 # this is not this patch's fault.
3569 if (!defined($stat_real) ||
3570 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3571 $check = 0;
3572 }
3573 if (defined($stat_real) && $cond_lines > 1) {
3574 $stat_real = "[...]\n$stat_real";
3575 }
3576
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003577 #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 -07003578
Joe Perches9f5af482015-09-09 15:37:30 -07003579 if ($check && $s ne '' &&
3580 (($sindent % 8) != 0 ||
3581 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003582 ($sindent == $indent &&
3583 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003584 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003585 WARN("SUSPECT_CODE_INDENT",
3586 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003587 }
3588 }
3589
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003590 # Track the 'values' across context and added lines.
3591 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003592 my ($curr_values, $curr_vars) =
3593 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003594 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003595 if ($dbg_values) {
3596 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003597 print "$linenr > .$outline\n";
3598 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003599 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003600 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003601 $prev_values = substr($curr_values, -1);
3602
Andy Whitcroft00df3442007-06-08 13:47:06 -07003603#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003604 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003605
Joe Perches11ca40a2016-12-12 16:46:31 -08003606# check for dereferences that span multiple lines
3607 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3608 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3609 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3610 my $ref = $1;
3611 $line =~ /^.\s*($Lval)/;
3612 $ref .= $1;
3613 $ref =~ s/\s//g;
3614 WARN("MULTILINE_DEREFERENCE",
3615 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3616 }
3617
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003618# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003619 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 -07003620 my $type = $1;
3621 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003622 $var = "" if (!defined $var);
3623 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003624 my $sign = $1;
3625 my $pointer = $2;
3626
3627 $pointer = "" if (!defined $pointer);
3628
3629 if (WARN("UNSPECIFIED_INT",
3630 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3631 $fix) {
3632 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003633 my $comp_pointer = $pointer;
3634 $comp_pointer =~ s/\s//g;
3635 $decl .= $comp_pointer;
3636 $decl = rtrim($decl) if ($var eq "");
3637 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003638 }
3639 }
3640 }
3641
Andy Whitcroft653d4872007-06-23 17:16:34 -07003642# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003643 if ($dbg_type) {
3644 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003645 ERROR("TEST_TYPE",
3646 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003647 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003648 ERROR("TEST_NOT_TYPE",
3649 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003650 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003651 next;
3652 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003653# TEST: allow direct testing of the attribute matcher.
3654 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003655 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003656 ERROR("TEST_ATTR",
3657 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003658 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003659 ERROR("TEST_NOT_ATTR",
3660 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003661 }
3662 next;
3663 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003664
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003665# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003666 if ($line =~ /^.\s*{/ &&
3667 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003668 if (ERROR("OPEN_BRACE",
3669 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003670 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3671 fix_delete_line($fixlinenr - 1, $prevrawline);
3672 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003673 my $fixedline = $prevrawline;
3674 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003675 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003676 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003677 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003678 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003679 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003680 }
3681
Andy Whitcroft653d4872007-06-23 17:16:34 -07003682#
3683# Checks which are anchored on the added line.
3684#
3685
3686# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003687 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003688 my $path = $1;
3689 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003690 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003691 "malformed #include filename\n" . $herecurr);
3692 }
3693 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3694 ERROR("UAPI_INCLUDE",
3695 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003696 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003697 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003698
3699# no C99 // comments
3700 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003701 if (ERROR("C99_COMMENTS",
3702 "do not use C99 // comments\n" . $herecurr) &&
3703 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003704 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003705 if ($line =~ /\/\/(.*)$/) {
3706 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003707 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003708 }
3709 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003710 }
3711 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003712 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003713 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003714
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003715# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3716# the whole statement.
3717#print "APW <$lines[$realline_next - 1]>\n";
3718 if (defined $realline_next &&
3719 exists $lines[$realline_next - 1] &&
3720 !defined $suppress_export{$realline_next} &&
3721 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3722 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003723 # Handle definitions which produce identifiers with
3724 # a prefix:
3725 # XXX(foo);
3726 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003727 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003728 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003729 $name =~ /^${Ident}_$2/) {
3730#print "FOO C name<$name>\n";
3731 $suppress_export{$realline_next} = 1;
3732
3733 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003734 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003735 ^.DEFINE_$Ident\(\Q$name\E\)|
3736 ^.DECLARE_$Ident\(\Q$name\E\)|
3737 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003738 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3739 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003740 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003741#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3742 $suppress_export{$realline_next} = 2;
3743 } else {
3744 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003745 }
3746 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003747 if (!defined $suppress_export{$linenr} &&
3748 $prevline =~ /^.\s*$/ &&
3749 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3750 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3751#print "FOO B <$lines[$linenr - 1]>\n";
3752 $suppress_export{$linenr} = 2;
3753 }
3754 if (defined $suppress_export{$linenr} &&
3755 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003756 WARN("EXPORT_SYMBOL",
3757 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003758 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003759
Joe Eloff5150bda2010-08-09 17:21:00 -07003760# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003761 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003762 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003763 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003764 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003765 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003766 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003767 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003768# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003769 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003770 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003771 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003772 $herecurr) &&
3773 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003774 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003775 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003776 }
3777
Joe Perches18130872014-08-06 16:11:22 -07003778# check for misordered declarations of char/short/int/long with signed/unsigned
3779 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3780 my $tmp = trim($1);
3781 WARN("MISORDERED_TYPE",
3782 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3783 }
3784
Joe Perchescb710ec2010-10-26 14:23:20 -07003785# check for static const char * arrays.
3786 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003787 WARN("STATIC_CONST_CHAR_ARRAY",
3788 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003789 $herecurr);
3790 }
3791
3792# check for static char foo[] = "bar" declarations.
3793 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003794 WARN("STATIC_CONST_CHAR_ARRAY",
3795 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003796 $herecurr);
3797 }
3798
Joe Perchesab7e23f2015-04-16 12:44:22 -07003799# check for const <foo> const where <foo> is not a pointer or array type
3800 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3801 my $found = $1;
3802 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3803 WARN("CONST_CONST",
3804 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3805 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3806 WARN("CONST_CONST",
3807 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3808 }
3809 }
3810
Joe Perches9b0fa602014-04-03 14:49:18 -07003811# check for non-global char *foo[] = {"bar", ...} declarations.
3812 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3813 WARN("STATIC_CONST_CHAR_ARRAY",
3814 "char * array declaration might be better as static const\n" .
3815 $herecurr);
3816 }
3817
Joe Perchesb598b672015-04-16 12:44:36 -07003818# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3819 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3820 my $array = $1;
3821 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3822 my $array_div = $1;
3823 if (WARN("ARRAY_SIZE",
3824 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3825 $fix) {
3826 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3827 }
3828 }
3829 }
3830
Joe Perchesb36190c2014-01-27 17:07:18 -08003831# check for function declarations without arguments like "int foo()"
3832 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3833 if (ERROR("FUNCTION_WITHOUT_ARGS",
3834 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3835 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003836 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003837 }
3838 }
3839
Andy Whitcroft653d4872007-06-23 17:16:34 -07003840# check for new typedefs, only function parameters and sparse annotations
3841# make sense.
3842 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003843 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003844 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003845 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003846 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003847 WARN("NEW_TYPEDEFS",
3848 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003849 }
3850
3851# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003852 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003853 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3854 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003855 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003856
Andy Whitcroft65863862009-01-06 14:41:21 -08003857 # Should start with a space.
3858 $to =~ s/^(\S)/ $1/;
3859 # Should not end with a space.
3860 $to =~ s/\s+$//;
3861 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003862 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003863 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003864
Joe Perches3705ce52013-07-03 15:05:31 -07003865## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003866 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003867 if (ERROR("POINTER_LOCATION",
3868 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3869 $fix) {
3870 my $sub_from = $ident;
3871 my $sub_to = $ident;
3872 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003873 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003874 s@\Q$sub_from\E@$sub_to@;
3875 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003876 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003877 }
3878 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3879 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003880 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003881
Andy Whitcroft65863862009-01-06 14:41:21 -08003882 # Should start with a space.
3883 $to =~ s/^(\S)/ $1/;
3884 # Should not end with a space.
3885 $to =~ s/\s+$//;
3886 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003887 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003888 }
3889 # Modifiers should have spaces.
3890 $to =~ s/(\b$Modifier$)/$1 /;
3891
Joe Perches3705ce52013-07-03 15:05:31 -07003892## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003893 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003894 if (ERROR("POINTER_LOCATION",
3895 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3896 $fix) {
3897
3898 my $sub_from = $match;
3899 my $sub_to = $match;
3900 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003901 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003902 s@\Q$sub_from\E@$sub_to@;
3903 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003904 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003905 }
3906
Joe Perches9d3e3c72015-09-09 15:37:27 -07003907# avoid BUG() or BUG_ON()
3908 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07003909 my $msg_level = \&WARN;
3910 $msg_level = \&CHK if ($file);
3911 &{$msg_level}("AVOID_BUG",
3912 "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 -07003913 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003914
Joe Perches9d3e3c72015-09-09 15:37:27 -07003915# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003916 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003917 WARN("LINUX_VERSION_CODE",
3918 "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 -08003919 }
3920
Joe Perches17441222011-06-15 15:08:17 -07003921# check for uses of printk_ratelimit
3922 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003923 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003924 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003925 }
3926
Joe Percheseeef5732017-11-17 15:28:41 -08003927# printk should use KERN_* levels
3928 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
3929 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3930 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003931 }
3932
Joe Perches243f3802012-05-31 16:26:09 -07003933 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3934 my $orig = $1;
3935 my $level = lc($orig);
3936 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003937 my $level2 = $level;
3938 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003939 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003940 "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 -07003941 }
3942
3943 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003944 if (WARN("PREFER_PR_LEVEL",
3945 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3946 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003947 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003948 s/\bpr_warning\b/pr_warn/;
3949 }
Joe Perches243f3802012-05-31 16:26:09 -07003950 }
3951
Joe Perchesdc139312013-02-21 16:44:13 -08003952 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3953 my $orig = $1;
3954 my $level = lc($orig);
3955 $level = "warn" if ($level eq "warning");
3956 $level = "dbg" if ($level eq "debug");
3957 WARN("PREFER_DEV_LEVEL",
3958 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3959 }
3960
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003961# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3962# number of false positives, but assembly files are not checked, so at
3963# least the arch entry code will not trigger this warning.
3964 if ($line =~ /\bENOSYS\b/) {
3965 WARN("ENOSYS",
3966 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3967 }
3968
Andy Whitcroft653d4872007-06-23 17:16:34 -07003969# function brace can't be on same line, except for #defines of do while,
3970# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07003971 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08003972 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
3973 $sline !~ /\#\s*define\b.*do\s*\{/ &&
3974 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07003975 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08003976 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07003977 $fix) {
3978 fix_delete_line($fixlinenr, $rawline);
3979 my $fixed_line = $rawline;
3980 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3981 my $line1 = $1;
3982 my $line2 = $2;
3983 fix_insert_line($fixlinenr, ltrim($line1));
3984 fix_insert_line($fixlinenr, "\+{");
3985 if ($line2 !~ /^\s*$/) {
3986 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3987 }
3988 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003989 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003990
Andy Whitcroft8905a672007-11-28 16:21:06 -08003991# open braces for enum, union and struct go on the same line.
3992 if ($line =~ /^.\s*{/ &&
3993 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003994 if (ERROR("OPEN_BRACE",
3995 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3996 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3997 fix_delete_line($fixlinenr - 1, $prevrawline);
3998 fix_delete_line($fixlinenr, $rawline);
3999 my $fixedline = rtrim($prevrawline) . " {";
4000 fix_insert_line($fixlinenr, $fixedline);
4001 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004002 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004003 if ($fixedline !~ /^\+\s*$/) {
4004 fix_insert_line($fixlinenr, $fixedline);
4005 }
4006 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004007 }
4008
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004009# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004010 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4011 if (WARN("SPACING",
4012 "missing space after $1 definition\n" . $herecurr) &&
4013 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004014 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004015 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4016 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004017 }
4018
Joe Perches31070b52014-01-23 15:54:49 -08004019# Function pointer declarations
4020# check spacing between type, funcptr, and args
4021# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004022 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004023 my $declare = $1;
4024 my $pre_pointer_space = $2;
4025 my $post_pointer_space = $3;
4026 my $funcname = $4;
4027 my $post_funcname_space = $5;
4028 my $pre_args_space = $6;
4029
Joe Perches91f72e92014-04-03 14:49:12 -07004030# the $Declare variable will capture all spaces after the type
4031# so check it for a missing trailing missing space but pointer return types
4032# don't need a space so don't warn for those.
4033 my $post_declare_space = "";
4034 if ($declare =~ /(\s+)$/) {
4035 $post_declare_space = $1;
4036 $declare = rtrim($declare);
4037 }
4038 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004039 WARN("SPACING",
4040 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004041 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004042 }
4043
4044# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004045# This test is not currently implemented because these declarations are
4046# equivalent to
4047# int foo(int bar, ...)
4048# and this is form shouldn't/doesn't generate a checkpatch warning.
4049#
4050# elsif ($declare =~ /\s{2,}$/) {
4051# WARN("SPACING",
4052# "Multiple spaces after return type\n" . $herecurr);
4053# }
Joe Perches31070b52014-01-23 15:54:49 -08004054
4055# unnecessary space "type ( *funcptr)(args...)"
4056 if (defined $pre_pointer_space &&
4057 $pre_pointer_space =~ /^\s/) {
4058 WARN("SPACING",
4059 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4060 }
4061
4062# unnecessary space "type (* funcptr)(args...)"
4063 if (defined $post_pointer_space &&
4064 $post_pointer_space =~ /^\s/) {
4065 WARN("SPACING",
4066 "Unnecessary space before function pointer name\n" . $herecurr);
4067 }
4068
4069# unnecessary space "type (*funcptr )(args...)"
4070 if (defined $post_funcname_space &&
4071 $post_funcname_space =~ /^\s/) {
4072 WARN("SPACING",
4073 "Unnecessary space after function pointer name\n" . $herecurr);
4074 }
4075
4076# unnecessary space "type (*funcptr) (args...)"
4077 if (defined $pre_args_space &&
4078 $pre_args_space =~ /^\s/) {
4079 WARN("SPACING",
4080 "Unnecessary space before function pointer arguments\n" . $herecurr);
4081 }
4082
4083 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004084 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004085 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004086 }
4087 }
4088
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004089# check for spacing round square brackets; allowed:
4090# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004091# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4092# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004093 while ($line =~ /(.*?\s)\[/g) {
4094 my ($where, $prefix) = ($-[1], $1);
4095 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004096 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004097 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004098 if (ERROR("BRACKET_SPACE",
4099 "space prohibited before open square bracket '['\n" . $herecurr) &&
4100 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004101 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004102 s/^(\+.*?)\s+\[/$1\[/;
4103 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004104 }
4105 }
4106
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004107# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004108 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004109 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004110 my $ctx_before = substr($line, 0, $-[1]);
4111 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004112
4113 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004114 if ($name =~ /^(?:
4115 if|for|while|switch|return|case|
4116 volatile|__volatile__|
4117 __attribute__|format|__extension__|
4118 asm|__asm__)$/x)
4119 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004120 # cpp #define statements have non-optional spaces, ie
4121 # if there is a space between the name and the open
4122 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004123 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004124
4125 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004126 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004127
4128 # If this whole things ends with a type its most
4129 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004130 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004131
4132 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004133 if (WARN("SPACING",
4134 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4135 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004136 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004137 s/\b$name\s+\(/$name\(/;
4138 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004139 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004140 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004141
Andy Whitcroft653d4872007-06-23 17:16:34 -07004142# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004143 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004144 my $fixed_line = "";
4145 my $line_fixed = 0;
4146
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004147 my $ops = qr{
4148 <<=|>>=|<=|>=|==|!=|
4149 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4150 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004151 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004152 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004153 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004154 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004155
4156## print("element count: <" . $#elements . ">\n");
4157## foreach my $el (@elements) {
4158## print("el: <$el>\n");
4159## }
4160
4161 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004162 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004163
Joe Perches3705ce52013-07-03 15:05:31 -07004164 foreach my $el (@elements) {
4165 push(@fix_elements, substr($rawline, $off, length($el)));
4166 $off += length($el);
4167 }
4168
4169 $off = 0;
4170
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004171 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004172 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004173
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004174 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004175
4176 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4177
4178## print("n: <$n> good: <$good>\n");
4179
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004180 $off += length($elements[$n]);
4181
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004182 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004183 my $ca = substr($opline, 0, $off);
4184 my $cc = '';
4185 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4186 $cc = substr($opline, $off + length($elements[$n + 1]));
4187 }
4188 my $cb = "$ca$;$cc";
4189
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004190 my $a = '';
4191 $a = 'V' if ($elements[$n] ne '');
4192 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004193 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004194 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4195 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004196 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004197
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004198 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004199
4200 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004201 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004202 $c = 'V' if ($elements[$n + 2] ne '');
4203 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004204 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004205 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4206 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004207 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004208 } else {
4209 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004210 }
4211
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004212 my $ctx = "${a}x${c}";
4213
4214 my $at = "(ctx:$ctx)";
4215
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004216 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004217 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004218
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004219 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004220 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004221
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004222 # Get the full operator variant.
4223 my $opv = $op . substr($curr_vars, $off, 1);
4224
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004225 # Ignore operators passed as parameters.
4226 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004227 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004228
Andy Whitcroftcf655042008-03-04 14:28:20 -08004229# # Ignore comments
4230# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004231
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004232 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004233 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004234 if ($ctx !~ /.x[WEBC]/ &&
4235 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004236 if (ERROR("SPACING",
4237 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004238 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004239 $line_fixed = 1;
4240 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004241 }
4242
4243 # // is a comment
4244 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004245
Joe Perchesb00e4812014-04-03 14:49:33 -07004246 # : when part of a bitfield
4247 } elsif ($opv eq ':B') {
4248 # skip the bitfield test for now
4249
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004250 # No spaces for:
4251 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004252 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004253 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004254 if (ERROR("SPACING",
4255 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004256 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004257 if (defined $fix_elements[$n + 2]) {
4258 $fix_elements[$n + 2] =~ s/^\s+//;
4259 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004260 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004261 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004262 }
4263
Joe Perches23810972014-12-10 15:51:32 -08004264 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004265 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004266 my $rtrim_before = 0;
4267 my $space_after = 0;
4268 if ($ctx =~ /Wx./) {
4269 if (ERROR("SPACING",
4270 "space prohibited before that '$op' $at\n" . $hereptr)) {
4271 $line_fixed = 1;
4272 $rtrim_before = 1;
4273 }
4274 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004275 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004276 if (ERROR("SPACING",
4277 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004278 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004279 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004280 $space_after = 1;
4281 }
4282 }
4283 if ($rtrim_before || $space_after) {
4284 if ($rtrim_before) {
4285 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4286 } else {
4287 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4288 }
4289 if ($space_after) {
4290 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004291 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004292 }
4293
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004294 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004295 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004296 #warn "'*' is part of type\n";
4297
4298 # unary operators should have a space before and
4299 # none after. May be left adjacent to another
4300 # unary operator, or a cast
4301 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004302 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004303 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004304 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004305 if (ERROR("SPACING",
4306 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004307 if ($n != $last_after + 2) {
4308 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4309 $line_fixed = 1;
4310 }
Joe Perches3705ce52013-07-03 15:05:31 -07004311 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004312 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004313 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004314 # A unary '*' may be const
4315
4316 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004317 if (ERROR("SPACING",
4318 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004319 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004320 if (defined $fix_elements[$n + 2]) {
4321 $fix_elements[$n + 2] =~ s/^\s+//;
4322 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004323 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004324 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004325 }
4326
4327 # unary ++ and unary -- are allowed no space on one side.
4328 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004329 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004330 if (ERROR("SPACING",
4331 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004332 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004333 $line_fixed = 1;
4334 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004335 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004336 if ($ctx =~ /Wx[BE]/ ||
4337 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004338 if (ERROR("SPACING",
4339 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004340 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004341 $line_fixed = 1;
4342 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004343 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004344 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004345 if (ERROR("SPACING",
4346 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004347 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004348 if (defined $fix_elements[$n + 2]) {
4349 $fix_elements[$n + 2] =~ s/^\s+//;
4350 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004351 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004352 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004353 }
4354
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004355 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004356 } elsif ($op eq '<<' or $op eq '>>' or
4357 $op eq '&' or $op eq '^' or $op eq '|' or
4358 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004359 $op eq '*' or $op eq '/' or
4360 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004361 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004362 if ($check) {
4363 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4364 if (CHK("SPACING",
4365 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4366 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4367 $fix_elements[$n + 2] =~ s/^\s+//;
4368 $line_fixed = 1;
4369 }
4370 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4371 if (CHK("SPACING",
4372 "space preferred before that '$op' $at\n" . $hereptr)) {
4373 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4374 $line_fixed = 1;
4375 }
4376 }
4377 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004378 if (ERROR("SPACING",
4379 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004380 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4381 if (defined $fix_elements[$n + 2]) {
4382 $fix_elements[$n + 2] =~ s/^\s+//;
4383 }
Joe Perches3705ce52013-07-03 15:05:31 -07004384 $line_fixed = 1;
4385 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004386 }
4387
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004388 # A colon needs no spaces before when it is
4389 # terminating a case value or a label.
4390 } elsif ($opv eq ':C' || $opv eq ':L') {
4391 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004392 if (ERROR("SPACING",
4393 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004394 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004395 $line_fixed = 1;
4396 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004397 }
4398
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004399 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004400 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004401 my $ok = 0;
4402
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004403 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004404 if (($op eq '<' &&
4405 $cc =~ /^\S+\@\S+>/) ||
4406 ($op eq '>' &&
4407 $ca =~ /<\S+\@\S+$/))
4408 {
4409 $ok = 1;
4410 }
4411
Joe Perchese0df7e12015-04-16 12:44:53 -07004412 # for asm volatile statements
4413 # ignore a colon with another
4414 # colon immediately before or after
4415 if (($op eq ':') &&
4416 ($ca =~ /:$/ || $cc =~ /^:/)) {
4417 $ok = 1;
4418 }
4419
Joe Perches84731622013-11-12 15:10:05 -08004420 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004421 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004422 my $msg_level = \&ERROR;
4423 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004424
Jean Delvare0675a8f2017-09-08 16:16:07 -07004425 if (&{$msg_level}("SPACING",
4426 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004427 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4428 if (defined $fix_elements[$n + 2]) {
4429 $fix_elements[$n + 2] =~ s/^\s+//;
4430 }
Joe Perches3705ce52013-07-03 15:05:31 -07004431 $line_fixed = 1;
4432 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004433 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004434 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004435 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004436
4437## print("n: <$n> GOOD: <$good>\n");
4438
4439 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004440 }
Joe Perches3705ce52013-07-03 15:05:31 -07004441
4442 if (($#elements % 2) == 0) {
4443 $fixed_line = $fixed_line . $fix_elements[$#elements];
4444 }
4445
Joe Perches194f66f2014-08-06 16:11:03 -07004446 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4447 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004448 }
4449
4450
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004451 }
4452
Joe Perches786b6322013-07-03 15:05:32 -07004453# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004454 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004455 if (WARN("SPACING",
4456 "space prohibited before semicolon\n" . $herecurr) &&
4457 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004458 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004459 s/^(\+.*\S)\s+;/$1;/;
4460 }
4461 }
4462
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004463# check for multiple assignments
4464 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004465 CHK("MULTIPLE_ASSIGNMENTS",
4466 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004467 }
4468
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004469## # check for multiple declarations, allowing for a function declaration
4470## # continuation.
4471## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4472## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4473##
4474## # Remove any bracketed sections to ensure we do not
4475## # falsly report the parameters of functions.
4476## my $ln = $line;
4477## while ($ln =~ s/\([^\(\)]*\)//g) {
4478## }
4479## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004480## WARN("MULTIPLE_DECLARATION",
4481## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004482## }
4483## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004484
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004485#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004486 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004487 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004488 if (ERROR("SPACING",
4489 "space required before the open brace '{'\n" . $herecurr) &&
4490 $fix) {
Cyril Bur8d81ae02017-07-10 15:52:21 -07004491 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004492 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004493 }
4494
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004495## # check for blank lines before declarations
4496## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4497## $prevrawline =~ /^.\s*$/) {
4498## WARN("SPACING",
4499## "No blank lines before declarations\n" . $hereprev);
4500## }
4501##
4502
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004503# closing brace should have a space following it when it has anything
4504# on the line
4505 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004506 if (ERROR("SPACING",
4507 "space required after that close brace '}'\n" . $herecurr) &&
4508 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004509 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004510 s/}((?!(?:,|;|\)))\S)/} $1/;
4511 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004512 }
4513
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004514# check spacing on square brackets
4515 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004516 if (ERROR("SPACING",
4517 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4518 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004519 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004520 s/\[\s+/\[/;
4521 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004522 }
4523 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004524 if (ERROR("SPACING",
4525 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4526 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004527 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004528 s/\s+\]/\]/;
4529 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004530 }
4531
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004532# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004533 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4534 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004535 if (ERROR("SPACING",
4536 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4537 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004538 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004539 s/\(\s+/\(/;
4540 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004541 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004542 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004543 $line !~ /for\s*\(.*;\s+\)/ &&
4544 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004545 if (ERROR("SPACING",
4546 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4547 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004548 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004549 s/\s+\)/\)/;
4550 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004551 }
4552
Joe Perchese2826fd2014-08-06 16:10:48 -07004553# check unnecessary parentheses around addressof/dereference single $Lvals
4554# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4555
4556 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004557 my $var = $1;
4558 if (CHK("UNNECESSARY_PARENTHESES",
4559 "Unnecessary parentheses around $var\n" . $herecurr) &&
4560 $fix) {
4561 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4562 }
4563 }
4564
4565# check for unnecessary parentheses around function pointer uses
4566# ie: (foo->bar)(); should be foo->bar();
4567# but not "if (foo->bar) (" to avoid some false positives
4568 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4569 my $var = $2;
4570 if (CHK("UNNECESSARY_PARENTHESES",
4571 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4572 $fix) {
4573 my $var2 = deparenthesize($var);
4574 $var2 =~ s/\s//g;
4575 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4576 }
4577 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004578
Joe Perches63b7c732017-09-08 16:16:01 -07004579# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004580# when !drivers/staging or command-line uses --strict
4581 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004582 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004583 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4584 my $if_stat = $1;
4585 my $test = substr($2, 1, -1);
4586 my $herectx;
4587 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4588 my $match = $1;
4589 # avoid parentheses around potential macro args
4590 next if ($match =~ /^\s*\w+\s*$/);
4591 if (!defined($herectx)) {
4592 $herectx = $here . "\n";
4593 my $cnt = statement_rawlines($if_stat);
4594 for (my $n = 0; $n < $cnt; $n++) {
4595 my $rl = raw_line($linenr, $n);
4596 $herectx .= $rl . "\n";
4597 last if $rl =~ /^[ \+].*\{/;
4598 }
4599 }
4600 CHK("UNNECESSARY_PARENTHESES",
4601 "Unnecessary parentheses around '$match'\n" . $herectx);
4602 }
4603 }
4604
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004605#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004606 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004607 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004608 if (WARN("INDENTED_LABEL",
4609 "labels should not be indented\n" . $herecurr) &&
4610 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004611 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004612 s/^(.)\s+/$1/;
4613 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004614 }
4615
Joe Perches5b9553a2014-04-03 14:49:21 -07004616# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004617 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004618 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004619 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004620 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4621 my $value = $1;
4622 $value = deparenthesize($value);
4623 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4624 ERROR("RETURN_PARENTHESES",
4625 "return is not a function, parentheses are not required\n" . $herecurr);
4626 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004627 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004628 ERROR("SPACING",
4629 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004630 }
4631 }
Joe Perches507e5142013-11-12 15:10:13 -08004632
Joe Perchesb43ae212014-06-23 13:22:07 -07004633# unnecessary return in a void function
4634# at end-of-function, with the previous line a single leading tab, then return;
4635# and the line before that not a goto label target like "out:"
4636 if ($sline =~ /^[ \+]}\s*$/ &&
4637 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4638 $linenr >= 3 &&
4639 $lines[$linenr - 3] =~ /^[ +]/ &&
4640 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004641 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004642 "void function return statements are not generally useful\n" . $hereprev);
4643 }
Joe Perches9819cf22014-06-04 16:12:09 -07004644
Joe Perches189248d2014-01-23 15:54:47 -08004645# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004646 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004647 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4648 my $openparens = $1;
4649 my $count = $openparens =~ tr@\(@\(@;
4650 my $msg = "";
4651 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4652 my $comp = $4; #Not $1 because of $LvalOrFunc
4653 $msg = " - maybe == should be = ?" if ($comp eq "==");
4654 WARN("UNNECESSARY_PARENTHESES",
4655 "Unnecessary parentheses$msg\n" . $herecurr);
4656 }
4657 }
4658
Joe Perchesc5595fa2015-09-09 15:37:58 -07004659# comparisons with a constant or upper case identifier on the left
4660# avoid cases like "foo + BAR < baz"
4661# only fix matches surrounded by parentheses to avoid incorrect
4662# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004663 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004664 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4665 my $lead = $1;
4666 my $const = $2;
4667 my $comp = $3;
4668 my $to = $4;
4669 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004670 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004671 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4672 WARN("CONSTANT_COMPARISON",
4673 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4674 $fix) {
4675 if ($comp eq "<") {
4676 $newcomp = ">";
4677 } elsif ($comp eq "<=") {
4678 $newcomp = ">=";
4679 } elsif ($comp eq ">") {
4680 $newcomp = "<";
4681 } elsif ($comp eq ">=") {
4682 $newcomp = "<=";
4683 }
4684 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4685 }
4686 }
4687
Joe Perchesf34e4a42015-04-16 12:44:19 -07004688# Return of what appears to be an errno should normally be negative
4689 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004690 my $name = $1;
4691 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004692 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004693 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004694 }
4695 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004696
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004697# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004698 if ($line =~ /\b(if|while|for|switch)\(/) {
4699 if (ERROR("SPACING",
4700 "space required before the open parenthesis '('\n" . $herecurr) &&
4701 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004702 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004703 s/\b(if|while|for|switch)\(/$1 \(/;
4704 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004705 }
4706
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004707# Check for illegal assignment in if conditional -- and check for trailing
4708# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004709 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004710 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4711 ctx_statement_block($linenr, $realcnt, 0)
4712 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004713 my ($stat_next) = ctx_statement_block($line_nr_next,
4714 $remain_next, $off_next);
4715 $stat_next =~ s/\n./\n /g;
4716 ##print "stat<$stat> stat_next<$stat_next>\n";
4717
4718 if ($stat_next =~ /^\s*while\b/) {
4719 # If the statement carries leading newlines,
4720 # then count those as offsets.
4721 my ($whitespace) =
4722 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4723 my $offset =
4724 statement_rawlines($whitespace) - 1;
4725
4726 $suppress_whiletrailers{$line_nr_next +
4727 $offset} = 1;
4728 }
4729 }
4730 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004731 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004732 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004733 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004734
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004735 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004736 ERROR("ASSIGN_IN_IF",
4737 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004738 }
4739
4740 # Find out what is on the end of the line after the
4741 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004742 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004743 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004744 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004745 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4746 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004747 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004748 # Find out how long the conditional actually is.
4749 my @newlines = ($c =~ /\n/gs);
4750 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004751 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004752
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004753 $stat_real = raw_line($linenr, $cond_lines)
4754 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004755 if (defined($stat_real) && $cond_lines > 1) {
4756 $stat_real = "[...]\n$stat_real";
4757 }
4758
Joe Perches000d1cc12011-07-25 17:13:25 -07004759 ERROR("TRAILING_STATEMENTS",
4760 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004761 }
4762 }
4763
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004764# Check for bitwise tests written as boolean
4765 if ($line =~ /
4766 (?:
4767 (?:\[|\(|\&\&|\|\|)
4768 \s*0[xX][0-9]+\s*
4769 (?:\&\&|\|\|)
4770 |
4771 (?:\&\&|\|\|)
4772 \s*0[xX][0-9]+\s*
4773 (?:\&\&|\|\||\)|\])
4774 )/x)
4775 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004776 WARN("HEXADECIMAL_BOOLEAN_TEST",
4777 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004778 }
4779
Andy Whitcroft8905a672007-11-28 16:21:06 -08004780# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004781 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4782 my $s = $1;
4783 $s =~ s/$;//g; # Remove any comments
4784 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004785 ERROR("TRAILING_STATEMENTS",
4786 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004787 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004788 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004789# if should not continue a brace
4790 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004791 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004792 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004793 $herecurr);
4794 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004795# case and default should not have general statements after them
4796 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4797 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004798 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004799 \s*return\s+
4800 )/xg)
4801 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004802 ERROR("TRAILING_STATEMENTS",
4803 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004804 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004805
4806 # Check for }<nl>else {, these must be at the same
4807 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004808 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4809 $previndent == $indent) {
4810 if (ERROR("ELSE_AFTER_BRACE",
4811 "else should follow close brace '}'\n" . $hereprev) &&
4812 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4813 fix_delete_line($fixlinenr - 1, $prevrawline);
4814 fix_delete_line($fixlinenr, $rawline);
4815 my $fixedline = $prevrawline;
4816 $fixedline =~ s/}\s*$//;
4817 if ($fixedline !~ /^\+\s*$/) {
4818 fix_insert_line($fixlinenr, $fixedline);
4819 }
4820 $fixedline = $rawline;
4821 $fixedline =~ s/^(.\s*)else/$1} else/;
4822 fix_insert_line($fixlinenr, $fixedline);
4823 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004824 }
4825
Joe Perches8b8856f2014-08-06 16:11:14 -07004826 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4827 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004828 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4829
4830 # Find out what is on the end of the line after the
4831 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004832 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004833 $s =~ s/\n.*//g;
4834
4835 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004836 if (ERROR("WHILE_AFTER_BRACE",
4837 "while should follow close brace '}'\n" . $hereprev) &&
4838 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4839 fix_delete_line($fixlinenr - 1, $prevrawline);
4840 fix_delete_line($fixlinenr, $rawline);
4841 my $fixedline = $prevrawline;
4842 my $trailing = $rawline;
4843 $trailing =~ s/^\+//;
4844 $trailing = trim($trailing);
4845 $fixedline =~ s/}\s*$/} $trailing/;
4846 fix_insert_line($fixlinenr, $fixedline);
4847 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004848 }
4849 }
4850
Joe Perches95e2c602013-07-03 15:05:20 -07004851#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004852 while ($line =~ m{($Constant|$Lval)}g) {
4853 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004854
4855#gcc binary extension
4856 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004857 if (WARN("GCC_BINARY_CONSTANT",
4858 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4859 $fix) {
4860 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004861 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004862 s/\b$var\b/$hexval/;
4863 }
Joe Perches95e2c602013-07-03 15:05:20 -07004864 }
4865
4866#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004867 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004868 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004869#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004870 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004871#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004872 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4873#Ignore some three character SI units explicitly, like MiB and KHz
4874 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004875 while ($var =~ m{($Ident)}g) {
4876 my $word = $1;
4877 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004878 if ($check) {
4879 seed_camelcase_includes();
4880 if (!$file && !$camelcase_file_seeded) {
4881 seed_camelcase_file($realfile);
4882 $camelcase_file_seeded = 1;
4883 }
4884 }
Joe Perches7e781f62013-09-11 14:23:55 -07004885 if (!defined $camelcase{$word}) {
4886 $camelcase{$word} = 1;
4887 CHK("CAMELCASE",
4888 "Avoid CamelCase: <$word>\n" . $herecurr);
4889 }
Joe Perches34456862013-07-03 15:05:34 -07004890 }
Joe Perches323c1262012-12-17 16:02:07 -08004891 }
4892 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004893
4894#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004895 if ($line =~ /\#\s*define.*\\\s+$/) {
4896 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4897 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4898 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004899 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004900 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004901 }
4902
Fabian Frederick0e212e02015-04-16 12:44:25 -07004903# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4904# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004905 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004906 my $file = "$1.h";
4907 my $checkfile = "include/linux/$file";
4908 if (-f "$root/$checkfile" &&
4909 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004910 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004911 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004912 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4913 if ($asminclude > 0) {
4914 if ($realfile =~ m{^arch/}) {
4915 CHK("ARCH_INCLUDE_LINUX",
4916 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4917 } else {
4918 WARN("INCLUDE_LINUX",
4919 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4920 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004921 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004922 }
4923 }
4924
Andy Whitcroft653d4872007-06-23 17:16:34 -07004925# multi-statement macros should be enclosed in a do while loop, grab the
4926# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004927# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004928 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4929 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004930 my $ln = $linenr;
4931 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004932 my ($off, $dstat, $dcond, $rest);
4933 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004934 my $has_flow_statement = 0;
4935 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004936 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004937 ctx_statement_block($linenr, $realcnt, 0);
4938 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004939 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004940 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004941
Joe Perches08a28432014-10-13 15:51:55 -07004942 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004943 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004944
Joe Perchesf59b64b2016-10-11 13:52:08 -07004945 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4946 my $define_args = $1;
4947 my $define_stmt = $dstat;
4948 my @def_args = ();
4949
4950 if (defined $define_args && $define_args ne "") {
4951 $define_args = substr($define_args, 1, length($define_args) - 2);
4952 $define_args =~ s/\s*//g;
4953 @def_args = split(",", $define_args);
4954 }
4955
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004956 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004957 $dstat =~ s/\\\n.//g;
4958 $dstat =~ s/^\s*//s;
4959 $dstat =~ s/\s*$//s;
4960
4961 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004962 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4963 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004964 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004965 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004966 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004967
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004968 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004969 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4970 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004971 {
4972 }
4973
Joe Perches42e15292016-03-15 14:58:01 -07004974 # Make asm volatile uses seem like a generic function
4975 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4976
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004977 my $exceptions = qr{
4978 $Declare|
4979 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004980 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004981 DECLARE_PER_CPU|
4982 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004983 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004984 union|
4985 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004986 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004987 ^\"|\"$|
4988 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004989 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004990 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004991
4992 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07004993 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07004994 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07004995
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004996 if ($dstat ne '' &&
4997 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4998 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004999 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005000 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005001 $dstat !~ /$exceptions/ &&
5002 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005003 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005004 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005005 $dstat !~ /^for\s*$Constant$/ && # for (...)
5006 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5007 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005008 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005009 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005010 {
Joe Perchese7955562017-05-08 15:55:48 -07005011 if ($dstat =~ /^\s*if\b/) {
5012 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5013 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5014 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005015 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5016 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5017 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005018 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005019 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005020 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005021
5022 }
Joe Perches52076492016-10-11 13:52:14 -07005023
5024 # Make $define_stmt single line, comment-free, etc
5025 my @stmt_array = split('\n', $define_stmt);
5026 my $first = 1;
5027 $define_stmt = "";
5028 foreach my $l (@stmt_array) {
5029 $l =~ s/\\$//;
5030 if ($first) {
5031 $define_stmt = $l;
5032 $first = 0;
5033 } elsif ($l =~ /^[\+ ]/) {
5034 $define_stmt .= substr($l, 1);
5035 }
5036 }
5037 $define_stmt =~ s/$;//g;
5038 $define_stmt =~ s/\s+/ /g;
5039 $define_stmt = trim($define_stmt);
5040
Joe Perchesf59b64b2016-10-11 13:52:08 -07005041# check if any macro arguments are reused (ignore '...' and 'type')
5042 foreach my $arg (@def_args) {
5043 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005044 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005045 my $tmp_stmt = $define_stmt;
5046 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5047 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5048 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005049 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005050 if ($use_cnt > 1) {
5051 CHK("MACRO_ARG_REUSE",
5052 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005053 }
5054# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005055 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005056 ((defined($1) && $1 ne ',') ||
5057 (defined($2) && $2 ne ','))) {
5058 CHK("MACRO_ARG_PRECEDENCE",
5059 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005060 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005061 }
Joe Perches5023d342012-12-17 16:01:47 -08005062
Joe Perches08a28432014-10-13 15:51:55 -07005063# check for macros with flow control, but without ## concatenation
5064# ## concatenation is commonly a macro that defines a function so ignore those
5065 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005066 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005067 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005068
Joe Perches08a28432014-10-13 15:51:55 -07005069 WARN("MACRO_WITH_FLOW_CONTROL",
5070 "Macros with flow control statements should be avoided\n" . "$herectx");
5071 }
5072
Joe Perches481eb482012-12-17 16:01:56 -08005073# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005074
5075 } else {
5076 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005077 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5078 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005079 $line =~ /^\+.*\\$/) {
5080 WARN("LINE_CONTINUATIONS",
5081 "Avoid unnecessary line continuations\n" . $herecurr);
5082 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005083 }
5084
Joe Perchesb13edf72012-07-30 14:41:24 -07005085# do {} while (0) macro tests:
5086# single-statement macros do not need to be enclosed in do while (0) loop,
5087# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005088 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005089 $realfile !~ m@/vmlinux.lds.h$@ &&
5090 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5091 my $ln = $linenr;
5092 my $cnt = $realcnt;
5093 my ($off, $dstat, $dcond, $rest);
5094 my $ctx = '';
5095 ($dstat, $dcond, $ln, $cnt, $off) =
5096 ctx_statement_block($linenr, $realcnt, 0);
5097 $ctx = $dstat;
5098
5099 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005100 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005101
5102 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5103 my $stmts = $2;
5104 my $semis = $3;
5105
5106 $ctx =~ s/\n*$//;
5107 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005108 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005109
Joe Perchesac8e97f2012-08-21 16:15:53 -07005110 if (($stmts =~ tr/;/;/) == 1 &&
5111 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005112 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5113 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5114 }
5115 if (defined $semis && $semis ne "") {
5116 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5117 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5118 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005119 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5120 $ctx =~ s/\n*$//;
5121 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005122 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005123
5124 WARN("TRAILING_SEMICOLON",
5125 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005126 }
5127 }
5128
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005129# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005130 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5131 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005132 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005133 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005134 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5135 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005136 my @allowed = ();
5137 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005138 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005139 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005140 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005141 for my $chunk (@chunks) {
5142 my ($cond, $block) = @{$chunk};
5143
Andy Whitcroft773647a2008-03-28 14:15:58 -07005144 # If the condition carries leading newlines, then count those as offsets.
5145 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5146 my $offset = statement_rawlines($whitespace) - 1;
5147
Joe Perchesaad4f612012-03-23 15:02:19 -07005148 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005149 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5150
5151 # We have looked at and allowed this specific line.
5152 $suppress_ifbraces{$ln + $offset} = 1;
5153
5154 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005155 $ln += statement_rawlines($block) - 1;
5156
Andy Whitcroft773647a2008-03-28 14:15:58 -07005157 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005158
5159 $seen++ if ($block =~ /^\s*{/);
5160
Joe Perchesaad4f612012-03-23 15:02:19 -07005161 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005162 if (statement_lines($cond) > 1) {
5163 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005164 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005165 }
5166 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005167 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005168 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005169 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005170 if (statement_block_size($block) > 1) {
5171 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005172 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005173 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005174 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005175 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005176 if ($seen) {
5177 my $sum_allowed = 0;
5178 foreach (@allowed) {
5179 $sum_allowed += $_;
5180 }
5181 if ($sum_allowed == 0) {
5182 WARN("BRACES",
5183 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5184 } elsif ($sum_allowed != $allow &&
5185 $seen != $allow) {
5186 CHK("BRACES",
5187 "braces {} should be used on all arms of this statement\n" . $herectx);
5188 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005189 }
5190 }
5191 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005192 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005193 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005194 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005195
Andy Whitcroftcf655042008-03-04 14:28:20 -08005196 # Check the pre-context.
5197 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5198 #print "APW: ALLOWED: pre<$1>\n";
5199 $allowed = 1;
5200 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005201
5202 my ($level, $endln, @chunks) =
5203 ctx_statement_full($linenr, $realcnt, $-[0]);
5204
Andy Whitcroftcf655042008-03-04 14:28:20 -08005205 # Check the condition.
5206 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005207 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005208 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005209 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005210 }
5211 if (statement_lines($cond) > 1) {
5212 #print "APW: ALLOWED: cond<$cond>\n";
5213 $allowed = 1;
5214 }
5215 if ($block =~/\b(?:if|for|while)\b/) {
5216 #print "APW: ALLOWED: block<$block>\n";
5217 $allowed = 1;
5218 }
5219 if (statement_block_size($block) > 1) {
5220 #print "APW: ALLOWED: lines block<$block>\n";
5221 $allowed = 1;
5222 }
5223 # Check the post-context.
5224 if (defined $chunks[1]) {
5225 my ($cond, $block) = @{$chunks[1]};
5226 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005227 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005228 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005229 if ($block =~ /^\s*\{/) {
5230 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5231 $allowed = 1;
5232 }
5233 }
5234 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005235 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005236 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005237
Joe Perches000d1cc12011-07-25 17:13:25 -07005238 WARN("BRACES",
5239 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005240 }
5241 }
5242
Joe Perchese4c5bab2017-02-24 15:01:41 -08005243# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005244 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5245 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005246 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5247 }
5248
Joe Perches0979ae62012-12-17 16:01:59 -08005249# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005250 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005251 if (CHK("BRACES",
5252 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5253 $fix && $prevrawline =~ /^\+/) {
5254 fix_delete_line($fixlinenr - 1, $prevrawline);
5255 }
Joe Perches0979ae62012-12-17 16:01:59 -08005256 }
Joe Perches77b9a532013-07-03 15:05:29 -07005257 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005258 if (CHK("BRACES",
5259 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5260 $fix) {
5261 fix_delete_line($fixlinenr, $rawline);
5262 }
Joe Perches0979ae62012-12-17 16:01:59 -08005263 }
5264
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005265# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005266 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5267 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005268 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005269 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005270 }
5271
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005272# Check for user-visible strings broken across lines, which breaks the ability
5273# to grep for the string. Make exceptions when the previous string ends in a
5274# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5275# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005276 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005277 $prevline =~ /"\s*$/ &&
5278 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5279 if (WARN("SPLIT_STRING",
5280 "quoted string split across lines\n" . $hereprev) &&
5281 $fix &&
5282 $prevrawline =~ /^\+.*"\s*$/ &&
5283 $last_coalesced_string_linenr != $linenr - 1) {
5284 my $extracted_string = get_quoted_string($line, $rawline);
5285 my $comma_close = "";
5286 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5287 $comma_close = $1;
5288 }
5289
5290 fix_delete_line($fixlinenr - 1, $prevrawline);
5291 fix_delete_line($fixlinenr, $rawline);
5292 my $fixedline = $prevrawline;
5293 $fixedline =~ s/"\s*$//;
5294 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5295 fix_insert_line($fixlinenr - 1, $fixedline);
5296 $fixedline = $rawline;
5297 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5298 if ($fixedline !~ /\+\s*$/) {
5299 fix_insert_line($fixlinenr, $fixedline);
5300 }
5301 $last_coalesced_string_linenr = $linenr;
5302 }
5303 }
5304
5305# check for missing a space in a string concatenation
5306 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5307 WARN('MISSING_SPACE',
5308 "break quoted strings at a space character\n" . $hereprev);
5309 }
5310
Joe Perchese4b7d302017-05-08 15:55:51 -07005311# check for an embedded function name in a string when the function is known
5312# This does not work very well for -f --file checking as it depends on patch
5313# context providing the function name or a single line form for in-file
5314# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005315 if ($line =~ /^\+.*$String/ &&
5316 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005317 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5318 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005319 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005320 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005321 }
5322
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005323# check for spaces before a quoted newline
5324 if ($rawline =~ /^.*\".*\s\\n/) {
5325 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5326 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5327 $fix) {
5328 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5329 }
5330
5331 }
5332
Joe Perchesf17dba42014-10-13 15:51:51 -07005333# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005334 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5335 if (CHK("CONCATENATED_STRING",
5336 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5337 $fix) {
5338 while ($line =~ /($String)/g) {
5339 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5340 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5341 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5342 }
5343 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005344 }
5345
Joe Perches90ad30e2014-12-10 15:51:59 -08005346# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005347 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005348 if (WARN("STRING_FRAGMENTS",
5349 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5350 $fix) {
5351 while ($line =~ /($String)(?=\s*")/g) {
5352 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5353 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5354 }
5355 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005356 }
5357
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005358# check for non-standard and hex prefixed decimal printf formats
5359 my $show_L = 1; #don't show the same defect twice
5360 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005361 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005362 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005363 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005364 # check for %L
5365 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005366 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005367 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5368 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005369 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005370 # check for %Z
5371 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5372 WARN("PRINTF_Z",
5373 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5374 $show_Z = 0;
5375 }
5376 # check for 0x<decimal>
5377 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5378 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005379 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5380 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005381 }
5382
5383# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005384 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005385 WARN("LINE_CONTINUATIONS",
5386 "Avoid line continuations in quoted strings\n" . $herecurr);
5387 }
5388
Andy Whitcroft00df3442007-06-08 13:47:06 -07005389# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005390 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005391 CHK("REDUNDANT_CODE",
5392 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005393 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005394 }
5395
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005396# check for needless "if (<foo>) fn(<foo>)" uses
5397 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005398 my $tested = quotemeta($1);
5399 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5400 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5401 my $func = $1;
5402 if (WARN('NEEDLESS_IF',
5403 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5404 $fix) {
5405 my $do_fix = 1;
5406 my $leading_tabs = "";
5407 my $new_leading_tabs = "";
5408 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5409 $leading_tabs = $1;
5410 } else {
5411 $do_fix = 0;
5412 }
5413 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5414 $new_leading_tabs = $1;
5415 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5416 $do_fix = 0;
5417 }
5418 } else {
5419 $do_fix = 0;
5420 }
5421 if ($do_fix) {
5422 fix_delete_line($fixlinenr - 1, $prevrawline);
5423 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5424 }
5425 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005426 }
5427 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005428
Joe Perchesebfdc402014-08-06 16:10:27 -07005429# check for unnecessary "Out of Memory" messages
5430 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5431 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5432 (defined $1 || defined $3) &&
5433 $linenr > 3) {
5434 my $testval = $2;
5435 my $testline = $lines[$linenr - 3];
5436
5437 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5438# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5439
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005440 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 -07005441 WARN("OOM_MESSAGE",
5442 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5443 }
5444 }
5445
Joe Perchesf78d98f2014-10-13 15:52:01 -07005446# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005447 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005448 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5449 my $level = $1;
5450 if (WARN("UNNECESSARY_KERN_LEVEL",
5451 "Possible unnecessary $level\n" . $herecurr) &&
5452 $fix) {
5453 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5454 }
5455 }
5456
Joe Perches45c55e92017-02-24 15:01:31 -08005457# check for logging continuations
5458 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5459 WARN("LOGGING_CONTINUATION",
5460 "Avoid logging continuation uses where feasible\n" . $herecurr);
5461 }
5462
Joe Perchesabb08a52014-12-10 15:51:46 -08005463# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005464 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005465 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5466 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5467 WARN("MASK_THEN_SHIFT",
5468 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5469 }
5470
Joe Perchesb75ac612014-12-10 15:52:02 -08005471# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005472 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005473 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5474 my $val = $1;
5475 my $equal = "!";
5476 $equal = "" if ($4 eq "!=");
5477 if (CHK("COMPARISON_TO_NULL",
5478 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5479 $fix) {
5480 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5481 }
5482 }
5483 }
5484
Joe Perches8716de32013-09-11 14:24:05 -07005485# check for bad placement of section $InitAttribute (e.g.: __initdata)
5486 if ($line =~ /(\b$InitAttribute\b)/) {
5487 my $attr = $1;
5488 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5489 my $ptr = $1;
5490 my $var = $2;
5491 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5492 ERROR("MISPLACED_INIT",
5493 "$attr should be placed after $var\n" . $herecurr)) ||
5494 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5495 WARN("MISPLACED_INIT",
5496 "$attr should be placed after $var\n" . $herecurr))) &&
5497 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005498 $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 -07005499 }
5500 }
5501 }
5502
Joe Perchese970b8842013-11-12 15:10:10 -08005503# check for $InitAttributeData (ie: __initdata) with const
5504 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5505 my $attr = $1;
5506 $attr =~ /($InitAttributePrefix)(.*)/;
5507 my $attr_prefix = $1;
5508 my $attr_type = $2;
5509 if (ERROR("INIT_ATTRIBUTE",
5510 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5511 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005512 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005513 s/$InitAttributeData/${attr_prefix}initconst/;
5514 }
5515 }
5516
5517# check for $InitAttributeConst (ie: __initconst) without const
5518 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5519 my $attr = $1;
5520 if (ERROR("INIT_ATTRIBUTE",
5521 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5522 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005523 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005524 /(^\+\s*(?:static\s+))/;
5525 $lead = rtrim($1);
5526 $lead = "$lead " if ($lead !~ /^\+$/);
5527 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005528 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005529 }
5530 }
5531
Joe Perchesc17893c2015-04-16 12:44:42 -07005532# check for __read_mostly with const non-pointer (should just be const)
5533 if ($line =~ /\b__read_mostly\b/ &&
5534 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5535 if (ERROR("CONST_READ_MOSTLY",
5536 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5537 $fix) {
5538 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5539 }
5540 }
5541
Joe Perchesfbdb8132014-04-03 14:49:14 -07005542# don't use __constant_<foo> functions outside of include/uapi/
5543 if ($realfile !~ m@^include/uapi/@ &&
5544 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5545 my $constant_func = $1;
5546 my $func = $constant_func;
5547 $func =~ s/^__constant_//;
5548 if (WARN("CONSTANT_CONVERSION",
5549 "$constant_func should be $func\n" . $herecurr) &&
5550 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005551 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005552 }
5553 }
5554
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005555# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005556 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005557 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005558 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005559 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005560 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005561 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5562 }
5563 if ($delay > 2000) {
5564 WARN("LONG_UDELAY",
5565 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005566 }
5567 }
5568
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005569# warn about unexpectedly long msleep's
5570 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5571 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005572 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005573 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005574 }
5575 }
5576
Joe Perches36ec1932013-07-03 15:05:25 -07005577# check for comparisons of jiffies
5578 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5579 WARN("JIFFIES_COMPARISON",
5580 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5581 }
5582
Joe Perches9d7a34a2013-07-03 15:05:26 -07005583# check for comparisons of get_jiffies_64()
5584 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5585 WARN("JIFFIES_COMPARISON",
5586 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5587 }
5588
Andy Whitcroft00df3442007-06-08 13:47:06 -07005589# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005590# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005591# print "#ifdef in C files should be avoided\n";
5592# print "$herecurr";
5593# $clean = 0;
5594# }
5595
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005596# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005597 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005598 if (ERROR("SPACING",
5599 "exactly one space required after that #$1\n" . $herecurr) &&
5600 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005601 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005602 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5603 }
5604
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005605 }
5606
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005607# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005608 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5609 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005610 my $which = $1;
5611 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005612 CHK("UNCOMMENTED_DEFINITION",
5613 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005614 }
5615 }
5616# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005617
5618 my $barriers = qr{
5619 mb|
5620 rmb|
5621 wmb|
5622 read_barrier_depends
5623 }x;
5624 my $barrier_stems = qr{
5625 mb__before_atomic|
5626 mb__after_atomic|
5627 store_release|
5628 load_acquire|
5629 store_mb|
5630 (?:$barriers)
5631 }x;
5632 my $all_barriers = qr{
5633 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005634 smp_(?:$barrier_stems)|
5635 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005636 }x;
5637
5638 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005639 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005640 WARN("MEMORY_BARRIER",
5641 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005642 }
5643 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005644
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005645 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5646
5647 if ($realfile !~ m@^include/asm-generic/@ &&
5648 $realfile !~ m@/barrier\.h$@ &&
5649 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5650 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5651 WARN("MEMORY_BARRIER",
5652 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5653 }
5654
Joe Perchescb426e92015-06-25 15:02:46 -07005655# check for waitqueue_active without a comment.
5656 if ($line =~ /\bwaitqueue_active\s*\(/) {
5657 if (!ctx_has_comment($first_line, $linenr)) {
5658 WARN("WAITQUEUE_ACTIVE",
5659 "waitqueue_active without comment\n" . $herecurr);
5660 }
5661 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005662
Paul E. McKenney91db2592017-11-27 09:37:35 -08005663# check for smp_read_barrier_depends and read_barrier_depends
5664 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5665 WARN("READ_BARRIER_DEPENDS",
5666 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5667 }
5668
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005669# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005670 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005671 CHK("ARCH_DEFINES",
5672 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005673 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005674
Joe Perches596ed452017-07-12 14:37:02 -07005675# check that the storage class is not after a type
5676 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005677 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005678 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5679 }
5680# Check that the storage class is at the beginning of a declaration
5681 if ($line =~ /\b$Storage\b/ &&
5682 $line !~ /^.\s*$Storage/ &&
5683 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5684 $1 !~ /[\,\)]\s*$/) {
5685 WARN("STORAGE_CLASS",
5686 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005687 }
5688
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005689# check the location of the inline attribute, that it is between
5690# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005691 if ($line =~ /\b$Type\s+$Inline\b/ ||
5692 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005693 ERROR("INLINE_LOCATION",
5694 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005695 }
5696
Andy Whitcroft8905a672007-11-28 16:21:06 -08005697# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005698 if ($realfile !~ m@\binclude/uapi/@ &&
5699 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005700 if (WARN("INLINE",
5701 "plain inline is preferred over $1\n" . $herecurr) &&
5702 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005703 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005704
5705 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005706 }
5707
Joe Perches3d130fd2011-01-12 17:00:00 -08005708# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005709 if ($realfile !~ m@\binclude/uapi/@ &&
5710 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005711 WARN("PREFER_PACKED",
5712 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005713 }
5714
Joe Perches39b7e282011-07-25 17:13:24 -07005715# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005716 if ($realfile !~ m@\binclude/uapi/@ &&
5717 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005718 WARN("PREFER_ALIGNED",
5719 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005720 }
5721
Joe Perches5f14d3b2012-01-10 15:09:52 -08005722# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005723 if ($realfile !~ m@\binclude/uapi/@ &&
5724 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005725 if (WARN("PREFER_PRINTF",
5726 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5727 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005728 $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 -07005729
5730 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005731 }
5732
Joe Perches6061d942012-03-23 15:02:16 -07005733# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005734 if ($realfile !~ m@\binclude/uapi/@ &&
5735 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005736 if (WARN("PREFER_SCANF",
5737 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5738 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005739 $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 -07005740 }
Joe Perches6061d942012-03-23 15:02:16 -07005741 }
5742
Joe Perches619a9082014-12-10 15:51:35 -08005743# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005744 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005745 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5746 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5747 $line =~ /\b__weak\b/)) {
5748 ERROR("WEAK_DECLARATION",
5749 "Using weak declarations can have unintended link defects\n" . $herecurr);
5750 }
5751
Tomas Winklerfd39f902016-12-12 16:46:34 -08005752# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005753 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005754 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005755 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5756 my $type = $1;
5757 if ($type =~ /\b($typeC99Typedefs)\b/) {
5758 $type = $1;
5759 my $kernel_type = 'u';
5760 $kernel_type = 's' if ($type =~ /^_*[si]/);
5761 $type =~ /(\d+)/;
5762 $kernel_type .= $1;
5763 if (CHK("PREFER_KERNEL_TYPES",
5764 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5765 $fix) {
5766 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5767 }
5768 }
5769 }
5770
Joe Perches938224b2016-01-20 14:59:15 -08005771# check for cast of C90 native int or longer types constants
5772 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5773 my $cast = $1;
5774 my $const = $2;
5775 if (WARN("TYPECAST_INT_CONSTANT",
5776 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5777 $fix) {
5778 my $suffix = "";
5779 my $newconst = $const;
5780 $newconst =~ s/${Int_type}$//;
5781 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5782 if ($cast =~ /\blong\s+long\b/) {
5783 $suffix .= 'LL';
5784 } elsif ($cast =~ /\blong\b/) {
5785 $suffix .= 'L';
5786 }
5787 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5788 }
5789 }
5790
Joe Perches8f53a9b2010-03-05 13:43:48 -08005791# check for sizeof(&)
5792 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005793 WARN("SIZEOF_ADDRESS",
5794 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005795 }
5796
Joe Perches66c80b62012-07-30 14:41:22 -07005797# check for sizeof without parenthesis
5798 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005799 if (WARN("SIZEOF_PARENTHESIS",
5800 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5801 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005802 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005803 }
Joe Perches66c80b62012-07-30 14:41:22 -07005804 }
5805
Joe Perches88982fe2012-12-17 16:02:00 -08005806# check for struct spinlock declarations
5807 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5808 WARN("USE_SPINLOCK_T",
5809 "struct spinlock should be spinlock_t\n" . $herecurr);
5810 }
5811
Joe Perchesa6962d72013-04-29 16:18:13 -07005812# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005813 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005814 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005815 $fmt =~ s/%%//g;
5816 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005817 if (WARN("PREFER_SEQ_PUTS",
5818 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5819 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005820 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005821 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005822 }
5823 }
5824
Joe Perches478b1792018-04-10 16:33:34 -07005825# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07005826 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07005827 defined $stat &&
5828 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5829 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005830 my $stat_real;
5831
Joe Perches0b523762017-05-08 15:55:36 -07005832 my $lc = $stat =~ tr@\n@@;
5833 $lc = $lc + $linenr;
5834 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07005835 my $specifier;
5836 my $extension;
5837 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07005838 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5839 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005840
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005841 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5842 $specifier = $1;
5843 $extension = $2;
5844 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5845 $bad_specifier = $specifier;
5846 last;
5847 }
5848 if ($extension eq "x" && !defined($stat_real)) {
5849 if (!defined($stat_real)) {
5850 $stat_real = get_stat_real($linenr, $lc);
5851 }
5852 WARN("VSPRINTF_SPECIFIER_PX",
5853 "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");
5854 }
5855 }
5856 if ($bad_specifier ne "") {
5857 my $stat_real = get_stat_real($linenr, $lc);
5858 my $ext_type = "Invalid";
5859 my $use = "";
5860 if ($bad_specifier =~ /p[Ff]/) {
5861 $ext_type = "Deprecated";
5862 $use = " - use %pS instead";
5863 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5864 }
5865
5866 WARN("VSPRINTF_POINTER_EXTENSION",
5867 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5868 }
Joe Perches0b523762017-05-08 15:55:36 -07005869 }
5870 }
5871
Andy Whitcroft554e1652012-01-10 15:09:57 -08005872# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07005873 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005874 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005875 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005876
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005877 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005878 my $ms_val = $7;
5879 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005880
Andy Whitcroft554e1652012-01-10 15:09:57 -08005881 if ($ms_size =~ /^(0x|)0$/i) {
5882 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005883 "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 -08005884 } elsif ($ms_size =~ /^(0x|)1$/i) {
5885 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005886 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5887 }
5888 }
5889
Joe Perches98a9bba2014-01-23 15:54:52 -08005890# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005891# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07005892# defined $stat &&
5893# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5894# if (WARN("PREFER_ETHER_ADDR_COPY",
5895# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5896# $fix) {
5897# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5898# }
5899# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005900
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005901# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005902# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07005903# defined $stat &&
5904# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5905# WARN("PREFER_ETHER_ADDR_EQUAL",
5906# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5907# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005908
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005909# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5910# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07005911# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07005912# defined $stat &&
5913# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5914#
5915# my $ms_val = $7;
5916#
5917# if ($ms_val =~ /^(?:0x|)0+$/i) {
5918# if (WARN("PREFER_ETH_ZERO_ADDR",
5919# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5920# $fix) {
5921# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5922# }
5923# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5924# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5925# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5926# $fix) {
5927# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5928# }
5929# }
5930# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005931
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005932# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07005933 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005934 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005935 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005936 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005937 my $call = $1;
5938 my $cast1 = deparenthesize($2);
5939 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005940 my $cast2 = deparenthesize($7);
5941 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005942 my $cast;
5943
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005944 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005945 $cast = "$cast1 or $cast2";
5946 } elsif ($cast1 ne "") {
5947 $cast = $cast1;
5948 } else {
5949 $cast = $cast2;
5950 }
5951 WARN("MINMAX",
5952 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005953 }
5954 }
5955
Joe Perches4a273192012-07-30 14:41:20 -07005956# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07005957 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07005958 defined $stat &&
5959 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5960 my $min = $1;
5961 my $max = $7;
5962 if ($min eq $max) {
5963 WARN("USLEEP_RANGE",
5964 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5965 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5966 $min > $max) {
5967 WARN("USLEEP_RANGE",
5968 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5969 }
5970 }
5971
Joe Perches823b7942013-11-12 15:10:15 -08005972# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07005973 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08005974 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005975 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005976 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5977 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5978 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5979 my $lc = $stat =~ tr@\n@@;
5980 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005981 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08005982 WARN("NAKED_SSCANF",
5983 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5984 }
5985
Joe Perchesafc819a2014-06-04 16:12:08 -07005986# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07005987 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07005988 defined $stat &&
5989 $line =~ /\bsscanf\b/) {
5990 my $lc = $stat =~ tr@\n@@;
5991 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005992 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07005993 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5994 my $format = $6;
5995 my $count = $format =~ tr@%@%@;
5996 if ($count == 1 &&
5997 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5998 WARN("SSCANF_TO_KSTRTO",
5999 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6000 }
6001 }
6002 }
6003
Joe Perches70dc8a42013-09-11 14:23:58 -07006004# check for new externs in .h files.
6005 if ($realfile =~ /\.h$/ &&
6006 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006007 if (CHK("AVOID_EXTERNS",
6008 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006009 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006010 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006011 }
6012 }
6013
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006014# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006015 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006016 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006017 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006018 my $function_name = $1;
6019 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006020
6021 my $s = $stat;
6022 if (defined $cond) {
6023 substr($s, 0, length($cond), '');
6024 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006025 if ($s =~ /^\s*;/ &&
6026 $function_name ne 'uninitialized_var')
6027 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006028 WARN("AVOID_EXTERNS",
6029 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006030 }
6031
6032 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006033 WARN("FUNCTION_ARGUMENTS",
6034 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006035 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006036
6037 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6038 $stat =~ /^.\s*extern\s+/)
6039 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006040 WARN("AVOID_EXTERNS",
6041 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006042 }
6043
Joe Perchesa0ad7592017-07-10 15:52:19 -07006044# check for function declarations that have arguments without identifier names
6045 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006046 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006047 $1 ne "void") {
6048 my $args = trim($1);
6049 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6050 my $arg = trim($1);
6051 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6052 WARN("FUNCTION_ARGUMENTS",
6053 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6054 }
6055 }
6056 }
6057
Joe Perchesa0ad7592017-07-10 15:52:19 -07006058# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006059 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006060 defined $stat &&
6061 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6062 $context_function = $1;
6063
6064# check for multiline function definition with misplaced open brace
6065 my $ok = 0;
6066 my $cnt = statement_rawlines($stat);
6067 my $herectx = $here . "\n";
6068 for (my $n = 0; $n < $cnt; $n++) {
6069 my $rl = raw_line($linenr, $n);
6070 $herectx .= $rl . "\n";
6071 $ok = 1 if ($rl =~ /^[ \+]\{/);
6072 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6073 last if $rl =~ /^[ \+].*\{/;
6074 }
6075 if (!$ok) {
6076 ERROR("OPEN_BRACE",
6077 "open brace '{' following function definitions go on the next line\n" . $herectx);
6078 }
6079 }
6080
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006081# checks for new __setup's
6082 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6083 my $name = $1;
6084
6085 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006086 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006087 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006088 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006089 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006090
6091# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006092 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006093 WARN("UNNECESSARY_CASTS",
6094 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006095 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006096
Joe Perchesa640d252013-07-03 15:05:21 -07006097# alloc style
6098# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006099 if ($perl_version_ok &&
Joe Perchesa640d252013-07-03 15:05:21 -07006100 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6101 CHK("ALLOC_SIZEOF_STRUCT",
6102 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6103 }
6104
Joe Perches60a55362014-06-04 16:12:07 -07006105# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006106 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006107 defined $stat &&
6108 $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 -07006109 my $oldfunc = $3;
6110 my $a1 = $4;
6111 my $a2 = $10;
6112 my $newfunc = "kmalloc_array";
6113 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006114 my $r1 = $a1;
6115 my $r2 = $a2;
6116 if ($a1 =~ /^sizeof\s*\S/) {
6117 $r1 = $a2;
6118 $r2 = $a1;
6119 }
6120 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6121 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006122 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006123 my $herectx = get_stat_here($linenr, $cnt, $here);
6124
Joe Perches60a55362014-06-04 16:12:07 -07006125 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006126 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6127 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006128 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006129 $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 -07006130 }
6131 }
6132 }
6133
Joe Perches972fdea2013-04-29 16:18:12 -07006134# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006135 if ($perl_version_ok &&
Joe Perches972fdea2013-04-29 16:18:12 -07006136 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6137 WARN("KREALLOC_ARG_REUSE",
6138 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6139 }
6140
Joe Perches5ce59ae2013-02-21 16:44:18 -08006141# check for alloc argument mismatch
6142 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6143 WARN("ALLOC_ARRAY_ARGS",
6144 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6145 }
6146
Joe Perchescaf2a542011-01-12 16:59:56 -08006147# check for multiple semicolons
6148 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006149 if (WARN("ONE_SEMICOLON",
6150 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6151 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006152 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006153 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006154 }
6155
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006156# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6157 if ($realfile !~ m@^include/uapi/@ &&
6158 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006159 my $ull = "";
6160 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6161 if (CHK("BIT_MACRO",
6162 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6163 $fix) {
6164 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6165 }
6166 }
6167
Joe Perches2d632742016-05-20 17:04:00 -07006168# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6169 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6170 my $config = $1;
6171 if (WARN("PREFER_IS_ENABLED",
6172 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6173 $fix) {
6174 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6175 }
6176 }
6177
Joe Perchese81f2392014-08-06 16:11:25 -07006178# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006179 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6180 my $has_break = 0;
6181 my $has_statement = 0;
6182 my $count = 0;
6183 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006184 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006185 $prevline--;
6186 my $rline = $rawlines[$prevline - 1];
6187 my $fline = $lines[$prevline - 1];
6188 last if ($fline =~ /^\@\@/);
6189 next if ($fline =~ /^\-/);
6190 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6191 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6192 next if ($fline =~ /^.[\s$;]*$/);
6193 $has_statement = 1;
6194 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006195 $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 -08006196 }
6197 if (!$has_break && $has_statement) {
6198 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006199 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006200 }
6201 }
6202
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006203# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006204 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006205 defined $stat &&
6206 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006207 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006208 my $herectx = get_stat_here($linenr, $cnt, $here);
6209
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006210 WARN("DEFAULT_NO_BREAK",
6211 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006212 }
6213
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006214# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006215 if ($line =~ /\b__FUNCTION__\b/) {
6216 if (WARN("USE_FUNC",
6217 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6218 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006219 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006220 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006221 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006222
Joe Perches62ec8182015-02-13 14:38:18 -08006223# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6224 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6225 ERROR("DATE_TIME",
6226 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6227 }
6228
Joe Perches2c924882012-03-23 15:02:20 -07006229# check for use of yield()
6230 if ($line =~ /\byield\s*\(\s*\)/) {
6231 WARN("YIELD",
6232 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6233 }
6234
Joe Perches179f8f42013-07-03 15:05:30 -07006235# check for comparisons against true and false
6236 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6237 my $lead = $1;
6238 my $arg = $2;
6239 my $test = $3;
6240 my $otype = $4;
6241 my $trail = $5;
6242 my $op = "!";
6243
6244 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6245
6246 my $type = lc($otype);
6247 if ($type =~ /^(?:true|false)$/) {
6248 if (("$test" eq "==" && "$type" eq "true") ||
6249 ("$test" eq "!=" && "$type" eq "false")) {
6250 $op = "";
6251 }
6252
6253 CHK("BOOL_COMPARISON",
6254 "Using comparison to $otype is error prone\n" . $herecurr);
6255
6256## maybe suggesting a correct construct would better
6257## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6258
6259 }
6260 }
6261
Joe Perches5d430902018-04-10 16:34:25 -07006262# check for bool bitfields
6263 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6264 WARN("BOOL_BITFIELD",
6265 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6266 }
6267
Joe Perchesd7295932018-08-21 21:57:26 -07006268# check for bool use in .h files
6269 if ($realfile =~ /\.h$/ &&
6270 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6271 CHK("BOOL_MEMBER",
6272 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6273 }
6274
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006275# check for semaphores initialized locked
6276 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006277 WARN("CONSIDER_COMPLETION",
6278 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006279 }
Joe Perches6712d852012-03-23 15:02:20 -07006280
Joe Perches67d0a072011-10-31 17:13:10 -07006281# recommend kstrto* over simple_strto* and strict_strto*
6282 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006283 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006284 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006285 }
Joe Perches6712d852012-03-23 15:02:20 -07006286
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006287# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006288 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006289 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006290 "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 -07006291 }
Joe Perches6712d852012-03-23 15:02:20 -07006292
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006293# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006294# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006295 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006296 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006297 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006298 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006299 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006300
6301# use of NR_CPUS is usually wrong
6302# ignore definitions of NR_CPUS and usage to define arrays as likely right
6303 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006304 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6305 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006306 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6307 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6308 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006309 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006310 WARN("NR_CPUS",
6311 "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 -07006312 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006313
Joe Perches52ea8502013-11-12 15:10:09 -08006314# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6315 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6316 ERROR("DEFINE_ARCH_HAS",
6317 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6318 }
6319
Joe Perchesacd93622015-02-13 14:38:38 -08006320# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006321 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006322 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6323 WARN("LIKELY_MISUSE",
6324 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6325 }
6326
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006327# whine mightly about in_atomic
6328 if ($line =~ /\bin_atomic\s*\(/) {
6329 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006330 ERROR("IN_ATOMIC",
6331 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006332 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006333 WARN("IN_ATOMIC",
6334 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006335 }
6336 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006337
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006338# check for mutex_trylock_recursive usage
6339 if ($line =~ /mutex_trylock_recursive/) {
6340 ERROR("LOCKING",
6341 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6342 }
6343
Peter Zijlstra1704f472010-03-19 01:37:42 +01006344# check for lockdep_set_novalidate_class
6345 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6346 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6347 if ($realfile !~ m@^kernel/lockdep@ &&
6348 $realfile !~ m@^include/linux/lockdep@ &&
6349 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006350 ERROR("LOCKDEP",
6351 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006352 }
6353 }
Dave Jones88f88312011-01-12 16:59:59 -08006354
Joe Perchesb392c642015-04-16 12:44:16 -07006355 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6356 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006357 WARN("EXPORTED_WORLD_WRITABLE",
6358 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006359 }
Joe Perches24358802014-04-03 14:49:13 -07006360
Joe Perches00180462018-02-06 15:38:55 -08006361# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6362# and whether or not function naming is typical and if
6363# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006364 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006365 defined $stat &&
6366 $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*\)/) {
6367 my $var = $1;
6368 my $perms = $2;
6369 my $show = $3;
6370 my $store = $4;
6371 my $octal_perms = perms_to_octal($perms);
6372 if ($show =~ /^${var}_show$/ &&
6373 $store =~ /^${var}_store$/ &&
6374 $octal_perms eq "0644") {
6375 if (WARN("DEVICE_ATTR_RW",
6376 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6377 $fix) {
6378 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6379 }
6380 } elsif ($show =~ /^${var}_show$/ &&
6381 $store =~ /^NULL$/ &&
6382 $octal_perms eq "0444") {
6383 if (WARN("DEVICE_ATTR_RO",
6384 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6385 $fix) {
6386 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6387 }
6388 } elsif ($show =~ /^NULL$/ &&
6389 $store =~ /^${var}_store$/ &&
6390 $octal_perms eq "0200") {
6391 if (WARN("DEVICE_ATTR_WO",
6392 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6393 $fix) {
6394 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6395 }
6396 } elsif ($octal_perms eq "0644" ||
6397 $octal_perms eq "0444" ||
6398 $octal_perms eq "0200") {
6399 my $newshow = "$show";
6400 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6401 my $newstore = $store;
6402 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6403 my $rename = "";
6404 if ($show ne $newshow) {
6405 $rename .= " '$show' to '$newshow'";
6406 }
6407 if ($store ne $newstore) {
6408 $rename .= " '$store' to '$newstore'";
6409 }
6410 WARN("DEVICE_ATTR_FUNCTIONS",
6411 "Consider renaming function(s)$rename\n" . $herecurr);
6412 } else {
6413 WARN("DEVICE_ATTR_PERMS",
6414 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6415 }
6416 }
6417
Joe Perches515a2352014-04-03 14:49:24 -07006418# Mode permission misuses where it seems decimal should be octal
6419# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006420# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6421# specific definition of not visible in sysfs.
6422# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6423# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006424 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006425 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006426 $line =~ /$mode_perms_search/) {
6427 foreach my $entry (@mode_permission_funcs) {
6428 my $func = $entry->[0];
6429 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006430
Joe Perches459cf0a2016-10-11 13:52:19 -07006431 my $lc = $stat =~ tr@\n@@;
6432 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006433 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006434
Joe Perches515a2352014-04-03 14:49:24 -07006435 my $skip_args = "";
6436 if ($arg_pos > 1) {
6437 $arg_pos--;
6438 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6439 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006440 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006441 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006442 my $val = $1;
6443 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006444 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6445 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6446 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006447 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006448 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006449 }
6450 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006451 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006452 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006453 }
Joe Perches24358802014-04-03 14:49:13 -07006454 }
6455 }
6456 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006457
Joe Perches459cf0a2016-10-11 13:52:19 -07006458# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006459 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006460 my $oval = $1;
6461 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006462 if (WARN("SYMBOLIC_PERMS",
6463 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6464 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006465 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006466 }
6467 }
6468
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006469# validate content of MODULE_LICENSE against list from include/linux/module.h
6470 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6471 my $extracted_string = get_quoted_string($line, $rawline);
6472 my $valid_licenses = qr{
6473 GPL|
6474 GPL\ v2|
6475 GPL\ and\ additional\ rights|
6476 Dual\ BSD/GPL|
6477 Dual\ MIT/GPL|
6478 Dual\ MPL/GPL|
6479 Proprietary
6480 }x;
6481 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6482 WARN("MODULE_LICENSE",
6483 "unknown module license " . $extracted_string . "\n" . $herecurr);
6484 }
6485 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006486 }
6487
6488 # If we have no input at all, then there is nothing to report on
6489 # so just keep quiet.
6490 if ($#rawlines == -1) {
6491 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006492 }
6493
Andy Whitcroft8905a672007-11-28 16:21:06 -08006494 # In mailback mode only produce a report in the negative, for
6495 # things that appear to be patches.
6496 if ($mailback && ($clean == 1 || !$is_patch)) {
6497 exit(0);
6498 }
6499
6500 # This is not a patch, and we are are in 'no-patch' mode so
6501 # just keep quiet.
6502 if (!$chk_patch && !$is_patch) {
6503 exit(0);
6504 }
6505
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006506 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006507 ERROR("NOT_UNIFIED_DIFF",
6508 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006509 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006510 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006511 ERROR("MISSING_SIGN_OFF",
6512 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006513 }
6514
Andy Whitcroft8905a672007-11-28 16:21:06 -08006515 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006516 if ($summary && !($clean == 1 && $quiet == 1)) {
6517 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006518 print "total: $cnt_error errors, $cnt_warn warnings, " .
6519 (($check)? "$cnt_chk checks, " : "") .
6520 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006521 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006522
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006523 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006524 # If there were any defects found and not already fixing them
6525 if (!$clean and !$fix) {
6526 print << "EOM"
6527
6528NOTE: For some of the reported defects, checkpatch may be able to
6529 mechanically convert to the typical style using --fix or --fix-inplace.
6530EOM
6531 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006532 # If there were whitespace errors which cleanpatch can fix
6533 # then suggest that.
6534 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006535 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006536 print << "EOM"
6537
6538NOTE: Whitespace errors detected.
6539 You may wish to use scripts/cleanpatch or scripts/cleanfile
6540EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006541 }
6542 }
6543
Joe Perchesd752fcc2014-08-06 16:11:05 -07006544 if ($clean == 0 && $fix &&
6545 ("@rawlines" ne "@fixed" ||
6546 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006547 my $newfile = $filename;
6548 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006549 my $linecount = 0;
6550 my $f;
6551
Joe Perchesd752fcc2014-08-06 16:11:05 -07006552 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6553
Joe Perches3705ce52013-07-03 15:05:31 -07006554 open($f, '>', $newfile)
6555 or die "$P: Can't open $newfile for write\n";
6556 foreach my $fixed_line (@fixed) {
6557 $linecount++;
6558 if ($file) {
6559 if ($linecount > 3) {
6560 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006561 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006562 }
6563 } else {
6564 print $f $fixed_line . "\n";
6565 }
6566 }
6567 close($f);
6568
6569 if (!$quiet) {
6570 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006571
Joe Perches3705ce52013-07-03 15:05:31 -07006572Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6573
6574Do _NOT_ trust the results written to this file.
6575Do _NOT_ submit these changes without inspecting them for correctness.
6576
6577This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6578No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006579EOM
6580 }
6581 }
6582
Joe Perchesd8469f12015-06-25 15:03:00 -07006583 if ($quiet == 0) {
6584 print "\n";
6585 if ($clean == 1) {
6586 print "$vname has no obvious style problems and is ready for submission.\n";
6587 } else {
6588 print "$vname has style problems, please review.\n";
6589 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006590 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006591 return $clean;
6592}