blob: 76748ab23ce96cb737223e3174cfeefc32bc8194 [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 Perchese29a70f2019-03-07 16:28:35 -0800469our $allocFunctions = qr{(?x:
470 (?:(?:devm_)?
471 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
472 kstrdup(?:_const)? |
473 kmemdup(?:_nul)?) |
474 (?:\w+)?alloc_skb(?:ip_align)? |
475 # dev_alloc_skb/netdev_alloc_skb, et al
476 dma_alloc_coherent
477)};
478
Joe Perches20112472011-07-25 17:13:23 -0700479our $signature_tags = qr{(?xi:
480 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800481 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700482 Acked-by:|
483 Tested-by:|
484 Reviewed-by:|
485 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700486 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700487 To:|
488 Cc:
489)};
490
Joe Perches18130872014-08-06 16:11:22 -0700491our @typeListMisordered = (
492 qr{char\s+(?:un)?signed},
493 qr{int\s+(?:(?:un)?signed\s+)?short\s},
494 qr{int\s+short(?:\s+(?:un)?signed)},
495 qr{short\s+int(?:\s+(?:un)?signed)},
496 qr{(?:un)?signed\s+int\s+short},
497 qr{short\s+(?:un)?signed},
498 qr{long\s+int\s+(?:un)?signed},
499 qr{int\s+long\s+(?:un)?signed},
500 qr{long\s+(?:un)?signed\s+int},
501 qr{int\s+(?:un)?signed\s+long},
502 qr{int\s+(?:un)?signed},
503 qr{int\s+long\s+long\s+(?:un)?signed},
504 qr{long\s+long\s+int\s+(?:un)?signed},
505 qr{long\s+long\s+(?:un)?signed\s+int},
506 qr{long\s+long\s+(?:un)?signed},
507 qr{long\s+(?:un)?signed},
508);
509
Andy Whitcroft8905a672007-11-28 16:21:06 -0800510our @typeList = (
511 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700512 qr{(?:(?:un)?signed\s+)?char},
513 qr{(?:(?:un)?signed\s+)?short\s+int},
514 qr{(?:(?:un)?signed\s+)?short},
515 qr{(?:(?:un)?signed\s+)?int},
516 qr{(?:(?:un)?signed\s+)?long\s+int},
517 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
518 qr{(?:(?:un)?signed\s+)?long\s+long},
519 qr{(?:(?:un)?signed\s+)?long},
520 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800521 qr{float},
522 qr{double},
523 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800524 qr{struct\s+$Ident},
525 qr{union\s+$Ident},
526 qr{enum\s+$Ident},
527 qr{${Ident}_t},
528 qr{${Ident}_handler},
529 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700530 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800531);
Joe Perches938224b2016-01-20 14:59:15 -0800532
533our $C90_int_types = qr{(?x:
534 long\s+long\s+int\s+(?:un)?signed|
535 long\s+long\s+(?:un)?signed\s+int|
536 long\s+long\s+(?:un)?signed|
537 (?:(?:un)?signed\s+)?long\s+long\s+int|
538 (?:(?:un)?signed\s+)?long\s+long|
539 int\s+long\s+long\s+(?:un)?signed|
540 int\s+(?:(?:un)?signed\s+)?long\s+long|
541
542 long\s+int\s+(?:un)?signed|
543 long\s+(?:un)?signed\s+int|
544 long\s+(?:un)?signed|
545 (?:(?:un)?signed\s+)?long\s+int|
546 (?:(?:un)?signed\s+)?long|
547 int\s+long\s+(?:un)?signed|
548 int\s+(?:(?:un)?signed\s+)?long|
549
550 int\s+(?:un)?signed|
551 (?:(?:un)?signed\s+)?int
552)};
553
Alex Dowad485ff232015-06-25 15:02:52 -0700554our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700555our @typeListWithAttr = (
556 @typeList,
557 qr{struct\s+$InitAttribute\s+$Ident},
558 qr{union\s+$InitAttribute\s+$Ident},
559);
560
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700561our @modifierList = (
562 qr{fastcall},
563);
Alex Dowad485ff232015-06-25 15:02:52 -0700564our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800565
Joe Perches24358802014-04-03 14:49:13 -0700566our @mode_permission_funcs = (
567 ["module_param", 3],
568 ["module_param_(?:array|named|string)", 4],
569 ["module_param_array_named", 5],
570 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
571 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700572 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
573 ["IIO_DEV_ATTR_[A-Z_]+", 1],
574 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
575 ["SENSOR_TEMPLATE(?:_2|)", 3],
576 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700577);
578
Joe Perches515a2352014-04-03 14:49:24 -0700579#Create a search pattern for all these functions to speed up a loop below
580our $mode_perms_search = "";
581foreach my $entry (@mode_permission_funcs) {
582 $mode_perms_search .= '|' if ($mode_perms_search ne "");
583 $mode_perms_search .= $entry->[0];
584}
Joe Perches00180462018-02-06 15:38:55 -0800585$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700586
Joe Perches9189c7e2018-09-07 15:26:18 -0700587our %deprecated_apis = (
588 "synchronize_rcu_bh" => "synchronize_rcu",
589 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
590 "call_rcu_bh" => "call_rcu",
591 "rcu_barrier_bh" => "rcu_barrier",
592 "synchronize_sched" => "synchronize_rcu",
593 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
594 "call_rcu_sched" => "call_rcu",
595 "rcu_barrier_sched" => "rcu_barrier",
596 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
597 "cond_synchronize_sched" => "cond_synchronize_rcu",
598);
599
600#Create a search pattern for all these strings to speed up a loop below
601our $deprecated_apis_search = "";
602foreach my $entry (keys %deprecated_apis) {
603 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
604 $deprecated_apis_search .= $entry;
605}
606$deprecated_apis_search = "(?:${deprecated_apis_search})";
607
Joe Perchesb392c642015-04-16 12:44:16 -0700608our $mode_perms_world_writable = qr{
609 S_IWUGO |
610 S_IWOTH |
611 S_IRWXUGO |
612 S_IALLUGO |
613 0[0-7][0-7][2367]
614}x;
615
Joe Perchesf90774e2016-10-11 13:51:47 -0700616our %mode_permission_string_types = (
617 "S_IRWXU" => 0700,
618 "S_IRUSR" => 0400,
619 "S_IWUSR" => 0200,
620 "S_IXUSR" => 0100,
621 "S_IRWXG" => 0070,
622 "S_IRGRP" => 0040,
623 "S_IWGRP" => 0020,
624 "S_IXGRP" => 0010,
625 "S_IRWXO" => 0007,
626 "S_IROTH" => 0004,
627 "S_IWOTH" => 0002,
628 "S_IXOTH" => 0001,
629 "S_IRWXUGO" => 0777,
630 "S_IRUGO" => 0444,
631 "S_IWUGO" => 0222,
632 "S_IXUGO" => 0111,
633);
634
635#Create a search pattern for all these strings to speed up a loop below
636our $mode_perms_string_search = "";
637foreach my $entry (keys %mode_permission_string_types) {
638 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
639 $mode_perms_string_search .= $entry;
640}
Joe Perches00180462018-02-06 15:38:55 -0800641our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
642our $multi_mode_perms_string_search = qr{
643 ${single_mode_perms_string_search}
644 (?:\s*\|\s*${single_mode_perms_string_search})*
645}x;
646
647sub perms_to_octal {
648 my ($string) = @_;
649
650 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
651
652 my $val = "";
653 my $oval = "";
654 my $to = 0;
655 my $curpos = 0;
656 my $lastpos = 0;
657 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
658 $curpos = pos($string);
659 my $match = $2;
660 my $omatch = $1;
661 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
662 $lastpos = $curpos;
663 $to |= $mode_permission_string_types{$match};
664 $val .= '\s*\|\s*' if ($val ne "");
665 $val .= $match;
666 $oval .= $omatch;
667 }
668 $oval =~ s/^\s*\|\s*//;
669 $oval =~ s/\s*\|\s*$//;
670 return sprintf("%04o", $to);
671}
Joe Perchesf90774e2016-10-11 13:51:47 -0700672
Wolfram Sang7840a942010-08-09 17:20:57 -0700673our $allowed_asm_includes = qr{(?x:
674 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700675 memory|
676 time|
677 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700678)};
679# memory.h: ARM has a custom one
680
Kees Cook66b47b42014-10-13 15:51:57 -0700681# Load common spelling mistakes and build regular expression list.
682my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700683my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700684
Joe Perches36061e32014-12-10 15:51:43 -0800685if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800686 while (<$spelling>) {
687 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700688
Joe Perches36061e32014-12-10 15:51:43 -0800689 $line =~ s/\s*\n?$//g;
690 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700691
Joe Perches36061e32014-12-10 15:51:43 -0800692 next if ($line =~ m/^\s*#/);
693 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700694
Joe Perches36061e32014-12-10 15:51:43 -0800695 my ($suspect, $fix) = split(/\|\|/, $line);
696
Joe Perches36061e32014-12-10 15:51:43 -0800697 $spelling_fix{$suspect} = $fix;
698 }
699 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800700} else {
701 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700702}
Kees Cook66b47b42014-10-13 15:51:57 -0700703
Joe Perchesebfd7d62015-04-16 12:44:14 -0700704if ($codespell) {
705 if (open(my $spelling, '<', $codespellfile)) {
706 while (<$spelling>) {
707 my $line = $_;
708
709 $line =~ s/\s*\n?$//g;
710 $line =~ s/^\s*//g;
711
712 next if ($line =~ m/^\s*#/);
713 next if ($line =~ m/^\s*$/);
714 next if ($line =~ m/, disabled/i);
715
716 $line =~ s/,.*$//;
717
718 my ($suspect, $fix) = split(/->/, $line);
719
720 $spelling_fix{$suspect} = $fix;
721 }
722 close($spelling);
723 } else {
724 warn "No codespell typos will be found - file '$codespellfile': $!\n";
725 }
726}
727
728$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
729
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700730sub read_words {
731 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700732
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700733 if (open(my $words, '<', $file)) {
734 while (<$words>) {
735 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700736
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700737 $line =~ s/\s*\n?$//g;
738 $line =~ s/^\s*//g;
739
740 next if ($line =~ m/^\s*#/);
741 next if ($line =~ m/^\s*$/);
742 if ($line =~ /\s/) {
743 print("$file: '$line' invalid - ignored\n");
744 next;
745 }
746
747 $$wordsRef .= '|' if ($$wordsRef ne "");
748 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700749 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700750 close($file);
751 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700752 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700753
754 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700755}
756
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700757my $const_structs = "";
758read_words(\$const_structs, $conststructsfile)
759 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
760
761my $typeOtherTypedefs = "";
762if (length($typedefsfile)) {
763 read_words(\$typeOtherTypedefs, $typedefsfile)
764 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
765}
766$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
767
Andy Whitcroft8905a672007-11-28 16:21:06 -0800768sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700769 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
770 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700771 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700772 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700773 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700774 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700775 (?:$typeTypedefs\b)|
776 (?:${all}\b)
777 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800778 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700779 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800780 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800781 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700782 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700783 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800784 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700785 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800786 }x;
Joe Perches18130872014-08-06 16:11:22 -0700787 $NonptrTypeMisordered = qr{
788 (?:$Modifier\s+|const\s+)*
789 (?:
790 (?:${Misordered}\b)
791 )
792 (?:\s+$Modifier|\s+const)*
793 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700794 $NonptrTypeWithAttr = qr{
795 (?:$Modifier\s+|const\s+)*
796 (?:
797 (?:typeof|__typeof__)\s*\([^\)]*\)|
798 (?:$typeTypedefs\b)|
799 (?:${allWithAttr}\b)
800 )
801 (?:\s+$Modifier|\s+const)*
802 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800803 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700804 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700805 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700806 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800807 }x;
Joe Perches18130872014-08-06 16:11:22 -0700808 $TypeMisordered = qr{
809 $NonptrTypeMisordered
810 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
811 (?:\s+$Inline|\s+$Modifier)*
812 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700813 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700814 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800815}
816build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700817
Joe Perches7d2367a2011-07-25 17:13:22 -0700818our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700819
820# Using $balanced_parens, $LvalOrFunc, or $FuncArg
821# requires at least perl version v5.10.0
822# Any use must be runtime checked with $^V
823
824our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700825our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800826our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700827
Joe Perchesf8422302014-08-06 16:11:31 -0700828our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700829 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700830 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700831 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
832 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700833)};
834
Joe Perches7d2367a2011-07-25 17:13:22 -0700835sub deparenthesize {
836 my ($string) = @_;
837 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700838
839 while ($string =~ /^\s*\(.*\)\s*$/) {
840 $string =~ s@^\s*\(\s*@@;
841 $string =~ s@\s*\)\s*$@@;
842 }
843
Joe Perches7d2367a2011-07-25 17:13:22 -0700844 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700845
Joe Perches7d2367a2011-07-25 17:13:22 -0700846 return $string;
847}
848
Joe Perches34456862013-07-03 15:05:34 -0700849sub seed_camelcase_file {
850 my ($file) = @_;
851
852 return if (!(-f $file));
853
854 local $/;
855
856 open(my $include_file, '<', "$file")
857 or warn "$P: Can't read '$file' $!\n";
858 my $text = <$include_file>;
859 close($include_file);
860
861 my @lines = split('\n', $text);
862
863 foreach my $line (@lines) {
864 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
865 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
866 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800867 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
868 $camelcase{$1} = 1;
869 } 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 -0700870 $camelcase{$1} = 1;
871 }
872 }
873}
874
Joe Perches85b0ee12016-10-11 13:51:44 -0700875sub is_maintained_obsolete {
876 my ($filename) = @_;
877
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800878 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700879
Joe Perches0616efa2016-10-11 13:52:02 -0700880 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 -0700881
882 return $status =~ /obsolete/i;
883}
884
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700885sub is_SPDX_License_valid {
886 my ($license) = @_;
887
Joe Perches56294112018-08-21 21:58:04 -0700888 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700889
Joe Perches56294112018-08-21 21:58:04 -0700890 my $root_path = abs_path($root);
891 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700892 return 0 if ($status ne "");
893 return 1;
894}
895
Joe Perches34456862013-07-03 15:05:34 -0700896my $camelcase_seeded = 0;
897sub seed_camelcase_includes {
898 return if ($camelcase_seeded);
899
900 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700901 my $camelcase_cache = "";
902 my @include_files = ();
903
904 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700905
Richard Genoud3645e322014-02-10 14:25:32 -0800906 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700907 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
908 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700909 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700910 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700911 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700912 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700913 @include_files = split('\n', $files);
914 foreach my $file (@include_files) {
915 my $date = POSIX::strftime("%Y%m%d%H%M",
916 localtime((stat $file)[9]));
917 $last_mod_date = $date if ($last_mod_date < $date);
918 }
919 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700920 }
Joe Perchesc707a812013-07-08 16:00:43 -0700921
922 if ($camelcase_cache ne "" && -f $camelcase_cache) {
923 open(my $camelcase_file, '<', "$camelcase_cache")
924 or warn "$P: Can't read '$camelcase_cache' $!\n";
925 while (<$camelcase_file>) {
926 chomp;
927 $camelcase{$_} = 1;
928 }
929 close($camelcase_file);
930
931 return;
932 }
933
Richard Genoud3645e322014-02-10 14:25:32 -0800934 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700935 $files = `git ls-files "include/*.h"`;
936 @include_files = split('\n', $files);
937 }
938
Joe Perches34456862013-07-03 15:05:34 -0700939 foreach my $file (@include_files) {
940 seed_camelcase_file($file);
941 }
Joe Perches351b2a12013-07-03 15:05:36 -0700942
Joe Perchesc707a812013-07-08 16:00:43 -0700943 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700944 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700945 open(my $camelcase_file, '>', "$camelcase_cache")
946 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700947 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
948 print $camelcase_file ("$_\n");
949 }
950 close($camelcase_file);
951 }
Joe Perches34456862013-07-03 15:05:34 -0700952}
953
Joe Perchesd311cd42014-08-06 16:10:57 -0700954sub git_commit_info {
955 my ($commit, $id, $desc) = @_;
956
957 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
958
959 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
960 $output =~ s/^\s*//gm;
961 my @lines = split("\n", $output);
962
Joe Perches0d7835f2015-02-13 14:38:35 -0800963 return ($id, $desc) if ($#lines < 0);
964
Joe Perchesd311cd42014-08-06 16:10:57 -0700965 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
966# Maybe one day convert this block of bash into something that returns
967# all matching commit ids, but it's very slow...
968#
969# echo "checking commits $1..."
970# git rev-list --remotes | grep -i "^$1" |
971# while read line ; do
972# git log --format='%H %s' -1 $line |
973# echo "commit $(cut -c 1-12,41-)"
974# done
975 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700976 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700977 } else {
978 $id = substr($lines[0], 0, 12);
979 $desc = substr($lines[0], 41);
980 }
981
982 return ($id, $desc);
983}
984
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700985$chk_signoff = 0 if ($file);
986
Andy Whitcroft00df3442007-06-08 13:47:06 -0700987my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800988my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700989my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700990my @fixed_inserted = ();
991my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700992my $fixlinenr = -1;
993
Du, Changbin4a593c32016-05-20 17:04:16 -0700994# If input is git commits, extract all commits from the commit expressions.
995# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
996die "$P: No git repository found\n" if ($git && !-e ".git");
997
998if ($git) {
999 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001000 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001001 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001002 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1003 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001004 } elsif ($commit_expr =~ m/\.\./) {
1005 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001006 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001007 $git_range = "-1 $commit_expr";
1008 }
1009 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
1010 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001011 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1012 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001013 my $sha1 = $1;
1014 my $subject = $2;
1015 unshift(@commits, $sha1);
1016 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001017 }
1018 }
1019 die "$P: no git commits after extraction!\n" if (@commits == 0);
1020 @ARGV = @commits;
1021}
1022
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001023my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001025 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001026 if ($git) {
1027 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1028 die "$P: $filename: git format-patch failed - $!\n";
1029 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001030 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001031 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001032 } elsif ($filename eq '-') {
1033 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001034 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001035 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001036 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001037 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001038 if ($filename eq '-') {
1039 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001040 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001041 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001042 } else {
1043 $vname = $filename;
1044 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001045 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001046 chomp;
1047 push(@rawlines, $_);
1048 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001049 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001050
1051 if ($#ARGV > 0 && $quiet == 0) {
1052 print '-' x length($vname) . "\n";
1053 print "$vname\n";
1054 print '-' x length($vname) . "\n";
1055 }
1056
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001057 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001058 $exit = 1;
1059 }
1060 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001061 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001062 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001063 @fixed_inserted = ();
1064 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001065 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001066 @modifierListFile = ();
1067 @typeListFile = ();
1068 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001069}
1070
Joe Perchesd8469f12015-06-25 15:03:00 -07001071if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001072 hash_show_words(\%use_type, "Used");
1073 hash_show_words(\%ignore_type, "Ignored");
1074
Joe Perches5b579802018-08-21 21:57:33 -07001075 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001076 print << "EOM"
1077
1078NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001079 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001080EOM
1081 }
1082 if ($exit) {
1083 print << "EOM"
1084
1085NOTE: If any of the errors are false positives, please report
1086 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1087EOM
1088 }
1089}
1090
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001091exit($exit);
1092
1093sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001094 my ($root) = @_;
1095
1096 my @tree_check = (
1097 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1098 "README", "Documentation", "arch", "include", "drivers",
1099 "fs", "init", "ipc", "kernel", "lib", "scripts",
1100 );
1101
1102 foreach my $check (@tree_check) {
1103 if (! -e $root . '/' . $check) {
1104 return 0;
1105 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001106 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001107 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001108}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001109
Joe Perches20112472011-07-25 17:13:23 -07001110sub parse_email {
1111 my ($formatted_email) = @_;
1112
1113 my $name = "";
1114 my $address = "";
1115 my $comment = "";
1116
1117 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1118 $name = $1;
1119 $address = $2;
1120 $comment = $3 if defined $3;
1121 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1122 $address = $1;
1123 $comment = $2 if defined $2;
1124 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1125 $address = $1;
1126 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001127 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001128 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001129 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001130 $name =~ s/^\"|\"$//g;
1131 # If there's a name left after stripping spaces and
1132 # leading quotes, and the address doesn't have both
1133 # leading and trailing angle brackets, the address
1134 # is invalid. ie:
1135 # "joe smith joe@smith.com" bad
1136 # "joe smith <joe@smith.com" bad
1137 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1138 $name = "";
1139 $address = "";
1140 $comment = "";
1141 }
1142 }
1143
Joe Perches3705ce52013-07-03 15:05:31 -07001144 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001145 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001146 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001147 $address =~ s/^\<|\>$//g;
1148
1149 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1150 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1151 $name = "\"$name\"";
1152 }
1153
1154 return ($name, $address, $comment);
1155}
1156
1157sub format_email {
1158 my ($name, $address) = @_;
1159
1160 my $formatted_email;
1161
Joe Perches3705ce52013-07-03 15:05:31 -07001162 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001163 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001164 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001165
1166 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1167 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1168 $name = "\"$name\"";
1169 }
1170
1171 if ("$name" eq "") {
1172 $formatted_email = "$address";
1173 } else {
1174 $formatted_email = "$name <$address>";
1175 }
1176
1177 return $formatted_email;
1178}
1179
Joe Perchesd311cd42014-08-06 16:10:57 -07001180sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001181 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001182
Joe Perchesbd474ca2014-08-06 16:11:10 -07001183 foreach my $path (split(/:/, $ENV{PATH})) {
1184 if (-e "$path/$bin") {
1185 return "$path/$bin";
1186 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001187 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001188
Joe Perchesbd474ca2014-08-06 16:11:10 -07001189 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001190}
1191
Joe Perches000d1cc12011-07-25 17:13:25 -07001192sub which_conf {
1193 my ($conf) = @_;
1194
1195 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1196 if (-e "$path/$conf") {
1197 return "$path/$conf";
1198 }
1199 }
1200
1201 return "";
1202}
1203
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001204sub expand_tabs {
1205 my ($str) = @_;
1206
1207 my $res = '';
1208 my $n = 0;
1209 for my $c (split(//, $str)) {
1210 if ($c eq "\t") {
1211 $res .= ' ';
1212 $n++;
1213 for (; ($n % 8) != 0; $n++) {
1214 $res .= ' ';
1215 }
1216 next;
1217 }
1218 $res .= $c;
1219 $n++;
1220 }
1221
1222 return $res;
1223}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001224sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001225 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001226 return $res;
1227}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001228
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001229sub line_stats {
1230 my ($line) = @_;
1231
1232 # Drop the diff line leader and expand tabs
1233 $line =~ s/^.//;
1234 $line = expand_tabs($line);
1235
1236 # Pick the indent from the front of the line.
1237 my ($white) = ($line =~ /^(\s*)/);
1238
1239 return (length($line), length($white));
1240}
1241
Andy Whitcroft773647a2008-03-28 14:15:58 -07001242my $sanitise_quote = '';
1243
1244sub sanitise_line_reset {
1245 my ($in_comment) = @_;
1246
1247 if ($in_comment) {
1248 $sanitise_quote = '*/';
1249 } else {
1250 $sanitise_quote = '';
1251 }
1252}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001253sub sanitise_line {
1254 my ($line) = @_;
1255
1256 my $res = '';
1257 my $l = '';
1258
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001259 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001260 my $off = 0;
1261 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001262
Andy Whitcroft773647a2008-03-28 14:15:58 -07001263 # Always copy over the diff marker.
1264 $res = substr($line, 0, 1);
1265
1266 for ($off = 1; $off < length($line); $off++) {
1267 $c = substr($line, $off, 1);
1268
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001269 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001270 # and end, all to $;.
1271 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1272 $sanitise_quote = '*/';
1273
1274 substr($res, $off, 2, "$;$;");
1275 $off++;
1276 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001277 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001278 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001279 $sanitise_quote = '';
1280 substr($res, $off, 2, "$;$;");
1281 $off++;
1282 next;
1283 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001284 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1285 $sanitise_quote = '//';
1286
1287 substr($res, $off, 2, $sanitise_quote);
1288 $off++;
1289 next;
1290 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001291
1292 # A \ in a string means ignore the next character.
1293 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1294 $c eq "\\") {
1295 substr($res, $off, 2, 'XX');
1296 $off++;
1297 next;
1298 }
1299 # Regular quotes.
1300 if ($c eq "'" || $c eq '"') {
1301 if ($sanitise_quote eq '') {
1302 $sanitise_quote = $c;
1303
1304 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001305 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306 } elsif ($sanitise_quote eq $c) {
1307 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001308 }
1309 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001310
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001311 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001312 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1313 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001314 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1315 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001316 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1317 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001318 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001319 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001320 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001321 }
1322
Daniel Walker113f04a2009-09-21 17:04:35 -07001323 if ($sanitise_quote eq '//') {
1324 $sanitise_quote = '';
1325 }
1326
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001327 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001328 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001329 my $clean = 'X' x length($1);
1330 $res =~ s@\<.*\>@<$clean>@;
1331
1332 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001333 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001334 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001335 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001336 }
1337
Joe Perchesdadf6802016-08-02 14:04:33 -07001338 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1339 my $match = $1;
1340 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1341 }
1342
Andy Whitcroft00df3442007-06-08 13:47:06 -07001343 return $res;
1344}
1345
Joe Perchesa6962d72013-04-29 16:18:13 -07001346sub get_quoted_string {
1347 my ($line, $rawline) = @_;
1348
Joe Perches478b1792018-04-10 16:33:34 -07001349 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001350 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001351 return substr($rawline, $-[0], $+[0] - $-[0]);
1352}
1353
Andy Whitcroft8905a672007-11-28 16:21:06 -08001354sub ctx_statement_block {
1355 my ($linenr, $remain, $off) = @_;
1356 my $line = $linenr - 1;
1357 my $blk = '';
1358 my $soff = $off;
1359 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001360 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001361
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001362 my $loff = 0;
1363
Andy Whitcroft8905a672007-11-28 16:21:06 -08001364 my $type = '';
1365 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001366 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001367 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001368 my $c;
1369 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001370
1371 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001372 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001373 @stack = (['', 0]) if ($#stack == -1);
1374
Andy Whitcroft773647a2008-03-28 14:15:58 -07001375 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001376 # If we are about to drop off the end, pull in more
1377 # context.
1378 if ($off >= $len) {
1379 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001380 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001381 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001382 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001383 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001384 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001385 $len = length($blk);
1386 $line++;
1387 last;
1388 }
1389 # Bail if there is no further context.
1390 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001391 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001392 last;
1393 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001394 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1395 $level++;
1396 $type = '#';
1397 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001398 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001399 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001400 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001401 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001402
Andy Whitcroft773647a2008-03-28 14:15:58 -07001403 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001404
1405 # Handle nested #if/#else.
1406 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1407 push(@stack, [ $type, $level ]);
1408 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1409 ($type, $level) = @{$stack[$#stack - 1]};
1410 } elsif ($remainder =~ /^#\s*endif\b/) {
1411 ($type, $level) = @{pop(@stack)};
1412 }
1413
Andy Whitcroft8905a672007-11-28 16:21:06 -08001414 # Statement ends at the ';' or a close '}' at the
1415 # outermost level.
1416 if ($level == 0 && $c eq ';') {
1417 last;
1418 }
1419
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001420 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001421 if ($level == 0 && $coff_set == 0 &&
1422 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1423 $remainder =~ /^(else)(?:\s|{)/ &&
1424 $remainder !~ /^else\s+if\b/) {
1425 $coff = $off + length($1) - 1;
1426 $coff_set = 1;
1427 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1428 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001429 }
1430
Andy Whitcroft8905a672007-11-28 16:21:06 -08001431 if (($type eq '' || $type eq '(') && $c eq '(') {
1432 $level++;
1433 $type = '(';
1434 }
1435 if ($type eq '(' && $c eq ')') {
1436 $level--;
1437 $type = ($level != 0)? '(' : '';
1438
1439 if ($level == 0 && $coff < $soff) {
1440 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001441 $coff_set = 1;
1442 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443 }
1444 }
1445 if (($type eq '' || $type eq '{') && $c eq '{') {
1446 $level++;
1447 $type = '{';
1448 }
1449 if ($type eq '{' && $c eq '}') {
1450 $level--;
1451 $type = ($level != 0)? '{' : '';
1452
1453 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001454 if (substr($blk, $off + 1, 1) eq ';') {
1455 $off++;
1456 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001457 last;
1458 }
1459 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001460 # Preprocessor commands end at the newline unless escaped.
1461 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1462 $level--;
1463 $type = '';
1464 $off++;
1465 last;
1466 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001467 $off++;
1468 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001469 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001470 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001471 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001472 $line++;
1473 $remain--;
1474 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001475
1476 my $statement = substr($blk, $soff, $off - $soff + 1);
1477 my $condition = substr($blk, $soff, $coff - $soff + 1);
1478
1479 #warn "STATEMENT<$statement>\n";
1480 #warn "CONDITION<$condition>\n";
1481
Andy Whitcroft773647a2008-03-28 14:15:58 -07001482 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001483
1484 return ($statement, $condition,
1485 $line, $remain + 1, $off - $loff + 1, $level);
1486}
1487
Andy Whitcroftcf655042008-03-04 14:28:20 -08001488sub statement_lines {
1489 my ($stmt) = @_;
1490
1491 # Strip the diff line prefixes and rip blank lines at start and end.
1492 $stmt =~ s/(^|\n)./$1/g;
1493 $stmt =~ s/^\s*//;
1494 $stmt =~ s/\s*$//;
1495
1496 my @stmt_lines = ($stmt =~ /\n/g);
1497
1498 return $#stmt_lines + 2;
1499}
1500
1501sub statement_rawlines {
1502 my ($stmt) = @_;
1503
1504 my @stmt_lines = ($stmt =~ /\n/g);
1505
1506 return $#stmt_lines + 2;
1507}
1508
1509sub statement_block_size {
1510 my ($stmt) = @_;
1511
1512 $stmt =~ s/(^|\n)./$1/g;
1513 $stmt =~ s/^\s*{//;
1514 $stmt =~ s/}\s*$//;
1515 $stmt =~ s/^\s*//;
1516 $stmt =~ s/\s*$//;
1517
1518 my @stmt_lines = ($stmt =~ /\n/g);
1519 my @stmt_statements = ($stmt =~ /;/g);
1520
1521 my $stmt_lines = $#stmt_lines + 2;
1522 my $stmt_statements = $#stmt_statements + 1;
1523
1524 if ($stmt_lines > $stmt_statements) {
1525 return $stmt_lines;
1526 } else {
1527 return $stmt_statements;
1528 }
1529}
1530
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001531sub ctx_statement_full {
1532 my ($linenr, $remain, $off) = @_;
1533 my ($statement, $condition, $level);
1534
1535 my (@chunks);
1536
Andy Whitcroftcf655042008-03-04 14:28:20 -08001537 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001538 ($statement, $condition, $linenr, $remain, $off, $level) =
1539 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001540 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001541 push(@chunks, [ $condition, $statement ]);
1542 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1543 return ($level, $linenr, @chunks);
1544 }
1545
1546 # Pull in the following conditional/block pairs and see if they
1547 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001548 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001549 ($statement, $condition, $linenr, $remain, $off, $level) =
1550 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001551 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001552 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001553 #print "C: push\n";
1554 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001555 }
1556
1557 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001558}
1559
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001560sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001561 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001562 my $line;
1563 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001564 my $blk = '';
1565 my @o;
1566 my @c;
1567 my @res = ();
1568
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001569 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001570 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001571 for ($line = $start; $remain > 0; $line++) {
1572 next if ($rawlines[$line] =~ /^-/);
1573 $remain--;
1574
1575 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001576
1577 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001578 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001579 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001580 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001581 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001582 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001583 $level = pop(@stack);
1584 }
1585
Andy Whitcroft01464f32010-10-26 14:23:19 -07001586 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001587 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1588 if ($off > 0) {
1589 $off--;
1590 next;
1591 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001592
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001593 if ($c eq $close && $level > 0) {
1594 $level--;
1595 last if ($level == 0);
1596 } elsif ($c eq $open) {
1597 $level++;
1598 }
1599 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001600
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001601 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001602 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001603 }
1604
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001605 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001606 }
1607
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001608 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001609}
1610sub ctx_block_outer {
1611 my ($linenr, $remain) = @_;
1612
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001613 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1614 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001615}
1616sub ctx_block {
1617 my ($linenr, $remain) = @_;
1618
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001619 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1620 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001621}
1622sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001623 my ($linenr, $remain, $off) = @_;
1624
1625 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1626 return @r;
1627}
1628sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001629 my ($linenr, $remain) = @_;
1630
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001631 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001632}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001633sub ctx_statement_level {
1634 my ($linenr, $remain, $off) = @_;
1635
1636 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1637}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001638
1639sub ctx_locate_comment {
1640 my ($first_line, $end_line) = @_;
1641
1642 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001643 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001644 return $current_comment if (defined $current_comment);
1645
1646 # Look through the context and try and figure out if there is a
1647 # comment.
1648 my $in_comment = 0;
1649 $current_comment = '';
1650 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001651 my $line = $rawlines[$linenr - 1];
1652 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001653 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1654 $in_comment = 1;
1655 }
1656 if ($line =~ m@/\*@) {
1657 $in_comment = 1;
1658 }
1659 if (!$in_comment && $current_comment ne '') {
1660 $current_comment = '';
1661 }
1662 $current_comment .= $line . "\n" if ($in_comment);
1663 if ($line =~ m@\*/@) {
1664 $in_comment = 0;
1665 }
1666 }
1667
1668 chomp($current_comment);
1669 return($current_comment);
1670}
1671sub ctx_has_comment {
1672 my ($first_line, $end_line) = @_;
1673 my $cmt = ctx_locate_comment($first_line, $end_line);
1674
Andy Whitcroft00df3442007-06-08 13:47:06 -07001675 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001676 ##print "CMMT: $cmt\n";
1677
1678 return ($cmt ne '');
1679}
1680
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001681sub raw_line {
1682 my ($linenr, $cnt) = @_;
1683
1684 my $offset = $linenr - 1;
1685 $cnt++;
1686
1687 my $line;
1688 while ($cnt) {
1689 $line = $rawlines[$offset++];
1690 next if (defined($line) && $line =~ /^-/);
1691 $cnt--;
1692 }
1693
1694 return $line;
1695}
1696
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001697sub get_stat_real {
1698 my ($linenr, $lc) = @_;
1699
1700 my $stat_real = raw_line($linenr, 0);
1701 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1702 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1703 }
1704
1705 return $stat_real;
1706}
1707
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001708sub get_stat_here {
1709 my ($linenr, $cnt, $here) = @_;
1710
1711 my $herectx = $here . "\n";
1712 for (my $n = 0; $n < $cnt; $n++) {
1713 $herectx .= raw_line($linenr, $n) . "\n";
1714 }
1715
1716 return $herectx;
1717}
1718
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001719sub cat_vet {
1720 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001721 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001722
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001723 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001724 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1725 $res .= $1;
1726 if ($2 ne '') {
1727 $coded = sprintf("^%c", unpack('C', $2) + 64);
1728 $res .= $coded;
1729 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001730 }
1731 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001732
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001733 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001734}
1735
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001736my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001737my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001738my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001739my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001740
1741sub annotate_reset {
1742 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001743 $av_pending = '_';
1744 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001745 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001746}
1747
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001748sub annotate_values {
1749 my ($stream, $type) = @_;
1750
1751 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001752 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001753 my $cur = $stream;
1754
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001755 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001756
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001757 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001758 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001759 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001760 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001761 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001762 print "WS($1)\n" if ($dbg_values > 1);
1763 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001764 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001765 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001766 }
1767
Florian Micklerc023e4732011-01-12 16:59:58 -08001768 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001769 print "CAST($1)\n" if ($dbg_values > 1);
1770 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001771 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001772
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001773 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001774 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001775 $type = 'T';
1776
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001777 } elsif ($cur =~ /^($Modifier)\s*/) {
1778 print "MODIFIER($1)\n" if ($dbg_values > 1);
1779 $type = 'T';
1780
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001781 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001782 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001783 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001784 push(@av_paren_type, $type);
1785 if ($2 ne '') {
1786 $av_pending = 'N';
1787 }
1788 $type = 'E';
1789
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001790 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001791 print "UNDEF($1)\n" if ($dbg_values > 1);
1792 $av_preprocessor = 1;
1793 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001794
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001795 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001796 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001797 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001798
1799 push(@av_paren_type, $type);
1800 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001801 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001802
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001803 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001804 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1805 $av_preprocessor = 1;
1806
1807 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1808
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001809 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001810
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001811 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001812 print "PRE_END($1)\n" if ($dbg_values > 1);
1813
1814 $av_preprocessor = 1;
1815
1816 # Assume all arms of the conditional end as this
1817 # one does, and continue as if the #endif was not here.
1818 pop(@av_paren_type);
1819 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001820 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001821
1822 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001823 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001824
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001825 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1826 print "ATTR($1)\n" if ($dbg_values > 1);
1827 $av_pending = $type;
1828 $type = 'N';
1829
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001830 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001831 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001832 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001833 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001834 }
1835 $type = 'N';
1836
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001837 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001838 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001839 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001840 $type = 'N';
1841
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001842 } elsif ($cur =~/^(case)/o) {
1843 print "CASE($1)\n" if ($dbg_values > 1);
1844 $av_pend_colon = 'C';
1845 $type = 'N';
1846
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001847 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001848 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001849 $type = 'N';
1850
1851 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001852 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001853 push(@av_paren_type, $av_pending);
1854 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001855 $type = 'N';
1856
1857 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001858 my $new_type = pop(@av_paren_type);
1859 if ($new_type ne '_') {
1860 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001861 print "PAREN('$1') -> $type\n"
1862 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001863 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001864 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001865 }
1866
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001867 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001868 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001869 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001870 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001871
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001872 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1873 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001874 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001875 } elsif ($type eq 'E') {
1876 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001877 }
1878 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1879 $type = 'V';
1880
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001881 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001882 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001883 $type = 'V';
1884
1885 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001886 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001887 $type = 'N';
1888
Andy Whitcroftcf655042008-03-04 14:28:20 -08001889 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001890 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001891 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001892 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001893
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001894 } elsif ($cur =~/^(,)/) {
1895 print "COMMA($1)\n" if ($dbg_values > 1);
1896 $type = 'C';
1897
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001898 } elsif ($cur =~ /^(\?)/o) {
1899 print "QUESTION($1)\n" if ($dbg_values > 1);
1900 $type = 'N';
1901
1902 } elsif ($cur =~ /^(:)/o) {
1903 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1904
1905 substr($var, length($res), 1, $av_pend_colon);
1906 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1907 $type = 'E';
1908 } else {
1909 $type = 'N';
1910 }
1911 $av_pend_colon = 'O';
1912
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001913 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001914 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001915 $type = 'N';
1916
Andy Whitcroft0d413862008-10-15 22:02:16 -07001917 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001918 my $variant;
1919
1920 print "OPV($1)\n" if ($dbg_values > 1);
1921 if ($type eq 'V') {
1922 $variant = 'B';
1923 } else {
1924 $variant = 'U';
1925 }
1926
1927 substr($var, length($res), 1, $variant);
1928 $type = 'N';
1929
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001930 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001931 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001932 if ($1 ne '++' && $1 ne '--') {
1933 $type = 'N';
1934 }
1935
1936 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001937 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001938 }
1939 if (defined $1) {
1940 $cur = substr($cur, length($1));
1941 $res .= $type x length($1);
1942 }
1943 }
1944
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001945 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001946}
1947
Andy Whitcroft8905a672007-11-28 16:21:06 -08001948sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001949 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001950 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001951 ^(?:
1952 $Modifier|
1953 $Storage|
1954 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001955 DEFINE_\S+
1956 )$|
1957 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001958 goto|
1959 return|
1960 case|
1961 else|
1962 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001963 do|
1964 \#|
1965 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001966 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001967 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001968 )}x;
1969 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1970 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001971 # Check for modifiers.
1972 $possible =~ s/\s*$Storage\s*//g;
1973 $possible =~ s/\s*$Sparse\s*//g;
1974 if ($possible =~ /^\s*$/) {
1975
1976 } elsif ($possible =~ /\s/) {
1977 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001978 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001979 if ($modifier !~ $notPermitted) {
1980 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001981 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001982 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001983 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001984
1985 } else {
1986 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001987 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001988 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001989 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001990 } else {
1991 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001992 }
1993}
1994
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001995my $prefix = '';
1996
Joe Perches000d1cc12011-07-25 17:13:25 -07001997sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001998 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001999
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002000 $type =~ tr/[a-z]/[A-Z]/;
2001
Joe Perchescbec18a2014-04-03 14:49:19 -07002002 return defined $use_type{$type} if (scalar keys %use_type > 0);
2003
2004 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002005}
2006
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002007sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002008 my ($level, $type, $msg) = @_;
2009
2010 if (!show_type($type) ||
2011 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002012 return 0;
2013 }
Joe Perches57230292015-06-25 15:03:03 -07002014 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002015 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002016 if ($level eq 'ERROR') {
2017 $output .= RED;
2018 } elsif ($level eq 'WARNING') {
2019 $output .= YELLOW;
2020 } else {
2021 $output .= GREEN;
2022 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002023 }
Joe Perches57230292015-06-25 15:03:03 -07002024 $output .= $prefix . $level . ':';
2025 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002026 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002027 $output .= "$type:";
2028 }
John Brooks737c0762017-07-10 15:52:24 -07002029 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002030 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002031
2032 if ($showfile) {
2033 my @lines = split("\n", $output, -1);
2034 splice(@lines, 1, 1);
2035 $output = join("\n", @lines);
2036 }
Joe Perches57230292015-06-25 15:03:03 -07002037 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002038
Joe Perches57230292015-06-25 15:03:03 -07002039 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002040
2041 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002042}
Joe Perchescbec18a2014-04-03 14:49:19 -07002043
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002044sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002045 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002046}
Joe Perches000d1cc12011-07-25 17:13:25 -07002047
Joe Perchesd752fcc2014-08-06 16:11:05 -07002048sub fixup_current_range {
2049 my ($lineRef, $offset, $length) = @_;
2050
2051 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2052 my $o = $1;
2053 my $l = $2;
2054 my $no = $o + $offset;
2055 my $nl = $l + $length;
2056 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2057 }
2058}
2059
2060sub fix_inserted_deleted_lines {
2061 my ($linesRef, $insertedRef, $deletedRef) = @_;
2062
2063 my $range_last_linenr = 0;
2064 my $delta_offset = 0;
2065
2066 my $old_linenr = 0;
2067 my $new_linenr = 0;
2068
2069 my $next_insert = 0;
2070 my $next_delete = 0;
2071
2072 my @lines = ();
2073
2074 my $inserted = @{$insertedRef}[$next_insert++];
2075 my $deleted = @{$deletedRef}[$next_delete++];
2076
2077 foreach my $old_line (@{$linesRef}) {
2078 my $save_line = 1;
2079 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002080 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002081 $delta_offset = 0;
2082 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2083 $range_last_linenr = $new_linenr;
2084 fixup_current_range(\$line, $delta_offset, 0);
2085 }
2086
2087 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2088 $deleted = @{$deletedRef}[$next_delete++];
2089 $save_line = 0;
2090 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2091 }
2092
2093 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2094 push(@lines, ${$inserted}{'LINE'});
2095 $inserted = @{$insertedRef}[$next_insert++];
2096 $new_linenr++;
2097 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2098 }
2099
2100 if ($save_line) {
2101 push(@lines, $line);
2102 $new_linenr++;
2103 }
2104
2105 $old_linenr++;
2106 }
2107
2108 return @lines;
2109}
2110
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002111sub fix_insert_line {
2112 my ($linenr, $line) = @_;
2113
2114 my $inserted = {
2115 LINENR => $linenr,
2116 LINE => $line,
2117 };
2118 push(@fixed_inserted, $inserted);
2119}
2120
2121sub fix_delete_line {
2122 my ($linenr, $line) = @_;
2123
2124 my $deleted = {
2125 LINENR => $linenr,
2126 LINE => $line,
2127 };
2128
2129 push(@fixed_deleted, $deleted);
2130}
2131
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002132sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002133 my ($type, $msg) = @_;
2134
2135 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002136 our $clean = 0;
2137 our $cnt_error++;
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 WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002143 my ($type, $msg) = @_;
2144
2145 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002146 our $clean = 0;
2147 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002148 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002149 }
Joe Perches3705ce52013-07-03 15:05:31 -07002150 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002151}
2152sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002153 my ($type, $msg) = @_;
2154
2155 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002156 our $clean = 0;
2157 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002158 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002159 }
Joe Perches3705ce52013-07-03 15:05:31 -07002160 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002161}
2162
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002163sub check_absolute_file {
2164 my ($absolute, $herecurr) = @_;
2165 my $file = $absolute;
2166
2167 ##print "absolute<$absolute>\n";
2168
2169 # See if any suffix of this path is a path within the tree.
2170 while ($file =~ s@^[^/]*/@@) {
2171 if (-f "$root/$file") {
2172 ##print "file<$file>\n";
2173 last;
2174 }
2175 }
2176 if (! -f _) {
2177 return 0;
2178 }
2179
2180 # It is, so see if the prefix is acceptable.
2181 my $prefix = $absolute;
2182 substr($prefix, -length($file)) = '';
2183
2184 ##print "prefix<$prefix>\n";
2185 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002186 WARN("USE_RELATIVE_PATH",
2187 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002188 }
2189}
2190
Joe Perches3705ce52013-07-03 15:05:31 -07002191sub trim {
2192 my ($string) = @_;
2193
Joe Perchesb34c6482013-09-11 14:24:01 -07002194 $string =~ s/^\s+|\s+$//g;
2195
2196 return $string;
2197}
2198
2199sub ltrim {
2200 my ($string) = @_;
2201
2202 $string =~ s/^\s+//;
2203
2204 return $string;
2205}
2206
2207sub rtrim {
2208 my ($string) = @_;
2209
2210 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002211
2212 return $string;
2213}
2214
Joe Perches52ea8502013-11-12 15:10:09 -08002215sub string_find_replace {
2216 my ($string, $find, $replace) = @_;
2217
2218 $string =~ s/$find/$replace/g;
2219
2220 return $string;
2221}
2222
Joe Perches3705ce52013-07-03 15:05:31 -07002223sub tabify {
2224 my ($leading) = @_;
2225
2226 my $source_indent = 8;
2227 my $max_spaces_before_tab = $source_indent - 1;
2228 my $spaces_to_tab = " " x $source_indent;
2229
2230 #convert leading spaces to tabs
2231 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2232 #Remove spaces before a tab
2233 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2234
2235 return "$leading";
2236}
2237
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002238sub pos_last_openparen {
2239 my ($line) = @_;
2240
2241 my $pos = 0;
2242
2243 my $opens = $line =~ tr/\(/\(/;
2244 my $closes = $line =~ tr/\)/\)/;
2245
2246 my $last_openparen = 0;
2247
2248 if (($opens == 0) || ($closes >= $opens)) {
2249 return -1;
2250 }
2251
2252 my $len = length($line);
2253
2254 for ($pos = 0; $pos < $len; $pos++) {
2255 my $string = substr($line, $pos);
2256 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2257 $pos += length($1) - 1;
2258 } elsif (substr($line, $pos, 1) eq '(') {
2259 $last_openparen = $pos;
2260 } elsif (index($string, '(') == -1) {
2261 last;
2262 }
2263 }
2264
Joe Perches91cb5192014-04-03 14:49:32 -07002265 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002266}
2267
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002268sub process {
2269 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002270
2271 my $linenr=0;
2272 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002273 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002274 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002275 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002276
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002277 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002278 my $indent;
2279 my $previndent=0;
2280 my $stashindent=0;
2281
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002282 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002283 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002284 my $author = '';
2285 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002286 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002287 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002288 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002289 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002290 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002291 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002292 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002293 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002294 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002295 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002296 my $non_utf8_charset = 0;
2297
Joe Perches365dd4e2014-08-06 16:10:42 -07002298 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002299 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002300
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002301 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002302 our $cnt_lines = 0;
2303 our $cnt_error = 0;
2304 our $cnt_warn = 0;
2305 our $cnt_chk = 0;
2306
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002307 # Trace the real file/line as we go.
2308 my $realfile = '';
2309 my $realline = 0;
2310 my $realcnt = 0;
2311 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002312 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002313 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002314 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002315 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002316 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002317
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002318 my $prev_values = 'E';
2319
2320 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002321 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002322 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002323 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002324 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002325
Joe Perches7e51f192013-09-11 14:23:57 -07002326 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002327
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002328 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002329 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002330 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002331 my @setup_docs = ();
2332 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002333
Joe Perchesd8b07712013-11-12 15:10:06 -08002334 my $camelcase_file_seeded = 0;
2335
Rob Herring9f3a8992018-04-10 16:33:13 -07002336 my $checklicenseline = 1;
2337
Andy Whitcroft773647a2008-03-28 14:15:58 -07002338 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002339 my $line;
2340 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002341 $linenr++;
2342 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002343
Joe Perches3705ce52013-07-03 15:05:31 -07002344 push(@fixed, $rawline) if ($fix);
2345
Andy Whitcroft773647a2008-03-28 14:15:58 -07002346 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002347 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002348 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002349 $setup_docs = 1;
2350 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002351 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002352 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002353 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002354 $realline=$1-1;
2355 if (defined $2) {
2356 $realcnt=$3+1;
2357 } else {
2358 $realcnt=1+1;
2359 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002360 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002361
2362 # Guestimate if this is a continuing comment. Run
2363 # the context looking for a comment "edge". If this
2364 # edge is a close comment then we must be in a comment
2365 # at context start.
2366 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002367 my $cnt = $realcnt;
2368 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2369 next if (defined $rawlines[$ln - 1] &&
2370 $rawlines[$ln - 1] =~ /^-/);
2371 $cnt--;
2372 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002373 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002374 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2375 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2376 ($edge) = $1;
2377 last;
2378 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002379 }
2380 if (defined $edge && $edge eq '*/') {
2381 $in_comment = 1;
2382 }
2383
2384 # Guestimate if this is a continuing comment. If this
2385 # is the start of a diff block and this line starts
2386 # ' *' then it is very likely a comment.
2387 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002388 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002389 {
2390 $in_comment = 1;
2391 }
2392
2393 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2394 sanitise_line_reset($in_comment);
2395
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002396 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002397 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002398 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002399 $line = sanitise_line($rawline);
2400 }
2401 push(@lines, $line);
2402
2403 if ($realcnt > 1) {
2404 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2405 } else {
2406 $realcnt = 0;
2407 }
2408
2409 #print "==>$rawline\n";
2410 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002411
2412 if ($setup_docs && $line =~ /^\+/) {
2413 push(@setup_docs, $line);
2414 }
2415 }
2416
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002417 $prefix = '';
2418
Andy Whitcroft773647a2008-03-28 14:15:58 -07002419 $realcnt = 0;
2420 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002421 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002422 foreach my $line (@lines) {
2423 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002424 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002425 my $sline = $line; #copy of $line
2426 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002427
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002428 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002429
Joe Perches12c253a2018-06-07 17:10:58 -07002430# check if it's a mode change, rename or start of a patch
2431 if (!$in_commit_log &&
2432 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2433 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2434 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2435 $is_patch = 1;
2436 }
2437
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002438#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002439 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002440 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2441 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002442 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002443 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002444 $realline=$1-1;
2445 if (defined $2) {
2446 $realcnt=$3+1;
2447 } else {
2448 $realcnt=1+1;
2449 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002450 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002451 $prev_values = 'E';
2452
Andy Whitcroft773647a2008-03-28 14:15:58 -07002453 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002454 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002455 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002456 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002457 if ($context =~ /\b(\w+)\s*\(/) {
2458 $context_function = $1;
2459 } else {
2460 undef $context_function;
2461 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002462 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002463
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002464# track the line number as we move through the hunk, note that
2465# new versions of GNU diff omit the leading space on completely
2466# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002467 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002468 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002469 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002470
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002471 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002472 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002473
2474 # Track the previous line.
2475 ($prevline, $stashline) = ($stashline, $line);
2476 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002477 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2478
Andy Whitcroft773647a2008-03-28 14:15:58 -07002479 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002480
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002481 } elsif ($realcnt == 1) {
2482 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002483 }
2484
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002485 my $hunk_line = ($realcnt != 0);
2486
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002487 $here = "#$linenr: " if (!$file);
2488 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002489
Joe Perches2ac73b42014-06-04 16:12:05 -07002490 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002491 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002492 if ($line =~ /^diff --git.*?(\S+)$/) {
2493 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002494 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002495 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002496 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002497 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002498 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002499 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002500 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002501
2502 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002503 if (!$file && $tree && $p1_prefix ne '' &&
2504 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002505 WARN("PATCH_PREFIX",
2506 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002507 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002508
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002509 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002510 ERROR("MODIFIED_INCLUDE_ASM",
2511 "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 -07002512 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002513 $found_file = 1;
2514 }
2515
Joe Perches34d88152015-06-25 15:03:05 -07002516#make up the handle for any error we report on this line
2517 if ($showfile) {
2518 $prefix = "$realfile:$realline: "
2519 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002520 if ($file) {
2521 $prefix = "$filename:$realline: ";
2522 } else {
2523 $prefix = "$filename:$linenr: ";
2524 }
Joe Perches34d88152015-06-25 15:03:05 -07002525 }
2526
Joe Perches2ac73b42014-06-04 16:12:05 -07002527 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002528 if (is_maintained_obsolete($realfile)) {
2529 WARN("OBSOLETE",
2530 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2531 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002532 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002533 $check = 1;
2534 } else {
2535 $check = $check_orig;
2536 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002537 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002538
2539 if ($realfile !~ /^MAINTAINERS/) {
2540 my $last_binding_patch = $is_binding_patch;
2541
2542 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2543
2544 if (($last_binding_patch != -1) &&
2545 ($last_binding_patch ^ $is_binding_patch)) {
2546 WARN("DT_SPLIT_BINDING_PATCH",
2547 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2548 }
2549 }
2550
Andy Whitcroft773647a2008-03-28 14:15:58 -07002551 next;
2552 }
2553
Randy Dunlap389834b2007-06-08 13:47:03 -07002554 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002555
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002556 my $hereline = "$here\n$rawline\n";
2557 my $herecurr = "$here\n$rawline\n";
2558 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002559
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002560 $cnt_lines++ if ($realcnt != 0);
2561
Joe Perches490b2922018-08-21 21:58:01 -07002562# Verify the existence of a commit log if appropriate
2563# 2 is used because a $signature is counted in $commit_log_lines
2564 if ($in_commit_log) {
2565 if ($line !~ /^\s*$/) {
2566 $commit_log_lines++; #could be a $signature
2567 }
2568 } elsif ($has_commit_log && $commit_log_lines < 2) {
2569 WARN("COMMIT_MESSAGE",
2570 "Missing commit description - Add an appropriate one\n");
2571 $commit_log_lines = 2; #warn only once
2572 }
2573
Joe Perchese518e9a2015-06-25 15:03:27 -07002574# Check if the commit log has what seems like a diff which can confuse patch
2575 if ($in_commit_log && !$commit_log_has_diff &&
2576 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2577 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2578 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2579 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2580 ERROR("DIFF_IN_COMMIT_MSG",
2581 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2582 $commit_log_has_diff = 1;
2583 }
2584
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002585# Check for incorrect file permissions
2586 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2587 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002588 if ($realfile !~ m@scripts/@ &&
2589 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002590 ERROR("EXECUTE_PERMISSIONS",
2591 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002592 }
2593 }
2594
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002595# Check the patch for a From:
2596 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2597 $author = $1;
2598 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2599 $author =~ s/"//g;
2600 }
2601
Joe Perches20112472011-07-25 17:13:23 -07002602# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002603 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002604 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002605 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002606 if ($author ne '') {
2607 my $l = $line;
2608 $l =~ s/"//g;
2609 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2610 $authorsignoff = 1;
2611 }
2612 }
Joe Perches20112472011-07-25 17:13:23 -07002613 }
2614
Joe Perchese0d975b2014-12-10 15:51:49 -08002615# Check if MAINTAINERS is being updated. If so, there's probably no need to
2616# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2617 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2618 $reported_maintainer_file = 1;
2619 }
2620
Joe Perches20112472011-07-25 17:13:23 -07002621# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002622 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002623 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002624 my $space_before = $1;
2625 my $sign_off = $2;
2626 my $space_after = $3;
2627 my $email = $4;
2628 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2629
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002630 if ($sign_off !~ /$signature_tags/) {
2631 WARN("BAD_SIGN_OFF",
2632 "Non-standard signature: $sign_off\n" . $herecurr);
2633 }
Joe Perches20112472011-07-25 17:13:23 -07002634 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002635 if (WARN("BAD_SIGN_OFF",
2636 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2637 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002638 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002639 "$ucfirst_sign_off $email";
2640 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002641 }
Joe Perches20112472011-07-25 17:13:23 -07002642 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002643 if (WARN("BAD_SIGN_OFF",
2644 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2645 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002646 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002647 "$ucfirst_sign_off $email";
2648 }
2649
Joe Perches20112472011-07-25 17:13:23 -07002650 }
2651 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002652 if (WARN("BAD_SIGN_OFF",
2653 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2654 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002655 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002656 "$ucfirst_sign_off $email";
2657 }
Joe Perches20112472011-07-25 17:13:23 -07002658 }
2659
2660 my ($email_name, $email_address, $comment) = parse_email($email);
2661 my $suggested_email = format_email(($email_name, $email_address));
2662 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002663 ERROR("BAD_SIGN_OFF",
2664 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002665 } else {
2666 my $dequoted = $suggested_email;
2667 $dequoted =~ s/^"//;
2668 $dequoted =~ s/" </ </;
2669 # Don't force email to have quotes
2670 # Allow just an angle bracketed address
2671 if ("$dequoted$comment" ne $email &&
2672 "<$email_address>$comment" ne $email &&
2673 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002674 WARN("BAD_SIGN_OFF",
2675 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002676 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002677 }
Joe Perches7e51f192013-09-11 14:23:57 -07002678
2679# Check for duplicate signatures
2680 my $sig_nospace = $line;
2681 $sig_nospace =~ s/\s//g;
2682 $sig_nospace = lc($sig_nospace);
2683 if (defined $signatures{$sig_nospace}) {
2684 WARN("BAD_SIGN_OFF",
2685 "Duplicate signature\n" . $herecurr);
2686 } else {
2687 $signatures{$sig_nospace} = 1;
2688 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002689 }
2690
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002691# Check email subject for common tools that don't need to be mentioned
2692 if ($in_header_lines &&
2693 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2694 WARN("EMAIL_SUBJECT",
2695 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2696 }
2697
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002698# Check for unwanted Gerrit info
2699 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2700 ERROR("GERRIT_CHANGE_ID",
2701 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2702 }
2703
Joe Perches369c8dd2015-11-06 16:31:34 -08002704# Check if the commit log is in a possible stack dump
2705 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2706 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2707 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2708 # timestamp
2709 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2710 # stack dump address
2711 $commit_log_possible_stack_dump = 1;
2712 }
2713
Joe Perches2a076f42015-04-16 12:44:28 -07002714# Check for line lengths > 75 in commit log, warn once
2715 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002716 length($line) > 75 &&
2717 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2718 # file delta changes
2719 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2720 # filename then :
2721 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2722 # A Fixes: or Link: line
2723 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002724 WARN("COMMIT_LOG_LONG_LINE",
2725 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2726 $commit_log_long_line = 1;
2727 }
2728
Joe Perchesbf4daf12015-09-09 15:37:50 -07002729# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002730 if ($in_commit_log && $commit_log_possible_stack_dump &&
2731 $line =~ /^\s*$/) {
2732 $commit_log_possible_stack_dump = 0;
2733 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002734
Joe Perches0d7835f2015-02-13 14:38:35 -08002735# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002736 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002737 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002738 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002739 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002740 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002741 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2742 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002743 my $init_char = "c";
2744 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002745 my $short = 1;
2746 my $long = 0;
2747 my $case = 1;
2748 my $space = 1;
2749 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002750 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002751 my $id = '0123456789ab';
2752 my $orig_desc = "commit description";
2753 my $description = "";
2754
Joe Perchesfe043ea2015-09-09 15:37:25 -07002755 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2756 $init_char = $1;
2757 $orig_commit = lc($2);
2758 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2759 $orig_commit = lc($1);
2760 }
2761
Joe Perches0d7835f2015-02-13 14:38:35 -08002762 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2763 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2764 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2765 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2766 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2767 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002768 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002769 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2770 defined $rawlines[$linenr] &&
2771 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2772 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002773 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002774 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2775 defined $rawlines[$linenr] &&
2776 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2777 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2778 $orig_desc = $1;
2779 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2780 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002781 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002782 }
2783
2784 ($id, $description) = git_commit_info($orig_commit,
2785 $id, $orig_desc);
2786
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002787 if (defined($id) &&
2788 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002789 ERROR("GIT_COMMIT_ID",
2790 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2791 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002792 }
2793
Joe Perches13f19372014-08-06 16:10:59 -07002794# Check for added, moved or deleted files
2795 if (!$reported_maintainer_file && !$in_commit_log &&
2796 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2797 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2798 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2799 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002800 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002801 $reported_maintainer_file = 1;
2802 WARN("FILE_PATH_CHANGES",
2803 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2804 }
2805
Andy Whitcroft00df3442007-06-08 13:47:06 -07002806# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002807 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002808 ERROR("CORRUPTED_PATCH",
2809 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002810 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002811 }
2812
2813# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2814 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002815 $rawline !~ m/^$UTF8*$/) {
2816 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2817
2818 my $blank = copy_spacing($rawline);
2819 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2820 my $hereptr = "$hereline$ptr\n";
2821
Joe Perches34d99212011-07-25 17:13:26 -07002822 CHK("INVALID_UTF8",
2823 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002824 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002825
Joe Perches15662b32011-10-31 17:13:12 -07002826# Check if it's the start of a commit log
2827# (not a header line and we haven't seen the patch filename)
2828 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002829 !($rawline =~ /^\s+(?:\S|$)/ ||
2830 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002831 $in_header_lines = 0;
2832 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002833 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002834 }
2835
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002836# Check if there is UTF-8 in a commit log when a mail header has explicitly
2837# declined it, i.e defined some charset where it is missing.
2838 if ($in_header_lines &&
2839 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2840 $1 !~ /utf-8/i) {
2841 $non_utf8_charset = 1;
2842 }
2843
2844 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002845 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002846 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002847 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2848 }
2849
Joe Perchesd6430f72016-12-12 16:46:28 -08002850# Check for absolute kernel paths in commit message
2851 if ($tree && $in_commit_log) {
2852 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2853 my $file = $1;
2854
2855 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2856 check_absolute_file($1, $herecurr)) {
2857 #
2858 } else {
2859 check_absolute_file($file, $herecurr);
2860 }
2861 }
2862 }
2863
Kees Cook66b47b42014-10-13 15:51:57 -07002864# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002865 if (defined($misspellings) &&
2866 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002867 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002868 my $typo = $1;
2869 my $typo_fix = $spelling_fix{lc($typo)};
2870 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2871 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002872 my $msg_level = \&WARN;
2873 $msg_level = \&CHK if ($file);
2874 if (&{$msg_level}("TYPO_SPELLING",
2875 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002876 $fix) {
2877 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2878 }
2879 }
2880 }
2881
Andy Whitcroft306708542008-10-15 22:02:28 -07002882# ignore non-hunk lines and lines being removed
2883 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002884
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002885#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002886 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002887 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002888 if (ERROR("DOS_LINE_ENDINGS",
2889 "DOS line endings\n" . $herevet) &&
2890 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002891 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002892 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002893 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2894 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002895 if (ERROR("TRAILING_WHITESPACE",
2896 "trailing whitespace\n" . $herevet) &&
2897 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002898 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002899 }
2900
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002901 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002902 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002903
Josh Triplett4783f892013-11-12 15:10:12 -08002904# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002905 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002906 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002907 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2908 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002909 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002910 my $msg_level = \&ERROR;
2911 $msg_level = \&CHK if ($file);
2912 &{$msg_level}("FSF_MAILING_ADDRESS",
2913 "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 -08002914 }
2915
Andi Kleen33549572010-05-24 14:33:29 -07002916# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002917# Only applies when adding the entry originally, after that we do not have
2918# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002919 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002920 # 'choice' is usually the last thing on the line (though
2921 # Kconfig supports named choices), so use a word boundary
2922 # (\b) rather than a whitespace character (\s)
2923 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002924 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002925 my $cnt = $realcnt;
2926 my $ln = $linenr + 1;
2927 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002928 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002929 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002930 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002931 $f = $lines[$ln - 1];
2932 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2933 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002934
2935 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002936 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002937
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01002938 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002939 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01002940 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2941 if ($lines[$ln - 1] =~ "---help---") {
2942 WARN("CONFIG_DESCRIPTION",
2943 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2944 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002945 $length = -1;
2946 }
2947
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002948 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002949 $f =~ s/#.*//;
2950 $f =~ s/^\s+//;
2951 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01002952
2953 # This only checks context lines in the patch
2954 # and so hopefully shouldn't trigger false
2955 # positives, even though some of these are
2956 # common words in help texts
2957 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2958 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002959 $is_end = 1;
2960 last;
2961 }
Andi Kleen33549572010-05-24 14:33:29 -07002962 $length++;
2963 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002964 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2965 WARN("CONFIG_DESCRIPTION",
2966 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2967 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002968 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002969 }
2970
Joe Perches628f91a2017-07-10 15:52:07 -07002971# check for MAINTAINERS entries that don't have the right form
2972 if ($realfile =~ /^MAINTAINERS$/ &&
2973 $rawline =~ /^\+[A-Z]:/ &&
2974 $rawline !~ /^\+[A-Z]:\t\S/) {
2975 if (WARN("MAINTAINERS_STYLE",
2976 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2977 $fix) {
2978 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2979 }
2980 }
2981
Christoph Jaeger327953e2015-02-13 14:38:29 -08002982# discourage the use of boolean for type definition attributes of Kconfig options
2983 if ($realfile =~ /Kconfig/ &&
2984 $line =~ /^\+\s*\bboolean\b/) {
2985 WARN("CONFIG_TYPE_BOOLEAN",
2986 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2987 }
2988
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002989 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2990 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2991 my $flag = $1;
2992 my $replacement = {
2993 'EXTRA_AFLAGS' => 'asflags-y',
2994 'EXTRA_CFLAGS' => 'ccflags-y',
2995 'EXTRA_CPPFLAGS' => 'cppflags-y',
2996 'EXTRA_LDFLAGS' => 'ldflags-y',
2997 };
2998
2999 WARN("DEPRECATED_VARIABLE",
3000 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3001 }
3002
Rob Herringbff5da42014-01-23 15:54:51 -08003003# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003004 if (defined $root &&
3005 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3006 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3007
Rob Herringbff5da42014-01-23 15:54:51 -08003008 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3009
Florian Vaussardcc933192014-04-03 14:49:27 -07003010 my $dt_path = $root . "/Documentation/devicetree/bindings/";
3011 my $vp_file = $dt_path . "vendor-prefixes.txt";
3012
Rob Herringbff5da42014-01-23 15:54:51 -08003013 foreach my $compat (@compats) {
3014 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003015 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3016 my $compat3 = $compat;
3017 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3018 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003019 if ( $? >> 8 ) {
3020 WARN("UNDOCUMENTED_DT_STRING",
3021 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3022 }
3023
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003024 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3025 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07003026 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003027 if ( $? >> 8 ) {
3028 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003029 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003030 }
3031 }
3032 }
3033
Rob Herring9f3a8992018-04-10 16:33:13 -07003034# check for using SPDX license tag at beginning of files
3035 if ($realline == $checklicenseline) {
3036 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3037 $checklicenseline = 2;
3038 } elsif ($rawline =~ /^\+/) {
3039 my $comment = "";
3040 if ($realfile =~ /\.(h|s|S)$/) {
3041 $comment = '/*';
3042 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3043 $comment = '//';
3044 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
3045 $comment = '#';
3046 } elsif ($realfile =~ /\.rst$/) {
3047 $comment = '..';
3048 }
3049
Joe Perchesfdf13692019-03-07 16:28:32 -08003050# check SPDX comment style for .[chsS] files
3051 if ($realfile =~ /\.[chsS]$/ &&
3052 $rawline =~ /SPDX-License-Identifier:/ &&
3053 $rawline !~ /^\+\s*\Q$comment\E\s*/) {
3054 WARN("SPDX_LICENSE_TAG",
3055 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3056 }
3057
Rob Herring9f3a8992018-04-10 16:33:13 -07003058 if ($comment !~ /^$/ &&
3059 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003060 WARN("SPDX_LICENSE_TAG",
3061 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3062 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3063 my $spdx_license = $1;
3064 if (!is_SPDX_License_valid($spdx_license)) {
3065 WARN("SPDX_LICENSE_TAG",
3066 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3067 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003068 }
3069 }
3070 }
3071
Andy Whitcroft5368df202008-10-15 22:02:27 -07003072# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003073 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003074
Joe Perches47e0c882015-06-25 15:02:57 -07003075# line length limit (with some exclusions)
3076#
3077# There are a few types of lines that may extend beyond $max_line_length:
3078# logging functions like pr_info that end in a string
3079# lines with a single string
3080# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003081# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003082#
3083# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003084# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003085# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3086# LONG_LINE all other lines longer than $max_line_length
3087#
3088# if LONG_LINE is ignored, the other 2 types are also ignored
3089#
3090
Joe Perchesb4749e92015-07-17 16:24:01 -07003091 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003092 my $msg_type = "LONG_LINE";
3093
3094 # Check the allowed long line types first
3095
3096 # logging functions that end in a string that starts
3097 # before $max_line_length
3098 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3099 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3100 $msg_type = "";
3101
3102 # lines with only strings (w/ possible termination)
3103 # #defines with only strings
3104 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3105 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3106 $msg_type = "";
3107
Joe Perchescc147502017-11-17 15:28:44 -08003108 # More special cases
3109 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3110 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003111 $msg_type = "";
3112
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003113 # URL ($rawline is used in case the URL is in a comment)
3114 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3115 $msg_type = "";
3116
Joe Perches47e0c882015-06-25 15:02:57 -07003117 # Otherwise set the alternate message types
3118
3119 # a comment starts before $max_line_length
3120 } elsif ($line =~ /($;[\s$;]*)$/ &&
3121 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3122 $msg_type = "LONG_LINE_COMMENT"
3123
3124 # a quoted string starts before $max_line_length
3125 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3126 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3127 $msg_type = "LONG_LINE_STRING"
3128 }
3129
3130 if ($msg_type ne "" &&
3131 (show_type("LONG_LINE") || show_type($msg_type))) {
3132 WARN($msg_type,
3133 "line over $max_line_length characters\n" . $herecurr);
3134 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003135 }
3136
Andy Whitcroft8905a672007-11-28 16:21:06 -08003137# check for adding lines without a newline.
3138 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003139 WARN("MISSING_EOF_NEWLINE",
3140 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003141 }
3142
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003143# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003144 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003145
3146# at the beginning of a line any tabs must come first and anything
3147# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003148 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3149 $rawline =~ /^\+\s* \s*/) {
3150 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003151 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003152 if (ERROR("CODE_INDENT",
3153 "code indent should use tabs where possible\n" . $herevet) &&
3154 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003155 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003156 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003157 }
3158
Alberto Panizzo08e44362010-03-05 13:43:54 -08003159# check for space before tabs.
3160 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3161 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003162 if (WARN("SPACE_BEFORE_TAB",
3163 "please, no space before tabs\n" . $herevet) &&
3164 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003165 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003166 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003167 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003168 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003169 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003170 }
3171
Joe Perches6a487212018-04-10 16:34:04 -07003172# check for assignments on the start of a line
3173 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3174 CHK("ASSIGNMENT_CONTINUATIONS",
3175 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3176 }
3177
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003178# check for && or || at the start of a line
3179 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3180 CHK("LOGICAL_CONTINUATIONS",
3181 "Logical continuations should be on the previous line\n" . $hereprev);
3182 }
3183
Joe Perchesa91e8992016-05-20 17:04:05 -07003184# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003185 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003186 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003187 my $indent = length($1);
3188 if ($indent % 8) {
3189 if (WARN("TABSTOP",
3190 "Statements should start on a tabstop\n" . $herecurr) &&
3191 $fix) {
3192 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3193 }
3194 }
3195 }
3196
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003197# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003198 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003199 $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 -07003200 $prevline =~ /^\+(\t*)(.*)$/;
3201 my $oldindent = $1;
3202 my $rest = $2;
3203
3204 my $pos = pos_last_openparen($rest);
3205 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003206 $line =~ /^(\+| )([ \t]*)/;
3207 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003208
3209 my $goodtabindent = $oldindent .
3210 "\t" x ($pos / 8) .
3211 " " x ($pos % 8);
3212 my $goodspaceindent = $oldindent . " " x $pos;
3213
3214 if ($newindent ne $goodtabindent &&
3215 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003216
3217 if (CHK("PARENTHESIS_ALIGNMENT",
3218 "Alignment should match open parenthesis\n" . $hereprev) &&
3219 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003220 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003221 s/^\+[ \t]*/\+$goodtabindent/;
3222 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003223 }
3224 }
3225 }
3226
Joe Perches6ab3a972015-04-16 12:44:05 -07003227# check for space after cast like "(int) foo" or "(struct foo) bar"
3228# avoid checking a few false positives:
3229# "sizeof(<type>)" or "__alignof__(<type>)"
3230# function pointer declarations like "(*foo)(int) = bar;"
3231# structure definitions like "(struct foo) { 0 };"
3232# multiline macros that define functions
3233# known attributes or the __attribute__ keyword
3234 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3235 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003236 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003237 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003238 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003239 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003240 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003241 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003242 }
3243
Joe Perches86406b12015-09-09 15:37:41 -07003244# Block comment styles
3245# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003246 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003247 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003248 $rawline =~ /^\+[ \t]*\*/ &&
3249 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003250 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3251 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3252 }
3253
Joe Perches86406b12015-09-09 15:37:41 -07003254# Block comments use * on subsequent lines
3255 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3256 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003257 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003258 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003259 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003260 WARN("BLOCK_COMMENT_STYLE",
3261 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003262 }
3263
Joe Perches86406b12015-09-09 15:37:41 -07003264# Block comments use */ on trailing lines
3265 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003266 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3267 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3268 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003269 WARN("BLOCK_COMMENT_STYLE",
3270 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003271 }
3272
Joe Perches08eb9b82016-10-11 13:51:50 -07003273# Block comment * alignment
3274 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003275 $line =~ /^\+[ \t]*$;/ && #leading comment
3276 $rawline =~ /^\+[ \t]*\*/ && #leading *
3277 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003278 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003279 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3280 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003281 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003282 if (defined($1)) {
3283 $oldindent = expand_tabs($1);
3284 } else {
3285 $prevrawline =~ m@^\+(.*/?)\*@;
3286 $oldindent = expand_tabs($1);
3287 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003288 $rawline =~ m@^\+([ \t]*)\*@;
3289 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003290 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003291 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003292 WARN("BLOCK_COMMENT_STYLE",
3293 "Block comments should align the * on each line\n" . $hereprev);
3294 }
3295 }
3296
Joe Perches7f619192014-08-06 16:10:39 -07003297# check for missing blank lines after struct/union declarations
3298# with exceptions for various attributes and macros
3299 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3300 $line =~ /^\+/ &&
3301 !($line =~ /^\+\s*$/ ||
3302 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3303 $line =~ /^\+\s*MODULE_/i ||
3304 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3305 $line =~ /^\+[a-z_]*init/ ||
3306 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3307 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003308 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003309 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003310 if (CHK("LINE_SPACING",
3311 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3312 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003313 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003314 }
Joe Perches7f619192014-08-06 16:10:39 -07003315 }
3316
Joe Perches365dd4e2014-08-06 16:10:42 -07003317# check for multiple consecutive blank lines
3318 if ($prevline =~ /^[\+ ]\s*$/ &&
3319 $line =~ /^\+\s*$/ &&
3320 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003321 if (CHK("LINE_SPACING",
3322 "Please don't use multiple blank lines\n" . $hereprev) &&
3323 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003324 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003325 }
3326
Joe Perches365dd4e2014-08-06 16:10:42 -07003327 $last_blank_line = $linenr;
3328 }
3329
Joe Perches3b617e32014-04-03 14:49:28 -07003330# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003331 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3332 # actual declarations
3333 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003334 # function pointer declarations
3335 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003336 # foo bar; where foo is some local typedef or #define
3337 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3338 # known declaration macros
3339 $prevline =~ /^\+\s+$declaration_macros/) &&
3340 # for "else if" which can look like "$Ident $Ident"
3341 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3342 # other possible extensions of declaration lines
3343 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3344 # not starting a section or a macro "\" extended line
3345 $prevline =~ /(?:\{\s*|\\)$/) &&
3346 # looks like a declaration
3347 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003348 # function pointer declarations
3349 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003350 # foo bar; where foo is some local typedef or #define
3351 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3352 # known declaration macros
3353 $sline =~ /^\+\s+$declaration_macros/ ||
3354 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003355 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003356 # start or end of block or continuation of declaration
3357 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3358 # bitfield continuation
3359 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3360 # other possible extensions of declaration lines
3361 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3362 # indentation of previous and current line are the same
3363 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003364 if (WARN("LINE_SPACING",
3365 "Missing a blank line after declarations\n" . $hereprev) &&
3366 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003367 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003368 }
Joe Perches3b617e32014-04-03 14:49:28 -07003369 }
3370
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003371# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003372# Exceptions:
3373# 1) within comments
3374# 2) indented preprocessor commands
3375# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003376 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003377 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003378 if (WARN("LEADING_SPACE",
3379 "please, no spaces at the start of a line\n" . $herevet) &&
3380 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003381 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003382 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003383 }
3384
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003385# check we are in a valid C source file if not then ignore this hunk
3386 next if ($realfile !~ /\.(h|c)$/);
3387
Joe Perches5751a242017-11-17 15:28:52 -08003388# check for unusual line ending [ or (
3389 if ($line =~ /^\+.*([\[\(])\s*$/) {
3390 CHK("OPEN_ENDED_LINE",
3391 "Lines should not end with a '$1'\n" . $herecurr);
3392 }
3393
Joe Perches4dbed762017-05-08 15:55:39 -07003394# check if this appears to be the start function declaration, save the name
3395 if ($sline =~ /^\+\{\s*$/ &&
3396 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3397 $context_function = $1;
3398 }
3399
3400# check if this appears to be the end of function declaration
3401 if ($sline =~ /^\+\}\s*$/) {
3402 undef $context_function;
3403 }
3404
Joe Perches032a4c02014-08-06 16:10:29 -07003405# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003406# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003407# if the previous line is a break or return and is indented 1 tab more...
3408 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3409 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003410 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3411 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3412 defined $lines[$linenr] &&
3413 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003414 WARN("UNNECESSARY_ELSE",
3415 "else is not generally useful after a break or return\n" . $hereprev);
3416 }
3417 }
3418
Joe Perchesc00df192014-08-06 16:11:01 -07003419# check indentation of a line with a break;
3420# if the previous line is a goto or return and is indented the same # of tabs
3421 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3422 my $tabs = $1;
3423 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3424 WARN("UNNECESSARY_BREAK",
3425 "break is not useful after a goto or return\n" . $hereprev);
3426 }
3427 }
3428
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003429# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003430 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003431 WARN("CVS_KEYWORD",
3432 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003433 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003434
Joe Perches56e77d72013-02-21 16:44:14 -08003435# check for old HOTPLUG __dev<foo> section markings
3436 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3437 WARN("HOTPLUG_SECTION",
3438 "Using $1 is unnecessary\n" . $herecurr);
3439 }
3440
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003441# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003442 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3443 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003444#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003445 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003446 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003447 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003448 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003449 $stat =~ s/\n./\n /g;
3450 $cond =~ s/\n./\n /g;
3451
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003452#print "linenr<$linenr> <$stat>\n";
3453 # If this statement has no statement boundaries within
3454 # it there is no point in retrying a statement scan
3455 # until we hit end of it.
3456 my $frag = $stat; $frag =~ s/;+\s*$//;
3457 if ($frag !~ /(?:{|;)/) {
3458#print "skip<$line_nr_next>\n";
3459 $suppress_statement = $line_nr_next;
3460 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003461
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003462 # Find the real next line.
3463 $realline_next = $line_nr_next;
3464 if (defined $realline_next &&
3465 (!defined $lines[$realline_next - 1] ||
3466 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3467 $realline_next++;
3468 }
3469
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003470 my $s = $stat;
3471 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003472
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003473 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003474 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003475
3476 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003477 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003478
Andy Whitcroft463f2862009-09-21 17:04:34 -07003479 } elsif ($s =~ /^.\s*else\b/s) {
3480
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003481 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003482 } 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 -07003483 my $type = $1;
3484 $type =~ s/\s+/ /g;
3485 possible($type, "A:" . $s);
3486
Andy Whitcroft8905a672007-11-28 16:21:06 -08003487 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003488 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003489 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003490 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003491
3492 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003493 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003494 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003495 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003496
3497 # Check for any sort of function declaration.
3498 # int foo(something bar, other baz);
3499 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003500 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 -08003501 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003502
Andy Whitcroftcf655042008-03-04 14:28:20 -08003503 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003504 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003505 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003506
Andy Whitcroft8905a672007-11-28 16:21:06 -08003507 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003508 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003509
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003510 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003511 }
3512 }
3513 }
3514
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003515 }
3516
Andy Whitcroft653d4872007-06-23 17:16:34 -07003517#
3518# Checks which may be anchored in the context.
3519#
3520
3521# Check for switch () and associated case and default
3522# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003523 if ($line=~/\bswitch\s*\(.*\)/) {
3524 my $err = '';
3525 my $sep = '';
3526 my @ctx = ctx_block_outer($linenr, $realcnt);
3527 shift(@ctx);
3528 for my $ctx (@ctx) {
3529 my ($clen, $cindent) = line_stats($ctx);
3530 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3531 $indent != $cindent) {
3532 $err .= "$sep$ctx\n";
3533 $sep = '';
3534 } else {
3535 $sep = "[...]\n";
3536 }
3537 }
3538 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003539 ERROR("SWITCH_CASE_INDENT_LEVEL",
3540 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003541 }
3542 }
3543
3544# if/while/etc brace do not go on next line, unless defining a do while loop,
3545# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003546 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 -07003547 my $pre_ctx = "$1$2";
3548
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003549 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003550
3551 if ($line =~ /^\+\t{6,}/) {
3552 WARN("DEEP_INDENTATION",
3553 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3554 }
3555
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003556 my $ctx_cnt = $realcnt - $#ctx - 1;
3557 my $ctx = join("\n", @ctx);
3558
Andy Whitcroft548596d2008-07-23 21:29:01 -07003559 my $ctx_ln = $linenr;
3560 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003561
Andy Whitcroft548596d2008-07-23 21:29:01 -07003562 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3563 defined $lines[$ctx_ln - 1] &&
3564 $lines[$ctx_ln - 1] =~ /^-/)) {
3565 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3566 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003567 $ctx_ln++;
3568 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003569
Andy Whitcroft53210162008-07-23 21:29:03 -07003570 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3571 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003572
Joe Perchesd752fcc2014-08-06 16:11:05 -07003573 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003574 ERROR("OPEN_BRACE",
3575 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003576 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003577 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003578 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3579 $ctx =~ /\)\s*\;\s*$/ &&
3580 defined $lines[$ctx_ln - 1])
3581 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003582 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3583 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003584 WARN("TRAILING_SEMICOLON",
3585 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003586 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003587 }
3588 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003589 }
3590
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003591# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003592 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 -08003593 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3594 ctx_statement_block($linenr, $realcnt, 0)
3595 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003596 my ($s, $c) = ($stat, $cond);
3597
3598 substr($s, 0, length($c), '');
3599
Joe Perches9f5af482015-09-09 15:37:30 -07003600 # remove inline comments
3601 $s =~ s/$;/ /g;
3602 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003603
3604 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003605 my @newlines = ($c =~ /\n/gs);
3606 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003607
Joe Perches9f5af482015-09-09 15:37:30 -07003608 # Make sure we remove the line prefixes as we have
3609 # none on the first line, and are going to readd them
3610 # where necessary.
3611 $s =~ s/\n./\n/gs;
3612 while ($s =~ /\n\s+\\\n/) {
3613 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3614 }
3615
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003616 # We want to check the first line inside the block
3617 # starting at the end of the conditional, so remove:
3618 # 1) any blank line termination
3619 # 2) any opening brace { on end of the line
3620 # 3) any do (...) {
3621 my $continuation = 0;
3622 my $check = 0;
3623 $s =~ s/^.*\bdo\b//;
3624 $s =~ s/^\s*{//;
3625 if ($s =~ s/^\s*\\//) {
3626 $continuation = 1;
3627 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003628 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003629 $check = 1;
3630 $cond_lines++;
3631 }
3632
3633 # Also ignore a loop construct at the end of a
3634 # preprocessor statement.
3635 if (($prevline =~ /^.\s*#\s*define\s/ ||
3636 $prevline =~ /\\\s*$/) && $continuation == 0) {
3637 $check = 0;
3638 }
3639
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003640 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003641 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003642 while ($cond_ptr != $cond_lines) {
3643 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003644
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003645 # If we see an #else/#elif then the code
3646 # is not linear.
3647 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3648 $check = 0;
3649 }
3650
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003651 # Ignore:
3652 # 1) blank lines, they should be at 0,
3653 # 2) preprocessor lines, and
3654 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003655 if ($continuation ||
3656 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003657 $s =~ /^\s*#\s*?/ ||
3658 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003659 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003660 if ($s =~ s/^.*?\n//) {
3661 $cond_lines++;
3662 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003663 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003664 }
3665
3666 my (undef, $sindent) = line_stats("+" . $s);
3667 my $stat_real = raw_line($linenr, $cond_lines);
3668
3669 # Check if either of these lines are modified, else
3670 # this is not this patch's fault.
3671 if (!defined($stat_real) ||
3672 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3673 $check = 0;
3674 }
3675 if (defined($stat_real) && $cond_lines > 1) {
3676 $stat_real = "[...]\n$stat_real";
3677 }
3678
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003679 #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 -07003680
Joe Perches9f5af482015-09-09 15:37:30 -07003681 if ($check && $s ne '' &&
3682 (($sindent % 8) != 0 ||
3683 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003684 ($sindent == $indent &&
3685 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003686 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003687 WARN("SUSPECT_CODE_INDENT",
3688 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003689 }
3690 }
3691
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003692 # Track the 'values' across context and added lines.
3693 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003694 my ($curr_values, $curr_vars) =
3695 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003696 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003697 if ($dbg_values) {
3698 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003699 print "$linenr > .$outline\n";
3700 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003701 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003702 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003703 $prev_values = substr($curr_values, -1);
3704
Andy Whitcroft00df3442007-06-08 13:47:06 -07003705#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003706 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003707
Joe Perches11ca40a2016-12-12 16:46:31 -08003708# check for dereferences that span multiple lines
3709 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3710 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3711 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3712 my $ref = $1;
3713 $line =~ /^.\s*($Lval)/;
3714 $ref .= $1;
3715 $ref =~ s/\s//g;
3716 WARN("MULTILINE_DEREFERENCE",
3717 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3718 }
3719
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003720# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003721 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 -07003722 my $type = $1;
3723 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003724 $var = "" if (!defined $var);
3725 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003726 my $sign = $1;
3727 my $pointer = $2;
3728
3729 $pointer = "" if (!defined $pointer);
3730
3731 if (WARN("UNSPECIFIED_INT",
3732 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3733 $fix) {
3734 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003735 my $comp_pointer = $pointer;
3736 $comp_pointer =~ s/\s//g;
3737 $decl .= $comp_pointer;
3738 $decl = rtrim($decl) if ($var eq "");
3739 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003740 }
3741 }
3742 }
3743
Andy Whitcroft653d4872007-06-23 17:16:34 -07003744# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003745 if ($dbg_type) {
3746 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003747 ERROR("TEST_TYPE",
3748 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003749 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003750 ERROR("TEST_NOT_TYPE",
3751 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003752 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003753 next;
3754 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003755# TEST: allow direct testing of the attribute matcher.
3756 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003757 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003758 ERROR("TEST_ATTR",
3759 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003760 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003761 ERROR("TEST_NOT_ATTR",
3762 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003763 }
3764 next;
3765 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003766
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003767# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003768 if ($line =~ /^.\s*{/ &&
3769 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003770 if (ERROR("OPEN_BRACE",
3771 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003772 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3773 fix_delete_line($fixlinenr - 1, $prevrawline);
3774 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003775 my $fixedline = $prevrawline;
3776 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003777 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003778 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003779 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003780 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003781 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003782 }
3783
Andy Whitcroft653d4872007-06-23 17:16:34 -07003784#
3785# Checks which are anchored on the added line.
3786#
3787
3788# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003789 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003790 my $path = $1;
3791 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003792 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003793 "malformed #include filename\n" . $herecurr);
3794 }
3795 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3796 ERROR("UAPI_INCLUDE",
3797 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003798 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003799 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003800
3801# no C99 // comments
3802 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003803 if (ERROR("C99_COMMENTS",
3804 "do not use C99 // comments\n" . $herecurr) &&
3805 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003806 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003807 if ($line =~ /\/\/(.*)$/) {
3808 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003809 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003810 }
3811 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003812 }
3813 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003814 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003815 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003816
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003817# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3818# the whole statement.
3819#print "APW <$lines[$realline_next - 1]>\n";
3820 if (defined $realline_next &&
3821 exists $lines[$realline_next - 1] &&
3822 !defined $suppress_export{$realline_next} &&
3823 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3824 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003825 # Handle definitions which produce identifiers with
3826 # a prefix:
3827 # XXX(foo);
3828 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003829 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003830 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003831 $name =~ /^${Ident}_$2/) {
3832#print "FOO C name<$name>\n";
3833 $suppress_export{$realline_next} = 1;
3834
3835 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003836 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003837 ^.DEFINE_$Ident\(\Q$name\E\)|
3838 ^.DECLARE_$Ident\(\Q$name\E\)|
3839 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003840 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3841 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003842 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003843#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3844 $suppress_export{$realline_next} = 2;
3845 } else {
3846 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003847 }
3848 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003849 if (!defined $suppress_export{$linenr} &&
3850 $prevline =~ /^.\s*$/ &&
3851 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3852 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3853#print "FOO B <$lines[$linenr - 1]>\n";
3854 $suppress_export{$linenr} = 2;
3855 }
3856 if (defined $suppress_export{$linenr} &&
3857 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003858 WARN("EXPORT_SYMBOL",
3859 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003860 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003861
Joe Eloff5150bda2010-08-09 17:21:00 -07003862# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003863 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003864 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003865 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003866 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003867 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003868 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003869 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003870# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003871 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003872 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003873 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003874 $herecurr) &&
3875 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003876 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003877 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003878 }
3879
Joe Perches18130872014-08-06 16:11:22 -07003880# check for misordered declarations of char/short/int/long with signed/unsigned
3881 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3882 my $tmp = trim($1);
3883 WARN("MISORDERED_TYPE",
3884 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3885 }
3886
Joe Perches809e0822018-08-21 21:58:12 -07003887# check for unnecessary <signed> int declarations of short/long/long long
3888 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3889 my $type = trim($1);
3890 next if ($type !~ /\bint\b/);
3891 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3892 my $new_type = $type;
3893 $new_type =~ s/\b\s*int\s*\b/ /;
3894 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3895 $new_type =~ s/^const\s+//;
3896 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3897 $new_type = "const $new_type" if ($type =~ /^const\b/);
3898 $new_type =~ s/\s+/ /g;
3899 $new_type = trim($new_type);
3900 if (WARN("UNNECESSARY_INT",
3901 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3902 $fix) {
3903 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3904 }
3905 }
3906
Joe Perchescb710ec2010-10-26 14:23:20 -07003907# check for static const char * arrays.
3908 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003909 WARN("STATIC_CONST_CHAR_ARRAY",
3910 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003911 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003912 }
3913
3914# check for initialized const char arrays that should be static const
3915 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
3916 if (WARN("STATIC_CONST_CHAR_ARRAY",
3917 "const array should probably be static const\n" . $herecurr) &&
3918 $fix) {
3919 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
3920 }
3921 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003922
3923# check for static char foo[] = "bar" declarations.
3924 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003925 WARN("STATIC_CONST_CHAR_ARRAY",
3926 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003927 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003928 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003929
Joe Perchesab7e23f2015-04-16 12:44:22 -07003930# check for const <foo> const where <foo> is not a pointer or array type
3931 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3932 my $found = $1;
3933 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3934 WARN("CONST_CONST",
3935 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3936 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3937 WARN("CONST_CONST",
3938 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3939 }
3940 }
3941
Joe Perches9b0fa602014-04-03 14:49:18 -07003942# check for non-global char *foo[] = {"bar", ...} declarations.
3943 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3944 WARN("STATIC_CONST_CHAR_ARRAY",
3945 "char * array declaration might be better as static const\n" .
3946 $herecurr);
3947 }
3948
Joe Perchesb598b672015-04-16 12:44:36 -07003949# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3950 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3951 my $array = $1;
3952 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3953 my $array_div = $1;
3954 if (WARN("ARRAY_SIZE",
3955 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3956 $fix) {
3957 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3958 }
3959 }
3960 }
3961
Joe Perchesb36190c2014-01-27 17:07:18 -08003962# check for function declarations without arguments like "int foo()"
3963 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3964 if (ERROR("FUNCTION_WITHOUT_ARGS",
3965 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3966 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003967 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003968 }
3969 }
3970
Andy Whitcroft653d4872007-06-23 17:16:34 -07003971# check for new typedefs, only function parameters and sparse annotations
3972# make sense.
3973 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003974 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003975 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003976 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003977 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003978 WARN("NEW_TYPEDEFS",
3979 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003980 }
3981
3982# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003983 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003984 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3985 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003986 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003987
Andy Whitcroft65863862009-01-06 14:41:21 -08003988 # Should start with a space.
3989 $to =~ s/^(\S)/ $1/;
3990 # Should not end with a space.
3991 $to =~ s/\s+$//;
3992 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003993 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003994 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003995
Joe Perches3705ce52013-07-03 15:05:31 -07003996## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003997 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003998 if (ERROR("POINTER_LOCATION",
3999 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4000 $fix) {
4001 my $sub_from = $ident;
4002 my $sub_to = $ident;
4003 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004004 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004005 s@\Q$sub_from\E@$sub_to@;
4006 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004007 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004008 }
4009 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4010 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004011 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004012
Andy Whitcroft65863862009-01-06 14:41:21 -08004013 # Should start with a space.
4014 $to =~ s/^(\S)/ $1/;
4015 # Should not end with a space.
4016 $to =~ s/\s+$//;
4017 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004018 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004019 }
4020 # Modifiers should have spaces.
4021 $to =~ s/(\b$Modifier$)/$1 /;
4022
Joe Perches3705ce52013-07-03 15:05:31 -07004023## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004024 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004025 if (ERROR("POINTER_LOCATION",
4026 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4027 $fix) {
4028
4029 my $sub_from = $match;
4030 my $sub_to = $match;
4031 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004032 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004033 s@\Q$sub_from\E@$sub_to@;
4034 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004035 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004036 }
4037
Joe Perches9d3e3c72015-09-09 15:37:27 -07004038# avoid BUG() or BUG_ON()
4039 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004040 my $msg_level = \&WARN;
4041 $msg_level = \&CHK if ($file);
4042 &{$msg_level}("AVOID_BUG",
4043 "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 -07004044 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004045
Joe Perches9d3e3c72015-09-09 15:37:27 -07004046# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004047 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004048 WARN("LINUX_VERSION_CODE",
4049 "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 -08004050 }
4051
Joe Perches17441222011-06-15 15:08:17 -07004052# check for uses of printk_ratelimit
4053 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004054 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004055 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004056 }
4057
Joe Percheseeef5732017-11-17 15:28:41 -08004058# printk should use KERN_* levels
4059 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4060 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4061 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004062 }
4063
Joe Perches243f3802012-05-31 16:26:09 -07004064 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4065 my $orig = $1;
4066 my $level = lc($orig);
4067 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004068 my $level2 = $level;
4069 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004070 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004071 "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 -07004072 }
4073
4074 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004075 if (WARN("PREFER_PR_LEVEL",
4076 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4077 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004078 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004079 s/\bpr_warning\b/pr_warn/;
4080 }
Joe Perches243f3802012-05-31 16:26:09 -07004081 }
4082
Joe Perchesdc139312013-02-21 16:44:13 -08004083 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4084 my $orig = $1;
4085 my $level = lc($orig);
4086 $level = "warn" if ($level eq "warning");
4087 $level = "dbg" if ($level eq "debug");
4088 WARN("PREFER_DEV_LEVEL",
4089 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4090 }
4091
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004092# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4093# number of false positives, but assembly files are not checked, so at
4094# least the arch entry code will not trigger this warning.
4095 if ($line =~ /\bENOSYS\b/) {
4096 WARN("ENOSYS",
4097 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4098 }
4099
Andy Whitcroft653d4872007-06-23 17:16:34 -07004100# function brace can't be on same line, except for #defines of do while,
4101# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004102 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004103 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4104 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4105 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004106 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004107 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004108 $fix) {
4109 fix_delete_line($fixlinenr, $rawline);
4110 my $fixed_line = $rawline;
4111 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4112 my $line1 = $1;
4113 my $line2 = $2;
4114 fix_insert_line($fixlinenr, ltrim($line1));
4115 fix_insert_line($fixlinenr, "\+{");
4116 if ($line2 !~ /^\s*$/) {
4117 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4118 }
4119 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004120 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004121
Andy Whitcroft8905a672007-11-28 16:21:06 -08004122# open braces for enum, union and struct go on the same line.
4123 if ($line =~ /^.\s*{/ &&
4124 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004125 if (ERROR("OPEN_BRACE",
4126 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4127 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4128 fix_delete_line($fixlinenr - 1, $prevrawline);
4129 fix_delete_line($fixlinenr, $rawline);
4130 my $fixedline = rtrim($prevrawline) . " {";
4131 fix_insert_line($fixlinenr, $fixedline);
4132 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004133 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004134 if ($fixedline !~ /^\+\s*$/) {
4135 fix_insert_line($fixlinenr, $fixedline);
4136 }
4137 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004138 }
4139
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004140# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004141 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4142 if (WARN("SPACING",
4143 "missing space after $1 definition\n" . $herecurr) &&
4144 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004145 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004146 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4147 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004148 }
4149
Joe Perches31070b52014-01-23 15:54:49 -08004150# Function pointer declarations
4151# check spacing between type, funcptr, and args
4152# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004153 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004154 my $declare = $1;
4155 my $pre_pointer_space = $2;
4156 my $post_pointer_space = $3;
4157 my $funcname = $4;
4158 my $post_funcname_space = $5;
4159 my $pre_args_space = $6;
4160
Joe Perches91f72e92014-04-03 14:49:12 -07004161# the $Declare variable will capture all spaces after the type
4162# so check it for a missing trailing missing space but pointer return types
4163# don't need a space so don't warn for those.
4164 my $post_declare_space = "";
4165 if ($declare =~ /(\s+)$/) {
4166 $post_declare_space = $1;
4167 $declare = rtrim($declare);
4168 }
4169 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004170 WARN("SPACING",
4171 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004172 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004173 }
4174
4175# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004176# This test is not currently implemented because these declarations are
4177# equivalent to
4178# int foo(int bar, ...)
4179# and this is form shouldn't/doesn't generate a checkpatch warning.
4180#
4181# elsif ($declare =~ /\s{2,}$/) {
4182# WARN("SPACING",
4183# "Multiple spaces after return type\n" . $herecurr);
4184# }
Joe Perches31070b52014-01-23 15:54:49 -08004185
4186# unnecessary space "type ( *funcptr)(args...)"
4187 if (defined $pre_pointer_space &&
4188 $pre_pointer_space =~ /^\s/) {
4189 WARN("SPACING",
4190 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4191 }
4192
4193# unnecessary space "type (* funcptr)(args...)"
4194 if (defined $post_pointer_space &&
4195 $post_pointer_space =~ /^\s/) {
4196 WARN("SPACING",
4197 "Unnecessary space before function pointer name\n" . $herecurr);
4198 }
4199
4200# unnecessary space "type (*funcptr )(args...)"
4201 if (defined $post_funcname_space &&
4202 $post_funcname_space =~ /^\s/) {
4203 WARN("SPACING",
4204 "Unnecessary space after function pointer name\n" . $herecurr);
4205 }
4206
4207# unnecessary space "type (*funcptr) (args...)"
4208 if (defined $pre_args_space &&
4209 $pre_args_space =~ /^\s/) {
4210 WARN("SPACING",
4211 "Unnecessary space before function pointer arguments\n" . $herecurr);
4212 }
4213
4214 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004215 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004216 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004217 }
4218 }
4219
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004220# check for spacing round square brackets; allowed:
4221# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004222# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4223# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004224 while ($line =~ /(.*?\s)\[/g) {
4225 my ($where, $prefix) = ($-[1], $1);
4226 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004227 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004228 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004229 if (ERROR("BRACKET_SPACE",
4230 "space prohibited before open square bracket '['\n" . $herecurr) &&
4231 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004232 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004233 s/^(\+.*?)\s+\[/$1\[/;
4234 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004235 }
4236 }
4237
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004238# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004239 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004240 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004241 my $ctx_before = substr($line, 0, $-[1]);
4242 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004243
4244 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004245 if ($name =~ /^(?:
4246 if|for|while|switch|return|case|
4247 volatile|__volatile__|
4248 __attribute__|format|__extension__|
4249 asm|__asm__)$/x)
4250 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004251 # cpp #define statements have non-optional spaces, ie
4252 # if there is a space between the name and the open
4253 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004254 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004255
4256 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004257 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004258
4259 # If this whole things ends with a type its most
4260 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004261 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004262
4263 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004264 if (WARN("SPACING",
4265 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4266 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004267 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004268 s/\b$name\s+\(/$name\(/;
4269 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004270 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004271 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004272
Andy Whitcroft653d4872007-06-23 17:16:34 -07004273# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004274 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004275 my $fixed_line = "";
4276 my $line_fixed = 0;
4277
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004278 my $ops = qr{
4279 <<=|>>=|<=|>=|==|!=|
4280 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4281 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004282 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004283 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004284 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004285 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004286
4287## print("element count: <" . $#elements . ">\n");
4288## foreach my $el (@elements) {
4289## print("el: <$el>\n");
4290## }
4291
4292 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004293 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004294
Joe Perches3705ce52013-07-03 15:05:31 -07004295 foreach my $el (@elements) {
4296 push(@fix_elements, substr($rawline, $off, length($el)));
4297 $off += length($el);
4298 }
4299
4300 $off = 0;
4301
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004302 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004303 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004304
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004305 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004306
4307 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4308
4309## print("n: <$n> good: <$good>\n");
4310
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004311 $off += length($elements[$n]);
4312
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004313 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004314 my $ca = substr($opline, 0, $off);
4315 my $cc = '';
4316 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4317 $cc = substr($opline, $off + length($elements[$n + 1]));
4318 }
4319 my $cb = "$ca$;$cc";
4320
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004321 my $a = '';
4322 $a = 'V' if ($elements[$n] ne '');
4323 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004324 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004325 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4326 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004327 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004328
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004329 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004330
4331 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004332 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004333 $c = 'V' if ($elements[$n + 2] ne '');
4334 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004335 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004336 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4337 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004338 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004339 } else {
4340 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004341 }
4342
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004343 my $ctx = "${a}x${c}";
4344
4345 my $at = "(ctx:$ctx)";
4346
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004347 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004348 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004349
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004350 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004351 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004352
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004353 # Get the full operator variant.
4354 my $opv = $op . substr($curr_vars, $off, 1);
4355
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004356 # Ignore operators passed as parameters.
4357 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004358 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004359
Andy Whitcroftcf655042008-03-04 14:28:20 -08004360# # Ignore comments
4361# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004362
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004363 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004364 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004365 if ($ctx !~ /.x[WEBC]/ &&
4366 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004367 if (ERROR("SPACING",
4368 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004369 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004370 $line_fixed = 1;
4371 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004372 }
4373
4374 # // is a comment
4375 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004376
Joe Perchesb00e4812014-04-03 14:49:33 -07004377 # : when part of a bitfield
4378 } elsif ($opv eq ':B') {
4379 # skip the bitfield test for now
4380
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004381 # No spaces for:
4382 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004383 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004384 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004385 if (ERROR("SPACING",
4386 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004387 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004388 if (defined $fix_elements[$n + 2]) {
4389 $fix_elements[$n + 2] =~ s/^\s+//;
4390 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004391 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004392 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004393 }
4394
Joe Perches23810972014-12-10 15:51:32 -08004395 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004396 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004397 my $rtrim_before = 0;
4398 my $space_after = 0;
4399 if ($ctx =~ /Wx./) {
4400 if (ERROR("SPACING",
4401 "space prohibited before that '$op' $at\n" . $hereptr)) {
4402 $line_fixed = 1;
4403 $rtrim_before = 1;
4404 }
4405 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004406 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004407 if (ERROR("SPACING",
4408 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004409 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004410 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004411 $space_after = 1;
4412 }
4413 }
4414 if ($rtrim_before || $space_after) {
4415 if ($rtrim_before) {
4416 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4417 } else {
4418 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4419 }
4420 if ($space_after) {
4421 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004422 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004423 }
4424
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004425 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004426 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004427 #warn "'*' is part of type\n";
4428
4429 # unary operators should have a space before and
4430 # none after. May be left adjacent to another
4431 # unary operator, or a cast
4432 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004433 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004434 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004435 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004436 if (ERROR("SPACING",
4437 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004438 if ($n != $last_after + 2) {
4439 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4440 $line_fixed = 1;
4441 }
Joe Perches3705ce52013-07-03 15:05:31 -07004442 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004443 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004444 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004445 # A unary '*' may be const
4446
4447 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004448 if (ERROR("SPACING",
4449 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004450 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004451 if (defined $fix_elements[$n + 2]) {
4452 $fix_elements[$n + 2] =~ s/^\s+//;
4453 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004454 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004455 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004456 }
4457
4458 # unary ++ and unary -- are allowed no space on one side.
4459 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004460 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004461 if (ERROR("SPACING",
4462 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004463 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004464 $line_fixed = 1;
4465 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004466 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004467 if ($ctx =~ /Wx[BE]/ ||
4468 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004469 if (ERROR("SPACING",
4470 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004471 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004472 $line_fixed = 1;
4473 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004474 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004475 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004476 if (ERROR("SPACING",
4477 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004478 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004479 if (defined $fix_elements[$n + 2]) {
4480 $fix_elements[$n + 2] =~ s/^\s+//;
4481 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004482 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004483 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004484 }
4485
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004486 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004487 } elsif ($op eq '<<' or $op eq '>>' or
4488 $op eq '&' or $op eq '^' or $op eq '|' or
4489 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004490 $op eq '*' or $op eq '/' or
4491 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004492 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004493 if ($check) {
4494 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4495 if (CHK("SPACING",
4496 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4497 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4498 $fix_elements[$n + 2] =~ s/^\s+//;
4499 $line_fixed = 1;
4500 }
4501 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4502 if (CHK("SPACING",
4503 "space preferred before that '$op' $at\n" . $hereptr)) {
4504 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4505 $line_fixed = 1;
4506 }
4507 }
4508 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004509 if (ERROR("SPACING",
4510 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004511 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4512 if (defined $fix_elements[$n + 2]) {
4513 $fix_elements[$n + 2] =~ s/^\s+//;
4514 }
Joe Perches3705ce52013-07-03 15:05:31 -07004515 $line_fixed = 1;
4516 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004517 }
4518
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004519 # A colon needs no spaces before when it is
4520 # terminating a case value or a label.
4521 } elsif ($opv eq ':C' || $opv eq ':L') {
4522 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004523 if (ERROR("SPACING",
4524 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004525 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004526 $line_fixed = 1;
4527 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004528 }
4529
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004530 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004531 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004532 my $ok = 0;
4533
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004534 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004535 if (($op eq '<' &&
4536 $cc =~ /^\S+\@\S+>/) ||
4537 ($op eq '>' &&
4538 $ca =~ /<\S+\@\S+$/))
4539 {
4540 $ok = 1;
4541 }
4542
Joe Perchese0df7e12015-04-16 12:44:53 -07004543 # for asm volatile statements
4544 # ignore a colon with another
4545 # colon immediately before or after
4546 if (($op eq ':') &&
4547 ($ca =~ /:$/ || $cc =~ /^:/)) {
4548 $ok = 1;
4549 }
4550
Joe Perches84731622013-11-12 15:10:05 -08004551 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004552 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004553 my $msg_level = \&ERROR;
4554 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004555
Jean Delvare0675a8f2017-09-08 16:16:07 -07004556 if (&{$msg_level}("SPACING",
4557 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004558 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4559 if (defined $fix_elements[$n + 2]) {
4560 $fix_elements[$n + 2] =~ s/^\s+//;
4561 }
Joe Perches3705ce52013-07-03 15:05:31 -07004562 $line_fixed = 1;
4563 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004564 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004565 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004566 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004567
4568## print("n: <$n> GOOD: <$good>\n");
4569
4570 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004571 }
Joe Perches3705ce52013-07-03 15:05:31 -07004572
4573 if (($#elements % 2) == 0) {
4574 $fixed_line = $fixed_line . $fix_elements[$#elements];
4575 }
4576
Joe Perches194f66f2014-08-06 16:11:03 -07004577 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4578 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004579 }
4580
4581
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004582 }
4583
Joe Perches786b6322013-07-03 15:05:32 -07004584# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004585 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004586 if (WARN("SPACING",
4587 "space prohibited before semicolon\n" . $herecurr) &&
4588 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004589 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004590 s/^(\+.*\S)\s+;/$1;/;
4591 }
4592 }
4593
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004594# check for multiple assignments
4595 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004596 CHK("MULTIPLE_ASSIGNMENTS",
4597 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004598 }
4599
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004600## # check for multiple declarations, allowing for a function declaration
4601## # continuation.
4602## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4603## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4604##
4605## # Remove any bracketed sections to ensure we do not
4606## # falsly report the parameters of functions.
4607## my $ln = $line;
4608## while ($ln =~ s/\([^\(\)]*\)//g) {
4609## }
4610## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004611## WARN("MULTIPLE_DECLARATION",
4612## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004613## }
4614## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004615
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004616#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004617 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004618 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004619 if (ERROR("SPACING",
4620 "space required before the open brace '{'\n" . $herecurr) &&
4621 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004622 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004623 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004624 }
4625
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004626## # check for blank lines before declarations
4627## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4628## $prevrawline =~ /^.\s*$/) {
4629## WARN("SPACING",
4630## "No blank lines before declarations\n" . $hereprev);
4631## }
4632##
4633
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004634# closing brace should have a space following it when it has anything
4635# on the line
4636 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004637 if (ERROR("SPACING",
4638 "space required after that close brace '}'\n" . $herecurr) &&
4639 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004640 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004641 s/}((?!(?:,|;|\)))\S)/} $1/;
4642 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004643 }
4644
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004645# check spacing on square brackets
4646 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004647 if (ERROR("SPACING",
4648 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4649 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004650 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004651 s/\[\s+/\[/;
4652 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004653 }
4654 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004655 if (ERROR("SPACING",
4656 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4657 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004658 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004659 s/\s+\]/\]/;
4660 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004661 }
4662
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004663# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004664 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4665 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004666 if (ERROR("SPACING",
4667 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4668 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004669 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004670 s/\(\s+/\(/;
4671 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004672 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004673 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004674 $line !~ /for\s*\(.*;\s+\)/ &&
4675 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004676 if (ERROR("SPACING",
4677 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4678 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004679 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004680 s/\s+\)/\)/;
4681 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004682 }
4683
Joe Perchese2826fd2014-08-06 16:10:48 -07004684# check unnecessary parentheses around addressof/dereference single $Lvals
4685# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4686
4687 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004688 my $var = $1;
4689 if (CHK("UNNECESSARY_PARENTHESES",
4690 "Unnecessary parentheses around $var\n" . $herecurr) &&
4691 $fix) {
4692 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4693 }
4694 }
4695
4696# check for unnecessary parentheses around function pointer uses
4697# ie: (foo->bar)(); should be foo->bar();
4698# but not "if (foo->bar) (" to avoid some false positives
4699 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4700 my $var = $2;
4701 if (CHK("UNNECESSARY_PARENTHESES",
4702 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4703 $fix) {
4704 my $var2 = deparenthesize($var);
4705 $var2 =~ s/\s//g;
4706 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4707 }
4708 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004709
Joe Perches63b7c732017-09-08 16:16:01 -07004710# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004711# when !drivers/staging or command-line uses --strict
4712 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004713 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004714 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4715 my $if_stat = $1;
4716 my $test = substr($2, 1, -1);
4717 my $herectx;
4718 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4719 my $match = $1;
4720 # avoid parentheses around potential macro args
4721 next if ($match =~ /^\s*\w+\s*$/);
4722 if (!defined($herectx)) {
4723 $herectx = $here . "\n";
4724 my $cnt = statement_rawlines($if_stat);
4725 for (my $n = 0; $n < $cnt; $n++) {
4726 my $rl = raw_line($linenr, $n);
4727 $herectx .= $rl . "\n";
4728 last if $rl =~ /^[ \+].*\{/;
4729 }
4730 }
4731 CHK("UNNECESSARY_PARENTHESES",
4732 "Unnecessary parentheses around '$match'\n" . $herectx);
4733 }
4734 }
4735
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004736#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004737 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004738 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004739 if (WARN("INDENTED_LABEL",
4740 "labels should not be indented\n" . $herecurr) &&
4741 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004742 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004743 s/^(.)\s+/$1/;
4744 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004745 }
4746
Joe Perches5b9553a2014-04-03 14:49:21 -07004747# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004748 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004749 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004750 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004751 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4752 my $value = $1;
4753 $value = deparenthesize($value);
4754 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4755 ERROR("RETURN_PARENTHESES",
4756 "return is not a function, parentheses are not required\n" . $herecurr);
4757 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004758 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004759 ERROR("SPACING",
4760 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004761 }
4762 }
Joe Perches507e5142013-11-12 15:10:13 -08004763
Joe Perchesb43ae212014-06-23 13:22:07 -07004764# unnecessary return in a void function
4765# at end-of-function, with the previous line a single leading tab, then return;
4766# and the line before that not a goto label target like "out:"
4767 if ($sline =~ /^[ \+]}\s*$/ &&
4768 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4769 $linenr >= 3 &&
4770 $lines[$linenr - 3] =~ /^[ +]/ &&
4771 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004772 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004773 "void function return statements are not generally useful\n" . $hereprev);
4774 }
Joe Perches9819cf22014-06-04 16:12:09 -07004775
Joe Perches189248d2014-01-23 15:54:47 -08004776# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004777 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004778 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4779 my $openparens = $1;
4780 my $count = $openparens =~ tr@\(@\(@;
4781 my $msg = "";
4782 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4783 my $comp = $4; #Not $1 because of $LvalOrFunc
4784 $msg = " - maybe == should be = ?" if ($comp eq "==");
4785 WARN("UNNECESSARY_PARENTHESES",
4786 "Unnecessary parentheses$msg\n" . $herecurr);
4787 }
4788 }
4789
Joe Perchesc5595fa2015-09-09 15:37:58 -07004790# comparisons with a constant or upper case identifier on the left
4791# avoid cases like "foo + BAR < baz"
4792# only fix matches surrounded by parentheses to avoid incorrect
4793# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004794 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004795 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4796 my $lead = $1;
4797 my $const = $2;
4798 my $comp = $3;
4799 my $to = $4;
4800 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004801 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004802 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4803 WARN("CONSTANT_COMPARISON",
4804 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4805 $fix) {
4806 if ($comp eq "<") {
4807 $newcomp = ">";
4808 } elsif ($comp eq "<=") {
4809 $newcomp = ">=";
4810 } elsif ($comp eq ">") {
4811 $newcomp = "<";
4812 } elsif ($comp eq ">=") {
4813 $newcomp = "<=";
4814 }
4815 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4816 }
4817 }
4818
Joe Perchesf34e4a42015-04-16 12:44:19 -07004819# Return of what appears to be an errno should normally be negative
4820 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004821 my $name = $1;
4822 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004823 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004824 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004825 }
4826 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004827
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004828# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004829 if ($line =~ /\b(if|while|for|switch)\(/) {
4830 if (ERROR("SPACING",
4831 "space required before the open parenthesis '('\n" . $herecurr) &&
4832 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004833 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004834 s/\b(if|while|for|switch)\(/$1 \(/;
4835 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004836 }
4837
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004838# Check for illegal assignment in if conditional -- and check for trailing
4839# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004840 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004841 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4842 ctx_statement_block($linenr, $realcnt, 0)
4843 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004844 my ($stat_next) = ctx_statement_block($line_nr_next,
4845 $remain_next, $off_next);
4846 $stat_next =~ s/\n./\n /g;
4847 ##print "stat<$stat> stat_next<$stat_next>\n";
4848
4849 if ($stat_next =~ /^\s*while\b/) {
4850 # If the statement carries leading newlines,
4851 # then count those as offsets.
4852 my ($whitespace) =
4853 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4854 my $offset =
4855 statement_rawlines($whitespace) - 1;
4856
4857 $suppress_whiletrailers{$line_nr_next +
4858 $offset} = 1;
4859 }
4860 }
4861 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004862 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004863 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004864 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004865
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004866 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004867 ERROR("ASSIGN_IN_IF",
4868 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004869 }
4870
4871 # Find out what is on the end of the line after the
4872 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004873 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004874 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004875 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004876 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4877 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004878 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004879 # Find out how long the conditional actually is.
4880 my @newlines = ($c =~ /\n/gs);
4881 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004882 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004883
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004884 $stat_real = raw_line($linenr, $cond_lines)
4885 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004886 if (defined($stat_real) && $cond_lines > 1) {
4887 $stat_real = "[...]\n$stat_real";
4888 }
4889
Joe Perches000d1cc12011-07-25 17:13:25 -07004890 ERROR("TRAILING_STATEMENTS",
4891 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004892 }
4893 }
4894
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004895# Check for bitwise tests written as boolean
4896 if ($line =~ /
4897 (?:
4898 (?:\[|\(|\&\&|\|\|)
4899 \s*0[xX][0-9]+\s*
4900 (?:\&\&|\|\|)
4901 |
4902 (?:\&\&|\|\|)
4903 \s*0[xX][0-9]+\s*
4904 (?:\&\&|\|\||\)|\])
4905 )/x)
4906 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004907 WARN("HEXADECIMAL_BOOLEAN_TEST",
4908 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004909 }
4910
Andy Whitcroft8905a672007-11-28 16:21:06 -08004911# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004912 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4913 my $s = $1;
4914 $s =~ s/$;//g; # Remove any comments
4915 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004916 ERROR("TRAILING_STATEMENTS",
4917 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004918 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004919 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004920# if should not continue a brace
4921 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004922 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004923 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004924 $herecurr);
4925 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004926# case and default should not have general statements after them
4927 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4928 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004929 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004930 \s*return\s+
4931 )/xg)
4932 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004933 ERROR("TRAILING_STATEMENTS",
4934 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004935 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004936
4937 # Check for }<nl>else {, these must be at the same
4938 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004939 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4940 $previndent == $indent) {
4941 if (ERROR("ELSE_AFTER_BRACE",
4942 "else should follow close brace '}'\n" . $hereprev) &&
4943 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4944 fix_delete_line($fixlinenr - 1, $prevrawline);
4945 fix_delete_line($fixlinenr, $rawline);
4946 my $fixedline = $prevrawline;
4947 $fixedline =~ s/}\s*$//;
4948 if ($fixedline !~ /^\+\s*$/) {
4949 fix_insert_line($fixlinenr, $fixedline);
4950 }
4951 $fixedline = $rawline;
4952 $fixedline =~ s/^(.\s*)else/$1} else/;
4953 fix_insert_line($fixlinenr, $fixedline);
4954 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004955 }
4956
Joe Perches8b8856f2014-08-06 16:11:14 -07004957 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4958 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004959 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4960
4961 # Find out what is on the end of the line after the
4962 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004963 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004964 $s =~ s/\n.*//g;
4965
4966 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004967 if (ERROR("WHILE_AFTER_BRACE",
4968 "while should follow close brace '}'\n" . $hereprev) &&
4969 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4970 fix_delete_line($fixlinenr - 1, $prevrawline);
4971 fix_delete_line($fixlinenr, $rawline);
4972 my $fixedline = $prevrawline;
4973 my $trailing = $rawline;
4974 $trailing =~ s/^\+//;
4975 $trailing = trim($trailing);
4976 $fixedline =~ s/}\s*$/} $trailing/;
4977 fix_insert_line($fixlinenr, $fixedline);
4978 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004979 }
4980 }
4981
Joe Perches95e2c602013-07-03 15:05:20 -07004982#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004983 while ($line =~ m{($Constant|$Lval)}g) {
4984 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004985
Joe Perches95e2c602013-07-03 15:05:20 -07004986#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004987 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004988 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004989#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004990 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004991#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004992 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4993#Ignore some three character SI units explicitly, like MiB and KHz
4994 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004995 while ($var =~ m{($Ident)}g) {
4996 my $word = $1;
4997 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004998 if ($check) {
4999 seed_camelcase_includes();
5000 if (!$file && !$camelcase_file_seeded) {
5001 seed_camelcase_file($realfile);
5002 $camelcase_file_seeded = 1;
5003 }
5004 }
Joe Perches7e781f62013-09-11 14:23:55 -07005005 if (!defined $camelcase{$word}) {
5006 $camelcase{$word} = 1;
5007 CHK("CAMELCASE",
5008 "Avoid CamelCase: <$word>\n" . $herecurr);
5009 }
Joe Perches34456862013-07-03 15:05:34 -07005010 }
Joe Perches323c1262012-12-17 16:02:07 -08005011 }
5012 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005013
5014#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005015 if ($line =~ /\#\s*define.*\\\s+$/) {
5016 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5017 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5018 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005019 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005020 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005021 }
5022
Fabian Frederick0e212e02015-04-16 12:44:25 -07005023# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5024# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005025 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005026 my $file = "$1.h";
5027 my $checkfile = "include/linux/$file";
5028 if (-f "$root/$checkfile" &&
5029 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005030 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005031 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005032 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5033 if ($asminclude > 0) {
5034 if ($realfile =~ m{^arch/}) {
5035 CHK("ARCH_INCLUDE_LINUX",
5036 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5037 } else {
5038 WARN("INCLUDE_LINUX",
5039 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5040 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005041 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005042 }
5043 }
5044
Andy Whitcroft653d4872007-06-23 17:16:34 -07005045# multi-statement macros should be enclosed in a do while loop, grab the
5046# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005047# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005048 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5049 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005050 my $ln = $linenr;
5051 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005052 my ($off, $dstat, $dcond, $rest);
5053 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005054 my $has_flow_statement = 0;
5055 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005056 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005057 ctx_statement_block($linenr, $realcnt, 0);
5058 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005059 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005060 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005061
Joe Perches08a28432014-10-13 15:51:55 -07005062 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005063 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005064
Joe Perchesf59b64b2016-10-11 13:52:08 -07005065 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5066 my $define_args = $1;
5067 my $define_stmt = $dstat;
5068 my @def_args = ();
5069
5070 if (defined $define_args && $define_args ne "") {
5071 $define_args = substr($define_args, 1, length($define_args) - 2);
5072 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005073 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005074 @def_args = split(",", $define_args);
5075 }
5076
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005077 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005078 $dstat =~ s/\\\n.//g;
5079 $dstat =~ s/^\s*//s;
5080 $dstat =~ s/\s*$//s;
5081
5082 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005083 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5084 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005085 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005086 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005087 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005088
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005089 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005090 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5091 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005092 {
5093 }
5094
Joe Perches42e15292016-03-15 14:58:01 -07005095 # Make asm volatile uses seem like a generic function
5096 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5097
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005098 my $exceptions = qr{
5099 $Declare|
5100 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005101 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005102 DECLARE_PER_CPU|
5103 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005104 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005105 union|
5106 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005107 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005108 ^\"|\"$|
5109 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005110 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005111 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005112
5113 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005114 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005115 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005116
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005117 if ($dstat ne '' &&
5118 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5119 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005120 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005121 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005122 $dstat !~ /$exceptions/ &&
5123 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005124 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005125 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005126 $dstat !~ /^for\s*$Constant$/ && # for (...)
5127 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5128 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005129 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005130 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005131 {
Joe Perchese7955562017-05-08 15:55:48 -07005132 if ($dstat =~ /^\s*if\b/) {
5133 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5134 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5135 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005136 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5137 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5138 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005139 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005140 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005141 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005142
5143 }
Joe Perches52076492016-10-11 13:52:14 -07005144
5145 # Make $define_stmt single line, comment-free, etc
5146 my @stmt_array = split('\n', $define_stmt);
5147 my $first = 1;
5148 $define_stmt = "";
5149 foreach my $l (@stmt_array) {
5150 $l =~ s/\\$//;
5151 if ($first) {
5152 $define_stmt = $l;
5153 $first = 0;
5154 } elsif ($l =~ /^[\+ ]/) {
5155 $define_stmt .= substr($l, 1);
5156 }
5157 }
5158 $define_stmt =~ s/$;//g;
5159 $define_stmt =~ s/\s+/ /g;
5160 $define_stmt = trim($define_stmt);
5161
Joe Perchesf59b64b2016-10-11 13:52:08 -07005162# check if any macro arguments are reused (ignore '...' and 'type')
5163 foreach my $arg (@def_args) {
5164 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005165 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005166 my $tmp_stmt = $define_stmt;
5167 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5168 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5169 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005170 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005171 if ($use_cnt > 1) {
5172 CHK("MACRO_ARG_REUSE",
5173 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005174 }
5175# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005176 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005177 ((defined($1) && $1 ne ',') ||
5178 (defined($2) && $2 ne ','))) {
5179 CHK("MACRO_ARG_PRECEDENCE",
5180 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005181 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005182 }
Joe Perches5023d342012-12-17 16:01:47 -08005183
Joe Perches08a28432014-10-13 15:51:55 -07005184# check for macros with flow control, but without ## concatenation
5185# ## concatenation is commonly a macro that defines a function so ignore those
5186 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005187 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005188 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005189
Joe Perches08a28432014-10-13 15:51:55 -07005190 WARN("MACRO_WITH_FLOW_CONTROL",
5191 "Macros with flow control statements should be avoided\n" . "$herectx");
5192 }
5193
Joe Perches481eb482012-12-17 16:01:56 -08005194# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005195
5196 } else {
5197 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005198 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5199 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005200 $line =~ /^\+.*\\$/) {
5201 WARN("LINE_CONTINUATIONS",
5202 "Avoid unnecessary line continuations\n" . $herecurr);
5203 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005204 }
5205
Joe Perchesb13edf72012-07-30 14:41:24 -07005206# do {} while (0) macro tests:
5207# single-statement macros do not need to be enclosed in do while (0) loop,
5208# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005209 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005210 $realfile !~ m@/vmlinux.lds.h$@ &&
5211 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5212 my $ln = $linenr;
5213 my $cnt = $realcnt;
5214 my ($off, $dstat, $dcond, $rest);
5215 my $ctx = '';
5216 ($dstat, $dcond, $ln, $cnt, $off) =
5217 ctx_statement_block($linenr, $realcnt, 0);
5218 $ctx = $dstat;
5219
5220 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005221 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005222
5223 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5224 my $stmts = $2;
5225 my $semis = $3;
5226
5227 $ctx =~ s/\n*$//;
5228 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005229 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005230
Joe Perchesac8e97f2012-08-21 16:15:53 -07005231 if (($stmts =~ tr/;/;/) == 1 &&
5232 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005233 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5234 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5235 }
5236 if (defined $semis && $semis ne "") {
5237 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5238 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5239 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005240 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5241 $ctx =~ s/\n*$//;
5242 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005243 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005244
5245 WARN("TRAILING_SEMICOLON",
5246 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005247 }
5248 }
5249
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005250# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005251 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5252 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005253 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005254 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005255 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5256 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005257 my @allowed = ();
5258 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005259 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005260 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005261 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005262 for my $chunk (@chunks) {
5263 my ($cond, $block) = @{$chunk};
5264
Andy Whitcroft773647a2008-03-28 14:15:58 -07005265 # If the condition carries leading newlines, then count those as offsets.
5266 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5267 my $offset = statement_rawlines($whitespace) - 1;
5268
Joe Perchesaad4f612012-03-23 15:02:19 -07005269 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005270 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5271
5272 # We have looked at and allowed this specific line.
5273 $suppress_ifbraces{$ln + $offset} = 1;
5274
5275 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005276 $ln += statement_rawlines($block) - 1;
5277
Andy Whitcroft773647a2008-03-28 14:15:58 -07005278 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005279
5280 $seen++ if ($block =~ /^\s*{/);
5281
Joe Perchesaad4f612012-03-23 15:02:19 -07005282 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005283 if (statement_lines($cond) > 1) {
5284 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005285 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005286 }
5287 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005288 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005289 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005290 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005291 if (statement_block_size($block) > 1) {
5292 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005293 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005294 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005295 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005296 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005297 if ($seen) {
5298 my $sum_allowed = 0;
5299 foreach (@allowed) {
5300 $sum_allowed += $_;
5301 }
5302 if ($sum_allowed == 0) {
5303 WARN("BRACES",
5304 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5305 } elsif ($sum_allowed != $allow &&
5306 $seen != $allow) {
5307 CHK("BRACES",
5308 "braces {} should be used on all arms of this statement\n" . $herectx);
5309 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005310 }
5311 }
5312 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005313 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005314 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005315 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005316
Andy Whitcroftcf655042008-03-04 14:28:20 -08005317 # Check the pre-context.
5318 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5319 #print "APW: ALLOWED: pre<$1>\n";
5320 $allowed = 1;
5321 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005322
5323 my ($level, $endln, @chunks) =
5324 ctx_statement_full($linenr, $realcnt, $-[0]);
5325
Andy Whitcroftcf655042008-03-04 14:28:20 -08005326 # Check the condition.
5327 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005328 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005329 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005330 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005331 }
5332 if (statement_lines($cond) > 1) {
5333 #print "APW: ALLOWED: cond<$cond>\n";
5334 $allowed = 1;
5335 }
5336 if ($block =~/\b(?:if|for|while)\b/) {
5337 #print "APW: ALLOWED: block<$block>\n";
5338 $allowed = 1;
5339 }
5340 if (statement_block_size($block) > 1) {
5341 #print "APW: ALLOWED: lines block<$block>\n";
5342 $allowed = 1;
5343 }
5344 # Check the post-context.
5345 if (defined $chunks[1]) {
5346 my ($cond, $block) = @{$chunks[1]};
5347 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005348 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005349 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005350 if ($block =~ /^\s*\{/) {
5351 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5352 $allowed = 1;
5353 }
5354 }
5355 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005356 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005357 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005358
Joe Perches000d1cc12011-07-25 17:13:25 -07005359 WARN("BRACES",
5360 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005361 }
5362 }
5363
Joe Perchese4c5bab2017-02-24 15:01:41 -08005364# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005365 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5366 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005367 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5368 }
5369
Joe Perches0979ae62012-12-17 16:01:59 -08005370# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005371 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005372 if (CHK("BRACES",
5373 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5374 $fix && $prevrawline =~ /^\+/) {
5375 fix_delete_line($fixlinenr - 1, $prevrawline);
5376 }
Joe Perches0979ae62012-12-17 16:01:59 -08005377 }
Joe Perches77b9a532013-07-03 15:05:29 -07005378 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005379 if (CHK("BRACES",
5380 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5381 $fix) {
5382 fix_delete_line($fixlinenr, $rawline);
5383 }
Joe Perches0979ae62012-12-17 16:01:59 -08005384 }
5385
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005386# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005387 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5388 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005389 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005390 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005391 }
5392
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005393# Check for user-visible strings broken across lines, which breaks the ability
5394# to grep for the string. Make exceptions when the previous string ends in a
5395# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5396# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005397 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005398 $prevline =~ /"\s*$/ &&
5399 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5400 if (WARN("SPLIT_STRING",
5401 "quoted string split across lines\n" . $hereprev) &&
5402 $fix &&
5403 $prevrawline =~ /^\+.*"\s*$/ &&
5404 $last_coalesced_string_linenr != $linenr - 1) {
5405 my $extracted_string = get_quoted_string($line, $rawline);
5406 my $comma_close = "";
5407 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5408 $comma_close = $1;
5409 }
5410
5411 fix_delete_line($fixlinenr - 1, $prevrawline);
5412 fix_delete_line($fixlinenr, $rawline);
5413 my $fixedline = $prevrawline;
5414 $fixedline =~ s/"\s*$//;
5415 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5416 fix_insert_line($fixlinenr - 1, $fixedline);
5417 $fixedline = $rawline;
5418 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5419 if ($fixedline !~ /\+\s*$/) {
5420 fix_insert_line($fixlinenr, $fixedline);
5421 }
5422 $last_coalesced_string_linenr = $linenr;
5423 }
5424 }
5425
5426# check for missing a space in a string concatenation
5427 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5428 WARN('MISSING_SPACE',
5429 "break quoted strings at a space character\n" . $hereprev);
5430 }
5431
Joe Perchese4b7d302017-05-08 15:55:51 -07005432# check for an embedded function name in a string when the function is known
5433# This does not work very well for -f --file checking as it depends on patch
5434# context providing the function name or a single line form for in-file
5435# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005436 if ($line =~ /^\+.*$String/ &&
5437 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005438 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5439 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005440 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005441 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005442 }
5443
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005444# check for spaces before a quoted newline
5445 if ($rawline =~ /^.*\".*\s\\n/) {
5446 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5447 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5448 $fix) {
5449 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5450 }
5451
5452 }
5453
Joe Perchesf17dba42014-10-13 15:51:51 -07005454# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005455 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5456 if (CHK("CONCATENATED_STRING",
5457 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5458 $fix) {
5459 while ($line =~ /($String)/g) {
5460 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5461 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5462 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5463 }
5464 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005465 }
5466
Joe Perches90ad30e2014-12-10 15:51:59 -08005467# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005468 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005469 if (WARN("STRING_FRAGMENTS",
5470 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5471 $fix) {
5472 while ($line =~ /($String)(?=\s*")/g) {
5473 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5474 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5475 }
5476 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005477 }
5478
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005479# check for non-standard and hex prefixed decimal printf formats
5480 my $show_L = 1; #don't show the same defect twice
5481 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005482 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005483 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005484 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005485 # check for %L
5486 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005487 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005488 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5489 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005490 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005491 # check for %Z
5492 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5493 WARN("PRINTF_Z",
5494 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5495 $show_Z = 0;
5496 }
5497 # check for 0x<decimal>
5498 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5499 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005500 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5501 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005502 }
5503
5504# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005505 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005506 WARN("LINE_CONTINUATIONS",
5507 "Avoid line continuations in quoted strings\n" . $herecurr);
5508 }
5509
Andy Whitcroft00df3442007-06-08 13:47:06 -07005510# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005511 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005512 WARN("IF_0",
5513 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5514 }
5515
5516# warn about #if 1
5517 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5518 WARN("IF_1",
5519 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005520 }
5521
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005522# check for needless "if (<foo>) fn(<foo>)" uses
5523 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005524 my $tested = quotemeta($1);
5525 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5526 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5527 my $func = $1;
5528 if (WARN('NEEDLESS_IF',
5529 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5530 $fix) {
5531 my $do_fix = 1;
5532 my $leading_tabs = "";
5533 my $new_leading_tabs = "";
5534 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5535 $leading_tabs = $1;
5536 } else {
5537 $do_fix = 0;
5538 }
5539 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5540 $new_leading_tabs = $1;
5541 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5542 $do_fix = 0;
5543 }
5544 } else {
5545 $do_fix = 0;
5546 }
5547 if ($do_fix) {
5548 fix_delete_line($fixlinenr - 1, $prevrawline);
5549 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5550 }
5551 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005552 }
5553 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005554
Joe Perchesebfdc402014-08-06 16:10:27 -07005555# check for unnecessary "Out of Memory" messages
5556 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5557 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5558 (defined $1 || defined $3) &&
5559 $linenr > 3) {
5560 my $testval = $2;
5561 my $testline = $lines[$linenr - 3];
5562
5563 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5564# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5565
Joe Perchese29a70f2019-03-07 16:28:35 -08005566 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5567 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005568 WARN("OOM_MESSAGE",
5569 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5570 }
5571 }
5572
Joe Perchesf78d98f2014-10-13 15:52:01 -07005573# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005574 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005575 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5576 my $level = $1;
5577 if (WARN("UNNECESSARY_KERN_LEVEL",
5578 "Possible unnecessary $level\n" . $herecurr) &&
5579 $fix) {
5580 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5581 }
5582 }
5583
Joe Perches45c55e92017-02-24 15:01:31 -08005584# check for logging continuations
5585 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5586 WARN("LOGGING_CONTINUATION",
5587 "Avoid logging continuation uses where feasible\n" . $herecurr);
5588 }
5589
Joe Perchesabb08a52014-12-10 15:51:46 -08005590# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005591 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005592 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5593 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5594 WARN("MASK_THEN_SHIFT",
5595 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5596 }
5597
Joe Perchesb75ac612014-12-10 15:52:02 -08005598# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005599 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005600 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5601 my $val = $1;
5602 my $equal = "!";
5603 $equal = "" if ($4 eq "!=");
5604 if (CHK("COMPARISON_TO_NULL",
5605 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5606 $fix) {
5607 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5608 }
5609 }
5610 }
5611
Joe Perches8716de32013-09-11 14:24:05 -07005612# check for bad placement of section $InitAttribute (e.g.: __initdata)
5613 if ($line =~ /(\b$InitAttribute\b)/) {
5614 my $attr = $1;
5615 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5616 my $ptr = $1;
5617 my $var = $2;
5618 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5619 ERROR("MISPLACED_INIT",
5620 "$attr should be placed after $var\n" . $herecurr)) ||
5621 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5622 WARN("MISPLACED_INIT",
5623 "$attr should be placed after $var\n" . $herecurr))) &&
5624 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005625 $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 -07005626 }
5627 }
5628 }
5629
Joe Perchese970b8842013-11-12 15:10:10 -08005630# check for $InitAttributeData (ie: __initdata) with const
5631 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5632 my $attr = $1;
5633 $attr =~ /($InitAttributePrefix)(.*)/;
5634 my $attr_prefix = $1;
5635 my $attr_type = $2;
5636 if (ERROR("INIT_ATTRIBUTE",
5637 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5638 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005639 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005640 s/$InitAttributeData/${attr_prefix}initconst/;
5641 }
5642 }
5643
5644# check for $InitAttributeConst (ie: __initconst) without const
5645 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5646 my $attr = $1;
5647 if (ERROR("INIT_ATTRIBUTE",
5648 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5649 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005650 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005651 /(^\+\s*(?:static\s+))/;
5652 $lead = rtrim($1);
5653 $lead = "$lead " if ($lead !~ /^\+$/);
5654 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005655 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005656 }
5657 }
5658
Joe Perchesc17893c2015-04-16 12:44:42 -07005659# check for __read_mostly with const non-pointer (should just be const)
5660 if ($line =~ /\b__read_mostly\b/ &&
5661 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5662 if (ERROR("CONST_READ_MOSTLY",
5663 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5664 $fix) {
5665 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5666 }
5667 }
5668
Joe Perchesfbdb8132014-04-03 14:49:14 -07005669# don't use __constant_<foo> functions outside of include/uapi/
5670 if ($realfile !~ m@^include/uapi/@ &&
5671 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5672 my $constant_func = $1;
5673 my $func = $constant_func;
5674 $func =~ s/^__constant_//;
5675 if (WARN("CONSTANT_CONVERSION",
5676 "$constant_func should be $func\n" . $herecurr) &&
5677 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005678 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005679 }
5680 }
5681
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005682# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005683 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005684 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005685 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005686 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005687 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005688 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5689 }
5690 if ($delay > 2000) {
5691 WARN("LONG_UDELAY",
5692 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005693 }
5694 }
5695
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005696# warn about unexpectedly long msleep's
5697 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5698 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005699 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005700 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005701 }
5702 }
5703
Joe Perches36ec1932013-07-03 15:05:25 -07005704# check for comparisons of jiffies
5705 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5706 WARN("JIFFIES_COMPARISON",
5707 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5708 }
5709
Joe Perches9d7a34a2013-07-03 15:05:26 -07005710# check for comparisons of get_jiffies_64()
5711 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5712 WARN("JIFFIES_COMPARISON",
5713 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5714 }
5715
Andy Whitcroft00df3442007-06-08 13:47:06 -07005716# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005717# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005718# print "#ifdef in C files should be avoided\n";
5719# print "$herecurr";
5720# $clean = 0;
5721# }
5722
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005723# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005724 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005725 if (ERROR("SPACING",
5726 "exactly one space required after that #$1\n" . $herecurr) &&
5727 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005728 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005729 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5730 }
5731
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005732 }
5733
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005734# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005735 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5736 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005737 my $which = $1;
5738 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005739 CHK("UNCOMMENTED_DEFINITION",
5740 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005741 }
5742 }
5743# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005744
5745 my $barriers = qr{
5746 mb|
5747 rmb|
5748 wmb|
5749 read_barrier_depends
5750 }x;
5751 my $barrier_stems = qr{
5752 mb__before_atomic|
5753 mb__after_atomic|
5754 store_release|
5755 load_acquire|
5756 store_mb|
5757 (?:$barriers)
5758 }x;
5759 my $all_barriers = qr{
5760 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005761 smp_(?:$barrier_stems)|
5762 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005763 }x;
5764
5765 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005766 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005767 WARN("MEMORY_BARRIER",
5768 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005769 }
5770 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005771
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005772 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5773
5774 if ($realfile !~ m@^include/asm-generic/@ &&
5775 $realfile !~ m@/barrier\.h$@ &&
5776 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5777 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5778 WARN("MEMORY_BARRIER",
5779 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5780 }
5781
Joe Perchescb426e92015-06-25 15:02:46 -07005782# check for waitqueue_active without a comment.
5783 if ($line =~ /\bwaitqueue_active\s*\(/) {
5784 if (!ctx_has_comment($first_line, $linenr)) {
5785 WARN("WAITQUEUE_ACTIVE",
5786 "waitqueue_active without comment\n" . $herecurr);
5787 }
5788 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005789
Paul E. McKenney91db2592017-11-27 09:37:35 -08005790# check for smp_read_barrier_depends and read_barrier_depends
5791 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5792 WARN("READ_BARRIER_DEPENDS",
5793 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5794 }
5795
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005796# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005797 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005798 CHK("ARCH_DEFINES",
5799 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005800 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005801
Joe Perches596ed452017-07-12 14:37:02 -07005802# check that the storage class is not after a type
5803 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005804 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005805 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5806 }
5807# Check that the storage class is at the beginning of a declaration
5808 if ($line =~ /\b$Storage\b/ &&
5809 $line !~ /^.\s*$Storage/ &&
5810 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5811 $1 !~ /[\,\)]\s*$/) {
5812 WARN("STORAGE_CLASS",
5813 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005814 }
5815
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005816# check the location of the inline attribute, that it is between
5817# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005818 if ($line =~ /\b$Type\s+$Inline\b/ ||
5819 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005820 ERROR("INLINE_LOCATION",
5821 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005822 }
5823
Andy Whitcroft8905a672007-11-28 16:21:06 -08005824# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005825 if ($realfile !~ m@\binclude/uapi/@ &&
5826 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005827 if (WARN("INLINE",
5828 "plain inline is preferred over $1\n" . $herecurr) &&
5829 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005830 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005831
5832 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005833 }
5834
Joe Perches3d130fd2011-01-12 17:00:00 -08005835# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005836 if ($realfile !~ m@\binclude/uapi/@ &&
5837 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005838 WARN("PREFER_PACKED",
5839 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005840 }
5841
Joe Perches39b7e282011-07-25 17:13:24 -07005842# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005843 if ($realfile !~ m@\binclude/uapi/@ &&
5844 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005845 WARN("PREFER_ALIGNED",
5846 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005847 }
5848
Joe Perches5f14d3b2012-01-10 15:09:52 -08005849# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005850 if ($realfile !~ m@\binclude/uapi/@ &&
5851 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005852 if (WARN("PREFER_PRINTF",
5853 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5854 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005855 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005856
5857 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005858 }
5859
Joe Perches6061d942012-03-23 15:02:16 -07005860# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005861 if ($realfile !~ m@\binclude/uapi/@ &&
5862 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005863 if (WARN("PREFER_SCANF",
5864 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5865 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005866 $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 -07005867 }
Joe Perches6061d942012-03-23 15:02:16 -07005868 }
5869
Joe Perches619a9082014-12-10 15:51:35 -08005870# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005871 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005872 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5873 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5874 $line =~ /\b__weak\b/)) {
5875 ERROR("WEAK_DECLARATION",
5876 "Using weak declarations can have unintended link defects\n" . $herecurr);
5877 }
5878
Tomas Winklerfd39f902016-12-12 16:46:34 -08005879# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005880 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005881 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005882 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5883 my $type = $1;
5884 if ($type =~ /\b($typeC99Typedefs)\b/) {
5885 $type = $1;
5886 my $kernel_type = 'u';
5887 $kernel_type = 's' if ($type =~ /^_*[si]/);
5888 $type =~ /(\d+)/;
5889 $kernel_type .= $1;
5890 if (CHK("PREFER_KERNEL_TYPES",
5891 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5892 $fix) {
5893 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5894 }
5895 }
5896 }
5897
Joe Perches938224b2016-01-20 14:59:15 -08005898# check for cast of C90 native int or longer types constants
5899 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5900 my $cast = $1;
5901 my $const = $2;
5902 if (WARN("TYPECAST_INT_CONSTANT",
5903 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5904 $fix) {
5905 my $suffix = "";
5906 my $newconst = $const;
5907 $newconst =~ s/${Int_type}$//;
5908 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5909 if ($cast =~ /\blong\s+long\b/) {
5910 $suffix .= 'LL';
5911 } elsif ($cast =~ /\blong\b/) {
5912 $suffix .= 'L';
5913 }
5914 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5915 }
5916 }
5917
Joe Perches8f53a9b2010-03-05 13:43:48 -08005918# check for sizeof(&)
5919 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005920 WARN("SIZEOF_ADDRESS",
5921 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005922 }
5923
Joe Perches66c80b62012-07-30 14:41:22 -07005924# check for sizeof without parenthesis
5925 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005926 if (WARN("SIZEOF_PARENTHESIS",
5927 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5928 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005929 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005930 }
Joe Perches66c80b62012-07-30 14:41:22 -07005931 }
5932
Joe Perches88982fe2012-12-17 16:02:00 -08005933# check for struct spinlock declarations
5934 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5935 WARN("USE_SPINLOCK_T",
5936 "struct spinlock should be spinlock_t\n" . $herecurr);
5937 }
5938
Joe Perchesa6962d72013-04-29 16:18:13 -07005939# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005940 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005941 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005942 $fmt =~ s/%%//g;
5943 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005944 if (WARN("PREFER_SEQ_PUTS",
5945 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5946 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005947 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005948 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005949 }
5950 }
5951
Joe Perches478b1792018-04-10 16:33:34 -07005952# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07005953 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07005954 defined $stat &&
5955 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5956 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005957 my $stat_real;
5958
Joe Perches0b523762017-05-08 15:55:36 -07005959 my $lc = $stat =~ tr@\n@@;
5960 $lc = $lc + $linenr;
5961 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07005962 my $specifier;
5963 my $extension;
5964 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07005965 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5966 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005967
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005968 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5969 $specifier = $1;
5970 $extension = $2;
5971 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5972 $bad_specifier = $specifier;
5973 last;
5974 }
5975 if ($extension eq "x" && !defined($stat_real)) {
5976 if (!defined($stat_real)) {
5977 $stat_real = get_stat_real($linenr, $lc);
5978 }
5979 WARN("VSPRINTF_SPECIFIER_PX",
5980 "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");
5981 }
5982 }
5983 if ($bad_specifier ne "") {
5984 my $stat_real = get_stat_real($linenr, $lc);
5985 my $ext_type = "Invalid";
5986 my $use = "";
5987 if ($bad_specifier =~ /p[Ff]/) {
5988 $ext_type = "Deprecated";
5989 $use = " - use %pS instead";
5990 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5991 }
5992
5993 WARN("VSPRINTF_POINTER_EXTENSION",
5994 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5995 }
Joe Perches0b523762017-05-08 15:55:36 -07005996 }
5997 }
5998
Andy Whitcroft554e1652012-01-10 15:09:57 -08005999# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006000 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006001 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006002 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006003
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006004 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006005 my $ms_val = $7;
6006 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006007
Andy Whitcroft554e1652012-01-10 15:09:57 -08006008 if ($ms_size =~ /^(0x|)0$/i) {
6009 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006010 "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 -08006011 } elsif ($ms_size =~ /^(0x|)1$/i) {
6012 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006013 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6014 }
6015 }
6016
Joe Perches98a9bba2014-01-23 15:54:52 -08006017# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006018# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006019# defined $stat &&
6020# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6021# if (WARN("PREFER_ETHER_ADDR_COPY",
6022# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6023# $fix) {
6024# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6025# }
6026# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006027
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006028# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006029# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006030# defined $stat &&
6031# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6032# WARN("PREFER_ETHER_ADDR_EQUAL",
6033# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6034# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006035
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006036# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6037# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006038# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006039# defined $stat &&
6040# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6041#
6042# my $ms_val = $7;
6043#
6044# if ($ms_val =~ /^(?:0x|)0+$/i) {
6045# if (WARN("PREFER_ETH_ZERO_ADDR",
6046# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6047# $fix) {
6048# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6049# }
6050# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6051# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6052# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6053# $fix) {
6054# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6055# }
6056# }
6057# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006058
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006059# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006060 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006061 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006062 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006063 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006064 my $call = $1;
6065 my $cast1 = deparenthesize($2);
6066 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006067 my $cast2 = deparenthesize($7);
6068 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006069 my $cast;
6070
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006071 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006072 $cast = "$cast1 or $cast2";
6073 } elsif ($cast1 ne "") {
6074 $cast = $cast1;
6075 } else {
6076 $cast = $cast2;
6077 }
6078 WARN("MINMAX",
6079 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006080 }
6081 }
6082
Joe Perches4a273192012-07-30 14:41:20 -07006083# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006084 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006085 defined $stat &&
6086 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6087 my $min = $1;
6088 my $max = $7;
6089 if ($min eq $max) {
6090 WARN("USLEEP_RANGE",
6091 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6092 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6093 $min > $max) {
6094 WARN("USLEEP_RANGE",
6095 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6096 }
6097 }
6098
Joe Perches823b7942013-11-12 15:10:15 -08006099# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006100 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006101 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006102 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006103 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6104 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6105 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6106 my $lc = $stat =~ tr@\n@@;
6107 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006108 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006109 WARN("NAKED_SSCANF",
6110 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6111 }
6112
Joe Perchesafc819a2014-06-04 16:12:08 -07006113# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006114 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006115 defined $stat &&
6116 $line =~ /\bsscanf\b/) {
6117 my $lc = $stat =~ tr@\n@@;
6118 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006119 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006120 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6121 my $format = $6;
6122 my $count = $format =~ tr@%@%@;
6123 if ($count == 1 &&
6124 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6125 WARN("SSCANF_TO_KSTRTO",
6126 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6127 }
6128 }
6129 }
6130
Joe Perches70dc8a42013-09-11 14:23:58 -07006131# check for new externs in .h files.
6132 if ($realfile =~ /\.h$/ &&
6133 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006134 if (CHK("AVOID_EXTERNS",
6135 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006136 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006137 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006138 }
6139 }
6140
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006141# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006142 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006143 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006144 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006145 my $function_name = $1;
6146 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006147
6148 my $s = $stat;
6149 if (defined $cond) {
6150 substr($s, 0, length($cond), '');
6151 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006152 if ($s =~ /^\s*;/ &&
6153 $function_name ne 'uninitialized_var')
6154 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006155 WARN("AVOID_EXTERNS",
6156 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006157 }
6158
6159 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006160 WARN("FUNCTION_ARGUMENTS",
6161 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006162 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006163
6164 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6165 $stat =~ /^.\s*extern\s+/)
6166 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006167 WARN("AVOID_EXTERNS",
6168 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006169 }
6170
Joe Perchesa0ad7592017-07-10 15:52:19 -07006171# check for function declarations that have arguments without identifier names
6172 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006173 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006174 $1 ne "void") {
6175 my $args = trim($1);
6176 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6177 my $arg = trim($1);
6178 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6179 WARN("FUNCTION_ARGUMENTS",
6180 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6181 }
6182 }
6183 }
6184
Joe Perchesa0ad7592017-07-10 15:52:19 -07006185# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006186 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006187 defined $stat &&
6188 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6189 $context_function = $1;
6190
6191# check for multiline function definition with misplaced open brace
6192 my $ok = 0;
6193 my $cnt = statement_rawlines($stat);
6194 my $herectx = $here . "\n";
6195 for (my $n = 0; $n < $cnt; $n++) {
6196 my $rl = raw_line($linenr, $n);
6197 $herectx .= $rl . "\n";
6198 $ok = 1 if ($rl =~ /^[ \+]\{/);
6199 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6200 last if $rl =~ /^[ \+].*\{/;
6201 }
6202 if (!$ok) {
6203 ERROR("OPEN_BRACE",
6204 "open brace '{' following function definitions go on the next line\n" . $herectx);
6205 }
6206 }
6207
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006208# checks for new __setup's
6209 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6210 my $name = $1;
6211
6212 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006213 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006214 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006215 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006216 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006217
Joe Perchese29a70f2019-03-07 16:28:35 -08006218# check for pointless casting of alloc functions
6219 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006220 WARN("UNNECESSARY_CASTS",
6221 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006222 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006223
Joe Perchesa640d252013-07-03 15:05:21 -07006224# alloc style
6225# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006226 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006227 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Joe Perchesa640d252013-07-03 15:05:21 -07006228 CHK("ALLOC_SIZEOF_STRUCT",
6229 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6230 }
6231
Joe Perches60a55362014-06-04 16:12:07 -07006232# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006233 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006234 defined $stat &&
6235 $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 -07006236 my $oldfunc = $3;
6237 my $a1 = $4;
6238 my $a2 = $10;
6239 my $newfunc = "kmalloc_array";
6240 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006241 my $r1 = $a1;
6242 my $r2 = $a2;
6243 if ($a1 =~ /^sizeof\s*\S/) {
6244 $r1 = $a2;
6245 $r2 = $a1;
6246 }
6247 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6248 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006249 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006250 my $herectx = get_stat_here($linenr, $cnt, $here);
6251
Joe Perches60a55362014-06-04 16:12:07 -07006252 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006253 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6254 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006255 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006256 $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 -07006257 }
6258 }
6259 }
6260
Joe Perches972fdea2013-04-29 16:18:12 -07006261# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006262 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006263 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6264 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006265 WARN("KREALLOC_ARG_REUSE",
6266 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6267 }
6268
Joe Perches5ce59ae2013-02-21 16:44:18 -08006269# check for alloc argument mismatch
6270 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6271 WARN("ALLOC_ARRAY_ARGS",
6272 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6273 }
6274
Joe Perchescaf2a542011-01-12 16:59:56 -08006275# check for multiple semicolons
6276 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006277 if (WARN("ONE_SEMICOLON",
6278 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6279 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006280 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006281 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006282 }
6283
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006284# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6285 if ($realfile !~ m@^include/uapi/@ &&
6286 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006287 my $ull = "";
6288 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6289 if (CHK("BIT_MACRO",
6290 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6291 $fix) {
6292 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6293 }
6294 }
6295
Joe Perches2d632742016-05-20 17:04:00 -07006296# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6297 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6298 my $config = $1;
6299 if (WARN("PREFER_IS_ENABLED",
6300 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6301 $fix) {
6302 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6303 }
6304 }
6305
Joe Perchese81f2392014-08-06 16:11:25 -07006306# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006307 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6308 my $has_break = 0;
6309 my $has_statement = 0;
6310 my $count = 0;
6311 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006312 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006313 $prevline--;
6314 my $rline = $rawlines[$prevline - 1];
6315 my $fline = $lines[$prevline - 1];
6316 last if ($fline =~ /^\@\@/);
6317 next if ($fline =~ /^\-/);
6318 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6319 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6320 next if ($fline =~ /^.[\s$;]*$/);
6321 $has_statement = 1;
6322 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006323 $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 -08006324 }
6325 if (!$has_break && $has_statement) {
6326 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006327 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006328 }
6329 }
6330
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006331# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006332 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006333 defined $stat &&
6334 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006335 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006336 my $herectx = get_stat_here($linenr, $cnt, $here);
6337
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006338 WARN("DEFAULT_NO_BREAK",
6339 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006340 }
6341
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006342# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006343 if ($line =~ /\b__FUNCTION__\b/) {
6344 if (WARN("USE_FUNC",
6345 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6346 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006347 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006348 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006349 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006350
Joe Perches62ec8182015-02-13 14:38:18 -08006351# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6352 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6353 ERROR("DATE_TIME",
6354 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6355 }
6356
Joe Perches2c924882012-03-23 15:02:20 -07006357# check for use of yield()
6358 if ($line =~ /\byield\s*\(\s*\)/) {
6359 WARN("YIELD",
6360 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6361 }
6362
Joe Perches179f8f42013-07-03 15:05:30 -07006363# check for comparisons against true and false
6364 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6365 my $lead = $1;
6366 my $arg = $2;
6367 my $test = $3;
6368 my $otype = $4;
6369 my $trail = $5;
6370 my $op = "!";
6371
6372 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6373
6374 my $type = lc($otype);
6375 if ($type =~ /^(?:true|false)$/) {
6376 if (("$test" eq "==" && "$type" eq "true") ||
6377 ("$test" eq "!=" && "$type" eq "false")) {
6378 $op = "";
6379 }
6380
6381 CHK("BOOL_COMPARISON",
6382 "Using comparison to $otype is error prone\n" . $herecurr);
6383
6384## maybe suggesting a correct construct would better
6385## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6386
6387 }
6388 }
6389
Joe Perches5d430902018-04-10 16:34:25 -07006390# check for bool bitfields
6391 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6392 WARN("BOOL_BITFIELD",
6393 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6394 }
6395
Joe Perchesd7295932018-08-21 21:57:26 -07006396# check for bool use in .h files
6397 if ($realfile =~ /\.h$/ &&
6398 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6399 CHK("BOOL_MEMBER",
6400 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6401 }
6402
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006403# check for semaphores initialized locked
6404 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006405 WARN("CONSIDER_COMPLETION",
6406 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006407 }
Joe Perches6712d852012-03-23 15:02:20 -07006408
Joe Perches67d0a072011-10-31 17:13:10 -07006409# recommend kstrto* over simple_strto* and strict_strto*
6410 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006411 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006412 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006413 }
Joe Perches6712d852012-03-23 15:02:20 -07006414
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006415# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006416 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006417 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006418 "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 -07006419 }
Joe Perches6712d852012-03-23 15:02:20 -07006420
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006421# check for spin_is_locked(), suggest lockdep instead
6422 if ($line =~ /\bspin_is_locked\(/) {
6423 WARN("USE_LOCKDEP",
6424 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6425 }
6426
Joe Perches9189c7e2018-09-07 15:26:18 -07006427# check for deprecated apis
6428 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6429 my $deprecated_api = $1;
6430 my $new_api = $deprecated_apis{$deprecated_api};
6431 WARN("DEPRECATED_API",
6432 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6433 }
6434
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006435# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006436# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006437 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006438 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006439 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006440 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006441 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006442
6443# use of NR_CPUS is usually wrong
6444# ignore definitions of NR_CPUS and usage to define arrays as likely right
6445 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006446 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6447 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006448 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6449 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6450 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006451 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006452 WARN("NR_CPUS",
6453 "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 -07006454 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006455
Joe Perches52ea8502013-11-12 15:10:09 -08006456# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6457 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6458 ERROR("DEFINE_ARCH_HAS",
6459 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6460 }
6461
Joe Perchesacd93622015-02-13 14:38:38 -08006462# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006463 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006464 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6465 WARN("LIKELY_MISUSE",
6466 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6467 }
6468
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006469# whine mightly about in_atomic
6470 if ($line =~ /\bin_atomic\s*\(/) {
6471 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006472 ERROR("IN_ATOMIC",
6473 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006474 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006475 WARN("IN_ATOMIC",
6476 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006477 }
6478 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006479
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006480# check for mutex_trylock_recursive usage
6481 if ($line =~ /mutex_trylock_recursive/) {
6482 ERROR("LOCKING",
6483 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6484 }
6485
Peter Zijlstra1704f472010-03-19 01:37:42 +01006486# check for lockdep_set_novalidate_class
6487 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6488 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6489 if ($realfile !~ m@^kernel/lockdep@ &&
6490 $realfile !~ m@^include/linux/lockdep@ &&
6491 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006492 ERROR("LOCKDEP",
6493 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006494 }
6495 }
Dave Jones88f88312011-01-12 16:59:59 -08006496
Joe Perchesb392c642015-04-16 12:44:16 -07006497 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6498 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006499 WARN("EXPORTED_WORLD_WRITABLE",
6500 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006501 }
Joe Perches24358802014-04-03 14:49:13 -07006502
Joe Perches00180462018-02-06 15:38:55 -08006503# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6504# and whether or not function naming is typical and if
6505# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006506 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006507 defined $stat &&
6508 $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*\)/) {
6509 my $var = $1;
6510 my $perms = $2;
6511 my $show = $3;
6512 my $store = $4;
6513 my $octal_perms = perms_to_octal($perms);
6514 if ($show =~ /^${var}_show$/ &&
6515 $store =~ /^${var}_store$/ &&
6516 $octal_perms eq "0644") {
6517 if (WARN("DEVICE_ATTR_RW",
6518 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6519 $fix) {
6520 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6521 }
6522 } elsif ($show =~ /^${var}_show$/ &&
6523 $store =~ /^NULL$/ &&
6524 $octal_perms eq "0444") {
6525 if (WARN("DEVICE_ATTR_RO",
6526 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6527 $fix) {
6528 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6529 }
6530 } elsif ($show =~ /^NULL$/ &&
6531 $store =~ /^${var}_store$/ &&
6532 $octal_perms eq "0200") {
6533 if (WARN("DEVICE_ATTR_WO",
6534 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6535 $fix) {
6536 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6537 }
6538 } elsif ($octal_perms eq "0644" ||
6539 $octal_perms eq "0444" ||
6540 $octal_perms eq "0200") {
6541 my $newshow = "$show";
6542 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6543 my $newstore = $store;
6544 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6545 my $rename = "";
6546 if ($show ne $newshow) {
6547 $rename .= " '$show' to '$newshow'";
6548 }
6549 if ($store ne $newstore) {
6550 $rename .= " '$store' to '$newstore'";
6551 }
6552 WARN("DEVICE_ATTR_FUNCTIONS",
6553 "Consider renaming function(s)$rename\n" . $herecurr);
6554 } else {
6555 WARN("DEVICE_ATTR_PERMS",
6556 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6557 }
6558 }
6559
Joe Perches515a2352014-04-03 14:49:24 -07006560# Mode permission misuses where it seems decimal should be octal
6561# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006562# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6563# specific definition of not visible in sysfs.
6564# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6565# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006566 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006567 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006568 $line =~ /$mode_perms_search/) {
6569 foreach my $entry (@mode_permission_funcs) {
6570 my $func = $entry->[0];
6571 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006572
Joe Perches459cf0a2016-10-11 13:52:19 -07006573 my $lc = $stat =~ tr@\n@@;
6574 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006575 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006576
Joe Perches515a2352014-04-03 14:49:24 -07006577 my $skip_args = "";
6578 if ($arg_pos > 1) {
6579 $arg_pos--;
6580 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6581 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006582 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006583 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006584 my $val = $1;
6585 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006586 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6587 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6588 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006589 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006590 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006591 }
6592 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006593 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006594 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006595 }
Joe Perches24358802014-04-03 14:49:13 -07006596 }
6597 }
6598 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006599
Joe Perches459cf0a2016-10-11 13:52:19 -07006600# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006601 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006602 my $oval = $1;
6603 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006604 if (WARN("SYMBOLIC_PERMS",
6605 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6606 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006607 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006608 }
6609 }
6610
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006611# validate content of MODULE_LICENSE against list from include/linux/module.h
6612 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6613 my $extracted_string = get_quoted_string($line, $rawline);
6614 my $valid_licenses = qr{
6615 GPL|
6616 GPL\ v2|
6617 GPL\ and\ additional\ rights|
6618 Dual\ BSD/GPL|
6619 Dual\ MIT/GPL|
6620 Dual\ MPL/GPL|
6621 Proprietary
6622 }x;
6623 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6624 WARN("MODULE_LICENSE",
6625 "unknown module license " . $extracted_string . "\n" . $herecurr);
6626 }
6627 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006628 }
6629
6630 # If we have no input at all, then there is nothing to report on
6631 # so just keep quiet.
6632 if ($#rawlines == -1) {
6633 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006634 }
6635
Andy Whitcroft8905a672007-11-28 16:21:06 -08006636 # In mailback mode only produce a report in the negative, for
6637 # things that appear to be patches.
6638 if ($mailback && ($clean == 1 || !$is_patch)) {
6639 exit(0);
6640 }
6641
6642 # This is not a patch, and we are are in 'no-patch' mode so
6643 # just keep quiet.
6644 if (!$chk_patch && !$is_patch) {
6645 exit(0);
6646 }
6647
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006648 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006649 ERROR("NOT_UNIFIED_DIFF",
6650 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006651 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006652 if ($is_patch && $has_commit_log && $chk_signoff) {
6653 if ($signoff == 0) {
6654 ERROR("MISSING_SIGN_OFF",
6655 "Missing Signed-off-by: line(s)\n");
6656 } elsif (!$authorsignoff) {
6657 WARN("NO_AUTHOR_SIGN_OFF",
6658 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6659 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006660 }
6661
Andy Whitcroft8905a672007-11-28 16:21:06 -08006662 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006663 if ($summary && !($clean == 1 && $quiet == 1)) {
6664 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006665 print "total: $cnt_error errors, $cnt_warn warnings, " .
6666 (($check)? "$cnt_chk checks, " : "") .
6667 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006668 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006669
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006670 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006671 # If there were any defects found and not already fixing them
6672 if (!$clean and !$fix) {
6673 print << "EOM"
6674
6675NOTE: For some of the reported defects, checkpatch may be able to
6676 mechanically convert to the typical style using --fix or --fix-inplace.
6677EOM
6678 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006679 # If there were whitespace errors which cleanpatch can fix
6680 # then suggest that.
6681 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006682 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006683 print << "EOM"
6684
6685NOTE: Whitespace errors detected.
6686 You may wish to use scripts/cleanpatch or scripts/cleanfile
6687EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006688 }
6689 }
6690
Joe Perchesd752fcc2014-08-06 16:11:05 -07006691 if ($clean == 0 && $fix &&
6692 ("@rawlines" ne "@fixed" ||
6693 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006694 my $newfile = $filename;
6695 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006696 my $linecount = 0;
6697 my $f;
6698
Joe Perchesd752fcc2014-08-06 16:11:05 -07006699 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6700
Joe Perches3705ce52013-07-03 15:05:31 -07006701 open($f, '>', $newfile)
6702 or die "$P: Can't open $newfile for write\n";
6703 foreach my $fixed_line (@fixed) {
6704 $linecount++;
6705 if ($file) {
6706 if ($linecount > 3) {
6707 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006708 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006709 }
6710 } else {
6711 print $f $fixed_line . "\n";
6712 }
6713 }
6714 close($f);
6715
6716 if (!$quiet) {
6717 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006718
Joe Perches3705ce52013-07-03 15:05:31 -07006719Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6720
6721Do _NOT_ trust the results written to this file.
6722Do _NOT_ submit these changes without inspecting them for correctness.
6723
6724This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6725No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006726EOM
6727 }
6728 }
6729
Joe Perchesd8469f12015-06-25 15:03:00 -07006730 if ($quiet == 0) {
6731 print "\n";
6732 if ($clean == 1) {
6733 print "$vname has no obvious style problems and is ready for submission.\n";
6734 } else {
6735 print "$vname has style problems, please review.\n";
6736 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006737 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006738 return $clean;
6739}