blob: c17cbc06911f9fcfd4af8391f460d41afe6339b2 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046my %debug;
Joe Perches34456862013-07-03 15:05:34 -070047my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070048my %use_type = ();
49my @use = ();
50my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070051my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070052my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070053my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080054my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070057my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070058my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070059my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070060my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070061my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070062my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070063my $color = "auto";
Joe Perchesdadf6802016-08-02 14:04:33 -070064my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070065
66sub help {
67 my ($exitcode) = @_;
68
69 print << "EOM";
70Usage: $P [OPTION]... [FILE]...
71Version: $V
72
73Options:
74 -q, --quiet quiet
75 --no-tree run without a kernel tree
76 --no-signoff do not check for 'Signed-off-by' line
77 --patch treat FILE as patchfile (default)
78 --emacs emacs compile window format
79 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070080 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070081 -g, --git treat FILE as a single commit or git revision range
82 single git commit with:
83 <rev>
84 <rev>^
85 <rev>~n
86 multiple git commits with:
87 <rev1>..<rev2>
88 <rev1>...<rev2>
89 <rev>-<count>
90 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070091 -f, --file treat FILE as regular source file
92 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070093 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070094 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070095 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070096 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080097 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070098 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -070099 --root=PATH PATH to the kernel tree root
100 --no-summary suppress the per-file summary
101 --mailback only produce a report in case of warnings/errors
102 --summary-file include the filename in summary
103 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
104 'values', 'possible', 'type', and 'attr' (default
105 is all off)
106 --test-only=WORD report only warnings/errors containing WORD
107 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700108 --fix EXPERIMENTAL - may create horrible results
109 If correctable single-line errors exist, create
110 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
111 with potential errors corrected to the preferred
112 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800113 --fix-inplace EXPERIMENTAL - may create horrible results
114 Is the same as --fix, but overwrites the input
115 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700116 --ignore-perl-version override checking of perl version. expect
117 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700118 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700119 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700120 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700121 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700122 --color[=WHEN] Use colors 'always', 'never', or only when output
123 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700124 -h, --help, --version display this help and exit
125
126When FILE is - read standard input.
127EOM
128
129 exit($exitcode);
130}
131
Joe Perches3beb42e2016-05-20 17:04:14 -0700132sub uniq {
133 my %seen;
134 return grep { !$seen{$_}++ } @_;
135}
136
137sub list_types {
138 my ($exitcode) = @_;
139
140 my $count = 0;
141
142 local $/ = undef;
143
144 open(my $script, '<', abs_path($P)) or
145 die "$P: Can't read '$P' $!\n";
146
147 my $text = <$script>;
148 close($script);
149
150 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700151 # Also catch when type or level is passed through a variable
152 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700153 push (@types, $_);
154 }
155 @types = sort(uniq(@types));
156 print("#\tMessage type\n\n");
157 foreach my $type (@types) {
158 print(++$count . "\t" . $type . "\n");
159 }
160
161 exit($exitcode);
162}
163
Joe Perches000d1cc12011-07-25 17:13:25 -0700164my $conf = which_conf($configuration_file);
165if (-f $conf) {
166 my @conf_args;
167 open(my $conffile, '<', "$conf")
168 or warn "$P: Can't find a readable $configuration_file file $!\n";
169
170 while (<$conffile>) {
171 my $line = $_;
172
173 $line =~ s/\s*\n?$//g;
174 $line =~ s/^\s*//g;
175 $line =~ s/\s+/ /g;
176
177 next if ($line =~ m/^\s*#/);
178 next if ($line =~ m/^\s*$/);
179
180 my @words = split(" ", $line);
181 foreach my $word (@words) {
182 last if ($word =~ m/^#/);
183 push (@conf_args, $word);
184 }
185 }
186 close($conffile);
187 unshift(@ARGV, @conf_args) if @conf_args;
188}
189
John Brooks737c0762017-07-10 15:52:24 -0700190# Perl's Getopt::Long allows options to take optional arguments after a space.
191# Prevent --color by itself from consuming other arguments
192foreach (@ARGV) {
193 if ($_ eq "--color" || $_ eq "-color") {
194 $_ = "--color=$color";
195 }
196}
197
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700198GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700199 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700200 'tree!' => \$tree,
201 'signoff!' => \$chk_signoff,
202 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700203 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800204 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700205 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700206 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700207 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208 'subjective!' => \$check,
209 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700210 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700211 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700212 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700213 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800214 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700215 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700216 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800217 'summary!' => \$summary,
218 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800219 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700220 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800221 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700222 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800223 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700224 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700225 'codespell!' => \$codespell,
226 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700227 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700228 'color=s' => \$color,
229 'no-color' => \$color, #keep old behaviors of -nocolor
230 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700231 'h|help' => \$help,
232 'version' => \$help
233) or help(1);
234
235help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700236
Joe Perches3beb42e2016-05-20 17:04:14 -0700237list_types(0) if ($list_types);
238
Joe Perches9624b8d2014-01-23 15:54:44 -0800239$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700240$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800241
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700242my $exit = 0;
243
Joe Perches5b579802018-08-21 21:57:33 -0700244my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700245if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700246 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700247 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700248 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700249}
250
Allen Hubbe45107ff2016-08-02 14:04:47 -0700251#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700252if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700253 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700254}
255
John Brooks737c0762017-07-10 15:52:24 -0700256if ($color =~ /^[01]$/) {
257 $color = !$color;
258} elsif ($color =~ /^always$/i) {
259 $color = 1;
260} elsif ($color =~ /^never$/i) {
261 $color = 0;
262} elsif ($color =~ /^auto$/i) {
263 $color = (-t STDOUT);
264} else {
265 die "Invalid color mode: $color\n";
266}
267
Joe Perches91bfe482013-09-11 14:23:59 -0700268sub hash_save_array_words {
269 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700270
Joe Perches91bfe482013-09-11 14:23:59 -0700271 my @array = split(/,/, join(',', @$arrayRef));
272 foreach my $word (@array) {
273 $word =~ s/\s*\n?$//g;
274 $word =~ s/^\s*//g;
275 $word =~ s/\s+/ /g;
276 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700277
Joe Perches91bfe482013-09-11 14:23:59 -0700278 next if ($word =~ m/^\s*#/);
279 next if ($word =~ m/^\s*$/);
280
281 $hashRef->{$word}++;
282 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700283}
284
Joe Perches91bfe482013-09-11 14:23:59 -0700285sub hash_show_words {
286 my ($hashRef, $prefix) = @_;
287
Joe Perches3c816e42015-06-25 15:03:29 -0700288 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700289 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700290 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700291 print " $word";
292 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700293 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700294 }
295}
296
297hash_save_array_words(\%ignore_type, \@ignore);
298hash_save_array_words(\%use_type, \@use);
299
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800300my $dbg_values = 0;
301my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700302my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700303my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800304for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800305 ## no critic
306 eval "\${dbg_$key} = '$debug{$key}';";
307 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800308}
309
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700310my $rpt_cleaners = 0;
311
Andy Whitcroft8905a672007-11-28 16:21:06 -0800312if ($terse) {
313 $emacs = 1;
314 $quiet++;
315}
316
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700317if ($tree) {
318 if (defined $root) {
319 if (!top_of_kernel_tree($root)) {
320 die "$P: $root: --root does not point at a valid tree\n";
321 }
322 } else {
323 if (top_of_kernel_tree('.')) {
324 $root = '.';
325 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
326 top_of_kernel_tree($1)) {
327 $root = $1;
328 }
329 }
330
331 if (!defined $root) {
332 print "Must be run from the top-level dir. of a kernel tree\n";
333 exit(2);
334 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700335}
336
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700337my $emitted_corrupt = 0;
338
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700339our $Ident = qr{
340 [A-Za-z_][A-Za-z\d_]*
341 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
342 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700343our $Storage = qr{extern|static|asmlinkage};
344our $Sparse = qr{
345 __user|
346 __kernel|
347 __force|
348 __iomem|
349 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800350 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700351 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700352 __refconst|
353 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800354 __rcu|
355 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700356 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800357our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
358our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
359our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
360our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
361our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700362
Wolfram Sang52131292010-03-05 13:43:51 -0800363# Notes to $Attribute:
364# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700365our $Attribute = qr{
366 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700367 __percpu|
368 __nocast|
369 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200370 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700371 __packed__|
372 __packed2__|
373 __naked|
374 __maybe_unused|
375 __always_unused|
376 __noreturn|
377 __used|
378 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800379 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700380 __noclone|
381 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700382 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700383 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700384 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700385 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700386 ____cacheline_aligned|
387 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800388 ____cacheline_internodealigned_in_smp|
389 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700390 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700391our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700392our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700393our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
394our $Lval = qr{$Ident(?:$Member)*};
395
Joe Perches95e2c602013-07-03 15:05:20 -0700396our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
397our $Binary = qr{(?i)0b[01]+$Int_type?};
398our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
399our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700400our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800401our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800402our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
403our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
404our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800405our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700406our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800407our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700408our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700409our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700410our $Operators = qr{
411 <=|>=|==|!=|
412 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700413 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700414 }x;
415
Joe Perches91cb5192014-04-03 14:49:32 -0700416our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
417
Joe Perchesab7e23f2015-04-16 12:44:22 -0700418our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800419our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700420our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700421our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800422our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700423our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800424our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700425our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800426
Joe Perches15662b32011-10-31 17:13:12 -0700427our $NON_ASCII_UTF8 = qr{
428 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700429 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
430 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
431 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
432 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
433 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
434 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
435}x;
436
Joe Perches15662b32011-10-31 17:13:12 -0700437our $UTF8 = qr{
438 [\x09\x0A\x0D\x20-\x7E] # ASCII
439 | $NON_ASCII_UTF8
440}x;
441
Joe Perchese6176fa2015-06-25 15:02:49 -0700442our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800443our $typeOtherOSTypedefs = qr{(?x:
444 u_(?:char|short|int|long) | # bsd
445 u(?:nchar|short|int|long) # sysv
446)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700447our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700448 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700449 atomic_t
450)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700451our $typeTypedefs = qr{(?x:
452 $typeC99Typedefs\b|
453 $typeOtherOSTypedefs\b|
454 $typeKernelTypedefs\b
455)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700456
Joe Perches6d32f7a2015-11-06 16:31:37 -0800457our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
458
Joe Perches691e6692010-03-05 13:43:51 -0800459our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800460 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700461 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches87bd4992017-11-17 15:28:48 -0800462 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700463 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700464 panic|
Joe Perches06668722013-11-12 15:10:07 -0800465 MODULE_[A-Z_]+|
466 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800467)};
468
Joe Perches20112472011-07-25 17:13:23 -0700469our $signature_tags = qr{(?xi:
470 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800471 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700472 Acked-by:|
473 Tested-by:|
474 Reviewed-by:|
475 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700476 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700477 To:|
478 Cc:
479)};
480
Joe Perches18130872014-08-06 16:11:22 -0700481our @typeListMisordered = (
482 qr{char\s+(?:un)?signed},
483 qr{int\s+(?:(?:un)?signed\s+)?short\s},
484 qr{int\s+short(?:\s+(?:un)?signed)},
485 qr{short\s+int(?:\s+(?:un)?signed)},
486 qr{(?:un)?signed\s+int\s+short},
487 qr{short\s+(?:un)?signed},
488 qr{long\s+int\s+(?:un)?signed},
489 qr{int\s+long\s+(?:un)?signed},
490 qr{long\s+(?:un)?signed\s+int},
491 qr{int\s+(?:un)?signed\s+long},
492 qr{int\s+(?:un)?signed},
493 qr{int\s+long\s+long\s+(?:un)?signed},
494 qr{long\s+long\s+int\s+(?:un)?signed},
495 qr{long\s+long\s+(?:un)?signed\s+int},
496 qr{long\s+long\s+(?:un)?signed},
497 qr{long\s+(?:un)?signed},
498);
499
Andy Whitcroft8905a672007-11-28 16:21:06 -0800500our @typeList = (
501 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700502 qr{(?:(?:un)?signed\s+)?char},
503 qr{(?:(?:un)?signed\s+)?short\s+int},
504 qr{(?:(?:un)?signed\s+)?short},
505 qr{(?:(?:un)?signed\s+)?int},
506 qr{(?:(?:un)?signed\s+)?long\s+int},
507 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
508 qr{(?:(?:un)?signed\s+)?long\s+long},
509 qr{(?:(?:un)?signed\s+)?long},
510 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800511 qr{float},
512 qr{double},
513 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800514 qr{struct\s+$Ident},
515 qr{union\s+$Ident},
516 qr{enum\s+$Ident},
517 qr{${Ident}_t},
518 qr{${Ident}_handler},
519 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700520 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800521);
Joe Perches938224b2016-01-20 14:59:15 -0800522
523our $C90_int_types = qr{(?x:
524 long\s+long\s+int\s+(?:un)?signed|
525 long\s+long\s+(?:un)?signed\s+int|
526 long\s+long\s+(?:un)?signed|
527 (?:(?:un)?signed\s+)?long\s+long\s+int|
528 (?:(?:un)?signed\s+)?long\s+long|
529 int\s+long\s+long\s+(?:un)?signed|
530 int\s+(?:(?:un)?signed\s+)?long\s+long|
531
532 long\s+int\s+(?:un)?signed|
533 long\s+(?:un)?signed\s+int|
534 long\s+(?:un)?signed|
535 (?:(?:un)?signed\s+)?long\s+int|
536 (?:(?:un)?signed\s+)?long|
537 int\s+long\s+(?:un)?signed|
538 int\s+(?:(?:un)?signed\s+)?long|
539
540 int\s+(?:un)?signed|
541 (?:(?:un)?signed\s+)?int
542)};
543
Alex Dowad485ff232015-06-25 15:02:52 -0700544our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700545our @typeListWithAttr = (
546 @typeList,
547 qr{struct\s+$InitAttribute\s+$Ident},
548 qr{union\s+$InitAttribute\s+$Ident},
549);
550
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700551our @modifierList = (
552 qr{fastcall},
553);
Alex Dowad485ff232015-06-25 15:02:52 -0700554our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800555
Joe Perches24358802014-04-03 14:49:13 -0700556our @mode_permission_funcs = (
557 ["module_param", 3],
558 ["module_param_(?:array|named|string)", 4],
559 ["module_param_array_named", 5],
560 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
561 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700562 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
563 ["IIO_DEV_ATTR_[A-Z_]+", 1],
564 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
565 ["SENSOR_TEMPLATE(?:_2|)", 3],
566 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700567);
568
Joe Perches515a2352014-04-03 14:49:24 -0700569#Create a search pattern for all these functions to speed up a loop below
570our $mode_perms_search = "";
571foreach my $entry (@mode_permission_funcs) {
572 $mode_perms_search .= '|' if ($mode_perms_search ne "");
573 $mode_perms_search .= $entry->[0];
574}
Joe Perches00180462018-02-06 15:38:55 -0800575$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700576
Joe Perches9189c7e2018-09-07 15:26:18 -0700577our %deprecated_apis = (
578 "synchronize_rcu_bh" => "synchronize_rcu",
579 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
580 "call_rcu_bh" => "call_rcu",
581 "rcu_barrier_bh" => "rcu_barrier",
582 "synchronize_sched" => "synchronize_rcu",
583 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
584 "call_rcu_sched" => "call_rcu",
585 "rcu_barrier_sched" => "rcu_barrier",
586 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
587 "cond_synchronize_sched" => "cond_synchronize_rcu",
588);
589
590#Create a search pattern for all these strings to speed up a loop below
591our $deprecated_apis_search = "";
592foreach my $entry (keys %deprecated_apis) {
593 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
594 $deprecated_apis_search .= $entry;
595}
596$deprecated_apis_search = "(?:${deprecated_apis_search})";
597
Joe Perchesb392c642015-04-16 12:44:16 -0700598our $mode_perms_world_writable = qr{
599 S_IWUGO |
600 S_IWOTH |
601 S_IRWXUGO |
602 S_IALLUGO |
603 0[0-7][0-7][2367]
604}x;
605
Joe Perchesf90774e2016-10-11 13:51:47 -0700606our %mode_permission_string_types = (
607 "S_IRWXU" => 0700,
608 "S_IRUSR" => 0400,
609 "S_IWUSR" => 0200,
610 "S_IXUSR" => 0100,
611 "S_IRWXG" => 0070,
612 "S_IRGRP" => 0040,
613 "S_IWGRP" => 0020,
614 "S_IXGRP" => 0010,
615 "S_IRWXO" => 0007,
616 "S_IROTH" => 0004,
617 "S_IWOTH" => 0002,
618 "S_IXOTH" => 0001,
619 "S_IRWXUGO" => 0777,
620 "S_IRUGO" => 0444,
621 "S_IWUGO" => 0222,
622 "S_IXUGO" => 0111,
623);
624
625#Create a search pattern for all these strings to speed up a loop below
626our $mode_perms_string_search = "";
627foreach my $entry (keys %mode_permission_string_types) {
628 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
629 $mode_perms_string_search .= $entry;
630}
Joe Perches00180462018-02-06 15:38:55 -0800631our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
632our $multi_mode_perms_string_search = qr{
633 ${single_mode_perms_string_search}
634 (?:\s*\|\s*${single_mode_perms_string_search})*
635}x;
636
637sub perms_to_octal {
638 my ($string) = @_;
639
640 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
641
642 my $val = "";
643 my $oval = "";
644 my $to = 0;
645 my $curpos = 0;
646 my $lastpos = 0;
647 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
648 $curpos = pos($string);
649 my $match = $2;
650 my $omatch = $1;
651 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
652 $lastpos = $curpos;
653 $to |= $mode_permission_string_types{$match};
654 $val .= '\s*\|\s*' if ($val ne "");
655 $val .= $match;
656 $oval .= $omatch;
657 }
658 $oval =~ s/^\s*\|\s*//;
659 $oval =~ s/\s*\|\s*$//;
660 return sprintf("%04o", $to);
661}
Joe Perchesf90774e2016-10-11 13:51:47 -0700662
Wolfram Sang7840a942010-08-09 17:20:57 -0700663our $allowed_asm_includes = qr{(?x:
664 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700665 memory|
666 time|
667 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700668)};
669# memory.h: ARM has a custom one
670
Kees Cook66b47b42014-10-13 15:51:57 -0700671# Load common spelling mistakes and build regular expression list.
672my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700673my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700674
Joe Perches36061e32014-12-10 15:51:43 -0800675if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800676 while (<$spelling>) {
677 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700678
Joe Perches36061e32014-12-10 15:51:43 -0800679 $line =~ s/\s*\n?$//g;
680 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700681
Joe Perches36061e32014-12-10 15:51:43 -0800682 next if ($line =~ m/^\s*#/);
683 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700684
Joe Perches36061e32014-12-10 15:51:43 -0800685 my ($suspect, $fix) = split(/\|\|/, $line);
686
Joe Perches36061e32014-12-10 15:51:43 -0800687 $spelling_fix{$suspect} = $fix;
688 }
689 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800690} else {
691 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700692}
Kees Cook66b47b42014-10-13 15:51:57 -0700693
Joe Perchesebfd7d62015-04-16 12:44:14 -0700694if ($codespell) {
695 if (open(my $spelling, '<', $codespellfile)) {
696 while (<$spelling>) {
697 my $line = $_;
698
699 $line =~ s/\s*\n?$//g;
700 $line =~ s/^\s*//g;
701
702 next if ($line =~ m/^\s*#/);
703 next if ($line =~ m/^\s*$/);
704 next if ($line =~ m/, disabled/i);
705
706 $line =~ s/,.*$//;
707
708 my ($suspect, $fix) = split(/->/, $line);
709
710 $spelling_fix{$suspect} = $fix;
711 }
712 close($spelling);
713 } else {
714 warn "No codespell typos will be found - file '$codespellfile': $!\n";
715 }
716}
717
718$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
719
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700720sub read_words {
721 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700722
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700723 if (open(my $words, '<', $file)) {
724 while (<$words>) {
725 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700726
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700727 $line =~ s/\s*\n?$//g;
728 $line =~ s/^\s*//g;
729
730 next if ($line =~ m/^\s*#/);
731 next if ($line =~ m/^\s*$/);
732 if ($line =~ /\s/) {
733 print("$file: '$line' invalid - ignored\n");
734 next;
735 }
736
737 $$wordsRef .= '|' if ($$wordsRef ne "");
738 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700739 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700740 close($file);
741 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700742 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700743
744 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700745}
746
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700747my $const_structs = "";
748read_words(\$const_structs, $conststructsfile)
749 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
750
751my $typeOtherTypedefs = "";
752if (length($typedefsfile)) {
753 read_words(\$typeOtherTypedefs, $typedefsfile)
754 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
755}
756$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
757
Andy Whitcroft8905a672007-11-28 16:21:06 -0800758sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700759 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
760 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700761 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700762 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700763 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700764 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700765 (?:$typeTypedefs\b)|
766 (?:${all}\b)
767 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800768 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700769 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800770 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800771 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700772 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700773 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800774 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700775 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800776 }x;
Joe Perches18130872014-08-06 16:11:22 -0700777 $NonptrTypeMisordered = qr{
778 (?:$Modifier\s+|const\s+)*
779 (?:
780 (?:${Misordered}\b)
781 )
782 (?:\s+$Modifier|\s+const)*
783 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700784 $NonptrTypeWithAttr = qr{
785 (?:$Modifier\s+|const\s+)*
786 (?:
787 (?:typeof|__typeof__)\s*\([^\)]*\)|
788 (?:$typeTypedefs\b)|
789 (?:${allWithAttr}\b)
790 )
791 (?:\s+$Modifier|\s+const)*
792 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800793 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700794 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700795 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700796 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800797 }x;
Joe Perches18130872014-08-06 16:11:22 -0700798 $TypeMisordered = qr{
799 $NonptrTypeMisordered
800 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
801 (?:\s+$Inline|\s+$Modifier)*
802 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700803 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700804 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800805}
806build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700807
Joe Perches7d2367a2011-07-25 17:13:22 -0700808our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700809
810# Using $balanced_parens, $LvalOrFunc, or $FuncArg
811# requires at least perl version v5.10.0
812# Any use must be runtime checked with $^V
813
814our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700815our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800816our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700817
Joe Perchesf8422302014-08-06 16:11:31 -0700818our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700819 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700820 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700821 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
822 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700823)};
824
Joe Perches7d2367a2011-07-25 17:13:22 -0700825sub deparenthesize {
826 my ($string) = @_;
827 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700828
829 while ($string =~ /^\s*\(.*\)\s*$/) {
830 $string =~ s@^\s*\(\s*@@;
831 $string =~ s@\s*\)\s*$@@;
832 }
833
Joe Perches7d2367a2011-07-25 17:13:22 -0700834 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700835
Joe Perches7d2367a2011-07-25 17:13:22 -0700836 return $string;
837}
838
Joe Perches34456862013-07-03 15:05:34 -0700839sub seed_camelcase_file {
840 my ($file) = @_;
841
842 return if (!(-f $file));
843
844 local $/;
845
846 open(my $include_file, '<', "$file")
847 or warn "$P: Can't read '$file' $!\n";
848 my $text = <$include_file>;
849 close($include_file);
850
851 my @lines = split('\n', $text);
852
853 foreach my $line (@lines) {
854 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
855 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
856 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800857 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
858 $camelcase{$1} = 1;
859 } 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 -0700860 $camelcase{$1} = 1;
861 }
862 }
863}
864
Joe Perches85b0ee12016-10-11 13:51:44 -0700865sub is_maintained_obsolete {
866 my ($filename) = @_;
867
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800868 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700869
Joe Perches0616efa2016-10-11 13:52:02 -0700870 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 -0700871
872 return $status =~ /obsolete/i;
873}
874
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700875sub is_SPDX_License_valid {
876 my ($license) = @_;
877
Joe Perches56294112018-08-21 21:58:04 -0700878 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700879
Joe Perches56294112018-08-21 21:58:04 -0700880 my $root_path = abs_path($root);
881 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700882 return 0 if ($status ne "");
883 return 1;
884}
885
Joe Perches34456862013-07-03 15:05:34 -0700886my $camelcase_seeded = 0;
887sub seed_camelcase_includes {
888 return if ($camelcase_seeded);
889
890 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700891 my $camelcase_cache = "";
892 my @include_files = ();
893
894 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700895
Richard Genoud3645e322014-02-10 14:25:32 -0800896 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700897 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
898 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700899 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700900 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700901 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700902 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700903 @include_files = split('\n', $files);
904 foreach my $file (@include_files) {
905 my $date = POSIX::strftime("%Y%m%d%H%M",
906 localtime((stat $file)[9]));
907 $last_mod_date = $date if ($last_mod_date < $date);
908 }
909 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700910 }
Joe Perchesc707a812013-07-08 16:00:43 -0700911
912 if ($camelcase_cache ne "" && -f $camelcase_cache) {
913 open(my $camelcase_file, '<', "$camelcase_cache")
914 or warn "$P: Can't read '$camelcase_cache' $!\n";
915 while (<$camelcase_file>) {
916 chomp;
917 $camelcase{$_} = 1;
918 }
919 close($camelcase_file);
920
921 return;
922 }
923
Richard Genoud3645e322014-02-10 14:25:32 -0800924 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700925 $files = `git ls-files "include/*.h"`;
926 @include_files = split('\n', $files);
927 }
928
Joe Perches34456862013-07-03 15:05:34 -0700929 foreach my $file (@include_files) {
930 seed_camelcase_file($file);
931 }
Joe Perches351b2a12013-07-03 15:05:36 -0700932
Joe Perchesc707a812013-07-08 16:00:43 -0700933 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700934 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700935 open(my $camelcase_file, '>', "$camelcase_cache")
936 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700937 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
938 print $camelcase_file ("$_\n");
939 }
940 close($camelcase_file);
941 }
Joe Perches34456862013-07-03 15:05:34 -0700942}
943
Joe Perchesd311cd42014-08-06 16:10:57 -0700944sub git_commit_info {
945 my ($commit, $id, $desc) = @_;
946
947 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
948
949 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
950 $output =~ s/^\s*//gm;
951 my @lines = split("\n", $output);
952
Joe Perches0d7835f2015-02-13 14:38:35 -0800953 return ($id, $desc) if ($#lines < 0);
954
Joe Perchesd311cd42014-08-06 16:10:57 -0700955 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
956# Maybe one day convert this block of bash into something that returns
957# all matching commit ids, but it's very slow...
958#
959# echo "checking commits $1..."
960# git rev-list --remotes | grep -i "^$1" |
961# while read line ; do
962# git log --format='%H %s' -1 $line |
963# echo "commit $(cut -c 1-12,41-)"
964# done
965 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700966 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700967 } else {
968 $id = substr($lines[0], 0, 12);
969 $desc = substr($lines[0], 41);
970 }
971
972 return ($id, $desc);
973}
974
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700975$chk_signoff = 0 if ($file);
976
Andy Whitcroft00df3442007-06-08 13:47:06 -0700977my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800978my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700979my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700980my @fixed_inserted = ();
981my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700982my $fixlinenr = -1;
983
Du, Changbin4a593c32016-05-20 17:04:16 -0700984# If input is git commits, extract all commits from the commit expressions.
985# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
986die "$P: No git repository found\n" if ($git && !-e ".git");
987
988if ($git) {
989 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700990 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700991 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700992 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
993 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700994 } elsif ($commit_expr =~ m/\.\./) {
995 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700996 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700997 $git_range = "-1 $commit_expr";
998 }
999 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
1000 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001001 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1002 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001003 my $sha1 = $1;
1004 my $subject = $2;
1005 unshift(@commits, $sha1);
1006 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001007 }
1008 }
1009 die "$P: no git commits after extraction!\n" if (@commits == 0);
1010 @ARGV = @commits;
1011}
1012
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001013my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001014for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001015 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001016 if ($git) {
1017 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1018 die "$P: $filename: git format-patch failed - $!\n";
1019 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001020 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001021 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001022 } elsif ($filename eq '-') {
1023 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001025 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001026 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001027 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001028 if ($filename eq '-') {
1029 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001030 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001031 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001032 } else {
1033 $vname = $filename;
1034 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001035 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001036 chomp;
1037 push(@rawlines, $_);
1038 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001039 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001040
1041 if ($#ARGV > 0 && $quiet == 0) {
1042 print '-' x length($vname) . "\n";
1043 print "$vname\n";
1044 print '-' x length($vname) . "\n";
1045 }
1046
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001047 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001048 $exit = 1;
1049 }
1050 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001051 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001052 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001053 @fixed_inserted = ();
1054 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001055 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001056 @modifierListFile = ();
1057 @typeListFile = ();
1058 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001059}
1060
Joe Perchesd8469f12015-06-25 15:03:00 -07001061if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001062 hash_show_words(\%use_type, "Used");
1063 hash_show_words(\%ignore_type, "Ignored");
1064
Joe Perches5b579802018-08-21 21:57:33 -07001065 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001066 print << "EOM"
1067
1068NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001069 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001070EOM
1071 }
1072 if ($exit) {
1073 print << "EOM"
1074
1075NOTE: If any of the errors are false positives, please report
1076 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1077EOM
1078 }
1079}
1080
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001081exit($exit);
1082
1083sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001084 my ($root) = @_;
1085
1086 my @tree_check = (
1087 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1088 "README", "Documentation", "arch", "include", "drivers",
1089 "fs", "init", "ipc", "kernel", "lib", "scripts",
1090 );
1091
1092 foreach my $check (@tree_check) {
1093 if (! -e $root . '/' . $check) {
1094 return 0;
1095 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001096 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001097 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001098}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001099
Joe Perches20112472011-07-25 17:13:23 -07001100sub parse_email {
1101 my ($formatted_email) = @_;
1102
1103 my $name = "";
1104 my $address = "";
1105 my $comment = "";
1106
1107 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1108 $name = $1;
1109 $address = $2;
1110 $comment = $3 if defined $3;
1111 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1112 $address = $1;
1113 $comment = $2 if defined $2;
1114 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1115 $address = $1;
1116 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001117 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001118 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001119 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001120 $name =~ s/^\"|\"$//g;
1121 # If there's a name left after stripping spaces and
1122 # leading quotes, and the address doesn't have both
1123 # leading and trailing angle brackets, the address
1124 # is invalid. ie:
1125 # "joe smith joe@smith.com" bad
1126 # "joe smith <joe@smith.com" bad
1127 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1128 $name = "";
1129 $address = "";
1130 $comment = "";
1131 }
1132 }
1133
Joe Perches3705ce52013-07-03 15:05:31 -07001134 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001135 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001136 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001137 $address =~ s/^\<|\>$//g;
1138
1139 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1140 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1141 $name = "\"$name\"";
1142 }
1143
1144 return ($name, $address, $comment);
1145}
1146
1147sub format_email {
1148 my ($name, $address) = @_;
1149
1150 my $formatted_email;
1151
Joe Perches3705ce52013-07-03 15:05:31 -07001152 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001153 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001154 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001155
1156 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1157 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1158 $name = "\"$name\"";
1159 }
1160
1161 if ("$name" eq "") {
1162 $formatted_email = "$address";
1163 } else {
1164 $formatted_email = "$name <$address>";
1165 }
1166
1167 return $formatted_email;
1168}
1169
Joe Perchesd311cd42014-08-06 16:10:57 -07001170sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001171 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001172
Joe Perchesbd474ca2014-08-06 16:11:10 -07001173 foreach my $path (split(/:/, $ENV{PATH})) {
1174 if (-e "$path/$bin") {
1175 return "$path/$bin";
1176 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001177 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001178
Joe Perchesbd474ca2014-08-06 16:11:10 -07001179 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001180}
1181
Joe Perches000d1cc12011-07-25 17:13:25 -07001182sub which_conf {
1183 my ($conf) = @_;
1184
1185 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1186 if (-e "$path/$conf") {
1187 return "$path/$conf";
1188 }
1189 }
1190
1191 return "";
1192}
1193
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001194sub expand_tabs {
1195 my ($str) = @_;
1196
1197 my $res = '';
1198 my $n = 0;
1199 for my $c (split(//, $str)) {
1200 if ($c eq "\t") {
1201 $res .= ' ';
1202 $n++;
1203 for (; ($n % 8) != 0; $n++) {
1204 $res .= ' ';
1205 }
1206 next;
1207 }
1208 $res .= $c;
1209 $n++;
1210 }
1211
1212 return $res;
1213}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001214sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001215 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001216 return $res;
1217}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001218
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001219sub line_stats {
1220 my ($line) = @_;
1221
1222 # Drop the diff line leader and expand tabs
1223 $line =~ s/^.//;
1224 $line = expand_tabs($line);
1225
1226 # Pick the indent from the front of the line.
1227 my ($white) = ($line =~ /^(\s*)/);
1228
1229 return (length($line), length($white));
1230}
1231
Andy Whitcroft773647a2008-03-28 14:15:58 -07001232my $sanitise_quote = '';
1233
1234sub sanitise_line_reset {
1235 my ($in_comment) = @_;
1236
1237 if ($in_comment) {
1238 $sanitise_quote = '*/';
1239 } else {
1240 $sanitise_quote = '';
1241 }
1242}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001243sub sanitise_line {
1244 my ($line) = @_;
1245
1246 my $res = '';
1247 my $l = '';
1248
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001249 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001250 my $off = 0;
1251 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001252
Andy Whitcroft773647a2008-03-28 14:15:58 -07001253 # Always copy over the diff marker.
1254 $res = substr($line, 0, 1);
1255
1256 for ($off = 1; $off < length($line); $off++) {
1257 $c = substr($line, $off, 1);
1258
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001259 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001260 # and end, all to $;.
1261 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1262 $sanitise_quote = '*/';
1263
1264 substr($res, $off, 2, "$;$;");
1265 $off++;
1266 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001267 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001268 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001269 $sanitise_quote = '';
1270 substr($res, $off, 2, "$;$;");
1271 $off++;
1272 next;
1273 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001274 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1275 $sanitise_quote = '//';
1276
1277 substr($res, $off, 2, $sanitise_quote);
1278 $off++;
1279 next;
1280 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001281
1282 # A \ in a string means ignore the next character.
1283 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1284 $c eq "\\") {
1285 substr($res, $off, 2, 'XX');
1286 $off++;
1287 next;
1288 }
1289 # Regular quotes.
1290 if ($c eq "'" || $c eq '"') {
1291 if ($sanitise_quote eq '') {
1292 $sanitise_quote = $c;
1293
1294 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001295 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001296 } elsif ($sanitise_quote eq $c) {
1297 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001298 }
1299 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001301 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001302 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1303 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001304 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1305 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1307 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001308 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001309 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001310 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001311 }
1312
Daniel Walker113f04a2009-09-21 17:04:35 -07001313 if ($sanitise_quote eq '//') {
1314 $sanitise_quote = '';
1315 }
1316
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001317 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001318 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001319 my $clean = 'X' x length($1);
1320 $res =~ s@\<.*\>@<$clean>@;
1321
1322 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001323 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001324 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001325 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001326 }
1327
Joe Perchesdadf6802016-08-02 14:04:33 -07001328 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1329 my $match = $1;
1330 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1331 }
1332
Andy Whitcroft00df3442007-06-08 13:47:06 -07001333 return $res;
1334}
1335
Joe Perchesa6962d72013-04-29 16:18:13 -07001336sub get_quoted_string {
1337 my ($line, $rawline) = @_;
1338
Joe Perches478b1792018-04-10 16:33:34 -07001339 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001340 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001341 return substr($rawline, $-[0], $+[0] - $-[0]);
1342}
1343
Andy Whitcroft8905a672007-11-28 16:21:06 -08001344sub ctx_statement_block {
1345 my ($linenr, $remain, $off) = @_;
1346 my $line = $linenr - 1;
1347 my $blk = '';
1348 my $soff = $off;
1349 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001350 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001351
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001352 my $loff = 0;
1353
Andy Whitcroft8905a672007-11-28 16:21:06 -08001354 my $type = '';
1355 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001356 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001357 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001358 my $c;
1359 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001360
1361 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001362 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001363 @stack = (['', 0]) if ($#stack == -1);
1364
Andy Whitcroft773647a2008-03-28 14:15:58 -07001365 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001366 # If we are about to drop off the end, pull in more
1367 # context.
1368 if ($off >= $len) {
1369 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001370 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001371 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001372 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001373 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001374 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001375 $len = length($blk);
1376 $line++;
1377 last;
1378 }
1379 # Bail if there is no further context.
1380 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001381 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001382 last;
1383 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001384 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1385 $level++;
1386 $type = '#';
1387 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001388 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001389 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001390 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001391 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001392
Andy Whitcroft773647a2008-03-28 14:15:58 -07001393 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001394
1395 # Handle nested #if/#else.
1396 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1397 push(@stack, [ $type, $level ]);
1398 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1399 ($type, $level) = @{$stack[$#stack - 1]};
1400 } elsif ($remainder =~ /^#\s*endif\b/) {
1401 ($type, $level) = @{pop(@stack)};
1402 }
1403
Andy Whitcroft8905a672007-11-28 16:21:06 -08001404 # Statement ends at the ';' or a close '}' at the
1405 # outermost level.
1406 if ($level == 0 && $c eq ';') {
1407 last;
1408 }
1409
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001410 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001411 if ($level == 0 && $coff_set == 0 &&
1412 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1413 $remainder =~ /^(else)(?:\s|{)/ &&
1414 $remainder !~ /^else\s+if\b/) {
1415 $coff = $off + length($1) - 1;
1416 $coff_set = 1;
1417 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1418 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001419 }
1420
Andy Whitcroft8905a672007-11-28 16:21:06 -08001421 if (($type eq '' || $type eq '(') && $c eq '(') {
1422 $level++;
1423 $type = '(';
1424 }
1425 if ($type eq '(' && $c eq ')') {
1426 $level--;
1427 $type = ($level != 0)? '(' : '';
1428
1429 if ($level == 0 && $coff < $soff) {
1430 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001431 $coff_set = 1;
1432 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001433 }
1434 }
1435 if (($type eq '' || $type eq '{') && $c eq '{') {
1436 $level++;
1437 $type = '{';
1438 }
1439 if ($type eq '{' && $c eq '}') {
1440 $level--;
1441 $type = ($level != 0)? '{' : '';
1442
1443 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001444 if (substr($blk, $off + 1, 1) eq ';') {
1445 $off++;
1446 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001447 last;
1448 }
1449 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001450 # Preprocessor commands end at the newline unless escaped.
1451 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1452 $level--;
1453 $type = '';
1454 $off++;
1455 last;
1456 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001457 $off++;
1458 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001459 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001460 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001461 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001462 $line++;
1463 $remain--;
1464 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001465
1466 my $statement = substr($blk, $soff, $off - $soff + 1);
1467 my $condition = substr($blk, $soff, $coff - $soff + 1);
1468
1469 #warn "STATEMENT<$statement>\n";
1470 #warn "CONDITION<$condition>\n";
1471
Andy Whitcroft773647a2008-03-28 14:15:58 -07001472 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001473
1474 return ($statement, $condition,
1475 $line, $remain + 1, $off - $loff + 1, $level);
1476}
1477
Andy Whitcroftcf655042008-03-04 14:28:20 -08001478sub statement_lines {
1479 my ($stmt) = @_;
1480
1481 # Strip the diff line prefixes and rip blank lines at start and end.
1482 $stmt =~ s/(^|\n)./$1/g;
1483 $stmt =~ s/^\s*//;
1484 $stmt =~ s/\s*$//;
1485
1486 my @stmt_lines = ($stmt =~ /\n/g);
1487
1488 return $#stmt_lines + 2;
1489}
1490
1491sub statement_rawlines {
1492 my ($stmt) = @_;
1493
1494 my @stmt_lines = ($stmt =~ /\n/g);
1495
1496 return $#stmt_lines + 2;
1497}
1498
1499sub statement_block_size {
1500 my ($stmt) = @_;
1501
1502 $stmt =~ s/(^|\n)./$1/g;
1503 $stmt =~ s/^\s*{//;
1504 $stmt =~ s/}\s*$//;
1505 $stmt =~ s/^\s*//;
1506 $stmt =~ s/\s*$//;
1507
1508 my @stmt_lines = ($stmt =~ /\n/g);
1509 my @stmt_statements = ($stmt =~ /;/g);
1510
1511 my $stmt_lines = $#stmt_lines + 2;
1512 my $stmt_statements = $#stmt_statements + 1;
1513
1514 if ($stmt_lines > $stmt_statements) {
1515 return $stmt_lines;
1516 } else {
1517 return $stmt_statements;
1518 }
1519}
1520
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001521sub ctx_statement_full {
1522 my ($linenr, $remain, $off) = @_;
1523 my ($statement, $condition, $level);
1524
1525 my (@chunks);
1526
Andy Whitcroftcf655042008-03-04 14:28:20 -08001527 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001528 ($statement, $condition, $linenr, $remain, $off, $level) =
1529 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001530 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001531 push(@chunks, [ $condition, $statement ]);
1532 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1533 return ($level, $linenr, @chunks);
1534 }
1535
1536 # Pull in the following conditional/block pairs and see if they
1537 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001538 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001539 ($statement, $condition, $linenr, $remain, $off, $level) =
1540 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001541 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001542 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001543 #print "C: push\n";
1544 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001545 }
1546
1547 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001548}
1549
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001550sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001551 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001552 my $line;
1553 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001554 my $blk = '';
1555 my @o;
1556 my @c;
1557 my @res = ();
1558
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001559 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001560 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001561 for ($line = $start; $remain > 0; $line++) {
1562 next if ($rawlines[$line] =~ /^-/);
1563 $remain--;
1564
1565 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001566
1567 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001568 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001569 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001570 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001571 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001572 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001573 $level = pop(@stack);
1574 }
1575
Andy Whitcroft01464f32010-10-26 14:23:19 -07001576 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001577 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1578 if ($off > 0) {
1579 $off--;
1580 next;
1581 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001582
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001583 if ($c eq $close && $level > 0) {
1584 $level--;
1585 last if ($level == 0);
1586 } elsif ($c eq $open) {
1587 $level++;
1588 }
1589 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001590
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001591 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001592 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001593 }
1594
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001595 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001596 }
1597
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001598 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001599}
1600sub ctx_block_outer {
1601 my ($linenr, $remain) = @_;
1602
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001603 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1604 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001605}
1606sub ctx_block {
1607 my ($linenr, $remain) = @_;
1608
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001609 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1610 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001611}
1612sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001613 my ($linenr, $remain, $off) = @_;
1614
1615 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1616 return @r;
1617}
1618sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001619 my ($linenr, $remain) = @_;
1620
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001621 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001622}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001623sub ctx_statement_level {
1624 my ($linenr, $remain, $off) = @_;
1625
1626 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1627}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001628
1629sub ctx_locate_comment {
1630 my ($first_line, $end_line) = @_;
1631
1632 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001633 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001634 return $current_comment if (defined $current_comment);
1635
1636 # Look through the context and try and figure out if there is a
1637 # comment.
1638 my $in_comment = 0;
1639 $current_comment = '';
1640 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001641 my $line = $rawlines[$linenr - 1];
1642 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001643 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1644 $in_comment = 1;
1645 }
1646 if ($line =~ m@/\*@) {
1647 $in_comment = 1;
1648 }
1649 if (!$in_comment && $current_comment ne '') {
1650 $current_comment = '';
1651 }
1652 $current_comment .= $line . "\n" if ($in_comment);
1653 if ($line =~ m@\*/@) {
1654 $in_comment = 0;
1655 }
1656 }
1657
1658 chomp($current_comment);
1659 return($current_comment);
1660}
1661sub ctx_has_comment {
1662 my ($first_line, $end_line) = @_;
1663 my $cmt = ctx_locate_comment($first_line, $end_line);
1664
Andy Whitcroft00df3442007-06-08 13:47:06 -07001665 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001666 ##print "CMMT: $cmt\n";
1667
1668 return ($cmt ne '');
1669}
1670
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001671sub raw_line {
1672 my ($linenr, $cnt) = @_;
1673
1674 my $offset = $linenr - 1;
1675 $cnt++;
1676
1677 my $line;
1678 while ($cnt) {
1679 $line = $rawlines[$offset++];
1680 next if (defined($line) && $line =~ /^-/);
1681 $cnt--;
1682 }
1683
1684 return $line;
1685}
1686
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001687sub get_stat_real {
1688 my ($linenr, $lc) = @_;
1689
1690 my $stat_real = raw_line($linenr, 0);
1691 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1692 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1693 }
1694
1695 return $stat_real;
1696}
1697
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001698sub get_stat_here {
1699 my ($linenr, $cnt, $here) = @_;
1700
1701 my $herectx = $here . "\n";
1702 for (my $n = 0; $n < $cnt; $n++) {
1703 $herectx .= raw_line($linenr, $n) . "\n";
1704 }
1705
1706 return $herectx;
1707}
1708
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001709sub cat_vet {
1710 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001711 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001712
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001713 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001714 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1715 $res .= $1;
1716 if ($2 ne '') {
1717 $coded = sprintf("^%c", unpack('C', $2) + 64);
1718 $res .= $coded;
1719 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001720 }
1721 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001722
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001723 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001724}
1725
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001726my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001727my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001728my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001729my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001730
1731sub annotate_reset {
1732 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001733 $av_pending = '_';
1734 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001735 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001736}
1737
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001738sub annotate_values {
1739 my ($stream, $type) = @_;
1740
1741 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001742 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001743 my $cur = $stream;
1744
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001745 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001746
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001747 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001748 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001749 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001750 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001751 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001752 print "WS($1)\n" if ($dbg_values > 1);
1753 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001754 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001755 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001756 }
1757
Florian Micklerc023e4732011-01-12 16:59:58 -08001758 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001759 print "CAST($1)\n" if ($dbg_values > 1);
1760 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001761 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001762
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001763 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001764 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001765 $type = 'T';
1766
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001767 } elsif ($cur =~ /^($Modifier)\s*/) {
1768 print "MODIFIER($1)\n" if ($dbg_values > 1);
1769 $type = 'T';
1770
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001771 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001772 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001773 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001774 push(@av_paren_type, $type);
1775 if ($2 ne '') {
1776 $av_pending = 'N';
1777 }
1778 $type = 'E';
1779
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001780 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001781 print "UNDEF($1)\n" if ($dbg_values > 1);
1782 $av_preprocessor = 1;
1783 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001784
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001785 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001786 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001787 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001788
1789 push(@av_paren_type, $type);
1790 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001791 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001792
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001793 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001794 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1795 $av_preprocessor = 1;
1796
1797 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1798
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001799 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001800
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001801 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001802 print "PRE_END($1)\n" if ($dbg_values > 1);
1803
1804 $av_preprocessor = 1;
1805
1806 # Assume all arms of the conditional end as this
1807 # one does, and continue as if the #endif was not here.
1808 pop(@av_paren_type);
1809 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001810 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001811
1812 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001813 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001814
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001815 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1816 print "ATTR($1)\n" if ($dbg_values > 1);
1817 $av_pending = $type;
1818 $type = 'N';
1819
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001820 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001821 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001822 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001823 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001824 }
1825 $type = 'N';
1826
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001827 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001828 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001829 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001830 $type = 'N';
1831
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001832 } elsif ($cur =~/^(case)/o) {
1833 print "CASE($1)\n" if ($dbg_values > 1);
1834 $av_pend_colon = 'C';
1835 $type = 'N';
1836
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001837 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001838 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001839 $type = 'N';
1840
1841 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001842 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001843 push(@av_paren_type, $av_pending);
1844 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001845 $type = 'N';
1846
1847 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001848 my $new_type = pop(@av_paren_type);
1849 if ($new_type ne '_') {
1850 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001851 print "PAREN('$1') -> $type\n"
1852 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001853 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001854 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001855 }
1856
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001857 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001858 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001859 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001860 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001861
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001862 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1863 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001864 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001865 } elsif ($type eq 'E') {
1866 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001867 }
1868 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1869 $type = 'V';
1870
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001871 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001872 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001873 $type = 'V';
1874
1875 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001876 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001877 $type = 'N';
1878
Andy Whitcroftcf655042008-03-04 14:28:20 -08001879 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001880 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001881 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001882 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001883
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001884 } elsif ($cur =~/^(,)/) {
1885 print "COMMA($1)\n" if ($dbg_values > 1);
1886 $type = 'C';
1887
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001888 } elsif ($cur =~ /^(\?)/o) {
1889 print "QUESTION($1)\n" if ($dbg_values > 1);
1890 $type = 'N';
1891
1892 } elsif ($cur =~ /^(:)/o) {
1893 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1894
1895 substr($var, length($res), 1, $av_pend_colon);
1896 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1897 $type = 'E';
1898 } else {
1899 $type = 'N';
1900 }
1901 $av_pend_colon = 'O';
1902
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001903 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001904 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001905 $type = 'N';
1906
Andy Whitcroft0d413862008-10-15 22:02:16 -07001907 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001908 my $variant;
1909
1910 print "OPV($1)\n" if ($dbg_values > 1);
1911 if ($type eq 'V') {
1912 $variant = 'B';
1913 } else {
1914 $variant = 'U';
1915 }
1916
1917 substr($var, length($res), 1, $variant);
1918 $type = 'N';
1919
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001920 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001921 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001922 if ($1 ne '++' && $1 ne '--') {
1923 $type = 'N';
1924 }
1925
1926 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001927 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001928 }
1929 if (defined $1) {
1930 $cur = substr($cur, length($1));
1931 $res .= $type x length($1);
1932 }
1933 }
1934
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001935 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001936}
1937
Andy Whitcroft8905a672007-11-28 16:21:06 -08001938sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001939 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001940 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001941 ^(?:
1942 $Modifier|
1943 $Storage|
1944 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001945 DEFINE_\S+
1946 )$|
1947 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001948 goto|
1949 return|
1950 case|
1951 else|
1952 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001953 do|
1954 \#|
1955 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001956 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001957 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001958 )}x;
1959 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1960 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001961 # Check for modifiers.
1962 $possible =~ s/\s*$Storage\s*//g;
1963 $possible =~ s/\s*$Sparse\s*//g;
1964 if ($possible =~ /^\s*$/) {
1965
1966 } elsif ($possible =~ /\s/) {
1967 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001968 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001969 if ($modifier !~ $notPermitted) {
1970 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001971 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001972 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001973 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001974
1975 } else {
1976 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001977 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001978 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001979 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001980 } else {
1981 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001982 }
1983}
1984
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001985my $prefix = '';
1986
Joe Perches000d1cc12011-07-25 17:13:25 -07001987sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001988 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001989
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001990 $type =~ tr/[a-z]/[A-Z]/;
1991
Joe Perchescbec18a2014-04-03 14:49:19 -07001992 return defined $use_type{$type} if (scalar keys %use_type > 0);
1993
1994 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001995}
1996
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001997sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001998 my ($level, $type, $msg) = @_;
1999
2000 if (!show_type($type) ||
2001 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002002 return 0;
2003 }
Joe Perches57230292015-06-25 15:03:03 -07002004 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002005 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002006 if ($level eq 'ERROR') {
2007 $output .= RED;
2008 } elsif ($level eq 'WARNING') {
2009 $output .= YELLOW;
2010 } else {
2011 $output .= GREEN;
2012 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002013 }
Joe Perches57230292015-06-25 15:03:03 -07002014 $output .= $prefix . $level . ':';
2015 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002016 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002017 $output .= "$type:";
2018 }
John Brooks737c0762017-07-10 15:52:24 -07002019 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002020 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002021
2022 if ($showfile) {
2023 my @lines = split("\n", $output, -1);
2024 splice(@lines, 1, 1);
2025 $output = join("\n", @lines);
2026 }
Joe Perches57230292015-06-25 15:03:03 -07002027 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002028
Joe Perches57230292015-06-25 15:03:03 -07002029 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002030
2031 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002032}
Joe Perchescbec18a2014-04-03 14:49:19 -07002033
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002034sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002035 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002036}
Joe Perches000d1cc12011-07-25 17:13:25 -07002037
Joe Perchesd752fcc2014-08-06 16:11:05 -07002038sub fixup_current_range {
2039 my ($lineRef, $offset, $length) = @_;
2040
2041 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2042 my $o = $1;
2043 my $l = $2;
2044 my $no = $o + $offset;
2045 my $nl = $l + $length;
2046 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2047 }
2048}
2049
2050sub fix_inserted_deleted_lines {
2051 my ($linesRef, $insertedRef, $deletedRef) = @_;
2052
2053 my $range_last_linenr = 0;
2054 my $delta_offset = 0;
2055
2056 my $old_linenr = 0;
2057 my $new_linenr = 0;
2058
2059 my $next_insert = 0;
2060 my $next_delete = 0;
2061
2062 my @lines = ();
2063
2064 my $inserted = @{$insertedRef}[$next_insert++];
2065 my $deleted = @{$deletedRef}[$next_delete++];
2066
2067 foreach my $old_line (@{$linesRef}) {
2068 my $save_line = 1;
2069 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002070 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002071 $delta_offset = 0;
2072 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2073 $range_last_linenr = $new_linenr;
2074 fixup_current_range(\$line, $delta_offset, 0);
2075 }
2076
2077 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2078 $deleted = @{$deletedRef}[$next_delete++];
2079 $save_line = 0;
2080 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2081 }
2082
2083 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2084 push(@lines, ${$inserted}{'LINE'});
2085 $inserted = @{$insertedRef}[$next_insert++];
2086 $new_linenr++;
2087 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2088 }
2089
2090 if ($save_line) {
2091 push(@lines, $line);
2092 $new_linenr++;
2093 }
2094
2095 $old_linenr++;
2096 }
2097
2098 return @lines;
2099}
2100
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002101sub fix_insert_line {
2102 my ($linenr, $line) = @_;
2103
2104 my $inserted = {
2105 LINENR => $linenr,
2106 LINE => $line,
2107 };
2108 push(@fixed_inserted, $inserted);
2109}
2110
2111sub fix_delete_line {
2112 my ($linenr, $line) = @_;
2113
2114 my $deleted = {
2115 LINENR => $linenr,
2116 LINE => $line,
2117 };
2118
2119 push(@fixed_deleted, $deleted);
2120}
2121
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002122sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002123 my ($type, $msg) = @_;
2124
2125 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002126 our $clean = 0;
2127 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002128 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002129 }
Joe Perches3705ce52013-07-03 15:05:31 -07002130 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002131}
2132sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002133 my ($type, $msg) = @_;
2134
2135 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002136 our $clean = 0;
2137 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002138 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002139 }
Joe Perches3705ce52013-07-03 15:05:31 -07002140 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002141}
2142sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002143 my ($type, $msg) = @_;
2144
2145 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002146 our $clean = 0;
2147 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002148 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002149 }
Joe Perches3705ce52013-07-03 15:05:31 -07002150 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002151}
2152
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002153sub check_absolute_file {
2154 my ($absolute, $herecurr) = @_;
2155 my $file = $absolute;
2156
2157 ##print "absolute<$absolute>\n";
2158
2159 # See if any suffix of this path is a path within the tree.
2160 while ($file =~ s@^[^/]*/@@) {
2161 if (-f "$root/$file") {
2162 ##print "file<$file>\n";
2163 last;
2164 }
2165 }
2166 if (! -f _) {
2167 return 0;
2168 }
2169
2170 # It is, so see if the prefix is acceptable.
2171 my $prefix = $absolute;
2172 substr($prefix, -length($file)) = '';
2173
2174 ##print "prefix<$prefix>\n";
2175 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002176 WARN("USE_RELATIVE_PATH",
2177 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002178 }
2179}
2180
Joe Perches3705ce52013-07-03 15:05:31 -07002181sub trim {
2182 my ($string) = @_;
2183
Joe Perchesb34c6482013-09-11 14:24:01 -07002184 $string =~ s/^\s+|\s+$//g;
2185
2186 return $string;
2187}
2188
2189sub ltrim {
2190 my ($string) = @_;
2191
2192 $string =~ s/^\s+//;
2193
2194 return $string;
2195}
2196
2197sub rtrim {
2198 my ($string) = @_;
2199
2200 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002201
2202 return $string;
2203}
2204
Joe Perches52ea8502013-11-12 15:10:09 -08002205sub string_find_replace {
2206 my ($string, $find, $replace) = @_;
2207
2208 $string =~ s/$find/$replace/g;
2209
2210 return $string;
2211}
2212
Joe Perches3705ce52013-07-03 15:05:31 -07002213sub tabify {
2214 my ($leading) = @_;
2215
2216 my $source_indent = 8;
2217 my $max_spaces_before_tab = $source_indent - 1;
2218 my $spaces_to_tab = " " x $source_indent;
2219
2220 #convert leading spaces to tabs
2221 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2222 #Remove spaces before a tab
2223 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2224
2225 return "$leading";
2226}
2227
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002228sub pos_last_openparen {
2229 my ($line) = @_;
2230
2231 my $pos = 0;
2232
2233 my $opens = $line =~ tr/\(/\(/;
2234 my $closes = $line =~ tr/\)/\)/;
2235
2236 my $last_openparen = 0;
2237
2238 if (($opens == 0) || ($closes >= $opens)) {
2239 return -1;
2240 }
2241
2242 my $len = length($line);
2243
2244 for ($pos = 0; $pos < $len; $pos++) {
2245 my $string = substr($line, $pos);
2246 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2247 $pos += length($1) - 1;
2248 } elsif (substr($line, $pos, 1) eq '(') {
2249 $last_openparen = $pos;
2250 } elsif (index($string, '(') == -1) {
2251 last;
2252 }
2253 }
2254
Joe Perches91cb5192014-04-03 14:49:32 -07002255 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002256}
2257
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002258sub process {
2259 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002260
2261 my $linenr=0;
2262 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002263 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002264 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002265 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002266
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002267 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002268 my $indent;
2269 my $previndent=0;
2270 my $stashindent=0;
2271
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002272 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002273 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002274 my $author = '';
2275 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002276 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002277 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002278 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002279 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002280 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002281 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002282 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002283 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002284 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002285 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002286 my $non_utf8_charset = 0;
2287
Joe Perches365dd4e2014-08-06 16:10:42 -07002288 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002289 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002290
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002291 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002292 our $cnt_lines = 0;
2293 our $cnt_error = 0;
2294 our $cnt_warn = 0;
2295 our $cnt_chk = 0;
2296
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002297 # Trace the real file/line as we go.
2298 my $realfile = '';
2299 my $realline = 0;
2300 my $realcnt = 0;
2301 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002302 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002303 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002304 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002305 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002306 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002307
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002308 my $prev_values = 'E';
2309
2310 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002311 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002312 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002313 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002314 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002315
Joe Perches7e51f192013-09-11 14:23:57 -07002316 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002317
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002318 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002319 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002320 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002321 my @setup_docs = ();
2322 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002323
Joe Perchesd8b07712013-11-12 15:10:06 -08002324 my $camelcase_file_seeded = 0;
2325
Rob Herring9f3a8992018-04-10 16:33:13 -07002326 my $checklicenseline = 1;
2327
Andy Whitcroft773647a2008-03-28 14:15:58 -07002328 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002329 my $line;
2330 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002331 $linenr++;
2332 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002333
Joe Perches3705ce52013-07-03 15:05:31 -07002334 push(@fixed, $rawline) if ($fix);
2335
Andy Whitcroft773647a2008-03-28 14:15:58 -07002336 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002337 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002338 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002339 $setup_docs = 1;
2340 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002341 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002342 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002343 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002344 $realline=$1-1;
2345 if (defined $2) {
2346 $realcnt=$3+1;
2347 } else {
2348 $realcnt=1+1;
2349 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002350 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002351
2352 # Guestimate if this is a continuing comment. Run
2353 # the context looking for a comment "edge". If this
2354 # edge is a close comment then we must be in a comment
2355 # at context start.
2356 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002357 my $cnt = $realcnt;
2358 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2359 next if (defined $rawlines[$ln - 1] &&
2360 $rawlines[$ln - 1] =~ /^-/);
2361 $cnt--;
2362 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002363 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002364 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2365 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2366 ($edge) = $1;
2367 last;
2368 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002369 }
2370 if (defined $edge && $edge eq '*/') {
2371 $in_comment = 1;
2372 }
2373
2374 # Guestimate if this is a continuing comment. If this
2375 # is the start of a diff block and this line starts
2376 # ' *' then it is very likely a comment.
2377 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002378 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002379 {
2380 $in_comment = 1;
2381 }
2382
2383 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2384 sanitise_line_reset($in_comment);
2385
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002386 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002387 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002388 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002389 $line = sanitise_line($rawline);
2390 }
2391 push(@lines, $line);
2392
2393 if ($realcnt > 1) {
2394 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2395 } else {
2396 $realcnt = 0;
2397 }
2398
2399 #print "==>$rawline\n";
2400 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002401
2402 if ($setup_docs && $line =~ /^\+/) {
2403 push(@setup_docs, $line);
2404 }
2405 }
2406
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002407 $prefix = '';
2408
Andy Whitcroft773647a2008-03-28 14:15:58 -07002409 $realcnt = 0;
2410 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002411 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002412 foreach my $line (@lines) {
2413 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002414 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002415 my $sline = $line; #copy of $line
2416 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002417
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002418 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002419
Joe Perches12c253a2018-06-07 17:10:58 -07002420# check if it's a mode change, rename or start of a patch
2421 if (!$in_commit_log &&
2422 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2423 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2424 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2425 $is_patch = 1;
2426 }
2427
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002428#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002429 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002430 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2431 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002432 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002433 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002434 $realline=$1-1;
2435 if (defined $2) {
2436 $realcnt=$3+1;
2437 } else {
2438 $realcnt=1+1;
2439 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002440 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002441 $prev_values = 'E';
2442
Andy Whitcroft773647a2008-03-28 14:15:58 -07002443 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002444 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002445 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002446 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002447 if ($context =~ /\b(\w+)\s*\(/) {
2448 $context_function = $1;
2449 } else {
2450 undef $context_function;
2451 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002452 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002453
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002454# track the line number as we move through the hunk, note that
2455# new versions of GNU diff omit the leading space on completely
2456# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002457 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002458 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002459 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002460
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002461 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002462 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002463
2464 # Track the previous line.
2465 ($prevline, $stashline) = ($stashline, $line);
2466 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002467 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2468
Andy Whitcroft773647a2008-03-28 14:15:58 -07002469 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002470
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002471 } elsif ($realcnt == 1) {
2472 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002473 }
2474
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002475 my $hunk_line = ($realcnt != 0);
2476
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002477 $here = "#$linenr: " if (!$file);
2478 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002479
Joe Perches2ac73b42014-06-04 16:12:05 -07002480 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002481 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002482 if ($line =~ /^diff --git.*?(\S+)$/) {
2483 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002484 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002485 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002486 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002487 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002488 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002489 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002490 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002491
2492 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002493 if (!$file && $tree && $p1_prefix ne '' &&
2494 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002495 WARN("PATCH_PREFIX",
2496 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002497 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002498
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002499 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002500 ERROR("MODIFIED_INCLUDE_ASM",
2501 "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 -07002502 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002503 $found_file = 1;
2504 }
2505
Joe Perches34d88152015-06-25 15:03:05 -07002506#make up the handle for any error we report on this line
2507 if ($showfile) {
2508 $prefix = "$realfile:$realline: "
2509 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002510 if ($file) {
2511 $prefix = "$filename:$realline: ";
2512 } else {
2513 $prefix = "$filename:$linenr: ";
2514 }
Joe Perches34d88152015-06-25 15:03:05 -07002515 }
2516
Joe Perches2ac73b42014-06-04 16:12:05 -07002517 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002518 if (is_maintained_obsolete($realfile)) {
2519 WARN("OBSOLETE",
2520 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2521 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002522 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002523 $check = 1;
2524 } else {
2525 $check = $check_orig;
2526 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002527 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002528
2529 if ($realfile !~ /^MAINTAINERS/) {
2530 my $last_binding_patch = $is_binding_patch;
2531
2532 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2533
2534 if (($last_binding_patch != -1) &&
2535 ($last_binding_patch ^ $is_binding_patch)) {
2536 WARN("DT_SPLIT_BINDING_PATCH",
2537 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2538 }
2539 }
2540
Andy Whitcroft773647a2008-03-28 14:15:58 -07002541 next;
2542 }
2543
Randy Dunlap389834b2007-06-08 13:47:03 -07002544 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002545
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002546 my $hereline = "$here\n$rawline\n";
2547 my $herecurr = "$here\n$rawline\n";
2548 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002549
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002550 $cnt_lines++ if ($realcnt != 0);
2551
Joe Perches490b2922018-08-21 21:58:01 -07002552# Verify the existence of a commit log if appropriate
2553# 2 is used because a $signature is counted in $commit_log_lines
2554 if ($in_commit_log) {
2555 if ($line !~ /^\s*$/) {
2556 $commit_log_lines++; #could be a $signature
2557 }
2558 } elsif ($has_commit_log && $commit_log_lines < 2) {
2559 WARN("COMMIT_MESSAGE",
2560 "Missing commit description - Add an appropriate one\n");
2561 $commit_log_lines = 2; #warn only once
2562 }
2563
Joe Perchese518e9a2015-06-25 15:03:27 -07002564# Check if the commit log has what seems like a diff which can confuse patch
2565 if ($in_commit_log && !$commit_log_has_diff &&
2566 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2567 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2568 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2569 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2570 ERROR("DIFF_IN_COMMIT_MSG",
2571 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2572 $commit_log_has_diff = 1;
2573 }
2574
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002575# Check for incorrect file permissions
2576 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2577 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002578 if ($realfile !~ m@scripts/@ &&
2579 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002580 ERROR("EXECUTE_PERMISSIONS",
2581 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002582 }
2583 }
2584
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002585# Check the patch for a From:
2586 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2587 $author = $1;
2588 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2589 $author =~ s/"//g;
2590 }
2591
Joe Perches20112472011-07-25 17:13:23 -07002592# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002593 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002594 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002595 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002596 if ($author ne '') {
2597 my $l = $line;
2598 $l =~ s/"//g;
2599 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2600 $authorsignoff = 1;
2601 }
2602 }
Joe Perches20112472011-07-25 17:13:23 -07002603 }
2604
Joe Perchese0d975b2014-12-10 15:51:49 -08002605# Check if MAINTAINERS is being updated. If so, there's probably no need to
2606# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2607 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2608 $reported_maintainer_file = 1;
2609 }
2610
Joe Perches20112472011-07-25 17:13:23 -07002611# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002612 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002613 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002614 my $space_before = $1;
2615 my $sign_off = $2;
2616 my $space_after = $3;
2617 my $email = $4;
2618 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2619
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002620 if ($sign_off !~ /$signature_tags/) {
2621 WARN("BAD_SIGN_OFF",
2622 "Non-standard signature: $sign_off\n" . $herecurr);
2623 }
Joe Perches20112472011-07-25 17:13:23 -07002624 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002625 if (WARN("BAD_SIGN_OFF",
2626 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2627 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002628 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002629 "$ucfirst_sign_off $email";
2630 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002631 }
Joe Perches20112472011-07-25 17:13:23 -07002632 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002633 if (WARN("BAD_SIGN_OFF",
2634 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2635 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002636 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002637 "$ucfirst_sign_off $email";
2638 }
2639
Joe Perches20112472011-07-25 17:13:23 -07002640 }
2641 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002642 if (WARN("BAD_SIGN_OFF",
2643 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2644 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002645 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002646 "$ucfirst_sign_off $email";
2647 }
Joe Perches20112472011-07-25 17:13:23 -07002648 }
2649
2650 my ($email_name, $email_address, $comment) = parse_email($email);
2651 my $suggested_email = format_email(($email_name, $email_address));
2652 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002653 ERROR("BAD_SIGN_OFF",
2654 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002655 } else {
2656 my $dequoted = $suggested_email;
2657 $dequoted =~ s/^"//;
2658 $dequoted =~ s/" </ </;
2659 # Don't force email to have quotes
2660 # Allow just an angle bracketed address
2661 if ("$dequoted$comment" ne $email &&
2662 "<$email_address>$comment" ne $email &&
2663 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002664 WARN("BAD_SIGN_OFF",
2665 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002666 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002667 }
Joe Perches7e51f192013-09-11 14:23:57 -07002668
2669# Check for duplicate signatures
2670 my $sig_nospace = $line;
2671 $sig_nospace =~ s/\s//g;
2672 $sig_nospace = lc($sig_nospace);
2673 if (defined $signatures{$sig_nospace}) {
2674 WARN("BAD_SIGN_OFF",
2675 "Duplicate signature\n" . $herecurr);
2676 } else {
2677 $signatures{$sig_nospace} = 1;
2678 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002679 }
2680
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002681# Check email subject for common tools that don't need to be mentioned
2682 if ($in_header_lines &&
2683 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2684 WARN("EMAIL_SUBJECT",
2685 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2686 }
2687
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002688# Check for unwanted Gerrit info
2689 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2690 ERROR("GERRIT_CHANGE_ID",
2691 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2692 }
2693
Joe Perches369c8dd2015-11-06 16:31:34 -08002694# Check if the commit log is in a possible stack dump
2695 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2696 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2697 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2698 # timestamp
2699 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2700 # stack dump address
2701 $commit_log_possible_stack_dump = 1;
2702 }
2703
Joe Perches2a076f42015-04-16 12:44:28 -07002704# Check for line lengths > 75 in commit log, warn once
2705 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002706 length($line) > 75 &&
2707 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2708 # file delta changes
2709 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2710 # filename then :
2711 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2712 # A Fixes: or Link: line
2713 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002714 WARN("COMMIT_LOG_LONG_LINE",
2715 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2716 $commit_log_long_line = 1;
2717 }
2718
Joe Perchesbf4daf12015-09-09 15:37:50 -07002719# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002720 if ($in_commit_log && $commit_log_possible_stack_dump &&
2721 $line =~ /^\s*$/) {
2722 $commit_log_possible_stack_dump = 0;
2723 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002724
Joe Perches0d7835f2015-02-13 14:38:35 -08002725# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002726 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002727 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002728 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002729 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002730 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002731 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2732 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002733 my $init_char = "c";
2734 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002735 my $short = 1;
2736 my $long = 0;
2737 my $case = 1;
2738 my $space = 1;
2739 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002740 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002741 my $id = '0123456789ab';
2742 my $orig_desc = "commit description";
2743 my $description = "";
2744
Joe Perchesfe043ea2015-09-09 15:37:25 -07002745 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2746 $init_char = $1;
2747 $orig_commit = lc($2);
2748 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2749 $orig_commit = lc($1);
2750 }
2751
Joe Perches0d7835f2015-02-13 14:38:35 -08002752 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2753 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2754 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2755 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2756 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2757 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002758 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002759 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2760 defined $rawlines[$linenr] &&
2761 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2762 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002763 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002764 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2765 defined $rawlines[$linenr] &&
2766 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2767 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2768 $orig_desc = $1;
2769 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2770 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002771 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002772 }
2773
2774 ($id, $description) = git_commit_info($orig_commit,
2775 $id, $orig_desc);
2776
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002777 if (defined($id) &&
2778 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002779 ERROR("GIT_COMMIT_ID",
2780 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2781 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002782 }
2783
Joe Perches13f19372014-08-06 16:10:59 -07002784# Check for added, moved or deleted files
2785 if (!$reported_maintainer_file && !$in_commit_log &&
2786 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2787 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2788 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2789 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002790 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002791 $reported_maintainer_file = 1;
2792 WARN("FILE_PATH_CHANGES",
2793 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2794 }
2795
Andy Whitcroft00df3442007-06-08 13:47:06 -07002796# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002797 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002798 ERROR("CORRUPTED_PATCH",
2799 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002800 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002801 }
2802
2803# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2804 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002805 $rawline !~ m/^$UTF8*$/) {
2806 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2807
2808 my $blank = copy_spacing($rawline);
2809 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2810 my $hereptr = "$hereline$ptr\n";
2811
Joe Perches34d99212011-07-25 17:13:26 -07002812 CHK("INVALID_UTF8",
2813 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002814 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002815
Joe Perches15662b32011-10-31 17:13:12 -07002816# Check if it's the start of a commit log
2817# (not a header line and we haven't seen the patch filename)
2818 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002819 !($rawline =~ /^\s+(?:\S|$)/ ||
2820 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002821 $in_header_lines = 0;
2822 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002823 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002824 }
2825
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002826# Check if there is UTF-8 in a commit log when a mail header has explicitly
2827# declined it, i.e defined some charset where it is missing.
2828 if ($in_header_lines &&
2829 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2830 $1 !~ /utf-8/i) {
2831 $non_utf8_charset = 1;
2832 }
2833
2834 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002835 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002836 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002837 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2838 }
2839
Joe Perchesd6430f72016-12-12 16:46:28 -08002840# Check for absolute kernel paths in commit message
2841 if ($tree && $in_commit_log) {
2842 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2843 my $file = $1;
2844
2845 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2846 check_absolute_file($1, $herecurr)) {
2847 #
2848 } else {
2849 check_absolute_file($file, $herecurr);
2850 }
2851 }
2852 }
2853
Kees Cook66b47b42014-10-13 15:51:57 -07002854# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002855 if (defined($misspellings) &&
2856 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002857 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002858 my $typo = $1;
2859 my $typo_fix = $spelling_fix{lc($typo)};
2860 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2861 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002862 my $msg_level = \&WARN;
2863 $msg_level = \&CHK if ($file);
2864 if (&{$msg_level}("TYPO_SPELLING",
2865 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002866 $fix) {
2867 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2868 }
2869 }
2870 }
2871
Andy Whitcroft306708542008-10-15 22:02:28 -07002872# ignore non-hunk lines and lines being removed
2873 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002874
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002875#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002876 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002877 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002878 if (ERROR("DOS_LINE_ENDINGS",
2879 "DOS line endings\n" . $herevet) &&
2880 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002881 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002882 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002883 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2884 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002885 if (ERROR("TRAILING_WHITESPACE",
2886 "trailing whitespace\n" . $herevet) &&
2887 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002888 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002889 }
2890
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002891 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002892 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002893
Josh Triplett4783f892013-11-12 15:10:12 -08002894# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002895 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002896 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002897 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2898 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002899 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002900 my $msg_level = \&ERROR;
2901 $msg_level = \&CHK if ($file);
2902 &{$msg_level}("FSF_MAILING_ADDRESS",
2903 "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 -08002904 }
2905
Andi Kleen33549572010-05-24 14:33:29 -07002906# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002907# Only applies when adding the entry originally, after that we do not have
2908# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002909 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002910 # 'choice' is usually the last thing on the line (though
2911 # Kconfig supports named choices), so use a word boundary
2912 # (\b) rather than a whitespace character (\s)
2913 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002914 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002915 my $cnt = $realcnt;
2916 my $ln = $linenr + 1;
2917 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002918 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002919 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002920 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002921 $f = $lines[$ln - 1];
2922 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2923 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002924
2925 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002926 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002927
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01002928 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002929 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01002930 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2931 if ($lines[$ln - 1] =~ "---help---") {
2932 WARN("CONFIG_DESCRIPTION",
2933 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2934 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002935 $length = -1;
2936 }
2937
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002938 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002939 $f =~ s/#.*//;
2940 $f =~ s/^\s+//;
2941 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01002942
2943 # This only checks context lines in the patch
2944 # and so hopefully shouldn't trigger false
2945 # positives, even though some of these are
2946 # common words in help texts
2947 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2948 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002949 $is_end = 1;
2950 last;
2951 }
Andi Kleen33549572010-05-24 14:33:29 -07002952 $length++;
2953 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002954 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2955 WARN("CONFIG_DESCRIPTION",
2956 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2957 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002958 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002959 }
2960
Joe Perches628f91a2017-07-10 15:52:07 -07002961# check for MAINTAINERS entries that don't have the right form
2962 if ($realfile =~ /^MAINTAINERS$/ &&
2963 $rawline =~ /^\+[A-Z]:/ &&
2964 $rawline !~ /^\+[A-Z]:\t\S/) {
2965 if (WARN("MAINTAINERS_STYLE",
2966 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2967 $fix) {
2968 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2969 }
2970 }
2971
Christoph Jaeger327953e2015-02-13 14:38:29 -08002972# discourage the use of boolean for type definition attributes of Kconfig options
2973 if ($realfile =~ /Kconfig/ &&
2974 $line =~ /^\+\s*\bboolean\b/) {
2975 WARN("CONFIG_TYPE_BOOLEAN",
2976 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2977 }
2978
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002979 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2980 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2981 my $flag = $1;
2982 my $replacement = {
2983 'EXTRA_AFLAGS' => 'asflags-y',
2984 'EXTRA_CFLAGS' => 'ccflags-y',
2985 'EXTRA_CPPFLAGS' => 'cppflags-y',
2986 'EXTRA_LDFLAGS' => 'ldflags-y',
2987 };
2988
2989 WARN("DEPRECATED_VARIABLE",
2990 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2991 }
2992
Rob Herringbff5da42014-01-23 15:54:51 -08002993# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002994 if (defined $root &&
2995 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2996 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2997
Rob Herringbff5da42014-01-23 15:54:51 -08002998 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2999
Florian Vaussardcc933192014-04-03 14:49:27 -07003000 my $dt_path = $root . "/Documentation/devicetree/bindings/";
3001 my $vp_file = $dt_path . "vendor-prefixes.txt";
3002
Rob Herringbff5da42014-01-23 15:54:51 -08003003 foreach my $compat (@compats) {
3004 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003005 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3006 my $compat3 = $compat;
3007 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3008 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003009 if ( $? >> 8 ) {
3010 WARN("UNDOCUMENTED_DT_STRING",
3011 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3012 }
3013
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003014 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3015 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07003016 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003017 if ( $? >> 8 ) {
3018 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003019 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003020 }
3021 }
3022 }
3023
Rob Herring9f3a8992018-04-10 16:33:13 -07003024# check for using SPDX license tag at beginning of files
3025 if ($realline == $checklicenseline) {
3026 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3027 $checklicenseline = 2;
3028 } elsif ($rawline =~ /^\+/) {
3029 my $comment = "";
3030 if ($realfile =~ /\.(h|s|S)$/) {
3031 $comment = '/*';
3032 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3033 $comment = '//';
3034 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
3035 $comment = '#';
3036 } elsif ($realfile =~ /\.rst$/) {
3037 $comment = '..';
3038 }
3039
Joe Perchesfdf13692019-03-07 16:28:32 -08003040# check SPDX comment style for .[chsS] files
3041 if ($realfile =~ /\.[chsS]$/ &&
3042 $rawline =~ /SPDX-License-Identifier:/ &&
3043 $rawline !~ /^\+\s*\Q$comment\E\s*/) {
3044 WARN("SPDX_LICENSE_TAG",
3045 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3046 }
3047
Rob Herring9f3a8992018-04-10 16:33:13 -07003048 if ($comment !~ /^$/ &&
3049 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003050 WARN("SPDX_LICENSE_TAG",
3051 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3052 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3053 my $spdx_license = $1;
3054 if (!is_SPDX_License_valid($spdx_license)) {
3055 WARN("SPDX_LICENSE_TAG",
3056 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3057 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003058 }
3059 }
3060 }
3061
Andy Whitcroft5368df202008-10-15 22:02:27 -07003062# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003063 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003064
Joe Perches47e0c882015-06-25 15:02:57 -07003065# line length limit (with some exclusions)
3066#
3067# There are a few types of lines that may extend beyond $max_line_length:
3068# logging functions like pr_info that end in a string
3069# lines with a single string
3070# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003071# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003072#
3073# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003074# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003075# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3076# LONG_LINE all other lines longer than $max_line_length
3077#
3078# if LONG_LINE is ignored, the other 2 types are also ignored
3079#
3080
Joe Perchesb4749e92015-07-17 16:24:01 -07003081 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003082 my $msg_type = "LONG_LINE";
3083
3084 # Check the allowed long line types first
3085
3086 # logging functions that end in a string that starts
3087 # before $max_line_length
3088 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3089 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3090 $msg_type = "";
3091
3092 # lines with only strings (w/ possible termination)
3093 # #defines with only strings
3094 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3095 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3096 $msg_type = "";
3097
Joe Perchescc147502017-11-17 15:28:44 -08003098 # More special cases
3099 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3100 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003101 $msg_type = "";
3102
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003103 # URL ($rawline is used in case the URL is in a comment)
3104 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3105 $msg_type = "";
3106
Joe Perches47e0c882015-06-25 15:02:57 -07003107 # Otherwise set the alternate message types
3108
3109 # a comment starts before $max_line_length
3110 } elsif ($line =~ /($;[\s$;]*)$/ &&
3111 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3112 $msg_type = "LONG_LINE_COMMENT"
3113
3114 # a quoted string starts before $max_line_length
3115 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3116 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3117 $msg_type = "LONG_LINE_STRING"
3118 }
3119
3120 if ($msg_type ne "" &&
3121 (show_type("LONG_LINE") || show_type($msg_type))) {
3122 WARN($msg_type,
3123 "line over $max_line_length characters\n" . $herecurr);
3124 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003125 }
3126
Andy Whitcroft8905a672007-11-28 16:21:06 -08003127# check for adding lines without a newline.
3128 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003129 WARN("MISSING_EOF_NEWLINE",
3130 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003131 }
3132
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003133# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003134 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003135
3136# at the beginning of a line any tabs must come first and anything
3137# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003138 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3139 $rawline =~ /^\+\s* \s*/) {
3140 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003141 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003142 if (ERROR("CODE_INDENT",
3143 "code indent should use tabs where possible\n" . $herevet) &&
3144 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003145 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003146 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003147 }
3148
Alberto Panizzo08e44362010-03-05 13:43:54 -08003149# check for space before tabs.
3150 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3151 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003152 if (WARN("SPACE_BEFORE_TAB",
3153 "please, no space before tabs\n" . $herevet) &&
3154 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003155 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003156 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003157 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003158 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003159 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003160 }
3161
Joe Perches6a487212018-04-10 16:34:04 -07003162# check for assignments on the start of a line
3163 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3164 CHK("ASSIGNMENT_CONTINUATIONS",
3165 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3166 }
3167
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003168# check for && or || at the start of a line
3169 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3170 CHK("LOGICAL_CONTINUATIONS",
3171 "Logical continuations should be on the previous line\n" . $hereprev);
3172 }
3173
Joe Perchesa91e8992016-05-20 17:04:05 -07003174# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003175 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003176 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003177 my $indent = length($1);
3178 if ($indent % 8) {
3179 if (WARN("TABSTOP",
3180 "Statements should start on a tabstop\n" . $herecurr) &&
3181 $fix) {
3182 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3183 }
3184 }
3185 }
3186
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003187# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003188 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003189 $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 -07003190 $prevline =~ /^\+(\t*)(.*)$/;
3191 my $oldindent = $1;
3192 my $rest = $2;
3193
3194 my $pos = pos_last_openparen($rest);
3195 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003196 $line =~ /^(\+| )([ \t]*)/;
3197 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003198
3199 my $goodtabindent = $oldindent .
3200 "\t" x ($pos / 8) .
3201 " " x ($pos % 8);
3202 my $goodspaceindent = $oldindent . " " x $pos;
3203
3204 if ($newindent ne $goodtabindent &&
3205 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003206
3207 if (CHK("PARENTHESIS_ALIGNMENT",
3208 "Alignment should match open parenthesis\n" . $hereprev) &&
3209 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003210 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003211 s/^\+[ \t]*/\+$goodtabindent/;
3212 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003213 }
3214 }
3215 }
3216
Joe Perches6ab3a972015-04-16 12:44:05 -07003217# check for space after cast like "(int) foo" or "(struct foo) bar"
3218# avoid checking a few false positives:
3219# "sizeof(<type>)" or "__alignof__(<type>)"
3220# function pointer declarations like "(*foo)(int) = bar;"
3221# structure definitions like "(struct foo) { 0 };"
3222# multiline macros that define functions
3223# known attributes or the __attribute__ keyword
3224 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3225 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003226 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003227 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003228 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003229 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003230 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003231 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003232 }
3233
Joe Perches86406b12015-09-09 15:37:41 -07003234# Block comment styles
3235# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003236 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003237 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003238 $rawline =~ /^\+[ \t]*\*/ &&
3239 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003240 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3241 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3242 }
3243
Joe Perches86406b12015-09-09 15:37:41 -07003244# Block comments use * on subsequent lines
3245 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3246 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003247 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003248 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003249 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003250 WARN("BLOCK_COMMENT_STYLE",
3251 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003252 }
3253
Joe Perches86406b12015-09-09 15:37:41 -07003254# Block comments use */ on trailing lines
3255 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003256 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3257 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3258 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003259 WARN("BLOCK_COMMENT_STYLE",
3260 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003261 }
3262
Joe Perches08eb9b82016-10-11 13:51:50 -07003263# Block comment * alignment
3264 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003265 $line =~ /^\+[ \t]*$;/ && #leading comment
3266 $rawline =~ /^\+[ \t]*\*/ && #leading *
3267 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003268 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003269 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3270 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003271 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003272 if (defined($1)) {
3273 $oldindent = expand_tabs($1);
3274 } else {
3275 $prevrawline =~ m@^\+(.*/?)\*@;
3276 $oldindent = expand_tabs($1);
3277 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003278 $rawline =~ m@^\+([ \t]*)\*@;
3279 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003280 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003281 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003282 WARN("BLOCK_COMMENT_STYLE",
3283 "Block comments should align the * on each line\n" . $hereprev);
3284 }
3285 }
3286
Joe Perches7f619192014-08-06 16:10:39 -07003287# check for missing blank lines after struct/union declarations
3288# with exceptions for various attributes and macros
3289 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3290 $line =~ /^\+/ &&
3291 !($line =~ /^\+\s*$/ ||
3292 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3293 $line =~ /^\+\s*MODULE_/i ||
3294 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3295 $line =~ /^\+[a-z_]*init/ ||
3296 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3297 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003298 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003299 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003300 if (CHK("LINE_SPACING",
3301 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3302 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003303 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003304 }
Joe Perches7f619192014-08-06 16:10:39 -07003305 }
3306
Joe Perches365dd4e2014-08-06 16:10:42 -07003307# check for multiple consecutive blank lines
3308 if ($prevline =~ /^[\+ ]\s*$/ &&
3309 $line =~ /^\+\s*$/ &&
3310 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003311 if (CHK("LINE_SPACING",
3312 "Please don't use multiple blank lines\n" . $hereprev) &&
3313 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003314 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003315 }
3316
Joe Perches365dd4e2014-08-06 16:10:42 -07003317 $last_blank_line = $linenr;
3318 }
3319
Joe Perches3b617e32014-04-03 14:49:28 -07003320# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003321 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3322 # actual declarations
3323 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003324 # function pointer declarations
3325 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003326 # foo bar; where foo is some local typedef or #define
3327 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3328 # known declaration macros
3329 $prevline =~ /^\+\s+$declaration_macros/) &&
3330 # for "else if" which can look like "$Ident $Ident"
3331 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3332 # other possible extensions of declaration lines
3333 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3334 # not starting a section or a macro "\" extended line
3335 $prevline =~ /(?:\{\s*|\\)$/) &&
3336 # looks like a declaration
3337 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003338 # function pointer declarations
3339 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003340 # foo bar; where foo is some local typedef or #define
3341 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3342 # known declaration macros
3343 $sline =~ /^\+\s+$declaration_macros/ ||
3344 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003345 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003346 # start or end of block or continuation of declaration
3347 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3348 # bitfield continuation
3349 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3350 # other possible extensions of declaration lines
3351 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3352 # indentation of previous and current line are the same
3353 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003354 if (WARN("LINE_SPACING",
3355 "Missing a blank line after declarations\n" . $hereprev) &&
3356 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003357 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003358 }
Joe Perches3b617e32014-04-03 14:49:28 -07003359 }
3360
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003361# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003362# Exceptions:
3363# 1) within comments
3364# 2) indented preprocessor commands
3365# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003366 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003367 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003368 if (WARN("LEADING_SPACE",
3369 "please, no spaces at the start of a line\n" . $herevet) &&
3370 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003371 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003372 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003373 }
3374
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003375# check we are in a valid C source file if not then ignore this hunk
3376 next if ($realfile !~ /\.(h|c)$/);
3377
Joe Perches5751a242017-11-17 15:28:52 -08003378# check for unusual line ending [ or (
3379 if ($line =~ /^\+.*([\[\(])\s*$/) {
3380 CHK("OPEN_ENDED_LINE",
3381 "Lines should not end with a '$1'\n" . $herecurr);
3382 }
3383
Joe Perches4dbed762017-05-08 15:55:39 -07003384# check if this appears to be the start function declaration, save the name
3385 if ($sline =~ /^\+\{\s*$/ &&
3386 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3387 $context_function = $1;
3388 }
3389
3390# check if this appears to be the end of function declaration
3391 if ($sline =~ /^\+\}\s*$/) {
3392 undef $context_function;
3393 }
3394
Joe Perches032a4c02014-08-06 16:10:29 -07003395# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003396# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003397# if the previous line is a break or return and is indented 1 tab more...
3398 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3399 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003400 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3401 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3402 defined $lines[$linenr] &&
3403 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003404 WARN("UNNECESSARY_ELSE",
3405 "else is not generally useful after a break or return\n" . $hereprev);
3406 }
3407 }
3408
Joe Perchesc00df192014-08-06 16:11:01 -07003409# check indentation of a line with a break;
3410# if the previous line is a goto or return and is indented the same # of tabs
3411 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3412 my $tabs = $1;
3413 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3414 WARN("UNNECESSARY_BREAK",
3415 "break is not useful after a goto or return\n" . $hereprev);
3416 }
3417 }
3418
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003419# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003420 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003421 WARN("CVS_KEYWORD",
3422 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003423 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003424
Joe Perches56e77d72013-02-21 16:44:14 -08003425# check for old HOTPLUG __dev<foo> section markings
3426 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3427 WARN("HOTPLUG_SECTION",
3428 "Using $1 is unnecessary\n" . $herecurr);
3429 }
3430
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003431# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003432 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3433 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003434#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003435 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003436 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003437 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003438 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003439 $stat =~ s/\n./\n /g;
3440 $cond =~ s/\n./\n /g;
3441
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003442#print "linenr<$linenr> <$stat>\n";
3443 # If this statement has no statement boundaries within
3444 # it there is no point in retrying a statement scan
3445 # until we hit end of it.
3446 my $frag = $stat; $frag =~ s/;+\s*$//;
3447 if ($frag !~ /(?:{|;)/) {
3448#print "skip<$line_nr_next>\n";
3449 $suppress_statement = $line_nr_next;
3450 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003451
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003452 # Find the real next line.
3453 $realline_next = $line_nr_next;
3454 if (defined $realline_next &&
3455 (!defined $lines[$realline_next - 1] ||
3456 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3457 $realline_next++;
3458 }
3459
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003460 my $s = $stat;
3461 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003462
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003463 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003464 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003465
3466 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003467 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003468
Andy Whitcroft463f2862009-09-21 17:04:34 -07003469 } elsif ($s =~ /^.\s*else\b/s) {
3470
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003471 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003472 } 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 -07003473 my $type = $1;
3474 $type =~ s/\s+/ /g;
3475 possible($type, "A:" . $s);
3476
Andy Whitcroft8905a672007-11-28 16:21:06 -08003477 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003478 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003479 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003480 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003481
3482 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003483 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003484 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003485 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003486
3487 # Check for any sort of function declaration.
3488 # int foo(something bar, other baz);
3489 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003490 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 -08003491 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003492
Andy Whitcroftcf655042008-03-04 14:28:20 -08003493 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003494 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003495 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003496
Andy Whitcroft8905a672007-11-28 16:21:06 -08003497 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003498 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003499
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003500 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003501 }
3502 }
3503 }
3504
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003505 }
3506
Andy Whitcroft653d4872007-06-23 17:16:34 -07003507#
3508# Checks which may be anchored in the context.
3509#
3510
3511# Check for switch () and associated case and default
3512# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003513 if ($line=~/\bswitch\s*\(.*\)/) {
3514 my $err = '';
3515 my $sep = '';
3516 my @ctx = ctx_block_outer($linenr, $realcnt);
3517 shift(@ctx);
3518 for my $ctx (@ctx) {
3519 my ($clen, $cindent) = line_stats($ctx);
3520 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3521 $indent != $cindent) {
3522 $err .= "$sep$ctx\n";
3523 $sep = '';
3524 } else {
3525 $sep = "[...]\n";
3526 }
3527 }
3528 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003529 ERROR("SWITCH_CASE_INDENT_LEVEL",
3530 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003531 }
3532 }
3533
3534# if/while/etc brace do not go on next line, unless defining a do while loop,
3535# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003536 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 -07003537 my $pre_ctx = "$1$2";
3538
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003539 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003540
3541 if ($line =~ /^\+\t{6,}/) {
3542 WARN("DEEP_INDENTATION",
3543 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3544 }
3545
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003546 my $ctx_cnt = $realcnt - $#ctx - 1;
3547 my $ctx = join("\n", @ctx);
3548
Andy Whitcroft548596d2008-07-23 21:29:01 -07003549 my $ctx_ln = $linenr;
3550 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003551
Andy Whitcroft548596d2008-07-23 21:29:01 -07003552 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3553 defined $lines[$ctx_ln - 1] &&
3554 $lines[$ctx_ln - 1] =~ /^-/)) {
3555 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3556 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003557 $ctx_ln++;
3558 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003559
Andy Whitcroft53210162008-07-23 21:29:03 -07003560 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3561 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003562
Joe Perchesd752fcc2014-08-06 16:11:05 -07003563 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003564 ERROR("OPEN_BRACE",
3565 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003566 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003567 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003568 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3569 $ctx =~ /\)\s*\;\s*$/ &&
3570 defined $lines[$ctx_ln - 1])
3571 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003572 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3573 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003574 WARN("TRAILING_SEMICOLON",
3575 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003576 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003577 }
3578 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003579 }
3580
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003581# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003582 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 -08003583 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3584 ctx_statement_block($linenr, $realcnt, 0)
3585 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003586 my ($s, $c) = ($stat, $cond);
3587
3588 substr($s, 0, length($c), '');
3589
Joe Perches9f5af482015-09-09 15:37:30 -07003590 # remove inline comments
3591 $s =~ s/$;/ /g;
3592 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003593
3594 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003595 my @newlines = ($c =~ /\n/gs);
3596 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003597
Joe Perches9f5af482015-09-09 15:37:30 -07003598 # Make sure we remove the line prefixes as we have
3599 # none on the first line, and are going to readd them
3600 # where necessary.
3601 $s =~ s/\n./\n/gs;
3602 while ($s =~ /\n\s+\\\n/) {
3603 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3604 }
3605
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003606 # We want to check the first line inside the block
3607 # starting at the end of the conditional, so remove:
3608 # 1) any blank line termination
3609 # 2) any opening brace { on end of the line
3610 # 3) any do (...) {
3611 my $continuation = 0;
3612 my $check = 0;
3613 $s =~ s/^.*\bdo\b//;
3614 $s =~ s/^\s*{//;
3615 if ($s =~ s/^\s*\\//) {
3616 $continuation = 1;
3617 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003618 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003619 $check = 1;
3620 $cond_lines++;
3621 }
3622
3623 # Also ignore a loop construct at the end of a
3624 # preprocessor statement.
3625 if (($prevline =~ /^.\s*#\s*define\s/ ||
3626 $prevline =~ /\\\s*$/) && $continuation == 0) {
3627 $check = 0;
3628 }
3629
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003630 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003631 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003632 while ($cond_ptr != $cond_lines) {
3633 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003634
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003635 # If we see an #else/#elif then the code
3636 # is not linear.
3637 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3638 $check = 0;
3639 }
3640
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003641 # Ignore:
3642 # 1) blank lines, they should be at 0,
3643 # 2) preprocessor lines, and
3644 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003645 if ($continuation ||
3646 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003647 $s =~ /^\s*#\s*?/ ||
3648 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003649 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003650 if ($s =~ s/^.*?\n//) {
3651 $cond_lines++;
3652 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003653 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003654 }
3655
3656 my (undef, $sindent) = line_stats("+" . $s);
3657 my $stat_real = raw_line($linenr, $cond_lines);
3658
3659 # Check if either of these lines are modified, else
3660 # this is not this patch's fault.
3661 if (!defined($stat_real) ||
3662 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3663 $check = 0;
3664 }
3665 if (defined($stat_real) && $cond_lines > 1) {
3666 $stat_real = "[...]\n$stat_real";
3667 }
3668
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003669 #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 -07003670
Joe Perches9f5af482015-09-09 15:37:30 -07003671 if ($check && $s ne '' &&
3672 (($sindent % 8) != 0 ||
3673 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003674 ($sindent == $indent &&
3675 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003676 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003677 WARN("SUSPECT_CODE_INDENT",
3678 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003679 }
3680 }
3681
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003682 # Track the 'values' across context and added lines.
3683 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003684 my ($curr_values, $curr_vars) =
3685 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003686 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003687 if ($dbg_values) {
3688 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003689 print "$linenr > .$outline\n";
3690 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003691 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003692 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003693 $prev_values = substr($curr_values, -1);
3694
Andy Whitcroft00df3442007-06-08 13:47:06 -07003695#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003696 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003697
Joe Perches11ca40a2016-12-12 16:46:31 -08003698# check for dereferences that span multiple lines
3699 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3700 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3701 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3702 my $ref = $1;
3703 $line =~ /^.\s*($Lval)/;
3704 $ref .= $1;
3705 $ref =~ s/\s//g;
3706 WARN("MULTILINE_DEREFERENCE",
3707 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3708 }
3709
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003710# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003711 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 -07003712 my $type = $1;
3713 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003714 $var = "" if (!defined $var);
3715 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003716 my $sign = $1;
3717 my $pointer = $2;
3718
3719 $pointer = "" if (!defined $pointer);
3720
3721 if (WARN("UNSPECIFIED_INT",
3722 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3723 $fix) {
3724 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003725 my $comp_pointer = $pointer;
3726 $comp_pointer =~ s/\s//g;
3727 $decl .= $comp_pointer;
3728 $decl = rtrim($decl) if ($var eq "");
3729 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003730 }
3731 }
3732 }
3733
Andy Whitcroft653d4872007-06-23 17:16:34 -07003734# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003735 if ($dbg_type) {
3736 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003737 ERROR("TEST_TYPE",
3738 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003739 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003740 ERROR("TEST_NOT_TYPE",
3741 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003742 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003743 next;
3744 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003745# TEST: allow direct testing of the attribute matcher.
3746 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003747 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003748 ERROR("TEST_ATTR",
3749 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003750 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003751 ERROR("TEST_NOT_ATTR",
3752 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003753 }
3754 next;
3755 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003756
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003757# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003758 if ($line =~ /^.\s*{/ &&
3759 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003760 if (ERROR("OPEN_BRACE",
3761 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003762 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3763 fix_delete_line($fixlinenr - 1, $prevrawline);
3764 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003765 my $fixedline = $prevrawline;
3766 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003767 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003768 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003769 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003770 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003771 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003772 }
3773
Andy Whitcroft653d4872007-06-23 17:16:34 -07003774#
3775# Checks which are anchored on the added line.
3776#
3777
3778# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003779 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003780 my $path = $1;
3781 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003782 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003783 "malformed #include filename\n" . $herecurr);
3784 }
3785 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3786 ERROR("UAPI_INCLUDE",
3787 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003788 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003789 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003790
3791# no C99 // comments
3792 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003793 if (ERROR("C99_COMMENTS",
3794 "do not use C99 // comments\n" . $herecurr) &&
3795 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003796 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003797 if ($line =~ /\/\/(.*)$/) {
3798 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003799 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003800 }
3801 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003802 }
3803 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003804 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003805 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003806
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003807# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3808# the whole statement.
3809#print "APW <$lines[$realline_next - 1]>\n";
3810 if (defined $realline_next &&
3811 exists $lines[$realline_next - 1] &&
3812 !defined $suppress_export{$realline_next} &&
3813 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3814 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003815 # Handle definitions which produce identifiers with
3816 # a prefix:
3817 # XXX(foo);
3818 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003819 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003820 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003821 $name =~ /^${Ident}_$2/) {
3822#print "FOO C name<$name>\n";
3823 $suppress_export{$realline_next} = 1;
3824
3825 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003826 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003827 ^.DEFINE_$Ident\(\Q$name\E\)|
3828 ^.DECLARE_$Ident\(\Q$name\E\)|
3829 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003830 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3831 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003832 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003833#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3834 $suppress_export{$realline_next} = 2;
3835 } else {
3836 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003837 }
3838 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003839 if (!defined $suppress_export{$linenr} &&
3840 $prevline =~ /^.\s*$/ &&
3841 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3842 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3843#print "FOO B <$lines[$linenr - 1]>\n";
3844 $suppress_export{$linenr} = 2;
3845 }
3846 if (defined $suppress_export{$linenr} &&
3847 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003848 WARN("EXPORT_SYMBOL",
3849 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003850 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003851
Joe Eloff5150bda2010-08-09 17:21:00 -07003852# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003853 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003854 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003855 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003856 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003857 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003858 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003859 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003860# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003861 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003862 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003863 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003864 $herecurr) &&
3865 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003866 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003867 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003868 }
3869
Joe Perches18130872014-08-06 16:11:22 -07003870# check for misordered declarations of char/short/int/long with signed/unsigned
3871 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3872 my $tmp = trim($1);
3873 WARN("MISORDERED_TYPE",
3874 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3875 }
3876
Joe Perches809e0822018-08-21 21:58:12 -07003877# check for unnecessary <signed> int declarations of short/long/long long
3878 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3879 my $type = trim($1);
3880 next if ($type !~ /\bint\b/);
3881 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3882 my $new_type = $type;
3883 $new_type =~ s/\b\s*int\s*\b/ /;
3884 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3885 $new_type =~ s/^const\s+//;
3886 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3887 $new_type = "const $new_type" if ($type =~ /^const\b/);
3888 $new_type =~ s/\s+/ /g;
3889 $new_type = trim($new_type);
3890 if (WARN("UNNECESSARY_INT",
3891 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3892 $fix) {
3893 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3894 }
3895 }
3896
Joe Perchescb710ec2010-10-26 14:23:20 -07003897# check for static const char * arrays.
3898 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003899 WARN("STATIC_CONST_CHAR_ARRAY",
3900 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003901 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003902 }
3903
3904# check for initialized const char arrays that should be static const
3905 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
3906 if (WARN("STATIC_CONST_CHAR_ARRAY",
3907 "const array should probably be static const\n" . $herecurr) &&
3908 $fix) {
3909 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
3910 }
3911 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003912
3913# check for static char foo[] = "bar" declarations.
3914 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003915 WARN("STATIC_CONST_CHAR_ARRAY",
3916 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003917 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003918 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003919
Joe Perchesab7e23f2015-04-16 12:44:22 -07003920# check for const <foo> const where <foo> is not a pointer or array type
3921 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3922 my $found = $1;
3923 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3924 WARN("CONST_CONST",
3925 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3926 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3927 WARN("CONST_CONST",
3928 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3929 }
3930 }
3931
Joe Perches9b0fa602014-04-03 14:49:18 -07003932# check for non-global char *foo[] = {"bar", ...} declarations.
3933 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3934 WARN("STATIC_CONST_CHAR_ARRAY",
3935 "char * array declaration might be better as static const\n" .
3936 $herecurr);
3937 }
3938
Joe Perchesb598b672015-04-16 12:44:36 -07003939# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3940 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3941 my $array = $1;
3942 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3943 my $array_div = $1;
3944 if (WARN("ARRAY_SIZE",
3945 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3946 $fix) {
3947 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3948 }
3949 }
3950 }
3951
Joe Perchesb36190c2014-01-27 17:07:18 -08003952# check for function declarations without arguments like "int foo()"
3953 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3954 if (ERROR("FUNCTION_WITHOUT_ARGS",
3955 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3956 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003957 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003958 }
3959 }
3960
Andy Whitcroft653d4872007-06-23 17:16:34 -07003961# check for new typedefs, only function parameters and sparse annotations
3962# make sense.
3963 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003964 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003965 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003966 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003967 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003968 WARN("NEW_TYPEDEFS",
3969 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003970 }
3971
3972# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003973 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003974 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3975 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003976 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003977
Andy Whitcroft65863862009-01-06 14:41:21 -08003978 # Should start with a space.
3979 $to =~ s/^(\S)/ $1/;
3980 # Should not end with a space.
3981 $to =~ s/\s+$//;
3982 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003983 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003984 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003985
Joe Perches3705ce52013-07-03 15:05:31 -07003986## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003987 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003988 if (ERROR("POINTER_LOCATION",
3989 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3990 $fix) {
3991 my $sub_from = $ident;
3992 my $sub_to = $ident;
3993 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003994 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003995 s@\Q$sub_from\E@$sub_to@;
3996 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003997 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003998 }
3999 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4000 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004001 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004002
Andy Whitcroft65863862009-01-06 14:41:21 -08004003 # Should start with a space.
4004 $to =~ s/^(\S)/ $1/;
4005 # Should not end with a space.
4006 $to =~ s/\s+$//;
4007 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004008 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004009 }
4010 # Modifiers should have spaces.
4011 $to =~ s/(\b$Modifier$)/$1 /;
4012
Joe Perches3705ce52013-07-03 15:05:31 -07004013## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004014 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004015 if (ERROR("POINTER_LOCATION",
4016 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4017 $fix) {
4018
4019 my $sub_from = $match;
4020 my $sub_to = $match;
4021 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004022 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004023 s@\Q$sub_from\E@$sub_to@;
4024 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004025 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004026 }
4027
Joe Perches9d3e3c72015-09-09 15:37:27 -07004028# avoid BUG() or BUG_ON()
4029 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004030 my $msg_level = \&WARN;
4031 $msg_level = \&CHK if ($file);
4032 &{$msg_level}("AVOID_BUG",
4033 "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 -07004034 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004035
Joe Perches9d3e3c72015-09-09 15:37:27 -07004036# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004037 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004038 WARN("LINUX_VERSION_CODE",
4039 "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 -08004040 }
4041
Joe Perches17441222011-06-15 15:08:17 -07004042# check for uses of printk_ratelimit
4043 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004044 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004045 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004046 }
4047
Joe Percheseeef5732017-11-17 15:28:41 -08004048# printk should use KERN_* levels
4049 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4050 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4051 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004052 }
4053
Joe Perches243f3802012-05-31 16:26:09 -07004054 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4055 my $orig = $1;
4056 my $level = lc($orig);
4057 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004058 my $level2 = $level;
4059 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004060 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004061 "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 -07004062 }
4063
4064 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004065 if (WARN("PREFER_PR_LEVEL",
4066 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4067 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004068 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004069 s/\bpr_warning\b/pr_warn/;
4070 }
Joe Perches243f3802012-05-31 16:26:09 -07004071 }
4072
Joe Perchesdc139312013-02-21 16:44:13 -08004073 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4074 my $orig = $1;
4075 my $level = lc($orig);
4076 $level = "warn" if ($level eq "warning");
4077 $level = "dbg" if ($level eq "debug");
4078 WARN("PREFER_DEV_LEVEL",
4079 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4080 }
4081
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004082# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4083# number of false positives, but assembly files are not checked, so at
4084# least the arch entry code will not trigger this warning.
4085 if ($line =~ /\bENOSYS\b/) {
4086 WARN("ENOSYS",
4087 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4088 }
4089
Andy Whitcroft653d4872007-06-23 17:16:34 -07004090# function brace can't be on same line, except for #defines of do while,
4091# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004092 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004093 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4094 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4095 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004096 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004097 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004098 $fix) {
4099 fix_delete_line($fixlinenr, $rawline);
4100 my $fixed_line = $rawline;
4101 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4102 my $line1 = $1;
4103 my $line2 = $2;
4104 fix_insert_line($fixlinenr, ltrim($line1));
4105 fix_insert_line($fixlinenr, "\+{");
4106 if ($line2 !~ /^\s*$/) {
4107 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4108 }
4109 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004110 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004111
Andy Whitcroft8905a672007-11-28 16:21:06 -08004112# open braces for enum, union and struct go on the same line.
4113 if ($line =~ /^.\s*{/ &&
4114 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004115 if (ERROR("OPEN_BRACE",
4116 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4117 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4118 fix_delete_line($fixlinenr - 1, $prevrawline);
4119 fix_delete_line($fixlinenr, $rawline);
4120 my $fixedline = rtrim($prevrawline) . " {";
4121 fix_insert_line($fixlinenr, $fixedline);
4122 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004123 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004124 if ($fixedline !~ /^\+\s*$/) {
4125 fix_insert_line($fixlinenr, $fixedline);
4126 }
4127 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004128 }
4129
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004130# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004131 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4132 if (WARN("SPACING",
4133 "missing space after $1 definition\n" . $herecurr) &&
4134 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004135 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004136 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4137 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004138 }
4139
Joe Perches31070b52014-01-23 15:54:49 -08004140# Function pointer declarations
4141# check spacing between type, funcptr, and args
4142# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004143 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004144 my $declare = $1;
4145 my $pre_pointer_space = $2;
4146 my $post_pointer_space = $3;
4147 my $funcname = $4;
4148 my $post_funcname_space = $5;
4149 my $pre_args_space = $6;
4150
Joe Perches91f72e92014-04-03 14:49:12 -07004151# the $Declare variable will capture all spaces after the type
4152# so check it for a missing trailing missing space but pointer return types
4153# don't need a space so don't warn for those.
4154 my $post_declare_space = "";
4155 if ($declare =~ /(\s+)$/) {
4156 $post_declare_space = $1;
4157 $declare = rtrim($declare);
4158 }
4159 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004160 WARN("SPACING",
4161 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004162 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004163 }
4164
4165# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004166# This test is not currently implemented because these declarations are
4167# equivalent to
4168# int foo(int bar, ...)
4169# and this is form shouldn't/doesn't generate a checkpatch warning.
4170#
4171# elsif ($declare =~ /\s{2,}$/) {
4172# WARN("SPACING",
4173# "Multiple spaces after return type\n" . $herecurr);
4174# }
Joe Perches31070b52014-01-23 15:54:49 -08004175
4176# unnecessary space "type ( *funcptr)(args...)"
4177 if (defined $pre_pointer_space &&
4178 $pre_pointer_space =~ /^\s/) {
4179 WARN("SPACING",
4180 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4181 }
4182
4183# unnecessary space "type (* funcptr)(args...)"
4184 if (defined $post_pointer_space &&
4185 $post_pointer_space =~ /^\s/) {
4186 WARN("SPACING",
4187 "Unnecessary space before function pointer name\n" . $herecurr);
4188 }
4189
4190# unnecessary space "type (*funcptr )(args...)"
4191 if (defined $post_funcname_space &&
4192 $post_funcname_space =~ /^\s/) {
4193 WARN("SPACING",
4194 "Unnecessary space after function pointer name\n" . $herecurr);
4195 }
4196
4197# unnecessary space "type (*funcptr) (args...)"
4198 if (defined $pre_args_space &&
4199 $pre_args_space =~ /^\s/) {
4200 WARN("SPACING",
4201 "Unnecessary space before function pointer arguments\n" . $herecurr);
4202 }
4203
4204 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004205 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004206 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004207 }
4208 }
4209
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004210# check for spacing round square brackets; allowed:
4211# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004212# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4213# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004214 while ($line =~ /(.*?\s)\[/g) {
4215 my ($where, $prefix) = ($-[1], $1);
4216 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004217 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004218 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004219 if (ERROR("BRACKET_SPACE",
4220 "space prohibited before open square bracket '['\n" . $herecurr) &&
4221 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004222 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004223 s/^(\+.*?)\s+\[/$1\[/;
4224 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004225 }
4226 }
4227
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004228# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004229 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004230 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004231 my $ctx_before = substr($line, 0, $-[1]);
4232 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004233
4234 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004235 if ($name =~ /^(?:
4236 if|for|while|switch|return|case|
4237 volatile|__volatile__|
4238 __attribute__|format|__extension__|
4239 asm|__asm__)$/x)
4240 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004241 # cpp #define statements have non-optional spaces, ie
4242 # if there is a space between the name and the open
4243 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004244 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004245
4246 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004247 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004248
4249 # If this whole things ends with a type its most
4250 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004251 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004252
4253 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004254 if (WARN("SPACING",
4255 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4256 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004257 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004258 s/\b$name\s+\(/$name\(/;
4259 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004260 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004261 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004262
Andy Whitcroft653d4872007-06-23 17:16:34 -07004263# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004264 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004265 my $fixed_line = "";
4266 my $line_fixed = 0;
4267
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004268 my $ops = qr{
4269 <<=|>>=|<=|>=|==|!=|
4270 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4271 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004272 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004273 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004274 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004275 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004276
4277## print("element count: <" . $#elements . ">\n");
4278## foreach my $el (@elements) {
4279## print("el: <$el>\n");
4280## }
4281
4282 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004283 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004284
Joe Perches3705ce52013-07-03 15:05:31 -07004285 foreach my $el (@elements) {
4286 push(@fix_elements, substr($rawline, $off, length($el)));
4287 $off += length($el);
4288 }
4289
4290 $off = 0;
4291
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004292 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004293 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004294
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004295 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004296
4297 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4298
4299## print("n: <$n> good: <$good>\n");
4300
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004301 $off += length($elements[$n]);
4302
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004303 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004304 my $ca = substr($opline, 0, $off);
4305 my $cc = '';
4306 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4307 $cc = substr($opline, $off + length($elements[$n + 1]));
4308 }
4309 my $cb = "$ca$;$cc";
4310
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004311 my $a = '';
4312 $a = 'V' if ($elements[$n] ne '');
4313 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004314 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004315 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4316 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004317 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004318
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004319 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004320
4321 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004322 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004323 $c = 'V' if ($elements[$n + 2] ne '');
4324 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004325 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004326 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4327 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004328 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004329 } else {
4330 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004331 }
4332
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004333 my $ctx = "${a}x${c}";
4334
4335 my $at = "(ctx:$ctx)";
4336
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004337 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004338 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004339
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004340 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004341 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004342
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004343 # Get the full operator variant.
4344 my $opv = $op . substr($curr_vars, $off, 1);
4345
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004346 # Ignore operators passed as parameters.
4347 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004348 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004349
Andy Whitcroftcf655042008-03-04 14:28:20 -08004350# # Ignore comments
4351# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004352
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004353 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004354 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004355 if ($ctx !~ /.x[WEBC]/ &&
4356 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004357 if (ERROR("SPACING",
4358 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004359 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004360 $line_fixed = 1;
4361 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004362 }
4363
4364 # // is a comment
4365 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004366
Joe Perchesb00e4812014-04-03 14:49:33 -07004367 # : when part of a bitfield
4368 } elsif ($opv eq ':B') {
4369 # skip the bitfield test for now
4370
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004371 # No spaces for:
4372 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004373 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004374 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004375 if (ERROR("SPACING",
4376 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004377 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004378 if (defined $fix_elements[$n + 2]) {
4379 $fix_elements[$n + 2] =~ s/^\s+//;
4380 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004381 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004382 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004383 }
4384
Joe Perches23810972014-12-10 15:51:32 -08004385 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004386 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004387 my $rtrim_before = 0;
4388 my $space_after = 0;
4389 if ($ctx =~ /Wx./) {
4390 if (ERROR("SPACING",
4391 "space prohibited before that '$op' $at\n" . $hereptr)) {
4392 $line_fixed = 1;
4393 $rtrim_before = 1;
4394 }
4395 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004396 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004397 if (ERROR("SPACING",
4398 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004399 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004400 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004401 $space_after = 1;
4402 }
4403 }
4404 if ($rtrim_before || $space_after) {
4405 if ($rtrim_before) {
4406 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4407 } else {
4408 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4409 }
4410 if ($space_after) {
4411 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004412 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004413 }
4414
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004415 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004416 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004417 #warn "'*' is part of type\n";
4418
4419 # unary operators should have a space before and
4420 # none after. May be left adjacent to another
4421 # unary operator, or a cast
4422 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004423 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004424 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004425 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004426 if (ERROR("SPACING",
4427 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004428 if ($n != $last_after + 2) {
4429 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4430 $line_fixed = 1;
4431 }
Joe Perches3705ce52013-07-03 15:05:31 -07004432 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004433 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004434 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004435 # A unary '*' may be const
4436
4437 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004438 if (ERROR("SPACING",
4439 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004440 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004441 if (defined $fix_elements[$n + 2]) {
4442 $fix_elements[$n + 2] =~ s/^\s+//;
4443 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004444 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004445 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004446 }
4447
4448 # unary ++ and unary -- are allowed no space on one side.
4449 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004450 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004451 if (ERROR("SPACING",
4452 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004453 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004454 $line_fixed = 1;
4455 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004456 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004457 if ($ctx =~ /Wx[BE]/ ||
4458 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
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 Whitcroft653d4872007-06-23 17:16:34 -07004464 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004465 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004466 if (ERROR("SPACING",
4467 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004468 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004469 if (defined $fix_elements[$n + 2]) {
4470 $fix_elements[$n + 2] =~ s/^\s+//;
4471 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004472 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004473 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004474 }
4475
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004476 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004477 } elsif ($op eq '<<' or $op eq '>>' or
4478 $op eq '&' or $op eq '^' or $op eq '|' or
4479 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004480 $op eq '*' or $op eq '/' or
4481 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004482 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004483 if ($check) {
4484 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4485 if (CHK("SPACING",
4486 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4487 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4488 $fix_elements[$n + 2] =~ s/^\s+//;
4489 $line_fixed = 1;
4490 }
4491 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4492 if (CHK("SPACING",
4493 "space preferred before that '$op' $at\n" . $hereptr)) {
4494 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4495 $line_fixed = 1;
4496 }
4497 }
4498 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004499 if (ERROR("SPACING",
4500 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004501 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4502 if (defined $fix_elements[$n + 2]) {
4503 $fix_elements[$n + 2] =~ s/^\s+//;
4504 }
Joe Perches3705ce52013-07-03 15:05:31 -07004505 $line_fixed = 1;
4506 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004507 }
4508
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004509 # A colon needs no spaces before when it is
4510 # terminating a case value or a label.
4511 } elsif ($opv eq ':C' || $opv eq ':L') {
4512 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004513 if (ERROR("SPACING",
4514 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004515 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004516 $line_fixed = 1;
4517 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004518 }
4519
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004520 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004521 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004522 my $ok = 0;
4523
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004524 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004525 if (($op eq '<' &&
4526 $cc =~ /^\S+\@\S+>/) ||
4527 ($op eq '>' &&
4528 $ca =~ /<\S+\@\S+$/))
4529 {
4530 $ok = 1;
4531 }
4532
Joe Perchese0df7e12015-04-16 12:44:53 -07004533 # for asm volatile statements
4534 # ignore a colon with another
4535 # colon immediately before or after
4536 if (($op eq ':') &&
4537 ($ca =~ /:$/ || $cc =~ /^:/)) {
4538 $ok = 1;
4539 }
4540
Joe Perches84731622013-11-12 15:10:05 -08004541 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004542 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004543 my $msg_level = \&ERROR;
4544 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004545
Jean Delvare0675a8f2017-09-08 16:16:07 -07004546 if (&{$msg_level}("SPACING",
4547 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004548 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4549 if (defined $fix_elements[$n + 2]) {
4550 $fix_elements[$n + 2] =~ s/^\s+//;
4551 }
Joe Perches3705ce52013-07-03 15:05:31 -07004552 $line_fixed = 1;
4553 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004554 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004555 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004556 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004557
4558## print("n: <$n> GOOD: <$good>\n");
4559
4560 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004561 }
Joe Perches3705ce52013-07-03 15:05:31 -07004562
4563 if (($#elements % 2) == 0) {
4564 $fixed_line = $fixed_line . $fix_elements[$#elements];
4565 }
4566
Joe Perches194f66f2014-08-06 16:11:03 -07004567 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4568 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004569 }
4570
4571
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004572 }
4573
Joe Perches786b6322013-07-03 15:05:32 -07004574# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004575 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004576 if (WARN("SPACING",
4577 "space prohibited before semicolon\n" . $herecurr) &&
4578 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004579 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004580 s/^(\+.*\S)\s+;/$1;/;
4581 }
4582 }
4583
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004584# check for multiple assignments
4585 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004586 CHK("MULTIPLE_ASSIGNMENTS",
4587 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004588 }
4589
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004590## # check for multiple declarations, allowing for a function declaration
4591## # continuation.
4592## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4593## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4594##
4595## # Remove any bracketed sections to ensure we do not
4596## # falsly report the parameters of functions.
4597## my $ln = $line;
4598## while ($ln =~ s/\([^\(\)]*\)//g) {
4599## }
4600## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004601## WARN("MULTIPLE_DECLARATION",
4602## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004603## }
4604## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004605
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004606#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004607 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004608 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004609 if (ERROR("SPACING",
4610 "space required before the open brace '{'\n" . $herecurr) &&
4611 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004612 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004613 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004614 }
4615
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004616## # check for blank lines before declarations
4617## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4618## $prevrawline =~ /^.\s*$/) {
4619## WARN("SPACING",
4620## "No blank lines before declarations\n" . $hereprev);
4621## }
4622##
4623
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004624# closing brace should have a space following it when it has anything
4625# on the line
4626 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004627 if (ERROR("SPACING",
4628 "space required after that close brace '}'\n" . $herecurr) &&
4629 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004630 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004631 s/}((?!(?:,|;|\)))\S)/} $1/;
4632 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004633 }
4634
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004635# check spacing on square brackets
4636 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004637 if (ERROR("SPACING",
4638 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4639 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004640 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004641 s/\[\s+/\[/;
4642 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004643 }
4644 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004645 if (ERROR("SPACING",
4646 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4647 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004648 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004649 s/\s+\]/\]/;
4650 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004651 }
4652
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004653# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004654 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4655 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004656 if (ERROR("SPACING",
4657 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4658 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004659 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004660 s/\(\s+/\(/;
4661 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004662 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004663 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004664 $line !~ /for\s*\(.*;\s+\)/ &&
4665 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004666 if (ERROR("SPACING",
4667 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4668 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004669 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004670 s/\s+\)/\)/;
4671 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004672 }
4673
Joe Perchese2826fd2014-08-06 16:10:48 -07004674# check unnecessary parentheses around addressof/dereference single $Lvals
4675# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4676
4677 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004678 my $var = $1;
4679 if (CHK("UNNECESSARY_PARENTHESES",
4680 "Unnecessary parentheses around $var\n" . $herecurr) &&
4681 $fix) {
4682 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4683 }
4684 }
4685
4686# check for unnecessary parentheses around function pointer uses
4687# ie: (foo->bar)(); should be foo->bar();
4688# but not "if (foo->bar) (" to avoid some false positives
4689 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4690 my $var = $2;
4691 if (CHK("UNNECESSARY_PARENTHESES",
4692 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4693 $fix) {
4694 my $var2 = deparenthesize($var);
4695 $var2 =~ s/\s//g;
4696 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4697 }
4698 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004699
Joe Perches63b7c732017-09-08 16:16:01 -07004700# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004701# when !drivers/staging or command-line uses --strict
4702 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004703 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004704 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4705 my $if_stat = $1;
4706 my $test = substr($2, 1, -1);
4707 my $herectx;
4708 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4709 my $match = $1;
4710 # avoid parentheses around potential macro args
4711 next if ($match =~ /^\s*\w+\s*$/);
4712 if (!defined($herectx)) {
4713 $herectx = $here . "\n";
4714 my $cnt = statement_rawlines($if_stat);
4715 for (my $n = 0; $n < $cnt; $n++) {
4716 my $rl = raw_line($linenr, $n);
4717 $herectx .= $rl . "\n";
4718 last if $rl =~ /^[ \+].*\{/;
4719 }
4720 }
4721 CHK("UNNECESSARY_PARENTHESES",
4722 "Unnecessary parentheses around '$match'\n" . $herectx);
4723 }
4724 }
4725
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004726#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004727 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004728 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004729 if (WARN("INDENTED_LABEL",
4730 "labels should not be indented\n" . $herecurr) &&
4731 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004732 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004733 s/^(.)\s+/$1/;
4734 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004735 }
4736
Joe Perches5b9553a2014-04-03 14:49:21 -07004737# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004738 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004739 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004740 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004741 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4742 my $value = $1;
4743 $value = deparenthesize($value);
4744 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4745 ERROR("RETURN_PARENTHESES",
4746 "return is not a function, parentheses are not required\n" . $herecurr);
4747 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004748 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004749 ERROR("SPACING",
4750 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004751 }
4752 }
Joe Perches507e5142013-11-12 15:10:13 -08004753
Joe Perchesb43ae212014-06-23 13:22:07 -07004754# unnecessary return in a void function
4755# at end-of-function, with the previous line a single leading tab, then return;
4756# and the line before that not a goto label target like "out:"
4757 if ($sline =~ /^[ \+]}\s*$/ &&
4758 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4759 $linenr >= 3 &&
4760 $lines[$linenr - 3] =~ /^[ +]/ &&
4761 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004762 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004763 "void function return statements are not generally useful\n" . $hereprev);
4764 }
Joe Perches9819cf22014-06-04 16:12:09 -07004765
Joe Perches189248d2014-01-23 15:54:47 -08004766# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004767 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004768 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4769 my $openparens = $1;
4770 my $count = $openparens =~ tr@\(@\(@;
4771 my $msg = "";
4772 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4773 my $comp = $4; #Not $1 because of $LvalOrFunc
4774 $msg = " - maybe == should be = ?" if ($comp eq "==");
4775 WARN("UNNECESSARY_PARENTHESES",
4776 "Unnecessary parentheses$msg\n" . $herecurr);
4777 }
4778 }
4779
Joe Perchesc5595fa2015-09-09 15:37:58 -07004780# comparisons with a constant or upper case identifier on the left
4781# avoid cases like "foo + BAR < baz"
4782# only fix matches surrounded by parentheses to avoid incorrect
4783# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004784 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004785 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4786 my $lead = $1;
4787 my $const = $2;
4788 my $comp = $3;
4789 my $to = $4;
4790 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004791 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004792 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4793 WARN("CONSTANT_COMPARISON",
4794 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4795 $fix) {
4796 if ($comp eq "<") {
4797 $newcomp = ">";
4798 } elsif ($comp eq "<=") {
4799 $newcomp = ">=";
4800 } elsif ($comp eq ">") {
4801 $newcomp = "<";
4802 } elsif ($comp eq ">=") {
4803 $newcomp = "<=";
4804 }
4805 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4806 }
4807 }
4808
Joe Perchesf34e4a42015-04-16 12:44:19 -07004809# Return of what appears to be an errno should normally be negative
4810 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004811 my $name = $1;
4812 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004813 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004814 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004815 }
4816 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004817
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004818# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004819 if ($line =~ /\b(if|while|for|switch)\(/) {
4820 if (ERROR("SPACING",
4821 "space required before the open parenthesis '('\n" . $herecurr) &&
4822 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004823 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004824 s/\b(if|while|for|switch)\(/$1 \(/;
4825 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004826 }
4827
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004828# Check for illegal assignment in if conditional -- and check for trailing
4829# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004830 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004831 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4832 ctx_statement_block($linenr, $realcnt, 0)
4833 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004834 my ($stat_next) = ctx_statement_block($line_nr_next,
4835 $remain_next, $off_next);
4836 $stat_next =~ s/\n./\n /g;
4837 ##print "stat<$stat> stat_next<$stat_next>\n";
4838
4839 if ($stat_next =~ /^\s*while\b/) {
4840 # If the statement carries leading newlines,
4841 # then count those as offsets.
4842 my ($whitespace) =
4843 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4844 my $offset =
4845 statement_rawlines($whitespace) - 1;
4846
4847 $suppress_whiletrailers{$line_nr_next +
4848 $offset} = 1;
4849 }
4850 }
4851 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004852 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004853 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004854 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004855
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004856 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004857 ERROR("ASSIGN_IN_IF",
4858 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004859 }
4860
4861 # Find out what is on the end of the line after the
4862 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004863 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004864 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004865 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004866 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4867 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004868 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004869 # Find out how long the conditional actually is.
4870 my @newlines = ($c =~ /\n/gs);
4871 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004872 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004873
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004874 $stat_real = raw_line($linenr, $cond_lines)
4875 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004876 if (defined($stat_real) && $cond_lines > 1) {
4877 $stat_real = "[...]\n$stat_real";
4878 }
4879
Joe Perches000d1cc12011-07-25 17:13:25 -07004880 ERROR("TRAILING_STATEMENTS",
4881 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004882 }
4883 }
4884
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004885# Check for bitwise tests written as boolean
4886 if ($line =~ /
4887 (?:
4888 (?:\[|\(|\&\&|\|\|)
4889 \s*0[xX][0-9]+\s*
4890 (?:\&\&|\|\|)
4891 |
4892 (?:\&\&|\|\|)
4893 \s*0[xX][0-9]+\s*
4894 (?:\&\&|\|\||\)|\])
4895 )/x)
4896 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004897 WARN("HEXADECIMAL_BOOLEAN_TEST",
4898 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004899 }
4900
Andy Whitcroft8905a672007-11-28 16:21:06 -08004901# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004902 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4903 my $s = $1;
4904 $s =~ s/$;//g; # Remove any comments
4905 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004906 ERROR("TRAILING_STATEMENTS",
4907 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004908 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004909 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004910# if should not continue a brace
4911 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004912 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004913 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004914 $herecurr);
4915 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004916# case and default should not have general statements after them
4917 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4918 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004919 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004920 \s*return\s+
4921 )/xg)
4922 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004923 ERROR("TRAILING_STATEMENTS",
4924 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004925 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004926
4927 # Check for }<nl>else {, these must be at the same
4928 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004929 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4930 $previndent == $indent) {
4931 if (ERROR("ELSE_AFTER_BRACE",
4932 "else should follow close brace '}'\n" . $hereprev) &&
4933 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4934 fix_delete_line($fixlinenr - 1, $prevrawline);
4935 fix_delete_line($fixlinenr, $rawline);
4936 my $fixedline = $prevrawline;
4937 $fixedline =~ s/}\s*$//;
4938 if ($fixedline !~ /^\+\s*$/) {
4939 fix_insert_line($fixlinenr, $fixedline);
4940 }
4941 $fixedline = $rawline;
4942 $fixedline =~ s/^(.\s*)else/$1} else/;
4943 fix_insert_line($fixlinenr, $fixedline);
4944 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004945 }
4946
Joe Perches8b8856f2014-08-06 16:11:14 -07004947 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4948 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004949 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4950
4951 # Find out what is on the end of the line after the
4952 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004953 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004954 $s =~ s/\n.*//g;
4955
4956 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004957 if (ERROR("WHILE_AFTER_BRACE",
4958 "while should follow close brace '}'\n" . $hereprev) &&
4959 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4960 fix_delete_line($fixlinenr - 1, $prevrawline);
4961 fix_delete_line($fixlinenr, $rawline);
4962 my $fixedline = $prevrawline;
4963 my $trailing = $rawline;
4964 $trailing =~ s/^\+//;
4965 $trailing = trim($trailing);
4966 $fixedline =~ s/}\s*$/} $trailing/;
4967 fix_insert_line($fixlinenr, $fixedline);
4968 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004969 }
4970 }
4971
Joe Perches95e2c602013-07-03 15:05:20 -07004972#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004973 while ($line =~ m{($Constant|$Lval)}g) {
4974 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004975
Joe Perches95e2c602013-07-03 15:05:20 -07004976#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004977 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004978 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004979#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004980 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004981#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004982 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4983#Ignore some three character SI units explicitly, like MiB and KHz
4984 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004985 while ($var =~ m{($Ident)}g) {
4986 my $word = $1;
4987 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004988 if ($check) {
4989 seed_camelcase_includes();
4990 if (!$file && !$camelcase_file_seeded) {
4991 seed_camelcase_file($realfile);
4992 $camelcase_file_seeded = 1;
4993 }
4994 }
Joe Perches7e781f62013-09-11 14:23:55 -07004995 if (!defined $camelcase{$word}) {
4996 $camelcase{$word} = 1;
4997 CHK("CAMELCASE",
4998 "Avoid CamelCase: <$word>\n" . $herecurr);
4999 }
Joe Perches34456862013-07-03 15:05:34 -07005000 }
Joe Perches323c1262012-12-17 16:02:07 -08005001 }
5002 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005003
5004#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005005 if ($line =~ /\#\s*define.*\\\s+$/) {
5006 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5007 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5008 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005009 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005010 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005011 }
5012
Fabian Frederick0e212e02015-04-16 12:44:25 -07005013# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5014# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005015 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005016 my $file = "$1.h";
5017 my $checkfile = "include/linux/$file";
5018 if (-f "$root/$checkfile" &&
5019 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005020 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005021 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005022 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5023 if ($asminclude > 0) {
5024 if ($realfile =~ m{^arch/}) {
5025 CHK("ARCH_INCLUDE_LINUX",
5026 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5027 } else {
5028 WARN("INCLUDE_LINUX",
5029 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5030 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005031 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005032 }
5033 }
5034
Andy Whitcroft653d4872007-06-23 17:16:34 -07005035# multi-statement macros should be enclosed in a do while loop, grab the
5036# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005037# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07005038 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5039 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005040 my $ln = $linenr;
5041 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005042 my ($off, $dstat, $dcond, $rest);
5043 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005044 my $has_flow_statement = 0;
5045 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005046 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005047 ctx_statement_block($linenr, $realcnt, 0);
5048 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005049 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005050 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005051
Joe Perches08a28432014-10-13 15:51:55 -07005052 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005053 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005054
Joe Perchesf59b64b2016-10-11 13:52:08 -07005055 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5056 my $define_args = $1;
5057 my $define_stmt = $dstat;
5058 my @def_args = ();
5059
5060 if (defined $define_args && $define_args ne "") {
5061 $define_args = substr($define_args, 1, length($define_args) - 2);
5062 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005063 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005064 @def_args = split(",", $define_args);
5065 }
5066
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005067 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005068 $dstat =~ s/\\\n.//g;
5069 $dstat =~ s/^\s*//s;
5070 $dstat =~ s/\s*$//s;
5071
5072 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005073 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5074 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005075 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005076 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005077 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005078
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005079 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005080 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5081 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005082 {
5083 }
5084
Joe Perches42e15292016-03-15 14:58:01 -07005085 # Make asm volatile uses seem like a generic function
5086 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5087
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005088 my $exceptions = qr{
5089 $Declare|
5090 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005091 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005092 DECLARE_PER_CPU|
5093 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005094 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005095 union|
5096 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005097 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005098 ^\"|\"$|
5099 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005100 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005101 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005102
5103 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005104 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005105 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005106
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005107 if ($dstat ne '' &&
5108 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5109 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005110 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005111 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005112 $dstat !~ /$exceptions/ &&
5113 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005114 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005115 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005116 $dstat !~ /^for\s*$Constant$/ && # for (...)
5117 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5118 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005119 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005120 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005121 {
Joe Perchese7955562017-05-08 15:55:48 -07005122 if ($dstat =~ /^\s*if\b/) {
5123 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5124 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5125 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005126 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5127 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5128 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005129 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005130 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005131 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005132
5133 }
Joe Perches52076492016-10-11 13:52:14 -07005134
5135 # Make $define_stmt single line, comment-free, etc
5136 my @stmt_array = split('\n', $define_stmt);
5137 my $first = 1;
5138 $define_stmt = "";
5139 foreach my $l (@stmt_array) {
5140 $l =~ s/\\$//;
5141 if ($first) {
5142 $define_stmt = $l;
5143 $first = 0;
5144 } elsif ($l =~ /^[\+ ]/) {
5145 $define_stmt .= substr($l, 1);
5146 }
5147 }
5148 $define_stmt =~ s/$;//g;
5149 $define_stmt =~ s/\s+/ /g;
5150 $define_stmt = trim($define_stmt);
5151
Joe Perchesf59b64b2016-10-11 13:52:08 -07005152# check if any macro arguments are reused (ignore '...' and 'type')
5153 foreach my $arg (@def_args) {
5154 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005155 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005156 my $tmp_stmt = $define_stmt;
5157 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5158 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5159 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005160 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005161 if ($use_cnt > 1) {
5162 CHK("MACRO_ARG_REUSE",
5163 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005164 }
5165# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005166 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005167 ((defined($1) && $1 ne ',') ||
5168 (defined($2) && $2 ne ','))) {
5169 CHK("MACRO_ARG_PRECEDENCE",
5170 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005171 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005172 }
Joe Perches5023d342012-12-17 16:01:47 -08005173
Joe Perches08a28432014-10-13 15:51:55 -07005174# check for macros with flow control, but without ## concatenation
5175# ## concatenation is commonly a macro that defines a function so ignore those
5176 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005177 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005178 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005179
Joe Perches08a28432014-10-13 15:51:55 -07005180 WARN("MACRO_WITH_FLOW_CONTROL",
5181 "Macros with flow control statements should be avoided\n" . "$herectx");
5182 }
5183
Joe Perches481eb482012-12-17 16:01:56 -08005184# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005185
5186 } else {
5187 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005188 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5189 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005190 $line =~ /^\+.*\\$/) {
5191 WARN("LINE_CONTINUATIONS",
5192 "Avoid unnecessary line continuations\n" . $herecurr);
5193 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005194 }
5195
Joe Perchesb13edf72012-07-30 14:41:24 -07005196# do {} while (0) macro tests:
5197# single-statement macros do not need to be enclosed in do while (0) loop,
5198# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005199 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005200 $realfile !~ m@/vmlinux.lds.h$@ &&
5201 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5202 my $ln = $linenr;
5203 my $cnt = $realcnt;
5204 my ($off, $dstat, $dcond, $rest);
5205 my $ctx = '';
5206 ($dstat, $dcond, $ln, $cnt, $off) =
5207 ctx_statement_block($linenr, $realcnt, 0);
5208 $ctx = $dstat;
5209
5210 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005211 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005212
5213 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5214 my $stmts = $2;
5215 my $semis = $3;
5216
5217 $ctx =~ s/\n*$//;
5218 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005219 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005220
Joe Perchesac8e97f2012-08-21 16:15:53 -07005221 if (($stmts =~ tr/;/;/) == 1 &&
5222 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005223 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5224 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5225 }
5226 if (defined $semis && $semis ne "") {
5227 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5228 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5229 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005230 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5231 $ctx =~ s/\n*$//;
5232 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005233 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005234
5235 WARN("TRAILING_SEMICOLON",
5236 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005237 }
5238 }
5239
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005240# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005241 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5242 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005243 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005244 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005245 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5246 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005247 my @allowed = ();
5248 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005249 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005250 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005251 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005252 for my $chunk (@chunks) {
5253 my ($cond, $block) = @{$chunk};
5254
Andy Whitcroft773647a2008-03-28 14:15:58 -07005255 # If the condition carries leading newlines, then count those as offsets.
5256 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5257 my $offset = statement_rawlines($whitespace) - 1;
5258
Joe Perchesaad4f612012-03-23 15:02:19 -07005259 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005260 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5261
5262 # We have looked at and allowed this specific line.
5263 $suppress_ifbraces{$ln + $offset} = 1;
5264
5265 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005266 $ln += statement_rawlines($block) - 1;
5267
Andy Whitcroft773647a2008-03-28 14:15:58 -07005268 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005269
5270 $seen++ if ($block =~ /^\s*{/);
5271
Joe Perchesaad4f612012-03-23 15:02:19 -07005272 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005273 if (statement_lines($cond) > 1) {
5274 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005275 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005276 }
5277 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005278 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005279 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005280 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005281 if (statement_block_size($block) > 1) {
5282 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005283 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005284 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005285 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005286 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005287 if ($seen) {
5288 my $sum_allowed = 0;
5289 foreach (@allowed) {
5290 $sum_allowed += $_;
5291 }
5292 if ($sum_allowed == 0) {
5293 WARN("BRACES",
5294 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5295 } elsif ($sum_allowed != $allow &&
5296 $seen != $allow) {
5297 CHK("BRACES",
5298 "braces {} should be used on all arms of this statement\n" . $herectx);
5299 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005300 }
5301 }
5302 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005303 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005304 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005305 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005306
Andy Whitcroftcf655042008-03-04 14:28:20 -08005307 # Check the pre-context.
5308 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5309 #print "APW: ALLOWED: pre<$1>\n";
5310 $allowed = 1;
5311 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005312
5313 my ($level, $endln, @chunks) =
5314 ctx_statement_full($linenr, $realcnt, $-[0]);
5315
Andy Whitcroftcf655042008-03-04 14:28:20 -08005316 # Check the condition.
5317 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005318 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005319 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005320 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005321 }
5322 if (statement_lines($cond) > 1) {
5323 #print "APW: ALLOWED: cond<$cond>\n";
5324 $allowed = 1;
5325 }
5326 if ($block =~/\b(?:if|for|while)\b/) {
5327 #print "APW: ALLOWED: block<$block>\n";
5328 $allowed = 1;
5329 }
5330 if (statement_block_size($block) > 1) {
5331 #print "APW: ALLOWED: lines block<$block>\n";
5332 $allowed = 1;
5333 }
5334 # Check the post-context.
5335 if (defined $chunks[1]) {
5336 my ($cond, $block) = @{$chunks[1]};
5337 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005338 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005339 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005340 if ($block =~ /^\s*\{/) {
5341 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5342 $allowed = 1;
5343 }
5344 }
5345 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005346 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005347 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005348
Joe Perches000d1cc12011-07-25 17:13:25 -07005349 WARN("BRACES",
5350 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005351 }
5352 }
5353
Joe Perchese4c5bab2017-02-24 15:01:41 -08005354# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005355 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5356 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005357 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5358 }
5359
Joe Perches0979ae62012-12-17 16:01:59 -08005360# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005361 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005362 if (CHK("BRACES",
5363 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5364 $fix && $prevrawline =~ /^\+/) {
5365 fix_delete_line($fixlinenr - 1, $prevrawline);
5366 }
Joe Perches0979ae62012-12-17 16:01:59 -08005367 }
Joe Perches77b9a532013-07-03 15:05:29 -07005368 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005369 if (CHK("BRACES",
5370 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5371 $fix) {
5372 fix_delete_line($fixlinenr, $rawline);
5373 }
Joe Perches0979ae62012-12-17 16:01:59 -08005374 }
5375
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005376# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005377 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5378 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005379 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005380 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005381 }
5382
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005383# Check for user-visible strings broken across lines, which breaks the ability
5384# to grep for the string. Make exceptions when the previous string ends in a
5385# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5386# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005387 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005388 $prevline =~ /"\s*$/ &&
5389 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5390 if (WARN("SPLIT_STRING",
5391 "quoted string split across lines\n" . $hereprev) &&
5392 $fix &&
5393 $prevrawline =~ /^\+.*"\s*$/ &&
5394 $last_coalesced_string_linenr != $linenr - 1) {
5395 my $extracted_string = get_quoted_string($line, $rawline);
5396 my $comma_close = "";
5397 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5398 $comma_close = $1;
5399 }
5400
5401 fix_delete_line($fixlinenr - 1, $prevrawline);
5402 fix_delete_line($fixlinenr, $rawline);
5403 my $fixedline = $prevrawline;
5404 $fixedline =~ s/"\s*$//;
5405 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5406 fix_insert_line($fixlinenr - 1, $fixedline);
5407 $fixedline = $rawline;
5408 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5409 if ($fixedline !~ /\+\s*$/) {
5410 fix_insert_line($fixlinenr, $fixedline);
5411 }
5412 $last_coalesced_string_linenr = $linenr;
5413 }
5414 }
5415
5416# check for missing a space in a string concatenation
5417 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5418 WARN('MISSING_SPACE',
5419 "break quoted strings at a space character\n" . $hereprev);
5420 }
5421
Joe Perchese4b7d302017-05-08 15:55:51 -07005422# check for an embedded function name in a string when the function is known
5423# This does not work very well for -f --file checking as it depends on patch
5424# context providing the function name or a single line form for in-file
5425# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005426 if ($line =~ /^\+.*$String/ &&
5427 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005428 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5429 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005430 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005431 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005432 }
5433
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005434# check for spaces before a quoted newline
5435 if ($rawline =~ /^.*\".*\s\\n/) {
5436 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5437 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5438 $fix) {
5439 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5440 }
5441
5442 }
5443
Joe Perchesf17dba42014-10-13 15:51:51 -07005444# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005445 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5446 if (CHK("CONCATENATED_STRING",
5447 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5448 $fix) {
5449 while ($line =~ /($String)/g) {
5450 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5451 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5452 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5453 }
5454 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005455 }
5456
Joe Perches90ad30e2014-12-10 15:51:59 -08005457# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005458 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005459 if (WARN("STRING_FRAGMENTS",
5460 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5461 $fix) {
5462 while ($line =~ /($String)(?=\s*")/g) {
5463 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5464 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5465 }
5466 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005467 }
5468
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005469# check for non-standard and hex prefixed decimal printf formats
5470 my $show_L = 1; #don't show the same defect twice
5471 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005472 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005473 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005474 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005475 # check for %L
5476 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005477 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005478 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5479 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005480 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005481 # check for %Z
5482 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5483 WARN("PRINTF_Z",
5484 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5485 $show_Z = 0;
5486 }
5487 # check for 0x<decimal>
5488 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5489 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005490 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5491 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005492 }
5493
5494# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005495 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005496 WARN("LINE_CONTINUATIONS",
5497 "Avoid line continuations in quoted strings\n" . $herecurr);
5498 }
5499
Andy Whitcroft00df3442007-06-08 13:47:06 -07005500# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005501 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005502 WARN("IF_0",
5503 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5504 }
5505
5506# warn about #if 1
5507 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5508 WARN("IF_1",
5509 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005510 }
5511
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005512# check for needless "if (<foo>) fn(<foo>)" uses
5513 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005514 my $tested = quotemeta($1);
5515 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5516 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5517 my $func = $1;
5518 if (WARN('NEEDLESS_IF',
5519 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5520 $fix) {
5521 my $do_fix = 1;
5522 my $leading_tabs = "";
5523 my $new_leading_tabs = "";
5524 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5525 $leading_tabs = $1;
5526 } else {
5527 $do_fix = 0;
5528 }
5529 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5530 $new_leading_tabs = $1;
5531 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5532 $do_fix = 0;
5533 }
5534 } else {
5535 $do_fix = 0;
5536 }
5537 if ($do_fix) {
5538 fix_delete_line($fixlinenr - 1, $prevrawline);
5539 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5540 }
5541 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005542 }
5543 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005544
Joe Perchesebfdc402014-08-06 16:10:27 -07005545# check for unnecessary "Out of Memory" messages
5546 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5547 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5548 (defined $1 || defined $3) &&
5549 $linenr > 3) {
5550 my $testval = $2;
5551 my $testline = $lines[$linenr - 3];
5552
5553 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5554# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5555
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005556 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 -07005557 WARN("OOM_MESSAGE",
5558 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5559 }
5560 }
5561
Joe Perchesf78d98f2014-10-13 15:52:01 -07005562# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005563 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005564 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5565 my $level = $1;
5566 if (WARN("UNNECESSARY_KERN_LEVEL",
5567 "Possible unnecessary $level\n" . $herecurr) &&
5568 $fix) {
5569 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5570 }
5571 }
5572
Joe Perches45c55e92017-02-24 15:01:31 -08005573# check for logging continuations
5574 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5575 WARN("LOGGING_CONTINUATION",
5576 "Avoid logging continuation uses where feasible\n" . $herecurr);
5577 }
5578
Joe Perchesabb08a52014-12-10 15:51:46 -08005579# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005580 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005581 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5582 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5583 WARN("MASK_THEN_SHIFT",
5584 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5585 }
5586
Joe Perchesb75ac612014-12-10 15:52:02 -08005587# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005588 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005589 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5590 my $val = $1;
5591 my $equal = "!";
5592 $equal = "" if ($4 eq "!=");
5593 if (CHK("COMPARISON_TO_NULL",
5594 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5595 $fix) {
5596 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5597 }
5598 }
5599 }
5600
Joe Perches8716de32013-09-11 14:24:05 -07005601# check for bad placement of section $InitAttribute (e.g.: __initdata)
5602 if ($line =~ /(\b$InitAttribute\b)/) {
5603 my $attr = $1;
5604 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5605 my $ptr = $1;
5606 my $var = $2;
5607 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5608 ERROR("MISPLACED_INIT",
5609 "$attr should be placed after $var\n" . $herecurr)) ||
5610 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5611 WARN("MISPLACED_INIT",
5612 "$attr should be placed after $var\n" . $herecurr))) &&
5613 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005614 $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 -07005615 }
5616 }
5617 }
5618
Joe Perchese970b8842013-11-12 15:10:10 -08005619# check for $InitAttributeData (ie: __initdata) with const
5620 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5621 my $attr = $1;
5622 $attr =~ /($InitAttributePrefix)(.*)/;
5623 my $attr_prefix = $1;
5624 my $attr_type = $2;
5625 if (ERROR("INIT_ATTRIBUTE",
5626 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5627 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005628 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005629 s/$InitAttributeData/${attr_prefix}initconst/;
5630 }
5631 }
5632
5633# check for $InitAttributeConst (ie: __initconst) without const
5634 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5635 my $attr = $1;
5636 if (ERROR("INIT_ATTRIBUTE",
5637 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5638 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005639 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005640 /(^\+\s*(?:static\s+))/;
5641 $lead = rtrim($1);
5642 $lead = "$lead " if ($lead !~ /^\+$/);
5643 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005644 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005645 }
5646 }
5647
Joe Perchesc17893c2015-04-16 12:44:42 -07005648# check for __read_mostly with const non-pointer (should just be const)
5649 if ($line =~ /\b__read_mostly\b/ &&
5650 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5651 if (ERROR("CONST_READ_MOSTLY",
5652 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5653 $fix) {
5654 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5655 }
5656 }
5657
Joe Perchesfbdb8132014-04-03 14:49:14 -07005658# don't use __constant_<foo> functions outside of include/uapi/
5659 if ($realfile !~ m@^include/uapi/@ &&
5660 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5661 my $constant_func = $1;
5662 my $func = $constant_func;
5663 $func =~ s/^__constant_//;
5664 if (WARN("CONSTANT_CONVERSION",
5665 "$constant_func should be $func\n" . $herecurr) &&
5666 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005667 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005668 }
5669 }
5670
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005671# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005672 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005673 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005674 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005675 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005676 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005677 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5678 }
5679 if ($delay > 2000) {
5680 WARN("LONG_UDELAY",
5681 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005682 }
5683 }
5684
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005685# warn about unexpectedly long msleep's
5686 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5687 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005688 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005689 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005690 }
5691 }
5692
Joe Perches36ec1932013-07-03 15:05:25 -07005693# check for comparisons of jiffies
5694 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5695 WARN("JIFFIES_COMPARISON",
5696 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5697 }
5698
Joe Perches9d7a34a2013-07-03 15:05:26 -07005699# check for comparisons of get_jiffies_64()
5700 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5701 WARN("JIFFIES_COMPARISON",
5702 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5703 }
5704
Andy Whitcroft00df3442007-06-08 13:47:06 -07005705# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005706# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005707# print "#ifdef in C files should be avoided\n";
5708# print "$herecurr";
5709# $clean = 0;
5710# }
5711
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005712# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005713 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005714 if (ERROR("SPACING",
5715 "exactly one space required after that #$1\n" . $herecurr) &&
5716 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005717 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005718 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5719 }
5720
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005721 }
5722
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005723# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005724 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5725 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005726 my $which = $1;
5727 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005728 CHK("UNCOMMENTED_DEFINITION",
5729 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005730 }
5731 }
5732# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005733
5734 my $barriers = qr{
5735 mb|
5736 rmb|
5737 wmb|
5738 read_barrier_depends
5739 }x;
5740 my $barrier_stems = qr{
5741 mb__before_atomic|
5742 mb__after_atomic|
5743 store_release|
5744 load_acquire|
5745 store_mb|
5746 (?:$barriers)
5747 }x;
5748 my $all_barriers = qr{
5749 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005750 smp_(?:$barrier_stems)|
5751 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005752 }x;
5753
5754 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005755 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005756 WARN("MEMORY_BARRIER",
5757 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005758 }
5759 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005760
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005761 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5762
5763 if ($realfile !~ m@^include/asm-generic/@ &&
5764 $realfile !~ m@/barrier\.h$@ &&
5765 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5766 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5767 WARN("MEMORY_BARRIER",
5768 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5769 }
5770
Joe Perchescb426e92015-06-25 15:02:46 -07005771# check for waitqueue_active without a comment.
5772 if ($line =~ /\bwaitqueue_active\s*\(/) {
5773 if (!ctx_has_comment($first_line, $linenr)) {
5774 WARN("WAITQUEUE_ACTIVE",
5775 "waitqueue_active without comment\n" . $herecurr);
5776 }
5777 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005778
Paul E. McKenney91db2592017-11-27 09:37:35 -08005779# check for smp_read_barrier_depends and read_barrier_depends
5780 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5781 WARN("READ_BARRIER_DEPENDS",
5782 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5783 }
5784
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005785# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005786 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005787 CHK("ARCH_DEFINES",
5788 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005789 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005790
Joe Perches596ed452017-07-12 14:37:02 -07005791# check that the storage class is not after a type
5792 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005793 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005794 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5795 }
5796# Check that the storage class is at the beginning of a declaration
5797 if ($line =~ /\b$Storage\b/ &&
5798 $line !~ /^.\s*$Storage/ &&
5799 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5800 $1 !~ /[\,\)]\s*$/) {
5801 WARN("STORAGE_CLASS",
5802 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005803 }
5804
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005805# check the location of the inline attribute, that it is between
5806# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005807 if ($line =~ /\b$Type\s+$Inline\b/ ||
5808 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005809 ERROR("INLINE_LOCATION",
5810 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005811 }
5812
Andy Whitcroft8905a672007-11-28 16:21:06 -08005813# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005814 if ($realfile !~ m@\binclude/uapi/@ &&
5815 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005816 if (WARN("INLINE",
5817 "plain inline is preferred over $1\n" . $herecurr) &&
5818 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005819 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005820
5821 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005822 }
5823
Joe Perches3d130fd2011-01-12 17:00:00 -08005824# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005825 if ($realfile !~ m@\binclude/uapi/@ &&
5826 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005827 WARN("PREFER_PACKED",
5828 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005829 }
5830
Joe Perches39b7e282011-07-25 17:13:24 -07005831# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005832 if ($realfile !~ m@\binclude/uapi/@ &&
5833 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005834 WARN("PREFER_ALIGNED",
5835 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005836 }
5837
Joe Perches5f14d3b2012-01-10 15:09:52 -08005838# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005839 if ($realfile !~ m@\binclude/uapi/@ &&
5840 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005841 if (WARN("PREFER_PRINTF",
5842 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5843 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005844 $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 -07005845
5846 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005847 }
5848
Joe Perches6061d942012-03-23 15:02:16 -07005849# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005850 if ($realfile !~ m@\binclude/uapi/@ &&
5851 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005852 if (WARN("PREFER_SCANF",
5853 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5854 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005855 $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 -07005856 }
Joe Perches6061d942012-03-23 15:02:16 -07005857 }
5858
Joe Perches619a9082014-12-10 15:51:35 -08005859# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005860 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005861 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5862 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5863 $line =~ /\b__weak\b/)) {
5864 ERROR("WEAK_DECLARATION",
5865 "Using weak declarations can have unintended link defects\n" . $herecurr);
5866 }
5867
Tomas Winklerfd39f902016-12-12 16:46:34 -08005868# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005869 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005870 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005871 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5872 my $type = $1;
5873 if ($type =~ /\b($typeC99Typedefs)\b/) {
5874 $type = $1;
5875 my $kernel_type = 'u';
5876 $kernel_type = 's' if ($type =~ /^_*[si]/);
5877 $type =~ /(\d+)/;
5878 $kernel_type .= $1;
5879 if (CHK("PREFER_KERNEL_TYPES",
5880 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5881 $fix) {
5882 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5883 }
5884 }
5885 }
5886
Joe Perches938224b2016-01-20 14:59:15 -08005887# check for cast of C90 native int or longer types constants
5888 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5889 my $cast = $1;
5890 my $const = $2;
5891 if (WARN("TYPECAST_INT_CONSTANT",
5892 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5893 $fix) {
5894 my $suffix = "";
5895 my $newconst = $const;
5896 $newconst =~ s/${Int_type}$//;
5897 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5898 if ($cast =~ /\blong\s+long\b/) {
5899 $suffix .= 'LL';
5900 } elsif ($cast =~ /\blong\b/) {
5901 $suffix .= 'L';
5902 }
5903 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5904 }
5905 }
5906
Joe Perches8f53a9b2010-03-05 13:43:48 -08005907# check for sizeof(&)
5908 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005909 WARN("SIZEOF_ADDRESS",
5910 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005911 }
5912
Joe Perches66c80b62012-07-30 14:41:22 -07005913# check for sizeof without parenthesis
5914 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005915 if (WARN("SIZEOF_PARENTHESIS",
5916 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5917 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005918 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005919 }
Joe Perches66c80b62012-07-30 14:41:22 -07005920 }
5921
Joe Perches88982fe2012-12-17 16:02:00 -08005922# check for struct spinlock declarations
5923 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5924 WARN("USE_SPINLOCK_T",
5925 "struct spinlock should be spinlock_t\n" . $herecurr);
5926 }
5927
Joe Perchesa6962d72013-04-29 16:18:13 -07005928# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005929 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005930 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005931 $fmt =~ s/%%//g;
5932 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005933 if (WARN("PREFER_SEQ_PUTS",
5934 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5935 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005936 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005937 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005938 }
5939 }
5940
Joe Perches478b1792018-04-10 16:33:34 -07005941# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07005942 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07005943 defined $stat &&
5944 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5945 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005946 my $stat_real;
5947
Joe Perches0b523762017-05-08 15:55:36 -07005948 my $lc = $stat =~ tr@\n@@;
5949 $lc = $lc + $linenr;
5950 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07005951 my $specifier;
5952 my $extension;
5953 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07005954 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5955 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005956
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005957 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5958 $specifier = $1;
5959 $extension = $2;
5960 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5961 $bad_specifier = $specifier;
5962 last;
5963 }
5964 if ($extension eq "x" && !defined($stat_real)) {
5965 if (!defined($stat_real)) {
5966 $stat_real = get_stat_real($linenr, $lc);
5967 }
5968 WARN("VSPRINTF_SPECIFIER_PX",
5969 "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");
5970 }
5971 }
5972 if ($bad_specifier ne "") {
5973 my $stat_real = get_stat_real($linenr, $lc);
5974 my $ext_type = "Invalid";
5975 my $use = "";
5976 if ($bad_specifier =~ /p[Ff]/) {
5977 $ext_type = "Deprecated";
5978 $use = " - use %pS instead";
5979 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5980 }
5981
5982 WARN("VSPRINTF_POINTER_EXTENSION",
5983 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5984 }
Joe Perches0b523762017-05-08 15:55:36 -07005985 }
5986 }
5987
Andy Whitcroft554e1652012-01-10 15:09:57 -08005988# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07005989 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005990 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005991 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005992
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005993 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005994 my $ms_val = $7;
5995 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005996
Andy Whitcroft554e1652012-01-10 15:09:57 -08005997 if ($ms_size =~ /^(0x|)0$/i) {
5998 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005999 "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 -08006000 } elsif ($ms_size =~ /^(0x|)1$/i) {
6001 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006002 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6003 }
6004 }
6005
Joe Perches98a9bba2014-01-23 15:54:52 -08006006# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006007# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006008# defined $stat &&
6009# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6010# if (WARN("PREFER_ETHER_ADDR_COPY",
6011# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6012# $fix) {
6013# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6014# }
6015# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006016
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006017# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006018# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006019# defined $stat &&
6020# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6021# WARN("PREFER_ETHER_ADDR_EQUAL",
6022# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6023# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006024
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006025# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6026# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006027# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006028# defined $stat &&
6029# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6030#
6031# my $ms_val = $7;
6032#
6033# if ($ms_val =~ /^(?:0x|)0+$/i) {
6034# if (WARN("PREFER_ETH_ZERO_ADDR",
6035# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6036# $fix) {
6037# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6038# }
6039# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6040# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6041# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6042# $fix) {
6043# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6044# }
6045# }
6046# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006047
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006048# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006049 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006050 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006051 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006052 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006053 my $call = $1;
6054 my $cast1 = deparenthesize($2);
6055 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006056 my $cast2 = deparenthesize($7);
6057 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006058 my $cast;
6059
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006060 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006061 $cast = "$cast1 or $cast2";
6062 } elsif ($cast1 ne "") {
6063 $cast = $cast1;
6064 } else {
6065 $cast = $cast2;
6066 }
6067 WARN("MINMAX",
6068 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006069 }
6070 }
6071
Joe Perches4a273192012-07-30 14:41:20 -07006072# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006073 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006074 defined $stat &&
6075 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6076 my $min = $1;
6077 my $max = $7;
6078 if ($min eq $max) {
6079 WARN("USLEEP_RANGE",
6080 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6081 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6082 $min > $max) {
6083 WARN("USLEEP_RANGE",
6084 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6085 }
6086 }
6087
Joe Perches823b7942013-11-12 15:10:15 -08006088# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006089 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006090 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006091 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006092 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6093 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6094 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6095 my $lc = $stat =~ tr@\n@@;
6096 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006097 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006098 WARN("NAKED_SSCANF",
6099 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6100 }
6101
Joe Perchesafc819a2014-06-04 16:12:08 -07006102# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006103 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006104 defined $stat &&
6105 $line =~ /\bsscanf\b/) {
6106 my $lc = $stat =~ tr@\n@@;
6107 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006108 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006109 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6110 my $format = $6;
6111 my $count = $format =~ tr@%@%@;
6112 if ($count == 1 &&
6113 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6114 WARN("SSCANF_TO_KSTRTO",
6115 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6116 }
6117 }
6118 }
6119
Joe Perches70dc8a42013-09-11 14:23:58 -07006120# check for new externs in .h files.
6121 if ($realfile =~ /\.h$/ &&
6122 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006123 if (CHK("AVOID_EXTERNS",
6124 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006125 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006126 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006127 }
6128 }
6129
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006130# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006131 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006132 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006133 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006134 my $function_name = $1;
6135 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006136
6137 my $s = $stat;
6138 if (defined $cond) {
6139 substr($s, 0, length($cond), '');
6140 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006141 if ($s =~ /^\s*;/ &&
6142 $function_name ne 'uninitialized_var')
6143 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006144 WARN("AVOID_EXTERNS",
6145 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006146 }
6147
6148 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006149 WARN("FUNCTION_ARGUMENTS",
6150 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006151 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006152
6153 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6154 $stat =~ /^.\s*extern\s+/)
6155 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006156 WARN("AVOID_EXTERNS",
6157 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006158 }
6159
Joe Perchesa0ad7592017-07-10 15:52:19 -07006160# check for function declarations that have arguments without identifier names
6161 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006162 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006163 $1 ne "void") {
6164 my $args = trim($1);
6165 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6166 my $arg = trim($1);
6167 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6168 WARN("FUNCTION_ARGUMENTS",
6169 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6170 }
6171 }
6172 }
6173
Joe Perchesa0ad7592017-07-10 15:52:19 -07006174# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006175 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006176 defined $stat &&
6177 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6178 $context_function = $1;
6179
6180# check for multiline function definition with misplaced open brace
6181 my $ok = 0;
6182 my $cnt = statement_rawlines($stat);
6183 my $herectx = $here . "\n";
6184 for (my $n = 0; $n < $cnt; $n++) {
6185 my $rl = raw_line($linenr, $n);
6186 $herectx .= $rl . "\n";
6187 $ok = 1 if ($rl =~ /^[ \+]\{/);
6188 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6189 last if $rl =~ /^[ \+].*\{/;
6190 }
6191 if (!$ok) {
6192 ERROR("OPEN_BRACE",
6193 "open brace '{' following function definitions go on the next line\n" . $herectx);
6194 }
6195 }
6196
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006197# checks for new __setup's
6198 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6199 my $name = $1;
6200
6201 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006202 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006203 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006204 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006205 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006206
6207# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006208 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006209 WARN("UNNECESSARY_CASTS",
6210 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006211 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006212
Joe Perchesa640d252013-07-03 15:05:21 -07006213# alloc style
6214# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006215 if ($perl_version_ok &&
Joe Perchesa640d252013-07-03 15:05:21 -07006216 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6217 CHK("ALLOC_SIZEOF_STRUCT",
6218 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6219 }
6220
Joe Perches60a55362014-06-04 16:12:07 -07006221# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006222 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006223 defined $stat &&
6224 $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 -07006225 my $oldfunc = $3;
6226 my $a1 = $4;
6227 my $a2 = $10;
6228 my $newfunc = "kmalloc_array";
6229 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006230 my $r1 = $a1;
6231 my $r2 = $a2;
6232 if ($a1 =~ /^sizeof\s*\S/) {
6233 $r1 = $a2;
6234 $r2 = $a1;
6235 }
6236 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6237 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006238 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006239 my $herectx = get_stat_here($linenr, $cnt, $here);
6240
Joe Perches60a55362014-06-04 16:12:07 -07006241 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006242 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6243 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006244 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006245 $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 -07006246 }
6247 }
6248 }
6249
Joe Perches972fdea2013-04-29 16:18:12 -07006250# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006251 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006252 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6253 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006254 WARN("KREALLOC_ARG_REUSE",
6255 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6256 }
6257
Joe Perches5ce59ae2013-02-21 16:44:18 -08006258# check for alloc argument mismatch
6259 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6260 WARN("ALLOC_ARRAY_ARGS",
6261 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6262 }
6263
Joe Perchescaf2a542011-01-12 16:59:56 -08006264# check for multiple semicolons
6265 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006266 if (WARN("ONE_SEMICOLON",
6267 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6268 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006269 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006270 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006271 }
6272
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006273# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6274 if ($realfile !~ m@^include/uapi/@ &&
6275 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006276 my $ull = "";
6277 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6278 if (CHK("BIT_MACRO",
6279 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6280 $fix) {
6281 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6282 }
6283 }
6284
Joe Perches2d632742016-05-20 17:04:00 -07006285# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6286 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6287 my $config = $1;
6288 if (WARN("PREFER_IS_ENABLED",
6289 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6290 $fix) {
6291 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6292 }
6293 }
6294
Joe Perchese81f2392014-08-06 16:11:25 -07006295# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006296 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6297 my $has_break = 0;
6298 my $has_statement = 0;
6299 my $count = 0;
6300 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006301 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006302 $prevline--;
6303 my $rline = $rawlines[$prevline - 1];
6304 my $fline = $lines[$prevline - 1];
6305 last if ($fline =~ /^\@\@/);
6306 next if ($fline =~ /^\-/);
6307 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6308 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6309 next if ($fline =~ /^.[\s$;]*$/);
6310 $has_statement = 1;
6311 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006312 $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 -08006313 }
6314 if (!$has_break && $has_statement) {
6315 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006316 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006317 }
6318 }
6319
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006320# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006321 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006322 defined $stat &&
6323 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006324 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006325 my $herectx = get_stat_here($linenr, $cnt, $here);
6326
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006327 WARN("DEFAULT_NO_BREAK",
6328 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006329 }
6330
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006331# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006332 if ($line =~ /\b__FUNCTION__\b/) {
6333 if (WARN("USE_FUNC",
6334 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6335 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006336 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006337 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006338 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006339
Joe Perches62ec8182015-02-13 14:38:18 -08006340# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6341 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6342 ERROR("DATE_TIME",
6343 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6344 }
6345
Joe Perches2c924882012-03-23 15:02:20 -07006346# check for use of yield()
6347 if ($line =~ /\byield\s*\(\s*\)/) {
6348 WARN("YIELD",
6349 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6350 }
6351
Joe Perches179f8f42013-07-03 15:05:30 -07006352# check for comparisons against true and false
6353 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6354 my $lead = $1;
6355 my $arg = $2;
6356 my $test = $3;
6357 my $otype = $4;
6358 my $trail = $5;
6359 my $op = "!";
6360
6361 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6362
6363 my $type = lc($otype);
6364 if ($type =~ /^(?:true|false)$/) {
6365 if (("$test" eq "==" && "$type" eq "true") ||
6366 ("$test" eq "!=" && "$type" eq "false")) {
6367 $op = "";
6368 }
6369
6370 CHK("BOOL_COMPARISON",
6371 "Using comparison to $otype is error prone\n" . $herecurr);
6372
6373## maybe suggesting a correct construct would better
6374## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6375
6376 }
6377 }
6378
Joe Perches5d430902018-04-10 16:34:25 -07006379# check for bool bitfields
6380 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6381 WARN("BOOL_BITFIELD",
6382 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6383 }
6384
Joe Perchesd7295932018-08-21 21:57:26 -07006385# check for bool use in .h files
6386 if ($realfile =~ /\.h$/ &&
6387 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6388 CHK("BOOL_MEMBER",
6389 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6390 }
6391
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006392# check for semaphores initialized locked
6393 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006394 WARN("CONSIDER_COMPLETION",
6395 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006396 }
Joe Perches6712d852012-03-23 15:02:20 -07006397
Joe Perches67d0a072011-10-31 17:13:10 -07006398# recommend kstrto* over simple_strto* and strict_strto*
6399 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006400 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006401 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006402 }
Joe Perches6712d852012-03-23 15:02:20 -07006403
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006404# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006405 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006406 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006407 "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 -07006408 }
Joe Perches6712d852012-03-23 15:02:20 -07006409
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006410# check for spin_is_locked(), suggest lockdep instead
6411 if ($line =~ /\bspin_is_locked\(/) {
6412 WARN("USE_LOCKDEP",
6413 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6414 }
6415
Joe Perches9189c7e2018-09-07 15:26:18 -07006416# check for deprecated apis
6417 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6418 my $deprecated_api = $1;
6419 my $new_api = $deprecated_apis{$deprecated_api};
6420 WARN("DEPRECATED_API",
6421 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6422 }
6423
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006424# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006425# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006426 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006427 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006428 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006429 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006430 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006431
6432# use of NR_CPUS is usually wrong
6433# ignore definitions of NR_CPUS and usage to define arrays as likely right
6434 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006435 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6436 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006437 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6438 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6439 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006440 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006441 WARN("NR_CPUS",
6442 "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 -07006443 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006444
Joe Perches52ea8502013-11-12 15:10:09 -08006445# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6446 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6447 ERROR("DEFINE_ARCH_HAS",
6448 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6449 }
6450
Joe Perchesacd93622015-02-13 14:38:38 -08006451# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006452 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006453 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6454 WARN("LIKELY_MISUSE",
6455 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6456 }
6457
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006458# whine mightly about in_atomic
6459 if ($line =~ /\bin_atomic\s*\(/) {
6460 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006461 ERROR("IN_ATOMIC",
6462 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006463 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006464 WARN("IN_ATOMIC",
6465 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006466 }
6467 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006468
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006469# check for mutex_trylock_recursive usage
6470 if ($line =~ /mutex_trylock_recursive/) {
6471 ERROR("LOCKING",
6472 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6473 }
6474
Peter Zijlstra1704f472010-03-19 01:37:42 +01006475# check for lockdep_set_novalidate_class
6476 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6477 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6478 if ($realfile !~ m@^kernel/lockdep@ &&
6479 $realfile !~ m@^include/linux/lockdep@ &&
6480 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006481 ERROR("LOCKDEP",
6482 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006483 }
6484 }
Dave Jones88f88312011-01-12 16:59:59 -08006485
Joe Perchesb392c642015-04-16 12:44:16 -07006486 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6487 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006488 WARN("EXPORTED_WORLD_WRITABLE",
6489 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006490 }
Joe Perches24358802014-04-03 14:49:13 -07006491
Joe Perches00180462018-02-06 15:38:55 -08006492# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6493# and whether or not function naming is typical and if
6494# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006495 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006496 defined $stat &&
6497 $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*\)/) {
6498 my $var = $1;
6499 my $perms = $2;
6500 my $show = $3;
6501 my $store = $4;
6502 my $octal_perms = perms_to_octal($perms);
6503 if ($show =~ /^${var}_show$/ &&
6504 $store =~ /^${var}_store$/ &&
6505 $octal_perms eq "0644") {
6506 if (WARN("DEVICE_ATTR_RW",
6507 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6508 $fix) {
6509 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6510 }
6511 } elsif ($show =~ /^${var}_show$/ &&
6512 $store =~ /^NULL$/ &&
6513 $octal_perms eq "0444") {
6514 if (WARN("DEVICE_ATTR_RO",
6515 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6516 $fix) {
6517 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6518 }
6519 } elsif ($show =~ /^NULL$/ &&
6520 $store =~ /^${var}_store$/ &&
6521 $octal_perms eq "0200") {
6522 if (WARN("DEVICE_ATTR_WO",
6523 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6524 $fix) {
6525 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6526 }
6527 } elsif ($octal_perms eq "0644" ||
6528 $octal_perms eq "0444" ||
6529 $octal_perms eq "0200") {
6530 my $newshow = "$show";
6531 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6532 my $newstore = $store;
6533 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6534 my $rename = "";
6535 if ($show ne $newshow) {
6536 $rename .= " '$show' to '$newshow'";
6537 }
6538 if ($store ne $newstore) {
6539 $rename .= " '$store' to '$newstore'";
6540 }
6541 WARN("DEVICE_ATTR_FUNCTIONS",
6542 "Consider renaming function(s)$rename\n" . $herecurr);
6543 } else {
6544 WARN("DEVICE_ATTR_PERMS",
6545 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6546 }
6547 }
6548
Joe Perches515a2352014-04-03 14:49:24 -07006549# Mode permission misuses where it seems decimal should be octal
6550# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006551# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6552# specific definition of not visible in sysfs.
6553# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6554# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006555 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006556 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006557 $line =~ /$mode_perms_search/) {
6558 foreach my $entry (@mode_permission_funcs) {
6559 my $func = $entry->[0];
6560 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006561
Joe Perches459cf0a2016-10-11 13:52:19 -07006562 my $lc = $stat =~ tr@\n@@;
6563 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006564 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006565
Joe Perches515a2352014-04-03 14:49:24 -07006566 my $skip_args = "";
6567 if ($arg_pos > 1) {
6568 $arg_pos--;
6569 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6570 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006571 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006572 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006573 my $val = $1;
6574 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006575 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6576 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6577 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006578 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006579 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006580 }
6581 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006582 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006583 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006584 }
Joe Perches24358802014-04-03 14:49:13 -07006585 }
6586 }
6587 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006588
Joe Perches459cf0a2016-10-11 13:52:19 -07006589# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006590 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006591 my $oval = $1;
6592 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006593 if (WARN("SYMBOLIC_PERMS",
6594 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6595 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006596 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006597 }
6598 }
6599
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006600# validate content of MODULE_LICENSE against list from include/linux/module.h
6601 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6602 my $extracted_string = get_quoted_string($line, $rawline);
6603 my $valid_licenses = qr{
6604 GPL|
6605 GPL\ v2|
6606 GPL\ and\ additional\ rights|
6607 Dual\ BSD/GPL|
6608 Dual\ MIT/GPL|
6609 Dual\ MPL/GPL|
6610 Proprietary
6611 }x;
6612 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6613 WARN("MODULE_LICENSE",
6614 "unknown module license " . $extracted_string . "\n" . $herecurr);
6615 }
6616 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006617 }
6618
6619 # If we have no input at all, then there is nothing to report on
6620 # so just keep quiet.
6621 if ($#rawlines == -1) {
6622 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006623 }
6624
Andy Whitcroft8905a672007-11-28 16:21:06 -08006625 # In mailback mode only produce a report in the negative, for
6626 # things that appear to be patches.
6627 if ($mailback && ($clean == 1 || !$is_patch)) {
6628 exit(0);
6629 }
6630
6631 # This is not a patch, and we are are in 'no-patch' mode so
6632 # just keep quiet.
6633 if (!$chk_patch && !$is_patch) {
6634 exit(0);
6635 }
6636
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006637 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006638 ERROR("NOT_UNIFIED_DIFF",
6639 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006640 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006641 if ($is_patch && $has_commit_log && $chk_signoff) {
6642 if ($signoff == 0) {
6643 ERROR("MISSING_SIGN_OFF",
6644 "Missing Signed-off-by: line(s)\n");
6645 } elsif (!$authorsignoff) {
6646 WARN("NO_AUTHOR_SIGN_OFF",
6647 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6648 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006649 }
6650
Andy Whitcroft8905a672007-11-28 16:21:06 -08006651 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006652 if ($summary && !($clean == 1 && $quiet == 1)) {
6653 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006654 print "total: $cnt_error errors, $cnt_warn warnings, " .
6655 (($check)? "$cnt_chk checks, " : "") .
6656 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006657 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006658
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006659 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006660 # If there were any defects found and not already fixing them
6661 if (!$clean and !$fix) {
6662 print << "EOM"
6663
6664NOTE: For some of the reported defects, checkpatch may be able to
6665 mechanically convert to the typical style using --fix or --fix-inplace.
6666EOM
6667 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006668 # If there were whitespace errors which cleanpatch can fix
6669 # then suggest that.
6670 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006671 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006672 print << "EOM"
6673
6674NOTE: Whitespace errors detected.
6675 You may wish to use scripts/cleanpatch or scripts/cleanfile
6676EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006677 }
6678 }
6679
Joe Perchesd752fcc2014-08-06 16:11:05 -07006680 if ($clean == 0 && $fix &&
6681 ("@rawlines" ne "@fixed" ||
6682 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006683 my $newfile = $filename;
6684 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006685 my $linecount = 0;
6686 my $f;
6687
Joe Perchesd752fcc2014-08-06 16:11:05 -07006688 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6689
Joe Perches3705ce52013-07-03 15:05:31 -07006690 open($f, '>', $newfile)
6691 or die "$P: Can't open $newfile for write\n";
6692 foreach my $fixed_line (@fixed) {
6693 $linecount++;
6694 if ($file) {
6695 if ($linecount > 3) {
6696 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006697 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006698 }
6699 } else {
6700 print $f $fixed_line . "\n";
6701 }
6702 }
6703 close($f);
6704
6705 if (!$quiet) {
6706 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006707
Joe Perches3705ce52013-07-03 15:05:31 -07006708Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6709
6710Do _NOT_ trust the results written to this file.
6711Do _NOT_ submit these changes without inspecting them for correctness.
6712
6713This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6714No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006715EOM
6716 }
6717 }
6718
Joe Perchesd8469f12015-06-25 15:03:00 -07006719 if ($quiet == 0) {
6720 print "\n";
6721 if ($clean == 1) {
6722 print "$vname has no obvious style problems and is ready for submission.\n";
6723 } else {
6724 print "$vname has style problems, please review.\n";
6725 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006726 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006727 return $clean;
6728}