blob: b737ca9d720441989b52d066130db5157a4172f7 [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
3040 if ($comment !~ /^$/ &&
3041 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003042 WARN("SPDX_LICENSE_TAG",
3043 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3044 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3045 my $spdx_license = $1;
3046 if (!is_SPDX_License_valid($spdx_license)) {
3047 WARN("SPDX_LICENSE_TAG",
3048 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3049 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003050 }
3051 }
3052 }
3053
Andy Whitcroft5368df202008-10-15 22:02:27 -07003054# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003055 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003056
Joe Perches47e0c882015-06-25 15:02:57 -07003057# line length limit (with some exclusions)
3058#
3059# There are a few types of lines that may extend beyond $max_line_length:
3060# logging functions like pr_info that end in a string
3061# lines with a single string
3062# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003063# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003064#
3065# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003066# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003067# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3068# LONG_LINE all other lines longer than $max_line_length
3069#
3070# if LONG_LINE is ignored, the other 2 types are also ignored
3071#
3072
Joe Perchesb4749e92015-07-17 16:24:01 -07003073 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003074 my $msg_type = "LONG_LINE";
3075
3076 # Check the allowed long line types first
3077
3078 # logging functions that end in a string that starts
3079 # before $max_line_length
3080 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3081 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3082 $msg_type = "";
3083
3084 # lines with only strings (w/ possible termination)
3085 # #defines with only strings
3086 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3087 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3088 $msg_type = "";
3089
Joe Perchescc147502017-11-17 15:28:44 -08003090 # More special cases
3091 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3092 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003093 $msg_type = "";
3094
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003095 # URL ($rawline is used in case the URL is in a comment)
3096 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3097 $msg_type = "";
3098
Joe Perches47e0c882015-06-25 15:02:57 -07003099 # Otherwise set the alternate message types
3100
3101 # a comment starts before $max_line_length
3102 } elsif ($line =~ /($;[\s$;]*)$/ &&
3103 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3104 $msg_type = "LONG_LINE_COMMENT"
3105
3106 # a quoted string starts before $max_line_length
3107 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3108 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3109 $msg_type = "LONG_LINE_STRING"
3110 }
3111
3112 if ($msg_type ne "" &&
3113 (show_type("LONG_LINE") || show_type($msg_type))) {
3114 WARN($msg_type,
3115 "line over $max_line_length characters\n" . $herecurr);
3116 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003117 }
3118
Andy Whitcroft8905a672007-11-28 16:21:06 -08003119# check for adding lines without a newline.
3120 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003121 WARN("MISSING_EOF_NEWLINE",
3122 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003123 }
3124
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003125# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003126 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003127
3128# at the beginning of a line any tabs must come first and anything
3129# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003130 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3131 $rawline =~ /^\+\s* \s*/) {
3132 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003133 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003134 if (ERROR("CODE_INDENT",
3135 "code indent should use tabs where possible\n" . $herevet) &&
3136 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003137 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003138 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003139 }
3140
Alberto Panizzo08e44362010-03-05 13:43:54 -08003141# check for space before tabs.
3142 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3143 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003144 if (WARN("SPACE_BEFORE_TAB",
3145 "please, no space before tabs\n" . $herevet) &&
3146 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003147 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003148 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003149 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003150 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003151 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003152 }
3153
Joe Perches6a487212018-04-10 16:34:04 -07003154# check for assignments on the start of a line
3155 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3156 CHK("ASSIGNMENT_CONTINUATIONS",
3157 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3158 }
3159
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003160# check for && or || at the start of a line
3161 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3162 CHK("LOGICAL_CONTINUATIONS",
3163 "Logical continuations should be on the previous line\n" . $hereprev);
3164 }
3165
Joe Perchesa91e8992016-05-20 17:04:05 -07003166# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003167 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003168 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003169 my $indent = length($1);
3170 if ($indent % 8) {
3171 if (WARN("TABSTOP",
3172 "Statements should start on a tabstop\n" . $herecurr) &&
3173 $fix) {
3174 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3175 }
3176 }
3177 }
3178
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003179# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003180 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003181 $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 -07003182 $prevline =~ /^\+(\t*)(.*)$/;
3183 my $oldindent = $1;
3184 my $rest = $2;
3185
3186 my $pos = pos_last_openparen($rest);
3187 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003188 $line =~ /^(\+| )([ \t]*)/;
3189 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003190
3191 my $goodtabindent = $oldindent .
3192 "\t" x ($pos / 8) .
3193 " " x ($pos % 8);
3194 my $goodspaceindent = $oldindent . " " x $pos;
3195
3196 if ($newindent ne $goodtabindent &&
3197 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003198
3199 if (CHK("PARENTHESIS_ALIGNMENT",
3200 "Alignment should match open parenthesis\n" . $hereprev) &&
3201 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003202 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003203 s/^\+[ \t]*/\+$goodtabindent/;
3204 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003205 }
3206 }
3207 }
3208
Joe Perches6ab3a972015-04-16 12:44:05 -07003209# check for space after cast like "(int) foo" or "(struct foo) bar"
3210# avoid checking a few false positives:
3211# "sizeof(<type>)" or "__alignof__(<type>)"
3212# function pointer declarations like "(*foo)(int) = bar;"
3213# structure definitions like "(struct foo) { 0 };"
3214# multiline macros that define functions
3215# known attributes or the __attribute__ keyword
3216 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3217 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003218 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003219 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003220 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003221 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003222 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003223 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003224 }
3225
Joe Perches86406b12015-09-09 15:37:41 -07003226# Block comment styles
3227# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003228 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003229 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003230 $rawline =~ /^\+[ \t]*\*/ &&
3231 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003232 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3233 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3234 }
3235
Joe Perches86406b12015-09-09 15:37:41 -07003236# Block comments use * on subsequent lines
3237 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3238 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003239 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003240 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003241 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003242 WARN("BLOCK_COMMENT_STYLE",
3243 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003244 }
3245
Joe Perches86406b12015-09-09 15:37:41 -07003246# Block comments use */ on trailing lines
3247 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003248 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3249 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3250 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003251 WARN("BLOCK_COMMENT_STYLE",
3252 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003253 }
3254
Joe Perches08eb9b82016-10-11 13:51:50 -07003255# Block comment * alignment
3256 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003257 $line =~ /^\+[ \t]*$;/ && #leading comment
3258 $rawline =~ /^\+[ \t]*\*/ && #leading *
3259 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003260 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003261 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3262 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003263 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003264 if (defined($1)) {
3265 $oldindent = expand_tabs($1);
3266 } else {
3267 $prevrawline =~ m@^\+(.*/?)\*@;
3268 $oldindent = expand_tabs($1);
3269 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003270 $rawline =~ m@^\+([ \t]*)\*@;
3271 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003272 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003273 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003274 WARN("BLOCK_COMMENT_STYLE",
3275 "Block comments should align the * on each line\n" . $hereprev);
3276 }
3277 }
3278
Joe Perches7f619192014-08-06 16:10:39 -07003279# check for missing blank lines after struct/union declarations
3280# with exceptions for various attributes and macros
3281 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3282 $line =~ /^\+/ &&
3283 !($line =~ /^\+\s*$/ ||
3284 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3285 $line =~ /^\+\s*MODULE_/i ||
3286 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3287 $line =~ /^\+[a-z_]*init/ ||
3288 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3289 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003290 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003291 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003292 if (CHK("LINE_SPACING",
3293 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3294 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003295 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003296 }
Joe Perches7f619192014-08-06 16:10:39 -07003297 }
3298
Joe Perches365dd4e2014-08-06 16:10:42 -07003299# check for multiple consecutive blank lines
3300 if ($prevline =~ /^[\+ ]\s*$/ &&
3301 $line =~ /^\+\s*$/ &&
3302 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003303 if (CHK("LINE_SPACING",
3304 "Please don't use multiple blank lines\n" . $hereprev) &&
3305 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003306 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003307 }
3308
Joe Perches365dd4e2014-08-06 16:10:42 -07003309 $last_blank_line = $linenr;
3310 }
3311
Joe Perches3b617e32014-04-03 14:49:28 -07003312# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003313 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3314 # actual declarations
3315 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003316 # function pointer declarations
3317 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003318 # foo bar; where foo is some local typedef or #define
3319 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3320 # known declaration macros
3321 $prevline =~ /^\+\s+$declaration_macros/) &&
3322 # for "else if" which can look like "$Ident $Ident"
3323 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3324 # other possible extensions of declaration lines
3325 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3326 # not starting a section or a macro "\" extended line
3327 $prevline =~ /(?:\{\s*|\\)$/) &&
3328 # looks like a declaration
3329 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003330 # function pointer declarations
3331 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003332 # foo bar; where foo is some local typedef or #define
3333 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3334 # known declaration macros
3335 $sline =~ /^\+\s+$declaration_macros/ ||
3336 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003337 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003338 # start or end of block or continuation of declaration
3339 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3340 # bitfield continuation
3341 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3342 # other possible extensions of declaration lines
3343 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3344 # indentation of previous and current line are the same
3345 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003346 if (WARN("LINE_SPACING",
3347 "Missing a blank line after declarations\n" . $hereprev) &&
3348 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003349 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003350 }
Joe Perches3b617e32014-04-03 14:49:28 -07003351 }
3352
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003353# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003354# Exceptions:
3355# 1) within comments
3356# 2) indented preprocessor commands
3357# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003358 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003359 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003360 if (WARN("LEADING_SPACE",
3361 "please, no spaces at the start of a line\n" . $herevet) &&
3362 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003363 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003364 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003365 }
3366
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003367# check we are in a valid C source file if not then ignore this hunk
3368 next if ($realfile !~ /\.(h|c)$/);
3369
Joe Perches5751a242017-11-17 15:28:52 -08003370# check for unusual line ending [ or (
3371 if ($line =~ /^\+.*([\[\(])\s*$/) {
3372 CHK("OPEN_ENDED_LINE",
3373 "Lines should not end with a '$1'\n" . $herecurr);
3374 }
3375
Joe Perches4dbed762017-05-08 15:55:39 -07003376# check if this appears to be the start function declaration, save the name
3377 if ($sline =~ /^\+\{\s*$/ &&
3378 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3379 $context_function = $1;
3380 }
3381
3382# check if this appears to be the end of function declaration
3383 if ($sline =~ /^\+\}\s*$/) {
3384 undef $context_function;
3385 }
3386
Joe Perches032a4c02014-08-06 16:10:29 -07003387# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003388# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003389# if the previous line is a break or return and is indented 1 tab more...
3390 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3391 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003392 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3393 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3394 defined $lines[$linenr] &&
3395 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003396 WARN("UNNECESSARY_ELSE",
3397 "else is not generally useful after a break or return\n" . $hereprev);
3398 }
3399 }
3400
Joe Perchesc00df192014-08-06 16:11:01 -07003401# check indentation of a line with a break;
3402# if the previous line is a goto or return and is indented the same # of tabs
3403 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3404 my $tabs = $1;
3405 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3406 WARN("UNNECESSARY_BREAK",
3407 "break is not useful after a goto or return\n" . $hereprev);
3408 }
3409 }
3410
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003411# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003412 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003413 WARN("CVS_KEYWORD",
3414 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003415 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003416
Joe Perches56e77d72013-02-21 16:44:14 -08003417# check for old HOTPLUG __dev<foo> section markings
3418 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3419 WARN("HOTPLUG_SECTION",
3420 "Using $1 is unnecessary\n" . $herecurr);
3421 }
3422
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003423# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003424 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3425 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003426#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003427 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003428 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003429 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003430 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003431 $stat =~ s/\n./\n /g;
3432 $cond =~ s/\n./\n /g;
3433
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003434#print "linenr<$linenr> <$stat>\n";
3435 # If this statement has no statement boundaries within
3436 # it there is no point in retrying a statement scan
3437 # until we hit end of it.
3438 my $frag = $stat; $frag =~ s/;+\s*$//;
3439 if ($frag !~ /(?:{|;)/) {
3440#print "skip<$line_nr_next>\n";
3441 $suppress_statement = $line_nr_next;
3442 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003443
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003444 # Find the real next line.
3445 $realline_next = $line_nr_next;
3446 if (defined $realline_next &&
3447 (!defined $lines[$realline_next - 1] ||
3448 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3449 $realline_next++;
3450 }
3451
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003452 my $s = $stat;
3453 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003454
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003455 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003456 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003457
3458 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003459 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003460
Andy Whitcroft463f2862009-09-21 17:04:34 -07003461 } elsif ($s =~ /^.\s*else\b/s) {
3462
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003463 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003464 } 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 -07003465 my $type = $1;
3466 $type =~ s/\s+/ /g;
3467 possible($type, "A:" . $s);
3468
Andy Whitcroft8905a672007-11-28 16:21:06 -08003469 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003470 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003471 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003472 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003473
3474 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003475 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003476 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003477 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003478
3479 # Check for any sort of function declaration.
3480 # int foo(something bar, other baz);
3481 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003482 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 -08003483 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003484
Andy Whitcroftcf655042008-03-04 14:28:20 -08003485 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003486 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003487 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003488
Andy Whitcroft8905a672007-11-28 16:21:06 -08003489 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003490 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003491
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003492 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003493 }
3494 }
3495 }
3496
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003497 }
3498
Andy Whitcroft653d4872007-06-23 17:16:34 -07003499#
3500# Checks which may be anchored in the context.
3501#
3502
3503# Check for switch () and associated case and default
3504# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003505 if ($line=~/\bswitch\s*\(.*\)/) {
3506 my $err = '';
3507 my $sep = '';
3508 my @ctx = ctx_block_outer($linenr, $realcnt);
3509 shift(@ctx);
3510 for my $ctx (@ctx) {
3511 my ($clen, $cindent) = line_stats($ctx);
3512 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3513 $indent != $cindent) {
3514 $err .= "$sep$ctx\n";
3515 $sep = '';
3516 } else {
3517 $sep = "[...]\n";
3518 }
3519 }
3520 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003521 ERROR("SWITCH_CASE_INDENT_LEVEL",
3522 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003523 }
3524 }
3525
3526# if/while/etc brace do not go on next line, unless defining a do while loop,
3527# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003528 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 -07003529 my $pre_ctx = "$1$2";
3530
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003531 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003532
3533 if ($line =~ /^\+\t{6,}/) {
3534 WARN("DEEP_INDENTATION",
3535 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3536 }
3537
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003538 my $ctx_cnt = $realcnt - $#ctx - 1;
3539 my $ctx = join("\n", @ctx);
3540
Andy Whitcroft548596d2008-07-23 21:29:01 -07003541 my $ctx_ln = $linenr;
3542 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003543
Andy Whitcroft548596d2008-07-23 21:29:01 -07003544 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3545 defined $lines[$ctx_ln - 1] &&
3546 $lines[$ctx_ln - 1] =~ /^-/)) {
3547 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3548 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003549 $ctx_ln++;
3550 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003551
Andy Whitcroft53210162008-07-23 21:29:03 -07003552 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3553 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003554
Joe Perchesd752fcc2014-08-06 16:11:05 -07003555 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003556 ERROR("OPEN_BRACE",
3557 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003558 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003559 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003560 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3561 $ctx =~ /\)\s*\;\s*$/ &&
3562 defined $lines[$ctx_ln - 1])
3563 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003564 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3565 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003566 WARN("TRAILING_SEMICOLON",
3567 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003568 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003569 }
3570 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003571 }
3572
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003573# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003574 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 -08003575 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3576 ctx_statement_block($linenr, $realcnt, 0)
3577 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003578 my ($s, $c) = ($stat, $cond);
3579
3580 substr($s, 0, length($c), '');
3581
Joe Perches9f5af482015-09-09 15:37:30 -07003582 # remove inline comments
3583 $s =~ s/$;/ /g;
3584 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003585
3586 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003587 my @newlines = ($c =~ /\n/gs);
3588 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003589
Joe Perches9f5af482015-09-09 15:37:30 -07003590 # Make sure we remove the line prefixes as we have
3591 # none on the first line, and are going to readd them
3592 # where necessary.
3593 $s =~ s/\n./\n/gs;
3594 while ($s =~ /\n\s+\\\n/) {
3595 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3596 }
3597
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003598 # We want to check the first line inside the block
3599 # starting at the end of the conditional, so remove:
3600 # 1) any blank line termination
3601 # 2) any opening brace { on end of the line
3602 # 3) any do (...) {
3603 my $continuation = 0;
3604 my $check = 0;
3605 $s =~ s/^.*\bdo\b//;
3606 $s =~ s/^\s*{//;
3607 if ($s =~ s/^\s*\\//) {
3608 $continuation = 1;
3609 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003610 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003611 $check = 1;
3612 $cond_lines++;
3613 }
3614
3615 # Also ignore a loop construct at the end of a
3616 # preprocessor statement.
3617 if (($prevline =~ /^.\s*#\s*define\s/ ||
3618 $prevline =~ /\\\s*$/) && $continuation == 0) {
3619 $check = 0;
3620 }
3621
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003622 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003623 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003624 while ($cond_ptr != $cond_lines) {
3625 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003626
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003627 # If we see an #else/#elif then the code
3628 # is not linear.
3629 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3630 $check = 0;
3631 }
3632
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003633 # Ignore:
3634 # 1) blank lines, they should be at 0,
3635 # 2) preprocessor lines, and
3636 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003637 if ($continuation ||
3638 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003639 $s =~ /^\s*#\s*?/ ||
3640 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003641 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003642 if ($s =~ s/^.*?\n//) {
3643 $cond_lines++;
3644 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003645 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003646 }
3647
3648 my (undef, $sindent) = line_stats("+" . $s);
3649 my $stat_real = raw_line($linenr, $cond_lines);
3650
3651 # Check if either of these lines are modified, else
3652 # this is not this patch's fault.
3653 if (!defined($stat_real) ||
3654 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3655 $check = 0;
3656 }
3657 if (defined($stat_real) && $cond_lines > 1) {
3658 $stat_real = "[...]\n$stat_real";
3659 }
3660
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003661 #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 -07003662
Joe Perches9f5af482015-09-09 15:37:30 -07003663 if ($check && $s ne '' &&
3664 (($sindent % 8) != 0 ||
3665 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003666 ($sindent == $indent &&
3667 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003668 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003669 WARN("SUSPECT_CODE_INDENT",
3670 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003671 }
3672 }
3673
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003674 # Track the 'values' across context and added lines.
3675 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003676 my ($curr_values, $curr_vars) =
3677 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003678 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003679 if ($dbg_values) {
3680 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003681 print "$linenr > .$outline\n";
3682 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003683 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003684 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003685 $prev_values = substr($curr_values, -1);
3686
Andy Whitcroft00df3442007-06-08 13:47:06 -07003687#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003688 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003689
Joe Perches11ca40a2016-12-12 16:46:31 -08003690# check for dereferences that span multiple lines
3691 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3692 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3693 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3694 my $ref = $1;
3695 $line =~ /^.\s*($Lval)/;
3696 $ref .= $1;
3697 $ref =~ s/\s//g;
3698 WARN("MULTILINE_DEREFERENCE",
3699 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3700 }
3701
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003702# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003703 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 -07003704 my $type = $1;
3705 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003706 $var = "" if (!defined $var);
3707 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003708 my $sign = $1;
3709 my $pointer = $2;
3710
3711 $pointer = "" if (!defined $pointer);
3712
3713 if (WARN("UNSPECIFIED_INT",
3714 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3715 $fix) {
3716 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003717 my $comp_pointer = $pointer;
3718 $comp_pointer =~ s/\s//g;
3719 $decl .= $comp_pointer;
3720 $decl = rtrim($decl) if ($var eq "");
3721 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003722 }
3723 }
3724 }
3725
Andy Whitcroft653d4872007-06-23 17:16:34 -07003726# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003727 if ($dbg_type) {
3728 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003729 ERROR("TEST_TYPE",
3730 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003731 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003732 ERROR("TEST_NOT_TYPE",
3733 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003734 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003735 next;
3736 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003737# TEST: allow direct testing of the attribute matcher.
3738 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003739 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003740 ERROR("TEST_ATTR",
3741 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003742 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003743 ERROR("TEST_NOT_ATTR",
3744 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003745 }
3746 next;
3747 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003748
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003749# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003750 if ($line =~ /^.\s*{/ &&
3751 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003752 if (ERROR("OPEN_BRACE",
3753 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003754 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3755 fix_delete_line($fixlinenr - 1, $prevrawline);
3756 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003757 my $fixedline = $prevrawline;
3758 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003759 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003760 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003761 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003762 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003763 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003764 }
3765
Andy Whitcroft653d4872007-06-23 17:16:34 -07003766#
3767# Checks which are anchored on the added line.
3768#
3769
3770# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003771 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003772 my $path = $1;
3773 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003774 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003775 "malformed #include filename\n" . $herecurr);
3776 }
3777 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3778 ERROR("UAPI_INCLUDE",
3779 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003780 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003781 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003782
3783# no C99 // comments
3784 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003785 if (ERROR("C99_COMMENTS",
3786 "do not use C99 // comments\n" . $herecurr) &&
3787 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003788 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003789 if ($line =~ /\/\/(.*)$/) {
3790 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003791 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003792 }
3793 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003794 }
3795 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003796 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003797 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003798
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003799# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3800# the whole statement.
3801#print "APW <$lines[$realline_next - 1]>\n";
3802 if (defined $realline_next &&
3803 exists $lines[$realline_next - 1] &&
3804 !defined $suppress_export{$realline_next} &&
3805 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3806 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003807 # Handle definitions which produce identifiers with
3808 # a prefix:
3809 # XXX(foo);
3810 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003811 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003812 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003813 $name =~ /^${Ident}_$2/) {
3814#print "FOO C name<$name>\n";
3815 $suppress_export{$realline_next} = 1;
3816
3817 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003818 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003819 ^.DEFINE_$Ident\(\Q$name\E\)|
3820 ^.DECLARE_$Ident\(\Q$name\E\)|
3821 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003822 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3823 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003824 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003825#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3826 $suppress_export{$realline_next} = 2;
3827 } else {
3828 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003829 }
3830 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003831 if (!defined $suppress_export{$linenr} &&
3832 $prevline =~ /^.\s*$/ &&
3833 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3834 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3835#print "FOO B <$lines[$linenr - 1]>\n";
3836 $suppress_export{$linenr} = 2;
3837 }
3838 if (defined $suppress_export{$linenr} &&
3839 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003840 WARN("EXPORT_SYMBOL",
3841 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003842 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003843
Joe Eloff5150bda2010-08-09 17:21:00 -07003844# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003845 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003846 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003847 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003848 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003849 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003850 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003851 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003852# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003853 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003854 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003855 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003856 $herecurr) &&
3857 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003858 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003859 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003860 }
3861
Joe Perches18130872014-08-06 16:11:22 -07003862# check for misordered declarations of char/short/int/long with signed/unsigned
3863 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3864 my $tmp = trim($1);
3865 WARN("MISORDERED_TYPE",
3866 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3867 }
3868
Joe Perches809e0822018-08-21 21:58:12 -07003869# check for unnecessary <signed> int declarations of short/long/long long
3870 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3871 my $type = trim($1);
3872 next if ($type !~ /\bint\b/);
3873 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3874 my $new_type = $type;
3875 $new_type =~ s/\b\s*int\s*\b/ /;
3876 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3877 $new_type =~ s/^const\s+//;
3878 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3879 $new_type = "const $new_type" if ($type =~ /^const\b/);
3880 $new_type =~ s/\s+/ /g;
3881 $new_type = trim($new_type);
3882 if (WARN("UNNECESSARY_INT",
3883 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3884 $fix) {
3885 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3886 }
3887 }
3888
Joe Perchescb710ec2010-10-26 14:23:20 -07003889# check for static const char * arrays.
3890 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003891 WARN("STATIC_CONST_CHAR_ARRAY",
3892 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003893 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003894 }
3895
3896# check for initialized const char arrays that should be static const
3897 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
3898 if (WARN("STATIC_CONST_CHAR_ARRAY",
3899 "const array should probably be static const\n" . $herecurr) &&
3900 $fix) {
3901 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
3902 }
3903 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003904
3905# check for static char foo[] = "bar" declarations.
3906 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003907 WARN("STATIC_CONST_CHAR_ARRAY",
3908 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003909 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003910 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003911
Joe Perchesab7e23f2015-04-16 12:44:22 -07003912# check for const <foo> const where <foo> is not a pointer or array type
3913 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3914 my $found = $1;
3915 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3916 WARN("CONST_CONST",
3917 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3918 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3919 WARN("CONST_CONST",
3920 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3921 }
3922 }
3923
Joe Perches9b0fa602014-04-03 14:49:18 -07003924# check for non-global char *foo[] = {"bar", ...} declarations.
3925 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3926 WARN("STATIC_CONST_CHAR_ARRAY",
3927 "char * array declaration might be better as static const\n" .
3928 $herecurr);
3929 }
3930
Joe Perchesb598b672015-04-16 12:44:36 -07003931# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3932 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3933 my $array = $1;
3934 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3935 my $array_div = $1;
3936 if (WARN("ARRAY_SIZE",
3937 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3938 $fix) {
3939 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3940 }
3941 }
3942 }
3943
Joe Perchesb36190c2014-01-27 17:07:18 -08003944# check for function declarations without arguments like "int foo()"
3945 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3946 if (ERROR("FUNCTION_WITHOUT_ARGS",
3947 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3948 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003949 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003950 }
3951 }
3952
Andy Whitcroft653d4872007-06-23 17:16:34 -07003953# check for new typedefs, only function parameters and sparse annotations
3954# make sense.
3955 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003956 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003957 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003958 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003959 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003960 WARN("NEW_TYPEDEFS",
3961 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003962 }
3963
3964# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003965 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003966 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3967 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003968 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003969
Andy Whitcroft65863862009-01-06 14:41:21 -08003970 # Should start with a space.
3971 $to =~ s/^(\S)/ $1/;
3972 # Should not end with a space.
3973 $to =~ s/\s+$//;
3974 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003975 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003976 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003977
Joe Perches3705ce52013-07-03 15:05:31 -07003978## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003979 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003980 if (ERROR("POINTER_LOCATION",
3981 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3982 $fix) {
3983 my $sub_from = $ident;
3984 my $sub_to = $ident;
3985 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003986 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003987 s@\Q$sub_from\E@$sub_to@;
3988 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003989 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003990 }
3991 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3992 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003993 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003994
Andy Whitcroft65863862009-01-06 14:41:21 -08003995 # Should start with a space.
3996 $to =~ s/^(\S)/ $1/;
3997 # Should not end with a space.
3998 $to =~ s/\s+$//;
3999 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004000 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004001 }
4002 # Modifiers should have spaces.
4003 $to =~ s/(\b$Modifier$)/$1 /;
4004
Joe Perches3705ce52013-07-03 15:05:31 -07004005## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004006 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004007 if (ERROR("POINTER_LOCATION",
4008 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4009 $fix) {
4010
4011 my $sub_from = $match;
4012 my $sub_to = $match;
4013 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004014 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004015 s@\Q$sub_from\E@$sub_to@;
4016 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004017 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004018 }
4019
Joe Perches9d3e3c72015-09-09 15:37:27 -07004020# avoid BUG() or BUG_ON()
4021 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004022 my $msg_level = \&WARN;
4023 $msg_level = \&CHK if ($file);
4024 &{$msg_level}("AVOID_BUG",
4025 "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 -07004026 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004027
Joe Perches9d3e3c72015-09-09 15:37:27 -07004028# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004029 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004030 WARN("LINUX_VERSION_CODE",
4031 "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 -08004032 }
4033
Joe Perches17441222011-06-15 15:08:17 -07004034# check for uses of printk_ratelimit
4035 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004036 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004037 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004038 }
4039
Joe Percheseeef5732017-11-17 15:28:41 -08004040# printk should use KERN_* levels
4041 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4042 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4043 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004044 }
4045
Joe Perches243f3802012-05-31 16:26:09 -07004046 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4047 my $orig = $1;
4048 my $level = lc($orig);
4049 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004050 my $level2 = $level;
4051 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004052 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004053 "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 -07004054 }
4055
4056 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004057 if (WARN("PREFER_PR_LEVEL",
4058 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4059 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004060 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004061 s/\bpr_warning\b/pr_warn/;
4062 }
Joe Perches243f3802012-05-31 16:26:09 -07004063 }
4064
Joe Perchesdc139312013-02-21 16:44:13 -08004065 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4066 my $orig = $1;
4067 my $level = lc($orig);
4068 $level = "warn" if ($level eq "warning");
4069 $level = "dbg" if ($level eq "debug");
4070 WARN("PREFER_DEV_LEVEL",
4071 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4072 }
4073
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004074# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4075# number of false positives, but assembly files are not checked, so at
4076# least the arch entry code will not trigger this warning.
4077 if ($line =~ /\bENOSYS\b/) {
4078 WARN("ENOSYS",
4079 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4080 }
4081
Andy Whitcroft653d4872007-06-23 17:16:34 -07004082# function brace can't be on same line, except for #defines of do while,
4083# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004084 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004085 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4086 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4087 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004088 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004089 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004090 $fix) {
4091 fix_delete_line($fixlinenr, $rawline);
4092 my $fixed_line = $rawline;
4093 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4094 my $line1 = $1;
4095 my $line2 = $2;
4096 fix_insert_line($fixlinenr, ltrim($line1));
4097 fix_insert_line($fixlinenr, "\+{");
4098 if ($line2 !~ /^\s*$/) {
4099 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4100 }
4101 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004102 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004103
Andy Whitcroft8905a672007-11-28 16:21:06 -08004104# open braces for enum, union and struct go on the same line.
4105 if ($line =~ /^.\s*{/ &&
4106 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004107 if (ERROR("OPEN_BRACE",
4108 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4109 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4110 fix_delete_line($fixlinenr - 1, $prevrawline);
4111 fix_delete_line($fixlinenr, $rawline);
4112 my $fixedline = rtrim($prevrawline) . " {";
4113 fix_insert_line($fixlinenr, $fixedline);
4114 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004115 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004116 if ($fixedline !~ /^\+\s*$/) {
4117 fix_insert_line($fixlinenr, $fixedline);
4118 }
4119 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004120 }
4121
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004122# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004123 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4124 if (WARN("SPACING",
4125 "missing space after $1 definition\n" . $herecurr) &&
4126 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004127 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004128 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4129 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004130 }
4131
Joe Perches31070b52014-01-23 15:54:49 -08004132# Function pointer declarations
4133# check spacing between type, funcptr, and args
4134# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004135 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004136 my $declare = $1;
4137 my $pre_pointer_space = $2;
4138 my $post_pointer_space = $3;
4139 my $funcname = $4;
4140 my $post_funcname_space = $5;
4141 my $pre_args_space = $6;
4142
Joe Perches91f72e92014-04-03 14:49:12 -07004143# the $Declare variable will capture all spaces after the type
4144# so check it for a missing trailing missing space but pointer return types
4145# don't need a space so don't warn for those.
4146 my $post_declare_space = "";
4147 if ($declare =~ /(\s+)$/) {
4148 $post_declare_space = $1;
4149 $declare = rtrim($declare);
4150 }
4151 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004152 WARN("SPACING",
4153 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004154 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004155 }
4156
4157# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004158# This test is not currently implemented because these declarations are
4159# equivalent to
4160# int foo(int bar, ...)
4161# and this is form shouldn't/doesn't generate a checkpatch warning.
4162#
4163# elsif ($declare =~ /\s{2,}$/) {
4164# WARN("SPACING",
4165# "Multiple spaces after return type\n" . $herecurr);
4166# }
Joe Perches31070b52014-01-23 15:54:49 -08004167
4168# unnecessary space "type ( *funcptr)(args...)"
4169 if (defined $pre_pointer_space &&
4170 $pre_pointer_space =~ /^\s/) {
4171 WARN("SPACING",
4172 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4173 }
4174
4175# unnecessary space "type (* funcptr)(args...)"
4176 if (defined $post_pointer_space &&
4177 $post_pointer_space =~ /^\s/) {
4178 WARN("SPACING",
4179 "Unnecessary space before function pointer name\n" . $herecurr);
4180 }
4181
4182# unnecessary space "type (*funcptr )(args...)"
4183 if (defined $post_funcname_space &&
4184 $post_funcname_space =~ /^\s/) {
4185 WARN("SPACING",
4186 "Unnecessary space after function pointer name\n" . $herecurr);
4187 }
4188
4189# unnecessary space "type (*funcptr) (args...)"
4190 if (defined $pre_args_space &&
4191 $pre_args_space =~ /^\s/) {
4192 WARN("SPACING",
4193 "Unnecessary space before function pointer arguments\n" . $herecurr);
4194 }
4195
4196 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004197 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004198 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004199 }
4200 }
4201
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004202# check for spacing round square brackets; allowed:
4203# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004204# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4205# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004206 while ($line =~ /(.*?\s)\[/g) {
4207 my ($where, $prefix) = ($-[1], $1);
4208 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004209 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004210 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004211 if (ERROR("BRACKET_SPACE",
4212 "space prohibited before open square bracket '['\n" . $herecurr) &&
4213 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004214 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004215 s/^(\+.*?)\s+\[/$1\[/;
4216 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004217 }
4218 }
4219
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004220# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004221 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004222 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004223 my $ctx_before = substr($line, 0, $-[1]);
4224 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004225
4226 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004227 if ($name =~ /^(?:
4228 if|for|while|switch|return|case|
4229 volatile|__volatile__|
4230 __attribute__|format|__extension__|
4231 asm|__asm__)$/x)
4232 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004233 # cpp #define statements have non-optional spaces, ie
4234 # if there is a space between the name and the open
4235 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004236 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004237
4238 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004239 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004240
4241 # If this whole things ends with a type its most
4242 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004243 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004244
4245 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004246 if (WARN("SPACING",
4247 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4248 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004249 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004250 s/\b$name\s+\(/$name\(/;
4251 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004252 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004253 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004254
Andy Whitcroft653d4872007-06-23 17:16:34 -07004255# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004256 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004257 my $fixed_line = "";
4258 my $line_fixed = 0;
4259
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004260 my $ops = qr{
4261 <<=|>>=|<=|>=|==|!=|
4262 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4263 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004264 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004265 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004266 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004267 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004268
4269## print("element count: <" . $#elements . ">\n");
4270## foreach my $el (@elements) {
4271## print("el: <$el>\n");
4272## }
4273
4274 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004275 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004276
Joe Perches3705ce52013-07-03 15:05:31 -07004277 foreach my $el (@elements) {
4278 push(@fix_elements, substr($rawline, $off, length($el)));
4279 $off += length($el);
4280 }
4281
4282 $off = 0;
4283
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004284 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004285 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004286
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004287 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004288
4289 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4290
4291## print("n: <$n> good: <$good>\n");
4292
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004293 $off += length($elements[$n]);
4294
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004295 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004296 my $ca = substr($opline, 0, $off);
4297 my $cc = '';
4298 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4299 $cc = substr($opline, $off + length($elements[$n + 1]));
4300 }
4301 my $cb = "$ca$;$cc";
4302
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004303 my $a = '';
4304 $a = 'V' if ($elements[$n] ne '');
4305 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004306 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004307 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4308 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004309 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004310
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004311 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004312
4313 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004314 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004315 $c = 'V' if ($elements[$n + 2] ne '');
4316 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004317 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004318 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4319 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004320 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004321 } else {
4322 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004323 }
4324
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004325 my $ctx = "${a}x${c}";
4326
4327 my $at = "(ctx:$ctx)";
4328
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004329 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004330 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004331
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004332 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004333 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004334
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004335 # Get the full operator variant.
4336 my $opv = $op . substr($curr_vars, $off, 1);
4337
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004338 # Ignore operators passed as parameters.
4339 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004340 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004341
Andy Whitcroftcf655042008-03-04 14:28:20 -08004342# # Ignore comments
4343# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004344
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004345 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004346 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004347 if ($ctx !~ /.x[WEBC]/ &&
4348 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004349 if (ERROR("SPACING",
4350 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004351 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004352 $line_fixed = 1;
4353 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004354 }
4355
4356 # // is a comment
4357 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004358
Joe Perchesb00e4812014-04-03 14:49:33 -07004359 # : when part of a bitfield
4360 } elsif ($opv eq ':B') {
4361 # skip the bitfield test for now
4362
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004363 # No spaces for:
4364 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004365 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004366 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004367 if (ERROR("SPACING",
4368 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004369 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004370 if (defined $fix_elements[$n + 2]) {
4371 $fix_elements[$n + 2] =~ s/^\s+//;
4372 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004373 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004374 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004375 }
4376
Joe Perches23810972014-12-10 15:51:32 -08004377 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004378 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004379 my $rtrim_before = 0;
4380 my $space_after = 0;
4381 if ($ctx =~ /Wx./) {
4382 if (ERROR("SPACING",
4383 "space prohibited before that '$op' $at\n" . $hereptr)) {
4384 $line_fixed = 1;
4385 $rtrim_before = 1;
4386 }
4387 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004388 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004389 if (ERROR("SPACING",
4390 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004391 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004392 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004393 $space_after = 1;
4394 }
4395 }
4396 if ($rtrim_before || $space_after) {
4397 if ($rtrim_before) {
4398 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4399 } else {
4400 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4401 }
4402 if ($space_after) {
4403 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004404 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004405 }
4406
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004407 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004408 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004409 #warn "'*' is part of type\n";
4410
4411 # unary operators should have a space before and
4412 # none after. May be left adjacent to another
4413 # unary operator, or a cast
4414 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004415 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004416 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004417 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004418 if (ERROR("SPACING",
4419 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004420 if ($n != $last_after + 2) {
4421 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4422 $line_fixed = 1;
4423 }
Joe Perches3705ce52013-07-03 15:05:31 -07004424 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004425 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004426 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004427 # A unary '*' may be const
4428
4429 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004430 if (ERROR("SPACING",
4431 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004432 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004433 if (defined $fix_elements[$n + 2]) {
4434 $fix_elements[$n + 2] =~ s/^\s+//;
4435 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004436 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004437 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004438 }
4439
4440 # unary ++ and unary -- are allowed no space on one side.
4441 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004442 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004443 if (ERROR("SPACING",
4444 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004445 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004446 $line_fixed = 1;
4447 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004448 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004449 if ($ctx =~ /Wx[BE]/ ||
4450 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004451 if (ERROR("SPACING",
4452 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004453 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004454 $line_fixed = 1;
4455 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004456 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004457 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004458 if (ERROR("SPACING",
4459 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004460 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004461 if (defined $fix_elements[$n + 2]) {
4462 $fix_elements[$n + 2] =~ s/^\s+//;
4463 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004464 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004465 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004466 }
4467
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004468 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004469 } elsif ($op eq '<<' or $op eq '>>' or
4470 $op eq '&' or $op eq '^' or $op eq '|' or
4471 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004472 $op eq '*' or $op eq '/' or
4473 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004474 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004475 if ($check) {
4476 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4477 if (CHK("SPACING",
4478 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4479 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4480 $fix_elements[$n + 2] =~ s/^\s+//;
4481 $line_fixed = 1;
4482 }
4483 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4484 if (CHK("SPACING",
4485 "space preferred before that '$op' $at\n" . $hereptr)) {
4486 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4487 $line_fixed = 1;
4488 }
4489 }
4490 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004491 if (ERROR("SPACING",
4492 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004493 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4494 if (defined $fix_elements[$n + 2]) {
4495 $fix_elements[$n + 2] =~ s/^\s+//;
4496 }
Joe Perches3705ce52013-07-03 15:05:31 -07004497 $line_fixed = 1;
4498 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004499 }
4500
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004501 # A colon needs no spaces before when it is
4502 # terminating a case value or a label.
4503 } elsif ($opv eq ':C' || $opv eq ':L') {
4504 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004505 if (ERROR("SPACING",
4506 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004507 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004508 $line_fixed = 1;
4509 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004510 }
4511
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004512 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004513 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004514 my $ok = 0;
4515
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004516 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004517 if (($op eq '<' &&
4518 $cc =~ /^\S+\@\S+>/) ||
4519 ($op eq '>' &&
4520 $ca =~ /<\S+\@\S+$/))
4521 {
4522 $ok = 1;
4523 }
4524
Joe Perchese0df7e12015-04-16 12:44:53 -07004525 # for asm volatile statements
4526 # ignore a colon with another
4527 # colon immediately before or after
4528 if (($op eq ':') &&
4529 ($ca =~ /:$/ || $cc =~ /^:/)) {
4530 $ok = 1;
4531 }
4532
Joe Perches84731622013-11-12 15:10:05 -08004533 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004534 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004535 my $msg_level = \&ERROR;
4536 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004537
Jean Delvare0675a8f2017-09-08 16:16:07 -07004538 if (&{$msg_level}("SPACING",
4539 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004540 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4541 if (defined $fix_elements[$n + 2]) {
4542 $fix_elements[$n + 2] =~ s/^\s+//;
4543 }
Joe Perches3705ce52013-07-03 15:05:31 -07004544 $line_fixed = 1;
4545 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004546 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004547 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004548 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004549
4550## print("n: <$n> GOOD: <$good>\n");
4551
4552 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004553 }
Joe Perches3705ce52013-07-03 15:05:31 -07004554
4555 if (($#elements % 2) == 0) {
4556 $fixed_line = $fixed_line . $fix_elements[$#elements];
4557 }
4558
Joe Perches194f66f2014-08-06 16:11:03 -07004559 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4560 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004561 }
4562
4563
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004564 }
4565
Joe Perches786b6322013-07-03 15:05:32 -07004566# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004567 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004568 if (WARN("SPACING",
4569 "space prohibited before semicolon\n" . $herecurr) &&
4570 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004571 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004572 s/^(\+.*\S)\s+;/$1;/;
4573 }
4574 }
4575
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004576# check for multiple assignments
4577 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004578 CHK("MULTIPLE_ASSIGNMENTS",
4579 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004580 }
4581
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004582## # check for multiple declarations, allowing for a function declaration
4583## # continuation.
4584## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4585## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4586##
4587## # Remove any bracketed sections to ensure we do not
4588## # falsly report the parameters of functions.
4589## my $ln = $line;
4590## while ($ln =~ s/\([^\(\)]*\)//g) {
4591## }
4592## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004593## WARN("MULTIPLE_DECLARATION",
4594## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004595## }
4596## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004597
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004598#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004599 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004600 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004601 if (ERROR("SPACING",
4602 "space required before the open brace '{'\n" . $herecurr) &&
4603 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004604 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004605 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004606 }
4607
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004608## # check for blank lines before declarations
4609## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4610## $prevrawline =~ /^.\s*$/) {
4611## WARN("SPACING",
4612## "No blank lines before declarations\n" . $hereprev);
4613## }
4614##
4615
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004616# closing brace should have a space following it when it has anything
4617# on the line
4618 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004619 if (ERROR("SPACING",
4620 "space required after that close brace '}'\n" . $herecurr) &&
4621 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004622 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004623 s/}((?!(?:,|;|\)))\S)/} $1/;
4624 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004625 }
4626
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004627# check spacing on square brackets
4628 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004629 if (ERROR("SPACING",
4630 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4631 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004632 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004633 s/\[\s+/\[/;
4634 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004635 }
4636 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004637 if (ERROR("SPACING",
4638 "space prohibited before that close 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
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004645# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004646 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4647 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004648 if (ERROR("SPACING",
4649 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4650 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004651 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004652 s/\(\s+/\(/;
4653 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004654 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004655 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004656 $line !~ /for\s*\(.*;\s+\)/ &&
4657 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004658 if (ERROR("SPACING",
4659 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4660 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004661 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004662 s/\s+\)/\)/;
4663 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004664 }
4665
Joe Perchese2826fd2014-08-06 16:10:48 -07004666# check unnecessary parentheses around addressof/dereference single $Lvals
4667# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4668
4669 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004670 my $var = $1;
4671 if (CHK("UNNECESSARY_PARENTHESES",
4672 "Unnecessary parentheses around $var\n" . $herecurr) &&
4673 $fix) {
4674 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4675 }
4676 }
4677
4678# check for unnecessary parentheses around function pointer uses
4679# ie: (foo->bar)(); should be foo->bar();
4680# but not "if (foo->bar) (" to avoid some false positives
4681 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4682 my $var = $2;
4683 if (CHK("UNNECESSARY_PARENTHESES",
4684 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4685 $fix) {
4686 my $var2 = deparenthesize($var);
4687 $var2 =~ s/\s//g;
4688 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4689 }
4690 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004691
Joe Perches63b7c732017-09-08 16:16:01 -07004692# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004693# when !drivers/staging or command-line uses --strict
4694 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004695 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004696 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4697 my $if_stat = $1;
4698 my $test = substr($2, 1, -1);
4699 my $herectx;
4700 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4701 my $match = $1;
4702 # avoid parentheses around potential macro args
4703 next if ($match =~ /^\s*\w+\s*$/);
4704 if (!defined($herectx)) {
4705 $herectx = $here . "\n";
4706 my $cnt = statement_rawlines($if_stat);
4707 for (my $n = 0; $n < $cnt; $n++) {
4708 my $rl = raw_line($linenr, $n);
4709 $herectx .= $rl . "\n";
4710 last if $rl =~ /^[ \+].*\{/;
4711 }
4712 }
4713 CHK("UNNECESSARY_PARENTHESES",
4714 "Unnecessary parentheses around '$match'\n" . $herectx);
4715 }
4716 }
4717
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004718#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004719 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004720 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004721 if (WARN("INDENTED_LABEL",
4722 "labels should not be indented\n" . $herecurr) &&
4723 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004724 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004725 s/^(.)\s+/$1/;
4726 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004727 }
4728
Joe Perches5b9553a2014-04-03 14:49:21 -07004729# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004730 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004731 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004732 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004733 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4734 my $value = $1;
4735 $value = deparenthesize($value);
4736 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4737 ERROR("RETURN_PARENTHESES",
4738 "return is not a function, parentheses are not required\n" . $herecurr);
4739 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004740 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004741 ERROR("SPACING",
4742 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004743 }
4744 }
Joe Perches507e5142013-11-12 15:10:13 -08004745
Joe Perchesb43ae212014-06-23 13:22:07 -07004746# unnecessary return in a void function
4747# at end-of-function, with the previous line a single leading tab, then return;
4748# and the line before that not a goto label target like "out:"
4749 if ($sline =~ /^[ \+]}\s*$/ &&
4750 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4751 $linenr >= 3 &&
4752 $lines[$linenr - 3] =~ /^[ +]/ &&
4753 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004754 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004755 "void function return statements are not generally useful\n" . $hereprev);
4756 }
Joe Perches9819cf22014-06-04 16:12:09 -07004757
Joe Perches189248d2014-01-23 15:54:47 -08004758# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004759 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004760 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4761 my $openparens = $1;
4762 my $count = $openparens =~ tr@\(@\(@;
4763 my $msg = "";
4764 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4765 my $comp = $4; #Not $1 because of $LvalOrFunc
4766 $msg = " - maybe == should be = ?" if ($comp eq "==");
4767 WARN("UNNECESSARY_PARENTHESES",
4768 "Unnecessary parentheses$msg\n" . $herecurr);
4769 }
4770 }
4771
Joe Perchesc5595fa2015-09-09 15:37:58 -07004772# comparisons with a constant or upper case identifier on the left
4773# avoid cases like "foo + BAR < baz"
4774# only fix matches surrounded by parentheses to avoid incorrect
4775# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004776 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004777 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4778 my $lead = $1;
4779 my $const = $2;
4780 my $comp = $3;
4781 my $to = $4;
4782 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004783 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004784 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4785 WARN("CONSTANT_COMPARISON",
4786 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4787 $fix) {
4788 if ($comp eq "<") {
4789 $newcomp = ">";
4790 } elsif ($comp eq "<=") {
4791 $newcomp = ">=";
4792 } elsif ($comp eq ">") {
4793 $newcomp = "<";
4794 } elsif ($comp eq ">=") {
4795 $newcomp = "<=";
4796 }
4797 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4798 }
4799 }
4800
Joe Perchesf34e4a42015-04-16 12:44:19 -07004801# Return of what appears to be an errno should normally be negative
4802 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004803 my $name = $1;
4804 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004805 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004806 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004807 }
4808 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004809
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004810# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004811 if ($line =~ /\b(if|while|for|switch)\(/) {
4812 if (ERROR("SPACING",
4813 "space required before the open parenthesis '('\n" . $herecurr) &&
4814 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004815 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004816 s/\b(if|while|for|switch)\(/$1 \(/;
4817 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004818 }
4819
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004820# Check for illegal assignment in if conditional -- and check for trailing
4821# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004822 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004823 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4824 ctx_statement_block($linenr, $realcnt, 0)
4825 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004826 my ($stat_next) = ctx_statement_block($line_nr_next,
4827 $remain_next, $off_next);
4828 $stat_next =~ s/\n./\n /g;
4829 ##print "stat<$stat> stat_next<$stat_next>\n";
4830
4831 if ($stat_next =~ /^\s*while\b/) {
4832 # If the statement carries leading newlines,
4833 # then count those as offsets.
4834 my ($whitespace) =
4835 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4836 my $offset =
4837 statement_rawlines($whitespace) - 1;
4838
4839 $suppress_whiletrailers{$line_nr_next +
4840 $offset} = 1;
4841 }
4842 }
4843 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004844 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004845 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004846 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004847
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004848 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004849 ERROR("ASSIGN_IN_IF",
4850 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004851 }
4852
4853 # Find out what is on the end of the line after the
4854 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004855 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004856 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004857 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004858 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4859 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004860 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004861 # Find out how long the conditional actually is.
4862 my @newlines = ($c =~ /\n/gs);
4863 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004864 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004865
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004866 $stat_real = raw_line($linenr, $cond_lines)
4867 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004868 if (defined($stat_real) && $cond_lines > 1) {
4869 $stat_real = "[...]\n$stat_real";
4870 }
4871
Joe Perches000d1cc12011-07-25 17:13:25 -07004872 ERROR("TRAILING_STATEMENTS",
4873 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004874 }
4875 }
4876
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004877# Check for bitwise tests written as boolean
4878 if ($line =~ /
4879 (?:
4880 (?:\[|\(|\&\&|\|\|)
4881 \s*0[xX][0-9]+\s*
4882 (?:\&\&|\|\|)
4883 |
4884 (?:\&\&|\|\|)
4885 \s*0[xX][0-9]+\s*
4886 (?:\&\&|\|\||\)|\])
4887 )/x)
4888 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004889 WARN("HEXADECIMAL_BOOLEAN_TEST",
4890 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004891 }
4892
Andy Whitcroft8905a672007-11-28 16:21:06 -08004893# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004894 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4895 my $s = $1;
4896 $s =~ s/$;//g; # Remove any comments
4897 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004898 ERROR("TRAILING_STATEMENTS",
4899 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004900 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004901 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004902# if should not continue a brace
4903 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004904 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004905 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004906 $herecurr);
4907 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004908# case and default should not have general statements after them
4909 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4910 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004911 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004912 \s*return\s+
4913 )/xg)
4914 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004915 ERROR("TRAILING_STATEMENTS",
4916 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004917 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004918
4919 # Check for }<nl>else {, these must be at the same
4920 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004921 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4922 $previndent == $indent) {
4923 if (ERROR("ELSE_AFTER_BRACE",
4924 "else should follow close brace '}'\n" . $hereprev) &&
4925 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4926 fix_delete_line($fixlinenr - 1, $prevrawline);
4927 fix_delete_line($fixlinenr, $rawline);
4928 my $fixedline = $prevrawline;
4929 $fixedline =~ s/}\s*$//;
4930 if ($fixedline !~ /^\+\s*$/) {
4931 fix_insert_line($fixlinenr, $fixedline);
4932 }
4933 $fixedline = $rawline;
4934 $fixedline =~ s/^(.\s*)else/$1} else/;
4935 fix_insert_line($fixlinenr, $fixedline);
4936 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004937 }
4938
Joe Perches8b8856f2014-08-06 16:11:14 -07004939 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4940 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004941 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4942
4943 # Find out what is on the end of the line after the
4944 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004945 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004946 $s =~ s/\n.*//g;
4947
4948 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004949 if (ERROR("WHILE_AFTER_BRACE",
4950 "while should follow close brace '}'\n" . $hereprev) &&
4951 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4952 fix_delete_line($fixlinenr - 1, $prevrawline);
4953 fix_delete_line($fixlinenr, $rawline);
4954 my $fixedline = $prevrawline;
4955 my $trailing = $rawline;
4956 $trailing =~ s/^\+//;
4957 $trailing = trim($trailing);
4958 $fixedline =~ s/}\s*$/} $trailing/;
4959 fix_insert_line($fixlinenr, $fixedline);
4960 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004961 }
4962 }
4963
Joe Perches95e2c602013-07-03 15:05:20 -07004964#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004965 while ($line =~ m{($Constant|$Lval)}g) {
4966 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004967
Joe Perches95e2c602013-07-03 15:05:20 -07004968#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004969 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004970 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004971#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004972 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004973#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004974 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4975#Ignore some three character SI units explicitly, like MiB and KHz
4976 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004977 while ($var =~ m{($Ident)}g) {
4978 my $word = $1;
4979 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004980 if ($check) {
4981 seed_camelcase_includes();
4982 if (!$file && !$camelcase_file_seeded) {
4983 seed_camelcase_file($realfile);
4984 $camelcase_file_seeded = 1;
4985 }
4986 }
Joe Perches7e781f62013-09-11 14:23:55 -07004987 if (!defined $camelcase{$word}) {
4988 $camelcase{$word} = 1;
4989 CHK("CAMELCASE",
4990 "Avoid CamelCase: <$word>\n" . $herecurr);
4991 }
Joe Perches34456862013-07-03 15:05:34 -07004992 }
Joe Perches323c1262012-12-17 16:02:07 -08004993 }
4994 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004995
4996#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004997 if ($line =~ /\#\s*define.*\\\s+$/) {
4998 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4999 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5000 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005001 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005002 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005003 }
5004
Fabian Frederick0e212e02015-04-16 12:44:25 -07005005# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5006# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005007 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005008 my $file = "$1.h";
5009 my $checkfile = "include/linux/$file";
5010 if (-f "$root/$checkfile" &&
5011 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005012 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005013 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005014 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5015 if ($asminclude > 0) {
5016 if ($realfile =~ m{^arch/}) {
5017 CHK("ARCH_INCLUDE_LINUX",
5018 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5019 } else {
5020 WARN("INCLUDE_LINUX",
5021 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5022 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005023 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005024 }
5025 }
5026
Andy Whitcroft653d4872007-06-23 17:16:34 -07005027# multi-statement macros should be enclosed in a do while loop, grab the
5028# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005029# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07005030 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5031 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005032 my $ln = $linenr;
5033 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005034 my ($off, $dstat, $dcond, $rest);
5035 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005036 my $has_flow_statement = 0;
5037 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005038 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005039 ctx_statement_block($linenr, $realcnt, 0);
5040 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005041 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005042 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005043
Joe Perches08a28432014-10-13 15:51:55 -07005044 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005045 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005046
Joe Perchesf59b64b2016-10-11 13:52:08 -07005047 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5048 my $define_args = $1;
5049 my $define_stmt = $dstat;
5050 my @def_args = ();
5051
5052 if (defined $define_args && $define_args ne "") {
5053 $define_args = substr($define_args, 1, length($define_args) - 2);
5054 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005055 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005056 @def_args = split(",", $define_args);
5057 }
5058
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005059 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005060 $dstat =~ s/\\\n.//g;
5061 $dstat =~ s/^\s*//s;
5062 $dstat =~ s/\s*$//s;
5063
5064 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005065 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5066 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005067 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005068 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005069 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005070
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005071 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005072 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5073 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005074 {
5075 }
5076
Joe Perches42e15292016-03-15 14:58:01 -07005077 # Make asm volatile uses seem like a generic function
5078 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5079
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005080 my $exceptions = qr{
5081 $Declare|
5082 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005083 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005084 DECLARE_PER_CPU|
5085 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005086 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005087 union|
5088 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005089 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005090 ^\"|\"$|
5091 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005092 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005093 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005094
5095 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005096 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005097 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005098
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005099 if ($dstat ne '' &&
5100 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5101 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005102 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005103 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005104 $dstat !~ /$exceptions/ &&
5105 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005106 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005107 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005108 $dstat !~ /^for\s*$Constant$/ && # for (...)
5109 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5110 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005111 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005112 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005113 {
Joe Perchese7955562017-05-08 15:55:48 -07005114 if ($dstat =~ /^\s*if\b/) {
5115 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5116 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5117 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005118 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5119 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5120 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005121 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005122 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005123 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005124
5125 }
Joe Perches52076492016-10-11 13:52:14 -07005126
5127 # Make $define_stmt single line, comment-free, etc
5128 my @stmt_array = split('\n', $define_stmt);
5129 my $first = 1;
5130 $define_stmt = "";
5131 foreach my $l (@stmt_array) {
5132 $l =~ s/\\$//;
5133 if ($first) {
5134 $define_stmt = $l;
5135 $first = 0;
5136 } elsif ($l =~ /^[\+ ]/) {
5137 $define_stmt .= substr($l, 1);
5138 }
5139 }
5140 $define_stmt =~ s/$;//g;
5141 $define_stmt =~ s/\s+/ /g;
5142 $define_stmt = trim($define_stmt);
5143
Joe Perchesf59b64b2016-10-11 13:52:08 -07005144# check if any macro arguments are reused (ignore '...' and 'type')
5145 foreach my $arg (@def_args) {
5146 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005147 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005148 my $tmp_stmt = $define_stmt;
5149 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5150 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5151 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005152 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005153 if ($use_cnt > 1) {
5154 CHK("MACRO_ARG_REUSE",
5155 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005156 }
5157# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005158 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005159 ((defined($1) && $1 ne ',') ||
5160 (defined($2) && $2 ne ','))) {
5161 CHK("MACRO_ARG_PRECEDENCE",
5162 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005163 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005164 }
Joe Perches5023d342012-12-17 16:01:47 -08005165
Joe Perches08a28432014-10-13 15:51:55 -07005166# check for macros with flow control, but without ## concatenation
5167# ## concatenation is commonly a macro that defines a function so ignore those
5168 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005169 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005170 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005171
Joe Perches08a28432014-10-13 15:51:55 -07005172 WARN("MACRO_WITH_FLOW_CONTROL",
5173 "Macros with flow control statements should be avoided\n" . "$herectx");
5174 }
5175
Joe Perches481eb482012-12-17 16:01:56 -08005176# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005177
5178 } else {
5179 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005180 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5181 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005182 $line =~ /^\+.*\\$/) {
5183 WARN("LINE_CONTINUATIONS",
5184 "Avoid unnecessary line continuations\n" . $herecurr);
5185 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005186 }
5187
Joe Perchesb13edf72012-07-30 14:41:24 -07005188# do {} while (0) macro tests:
5189# single-statement macros do not need to be enclosed in do while (0) loop,
5190# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005191 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005192 $realfile !~ m@/vmlinux.lds.h$@ &&
5193 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5194 my $ln = $linenr;
5195 my $cnt = $realcnt;
5196 my ($off, $dstat, $dcond, $rest);
5197 my $ctx = '';
5198 ($dstat, $dcond, $ln, $cnt, $off) =
5199 ctx_statement_block($linenr, $realcnt, 0);
5200 $ctx = $dstat;
5201
5202 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005203 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005204
5205 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5206 my $stmts = $2;
5207 my $semis = $3;
5208
5209 $ctx =~ s/\n*$//;
5210 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005211 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005212
Joe Perchesac8e97f2012-08-21 16:15:53 -07005213 if (($stmts =~ tr/;/;/) == 1 &&
5214 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005215 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5216 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5217 }
5218 if (defined $semis && $semis ne "") {
5219 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5220 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5221 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005222 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5223 $ctx =~ s/\n*$//;
5224 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005225 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005226
5227 WARN("TRAILING_SEMICOLON",
5228 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005229 }
5230 }
5231
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005232# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005233 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5234 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005235 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005236 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005237 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5238 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005239 my @allowed = ();
5240 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005241 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005242 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005243 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005244 for my $chunk (@chunks) {
5245 my ($cond, $block) = @{$chunk};
5246
Andy Whitcroft773647a2008-03-28 14:15:58 -07005247 # If the condition carries leading newlines, then count those as offsets.
5248 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5249 my $offset = statement_rawlines($whitespace) - 1;
5250
Joe Perchesaad4f612012-03-23 15:02:19 -07005251 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005252 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5253
5254 # We have looked at and allowed this specific line.
5255 $suppress_ifbraces{$ln + $offset} = 1;
5256
5257 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005258 $ln += statement_rawlines($block) - 1;
5259
Andy Whitcroft773647a2008-03-28 14:15:58 -07005260 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005261
5262 $seen++ if ($block =~ /^\s*{/);
5263
Joe Perchesaad4f612012-03-23 15:02:19 -07005264 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005265 if (statement_lines($cond) > 1) {
5266 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005267 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005268 }
5269 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005270 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005271 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005272 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005273 if (statement_block_size($block) > 1) {
5274 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005275 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005276 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005277 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005278 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005279 if ($seen) {
5280 my $sum_allowed = 0;
5281 foreach (@allowed) {
5282 $sum_allowed += $_;
5283 }
5284 if ($sum_allowed == 0) {
5285 WARN("BRACES",
5286 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5287 } elsif ($sum_allowed != $allow &&
5288 $seen != $allow) {
5289 CHK("BRACES",
5290 "braces {} should be used on all arms of this statement\n" . $herectx);
5291 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005292 }
5293 }
5294 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005295 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005296 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005297 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005298
Andy Whitcroftcf655042008-03-04 14:28:20 -08005299 # Check the pre-context.
5300 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5301 #print "APW: ALLOWED: pre<$1>\n";
5302 $allowed = 1;
5303 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005304
5305 my ($level, $endln, @chunks) =
5306 ctx_statement_full($linenr, $realcnt, $-[0]);
5307
Andy Whitcroftcf655042008-03-04 14:28:20 -08005308 # Check the condition.
5309 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005310 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005311 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005312 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005313 }
5314 if (statement_lines($cond) > 1) {
5315 #print "APW: ALLOWED: cond<$cond>\n";
5316 $allowed = 1;
5317 }
5318 if ($block =~/\b(?:if|for|while)\b/) {
5319 #print "APW: ALLOWED: block<$block>\n";
5320 $allowed = 1;
5321 }
5322 if (statement_block_size($block) > 1) {
5323 #print "APW: ALLOWED: lines block<$block>\n";
5324 $allowed = 1;
5325 }
5326 # Check the post-context.
5327 if (defined $chunks[1]) {
5328 my ($cond, $block) = @{$chunks[1]};
5329 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005330 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005331 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005332 if ($block =~ /^\s*\{/) {
5333 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5334 $allowed = 1;
5335 }
5336 }
5337 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005338 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005339 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005340
Joe Perches000d1cc12011-07-25 17:13:25 -07005341 WARN("BRACES",
5342 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005343 }
5344 }
5345
Joe Perchese4c5bab2017-02-24 15:01:41 -08005346# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005347 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5348 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005349 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5350 }
5351
Joe Perches0979ae62012-12-17 16:01:59 -08005352# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005353 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005354 if (CHK("BRACES",
5355 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5356 $fix && $prevrawline =~ /^\+/) {
5357 fix_delete_line($fixlinenr - 1, $prevrawline);
5358 }
Joe Perches0979ae62012-12-17 16:01:59 -08005359 }
Joe Perches77b9a532013-07-03 15:05:29 -07005360 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005361 if (CHK("BRACES",
5362 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5363 $fix) {
5364 fix_delete_line($fixlinenr, $rawline);
5365 }
Joe Perches0979ae62012-12-17 16:01:59 -08005366 }
5367
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005368# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005369 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5370 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005371 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005372 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005373 }
5374
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005375# Check for user-visible strings broken across lines, which breaks the ability
5376# to grep for the string. Make exceptions when the previous string ends in a
5377# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5378# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005379 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005380 $prevline =~ /"\s*$/ &&
5381 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5382 if (WARN("SPLIT_STRING",
5383 "quoted string split across lines\n" . $hereprev) &&
5384 $fix &&
5385 $prevrawline =~ /^\+.*"\s*$/ &&
5386 $last_coalesced_string_linenr != $linenr - 1) {
5387 my $extracted_string = get_quoted_string($line, $rawline);
5388 my $comma_close = "";
5389 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5390 $comma_close = $1;
5391 }
5392
5393 fix_delete_line($fixlinenr - 1, $prevrawline);
5394 fix_delete_line($fixlinenr, $rawline);
5395 my $fixedline = $prevrawline;
5396 $fixedline =~ s/"\s*$//;
5397 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5398 fix_insert_line($fixlinenr - 1, $fixedline);
5399 $fixedline = $rawline;
5400 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5401 if ($fixedline !~ /\+\s*$/) {
5402 fix_insert_line($fixlinenr, $fixedline);
5403 }
5404 $last_coalesced_string_linenr = $linenr;
5405 }
5406 }
5407
5408# check for missing a space in a string concatenation
5409 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5410 WARN('MISSING_SPACE',
5411 "break quoted strings at a space character\n" . $hereprev);
5412 }
5413
Joe Perchese4b7d302017-05-08 15:55:51 -07005414# check for an embedded function name in a string when the function is known
5415# This does not work very well for -f --file checking as it depends on patch
5416# context providing the function name or a single line form for in-file
5417# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005418 if ($line =~ /^\+.*$String/ &&
5419 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005420 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5421 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005422 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005423 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005424 }
5425
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005426# check for spaces before a quoted newline
5427 if ($rawline =~ /^.*\".*\s\\n/) {
5428 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5429 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5430 $fix) {
5431 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5432 }
5433
5434 }
5435
Joe Perchesf17dba42014-10-13 15:51:51 -07005436# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005437 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5438 if (CHK("CONCATENATED_STRING",
5439 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5440 $fix) {
5441 while ($line =~ /($String)/g) {
5442 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5443 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5444 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5445 }
5446 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005447 }
5448
Joe Perches90ad30e2014-12-10 15:51:59 -08005449# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005450 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005451 if (WARN("STRING_FRAGMENTS",
5452 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5453 $fix) {
5454 while ($line =~ /($String)(?=\s*")/g) {
5455 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5456 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5457 }
5458 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005459 }
5460
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005461# check for non-standard and hex prefixed decimal printf formats
5462 my $show_L = 1; #don't show the same defect twice
5463 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005464 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005465 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005466 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005467 # check for %L
5468 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005469 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005470 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5471 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005472 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005473 # check for %Z
5474 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5475 WARN("PRINTF_Z",
5476 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5477 $show_Z = 0;
5478 }
5479 # check for 0x<decimal>
5480 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5481 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005482 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5483 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005484 }
5485
5486# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005487 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005488 WARN("LINE_CONTINUATIONS",
5489 "Avoid line continuations in quoted strings\n" . $herecurr);
5490 }
5491
Andy Whitcroft00df3442007-06-08 13:47:06 -07005492# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005493 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005494 WARN("IF_0",
5495 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5496 }
5497
5498# warn about #if 1
5499 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5500 WARN("IF_1",
5501 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005502 }
5503
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005504# check for needless "if (<foo>) fn(<foo>)" uses
5505 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005506 my $tested = quotemeta($1);
5507 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5508 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5509 my $func = $1;
5510 if (WARN('NEEDLESS_IF',
5511 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5512 $fix) {
5513 my $do_fix = 1;
5514 my $leading_tabs = "";
5515 my $new_leading_tabs = "";
5516 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5517 $leading_tabs = $1;
5518 } else {
5519 $do_fix = 0;
5520 }
5521 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5522 $new_leading_tabs = $1;
5523 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5524 $do_fix = 0;
5525 }
5526 } else {
5527 $do_fix = 0;
5528 }
5529 if ($do_fix) {
5530 fix_delete_line($fixlinenr - 1, $prevrawline);
5531 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5532 }
5533 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005534 }
5535 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005536
Joe Perchesebfdc402014-08-06 16:10:27 -07005537# check for unnecessary "Out of Memory" messages
5538 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5539 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5540 (defined $1 || defined $3) &&
5541 $linenr > 3) {
5542 my $testval = $2;
5543 my $testline = $lines[$linenr - 3];
5544
5545 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5546# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5547
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005548 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 -07005549 WARN("OOM_MESSAGE",
5550 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5551 }
5552 }
5553
Joe Perchesf78d98f2014-10-13 15:52:01 -07005554# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005555 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005556 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5557 my $level = $1;
5558 if (WARN("UNNECESSARY_KERN_LEVEL",
5559 "Possible unnecessary $level\n" . $herecurr) &&
5560 $fix) {
5561 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5562 }
5563 }
5564
Joe Perches45c55e92017-02-24 15:01:31 -08005565# check for logging continuations
5566 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5567 WARN("LOGGING_CONTINUATION",
5568 "Avoid logging continuation uses where feasible\n" . $herecurr);
5569 }
5570
Joe Perchesabb08a52014-12-10 15:51:46 -08005571# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005572 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005573 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5574 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5575 WARN("MASK_THEN_SHIFT",
5576 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5577 }
5578
Joe Perchesb75ac612014-12-10 15:52:02 -08005579# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005580 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005581 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5582 my $val = $1;
5583 my $equal = "!";
5584 $equal = "" if ($4 eq "!=");
5585 if (CHK("COMPARISON_TO_NULL",
5586 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5587 $fix) {
5588 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5589 }
5590 }
5591 }
5592
Joe Perches8716de32013-09-11 14:24:05 -07005593# check for bad placement of section $InitAttribute (e.g.: __initdata)
5594 if ($line =~ /(\b$InitAttribute\b)/) {
5595 my $attr = $1;
5596 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5597 my $ptr = $1;
5598 my $var = $2;
5599 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5600 ERROR("MISPLACED_INIT",
5601 "$attr should be placed after $var\n" . $herecurr)) ||
5602 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5603 WARN("MISPLACED_INIT",
5604 "$attr should be placed after $var\n" . $herecurr))) &&
5605 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005606 $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 -07005607 }
5608 }
5609 }
5610
Joe Perchese970b8842013-11-12 15:10:10 -08005611# check for $InitAttributeData (ie: __initdata) with const
5612 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5613 my $attr = $1;
5614 $attr =~ /($InitAttributePrefix)(.*)/;
5615 my $attr_prefix = $1;
5616 my $attr_type = $2;
5617 if (ERROR("INIT_ATTRIBUTE",
5618 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5619 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005620 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005621 s/$InitAttributeData/${attr_prefix}initconst/;
5622 }
5623 }
5624
5625# check for $InitAttributeConst (ie: __initconst) without const
5626 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5627 my $attr = $1;
5628 if (ERROR("INIT_ATTRIBUTE",
5629 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5630 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005631 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005632 /(^\+\s*(?:static\s+))/;
5633 $lead = rtrim($1);
5634 $lead = "$lead " if ($lead !~ /^\+$/);
5635 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005636 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005637 }
5638 }
5639
Joe Perchesc17893c2015-04-16 12:44:42 -07005640# check for __read_mostly with const non-pointer (should just be const)
5641 if ($line =~ /\b__read_mostly\b/ &&
5642 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5643 if (ERROR("CONST_READ_MOSTLY",
5644 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5645 $fix) {
5646 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5647 }
5648 }
5649
Joe Perchesfbdb8132014-04-03 14:49:14 -07005650# don't use __constant_<foo> functions outside of include/uapi/
5651 if ($realfile !~ m@^include/uapi/@ &&
5652 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5653 my $constant_func = $1;
5654 my $func = $constant_func;
5655 $func =~ s/^__constant_//;
5656 if (WARN("CONSTANT_CONVERSION",
5657 "$constant_func should be $func\n" . $herecurr) &&
5658 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005659 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005660 }
5661 }
5662
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005663# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005664 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005665 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005666 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005667 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005668 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005669 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5670 }
5671 if ($delay > 2000) {
5672 WARN("LONG_UDELAY",
5673 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005674 }
5675 }
5676
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005677# warn about unexpectedly long msleep's
5678 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5679 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005680 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005681 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005682 }
5683 }
5684
Joe Perches36ec1932013-07-03 15:05:25 -07005685# check for comparisons of jiffies
5686 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5687 WARN("JIFFIES_COMPARISON",
5688 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5689 }
5690
Joe Perches9d7a34a2013-07-03 15:05:26 -07005691# check for comparisons of get_jiffies_64()
5692 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5693 WARN("JIFFIES_COMPARISON",
5694 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5695 }
5696
Andy Whitcroft00df3442007-06-08 13:47:06 -07005697# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005698# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005699# print "#ifdef in C files should be avoided\n";
5700# print "$herecurr";
5701# $clean = 0;
5702# }
5703
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005704# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005705 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005706 if (ERROR("SPACING",
5707 "exactly one space required after that #$1\n" . $herecurr) &&
5708 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005709 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005710 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5711 }
5712
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005713 }
5714
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005715# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005716 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5717 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005718 my $which = $1;
5719 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005720 CHK("UNCOMMENTED_DEFINITION",
5721 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005722 }
5723 }
5724# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005725
5726 my $barriers = qr{
5727 mb|
5728 rmb|
5729 wmb|
5730 read_barrier_depends
5731 }x;
5732 my $barrier_stems = qr{
5733 mb__before_atomic|
5734 mb__after_atomic|
5735 store_release|
5736 load_acquire|
5737 store_mb|
5738 (?:$barriers)
5739 }x;
5740 my $all_barriers = qr{
5741 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005742 smp_(?:$barrier_stems)|
5743 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005744 }x;
5745
5746 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005747 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005748 WARN("MEMORY_BARRIER",
5749 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005750 }
5751 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005752
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005753 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5754
5755 if ($realfile !~ m@^include/asm-generic/@ &&
5756 $realfile !~ m@/barrier\.h$@ &&
5757 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5758 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5759 WARN("MEMORY_BARRIER",
5760 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5761 }
5762
Joe Perchescb426e92015-06-25 15:02:46 -07005763# check for waitqueue_active without a comment.
5764 if ($line =~ /\bwaitqueue_active\s*\(/) {
5765 if (!ctx_has_comment($first_line, $linenr)) {
5766 WARN("WAITQUEUE_ACTIVE",
5767 "waitqueue_active without comment\n" . $herecurr);
5768 }
5769 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005770
Paul E. McKenney91db2592017-11-27 09:37:35 -08005771# check for smp_read_barrier_depends and read_barrier_depends
5772 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5773 WARN("READ_BARRIER_DEPENDS",
5774 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5775 }
5776
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005777# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005778 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005779 CHK("ARCH_DEFINES",
5780 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005781 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005782
Joe Perches596ed452017-07-12 14:37:02 -07005783# check that the storage class is not after a type
5784 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005785 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005786 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5787 }
5788# Check that the storage class is at the beginning of a declaration
5789 if ($line =~ /\b$Storage\b/ &&
5790 $line !~ /^.\s*$Storage/ &&
5791 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5792 $1 !~ /[\,\)]\s*$/) {
5793 WARN("STORAGE_CLASS",
5794 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005795 }
5796
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005797# check the location of the inline attribute, that it is between
5798# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005799 if ($line =~ /\b$Type\s+$Inline\b/ ||
5800 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005801 ERROR("INLINE_LOCATION",
5802 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005803 }
5804
Andy Whitcroft8905a672007-11-28 16:21:06 -08005805# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005806 if ($realfile !~ m@\binclude/uapi/@ &&
5807 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005808 if (WARN("INLINE",
5809 "plain inline is preferred over $1\n" . $herecurr) &&
5810 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005811 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005812
5813 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005814 }
5815
Joe Perches3d130fd2011-01-12 17:00:00 -08005816# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005817 if ($realfile !~ m@\binclude/uapi/@ &&
5818 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005819 WARN("PREFER_PACKED",
5820 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005821 }
5822
Joe Perches39b7e282011-07-25 17:13:24 -07005823# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005824 if ($realfile !~ m@\binclude/uapi/@ &&
5825 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005826 WARN("PREFER_ALIGNED",
5827 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005828 }
5829
Joe Perches5f14d3b2012-01-10 15:09:52 -08005830# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005831 if ($realfile !~ m@\binclude/uapi/@ &&
5832 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005833 if (WARN("PREFER_PRINTF",
5834 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5835 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005836 $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 -07005837
5838 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005839 }
5840
Joe Perches6061d942012-03-23 15:02:16 -07005841# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005842 if ($realfile !~ m@\binclude/uapi/@ &&
5843 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005844 if (WARN("PREFER_SCANF",
5845 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5846 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005847 $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 -07005848 }
Joe Perches6061d942012-03-23 15:02:16 -07005849 }
5850
Joe Perches619a9082014-12-10 15:51:35 -08005851# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005852 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005853 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5854 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5855 $line =~ /\b__weak\b/)) {
5856 ERROR("WEAK_DECLARATION",
5857 "Using weak declarations can have unintended link defects\n" . $herecurr);
5858 }
5859
Tomas Winklerfd39f902016-12-12 16:46:34 -08005860# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005861 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005862 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005863 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5864 my $type = $1;
5865 if ($type =~ /\b($typeC99Typedefs)\b/) {
5866 $type = $1;
5867 my $kernel_type = 'u';
5868 $kernel_type = 's' if ($type =~ /^_*[si]/);
5869 $type =~ /(\d+)/;
5870 $kernel_type .= $1;
5871 if (CHK("PREFER_KERNEL_TYPES",
5872 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5873 $fix) {
5874 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5875 }
5876 }
5877 }
5878
Joe Perches938224b2016-01-20 14:59:15 -08005879# check for cast of C90 native int or longer types constants
5880 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5881 my $cast = $1;
5882 my $const = $2;
5883 if (WARN("TYPECAST_INT_CONSTANT",
5884 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5885 $fix) {
5886 my $suffix = "";
5887 my $newconst = $const;
5888 $newconst =~ s/${Int_type}$//;
5889 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5890 if ($cast =~ /\blong\s+long\b/) {
5891 $suffix .= 'LL';
5892 } elsif ($cast =~ /\blong\b/) {
5893 $suffix .= 'L';
5894 }
5895 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5896 }
5897 }
5898
Joe Perches8f53a9b2010-03-05 13:43:48 -08005899# check for sizeof(&)
5900 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005901 WARN("SIZEOF_ADDRESS",
5902 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005903 }
5904
Joe Perches66c80b62012-07-30 14:41:22 -07005905# check for sizeof without parenthesis
5906 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005907 if (WARN("SIZEOF_PARENTHESIS",
5908 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5909 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005910 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005911 }
Joe Perches66c80b62012-07-30 14:41:22 -07005912 }
5913
Joe Perches88982fe2012-12-17 16:02:00 -08005914# check for struct spinlock declarations
5915 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5916 WARN("USE_SPINLOCK_T",
5917 "struct spinlock should be spinlock_t\n" . $herecurr);
5918 }
5919
Joe Perchesa6962d72013-04-29 16:18:13 -07005920# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005921 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005922 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005923 $fmt =~ s/%%//g;
5924 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005925 if (WARN("PREFER_SEQ_PUTS",
5926 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5927 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005928 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005929 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005930 }
5931 }
5932
Joe Perches478b1792018-04-10 16:33:34 -07005933# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07005934 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07005935 defined $stat &&
5936 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5937 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005938 my $stat_real;
5939
Joe Perches0b523762017-05-08 15:55:36 -07005940 my $lc = $stat =~ tr@\n@@;
5941 $lc = $lc + $linenr;
5942 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07005943 my $specifier;
5944 my $extension;
5945 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07005946 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5947 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005948
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005949 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5950 $specifier = $1;
5951 $extension = $2;
5952 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5953 $bad_specifier = $specifier;
5954 last;
5955 }
5956 if ($extension eq "x" && !defined($stat_real)) {
5957 if (!defined($stat_real)) {
5958 $stat_real = get_stat_real($linenr, $lc);
5959 }
5960 WARN("VSPRINTF_SPECIFIER_PX",
5961 "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");
5962 }
5963 }
5964 if ($bad_specifier ne "") {
5965 my $stat_real = get_stat_real($linenr, $lc);
5966 my $ext_type = "Invalid";
5967 my $use = "";
5968 if ($bad_specifier =~ /p[Ff]/) {
5969 $ext_type = "Deprecated";
5970 $use = " - use %pS instead";
5971 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5972 }
5973
5974 WARN("VSPRINTF_POINTER_EXTENSION",
5975 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5976 }
Joe Perches0b523762017-05-08 15:55:36 -07005977 }
5978 }
5979
Andy Whitcroft554e1652012-01-10 15:09:57 -08005980# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07005981 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005982 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005983 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005984
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005985 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005986 my $ms_val = $7;
5987 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005988
Andy Whitcroft554e1652012-01-10 15:09:57 -08005989 if ($ms_size =~ /^(0x|)0$/i) {
5990 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005991 "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 -08005992 } elsif ($ms_size =~ /^(0x|)1$/i) {
5993 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005994 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5995 }
5996 }
5997
Joe Perches98a9bba2014-01-23 15:54:52 -08005998# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005999# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006000# defined $stat &&
6001# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6002# if (WARN("PREFER_ETHER_ADDR_COPY",
6003# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6004# $fix) {
6005# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6006# }
6007# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006008
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006009# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006010# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006011# defined $stat &&
6012# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6013# WARN("PREFER_ETHER_ADDR_EQUAL",
6014# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6015# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006016
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006017# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6018# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006019# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006020# defined $stat &&
6021# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6022#
6023# my $ms_val = $7;
6024#
6025# if ($ms_val =~ /^(?:0x|)0+$/i) {
6026# if (WARN("PREFER_ETH_ZERO_ADDR",
6027# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6028# $fix) {
6029# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6030# }
6031# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6032# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6033# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6034# $fix) {
6035# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6036# }
6037# }
6038# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006039
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006040# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006041 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006042 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006043 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006044 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006045 my $call = $1;
6046 my $cast1 = deparenthesize($2);
6047 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006048 my $cast2 = deparenthesize($7);
6049 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006050 my $cast;
6051
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006052 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006053 $cast = "$cast1 or $cast2";
6054 } elsif ($cast1 ne "") {
6055 $cast = $cast1;
6056 } else {
6057 $cast = $cast2;
6058 }
6059 WARN("MINMAX",
6060 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006061 }
6062 }
6063
Joe Perches4a273192012-07-30 14:41:20 -07006064# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006065 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006066 defined $stat &&
6067 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6068 my $min = $1;
6069 my $max = $7;
6070 if ($min eq $max) {
6071 WARN("USLEEP_RANGE",
6072 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6073 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6074 $min > $max) {
6075 WARN("USLEEP_RANGE",
6076 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6077 }
6078 }
6079
Joe Perches823b7942013-11-12 15:10:15 -08006080# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006081 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006082 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006083 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006084 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6085 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6086 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6087 my $lc = $stat =~ tr@\n@@;
6088 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006089 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006090 WARN("NAKED_SSCANF",
6091 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6092 }
6093
Joe Perchesafc819a2014-06-04 16:12:08 -07006094# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006095 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006096 defined $stat &&
6097 $line =~ /\bsscanf\b/) {
6098 my $lc = $stat =~ tr@\n@@;
6099 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006100 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006101 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6102 my $format = $6;
6103 my $count = $format =~ tr@%@%@;
6104 if ($count == 1 &&
6105 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6106 WARN("SSCANF_TO_KSTRTO",
6107 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6108 }
6109 }
6110 }
6111
Joe Perches70dc8a42013-09-11 14:23:58 -07006112# check for new externs in .h files.
6113 if ($realfile =~ /\.h$/ &&
6114 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006115 if (CHK("AVOID_EXTERNS",
6116 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006117 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006118 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006119 }
6120 }
6121
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006122# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006123 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006124 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006125 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006126 my $function_name = $1;
6127 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006128
6129 my $s = $stat;
6130 if (defined $cond) {
6131 substr($s, 0, length($cond), '');
6132 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006133 if ($s =~ /^\s*;/ &&
6134 $function_name ne 'uninitialized_var')
6135 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006136 WARN("AVOID_EXTERNS",
6137 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006138 }
6139
6140 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006141 WARN("FUNCTION_ARGUMENTS",
6142 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006143 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006144
6145 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6146 $stat =~ /^.\s*extern\s+/)
6147 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006148 WARN("AVOID_EXTERNS",
6149 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006150 }
6151
Joe Perchesa0ad7592017-07-10 15:52:19 -07006152# check for function declarations that have arguments without identifier names
6153 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006154 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006155 $1 ne "void") {
6156 my $args = trim($1);
6157 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6158 my $arg = trim($1);
6159 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6160 WARN("FUNCTION_ARGUMENTS",
6161 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6162 }
6163 }
6164 }
6165
Joe Perchesa0ad7592017-07-10 15:52:19 -07006166# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006167 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006168 defined $stat &&
6169 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6170 $context_function = $1;
6171
6172# check for multiline function definition with misplaced open brace
6173 my $ok = 0;
6174 my $cnt = statement_rawlines($stat);
6175 my $herectx = $here . "\n";
6176 for (my $n = 0; $n < $cnt; $n++) {
6177 my $rl = raw_line($linenr, $n);
6178 $herectx .= $rl . "\n";
6179 $ok = 1 if ($rl =~ /^[ \+]\{/);
6180 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6181 last if $rl =~ /^[ \+].*\{/;
6182 }
6183 if (!$ok) {
6184 ERROR("OPEN_BRACE",
6185 "open brace '{' following function definitions go on the next line\n" . $herectx);
6186 }
6187 }
6188
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006189# checks for new __setup's
6190 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6191 my $name = $1;
6192
6193 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006194 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006195 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006196 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006197 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006198
6199# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006200 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006201 WARN("UNNECESSARY_CASTS",
6202 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006203 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006204
Joe Perchesa640d252013-07-03 15:05:21 -07006205# alloc style
6206# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006207 if ($perl_version_ok &&
Joe Perchesa640d252013-07-03 15:05:21 -07006208 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6209 CHK("ALLOC_SIZEOF_STRUCT",
6210 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6211 }
6212
Joe Perches60a55362014-06-04 16:12:07 -07006213# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006214 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006215 defined $stat &&
6216 $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 -07006217 my $oldfunc = $3;
6218 my $a1 = $4;
6219 my $a2 = $10;
6220 my $newfunc = "kmalloc_array";
6221 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006222 my $r1 = $a1;
6223 my $r2 = $a2;
6224 if ($a1 =~ /^sizeof\s*\S/) {
6225 $r1 = $a2;
6226 $r2 = $a1;
6227 }
6228 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6229 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006230 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006231 my $herectx = get_stat_here($linenr, $cnt, $here);
6232
Joe Perches60a55362014-06-04 16:12:07 -07006233 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006234 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6235 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006236 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006237 $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 -07006238 }
6239 }
6240 }
6241
Joe Perches972fdea2013-04-29 16:18:12 -07006242# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006243 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006244 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6245 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006246 WARN("KREALLOC_ARG_REUSE",
6247 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6248 }
6249
Joe Perches5ce59ae2013-02-21 16:44:18 -08006250# check for alloc argument mismatch
6251 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6252 WARN("ALLOC_ARRAY_ARGS",
6253 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6254 }
6255
Joe Perchescaf2a542011-01-12 16:59:56 -08006256# check for multiple semicolons
6257 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006258 if (WARN("ONE_SEMICOLON",
6259 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6260 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006261 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006262 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006263 }
6264
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006265# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6266 if ($realfile !~ m@^include/uapi/@ &&
6267 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006268 my $ull = "";
6269 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6270 if (CHK("BIT_MACRO",
6271 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6272 $fix) {
6273 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6274 }
6275 }
6276
Joe Perches2d632742016-05-20 17:04:00 -07006277# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6278 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6279 my $config = $1;
6280 if (WARN("PREFER_IS_ENABLED",
6281 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6282 $fix) {
6283 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6284 }
6285 }
6286
Joe Perchese81f2392014-08-06 16:11:25 -07006287# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006288 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6289 my $has_break = 0;
6290 my $has_statement = 0;
6291 my $count = 0;
6292 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006293 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006294 $prevline--;
6295 my $rline = $rawlines[$prevline - 1];
6296 my $fline = $lines[$prevline - 1];
6297 last if ($fline =~ /^\@\@/);
6298 next if ($fline =~ /^\-/);
6299 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6300 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6301 next if ($fline =~ /^.[\s$;]*$/);
6302 $has_statement = 1;
6303 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006304 $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 -08006305 }
6306 if (!$has_break && $has_statement) {
6307 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006308 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006309 }
6310 }
6311
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006312# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006313 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006314 defined $stat &&
6315 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006316 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006317 my $herectx = get_stat_here($linenr, $cnt, $here);
6318
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006319 WARN("DEFAULT_NO_BREAK",
6320 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006321 }
6322
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006323# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006324 if ($line =~ /\b__FUNCTION__\b/) {
6325 if (WARN("USE_FUNC",
6326 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6327 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006328 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006329 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006330 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006331
Joe Perches62ec8182015-02-13 14:38:18 -08006332# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6333 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6334 ERROR("DATE_TIME",
6335 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6336 }
6337
Joe Perches2c924882012-03-23 15:02:20 -07006338# check for use of yield()
6339 if ($line =~ /\byield\s*\(\s*\)/) {
6340 WARN("YIELD",
6341 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6342 }
6343
Joe Perches179f8f42013-07-03 15:05:30 -07006344# check for comparisons against true and false
6345 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6346 my $lead = $1;
6347 my $arg = $2;
6348 my $test = $3;
6349 my $otype = $4;
6350 my $trail = $5;
6351 my $op = "!";
6352
6353 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6354
6355 my $type = lc($otype);
6356 if ($type =~ /^(?:true|false)$/) {
6357 if (("$test" eq "==" && "$type" eq "true") ||
6358 ("$test" eq "!=" && "$type" eq "false")) {
6359 $op = "";
6360 }
6361
6362 CHK("BOOL_COMPARISON",
6363 "Using comparison to $otype is error prone\n" . $herecurr);
6364
6365## maybe suggesting a correct construct would better
6366## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6367
6368 }
6369 }
6370
Joe Perches5d430902018-04-10 16:34:25 -07006371# check for bool bitfields
6372 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6373 WARN("BOOL_BITFIELD",
6374 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6375 }
6376
Joe Perchesd7295932018-08-21 21:57:26 -07006377# check for bool use in .h files
6378 if ($realfile =~ /\.h$/ &&
6379 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6380 CHK("BOOL_MEMBER",
6381 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6382 }
6383
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006384# check for semaphores initialized locked
6385 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006386 WARN("CONSIDER_COMPLETION",
6387 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006388 }
Joe Perches6712d852012-03-23 15:02:20 -07006389
Joe Perches67d0a072011-10-31 17:13:10 -07006390# recommend kstrto* over simple_strto* and strict_strto*
6391 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006392 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006393 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006394 }
Joe Perches6712d852012-03-23 15:02:20 -07006395
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006396# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006397 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006398 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006399 "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 -07006400 }
Joe Perches6712d852012-03-23 15:02:20 -07006401
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006402# check for spin_is_locked(), suggest lockdep instead
6403 if ($line =~ /\bspin_is_locked\(/) {
6404 WARN("USE_LOCKDEP",
6405 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6406 }
6407
Joe Perches9189c7e2018-09-07 15:26:18 -07006408# check for deprecated apis
6409 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6410 my $deprecated_api = $1;
6411 my $new_api = $deprecated_apis{$deprecated_api};
6412 WARN("DEPRECATED_API",
6413 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6414 }
6415
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006416# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006417# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006418 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006419 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006420 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006421 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006422 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006423
6424# use of NR_CPUS is usually wrong
6425# ignore definitions of NR_CPUS and usage to define arrays as likely right
6426 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006427 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6428 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006429 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6430 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6431 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006432 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006433 WARN("NR_CPUS",
6434 "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 -07006435 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006436
Joe Perches52ea8502013-11-12 15:10:09 -08006437# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6438 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6439 ERROR("DEFINE_ARCH_HAS",
6440 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6441 }
6442
Joe Perchesacd93622015-02-13 14:38:38 -08006443# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006444 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006445 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6446 WARN("LIKELY_MISUSE",
6447 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6448 }
6449
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006450# whine mightly about in_atomic
6451 if ($line =~ /\bin_atomic\s*\(/) {
6452 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006453 ERROR("IN_ATOMIC",
6454 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006455 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006456 WARN("IN_ATOMIC",
6457 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006458 }
6459 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006460
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006461# check for mutex_trylock_recursive usage
6462 if ($line =~ /mutex_trylock_recursive/) {
6463 ERROR("LOCKING",
6464 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6465 }
6466
Peter Zijlstra1704f472010-03-19 01:37:42 +01006467# check for lockdep_set_novalidate_class
6468 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6469 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6470 if ($realfile !~ m@^kernel/lockdep@ &&
6471 $realfile !~ m@^include/linux/lockdep@ &&
6472 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006473 ERROR("LOCKDEP",
6474 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006475 }
6476 }
Dave Jones88f88312011-01-12 16:59:59 -08006477
Joe Perchesb392c642015-04-16 12:44:16 -07006478 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6479 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006480 WARN("EXPORTED_WORLD_WRITABLE",
6481 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006482 }
Joe Perches24358802014-04-03 14:49:13 -07006483
Joe Perches00180462018-02-06 15:38:55 -08006484# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6485# and whether or not function naming is typical and if
6486# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006487 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006488 defined $stat &&
6489 $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*\)/) {
6490 my $var = $1;
6491 my $perms = $2;
6492 my $show = $3;
6493 my $store = $4;
6494 my $octal_perms = perms_to_octal($perms);
6495 if ($show =~ /^${var}_show$/ &&
6496 $store =~ /^${var}_store$/ &&
6497 $octal_perms eq "0644") {
6498 if (WARN("DEVICE_ATTR_RW",
6499 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6500 $fix) {
6501 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6502 }
6503 } elsif ($show =~ /^${var}_show$/ &&
6504 $store =~ /^NULL$/ &&
6505 $octal_perms eq "0444") {
6506 if (WARN("DEVICE_ATTR_RO",
6507 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6508 $fix) {
6509 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6510 }
6511 } elsif ($show =~ /^NULL$/ &&
6512 $store =~ /^${var}_store$/ &&
6513 $octal_perms eq "0200") {
6514 if (WARN("DEVICE_ATTR_WO",
6515 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6516 $fix) {
6517 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6518 }
6519 } elsif ($octal_perms eq "0644" ||
6520 $octal_perms eq "0444" ||
6521 $octal_perms eq "0200") {
6522 my $newshow = "$show";
6523 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6524 my $newstore = $store;
6525 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6526 my $rename = "";
6527 if ($show ne $newshow) {
6528 $rename .= " '$show' to '$newshow'";
6529 }
6530 if ($store ne $newstore) {
6531 $rename .= " '$store' to '$newstore'";
6532 }
6533 WARN("DEVICE_ATTR_FUNCTIONS",
6534 "Consider renaming function(s)$rename\n" . $herecurr);
6535 } else {
6536 WARN("DEVICE_ATTR_PERMS",
6537 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6538 }
6539 }
6540
Joe Perches515a2352014-04-03 14:49:24 -07006541# Mode permission misuses where it seems decimal should be octal
6542# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006543# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6544# specific definition of not visible in sysfs.
6545# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6546# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006547 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006548 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006549 $line =~ /$mode_perms_search/) {
6550 foreach my $entry (@mode_permission_funcs) {
6551 my $func = $entry->[0];
6552 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006553
Joe Perches459cf0a2016-10-11 13:52:19 -07006554 my $lc = $stat =~ tr@\n@@;
6555 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006556 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006557
Joe Perches515a2352014-04-03 14:49:24 -07006558 my $skip_args = "";
6559 if ($arg_pos > 1) {
6560 $arg_pos--;
6561 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6562 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006563 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006564 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006565 my $val = $1;
6566 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006567 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6568 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6569 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006570 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006571 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006572 }
6573 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006574 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006575 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006576 }
Joe Perches24358802014-04-03 14:49:13 -07006577 }
6578 }
6579 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006580
Joe Perches459cf0a2016-10-11 13:52:19 -07006581# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006582 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006583 my $oval = $1;
6584 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006585 if (WARN("SYMBOLIC_PERMS",
6586 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6587 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006588 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006589 }
6590 }
6591
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006592# validate content of MODULE_LICENSE against list from include/linux/module.h
6593 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6594 my $extracted_string = get_quoted_string($line, $rawline);
6595 my $valid_licenses = qr{
6596 GPL|
6597 GPL\ v2|
6598 GPL\ and\ additional\ rights|
6599 Dual\ BSD/GPL|
6600 Dual\ MIT/GPL|
6601 Dual\ MPL/GPL|
6602 Proprietary
6603 }x;
6604 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6605 WARN("MODULE_LICENSE",
6606 "unknown module license " . $extracted_string . "\n" . $herecurr);
6607 }
6608 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006609 }
6610
6611 # If we have no input at all, then there is nothing to report on
6612 # so just keep quiet.
6613 if ($#rawlines == -1) {
6614 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006615 }
6616
Andy Whitcroft8905a672007-11-28 16:21:06 -08006617 # In mailback mode only produce a report in the negative, for
6618 # things that appear to be patches.
6619 if ($mailback && ($clean == 1 || !$is_patch)) {
6620 exit(0);
6621 }
6622
6623 # This is not a patch, and we are are in 'no-patch' mode so
6624 # just keep quiet.
6625 if (!$chk_patch && !$is_patch) {
6626 exit(0);
6627 }
6628
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006629 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006630 ERROR("NOT_UNIFIED_DIFF",
6631 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006632 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006633 if ($is_patch && $has_commit_log && $chk_signoff) {
6634 if ($signoff == 0) {
6635 ERROR("MISSING_SIGN_OFF",
6636 "Missing Signed-off-by: line(s)\n");
6637 } elsif (!$authorsignoff) {
6638 WARN("NO_AUTHOR_SIGN_OFF",
6639 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6640 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006641 }
6642
Andy Whitcroft8905a672007-11-28 16:21:06 -08006643 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006644 if ($summary && !($clean == 1 && $quiet == 1)) {
6645 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006646 print "total: $cnt_error errors, $cnt_warn warnings, " .
6647 (($check)? "$cnt_chk checks, " : "") .
6648 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006649 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006650
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006651 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006652 # If there were any defects found and not already fixing them
6653 if (!$clean and !$fix) {
6654 print << "EOM"
6655
6656NOTE: For some of the reported defects, checkpatch may be able to
6657 mechanically convert to the typical style using --fix or --fix-inplace.
6658EOM
6659 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006660 # If there were whitespace errors which cleanpatch can fix
6661 # then suggest that.
6662 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006663 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006664 print << "EOM"
6665
6666NOTE: Whitespace errors detected.
6667 You may wish to use scripts/cleanpatch or scripts/cleanfile
6668EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006669 }
6670 }
6671
Joe Perchesd752fcc2014-08-06 16:11:05 -07006672 if ($clean == 0 && $fix &&
6673 ("@rawlines" ne "@fixed" ||
6674 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006675 my $newfile = $filename;
6676 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006677 my $linecount = 0;
6678 my $f;
6679
Joe Perchesd752fcc2014-08-06 16:11:05 -07006680 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6681
Joe Perches3705ce52013-07-03 15:05:31 -07006682 open($f, '>', $newfile)
6683 or die "$P: Can't open $newfile for write\n";
6684 foreach my $fixed_line (@fixed) {
6685 $linecount++;
6686 if ($file) {
6687 if ($linecount > 3) {
6688 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006689 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006690 }
6691 } else {
6692 print $f $fixed_line . "\n";
6693 }
6694 }
6695 close($f);
6696
6697 if (!$quiet) {
6698 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006699
Joe Perches3705ce52013-07-03 15:05:31 -07006700Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6701
6702Do _NOT_ trust the results written to this file.
6703Do _NOT_ submit these changes without inspecting them for correctness.
6704
6705This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6706No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006707EOM
6708 }
6709 }
6710
Joe Perchesd8469f12015-06-25 15:03:00 -07006711 if ($quiet == 0) {
6712 print "\n";
6713 if ($clean == 1) {
6714 print "$vname has no obvious style problems and is ready for submission.\n";
6715 } else {
6716 print "$vname has style problems, please review.\n";
6717 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006718 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006719 return $clean;
6720}