blob: 5971b0c1161630c0d792a856d34a57c59f7e30ab [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|
383 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700384 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700385 ____cacheline_aligned|
386 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800387 ____cacheline_internodealigned_in_smp|
388 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700390our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700391our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
393our $Lval = qr{$Ident(?:$Member)*};
394
Joe Perches95e2c602013-07-03 15:05:20 -0700395our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
396our $Binary = qr{(?i)0b[01]+$Int_type?};
397our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
398our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700399our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800400our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800401our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
402our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
403our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800404our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700405our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800406our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700407our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700408our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700409our $Operators = qr{
410 <=|>=|==|!=|
411 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700412 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700413 }x;
414
Joe Perches91cb5192014-04-03 14:49:32 -0700415our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
416
Joe Perchesab7e23f2015-04-16 12:44:22 -0700417our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800418our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700419our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700420our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800421our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700422our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800423our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700424our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800425
Joe Perches15662b32011-10-31 17:13:12 -0700426our $NON_ASCII_UTF8 = qr{
427 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700428 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
429 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
430 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
431 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
432 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
433 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
434}x;
435
Joe Perches15662b32011-10-31 17:13:12 -0700436our $UTF8 = qr{
437 [\x09\x0A\x0D\x20-\x7E] # ASCII
438 | $NON_ASCII_UTF8
439}x;
440
Joe Perchese6176fa2015-06-25 15:02:49 -0700441our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800442our $typeOtherOSTypedefs = qr{(?x:
443 u_(?:char|short|int|long) | # bsd
444 u(?:nchar|short|int|long) # sysv
445)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700446our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700447 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700448 atomic_t
449)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700450our $typeTypedefs = qr{(?x:
451 $typeC99Typedefs\b|
452 $typeOtherOSTypedefs\b|
453 $typeKernelTypedefs\b
454)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700455
Joe Perches6d32f7a2015-11-06 16:31:37 -0800456our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
457
Joe Perches691e6692010-03-05 13:43:51 -0800458our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800459 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700460 (?:[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 -0800461 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700462 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700463 panic|
Joe Perches06668722013-11-12 15:10:07 -0800464 MODULE_[A-Z_]+|
465 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800466)};
467
Joe Perches20112472011-07-25 17:13:23 -0700468our $signature_tags = qr{(?xi:
469 Signed-off-by:|
470 Acked-by:|
471 Tested-by:|
472 Reviewed-by:|
473 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700474 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700475 To:|
476 Cc:
477)};
478
Joe Perches18130872014-08-06 16:11:22 -0700479our @typeListMisordered = (
480 qr{char\s+(?:un)?signed},
481 qr{int\s+(?:(?:un)?signed\s+)?short\s},
482 qr{int\s+short(?:\s+(?:un)?signed)},
483 qr{short\s+int(?:\s+(?:un)?signed)},
484 qr{(?:un)?signed\s+int\s+short},
485 qr{short\s+(?:un)?signed},
486 qr{long\s+int\s+(?:un)?signed},
487 qr{int\s+long\s+(?:un)?signed},
488 qr{long\s+(?:un)?signed\s+int},
489 qr{int\s+(?:un)?signed\s+long},
490 qr{int\s+(?:un)?signed},
491 qr{int\s+long\s+long\s+(?:un)?signed},
492 qr{long\s+long\s+int\s+(?:un)?signed},
493 qr{long\s+long\s+(?:un)?signed\s+int},
494 qr{long\s+long\s+(?:un)?signed},
495 qr{long\s+(?:un)?signed},
496);
497
Andy Whitcroft8905a672007-11-28 16:21:06 -0800498our @typeList = (
499 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700500 qr{(?:(?:un)?signed\s+)?char},
501 qr{(?:(?:un)?signed\s+)?short\s+int},
502 qr{(?:(?:un)?signed\s+)?short},
503 qr{(?:(?:un)?signed\s+)?int},
504 qr{(?:(?:un)?signed\s+)?long\s+int},
505 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
506 qr{(?:(?:un)?signed\s+)?long\s+long},
507 qr{(?:(?:un)?signed\s+)?long},
508 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509 qr{float},
510 qr{double},
511 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800512 qr{struct\s+$Ident},
513 qr{union\s+$Ident},
514 qr{enum\s+$Ident},
515 qr{${Ident}_t},
516 qr{${Ident}_handler},
517 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700518 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800519);
Joe Perches938224b2016-01-20 14:59:15 -0800520
521our $C90_int_types = qr{(?x:
522 long\s+long\s+int\s+(?:un)?signed|
523 long\s+long\s+(?:un)?signed\s+int|
524 long\s+long\s+(?:un)?signed|
525 (?:(?:un)?signed\s+)?long\s+long\s+int|
526 (?:(?:un)?signed\s+)?long\s+long|
527 int\s+long\s+long\s+(?:un)?signed|
528 int\s+(?:(?:un)?signed\s+)?long\s+long|
529
530 long\s+int\s+(?:un)?signed|
531 long\s+(?:un)?signed\s+int|
532 long\s+(?:un)?signed|
533 (?:(?:un)?signed\s+)?long\s+int|
534 (?:(?:un)?signed\s+)?long|
535 int\s+long\s+(?:un)?signed|
536 int\s+(?:(?:un)?signed\s+)?long|
537
538 int\s+(?:un)?signed|
539 (?:(?:un)?signed\s+)?int
540)};
541
Alex Dowad485ff232015-06-25 15:02:52 -0700542our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700543our @typeListWithAttr = (
544 @typeList,
545 qr{struct\s+$InitAttribute\s+$Ident},
546 qr{union\s+$InitAttribute\s+$Ident},
547);
548
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700549our @modifierList = (
550 qr{fastcall},
551);
Alex Dowad485ff232015-06-25 15:02:52 -0700552our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800553
Joe Perches24358802014-04-03 14:49:13 -0700554our @mode_permission_funcs = (
555 ["module_param", 3],
556 ["module_param_(?:array|named|string)", 4],
557 ["module_param_array_named", 5],
558 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
559 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700560 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
561 ["IIO_DEV_ATTR_[A-Z_]+", 1],
562 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
563 ["SENSOR_TEMPLATE(?:_2|)", 3],
564 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700565);
566
Joe Perches515a2352014-04-03 14:49:24 -0700567#Create a search pattern for all these functions to speed up a loop below
568our $mode_perms_search = "";
569foreach my $entry (@mode_permission_funcs) {
570 $mode_perms_search .= '|' if ($mode_perms_search ne "");
571 $mode_perms_search .= $entry->[0];
572}
Joe Perches00180462018-02-06 15:38:55 -0800573$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700574
Joe Perchesb392c642015-04-16 12:44:16 -0700575our $mode_perms_world_writable = qr{
576 S_IWUGO |
577 S_IWOTH |
578 S_IRWXUGO |
579 S_IALLUGO |
580 0[0-7][0-7][2367]
581}x;
582
Joe Perchesf90774e2016-10-11 13:51:47 -0700583our %mode_permission_string_types = (
584 "S_IRWXU" => 0700,
585 "S_IRUSR" => 0400,
586 "S_IWUSR" => 0200,
587 "S_IXUSR" => 0100,
588 "S_IRWXG" => 0070,
589 "S_IRGRP" => 0040,
590 "S_IWGRP" => 0020,
591 "S_IXGRP" => 0010,
592 "S_IRWXO" => 0007,
593 "S_IROTH" => 0004,
594 "S_IWOTH" => 0002,
595 "S_IXOTH" => 0001,
596 "S_IRWXUGO" => 0777,
597 "S_IRUGO" => 0444,
598 "S_IWUGO" => 0222,
599 "S_IXUGO" => 0111,
600);
601
602#Create a search pattern for all these strings to speed up a loop below
603our $mode_perms_string_search = "";
604foreach my $entry (keys %mode_permission_string_types) {
605 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
606 $mode_perms_string_search .= $entry;
607}
Joe Perches00180462018-02-06 15:38:55 -0800608our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
609our $multi_mode_perms_string_search = qr{
610 ${single_mode_perms_string_search}
611 (?:\s*\|\s*${single_mode_perms_string_search})*
612}x;
613
614sub perms_to_octal {
615 my ($string) = @_;
616
617 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
618
619 my $val = "";
620 my $oval = "";
621 my $to = 0;
622 my $curpos = 0;
623 my $lastpos = 0;
624 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
625 $curpos = pos($string);
626 my $match = $2;
627 my $omatch = $1;
628 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
629 $lastpos = $curpos;
630 $to |= $mode_permission_string_types{$match};
631 $val .= '\s*\|\s*' if ($val ne "");
632 $val .= $match;
633 $oval .= $omatch;
634 }
635 $oval =~ s/^\s*\|\s*//;
636 $oval =~ s/\s*\|\s*$//;
637 return sprintf("%04o", $to);
638}
Joe Perchesf90774e2016-10-11 13:51:47 -0700639
Wolfram Sang7840a942010-08-09 17:20:57 -0700640our $allowed_asm_includes = qr{(?x:
641 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700642 memory|
643 time|
644 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700645)};
646# memory.h: ARM has a custom one
647
Kees Cook66b47b42014-10-13 15:51:57 -0700648# Load common spelling mistakes and build regular expression list.
649my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700650my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700651
Joe Perches36061e32014-12-10 15:51:43 -0800652if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800653 while (<$spelling>) {
654 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700655
Joe Perches36061e32014-12-10 15:51:43 -0800656 $line =~ s/\s*\n?$//g;
657 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700658
Joe Perches36061e32014-12-10 15:51:43 -0800659 next if ($line =~ m/^\s*#/);
660 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700661
Joe Perches36061e32014-12-10 15:51:43 -0800662 my ($suspect, $fix) = split(/\|\|/, $line);
663
Joe Perches36061e32014-12-10 15:51:43 -0800664 $spelling_fix{$suspect} = $fix;
665 }
666 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800667} else {
668 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700669}
Kees Cook66b47b42014-10-13 15:51:57 -0700670
Joe Perchesebfd7d62015-04-16 12:44:14 -0700671if ($codespell) {
672 if (open(my $spelling, '<', $codespellfile)) {
673 while (<$spelling>) {
674 my $line = $_;
675
676 $line =~ s/\s*\n?$//g;
677 $line =~ s/^\s*//g;
678
679 next if ($line =~ m/^\s*#/);
680 next if ($line =~ m/^\s*$/);
681 next if ($line =~ m/, disabled/i);
682
683 $line =~ s/,.*$//;
684
685 my ($suspect, $fix) = split(/->/, $line);
686
687 $spelling_fix{$suspect} = $fix;
688 }
689 close($spelling);
690 } else {
691 warn "No codespell typos will be found - file '$codespellfile': $!\n";
692 }
693}
694
695$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
696
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700697sub read_words {
698 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700699
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700700 if (open(my $words, '<', $file)) {
701 while (<$words>) {
702 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700703
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700704 $line =~ s/\s*\n?$//g;
705 $line =~ s/^\s*//g;
706
707 next if ($line =~ m/^\s*#/);
708 next if ($line =~ m/^\s*$/);
709 if ($line =~ /\s/) {
710 print("$file: '$line' invalid - ignored\n");
711 next;
712 }
713
714 $$wordsRef .= '|' if ($$wordsRef ne "");
715 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700716 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700717 close($file);
718 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700719 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700720
721 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700722}
723
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700724my $const_structs = "";
725read_words(\$const_structs, $conststructsfile)
726 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
727
728my $typeOtherTypedefs = "";
729if (length($typedefsfile)) {
730 read_words(\$typeOtherTypedefs, $typedefsfile)
731 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
732}
733$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
734
Andy Whitcroft8905a672007-11-28 16:21:06 -0800735sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700736 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
737 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700738 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700739 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700740 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700741 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700742 (?:$typeTypedefs\b)|
743 (?:${all}\b)
744 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800745 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700746 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800747 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800748 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700749 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700750 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800751 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700752 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800753 }x;
Joe Perches18130872014-08-06 16:11:22 -0700754 $NonptrTypeMisordered = qr{
755 (?:$Modifier\s+|const\s+)*
756 (?:
757 (?:${Misordered}\b)
758 )
759 (?:\s+$Modifier|\s+const)*
760 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700761 $NonptrTypeWithAttr = qr{
762 (?:$Modifier\s+|const\s+)*
763 (?:
764 (?:typeof|__typeof__)\s*\([^\)]*\)|
765 (?:$typeTypedefs\b)|
766 (?:${allWithAttr}\b)
767 )
768 (?:\s+$Modifier|\s+const)*
769 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800770 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700771 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700772 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700773 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800774 }x;
Joe Perches18130872014-08-06 16:11:22 -0700775 $TypeMisordered = qr{
776 $NonptrTypeMisordered
777 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
778 (?:\s+$Inline|\s+$Modifier)*
779 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700780 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700781 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800782}
783build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700784
Joe Perches7d2367a2011-07-25 17:13:22 -0700785our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700786
787# Using $balanced_parens, $LvalOrFunc, or $FuncArg
788# requires at least perl version v5.10.0
789# Any use must be runtime checked with $^V
790
791our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700792our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800793our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700794
Joe Perchesf8422302014-08-06 16:11:31 -0700795our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700796 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700797 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700798 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
799 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700800)};
801
Joe Perches7d2367a2011-07-25 17:13:22 -0700802sub deparenthesize {
803 my ($string) = @_;
804 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700805
806 while ($string =~ /^\s*\(.*\)\s*$/) {
807 $string =~ s@^\s*\(\s*@@;
808 $string =~ s@\s*\)\s*$@@;
809 }
810
Joe Perches7d2367a2011-07-25 17:13:22 -0700811 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700812
Joe Perches7d2367a2011-07-25 17:13:22 -0700813 return $string;
814}
815
Joe Perches34456862013-07-03 15:05:34 -0700816sub seed_camelcase_file {
817 my ($file) = @_;
818
819 return if (!(-f $file));
820
821 local $/;
822
823 open(my $include_file, '<', "$file")
824 or warn "$P: Can't read '$file' $!\n";
825 my $text = <$include_file>;
826 close($include_file);
827
828 my @lines = split('\n', $text);
829
830 foreach my $line (@lines) {
831 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
832 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
833 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800834 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
835 $camelcase{$1} = 1;
836 } 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 -0700837 $camelcase{$1} = 1;
838 }
839 }
840}
841
Joe Perches85b0ee12016-10-11 13:51:44 -0700842sub is_maintained_obsolete {
843 my ($filename) = @_;
844
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800845 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700846
Joe Perches0616efa2016-10-11 13:52:02 -0700847 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 -0700848
849 return $status =~ /obsolete/i;
850}
851
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700852sub is_SPDX_License_valid {
853 my ($license) = @_;
854
Joe Perches56294112018-08-21 21:58:04 -0700855 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700856
Joe Perches56294112018-08-21 21:58:04 -0700857 my $root_path = abs_path($root);
858 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700859 return 0 if ($status ne "");
860 return 1;
861}
862
Joe Perches34456862013-07-03 15:05:34 -0700863my $camelcase_seeded = 0;
864sub seed_camelcase_includes {
865 return if ($camelcase_seeded);
866
867 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700868 my $camelcase_cache = "";
869 my @include_files = ();
870
871 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700872
Richard Genoud3645e322014-02-10 14:25:32 -0800873 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700874 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
875 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700876 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700877 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700878 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700879 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700880 @include_files = split('\n', $files);
881 foreach my $file (@include_files) {
882 my $date = POSIX::strftime("%Y%m%d%H%M",
883 localtime((stat $file)[9]));
884 $last_mod_date = $date if ($last_mod_date < $date);
885 }
886 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700887 }
Joe Perchesc707a812013-07-08 16:00:43 -0700888
889 if ($camelcase_cache ne "" && -f $camelcase_cache) {
890 open(my $camelcase_file, '<', "$camelcase_cache")
891 or warn "$P: Can't read '$camelcase_cache' $!\n";
892 while (<$camelcase_file>) {
893 chomp;
894 $camelcase{$_} = 1;
895 }
896 close($camelcase_file);
897
898 return;
899 }
900
Richard Genoud3645e322014-02-10 14:25:32 -0800901 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700902 $files = `git ls-files "include/*.h"`;
903 @include_files = split('\n', $files);
904 }
905
Joe Perches34456862013-07-03 15:05:34 -0700906 foreach my $file (@include_files) {
907 seed_camelcase_file($file);
908 }
Joe Perches351b2a12013-07-03 15:05:36 -0700909
Joe Perchesc707a812013-07-08 16:00:43 -0700910 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700911 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700912 open(my $camelcase_file, '>', "$camelcase_cache")
913 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700914 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
915 print $camelcase_file ("$_\n");
916 }
917 close($camelcase_file);
918 }
Joe Perches34456862013-07-03 15:05:34 -0700919}
920
Joe Perchesd311cd42014-08-06 16:10:57 -0700921sub git_commit_info {
922 my ($commit, $id, $desc) = @_;
923
924 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
925
926 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
927 $output =~ s/^\s*//gm;
928 my @lines = split("\n", $output);
929
Joe Perches0d7835f2015-02-13 14:38:35 -0800930 return ($id, $desc) if ($#lines < 0);
931
Joe Perchesd311cd42014-08-06 16:10:57 -0700932 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
933# Maybe one day convert this block of bash into something that returns
934# all matching commit ids, but it's very slow...
935#
936# echo "checking commits $1..."
937# git rev-list --remotes | grep -i "^$1" |
938# while read line ; do
939# git log --format='%H %s' -1 $line |
940# echo "commit $(cut -c 1-12,41-)"
941# done
942 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700943 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700944 } else {
945 $id = substr($lines[0], 0, 12);
946 $desc = substr($lines[0], 41);
947 }
948
949 return ($id, $desc);
950}
951
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700952$chk_signoff = 0 if ($file);
953
Andy Whitcroft00df3442007-06-08 13:47:06 -0700954my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800955my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700956my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700957my @fixed_inserted = ();
958my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700959my $fixlinenr = -1;
960
Du, Changbin4a593c32016-05-20 17:04:16 -0700961# If input is git commits, extract all commits from the commit expressions.
962# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
963die "$P: No git repository found\n" if ($git && !-e ".git");
964
965if ($git) {
966 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700967 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700968 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700969 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
970 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700971 } elsif ($commit_expr =~ m/\.\./) {
972 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700973 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700974 $git_range = "-1 $commit_expr";
975 }
976 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
977 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700978 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
979 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700980 my $sha1 = $1;
981 my $subject = $2;
982 unshift(@commits, $sha1);
983 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700984 }
985 }
986 die "$P: no git commits after extraction!\n" if (@commits == 0);
987 @ARGV = @commits;
988}
989
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800990my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700991for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800992 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700993 if ($git) {
994 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
995 die "$P: $filename: git format-patch failed - $!\n";
996 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800997 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700998 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800999 } elsif ($filename eq '-') {
1000 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001001 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001002 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001003 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001004 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001005 if ($filename eq '-') {
1006 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001007 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001008 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001009 } else {
1010 $vname = $filename;
1011 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001012 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001013 chomp;
1014 push(@rawlines, $_);
1015 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001016 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001017
1018 if ($#ARGV > 0 && $quiet == 0) {
1019 print '-' x length($vname) . "\n";
1020 print "$vname\n";
1021 print '-' x length($vname) . "\n";
1022 }
1023
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001024 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001025 $exit = 1;
1026 }
1027 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001028 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001029 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001030 @fixed_inserted = ();
1031 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001032 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001033 @modifierListFile = ();
1034 @typeListFile = ();
1035 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001036}
1037
Joe Perchesd8469f12015-06-25 15:03:00 -07001038if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001039 hash_show_words(\%use_type, "Used");
1040 hash_show_words(\%ignore_type, "Ignored");
1041
Joe Perches5b579802018-08-21 21:57:33 -07001042 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001043 print << "EOM"
1044
1045NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001046 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001047EOM
1048 }
1049 if ($exit) {
1050 print << "EOM"
1051
1052NOTE: If any of the errors are false positives, please report
1053 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1054EOM
1055 }
1056}
1057
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001058exit($exit);
1059
1060sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001061 my ($root) = @_;
1062
1063 my @tree_check = (
1064 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1065 "README", "Documentation", "arch", "include", "drivers",
1066 "fs", "init", "ipc", "kernel", "lib", "scripts",
1067 );
1068
1069 foreach my $check (@tree_check) {
1070 if (! -e $root . '/' . $check) {
1071 return 0;
1072 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001073 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001074 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001075}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001076
Joe Perches20112472011-07-25 17:13:23 -07001077sub parse_email {
1078 my ($formatted_email) = @_;
1079
1080 my $name = "";
1081 my $address = "";
1082 my $comment = "";
1083
1084 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1085 $name = $1;
1086 $address = $2;
1087 $comment = $3 if defined $3;
1088 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1089 $address = $1;
1090 $comment = $2 if defined $2;
1091 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1092 $address = $1;
1093 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001094 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001095 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001096 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001097 $name =~ s/^\"|\"$//g;
1098 # If there's a name left after stripping spaces and
1099 # leading quotes, and the address doesn't have both
1100 # leading and trailing angle brackets, the address
1101 # is invalid. ie:
1102 # "joe smith joe@smith.com" bad
1103 # "joe smith <joe@smith.com" bad
1104 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1105 $name = "";
1106 $address = "";
1107 $comment = "";
1108 }
1109 }
1110
Joe Perches3705ce52013-07-03 15:05:31 -07001111 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001112 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001113 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001114 $address =~ s/^\<|\>$//g;
1115
1116 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1117 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1118 $name = "\"$name\"";
1119 }
1120
1121 return ($name, $address, $comment);
1122}
1123
1124sub format_email {
1125 my ($name, $address) = @_;
1126
1127 my $formatted_email;
1128
Joe Perches3705ce52013-07-03 15:05:31 -07001129 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001130 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001131 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001132
1133 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1134 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1135 $name = "\"$name\"";
1136 }
1137
1138 if ("$name" eq "") {
1139 $formatted_email = "$address";
1140 } else {
1141 $formatted_email = "$name <$address>";
1142 }
1143
1144 return $formatted_email;
1145}
1146
Joe Perchesd311cd42014-08-06 16:10:57 -07001147sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001148 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001149
Joe Perchesbd474ca2014-08-06 16:11:10 -07001150 foreach my $path (split(/:/, $ENV{PATH})) {
1151 if (-e "$path/$bin") {
1152 return "$path/$bin";
1153 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001154 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001155
Joe Perchesbd474ca2014-08-06 16:11:10 -07001156 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001157}
1158
Joe Perches000d1cc12011-07-25 17:13:25 -07001159sub which_conf {
1160 my ($conf) = @_;
1161
1162 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1163 if (-e "$path/$conf") {
1164 return "$path/$conf";
1165 }
1166 }
1167
1168 return "";
1169}
1170
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001171sub expand_tabs {
1172 my ($str) = @_;
1173
1174 my $res = '';
1175 my $n = 0;
1176 for my $c (split(//, $str)) {
1177 if ($c eq "\t") {
1178 $res .= ' ';
1179 $n++;
1180 for (; ($n % 8) != 0; $n++) {
1181 $res .= ' ';
1182 }
1183 next;
1184 }
1185 $res .= $c;
1186 $n++;
1187 }
1188
1189 return $res;
1190}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001191sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001192 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001193 return $res;
1194}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001195
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001196sub line_stats {
1197 my ($line) = @_;
1198
1199 # Drop the diff line leader and expand tabs
1200 $line =~ s/^.//;
1201 $line = expand_tabs($line);
1202
1203 # Pick the indent from the front of the line.
1204 my ($white) = ($line =~ /^(\s*)/);
1205
1206 return (length($line), length($white));
1207}
1208
Andy Whitcroft773647a2008-03-28 14:15:58 -07001209my $sanitise_quote = '';
1210
1211sub sanitise_line_reset {
1212 my ($in_comment) = @_;
1213
1214 if ($in_comment) {
1215 $sanitise_quote = '*/';
1216 } else {
1217 $sanitise_quote = '';
1218 }
1219}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001220sub sanitise_line {
1221 my ($line) = @_;
1222
1223 my $res = '';
1224 my $l = '';
1225
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001226 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001227 my $off = 0;
1228 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001229
Andy Whitcroft773647a2008-03-28 14:15:58 -07001230 # Always copy over the diff marker.
1231 $res = substr($line, 0, 1);
1232
1233 for ($off = 1; $off < length($line); $off++) {
1234 $c = substr($line, $off, 1);
1235
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001236 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001237 # and end, all to $;.
1238 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1239 $sanitise_quote = '*/';
1240
1241 substr($res, $off, 2, "$;$;");
1242 $off++;
1243 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001244 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001245 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001246 $sanitise_quote = '';
1247 substr($res, $off, 2, "$;$;");
1248 $off++;
1249 next;
1250 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001251 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1252 $sanitise_quote = '//';
1253
1254 substr($res, $off, 2, $sanitise_quote);
1255 $off++;
1256 next;
1257 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001258
1259 # A \ in a string means ignore the next character.
1260 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1261 $c eq "\\") {
1262 substr($res, $off, 2, 'XX');
1263 $off++;
1264 next;
1265 }
1266 # Regular quotes.
1267 if ($c eq "'" || $c eq '"') {
1268 if ($sanitise_quote eq '') {
1269 $sanitise_quote = $c;
1270
1271 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001272 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001273 } elsif ($sanitise_quote eq $c) {
1274 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001275 }
1276 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001277
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001278 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001279 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1280 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001281 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1282 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001283 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1284 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001285 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001286 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001287 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001288 }
1289
Daniel Walker113f04a2009-09-21 17:04:35 -07001290 if ($sanitise_quote eq '//') {
1291 $sanitise_quote = '';
1292 }
1293
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001294 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001295 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001296 my $clean = 'X' x length($1);
1297 $res =~ s@\<.*\>@<$clean>@;
1298
1299 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001300 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001301 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001302 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001303 }
1304
Joe Perchesdadf6802016-08-02 14:04:33 -07001305 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1306 my $match = $1;
1307 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1308 }
1309
Andy Whitcroft00df3442007-06-08 13:47:06 -07001310 return $res;
1311}
1312
Joe Perchesa6962d72013-04-29 16:18:13 -07001313sub get_quoted_string {
1314 my ($line, $rawline) = @_;
1315
Joe Perches478b1792018-04-10 16:33:34 -07001316 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001317 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001318 return substr($rawline, $-[0], $+[0] - $-[0]);
1319}
1320
Andy Whitcroft8905a672007-11-28 16:21:06 -08001321sub ctx_statement_block {
1322 my ($linenr, $remain, $off) = @_;
1323 my $line = $linenr - 1;
1324 my $blk = '';
1325 my $soff = $off;
1326 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001327 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001328
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001329 my $loff = 0;
1330
Andy Whitcroft8905a672007-11-28 16:21:06 -08001331 my $type = '';
1332 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001333 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001334 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001335 my $c;
1336 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001337
1338 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001339 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001340 @stack = (['', 0]) if ($#stack == -1);
1341
Andy Whitcroft773647a2008-03-28 14:15:58 -07001342 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001343 # If we are about to drop off the end, pull in more
1344 # context.
1345 if ($off >= $len) {
1346 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001347 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001348 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001349 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001350 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001351 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001352 $len = length($blk);
1353 $line++;
1354 last;
1355 }
1356 # Bail if there is no further context.
1357 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001358 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001359 last;
1360 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001361 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1362 $level++;
1363 $type = '#';
1364 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001365 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001366 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001367 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001368 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001369
Andy Whitcroft773647a2008-03-28 14:15:58 -07001370 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001371
1372 # Handle nested #if/#else.
1373 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1374 push(@stack, [ $type, $level ]);
1375 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1376 ($type, $level) = @{$stack[$#stack - 1]};
1377 } elsif ($remainder =~ /^#\s*endif\b/) {
1378 ($type, $level) = @{pop(@stack)};
1379 }
1380
Andy Whitcroft8905a672007-11-28 16:21:06 -08001381 # Statement ends at the ';' or a close '}' at the
1382 # outermost level.
1383 if ($level == 0 && $c eq ';') {
1384 last;
1385 }
1386
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001387 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001388 if ($level == 0 && $coff_set == 0 &&
1389 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1390 $remainder =~ /^(else)(?:\s|{)/ &&
1391 $remainder !~ /^else\s+if\b/) {
1392 $coff = $off + length($1) - 1;
1393 $coff_set = 1;
1394 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1395 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001396 }
1397
Andy Whitcroft8905a672007-11-28 16:21:06 -08001398 if (($type eq '' || $type eq '(') && $c eq '(') {
1399 $level++;
1400 $type = '(';
1401 }
1402 if ($type eq '(' && $c eq ')') {
1403 $level--;
1404 $type = ($level != 0)? '(' : '';
1405
1406 if ($level == 0 && $coff < $soff) {
1407 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001408 $coff_set = 1;
1409 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001410 }
1411 }
1412 if (($type eq '' || $type eq '{') && $c eq '{') {
1413 $level++;
1414 $type = '{';
1415 }
1416 if ($type eq '{' && $c eq '}') {
1417 $level--;
1418 $type = ($level != 0)? '{' : '';
1419
1420 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001421 if (substr($blk, $off + 1, 1) eq ';') {
1422 $off++;
1423 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001424 last;
1425 }
1426 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001427 # Preprocessor commands end at the newline unless escaped.
1428 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1429 $level--;
1430 $type = '';
1431 $off++;
1432 last;
1433 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001434 $off++;
1435 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001436 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001437 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001438 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001439 $line++;
1440 $remain--;
1441 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001442
1443 my $statement = substr($blk, $soff, $off - $soff + 1);
1444 my $condition = substr($blk, $soff, $coff - $soff + 1);
1445
1446 #warn "STATEMENT<$statement>\n";
1447 #warn "CONDITION<$condition>\n";
1448
Andy Whitcroft773647a2008-03-28 14:15:58 -07001449 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001450
1451 return ($statement, $condition,
1452 $line, $remain + 1, $off - $loff + 1, $level);
1453}
1454
Andy Whitcroftcf655042008-03-04 14:28:20 -08001455sub statement_lines {
1456 my ($stmt) = @_;
1457
1458 # Strip the diff line prefixes and rip blank lines at start and end.
1459 $stmt =~ s/(^|\n)./$1/g;
1460 $stmt =~ s/^\s*//;
1461 $stmt =~ s/\s*$//;
1462
1463 my @stmt_lines = ($stmt =~ /\n/g);
1464
1465 return $#stmt_lines + 2;
1466}
1467
1468sub statement_rawlines {
1469 my ($stmt) = @_;
1470
1471 my @stmt_lines = ($stmt =~ /\n/g);
1472
1473 return $#stmt_lines + 2;
1474}
1475
1476sub statement_block_size {
1477 my ($stmt) = @_;
1478
1479 $stmt =~ s/(^|\n)./$1/g;
1480 $stmt =~ s/^\s*{//;
1481 $stmt =~ s/}\s*$//;
1482 $stmt =~ s/^\s*//;
1483 $stmt =~ s/\s*$//;
1484
1485 my @stmt_lines = ($stmt =~ /\n/g);
1486 my @stmt_statements = ($stmt =~ /;/g);
1487
1488 my $stmt_lines = $#stmt_lines + 2;
1489 my $stmt_statements = $#stmt_statements + 1;
1490
1491 if ($stmt_lines > $stmt_statements) {
1492 return $stmt_lines;
1493 } else {
1494 return $stmt_statements;
1495 }
1496}
1497
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001498sub ctx_statement_full {
1499 my ($linenr, $remain, $off) = @_;
1500 my ($statement, $condition, $level);
1501
1502 my (@chunks);
1503
Andy Whitcroftcf655042008-03-04 14:28:20 -08001504 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001505 ($statement, $condition, $linenr, $remain, $off, $level) =
1506 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001507 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001508 push(@chunks, [ $condition, $statement ]);
1509 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1510 return ($level, $linenr, @chunks);
1511 }
1512
1513 # Pull in the following conditional/block pairs and see if they
1514 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001515 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001516 ($statement, $condition, $linenr, $remain, $off, $level) =
1517 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001518 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001519 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001520 #print "C: push\n";
1521 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001522 }
1523
1524 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001525}
1526
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001527sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001528 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001529 my $line;
1530 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001531 my $blk = '';
1532 my @o;
1533 my @c;
1534 my @res = ();
1535
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001536 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001537 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001538 for ($line = $start; $remain > 0; $line++) {
1539 next if ($rawlines[$line] =~ /^-/);
1540 $remain--;
1541
1542 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001543
1544 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001545 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001546 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001547 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001548 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001549 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001550 $level = pop(@stack);
1551 }
1552
Andy Whitcroft01464f32010-10-26 14:23:19 -07001553 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001554 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1555 if ($off > 0) {
1556 $off--;
1557 next;
1558 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001559
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001560 if ($c eq $close && $level > 0) {
1561 $level--;
1562 last if ($level == 0);
1563 } elsif ($c eq $open) {
1564 $level++;
1565 }
1566 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001567
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001568 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001569 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001570 }
1571
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001572 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001573 }
1574
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001575 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001576}
1577sub ctx_block_outer {
1578 my ($linenr, $remain) = @_;
1579
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001580 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1581 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001582}
1583sub ctx_block {
1584 my ($linenr, $remain) = @_;
1585
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001586 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1587 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001588}
1589sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001590 my ($linenr, $remain, $off) = @_;
1591
1592 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1593 return @r;
1594}
1595sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001596 my ($linenr, $remain) = @_;
1597
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001598 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001599}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001600sub ctx_statement_level {
1601 my ($linenr, $remain, $off) = @_;
1602
1603 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1604}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001605
1606sub ctx_locate_comment {
1607 my ($first_line, $end_line) = @_;
1608
1609 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001610 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001611 return $current_comment if (defined $current_comment);
1612
1613 # Look through the context and try and figure out if there is a
1614 # comment.
1615 my $in_comment = 0;
1616 $current_comment = '';
1617 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001618 my $line = $rawlines[$linenr - 1];
1619 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001620 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1621 $in_comment = 1;
1622 }
1623 if ($line =~ m@/\*@) {
1624 $in_comment = 1;
1625 }
1626 if (!$in_comment && $current_comment ne '') {
1627 $current_comment = '';
1628 }
1629 $current_comment .= $line . "\n" if ($in_comment);
1630 if ($line =~ m@\*/@) {
1631 $in_comment = 0;
1632 }
1633 }
1634
1635 chomp($current_comment);
1636 return($current_comment);
1637}
1638sub ctx_has_comment {
1639 my ($first_line, $end_line) = @_;
1640 my $cmt = ctx_locate_comment($first_line, $end_line);
1641
Andy Whitcroft00df3442007-06-08 13:47:06 -07001642 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001643 ##print "CMMT: $cmt\n";
1644
1645 return ($cmt ne '');
1646}
1647
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001648sub raw_line {
1649 my ($linenr, $cnt) = @_;
1650
1651 my $offset = $linenr - 1;
1652 $cnt++;
1653
1654 my $line;
1655 while ($cnt) {
1656 $line = $rawlines[$offset++];
1657 next if (defined($line) && $line =~ /^-/);
1658 $cnt--;
1659 }
1660
1661 return $line;
1662}
1663
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001664sub get_stat_real {
1665 my ($linenr, $lc) = @_;
1666
1667 my $stat_real = raw_line($linenr, 0);
1668 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1669 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1670 }
1671
1672 return $stat_real;
1673}
1674
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001675sub get_stat_here {
1676 my ($linenr, $cnt, $here) = @_;
1677
1678 my $herectx = $here . "\n";
1679 for (my $n = 0; $n < $cnt; $n++) {
1680 $herectx .= raw_line($linenr, $n) . "\n";
1681 }
1682
1683 return $herectx;
1684}
1685
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001686sub cat_vet {
1687 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001688 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001689
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001690 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001691 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1692 $res .= $1;
1693 if ($2 ne '') {
1694 $coded = sprintf("^%c", unpack('C', $2) + 64);
1695 $res .= $coded;
1696 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001697 }
1698 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001699
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001700 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001701}
1702
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001703my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001704my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001705my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001706my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001707
1708sub annotate_reset {
1709 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001710 $av_pending = '_';
1711 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001712 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001713}
1714
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001715sub annotate_values {
1716 my ($stream, $type) = @_;
1717
1718 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001719 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001720 my $cur = $stream;
1721
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001722 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001723
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001724 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001725 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001726 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001727 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001729 print "WS($1)\n" if ($dbg_values > 1);
1730 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001731 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001732 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001733 }
1734
Florian Micklerc023e4732011-01-12 16:59:58 -08001735 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001736 print "CAST($1)\n" if ($dbg_values > 1);
1737 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001738 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001739
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001740 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001741 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001742 $type = 'T';
1743
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001744 } elsif ($cur =~ /^($Modifier)\s*/) {
1745 print "MODIFIER($1)\n" if ($dbg_values > 1);
1746 $type = 'T';
1747
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001748 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001749 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001750 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001751 push(@av_paren_type, $type);
1752 if ($2 ne '') {
1753 $av_pending = 'N';
1754 }
1755 $type = 'E';
1756
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001757 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001758 print "UNDEF($1)\n" if ($dbg_values > 1);
1759 $av_preprocessor = 1;
1760 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001761
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001762 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001763 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001764 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001765
1766 push(@av_paren_type, $type);
1767 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001768 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001769
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001770 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001771 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1772 $av_preprocessor = 1;
1773
1774 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1775
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001776 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001777
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001778 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001779 print "PRE_END($1)\n" if ($dbg_values > 1);
1780
1781 $av_preprocessor = 1;
1782
1783 # Assume all arms of the conditional end as this
1784 # one does, and continue as if the #endif was not here.
1785 pop(@av_paren_type);
1786 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001787 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001788
1789 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001790 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001791
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001792 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1793 print "ATTR($1)\n" if ($dbg_values > 1);
1794 $av_pending = $type;
1795 $type = 'N';
1796
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001797 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001798 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001799 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001800 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801 }
1802 $type = 'N';
1803
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001804 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001805 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001806 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001807 $type = 'N';
1808
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001809 } elsif ($cur =~/^(case)/o) {
1810 print "CASE($1)\n" if ($dbg_values > 1);
1811 $av_pend_colon = 'C';
1812 $type = 'N';
1813
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001814 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001815 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001816 $type = 'N';
1817
1818 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001819 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001820 push(@av_paren_type, $av_pending);
1821 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001822 $type = 'N';
1823
1824 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001825 my $new_type = pop(@av_paren_type);
1826 if ($new_type ne '_') {
1827 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001828 print "PAREN('$1') -> $type\n"
1829 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001830 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001831 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001832 }
1833
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001834 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001835 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001836 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001837 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001838
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001839 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1840 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001841 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001842 } elsif ($type eq 'E') {
1843 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001844 }
1845 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1846 $type = 'V';
1847
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001848 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001849 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001850 $type = 'V';
1851
1852 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001853 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001854 $type = 'N';
1855
Andy Whitcroftcf655042008-03-04 14:28:20 -08001856 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001857 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001858 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001859 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001860
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001861 } elsif ($cur =~/^(,)/) {
1862 print "COMMA($1)\n" if ($dbg_values > 1);
1863 $type = 'C';
1864
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001865 } elsif ($cur =~ /^(\?)/o) {
1866 print "QUESTION($1)\n" if ($dbg_values > 1);
1867 $type = 'N';
1868
1869 } elsif ($cur =~ /^(:)/o) {
1870 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1871
1872 substr($var, length($res), 1, $av_pend_colon);
1873 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1874 $type = 'E';
1875 } else {
1876 $type = 'N';
1877 }
1878 $av_pend_colon = 'O';
1879
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001880 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001881 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001882 $type = 'N';
1883
Andy Whitcroft0d413862008-10-15 22:02:16 -07001884 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001885 my $variant;
1886
1887 print "OPV($1)\n" if ($dbg_values > 1);
1888 if ($type eq 'V') {
1889 $variant = 'B';
1890 } else {
1891 $variant = 'U';
1892 }
1893
1894 substr($var, length($res), 1, $variant);
1895 $type = 'N';
1896
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001897 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001898 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001899 if ($1 ne '++' && $1 ne '--') {
1900 $type = 'N';
1901 }
1902
1903 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001904 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001905 }
1906 if (defined $1) {
1907 $cur = substr($cur, length($1));
1908 $res .= $type x length($1);
1909 }
1910 }
1911
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001912 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001913}
1914
Andy Whitcroft8905a672007-11-28 16:21:06 -08001915sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001916 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001917 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001918 ^(?:
1919 $Modifier|
1920 $Storage|
1921 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001922 DEFINE_\S+
1923 )$|
1924 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001925 goto|
1926 return|
1927 case|
1928 else|
1929 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001930 do|
1931 \#|
1932 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001933 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001934 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001935 )}x;
1936 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1937 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001938 # Check for modifiers.
1939 $possible =~ s/\s*$Storage\s*//g;
1940 $possible =~ s/\s*$Sparse\s*//g;
1941 if ($possible =~ /^\s*$/) {
1942
1943 } elsif ($possible =~ /\s/) {
1944 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001945 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001946 if ($modifier !~ $notPermitted) {
1947 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001948 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001949 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001950 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001951
1952 } else {
1953 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001954 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001955 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001956 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001957 } else {
1958 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001959 }
1960}
1961
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001962my $prefix = '';
1963
Joe Perches000d1cc12011-07-25 17:13:25 -07001964sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001965 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001966
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001967 $type =~ tr/[a-z]/[A-Z]/;
1968
Joe Perchescbec18a2014-04-03 14:49:19 -07001969 return defined $use_type{$type} if (scalar keys %use_type > 0);
1970
1971 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001972}
1973
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001974sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001975 my ($level, $type, $msg) = @_;
1976
1977 if (!show_type($type) ||
1978 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001979 return 0;
1980 }
Joe Perches57230292015-06-25 15:03:03 -07001981 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07001982 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07001983 if ($level eq 'ERROR') {
1984 $output .= RED;
1985 } elsif ($level eq 'WARNING') {
1986 $output .= YELLOW;
1987 } else {
1988 $output .= GREEN;
1989 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001990 }
Joe Perches57230292015-06-25 15:03:03 -07001991 $output .= $prefix . $level . ':';
1992 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07001993 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001994 $output .= "$type:";
1995 }
John Brooks737c0762017-07-10 15:52:24 -07001996 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001997 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001998
1999 if ($showfile) {
2000 my @lines = split("\n", $output, -1);
2001 splice(@lines, 1, 1);
2002 $output = join("\n", @lines);
2003 }
Joe Perches57230292015-06-25 15:03:03 -07002004 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002005
Joe Perches57230292015-06-25 15:03:03 -07002006 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002007
2008 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002009}
Joe Perchescbec18a2014-04-03 14:49:19 -07002010
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002011sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002012 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002013}
Joe Perches000d1cc12011-07-25 17:13:25 -07002014
Joe Perchesd752fcc2014-08-06 16:11:05 -07002015sub fixup_current_range {
2016 my ($lineRef, $offset, $length) = @_;
2017
2018 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2019 my $o = $1;
2020 my $l = $2;
2021 my $no = $o + $offset;
2022 my $nl = $l + $length;
2023 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2024 }
2025}
2026
2027sub fix_inserted_deleted_lines {
2028 my ($linesRef, $insertedRef, $deletedRef) = @_;
2029
2030 my $range_last_linenr = 0;
2031 my $delta_offset = 0;
2032
2033 my $old_linenr = 0;
2034 my $new_linenr = 0;
2035
2036 my $next_insert = 0;
2037 my $next_delete = 0;
2038
2039 my @lines = ();
2040
2041 my $inserted = @{$insertedRef}[$next_insert++];
2042 my $deleted = @{$deletedRef}[$next_delete++];
2043
2044 foreach my $old_line (@{$linesRef}) {
2045 my $save_line = 1;
2046 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002047 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002048 $delta_offset = 0;
2049 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2050 $range_last_linenr = $new_linenr;
2051 fixup_current_range(\$line, $delta_offset, 0);
2052 }
2053
2054 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2055 $deleted = @{$deletedRef}[$next_delete++];
2056 $save_line = 0;
2057 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2058 }
2059
2060 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2061 push(@lines, ${$inserted}{'LINE'});
2062 $inserted = @{$insertedRef}[$next_insert++];
2063 $new_linenr++;
2064 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2065 }
2066
2067 if ($save_line) {
2068 push(@lines, $line);
2069 $new_linenr++;
2070 }
2071
2072 $old_linenr++;
2073 }
2074
2075 return @lines;
2076}
2077
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002078sub fix_insert_line {
2079 my ($linenr, $line) = @_;
2080
2081 my $inserted = {
2082 LINENR => $linenr,
2083 LINE => $line,
2084 };
2085 push(@fixed_inserted, $inserted);
2086}
2087
2088sub fix_delete_line {
2089 my ($linenr, $line) = @_;
2090
2091 my $deleted = {
2092 LINENR => $linenr,
2093 LINE => $line,
2094 };
2095
2096 push(@fixed_deleted, $deleted);
2097}
2098
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002099sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002100 my ($type, $msg) = @_;
2101
2102 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002103 our $clean = 0;
2104 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002105 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002106 }
Joe Perches3705ce52013-07-03 15:05:31 -07002107 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002108}
2109sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002110 my ($type, $msg) = @_;
2111
2112 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002113 our $clean = 0;
2114 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002115 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002116 }
Joe Perches3705ce52013-07-03 15:05:31 -07002117 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002118}
2119sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002120 my ($type, $msg) = @_;
2121
2122 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002123 our $clean = 0;
2124 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002125 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002126 }
Joe Perches3705ce52013-07-03 15:05:31 -07002127 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002128}
2129
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002130sub check_absolute_file {
2131 my ($absolute, $herecurr) = @_;
2132 my $file = $absolute;
2133
2134 ##print "absolute<$absolute>\n";
2135
2136 # See if any suffix of this path is a path within the tree.
2137 while ($file =~ s@^[^/]*/@@) {
2138 if (-f "$root/$file") {
2139 ##print "file<$file>\n";
2140 last;
2141 }
2142 }
2143 if (! -f _) {
2144 return 0;
2145 }
2146
2147 # It is, so see if the prefix is acceptable.
2148 my $prefix = $absolute;
2149 substr($prefix, -length($file)) = '';
2150
2151 ##print "prefix<$prefix>\n";
2152 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002153 WARN("USE_RELATIVE_PATH",
2154 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002155 }
2156}
2157
Joe Perches3705ce52013-07-03 15:05:31 -07002158sub trim {
2159 my ($string) = @_;
2160
Joe Perchesb34c6482013-09-11 14:24:01 -07002161 $string =~ s/^\s+|\s+$//g;
2162
2163 return $string;
2164}
2165
2166sub ltrim {
2167 my ($string) = @_;
2168
2169 $string =~ s/^\s+//;
2170
2171 return $string;
2172}
2173
2174sub rtrim {
2175 my ($string) = @_;
2176
2177 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002178
2179 return $string;
2180}
2181
Joe Perches52ea8502013-11-12 15:10:09 -08002182sub string_find_replace {
2183 my ($string, $find, $replace) = @_;
2184
2185 $string =~ s/$find/$replace/g;
2186
2187 return $string;
2188}
2189
Joe Perches3705ce52013-07-03 15:05:31 -07002190sub tabify {
2191 my ($leading) = @_;
2192
2193 my $source_indent = 8;
2194 my $max_spaces_before_tab = $source_indent - 1;
2195 my $spaces_to_tab = " " x $source_indent;
2196
2197 #convert leading spaces to tabs
2198 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2199 #Remove spaces before a tab
2200 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2201
2202 return "$leading";
2203}
2204
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002205sub pos_last_openparen {
2206 my ($line) = @_;
2207
2208 my $pos = 0;
2209
2210 my $opens = $line =~ tr/\(/\(/;
2211 my $closes = $line =~ tr/\)/\)/;
2212
2213 my $last_openparen = 0;
2214
2215 if (($opens == 0) || ($closes >= $opens)) {
2216 return -1;
2217 }
2218
2219 my $len = length($line);
2220
2221 for ($pos = 0; $pos < $len; $pos++) {
2222 my $string = substr($line, $pos);
2223 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2224 $pos += length($1) - 1;
2225 } elsif (substr($line, $pos, 1) eq '(') {
2226 $last_openparen = $pos;
2227 } elsif (index($string, '(') == -1) {
2228 last;
2229 }
2230 }
2231
Joe Perches91cb5192014-04-03 14:49:32 -07002232 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002233}
2234
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002235sub process {
2236 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002237
2238 my $linenr=0;
2239 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002240 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002241 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002242 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002243
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002244 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002245 my $indent;
2246 my $previndent=0;
2247 my $stashindent=0;
2248
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002249 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002250 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002251 my $author = '';
2252 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002253 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002254 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002255 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002256 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002257 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002258 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002259 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002260 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002261 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002262 my $non_utf8_charset = 0;
2263
Joe Perches365dd4e2014-08-06 16:10:42 -07002264 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002265 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002266
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002267 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002268 our $cnt_lines = 0;
2269 our $cnt_error = 0;
2270 our $cnt_warn = 0;
2271 our $cnt_chk = 0;
2272
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002273 # Trace the real file/line as we go.
2274 my $realfile = '';
2275 my $realline = 0;
2276 my $realcnt = 0;
2277 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002278 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002279 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002280 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002281 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002282 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002283
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002284 my $prev_values = 'E';
2285
2286 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002287 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002288 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002289 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002290 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002291
Joe Perches7e51f192013-09-11 14:23:57 -07002292 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002293
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002294 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002295 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002296 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002297 my @setup_docs = ();
2298 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002299
Joe Perchesd8b07712013-11-12 15:10:06 -08002300 my $camelcase_file_seeded = 0;
2301
Rob Herring9f3a8992018-04-10 16:33:13 -07002302 my $checklicenseline = 1;
2303
Andy Whitcroft773647a2008-03-28 14:15:58 -07002304 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002305 my $line;
2306 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002307 $linenr++;
2308 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002309
Joe Perches3705ce52013-07-03 15:05:31 -07002310 push(@fixed, $rawline) if ($fix);
2311
Andy Whitcroft773647a2008-03-28 14:15:58 -07002312 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002313 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002314 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002315 $setup_docs = 1;
2316 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002317 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002318 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002319 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002320 $realline=$1-1;
2321 if (defined $2) {
2322 $realcnt=$3+1;
2323 } else {
2324 $realcnt=1+1;
2325 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002326 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002327
2328 # Guestimate if this is a continuing comment. Run
2329 # the context looking for a comment "edge". If this
2330 # edge is a close comment then we must be in a comment
2331 # at context start.
2332 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002333 my $cnt = $realcnt;
2334 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2335 next if (defined $rawlines[$ln - 1] &&
2336 $rawlines[$ln - 1] =~ /^-/);
2337 $cnt--;
2338 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002339 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002340 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2341 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2342 ($edge) = $1;
2343 last;
2344 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002345 }
2346 if (defined $edge && $edge eq '*/') {
2347 $in_comment = 1;
2348 }
2349
2350 # Guestimate if this is a continuing comment. If this
2351 # is the start of a diff block and this line starts
2352 # ' *' then it is very likely a comment.
2353 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002354 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002355 {
2356 $in_comment = 1;
2357 }
2358
2359 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2360 sanitise_line_reset($in_comment);
2361
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002362 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002363 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002364 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002365 $line = sanitise_line($rawline);
2366 }
2367 push(@lines, $line);
2368
2369 if ($realcnt > 1) {
2370 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2371 } else {
2372 $realcnt = 0;
2373 }
2374
2375 #print "==>$rawline\n";
2376 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002377
2378 if ($setup_docs && $line =~ /^\+/) {
2379 push(@setup_docs, $line);
2380 }
2381 }
2382
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002383 $prefix = '';
2384
Andy Whitcroft773647a2008-03-28 14:15:58 -07002385 $realcnt = 0;
2386 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002387 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002388 foreach my $line (@lines) {
2389 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002390 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002391 my $sline = $line; #copy of $line
2392 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002393
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002394 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002395
Joe Perches12c253a2018-06-07 17:10:58 -07002396# check if it's a mode change, rename or start of a patch
2397 if (!$in_commit_log &&
2398 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2399 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2400 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2401 $is_patch = 1;
2402 }
2403
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002404#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002405 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002406 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2407 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002408 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002409 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002410 $realline=$1-1;
2411 if (defined $2) {
2412 $realcnt=$3+1;
2413 } else {
2414 $realcnt=1+1;
2415 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002416 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002417 $prev_values = 'E';
2418
Andy Whitcroft773647a2008-03-28 14:15:58 -07002419 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002420 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002421 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002422 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002423 if ($context =~ /\b(\w+)\s*\(/) {
2424 $context_function = $1;
2425 } else {
2426 undef $context_function;
2427 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002428 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002429
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002430# track the line number as we move through the hunk, note that
2431# new versions of GNU diff omit the leading space on completely
2432# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002433 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002434 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002435 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002436
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002437 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002438 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002439
2440 # Track the previous line.
2441 ($prevline, $stashline) = ($stashline, $line);
2442 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002443 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2444
Andy Whitcroft773647a2008-03-28 14:15:58 -07002445 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002446
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002447 } elsif ($realcnt == 1) {
2448 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002449 }
2450
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002451 my $hunk_line = ($realcnt != 0);
2452
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002453 $here = "#$linenr: " if (!$file);
2454 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002455
Joe Perches2ac73b42014-06-04 16:12:05 -07002456 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002457 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002458 if ($line =~ /^diff --git.*?(\S+)$/) {
2459 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002460 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002461 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002462 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002463 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002464 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002465 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002466 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002467
2468 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002469 if (!$file && $tree && $p1_prefix ne '' &&
2470 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002471 WARN("PATCH_PREFIX",
2472 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002473 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002474
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002475 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002476 ERROR("MODIFIED_INCLUDE_ASM",
2477 "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 -07002478 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002479 $found_file = 1;
2480 }
2481
Joe Perches34d88152015-06-25 15:03:05 -07002482#make up the handle for any error we report on this line
2483 if ($showfile) {
2484 $prefix = "$realfile:$realline: "
2485 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002486 if ($file) {
2487 $prefix = "$filename:$realline: ";
2488 } else {
2489 $prefix = "$filename:$linenr: ";
2490 }
Joe Perches34d88152015-06-25 15:03:05 -07002491 }
2492
Joe Perches2ac73b42014-06-04 16:12:05 -07002493 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002494 if (is_maintained_obsolete($realfile)) {
2495 WARN("OBSOLETE",
2496 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2497 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002498 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002499 $check = 1;
2500 } else {
2501 $check = $check_orig;
2502 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002503 $checklicenseline = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002504 next;
2505 }
2506
Randy Dunlap389834b2007-06-08 13:47:03 -07002507 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002508
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002509 my $hereline = "$here\n$rawline\n";
2510 my $herecurr = "$here\n$rawline\n";
2511 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002512
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002513 $cnt_lines++ if ($realcnt != 0);
2514
Joe Perches490b2922018-08-21 21:58:01 -07002515# Verify the existence of a commit log if appropriate
2516# 2 is used because a $signature is counted in $commit_log_lines
2517 if ($in_commit_log) {
2518 if ($line !~ /^\s*$/) {
2519 $commit_log_lines++; #could be a $signature
2520 }
2521 } elsif ($has_commit_log && $commit_log_lines < 2) {
2522 WARN("COMMIT_MESSAGE",
2523 "Missing commit description - Add an appropriate one\n");
2524 $commit_log_lines = 2; #warn only once
2525 }
2526
Joe Perchese518e9a2015-06-25 15:03:27 -07002527# Check if the commit log has what seems like a diff which can confuse patch
2528 if ($in_commit_log && !$commit_log_has_diff &&
2529 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2530 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2531 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2532 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2533 ERROR("DIFF_IN_COMMIT_MSG",
2534 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2535 $commit_log_has_diff = 1;
2536 }
2537
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002538# Check for incorrect file permissions
2539 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2540 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002541 if ($realfile !~ m@scripts/@ &&
2542 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002543 ERROR("EXECUTE_PERMISSIONS",
2544 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002545 }
2546 }
2547
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002548# Check the patch for a From:
2549 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2550 $author = $1;
2551 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2552 $author =~ s/"//g;
2553 }
2554
Joe Perches20112472011-07-25 17:13:23 -07002555# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002556 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002557 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002558 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002559 if ($author ne '') {
2560 my $l = $line;
2561 $l =~ s/"//g;
2562 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2563 $authorsignoff = 1;
2564 }
2565 }
Joe Perches20112472011-07-25 17:13:23 -07002566 }
2567
Joe Perchese0d975b2014-12-10 15:51:49 -08002568# Check if MAINTAINERS is being updated. If so, there's probably no need to
2569# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2570 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2571 $reported_maintainer_file = 1;
2572 }
2573
Joe Perches20112472011-07-25 17:13:23 -07002574# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002575 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002576 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002577 my $space_before = $1;
2578 my $sign_off = $2;
2579 my $space_after = $3;
2580 my $email = $4;
2581 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2582
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002583 if ($sign_off !~ /$signature_tags/) {
2584 WARN("BAD_SIGN_OFF",
2585 "Non-standard signature: $sign_off\n" . $herecurr);
2586 }
Joe Perches20112472011-07-25 17:13:23 -07002587 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002588 if (WARN("BAD_SIGN_OFF",
2589 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2590 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002591 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002592 "$ucfirst_sign_off $email";
2593 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002594 }
Joe Perches20112472011-07-25 17:13:23 -07002595 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002596 if (WARN("BAD_SIGN_OFF",
2597 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2598 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002599 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002600 "$ucfirst_sign_off $email";
2601 }
2602
Joe Perches20112472011-07-25 17:13:23 -07002603 }
2604 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002605 if (WARN("BAD_SIGN_OFF",
2606 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2607 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002608 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002609 "$ucfirst_sign_off $email";
2610 }
Joe Perches20112472011-07-25 17:13:23 -07002611 }
2612
2613 my ($email_name, $email_address, $comment) = parse_email($email);
2614 my $suggested_email = format_email(($email_name, $email_address));
2615 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002616 ERROR("BAD_SIGN_OFF",
2617 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002618 } else {
2619 my $dequoted = $suggested_email;
2620 $dequoted =~ s/^"//;
2621 $dequoted =~ s/" </ </;
2622 # Don't force email to have quotes
2623 # Allow just an angle bracketed address
2624 if ("$dequoted$comment" ne $email &&
2625 "<$email_address>$comment" ne $email &&
2626 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002627 WARN("BAD_SIGN_OFF",
2628 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002629 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002630 }
Joe Perches7e51f192013-09-11 14:23:57 -07002631
2632# Check for duplicate signatures
2633 my $sig_nospace = $line;
2634 $sig_nospace =~ s/\s//g;
2635 $sig_nospace = lc($sig_nospace);
2636 if (defined $signatures{$sig_nospace}) {
2637 WARN("BAD_SIGN_OFF",
2638 "Duplicate signature\n" . $herecurr);
2639 } else {
2640 $signatures{$sig_nospace} = 1;
2641 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002642 }
2643
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002644# Check email subject for common tools that don't need to be mentioned
2645 if ($in_header_lines &&
2646 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2647 WARN("EMAIL_SUBJECT",
2648 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2649 }
2650
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002651# Check for unwanted Gerrit info
2652 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2653 ERROR("GERRIT_CHANGE_ID",
2654 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2655 }
2656
Joe Perches369c8dd2015-11-06 16:31:34 -08002657# Check if the commit log is in a possible stack dump
2658 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2659 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2660 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2661 # timestamp
2662 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2663 # stack dump address
2664 $commit_log_possible_stack_dump = 1;
2665 }
2666
Joe Perches2a076f42015-04-16 12:44:28 -07002667# Check for line lengths > 75 in commit log, warn once
2668 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002669 length($line) > 75 &&
2670 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2671 # file delta changes
2672 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2673 # filename then :
2674 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2675 # A Fixes: or Link: line
2676 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002677 WARN("COMMIT_LOG_LONG_LINE",
2678 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2679 $commit_log_long_line = 1;
2680 }
2681
Joe Perchesbf4daf12015-09-09 15:37:50 -07002682# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002683 if ($in_commit_log && $commit_log_possible_stack_dump &&
2684 $line =~ /^\s*$/) {
2685 $commit_log_possible_stack_dump = 0;
2686 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002687
Joe Perches0d7835f2015-02-13 14:38:35 -08002688# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002689 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002690 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002691 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002692 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002693 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002694 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2695 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002696 my $init_char = "c";
2697 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002698 my $short = 1;
2699 my $long = 0;
2700 my $case = 1;
2701 my $space = 1;
2702 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002703 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002704 my $id = '0123456789ab';
2705 my $orig_desc = "commit description";
2706 my $description = "";
2707
Joe Perchesfe043ea2015-09-09 15:37:25 -07002708 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2709 $init_char = $1;
2710 $orig_commit = lc($2);
2711 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2712 $orig_commit = lc($1);
2713 }
2714
Joe Perches0d7835f2015-02-13 14:38:35 -08002715 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2716 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2717 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2718 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2719 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2720 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002721 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002722 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2723 defined $rawlines[$linenr] &&
2724 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2725 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002726 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002727 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2728 defined $rawlines[$linenr] &&
2729 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2730 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2731 $orig_desc = $1;
2732 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2733 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002734 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002735 }
2736
2737 ($id, $description) = git_commit_info($orig_commit,
2738 $id, $orig_desc);
2739
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002740 if (defined($id) &&
2741 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002742 ERROR("GIT_COMMIT_ID",
2743 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2744 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002745 }
2746
Joe Perches13f19372014-08-06 16:10:59 -07002747# Check for added, moved or deleted files
2748 if (!$reported_maintainer_file && !$in_commit_log &&
2749 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2750 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2751 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2752 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002753 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002754 $reported_maintainer_file = 1;
2755 WARN("FILE_PATH_CHANGES",
2756 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2757 }
2758
Andy Whitcroft00df3442007-06-08 13:47:06 -07002759# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002760 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002761 ERROR("CORRUPTED_PATCH",
2762 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002763 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002764 }
2765
2766# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2767 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002768 $rawline !~ m/^$UTF8*$/) {
2769 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2770
2771 my $blank = copy_spacing($rawline);
2772 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2773 my $hereptr = "$hereline$ptr\n";
2774
Joe Perches34d99212011-07-25 17:13:26 -07002775 CHK("INVALID_UTF8",
2776 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002777 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002778
Joe Perches15662b32011-10-31 17:13:12 -07002779# Check if it's the start of a commit log
2780# (not a header line and we haven't seen the patch filename)
2781 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002782 !($rawline =~ /^\s+(?:\S|$)/ ||
2783 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002784 $in_header_lines = 0;
2785 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002786 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002787 }
2788
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002789# Check if there is UTF-8 in a commit log when a mail header has explicitly
2790# declined it, i.e defined some charset where it is missing.
2791 if ($in_header_lines &&
2792 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2793 $1 !~ /utf-8/i) {
2794 $non_utf8_charset = 1;
2795 }
2796
2797 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002798 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002799 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002800 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2801 }
2802
Joe Perchesd6430f72016-12-12 16:46:28 -08002803# Check for absolute kernel paths in commit message
2804 if ($tree && $in_commit_log) {
2805 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2806 my $file = $1;
2807
2808 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2809 check_absolute_file($1, $herecurr)) {
2810 #
2811 } else {
2812 check_absolute_file($file, $herecurr);
2813 }
2814 }
2815 }
2816
Kees Cook66b47b42014-10-13 15:51:57 -07002817# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002818 if (defined($misspellings) &&
2819 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002820 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002821 my $typo = $1;
2822 my $typo_fix = $spelling_fix{lc($typo)};
2823 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2824 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002825 my $msg_level = \&WARN;
2826 $msg_level = \&CHK if ($file);
2827 if (&{$msg_level}("TYPO_SPELLING",
2828 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002829 $fix) {
2830 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2831 }
2832 }
2833 }
2834
Andy Whitcroft306708542008-10-15 22:02:28 -07002835# ignore non-hunk lines and lines being removed
2836 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002837
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002838#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002839 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002840 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002841 if (ERROR("DOS_LINE_ENDINGS",
2842 "DOS line endings\n" . $herevet) &&
2843 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002844 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002845 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002846 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2847 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002848 if (ERROR("TRAILING_WHITESPACE",
2849 "trailing whitespace\n" . $herevet) &&
2850 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002851 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002852 }
2853
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002854 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002855 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002856
Josh Triplett4783f892013-11-12 15:10:12 -08002857# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002858 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002859 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002860 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2861 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002862 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002863 my $msg_level = \&ERROR;
2864 $msg_level = \&CHK if ($file);
2865 &{$msg_level}("FSF_MAILING_ADDRESS",
2866 "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 -08002867 }
2868
Andi Kleen33549572010-05-24 14:33:29 -07002869# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002870# Only applies when adding the entry originally, after that we do not have
2871# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002872 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002873 # 'choice' is usually the last thing on the line (though
2874 # Kconfig supports named choices), so use a word boundary
2875 # (\b) rather than a whitespace character (\s)
2876 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002877 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002878 my $cnt = $realcnt;
2879 my $ln = $linenr + 1;
2880 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002881 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002882 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002883 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002884 $f = $lines[$ln - 1];
2885 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2886 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002887
2888 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002889 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002890
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01002891 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002892 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01002893 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2894 if ($lines[$ln - 1] =~ "---help---") {
2895 WARN("CONFIG_DESCRIPTION",
2896 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2897 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002898 $length = -1;
2899 }
2900
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002901 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002902 $f =~ s/#.*//;
2903 $f =~ s/^\s+//;
2904 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01002905
2906 # This only checks context lines in the patch
2907 # and so hopefully shouldn't trigger false
2908 # positives, even though some of these are
2909 # common words in help texts
2910 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2911 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002912 $is_end = 1;
2913 last;
2914 }
Andi Kleen33549572010-05-24 14:33:29 -07002915 $length++;
2916 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002917 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2918 WARN("CONFIG_DESCRIPTION",
2919 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2920 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002921 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002922 }
2923
Joe Perches628f91a2017-07-10 15:52:07 -07002924# check for MAINTAINERS entries that don't have the right form
2925 if ($realfile =~ /^MAINTAINERS$/ &&
2926 $rawline =~ /^\+[A-Z]:/ &&
2927 $rawline !~ /^\+[A-Z]:\t\S/) {
2928 if (WARN("MAINTAINERS_STYLE",
2929 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2930 $fix) {
2931 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2932 }
2933 }
2934
Christoph Jaeger327953e2015-02-13 14:38:29 -08002935# discourage the use of boolean for type definition attributes of Kconfig options
2936 if ($realfile =~ /Kconfig/ &&
2937 $line =~ /^\+\s*\bboolean\b/) {
2938 WARN("CONFIG_TYPE_BOOLEAN",
2939 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2940 }
2941
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002942 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2943 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2944 my $flag = $1;
2945 my $replacement = {
2946 'EXTRA_AFLAGS' => 'asflags-y',
2947 'EXTRA_CFLAGS' => 'ccflags-y',
2948 'EXTRA_CPPFLAGS' => 'cppflags-y',
2949 'EXTRA_LDFLAGS' => 'ldflags-y',
2950 };
2951
2952 WARN("DEPRECATED_VARIABLE",
2953 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2954 }
2955
Rob Herringbff5da42014-01-23 15:54:51 -08002956# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002957 if (defined $root &&
2958 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2959 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2960
Rob Herringbff5da42014-01-23 15:54:51 -08002961 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2962
Florian Vaussardcc933192014-04-03 14:49:27 -07002963 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2964 my $vp_file = $dt_path . "vendor-prefixes.txt";
2965
Rob Herringbff5da42014-01-23 15:54:51 -08002966 foreach my $compat (@compats) {
2967 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002968 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2969 my $compat3 = $compat;
2970 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2971 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002972 if ( $? >> 8 ) {
2973 WARN("UNDOCUMENTED_DT_STRING",
2974 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2975 }
2976
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002977 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2978 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002979 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002980 if ( $? >> 8 ) {
2981 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002982 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002983 }
2984 }
2985 }
2986
Rob Herring9f3a8992018-04-10 16:33:13 -07002987# check for using SPDX license tag at beginning of files
2988 if ($realline == $checklicenseline) {
2989 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
2990 $checklicenseline = 2;
2991 } elsif ($rawline =~ /^\+/) {
2992 my $comment = "";
2993 if ($realfile =~ /\.(h|s|S)$/) {
2994 $comment = '/*';
2995 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
2996 $comment = '//';
2997 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
2998 $comment = '#';
2999 } elsif ($realfile =~ /\.rst$/) {
3000 $comment = '..';
3001 }
3002
3003 if ($comment !~ /^$/ &&
3004 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003005 WARN("SPDX_LICENSE_TAG",
3006 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3007 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3008 my $spdx_license = $1;
3009 if (!is_SPDX_License_valid($spdx_license)) {
3010 WARN("SPDX_LICENSE_TAG",
3011 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3012 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003013 }
3014 }
3015 }
3016
Andy Whitcroft5368df202008-10-15 22:02:27 -07003017# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003018 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003019
Joe Perches47e0c882015-06-25 15:02:57 -07003020# line length limit (with some exclusions)
3021#
3022# There are a few types of lines that may extend beyond $max_line_length:
3023# logging functions like pr_info that end in a string
3024# lines with a single string
3025# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003026# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003027#
3028# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003029# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003030# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3031# LONG_LINE all other lines longer than $max_line_length
3032#
3033# if LONG_LINE is ignored, the other 2 types are also ignored
3034#
3035
Joe Perchesb4749e92015-07-17 16:24:01 -07003036 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003037 my $msg_type = "LONG_LINE";
3038
3039 # Check the allowed long line types first
3040
3041 # logging functions that end in a string that starts
3042 # before $max_line_length
3043 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3044 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3045 $msg_type = "";
3046
3047 # lines with only strings (w/ possible termination)
3048 # #defines with only strings
3049 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3050 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3051 $msg_type = "";
3052
Joe Perchescc147502017-11-17 15:28:44 -08003053 # More special cases
3054 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3055 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003056 $msg_type = "";
3057
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003058 # URL ($rawline is used in case the URL is in a comment)
3059 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3060 $msg_type = "";
3061
Joe Perches47e0c882015-06-25 15:02:57 -07003062 # Otherwise set the alternate message types
3063
3064 # a comment starts before $max_line_length
3065 } elsif ($line =~ /($;[\s$;]*)$/ &&
3066 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3067 $msg_type = "LONG_LINE_COMMENT"
3068
3069 # a quoted string starts before $max_line_length
3070 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3071 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3072 $msg_type = "LONG_LINE_STRING"
3073 }
3074
3075 if ($msg_type ne "" &&
3076 (show_type("LONG_LINE") || show_type($msg_type))) {
3077 WARN($msg_type,
3078 "line over $max_line_length characters\n" . $herecurr);
3079 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003080 }
3081
Andy Whitcroft8905a672007-11-28 16:21:06 -08003082# check for adding lines without a newline.
3083 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003084 WARN("MISSING_EOF_NEWLINE",
3085 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003086 }
3087
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003088# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003089 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003090
3091# at the beginning of a line any tabs must come first and anything
3092# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003093 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3094 $rawline =~ /^\+\s* \s*/) {
3095 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003096 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003097 if (ERROR("CODE_INDENT",
3098 "code indent should use tabs where possible\n" . $herevet) &&
3099 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003100 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003101 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003102 }
3103
Alberto Panizzo08e44362010-03-05 13:43:54 -08003104# check for space before tabs.
3105 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3106 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003107 if (WARN("SPACE_BEFORE_TAB",
3108 "please, no space before tabs\n" . $herevet) &&
3109 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003110 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003111 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003112 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003113 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003114 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003115 }
3116
Joe Perches6a487212018-04-10 16:34:04 -07003117# check for assignments on the start of a line
3118 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3119 CHK("ASSIGNMENT_CONTINUATIONS",
3120 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3121 }
3122
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003123# check for && or || at the start of a line
3124 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3125 CHK("LOGICAL_CONTINUATIONS",
3126 "Logical continuations should be on the previous line\n" . $hereprev);
3127 }
3128
Joe Perchesa91e8992016-05-20 17:04:05 -07003129# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003130 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003131 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003132 my $indent = length($1);
3133 if ($indent % 8) {
3134 if (WARN("TABSTOP",
3135 "Statements should start on a tabstop\n" . $herecurr) &&
3136 $fix) {
3137 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3138 }
3139 }
3140 }
3141
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003142# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003143 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003144 $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 -07003145 $prevline =~ /^\+(\t*)(.*)$/;
3146 my $oldindent = $1;
3147 my $rest = $2;
3148
3149 my $pos = pos_last_openparen($rest);
3150 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003151 $line =~ /^(\+| )([ \t]*)/;
3152 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003153
3154 my $goodtabindent = $oldindent .
3155 "\t" x ($pos / 8) .
3156 " " x ($pos % 8);
3157 my $goodspaceindent = $oldindent . " " x $pos;
3158
3159 if ($newindent ne $goodtabindent &&
3160 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003161
3162 if (CHK("PARENTHESIS_ALIGNMENT",
3163 "Alignment should match open parenthesis\n" . $hereprev) &&
3164 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003165 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003166 s/^\+[ \t]*/\+$goodtabindent/;
3167 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003168 }
3169 }
3170 }
3171
Joe Perches6ab3a972015-04-16 12:44:05 -07003172# check for space after cast like "(int) foo" or "(struct foo) bar"
3173# avoid checking a few false positives:
3174# "sizeof(<type>)" or "__alignof__(<type>)"
3175# function pointer declarations like "(*foo)(int) = bar;"
3176# structure definitions like "(struct foo) { 0 };"
3177# multiline macros that define functions
3178# known attributes or the __attribute__ keyword
3179 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3180 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003181 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003182 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003183 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003184 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003185 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003186 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003187 }
3188
Joe Perches86406b12015-09-09 15:37:41 -07003189# Block comment styles
3190# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003191 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003192 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003193 $rawline =~ /^\+[ \t]*\*/ &&
3194 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003195 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3196 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3197 }
3198
Joe Perches86406b12015-09-09 15:37:41 -07003199# Block comments use * on subsequent lines
3200 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3201 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003202 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003203 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003204 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003205 WARN("BLOCK_COMMENT_STYLE",
3206 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003207 }
3208
Joe Perches86406b12015-09-09 15:37:41 -07003209# Block comments use */ on trailing lines
3210 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003211 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3212 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3213 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003214 WARN("BLOCK_COMMENT_STYLE",
3215 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003216 }
3217
Joe Perches08eb9b82016-10-11 13:51:50 -07003218# Block comment * alignment
3219 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003220 $line =~ /^\+[ \t]*$;/ && #leading comment
3221 $rawline =~ /^\+[ \t]*\*/ && #leading *
3222 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003223 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003224 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3225 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003226 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003227 if (defined($1)) {
3228 $oldindent = expand_tabs($1);
3229 } else {
3230 $prevrawline =~ m@^\+(.*/?)\*@;
3231 $oldindent = expand_tabs($1);
3232 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003233 $rawline =~ m@^\+([ \t]*)\*@;
3234 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003235 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003236 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003237 WARN("BLOCK_COMMENT_STYLE",
3238 "Block comments should align the * on each line\n" . $hereprev);
3239 }
3240 }
3241
Joe Perches7f619192014-08-06 16:10:39 -07003242# check for missing blank lines after struct/union declarations
3243# with exceptions for various attributes and macros
3244 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3245 $line =~ /^\+/ &&
3246 !($line =~ /^\+\s*$/ ||
3247 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3248 $line =~ /^\+\s*MODULE_/i ||
3249 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3250 $line =~ /^\+[a-z_]*init/ ||
3251 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3252 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003253 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003254 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003255 if (CHK("LINE_SPACING",
3256 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3257 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003258 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003259 }
Joe Perches7f619192014-08-06 16:10:39 -07003260 }
3261
Joe Perches365dd4e2014-08-06 16:10:42 -07003262# check for multiple consecutive blank lines
3263 if ($prevline =~ /^[\+ ]\s*$/ &&
3264 $line =~ /^\+\s*$/ &&
3265 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003266 if (CHK("LINE_SPACING",
3267 "Please don't use multiple blank lines\n" . $hereprev) &&
3268 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003269 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003270 }
3271
Joe Perches365dd4e2014-08-06 16:10:42 -07003272 $last_blank_line = $linenr;
3273 }
3274
Joe Perches3b617e32014-04-03 14:49:28 -07003275# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003276 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3277 # actual declarations
3278 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003279 # function pointer declarations
3280 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003281 # foo bar; where foo is some local typedef or #define
3282 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3283 # known declaration macros
3284 $prevline =~ /^\+\s+$declaration_macros/) &&
3285 # for "else if" which can look like "$Ident $Ident"
3286 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3287 # other possible extensions of declaration lines
3288 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3289 # not starting a section or a macro "\" extended line
3290 $prevline =~ /(?:\{\s*|\\)$/) &&
3291 # looks like a declaration
3292 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003293 # function pointer declarations
3294 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003295 # foo bar; where foo is some local typedef or #define
3296 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3297 # known declaration macros
3298 $sline =~ /^\+\s+$declaration_macros/ ||
3299 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003300 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003301 # start or end of block or continuation of declaration
3302 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3303 # bitfield continuation
3304 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3305 # other possible extensions of declaration lines
3306 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3307 # indentation of previous and current line are the same
3308 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003309 if (WARN("LINE_SPACING",
3310 "Missing a blank line after declarations\n" . $hereprev) &&
3311 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003312 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003313 }
Joe Perches3b617e32014-04-03 14:49:28 -07003314 }
3315
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003316# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003317# Exceptions:
3318# 1) within comments
3319# 2) indented preprocessor commands
3320# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003321 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003322 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003323 if (WARN("LEADING_SPACE",
3324 "please, no spaces at the start of a line\n" . $herevet) &&
3325 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003326 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003327 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003328 }
3329
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003330# check we are in a valid C source file if not then ignore this hunk
3331 next if ($realfile !~ /\.(h|c)$/);
3332
Joe Perches5751a242017-11-17 15:28:52 -08003333# check for unusual line ending [ or (
3334 if ($line =~ /^\+.*([\[\(])\s*$/) {
3335 CHK("OPEN_ENDED_LINE",
3336 "Lines should not end with a '$1'\n" . $herecurr);
3337 }
3338
Joe Perches4dbed762017-05-08 15:55:39 -07003339# check if this appears to be the start function declaration, save the name
3340 if ($sline =~ /^\+\{\s*$/ &&
3341 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3342 $context_function = $1;
3343 }
3344
3345# check if this appears to be the end of function declaration
3346 if ($sline =~ /^\+\}\s*$/) {
3347 undef $context_function;
3348 }
3349
Joe Perches032a4c02014-08-06 16:10:29 -07003350# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003351# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003352# if the previous line is a break or return and is indented 1 tab more...
3353 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3354 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003355 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3356 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3357 defined $lines[$linenr] &&
3358 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003359 WARN("UNNECESSARY_ELSE",
3360 "else is not generally useful after a break or return\n" . $hereprev);
3361 }
3362 }
3363
Joe Perchesc00df192014-08-06 16:11:01 -07003364# check indentation of a line with a break;
3365# if the previous line is a goto or return and is indented the same # of tabs
3366 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3367 my $tabs = $1;
3368 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3369 WARN("UNNECESSARY_BREAK",
3370 "break is not useful after a goto or return\n" . $hereprev);
3371 }
3372 }
3373
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003374# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003375 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003376 WARN("CVS_KEYWORD",
3377 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003378 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003379
Joe Perches56e77d72013-02-21 16:44:14 -08003380# check for old HOTPLUG __dev<foo> section markings
3381 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3382 WARN("HOTPLUG_SECTION",
3383 "Using $1 is unnecessary\n" . $herecurr);
3384 }
3385
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003386# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003387 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3388 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003389#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003390 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003391 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003392 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003393 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003394 $stat =~ s/\n./\n /g;
3395 $cond =~ s/\n./\n /g;
3396
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003397#print "linenr<$linenr> <$stat>\n";
3398 # If this statement has no statement boundaries within
3399 # it there is no point in retrying a statement scan
3400 # until we hit end of it.
3401 my $frag = $stat; $frag =~ s/;+\s*$//;
3402 if ($frag !~ /(?:{|;)/) {
3403#print "skip<$line_nr_next>\n";
3404 $suppress_statement = $line_nr_next;
3405 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003406
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003407 # Find the real next line.
3408 $realline_next = $line_nr_next;
3409 if (defined $realline_next &&
3410 (!defined $lines[$realline_next - 1] ||
3411 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3412 $realline_next++;
3413 }
3414
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003415 my $s = $stat;
3416 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003417
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003418 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003419 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003420
3421 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003422 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003423
Andy Whitcroft463f2862009-09-21 17:04:34 -07003424 } elsif ($s =~ /^.\s*else\b/s) {
3425
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003426 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003427 } 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 -07003428 my $type = $1;
3429 $type =~ s/\s+/ /g;
3430 possible($type, "A:" . $s);
3431
Andy Whitcroft8905a672007-11-28 16:21:06 -08003432 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003433 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003434 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003435 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003436
3437 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003438 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003439 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003440 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003441
3442 # Check for any sort of function declaration.
3443 # int foo(something bar, other baz);
3444 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003445 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 -08003446 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003447
Andy Whitcroftcf655042008-03-04 14:28:20 -08003448 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003449 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003450 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003451
Andy Whitcroft8905a672007-11-28 16:21:06 -08003452 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003453 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003454
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003455 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003456 }
3457 }
3458 }
3459
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003460 }
3461
Andy Whitcroft653d4872007-06-23 17:16:34 -07003462#
3463# Checks which may be anchored in the context.
3464#
3465
3466# Check for switch () and associated case and default
3467# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003468 if ($line=~/\bswitch\s*\(.*\)/) {
3469 my $err = '';
3470 my $sep = '';
3471 my @ctx = ctx_block_outer($linenr, $realcnt);
3472 shift(@ctx);
3473 for my $ctx (@ctx) {
3474 my ($clen, $cindent) = line_stats($ctx);
3475 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3476 $indent != $cindent) {
3477 $err .= "$sep$ctx\n";
3478 $sep = '';
3479 } else {
3480 $sep = "[...]\n";
3481 }
3482 }
3483 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003484 ERROR("SWITCH_CASE_INDENT_LEVEL",
3485 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003486 }
3487 }
3488
3489# if/while/etc brace do not go on next line, unless defining a do while loop,
3490# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003491 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 -07003492 my $pre_ctx = "$1$2";
3493
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003494 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003495
3496 if ($line =~ /^\+\t{6,}/) {
3497 WARN("DEEP_INDENTATION",
3498 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3499 }
3500
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003501 my $ctx_cnt = $realcnt - $#ctx - 1;
3502 my $ctx = join("\n", @ctx);
3503
Andy Whitcroft548596d2008-07-23 21:29:01 -07003504 my $ctx_ln = $linenr;
3505 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003506
Andy Whitcroft548596d2008-07-23 21:29:01 -07003507 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3508 defined $lines[$ctx_ln - 1] &&
3509 $lines[$ctx_ln - 1] =~ /^-/)) {
3510 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3511 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003512 $ctx_ln++;
3513 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003514
Andy Whitcroft53210162008-07-23 21:29:03 -07003515 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3516 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003517
Joe Perchesd752fcc2014-08-06 16:11:05 -07003518 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003519 ERROR("OPEN_BRACE",
3520 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003521 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003522 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003523 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3524 $ctx =~ /\)\s*\;\s*$/ &&
3525 defined $lines[$ctx_ln - 1])
3526 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003527 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3528 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003529 WARN("TRAILING_SEMICOLON",
3530 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003531 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003532 }
3533 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003534 }
3535
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003536# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003537 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 -08003538 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3539 ctx_statement_block($linenr, $realcnt, 0)
3540 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003541 my ($s, $c) = ($stat, $cond);
3542
3543 substr($s, 0, length($c), '');
3544
Joe Perches9f5af482015-09-09 15:37:30 -07003545 # remove inline comments
3546 $s =~ s/$;/ /g;
3547 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003548
3549 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003550 my @newlines = ($c =~ /\n/gs);
3551 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003552
Joe Perches9f5af482015-09-09 15:37:30 -07003553 # Make sure we remove the line prefixes as we have
3554 # none on the first line, and are going to readd them
3555 # where necessary.
3556 $s =~ s/\n./\n/gs;
3557 while ($s =~ /\n\s+\\\n/) {
3558 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3559 }
3560
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003561 # We want to check the first line inside the block
3562 # starting at the end of the conditional, so remove:
3563 # 1) any blank line termination
3564 # 2) any opening brace { on end of the line
3565 # 3) any do (...) {
3566 my $continuation = 0;
3567 my $check = 0;
3568 $s =~ s/^.*\bdo\b//;
3569 $s =~ s/^\s*{//;
3570 if ($s =~ s/^\s*\\//) {
3571 $continuation = 1;
3572 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003573 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003574 $check = 1;
3575 $cond_lines++;
3576 }
3577
3578 # Also ignore a loop construct at the end of a
3579 # preprocessor statement.
3580 if (($prevline =~ /^.\s*#\s*define\s/ ||
3581 $prevline =~ /\\\s*$/) && $continuation == 0) {
3582 $check = 0;
3583 }
3584
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003585 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003586 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003587 while ($cond_ptr != $cond_lines) {
3588 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003589
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003590 # If we see an #else/#elif then the code
3591 # is not linear.
3592 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3593 $check = 0;
3594 }
3595
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003596 # Ignore:
3597 # 1) blank lines, they should be at 0,
3598 # 2) preprocessor lines, and
3599 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003600 if ($continuation ||
3601 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003602 $s =~ /^\s*#\s*?/ ||
3603 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003604 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003605 if ($s =~ s/^.*?\n//) {
3606 $cond_lines++;
3607 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003608 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003609 }
3610
3611 my (undef, $sindent) = line_stats("+" . $s);
3612 my $stat_real = raw_line($linenr, $cond_lines);
3613
3614 # Check if either of these lines are modified, else
3615 # this is not this patch's fault.
3616 if (!defined($stat_real) ||
3617 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3618 $check = 0;
3619 }
3620 if (defined($stat_real) && $cond_lines > 1) {
3621 $stat_real = "[...]\n$stat_real";
3622 }
3623
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003624 #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 -07003625
Joe Perches9f5af482015-09-09 15:37:30 -07003626 if ($check && $s ne '' &&
3627 (($sindent % 8) != 0 ||
3628 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003629 ($sindent == $indent &&
3630 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003631 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003632 WARN("SUSPECT_CODE_INDENT",
3633 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003634 }
3635 }
3636
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003637 # Track the 'values' across context and added lines.
3638 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003639 my ($curr_values, $curr_vars) =
3640 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003641 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003642 if ($dbg_values) {
3643 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003644 print "$linenr > .$outline\n";
3645 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003646 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003647 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003648 $prev_values = substr($curr_values, -1);
3649
Andy Whitcroft00df3442007-06-08 13:47:06 -07003650#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003651 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003652
Joe Perches11ca40a2016-12-12 16:46:31 -08003653# check for dereferences that span multiple lines
3654 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3655 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3656 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3657 my $ref = $1;
3658 $line =~ /^.\s*($Lval)/;
3659 $ref .= $1;
3660 $ref =~ s/\s//g;
3661 WARN("MULTILINE_DEREFERENCE",
3662 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3663 }
3664
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003665# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003666 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 -07003667 my $type = $1;
3668 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003669 $var = "" if (!defined $var);
3670 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003671 my $sign = $1;
3672 my $pointer = $2;
3673
3674 $pointer = "" if (!defined $pointer);
3675
3676 if (WARN("UNSPECIFIED_INT",
3677 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3678 $fix) {
3679 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003680 my $comp_pointer = $pointer;
3681 $comp_pointer =~ s/\s//g;
3682 $decl .= $comp_pointer;
3683 $decl = rtrim($decl) if ($var eq "");
3684 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003685 }
3686 }
3687 }
3688
Andy Whitcroft653d4872007-06-23 17:16:34 -07003689# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003690 if ($dbg_type) {
3691 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003692 ERROR("TEST_TYPE",
3693 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003694 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003695 ERROR("TEST_NOT_TYPE",
3696 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003697 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003698 next;
3699 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003700# TEST: allow direct testing of the attribute matcher.
3701 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003702 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003703 ERROR("TEST_ATTR",
3704 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003705 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003706 ERROR("TEST_NOT_ATTR",
3707 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003708 }
3709 next;
3710 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003711
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003712# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003713 if ($line =~ /^.\s*{/ &&
3714 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003715 if (ERROR("OPEN_BRACE",
3716 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003717 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3718 fix_delete_line($fixlinenr - 1, $prevrawline);
3719 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003720 my $fixedline = $prevrawline;
3721 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003722 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003723 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003724 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003725 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003726 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003727 }
3728
Andy Whitcroft653d4872007-06-23 17:16:34 -07003729#
3730# Checks which are anchored on the added line.
3731#
3732
3733# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003734 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003735 my $path = $1;
3736 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003737 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003738 "malformed #include filename\n" . $herecurr);
3739 }
3740 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3741 ERROR("UAPI_INCLUDE",
3742 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003743 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003744 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003745
3746# no C99 // comments
3747 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003748 if (ERROR("C99_COMMENTS",
3749 "do not use C99 // comments\n" . $herecurr) &&
3750 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003751 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003752 if ($line =~ /\/\/(.*)$/) {
3753 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003754 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003755 }
3756 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003757 }
3758 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003759 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003760 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003761
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003762# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3763# the whole statement.
3764#print "APW <$lines[$realline_next - 1]>\n";
3765 if (defined $realline_next &&
3766 exists $lines[$realline_next - 1] &&
3767 !defined $suppress_export{$realline_next} &&
3768 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3769 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003770 # Handle definitions which produce identifiers with
3771 # a prefix:
3772 # XXX(foo);
3773 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003774 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003775 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003776 $name =~ /^${Ident}_$2/) {
3777#print "FOO C name<$name>\n";
3778 $suppress_export{$realline_next} = 1;
3779
3780 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003781 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003782 ^.DEFINE_$Ident\(\Q$name\E\)|
3783 ^.DECLARE_$Ident\(\Q$name\E\)|
3784 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003785 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3786 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003787 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003788#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3789 $suppress_export{$realline_next} = 2;
3790 } else {
3791 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003792 }
3793 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003794 if (!defined $suppress_export{$linenr} &&
3795 $prevline =~ /^.\s*$/ &&
3796 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3797 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3798#print "FOO B <$lines[$linenr - 1]>\n";
3799 $suppress_export{$linenr} = 2;
3800 }
3801 if (defined $suppress_export{$linenr} &&
3802 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003803 WARN("EXPORT_SYMBOL",
3804 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003805 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003806
Joe Eloff5150bda2010-08-09 17:21:00 -07003807# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003808 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003809 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003810 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003811 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003812 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003813 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003814 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003815# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003816 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003817 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003818 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003819 $herecurr) &&
3820 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003821 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003822 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003823 }
3824
Joe Perches18130872014-08-06 16:11:22 -07003825# check for misordered declarations of char/short/int/long with signed/unsigned
3826 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3827 my $tmp = trim($1);
3828 WARN("MISORDERED_TYPE",
3829 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3830 }
3831
Joe Perches809e0822018-08-21 21:58:12 -07003832# check for unnecessary <signed> int declarations of short/long/long long
3833 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3834 my $type = trim($1);
3835 next if ($type !~ /\bint\b/);
3836 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3837 my $new_type = $type;
3838 $new_type =~ s/\b\s*int\s*\b/ /;
3839 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3840 $new_type =~ s/^const\s+//;
3841 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3842 $new_type = "const $new_type" if ($type =~ /^const\b/);
3843 $new_type =~ s/\s+/ /g;
3844 $new_type = trim($new_type);
3845 if (WARN("UNNECESSARY_INT",
3846 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3847 $fix) {
3848 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3849 }
3850 }
3851
Joe Perchescb710ec2010-10-26 14:23:20 -07003852# check for static const char * arrays.
3853 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003854 WARN("STATIC_CONST_CHAR_ARRAY",
3855 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003856 $herecurr);
3857 }
3858
3859# check for static char foo[] = "bar" declarations.
3860 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003861 WARN("STATIC_CONST_CHAR_ARRAY",
3862 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003863 $herecurr);
3864 }
3865
Joe Perchesab7e23f2015-04-16 12:44:22 -07003866# check for const <foo> const where <foo> is not a pointer or array type
3867 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3868 my $found = $1;
3869 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3870 WARN("CONST_CONST",
3871 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3872 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3873 WARN("CONST_CONST",
3874 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3875 }
3876 }
3877
Joe Perches9b0fa602014-04-03 14:49:18 -07003878# check for non-global char *foo[] = {"bar", ...} declarations.
3879 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3880 WARN("STATIC_CONST_CHAR_ARRAY",
3881 "char * array declaration might be better as static const\n" .
3882 $herecurr);
3883 }
3884
Joe Perchesb598b672015-04-16 12:44:36 -07003885# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3886 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3887 my $array = $1;
3888 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3889 my $array_div = $1;
3890 if (WARN("ARRAY_SIZE",
3891 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3892 $fix) {
3893 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3894 }
3895 }
3896 }
3897
Joe Perchesb36190c2014-01-27 17:07:18 -08003898# check for function declarations without arguments like "int foo()"
3899 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3900 if (ERROR("FUNCTION_WITHOUT_ARGS",
3901 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3902 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003903 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003904 }
3905 }
3906
Andy Whitcroft653d4872007-06-23 17:16:34 -07003907# check for new typedefs, only function parameters and sparse annotations
3908# make sense.
3909 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003910 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003911 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003912 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003913 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003914 WARN("NEW_TYPEDEFS",
3915 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003916 }
3917
3918# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003919 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003920 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3921 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003922 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003923
Andy Whitcroft65863862009-01-06 14:41:21 -08003924 # Should start with a space.
3925 $to =~ s/^(\S)/ $1/;
3926 # Should not end with a space.
3927 $to =~ s/\s+$//;
3928 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003929 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003930 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003931
Joe Perches3705ce52013-07-03 15:05:31 -07003932## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003933 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003934 if (ERROR("POINTER_LOCATION",
3935 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3936 $fix) {
3937 my $sub_from = $ident;
3938 my $sub_to = $ident;
3939 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003940 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003941 s@\Q$sub_from\E@$sub_to@;
3942 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003943 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003944 }
3945 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3946 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003947 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003948
Andy Whitcroft65863862009-01-06 14:41:21 -08003949 # Should start with a space.
3950 $to =~ s/^(\S)/ $1/;
3951 # Should not end with a space.
3952 $to =~ s/\s+$//;
3953 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003954 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003955 }
3956 # Modifiers should have spaces.
3957 $to =~ s/(\b$Modifier$)/$1 /;
3958
Joe Perches3705ce52013-07-03 15:05:31 -07003959## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003960 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003961 if (ERROR("POINTER_LOCATION",
3962 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3963 $fix) {
3964
3965 my $sub_from = $match;
3966 my $sub_to = $match;
3967 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003968 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003969 s@\Q$sub_from\E@$sub_to@;
3970 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003971 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003972 }
3973
Joe Perches9d3e3c72015-09-09 15:37:27 -07003974# avoid BUG() or BUG_ON()
3975 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07003976 my $msg_level = \&WARN;
3977 $msg_level = \&CHK if ($file);
3978 &{$msg_level}("AVOID_BUG",
3979 "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 -07003980 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003981
Joe Perches9d3e3c72015-09-09 15:37:27 -07003982# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003983 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003984 WARN("LINUX_VERSION_CODE",
3985 "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 -08003986 }
3987
Joe Perches17441222011-06-15 15:08:17 -07003988# check for uses of printk_ratelimit
3989 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003990 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003991 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003992 }
3993
Joe Percheseeef5732017-11-17 15:28:41 -08003994# printk should use KERN_* levels
3995 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
3996 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3997 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003998 }
3999
Joe Perches243f3802012-05-31 16:26:09 -07004000 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4001 my $orig = $1;
4002 my $level = lc($orig);
4003 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004004 my $level2 = $level;
4005 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004006 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004007 "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 -07004008 }
4009
4010 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004011 if (WARN("PREFER_PR_LEVEL",
4012 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4013 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004014 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004015 s/\bpr_warning\b/pr_warn/;
4016 }
Joe Perches243f3802012-05-31 16:26:09 -07004017 }
4018
Joe Perchesdc139312013-02-21 16:44:13 -08004019 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4020 my $orig = $1;
4021 my $level = lc($orig);
4022 $level = "warn" if ($level eq "warning");
4023 $level = "dbg" if ($level eq "debug");
4024 WARN("PREFER_DEV_LEVEL",
4025 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4026 }
4027
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004028# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4029# number of false positives, but assembly files are not checked, so at
4030# least the arch entry code will not trigger this warning.
4031 if ($line =~ /\bENOSYS\b/) {
4032 WARN("ENOSYS",
4033 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4034 }
4035
Andy Whitcroft653d4872007-06-23 17:16:34 -07004036# function brace can't be on same line, except for #defines of do while,
4037# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004038 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004039 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4040 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4041 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004042 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004043 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004044 $fix) {
4045 fix_delete_line($fixlinenr, $rawline);
4046 my $fixed_line = $rawline;
4047 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4048 my $line1 = $1;
4049 my $line2 = $2;
4050 fix_insert_line($fixlinenr, ltrim($line1));
4051 fix_insert_line($fixlinenr, "\+{");
4052 if ($line2 !~ /^\s*$/) {
4053 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4054 }
4055 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004056 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004057
Andy Whitcroft8905a672007-11-28 16:21:06 -08004058# open braces for enum, union and struct go on the same line.
4059 if ($line =~ /^.\s*{/ &&
4060 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004061 if (ERROR("OPEN_BRACE",
4062 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4063 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4064 fix_delete_line($fixlinenr - 1, $prevrawline);
4065 fix_delete_line($fixlinenr, $rawline);
4066 my $fixedline = rtrim($prevrawline) . " {";
4067 fix_insert_line($fixlinenr, $fixedline);
4068 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004069 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004070 if ($fixedline !~ /^\+\s*$/) {
4071 fix_insert_line($fixlinenr, $fixedline);
4072 }
4073 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004074 }
4075
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004076# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004077 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4078 if (WARN("SPACING",
4079 "missing space after $1 definition\n" . $herecurr) &&
4080 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004081 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004082 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4083 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004084 }
4085
Joe Perches31070b52014-01-23 15:54:49 -08004086# Function pointer declarations
4087# check spacing between type, funcptr, and args
4088# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004089 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004090 my $declare = $1;
4091 my $pre_pointer_space = $2;
4092 my $post_pointer_space = $3;
4093 my $funcname = $4;
4094 my $post_funcname_space = $5;
4095 my $pre_args_space = $6;
4096
Joe Perches91f72e92014-04-03 14:49:12 -07004097# the $Declare variable will capture all spaces after the type
4098# so check it for a missing trailing missing space but pointer return types
4099# don't need a space so don't warn for those.
4100 my $post_declare_space = "";
4101 if ($declare =~ /(\s+)$/) {
4102 $post_declare_space = $1;
4103 $declare = rtrim($declare);
4104 }
4105 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004106 WARN("SPACING",
4107 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004108 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004109 }
4110
4111# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004112# This test is not currently implemented because these declarations are
4113# equivalent to
4114# int foo(int bar, ...)
4115# and this is form shouldn't/doesn't generate a checkpatch warning.
4116#
4117# elsif ($declare =~ /\s{2,}$/) {
4118# WARN("SPACING",
4119# "Multiple spaces after return type\n" . $herecurr);
4120# }
Joe Perches31070b52014-01-23 15:54:49 -08004121
4122# unnecessary space "type ( *funcptr)(args...)"
4123 if (defined $pre_pointer_space &&
4124 $pre_pointer_space =~ /^\s/) {
4125 WARN("SPACING",
4126 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4127 }
4128
4129# unnecessary space "type (* funcptr)(args...)"
4130 if (defined $post_pointer_space &&
4131 $post_pointer_space =~ /^\s/) {
4132 WARN("SPACING",
4133 "Unnecessary space before function pointer name\n" . $herecurr);
4134 }
4135
4136# unnecessary space "type (*funcptr )(args...)"
4137 if (defined $post_funcname_space &&
4138 $post_funcname_space =~ /^\s/) {
4139 WARN("SPACING",
4140 "Unnecessary space after function pointer name\n" . $herecurr);
4141 }
4142
4143# unnecessary space "type (*funcptr) (args...)"
4144 if (defined $pre_args_space &&
4145 $pre_args_space =~ /^\s/) {
4146 WARN("SPACING",
4147 "Unnecessary space before function pointer arguments\n" . $herecurr);
4148 }
4149
4150 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004151 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004152 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004153 }
4154 }
4155
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004156# check for spacing round square brackets; allowed:
4157# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004158# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4159# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004160 while ($line =~ /(.*?\s)\[/g) {
4161 my ($where, $prefix) = ($-[1], $1);
4162 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004163 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004164 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004165 if (ERROR("BRACKET_SPACE",
4166 "space prohibited before open square bracket '['\n" . $herecurr) &&
4167 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004168 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004169 s/^(\+.*?)\s+\[/$1\[/;
4170 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004171 }
4172 }
4173
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004174# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004175 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004176 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004177 my $ctx_before = substr($line, 0, $-[1]);
4178 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004179
4180 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004181 if ($name =~ /^(?:
4182 if|for|while|switch|return|case|
4183 volatile|__volatile__|
4184 __attribute__|format|__extension__|
4185 asm|__asm__)$/x)
4186 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004187 # cpp #define statements have non-optional spaces, ie
4188 # if there is a space between the name and the open
4189 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004190 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004191
4192 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004193 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004194
4195 # If this whole things ends with a type its most
4196 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004197 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004198
4199 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004200 if (WARN("SPACING",
4201 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4202 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004203 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004204 s/\b$name\s+\(/$name\(/;
4205 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004206 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004207 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004208
Andy Whitcroft653d4872007-06-23 17:16:34 -07004209# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004210 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004211 my $fixed_line = "";
4212 my $line_fixed = 0;
4213
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004214 my $ops = qr{
4215 <<=|>>=|<=|>=|==|!=|
4216 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4217 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004218 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004219 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004220 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004221 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004222
4223## print("element count: <" . $#elements . ">\n");
4224## foreach my $el (@elements) {
4225## print("el: <$el>\n");
4226## }
4227
4228 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004229 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004230
Joe Perches3705ce52013-07-03 15:05:31 -07004231 foreach my $el (@elements) {
4232 push(@fix_elements, substr($rawline, $off, length($el)));
4233 $off += length($el);
4234 }
4235
4236 $off = 0;
4237
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004238 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004239 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004240
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004241 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004242
4243 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4244
4245## print("n: <$n> good: <$good>\n");
4246
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004247 $off += length($elements[$n]);
4248
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004249 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004250 my $ca = substr($opline, 0, $off);
4251 my $cc = '';
4252 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4253 $cc = substr($opline, $off + length($elements[$n + 1]));
4254 }
4255 my $cb = "$ca$;$cc";
4256
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004257 my $a = '';
4258 $a = 'V' if ($elements[$n] ne '');
4259 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004260 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004261 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4262 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004263 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004264
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004265 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004266
4267 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004268 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004269 $c = 'V' if ($elements[$n + 2] ne '');
4270 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004271 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004272 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4273 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004274 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004275 } else {
4276 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004277 }
4278
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004279 my $ctx = "${a}x${c}";
4280
4281 my $at = "(ctx:$ctx)";
4282
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004283 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004284 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004285
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004286 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004287 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004288
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004289 # Get the full operator variant.
4290 my $opv = $op . substr($curr_vars, $off, 1);
4291
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004292 # Ignore operators passed as parameters.
4293 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004294 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004295
Andy Whitcroftcf655042008-03-04 14:28:20 -08004296# # Ignore comments
4297# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004298
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004299 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004300 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004301 if ($ctx !~ /.x[WEBC]/ &&
4302 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004303 if (ERROR("SPACING",
4304 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004305 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004306 $line_fixed = 1;
4307 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004308 }
4309
4310 # // is a comment
4311 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004312
Joe Perchesb00e4812014-04-03 14:49:33 -07004313 # : when part of a bitfield
4314 } elsif ($opv eq ':B') {
4315 # skip the bitfield test for now
4316
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004317 # No spaces for:
4318 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004319 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004320 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004321 if (ERROR("SPACING",
4322 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004323 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004324 if (defined $fix_elements[$n + 2]) {
4325 $fix_elements[$n + 2] =~ s/^\s+//;
4326 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004327 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004328 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004329 }
4330
Joe Perches23810972014-12-10 15:51:32 -08004331 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004332 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004333 my $rtrim_before = 0;
4334 my $space_after = 0;
4335 if ($ctx =~ /Wx./) {
4336 if (ERROR("SPACING",
4337 "space prohibited before that '$op' $at\n" . $hereptr)) {
4338 $line_fixed = 1;
4339 $rtrim_before = 1;
4340 }
4341 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004342 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004343 if (ERROR("SPACING",
4344 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004345 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004346 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004347 $space_after = 1;
4348 }
4349 }
4350 if ($rtrim_before || $space_after) {
4351 if ($rtrim_before) {
4352 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4353 } else {
4354 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4355 }
4356 if ($space_after) {
4357 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004358 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004359 }
4360
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004361 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004362 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004363 #warn "'*' is part of type\n";
4364
4365 # unary operators should have a space before and
4366 # none after. May be left adjacent to another
4367 # unary operator, or a cast
4368 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004369 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004370 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004371 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004372 if (ERROR("SPACING",
4373 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004374 if ($n != $last_after + 2) {
4375 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4376 $line_fixed = 1;
4377 }
Joe Perches3705ce52013-07-03 15:05:31 -07004378 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004379 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004380 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004381 # A unary '*' may be const
4382
4383 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004384 if (ERROR("SPACING",
4385 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004386 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004387 if (defined $fix_elements[$n + 2]) {
4388 $fix_elements[$n + 2] =~ s/^\s+//;
4389 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004390 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004391 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004392 }
4393
4394 # unary ++ and unary -- are allowed no space on one side.
4395 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004396 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004397 if (ERROR("SPACING",
4398 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004399 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004400 $line_fixed = 1;
4401 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004402 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004403 if ($ctx =~ /Wx[BE]/ ||
4404 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004405 if (ERROR("SPACING",
4406 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004407 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004408 $line_fixed = 1;
4409 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004410 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004411 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004412 if (ERROR("SPACING",
4413 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004414 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004415 if (defined $fix_elements[$n + 2]) {
4416 $fix_elements[$n + 2] =~ s/^\s+//;
4417 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004418 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004419 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004420 }
4421
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004422 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004423 } elsif ($op eq '<<' or $op eq '>>' or
4424 $op eq '&' or $op eq '^' or $op eq '|' or
4425 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004426 $op eq '*' or $op eq '/' or
4427 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004428 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004429 if ($check) {
4430 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4431 if (CHK("SPACING",
4432 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4433 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4434 $fix_elements[$n + 2] =~ s/^\s+//;
4435 $line_fixed = 1;
4436 }
4437 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4438 if (CHK("SPACING",
4439 "space preferred before that '$op' $at\n" . $hereptr)) {
4440 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4441 $line_fixed = 1;
4442 }
4443 }
4444 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004445 if (ERROR("SPACING",
4446 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004447 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4448 if (defined $fix_elements[$n + 2]) {
4449 $fix_elements[$n + 2] =~ s/^\s+//;
4450 }
Joe Perches3705ce52013-07-03 15:05:31 -07004451 $line_fixed = 1;
4452 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004453 }
4454
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004455 # A colon needs no spaces before when it is
4456 # terminating a case value or a label.
4457 } elsif ($opv eq ':C' || $opv eq ':L') {
4458 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004459 if (ERROR("SPACING",
4460 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004461 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004462 $line_fixed = 1;
4463 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004464 }
4465
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004466 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004467 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004468 my $ok = 0;
4469
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004470 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004471 if (($op eq '<' &&
4472 $cc =~ /^\S+\@\S+>/) ||
4473 ($op eq '>' &&
4474 $ca =~ /<\S+\@\S+$/))
4475 {
4476 $ok = 1;
4477 }
4478
Joe Perchese0df7e12015-04-16 12:44:53 -07004479 # for asm volatile statements
4480 # ignore a colon with another
4481 # colon immediately before or after
4482 if (($op eq ':') &&
4483 ($ca =~ /:$/ || $cc =~ /^:/)) {
4484 $ok = 1;
4485 }
4486
Joe Perches84731622013-11-12 15:10:05 -08004487 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004488 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004489 my $msg_level = \&ERROR;
4490 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004491
Jean Delvare0675a8f2017-09-08 16:16:07 -07004492 if (&{$msg_level}("SPACING",
4493 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004494 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4495 if (defined $fix_elements[$n + 2]) {
4496 $fix_elements[$n + 2] =~ s/^\s+//;
4497 }
Joe Perches3705ce52013-07-03 15:05:31 -07004498 $line_fixed = 1;
4499 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004500 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004501 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004502 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004503
4504## print("n: <$n> GOOD: <$good>\n");
4505
4506 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004507 }
Joe Perches3705ce52013-07-03 15:05:31 -07004508
4509 if (($#elements % 2) == 0) {
4510 $fixed_line = $fixed_line . $fix_elements[$#elements];
4511 }
4512
Joe Perches194f66f2014-08-06 16:11:03 -07004513 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4514 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004515 }
4516
4517
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004518 }
4519
Joe Perches786b6322013-07-03 15:05:32 -07004520# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004521 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004522 if (WARN("SPACING",
4523 "space prohibited before semicolon\n" . $herecurr) &&
4524 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004525 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004526 s/^(\+.*\S)\s+;/$1;/;
4527 }
4528 }
4529
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004530# check for multiple assignments
4531 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004532 CHK("MULTIPLE_ASSIGNMENTS",
4533 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004534 }
4535
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004536## # check for multiple declarations, allowing for a function declaration
4537## # continuation.
4538## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4539## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4540##
4541## # Remove any bracketed sections to ensure we do not
4542## # falsly report the parameters of functions.
4543## my $ln = $line;
4544## while ($ln =~ s/\([^\(\)]*\)//g) {
4545## }
4546## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004547## WARN("MULTIPLE_DECLARATION",
4548## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004549## }
4550## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004551
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004552#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004553 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004554 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004555 if (ERROR("SPACING",
4556 "space required before the open brace '{'\n" . $herecurr) &&
4557 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004558 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004559 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004560 }
4561
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004562## # check for blank lines before declarations
4563## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4564## $prevrawline =~ /^.\s*$/) {
4565## WARN("SPACING",
4566## "No blank lines before declarations\n" . $hereprev);
4567## }
4568##
4569
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004570# closing brace should have a space following it when it has anything
4571# on the line
4572 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004573 if (ERROR("SPACING",
4574 "space required after that close brace '}'\n" . $herecurr) &&
4575 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004576 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004577 s/}((?!(?:,|;|\)))\S)/} $1/;
4578 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004579 }
4580
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004581# check spacing on square brackets
4582 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004583 if (ERROR("SPACING",
4584 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4585 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004586 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004587 s/\[\s+/\[/;
4588 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004589 }
4590 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004591 if (ERROR("SPACING",
4592 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4593 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004594 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004595 s/\s+\]/\]/;
4596 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004597 }
4598
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004599# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004600 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4601 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004602 if (ERROR("SPACING",
4603 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4604 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004605 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004606 s/\(\s+/\(/;
4607 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004608 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004609 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004610 $line !~ /for\s*\(.*;\s+\)/ &&
4611 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004612 if (ERROR("SPACING",
4613 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4614 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004615 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004616 s/\s+\)/\)/;
4617 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004618 }
4619
Joe Perchese2826fd2014-08-06 16:10:48 -07004620# check unnecessary parentheses around addressof/dereference single $Lvals
4621# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4622
4623 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004624 my $var = $1;
4625 if (CHK("UNNECESSARY_PARENTHESES",
4626 "Unnecessary parentheses around $var\n" . $herecurr) &&
4627 $fix) {
4628 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4629 }
4630 }
4631
4632# check for unnecessary parentheses around function pointer uses
4633# ie: (foo->bar)(); should be foo->bar();
4634# but not "if (foo->bar) (" to avoid some false positives
4635 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4636 my $var = $2;
4637 if (CHK("UNNECESSARY_PARENTHESES",
4638 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4639 $fix) {
4640 my $var2 = deparenthesize($var);
4641 $var2 =~ s/\s//g;
4642 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4643 }
4644 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004645
Joe Perches63b7c732017-09-08 16:16:01 -07004646# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004647# when !drivers/staging or command-line uses --strict
4648 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004649 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004650 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4651 my $if_stat = $1;
4652 my $test = substr($2, 1, -1);
4653 my $herectx;
4654 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4655 my $match = $1;
4656 # avoid parentheses around potential macro args
4657 next if ($match =~ /^\s*\w+\s*$/);
4658 if (!defined($herectx)) {
4659 $herectx = $here . "\n";
4660 my $cnt = statement_rawlines($if_stat);
4661 for (my $n = 0; $n < $cnt; $n++) {
4662 my $rl = raw_line($linenr, $n);
4663 $herectx .= $rl . "\n";
4664 last if $rl =~ /^[ \+].*\{/;
4665 }
4666 }
4667 CHK("UNNECESSARY_PARENTHESES",
4668 "Unnecessary parentheses around '$match'\n" . $herectx);
4669 }
4670 }
4671
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004672#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004673 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004674 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004675 if (WARN("INDENTED_LABEL",
4676 "labels should not be indented\n" . $herecurr) &&
4677 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004678 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004679 s/^(.)\s+/$1/;
4680 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004681 }
4682
Joe Perches5b9553a2014-04-03 14:49:21 -07004683# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004684 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004685 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004686 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004687 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4688 my $value = $1;
4689 $value = deparenthesize($value);
4690 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4691 ERROR("RETURN_PARENTHESES",
4692 "return is not a function, parentheses are not required\n" . $herecurr);
4693 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004694 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004695 ERROR("SPACING",
4696 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004697 }
4698 }
Joe Perches507e5142013-11-12 15:10:13 -08004699
Joe Perchesb43ae212014-06-23 13:22:07 -07004700# unnecessary return in a void function
4701# at end-of-function, with the previous line a single leading tab, then return;
4702# and the line before that not a goto label target like "out:"
4703 if ($sline =~ /^[ \+]}\s*$/ &&
4704 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4705 $linenr >= 3 &&
4706 $lines[$linenr - 3] =~ /^[ +]/ &&
4707 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004708 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004709 "void function return statements are not generally useful\n" . $hereprev);
4710 }
Joe Perches9819cf22014-06-04 16:12:09 -07004711
Joe Perches189248d2014-01-23 15:54:47 -08004712# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004713 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004714 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4715 my $openparens = $1;
4716 my $count = $openparens =~ tr@\(@\(@;
4717 my $msg = "";
4718 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4719 my $comp = $4; #Not $1 because of $LvalOrFunc
4720 $msg = " - maybe == should be = ?" if ($comp eq "==");
4721 WARN("UNNECESSARY_PARENTHESES",
4722 "Unnecessary parentheses$msg\n" . $herecurr);
4723 }
4724 }
4725
Joe Perchesc5595fa2015-09-09 15:37:58 -07004726# comparisons with a constant or upper case identifier on the left
4727# avoid cases like "foo + BAR < baz"
4728# only fix matches surrounded by parentheses to avoid incorrect
4729# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004730 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004731 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4732 my $lead = $1;
4733 my $const = $2;
4734 my $comp = $3;
4735 my $to = $4;
4736 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004737 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004738 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4739 WARN("CONSTANT_COMPARISON",
4740 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4741 $fix) {
4742 if ($comp eq "<") {
4743 $newcomp = ">";
4744 } elsif ($comp eq "<=") {
4745 $newcomp = ">=";
4746 } elsif ($comp eq ">") {
4747 $newcomp = "<";
4748 } elsif ($comp eq ">=") {
4749 $newcomp = "<=";
4750 }
4751 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4752 }
4753 }
4754
Joe Perchesf34e4a42015-04-16 12:44:19 -07004755# Return of what appears to be an errno should normally be negative
4756 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004757 my $name = $1;
4758 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004759 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004760 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004761 }
4762 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004763
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004764# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004765 if ($line =~ /\b(if|while|for|switch)\(/) {
4766 if (ERROR("SPACING",
4767 "space required before the open parenthesis '('\n" . $herecurr) &&
4768 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004769 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004770 s/\b(if|while|for|switch)\(/$1 \(/;
4771 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004772 }
4773
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004774# Check for illegal assignment in if conditional -- and check for trailing
4775# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004776 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004777 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4778 ctx_statement_block($linenr, $realcnt, 0)
4779 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004780 my ($stat_next) = ctx_statement_block($line_nr_next,
4781 $remain_next, $off_next);
4782 $stat_next =~ s/\n./\n /g;
4783 ##print "stat<$stat> stat_next<$stat_next>\n";
4784
4785 if ($stat_next =~ /^\s*while\b/) {
4786 # If the statement carries leading newlines,
4787 # then count those as offsets.
4788 my ($whitespace) =
4789 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4790 my $offset =
4791 statement_rawlines($whitespace) - 1;
4792
4793 $suppress_whiletrailers{$line_nr_next +
4794 $offset} = 1;
4795 }
4796 }
4797 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004798 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004799 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004800 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004801
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004802 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004803 ERROR("ASSIGN_IN_IF",
4804 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004805 }
4806
4807 # Find out what is on the end of the line after the
4808 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004809 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004810 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004811 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004812 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4813 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004814 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004815 # Find out how long the conditional actually is.
4816 my @newlines = ($c =~ /\n/gs);
4817 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004818 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004819
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004820 $stat_real = raw_line($linenr, $cond_lines)
4821 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004822 if (defined($stat_real) && $cond_lines > 1) {
4823 $stat_real = "[...]\n$stat_real";
4824 }
4825
Joe Perches000d1cc12011-07-25 17:13:25 -07004826 ERROR("TRAILING_STATEMENTS",
4827 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004828 }
4829 }
4830
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004831# Check for bitwise tests written as boolean
4832 if ($line =~ /
4833 (?:
4834 (?:\[|\(|\&\&|\|\|)
4835 \s*0[xX][0-9]+\s*
4836 (?:\&\&|\|\|)
4837 |
4838 (?:\&\&|\|\|)
4839 \s*0[xX][0-9]+\s*
4840 (?:\&\&|\|\||\)|\])
4841 )/x)
4842 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004843 WARN("HEXADECIMAL_BOOLEAN_TEST",
4844 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004845 }
4846
Andy Whitcroft8905a672007-11-28 16:21:06 -08004847# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004848 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4849 my $s = $1;
4850 $s =~ s/$;//g; # Remove any comments
4851 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004852 ERROR("TRAILING_STATEMENTS",
4853 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004854 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004855 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004856# if should not continue a brace
4857 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004858 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004859 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004860 $herecurr);
4861 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004862# case and default should not have general statements after them
4863 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4864 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004865 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004866 \s*return\s+
4867 )/xg)
4868 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004869 ERROR("TRAILING_STATEMENTS",
4870 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004871 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004872
4873 # Check for }<nl>else {, these must be at the same
4874 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004875 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4876 $previndent == $indent) {
4877 if (ERROR("ELSE_AFTER_BRACE",
4878 "else should follow close brace '}'\n" . $hereprev) &&
4879 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4880 fix_delete_line($fixlinenr - 1, $prevrawline);
4881 fix_delete_line($fixlinenr, $rawline);
4882 my $fixedline = $prevrawline;
4883 $fixedline =~ s/}\s*$//;
4884 if ($fixedline !~ /^\+\s*$/) {
4885 fix_insert_line($fixlinenr, $fixedline);
4886 }
4887 $fixedline = $rawline;
4888 $fixedline =~ s/^(.\s*)else/$1} else/;
4889 fix_insert_line($fixlinenr, $fixedline);
4890 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004891 }
4892
Joe Perches8b8856f2014-08-06 16:11:14 -07004893 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4894 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004895 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4896
4897 # Find out what is on the end of the line after the
4898 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004899 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004900 $s =~ s/\n.*//g;
4901
4902 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004903 if (ERROR("WHILE_AFTER_BRACE",
4904 "while should follow close brace '}'\n" . $hereprev) &&
4905 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4906 fix_delete_line($fixlinenr - 1, $prevrawline);
4907 fix_delete_line($fixlinenr, $rawline);
4908 my $fixedline = $prevrawline;
4909 my $trailing = $rawline;
4910 $trailing =~ s/^\+//;
4911 $trailing = trim($trailing);
4912 $fixedline =~ s/}\s*$/} $trailing/;
4913 fix_insert_line($fixlinenr, $fixedline);
4914 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004915 }
4916 }
4917
Joe Perches95e2c602013-07-03 15:05:20 -07004918#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004919 while ($line =~ m{($Constant|$Lval)}g) {
4920 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004921
4922#gcc binary extension
4923 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004924 if (WARN("GCC_BINARY_CONSTANT",
4925 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4926 $fix) {
4927 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004928 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004929 s/\b$var\b/$hexval/;
4930 }
Joe Perches95e2c602013-07-03 15:05:20 -07004931 }
4932
4933#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004934 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004935 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004936#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004937 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004938#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004939 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4940#Ignore some three character SI units explicitly, like MiB and KHz
4941 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004942 while ($var =~ m{($Ident)}g) {
4943 my $word = $1;
4944 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004945 if ($check) {
4946 seed_camelcase_includes();
4947 if (!$file && !$camelcase_file_seeded) {
4948 seed_camelcase_file($realfile);
4949 $camelcase_file_seeded = 1;
4950 }
4951 }
Joe Perches7e781f62013-09-11 14:23:55 -07004952 if (!defined $camelcase{$word}) {
4953 $camelcase{$word} = 1;
4954 CHK("CAMELCASE",
4955 "Avoid CamelCase: <$word>\n" . $herecurr);
4956 }
Joe Perches34456862013-07-03 15:05:34 -07004957 }
Joe Perches323c1262012-12-17 16:02:07 -08004958 }
4959 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004960
4961#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004962 if ($line =~ /\#\s*define.*\\\s+$/) {
4963 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4964 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4965 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004966 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004967 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004968 }
4969
Fabian Frederick0e212e02015-04-16 12:44:25 -07004970# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4971# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004972 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004973 my $file = "$1.h";
4974 my $checkfile = "include/linux/$file";
4975 if (-f "$root/$checkfile" &&
4976 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004977 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004978 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004979 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4980 if ($asminclude > 0) {
4981 if ($realfile =~ m{^arch/}) {
4982 CHK("ARCH_INCLUDE_LINUX",
4983 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4984 } else {
4985 WARN("INCLUDE_LINUX",
4986 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4987 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004988 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004989 }
4990 }
4991
Andy Whitcroft653d4872007-06-23 17:16:34 -07004992# multi-statement macros should be enclosed in a do while loop, grab the
4993# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004994# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004995 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4996 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004997 my $ln = $linenr;
4998 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004999 my ($off, $dstat, $dcond, $rest);
5000 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005001 my $has_flow_statement = 0;
5002 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005003 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005004 ctx_statement_block($linenr, $realcnt, 0);
5005 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005006 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005007 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005008
Joe Perches08a28432014-10-13 15:51:55 -07005009 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005010 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005011
Joe Perchesf59b64b2016-10-11 13:52:08 -07005012 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5013 my $define_args = $1;
5014 my $define_stmt = $dstat;
5015 my @def_args = ();
5016
5017 if (defined $define_args && $define_args ne "") {
5018 $define_args = substr($define_args, 1, length($define_args) - 2);
5019 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005020 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005021 @def_args = split(",", $define_args);
5022 }
5023
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005024 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005025 $dstat =~ s/\\\n.//g;
5026 $dstat =~ s/^\s*//s;
5027 $dstat =~ s/\s*$//s;
5028
5029 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005030 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5031 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005032 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005033 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005034 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005035
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005036 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005037 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5038 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005039 {
5040 }
5041
Joe Perches42e15292016-03-15 14:58:01 -07005042 # Make asm volatile uses seem like a generic function
5043 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5044
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005045 my $exceptions = qr{
5046 $Declare|
5047 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005048 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005049 DECLARE_PER_CPU|
5050 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005051 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005052 union|
5053 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005054 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005055 ^\"|\"$|
5056 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005057 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005058 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005059
5060 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005061 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005062 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005063
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005064 if ($dstat ne '' &&
5065 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5066 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005067 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005068 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005069 $dstat !~ /$exceptions/ &&
5070 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005071 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005072 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005073 $dstat !~ /^for\s*$Constant$/ && # for (...)
5074 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5075 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005076 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005077 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005078 {
Joe Perchese7955562017-05-08 15:55:48 -07005079 if ($dstat =~ /^\s*if\b/) {
5080 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5081 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5082 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005083 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5084 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5085 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005086 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005087 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005088 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005089
5090 }
Joe Perches52076492016-10-11 13:52:14 -07005091
5092 # Make $define_stmt single line, comment-free, etc
5093 my @stmt_array = split('\n', $define_stmt);
5094 my $first = 1;
5095 $define_stmt = "";
5096 foreach my $l (@stmt_array) {
5097 $l =~ s/\\$//;
5098 if ($first) {
5099 $define_stmt = $l;
5100 $first = 0;
5101 } elsif ($l =~ /^[\+ ]/) {
5102 $define_stmt .= substr($l, 1);
5103 }
5104 }
5105 $define_stmt =~ s/$;//g;
5106 $define_stmt =~ s/\s+/ /g;
5107 $define_stmt = trim($define_stmt);
5108
Joe Perchesf59b64b2016-10-11 13:52:08 -07005109# check if any macro arguments are reused (ignore '...' and 'type')
5110 foreach my $arg (@def_args) {
5111 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005112 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005113 my $tmp_stmt = $define_stmt;
5114 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5115 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5116 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005117 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005118 if ($use_cnt > 1) {
5119 CHK("MACRO_ARG_REUSE",
5120 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005121 }
5122# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005123 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005124 ((defined($1) && $1 ne ',') ||
5125 (defined($2) && $2 ne ','))) {
5126 CHK("MACRO_ARG_PRECEDENCE",
5127 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005128 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005129 }
Joe Perches5023d342012-12-17 16:01:47 -08005130
Joe Perches08a28432014-10-13 15:51:55 -07005131# check for macros with flow control, but without ## concatenation
5132# ## concatenation is commonly a macro that defines a function so ignore those
5133 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005134 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005135 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005136
Joe Perches08a28432014-10-13 15:51:55 -07005137 WARN("MACRO_WITH_FLOW_CONTROL",
5138 "Macros with flow control statements should be avoided\n" . "$herectx");
5139 }
5140
Joe Perches481eb482012-12-17 16:01:56 -08005141# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005142
5143 } else {
5144 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005145 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5146 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005147 $line =~ /^\+.*\\$/) {
5148 WARN("LINE_CONTINUATIONS",
5149 "Avoid unnecessary line continuations\n" . $herecurr);
5150 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005151 }
5152
Joe Perchesb13edf72012-07-30 14:41:24 -07005153# do {} while (0) macro tests:
5154# single-statement macros do not need to be enclosed in do while (0) loop,
5155# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005156 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005157 $realfile !~ m@/vmlinux.lds.h$@ &&
5158 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5159 my $ln = $linenr;
5160 my $cnt = $realcnt;
5161 my ($off, $dstat, $dcond, $rest);
5162 my $ctx = '';
5163 ($dstat, $dcond, $ln, $cnt, $off) =
5164 ctx_statement_block($linenr, $realcnt, 0);
5165 $ctx = $dstat;
5166
5167 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005168 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005169
5170 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5171 my $stmts = $2;
5172 my $semis = $3;
5173
5174 $ctx =~ s/\n*$//;
5175 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005176 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005177
Joe Perchesac8e97f2012-08-21 16:15:53 -07005178 if (($stmts =~ tr/;/;/) == 1 &&
5179 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005180 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5181 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5182 }
5183 if (defined $semis && $semis ne "") {
5184 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5185 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5186 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005187 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5188 $ctx =~ s/\n*$//;
5189 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005190 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005191
5192 WARN("TRAILING_SEMICOLON",
5193 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005194 }
5195 }
5196
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005197# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005198 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5199 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005200 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005201 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005202 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5203 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005204 my @allowed = ();
5205 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005206 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005207 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005208 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005209 for my $chunk (@chunks) {
5210 my ($cond, $block) = @{$chunk};
5211
Andy Whitcroft773647a2008-03-28 14:15:58 -07005212 # If the condition carries leading newlines, then count those as offsets.
5213 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5214 my $offset = statement_rawlines($whitespace) - 1;
5215
Joe Perchesaad4f612012-03-23 15:02:19 -07005216 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005217 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5218
5219 # We have looked at and allowed this specific line.
5220 $suppress_ifbraces{$ln + $offset} = 1;
5221
5222 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005223 $ln += statement_rawlines($block) - 1;
5224
Andy Whitcroft773647a2008-03-28 14:15:58 -07005225 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005226
5227 $seen++ if ($block =~ /^\s*{/);
5228
Joe Perchesaad4f612012-03-23 15:02:19 -07005229 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005230 if (statement_lines($cond) > 1) {
5231 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005232 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005233 }
5234 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005235 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005236 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005237 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005238 if (statement_block_size($block) > 1) {
5239 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005240 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005241 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005242 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005243 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005244 if ($seen) {
5245 my $sum_allowed = 0;
5246 foreach (@allowed) {
5247 $sum_allowed += $_;
5248 }
5249 if ($sum_allowed == 0) {
5250 WARN("BRACES",
5251 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5252 } elsif ($sum_allowed != $allow &&
5253 $seen != $allow) {
5254 CHK("BRACES",
5255 "braces {} should be used on all arms of this statement\n" . $herectx);
5256 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005257 }
5258 }
5259 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005260 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005261 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005262 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005263
Andy Whitcroftcf655042008-03-04 14:28:20 -08005264 # Check the pre-context.
5265 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5266 #print "APW: ALLOWED: pre<$1>\n";
5267 $allowed = 1;
5268 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005269
5270 my ($level, $endln, @chunks) =
5271 ctx_statement_full($linenr, $realcnt, $-[0]);
5272
Andy Whitcroftcf655042008-03-04 14:28:20 -08005273 # Check the condition.
5274 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005275 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005276 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005277 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005278 }
5279 if (statement_lines($cond) > 1) {
5280 #print "APW: ALLOWED: cond<$cond>\n";
5281 $allowed = 1;
5282 }
5283 if ($block =~/\b(?:if|for|while)\b/) {
5284 #print "APW: ALLOWED: block<$block>\n";
5285 $allowed = 1;
5286 }
5287 if (statement_block_size($block) > 1) {
5288 #print "APW: ALLOWED: lines block<$block>\n";
5289 $allowed = 1;
5290 }
5291 # Check the post-context.
5292 if (defined $chunks[1]) {
5293 my ($cond, $block) = @{$chunks[1]};
5294 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005295 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005296 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005297 if ($block =~ /^\s*\{/) {
5298 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5299 $allowed = 1;
5300 }
5301 }
5302 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005303 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005304 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005305
Joe Perches000d1cc12011-07-25 17:13:25 -07005306 WARN("BRACES",
5307 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005308 }
5309 }
5310
Joe Perchese4c5bab2017-02-24 15:01:41 -08005311# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005312 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5313 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005314 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5315 }
5316
Joe Perches0979ae62012-12-17 16:01:59 -08005317# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005318 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005319 if (CHK("BRACES",
5320 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5321 $fix && $prevrawline =~ /^\+/) {
5322 fix_delete_line($fixlinenr - 1, $prevrawline);
5323 }
Joe Perches0979ae62012-12-17 16:01:59 -08005324 }
Joe Perches77b9a532013-07-03 15:05:29 -07005325 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005326 if (CHK("BRACES",
5327 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5328 $fix) {
5329 fix_delete_line($fixlinenr, $rawline);
5330 }
Joe Perches0979ae62012-12-17 16:01:59 -08005331 }
5332
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005333# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005334 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5335 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005336 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005337 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005338 }
5339
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005340# Check for user-visible strings broken across lines, which breaks the ability
5341# to grep for the string. Make exceptions when the previous string ends in a
5342# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5343# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005344 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005345 $prevline =~ /"\s*$/ &&
5346 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5347 if (WARN("SPLIT_STRING",
5348 "quoted string split across lines\n" . $hereprev) &&
5349 $fix &&
5350 $prevrawline =~ /^\+.*"\s*$/ &&
5351 $last_coalesced_string_linenr != $linenr - 1) {
5352 my $extracted_string = get_quoted_string($line, $rawline);
5353 my $comma_close = "";
5354 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5355 $comma_close = $1;
5356 }
5357
5358 fix_delete_line($fixlinenr - 1, $prevrawline);
5359 fix_delete_line($fixlinenr, $rawline);
5360 my $fixedline = $prevrawline;
5361 $fixedline =~ s/"\s*$//;
5362 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5363 fix_insert_line($fixlinenr - 1, $fixedline);
5364 $fixedline = $rawline;
5365 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5366 if ($fixedline !~ /\+\s*$/) {
5367 fix_insert_line($fixlinenr, $fixedline);
5368 }
5369 $last_coalesced_string_linenr = $linenr;
5370 }
5371 }
5372
5373# check for missing a space in a string concatenation
5374 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5375 WARN('MISSING_SPACE',
5376 "break quoted strings at a space character\n" . $hereprev);
5377 }
5378
Joe Perchese4b7d302017-05-08 15:55:51 -07005379# check for an embedded function name in a string when the function is known
5380# This does not work very well for -f --file checking as it depends on patch
5381# context providing the function name or a single line form for in-file
5382# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005383 if ($line =~ /^\+.*$String/ &&
5384 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005385 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5386 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005387 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005388 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005389 }
5390
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005391# check for spaces before a quoted newline
5392 if ($rawline =~ /^.*\".*\s\\n/) {
5393 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5394 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5395 $fix) {
5396 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5397 }
5398
5399 }
5400
Joe Perchesf17dba42014-10-13 15:51:51 -07005401# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005402 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5403 if (CHK("CONCATENATED_STRING",
5404 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5405 $fix) {
5406 while ($line =~ /($String)/g) {
5407 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5408 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5409 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5410 }
5411 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005412 }
5413
Joe Perches90ad30e2014-12-10 15:51:59 -08005414# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005415 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005416 if (WARN("STRING_FRAGMENTS",
5417 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5418 $fix) {
5419 while ($line =~ /($String)(?=\s*")/g) {
5420 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5421 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5422 }
5423 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005424 }
5425
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005426# check for non-standard and hex prefixed decimal printf formats
5427 my $show_L = 1; #don't show the same defect twice
5428 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005429 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005430 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005431 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005432 # check for %L
5433 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005434 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005435 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5436 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005437 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005438 # check for %Z
5439 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5440 WARN("PRINTF_Z",
5441 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5442 $show_Z = 0;
5443 }
5444 # check for 0x<decimal>
5445 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5446 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005447 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5448 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005449 }
5450
5451# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005452 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005453 WARN("LINE_CONTINUATIONS",
5454 "Avoid line continuations in quoted strings\n" . $herecurr);
5455 }
5456
Andy Whitcroft00df3442007-06-08 13:47:06 -07005457# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005458 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005459 WARN("IF_0",
5460 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5461 }
5462
5463# warn about #if 1
5464 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5465 WARN("IF_1",
5466 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005467 }
5468
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005469# check for needless "if (<foo>) fn(<foo>)" uses
5470 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005471 my $tested = quotemeta($1);
5472 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5473 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5474 my $func = $1;
5475 if (WARN('NEEDLESS_IF',
5476 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5477 $fix) {
5478 my $do_fix = 1;
5479 my $leading_tabs = "";
5480 my $new_leading_tabs = "";
5481 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5482 $leading_tabs = $1;
5483 } else {
5484 $do_fix = 0;
5485 }
5486 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5487 $new_leading_tabs = $1;
5488 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5489 $do_fix = 0;
5490 }
5491 } else {
5492 $do_fix = 0;
5493 }
5494 if ($do_fix) {
5495 fix_delete_line($fixlinenr - 1, $prevrawline);
5496 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5497 }
5498 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005499 }
5500 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005501
Joe Perchesebfdc402014-08-06 16:10:27 -07005502# check for unnecessary "Out of Memory" messages
5503 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5504 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5505 (defined $1 || defined $3) &&
5506 $linenr > 3) {
5507 my $testval = $2;
5508 my $testline = $lines[$linenr - 3];
5509
5510 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5511# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5512
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005513 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 -07005514 WARN("OOM_MESSAGE",
5515 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5516 }
5517 }
5518
Joe Perchesf78d98f2014-10-13 15:52:01 -07005519# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005520 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005521 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5522 my $level = $1;
5523 if (WARN("UNNECESSARY_KERN_LEVEL",
5524 "Possible unnecessary $level\n" . $herecurr) &&
5525 $fix) {
5526 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5527 }
5528 }
5529
Joe Perches45c55e92017-02-24 15:01:31 -08005530# check for logging continuations
5531 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5532 WARN("LOGGING_CONTINUATION",
5533 "Avoid logging continuation uses where feasible\n" . $herecurr);
5534 }
5535
Joe Perchesabb08a52014-12-10 15:51:46 -08005536# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005537 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005538 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5539 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5540 WARN("MASK_THEN_SHIFT",
5541 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5542 }
5543
Joe Perchesb75ac612014-12-10 15:52:02 -08005544# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005545 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005546 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5547 my $val = $1;
5548 my $equal = "!";
5549 $equal = "" if ($4 eq "!=");
5550 if (CHK("COMPARISON_TO_NULL",
5551 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5552 $fix) {
5553 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5554 }
5555 }
5556 }
5557
Joe Perches8716de32013-09-11 14:24:05 -07005558# check for bad placement of section $InitAttribute (e.g.: __initdata)
5559 if ($line =~ /(\b$InitAttribute\b)/) {
5560 my $attr = $1;
5561 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5562 my $ptr = $1;
5563 my $var = $2;
5564 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5565 ERROR("MISPLACED_INIT",
5566 "$attr should be placed after $var\n" . $herecurr)) ||
5567 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5568 WARN("MISPLACED_INIT",
5569 "$attr should be placed after $var\n" . $herecurr))) &&
5570 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005571 $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 -07005572 }
5573 }
5574 }
5575
Joe Perchese970b8842013-11-12 15:10:10 -08005576# check for $InitAttributeData (ie: __initdata) with const
5577 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5578 my $attr = $1;
5579 $attr =~ /($InitAttributePrefix)(.*)/;
5580 my $attr_prefix = $1;
5581 my $attr_type = $2;
5582 if (ERROR("INIT_ATTRIBUTE",
5583 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5584 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005585 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005586 s/$InitAttributeData/${attr_prefix}initconst/;
5587 }
5588 }
5589
5590# check for $InitAttributeConst (ie: __initconst) without const
5591 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5592 my $attr = $1;
5593 if (ERROR("INIT_ATTRIBUTE",
5594 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5595 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005596 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005597 /(^\+\s*(?:static\s+))/;
5598 $lead = rtrim($1);
5599 $lead = "$lead " if ($lead !~ /^\+$/);
5600 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005601 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005602 }
5603 }
5604
Joe Perchesc17893c2015-04-16 12:44:42 -07005605# check for __read_mostly with const non-pointer (should just be const)
5606 if ($line =~ /\b__read_mostly\b/ &&
5607 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5608 if (ERROR("CONST_READ_MOSTLY",
5609 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5610 $fix) {
5611 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5612 }
5613 }
5614
Joe Perchesfbdb8132014-04-03 14:49:14 -07005615# don't use __constant_<foo> functions outside of include/uapi/
5616 if ($realfile !~ m@^include/uapi/@ &&
5617 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5618 my $constant_func = $1;
5619 my $func = $constant_func;
5620 $func =~ s/^__constant_//;
5621 if (WARN("CONSTANT_CONVERSION",
5622 "$constant_func should be $func\n" . $herecurr) &&
5623 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005624 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005625 }
5626 }
5627
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005628# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005629 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005630 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005631 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005632 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005633 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005634 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5635 }
5636 if ($delay > 2000) {
5637 WARN("LONG_UDELAY",
5638 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005639 }
5640 }
5641
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005642# warn about unexpectedly long msleep's
5643 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5644 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005645 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005646 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005647 }
5648 }
5649
Joe Perches36ec1932013-07-03 15:05:25 -07005650# check for comparisons of jiffies
5651 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5652 WARN("JIFFIES_COMPARISON",
5653 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5654 }
5655
Joe Perches9d7a34a2013-07-03 15:05:26 -07005656# check for comparisons of get_jiffies_64()
5657 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5658 WARN("JIFFIES_COMPARISON",
5659 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5660 }
5661
Andy Whitcroft00df3442007-06-08 13:47:06 -07005662# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005663# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005664# print "#ifdef in C files should be avoided\n";
5665# print "$herecurr";
5666# $clean = 0;
5667# }
5668
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005669# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005670 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005671 if (ERROR("SPACING",
5672 "exactly one space required after that #$1\n" . $herecurr) &&
5673 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005674 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005675 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5676 }
5677
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005678 }
5679
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005680# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005681 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5682 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005683 my $which = $1;
5684 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005685 CHK("UNCOMMENTED_DEFINITION",
5686 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005687 }
5688 }
5689# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005690
5691 my $barriers = qr{
5692 mb|
5693 rmb|
5694 wmb|
5695 read_barrier_depends
5696 }x;
5697 my $barrier_stems = qr{
5698 mb__before_atomic|
5699 mb__after_atomic|
5700 store_release|
5701 load_acquire|
5702 store_mb|
5703 (?:$barriers)
5704 }x;
5705 my $all_barriers = qr{
5706 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005707 smp_(?:$barrier_stems)|
5708 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005709 }x;
5710
5711 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005712 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005713 WARN("MEMORY_BARRIER",
5714 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005715 }
5716 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005717
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005718 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5719
5720 if ($realfile !~ m@^include/asm-generic/@ &&
5721 $realfile !~ m@/barrier\.h$@ &&
5722 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5723 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5724 WARN("MEMORY_BARRIER",
5725 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5726 }
5727
Joe Perchescb426e92015-06-25 15:02:46 -07005728# check for waitqueue_active without a comment.
5729 if ($line =~ /\bwaitqueue_active\s*\(/) {
5730 if (!ctx_has_comment($first_line, $linenr)) {
5731 WARN("WAITQUEUE_ACTIVE",
5732 "waitqueue_active without comment\n" . $herecurr);
5733 }
5734 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005735
Paul E. McKenney91db2592017-11-27 09:37:35 -08005736# check for smp_read_barrier_depends and read_barrier_depends
5737 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5738 WARN("READ_BARRIER_DEPENDS",
5739 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5740 }
5741
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005742# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005743 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005744 CHK("ARCH_DEFINES",
5745 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005746 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005747
Joe Perches596ed452017-07-12 14:37:02 -07005748# check that the storage class is not after a type
5749 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005750 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005751 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5752 }
5753# Check that the storage class is at the beginning of a declaration
5754 if ($line =~ /\b$Storage\b/ &&
5755 $line !~ /^.\s*$Storage/ &&
5756 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5757 $1 !~ /[\,\)]\s*$/) {
5758 WARN("STORAGE_CLASS",
5759 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005760 }
5761
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005762# check the location of the inline attribute, that it is between
5763# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005764 if ($line =~ /\b$Type\s+$Inline\b/ ||
5765 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005766 ERROR("INLINE_LOCATION",
5767 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005768 }
5769
Andy Whitcroft8905a672007-11-28 16:21:06 -08005770# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005771 if ($realfile !~ m@\binclude/uapi/@ &&
5772 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005773 if (WARN("INLINE",
5774 "plain inline is preferred over $1\n" . $herecurr) &&
5775 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005776 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005777
5778 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005779 }
5780
Joe Perches3d130fd2011-01-12 17:00:00 -08005781# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005782 if ($realfile !~ m@\binclude/uapi/@ &&
5783 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005784 WARN("PREFER_PACKED",
5785 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005786 }
5787
Joe Perches39b7e282011-07-25 17:13:24 -07005788# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005789 if ($realfile !~ m@\binclude/uapi/@ &&
5790 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005791 WARN("PREFER_ALIGNED",
5792 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005793 }
5794
Joe Perches5f14d3b2012-01-10 15:09:52 -08005795# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005796 if ($realfile !~ m@\binclude/uapi/@ &&
5797 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005798 if (WARN("PREFER_PRINTF",
5799 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5800 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005801 $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 -07005802
5803 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005804 }
5805
Joe Perches6061d942012-03-23 15:02:16 -07005806# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005807 if ($realfile !~ m@\binclude/uapi/@ &&
5808 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005809 if (WARN("PREFER_SCANF",
5810 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5811 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005812 $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 -07005813 }
Joe Perches6061d942012-03-23 15:02:16 -07005814 }
5815
Joe Perches619a9082014-12-10 15:51:35 -08005816# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005817 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005818 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5819 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5820 $line =~ /\b__weak\b/)) {
5821 ERROR("WEAK_DECLARATION",
5822 "Using weak declarations can have unintended link defects\n" . $herecurr);
5823 }
5824
Tomas Winklerfd39f902016-12-12 16:46:34 -08005825# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005826 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005827 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005828 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5829 my $type = $1;
5830 if ($type =~ /\b($typeC99Typedefs)\b/) {
5831 $type = $1;
5832 my $kernel_type = 'u';
5833 $kernel_type = 's' if ($type =~ /^_*[si]/);
5834 $type =~ /(\d+)/;
5835 $kernel_type .= $1;
5836 if (CHK("PREFER_KERNEL_TYPES",
5837 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5838 $fix) {
5839 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5840 }
5841 }
5842 }
5843
Joe Perches938224b2016-01-20 14:59:15 -08005844# check for cast of C90 native int or longer types constants
5845 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5846 my $cast = $1;
5847 my $const = $2;
5848 if (WARN("TYPECAST_INT_CONSTANT",
5849 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5850 $fix) {
5851 my $suffix = "";
5852 my $newconst = $const;
5853 $newconst =~ s/${Int_type}$//;
5854 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5855 if ($cast =~ /\blong\s+long\b/) {
5856 $suffix .= 'LL';
5857 } elsif ($cast =~ /\blong\b/) {
5858 $suffix .= 'L';
5859 }
5860 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5861 }
5862 }
5863
Joe Perches8f53a9b2010-03-05 13:43:48 -08005864# check for sizeof(&)
5865 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005866 WARN("SIZEOF_ADDRESS",
5867 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005868 }
5869
Joe Perches66c80b62012-07-30 14:41:22 -07005870# check for sizeof without parenthesis
5871 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005872 if (WARN("SIZEOF_PARENTHESIS",
5873 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5874 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005875 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005876 }
Joe Perches66c80b62012-07-30 14:41:22 -07005877 }
5878
Joe Perches88982fe2012-12-17 16:02:00 -08005879# check for struct spinlock declarations
5880 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5881 WARN("USE_SPINLOCK_T",
5882 "struct spinlock should be spinlock_t\n" . $herecurr);
5883 }
5884
Joe Perchesa6962d72013-04-29 16:18:13 -07005885# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005886 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005887 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005888 $fmt =~ s/%%//g;
5889 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005890 if (WARN("PREFER_SEQ_PUTS",
5891 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5892 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005893 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005894 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005895 }
5896 }
5897
Joe Perches478b1792018-04-10 16:33:34 -07005898# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07005899 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07005900 defined $stat &&
5901 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5902 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005903 my $stat_real;
5904
Joe Perches0b523762017-05-08 15:55:36 -07005905 my $lc = $stat =~ tr@\n@@;
5906 $lc = $lc + $linenr;
5907 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07005908 my $specifier;
5909 my $extension;
5910 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07005911 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5912 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005913
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005914 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5915 $specifier = $1;
5916 $extension = $2;
5917 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5918 $bad_specifier = $specifier;
5919 last;
5920 }
5921 if ($extension eq "x" && !defined($stat_real)) {
5922 if (!defined($stat_real)) {
5923 $stat_real = get_stat_real($linenr, $lc);
5924 }
5925 WARN("VSPRINTF_SPECIFIER_PX",
5926 "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");
5927 }
5928 }
5929 if ($bad_specifier ne "") {
5930 my $stat_real = get_stat_real($linenr, $lc);
5931 my $ext_type = "Invalid";
5932 my $use = "";
5933 if ($bad_specifier =~ /p[Ff]/) {
5934 $ext_type = "Deprecated";
5935 $use = " - use %pS instead";
5936 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5937 }
5938
5939 WARN("VSPRINTF_POINTER_EXTENSION",
5940 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5941 }
Joe Perches0b523762017-05-08 15:55:36 -07005942 }
5943 }
5944
Andy Whitcroft554e1652012-01-10 15:09:57 -08005945# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07005946 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005947 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005948 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005949
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005950 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005951 my $ms_val = $7;
5952 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005953
Andy Whitcroft554e1652012-01-10 15:09:57 -08005954 if ($ms_size =~ /^(0x|)0$/i) {
5955 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005956 "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 -08005957 } elsif ($ms_size =~ /^(0x|)1$/i) {
5958 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005959 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5960 }
5961 }
5962
Joe Perches98a9bba2014-01-23 15:54:52 -08005963# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005964# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07005965# defined $stat &&
5966# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5967# if (WARN("PREFER_ETHER_ADDR_COPY",
5968# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5969# $fix) {
5970# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5971# }
5972# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005973
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005974# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005975# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07005976# defined $stat &&
5977# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5978# WARN("PREFER_ETHER_ADDR_EQUAL",
5979# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5980# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005981
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005982# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5983# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07005984# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07005985# defined $stat &&
5986# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5987#
5988# my $ms_val = $7;
5989#
5990# if ($ms_val =~ /^(?:0x|)0+$/i) {
5991# if (WARN("PREFER_ETH_ZERO_ADDR",
5992# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5993# $fix) {
5994# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5995# }
5996# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5997# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5998# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5999# $fix) {
6000# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6001# }
6002# }
6003# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006004
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006005# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006006 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006007 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006008 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006009 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006010 my $call = $1;
6011 my $cast1 = deparenthesize($2);
6012 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006013 my $cast2 = deparenthesize($7);
6014 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006015 my $cast;
6016
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006017 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006018 $cast = "$cast1 or $cast2";
6019 } elsif ($cast1 ne "") {
6020 $cast = $cast1;
6021 } else {
6022 $cast = $cast2;
6023 }
6024 WARN("MINMAX",
6025 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006026 }
6027 }
6028
Joe Perches4a273192012-07-30 14:41:20 -07006029# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006030 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006031 defined $stat &&
6032 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6033 my $min = $1;
6034 my $max = $7;
6035 if ($min eq $max) {
6036 WARN("USLEEP_RANGE",
6037 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6038 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6039 $min > $max) {
6040 WARN("USLEEP_RANGE",
6041 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6042 }
6043 }
6044
Joe Perches823b7942013-11-12 15:10:15 -08006045# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006046 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006047 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006048 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006049 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6050 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6051 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6052 my $lc = $stat =~ tr@\n@@;
6053 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006054 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006055 WARN("NAKED_SSCANF",
6056 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6057 }
6058
Joe Perchesafc819a2014-06-04 16:12:08 -07006059# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006060 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006061 defined $stat &&
6062 $line =~ /\bsscanf\b/) {
6063 my $lc = $stat =~ tr@\n@@;
6064 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006065 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006066 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6067 my $format = $6;
6068 my $count = $format =~ tr@%@%@;
6069 if ($count == 1 &&
6070 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6071 WARN("SSCANF_TO_KSTRTO",
6072 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6073 }
6074 }
6075 }
6076
Joe Perches70dc8a42013-09-11 14:23:58 -07006077# check for new externs in .h files.
6078 if ($realfile =~ /\.h$/ &&
6079 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006080 if (CHK("AVOID_EXTERNS",
6081 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006082 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006083 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006084 }
6085 }
6086
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006087# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006088 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006089 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006090 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006091 my $function_name = $1;
6092 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006093
6094 my $s = $stat;
6095 if (defined $cond) {
6096 substr($s, 0, length($cond), '');
6097 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006098 if ($s =~ /^\s*;/ &&
6099 $function_name ne 'uninitialized_var')
6100 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006101 WARN("AVOID_EXTERNS",
6102 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006103 }
6104
6105 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006106 WARN("FUNCTION_ARGUMENTS",
6107 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006108 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006109
6110 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6111 $stat =~ /^.\s*extern\s+/)
6112 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006113 WARN("AVOID_EXTERNS",
6114 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006115 }
6116
Joe Perchesa0ad7592017-07-10 15:52:19 -07006117# check for function declarations that have arguments without identifier names
6118 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006119 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006120 $1 ne "void") {
6121 my $args = trim($1);
6122 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6123 my $arg = trim($1);
6124 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6125 WARN("FUNCTION_ARGUMENTS",
6126 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6127 }
6128 }
6129 }
6130
Joe Perchesa0ad7592017-07-10 15:52:19 -07006131# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006132 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006133 defined $stat &&
6134 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6135 $context_function = $1;
6136
6137# check for multiline function definition with misplaced open brace
6138 my $ok = 0;
6139 my $cnt = statement_rawlines($stat);
6140 my $herectx = $here . "\n";
6141 for (my $n = 0; $n < $cnt; $n++) {
6142 my $rl = raw_line($linenr, $n);
6143 $herectx .= $rl . "\n";
6144 $ok = 1 if ($rl =~ /^[ \+]\{/);
6145 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6146 last if $rl =~ /^[ \+].*\{/;
6147 }
6148 if (!$ok) {
6149 ERROR("OPEN_BRACE",
6150 "open brace '{' following function definitions go on the next line\n" . $herectx);
6151 }
6152 }
6153
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006154# checks for new __setup's
6155 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6156 my $name = $1;
6157
6158 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006159 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006160 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006161 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006162 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006163
6164# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006165 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006166 WARN("UNNECESSARY_CASTS",
6167 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006168 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006169
Joe Perchesa640d252013-07-03 15:05:21 -07006170# alloc style
6171# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006172 if ($perl_version_ok &&
Joe Perchesa640d252013-07-03 15:05:21 -07006173 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6174 CHK("ALLOC_SIZEOF_STRUCT",
6175 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6176 }
6177
Joe Perches60a55362014-06-04 16:12:07 -07006178# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006179 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006180 defined $stat &&
6181 $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 -07006182 my $oldfunc = $3;
6183 my $a1 = $4;
6184 my $a2 = $10;
6185 my $newfunc = "kmalloc_array";
6186 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006187 my $r1 = $a1;
6188 my $r2 = $a2;
6189 if ($a1 =~ /^sizeof\s*\S/) {
6190 $r1 = $a2;
6191 $r2 = $a1;
6192 }
6193 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6194 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006195 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006196 my $herectx = get_stat_here($linenr, $cnt, $here);
6197
Joe Perches60a55362014-06-04 16:12:07 -07006198 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006199 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6200 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006201 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006202 $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 -07006203 }
6204 }
6205 }
6206
Joe Perches972fdea2013-04-29 16:18:12 -07006207# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006208 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006209 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6210 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006211 WARN("KREALLOC_ARG_REUSE",
6212 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6213 }
6214
Joe Perches5ce59ae2013-02-21 16:44:18 -08006215# check for alloc argument mismatch
6216 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6217 WARN("ALLOC_ARRAY_ARGS",
6218 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6219 }
6220
Joe Perchescaf2a542011-01-12 16:59:56 -08006221# check for multiple semicolons
6222 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006223 if (WARN("ONE_SEMICOLON",
6224 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6225 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006226 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006227 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006228 }
6229
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006230# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6231 if ($realfile !~ m@^include/uapi/@ &&
6232 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006233 my $ull = "";
6234 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6235 if (CHK("BIT_MACRO",
6236 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6237 $fix) {
6238 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6239 }
6240 }
6241
Joe Perches2d632742016-05-20 17:04:00 -07006242# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6243 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6244 my $config = $1;
6245 if (WARN("PREFER_IS_ENABLED",
6246 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6247 $fix) {
6248 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6249 }
6250 }
6251
Joe Perchese81f2392014-08-06 16:11:25 -07006252# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006253 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6254 my $has_break = 0;
6255 my $has_statement = 0;
6256 my $count = 0;
6257 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006258 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006259 $prevline--;
6260 my $rline = $rawlines[$prevline - 1];
6261 my $fline = $lines[$prevline - 1];
6262 last if ($fline =~ /^\@\@/);
6263 next if ($fline =~ /^\-/);
6264 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6265 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6266 next if ($fline =~ /^.[\s$;]*$/);
6267 $has_statement = 1;
6268 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006269 $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 -08006270 }
6271 if (!$has_break && $has_statement) {
6272 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006273 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006274 }
6275 }
6276
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006277# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006278 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006279 defined $stat &&
6280 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006281 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006282 my $herectx = get_stat_here($linenr, $cnt, $here);
6283
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006284 WARN("DEFAULT_NO_BREAK",
6285 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006286 }
6287
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006288# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006289 if ($line =~ /\b__FUNCTION__\b/) {
6290 if (WARN("USE_FUNC",
6291 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6292 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006293 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006294 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006295 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006296
Joe Perches62ec8182015-02-13 14:38:18 -08006297# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6298 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6299 ERROR("DATE_TIME",
6300 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6301 }
6302
Joe Perches2c924882012-03-23 15:02:20 -07006303# check for use of yield()
6304 if ($line =~ /\byield\s*\(\s*\)/) {
6305 WARN("YIELD",
6306 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6307 }
6308
Joe Perches179f8f42013-07-03 15:05:30 -07006309# check for comparisons against true and false
6310 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6311 my $lead = $1;
6312 my $arg = $2;
6313 my $test = $3;
6314 my $otype = $4;
6315 my $trail = $5;
6316 my $op = "!";
6317
6318 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6319
6320 my $type = lc($otype);
6321 if ($type =~ /^(?:true|false)$/) {
6322 if (("$test" eq "==" && "$type" eq "true") ||
6323 ("$test" eq "!=" && "$type" eq "false")) {
6324 $op = "";
6325 }
6326
6327 CHK("BOOL_COMPARISON",
6328 "Using comparison to $otype is error prone\n" . $herecurr);
6329
6330## maybe suggesting a correct construct would better
6331## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6332
6333 }
6334 }
6335
Joe Perches5d430902018-04-10 16:34:25 -07006336# check for bool bitfields
6337 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6338 WARN("BOOL_BITFIELD",
6339 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6340 }
6341
Joe Perchesd7295932018-08-21 21:57:26 -07006342# check for bool use in .h files
6343 if ($realfile =~ /\.h$/ &&
6344 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6345 CHK("BOOL_MEMBER",
6346 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6347 }
6348
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006349# check for semaphores initialized locked
6350 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006351 WARN("CONSIDER_COMPLETION",
6352 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006353 }
Joe Perches6712d852012-03-23 15:02:20 -07006354
Joe Perches67d0a072011-10-31 17:13:10 -07006355# recommend kstrto* over simple_strto* and strict_strto*
6356 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006357 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006358 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006359 }
Joe Perches6712d852012-03-23 15:02:20 -07006360
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006361# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006362 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006363 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006364 "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 -07006365 }
Joe Perches6712d852012-03-23 15:02:20 -07006366
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006367# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006368# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006369 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006370 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006371 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006372 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006373 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006374
6375# use of NR_CPUS is usually wrong
6376# ignore definitions of NR_CPUS and usage to define arrays as likely right
6377 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006378 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6379 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006380 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6381 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6382 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006383 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006384 WARN("NR_CPUS",
6385 "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 -07006386 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006387
Joe Perches52ea8502013-11-12 15:10:09 -08006388# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6389 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6390 ERROR("DEFINE_ARCH_HAS",
6391 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6392 }
6393
Joe Perchesacd93622015-02-13 14:38:38 -08006394# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006395 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006396 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6397 WARN("LIKELY_MISUSE",
6398 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6399 }
6400
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006401# whine mightly about in_atomic
6402 if ($line =~ /\bin_atomic\s*\(/) {
6403 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006404 ERROR("IN_ATOMIC",
6405 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006406 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006407 WARN("IN_ATOMIC",
6408 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006409 }
6410 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006411
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006412# check for mutex_trylock_recursive usage
6413 if ($line =~ /mutex_trylock_recursive/) {
6414 ERROR("LOCKING",
6415 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6416 }
6417
Peter Zijlstra1704f472010-03-19 01:37:42 +01006418# check for lockdep_set_novalidate_class
6419 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6420 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6421 if ($realfile !~ m@^kernel/lockdep@ &&
6422 $realfile !~ m@^include/linux/lockdep@ &&
6423 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006424 ERROR("LOCKDEP",
6425 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006426 }
6427 }
Dave Jones88f88312011-01-12 16:59:59 -08006428
Joe Perchesb392c642015-04-16 12:44:16 -07006429 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6430 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006431 WARN("EXPORTED_WORLD_WRITABLE",
6432 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006433 }
Joe Perches24358802014-04-03 14:49:13 -07006434
Joe Perches00180462018-02-06 15:38:55 -08006435# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6436# and whether or not function naming is typical and if
6437# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006438 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006439 defined $stat &&
6440 $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*\)/) {
6441 my $var = $1;
6442 my $perms = $2;
6443 my $show = $3;
6444 my $store = $4;
6445 my $octal_perms = perms_to_octal($perms);
6446 if ($show =~ /^${var}_show$/ &&
6447 $store =~ /^${var}_store$/ &&
6448 $octal_perms eq "0644") {
6449 if (WARN("DEVICE_ATTR_RW",
6450 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6451 $fix) {
6452 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6453 }
6454 } elsif ($show =~ /^${var}_show$/ &&
6455 $store =~ /^NULL$/ &&
6456 $octal_perms eq "0444") {
6457 if (WARN("DEVICE_ATTR_RO",
6458 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6459 $fix) {
6460 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6461 }
6462 } elsif ($show =~ /^NULL$/ &&
6463 $store =~ /^${var}_store$/ &&
6464 $octal_perms eq "0200") {
6465 if (WARN("DEVICE_ATTR_WO",
6466 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6467 $fix) {
6468 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6469 }
6470 } elsif ($octal_perms eq "0644" ||
6471 $octal_perms eq "0444" ||
6472 $octal_perms eq "0200") {
6473 my $newshow = "$show";
6474 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6475 my $newstore = $store;
6476 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6477 my $rename = "";
6478 if ($show ne $newshow) {
6479 $rename .= " '$show' to '$newshow'";
6480 }
6481 if ($store ne $newstore) {
6482 $rename .= " '$store' to '$newstore'";
6483 }
6484 WARN("DEVICE_ATTR_FUNCTIONS",
6485 "Consider renaming function(s)$rename\n" . $herecurr);
6486 } else {
6487 WARN("DEVICE_ATTR_PERMS",
6488 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6489 }
6490 }
6491
Joe Perches515a2352014-04-03 14:49:24 -07006492# Mode permission misuses where it seems decimal should be octal
6493# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006494# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6495# specific definition of not visible in sysfs.
6496# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6497# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006498 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006499 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006500 $line =~ /$mode_perms_search/) {
6501 foreach my $entry (@mode_permission_funcs) {
6502 my $func = $entry->[0];
6503 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006504
Joe Perches459cf0a2016-10-11 13:52:19 -07006505 my $lc = $stat =~ tr@\n@@;
6506 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006507 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006508
Joe Perches515a2352014-04-03 14:49:24 -07006509 my $skip_args = "";
6510 if ($arg_pos > 1) {
6511 $arg_pos--;
6512 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6513 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006514 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006515 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006516 my $val = $1;
6517 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006518 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6519 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6520 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006521 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006522 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006523 }
6524 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006525 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006526 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006527 }
Joe Perches24358802014-04-03 14:49:13 -07006528 }
6529 }
6530 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006531
Joe Perches459cf0a2016-10-11 13:52:19 -07006532# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006533 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006534 my $oval = $1;
6535 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006536 if (WARN("SYMBOLIC_PERMS",
6537 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6538 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006539 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006540 }
6541 }
6542
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006543# validate content of MODULE_LICENSE against list from include/linux/module.h
6544 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6545 my $extracted_string = get_quoted_string($line, $rawline);
6546 my $valid_licenses = qr{
6547 GPL|
6548 GPL\ v2|
6549 GPL\ and\ additional\ rights|
6550 Dual\ BSD/GPL|
6551 Dual\ MIT/GPL|
6552 Dual\ MPL/GPL|
6553 Proprietary
6554 }x;
6555 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6556 WARN("MODULE_LICENSE",
6557 "unknown module license " . $extracted_string . "\n" . $herecurr);
6558 }
6559 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006560 }
6561
6562 # If we have no input at all, then there is nothing to report on
6563 # so just keep quiet.
6564 if ($#rawlines == -1) {
6565 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006566 }
6567
Andy Whitcroft8905a672007-11-28 16:21:06 -08006568 # In mailback mode only produce a report in the negative, for
6569 # things that appear to be patches.
6570 if ($mailback && ($clean == 1 || !$is_patch)) {
6571 exit(0);
6572 }
6573
6574 # This is not a patch, and we are are in 'no-patch' mode so
6575 # just keep quiet.
6576 if (!$chk_patch && !$is_patch) {
6577 exit(0);
6578 }
6579
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006580 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006581 ERROR("NOT_UNIFIED_DIFF",
6582 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006583 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006584 if ($is_patch && $has_commit_log && $chk_signoff) {
6585 if ($signoff == 0) {
6586 ERROR("MISSING_SIGN_OFF",
6587 "Missing Signed-off-by: line(s)\n");
6588 } elsif (!$authorsignoff) {
6589 WARN("NO_AUTHOR_SIGN_OFF",
6590 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6591 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006592 }
6593
Andy Whitcroft8905a672007-11-28 16:21:06 -08006594 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006595 if ($summary && !($clean == 1 && $quiet == 1)) {
6596 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006597 print "total: $cnt_error errors, $cnt_warn warnings, " .
6598 (($check)? "$cnt_chk checks, " : "") .
6599 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006600 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006601
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006602 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006603 # If there were any defects found and not already fixing them
6604 if (!$clean and !$fix) {
6605 print << "EOM"
6606
6607NOTE: For some of the reported defects, checkpatch may be able to
6608 mechanically convert to the typical style using --fix or --fix-inplace.
6609EOM
6610 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006611 # If there were whitespace errors which cleanpatch can fix
6612 # then suggest that.
6613 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006614 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006615 print << "EOM"
6616
6617NOTE: Whitespace errors detected.
6618 You may wish to use scripts/cleanpatch or scripts/cleanfile
6619EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006620 }
6621 }
6622
Joe Perchesd752fcc2014-08-06 16:11:05 -07006623 if ($clean == 0 && $fix &&
6624 ("@rawlines" ne "@fixed" ||
6625 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006626 my $newfile = $filename;
6627 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006628 my $linecount = 0;
6629 my $f;
6630
Joe Perchesd752fcc2014-08-06 16:11:05 -07006631 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6632
Joe Perches3705ce52013-07-03 15:05:31 -07006633 open($f, '>', $newfile)
6634 or die "$P: Can't open $newfile for write\n";
6635 foreach my $fixed_line (@fixed) {
6636 $linecount++;
6637 if ($file) {
6638 if ($linecount > 3) {
6639 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006640 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006641 }
6642 } else {
6643 print $f $fixed_line . "\n";
6644 }
6645 }
6646 close($f);
6647
6648 if (!$quiet) {
6649 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006650
Joe Perches3705ce52013-07-03 15:05:31 -07006651Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6652
6653Do _NOT_ trust the results written to this file.
6654Do _NOT_ submit these changes without inspecting them for correctness.
6655
6656This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6657No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006658EOM
6659 }
6660 }
6661
Joe Perchesd8469f12015-06-25 15:03:00 -07006662 if ($quiet == 0) {
6663 print "\n";
6664 if ($clean == 1) {
6665 print "$vname has no obvious style problems and is ready for submission.\n";
6666 } else {
6667 print "$vname has style problems, please review.\n";
6668 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006669 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006670 return $clean;
6671}