blob: 93e84c9504a15f526d1722426105377efa13330d [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046my %debug;
Joe Perches34456862013-07-03 15:05:34 -070047my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070048my %use_type = ();
49my @use = ();
50my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070051my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070052my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070053my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080054my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070057my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070058my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070059my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070060my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070061my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070062my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070063my $color = "auto";
Joe Perchesdadf6802016-08-02 14:04:33 -070064my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070065
66sub help {
67 my ($exitcode) = @_;
68
69 print << "EOM";
70Usage: $P [OPTION]... [FILE]...
71Version: $V
72
73Options:
74 -q, --quiet quiet
75 --no-tree run without a kernel tree
76 --no-signoff do not check for 'Signed-off-by' line
77 --patch treat FILE as patchfile (default)
78 --emacs emacs compile window format
79 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070080 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070081 -g, --git treat FILE as a single commit or git revision range
82 single git commit with:
83 <rev>
84 <rev>^
85 <rev>~n
86 multiple git commits with:
87 <rev1>..<rev2>
88 <rev1>...<rev2>
89 <rev>-<count>
90 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070091 -f, --file treat FILE as regular source file
92 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070093 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070094 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070095 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070096 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080097 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070098 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -070099 --root=PATH PATH to the kernel tree root
100 --no-summary suppress the per-file summary
101 --mailback only produce a report in case of warnings/errors
102 --summary-file include the filename in summary
103 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
104 'values', 'possible', 'type', and 'attr' (default
105 is all off)
106 --test-only=WORD report only warnings/errors containing WORD
107 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700108 --fix EXPERIMENTAL - may create horrible results
109 If correctable single-line errors exist, create
110 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
111 with potential errors corrected to the preferred
112 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800113 --fix-inplace EXPERIMENTAL - may create horrible results
114 Is the same as --fix, but overwrites the input
115 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700116 --ignore-perl-version override checking of perl version. expect
117 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700118 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700119 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700120 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700121 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700122 --color[=WHEN] Use colors 'always', 'never', or only when output
123 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700124 -h, --help, --version display this help and exit
125
126When FILE is - read standard input.
127EOM
128
129 exit($exitcode);
130}
131
Joe Perches3beb42e2016-05-20 17:04:14 -0700132sub uniq {
133 my %seen;
134 return grep { !$seen{$_}++ } @_;
135}
136
137sub list_types {
138 my ($exitcode) = @_;
139
140 my $count = 0;
141
142 local $/ = undef;
143
144 open(my $script, '<', abs_path($P)) or
145 die "$P: Can't read '$P' $!\n";
146
147 my $text = <$script>;
148 close($script);
149
150 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700151 # Also catch when type or level is passed through a variable
152 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700153 push (@types, $_);
154 }
155 @types = sort(uniq(@types));
156 print("#\tMessage type\n\n");
157 foreach my $type (@types) {
158 print(++$count . "\t" . $type . "\n");
159 }
160
161 exit($exitcode);
162}
163
Joe Perches000d1cc12011-07-25 17:13:25 -0700164my $conf = which_conf($configuration_file);
165if (-f $conf) {
166 my @conf_args;
167 open(my $conffile, '<', "$conf")
168 or warn "$P: Can't find a readable $configuration_file file $!\n";
169
170 while (<$conffile>) {
171 my $line = $_;
172
173 $line =~ s/\s*\n?$//g;
174 $line =~ s/^\s*//g;
175 $line =~ s/\s+/ /g;
176
177 next if ($line =~ m/^\s*#/);
178 next if ($line =~ m/^\s*$/);
179
180 my @words = split(" ", $line);
181 foreach my $word (@words) {
182 last if ($word =~ m/^#/);
183 push (@conf_args, $word);
184 }
185 }
186 close($conffile);
187 unshift(@ARGV, @conf_args) if @conf_args;
188}
189
John Brooks737c0762017-07-10 15:52:24 -0700190# Perl's Getopt::Long allows options to take optional arguments after a space.
191# Prevent --color by itself from consuming other arguments
192foreach (@ARGV) {
193 if ($_ eq "--color" || $_ eq "-color") {
194 $_ = "--color=$color";
195 }
196}
197
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700198GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700199 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700200 'tree!' => \$tree,
201 'signoff!' => \$chk_signoff,
202 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700203 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800204 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700205 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700206 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700207 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208 'subjective!' => \$check,
209 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700210 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700211 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700212 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700213 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800214 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700215 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700216 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800217 'summary!' => \$summary,
218 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800219 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700220 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800221 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700222 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800223 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700224 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700225 'codespell!' => \$codespell,
226 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700227 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700228 'color=s' => \$color,
229 'no-color' => \$color, #keep old behaviors of -nocolor
230 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700231 'h|help' => \$help,
232 'version' => \$help
233) or help(1);
234
235help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700236
Joe Perches3beb42e2016-05-20 17:04:14 -0700237list_types(0) if ($list_types);
238
Joe Perches9624b8d2014-01-23 15:54:44 -0800239$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700240$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800241
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700242my $exit = 0;
243
Joe Perches5b579802018-08-21 21:57:33 -0700244my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700245if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700246 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700247 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700248 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700249}
250
Allen Hubbe45107ff2016-08-02 14:04:47 -0700251#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700252if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700253 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700254}
255
John Brooks737c0762017-07-10 15:52:24 -0700256if ($color =~ /^[01]$/) {
257 $color = !$color;
258} elsif ($color =~ /^always$/i) {
259 $color = 1;
260} elsif ($color =~ /^never$/i) {
261 $color = 0;
262} elsif ($color =~ /^auto$/i) {
263 $color = (-t STDOUT);
264} else {
265 die "Invalid color mode: $color\n";
266}
267
Joe Perches91bfe482013-09-11 14:23:59 -0700268sub hash_save_array_words {
269 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700270
Joe Perches91bfe482013-09-11 14:23:59 -0700271 my @array = split(/,/, join(',', @$arrayRef));
272 foreach my $word (@array) {
273 $word =~ s/\s*\n?$//g;
274 $word =~ s/^\s*//g;
275 $word =~ s/\s+/ /g;
276 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700277
Joe Perches91bfe482013-09-11 14:23:59 -0700278 next if ($word =~ m/^\s*#/);
279 next if ($word =~ m/^\s*$/);
280
281 $hashRef->{$word}++;
282 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700283}
284
Joe Perches91bfe482013-09-11 14:23:59 -0700285sub hash_show_words {
286 my ($hashRef, $prefix) = @_;
287
Joe Perches3c816e42015-06-25 15:03:29 -0700288 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700289 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700290 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700291 print " $word";
292 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700293 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700294 }
295}
296
297hash_save_array_words(\%ignore_type, \@ignore);
298hash_save_array_words(\%use_type, \@use);
299
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800300my $dbg_values = 0;
301my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700302my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700303my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800304for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800305 ## no critic
306 eval "\${dbg_$key} = '$debug{$key}';";
307 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800308}
309
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700310my $rpt_cleaners = 0;
311
Andy Whitcroft8905a672007-11-28 16:21:06 -0800312if ($terse) {
313 $emacs = 1;
314 $quiet++;
315}
316
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700317if ($tree) {
318 if (defined $root) {
319 if (!top_of_kernel_tree($root)) {
320 die "$P: $root: --root does not point at a valid tree\n";
321 }
322 } else {
323 if (top_of_kernel_tree('.')) {
324 $root = '.';
325 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
326 top_of_kernel_tree($1)) {
327 $root = $1;
328 }
329 }
330
331 if (!defined $root) {
332 print "Must be run from the top-level dir. of a kernel tree\n";
333 exit(2);
334 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700335}
336
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700337my $emitted_corrupt = 0;
338
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700339our $Ident = qr{
340 [A-Za-z_][A-Za-z\d_]*
341 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
342 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700343our $Storage = qr{extern|static|asmlinkage};
344our $Sparse = qr{
345 __user|
346 __kernel|
347 __force|
348 __iomem|
349 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800350 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700351 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700352 __refconst|
353 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800354 __rcu|
355 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700356 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800357our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
358our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
359our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
360our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
361our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700362
Wolfram Sang52131292010-03-05 13:43:51 -0800363# Notes to $Attribute:
364# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700365our $Attribute = qr{
366 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700367 __percpu|
368 __nocast|
369 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200370 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700371 __packed__|
372 __packed2__|
373 __naked|
374 __maybe_unused|
375 __always_unused|
376 __noreturn|
377 __used|
378 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800379 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700380 __noclone|
381 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700382 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700383 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700384 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700385 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700386 ____cacheline_aligned|
387 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800388 ____cacheline_internodealigned_in_smp|
389 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700390 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700391our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700392our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700393our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
394our $Lval = qr{$Ident(?:$Member)*};
395
Joe Perches95e2c602013-07-03 15:05:20 -0700396our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
397our $Binary = qr{(?i)0b[01]+$Int_type?};
398our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
399our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700400our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800401our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800402our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
403our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
404our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800405our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700406our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800407our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700408our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700409our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700410our $Operators = qr{
411 <=|>=|==|!=|
412 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700413 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700414 }x;
415
Joe Perches91cb5192014-04-03 14:49:32 -0700416our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
417
Joe Perchesab7e23f2015-04-16 12:44:22 -0700418our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800419our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700420our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700421our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800422our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700423our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800424our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700425our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800426
Joe Perches15662b32011-10-31 17:13:12 -0700427our $NON_ASCII_UTF8 = qr{
428 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700429 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
430 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
431 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
432 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
433 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
434 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
435}x;
436
Joe Perches15662b32011-10-31 17:13:12 -0700437our $UTF8 = qr{
438 [\x09\x0A\x0D\x20-\x7E] # ASCII
439 | $NON_ASCII_UTF8
440}x;
441
Joe Perchese6176fa2015-06-25 15:02:49 -0700442our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800443our $typeOtherOSTypedefs = qr{(?x:
444 u_(?:char|short|int|long) | # bsd
445 u(?:nchar|short|int|long) # sysv
446)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700447our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700448 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700449 atomic_t
450)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700451our $typeTypedefs = qr{(?x:
452 $typeC99Typedefs\b|
453 $typeOtherOSTypedefs\b|
454 $typeKernelTypedefs\b
455)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700456
Joe Perches6d32f7a2015-11-06 16:31:37 -0800457our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
458
Joe Perches691e6692010-03-05 13:43:51 -0800459our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800460 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700461 (?:[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 -0800462 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700463 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700464 panic|
Joe Perches06668722013-11-12 15:10:07 -0800465 MODULE_[A-Z_]+|
466 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800467)};
468
Joe Perches20112472011-07-25 17:13:23 -0700469our $signature_tags = qr{(?xi:
470 Signed-off-by:|
471 Acked-by:|
472 Tested-by:|
473 Reviewed-by:|
474 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700475 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700476 To:|
477 Cc:
478)};
479
Joe Perches18130872014-08-06 16:11:22 -0700480our @typeListMisordered = (
481 qr{char\s+(?:un)?signed},
482 qr{int\s+(?:(?:un)?signed\s+)?short\s},
483 qr{int\s+short(?:\s+(?:un)?signed)},
484 qr{short\s+int(?:\s+(?:un)?signed)},
485 qr{(?:un)?signed\s+int\s+short},
486 qr{short\s+(?:un)?signed},
487 qr{long\s+int\s+(?:un)?signed},
488 qr{int\s+long\s+(?:un)?signed},
489 qr{long\s+(?:un)?signed\s+int},
490 qr{int\s+(?:un)?signed\s+long},
491 qr{int\s+(?:un)?signed},
492 qr{int\s+long\s+long\s+(?:un)?signed},
493 qr{long\s+long\s+int\s+(?:un)?signed},
494 qr{long\s+long\s+(?:un)?signed\s+int},
495 qr{long\s+long\s+(?:un)?signed},
496 qr{long\s+(?:un)?signed},
497);
498
Andy Whitcroft8905a672007-11-28 16:21:06 -0800499our @typeList = (
500 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700501 qr{(?:(?:un)?signed\s+)?char},
502 qr{(?:(?:un)?signed\s+)?short\s+int},
503 qr{(?:(?:un)?signed\s+)?short},
504 qr{(?:(?:un)?signed\s+)?int},
505 qr{(?:(?:un)?signed\s+)?long\s+int},
506 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
507 qr{(?:(?:un)?signed\s+)?long\s+long},
508 qr{(?:(?:un)?signed\s+)?long},
509 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800510 qr{float},
511 qr{double},
512 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800513 qr{struct\s+$Ident},
514 qr{union\s+$Ident},
515 qr{enum\s+$Ident},
516 qr{${Ident}_t},
517 qr{${Ident}_handler},
518 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700519 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800520);
Joe Perches938224b2016-01-20 14:59:15 -0800521
522our $C90_int_types = qr{(?x:
523 long\s+long\s+int\s+(?:un)?signed|
524 long\s+long\s+(?:un)?signed\s+int|
525 long\s+long\s+(?:un)?signed|
526 (?:(?:un)?signed\s+)?long\s+long\s+int|
527 (?:(?:un)?signed\s+)?long\s+long|
528 int\s+long\s+long\s+(?:un)?signed|
529 int\s+(?:(?:un)?signed\s+)?long\s+long|
530
531 long\s+int\s+(?:un)?signed|
532 long\s+(?:un)?signed\s+int|
533 long\s+(?:un)?signed|
534 (?:(?:un)?signed\s+)?long\s+int|
535 (?:(?:un)?signed\s+)?long|
536 int\s+long\s+(?:un)?signed|
537 int\s+(?:(?:un)?signed\s+)?long|
538
539 int\s+(?:un)?signed|
540 (?:(?:un)?signed\s+)?int
541)};
542
Alex Dowad485ff232015-06-25 15:02:52 -0700543our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700544our @typeListWithAttr = (
545 @typeList,
546 qr{struct\s+$InitAttribute\s+$Ident},
547 qr{union\s+$InitAttribute\s+$Ident},
548);
549
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700550our @modifierList = (
551 qr{fastcall},
552);
Alex Dowad485ff232015-06-25 15:02:52 -0700553our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800554
Joe Perches24358802014-04-03 14:49:13 -0700555our @mode_permission_funcs = (
556 ["module_param", 3],
557 ["module_param_(?:array|named|string)", 4],
558 ["module_param_array_named", 5],
559 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
560 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700561 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
562 ["IIO_DEV_ATTR_[A-Z_]+", 1],
563 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
564 ["SENSOR_TEMPLATE(?:_2|)", 3],
565 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700566);
567
Joe Perches515a2352014-04-03 14:49:24 -0700568#Create a search pattern for all these functions to speed up a loop below
569our $mode_perms_search = "";
570foreach my $entry (@mode_permission_funcs) {
571 $mode_perms_search .= '|' if ($mode_perms_search ne "");
572 $mode_perms_search .= $entry->[0];
573}
Joe Perches00180462018-02-06 15:38:55 -0800574$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700575
Joe Perches9189c7e2018-09-07 15:26:18 -0700576our %deprecated_apis = (
577 "synchronize_rcu_bh" => "synchronize_rcu",
578 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
579 "call_rcu_bh" => "call_rcu",
580 "rcu_barrier_bh" => "rcu_barrier",
581 "synchronize_sched" => "synchronize_rcu",
582 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
583 "call_rcu_sched" => "call_rcu",
584 "rcu_barrier_sched" => "rcu_barrier",
585 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
586 "cond_synchronize_sched" => "cond_synchronize_rcu",
587);
588
589#Create a search pattern for all these strings to speed up a loop below
590our $deprecated_apis_search = "";
591foreach my $entry (keys %deprecated_apis) {
592 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
593 $deprecated_apis_search .= $entry;
594}
595$deprecated_apis_search = "(?:${deprecated_apis_search})";
596
Joe Perchesb392c642015-04-16 12:44:16 -0700597our $mode_perms_world_writable = qr{
598 S_IWUGO |
599 S_IWOTH |
600 S_IRWXUGO |
601 S_IALLUGO |
602 0[0-7][0-7][2367]
603}x;
604
Joe Perchesf90774e2016-10-11 13:51:47 -0700605our %mode_permission_string_types = (
606 "S_IRWXU" => 0700,
607 "S_IRUSR" => 0400,
608 "S_IWUSR" => 0200,
609 "S_IXUSR" => 0100,
610 "S_IRWXG" => 0070,
611 "S_IRGRP" => 0040,
612 "S_IWGRP" => 0020,
613 "S_IXGRP" => 0010,
614 "S_IRWXO" => 0007,
615 "S_IROTH" => 0004,
616 "S_IWOTH" => 0002,
617 "S_IXOTH" => 0001,
618 "S_IRWXUGO" => 0777,
619 "S_IRUGO" => 0444,
620 "S_IWUGO" => 0222,
621 "S_IXUGO" => 0111,
622);
623
624#Create a search pattern for all these strings to speed up a loop below
625our $mode_perms_string_search = "";
626foreach my $entry (keys %mode_permission_string_types) {
627 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
628 $mode_perms_string_search .= $entry;
629}
Joe Perches00180462018-02-06 15:38:55 -0800630our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
631our $multi_mode_perms_string_search = qr{
632 ${single_mode_perms_string_search}
633 (?:\s*\|\s*${single_mode_perms_string_search})*
634}x;
635
636sub perms_to_octal {
637 my ($string) = @_;
638
639 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
640
641 my $val = "";
642 my $oval = "";
643 my $to = 0;
644 my $curpos = 0;
645 my $lastpos = 0;
646 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
647 $curpos = pos($string);
648 my $match = $2;
649 my $omatch = $1;
650 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
651 $lastpos = $curpos;
652 $to |= $mode_permission_string_types{$match};
653 $val .= '\s*\|\s*' if ($val ne "");
654 $val .= $match;
655 $oval .= $omatch;
656 }
657 $oval =~ s/^\s*\|\s*//;
658 $oval =~ s/\s*\|\s*$//;
659 return sprintf("%04o", $to);
660}
Joe Perchesf90774e2016-10-11 13:51:47 -0700661
Wolfram Sang7840a942010-08-09 17:20:57 -0700662our $allowed_asm_includes = qr{(?x:
663 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700664 memory|
665 time|
666 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700667)};
668# memory.h: ARM has a custom one
669
Kees Cook66b47b42014-10-13 15:51:57 -0700670# Load common spelling mistakes and build regular expression list.
671my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700672my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700673
Joe Perches36061e32014-12-10 15:51:43 -0800674if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800675 while (<$spelling>) {
676 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700677
Joe Perches36061e32014-12-10 15:51:43 -0800678 $line =~ s/\s*\n?$//g;
679 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700680
Joe Perches36061e32014-12-10 15:51:43 -0800681 next if ($line =~ m/^\s*#/);
682 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700683
Joe Perches36061e32014-12-10 15:51:43 -0800684 my ($suspect, $fix) = split(/\|\|/, $line);
685
Joe Perches36061e32014-12-10 15:51:43 -0800686 $spelling_fix{$suspect} = $fix;
687 }
688 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800689} else {
690 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700691}
Kees Cook66b47b42014-10-13 15:51:57 -0700692
Joe Perchesebfd7d62015-04-16 12:44:14 -0700693if ($codespell) {
694 if (open(my $spelling, '<', $codespellfile)) {
695 while (<$spelling>) {
696 my $line = $_;
697
698 $line =~ s/\s*\n?$//g;
699 $line =~ s/^\s*//g;
700
701 next if ($line =~ m/^\s*#/);
702 next if ($line =~ m/^\s*$/);
703 next if ($line =~ m/, disabled/i);
704
705 $line =~ s/,.*$//;
706
707 my ($suspect, $fix) = split(/->/, $line);
708
709 $spelling_fix{$suspect} = $fix;
710 }
711 close($spelling);
712 } else {
713 warn "No codespell typos will be found - file '$codespellfile': $!\n";
714 }
715}
716
717$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
718
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700719sub read_words {
720 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700721
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700722 if (open(my $words, '<', $file)) {
723 while (<$words>) {
724 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700725
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700726 $line =~ s/\s*\n?$//g;
727 $line =~ s/^\s*//g;
728
729 next if ($line =~ m/^\s*#/);
730 next if ($line =~ m/^\s*$/);
731 if ($line =~ /\s/) {
732 print("$file: '$line' invalid - ignored\n");
733 next;
734 }
735
736 $$wordsRef .= '|' if ($$wordsRef ne "");
737 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700738 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700739 close($file);
740 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700741 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700742
743 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700744}
745
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700746my $const_structs = "";
747read_words(\$const_structs, $conststructsfile)
748 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
749
750my $typeOtherTypedefs = "";
751if (length($typedefsfile)) {
752 read_words(\$typeOtherTypedefs, $typedefsfile)
753 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
754}
755$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
756
Andy Whitcroft8905a672007-11-28 16:21:06 -0800757sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700758 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
759 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700760 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700761 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700762 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700763 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700764 (?:$typeTypedefs\b)|
765 (?:${all}\b)
766 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800767 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700768 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800769 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800770 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700771 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700772 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800773 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700774 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800775 }x;
Joe Perches18130872014-08-06 16:11:22 -0700776 $NonptrTypeMisordered = qr{
777 (?:$Modifier\s+|const\s+)*
778 (?:
779 (?:${Misordered}\b)
780 )
781 (?:\s+$Modifier|\s+const)*
782 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700783 $NonptrTypeWithAttr = qr{
784 (?:$Modifier\s+|const\s+)*
785 (?:
786 (?:typeof|__typeof__)\s*\([^\)]*\)|
787 (?:$typeTypedefs\b)|
788 (?:${allWithAttr}\b)
789 )
790 (?:\s+$Modifier|\s+const)*
791 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800792 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700793 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700794 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700795 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800796 }x;
Joe Perches18130872014-08-06 16:11:22 -0700797 $TypeMisordered = qr{
798 $NonptrTypeMisordered
799 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
800 (?:\s+$Inline|\s+$Modifier)*
801 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700802 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700803 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800804}
805build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700806
Joe Perches7d2367a2011-07-25 17:13:22 -0700807our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700808
809# Using $balanced_parens, $LvalOrFunc, or $FuncArg
810# requires at least perl version v5.10.0
811# Any use must be runtime checked with $^V
812
813our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700814our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800815our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700816
Joe Perchesf8422302014-08-06 16:11:31 -0700817our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700818 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700819 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700820 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
821 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700822)};
823
Joe Perches7d2367a2011-07-25 17:13:22 -0700824sub deparenthesize {
825 my ($string) = @_;
826 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700827
828 while ($string =~ /^\s*\(.*\)\s*$/) {
829 $string =~ s@^\s*\(\s*@@;
830 $string =~ s@\s*\)\s*$@@;
831 }
832
Joe Perches7d2367a2011-07-25 17:13:22 -0700833 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700834
Joe Perches7d2367a2011-07-25 17:13:22 -0700835 return $string;
836}
837
Joe Perches34456862013-07-03 15:05:34 -0700838sub seed_camelcase_file {
839 my ($file) = @_;
840
841 return if (!(-f $file));
842
843 local $/;
844
845 open(my $include_file, '<', "$file")
846 or warn "$P: Can't read '$file' $!\n";
847 my $text = <$include_file>;
848 close($include_file);
849
850 my @lines = split('\n', $text);
851
852 foreach my $line (@lines) {
853 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
854 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
855 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800856 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
857 $camelcase{$1} = 1;
858 } 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 -0700859 $camelcase{$1} = 1;
860 }
861 }
862}
863
Joe Perches85b0ee12016-10-11 13:51:44 -0700864sub is_maintained_obsolete {
865 my ($filename) = @_;
866
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800867 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700868
Joe Perches0616efa2016-10-11 13:52:02 -0700869 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 -0700870
871 return $status =~ /obsolete/i;
872}
873
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700874sub is_SPDX_License_valid {
875 my ($license) = @_;
876
Joe Perches56294112018-08-21 21:58:04 -0700877 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700878
Joe Perches56294112018-08-21 21:58:04 -0700879 my $root_path = abs_path($root);
880 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700881 return 0 if ($status ne "");
882 return 1;
883}
884
Joe Perches34456862013-07-03 15:05:34 -0700885my $camelcase_seeded = 0;
886sub seed_camelcase_includes {
887 return if ($camelcase_seeded);
888
889 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700890 my $camelcase_cache = "";
891 my @include_files = ();
892
893 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700894
Richard Genoud3645e322014-02-10 14:25:32 -0800895 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700896 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
897 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700898 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700899 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700900 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700901 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700902 @include_files = split('\n', $files);
903 foreach my $file (@include_files) {
904 my $date = POSIX::strftime("%Y%m%d%H%M",
905 localtime((stat $file)[9]));
906 $last_mod_date = $date if ($last_mod_date < $date);
907 }
908 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700909 }
Joe Perchesc707a812013-07-08 16:00:43 -0700910
911 if ($camelcase_cache ne "" && -f $camelcase_cache) {
912 open(my $camelcase_file, '<', "$camelcase_cache")
913 or warn "$P: Can't read '$camelcase_cache' $!\n";
914 while (<$camelcase_file>) {
915 chomp;
916 $camelcase{$_} = 1;
917 }
918 close($camelcase_file);
919
920 return;
921 }
922
Richard Genoud3645e322014-02-10 14:25:32 -0800923 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700924 $files = `git ls-files "include/*.h"`;
925 @include_files = split('\n', $files);
926 }
927
Joe Perches34456862013-07-03 15:05:34 -0700928 foreach my $file (@include_files) {
929 seed_camelcase_file($file);
930 }
Joe Perches351b2a12013-07-03 15:05:36 -0700931
Joe Perchesc707a812013-07-08 16:00:43 -0700932 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700933 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700934 open(my $camelcase_file, '>', "$camelcase_cache")
935 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700936 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
937 print $camelcase_file ("$_\n");
938 }
939 close($camelcase_file);
940 }
Joe Perches34456862013-07-03 15:05:34 -0700941}
942
Joe Perchesd311cd42014-08-06 16:10:57 -0700943sub git_commit_info {
944 my ($commit, $id, $desc) = @_;
945
946 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
947
948 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
949 $output =~ s/^\s*//gm;
950 my @lines = split("\n", $output);
951
Joe Perches0d7835f2015-02-13 14:38:35 -0800952 return ($id, $desc) if ($#lines < 0);
953
Joe Perchesd311cd42014-08-06 16:10:57 -0700954 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
955# Maybe one day convert this block of bash into something that returns
956# all matching commit ids, but it's very slow...
957#
958# echo "checking commits $1..."
959# git rev-list --remotes | grep -i "^$1" |
960# while read line ; do
961# git log --format='%H %s' -1 $line |
962# echo "commit $(cut -c 1-12,41-)"
963# done
964 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700965 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700966 } else {
967 $id = substr($lines[0], 0, 12);
968 $desc = substr($lines[0], 41);
969 }
970
971 return ($id, $desc);
972}
973
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700974$chk_signoff = 0 if ($file);
975
Andy Whitcroft00df3442007-06-08 13:47:06 -0700976my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800977my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700978my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700979my @fixed_inserted = ();
980my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700981my $fixlinenr = -1;
982
Du, Changbin4a593c32016-05-20 17:04:16 -0700983# If input is git commits, extract all commits from the commit expressions.
984# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
985die "$P: No git repository found\n" if ($git && !-e ".git");
986
987if ($git) {
988 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700989 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700990 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700991 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
992 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700993 } elsif ($commit_expr =~ m/\.\./) {
994 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700995 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700996 $git_range = "-1 $commit_expr";
997 }
998 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
999 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001000 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1001 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001002 my $sha1 = $1;
1003 my $subject = $2;
1004 unshift(@commits, $sha1);
1005 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001006 }
1007 }
1008 die "$P: no git commits after extraction!\n" if (@commits == 0);
1009 @ARGV = @commits;
1010}
1011
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001012my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001013for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001014 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001015 if ($git) {
1016 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1017 die "$P: $filename: git format-patch failed - $!\n";
1018 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001019 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001020 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001021 } elsif ($filename eq '-') {
1022 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001023 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001024 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001025 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001026 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001027 if ($filename eq '-') {
1028 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001029 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001030 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001031 } else {
1032 $vname = $filename;
1033 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001034 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001035 chomp;
1036 push(@rawlines, $_);
1037 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001038 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001039
1040 if ($#ARGV > 0 && $quiet == 0) {
1041 print '-' x length($vname) . "\n";
1042 print "$vname\n";
1043 print '-' x length($vname) . "\n";
1044 }
1045
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001046 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001047 $exit = 1;
1048 }
1049 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001050 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001051 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001052 @fixed_inserted = ();
1053 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001054 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001055 @modifierListFile = ();
1056 @typeListFile = ();
1057 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001058}
1059
Joe Perchesd8469f12015-06-25 15:03:00 -07001060if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001061 hash_show_words(\%use_type, "Used");
1062 hash_show_words(\%ignore_type, "Ignored");
1063
Joe Perches5b579802018-08-21 21:57:33 -07001064 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001065 print << "EOM"
1066
1067NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001068 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001069EOM
1070 }
1071 if ($exit) {
1072 print << "EOM"
1073
1074NOTE: If any of the errors are false positives, please report
1075 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1076EOM
1077 }
1078}
1079
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001080exit($exit);
1081
1082sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001083 my ($root) = @_;
1084
1085 my @tree_check = (
1086 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1087 "README", "Documentation", "arch", "include", "drivers",
1088 "fs", "init", "ipc", "kernel", "lib", "scripts",
1089 );
1090
1091 foreach my $check (@tree_check) {
1092 if (! -e $root . '/' . $check) {
1093 return 0;
1094 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001095 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001096 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001097}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001098
Joe Perches20112472011-07-25 17:13:23 -07001099sub parse_email {
1100 my ($formatted_email) = @_;
1101
1102 my $name = "";
1103 my $address = "";
1104 my $comment = "";
1105
1106 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1107 $name = $1;
1108 $address = $2;
1109 $comment = $3 if defined $3;
1110 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1111 $address = $1;
1112 $comment = $2 if defined $2;
1113 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1114 $address = $1;
1115 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001116 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001117 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001118 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001119 $name =~ s/^\"|\"$//g;
1120 # If there's a name left after stripping spaces and
1121 # leading quotes, and the address doesn't have both
1122 # leading and trailing angle brackets, the address
1123 # is invalid. ie:
1124 # "joe smith joe@smith.com" bad
1125 # "joe smith <joe@smith.com" bad
1126 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1127 $name = "";
1128 $address = "";
1129 $comment = "";
1130 }
1131 }
1132
Joe Perches3705ce52013-07-03 15:05:31 -07001133 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001134 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001135 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001136 $address =~ s/^\<|\>$//g;
1137
1138 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1139 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1140 $name = "\"$name\"";
1141 }
1142
1143 return ($name, $address, $comment);
1144}
1145
1146sub format_email {
1147 my ($name, $address) = @_;
1148
1149 my $formatted_email;
1150
Joe Perches3705ce52013-07-03 15:05:31 -07001151 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001152 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001153 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001154
1155 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1156 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1157 $name = "\"$name\"";
1158 }
1159
1160 if ("$name" eq "") {
1161 $formatted_email = "$address";
1162 } else {
1163 $formatted_email = "$name <$address>";
1164 }
1165
1166 return $formatted_email;
1167}
1168
Joe Perchesd311cd42014-08-06 16:10:57 -07001169sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001170 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001171
Joe Perchesbd474ca2014-08-06 16:11:10 -07001172 foreach my $path (split(/:/, $ENV{PATH})) {
1173 if (-e "$path/$bin") {
1174 return "$path/$bin";
1175 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001176 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001177
Joe Perchesbd474ca2014-08-06 16:11:10 -07001178 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001179}
1180
Joe Perches000d1cc12011-07-25 17:13:25 -07001181sub which_conf {
1182 my ($conf) = @_;
1183
1184 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1185 if (-e "$path/$conf") {
1186 return "$path/$conf";
1187 }
1188 }
1189
1190 return "";
1191}
1192
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001193sub expand_tabs {
1194 my ($str) = @_;
1195
1196 my $res = '';
1197 my $n = 0;
1198 for my $c (split(//, $str)) {
1199 if ($c eq "\t") {
1200 $res .= ' ';
1201 $n++;
1202 for (; ($n % 8) != 0; $n++) {
1203 $res .= ' ';
1204 }
1205 next;
1206 }
1207 $res .= $c;
1208 $n++;
1209 }
1210
1211 return $res;
1212}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001213sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001214 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001215 return $res;
1216}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001217
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001218sub line_stats {
1219 my ($line) = @_;
1220
1221 # Drop the diff line leader and expand tabs
1222 $line =~ s/^.//;
1223 $line = expand_tabs($line);
1224
1225 # Pick the indent from the front of the line.
1226 my ($white) = ($line =~ /^(\s*)/);
1227
1228 return (length($line), length($white));
1229}
1230
Andy Whitcroft773647a2008-03-28 14:15:58 -07001231my $sanitise_quote = '';
1232
1233sub sanitise_line_reset {
1234 my ($in_comment) = @_;
1235
1236 if ($in_comment) {
1237 $sanitise_quote = '*/';
1238 } else {
1239 $sanitise_quote = '';
1240 }
1241}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001242sub sanitise_line {
1243 my ($line) = @_;
1244
1245 my $res = '';
1246 my $l = '';
1247
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001248 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001249 my $off = 0;
1250 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001251
Andy Whitcroft773647a2008-03-28 14:15:58 -07001252 # Always copy over the diff marker.
1253 $res = substr($line, 0, 1);
1254
1255 for ($off = 1; $off < length($line); $off++) {
1256 $c = substr($line, $off, 1);
1257
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001258 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001259 # and end, all to $;.
1260 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1261 $sanitise_quote = '*/';
1262
1263 substr($res, $off, 2, "$;$;");
1264 $off++;
1265 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001266 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001267 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001268 $sanitise_quote = '';
1269 substr($res, $off, 2, "$;$;");
1270 $off++;
1271 next;
1272 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001273 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1274 $sanitise_quote = '//';
1275
1276 substr($res, $off, 2, $sanitise_quote);
1277 $off++;
1278 next;
1279 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001280
1281 # A \ in a string means ignore the next character.
1282 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1283 $c eq "\\") {
1284 substr($res, $off, 2, 'XX');
1285 $off++;
1286 next;
1287 }
1288 # Regular quotes.
1289 if ($c eq "'" || $c eq '"') {
1290 if ($sanitise_quote eq '') {
1291 $sanitise_quote = $c;
1292
1293 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001294 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001295 } elsif ($sanitise_quote eq $c) {
1296 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001297 }
1298 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001299
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001300 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001301 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1302 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001303 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1304 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001305 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1306 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001307 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001308 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001309 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001310 }
1311
Daniel Walker113f04a2009-09-21 17:04:35 -07001312 if ($sanitise_quote eq '//') {
1313 $sanitise_quote = '';
1314 }
1315
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001316 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001317 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001318 my $clean = 'X' x length($1);
1319 $res =~ s@\<.*\>@<$clean>@;
1320
1321 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001322 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001323 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001324 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001325 }
1326
Joe Perchesdadf6802016-08-02 14:04:33 -07001327 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1328 my $match = $1;
1329 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1330 }
1331
Andy Whitcroft00df3442007-06-08 13:47:06 -07001332 return $res;
1333}
1334
Joe Perchesa6962d72013-04-29 16:18:13 -07001335sub get_quoted_string {
1336 my ($line, $rawline) = @_;
1337
Joe Perches478b1792018-04-10 16:33:34 -07001338 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001339 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001340 return substr($rawline, $-[0], $+[0] - $-[0]);
1341}
1342
Andy Whitcroft8905a672007-11-28 16:21:06 -08001343sub ctx_statement_block {
1344 my ($linenr, $remain, $off) = @_;
1345 my $line = $linenr - 1;
1346 my $blk = '';
1347 my $soff = $off;
1348 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001349 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001350
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001351 my $loff = 0;
1352
Andy Whitcroft8905a672007-11-28 16:21:06 -08001353 my $type = '';
1354 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001355 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001356 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001357 my $c;
1358 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001359
1360 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001361 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001362 @stack = (['', 0]) if ($#stack == -1);
1363
Andy Whitcroft773647a2008-03-28 14:15:58 -07001364 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001365 # If we are about to drop off the end, pull in more
1366 # context.
1367 if ($off >= $len) {
1368 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001369 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001370 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001371 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001372 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001373 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001374 $len = length($blk);
1375 $line++;
1376 last;
1377 }
1378 # Bail if there is no further context.
1379 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001380 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001381 last;
1382 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001383 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1384 $level++;
1385 $type = '#';
1386 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001387 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001388 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001389 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001390 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001391
Andy Whitcroft773647a2008-03-28 14:15:58 -07001392 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001393
1394 # Handle nested #if/#else.
1395 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1396 push(@stack, [ $type, $level ]);
1397 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1398 ($type, $level) = @{$stack[$#stack - 1]};
1399 } elsif ($remainder =~ /^#\s*endif\b/) {
1400 ($type, $level) = @{pop(@stack)};
1401 }
1402
Andy Whitcroft8905a672007-11-28 16:21:06 -08001403 # Statement ends at the ';' or a close '}' at the
1404 # outermost level.
1405 if ($level == 0 && $c eq ';') {
1406 last;
1407 }
1408
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001409 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001410 if ($level == 0 && $coff_set == 0 &&
1411 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1412 $remainder =~ /^(else)(?:\s|{)/ &&
1413 $remainder !~ /^else\s+if\b/) {
1414 $coff = $off + length($1) - 1;
1415 $coff_set = 1;
1416 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1417 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001418 }
1419
Andy Whitcroft8905a672007-11-28 16:21:06 -08001420 if (($type eq '' || $type eq '(') && $c eq '(') {
1421 $level++;
1422 $type = '(';
1423 }
1424 if ($type eq '(' && $c eq ')') {
1425 $level--;
1426 $type = ($level != 0)? '(' : '';
1427
1428 if ($level == 0 && $coff < $soff) {
1429 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001430 $coff_set = 1;
1431 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001432 }
1433 }
1434 if (($type eq '' || $type eq '{') && $c eq '{') {
1435 $level++;
1436 $type = '{';
1437 }
1438 if ($type eq '{' && $c eq '}') {
1439 $level--;
1440 $type = ($level != 0)? '{' : '';
1441
1442 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001443 if (substr($blk, $off + 1, 1) eq ';') {
1444 $off++;
1445 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001446 last;
1447 }
1448 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001449 # Preprocessor commands end at the newline unless escaped.
1450 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1451 $level--;
1452 $type = '';
1453 $off++;
1454 last;
1455 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001456 $off++;
1457 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001458 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001459 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001460 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001461 $line++;
1462 $remain--;
1463 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001464
1465 my $statement = substr($blk, $soff, $off - $soff + 1);
1466 my $condition = substr($blk, $soff, $coff - $soff + 1);
1467
1468 #warn "STATEMENT<$statement>\n";
1469 #warn "CONDITION<$condition>\n";
1470
Andy Whitcroft773647a2008-03-28 14:15:58 -07001471 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001472
1473 return ($statement, $condition,
1474 $line, $remain + 1, $off - $loff + 1, $level);
1475}
1476
Andy Whitcroftcf655042008-03-04 14:28:20 -08001477sub statement_lines {
1478 my ($stmt) = @_;
1479
1480 # Strip the diff line prefixes and rip blank lines at start and end.
1481 $stmt =~ s/(^|\n)./$1/g;
1482 $stmt =~ s/^\s*//;
1483 $stmt =~ s/\s*$//;
1484
1485 my @stmt_lines = ($stmt =~ /\n/g);
1486
1487 return $#stmt_lines + 2;
1488}
1489
1490sub statement_rawlines {
1491 my ($stmt) = @_;
1492
1493 my @stmt_lines = ($stmt =~ /\n/g);
1494
1495 return $#stmt_lines + 2;
1496}
1497
1498sub statement_block_size {
1499 my ($stmt) = @_;
1500
1501 $stmt =~ s/(^|\n)./$1/g;
1502 $stmt =~ s/^\s*{//;
1503 $stmt =~ s/}\s*$//;
1504 $stmt =~ s/^\s*//;
1505 $stmt =~ s/\s*$//;
1506
1507 my @stmt_lines = ($stmt =~ /\n/g);
1508 my @stmt_statements = ($stmt =~ /;/g);
1509
1510 my $stmt_lines = $#stmt_lines + 2;
1511 my $stmt_statements = $#stmt_statements + 1;
1512
1513 if ($stmt_lines > $stmt_statements) {
1514 return $stmt_lines;
1515 } else {
1516 return $stmt_statements;
1517 }
1518}
1519
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001520sub ctx_statement_full {
1521 my ($linenr, $remain, $off) = @_;
1522 my ($statement, $condition, $level);
1523
1524 my (@chunks);
1525
Andy Whitcroftcf655042008-03-04 14:28:20 -08001526 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001527 ($statement, $condition, $linenr, $remain, $off, $level) =
1528 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001529 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001530 push(@chunks, [ $condition, $statement ]);
1531 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1532 return ($level, $linenr, @chunks);
1533 }
1534
1535 # Pull in the following conditional/block pairs and see if they
1536 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001537 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001538 ($statement, $condition, $linenr, $remain, $off, $level) =
1539 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001540 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001541 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001542 #print "C: push\n";
1543 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001544 }
1545
1546 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001547}
1548
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001549sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001550 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001551 my $line;
1552 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001553 my $blk = '';
1554 my @o;
1555 my @c;
1556 my @res = ();
1557
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001558 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001559 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001560 for ($line = $start; $remain > 0; $line++) {
1561 next if ($rawlines[$line] =~ /^-/);
1562 $remain--;
1563
1564 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001565
1566 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001567 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001568 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001569 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001570 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001571 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001572 $level = pop(@stack);
1573 }
1574
Andy Whitcroft01464f32010-10-26 14:23:19 -07001575 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001576 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1577 if ($off > 0) {
1578 $off--;
1579 next;
1580 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001581
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001582 if ($c eq $close && $level > 0) {
1583 $level--;
1584 last if ($level == 0);
1585 } elsif ($c eq $open) {
1586 $level++;
1587 }
1588 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001589
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001590 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001591 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001592 }
1593
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001594 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001595 }
1596
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001597 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001598}
1599sub ctx_block_outer {
1600 my ($linenr, $remain) = @_;
1601
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001602 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1603 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001604}
1605sub ctx_block {
1606 my ($linenr, $remain) = @_;
1607
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001608 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1609 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001610}
1611sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001612 my ($linenr, $remain, $off) = @_;
1613
1614 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1615 return @r;
1616}
1617sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001618 my ($linenr, $remain) = @_;
1619
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001620 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001621}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001622sub ctx_statement_level {
1623 my ($linenr, $remain, $off) = @_;
1624
1625 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1626}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001627
1628sub ctx_locate_comment {
1629 my ($first_line, $end_line) = @_;
1630
1631 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001632 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001633 return $current_comment if (defined $current_comment);
1634
1635 # Look through the context and try and figure out if there is a
1636 # comment.
1637 my $in_comment = 0;
1638 $current_comment = '';
1639 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001640 my $line = $rawlines[$linenr - 1];
1641 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001642 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1643 $in_comment = 1;
1644 }
1645 if ($line =~ m@/\*@) {
1646 $in_comment = 1;
1647 }
1648 if (!$in_comment && $current_comment ne '') {
1649 $current_comment = '';
1650 }
1651 $current_comment .= $line . "\n" if ($in_comment);
1652 if ($line =~ m@\*/@) {
1653 $in_comment = 0;
1654 }
1655 }
1656
1657 chomp($current_comment);
1658 return($current_comment);
1659}
1660sub ctx_has_comment {
1661 my ($first_line, $end_line) = @_;
1662 my $cmt = ctx_locate_comment($first_line, $end_line);
1663
Andy Whitcroft00df3442007-06-08 13:47:06 -07001664 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001665 ##print "CMMT: $cmt\n";
1666
1667 return ($cmt ne '');
1668}
1669
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001670sub raw_line {
1671 my ($linenr, $cnt) = @_;
1672
1673 my $offset = $linenr - 1;
1674 $cnt++;
1675
1676 my $line;
1677 while ($cnt) {
1678 $line = $rawlines[$offset++];
1679 next if (defined($line) && $line =~ /^-/);
1680 $cnt--;
1681 }
1682
1683 return $line;
1684}
1685
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001686sub get_stat_real {
1687 my ($linenr, $lc) = @_;
1688
1689 my $stat_real = raw_line($linenr, 0);
1690 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1691 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1692 }
1693
1694 return $stat_real;
1695}
1696
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001697sub get_stat_here {
1698 my ($linenr, $cnt, $here) = @_;
1699
1700 my $herectx = $here . "\n";
1701 for (my $n = 0; $n < $cnt; $n++) {
1702 $herectx .= raw_line($linenr, $n) . "\n";
1703 }
1704
1705 return $herectx;
1706}
1707
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001708sub cat_vet {
1709 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001710 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001711
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001712 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001713 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1714 $res .= $1;
1715 if ($2 ne '') {
1716 $coded = sprintf("^%c", unpack('C', $2) + 64);
1717 $res .= $coded;
1718 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001719 }
1720 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001721
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001722 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001723}
1724
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001725my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001726my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001727my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001728my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001729
1730sub annotate_reset {
1731 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001732 $av_pending = '_';
1733 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001734 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001735}
1736
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001737sub annotate_values {
1738 my ($stream, $type) = @_;
1739
1740 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001741 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001742 my $cur = $stream;
1743
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001744 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001745
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001746 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001747 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001748 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001749 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001750 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001751 print "WS($1)\n" if ($dbg_values > 1);
1752 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001753 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001754 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001755 }
1756
Florian Micklerc023e4732011-01-12 16:59:58 -08001757 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001758 print "CAST($1)\n" if ($dbg_values > 1);
1759 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001760 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001761
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001762 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001763 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001764 $type = 'T';
1765
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001766 } elsif ($cur =~ /^($Modifier)\s*/) {
1767 print "MODIFIER($1)\n" if ($dbg_values > 1);
1768 $type = 'T';
1769
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001770 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001771 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001772 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001773 push(@av_paren_type, $type);
1774 if ($2 ne '') {
1775 $av_pending = 'N';
1776 }
1777 $type = 'E';
1778
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001779 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001780 print "UNDEF($1)\n" if ($dbg_values > 1);
1781 $av_preprocessor = 1;
1782 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001783
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001784 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001785 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001786 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001787
1788 push(@av_paren_type, $type);
1789 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001790 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001791
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001792 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001793 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1794 $av_preprocessor = 1;
1795
1796 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1797
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001798 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001799
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001800 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001801 print "PRE_END($1)\n" if ($dbg_values > 1);
1802
1803 $av_preprocessor = 1;
1804
1805 # Assume all arms of the conditional end as this
1806 # one does, and continue as if the #endif was not here.
1807 pop(@av_paren_type);
1808 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001809 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001810
1811 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001812 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001813
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001814 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1815 print "ATTR($1)\n" if ($dbg_values > 1);
1816 $av_pending = $type;
1817 $type = 'N';
1818
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001819 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001820 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001821 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001822 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001823 }
1824 $type = 'N';
1825
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001826 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001827 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001828 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001829 $type = 'N';
1830
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001831 } elsif ($cur =~/^(case)/o) {
1832 print "CASE($1)\n" if ($dbg_values > 1);
1833 $av_pend_colon = 'C';
1834 $type = 'N';
1835
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001836 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001837 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001838 $type = 'N';
1839
1840 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001841 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001842 push(@av_paren_type, $av_pending);
1843 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001844 $type = 'N';
1845
1846 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001847 my $new_type = pop(@av_paren_type);
1848 if ($new_type ne '_') {
1849 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001850 print "PAREN('$1') -> $type\n"
1851 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001852 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001853 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001854 }
1855
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001856 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001857 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001858 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001859 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001860
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001861 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1862 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001863 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001864 } elsif ($type eq 'E') {
1865 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001866 }
1867 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1868 $type = 'V';
1869
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001870 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001871 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001872 $type = 'V';
1873
1874 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001875 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001876 $type = 'N';
1877
Andy Whitcroftcf655042008-03-04 14:28:20 -08001878 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001879 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001880 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001881 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001882
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001883 } elsif ($cur =~/^(,)/) {
1884 print "COMMA($1)\n" if ($dbg_values > 1);
1885 $type = 'C';
1886
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001887 } elsif ($cur =~ /^(\?)/o) {
1888 print "QUESTION($1)\n" if ($dbg_values > 1);
1889 $type = 'N';
1890
1891 } elsif ($cur =~ /^(:)/o) {
1892 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1893
1894 substr($var, length($res), 1, $av_pend_colon);
1895 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1896 $type = 'E';
1897 } else {
1898 $type = 'N';
1899 }
1900 $av_pend_colon = 'O';
1901
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001902 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001903 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001904 $type = 'N';
1905
Andy Whitcroft0d413862008-10-15 22:02:16 -07001906 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001907 my $variant;
1908
1909 print "OPV($1)\n" if ($dbg_values > 1);
1910 if ($type eq 'V') {
1911 $variant = 'B';
1912 } else {
1913 $variant = 'U';
1914 }
1915
1916 substr($var, length($res), 1, $variant);
1917 $type = 'N';
1918
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001919 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001920 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001921 if ($1 ne '++' && $1 ne '--') {
1922 $type = 'N';
1923 }
1924
1925 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001926 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001927 }
1928 if (defined $1) {
1929 $cur = substr($cur, length($1));
1930 $res .= $type x length($1);
1931 }
1932 }
1933
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001934 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001935}
1936
Andy Whitcroft8905a672007-11-28 16:21:06 -08001937sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001938 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001939 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001940 ^(?:
1941 $Modifier|
1942 $Storage|
1943 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001944 DEFINE_\S+
1945 )$|
1946 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001947 goto|
1948 return|
1949 case|
1950 else|
1951 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001952 do|
1953 \#|
1954 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001955 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001956 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001957 )}x;
1958 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1959 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001960 # Check for modifiers.
1961 $possible =~ s/\s*$Storage\s*//g;
1962 $possible =~ s/\s*$Sparse\s*//g;
1963 if ($possible =~ /^\s*$/) {
1964
1965 } elsif ($possible =~ /\s/) {
1966 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001967 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001968 if ($modifier !~ $notPermitted) {
1969 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001970 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001971 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001972 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001973
1974 } else {
1975 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001976 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001977 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001978 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001979 } else {
1980 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001981 }
1982}
1983
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001984my $prefix = '';
1985
Joe Perches000d1cc12011-07-25 17:13:25 -07001986sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001987 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001988
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001989 $type =~ tr/[a-z]/[A-Z]/;
1990
Joe Perchescbec18a2014-04-03 14:49:19 -07001991 return defined $use_type{$type} if (scalar keys %use_type > 0);
1992
1993 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001994}
1995
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001996sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001997 my ($level, $type, $msg) = @_;
1998
1999 if (!show_type($type) ||
2000 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002001 return 0;
2002 }
Joe Perches57230292015-06-25 15:03:03 -07002003 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002004 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002005 if ($level eq 'ERROR') {
2006 $output .= RED;
2007 } elsif ($level eq 'WARNING') {
2008 $output .= YELLOW;
2009 } else {
2010 $output .= GREEN;
2011 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002012 }
Joe Perches57230292015-06-25 15:03:03 -07002013 $output .= $prefix . $level . ':';
2014 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002015 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002016 $output .= "$type:";
2017 }
John Brooks737c0762017-07-10 15:52:24 -07002018 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002019 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002020
2021 if ($showfile) {
2022 my @lines = split("\n", $output, -1);
2023 splice(@lines, 1, 1);
2024 $output = join("\n", @lines);
2025 }
Joe Perches57230292015-06-25 15:03:03 -07002026 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002027
Joe Perches57230292015-06-25 15:03:03 -07002028 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002029
2030 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002031}
Joe Perchescbec18a2014-04-03 14:49:19 -07002032
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002033sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002034 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002035}
Joe Perches000d1cc12011-07-25 17:13:25 -07002036
Joe Perchesd752fcc2014-08-06 16:11:05 -07002037sub fixup_current_range {
2038 my ($lineRef, $offset, $length) = @_;
2039
2040 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2041 my $o = $1;
2042 my $l = $2;
2043 my $no = $o + $offset;
2044 my $nl = $l + $length;
2045 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2046 }
2047}
2048
2049sub fix_inserted_deleted_lines {
2050 my ($linesRef, $insertedRef, $deletedRef) = @_;
2051
2052 my $range_last_linenr = 0;
2053 my $delta_offset = 0;
2054
2055 my $old_linenr = 0;
2056 my $new_linenr = 0;
2057
2058 my $next_insert = 0;
2059 my $next_delete = 0;
2060
2061 my @lines = ();
2062
2063 my $inserted = @{$insertedRef}[$next_insert++];
2064 my $deleted = @{$deletedRef}[$next_delete++];
2065
2066 foreach my $old_line (@{$linesRef}) {
2067 my $save_line = 1;
2068 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002069 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002070 $delta_offset = 0;
2071 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2072 $range_last_linenr = $new_linenr;
2073 fixup_current_range(\$line, $delta_offset, 0);
2074 }
2075
2076 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2077 $deleted = @{$deletedRef}[$next_delete++];
2078 $save_line = 0;
2079 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2080 }
2081
2082 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2083 push(@lines, ${$inserted}{'LINE'});
2084 $inserted = @{$insertedRef}[$next_insert++];
2085 $new_linenr++;
2086 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2087 }
2088
2089 if ($save_line) {
2090 push(@lines, $line);
2091 $new_linenr++;
2092 }
2093
2094 $old_linenr++;
2095 }
2096
2097 return @lines;
2098}
2099
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002100sub fix_insert_line {
2101 my ($linenr, $line) = @_;
2102
2103 my $inserted = {
2104 LINENR => $linenr,
2105 LINE => $line,
2106 };
2107 push(@fixed_inserted, $inserted);
2108}
2109
2110sub fix_delete_line {
2111 my ($linenr, $line) = @_;
2112
2113 my $deleted = {
2114 LINENR => $linenr,
2115 LINE => $line,
2116 };
2117
2118 push(@fixed_deleted, $deleted);
2119}
2120
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002121sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002122 my ($type, $msg) = @_;
2123
2124 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002125 our $clean = 0;
2126 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002127 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002128 }
Joe Perches3705ce52013-07-03 15:05:31 -07002129 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002130}
2131sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002132 my ($type, $msg) = @_;
2133
2134 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002135 our $clean = 0;
2136 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002137 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002138 }
Joe Perches3705ce52013-07-03 15:05:31 -07002139 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002140}
2141sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002142 my ($type, $msg) = @_;
2143
2144 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002145 our $clean = 0;
2146 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002147 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002148 }
Joe Perches3705ce52013-07-03 15:05:31 -07002149 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002150}
2151
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002152sub check_absolute_file {
2153 my ($absolute, $herecurr) = @_;
2154 my $file = $absolute;
2155
2156 ##print "absolute<$absolute>\n";
2157
2158 # See if any suffix of this path is a path within the tree.
2159 while ($file =~ s@^[^/]*/@@) {
2160 if (-f "$root/$file") {
2161 ##print "file<$file>\n";
2162 last;
2163 }
2164 }
2165 if (! -f _) {
2166 return 0;
2167 }
2168
2169 # It is, so see if the prefix is acceptable.
2170 my $prefix = $absolute;
2171 substr($prefix, -length($file)) = '';
2172
2173 ##print "prefix<$prefix>\n";
2174 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002175 WARN("USE_RELATIVE_PATH",
2176 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002177 }
2178}
2179
Joe Perches3705ce52013-07-03 15:05:31 -07002180sub trim {
2181 my ($string) = @_;
2182
Joe Perchesb34c6482013-09-11 14:24:01 -07002183 $string =~ s/^\s+|\s+$//g;
2184
2185 return $string;
2186}
2187
2188sub ltrim {
2189 my ($string) = @_;
2190
2191 $string =~ s/^\s+//;
2192
2193 return $string;
2194}
2195
2196sub rtrim {
2197 my ($string) = @_;
2198
2199 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002200
2201 return $string;
2202}
2203
Joe Perches52ea8502013-11-12 15:10:09 -08002204sub string_find_replace {
2205 my ($string, $find, $replace) = @_;
2206
2207 $string =~ s/$find/$replace/g;
2208
2209 return $string;
2210}
2211
Joe Perches3705ce52013-07-03 15:05:31 -07002212sub tabify {
2213 my ($leading) = @_;
2214
2215 my $source_indent = 8;
2216 my $max_spaces_before_tab = $source_indent - 1;
2217 my $spaces_to_tab = " " x $source_indent;
2218
2219 #convert leading spaces to tabs
2220 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2221 #Remove spaces before a tab
2222 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2223
2224 return "$leading";
2225}
2226
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002227sub pos_last_openparen {
2228 my ($line) = @_;
2229
2230 my $pos = 0;
2231
2232 my $opens = $line =~ tr/\(/\(/;
2233 my $closes = $line =~ tr/\)/\)/;
2234
2235 my $last_openparen = 0;
2236
2237 if (($opens == 0) || ($closes >= $opens)) {
2238 return -1;
2239 }
2240
2241 my $len = length($line);
2242
2243 for ($pos = 0; $pos < $len; $pos++) {
2244 my $string = substr($line, $pos);
2245 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2246 $pos += length($1) - 1;
2247 } elsif (substr($line, $pos, 1) eq '(') {
2248 $last_openparen = $pos;
2249 } elsif (index($string, '(') == -1) {
2250 last;
2251 }
2252 }
2253
Joe Perches91cb5192014-04-03 14:49:32 -07002254 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002255}
2256
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002257sub process {
2258 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002259
2260 my $linenr=0;
2261 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002262 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002263 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002264 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002265
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002266 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002267 my $indent;
2268 my $previndent=0;
2269 my $stashindent=0;
2270
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002271 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002272 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002273 my $author = '';
2274 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002275 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002276 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002277 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002278 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002279 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002280 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002281 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002282 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002283 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002284 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002285 my $non_utf8_charset = 0;
2286
Joe Perches365dd4e2014-08-06 16:10:42 -07002287 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002288 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002289
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002290 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002291 our $cnt_lines = 0;
2292 our $cnt_error = 0;
2293 our $cnt_warn = 0;
2294 our $cnt_chk = 0;
2295
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002296 # Trace the real file/line as we go.
2297 my $realfile = '';
2298 my $realline = 0;
2299 my $realcnt = 0;
2300 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002301 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002302 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002303 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002304 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002305 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002306
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002307 my $prev_values = 'E';
2308
2309 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002310 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002311 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002312 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002313 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002314
Joe Perches7e51f192013-09-11 14:23:57 -07002315 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002316
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002317 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002318 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002319 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002320 my @setup_docs = ();
2321 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002322
Joe Perchesd8b07712013-11-12 15:10:06 -08002323 my $camelcase_file_seeded = 0;
2324
Rob Herring9f3a8992018-04-10 16:33:13 -07002325 my $checklicenseline = 1;
2326
Andy Whitcroft773647a2008-03-28 14:15:58 -07002327 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002328 my $line;
2329 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002330 $linenr++;
2331 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002332
Joe Perches3705ce52013-07-03 15:05:31 -07002333 push(@fixed, $rawline) if ($fix);
2334
Andy Whitcroft773647a2008-03-28 14:15:58 -07002335 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002336 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002337 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002338 $setup_docs = 1;
2339 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002340 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002341 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002342 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002343 $realline=$1-1;
2344 if (defined $2) {
2345 $realcnt=$3+1;
2346 } else {
2347 $realcnt=1+1;
2348 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002349 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002350
2351 # Guestimate if this is a continuing comment. Run
2352 # the context looking for a comment "edge". If this
2353 # edge is a close comment then we must be in a comment
2354 # at context start.
2355 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002356 my $cnt = $realcnt;
2357 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2358 next if (defined $rawlines[$ln - 1] &&
2359 $rawlines[$ln - 1] =~ /^-/);
2360 $cnt--;
2361 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002362 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002363 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2364 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2365 ($edge) = $1;
2366 last;
2367 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002368 }
2369 if (defined $edge && $edge eq '*/') {
2370 $in_comment = 1;
2371 }
2372
2373 # Guestimate if this is a continuing comment. If this
2374 # is the start of a diff block and this line starts
2375 # ' *' then it is very likely a comment.
2376 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002377 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002378 {
2379 $in_comment = 1;
2380 }
2381
2382 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2383 sanitise_line_reset($in_comment);
2384
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002385 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002386 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002387 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002388 $line = sanitise_line($rawline);
2389 }
2390 push(@lines, $line);
2391
2392 if ($realcnt > 1) {
2393 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2394 } else {
2395 $realcnt = 0;
2396 }
2397
2398 #print "==>$rawline\n";
2399 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002400
2401 if ($setup_docs && $line =~ /^\+/) {
2402 push(@setup_docs, $line);
2403 }
2404 }
2405
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002406 $prefix = '';
2407
Andy Whitcroft773647a2008-03-28 14:15:58 -07002408 $realcnt = 0;
2409 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002410 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002411 foreach my $line (@lines) {
2412 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002413 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002414 my $sline = $line; #copy of $line
2415 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002416
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002417 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002418
Joe Perches12c253a2018-06-07 17:10:58 -07002419# check if it's a mode change, rename or start of a patch
2420 if (!$in_commit_log &&
2421 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2422 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2423 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2424 $is_patch = 1;
2425 }
2426
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002427#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002428 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002429 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2430 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002431 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002432 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002433 $realline=$1-1;
2434 if (defined $2) {
2435 $realcnt=$3+1;
2436 } else {
2437 $realcnt=1+1;
2438 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002439 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002440 $prev_values = 'E';
2441
Andy Whitcroft773647a2008-03-28 14:15:58 -07002442 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002443 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002444 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002445 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002446 if ($context =~ /\b(\w+)\s*\(/) {
2447 $context_function = $1;
2448 } else {
2449 undef $context_function;
2450 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002451 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002452
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002453# track the line number as we move through the hunk, note that
2454# new versions of GNU diff omit the leading space on completely
2455# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002456 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002457 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002458 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002459
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002460 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002461 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002462
2463 # Track the previous line.
2464 ($prevline, $stashline) = ($stashline, $line);
2465 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002466 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2467
Andy Whitcroft773647a2008-03-28 14:15:58 -07002468 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002469
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002470 } elsif ($realcnt == 1) {
2471 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002472 }
2473
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002474 my $hunk_line = ($realcnt != 0);
2475
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002476 $here = "#$linenr: " if (!$file);
2477 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002478
Joe Perches2ac73b42014-06-04 16:12:05 -07002479 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002480 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002481 if ($line =~ /^diff --git.*?(\S+)$/) {
2482 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002483 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002484 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002485 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002486 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002487 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002488 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002489 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002490
2491 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002492 if (!$file && $tree && $p1_prefix ne '' &&
2493 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002494 WARN("PATCH_PREFIX",
2495 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002496 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002497
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002498 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002499 ERROR("MODIFIED_INCLUDE_ASM",
2500 "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 -07002501 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002502 $found_file = 1;
2503 }
2504
Joe Perches34d88152015-06-25 15:03:05 -07002505#make up the handle for any error we report on this line
2506 if ($showfile) {
2507 $prefix = "$realfile:$realline: "
2508 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002509 if ($file) {
2510 $prefix = "$filename:$realline: ";
2511 } else {
2512 $prefix = "$filename:$linenr: ";
2513 }
Joe Perches34d88152015-06-25 15:03:05 -07002514 }
2515
Joe Perches2ac73b42014-06-04 16:12:05 -07002516 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002517 if (is_maintained_obsolete($realfile)) {
2518 WARN("OBSOLETE",
2519 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2520 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002521 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002522 $check = 1;
2523 } else {
2524 $check = $check_orig;
2525 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002526 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002527
2528 if ($realfile !~ /^MAINTAINERS/) {
2529 my $last_binding_patch = $is_binding_patch;
2530
2531 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2532
2533 if (($last_binding_patch != -1) &&
2534 ($last_binding_patch ^ $is_binding_patch)) {
2535 WARN("DT_SPLIT_BINDING_PATCH",
2536 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2537 }
2538 }
2539
Andy Whitcroft773647a2008-03-28 14:15:58 -07002540 next;
2541 }
2542
Randy Dunlap389834b2007-06-08 13:47:03 -07002543 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002544
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002545 my $hereline = "$here\n$rawline\n";
2546 my $herecurr = "$here\n$rawline\n";
2547 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002548
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002549 $cnt_lines++ if ($realcnt != 0);
2550
Joe Perches490b2922018-08-21 21:58:01 -07002551# Verify the existence of a commit log if appropriate
2552# 2 is used because a $signature is counted in $commit_log_lines
2553 if ($in_commit_log) {
2554 if ($line !~ /^\s*$/) {
2555 $commit_log_lines++; #could be a $signature
2556 }
2557 } elsif ($has_commit_log && $commit_log_lines < 2) {
2558 WARN("COMMIT_MESSAGE",
2559 "Missing commit description - Add an appropriate one\n");
2560 $commit_log_lines = 2; #warn only once
2561 }
2562
Joe Perchese518e9a2015-06-25 15:03:27 -07002563# Check if the commit log has what seems like a diff which can confuse patch
2564 if ($in_commit_log && !$commit_log_has_diff &&
2565 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2566 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2567 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2568 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2569 ERROR("DIFF_IN_COMMIT_MSG",
2570 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2571 $commit_log_has_diff = 1;
2572 }
2573
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002574# Check for incorrect file permissions
2575 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2576 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002577 if ($realfile !~ m@scripts/@ &&
2578 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002579 ERROR("EXECUTE_PERMISSIONS",
2580 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002581 }
2582 }
2583
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002584# Check the patch for a From:
2585 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2586 $author = $1;
2587 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2588 $author =~ s/"//g;
2589 }
2590
Joe Perches20112472011-07-25 17:13:23 -07002591# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002592 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002593 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002594 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002595 if ($author ne '') {
2596 my $l = $line;
2597 $l =~ s/"//g;
2598 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2599 $authorsignoff = 1;
2600 }
2601 }
Joe Perches20112472011-07-25 17:13:23 -07002602 }
2603
Joe Perchese0d975b2014-12-10 15:51:49 -08002604# Check if MAINTAINERS is being updated. If so, there's probably no need to
2605# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2606 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2607 $reported_maintainer_file = 1;
2608 }
2609
Joe Perches20112472011-07-25 17:13:23 -07002610# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002611 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002612 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002613 my $space_before = $1;
2614 my $sign_off = $2;
2615 my $space_after = $3;
2616 my $email = $4;
2617 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2618
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002619 if ($sign_off !~ /$signature_tags/) {
2620 WARN("BAD_SIGN_OFF",
2621 "Non-standard signature: $sign_off\n" . $herecurr);
2622 }
Joe Perches20112472011-07-25 17:13:23 -07002623 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002624 if (WARN("BAD_SIGN_OFF",
2625 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2626 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002627 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002628 "$ucfirst_sign_off $email";
2629 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002630 }
Joe Perches20112472011-07-25 17:13:23 -07002631 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002632 if (WARN("BAD_SIGN_OFF",
2633 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2634 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002635 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002636 "$ucfirst_sign_off $email";
2637 }
2638
Joe Perches20112472011-07-25 17:13:23 -07002639 }
2640 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002641 if (WARN("BAD_SIGN_OFF",
2642 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2643 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002644 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002645 "$ucfirst_sign_off $email";
2646 }
Joe Perches20112472011-07-25 17:13:23 -07002647 }
2648
2649 my ($email_name, $email_address, $comment) = parse_email($email);
2650 my $suggested_email = format_email(($email_name, $email_address));
2651 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002652 ERROR("BAD_SIGN_OFF",
2653 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002654 } else {
2655 my $dequoted = $suggested_email;
2656 $dequoted =~ s/^"//;
2657 $dequoted =~ s/" </ </;
2658 # Don't force email to have quotes
2659 # Allow just an angle bracketed address
2660 if ("$dequoted$comment" ne $email &&
2661 "<$email_address>$comment" ne $email &&
2662 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002663 WARN("BAD_SIGN_OFF",
2664 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002665 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002666 }
Joe Perches7e51f192013-09-11 14:23:57 -07002667
2668# Check for duplicate signatures
2669 my $sig_nospace = $line;
2670 $sig_nospace =~ s/\s//g;
2671 $sig_nospace = lc($sig_nospace);
2672 if (defined $signatures{$sig_nospace}) {
2673 WARN("BAD_SIGN_OFF",
2674 "Duplicate signature\n" . $herecurr);
2675 } else {
2676 $signatures{$sig_nospace} = 1;
2677 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002678 }
2679
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002680# Check email subject for common tools that don't need to be mentioned
2681 if ($in_header_lines &&
2682 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2683 WARN("EMAIL_SUBJECT",
2684 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2685 }
2686
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002687# Check for unwanted Gerrit info
2688 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2689 ERROR("GERRIT_CHANGE_ID",
2690 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2691 }
2692
Joe Perches369c8dd2015-11-06 16:31:34 -08002693# Check if the commit log is in a possible stack dump
2694 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2695 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2696 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2697 # timestamp
2698 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2699 # stack dump address
2700 $commit_log_possible_stack_dump = 1;
2701 }
2702
Joe Perches2a076f42015-04-16 12:44:28 -07002703# Check for line lengths > 75 in commit log, warn once
2704 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002705 length($line) > 75 &&
2706 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2707 # file delta changes
2708 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2709 # filename then :
2710 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2711 # A Fixes: or Link: line
2712 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002713 WARN("COMMIT_LOG_LONG_LINE",
2714 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2715 $commit_log_long_line = 1;
2716 }
2717
Joe Perchesbf4daf12015-09-09 15:37:50 -07002718# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002719 if ($in_commit_log && $commit_log_possible_stack_dump &&
2720 $line =~ /^\s*$/) {
2721 $commit_log_possible_stack_dump = 0;
2722 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002723
Joe Perches0d7835f2015-02-13 14:38:35 -08002724# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002725 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002726 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002727 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002728 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002729 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002730 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2731 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002732 my $init_char = "c";
2733 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002734 my $short = 1;
2735 my $long = 0;
2736 my $case = 1;
2737 my $space = 1;
2738 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002739 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002740 my $id = '0123456789ab';
2741 my $orig_desc = "commit description";
2742 my $description = "";
2743
Joe Perchesfe043ea2015-09-09 15:37:25 -07002744 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2745 $init_char = $1;
2746 $orig_commit = lc($2);
2747 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2748 $orig_commit = lc($1);
2749 }
2750
Joe Perches0d7835f2015-02-13 14:38:35 -08002751 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2752 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2753 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2754 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2755 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2756 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002757 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002758 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2759 defined $rawlines[$linenr] &&
2760 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2761 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002762 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002763 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2764 defined $rawlines[$linenr] &&
2765 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2766 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2767 $orig_desc = $1;
2768 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2769 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002770 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002771 }
2772
2773 ($id, $description) = git_commit_info($orig_commit,
2774 $id, $orig_desc);
2775
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002776 if (defined($id) &&
2777 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002778 ERROR("GIT_COMMIT_ID",
2779 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2780 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002781 }
2782
Joe Perches13f19372014-08-06 16:10:59 -07002783# Check for added, moved or deleted files
2784 if (!$reported_maintainer_file && !$in_commit_log &&
2785 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2786 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2787 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2788 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002789 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002790 $reported_maintainer_file = 1;
2791 WARN("FILE_PATH_CHANGES",
2792 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2793 }
2794
Andy Whitcroft00df3442007-06-08 13:47:06 -07002795# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002796 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002797 ERROR("CORRUPTED_PATCH",
2798 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002799 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002800 }
2801
2802# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2803 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002804 $rawline !~ m/^$UTF8*$/) {
2805 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2806
2807 my $blank = copy_spacing($rawline);
2808 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2809 my $hereptr = "$hereline$ptr\n";
2810
Joe Perches34d99212011-07-25 17:13:26 -07002811 CHK("INVALID_UTF8",
2812 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002813 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002814
Joe Perches15662b32011-10-31 17:13:12 -07002815# Check if it's the start of a commit log
2816# (not a header line and we haven't seen the patch filename)
2817 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002818 !($rawline =~ /^\s+(?:\S|$)/ ||
2819 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002820 $in_header_lines = 0;
2821 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002822 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002823 }
2824
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002825# Check if there is UTF-8 in a commit log when a mail header has explicitly
2826# declined it, i.e defined some charset where it is missing.
2827 if ($in_header_lines &&
2828 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2829 $1 !~ /utf-8/i) {
2830 $non_utf8_charset = 1;
2831 }
2832
2833 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002834 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002835 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002836 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2837 }
2838
Joe Perchesd6430f72016-12-12 16:46:28 -08002839# Check for absolute kernel paths in commit message
2840 if ($tree && $in_commit_log) {
2841 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2842 my $file = $1;
2843
2844 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2845 check_absolute_file($1, $herecurr)) {
2846 #
2847 } else {
2848 check_absolute_file($file, $herecurr);
2849 }
2850 }
2851 }
2852
Kees Cook66b47b42014-10-13 15:51:57 -07002853# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002854 if (defined($misspellings) &&
2855 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002856 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002857 my $typo = $1;
2858 my $typo_fix = $spelling_fix{lc($typo)};
2859 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2860 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002861 my $msg_level = \&WARN;
2862 $msg_level = \&CHK if ($file);
2863 if (&{$msg_level}("TYPO_SPELLING",
2864 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002865 $fix) {
2866 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2867 }
2868 }
2869 }
2870
Andy Whitcroft306708542008-10-15 22:02:28 -07002871# ignore non-hunk lines and lines being removed
2872 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002873
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002874#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002875 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002876 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002877 if (ERROR("DOS_LINE_ENDINGS",
2878 "DOS line endings\n" . $herevet) &&
2879 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002880 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002881 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002882 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2883 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002884 if (ERROR("TRAILING_WHITESPACE",
2885 "trailing whitespace\n" . $herevet) &&
2886 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002887 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002888 }
2889
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002890 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002891 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002892
Josh Triplett4783f892013-11-12 15:10:12 -08002893# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002894 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002895 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002896 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2897 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002898 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002899 my $msg_level = \&ERROR;
2900 $msg_level = \&CHK if ($file);
2901 &{$msg_level}("FSF_MAILING_ADDRESS",
2902 "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 -08002903 }
2904
Andi Kleen33549572010-05-24 14:33:29 -07002905# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002906# Only applies when adding the entry originally, after that we do not have
2907# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002908 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002909 # 'choice' is usually the last thing on the line (though
2910 # Kconfig supports named choices), so use a word boundary
2911 # (\b) rather than a whitespace character (\s)
2912 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002913 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002914 my $cnt = $realcnt;
2915 my $ln = $linenr + 1;
2916 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002917 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002918 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002919 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002920 $f = $lines[$ln - 1];
2921 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2922 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002923
2924 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002925 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002926
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01002927 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002928 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01002929 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2930 if ($lines[$ln - 1] =~ "---help---") {
2931 WARN("CONFIG_DESCRIPTION",
2932 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2933 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002934 $length = -1;
2935 }
2936
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002937 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002938 $f =~ s/#.*//;
2939 $f =~ s/^\s+//;
2940 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01002941
2942 # This only checks context lines in the patch
2943 # and so hopefully shouldn't trigger false
2944 # positives, even though some of these are
2945 # common words in help texts
2946 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2947 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002948 $is_end = 1;
2949 last;
2950 }
Andi Kleen33549572010-05-24 14:33:29 -07002951 $length++;
2952 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002953 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2954 WARN("CONFIG_DESCRIPTION",
2955 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2956 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002957 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002958 }
2959
Joe Perches628f91a2017-07-10 15:52:07 -07002960# check for MAINTAINERS entries that don't have the right form
2961 if ($realfile =~ /^MAINTAINERS$/ &&
2962 $rawline =~ /^\+[A-Z]:/ &&
2963 $rawline !~ /^\+[A-Z]:\t\S/) {
2964 if (WARN("MAINTAINERS_STYLE",
2965 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2966 $fix) {
2967 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2968 }
2969 }
2970
Christoph Jaeger327953e2015-02-13 14:38:29 -08002971# discourage the use of boolean for type definition attributes of Kconfig options
2972 if ($realfile =~ /Kconfig/ &&
2973 $line =~ /^\+\s*\bboolean\b/) {
2974 WARN("CONFIG_TYPE_BOOLEAN",
2975 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2976 }
2977
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002978 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2979 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2980 my $flag = $1;
2981 my $replacement = {
2982 'EXTRA_AFLAGS' => 'asflags-y',
2983 'EXTRA_CFLAGS' => 'ccflags-y',
2984 'EXTRA_CPPFLAGS' => 'cppflags-y',
2985 'EXTRA_LDFLAGS' => 'ldflags-y',
2986 };
2987
2988 WARN("DEPRECATED_VARIABLE",
2989 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2990 }
2991
Rob Herringbff5da42014-01-23 15:54:51 -08002992# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002993 if (defined $root &&
2994 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2995 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2996
Rob Herringbff5da42014-01-23 15:54:51 -08002997 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2998
Florian Vaussardcc933192014-04-03 14:49:27 -07002999 my $dt_path = $root . "/Documentation/devicetree/bindings/";
3000 my $vp_file = $dt_path . "vendor-prefixes.txt";
3001
Rob Herringbff5da42014-01-23 15:54:51 -08003002 foreach my $compat (@compats) {
3003 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003004 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3005 my $compat3 = $compat;
3006 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3007 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003008 if ( $? >> 8 ) {
3009 WARN("UNDOCUMENTED_DT_STRING",
3010 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3011 }
3012
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003013 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3014 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07003015 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003016 if ( $? >> 8 ) {
3017 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003018 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003019 }
3020 }
3021 }
3022
Rob Herring9f3a8992018-04-10 16:33:13 -07003023# check for using SPDX license tag at beginning of files
3024 if ($realline == $checklicenseline) {
3025 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3026 $checklicenseline = 2;
3027 } elsif ($rawline =~ /^\+/) {
3028 my $comment = "";
3029 if ($realfile =~ /\.(h|s|S)$/) {
3030 $comment = '/*';
3031 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3032 $comment = '//';
3033 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
3034 $comment = '#';
3035 } elsif ($realfile =~ /\.rst$/) {
3036 $comment = '..';
3037 }
3038
3039 if ($comment !~ /^$/ &&
3040 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003041 WARN("SPDX_LICENSE_TAG",
3042 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3043 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3044 my $spdx_license = $1;
3045 if (!is_SPDX_License_valid($spdx_license)) {
3046 WARN("SPDX_LICENSE_TAG",
3047 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3048 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003049 }
3050 }
3051 }
3052
Andy Whitcroft5368df202008-10-15 22:02:27 -07003053# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003054 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003055
Joe Perches47e0c882015-06-25 15:02:57 -07003056# line length limit (with some exclusions)
3057#
3058# There are a few types of lines that may extend beyond $max_line_length:
3059# logging functions like pr_info that end in a string
3060# lines with a single string
3061# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003062# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003063#
3064# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003065# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003066# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3067# LONG_LINE all other lines longer than $max_line_length
3068#
3069# if LONG_LINE is ignored, the other 2 types are also ignored
3070#
3071
Joe Perchesb4749e92015-07-17 16:24:01 -07003072 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003073 my $msg_type = "LONG_LINE";
3074
3075 # Check the allowed long line types first
3076
3077 # logging functions that end in a string that starts
3078 # before $max_line_length
3079 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3080 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3081 $msg_type = "";
3082
3083 # lines with only strings (w/ possible termination)
3084 # #defines with only strings
3085 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3086 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3087 $msg_type = "";
3088
Joe Perchescc147502017-11-17 15:28:44 -08003089 # More special cases
3090 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3091 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003092 $msg_type = "";
3093
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003094 # URL ($rawline is used in case the URL is in a comment)
3095 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3096 $msg_type = "";
3097
Joe Perches47e0c882015-06-25 15:02:57 -07003098 # Otherwise set the alternate message types
3099
3100 # a comment starts before $max_line_length
3101 } elsif ($line =~ /($;[\s$;]*)$/ &&
3102 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3103 $msg_type = "LONG_LINE_COMMENT"
3104
3105 # a quoted string starts before $max_line_length
3106 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3107 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3108 $msg_type = "LONG_LINE_STRING"
3109 }
3110
3111 if ($msg_type ne "" &&
3112 (show_type("LONG_LINE") || show_type($msg_type))) {
3113 WARN($msg_type,
3114 "line over $max_line_length characters\n" . $herecurr);
3115 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003116 }
3117
Andy Whitcroft8905a672007-11-28 16:21:06 -08003118# check for adding lines without a newline.
3119 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003120 WARN("MISSING_EOF_NEWLINE",
3121 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003122 }
3123
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003124# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003125 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003126
3127# at the beginning of a line any tabs must come first and anything
3128# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003129 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3130 $rawline =~ /^\+\s* \s*/) {
3131 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003132 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003133 if (ERROR("CODE_INDENT",
3134 "code indent should use tabs where possible\n" . $herevet) &&
3135 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003136 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003137 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003138 }
3139
Alberto Panizzo08e44362010-03-05 13:43:54 -08003140# check for space before tabs.
3141 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3142 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003143 if (WARN("SPACE_BEFORE_TAB",
3144 "please, no space before tabs\n" . $herevet) &&
3145 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003146 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003147 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003148 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003149 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003150 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003151 }
3152
Joe Perches6a487212018-04-10 16:34:04 -07003153# check for assignments on the start of a line
3154 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3155 CHK("ASSIGNMENT_CONTINUATIONS",
3156 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3157 }
3158
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003159# check for && or || at the start of a line
3160 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3161 CHK("LOGICAL_CONTINUATIONS",
3162 "Logical continuations should be on the previous line\n" . $hereprev);
3163 }
3164
Joe Perchesa91e8992016-05-20 17:04:05 -07003165# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003166 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003167 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003168 my $indent = length($1);
3169 if ($indent % 8) {
3170 if (WARN("TABSTOP",
3171 "Statements should start on a tabstop\n" . $herecurr) &&
3172 $fix) {
3173 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3174 }
3175 }
3176 }
3177
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003178# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003179 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003180 $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 -07003181 $prevline =~ /^\+(\t*)(.*)$/;
3182 my $oldindent = $1;
3183 my $rest = $2;
3184
3185 my $pos = pos_last_openparen($rest);
3186 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003187 $line =~ /^(\+| )([ \t]*)/;
3188 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003189
3190 my $goodtabindent = $oldindent .
3191 "\t" x ($pos / 8) .
3192 " " x ($pos % 8);
3193 my $goodspaceindent = $oldindent . " " x $pos;
3194
3195 if ($newindent ne $goodtabindent &&
3196 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003197
3198 if (CHK("PARENTHESIS_ALIGNMENT",
3199 "Alignment should match open parenthesis\n" . $hereprev) &&
3200 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003201 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003202 s/^\+[ \t]*/\+$goodtabindent/;
3203 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003204 }
3205 }
3206 }
3207
Joe Perches6ab3a972015-04-16 12:44:05 -07003208# check for space after cast like "(int) foo" or "(struct foo) bar"
3209# avoid checking a few false positives:
3210# "sizeof(<type>)" or "__alignof__(<type>)"
3211# function pointer declarations like "(*foo)(int) = bar;"
3212# structure definitions like "(struct foo) { 0 };"
3213# multiline macros that define functions
3214# known attributes or the __attribute__ keyword
3215 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3216 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003217 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003218 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003219 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003220 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003221 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003222 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003223 }
3224
Joe Perches86406b12015-09-09 15:37:41 -07003225# Block comment styles
3226# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003227 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003228 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003229 $rawline =~ /^\+[ \t]*\*/ &&
3230 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003231 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3232 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3233 }
3234
Joe Perches86406b12015-09-09 15:37:41 -07003235# Block comments use * on subsequent lines
3236 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3237 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003238 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003239 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003240 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003241 WARN("BLOCK_COMMENT_STYLE",
3242 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003243 }
3244
Joe Perches86406b12015-09-09 15:37:41 -07003245# Block comments use */ on trailing lines
3246 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003247 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3248 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3249 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003250 WARN("BLOCK_COMMENT_STYLE",
3251 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003252 }
3253
Joe Perches08eb9b82016-10-11 13:51:50 -07003254# Block comment * alignment
3255 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003256 $line =~ /^\+[ \t]*$;/ && #leading comment
3257 $rawline =~ /^\+[ \t]*\*/ && #leading *
3258 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003259 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003260 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3261 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003262 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003263 if (defined($1)) {
3264 $oldindent = expand_tabs($1);
3265 } else {
3266 $prevrawline =~ m@^\+(.*/?)\*@;
3267 $oldindent = expand_tabs($1);
3268 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003269 $rawline =~ m@^\+([ \t]*)\*@;
3270 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003271 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003272 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003273 WARN("BLOCK_COMMENT_STYLE",
3274 "Block comments should align the * on each line\n" . $hereprev);
3275 }
3276 }
3277
Joe Perches7f619192014-08-06 16:10:39 -07003278# check for missing blank lines after struct/union declarations
3279# with exceptions for various attributes and macros
3280 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3281 $line =~ /^\+/ &&
3282 !($line =~ /^\+\s*$/ ||
3283 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3284 $line =~ /^\+\s*MODULE_/i ||
3285 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3286 $line =~ /^\+[a-z_]*init/ ||
3287 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3288 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003289 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003290 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003291 if (CHK("LINE_SPACING",
3292 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3293 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003294 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003295 }
Joe Perches7f619192014-08-06 16:10:39 -07003296 }
3297
Joe Perches365dd4e2014-08-06 16:10:42 -07003298# check for multiple consecutive blank lines
3299 if ($prevline =~ /^[\+ ]\s*$/ &&
3300 $line =~ /^\+\s*$/ &&
3301 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003302 if (CHK("LINE_SPACING",
3303 "Please don't use multiple blank lines\n" . $hereprev) &&
3304 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003305 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003306 }
3307
Joe Perches365dd4e2014-08-06 16:10:42 -07003308 $last_blank_line = $linenr;
3309 }
3310
Joe Perches3b617e32014-04-03 14:49:28 -07003311# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003312 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3313 # actual declarations
3314 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003315 # function pointer declarations
3316 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003317 # foo bar; where foo is some local typedef or #define
3318 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3319 # known declaration macros
3320 $prevline =~ /^\+\s+$declaration_macros/) &&
3321 # for "else if" which can look like "$Ident $Ident"
3322 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3323 # other possible extensions of declaration lines
3324 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3325 # not starting a section or a macro "\" extended line
3326 $prevline =~ /(?:\{\s*|\\)$/) &&
3327 # looks like a declaration
3328 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003329 # function pointer declarations
3330 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003331 # foo bar; where foo is some local typedef or #define
3332 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3333 # known declaration macros
3334 $sline =~ /^\+\s+$declaration_macros/ ||
3335 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003336 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003337 # start or end of block or continuation of declaration
3338 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3339 # bitfield continuation
3340 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3341 # other possible extensions of declaration lines
3342 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3343 # indentation of previous and current line are the same
3344 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003345 if (WARN("LINE_SPACING",
3346 "Missing a blank line after declarations\n" . $hereprev) &&
3347 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003348 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003349 }
Joe Perches3b617e32014-04-03 14:49:28 -07003350 }
3351
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003352# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003353# Exceptions:
3354# 1) within comments
3355# 2) indented preprocessor commands
3356# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003357 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003358 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003359 if (WARN("LEADING_SPACE",
3360 "please, no spaces at the start of a line\n" . $herevet) &&
3361 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003362 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003363 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003364 }
3365
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003366# check we are in a valid C source file if not then ignore this hunk
3367 next if ($realfile !~ /\.(h|c)$/);
3368
Joe Perches5751a242017-11-17 15:28:52 -08003369# check for unusual line ending [ or (
3370 if ($line =~ /^\+.*([\[\(])\s*$/) {
3371 CHK("OPEN_ENDED_LINE",
3372 "Lines should not end with a '$1'\n" . $herecurr);
3373 }
3374
Joe Perches4dbed762017-05-08 15:55:39 -07003375# check if this appears to be the start function declaration, save the name
3376 if ($sline =~ /^\+\{\s*$/ &&
3377 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3378 $context_function = $1;
3379 }
3380
3381# check if this appears to be the end of function declaration
3382 if ($sline =~ /^\+\}\s*$/) {
3383 undef $context_function;
3384 }
3385
Joe Perches032a4c02014-08-06 16:10:29 -07003386# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003387# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003388# if the previous line is a break or return and is indented 1 tab more...
3389 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3390 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003391 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3392 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3393 defined $lines[$linenr] &&
3394 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003395 WARN("UNNECESSARY_ELSE",
3396 "else is not generally useful after a break or return\n" . $hereprev);
3397 }
3398 }
3399
Joe Perchesc00df192014-08-06 16:11:01 -07003400# check indentation of a line with a break;
3401# if the previous line is a goto or return and is indented the same # of tabs
3402 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3403 my $tabs = $1;
3404 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3405 WARN("UNNECESSARY_BREAK",
3406 "break is not useful after a goto or return\n" . $hereprev);
3407 }
3408 }
3409
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003410# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003411 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003412 WARN("CVS_KEYWORD",
3413 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003414 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003415
Joe Perches56e77d72013-02-21 16:44:14 -08003416# check for old HOTPLUG __dev<foo> section markings
3417 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3418 WARN("HOTPLUG_SECTION",
3419 "Using $1 is unnecessary\n" . $herecurr);
3420 }
3421
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003422# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003423 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3424 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003425#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003426 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003427 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003428 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003429 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003430 $stat =~ s/\n./\n /g;
3431 $cond =~ s/\n./\n /g;
3432
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003433#print "linenr<$linenr> <$stat>\n";
3434 # If this statement has no statement boundaries within
3435 # it there is no point in retrying a statement scan
3436 # until we hit end of it.
3437 my $frag = $stat; $frag =~ s/;+\s*$//;
3438 if ($frag !~ /(?:{|;)/) {
3439#print "skip<$line_nr_next>\n";
3440 $suppress_statement = $line_nr_next;
3441 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003442
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003443 # Find the real next line.
3444 $realline_next = $line_nr_next;
3445 if (defined $realline_next &&
3446 (!defined $lines[$realline_next - 1] ||
3447 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3448 $realline_next++;
3449 }
3450
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003451 my $s = $stat;
3452 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003453
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003454 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003455 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003456
3457 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003458 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003459
Andy Whitcroft463f2862009-09-21 17:04:34 -07003460 } elsif ($s =~ /^.\s*else\b/s) {
3461
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003462 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003463 } 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 -07003464 my $type = $1;
3465 $type =~ s/\s+/ /g;
3466 possible($type, "A:" . $s);
3467
Andy Whitcroft8905a672007-11-28 16:21:06 -08003468 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003469 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003470 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003471 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003472
3473 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003474 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003475 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003476 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003477
3478 # Check for any sort of function declaration.
3479 # int foo(something bar, other baz);
3480 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003481 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 -08003482 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003483
Andy Whitcroftcf655042008-03-04 14:28:20 -08003484 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003485 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003486 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003487
Andy Whitcroft8905a672007-11-28 16:21:06 -08003488 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003489 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003490
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003491 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003492 }
3493 }
3494 }
3495
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003496 }
3497
Andy Whitcroft653d4872007-06-23 17:16:34 -07003498#
3499# Checks which may be anchored in the context.
3500#
3501
3502# Check for switch () and associated case and default
3503# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003504 if ($line=~/\bswitch\s*\(.*\)/) {
3505 my $err = '';
3506 my $sep = '';
3507 my @ctx = ctx_block_outer($linenr, $realcnt);
3508 shift(@ctx);
3509 for my $ctx (@ctx) {
3510 my ($clen, $cindent) = line_stats($ctx);
3511 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3512 $indent != $cindent) {
3513 $err .= "$sep$ctx\n";
3514 $sep = '';
3515 } else {
3516 $sep = "[...]\n";
3517 }
3518 }
3519 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003520 ERROR("SWITCH_CASE_INDENT_LEVEL",
3521 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003522 }
3523 }
3524
3525# if/while/etc brace do not go on next line, unless defining a do while loop,
3526# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003527 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 -07003528 my $pre_ctx = "$1$2";
3529
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003530 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003531
3532 if ($line =~ /^\+\t{6,}/) {
3533 WARN("DEEP_INDENTATION",
3534 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3535 }
3536
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003537 my $ctx_cnt = $realcnt - $#ctx - 1;
3538 my $ctx = join("\n", @ctx);
3539
Andy Whitcroft548596d2008-07-23 21:29:01 -07003540 my $ctx_ln = $linenr;
3541 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003542
Andy Whitcroft548596d2008-07-23 21:29:01 -07003543 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3544 defined $lines[$ctx_ln - 1] &&
3545 $lines[$ctx_ln - 1] =~ /^-/)) {
3546 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3547 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003548 $ctx_ln++;
3549 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003550
Andy Whitcroft53210162008-07-23 21:29:03 -07003551 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3552 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003553
Joe Perchesd752fcc2014-08-06 16:11:05 -07003554 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003555 ERROR("OPEN_BRACE",
3556 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003557 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003558 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003559 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3560 $ctx =~ /\)\s*\;\s*$/ &&
3561 defined $lines[$ctx_ln - 1])
3562 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003563 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3564 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003565 WARN("TRAILING_SEMICOLON",
3566 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003567 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003568 }
3569 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003570 }
3571
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003572# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003573 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 -08003574 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3575 ctx_statement_block($linenr, $realcnt, 0)
3576 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003577 my ($s, $c) = ($stat, $cond);
3578
3579 substr($s, 0, length($c), '');
3580
Joe Perches9f5af482015-09-09 15:37:30 -07003581 # remove inline comments
3582 $s =~ s/$;/ /g;
3583 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003584
3585 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003586 my @newlines = ($c =~ /\n/gs);
3587 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003588
Joe Perches9f5af482015-09-09 15:37:30 -07003589 # Make sure we remove the line prefixes as we have
3590 # none on the first line, and are going to readd them
3591 # where necessary.
3592 $s =~ s/\n./\n/gs;
3593 while ($s =~ /\n\s+\\\n/) {
3594 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3595 }
3596
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003597 # We want to check the first line inside the block
3598 # starting at the end of the conditional, so remove:
3599 # 1) any blank line termination
3600 # 2) any opening brace { on end of the line
3601 # 3) any do (...) {
3602 my $continuation = 0;
3603 my $check = 0;
3604 $s =~ s/^.*\bdo\b//;
3605 $s =~ s/^\s*{//;
3606 if ($s =~ s/^\s*\\//) {
3607 $continuation = 1;
3608 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003609 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003610 $check = 1;
3611 $cond_lines++;
3612 }
3613
3614 # Also ignore a loop construct at the end of a
3615 # preprocessor statement.
3616 if (($prevline =~ /^.\s*#\s*define\s/ ||
3617 $prevline =~ /\\\s*$/) && $continuation == 0) {
3618 $check = 0;
3619 }
3620
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003621 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003622 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003623 while ($cond_ptr != $cond_lines) {
3624 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003625
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003626 # If we see an #else/#elif then the code
3627 # is not linear.
3628 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3629 $check = 0;
3630 }
3631
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003632 # Ignore:
3633 # 1) blank lines, they should be at 0,
3634 # 2) preprocessor lines, and
3635 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003636 if ($continuation ||
3637 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003638 $s =~ /^\s*#\s*?/ ||
3639 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003640 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003641 if ($s =~ s/^.*?\n//) {
3642 $cond_lines++;
3643 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003644 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003645 }
3646
3647 my (undef, $sindent) = line_stats("+" . $s);
3648 my $stat_real = raw_line($linenr, $cond_lines);
3649
3650 # Check if either of these lines are modified, else
3651 # this is not this patch's fault.
3652 if (!defined($stat_real) ||
3653 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3654 $check = 0;
3655 }
3656 if (defined($stat_real) && $cond_lines > 1) {
3657 $stat_real = "[...]\n$stat_real";
3658 }
3659
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003660 #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 -07003661
Joe Perches9f5af482015-09-09 15:37:30 -07003662 if ($check && $s ne '' &&
3663 (($sindent % 8) != 0 ||
3664 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003665 ($sindent == $indent &&
3666 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003667 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003668 WARN("SUSPECT_CODE_INDENT",
3669 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003670 }
3671 }
3672
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003673 # Track the 'values' across context and added lines.
3674 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003675 my ($curr_values, $curr_vars) =
3676 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003677 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003678 if ($dbg_values) {
3679 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003680 print "$linenr > .$outline\n";
3681 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003682 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003683 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003684 $prev_values = substr($curr_values, -1);
3685
Andy Whitcroft00df3442007-06-08 13:47:06 -07003686#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003687 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003688
Joe Perches11ca40a2016-12-12 16:46:31 -08003689# check for dereferences that span multiple lines
3690 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3691 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3692 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3693 my $ref = $1;
3694 $line =~ /^.\s*($Lval)/;
3695 $ref .= $1;
3696 $ref =~ s/\s//g;
3697 WARN("MULTILINE_DEREFERENCE",
3698 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3699 }
3700
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003701# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003702 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 -07003703 my $type = $1;
3704 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003705 $var = "" if (!defined $var);
3706 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003707 my $sign = $1;
3708 my $pointer = $2;
3709
3710 $pointer = "" if (!defined $pointer);
3711
3712 if (WARN("UNSPECIFIED_INT",
3713 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3714 $fix) {
3715 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003716 my $comp_pointer = $pointer;
3717 $comp_pointer =~ s/\s//g;
3718 $decl .= $comp_pointer;
3719 $decl = rtrim($decl) if ($var eq "");
3720 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003721 }
3722 }
3723 }
3724
Andy Whitcroft653d4872007-06-23 17:16:34 -07003725# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003726 if ($dbg_type) {
3727 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003728 ERROR("TEST_TYPE",
3729 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003730 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003731 ERROR("TEST_NOT_TYPE",
3732 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003733 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003734 next;
3735 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003736# TEST: allow direct testing of the attribute matcher.
3737 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003738 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003739 ERROR("TEST_ATTR",
3740 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003741 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003742 ERROR("TEST_NOT_ATTR",
3743 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003744 }
3745 next;
3746 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003747
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003748# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003749 if ($line =~ /^.\s*{/ &&
3750 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003751 if (ERROR("OPEN_BRACE",
3752 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003753 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3754 fix_delete_line($fixlinenr - 1, $prevrawline);
3755 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003756 my $fixedline = $prevrawline;
3757 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003758 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003759 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003760 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003761 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003762 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003763 }
3764
Andy Whitcroft653d4872007-06-23 17:16:34 -07003765#
3766# Checks which are anchored on the added line.
3767#
3768
3769# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003770 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003771 my $path = $1;
3772 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003773 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003774 "malformed #include filename\n" . $herecurr);
3775 }
3776 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3777 ERROR("UAPI_INCLUDE",
3778 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003779 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003780 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003781
3782# no C99 // comments
3783 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003784 if (ERROR("C99_COMMENTS",
3785 "do not use C99 // comments\n" . $herecurr) &&
3786 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003787 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003788 if ($line =~ /\/\/(.*)$/) {
3789 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003790 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003791 }
3792 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003793 }
3794 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003795 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003796 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003797
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003798# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3799# the whole statement.
3800#print "APW <$lines[$realline_next - 1]>\n";
3801 if (defined $realline_next &&
3802 exists $lines[$realline_next - 1] &&
3803 !defined $suppress_export{$realline_next} &&
3804 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3805 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003806 # Handle definitions which produce identifiers with
3807 # a prefix:
3808 # XXX(foo);
3809 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003810 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003811 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003812 $name =~ /^${Ident}_$2/) {
3813#print "FOO C name<$name>\n";
3814 $suppress_export{$realline_next} = 1;
3815
3816 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003817 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003818 ^.DEFINE_$Ident\(\Q$name\E\)|
3819 ^.DECLARE_$Ident\(\Q$name\E\)|
3820 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003821 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3822 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003823 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003824#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3825 $suppress_export{$realline_next} = 2;
3826 } else {
3827 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003828 }
3829 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003830 if (!defined $suppress_export{$linenr} &&
3831 $prevline =~ /^.\s*$/ &&
3832 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3833 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3834#print "FOO B <$lines[$linenr - 1]>\n";
3835 $suppress_export{$linenr} = 2;
3836 }
3837 if (defined $suppress_export{$linenr} &&
3838 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003839 WARN("EXPORT_SYMBOL",
3840 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003841 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003842
Joe Eloff5150bda2010-08-09 17:21:00 -07003843# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003844 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003845 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003846 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003847 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003848 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003849 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003850 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003851# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003852 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003853 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003854 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003855 $herecurr) &&
3856 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003857 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003858 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003859 }
3860
Joe Perches18130872014-08-06 16:11:22 -07003861# check for misordered declarations of char/short/int/long with signed/unsigned
3862 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3863 my $tmp = trim($1);
3864 WARN("MISORDERED_TYPE",
3865 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3866 }
3867
Joe Perches809e0822018-08-21 21:58:12 -07003868# check for unnecessary <signed> int declarations of short/long/long long
3869 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3870 my $type = trim($1);
3871 next if ($type !~ /\bint\b/);
3872 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3873 my $new_type = $type;
3874 $new_type =~ s/\b\s*int\s*\b/ /;
3875 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3876 $new_type =~ s/^const\s+//;
3877 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3878 $new_type = "const $new_type" if ($type =~ /^const\b/);
3879 $new_type =~ s/\s+/ /g;
3880 $new_type = trim($new_type);
3881 if (WARN("UNNECESSARY_INT",
3882 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3883 $fix) {
3884 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3885 }
3886 }
3887
Joe Perchescb710ec2010-10-26 14:23:20 -07003888# check for static const char * arrays.
3889 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003890 WARN("STATIC_CONST_CHAR_ARRAY",
3891 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003892 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003893 }
3894
3895# check for initialized const char arrays that should be static const
3896 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
3897 if (WARN("STATIC_CONST_CHAR_ARRAY",
3898 "const array should probably be static const\n" . $herecurr) &&
3899 $fix) {
3900 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
3901 }
3902 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003903
3904# check for static char foo[] = "bar" declarations.
3905 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003906 WARN("STATIC_CONST_CHAR_ARRAY",
3907 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003908 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003909 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003910
Joe Perchesab7e23f2015-04-16 12:44:22 -07003911# check for const <foo> const where <foo> is not a pointer or array type
3912 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3913 my $found = $1;
3914 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3915 WARN("CONST_CONST",
3916 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3917 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3918 WARN("CONST_CONST",
3919 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3920 }
3921 }
3922
Joe Perches9b0fa602014-04-03 14:49:18 -07003923# check for non-global char *foo[] = {"bar", ...} declarations.
3924 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3925 WARN("STATIC_CONST_CHAR_ARRAY",
3926 "char * array declaration might be better as static const\n" .
3927 $herecurr);
3928 }
3929
Joe Perchesb598b672015-04-16 12:44:36 -07003930# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3931 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3932 my $array = $1;
3933 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3934 my $array_div = $1;
3935 if (WARN("ARRAY_SIZE",
3936 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3937 $fix) {
3938 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3939 }
3940 }
3941 }
3942
Joe Perchesb36190c2014-01-27 17:07:18 -08003943# check for function declarations without arguments like "int foo()"
3944 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3945 if (ERROR("FUNCTION_WITHOUT_ARGS",
3946 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3947 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003948 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003949 }
3950 }
3951
Andy Whitcroft653d4872007-06-23 17:16:34 -07003952# check for new typedefs, only function parameters and sparse annotations
3953# make sense.
3954 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003955 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003956 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003957 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003958 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003959 WARN("NEW_TYPEDEFS",
3960 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003961 }
3962
3963# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003964 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003965 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3966 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003967 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003968
Andy Whitcroft65863862009-01-06 14:41:21 -08003969 # Should start with a space.
3970 $to =~ s/^(\S)/ $1/;
3971 # Should not end with a space.
3972 $to =~ s/\s+$//;
3973 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003974 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003975 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003976
Joe Perches3705ce52013-07-03 15:05:31 -07003977## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003978 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003979 if (ERROR("POINTER_LOCATION",
3980 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3981 $fix) {
3982 my $sub_from = $ident;
3983 my $sub_to = $ident;
3984 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003985 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003986 s@\Q$sub_from\E@$sub_to@;
3987 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003988 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003989 }
3990 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3991 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003992 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003993
Andy Whitcroft65863862009-01-06 14:41:21 -08003994 # Should start with a space.
3995 $to =~ s/^(\S)/ $1/;
3996 # Should not end with a space.
3997 $to =~ s/\s+$//;
3998 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003999 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004000 }
4001 # Modifiers should have spaces.
4002 $to =~ s/(\b$Modifier$)/$1 /;
4003
Joe Perches3705ce52013-07-03 15:05:31 -07004004## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004005 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004006 if (ERROR("POINTER_LOCATION",
4007 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4008 $fix) {
4009
4010 my $sub_from = $match;
4011 my $sub_to = $match;
4012 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004013 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004014 s@\Q$sub_from\E@$sub_to@;
4015 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004016 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004017 }
4018
Joe Perches9d3e3c72015-09-09 15:37:27 -07004019# avoid BUG() or BUG_ON()
4020 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004021 my $msg_level = \&WARN;
4022 $msg_level = \&CHK if ($file);
4023 &{$msg_level}("AVOID_BUG",
4024 "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 -07004025 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004026
Joe Perches9d3e3c72015-09-09 15:37:27 -07004027# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004028 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004029 WARN("LINUX_VERSION_CODE",
4030 "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 -08004031 }
4032
Joe Perches17441222011-06-15 15:08:17 -07004033# check for uses of printk_ratelimit
4034 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004035 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004036 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004037 }
4038
Joe Percheseeef5732017-11-17 15:28:41 -08004039# printk should use KERN_* levels
4040 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4041 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4042 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004043 }
4044
Joe Perches243f3802012-05-31 16:26:09 -07004045 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4046 my $orig = $1;
4047 my $level = lc($orig);
4048 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004049 my $level2 = $level;
4050 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004051 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004052 "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 -07004053 }
4054
4055 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004056 if (WARN("PREFER_PR_LEVEL",
4057 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4058 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004059 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004060 s/\bpr_warning\b/pr_warn/;
4061 }
Joe Perches243f3802012-05-31 16:26:09 -07004062 }
4063
Joe Perchesdc139312013-02-21 16:44:13 -08004064 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4065 my $orig = $1;
4066 my $level = lc($orig);
4067 $level = "warn" if ($level eq "warning");
4068 $level = "dbg" if ($level eq "debug");
4069 WARN("PREFER_DEV_LEVEL",
4070 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4071 }
4072
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004073# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4074# number of false positives, but assembly files are not checked, so at
4075# least the arch entry code will not trigger this warning.
4076 if ($line =~ /\bENOSYS\b/) {
4077 WARN("ENOSYS",
4078 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4079 }
4080
Andy Whitcroft653d4872007-06-23 17:16:34 -07004081# function brace can't be on same line, except for #defines of do while,
4082# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004083 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004084 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4085 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4086 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004087 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004088 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004089 $fix) {
4090 fix_delete_line($fixlinenr, $rawline);
4091 my $fixed_line = $rawline;
4092 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4093 my $line1 = $1;
4094 my $line2 = $2;
4095 fix_insert_line($fixlinenr, ltrim($line1));
4096 fix_insert_line($fixlinenr, "\+{");
4097 if ($line2 !~ /^\s*$/) {
4098 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4099 }
4100 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004101 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004102
Andy Whitcroft8905a672007-11-28 16:21:06 -08004103# open braces for enum, union and struct go on the same line.
4104 if ($line =~ /^.\s*{/ &&
4105 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004106 if (ERROR("OPEN_BRACE",
4107 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4108 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4109 fix_delete_line($fixlinenr - 1, $prevrawline);
4110 fix_delete_line($fixlinenr, $rawline);
4111 my $fixedline = rtrim($prevrawline) . " {";
4112 fix_insert_line($fixlinenr, $fixedline);
4113 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004114 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004115 if ($fixedline !~ /^\+\s*$/) {
4116 fix_insert_line($fixlinenr, $fixedline);
4117 }
4118 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004119 }
4120
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004121# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004122 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4123 if (WARN("SPACING",
4124 "missing space after $1 definition\n" . $herecurr) &&
4125 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004126 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004127 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4128 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004129 }
4130
Joe Perches31070b52014-01-23 15:54:49 -08004131# Function pointer declarations
4132# check spacing between type, funcptr, and args
4133# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004134 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004135 my $declare = $1;
4136 my $pre_pointer_space = $2;
4137 my $post_pointer_space = $3;
4138 my $funcname = $4;
4139 my $post_funcname_space = $5;
4140 my $pre_args_space = $6;
4141
Joe Perches91f72e92014-04-03 14:49:12 -07004142# the $Declare variable will capture all spaces after the type
4143# so check it for a missing trailing missing space but pointer return types
4144# don't need a space so don't warn for those.
4145 my $post_declare_space = "";
4146 if ($declare =~ /(\s+)$/) {
4147 $post_declare_space = $1;
4148 $declare = rtrim($declare);
4149 }
4150 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004151 WARN("SPACING",
4152 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004153 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004154 }
4155
4156# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004157# This test is not currently implemented because these declarations are
4158# equivalent to
4159# int foo(int bar, ...)
4160# and this is form shouldn't/doesn't generate a checkpatch warning.
4161#
4162# elsif ($declare =~ /\s{2,}$/) {
4163# WARN("SPACING",
4164# "Multiple spaces after return type\n" . $herecurr);
4165# }
Joe Perches31070b52014-01-23 15:54:49 -08004166
4167# unnecessary space "type ( *funcptr)(args...)"
4168 if (defined $pre_pointer_space &&
4169 $pre_pointer_space =~ /^\s/) {
4170 WARN("SPACING",
4171 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4172 }
4173
4174# unnecessary space "type (* funcptr)(args...)"
4175 if (defined $post_pointer_space &&
4176 $post_pointer_space =~ /^\s/) {
4177 WARN("SPACING",
4178 "Unnecessary space before function pointer name\n" . $herecurr);
4179 }
4180
4181# unnecessary space "type (*funcptr )(args...)"
4182 if (defined $post_funcname_space &&
4183 $post_funcname_space =~ /^\s/) {
4184 WARN("SPACING",
4185 "Unnecessary space after function pointer name\n" . $herecurr);
4186 }
4187
4188# unnecessary space "type (*funcptr) (args...)"
4189 if (defined $pre_args_space &&
4190 $pre_args_space =~ /^\s/) {
4191 WARN("SPACING",
4192 "Unnecessary space before function pointer arguments\n" . $herecurr);
4193 }
4194
4195 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004196 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004197 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004198 }
4199 }
4200
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004201# check for spacing round square brackets; allowed:
4202# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004203# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4204# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004205 while ($line =~ /(.*?\s)\[/g) {
4206 my ($where, $prefix) = ($-[1], $1);
4207 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004208 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004209 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004210 if (ERROR("BRACKET_SPACE",
4211 "space prohibited before open square bracket '['\n" . $herecurr) &&
4212 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004213 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004214 s/^(\+.*?)\s+\[/$1\[/;
4215 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004216 }
4217 }
4218
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004219# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004220 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004221 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004222 my $ctx_before = substr($line, 0, $-[1]);
4223 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004224
4225 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004226 if ($name =~ /^(?:
4227 if|for|while|switch|return|case|
4228 volatile|__volatile__|
4229 __attribute__|format|__extension__|
4230 asm|__asm__)$/x)
4231 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004232 # cpp #define statements have non-optional spaces, ie
4233 # if there is a space between the name and the open
4234 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004235 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004236
4237 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004238 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004239
4240 # If this whole things ends with a type its most
4241 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004242 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004243
4244 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004245 if (WARN("SPACING",
4246 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4247 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004248 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004249 s/\b$name\s+\(/$name\(/;
4250 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004251 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004252 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004253
Andy Whitcroft653d4872007-06-23 17:16:34 -07004254# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004255 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004256 my $fixed_line = "";
4257 my $line_fixed = 0;
4258
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004259 my $ops = qr{
4260 <<=|>>=|<=|>=|==|!=|
4261 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4262 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004263 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004264 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004265 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004266 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004267
4268## print("element count: <" . $#elements . ">\n");
4269## foreach my $el (@elements) {
4270## print("el: <$el>\n");
4271## }
4272
4273 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004274 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004275
Joe Perches3705ce52013-07-03 15:05:31 -07004276 foreach my $el (@elements) {
4277 push(@fix_elements, substr($rawline, $off, length($el)));
4278 $off += length($el);
4279 }
4280
4281 $off = 0;
4282
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004283 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004284 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004285
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004286 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004287
4288 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4289
4290## print("n: <$n> good: <$good>\n");
4291
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004292 $off += length($elements[$n]);
4293
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004294 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004295 my $ca = substr($opline, 0, $off);
4296 my $cc = '';
4297 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4298 $cc = substr($opline, $off + length($elements[$n + 1]));
4299 }
4300 my $cb = "$ca$;$cc";
4301
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004302 my $a = '';
4303 $a = 'V' if ($elements[$n] ne '');
4304 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004305 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004306 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4307 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004308 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004309
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004310 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004311
4312 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004313 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004314 $c = 'V' if ($elements[$n + 2] ne '');
4315 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004316 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004317 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4318 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004319 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004320 } else {
4321 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004322 }
4323
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004324 my $ctx = "${a}x${c}";
4325
4326 my $at = "(ctx:$ctx)";
4327
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004328 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004329 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004330
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004331 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004332 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004333
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004334 # Get the full operator variant.
4335 my $opv = $op . substr($curr_vars, $off, 1);
4336
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004337 # Ignore operators passed as parameters.
4338 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004339 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004340
Andy Whitcroftcf655042008-03-04 14:28:20 -08004341# # Ignore comments
4342# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004343
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004344 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004345 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004346 if ($ctx !~ /.x[WEBC]/ &&
4347 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004348 if (ERROR("SPACING",
4349 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004350 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004351 $line_fixed = 1;
4352 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004353 }
4354
4355 # // is a comment
4356 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004357
Joe Perchesb00e4812014-04-03 14:49:33 -07004358 # : when part of a bitfield
4359 } elsif ($opv eq ':B') {
4360 # skip the bitfield test for now
4361
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004362 # No spaces for:
4363 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004364 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004365 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004366 if (ERROR("SPACING",
4367 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004368 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004369 if (defined $fix_elements[$n + 2]) {
4370 $fix_elements[$n + 2] =~ s/^\s+//;
4371 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004372 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004373 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004374 }
4375
Joe Perches23810972014-12-10 15:51:32 -08004376 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004377 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004378 my $rtrim_before = 0;
4379 my $space_after = 0;
4380 if ($ctx =~ /Wx./) {
4381 if (ERROR("SPACING",
4382 "space prohibited before that '$op' $at\n" . $hereptr)) {
4383 $line_fixed = 1;
4384 $rtrim_before = 1;
4385 }
4386 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004387 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004388 if (ERROR("SPACING",
4389 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004390 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004391 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004392 $space_after = 1;
4393 }
4394 }
4395 if ($rtrim_before || $space_after) {
4396 if ($rtrim_before) {
4397 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4398 } else {
4399 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4400 }
4401 if ($space_after) {
4402 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004403 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004404 }
4405
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004406 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004407 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004408 #warn "'*' is part of type\n";
4409
4410 # unary operators should have a space before and
4411 # none after. May be left adjacent to another
4412 # unary operator, or a cast
4413 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004414 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004415 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004416 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004417 if (ERROR("SPACING",
4418 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004419 if ($n != $last_after + 2) {
4420 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4421 $line_fixed = 1;
4422 }
Joe Perches3705ce52013-07-03 15:05:31 -07004423 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004424 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004425 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004426 # A unary '*' may be const
4427
4428 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004429 if (ERROR("SPACING",
4430 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004431 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004432 if (defined $fix_elements[$n + 2]) {
4433 $fix_elements[$n + 2] =~ s/^\s+//;
4434 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004435 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004436 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004437 }
4438
4439 # unary ++ and unary -- are allowed no space on one side.
4440 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004441 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004442 if (ERROR("SPACING",
4443 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004444 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004445 $line_fixed = 1;
4446 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004447 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004448 if ($ctx =~ /Wx[BE]/ ||
4449 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004450 if (ERROR("SPACING",
4451 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004452 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004453 $line_fixed = 1;
4454 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004455 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004456 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004457 if (ERROR("SPACING",
4458 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004459 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004460 if (defined $fix_elements[$n + 2]) {
4461 $fix_elements[$n + 2] =~ s/^\s+//;
4462 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004463 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004464 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004465 }
4466
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004467 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004468 } elsif ($op eq '<<' or $op eq '>>' or
4469 $op eq '&' or $op eq '^' or $op eq '|' or
4470 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004471 $op eq '*' or $op eq '/' or
4472 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004473 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004474 if ($check) {
4475 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4476 if (CHK("SPACING",
4477 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4478 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4479 $fix_elements[$n + 2] =~ s/^\s+//;
4480 $line_fixed = 1;
4481 }
4482 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4483 if (CHK("SPACING",
4484 "space preferred before that '$op' $at\n" . $hereptr)) {
4485 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4486 $line_fixed = 1;
4487 }
4488 }
4489 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004490 if (ERROR("SPACING",
4491 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004492 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4493 if (defined $fix_elements[$n + 2]) {
4494 $fix_elements[$n + 2] =~ s/^\s+//;
4495 }
Joe Perches3705ce52013-07-03 15:05:31 -07004496 $line_fixed = 1;
4497 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004498 }
4499
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004500 # A colon needs no spaces before when it is
4501 # terminating a case value or a label.
4502 } elsif ($opv eq ':C' || $opv eq ':L') {
4503 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004504 if (ERROR("SPACING",
4505 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004506 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004507 $line_fixed = 1;
4508 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004509 }
4510
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004511 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004512 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004513 my $ok = 0;
4514
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004515 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004516 if (($op eq '<' &&
4517 $cc =~ /^\S+\@\S+>/) ||
4518 ($op eq '>' &&
4519 $ca =~ /<\S+\@\S+$/))
4520 {
4521 $ok = 1;
4522 }
4523
Joe Perchese0df7e12015-04-16 12:44:53 -07004524 # for asm volatile statements
4525 # ignore a colon with another
4526 # colon immediately before or after
4527 if (($op eq ':') &&
4528 ($ca =~ /:$/ || $cc =~ /^:/)) {
4529 $ok = 1;
4530 }
4531
Joe Perches84731622013-11-12 15:10:05 -08004532 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004533 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004534 my $msg_level = \&ERROR;
4535 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004536
Jean Delvare0675a8f2017-09-08 16:16:07 -07004537 if (&{$msg_level}("SPACING",
4538 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004539 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4540 if (defined $fix_elements[$n + 2]) {
4541 $fix_elements[$n + 2] =~ s/^\s+//;
4542 }
Joe Perches3705ce52013-07-03 15:05:31 -07004543 $line_fixed = 1;
4544 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004545 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004546 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004547 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004548
4549## print("n: <$n> GOOD: <$good>\n");
4550
4551 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004552 }
Joe Perches3705ce52013-07-03 15:05:31 -07004553
4554 if (($#elements % 2) == 0) {
4555 $fixed_line = $fixed_line . $fix_elements[$#elements];
4556 }
4557
Joe Perches194f66f2014-08-06 16:11:03 -07004558 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4559 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004560 }
4561
4562
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004563 }
4564
Joe Perches786b6322013-07-03 15:05:32 -07004565# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004566 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004567 if (WARN("SPACING",
4568 "space prohibited before semicolon\n" . $herecurr) &&
4569 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004570 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004571 s/^(\+.*\S)\s+;/$1;/;
4572 }
4573 }
4574
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004575# check for multiple assignments
4576 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004577 CHK("MULTIPLE_ASSIGNMENTS",
4578 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004579 }
4580
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004581## # check for multiple declarations, allowing for a function declaration
4582## # continuation.
4583## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4584## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4585##
4586## # Remove any bracketed sections to ensure we do not
4587## # falsly report the parameters of functions.
4588## my $ln = $line;
4589## while ($ln =~ s/\([^\(\)]*\)//g) {
4590## }
4591## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004592## WARN("MULTIPLE_DECLARATION",
4593## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004594## }
4595## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004596
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004597#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004598 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004599 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004600 if (ERROR("SPACING",
4601 "space required before the open brace '{'\n" . $herecurr) &&
4602 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004603 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004604 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004605 }
4606
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004607## # check for blank lines before declarations
4608## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4609## $prevrawline =~ /^.\s*$/) {
4610## WARN("SPACING",
4611## "No blank lines before declarations\n" . $hereprev);
4612## }
4613##
4614
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004615# closing brace should have a space following it when it has anything
4616# on the line
4617 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004618 if (ERROR("SPACING",
4619 "space required after that close brace '}'\n" . $herecurr) &&
4620 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004621 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004622 s/}((?!(?:,|;|\)))\S)/} $1/;
4623 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004624 }
4625
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004626# check spacing on square brackets
4627 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004628 if (ERROR("SPACING",
4629 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4630 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004631 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004632 s/\[\s+/\[/;
4633 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004634 }
4635 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004636 if (ERROR("SPACING",
4637 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4638 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004639 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004640 s/\s+\]/\]/;
4641 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004642 }
4643
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004644# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004645 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4646 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004647 if (ERROR("SPACING",
4648 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4649 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004650 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004651 s/\(\s+/\(/;
4652 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004653 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004654 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004655 $line !~ /for\s*\(.*;\s+\)/ &&
4656 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004657 if (ERROR("SPACING",
4658 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4659 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004660 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004661 s/\s+\)/\)/;
4662 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004663 }
4664
Joe Perchese2826fd2014-08-06 16:10:48 -07004665# check unnecessary parentheses around addressof/dereference single $Lvals
4666# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4667
4668 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004669 my $var = $1;
4670 if (CHK("UNNECESSARY_PARENTHESES",
4671 "Unnecessary parentheses around $var\n" . $herecurr) &&
4672 $fix) {
4673 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4674 }
4675 }
4676
4677# check for unnecessary parentheses around function pointer uses
4678# ie: (foo->bar)(); should be foo->bar();
4679# but not "if (foo->bar) (" to avoid some false positives
4680 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4681 my $var = $2;
4682 if (CHK("UNNECESSARY_PARENTHESES",
4683 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4684 $fix) {
4685 my $var2 = deparenthesize($var);
4686 $var2 =~ s/\s//g;
4687 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4688 }
4689 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004690
Joe Perches63b7c732017-09-08 16:16:01 -07004691# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004692# when !drivers/staging or command-line uses --strict
4693 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004694 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004695 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4696 my $if_stat = $1;
4697 my $test = substr($2, 1, -1);
4698 my $herectx;
4699 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4700 my $match = $1;
4701 # avoid parentheses around potential macro args
4702 next if ($match =~ /^\s*\w+\s*$/);
4703 if (!defined($herectx)) {
4704 $herectx = $here . "\n";
4705 my $cnt = statement_rawlines($if_stat);
4706 for (my $n = 0; $n < $cnt; $n++) {
4707 my $rl = raw_line($linenr, $n);
4708 $herectx .= $rl . "\n";
4709 last if $rl =~ /^[ \+].*\{/;
4710 }
4711 }
4712 CHK("UNNECESSARY_PARENTHESES",
4713 "Unnecessary parentheses around '$match'\n" . $herectx);
4714 }
4715 }
4716
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004717#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004718 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004719 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004720 if (WARN("INDENTED_LABEL",
4721 "labels should not be indented\n" . $herecurr) &&
4722 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004723 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004724 s/^(.)\s+/$1/;
4725 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004726 }
4727
Joe Perches5b9553a2014-04-03 14:49:21 -07004728# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004729 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004730 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004731 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004732 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4733 my $value = $1;
4734 $value = deparenthesize($value);
4735 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4736 ERROR("RETURN_PARENTHESES",
4737 "return is not a function, parentheses are not required\n" . $herecurr);
4738 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004739 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004740 ERROR("SPACING",
4741 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004742 }
4743 }
Joe Perches507e5142013-11-12 15:10:13 -08004744
Joe Perchesb43ae212014-06-23 13:22:07 -07004745# unnecessary return in a void function
4746# at end-of-function, with the previous line a single leading tab, then return;
4747# and the line before that not a goto label target like "out:"
4748 if ($sline =~ /^[ \+]}\s*$/ &&
4749 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4750 $linenr >= 3 &&
4751 $lines[$linenr - 3] =~ /^[ +]/ &&
4752 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004753 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004754 "void function return statements are not generally useful\n" . $hereprev);
4755 }
Joe Perches9819cf22014-06-04 16:12:09 -07004756
Joe Perches189248d2014-01-23 15:54:47 -08004757# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004758 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004759 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4760 my $openparens = $1;
4761 my $count = $openparens =~ tr@\(@\(@;
4762 my $msg = "";
4763 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4764 my $comp = $4; #Not $1 because of $LvalOrFunc
4765 $msg = " - maybe == should be = ?" if ($comp eq "==");
4766 WARN("UNNECESSARY_PARENTHESES",
4767 "Unnecessary parentheses$msg\n" . $herecurr);
4768 }
4769 }
4770
Joe Perchesc5595fa2015-09-09 15:37:58 -07004771# comparisons with a constant or upper case identifier on the left
4772# avoid cases like "foo + BAR < baz"
4773# only fix matches surrounded by parentheses to avoid incorrect
4774# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004775 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004776 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4777 my $lead = $1;
4778 my $const = $2;
4779 my $comp = $3;
4780 my $to = $4;
4781 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004782 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004783 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4784 WARN("CONSTANT_COMPARISON",
4785 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4786 $fix) {
4787 if ($comp eq "<") {
4788 $newcomp = ">";
4789 } elsif ($comp eq "<=") {
4790 $newcomp = ">=";
4791 } elsif ($comp eq ">") {
4792 $newcomp = "<";
4793 } elsif ($comp eq ">=") {
4794 $newcomp = "<=";
4795 }
4796 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4797 }
4798 }
4799
Joe Perchesf34e4a42015-04-16 12:44:19 -07004800# Return of what appears to be an errno should normally be negative
4801 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004802 my $name = $1;
4803 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004804 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004805 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004806 }
4807 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004808
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004809# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004810 if ($line =~ /\b(if|while|for|switch)\(/) {
4811 if (ERROR("SPACING",
4812 "space required before the open parenthesis '('\n" . $herecurr) &&
4813 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004814 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004815 s/\b(if|while|for|switch)\(/$1 \(/;
4816 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004817 }
4818
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004819# Check for illegal assignment in if conditional -- and check for trailing
4820# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004821 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004822 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4823 ctx_statement_block($linenr, $realcnt, 0)
4824 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004825 my ($stat_next) = ctx_statement_block($line_nr_next,
4826 $remain_next, $off_next);
4827 $stat_next =~ s/\n./\n /g;
4828 ##print "stat<$stat> stat_next<$stat_next>\n";
4829
4830 if ($stat_next =~ /^\s*while\b/) {
4831 # If the statement carries leading newlines,
4832 # then count those as offsets.
4833 my ($whitespace) =
4834 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4835 my $offset =
4836 statement_rawlines($whitespace) - 1;
4837
4838 $suppress_whiletrailers{$line_nr_next +
4839 $offset} = 1;
4840 }
4841 }
4842 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004843 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004844 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004845 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004846
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004847 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004848 ERROR("ASSIGN_IN_IF",
4849 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004850 }
4851
4852 # Find out what is on the end of the line after the
4853 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004854 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004855 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004856 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004857 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4858 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004859 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004860 # Find out how long the conditional actually is.
4861 my @newlines = ($c =~ /\n/gs);
4862 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004863 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004864
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004865 $stat_real = raw_line($linenr, $cond_lines)
4866 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004867 if (defined($stat_real) && $cond_lines > 1) {
4868 $stat_real = "[...]\n$stat_real";
4869 }
4870
Joe Perches000d1cc12011-07-25 17:13:25 -07004871 ERROR("TRAILING_STATEMENTS",
4872 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004873 }
4874 }
4875
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004876# Check for bitwise tests written as boolean
4877 if ($line =~ /
4878 (?:
4879 (?:\[|\(|\&\&|\|\|)
4880 \s*0[xX][0-9]+\s*
4881 (?:\&\&|\|\|)
4882 |
4883 (?:\&\&|\|\|)
4884 \s*0[xX][0-9]+\s*
4885 (?:\&\&|\|\||\)|\])
4886 )/x)
4887 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004888 WARN("HEXADECIMAL_BOOLEAN_TEST",
4889 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004890 }
4891
Andy Whitcroft8905a672007-11-28 16:21:06 -08004892# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004893 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4894 my $s = $1;
4895 $s =~ s/$;//g; # Remove any comments
4896 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004897 ERROR("TRAILING_STATEMENTS",
4898 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004899 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004900 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004901# if should not continue a brace
4902 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004903 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004904 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004905 $herecurr);
4906 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004907# case and default should not have general statements after them
4908 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4909 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004910 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004911 \s*return\s+
4912 )/xg)
4913 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004914 ERROR("TRAILING_STATEMENTS",
4915 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004916 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004917
4918 # Check for }<nl>else {, these must be at the same
4919 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004920 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4921 $previndent == $indent) {
4922 if (ERROR("ELSE_AFTER_BRACE",
4923 "else should follow close brace '}'\n" . $hereprev) &&
4924 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4925 fix_delete_line($fixlinenr - 1, $prevrawline);
4926 fix_delete_line($fixlinenr, $rawline);
4927 my $fixedline = $prevrawline;
4928 $fixedline =~ s/}\s*$//;
4929 if ($fixedline !~ /^\+\s*$/) {
4930 fix_insert_line($fixlinenr, $fixedline);
4931 }
4932 $fixedline = $rawline;
4933 $fixedline =~ s/^(.\s*)else/$1} else/;
4934 fix_insert_line($fixlinenr, $fixedline);
4935 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004936 }
4937
Joe Perches8b8856f2014-08-06 16:11:14 -07004938 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4939 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004940 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4941
4942 # Find out what is on the end of the line after the
4943 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004944 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004945 $s =~ s/\n.*//g;
4946
4947 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004948 if (ERROR("WHILE_AFTER_BRACE",
4949 "while should follow close brace '}'\n" . $hereprev) &&
4950 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4951 fix_delete_line($fixlinenr - 1, $prevrawline);
4952 fix_delete_line($fixlinenr, $rawline);
4953 my $fixedline = $prevrawline;
4954 my $trailing = $rawline;
4955 $trailing =~ s/^\+//;
4956 $trailing = trim($trailing);
4957 $fixedline =~ s/}\s*$/} $trailing/;
4958 fix_insert_line($fixlinenr, $fixedline);
4959 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004960 }
4961 }
4962
Joe Perches95e2c602013-07-03 15:05:20 -07004963#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004964 while ($line =~ m{($Constant|$Lval)}g) {
4965 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004966
Joe Perches95e2c602013-07-03 15:05:20 -07004967#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004968 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004969 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004970#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004971 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004972#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004973 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4974#Ignore some three character SI units explicitly, like MiB and KHz
4975 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004976 while ($var =~ m{($Ident)}g) {
4977 my $word = $1;
4978 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004979 if ($check) {
4980 seed_camelcase_includes();
4981 if (!$file && !$camelcase_file_seeded) {
4982 seed_camelcase_file($realfile);
4983 $camelcase_file_seeded = 1;
4984 }
4985 }
Joe Perches7e781f62013-09-11 14:23:55 -07004986 if (!defined $camelcase{$word}) {
4987 $camelcase{$word} = 1;
4988 CHK("CAMELCASE",
4989 "Avoid CamelCase: <$word>\n" . $herecurr);
4990 }
Joe Perches34456862013-07-03 15:05:34 -07004991 }
Joe Perches323c1262012-12-17 16:02:07 -08004992 }
4993 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004994
4995#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004996 if ($line =~ /\#\s*define.*\\\s+$/) {
4997 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4998 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4999 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005000 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005001 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005002 }
5003
Fabian Frederick0e212e02015-04-16 12:44:25 -07005004# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5005# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005006 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005007 my $file = "$1.h";
5008 my $checkfile = "include/linux/$file";
5009 if (-f "$root/$checkfile" &&
5010 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005011 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005012 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005013 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5014 if ($asminclude > 0) {
5015 if ($realfile =~ m{^arch/}) {
5016 CHK("ARCH_INCLUDE_LINUX",
5017 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5018 } else {
5019 WARN("INCLUDE_LINUX",
5020 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5021 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005022 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005023 }
5024 }
5025
Andy Whitcroft653d4872007-06-23 17:16:34 -07005026# multi-statement macros should be enclosed in a do while loop, grab the
5027# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005028# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005029 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5030 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005031 my $ln = $linenr;
5032 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005033 my ($off, $dstat, $dcond, $rest);
5034 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005035 my $has_flow_statement = 0;
5036 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005037 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005038 ctx_statement_block($linenr, $realcnt, 0);
5039 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005040 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005041 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005042
Joe Perches08a28432014-10-13 15:51:55 -07005043 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005044 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005045
Joe Perchesf59b64b2016-10-11 13:52:08 -07005046 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5047 my $define_args = $1;
5048 my $define_stmt = $dstat;
5049 my @def_args = ();
5050
5051 if (defined $define_args && $define_args ne "") {
5052 $define_args = substr($define_args, 1, length($define_args) - 2);
5053 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005054 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005055 @def_args = split(",", $define_args);
5056 }
5057
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005058 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005059 $dstat =~ s/\\\n.//g;
5060 $dstat =~ s/^\s*//s;
5061 $dstat =~ s/\s*$//s;
5062
5063 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005064 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5065 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005066 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005067 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005068 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005069
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005070 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005071 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5072 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005073 {
5074 }
5075
Joe Perches42e15292016-03-15 14:58:01 -07005076 # Make asm volatile uses seem like a generic function
5077 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5078
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005079 my $exceptions = qr{
5080 $Declare|
5081 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005082 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005083 DECLARE_PER_CPU|
5084 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005085 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005086 union|
5087 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005088 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005089 ^\"|\"$|
5090 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005091 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005092 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005093
5094 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005095 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005096 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005097
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005098 if ($dstat ne '' &&
5099 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5100 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005101 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005102 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005103 $dstat !~ /$exceptions/ &&
5104 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005105 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005106 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005107 $dstat !~ /^for\s*$Constant$/ && # for (...)
5108 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5109 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005110 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005111 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005112 {
Joe Perchese7955562017-05-08 15:55:48 -07005113 if ($dstat =~ /^\s*if\b/) {
5114 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5115 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5116 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005117 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5118 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5119 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005120 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005121 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005122 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005123
5124 }
Joe Perches52076492016-10-11 13:52:14 -07005125
5126 # Make $define_stmt single line, comment-free, etc
5127 my @stmt_array = split('\n', $define_stmt);
5128 my $first = 1;
5129 $define_stmt = "";
5130 foreach my $l (@stmt_array) {
5131 $l =~ s/\\$//;
5132 if ($first) {
5133 $define_stmt = $l;
5134 $first = 0;
5135 } elsif ($l =~ /^[\+ ]/) {
5136 $define_stmt .= substr($l, 1);
5137 }
5138 }
5139 $define_stmt =~ s/$;//g;
5140 $define_stmt =~ s/\s+/ /g;
5141 $define_stmt = trim($define_stmt);
5142
Joe Perchesf59b64b2016-10-11 13:52:08 -07005143# check if any macro arguments are reused (ignore '...' and 'type')
5144 foreach my $arg (@def_args) {
5145 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005146 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005147 my $tmp_stmt = $define_stmt;
5148 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5149 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5150 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005151 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005152 if ($use_cnt > 1) {
5153 CHK("MACRO_ARG_REUSE",
5154 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005155 }
5156# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005157 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005158 ((defined($1) && $1 ne ',') ||
5159 (defined($2) && $2 ne ','))) {
5160 CHK("MACRO_ARG_PRECEDENCE",
5161 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005162 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005163 }
Joe Perches5023d342012-12-17 16:01:47 -08005164
Joe Perches08a28432014-10-13 15:51:55 -07005165# check for macros with flow control, but without ## concatenation
5166# ## concatenation is commonly a macro that defines a function so ignore those
5167 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005168 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005169 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005170
Joe Perches08a28432014-10-13 15:51:55 -07005171 WARN("MACRO_WITH_FLOW_CONTROL",
5172 "Macros with flow control statements should be avoided\n" . "$herectx");
5173 }
5174
Joe Perches481eb482012-12-17 16:01:56 -08005175# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005176
5177 } else {
5178 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005179 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5180 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005181 $line =~ /^\+.*\\$/) {
5182 WARN("LINE_CONTINUATIONS",
5183 "Avoid unnecessary line continuations\n" . $herecurr);
5184 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005185 }
5186
Joe Perchesb13edf72012-07-30 14:41:24 -07005187# do {} while (0) macro tests:
5188# single-statement macros do not need to be enclosed in do while (0) loop,
5189# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005190 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005191 $realfile !~ m@/vmlinux.lds.h$@ &&
5192 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5193 my $ln = $linenr;
5194 my $cnt = $realcnt;
5195 my ($off, $dstat, $dcond, $rest);
5196 my $ctx = '';
5197 ($dstat, $dcond, $ln, $cnt, $off) =
5198 ctx_statement_block($linenr, $realcnt, 0);
5199 $ctx = $dstat;
5200
5201 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005202 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005203
5204 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5205 my $stmts = $2;
5206 my $semis = $3;
5207
5208 $ctx =~ s/\n*$//;
5209 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005210 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005211
Joe Perchesac8e97f2012-08-21 16:15:53 -07005212 if (($stmts =~ tr/;/;/) == 1 &&
5213 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005214 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5215 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5216 }
5217 if (defined $semis && $semis ne "") {
5218 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5219 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5220 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005221 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5222 $ctx =~ s/\n*$//;
5223 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005224 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005225
5226 WARN("TRAILING_SEMICOLON",
5227 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005228 }
5229 }
5230
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005231# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005232 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5233 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005234 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005235 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005236 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5237 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005238 my @allowed = ();
5239 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005240 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005241 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005242 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005243 for my $chunk (@chunks) {
5244 my ($cond, $block) = @{$chunk};
5245
Andy Whitcroft773647a2008-03-28 14:15:58 -07005246 # If the condition carries leading newlines, then count those as offsets.
5247 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5248 my $offset = statement_rawlines($whitespace) - 1;
5249
Joe Perchesaad4f612012-03-23 15:02:19 -07005250 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005251 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5252
5253 # We have looked at and allowed this specific line.
5254 $suppress_ifbraces{$ln + $offset} = 1;
5255
5256 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005257 $ln += statement_rawlines($block) - 1;
5258
Andy Whitcroft773647a2008-03-28 14:15:58 -07005259 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005260
5261 $seen++ if ($block =~ /^\s*{/);
5262
Joe Perchesaad4f612012-03-23 15:02:19 -07005263 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005264 if (statement_lines($cond) > 1) {
5265 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005266 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005267 }
5268 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005269 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005270 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005271 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005272 if (statement_block_size($block) > 1) {
5273 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005274 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005275 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005276 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005277 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005278 if ($seen) {
5279 my $sum_allowed = 0;
5280 foreach (@allowed) {
5281 $sum_allowed += $_;
5282 }
5283 if ($sum_allowed == 0) {
5284 WARN("BRACES",
5285 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5286 } elsif ($sum_allowed != $allow &&
5287 $seen != $allow) {
5288 CHK("BRACES",
5289 "braces {} should be used on all arms of this statement\n" . $herectx);
5290 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005291 }
5292 }
5293 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005294 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005295 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005296 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005297
Andy Whitcroftcf655042008-03-04 14:28:20 -08005298 # Check the pre-context.
5299 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5300 #print "APW: ALLOWED: pre<$1>\n";
5301 $allowed = 1;
5302 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005303
5304 my ($level, $endln, @chunks) =
5305 ctx_statement_full($linenr, $realcnt, $-[0]);
5306
Andy Whitcroftcf655042008-03-04 14:28:20 -08005307 # Check the condition.
5308 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005309 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005310 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005311 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005312 }
5313 if (statement_lines($cond) > 1) {
5314 #print "APW: ALLOWED: cond<$cond>\n";
5315 $allowed = 1;
5316 }
5317 if ($block =~/\b(?:if|for|while)\b/) {
5318 #print "APW: ALLOWED: block<$block>\n";
5319 $allowed = 1;
5320 }
5321 if (statement_block_size($block) > 1) {
5322 #print "APW: ALLOWED: lines block<$block>\n";
5323 $allowed = 1;
5324 }
5325 # Check the post-context.
5326 if (defined $chunks[1]) {
5327 my ($cond, $block) = @{$chunks[1]};
5328 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005329 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005330 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005331 if ($block =~ /^\s*\{/) {
5332 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5333 $allowed = 1;
5334 }
5335 }
5336 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005337 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005338 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005339
Joe Perches000d1cc12011-07-25 17:13:25 -07005340 WARN("BRACES",
5341 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005342 }
5343 }
5344
Joe Perchese4c5bab2017-02-24 15:01:41 -08005345# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005346 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5347 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005348 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5349 }
5350
Joe Perches0979ae62012-12-17 16:01:59 -08005351# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005352 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005353 if (CHK("BRACES",
5354 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5355 $fix && $prevrawline =~ /^\+/) {
5356 fix_delete_line($fixlinenr - 1, $prevrawline);
5357 }
Joe Perches0979ae62012-12-17 16:01:59 -08005358 }
Joe Perches77b9a532013-07-03 15:05:29 -07005359 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005360 if (CHK("BRACES",
5361 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5362 $fix) {
5363 fix_delete_line($fixlinenr, $rawline);
5364 }
Joe Perches0979ae62012-12-17 16:01:59 -08005365 }
5366
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005367# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005368 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5369 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005370 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005371 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005372 }
5373
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005374# Check for user-visible strings broken across lines, which breaks the ability
5375# to grep for the string. Make exceptions when the previous string ends in a
5376# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5377# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005378 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005379 $prevline =~ /"\s*$/ &&
5380 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5381 if (WARN("SPLIT_STRING",
5382 "quoted string split across lines\n" . $hereprev) &&
5383 $fix &&
5384 $prevrawline =~ /^\+.*"\s*$/ &&
5385 $last_coalesced_string_linenr != $linenr - 1) {
5386 my $extracted_string = get_quoted_string($line, $rawline);
5387 my $comma_close = "";
5388 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5389 $comma_close = $1;
5390 }
5391
5392 fix_delete_line($fixlinenr - 1, $prevrawline);
5393 fix_delete_line($fixlinenr, $rawline);
5394 my $fixedline = $prevrawline;
5395 $fixedline =~ s/"\s*$//;
5396 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5397 fix_insert_line($fixlinenr - 1, $fixedline);
5398 $fixedline = $rawline;
5399 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5400 if ($fixedline !~ /\+\s*$/) {
5401 fix_insert_line($fixlinenr, $fixedline);
5402 }
5403 $last_coalesced_string_linenr = $linenr;
5404 }
5405 }
5406
5407# check for missing a space in a string concatenation
5408 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5409 WARN('MISSING_SPACE',
5410 "break quoted strings at a space character\n" . $hereprev);
5411 }
5412
Joe Perchese4b7d302017-05-08 15:55:51 -07005413# check for an embedded function name in a string when the function is known
5414# This does not work very well for -f --file checking as it depends on patch
5415# context providing the function name or a single line form for in-file
5416# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005417 if ($line =~ /^\+.*$String/ &&
5418 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005419 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5420 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005421 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005422 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005423 }
5424
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005425# check for spaces before a quoted newline
5426 if ($rawline =~ /^.*\".*\s\\n/) {
5427 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5428 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5429 $fix) {
5430 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5431 }
5432
5433 }
5434
Joe Perchesf17dba42014-10-13 15:51:51 -07005435# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005436 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5437 if (CHK("CONCATENATED_STRING",
5438 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5439 $fix) {
5440 while ($line =~ /($String)/g) {
5441 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5442 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5443 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5444 }
5445 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005446 }
5447
Joe Perches90ad30e2014-12-10 15:51:59 -08005448# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005449 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005450 if (WARN("STRING_FRAGMENTS",
5451 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5452 $fix) {
5453 while ($line =~ /($String)(?=\s*")/g) {
5454 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5455 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5456 }
5457 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005458 }
5459
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005460# check for non-standard and hex prefixed decimal printf formats
5461 my $show_L = 1; #don't show the same defect twice
5462 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005463 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005464 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005465 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005466 # check for %L
5467 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005468 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005469 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5470 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005471 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005472 # check for %Z
5473 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5474 WARN("PRINTF_Z",
5475 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5476 $show_Z = 0;
5477 }
5478 # check for 0x<decimal>
5479 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5480 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005481 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5482 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005483 }
5484
5485# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005486 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005487 WARN("LINE_CONTINUATIONS",
5488 "Avoid line continuations in quoted strings\n" . $herecurr);
5489 }
5490
Andy Whitcroft00df3442007-06-08 13:47:06 -07005491# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005492 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005493 WARN("IF_0",
5494 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5495 }
5496
5497# warn about #if 1
5498 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5499 WARN("IF_1",
5500 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005501 }
5502
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005503# check for needless "if (<foo>) fn(<foo>)" uses
5504 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005505 my $tested = quotemeta($1);
5506 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5507 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5508 my $func = $1;
5509 if (WARN('NEEDLESS_IF',
5510 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5511 $fix) {
5512 my $do_fix = 1;
5513 my $leading_tabs = "";
5514 my $new_leading_tabs = "";
5515 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5516 $leading_tabs = $1;
5517 } else {
5518 $do_fix = 0;
5519 }
5520 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5521 $new_leading_tabs = $1;
5522 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5523 $do_fix = 0;
5524 }
5525 } else {
5526 $do_fix = 0;
5527 }
5528 if ($do_fix) {
5529 fix_delete_line($fixlinenr - 1, $prevrawline);
5530 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5531 }
5532 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005533 }
5534 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005535
Joe Perchesebfdc402014-08-06 16:10:27 -07005536# check for unnecessary "Out of Memory" messages
5537 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5538 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5539 (defined $1 || defined $3) &&
5540 $linenr > 3) {
5541 my $testval = $2;
5542 my $testline = $lines[$linenr - 3];
5543
5544 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5545# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5546
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005547 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 -07005548 WARN("OOM_MESSAGE",
5549 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5550 }
5551 }
5552
Joe Perchesf78d98f2014-10-13 15:52:01 -07005553# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005554 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005555 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5556 my $level = $1;
5557 if (WARN("UNNECESSARY_KERN_LEVEL",
5558 "Possible unnecessary $level\n" . $herecurr) &&
5559 $fix) {
5560 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5561 }
5562 }
5563
Joe Perches45c55e92017-02-24 15:01:31 -08005564# check for logging continuations
5565 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5566 WARN("LOGGING_CONTINUATION",
5567 "Avoid logging continuation uses where feasible\n" . $herecurr);
5568 }
5569
Joe Perchesabb08a52014-12-10 15:51:46 -08005570# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005571 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005572 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5573 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5574 WARN("MASK_THEN_SHIFT",
5575 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5576 }
5577
Joe Perchesb75ac612014-12-10 15:52:02 -08005578# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005579 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005580 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5581 my $val = $1;
5582 my $equal = "!";
5583 $equal = "" if ($4 eq "!=");
5584 if (CHK("COMPARISON_TO_NULL",
5585 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5586 $fix) {
5587 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5588 }
5589 }
5590 }
5591
Joe Perches8716de32013-09-11 14:24:05 -07005592# check for bad placement of section $InitAttribute (e.g.: __initdata)
5593 if ($line =~ /(\b$InitAttribute\b)/) {
5594 my $attr = $1;
5595 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5596 my $ptr = $1;
5597 my $var = $2;
5598 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5599 ERROR("MISPLACED_INIT",
5600 "$attr should be placed after $var\n" . $herecurr)) ||
5601 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5602 WARN("MISPLACED_INIT",
5603 "$attr should be placed after $var\n" . $herecurr))) &&
5604 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005605 $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 -07005606 }
5607 }
5608 }
5609
Joe Perchese970b8842013-11-12 15:10:10 -08005610# check for $InitAttributeData (ie: __initdata) with const
5611 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5612 my $attr = $1;
5613 $attr =~ /($InitAttributePrefix)(.*)/;
5614 my $attr_prefix = $1;
5615 my $attr_type = $2;
5616 if (ERROR("INIT_ATTRIBUTE",
5617 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5618 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005619 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005620 s/$InitAttributeData/${attr_prefix}initconst/;
5621 }
5622 }
5623
5624# check for $InitAttributeConst (ie: __initconst) without const
5625 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5626 my $attr = $1;
5627 if (ERROR("INIT_ATTRIBUTE",
5628 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5629 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005630 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005631 /(^\+\s*(?:static\s+))/;
5632 $lead = rtrim($1);
5633 $lead = "$lead " if ($lead !~ /^\+$/);
5634 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005635 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005636 }
5637 }
5638
Joe Perchesc17893c2015-04-16 12:44:42 -07005639# check for __read_mostly with const non-pointer (should just be const)
5640 if ($line =~ /\b__read_mostly\b/ &&
5641 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5642 if (ERROR("CONST_READ_MOSTLY",
5643 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5644 $fix) {
5645 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5646 }
5647 }
5648
Joe Perchesfbdb8132014-04-03 14:49:14 -07005649# don't use __constant_<foo> functions outside of include/uapi/
5650 if ($realfile !~ m@^include/uapi/@ &&
5651 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5652 my $constant_func = $1;
5653 my $func = $constant_func;
5654 $func =~ s/^__constant_//;
5655 if (WARN("CONSTANT_CONVERSION",
5656 "$constant_func should be $func\n" . $herecurr) &&
5657 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005658 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005659 }
5660 }
5661
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005662# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005663 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005664 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005665 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005666 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005667 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005668 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5669 }
5670 if ($delay > 2000) {
5671 WARN("LONG_UDELAY",
5672 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005673 }
5674 }
5675
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005676# warn about unexpectedly long msleep's
5677 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5678 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005679 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005680 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005681 }
5682 }
5683
Joe Perches36ec1932013-07-03 15:05:25 -07005684# check for comparisons of jiffies
5685 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5686 WARN("JIFFIES_COMPARISON",
5687 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5688 }
5689
Joe Perches9d7a34a2013-07-03 15:05:26 -07005690# check for comparisons of get_jiffies_64()
5691 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5692 WARN("JIFFIES_COMPARISON",
5693 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5694 }
5695
Andy Whitcroft00df3442007-06-08 13:47:06 -07005696# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005697# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005698# print "#ifdef in C files should be avoided\n";
5699# print "$herecurr";
5700# $clean = 0;
5701# }
5702
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005703# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005704 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005705 if (ERROR("SPACING",
5706 "exactly one space required after that #$1\n" . $herecurr) &&
5707 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005708 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005709 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5710 }
5711
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005712 }
5713
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005714# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005715 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5716 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005717 my $which = $1;
5718 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005719 CHK("UNCOMMENTED_DEFINITION",
5720 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005721 }
5722 }
5723# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005724
5725 my $barriers = qr{
5726 mb|
5727 rmb|
5728 wmb|
5729 read_barrier_depends
5730 }x;
5731 my $barrier_stems = qr{
5732 mb__before_atomic|
5733 mb__after_atomic|
5734 store_release|
5735 load_acquire|
5736 store_mb|
5737 (?:$barriers)
5738 }x;
5739 my $all_barriers = qr{
5740 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005741 smp_(?:$barrier_stems)|
5742 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005743 }x;
5744
5745 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005746 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005747 WARN("MEMORY_BARRIER",
5748 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005749 }
5750 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005751
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005752 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5753
5754 if ($realfile !~ m@^include/asm-generic/@ &&
5755 $realfile !~ m@/barrier\.h$@ &&
5756 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5757 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5758 WARN("MEMORY_BARRIER",
5759 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5760 }
5761
Joe Perchescb426e92015-06-25 15:02:46 -07005762# check for waitqueue_active without a comment.
5763 if ($line =~ /\bwaitqueue_active\s*\(/) {
5764 if (!ctx_has_comment($first_line, $linenr)) {
5765 WARN("WAITQUEUE_ACTIVE",
5766 "waitqueue_active without comment\n" . $herecurr);
5767 }
5768 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005769
Paul E. McKenney91db2592017-11-27 09:37:35 -08005770# check for smp_read_barrier_depends and read_barrier_depends
5771 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5772 WARN("READ_BARRIER_DEPENDS",
5773 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5774 }
5775
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005776# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005777 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005778 CHK("ARCH_DEFINES",
5779 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005780 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005781
Joe Perches596ed452017-07-12 14:37:02 -07005782# check that the storage class is not after a type
5783 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005784 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005785 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5786 }
5787# Check that the storage class is at the beginning of a declaration
5788 if ($line =~ /\b$Storage\b/ &&
5789 $line !~ /^.\s*$Storage/ &&
5790 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5791 $1 !~ /[\,\)]\s*$/) {
5792 WARN("STORAGE_CLASS",
5793 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005794 }
5795
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005796# check the location of the inline attribute, that it is between
5797# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005798 if ($line =~ /\b$Type\s+$Inline\b/ ||
5799 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005800 ERROR("INLINE_LOCATION",
5801 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005802 }
5803
Andy Whitcroft8905a672007-11-28 16:21:06 -08005804# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005805 if ($realfile !~ m@\binclude/uapi/@ &&
5806 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005807 if (WARN("INLINE",
5808 "plain inline is preferred over $1\n" . $herecurr) &&
5809 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005810 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005811
5812 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005813 }
5814
Joe Perches3d130fd2011-01-12 17:00:00 -08005815# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005816 if ($realfile !~ m@\binclude/uapi/@ &&
5817 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005818 WARN("PREFER_PACKED",
5819 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005820 }
5821
Joe Perches39b7e282011-07-25 17:13:24 -07005822# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005823 if ($realfile !~ m@\binclude/uapi/@ &&
5824 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005825 WARN("PREFER_ALIGNED",
5826 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005827 }
5828
Joe Perches5f14d3b2012-01-10 15:09:52 -08005829# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005830 if ($realfile !~ m@\binclude/uapi/@ &&
5831 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005832 if (WARN("PREFER_PRINTF",
5833 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5834 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005835 $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 -07005836
5837 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005838 }
5839
Joe Perches6061d942012-03-23 15:02:16 -07005840# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005841 if ($realfile !~ m@\binclude/uapi/@ &&
5842 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005843 if (WARN("PREFER_SCANF",
5844 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5845 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005846 $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 -07005847 }
Joe Perches6061d942012-03-23 15:02:16 -07005848 }
5849
Joe Perches619a9082014-12-10 15:51:35 -08005850# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005851 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005852 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5853 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5854 $line =~ /\b__weak\b/)) {
5855 ERROR("WEAK_DECLARATION",
5856 "Using weak declarations can have unintended link defects\n" . $herecurr);
5857 }
5858
Tomas Winklerfd39f902016-12-12 16:46:34 -08005859# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005860 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005861 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005862 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5863 my $type = $1;
5864 if ($type =~ /\b($typeC99Typedefs)\b/) {
5865 $type = $1;
5866 my $kernel_type = 'u';
5867 $kernel_type = 's' if ($type =~ /^_*[si]/);
5868 $type =~ /(\d+)/;
5869 $kernel_type .= $1;
5870 if (CHK("PREFER_KERNEL_TYPES",
5871 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5872 $fix) {
5873 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5874 }
5875 }
5876 }
5877
Joe Perches938224b2016-01-20 14:59:15 -08005878# check for cast of C90 native int or longer types constants
5879 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5880 my $cast = $1;
5881 my $const = $2;
5882 if (WARN("TYPECAST_INT_CONSTANT",
5883 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5884 $fix) {
5885 my $suffix = "";
5886 my $newconst = $const;
5887 $newconst =~ s/${Int_type}$//;
5888 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5889 if ($cast =~ /\blong\s+long\b/) {
5890 $suffix .= 'LL';
5891 } elsif ($cast =~ /\blong\b/) {
5892 $suffix .= 'L';
5893 }
5894 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5895 }
5896 }
5897
Joe Perches8f53a9b2010-03-05 13:43:48 -08005898# check for sizeof(&)
5899 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005900 WARN("SIZEOF_ADDRESS",
5901 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005902 }
5903
Joe Perches66c80b62012-07-30 14:41:22 -07005904# check for sizeof without parenthesis
5905 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005906 if (WARN("SIZEOF_PARENTHESIS",
5907 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5908 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005909 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005910 }
Joe Perches66c80b62012-07-30 14:41:22 -07005911 }
5912
Joe Perches88982fe2012-12-17 16:02:00 -08005913# check for struct spinlock declarations
5914 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5915 WARN("USE_SPINLOCK_T",
5916 "struct spinlock should be spinlock_t\n" . $herecurr);
5917 }
5918
Joe Perchesa6962d72013-04-29 16:18:13 -07005919# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005920 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005921 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005922 $fmt =~ s/%%//g;
5923 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005924 if (WARN("PREFER_SEQ_PUTS",
5925 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5926 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005927 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005928 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005929 }
5930 }
5931
Joe Perches478b1792018-04-10 16:33:34 -07005932# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07005933 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07005934 defined $stat &&
5935 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5936 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005937 my $stat_real;
5938
Joe Perches0b523762017-05-08 15:55:36 -07005939 my $lc = $stat =~ tr@\n@@;
5940 $lc = $lc + $linenr;
5941 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07005942 my $specifier;
5943 my $extension;
5944 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07005945 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5946 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005947
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005948 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5949 $specifier = $1;
5950 $extension = $2;
5951 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5952 $bad_specifier = $specifier;
5953 last;
5954 }
5955 if ($extension eq "x" && !defined($stat_real)) {
5956 if (!defined($stat_real)) {
5957 $stat_real = get_stat_real($linenr, $lc);
5958 }
5959 WARN("VSPRINTF_SPECIFIER_PX",
5960 "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");
5961 }
5962 }
5963 if ($bad_specifier ne "") {
5964 my $stat_real = get_stat_real($linenr, $lc);
5965 my $ext_type = "Invalid";
5966 my $use = "";
5967 if ($bad_specifier =~ /p[Ff]/) {
5968 $ext_type = "Deprecated";
5969 $use = " - use %pS instead";
5970 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5971 }
5972
5973 WARN("VSPRINTF_POINTER_EXTENSION",
5974 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5975 }
Joe Perches0b523762017-05-08 15:55:36 -07005976 }
5977 }
5978
Andy Whitcroft554e1652012-01-10 15:09:57 -08005979# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07005980 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005981 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005982 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005983
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005984 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005985 my $ms_val = $7;
5986 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005987
Andy Whitcroft554e1652012-01-10 15:09:57 -08005988 if ($ms_size =~ /^(0x|)0$/i) {
5989 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005990 "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 -08005991 } elsif ($ms_size =~ /^(0x|)1$/i) {
5992 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005993 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5994 }
5995 }
5996
Joe Perches98a9bba2014-01-23 15:54:52 -08005997# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005998# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07005999# defined $stat &&
6000# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6001# if (WARN("PREFER_ETHER_ADDR_COPY",
6002# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6003# $fix) {
6004# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6005# }
6006# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006007
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006008# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006009# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006010# defined $stat &&
6011# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6012# WARN("PREFER_ETHER_ADDR_EQUAL",
6013# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6014# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006015
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006016# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6017# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006018# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006019# defined $stat &&
6020# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6021#
6022# my $ms_val = $7;
6023#
6024# if ($ms_val =~ /^(?:0x|)0+$/i) {
6025# if (WARN("PREFER_ETH_ZERO_ADDR",
6026# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6027# $fix) {
6028# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6029# }
6030# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6031# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6032# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6033# $fix) {
6034# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6035# }
6036# }
6037# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006038
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006039# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006040 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006041 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006042 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006043 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006044 my $call = $1;
6045 my $cast1 = deparenthesize($2);
6046 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006047 my $cast2 = deparenthesize($7);
6048 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006049 my $cast;
6050
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006051 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006052 $cast = "$cast1 or $cast2";
6053 } elsif ($cast1 ne "") {
6054 $cast = $cast1;
6055 } else {
6056 $cast = $cast2;
6057 }
6058 WARN("MINMAX",
6059 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006060 }
6061 }
6062
Joe Perches4a273192012-07-30 14:41:20 -07006063# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006064 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006065 defined $stat &&
6066 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6067 my $min = $1;
6068 my $max = $7;
6069 if ($min eq $max) {
6070 WARN("USLEEP_RANGE",
6071 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6072 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6073 $min > $max) {
6074 WARN("USLEEP_RANGE",
6075 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6076 }
6077 }
6078
Joe Perches823b7942013-11-12 15:10:15 -08006079# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006080 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006081 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006082 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006083 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6084 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6085 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6086 my $lc = $stat =~ tr@\n@@;
6087 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006088 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006089 WARN("NAKED_SSCANF",
6090 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6091 }
6092
Joe Perchesafc819a2014-06-04 16:12:08 -07006093# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006094 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006095 defined $stat &&
6096 $line =~ /\bsscanf\b/) {
6097 my $lc = $stat =~ tr@\n@@;
6098 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006099 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006100 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6101 my $format = $6;
6102 my $count = $format =~ tr@%@%@;
6103 if ($count == 1 &&
6104 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6105 WARN("SSCANF_TO_KSTRTO",
6106 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6107 }
6108 }
6109 }
6110
Joe Perches70dc8a42013-09-11 14:23:58 -07006111# check for new externs in .h files.
6112 if ($realfile =~ /\.h$/ &&
6113 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006114 if (CHK("AVOID_EXTERNS",
6115 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006116 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006117 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006118 }
6119 }
6120
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006121# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006122 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006123 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006124 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006125 my $function_name = $1;
6126 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006127
6128 my $s = $stat;
6129 if (defined $cond) {
6130 substr($s, 0, length($cond), '');
6131 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006132 if ($s =~ /^\s*;/ &&
6133 $function_name ne 'uninitialized_var')
6134 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006135 WARN("AVOID_EXTERNS",
6136 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006137 }
6138
6139 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006140 WARN("FUNCTION_ARGUMENTS",
6141 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006142 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006143
6144 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6145 $stat =~ /^.\s*extern\s+/)
6146 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006147 WARN("AVOID_EXTERNS",
6148 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006149 }
6150
Joe Perchesa0ad7592017-07-10 15:52:19 -07006151# check for function declarations that have arguments without identifier names
6152 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006153 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006154 $1 ne "void") {
6155 my $args = trim($1);
6156 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6157 my $arg = trim($1);
6158 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6159 WARN("FUNCTION_ARGUMENTS",
6160 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6161 }
6162 }
6163 }
6164
Joe Perchesa0ad7592017-07-10 15:52:19 -07006165# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006166 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006167 defined $stat &&
6168 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6169 $context_function = $1;
6170
6171# check for multiline function definition with misplaced open brace
6172 my $ok = 0;
6173 my $cnt = statement_rawlines($stat);
6174 my $herectx = $here . "\n";
6175 for (my $n = 0; $n < $cnt; $n++) {
6176 my $rl = raw_line($linenr, $n);
6177 $herectx .= $rl . "\n";
6178 $ok = 1 if ($rl =~ /^[ \+]\{/);
6179 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6180 last if $rl =~ /^[ \+].*\{/;
6181 }
6182 if (!$ok) {
6183 ERROR("OPEN_BRACE",
6184 "open brace '{' following function definitions go on the next line\n" . $herectx);
6185 }
6186 }
6187
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006188# checks for new __setup's
6189 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6190 my $name = $1;
6191
6192 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006193 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006194 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006195 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006196 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006197
6198# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006199 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006200 WARN("UNNECESSARY_CASTS",
6201 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006202 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006203
Joe Perchesa640d252013-07-03 15:05:21 -07006204# alloc style
6205# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006206 if ($perl_version_ok &&
Joe Perchesa640d252013-07-03 15:05:21 -07006207 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6208 CHK("ALLOC_SIZEOF_STRUCT",
6209 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6210 }
6211
Joe Perches60a55362014-06-04 16:12:07 -07006212# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006213 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006214 defined $stat &&
6215 $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 -07006216 my $oldfunc = $3;
6217 my $a1 = $4;
6218 my $a2 = $10;
6219 my $newfunc = "kmalloc_array";
6220 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006221 my $r1 = $a1;
6222 my $r2 = $a2;
6223 if ($a1 =~ /^sizeof\s*\S/) {
6224 $r1 = $a2;
6225 $r2 = $a1;
6226 }
6227 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6228 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006229 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006230 my $herectx = get_stat_here($linenr, $cnt, $here);
6231
Joe Perches60a55362014-06-04 16:12:07 -07006232 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006233 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6234 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006235 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006236 $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 -07006237 }
6238 }
6239 }
6240
Joe Perches972fdea2013-04-29 16:18:12 -07006241# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006242 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006243 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6244 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006245 WARN("KREALLOC_ARG_REUSE",
6246 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6247 }
6248
Joe Perches5ce59ae2013-02-21 16:44:18 -08006249# check for alloc argument mismatch
6250 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6251 WARN("ALLOC_ARRAY_ARGS",
6252 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6253 }
6254
Joe Perchescaf2a542011-01-12 16:59:56 -08006255# check for multiple semicolons
6256 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006257 if (WARN("ONE_SEMICOLON",
6258 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6259 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006260 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006261 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006262 }
6263
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006264# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6265 if ($realfile !~ m@^include/uapi/@ &&
6266 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006267 my $ull = "";
6268 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6269 if (CHK("BIT_MACRO",
6270 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6271 $fix) {
6272 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6273 }
6274 }
6275
Joe Perches2d632742016-05-20 17:04:00 -07006276# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6277 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6278 my $config = $1;
6279 if (WARN("PREFER_IS_ENABLED",
6280 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6281 $fix) {
6282 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6283 }
6284 }
6285
Joe Perchese81f2392014-08-06 16:11:25 -07006286# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006287 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6288 my $has_break = 0;
6289 my $has_statement = 0;
6290 my $count = 0;
6291 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006292 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006293 $prevline--;
6294 my $rline = $rawlines[$prevline - 1];
6295 my $fline = $lines[$prevline - 1];
6296 last if ($fline =~ /^\@\@/);
6297 next if ($fline =~ /^\-/);
6298 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6299 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6300 next if ($fline =~ /^.[\s$;]*$/);
6301 $has_statement = 1;
6302 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006303 $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 -08006304 }
6305 if (!$has_break && $has_statement) {
6306 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006307 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006308 }
6309 }
6310
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006311# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006312 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006313 defined $stat &&
6314 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006315 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006316 my $herectx = get_stat_here($linenr, $cnt, $here);
6317
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006318 WARN("DEFAULT_NO_BREAK",
6319 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006320 }
6321
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006322# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006323 if ($line =~ /\b__FUNCTION__\b/) {
6324 if (WARN("USE_FUNC",
6325 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6326 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006327 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006328 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006329 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006330
Joe Perches62ec8182015-02-13 14:38:18 -08006331# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6332 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6333 ERROR("DATE_TIME",
6334 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6335 }
6336
Joe Perches2c924882012-03-23 15:02:20 -07006337# check for use of yield()
6338 if ($line =~ /\byield\s*\(\s*\)/) {
6339 WARN("YIELD",
6340 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6341 }
6342
Joe Perches179f8f42013-07-03 15:05:30 -07006343# check for comparisons against true and false
6344 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6345 my $lead = $1;
6346 my $arg = $2;
6347 my $test = $3;
6348 my $otype = $4;
6349 my $trail = $5;
6350 my $op = "!";
6351
6352 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6353
6354 my $type = lc($otype);
6355 if ($type =~ /^(?:true|false)$/) {
6356 if (("$test" eq "==" && "$type" eq "true") ||
6357 ("$test" eq "!=" && "$type" eq "false")) {
6358 $op = "";
6359 }
6360
6361 CHK("BOOL_COMPARISON",
6362 "Using comparison to $otype is error prone\n" . $herecurr);
6363
6364## maybe suggesting a correct construct would better
6365## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6366
6367 }
6368 }
6369
Joe Perches5d430902018-04-10 16:34:25 -07006370# check for bool bitfields
6371 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6372 WARN("BOOL_BITFIELD",
6373 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6374 }
6375
Joe Perchesd7295932018-08-21 21:57:26 -07006376# check for bool use in .h files
6377 if ($realfile =~ /\.h$/ &&
6378 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6379 CHK("BOOL_MEMBER",
6380 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6381 }
6382
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006383# check for semaphores initialized locked
6384 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006385 WARN("CONSIDER_COMPLETION",
6386 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006387 }
Joe Perches6712d852012-03-23 15:02:20 -07006388
Joe Perches67d0a072011-10-31 17:13:10 -07006389# recommend kstrto* over simple_strto* and strict_strto*
6390 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006391 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006392 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006393 }
Joe Perches6712d852012-03-23 15:02:20 -07006394
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006395# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006396 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006397 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006398 "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 -07006399 }
Joe Perches6712d852012-03-23 15:02:20 -07006400
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006401# check for spin_is_locked(), suggest lockdep instead
6402 if ($line =~ /\bspin_is_locked\(/) {
6403 WARN("USE_LOCKDEP",
6404 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6405 }
6406
Joe Perches9189c7e2018-09-07 15:26:18 -07006407# check for deprecated apis
6408 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6409 my $deprecated_api = $1;
6410 my $new_api = $deprecated_apis{$deprecated_api};
6411 WARN("DEPRECATED_API",
6412 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6413 }
6414
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006415# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006416# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006417 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006418 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006419 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006420 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006421 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006422
6423# use of NR_CPUS is usually wrong
6424# ignore definitions of NR_CPUS and usage to define arrays as likely right
6425 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006426 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6427 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006428 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6429 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6430 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006431 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006432 WARN("NR_CPUS",
6433 "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 -07006434 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006435
Joe Perches52ea8502013-11-12 15:10:09 -08006436# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6437 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6438 ERROR("DEFINE_ARCH_HAS",
6439 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6440 }
6441
Joe Perchesacd93622015-02-13 14:38:38 -08006442# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006443 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006444 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6445 WARN("LIKELY_MISUSE",
6446 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6447 }
6448
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006449# whine mightly about in_atomic
6450 if ($line =~ /\bin_atomic\s*\(/) {
6451 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006452 ERROR("IN_ATOMIC",
6453 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006454 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006455 WARN("IN_ATOMIC",
6456 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006457 }
6458 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006459
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006460# check for mutex_trylock_recursive usage
6461 if ($line =~ /mutex_trylock_recursive/) {
6462 ERROR("LOCKING",
6463 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6464 }
6465
Peter Zijlstra1704f472010-03-19 01:37:42 +01006466# check for lockdep_set_novalidate_class
6467 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6468 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6469 if ($realfile !~ m@^kernel/lockdep@ &&
6470 $realfile !~ m@^include/linux/lockdep@ &&
6471 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006472 ERROR("LOCKDEP",
6473 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006474 }
6475 }
Dave Jones88f88312011-01-12 16:59:59 -08006476
Joe Perchesb392c642015-04-16 12:44:16 -07006477 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6478 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006479 WARN("EXPORTED_WORLD_WRITABLE",
6480 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006481 }
Joe Perches24358802014-04-03 14:49:13 -07006482
Joe Perches00180462018-02-06 15:38:55 -08006483# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6484# and whether or not function naming is typical and if
6485# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006486 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006487 defined $stat &&
6488 $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*\)/) {
6489 my $var = $1;
6490 my $perms = $2;
6491 my $show = $3;
6492 my $store = $4;
6493 my $octal_perms = perms_to_octal($perms);
6494 if ($show =~ /^${var}_show$/ &&
6495 $store =~ /^${var}_store$/ &&
6496 $octal_perms eq "0644") {
6497 if (WARN("DEVICE_ATTR_RW",
6498 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6499 $fix) {
6500 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6501 }
6502 } elsif ($show =~ /^${var}_show$/ &&
6503 $store =~ /^NULL$/ &&
6504 $octal_perms eq "0444") {
6505 if (WARN("DEVICE_ATTR_RO",
6506 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6507 $fix) {
6508 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6509 }
6510 } elsif ($show =~ /^NULL$/ &&
6511 $store =~ /^${var}_store$/ &&
6512 $octal_perms eq "0200") {
6513 if (WARN("DEVICE_ATTR_WO",
6514 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6515 $fix) {
6516 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6517 }
6518 } elsif ($octal_perms eq "0644" ||
6519 $octal_perms eq "0444" ||
6520 $octal_perms eq "0200") {
6521 my $newshow = "$show";
6522 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6523 my $newstore = $store;
6524 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6525 my $rename = "";
6526 if ($show ne $newshow) {
6527 $rename .= " '$show' to '$newshow'";
6528 }
6529 if ($store ne $newstore) {
6530 $rename .= " '$store' to '$newstore'";
6531 }
6532 WARN("DEVICE_ATTR_FUNCTIONS",
6533 "Consider renaming function(s)$rename\n" . $herecurr);
6534 } else {
6535 WARN("DEVICE_ATTR_PERMS",
6536 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6537 }
6538 }
6539
Joe Perches515a2352014-04-03 14:49:24 -07006540# Mode permission misuses where it seems decimal should be octal
6541# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006542# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6543# specific definition of not visible in sysfs.
6544# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6545# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006546 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006547 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006548 $line =~ /$mode_perms_search/) {
6549 foreach my $entry (@mode_permission_funcs) {
6550 my $func = $entry->[0];
6551 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006552
Joe Perches459cf0a2016-10-11 13:52:19 -07006553 my $lc = $stat =~ tr@\n@@;
6554 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006555 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006556
Joe Perches515a2352014-04-03 14:49:24 -07006557 my $skip_args = "";
6558 if ($arg_pos > 1) {
6559 $arg_pos--;
6560 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6561 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006562 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006563 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006564 my $val = $1;
6565 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006566 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6567 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6568 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006569 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006570 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006571 }
6572 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006573 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006574 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006575 }
Joe Perches24358802014-04-03 14:49:13 -07006576 }
6577 }
6578 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006579
Joe Perches459cf0a2016-10-11 13:52:19 -07006580# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006581 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006582 my $oval = $1;
6583 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006584 if (WARN("SYMBOLIC_PERMS",
6585 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6586 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006587 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006588 }
6589 }
6590
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006591# validate content of MODULE_LICENSE against list from include/linux/module.h
6592 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6593 my $extracted_string = get_quoted_string($line, $rawline);
6594 my $valid_licenses = qr{
6595 GPL|
6596 GPL\ v2|
6597 GPL\ and\ additional\ rights|
6598 Dual\ BSD/GPL|
6599 Dual\ MIT/GPL|
6600 Dual\ MPL/GPL|
6601 Proprietary
6602 }x;
6603 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6604 WARN("MODULE_LICENSE",
6605 "unknown module license " . $extracted_string . "\n" . $herecurr);
6606 }
6607 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006608 }
6609
6610 # If we have no input at all, then there is nothing to report on
6611 # so just keep quiet.
6612 if ($#rawlines == -1) {
6613 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006614 }
6615
Andy Whitcroft8905a672007-11-28 16:21:06 -08006616 # In mailback mode only produce a report in the negative, for
6617 # things that appear to be patches.
6618 if ($mailback && ($clean == 1 || !$is_patch)) {
6619 exit(0);
6620 }
6621
6622 # This is not a patch, and we are are in 'no-patch' mode so
6623 # just keep quiet.
6624 if (!$chk_patch && !$is_patch) {
6625 exit(0);
6626 }
6627
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006628 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006629 ERROR("NOT_UNIFIED_DIFF",
6630 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006631 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006632 if ($is_patch && $has_commit_log && $chk_signoff) {
6633 if ($signoff == 0) {
6634 ERROR("MISSING_SIGN_OFF",
6635 "Missing Signed-off-by: line(s)\n");
6636 } elsif (!$authorsignoff) {
6637 WARN("NO_AUTHOR_SIGN_OFF",
6638 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6639 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006640 }
6641
Andy Whitcroft8905a672007-11-28 16:21:06 -08006642 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006643 if ($summary && !($clean == 1 && $quiet == 1)) {
6644 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006645 print "total: $cnt_error errors, $cnt_warn warnings, " .
6646 (($check)? "$cnt_chk checks, " : "") .
6647 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006648 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006649
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006650 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006651 # If there were any defects found and not already fixing them
6652 if (!$clean and !$fix) {
6653 print << "EOM"
6654
6655NOTE: For some of the reported defects, checkpatch may be able to
6656 mechanically convert to the typical style using --fix or --fix-inplace.
6657EOM
6658 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006659 # If there were whitespace errors which cleanpatch can fix
6660 # then suggest that.
6661 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006662 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006663 print << "EOM"
6664
6665NOTE: Whitespace errors detected.
6666 You may wish to use scripts/cleanpatch or scripts/cleanfile
6667EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006668 }
6669 }
6670
Joe Perchesd752fcc2014-08-06 16:11:05 -07006671 if ($clean == 0 && $fix &&
6672 ("@rawlines" ne "@fixed" ||
6673 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006674 my $newfile = $filename;
6675 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006676 my $linecount = 0;
6677 my $f;
6678
Joe Perchesd752fcc2014-08-06 16:11:05 -07006679 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6680
Joe Perches3705ce52013-07-03 15:05:31 -07006681 open($f, '>', $newfile)
6682 or die "$P: Can't open $newfile for write\n";
6683 foreach my $fixed_line (@fixed) {
6684 $linecount++;
6685 if ($file) {
6686 if ($linecount > 3) {
6687 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006688 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006689 }
6690 } else {
6691 print $f $fixed_line . "\n";
6692 }
6693 }
6694 close($f);
6695
6696 if (!$quiet) {
6697 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006698
Joe Perches3705ce52013-07-03 15:05:31 -07006699Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6700
6701Do _NOT_ trust the results written to this file.
6702Do _NOT_ submit these changes without inspecting them for correctness.
6703
6704This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6705No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006706EOM
6707 }
6708 }
6709
Joe Perchesd8469f12015-06-25 15:03:00 -07006710 if ($quiet == 0) {
6711 print "\n";
6712 if ($clean == 1) {
6713 print "$vname has no obvious style problems and is ready for submission.\n";
6714 } else {
6715 print "$vname has style problems, please review.\n";
6716 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006717 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006718 return $clean;
6719}