blob: b5eade941f4a2fc6e5770226ac0d30f6e685332d [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046my %debug;
Joe Perches34456862013-07-03 15:05:34 -070047my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070048my %use_type = ();
49my @use = ();
50my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070051my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070052my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070053my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080054my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070057my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070058my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070059my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070060my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070061my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070062my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070063my $color = "auto";
Joe Perchesdadf6802016-08-02 14:04:33 -070064my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070065
66sub help {
67 my ($exitcode) = @_;
68
69 print << "EOM";
70Usage: $P [OPTION]... [FILE]...
71Version: $V
72
73Options:
74 -q, --quiet quiet
75 --no-tree run without a kernel tree
76 --no-signoff do not check for 'Signed-off-by' line
77 --patch treat FILE as patchfile (default)
78 --emacs emacs compile window format
79 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070080 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070081 -g, --git treat FILE as a single commit or git revision range
82 single git commit with:
83 <rev>
84 <rev>^
85 <rev>~n
86 multiple git commits with:
87 <rev1>..<rev2>
88 <rev1>...<rev2>
89 <rev>-<count>
90 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070091 -f, --file treat FILE as regular source file
92 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070093 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070094 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070095 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070096 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080097 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070098 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -070099 --root=PATH PATH to the kernel tree root
100 --no-summary suppress the per-file summary
101 --mailback only produce a report in case of warnings/errors
102 --summary-file include the filename in summary
103 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
104 'values', 'possible', 'type', and 'attr' (default
105 is all off)
106 --test-only=WORD report only warnings/errors containing WORD
107 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700108 --fix EXPERIMENTAL - may create horrible results
109 If correctable single-line errors exist, create
110 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
111 with potential errors corrected to the preferred
112 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800113 --fix-inplace EXPERIMENTAL - may create horrible results
114 Is the same as --fix, but overwrites the input
115 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700116 --ignore-perl-version override checking of perl version. expect
117 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700118 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700119 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700120 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700121 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700122 --color[=WHEN] Use colors 'always', 'never', or only when output
123 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700124 -h, --help, --version display this help and exit
125
126When FILE is - read standard input.
127EOM
128
129 exit($exitcode);
130}
131
Joe Perches3beb42e2016-05-20 17:04:14 -0700132sub uniq {
133 my %seen;
134 return grep { !$seen{$_}++ } @_;
135}
136
137sub list_types {
138 my ($exitcode) = @_;
139
140 my $count = 0;
141
142 local $/ = undef;
143
144 open(my $script, '<', abs_path($P)) or
145 die "$P: Can't read '$P' $!\n";
146
147 my $text = <$script>;
148 close($script);
149
150 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700151 # Also catch when type or level is passed through a variable
152 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700153 push (@types, $_);
154 }
155 @types = sort(uniq(@types));
156 print("#\tMessage type\n\n");
157 foreach my $type (@types) {
158 print(++$count . "\t" . $type . "\n");
159 }
160
161 exit($exitcode);
162}
163
Joe Perches000d1cc12011-07-25 17:13:25 -0700164my $conf = which_conf($configuration_file);
165if (-f $conf) {
166 my @conf_args;
167 open(my $conffile, '<', "$conf")
168 or warn "$P: Can't find a readable $configuration_file file $!\n";
169
170 while (<$conffile>) {
171 my $line = $_;
172
173 $line =~ s/\s*\n?$//g;
174 $line =~ s/^\s*//g;
175 $line =~ s/\s+/ /g;
176
177 next if ($line =~ m/^\s*#/);
178 next if ($line =~ m/^\s*$/);
179
180 my @words = split(" ", $line);
181 foreach my $word (@words) {
182 last if ($word =~ m/^#/);
183 push (@conf_args, $word);
184 }
185 }
186 close($conffile);
187 unshift(@ARGV, @conf_args) if @conf_args;
188}
189
John Brooks737c0762017-07-10 15:52:24 -0700190# Perl's Getopt::Long allows options to take optional arguments after a space.
191# Prevent --color by itself from consuming other arguments
192foreach (@ARGV) {
193 if ($_ eq "--color" || $_ eq "-color") {
194 $_ = "--color=$color";
195 }
196}
197
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700198GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700199 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700200 'tree!' => \$tree,
201 'signoff!' => \$chk_signoff,
202 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700203 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800204 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700205 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700206 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700207 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208 'subjective!' => \$check,
209 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700210 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700211 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700212 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700213 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800214 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700215 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700216 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800217 'summary!' => \$summary,
218 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800219 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700220 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800221 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700222 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800223 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700224 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700225 'codespell!' => \$codespell,
226 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700227 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700228 'color=s' => \$color,
229 'no-color' => \$color, #keep old behaviors of -nocolor
230 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700231 'h|help' => \$help,
232 'version' => \$help
233) or help(1);
234
235help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700236
Joe Perches3beb42e2016-05-20 17:04:14 -0700237list_types(0) if ($list_types);
238
Joe Perches9624b8d2014-01-23 15:54:44 -0800239$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700240$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800241
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700242my $exit = 0;
243
Joe Perches5b579802018-08-21 21:57:33 -0700244my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700245if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700246 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700247 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700248 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700249}
250
Allen Hubbe45107ff2016-08-02 14:04:47 -0700251#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700252if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700253 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700254}
255
John Brooks737c0762017-07-10 15:52:24 -0700256if ($color =~ /^[01]$/) {
257 $color = !$color;
258} elsif ($color =~ /^always$/i) {
259 $color = 1;
260} elsif ($color =~ /^never$/i) {
261 $color = 0;
262} elsif ($color =~ /^auto$/i) {
263 $color = (-t STDOUT);
264} else {
265 die "Invalid color mode: $color\n";
266}
267
Joe Perches91bfe482013-09-11 14:23:59 -0700268sub hash_save_array_words {
269 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700270
Joe Perches91bfe482013-09-11 14:23:59 -0700271 my @array = split(/,/, join(',', @$arrayRef));
272 foreach my $word (@array) {
273 $word =~ s/\s*\n?$//g;
274 $word =~ s/^\s*//g;
275 $word =~ s/\s+/ /g;
276 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700277
Joe Perches91bfe482013-09-11 14:23:59 -0700278 next if ($word =~ m/^\s*#/);
279 next if ($word =~ m/^\s*$/);
280
281 $hashRef->{$word}++;
282 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700283}
284
Joe Perches91bfe482013-09-11 14:23:59 -0700285sub hash_show_words {
286 my ($hashRef, $prefix) = @_;
287
Joe Perches3c816e42015-06-25 15:03:29 -0700288 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700289 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700290 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700291 print " $word";
292 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700293 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700294 }
295}
296
297hash_save_array_words(\%ignore_type, \@ignore);
298hash_save_array_words(\%use_type, \@use);
299
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800300my $dbg_values = 0;
301my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700302my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700303my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800304for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800305 ## no critic
306 eval "\${dbg_$key} = '$debug{$key}';";
307 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800308}
309
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700310my $rpt_cleaners = 0;
311
Andy Whitcroft8905a672007-11-28 16:21:06 -0800312if ($terse) {
313 $emacs = 1;
314 $quiet++;
315}
316
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700317if ($tree) {
318 if (defined $root) {
319 if (!top_of_kernel_tree($root)) {
320 die "$P: $root: --root does not point at a valid tree\n";
321 }
322 } else {
323 if (top_of_kernel_tree('.')) {
324 $root = '.';
325 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
326 top_of_kernel_tree($1)) {
327 $root = $1;
328 }
329 }
330
331 if (!defined $root) {
332 print "Must be run from the top-level dir. of a kernel tree\n";
333 exit(2);
334 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700335}
336
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700337my $emitted_corrupt = 0;
338
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700339our $Ident = qr{
340 [A-Za-z_][A-Za-z\d_]*
341 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
342 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700343our $Storage = qr{extern|static|asmlinkage};
344our $Sparse = qr{
345 __user|
346 __kernel|
347 __force|
348 __iomem|
349 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800350 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700351 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700352 __refconst|
353 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800354 __rcu|
355 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700356 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800357our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
358our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
359our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
360our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
361our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700362
Wolfram Sang52131292010-03-05 13:43:51 -0800363# Notes to $Attribute:
364# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700365our $Attribute = qr{
366 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700367 __percpu|
368 __nocast|
369 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200370 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700371 __packed__|
372 __packed2__|
373 __naked|
374 __maybe_unused|
375 __always_unused|
376 __noreturn|
377 __used|
378 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800379 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700380 __noclone|
381 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700382 __read_mostly|
383 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700384 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700385 ____cacheline_aligned|
386 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800387 ____cacheline_internodealigned_in_smp|
388 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700390our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700391our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
393our $Lval = qr{$Ident(?:$Member)*};
394
Joe Perches95e2c602013-07-03 15:05:20 -0700395our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
396our $Binary = qr{(?i)0b[01]+$Int_type?};
397our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
398our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700399our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800400our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800401our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
402our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
403our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800404our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700405our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800406our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700407our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700408our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700409our $Operators = qr{
410 <=|>=|==|!=|
411 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700412 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700413 }x;
414
Joe Perches91cb5192014-04-03 14:49:32 -0700415our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
416
Joe Perchesab7e23f2015-04-16 12:44:22 -0700417our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800418our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700419our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700420our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800421our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700422our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800423our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700424our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800425
Joe Perches15662b32011-10-31 17:13:12 -0700426our $NON_ASCII_UTF8 = qr{
427 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700428 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
429 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
430 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
431 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
432 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
433 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
434}x;
435
Joe Perches15662b32011-10-31 17:13:12 -0700436our $UTF8 = qr{
437 [\x09\x0A\x0D\x20-\x7E] # ASCII
438 | $NON_ASCII_UTF8
439}x;
440
Joe Perchese6176fa2015-06-25 15:02:49 -0700441our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800442our $typeOtherOSTypedefs = qr{(?x:
443 u_(?:char|short|int|long) | # bsd
444 u(?:nchar|short|int|long) # sysv
445)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700446our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700447 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700448 atomic_t
449)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700450our $typeTypedefs = qr{(?x:
451 $typeC99Typedefs\b|
452 $typeOtherOSTypedefs\b|
453 $typeKernelTypedefs\b
454)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700455
Joe Perches6d32f7a2015-11-06 16:31:37 -0800456our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
457
Joe Perches691e6692010-03-05 13:43:51 -0800458our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800459 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700460 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches87bd4992017-11-17 15:28:48 -0800461 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700462 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700463 panic|
Joe Perches06668722013-11-12 15:10:07 -0800464 MODULE_[A-Z_]+|
465 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800466)};
467
Joe Perches20112472011-07-25 17:13:23 -0700468our $signature_tags = qr{(?xi:
469 Signed-off-by:|
470 Acked-by:|
471 Tested-by:|
472 Reviewed-by:|
473 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700474 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700475 To:|
476 Cc:
477)};
478
Joe Perches18130872014-08-06 16:11:22 -0700479our @typeListMisordered = (
480 qr{char\s+(?:un)?signed},
481 qr{int\s+(?:(?:un)?signed\s+)?short\s},
482 qr{int\s+short(?:\s+(?:un)?signed)},
483 qr{short\s+int(?:\s+(?:un)?signed)},
484 qr{(?:un)?signed\s+int\s+short},
485 qr{short\s+(?:un)?signed},
486 qr{long\s+int\s+(?:un)?signed},
487 qr{int\s+long\s+(?:un)?signed},
488 qr{long\s+(?:un)?signed\s+int},
489 qr{int\s+(?:un)?signed\s+long},
490 qr{int\s+(?:un)?signed},
491 qr{int\s+long\s+long\s+(?:un)?signed},
492 qr{long\s+long\s+int\s+(?:un)?signed},
493 qr{long\s+long\s+(?:un)?signed\s+int},
494 qr{long\s+long\s+(?:un)?signed},
495 qr{long\s+(?:un)?signed},
496);
497
Andy Whitcroft8905a672007-11-28 16:21:06 -0800498our @typeList = (
499 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700500 qr{(?:(?:un)?signed\s+)?char},
501 qr{(?:(?:un)?signed\s+)?short\s+int},
502 qr{(?:(?:un)?signed\s+)?short},
503 qr{(?:(?:un)?signed\s+)?int},
504 qr{(?:(?:un)?signed\s+)?long\s+int},
505 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
506 qr{(?:(?:un)?signed\s+)?long\s+long},
507 qr{(?:(?:un)?signed\s+)?long},
508 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509 qr{float},
510 qr{double},
511 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800512 qr{struct\s+$Ident},
513 qr{union\s+$Ident},
514 qr{enum\s+$Ident},
515 qr{${Ident}_t},
516 qr{${Ident}_handler},
517 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700518 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800519);
Joe Perches938224b2016-01-20 14:59:15 -0800520
521our $C90_int_types = qr{(?x:
522 long\s+long\s+int\s+(?:un)?signed|
523 long\s+long\s+(?:un)?signed\s+int|
524 long\s+long\s+(?:un)?signed|
525 (?:(?:un)?signed\s+)?long\s+long\s+int|
526 (?:(?:un)?signed\s+)?long\s+long|
527 int\s+long\s+long\s+(?:un)?signed|
528 int\s+(?:(?:un)?signed\s+)?long\s+long|
529
530 long\s+int\s+(?:un)?signed|
531 long\s+(?:un)?signed\s+int|
532 long\s+(?:un)?signed|
533 (?:(?:un)?signed\s+)?long\s+int|
534 (?:(?:un)?signed\s+)?long|
535 int\s+long\s+(?:un)?signed|
536 int\s+(?:(?:un)?signed\s+)?long|
537
538 int\s+(?:un)?signed|
539 (?:(?:un)?signed\s+)?int
540)};
541
Alex Dowad485ff232015-06-25 15:02:52 -0700542our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700543our @typeListWithAttr = (
544 @typeList,
545 qr{struct\s+$InitAttribute\s+$Ident},
546 qr{union\s+$InitAttribute\s+$Ident},
547);
548
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700549our @modifierList = (
550 qr{fastcall},
551);
Alex Dowad485ff232015-06-25 15:02:52 -0700552our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800553
Joe Perches24358802014-04-03 14:49:13 -0700554our @mode_permission_funcs = (
555 ["module_param", 3],
556 ["module_param_(?:array|named|string)", 4],
557 ["module_param_array_named", 5],
558 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
559 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700560 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
561 ["IIO_DEV_ATTR_[A-Z_]+", 1],
562 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
563 ["SENSOR_TEMPLATE(?:_2|)", 3],
564 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700565);
566
Joe Perches515a2352014-04-03 14:49:24 -0700567#Create a search pattern for all these functions to speed up a loop below
568our $mode_perms_search = "";
569foreach my $entry (@mode_permission_funcs) {
570 $mode_perms_search .= '|' if ($mode_perms_search ne "");
571 $mode_perms_search .= $entry->[0];
572}
Joe Perches00180462018-02-06 15:38:55 -0800573$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700574
Joe Perchesb392c642015-04-16 12:44:16 -0700575our $mode_perms_world_writable = qr{
576 S_IWUGO |
577 S_IWOTH |
578 S_IRWXUGO |
579 S_IALLUGO |
580 0[0-7][0-7][2367]
581}x;
582
Joe Perchesf90774e2016-10-11 13:51:47 -0700583our %mode_permission_string_types = (
584 "S_IRWXU" => 0700,
585 "S_IRUSR" => 0400,
586 "S_IWUSR" => 0200,
587 "S_IXUSR" => 0100,
588 "S_IRWXG" => 0070,
589 "S_IRGRP" => 0040,
590 "S_IWGRP" => 0020,
591 "S_IXGRP" => 0010,
592 "S_IRWXO" => 0007,
593 "S_IROTH" => 0004,
594 "S_IWOTH" => 0002,
595 "S_IXOTH" => 0001,
596 "S_IRWXUGO" => 0777,
597 "S_IRUGO" => 0444,
598 "S_IWUGO" => 0222,
599 "S_IXUGO" => 0111,
600);
601
602#Create a search pattern for all these strings to speed up a loop below
603our $mode_perms_string_search = "";
604foreach my $entry (keys %mode_permission_string_types) {
605 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
606 $mode_perms_string_search .= $entry;
607}
Joe Perches00180462018-02-06 15:38:55 -0800608our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
609our $multi_mode_perms_string_search = qr{
610 ${single_mode_perms_string_search}
611 (?:\s*\|\s*${single_mode_perms_string_search})*
612}x;
613
614sub perms_to_octal {
615 my ($string) = @_;
616
617 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
618
619 my $val = "";
620 my $oval = "";
621 my $to = 0;
622 my $curpos = 0;
623 my $lastpos = 0;
624 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
625 $curpos = pos($string);
626 my $match = $2;
627 my $omatch = $1;
628 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
629 $lastpos = $curpos;
630 $to |= $mode_permission_string_types{$match};
631 $val .= '\s*\|\s*' if ($val ne "");
632 $val .= $match;
633 $oval .= $omatch;
634 }
635 $oval =~ s/^\s*\|\s*//;
636 $oval =~ s/\s*\|\s*$//;
637 return sprintf("%04o", $to);
638}
Joe Perchesf90774e2016-10-11 13:51:47 -0700639
Wolfram Sang7840a942010-08-09 17:20:57 -0700640our $allowed_asm_includes = qr{(?x:
641 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700642 memory|
643 time|
644 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700645)};
646# memory.h: ARM has a custom one
647
Kees Cook66b47b42014-10-13 15:51:57 -0700648# Load common spelling mistakes and build regular expression list.
649my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700650my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700651
Joe Perches36061e32014-12-10 15:51:43 -0800652if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800653 while (<$spelling>) {
654 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700655
Joe Perches36061e32014-12-10 15:51:43 -0800656 $line =~ s/\s*\n?$//g;
657 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700658
Joe Perches36061e32014-12-10 15:51:43 -0800659 next if ($line =~ m/^\s*#/);
660 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700661
Joe Perches36061e32014-12-10 15:51:43 -0800662 my ($suspect, $fix) = split(/\|\|/, $line);
663
Joe Perches36061e32014-12-10 15:51:43 -0800664 $spelling_fix{$suspect} = $fix;
665 }
666 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800667} else {
668 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700669}
Kees Cook66b47b42014-10-13 15:51:57 -0700670
Joe Perchesebfd7d62015-04-16 12:44:14 -0700671if ($codespell) {
672 if (open(my $spelling, '<', $codespellfile)) {
673 while (<$spelling>) {
674 my $line = $_;
675
676 $line =~ s/\s*\n?$//g;
677 $line =~ s/^\s*//g;
678
679 next if ($line =~ m/^\s*#/);
680 next if ($line =~ m/^\s*$/);
681 next if ($line =~ m/, disabled/i);
682
683 $line =~ s/,.*$//;
684
685 my ($suspect, $fix) = split(/->/, $line);
686
687 $spelling_fix{$suspect} = $fix;
688 }
689 close($spelling);
690 } else {
691 warn "No codespell typos will be found - file '$codespellfile': $!\n";
692 }
693}
694
695$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
696
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700697sub read_words {
698 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700699
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700700 if (open(my $words, '<', $file)) {
701 while (<$words>) {
702 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700703
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700704 $line =~ s/\s*\n?$//g;
705 $line =~ s/^\s*//g;
706
707 next if ($line =~ m/^\s*#/);
708 next if ($line =~ m/^\s*$/);
709 if ($line =~ /\s/) {
710 print("$file: '$line' invalid - ignored\n");
711 next;
712 }
713
714 $$wordsRef .= '|' if ($$wordsRef ne "");
715 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700716 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700717 close($file);
718 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700719 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700720
721 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700722}
723
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700724my $const_structs = "";
725read_words(\$const_structs, $conststructsfile)
726 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
727
728my $typeOtherTypedefs = "";
729if (length($typedefsfile)) {
730 read_words(\$typeOtherTypedefs, $typedefsfile)
731 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
732}
733$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
734
Andy Whitcroft8905a672007-11-28 16:21:06 -0800735sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700736 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
737 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700738 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700739 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700740 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700741 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700742 (?:$typeTypedefs\b)|
743 (?:${all}\b)
744 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800745 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700746 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800747 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800748 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700749 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700750 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800751 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700752 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800753 }x;
Joe Perches18130872014-08-06 16:11:22 -0700754 $NonptrTypeMisordered = qr{
755 (?:$Modifier\s+|const\s+)*
756 (?:
757 (?:${Misordered}\b)
758 )
759 (?:\s+$Modifier|\s+const)*
760 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700761 $NonptrTypeWithAttr = qr{
762 (?:$Modifier\s+|const\s+)*
763 (?:
764 (?:typeof|__typeof__)\s*\([^\)]*\)|
765 (?:$typeTypedefs\b)|
766 (?:${allWithAttr}\b)
767 )
768 (?:\s+$Modifier|\s+const)*
769 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800770 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700771 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700772 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700773 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800774 }x;
Joe Perches18130872014-08-06 16:11:22 -0700775 $TypeMisordered = qr{
776 $NonptrTypeMisordered
777 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
778 (?:\s+$Inline|\s+$Modifier)*
779 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700780 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700781 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800782}
783build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700784
Joe Perches7d2367a2011-07-25 17:13:22 -0700785our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700786
787# Using $balanced_parens, $LvalOrFunc, or $FuncArg
788# requires at least perl version v5.10.0
789# Any use must be runtime checked with $^V
790
791our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700792our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800793our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700794
Joe Perchesf8422302014-08-06 16:11:31 -0700795our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700796 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700797 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700798 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
799 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700800)};
801
Joe Perches7d2367a2011-07-25 17:13:22 -0700802sub deparenthesize {
803 my ($string) = @_;
804 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700805
806 while ($string =~ /^\s*\(.*\)\s*$/) {
807 $string =~ s@^\s*\(\s*@@;
808 $string =~ s@\s*\)\s*$@@;
809 }
810
Joe Perches7d2367a2011-07-25 17:13:22 -0700811 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700812
Joe Perches7d2367a2011-07-25 17:13:22 -0700813 return $string;
814}
815
Joe Perches34456862013-07-03 15:05:34 -0700816sub seed_camelcase_file {
817 my ($file) = @_;
818
819 return if (!(-f $file));
820
821 local $/;
822
823 open(my $include_file, '<', "$file")
824 or warn "$P: Can't read '$file' $!\n";
825 my $text = <$include_file>;
826 close($include_file);
827
828 my @lines = split('\n', $text);
829
830 foreach my $line (@lines) {
831 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
832 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
833 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800834 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
835 $camelcase{$1} = 1;
836 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700837 $camelcase{$1} = 1;
838 }
839 }
840}
841
Joe Perches85b0ee12016-10-11 13:51:44 -0700842sub is_maintained_obsolete {
843 my ($filename) = @_;
844
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800845 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700846
Joe Perches0616efa2016-10-11 13:52:02 -0700847 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
Joe Perches85b0ee12016-10-11 13:51:44 -0700848
849 return $status =~ /obsolete/i;
850}
851
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700852sub is_SPDX_License_valid {
853 my ($license) = @_;
854
855 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py"));
856
857 my $status = `echo "$license" | python $root/scripts/spdxcheck.py -`;
858 return 0 if ($status ne "");
859 return 1;
860}
861
Joe Perches34456862013-07-03 15:05:34 -0700862my $camelcase_seeded = 0;
863sub seed_camelcase_includes {
864 return if ($camelcase_seeded);
865
866 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700867 my $camelcase_cache = "";
868 my @include_files = ();
869
870 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700871
Richard Genoud3645e322014-02-10 14:25:32 -0800872 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700873 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
874 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700875 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700876 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700877 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700878 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700879 @include_files = split('\n', $files);
880 foreach my $file (@include_files) {
881 my $date = POSIX::strftime("%Y%m%d%H%M",
882 localtime((stat $file)[9]));
883 $last_mod_date = $date if ($last_mod_date < $date);
884 }
885 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700886 }
Joe Perchesc707a812013-07-08 16:00:43 -0700887
888 if ($camelcase_cache ne "" && -f $camelcase_cache) {
889 open(my $camelcase_file, '<', "$camelcase_cache")
890 or warn "$P: Can't read '$camelcase_cache' $!\n";
891 while (<$camelcase_file>) {
892 chomp;
893 $camelcase{$_} = 1;
894 }
895 close($camelcase_file);
896
897 return;
898 }
899
Richard Genoud3645e322014-02-10 14:25:32 -0800900 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700901 $files = `git ls-files "include/*.h"`;
902 @include_files = split('\n', $files);
903 }
904
Joe Perches34456862013-07-03 15:05:34 -0700905 foreach my $file (@include_files) {
906 seed_camelcase_file($file);
907 }
Joe Perches351b2a12013-07-03 15:05:36 -0700908
Joe Perchesc707a812013-07-08 16:00:43 -0700909 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700910 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700911 open(my $camelcase_file, '>', "$camelcase_cache")
912 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700913 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
914 print $camelcase_file ("$_\n");
915 }
916 close($camelcase_file);
917 }
Joe Perches34456862013-07-03 15:05:34 -0700918}
919
Joe Perchesd311cd42014-08-06 16:10:57 -0700920sub git_commit_info {
921 my ($commit, $id, $desc) = @_;
922
923 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
924
925 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
926 $output =~ s/^\s*//gm;
927 my @lines = split("\n", $output);
928
Joe Perches0d7835f2015-02-13 14:38:35 -0800929 return ($id, $desc) if ($#lines < 0);
930
Joe Perchesd311cd42014-08-06 16:10:57 -0700931 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
932# Maybe one day convert this block of bash into something that returns
933# all matching commit ids, but it's very slow...
934#
935# echo "checking commits $1..."
936# git rev-list --remotes | grep -i "^$1" |
937# while read line ; do
938# git log --format='%H %s' -1 $line |
939# echo "commit $(cut -c 1-12,41-)"
940# done
941 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700942 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700943 } else {
944 $id = substr($lines[0], 0, 12);
945 $desc = substr($lines[0], 41);
946 }
947
948 return ($id, $desc);
949}
950
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700951$chk_signoff = 0 if ($file);
952
Andy Whitcroft00df3442007-06-08 13:47:06 -0700953my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800954my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700955my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700956my @fixed_inserted = ();
957my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700958my $fixlinenr = -1;
959
Du, Changbin4a593c32016-05-20 17:04:16 -0700960# If input is git commits, extract all commits from the commit expressions.
961# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
962die "$P: No git repository found\n" if ($git && !-e ".git");
963
964if ($git) {
965 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700966 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700967 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700968 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
969 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700970 } elsif ($commit_expr =~ m/\.\./) {
971 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700972 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700973 $git_range = "-1 $commit_expr";
974 }
975 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
976 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700977 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
978 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700979 my $sha1 = $1;
980 my $subject = $2;
981 unshift(@commits, $sha1);
982 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700983 }
984 }
985 die "$P: no git commits after extraction!\n" if (@commits == 0);
986 @ARGV = @commits;
987}
988
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800989my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700990for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800991 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700992 if ($git) {
993 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
994 die "$P: $filename: git format-patch failed - $!\n";
995 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800996 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700997 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800998 } elsif ($filename eq '-') {
999 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001000 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001001 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001002 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001003 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001004 if ($filename eq '-') {
1005 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001006 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001007 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001008 } else {
1009 $vname = $filename;
1010 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001011 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001012 chomp;
1013 push(@rawlines, $_);
1014 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001015 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001016
1017 if ($#ARGV > 0 && $quiet == 0) {
1018 print '-' x length($vname) . "\n";
1019 print "$vname\n";
1020 print '-' x length($vname) . "\n";
1021 }
1022
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001023 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024 $exit = 1;
1025 }
1026 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001027 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001028 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001029 @fixed_inserted = ();
1030 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001031 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001032 @modifierListFile = ();
1033 @typeListFile = ();
1034 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001035}
1036
Joe Perchesd8469f12015-06-25 15:03:00 -07001037if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001038 hash_show_words(\%use_type, "Used");
1039 hash_show_words(\%ignore_type, "Ignored");
1040
Joe Perches5b579802018-08-21 21:57:33 -07001041 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001042 print << "EOM"
1043
1044NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001045 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001046EOM
1047 }
1048 if ($exit) {
1049 print << "EOM"
1050
1051NOTE: If any of the errors are false positives, please report
1052 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1053EOM
1054 }
1055}
1056
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001057exit($exit);
1058
1059sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001060 my ($root) = @_;
1061
1062 my @tree_check = (
1063 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1064 "README", "Documentation", "arch", "include", "drivers",
1065 "fs", "init", "ipc", "kernel", "lib", "scripts",
1066 );
1067
1068 foreach my $check (@tree_check) {
1069 if (! -e $root . '/' . $check) {
1070 return 0;
1071 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001072 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001073 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001074}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001075
Joe Perches20112472011-07-25 17:13:23 -07001076sub parse_email {
1077 my ($formatted_email) = @_;
1078
1079 my $name = "";
1080 my $address = "";
1081 my $comment = "";
1082
1083 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1084 $name = $1;
1085 $address = $2;
1086 $comment = $3 if defined $3;
1087 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1088 $address = $1;
1089 $comment = $2 if defined $2;
1090 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1091 $address = $1;
1092 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001093 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001094 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001095 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001096 $name =~ s/^\"|\"$//g;
1097 # If there's a name left after stripping spaces and
1098 # leading quotes, and the address doesn't have both
1099 # leading and trailing angle brackets, the address
1100 # is invalid. ie:
1101 # "joe smith joe@smith.com" bad
1102 # "joe smith <joe@smith.com" bad
1103 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1104 $name = "";
1105 $address = "";
1106 $comment = "";
1107 }
1108 }
1109
Joe Perches3705ce52013-07-03 15:05:31 -07001110 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001111 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001112 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001113 $address =~ s/^\<|\>$//g;
1114
1115 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1116 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1117 $name = "\"$name\"";
1118 }
1119
1120 return ($name, $address, $comment);
1121}
1122
1123sub format_email {
1124 my ($name, $address) = @_;
1125
1126 my $formatted_email;
1127
Joe Perches3705ce52013-07-03 15:05:31 -07001128 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001129 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001130 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001131
1132 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1133 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1134 $name = "\"$name\"";
1135 }
1136
1137 if ("$name" eq "") {
1138 $formatted_email = "$address";
1139 } else {
1140 $formatted_email = "$name <$address>";
1141 }
1142
1143 return $formatted_email;
1144}
1145
Joe Perchesd311cd42014-08-06 16:10:57 -07001146sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001147 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001148
Joe Perchesbd474ca2014-08-06 16:11:10 -07001149 foreach my $path (split(/:/, $ENV{PATH})) {
1150 if (-e "$path/$bin") {
1151 return "$path/$bin";
1152 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001153 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001154
Joe Perchesbd474ca2014-08-06 16:11:10 -07001155 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001156}
1157
Joe Perches000d1cc12011-07-25 17:13:25 -07001158sub which_conf {
1159 my ($conf) = @_;
1160
1161 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1162 if (-e "$path/$conf") {
1163 return "$path/$conf";
1164 }
1165 }
1166
1167 return "";
1168}
1169
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001170sub expand_tabs {
1171 my ($str) = @_;
1172
1173 my $res = '';
1174 my $n = 0;
1175 for my $c (split(//, $str)) {
1176 if ($c eq "\t") {
1177 $res .= ' ';
1178 $n++;
1179 for (; ($n % 8) != 0; $n++) {
1180 $res .= ' ';
1181 }
1182 next;
1183 }
1184 $res .= $c;
1185 $n++;
1186 }
1187
1188 return $res;
1189}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001190sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001191 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001192 return $res;
1193}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001194
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001195sub line_stats {
1196 my ($line) = @_;
1197
1198 # Drop the diff line leader and expand tabs
1199 $line =~ s/^.//;
1200 $line = expand_tabs($line);
1201
1202 # Pick the indent from the front of the line.
1203 my ($white) = ($line =~ /^(\s*)/);
1204
1205 return (length($line), length($white));
1206}
1207
Andy Whitcroft773647a2008-03-28 14:15:58 -07001208my $sanitise_quote = '';
1209
1210sub sanitise_line_reset {
1211 my ($in_comment) = @_;
1212
1213 if ($in_comment) {
1214 $sanitise_quote = '*/';
1215 } else {
1216 $sanitise_quote = '';
1217 }
1218}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001219sub sanitise_line {
1220 my ($line) = @_;
1221
1222 my $res = '';
1223 my $l = '';
1224
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001225 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001226 my $off = 0;
1227 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001228
Andy Whitcroft773647a2008-03-28 14:15:58 -07001229 # Always copy over the diff marker.
1230 $res = substr($line, 0, 1);
1231
1232 for ($off = 1; $off < length($line); $off++) {
1233 $c = substr($line, $off, 1);
1234
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001235 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001236 # and end, all to $;.
1237 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1238 $sanitise_quote = '*/';
1239
1240 substr($res, $off, 2, "$;$;");
1241 $off++;
1242 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001243 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001244 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001245 $sanitise_quote = '';
1246 substr($res, $off, 2, "$;$;");
1247 $off++;
1248 next;
1249 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001250 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1251 $sanitise_quote = '//';
1252
1253 substr($res, $off, 2, $sanitise_quote);
1254 $off++;
1255 next;
1256 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001257
1258 # A \ in a string means ignore the next character.
1259 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1260 $c eq "\\") {
1261 substr($res, $off, 2, 'XX');
1262 $off++;
1263 next;
1264 }
1265 # Regular quotes.
1266 if ($c eq "'" || $c eq '"') {
1267 if ($sanitise_quote eq '') {
1268 $sanitise_quote = $c;
1269
1270 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001271 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001272 } elsif ($sanitise_quote eq $c) {
1273 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001274 }
1275 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001276
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001277 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001278 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1279 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001280 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1281 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001282 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1283 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001284 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001285 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001286 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001287 }
1288
Daniel Walker113f04a2009-09-21 17:04:35 -07001289 if ($sanitise_quote eq '//') {
1290 $sanitise_quote = '';
1291 }
1292
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001293 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001294 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001295 my $clean = 'X' x length($1);
1296 $res =~ s@\<.*\>@<$clean>@;
1297
1298 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001299 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001300 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001301 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001302 }
1303
Joe Perchesdadf6802016-08-02 14:04:33 -07001304 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1305 my $match = $1;
1306 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1307 }
1308
Andy Whitcroft00df3442007-06-08 13:47:06 -07001309 return $res;
1310}
1311
Joe Perchesa6962d72013-04-29 16:18:13 -07001312sub get_quoted_string {
1313 my ($line, $rawline) = @_;
1314
Joe Perches478b1792018-04-10 16:33:34 -07001315 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001316 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001317 return substr($rawline, $-[0], $+[0] - $-[0]);
1318}
1319
Andy Whitcroft8905a672007-11-28 16:21:06 -08001320sub ctx_statement_block {
1321 my ($linenr, $remain, $off) = @_;
1322 my $line = $linenr - 1;
1323 my $blk = '';
1324 my $soff = $off;
1325 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001326 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001327
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001328 my $loff = 0;
1329
Andy Whitcroft8905a672007-11-28 16:21:06 -08001330 my $type = '';
1331 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001332 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001333 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001334 my $c;
1335 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001336
1337 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001338 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001339 @stack = (['', 0]) if ($#stack == -1);
1340
Andy Whitcroft773647a2008-03-28 14:15:58 -07001341 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001342 # If we are about to drop off the end, pull in more
1343 # context.
1344 if ($off >= $len) {
1345 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001346 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001347 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001348 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001349 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001350 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001351 $len = length($blk);
1352 $line++;
1353 last;
1354 }
1355 # Bail if there is no further context.
1356 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001357 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001358 last;
1359 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001360 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1361 $level++;
1362 $type = '#';
1363 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001364 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001365 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001366 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001367 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001368
Andy Whitcroft773647a2008-03-28 14:15:58 -07001369 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001370
1371 # Handle nested #if/#else.
1372 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1373 push(@stack, [ $type, $level ]);
1374 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1375 ($type, $level) = @{$stack[$#stack - 1]};
1376 } elsif ($remainder =~ /^#\s*endif\b/) {
1377 ($type, $level) = @{pop(@stack)};
1378 }
1379
Andy Whitcroft8905a672007-11-28 16:21:06 -08001380 # Statement ends at the ';' or a close '}' at the
1381 # outermost level.
1382 if ($level == 0 && $c eq ';') {
1383 last;
1384 }
1385
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001386 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001387 if ($level == 0 && $coff_set == 0 &&
1388 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1389 $remainder =~ /^(else)(?:\s|{)/ &&
1390 $remainder !~ /^else\s+if\b/) {
1391 $coff = $off + length($1) - 1;
1392 $coff_set = 1;
1393 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1394 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001395 }
1396
Andy Whitcroft8905a672007-11-28 16:21:06 -08001397 if (($type eq '' || $type eq '(') && $c eq '(') {
1398 $level++;
1399 $type = '(';
1400 }
1401 if ($type eq '(' && $c eq ')') {
1402 $level--;
1403 $type = ($level != 0)? '(' : '';
1404
1405 if ($level == 0 && $coff < $soff) {
1406 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001407 $coff_set = 1;
1408 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001409 }
1410 }
1411 if (($type eq '' || $type eq '{') && $c eq '{') {
1412 $level++;
1413 $type = '{';
1414 }
1415 if ($type eq '{' && $c eq '}') {
1416 $level--;
1417 $type = ($level != 0)? '{' : '';
1418
1419 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001420 if (substr($blk, $off + 1, 1) eq ';') {
1421 $off++;
1422 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001423 last;
1424 }
1425 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001426 # Preprocessor commands end at the newline unless escaped.
1427 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1428 $level--;
1429 $type = '';
1430 $off++;
1431 last;
1432 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001433 $off++;
1434 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001435 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001436 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001437 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001438 $line++;
1439 $remain--;
1440 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001441
1442 my $statement = substr($blk, $soff, $off - $soff + 1);
1443 my $condition = substr($blk, $soff, $coff - $soff + 1);
1444
1445 #warn "STATEMENT<$statement>\n";
1446 #warn "CONDITION<$condition>\n";
1447
Andy Whitcroft773647a2008-03-28 14:15:58 -07001448 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001449
1450 return ($statement, $condition,
1451 $line, $remain + 1, $off - $loff + 1, $level);
1452}
1453
Andy Whitcroftcf655042008-03-04 14:28:20 -08001454sub statement_lines {
1455 my ($stmt) = @_;
1456
1457 # Strip the diff line prefixes and rip blank lines at start and end.
1458 $stmt =~ s/(^|\n)./$1/g;
1459 $stmt =~ s/^\s*//;
1460 $stmt =~ s/\s*$//;
1461
1462 my @stmt_lines = ($stmt =~ /\n/g);
1463
1464 return $#stmt_lines + 2;
1465}
1466
1467sub statement_rawlines {
1468 my ($stmt) = @_;
1469
1470 my @stmt_lines = ($stmt =~ /\n/g);
1471
1472 return $#stmt_lines + 2;
1473}
1474
1475sub statement_block_size {
1476 my ($stmt) = @_;
1477
1478 $stmt =~ s/(^|\n)./$1/g;
1479 $stmt =~ s/^\s*{//;
1480 $stmt =~ s/}\s*$//;
1481 $stmt =~ s/^\s*//;
1482 $stmt =~ s/\s*$//;
1483
1484 my @stmt_lines = ($stmt =~ /\n/g);
1485 my @stmt_statements = ($stmt =~ /;/g);
1486
1487 my $stmt_lines = $#stmt_lines + 2;
1488 my $stmt_statements = $#stmt_statements + 1;
1489
1490 if ($stmt_lines > $stmt_statements) {
1491 return $stmt_lines;
1492 } else {
1493 return $stmt_statements;
1494 }
1495}
1496
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001497sub ctx_statement_full {
1498 my ($linenr, $remain, $off) = @_;
1499 my ($statement, $condition, $level);
1500
1501 my (@chunks);
1502
Andy Whitcroftcf655042008-03-04 14:28:20 -08001503 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001504 ($statement, $condition, $linenr, $remain, $off, $level) =
1505 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001506 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001507 push(@chunks, [ $condition, $statement ]);
1508 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1509 return ($level, $linenr, @chunks);
1510 }
1511
1512 # Pull in the following conditional/block pairs and see if they
1513 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001514 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001515 ($statement, $condition, $linenr, $remain, $off, $level) =
1516 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001517 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001518 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001519 #print "C: push\n";
1520 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001521 }
1522
1523 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001524}
1525
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001526sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001527 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001528 my $line;
1529 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001530 my $blk = '';
1531 my @o;
1532 my @c;
1533 my @res = ();
1534
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001535 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001536 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001537 for ($line = $start; $remain > 0; $line++) {
1538 next if ($rawlines[$line] =~ /^-/);
1539 $remain--;
1540
1541 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001542
1543 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001544 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001545 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001546 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001547 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001548 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001549 $level = pop(@stack);
1550 }
1551
Andy Whitcroft01464f32010-10-26 14:23:19 -07001552 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001553 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1554 if ($off > 0) {
1555 $off--;
1556 next;
1557 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001558
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001559 if ($c eq $close && $level > 0) {
1560 $level--;
1561 last if ($level == 0);
1562 } elsif ($c eq $open) {
1563 $level++;
1564 }
1565 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001566
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001567 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001568 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001569 }
1570
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001571 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001572 }
1573
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001574 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001575}
1576sub ctx_block_outer {
1577 my ($linenr, $remain) = @_;
1578
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001579 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1580 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001581}
1582sub ctx_block {
1583 my ($linenr, $remain) = @_;
1584
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001585 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1586 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001587}
1588sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001589 my ($linenr, $remain, $off) = @_;
1590
1591 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1592 return @r;
1593}
1594sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001595 my ($linenr, $remain) = @_;
1596
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001597 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001598}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001599sub ctx_statement_level {
1600 my ($linenr, $remain, $off) = @_;
1601
1602 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1603}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001604
1605sub ctx_locate_comment {
1606 my ($first_line, $end_line) = @_;
1607
1608 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001609 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001610 return $current_comment if (defined $current_comment);
1611
1612 # Look through the context and try and figure out if there is a
1613 # comment.
1614 my $in_comment = 0;
1615 $current_comment = '';
1616 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001617 my $line = $rawlines[$linenr - 1];
1618 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001619 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1620 $in_comment = 1;
1621 }
1622 if ($line =~ m@/\*@) {
1623 $in_comment = 1;
1624 }
1625 if (!$in_comment && $current_comment ne '') {
1626 $current_comment = '';
1627 }
1628 $current_comment .= $line . "\n" if ($in_comment);
1629 if ($line =~ m@\*/@) {
1630 $in_comment = 0;
1631 }
1632 }
1633
1634 chomp($current_comment);
1635 return($current_comment);
1636}
1637sub ctx_has_comment {
1638 my ($first_line, $end_line) = @_;
1639 my $cmt = ctx_locate_comment($first_line, $end_line);
1640
Andy Whitcroft00df3442007-06-08 13:47:06 -07001641 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001642 ##print "CMMT: $cmt\n";
1643
1644 return ($cmt ne '');
1645}
1646
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001647sub raw_line {
1648 my ($linenr, $cnt) = @_;
1649
1650 my $offset = $linenr - 1;
1651 $cnt++;
1652
1653 my $line;
1654 while ($cnt) {
1655 $line = $rawlines[$offset++];
1656 next if (defined($line) && $line =~ /^-/);
1657 $cnt--;
1658 }
1659
1660 return $line;
1661}
1662
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001663sub get_stat_real {
1664 my ($linenr, $lc) = @_;
1665
1666 my $stat_real = raw_line($linenr, 0);
1667 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1668 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1669 }
1670
1671 return $stat_real;
1672}
1673
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001674sub get_stat_here {
1675 my ($linenr, $cnt, $here) = @_;
1676
1677 my $herectx = $here . "\n";
1678 for (my $n = 0; $n < $cnt; $n++) {
1679 $herectx .= raw_line($linenr, $n) . "\n";
1680 }
1681
1682 return $herectx;
1683}
1684
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001685sub cat_vet {
1686 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001687 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001688
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001689 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001690 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1691 $res .= $1;
1692 if ($2 ne '') {
1693 $coded = sprintf("^%c", unpack('C', $2) + 64);
1694 $res .= $coded;
1695 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001696 }
1697 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001698
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001699 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001700}
1701
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001702my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001703my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001704my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001705my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001706
1707sub annotate_reset {
1708 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001709 $av_pending = '_';
1710 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001711 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001712}
1713
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001714sub annotate_values {
1715 my ($stream, $type) = @_;
1716
1717 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001718 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001719 my $cur = $stream;
1720
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001721 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001722
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001723 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001724 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001725 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001726 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001727 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001728 print "WS($1)\n" if ($dbg_values > 1);
1729 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001730 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001731 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001732 }
1733
Florian Micklerc023e4732011-01-12 16:59:58 -08001734 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001735 print "CAST($1)\n" if ($dbg_values > 1);
1736 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001737 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001738
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001739 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001740 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001741 $type = 'T';
1742
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001743 } elsif ($cur =~ /^($Modifier)\s*/) {
1744 print "MODIFIER($1)\n" if ($dbg_values > 1);
1745 $type = 'T';
1746
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001747 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001748 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001749 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001750 push(@av_paren_type, $type);
1751 if ($2 ne '') {
1752 $av_pending = 'N';
1753 }
1754 $type = 'E';
1755
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001756 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001757 print "UNDEF($1)\n" if ($dbg_values > 1);
1758 $av_preprocessor = 1;
1759 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001760
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001761 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001762 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001763 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001764
1765 push(@av_paren_type, $type);
1766 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001767 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001768
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001769 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001770 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1771 $av_preprocessor = 1;
1772
1773 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1774
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001775 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001776
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001777 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001778 print "PRE_END($1)\n" if ($dbg_values > 1);
1779
1780 $av_preprocessor = 1;
1781
1782 # Assume all arms of the conditional end as this
1783 # one does, and continue as if the #endif was not here.
1784 pop(@av_paren_type);
1785 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001786 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001787
1788 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001789 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001790
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001791 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1792 print "ATTR($1)\n" if ($dbg_values > 1);
1793 $av_pending = $type;
1794 $type = 'N';
1795
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001796 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001797 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001798 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001799 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001800 }
1801 $type = 'N';
1802
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001803 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001804 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001805 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001806 $type = 'N';
1807
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001808 } elsif ($cur =~/^(case)/o) {
1809 print "CASE($1)\n" if ($dbg_values > 1);
1810 $av_pend_colon = 'C';
1811 $type = 'N';
1812
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001813 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001814 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001815 $type = 'N';
1816
1817 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001818 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001819 push(@av_paren_type, $av_pending);
1820 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001821 $type = 'N';
1822
1823 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001824 my $new_type = pop(@av_paren_type);
1825 if ($new_type ne '_') {
1826 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001827 print "PAREN('$1') -> $type\n"
1828 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001829 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001830 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001831 }
1832
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001833 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001834 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001835 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001836 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001837
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001838 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1839 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001840 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001841 } elsif ($type eq 'E') {
1842 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001843 }
1844 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1845 $type = 'V';
1846
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001847 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001848 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001849 $type = 'V';
1850
1851 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001852 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001853 $type = 'N';
1854
Andy Whitcroftcf655042008-03-04 14:28:20 -08001855 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001856 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001857 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001858 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001859
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001860 } elsif ($cur =~/^(,)/) {
1861 print "COMMA($1)\n" if ($dbg_values > 1);
1862 $type = 'C';
1863
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001864 } elsif ($cur =~ /^(\?)/o) {
1865 print "QUESTION($1)\n" if ($dbg_values > 1);
1866 $type = 'N';
1867
1868 } elsif ($cur =~ /^(:)/o) {
1869 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1870
1871 substr($var, length($res), 1, $av_pend_colon);
1872 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1873 $type = 'E';
1874 } else {
1875 $type = 'N';
1876 }
1877 $av_pend_colon = 'O';
1878
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001879 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001880 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001881 $type = 'N';
1882
Andy Whitcroft0d413862008-10-15 22:02:16 -07001883 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001884 my $variant;
1885
1886 print "OPV($1)\n" if ($dbg_values > 1);
1887 if ($type eq 'V') {
1888 $variant = 'B';
1889 } else {
1890 $variant = 'U';
1891 }
1892
1893 substr($var, length($res), 1, $variant);
1894 $type = 'N';
1895
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001896 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001897 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001898 if ($1 ne '++' && $1 ne '--') {
1899 $type = 'N';
1900 }
1901
1902 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001903 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001904 }
1905 if (defined $1) {
1906 $cur = substr($cur, length($1));
1907 $res .= $type x length($1);
1908 }
1909 }
1910
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001911 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001912}
1913
Andy Whitcroft8905a672007-11-28 16:21:06 -08001914sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001915 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001916 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001917 ^(?:
1918 $Modifier|
1919 $Storage|
1920 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001921 DEFINE_\S+
1922 )$|
1923 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001924 goto|
1925 return|
1926 case|
1927 else|
1928 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001929 do|
1930 \#|
1931 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001932 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001933 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001934 )}x;
1935 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1936 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001937 # Check for modifiers.
1938 $possible =~ s/\s*$Storage\s*//g;
1939 $possible =~ s/\s*$Sparse\s*//g;
1940 if ($possible =~ /^\s*$/) {
1941
1942 } elsif ($possible =~ /\s/) {
1943 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001944 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001945 if ($modifier !~ $notPermitted) {
1946 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001947 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001948 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001949 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001950
1951 } else {
1952 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001953 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001954 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001955 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001956 } else {
1957 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001958 }
1959}
1960
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001961my $prefix = '';
1962
Joe Perches000d1cc12011-07-25 17:13:25 -07001963sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001964 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001965
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001966 $type =~ tr/[a-z]/[A-Z]/;
1967
Joe Perchescbec18a2014-04-03 14:49:19 -07001968 return defined $use_type{$type} if (scalar keys %use_type > 0);
1969
1970 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001971}
1972
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001973sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001974 my ($level, $type, $msg) = @_;
1975
1976 if (!show_type($type) ||
1977 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001978 return 0;
1979 }
Joe Perches57230292015-06-25 15:03:03 -07001980 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07001981 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07001982 if ($level eq 'ERROR') {
1983 $output .= RED;
1984 } elsif ($level eq 'WARNING') {
1985 $output .= YELLOW;
1986 } else {
1987 $output .= GREEN;
1988 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001989 }
Joe Perches57230292015-06-25 15:03:03 -07001990 $output .= $prefix . $level . ':';
1991 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07001992 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001993 $output .= "$type:";
1994 }
John Brooks737c0762017-07-10 15:52:24 -07001995 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001996 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001997
1998 if ($showfile) {
1999 my @lines = split("\n", $output, -1);
2000 splice(@lines, 1, 1);
2001 $output = join("\n", @lines);
2002 }
Joe Perches57230292015-06-25 15:03:03 -07002003 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002004
Joe Perches57230292015-06-25 15:03:03 -07002005 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002006
2007 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002008}
Joe Perchescbec18a2014-04-03 14:49:19 -07002009
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002010sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002011 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002012}
Joe Perches000d1cc12011-07-25 17:13:25 -07002013
Joe Perchesd752fcc2014-08-06 16:11:05 -07002014sub fixup_current_range {
2015 my ($lineRef, $offset, $length) = @_;
2016
2017 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2018 my $o = $1;
2019 my $l = $2;
2020 my $no = $o + $offset;
2021 my $nl = $l + $length;
2022 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2023 }
2024}
2025
2026sub fix_inserted_deleted_lines {
2027 my ($linesRef, $insertedRef, $deletedRef) = @_;
2028
2029 my $range_last_linenr = 0;
2030 my $delta_offset = 0;
2031
2032 my $old_linenr = 0;
2033 my $new_linenr = 0;
2034
2035 my $next_insert = 0;
2036 my $next_delete = 0;
2037
2038 my @lines = ();
2039
2040 my $inserted = @{$insertedRef}[$next_insert++];
2041 my $deleted = @{$deletedRef}[$next_delete++];
2042
2043 foreach my $old_line (@{$linesRef}) {
2044 my $save_line = 1;
2045 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002046 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002047 $delta_offset = 0;
2048 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2049 $range_last_linenr = $new_linenr;
2050 fixup_current_range(\$line, $delta_offset, 0);
2051 }
2052
2053 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2054 $deleted = @{$deletedRef}[$next_delete++];
2055 $save_line = 0;
2056 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2057 }
2058
2059 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2060 push(@lines, ${$inserted}{'LINE'});
2061 $inserted = @{$insertedRef}[$next_insert++];
2062 $new_linenr++;
2063 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2064 }
2065
2066 if ($save_line) {
2067 push(@lines, $line);
2068 $new_linenr++;
2069 }
2070
2071 $old_linenr++;
2072 }
2073
2074 return @lines;
2075}
2076
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002077sub fix_insert_line {
2078 my ($linenr, $line) = @_;
2079
2080 my $inserted = {
2081 LINENR => $linenr,
2082 LINE => $line,
2083 };
2084 push(@fixed_inserted, $inserted);
2085}
2086
2087sub fix_delete_line {
2088 my ($linenr, $line) = @_;
2089
2090 my $deleted = {
2091 LINENR => $linenr,
2092 LINE => $line,
2093 };
2094
2095 push(@fixed_deleted, $deleted);
2096}
2097
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002098sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002099 my ($type, $msg) = @_;
2100
2101 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002102 our $clean = 0;
2103 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002104 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002105 }
Joe Perches3705ce52013-07-03 15:05:31 -07002106 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002107}
2108sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002109 my ($type, $msg) = @_;
2110
2111 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002112 our $clean = 0;
2113 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002114 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002115 }
Joe Perches3705ce52013-07-03 15:05:31 -07002116 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002117}
2118sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002119 my ($type, $msg) = @_;
2120
2121 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002122 our $clean = 0;
2123 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002124 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002125 }
Joe Perches3705ce52013-07-03 15:05:31 -07002126 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002127}
2128
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002129sub check_absolute_file {
2130 my ($absolute, $herecurr) = @_;
2131 my $file = $absolute;
2132
2133 ##print "absolute<$absolute>\n";
2134
2135 # See if any suffix of this path is a path within the tree.
2136 while ($file =~ s@^[^/]*/@@) {
2137 if (-f "$root/$file") {
2138 ##print "file<$file>\n";
2139 last;
2140 }
2141 }
2142 if (! -f _) {
2143 return 0;
2144 }
2145
2146 # It is, so see if the prefix is acceptable.
2147 my $prefix = $absolute;
2148 substr($prefix, -length($file)) = '';
2149
2150 ##print "prefix<$prefix>\n";
2151 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002152 WARN("USE_RELATIVE_PATH",
2153 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002154 }
2155}
2156
Joe Perches3705ce52013-07-03 15:05:31 -07002157sub trim {
2158 my ($string) = @_;
2159
Joe Perchesb34c6482013-09-11 14:24:01 -07002160 $string =~ s/^\s+|\s+$//g;
2161
2162 return $string;
2163}
2164
2165sub ltrim {
2166 my ($string) = @_;
2167
2168 $string =~ s/^\s+//;
2169
2170 return $string;
2171}
2172
2173sub rtrim {
2174 my ($string) = @_;
2175
2176 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002177
2178 return $string;
2179}
2180
Joe Perches52ea8502013-11-12 15:10:09 -08002181sub string_find_replace {
2182 my ($string, $find, $replace) = @_;
2183
2184 $string =~ s/$find/$replace/g;
2185
2186 return $string;
2187}
2188
Joe Perches3705ce52013-07-03 15:05:31 -07002189sub tabify {
2190 my ($leading) = @_;
2191
2192 my $source_indent = 8;
2193 my $max_spaces_before_tab = $source_indent - 1;
2194 my $spaces_to_tab = " " x $source_indent;
2195
2196 #convert leading spaces to tabs
2197 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2198 #Remove spaces before a tab
2199 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2200
2201 return "$leading";
2202}
2203
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002204sub pos_last_openparen {
2205 my ($line) = @_;
2206
2207 my $pos = 0;
2208
2209 my $opens = $line =~ tr/\(/\(/;
2210 my $closes = $line =~ tr/\)/\)/;
2211
2212 my $last_openparen = 0;
2213
2214 if (($opens == 0) || ($closes >= $opens)) {
2215 return -1;
2216 }
2217
2218 my $len = length($line);
2219
2220 for ($pos = 0; $pos < $len; $pos++) {
2221 my $string = substr($line, $pos);
2222 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2223 $pos += length($1) - 1;
2224 } elsif (substr($line, $pos, 1) eq '(') {
2225 $last_openparen = $pos;
2226 } elsif (index($string, '(') == -1) {
2227 last;
2228 }
2229 }
2230
Joe Perches91cb5192014-04-03 14:49:32 -07002231 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002232}
2233
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002234sub process {
2235 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002236
2237 my $linenr=0;
2238 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002239 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002240 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002241 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002242
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002243 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002244 my $indent;
2245 my $previndent=0;
2246 my $stashindent=0;
2247
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002248 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002249 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002250 my $author = '';
2251 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002252 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002253 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002254 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002255 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches77cb8542017-02-24 15:01:28 -08002256 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002257 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002258 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002259 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002260 my $non_utf8_charset = 0;
2261
Joe Perches365dd4e2014-08-06 16:10:42 -07002262 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002263 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002264
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002265 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002266 our $cnt_lines = 0;
2267 our $cnt_error = 0;
2268 our $cnt_warn = 0;
2269 our $cnt_chk = 0;
2270
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002271 # Trace the real file/line as we go.
2272 my $realfile = '';
2273 my $realline = 0;
2274 my $realcnt = 0;
2275 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002276 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002277 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002278 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002279 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002280 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002281
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002282 my $prev_values = 'E';
2283
2284 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002286 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002287 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002288 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002289
Joe Perches7e51f192013-09-11 14:23:57 -07002290 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002291
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002292 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002293 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002294 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002295 my @setup_docs = ();
2296 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002297
Joe Perchesd8b07712013-11-12 15:10:06 -08002298 my $camelcase_file_seeded = 0;
2299
Rob Herring9f3a8992018-04-10 16:33:13 -07002300 my $checklicenseline = 1;
2301
Andy Whitcroft773647a2008-03-28 14:15:58 -07002302 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002303 my $line;
2304 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002305 $linenr++;
2306 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002307
Joe Perches3705ce52013-07-03 15:05:31 -07002308 push(@fixed, $rawline) if ($fix);
2309
Andy Whitcroft773647a2008-03-28 14:15:58 -07002310 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002311 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002312 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002313 $setup_docs = 1;
2314 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002315 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002316 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002317 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002318 $realline=$1-1;
2319 if (defined $2) {
2320 $realcnt=$3+1;
2321 } else {
2322 $realcnt=1+1;
2323 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002324 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002325
2326 # Guestimate if this is a continuing comment. Run
2327 # the context looking for a comment "edge". If this
2328 # edge is a close comment then we must be in a comment
2329 # at context start.
2330 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002331 my $cnt = $realcnt;
2332 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2333 next if (defined $rawlines[$ln - 1] &&
2334 $rawlines[$ln - 1] =~ /^-/);
2335 $cnt--;
2336 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002337 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002338 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2339 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2340 ($edge) = $1;
2341 last;
2342 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002343 }
2344 if (defined $edge && $edge eq '*/') {
2345 $in_comment = 1;
2346 }
2347
2348 # Guestimate if this is a continuing comment. If this
2349 # is the start of a diff block and this line starts
2350 # ' *' then it is very likely a comment.
2351 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002352 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002353 {
2354 $in_comment = 1;
2355 }
2356
2357 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2358 sanitise_line_reset($in_comment);
2359
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002360 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002361 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002362 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002363 $line = sanitise_line($rawline);
2364 }
2365 push(@lines, $line);
2366
2367 if ($realcnt > 1) {
2368 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2369 } else {
2370 $realcnt = 0;
2371 }
2372
2373 #print "==>$rawline\n";
2374 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002375
2376 if ($setup_docs && $line =~ /^\+/) {
2377 push(@setup_docs, $line);
2378 }
2379 }
2380
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002381 $prefix = '';
2382
Andy Whitcroft773647a2008-03-28 14:15:58 -07002383 $realcnt = 0;
2384 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002385 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002386 foreach my $line (@lines) {
2387 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002388 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002389 my $sline = $line; #copy of $line
2390 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002391
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002392 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002393
Joe Perches12c253a2018-06-07 17:10:58 -07002394# check if it's a mode change, rename or start of a patch
2395 if (!$in_commit_log &&
2396 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2397 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2398 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2399 $is_patch = 1;
2400 }
2401
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002402#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002403 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002404 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2405 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002406 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002407 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002408 $realline=$1-1;
2409 if (defined $2) {
2410 $realcnt=$3+1;
2411 } else {
2412 $realcnt=1+1;
2413 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002414 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002415 $prev_values = 'E';
2416
Andy Whitcroft773647a2008-03-28 14:15:58 -07002417 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002418 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002419 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002420 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002421 if ($context =~ /\b(\w+)\s*\(/) {
2422 $context_function = $1;
2423 } else {
2424 undef $context_function;
2425 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002426 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002427
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002428# track the line number as we move through the hunk, note that
2429# new versions of GNU diff omit the leading space on completely
2430# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002431 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002432 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002433 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002434
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002435 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002436 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002437
2438 # Track the previous line.
2439 ($prevline, $stashline) = ($stashline, $line);
2440 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002441 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2442
Andy Whitcroft773647a2008-03-28 14:15:58 -07002443 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002444
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002445 } elsif ($realcnt == 1) {
2446 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002447 }
2448
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002449 my $hunk_line = ($realcnt != 0);
2450
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002451 $here = "#$linenr: " if (!$file);
2452 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002453
Joe Perches2ac73b42014-06-04 16:12:05 -07002454 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002455 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002456 if ($line =~ /^diff --git.*?(\S+)$/) {
2457 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002458 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002459 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002460 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002461 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002462 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002463 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002464 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002465
2466 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002467 if (!$file && $tree && $p1_prefix ne '' &&
2468 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002469 WARN("PATCH_PREFIX",
2470 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002471 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002472
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002473 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002474 ERROR("MODIFIED_INCLUDE_ASM",
2475 "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 -07002476 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002477 $found_file = 1;
2478 }
2479
Joe Perches34d88152015-06-25 15:03:05 -07002480#make up the handle for any error we report on this line
2481 if ($showfile) {
2482 $prefix = "$realfile:$realline: "
2483 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002484 if ($file) {
2485 $prefix = "$filename:$realline: ";
2486 } else {
2487 $prefix = "$filename:$linenr: ";
2488 }
Joe Perches34d88152015-06-25 15:03:05 -07002489 }
2490
Joe Perches2ac73b42014-06-04 16:12:05 -07002491 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002492 if (is_maintained_obsolete($realfile)) {
2493 WARN("OBSOLETE",
2494 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2495 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002496 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002497 $check = 1;
2498 } else {
2499 $check = $check_orig;
2500 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002501 $checklicenseline = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002502 next;
2503 }
2504
Randy Dunlap389834b2007-06-08 13:47:03 -07002505 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002506
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002507 my $hereline = "$here\n$rawline\n";
2508 my $herecurr = "$here\n$rawline\n";
2509 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002510
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002511 $cnt_lines++ if ($realcnt != 0);
2512
Joe Perchese518e9a2015-06-25 15:03:27 -07002513# Check if the commit log has what seems like a diff which can confuse patch
2514 if ($in_commit_log && !$commit_log_has_diff &&
2515 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2516 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2517 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2518 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2519 ERROR("DIFF_IN_COMMIT_MSG",
2520 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2521 $commit_log_has_diff = 1;
2522 }
2523
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002524# Check for incorrect file permissions
2525 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2526 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002527 if ($realfile !~ m@scripts/@ &&
2528 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002529 ERROR("EXECUTE_PERMISSIONS",
2530 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002531 }
2532 }
2533
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002534# Check the patch for a From:
2535 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2536 $author = $1;
2537 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2538 $author =~ s/"//g;
2539 }
2540
Joe Perches20112472011-07-25 17:13:23 -07002541# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002542 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002543 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002544 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002545 if ($author ne '') {
2546 my $l = $line;
2547 $l =~ s/"//g;
2548 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2549 $authorsignoff = 1;
2550 }
2551 }
Joe Perches20112472011-07-25 17:13:23 -07002552 }
2553
Joe Perchese0d975b2014-12-10 15:51:49 -08002554# Check if MAINTAINERS is being updated. If so, there's probably no need to
2555# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2556 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2557 $reported_maintainer_file = 1;
2558 }
2559
Joe Perches20112472011-07-25 17:13:23 -07002560# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002561 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002562 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002563 my $space_before = $1;
2564 my $sign_off = $2;
2565 my $space_after = $3;
2566 my $email = $4;
2567 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2568
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002569 if ($sign_off !~ /$signature_tags/) {
2570 WARN("BAD_SIGN_OFF",
2571 "Non-standard signature: $sign_off\n" . $herecurr);
2572 }
Joe Perches20112472011-07-25 17:13:23 -07002573 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002574 if (WARN("BAD_SIGN_OFF",
2575 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2576 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002577 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002578 "$ucfirst_sign_off $email";
2579 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002580 }
Joe Perches20112472011-07-25 17:13:23 -07002581 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002582 if (WARN("BAD_SIGN_OFF",
2583 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2584 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002585 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002586 "$ucfirst_sign_off $email";
2587 }
2588
Joe Perches20112472011-07-25 17:13:23 -07002589 }
2590 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002591 if (WARN("BAD_SIGN_OFF",
2592 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2593 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002594 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002595 "$ucfirst_sign_off $email";
2596 }
Joe Perches20112472011-07-25 17:13:23 -07002597 }
2598
2599 my ($email_name, $email_address, $comment) = parse_email($email);
2600 my $suggested_email = format_email(($email_name, $email_address));
2601 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002602 ERROR("BAD_SIGN_OFF",
2603 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002604 } else {
2605 my $dequoted = $suggested_email;
2606 $dequoted =~ s/^"//;
2607 $dequoted =~ s/" </ </;
2608 # Don't force email to have quotes
2609 # Allow just an angle bracketed address
2610 if ("$dequoted$comment" ne $email &&
2611 "<$email_address>$comment" ne $email &&
2612 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002613 WARN("BAD_SIGN_OFF",
2614 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002615 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002616 }
Joe Perches7e51f192013-09-11 14:23:57 -07002617
2618# Check for duplicate signatures
2619 my $sig_nospace = $line;
2620 $sig_nospace =~ s/\s//g;
2621 $sig_nospace = lc($sig_nospace);
2622 if (defined $signatures{$sig_nospace}) {
2623 WARN("BAD_SIGN_OFF",
2624 "Duplicate signature\n" . $herecurr);
2625 } else {
2626 $signatures{$sig_nospace} = 1;
2627 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002628 }
2629
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002630# Check email subject for common tools that don't need to be mentioned
2631 if ($in_header_lines &&
2632 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2633 WARN("EMAIL_SUBJECT",
2634 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2635 }
2636
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002637# Check for unwanted Gerrit info
2638 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2639 ERROR("GERRIT_CHANGE_ID",
2640 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2641 }
2642
Joe Perches369c8dd2015-11-06 16:31:34 -08002643# Check if the commit log is in a possible stack dump
2644 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2645 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2646 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2647 # timestamp
2648 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2649 # stack dump address
2650 $commit_log_possible_stack_dump = 1;
2651 }
2652
Joe Perches2a076f42015-04-16 12:44:28 -07002653# Check for line lengths > 75 in commit log, warn once
2654 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002655 length($line) > 75 &&
2656 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2657 # file delta changes
2658 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2659 # filename then :
2660 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2661 # A Fixes: or Link: line
2662 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002663 WARN("COMMIT_LOG_LONG_LINE",
2664 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2665 $commit_log_long_line = 1;
2666 }
2667
Joe Perchesbf4daf12015-09-09 15:37:50 -07002668# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002669 if ($in_commit_log && $commit_log_possible_stack_dump &&
2670 $line =~ /^\s*$/) {
2671 $commit_log_possible_stack_dump = 0;
2672 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002673
Joe Perches0d7835f2015-02-13 14:38:35 -08002674# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002675 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002676 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002677 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002678 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002679 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002680 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2681 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002682 my $init_char = "c";
2683 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002684 my $short = 1;
2685 my $long = 0;
2686 my $case = 1;
2687 my $space = 1;
2688 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002689 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002690 my $id = '0123456789ab';
2691 my $orig_desc = "commit description";
2692 my $description = "";
2693
Joe Perchesfe043ea2015-09-09 15:37:25 -07002694 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2695 $init_char = $1;
2696 $orig_commit = lc($2);
2697 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2698 $orig_commit = lc($1);
2699 }
2700
Joe Perches0d7835f2015-02-13 14:38:35 -08002701 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2702 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2703 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2704 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2705 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2706 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002707 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002708 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2709 defined $rawlines[$linenr] &&
2710 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2711 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002712 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002713 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2714 defined $rawlines[$linenr] &&
2715 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2716 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2717 $orig_desc = $1;
2718 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2719 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002720 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002721 }
2722
2723 ($id, $description) = git_commit_info($orig_commit,
2724 $id, $orig_desc);
2725
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002726 if (defined($id) &&
2727 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002728 ERROR("GIT_COMMIT_ID",
2729 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2730 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002731 }
2732
Joe Perches13f19372014-08-06 16:10:59 -07002733# Check for added, moved or deleted files
2734 if (!$reported_maintainer_file && !$in_commit_log &&
2735 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2736 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2737 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2738 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002739 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002740 $reported_maintainer_file = 1;
2741 WARN("FILE_PATH_CHANGES",
2742 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2743 }
2744
Andy Whitcroft00df3442007-06-08 13:47:06 -07002745# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002746 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002747 ERROR("CORRUPTED_PATCH",
2748 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002749 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002750 }
2751
2752# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2753 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002754 $rawline !~ m/^$UTF8*$/) {
2755 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2756
2757 my $blank = copy_spacing($rawline);
2758 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2759 my $hereptr = "$hereline$ptr\n";
2760
Joe Perches34d99212011-07-25 17:13:26 -07002761 CHK("INVALID_UTF8",
2762 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002763 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002764
Joe Perches15662b32011-10-31 17:13:12 -07002765# Check if it's the start of a commit log
2766# (not a header line and we haven't seen the patch filename)
2767 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002768 !($rawline =~ /^\s+(?:\S|$)/ ||
2769 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002770 $in_header_lines = 0;
2771 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002772 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002773 }
2774
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002775# Check if there is UTF-8 in a commit log when a mail header has explicitly
2776# declined it, i.e defined some charset where it is missing.
2777 if ($in_header_lines &&
2778 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2779 $1 !~ /utf-8/i) {
2780 $non_utf8_charset = 1;
2781 }
2782
2783 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002784 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002785 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002786 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2787 }
2788
Joe Perchesd6430f72016-12-12 16:46:28 -08002789# Check for absolute kernel paths in commit message
2790 if ($tree && $in_commit_log) {
2791 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2792 my $file = $1;
2793
2794 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2795 check_absolute_file($1, $herecurr)) {
2796 #
2797 } else {
2798 check_absolute_file($file, $herecurr);
2799 }
2800 }
2801 }
2802
Kees Cook66b47b42014-10-13 15:51:57 -07002803# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002804 if (defined($misspellings) &&
2805 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002806 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002807 my $typo = $1;
2808 my $typo_fix = $spelling_fix{lc($typo)};
2809 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2810 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002811 my $msg_level = \&WARN;
2812 $msg_level = \&CHK if ($file);
2813 if (&{$msg_level}("TYPO_SPELLING",
2814 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002815 $fix) {
2816 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2817 }
2818 }
2819 }
2820
Andy Whitcroft306708542008-10-15 22:02:28 -07002821# ignore non-hunk lines and lines being removed
2822 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002823
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002824#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002825 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002826 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002827 if (ERROR("DOS_LINE_ENDINGS",
2828 "DOS line endings\n" . $herevet) &&
2829 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002830 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002831 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002832 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2833 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002834 if (ERROR("TRAILING_WHITESPACE",
2835 "trailing whitespace\n" . $herevet) &&
2836 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002837 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002838 }
2839
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002840 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002841 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002842
Josh Triplett4783f892013-11-12 15:10:12 -08002843# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002844 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002845 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002846 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2847 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002848 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002849 my $msg_level = \&ERROR;
2850 $msg_level = \&CHK if ($file);
2851 &{$msg_level}("FSF_MAILING_ADDRESS",
2852 "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 -08002853 }
2854
Andi Kleen33549572010-05-24 14:33:29 -07002855# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002856# Only applies when adding the entry originally, after that we do not have
2857# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002858 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002859 # 'choice' is usually the last thing on the line (though
2860 # Kconfig supports named choices), so use a word boundary
2861 # (\b) rather than a whitespace character (\s)
2862 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002863 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002864 my $cnt = $realcnt;
2865 my $ln = $linenr + 1;
2866 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002867 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002868 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002869 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002870 $f = $lines[$ln - 1];
2871 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2872 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002873
2874 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002875 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002876
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01002877 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002878 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01002879 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2880 if ($lines[$ln - 1] =~ "---help---") {
2881 WARN("CONFIG_DESCRIPTION",
2882 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2883 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002884 $length = -1;
2885 }
2886
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002887 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002888 $f =~ s/#.*//;
2889 $f =~ s/^\s+//;
2890 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01002891
2892 # This only checks context lines in the patch
2893 # and so hopefully shouldn't trigger false
2894 # positives, even though some of these are
2895 # common words in help texts
2896 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2897 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002898 $is_end = 1;
2899 last;
2900 }
Andi Kleen33549572010-05-24 14:33:29 -07002901 $length++;
2902 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002903 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2904 WARN("CONFIG_DESCRIPTION",
2905 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2906 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002907 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002908 }
2909
Joe Perches628f91a2017-07-10 15:52:07 -07002910# check for MAINTAINERS entries that don't have the right form
2911 if ($realfile =~ /^MAINTAINERS$/ &&
2912 $rawline =~ /^\+[A-Z]:/ &&
2913 $rawline !~ /^\+[A-Z]:\t\S/) {
2914 if (WARN("MAINTAINERS_STYLE",
2915 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2916 $fix) {
2917 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2918 }
2919 }
2920
Christoph Jaeger327953e2015-02-13 14:38:29 -08002921# discourage the use of boolean for type definition attributes of Kconfig options
2922 if ($realfile =~ /Kconfig/ &&
2923 $line =~ /^\+\s*\bboolean\b/) {
2924 WARN("CONFIG_TYPE_BOOLEAN",
2925 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2926 }
2927
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002928 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2929 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2930 my $flag = $1;
2931 my $replacement = {
2932 'EXTRA_AFLAGS' => 'asflags-y',
2933 'EXTRA_CFLAGS' => 'ccflags-y',
2934 'EXTRA_CPPFLAGS' => 'cppflags-y',
2935 'EXTRA_LDFLAGS' => 'ldflags-y',
2936 };
2937
2938 WARN("DEPRECATED_VARIABLE",
2939 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2940 }
2941
Rob Herringbff5da42014-01-23 15:54:51 -08002942# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002943 if (defined $root &&
2944 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2945 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2946
Rob Herringbff5da42014-01-23 15:54:51 -08002947 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2948
Florian Vaussardcc933192014-04-03 14:49:27 -07002949 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2950 my $vp_file = $dt_path . "vendor-prefixes.txt";
2951
Rob Herringbff5da42014-01-23 15:54:51 -08002952 foreach my $compat (@compats) {
2953 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002954 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2955 my $compat3 = $compat;
2956 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2957 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002958 if ( $? >> 8 ) {
2959 WARN("UNDOCUMENTED_DT_STRING",
2960 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2961 }
2962
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002963 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2964 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002965 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002966 if ( $? >> 8 ) {
2967 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002968 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002969 }
2970 }
2971 }
2972
Rob Herring9f3a8992018-04-10 16:33:13 -07002973# check for using SPDX license tag at beginning of files
2974 if ($realline == $checklicenseline) {
2975 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
2976 $checklicenseline = 2;
2977 } elsif ($rawline =~ /^\+/) {
2978 my $comment = "";
2979 if ($realfile =~ /\.(h|s|S)$/) {
2980 $comment = '/*';
2981 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
2982 $comment = '//';
2983 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
2984 $comment = '#';
2985 } elsif ($realfile =~ /\.rst$/) {
2986 $comment = '..';
2987 }
2988
2989 if ($comment !~ /^$/ &&
2990 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
Joe Perches3b6e8ac2018-08-21 21:57:47 -07002991 WARN("SPDX_LICENSE_TAG",
2992 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
2993 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
2994 my $spdx_license = $1;
2995 if (!is_SPDX_License_valid($spdx_license)) {
2996 WARN("SPDX_LICENSE_TAG",
2997 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
2998 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002999 }
3000 }
3001 }
3002
Andy Whitcroft5368df202008-10-15 22:02:27 -07003003# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003004 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003005
Joe Perches47e0c882015-06-25 15:02:57 -07003006# line length limit (with some exclusions)
3007#
3008# There are a few types of lines that may extend beyond $max_line_length:
3009# logging functions like pr_info that end in a string
3010# lines with a single string
3011# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003012# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003013#
3014# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003015# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003016# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3017# LONG_LINE all other lines longer than $max_line_length
3018#
3019# if LONG_LINE is ignored, the other 2 types are also ignored
3020#
3021
Joe Perchesb4749e92015-07-17 16:24:01 -07003022 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003023 my $msg_type = "LONG_LINE";
3024
3025 # Check the allowed long line types first
3026
3027 # logging functions that end in a string that starts
3028 # before $max_line_length
3029 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3030 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3031 $msg_type = "";
3032
3033 # lines with only strings (w/ possible termination)
3034 # #defines with only strings
3035 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3036 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3037 $msg_type = "";
3038
Joe Perchescc147502017-11-17 15:28:44 -08003039 # More special cases
3040 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3041 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003042 $msg_type = "";
3043
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003044 # URL ($rawline is used in case the URL is in a comment)
3045 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3046 $msg_type = "";
3047
Joe Perches47e0c882015-06-25 15:02:57 -07003048 # Otherwise set the alternate message types
3049
3050 # a comment starts before $max_line_length
3051 } elsif ($line =~ /($;[\s$;]*)$/ &&
3052 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3053 $msg_type = "LONG_LINE_COMMENT"
3054
3055 # a quoted string starts before $max_line_length
3056 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3057 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3058 $msg_type = "LONG_LINE_STRING"
3059 }
3060
3061 if ($msg_type ne "" &&
3062 (show_type("LONG_LINE") || show_type($msg_type))) {
3063 WARN($msg_type,
3064 "line over $max_line_length characters\n" . $herecurr);
3065 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003066 }
3067
Andy Whitcroft8905a672007-11-28 16:21:06 -08003068# check for adding lines without a newline.
3069 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003070 WARN("MISSING_EOF_NEWLINE",
3071 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003072 }
3073
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003074# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003075 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003076
3077# at the beginning of a line any tabs must come first and anything
3078# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003079 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3080 $rawline =~ /^\+\s* \s*/) {
3081 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003082 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003083 if (ERROR("CODE_INDENT",
3084 "code indent should use tabs where possible\n" . $herevet) &&
3085 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003086 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003087 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003088 }
3089
Alberto Panizzo08e44362010-03-05 13:43:54 -08003090# check for space before tabs.
3091 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3092 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003093 if (WARN("SPACE_BEFORE_TAB",
3094 "please, no space before tabs\n" . $herevet) &&
3095 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003096 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003097 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003098 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003099 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003100 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003101 }
3102
Joe Perches6a487212018-04-10 16:34:04 -07003103# check for assignments on the start of a line
3104 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3105 CHK("ASSIGNMENT_CONTINUATIONS",
3106 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3107 }
3108
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003109# check for && or || at the start of a line
3110 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3111 CHK("LOGICAL_CONTINUATIONS",
3112 "Logical continuations should be on the previous line\n" . $hereprev);
3113 }
3114
Joe Perchesa91e8992016-05-20 17:04:05 -07003115# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003116 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003117 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003118 my $indent = length($1);
3119 if ($indent % 8) {
3120 if (WARN("TABSTOP",
3121 "Statements should start on a tabstop\n" . $herecurr) &&
3122 $fix) {
3123 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3124 }
3125 }
3126 }
3127
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003128# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003129 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003130 $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 -07003131 $prevline =~ /^\+(\t*)(.*)$/;
3132 my $oldindent = $1;
3133 my $rest = $2;
3134
3135 my $pos = pos_last_openparen($rest);
3136 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003137 $line =~ /^(\+| )([ \t]*)/;
3138 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003139
3140 my $goodtabindent = $oldindent .
3141 "\t" x ($pos / 8) .
3142 " " x ($pos % 8);
3143 my $goodspaceindent = $oldindent . " " x $pos;
3144
3145 if ($newindent ne $goodtabindent &&
3146 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003147
3148 if (CHK("PARENTHESIS_ALIGNMENT",
3149 "Alignment should match open parenthesis\n" . $hereprev) &&
3150 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003151 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003152 s/^\+[ \t]*/\+$goodtabindent/;
3153 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003154 }
3155 }
3156 }
3157
Joe Perches6ab3a972015-04-16 12:44:05 -07003158# check for space after cast like "(int) foo" or "(struct foo) bar"
3159# avoid checking a few false positives:
3160# "sizeof(<type>)" or "__alignof__(<type>)"
3161# function pointer declarations like "(*foo)(int) = bar;"
3162# structure definitions like "(struct foo) { 0 };"
3163# multiline macros that define functions
3164# known attributes or the __attribute__ keyword
3165 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3166 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003167 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003168 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003169 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003170 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003171 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003172 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003173 }
3174
Joe Perches86406b12015-09-09 15:37:41 -07003175# Block comment styles
3176# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003177 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003178 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003179 $rawline =~ /^\+[ \t]*\*/ &&
3180 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003181 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3182 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3183 }
3184
Joe Perches86406b12015-09-09 15:37:41 -07003185# Block comments use * on subsequent lines
3186 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3187 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003188 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003189 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003190 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003191 WARN("BLOCK_COMMENT_STYLE",
3192 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003193 }
3194
Joe Perches86406b12015-09-09 15:37:41 -07003195# Block comments use */ on trailing lines
3196 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003197 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3198 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3199 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003200 WARN("BLOCK_COMMENT_STYLE",
3201 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003202 }
3203
Joe Perches08eb9b82016-10-11 13:51:50 -07003204# Block comment * alignment
3205 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003206 $line =~ /^\+[ \t]*$;/ && #leading comment
3207 $rawline =~ /^\+[ \t]*\*/ && #leading *
3208 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003209 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003210 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3211 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003212 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003213 if (defined($1)) {
3214 $oldindent = expand_tabs($1);
3215 } else {
3216 $prevrawline =~ m@^\+(.*/?)\*@;
3217 $oldindent = expand_tabs($1);
3218 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003219 $rawline =~ m@^\+([ \t]*)\*@;
3220 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003221 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003222 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003223 WARN("BLOCK_COMMENT_STYLE",
3224 "Block comments should align the * on each line\n" . $hereprev);
3225 }
3226 }
3227
Joe Perches7f619192014-08-06 16:10:39 -07003228# check for missing blank lines after struct/union declarations
3229# with exceptions for various attributes and macros
3230 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3231 $line =~ /^\+/ &&
3232 !($line =~ /^\+\s*$/ ||
3233 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3234 $line =~ /^\+\s*MODULE_/i ||
3235 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3236 $line =~ /^\+[a-z_]*init/ ||
3237 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3238 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003239 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003240 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003241 if (CHK("LINE_SPACING",
3242 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3243 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003244 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003245 }
Joe Perches7f619192014-08-06 16:10:39 -07003246 }
3247
Joe Perches365dd4e2014-08-06 16:10:42 -07003248# check for multiple consecutive blank lines
3249 if ($prevline =~ /^[\+ ]\s*$/ &&
3250 $line =~ /^\+\s*$/ &&
3251 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003252 if (CHK("LINE_SPACING",
3253 "Please don't use multiple blank lines\n" . $hereprev) &&
3254 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003255 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003256 }
3257
Joe Perches365dd4e2014-08-06 16:10:42 -07003258 $last_blank_line = $linenr;
3259 }
3260
Joe Perches3b617e32014-04-03 14:49:28 -07003261# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003262 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3263 # actual declarations
3264 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003265 # function pointer declarations
3266 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003267 # foo bar; where foo is some local typedef or #define
3268 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3269 # known declaration macros
3270 $prevline =~ /^\+\s+$declaration_macros/) &&
3271 # for "else if" which can look like "$Ident $Ident"
3272 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3273 # other possible extensions of declaration lines
3274 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3275 # not starting a section or a macro "\" extended line
3276 $prevline =~ /(?:\{\s*|\\)$/) &&
3277 # looks like a declaration
3278 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003279 # function pointer declarations
3280 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003281 # foo bar; where foo is some local typedef or #define
3282 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3283 # known declaration macros
3284 $sline =~ /^\+\s+$declaration_macros/ ||
3285 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003286 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003287 # start or end of block or continuation of declaration
3288 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3289 # bitfield continuation
3290 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3291 # other possible extensions of declaration lines
3292 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3293 # indentation of previous and current line are the same
3294 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003295 if (WARN("LINE_SPACING",
3296 "Missing a blank line after declarations\n" . $hereprev) &&
3297 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003298 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003299 }
Joe Perches3b617e32014-04-03 14:49:28 -07003300 }
3301
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003302# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003303# Exceptions:
3304# 1) within comments
3305# 2) indented preprocessor commands
3306# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003307 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003308 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003309 if (WARN("LEADING_SPACE",
3310 "please, no spaces at the start of a line\n" . $herevet) &&
3311 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003312 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003313 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003314 }
3315
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003316# check we are in a valid C source file if not then ignore this hunk
3317 next if ($realfile !~ /\.(h|c)$/);
3318
Joe Perches5751a242017-11-17 15:28:52 -08003319# check for unusual line ending [ or (
3320 if ($line =~ /^\+.*([\[\(])\s*$/) {
3321 CHK("OPEN_ENDED_LINE",
3322 "Lines should not end with a '$1'\n" . $herecurr);
3323 }
3324
Joe Perches4dbed762017-05-08 15:55:39 -07003325# check if this appears to be the start function declaration, save the name
3326 if ($sline =~ /^\+\{\s*$/ &&
3327 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3328 $context_function = $1;
3329 }
3330
3331# check if this appears to be the end of function declaration
3332 if ($sline =~ /^\+\}\s*$/) {
3333 undef $context_function;
3334 }
3335
Joe Perches032a4c02014-08-06 16:10:29 -07003336# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003337# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003338# if the previous line is a break or return and is indented 1 tab more...
3339 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3340 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003341 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3342 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3343 defined $lines[$linenr] &&
3344 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003345 WARN("UNNECESSARY_ELSE",
3346 "else is not generally useful after a break or return\n" . $hereprev);
3347 }
3348 }
3349
Joe Perchesc00df192014-08-06 16:11:01 -07003350# check indentation of a line with a break;
3351# if the previous line is a goto or return and is indented the same # of tabs
3352 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3353 my $tabs = $1;
3354 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3355 WARN("UNNECESSARY_BREAK",
3356 "break is not useful after a goto or return\n" . $hereprev);
3357 }
3358 }
3359
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003360# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003361 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003362 WARN("CVS_KEYWORD",
3363 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003364 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003365
Joe Perches56e77d72013-02-21 16:44:14 -08003366# check for old HOTPLUG __dev<foo> section markings
3367 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3368 WARN("HOTPLUG_SECTION",
3369 "Using $1 is unnecessary\n" . $herecurr);
3370 }
3371
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003372# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003373 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3374 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003375#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003376 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003377 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003378 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003379 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003380 $stat =~ s/\n./\n /g;
3381 $cond =~ s/\n./\n /g;
3382
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003383#print "linenr<$linenr> <$stat>\n";
3384 # If this statement has no statement boundaries within
3385 # it there is no point in retrying a statement scan
3386 # until we hit end of it.
3387 my $frag = $stat; $frag =~ s/;+\s*$//;
3388 if ($frag !~ /(?:{|;)/) {
3389#print "skip<$line_nr_next>\n";
3390 $suppress_statement = $line_nr_next;
3391 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003392
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003393 # Find the real next line.
3394 $realline_next = $line_nr_next;
3395 if (defined $realline_next &&
3396 (!defined $lines[$realline_next - 1] ||
3397 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3398 $realline_next++;
3399 }
3400
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003401 my $s = $stat;
3402 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003403
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003404 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003405 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003406
3407 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003408 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003409
Andy Whitcroft463f2862009-09-21 17:04:34 -07003410 } elsif ($s =~ /^.\s*else\b/s) {
3411
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003412 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003413 } 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 -07003414 my $type = $1;
3415 $type =~ s/\s+/ /g;
3416 possible($type, "A:" . $s);
3417
Andy Whitcroft8905a672007-11-28 16:21:06 -08003418 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003419 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003420 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003421 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003422
3423 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003424 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003425 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003426 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003427
3428 # Check for any sort of function declaration.
3429 # int foo(something bar, other baz);
3430 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003431 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 -08003432 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003433
Andy Whitcroftcf655042008-03-04 14:28:20 -08003434 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003435 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003436 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003437
Andy Whitcroft8905a672007-11-28 16:21:06 -08003438 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003439 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003440
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003441 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003442 }
3443 }
3444 }
3445
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003446 }
3447
Andy Whitcroft653d4872007-06-23 17:16:34 -07003448#
3449# Checks which may be anchored in the context.
3450#
3451
3452# Check for switch () and associated case and default
3453# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003454 if ($line=~/\bswitch\s*\(.*\)/) {
3455 my $err = '';
3456 my $sep = '';
3457 my @ctx = ctx_block_outer($linenr, $realcnt);
3458 shift(@ctx);
3459 for my $ctx (@ctx) {
3460 my ($clen, $cindent) = line_stats($ctx);
3461 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3462 $indent != $cindent) {
3463 $err .= "$sep$ctx\n";
3464 $sep = '';
3465 } else {
3466 $sep = "[...]\n";
3467 }
3468 }
3469 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003470 ERROR("SWITCH_CASE_INDENT_LEVEL",
3471 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003472 }
3473 }
3474
3475# if/while/etc brace do not go on next line, unless defining a do while loop,
3476# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003477 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 -07003478 my $pre_ctx = "$1$2";
3479
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003480 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003481
3482 if ($line =~ /^\+\t{6,}/) {
3483 WARN("DEEP_INDENTATION",
3484 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3485 }
3486
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003487 my $ctx_cnt = $realcnt - $#ctx - 1;
3488 my $ctx = join("\n", @ctx);
3489
Andy Whitcroft548596d2008-07-23 21:29:01 -07003490 my $ctx_ln = $linenr;
3491 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003492
Andy Whitcroft548596d2008-07-23 21:29:01 -07003493 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3494 defined $lines[$ctx_ln - 1] &&
3495 $lines[$ctx_ln - 1] =~ /^-/)) {
3496 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3497 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003498 $ctx_ln++;
3499 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003500
Andy Whitcroft53210162008-07-23 21:29:03 -07003501 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3502 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003503
Joe Perchesd752fcc2014-08-06 16:11:05 -07003504 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003505 ERROR("OPEN_BRACE",
3506 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003507 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003508 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003509 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3510 $ctx =~ /\)\s*\;\s*$/ &&
3511 defined $lines[$ctx_ln - 1])
3512 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003513 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3514 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003515 WARN("TRAILING_SEMICOLON",
3516 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003517 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003518 }
3519 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003520 }
3521
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003522# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003523 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 -08003524 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3525 ctx_statement_block($linenr, $realcnt, 0)
3526 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003527 my ($s, $c) = ($stat, $cond);
3528
3529 substr($s, 0, length($c), '');
3530
Joe Perches9f5af482015-09-09 15:37:30 -07003531 # remove inline comments
3532 $s =~ s/$;/ /g;
3533 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003534
3535 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003536 my @newlines = ($c =~ /\n/gs);
3537 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003538
Joe Perches9f5af482015-09-09 15:37:30 -07003539 # Make sure we remove the line prefixes as we have
3540 # none on the first line, and are going to readd them
3541 # where necessary.
3542 $s =~ s/\n./\n/gs;
3543 while ($s =~ /\n\s+\\\n/) {
3544 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3545 }
3546
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003547 # We want to check the first line inside the block
3548 # starting at the end of the conditional, so remove:
3549 # 1) any blank line termination
3550 # 2) any opening brace { on end of the line
3551 # 3) any do (...) {
3552 my $continuation = 0;
3553 my $check = 0;
3554 $s =~ s/^.*\bdo\b//;
3555 $s =~ s/^\s*{//;
3556 if ($s =~ s/^\s*\\//) {
3557 $continuation = 1;
3558 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003559 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003560 $check = 1;
3561 $cond_lines++;
3562 }
3563
3564 # Also ignore a loop construct at the end of a
3565 # preprocessor statement.
3566 if (($prevline =~ /^.\s*#\s*define\s/ ||
3567 $prevline =~ /\\\s*$/) && $continuation == 0) {
3568 $check = 0;
3569 }
3570
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003571 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003572 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003573 while ($cond_ptr != $cond_lines) {
3574 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003575
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003576 # If we see an #else/#elif then the code
3577 # is not linear.
3578 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3579 $check = 0;
3580 }
3581
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003582 # Ignore:
3583 # 1) blank lines, they should be at 0,
3584 # 2) preprocessor lines, and
3585 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003586 if ($continuation ||
3587 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003588 $s =~ /^\s*#\s*?/ ||
3589 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003590 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003591 if ($s =~ s/^.*?\n//) {
3592 $cond_lines++;
3593 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003594 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003595 }
3596
3597 my (undef, $sindent) = line_stats("+" . $s);
3598 my $stat_real = raw_line($linenr, $cond_lines);
3599
3600 # Check if either of these lines are modified, else
3601 # this is not this patch's fault.
3602 if (!defined($stat_real) ||
3603 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3604 $check = 0;
3605 }
3606 if (defined($stat_real) && $cond_lines > 1) {
3607 $stat_real = "[...]\n$stat_real";
3608 }
3609
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003610 #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 -07003611
Joe Perches9f5af482015-09-09 15:37:30 -07003612 if ($check && $s ne '' &&
3613 (($sindent % 8) != 0 ||
3614 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003615 ($sindent == $indent &&
3616 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003617 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003618 WARN("SUSPECT_CODE_INDENT",
3619 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003620 }
3621 }
3622
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003623 # Track the 'values' across context and added lines.
3624 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003625 my ($curr_values, $curr_vars) =
3626 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003627 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003628 if ($dbg_values) {
3629 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003630 print "$linenr > .$outline\n";
3631 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003632 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003633 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003634 $prev_values = substr($curr_values, -1);
3635
Andy Whitcroft00df3442007-06-08 13:47:06 -07003636#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003637 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003638
Joe Perches11ca40a2016-12-12 16:46:31 -08003639# check for dereferences that span multiple lines
3640 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3641 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3642 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3643 my $ref = $1;
3644 $line =~ /^.\s*($Lval)/;
3645 $ref .= $1;
3646 $ref =~ s/\s//g;
3647 WARN("MULTILINE_DEREFERENCE",
3648 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3649 }
3650
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003651# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003652 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 -07003653 my $type = $1;
3654 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003655 $var = "" if (!defined $var);
3656 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003657 my $sign = $1;
3658 my $pointer = $2;
3659
3660 $pointer = "" if (!defined $pointer);
3661
3662 if (WARN("UNSPECIFIED_INT",
3663 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3664 $fix) {
3665 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003666 my $comp_pointer = $pointer;
3667 $comp_pointer =~ s/\s//g;
3668 $decl .= $comp_pointer;
3669 $decl = rtrim($decl) if ($var eq "");
3670 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003671 }
3672 }
3673 }
3674
Andy Whitcroft653d4872007-06-23 17:16:34 -07003675# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003676 if ($dbg_type) {
3677 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003678 ERROR("TEST_TYPE",
3679 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003680 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003681 ERROR("TEST_NOT_TYPE",
3682 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003683 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003684 next;
3685 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003686# TEST: allow direct testing of the attribute matcher.
3687 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003688 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003689 ERROR("TEST_ATTR",
3690 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003691 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003692 ERROR("TEST_NOT_ATTR",
3693 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003694 }
3695 next;
3696 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003697
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003698# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003699 if ($line =~ /^.\s*{/ &&
3700 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003701 if (ERROR("OPEN_BRACE",
3702 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003703 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3704 fix_delete_line($fixlinenr - 1, $prevrawline);
3705 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003706 my $fixedline = $prevrawline;
3707 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003708 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003709 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003710 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003711 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003712 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003713 }
3714
Andy Whitcroft653d4872007-06-23 17:16:34 -07003715#
3716# Checks which are anchored on the added line.
3717#
3718
3719# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003720 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003721 my $path = $1;
3722 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003723 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003724 "malformed #include filename\n" . $herecurr);
3725 }
3726 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3727 ERROR("UAPI_INCLUDE",
3728 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003729 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003730 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003731
3732# no C99 // comments
3733 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003734 if (ERROR("C99_COMMENTS",
3735 "do not use C99 // comments\n" . $herecurr) &&
3736 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003737 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003738 if ($line =~ /\/\/(.*)$/) {
3739 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003740 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003741 }
3742 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003743 }
3744 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003745 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003746 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003747
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003748# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3749# the whole statement.
3750#print "APW <$lines[$realline_next - 1]>\n";
3751 if (defined $realline_next &&
3752 exists $lines[$realline_next - 1] &&
3753 !defined $suppress_export{$realline_next} &&
3754 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3755 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003756 # Handle definitions which produce identifiers with
3757 # a prefix:
3758 # XXX(foo);
3759 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003760 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003761 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003762 $name =~ /^${Ident}_$2/) {
3763#print "FOO C name<$name>\n";
3764 $suppress_export{$realline_next} = 1;
3765
3766 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003767 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003768 ^.DEFINE_$Ident\(\Q$name\E\)|
3769 ^.DECLARE_$Ident\(\Q$name\E\)|
3770 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003771 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3772 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003773 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003774#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3775 $suppress_export{$realline_next} = 2;
3776 } else {
3777 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003778 }
3779 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003780 if (!defined $suppress_export{$linenr} &&
3781 $prevline =~ /^.\s*$/ &&
3782 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3783 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3784#print "FOO B <$lines[$linenr - 1]>\n";
3785 $suppress_export{$linenr} = 2;
3786 }
3787 if (defined $suppress_export{$linenr} &&
3788 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003789 WARN("EXPORT_SYMBOL",
3790 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003791 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003792
Joe Eloff5150bda2010-08-09 17:21:00 -07003793# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003794 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003795 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003796 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003797 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003798 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003799 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003800 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003801# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003802 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003803 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003804 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003805 $herecurr) &&
3806 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003807 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003808 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003809 }
3810
Joe Perches18130872014-08-06 16:11:22 -07003811# check for misordered declarations of char/short/int/long with signed/unsigned
3812 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3813 my $tmp = trim($1);
3814 WARN("MISORDERED_TYPE",
3815 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3816 }
3817
Joe Perchescb710ec2010-10-26 14:23:20 -07003818# check for static const char * arrays.
3819 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003820 WARN("STATIC_CONST_CHAR_ARRAY",
3821 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003822 $herecurr);
3823 }
3824
3825# check for static char foo[] = "bar" declarations.
3826 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003827 WARN("STATIC_CONST_CHAR_ARRAY",
3828 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003829 $herecurr);
3830 }
3831
Joe Perchesab7e23f2015-04-16 12:44:22 -07003832# check for const <foo> const where <foo> is not a pointer or array type
3833 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3834 my $found = $1;
3835 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3836 WARN("CONST_CONST",
3837 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3838 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3839 WARN("CONST_CONST",
3840 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3841 }
3842 }
3843
Joe Perches9b0fa602014-04-03 14:49:18 -07003844# check for non-global char *foo[] = {"bar", ...} declarations.
3845 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3846 WARN("STATIC_CONST_CHAR_ARRAY",
3847 "char * array declaration might be better as static const\n" .
3848 $herecurr);
3849 }
3850
Joe Perchesb598b672015-04-16 12:44:36 -07003851# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3852 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3853 my $array = $1;
3854 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3855 my $array_div = $1;
3856 if (WARN("ARRAY_SIZE",
3857 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3858 $fix) {
3859 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3860 }
3861 }
3862 }
3863
Joe Perchesb36190c2014-01-27 17:07:18 -08003864# check for function declarations without arguments like "int foo()"
3865 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3866 if (ERROR("FUNCTION_WITHOUT_ARGS",
3867 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3868 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003869 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003870 }
3871 }
3872
Andy Whitcroft653d4872007-06-23 17:16:34 -07003873# check for new typedefs, only function parameters and sparse annotations
3874# make sense.
3875 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003876 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003877 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003878 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003879 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003880 WARN("NEW_TYPEDEFS",
3881 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003882 }
3883
3884# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003885 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003886 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3887 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003888 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003889
Andy Whitcroft65863862009-01-06 14:41:21 -08003890 # Should start with a space.
3891 $to =~ s/^(\S)/ $1/;
3892 # Should not end with a space.
3893 $to =~ s/\s+$//;
3894 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003895 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003896 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003897
Joe Perches3705ce52013-07-03 15:05:31 -07003898## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003899 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003900 if (ERROR("POINTER_LOCATION",
3901 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3902 $fix) {
3903 my $sub_from = $ident;
3904 my $sub_to = $ident;
3905 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003906 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003907 s@\Q$sub_from\E@$sub_to@;
3908 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003909 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003910 }
3911 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3912 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003913 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003914
Andy Whitcroft65863862009-01-06 14:41:21 -08003915 # Should start with a space.
3916 $to =~ s/^(\S)/ $1/;
3917 # Should not end with a space.
3918 $to =~ s/\s+$//;
3919 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003920 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003921 }
3922 # Modifiers should have spaces.
3923 $to =~ s/(\b$Modifier$)/$1 /;
3924
Joe Perches3705ce52013-07-03 15:05:31 -07003925## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003926 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003927 if (ERROR("POINTER_LOCATION",
3928 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3929 $fix) {
3930
3931 my $sub_from = $match;
3932 my $sub_to = $match;
3933 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003934 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003935 s@\Q$sub_from\E@$sub_to@;
3936 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003937 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003938 }
3939
Joe Perches9d3e3c72015-09-09 15:37:27 -07003940# avoid BUG() or BUG_ON()
3941 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07003942 my $msg_level = \&WARN;
3943 $msg_level = \&CHK if ($file);
3944 &{$msg_level}("AVOID_BUG",
3945 "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 -07003946 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003947
Joe Perches9d3e3c72015-09-09 15:37:27 -07003948# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003949 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003950 WARN("LINUX_VERSION_CODE",
3951 "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 -08003952 }
3953
Joe Perches17441222011-06-15 15:08:17 -07003954# check for uses of printk_ratelimit
3955 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003956 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003957 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003958 }
3959
Joe Percheseeef5732017-11-17 15:28:41 -08003960# printk should use KERN_* levels
3961 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
3962 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3963 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003964 }
3965
Joe Perches243f3802012-05-31 16:26:09 -07003966 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3967 my $orig = $1;
3968 my $level = lc($orig);
3969 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003970 my $level2 = $level;
3971 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003972 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003973 "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 -07003974 }
3975
3976 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003977 if (WARN("PREFER_PR_LEVEL",
3978 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3979 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003980 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003981 s/\bpr_warning\b/pr_warn/;
3982 }
Joe Perches243f3802012-05-31 16:26:09 -07003983 }
3984
Joe Perchesdc139312013-02-21 16:44:13 -08003985 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3986 my $orig = $1;
3987 my $level = lc($orig);
3988 $level = "warn" if ($level eq "warning");
3989 $level = "dbg" if ($level eq "debug");
3990 WARN("PREFER_DEV_LEVEL",
3991 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3992 }
3993
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003994# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3995# number of false positives, but assembly files are not checked, so at
3996# least the arch entry code will not trigger this warning.
3997 if ($line =~ /\bENOSYS\b/) {
3998 WARN("ENOSYS",
3999 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4000 }
4001
Andy Whitcroft653d4872007-06-23 17:16:34 -07004002# function brace can't be on same line, except for #defines of do while,
4003# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004004 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004005 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4006 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4007 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004008 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004009 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004010 $fix) {
4011 fix_delete_line($fixlinenr, $rawline);
4012 my $fixed_line = $rawline;
4013 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4014 my $line1 = $1;
4015 my $line2 = $2;
4016 fix_insert_line($fixlinenr, ltrim($line1));
4017 fix_insert_line($fixlinenr, "\+{");
4018 if ($line2 !~ /^\s*$/) {
4019 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4020 }
4021 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004022 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004023
Andy Whitcroft8905a672007-11-28 16:21:06 -08004024# open braces for enum, union and struct go on the same line.
4025 if ($line =~ /^.\s*{/ &&
4026 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004027 if (ERROR("OPEN_BRACE",
4028 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4029 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4030 fix_delete_line($fixlinenr - 1, $prevrawline);
4031 fix_delete_line($fixlinenr, $rawline);
4032 my $fixedline = rtrim($prevrawline) . " {";
4033 fix_insert_line($fixlinenr, $fixedline);
4034 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004035 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004036 if ($fixedline !~ /^\+\s*$/) {
4037 fix_insert_line($fixlinenr, $fixedline);
4038 }
4039 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004040 }
4041
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004042# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004043 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4044 if (WARN("SPACING",
4045 "missing space after $1 definition\n" . $herecurr) &&
4046 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004047 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004048 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4049 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004050 }
4051
Joe Perches31070b52014-01-23 15:54:49 -08004052# Function pointer declarations
4053# check spacing between type, funcptr, and args
4054# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004055 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004056 my $declare = $1;
4057 my $pre_pointer_space = $2;
4058 my $post_pointer_space = $3;
4059 my $funcname = $4;
4060 my $post_funcname_space = $5;
4061 my $pre_args_space = $6;
4062
Joe Perches91f72e92014-04-03 14:49:12 -07004063# the $Declare variable will capture all spaces after the type
4064# so check it for a missing trailing missing space but pointer return types
4065# don't need a space so don't warn for those.
4066 my $post_declare_space = "";
4067 if ($declare =~ /(\s+)$/) {
4068 $post_declare_space = $1;
4069 $declare = rtrim($declare);
4070 }
4071 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004072 WARN("SPACING",
4073 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004074 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004075 }
4076
4077# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004078# This test is not currently implemented because these declarations are
4079# equivalent to
4080# int foo(int bar, ...)
4081# and this is form shouldn't/doesn't generate a checkpatch warning.
4082#
4083# elsif ($declare =~ /\s{2,}$/) {
4084# WARN("SPACING",
4085# "Multiple spaces after return type\n" . $herecurr);
4086# }
Joe Perches31070b52014-01-23 15:54:49 -08004087
4088# unnecessary space "type ( *funcptr)(args...)"
4089 if (defined $pre_pointer_space &&
4090 $pre_pointer_space =~ /^\s/) {
4091 WARN("SPACING",
4092 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4093 }
4094
4095# unnecessary space "type (* funcptr)(args...)"
4096 if (defined $post_pointer_space &&
4097 $post_pointer_space =~ /^\s/) {
4098 WARN("SPACING",
4099 "Unnecessary space before function pointer name\n" . $herecurr);
4100 }
4101
4102# unnecessary space "type (*funcptr )(args...)"
4103 if (defined $post_funcname_space &&
4104 $post_funcname_space =~ /^\s/) {
4105 WARN("SPACING",
4106 "Unnecessary space after function pointer name\n" . $herecurr);
4107 }
4108
4109# unnecessary space "type (*funcptr) (args...)"
4110 if (defined $pre_args_space &&
4111 $pre_args_space =~ /^\s/) {
4112 WARN("SPACING",
4113 "Unnecessary space before function pointer arguments\n" . $herecurr);
4114 }
4115
4116 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004117 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004118 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004119 }
4120 }
4121
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004122# check for spacing round square brackets; allowed:
4123# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004124# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4125# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004126 while ($line =~ /(.*?\s)\[/g) {
4127 my ($where, $prefix) = ($-[1], $1);
4128 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004129 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004130 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004131 if (ERROR("BRACKET_SPACE",
4132 "space prohibited before open square bracket '['\n" . $herecurr) &&
4133 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004134 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004135 s/^(\+.*?)\s+\[/$1\[/;
4136 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004137 }
4138 }
4139
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004140# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004141 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004142 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004143 my $ctx_before = substr($line, 0, $-[1]);
4144 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004145
4146 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004147 if ($name =~ /^(?:
4148 if|for|while|switch|return|case|
4149 volatile|__volatile__|
4150 __attribute__|format|__extension__|
4151 asm|__asm__)$/x)
4152 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004153 # cpp #define statements have non-optional spaces, ie
4154 # if there is a space between the name and the open
4155 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004156 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004157
4158 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004159 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004160
4161 # If this whole things ends with a type its most
4162 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004163 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004164
4165 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004166 if (WARN("SPACING",
4167 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4168 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004169 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004170 s/\b$name\s+\(/$name\(/;
4171 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004172 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004173 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004174
Andy Whitcroft653d4872007-06-23 17:16:34 -07004175# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004176 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004177 my $fixed_line = "";
4178 my $line_fixed = 0;
4179
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004180 my $ops = qr{
4181 <<=|>>=|<=|>=|==|!=|
4182 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4183 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004184 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004185 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004186 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004187 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004188
4189## print("element count: <" . $#elements . ">\n");
4190## foreach my $el (@elements) {
4191## print("el: <$el>\n");
4192## }
4193
4194 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004195 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004196
Joe Perches3705ce52013-07-03 15:05:31 -07004197 foreach my $el (@elements) {
4198 push(@fix_elements, substr($rawline, $off, length($el)));
4199 $off += length($el);
4200 }
4201
4202 $off = 0;
4203
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004204 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004205 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004206
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004207 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004208
4209 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4210
4211## print("n: <$n> good: <$good>\n");
4212
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004213 $off += length($elements[$n]);
4214
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004215 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004216 my $ca = substr($opline, 0, $off);
4217 my $cc = '';
4218 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4219 $cc = substr($opline, $off + length($elements[$n + 1]));
4220 }
4221 my $cb = "$ca$;$cc";
4222
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004223 my $a = '';
4224 $a = 'V' if ($elements[$n] ne '');
4225 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004226 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004227 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4228 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004229 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004230
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004231 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004232
4233 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004234 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004235 $c = 'V' if ($elements[$n + 2] ne '');
4236 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004237 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004238 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4239 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004240 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004241 } else {
4242 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004243 }
4244
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004245 my $ctx = "${a}x${c}";
4246
4247 my $at = "(ctx:$ctx)";
4248
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004249 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004250 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004251
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004252 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004253 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004254
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004255 # Get the full operator variant.
4256 my $opv = $op . substr($curr_vars, $off, 1);
4257
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004258 # Ignore operators passed as parameters.
4259 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004260 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004261
Andy Whitcroftcf655042008-03-04 14:28:20 -08004262# # Ignore comments
4263# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004264
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004265 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004266 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004267 if ($ctx !~ /.x[WEBC]/ &&
4268 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004269 if (ERROR("SPACING",
4270 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004271 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004272 $line_fixed = 1;
4273 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004274 }
4275
4276 # // is a comment
4277 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004278
Joe Perchesb00e4812014-04-03 14:49:33 -07004279 # : when part of a bitfield
4280 } elsif ($opv eq ':B') {
4281 # skip the bitfield test for now
4282
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004283 # No spaces for:
4284 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004285 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004286 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004287 if (ERROR("SPACING",
4288 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004289 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004290 if (defined $fix_elements[$n + 2]) {
4291 $fix_elements[$n + 2] =~ s/^\s+//;
4292 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004293 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004294 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004295 }
4296
Joe Perches23810972014-12-10 15:51:32 -08004297 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004298 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004299 my $rtrim_before = 0;
4300 my $space_after = 0;
4301 if ($ctx =~ /Wx./) {
4302 if (ERROR("SPACING",
4303 "space prohibited before that '$op' $at\n" . $hereptr)) {
4304 $line_fixed = 1;
4305 $rtrim_before = 1;
4306 }
4307 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004308 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004309 if (ERROR("SPACING",
4310 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004311 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004312 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004313 $space_after = 1;
4314 }
4315 }
4316 if ($rtrim_before || $space_after) {
4317 if ($rtrim_before) {
4318 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4319 } else {
4320 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4321 }
4322 if ($space_after) {
4323 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004324 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004325 }
4326
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004327 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004328 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004329 #warn "'*' is part of type\n";
4330
4331 # unary operators should have a space before and
4332 # none after. May be left adjacent to another
4333 # unary operator, or a cast
4334 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004335 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004336 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004337 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004338 if (ERROR("SPACING",
4339 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004340 if ($n != $last_after + 2) {
4341 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4342 $line_fixed = 1;
4343 }
Joe Perches3705ce52013-07-03 15:05:31 -07004344 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004345 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004346 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004347 # A unary '*' may be const
4348
4349 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004350 if (ERROR("SPACING",
4351 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004352 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004353 if (defined $fix_elements[$n + 2]) {
4354 $fix_elements[$n + 2] =~ s/^\s+//;
4355 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004356 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004357 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004358 }
4359
4360 # unary ++ and unary -- are allowed no space on one side.
4361 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004362 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004363 if (ERROR("SPACING",
4364 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004365 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004366 $line_fixed = 1;
4367 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004368 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004369 if ($ctx =~ /Wx[BE]/ ||
4370 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004371 if (ERROR("SPACING",
4372 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004373 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004374 $line_fixed = 1;
4375 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004376 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004377 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004378 if (ERROR("SPACING",
4379 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004380 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004381 if (defined $fix_elements[$n + 2]) {
4382 $fix_elements[$n + 2] =~ s/^\s+//;
4383 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004384 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004385 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004386 }
4387
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004388 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004389 } elsif ($op eq '<<' or $op eq '>>' or
4390 $op eq '&' or $op eq '^' or $op eq '|' or
4391 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004392 $op eq '*' or $op eq '/' or
4393 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004394 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004395 if ($check) {
4396 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4397 if (CHK("SPACING",
4398 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4399 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4400 $fix_elements[$n + 2] =~ s/^\s+//;
4401 $line_fixed = 1;
4402 }
4403 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4404 if (CHK("SPACING",
4405 "space preferred before that '$op' $at\n" . $hereptr)) {
4406 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4407 $line_fixed = 1;
4408 }
4409 }
4410 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004411 if (ERROR("SPACING",
4412 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004413 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4414 if (defined $fix_elements[$n + 2]) {
4415 $fix_elements[$n + 2] =~ s/^\s+//;
4416 }
Joe Perches3705ce52013-07-03 15:05:31 -07004417 $line_fixed = 1;
4418 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004419 }
4420
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004421 # A colon needs no spaces before when it is
4422 # terminating a case value or a label.
4423 } elsif ($opv eq ':C' || $opv eq ':L') {
4424 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004425 if (ERROR("SPACING",
4426 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004427 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004428 $line_fixed = 1;
4429 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004430 }
4431
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004432 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004433 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004434 my $ok = 0;
4435
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004436 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004437 if (($op eq '<' &&
4438 $cc =~ /^\S+\@\S+>/) ||
4439 ($op eq '>' &&
4440 $ca =~ /<\S+\@\S+$/))
4441 {
4442 $ok = 1;
4443 }
4444
Joe Perchese0df7e12015-04-16 12:44:53 -07004445 # for asm volatile statements
4446 # ignore a colon with another
4447 # colon immediately before or after
4448 if (($op eq ':') &&
4449 ($ca =~ /:$/ || $cc =~ /^:/)) {
4450 $ok = 1;
4451 }
4452
Joe Perches84731622013-11-12 15:10:05 -08004453 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004454 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004455 my $msg_level = \&ERROR;
4456 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004457
Jean Delvare0675a8f2017-09-08 16:16:07 -07004458 if (&{$msg_level}("SPACING",
4459 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004460 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4461 if (defined $fix_elements[$n + 2]) {
4462 $fix_elements[$n + 2] =~ s/^\s+//;
4463 }
Joe Perches3705ce52013-07-03 15:05:31 -07004464 $line_fixed = 1;
4465 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004466 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004467 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004468 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004469
4470## print("n: <$n> GOOD: <$good>\n");
4471
4472 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004473 }
Joe Perches3705ce52013-07-03 15:05:31 -07004474
4475 if (($#elements % 2) == 0) {
4476 $fixed_line = $fixed_line . $fix_elements[$#elements];
4477 }
4478
Joe Perches194f66f2014-08-06 16:11:03 -07004479 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4480 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004481 }
4482
4483
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004484 }
4485
Joe Perches786b6322013-07-03 15:05:32 -07004486# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004487 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004488 if (WARN("SPACING",
4489 "space prohibited before semicolon\n" . $herecurr) &&
4490 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004491 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004492 s/^(\+.*\S)\s+;/$1;/;
4493 }
4494 }
4495
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004496# check for multiple assignments
4497 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004498 CHK("MULTIPLE_ASSIGNMENTS",
4499 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004500 }
4501
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004502## # check for multiple declarations, allowing for a function declaration
4503## # continuation.
4504## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4505## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4506##
4507## # Remove any bracketed sections to ensure we do not
4508## # falsly report the parameters of functions.
4509## my $ln = $line;
4510## while ($ln =~ s/\([^\(\)]*\)//g) {
4511## }
4512## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004513## WARN("MULTIPLE_DECLARATION",
4514## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004515## }
4516## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004517
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004518#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004519 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004520 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004521 if (ERROR("SPACING",
4522 "space required before the open brace '{'\n" . $herecurr) &&
4523 $fix) {
Cyril Bur8d81ae02017-07-10 15:52:21 -07004524 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004525 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004526 }
4527
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004528## # check for blank lines before declarations
4529## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4530## $prevrawline =~ /^.\s*$/) {
4531## WARN("SPACING",
4532## "No blank lines before declarations\n" . $hereprev);
4533## }
4534##
4535
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004536# closing brace should have a space following it when it has anything
4537# on the line
4538 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004539 if (ERROR("SPACING",
4540 "space required after that close brace '}'\n" . $herecurr) &&
4541 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004542 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004543 s/}((?!(?:,|;|\)))\S)/} $1/;
4544 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004545 }
4546
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004547# check spacing on square brackets
4548 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004549 if (ERROR("SPACING",
4550 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4551 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004552 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004553 s/\[\s+/\[/;
4554 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004555 }
4556 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004557 if (ERROR("SPACING",
4558 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4559 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004560 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004561 s/\s+\]/\]/;
4562 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004563 }
4564
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004565# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004566 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4567 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004568 if (ERROR("SPACING",
4569 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4570 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004571 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004572 s/\(\s+/\(/;
4573 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004574 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004575 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004576 $line !~ /for\s*\(.*;\s+\)/ &&
4577 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004578 if (ERROR("SPACING",
4579 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4580 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004581 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004582 s/\s+\)/\)/;
4583 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004584 }
4585
Joe Perchese2826fd2014-08-06 16:10:48 -07004586# check unnecessary parentheses around addressof/dereference single $Lvals
4587# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4588
4589 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004590 my $var = $1;
4591 if (CHK("UNNECESSARY_PARENTHESES",
4592 "Unnecessary parentheses around $var\n" . $herecurr) &&
4593 $fix) {
4594 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4595 }
4596 }
4597
4598# check for unnecessary parentheses around function pointer uses
4599# ie: (foo->bar)(); should be foo->bar();
4600# but not "if (foo->bar) (" to avoid some false positives
4601 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4602 my $var = $2;
4603 if (CHK("UNNECESSARY_PARENTHESES",
4604 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4605 $fix) {
4606 my $var2 = deparenthesize($var);
4607 $var2 =~ s/\s//g;
4608 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4609 }
4610 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004611
Joe Perches63b7c732017-09-08 16:16:01 -07004612# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004613# when !drivers/staging or command-line uses --strict
4614 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004615 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004616 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4617 my $if_stat = $1;
4618 my $test = substr($2, 1, -1);
4619 my $herectx;
4620 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4621 my $match = $1;
4622 # avoid parentheses around potential macro args
4623 next if ($match =~ /^\s*\w+\s*$/);
4624 if (!defined($herectx)) {
4625 $herectx = $here . "\n";
4626 my $cnt = statement_rawlines($if_stat);
4627 for (my $n = 0; $n < $cnt; $n++) {
4628 my $rl = raw_line($linenr, $n);
4629 $herectx .= $rl . "\n";
4630 last if $rl =~ /^[ \+].*\{/;
4631 }
4632 }
4633 CHK("UNNECESSARY_PARENTHESES",
4634 "Unnecessary parentheses around '$match'\n" . $herectx);
4635 }
4636 }
4637
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004638#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004639 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004640 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004641 if (WARN("INDENTED_LABEL",
4642 "labels should not be indented\n" . $herecurr) &&
4643 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004644 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004645 s/^(.)\s+/$1/;
4646 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004647 }
4648
Joe Perches5b9553a2014-04-03 14:49:21 -07004649# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004650 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004651 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004652 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004653 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4654 my $value = $1;
4655 $value = deparenthesize($value);
4656 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4657 ERROR("RETURN_PARENTHESES",
4658 "return is not a function, parentheses are not required\n" . $herecurr);
4659 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004660 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004661 ERROR("SPACING",
4662 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004663 }
4664 }
Joe Perches507e5142013-11-12 15:10:13 -08004665
Joe Perchesb43ae212014-06-23 13:22:07 -07004666# unnecessary return in a void function
4667# at end-of-function, with the previous line a single leading tab, then return;
4668# and the line before that not a goto label target like "out:"
4669 if ($sline =~ /^[ \+]}\s*$/ &&
4670 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4671 $linenr >= 3 &&
4672 $lines[$linenr - 3] =~ /^[ +]/ &&
4673 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004674 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004675 "void function return statements are not generally useful\n" . $hereprev);
4676 }
Joe Perches9819cf22014-06-04 16:12:09 -07004677
Joe Perches189248d2014-01-23 15:54:47 -08004678# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004679 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004680 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4681 my $openparens = $1;
4682 my $count = $openparens =~ tr@\(@\(@;
4683 my $msg = "";
4684 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4685 my $comp = $4; #Not $1 because of $LvalOrFunc
4686 $msg = " - maybe == should be = ?" if ($comp eq "==");
4687 WARN("UNNECESSARY_PARENTHESES",
4688 "Unnecessary parentheses$msg\n" . $herecurr);
4689 }
4690 }
4691
Joe Perchesc5595fa2015-09-09 15:37:58 -07004692# comparisons with a constant or upper case identifier on the left
4693# avoid cases like "foo + BAR < baz"
4694# only fix matches surrounded by parentheses to avoid incorrect
4695# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004696 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004697 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4698 my $lead = $1;
4699 my $const = $2;
4700 my $comp = $3;
4701 my $to = $4;
4702 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004703 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004704 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4705 WARN("CONSTANT_COMPARISON",
4706 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4707 $fix) {
4708 if ($comp eq "<") {
4709 $newcomp = ">";
4710 } elsif ($comp eq "<=") {
4711 $newcomp = ">=";
4712 } elsif ($comp eq ">") {
4713 $newcomp = "<";
4714 } elsif ($comp eq ">=") {
4715 $newcomp = "<=";
4716 }
4717 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4718 }
4719 }
4720
Joe Perchesf34e4a42015-04-16 12:44:19 -07004721# Return of what appears to be an errno should normally be negative
4722 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004723 my $name = $1;
4724 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004725 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004726 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004727 }
4728 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004729
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004730# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004731 if ($line =~ /\b(if|while|for|switch)\(/) {
4732 if (ERROR("SPACING",
4733 "space required before the open parenthesis '('\n" . $herecurr) &&
4734 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004735 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004736 s/\b(if|while|for|switch)\(/$1 \(/;
4737 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004738 }
4739
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004740# Check for illegal assignment in if conditional -- and check for trailing
4741# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004742 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004743 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4744 ctx_statement_block($linenr, $realcnt, 0)
4745 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004746 my ($stat_next) = ctx_statement_block($line_nr_next,
4747 $remain_next, $off_next);
4748 $stat_next =~ s/\n./\n /g;
4749 ##print "stat<$stat> stat_next<$stat_next>\n";
4750
4751 if ($stat_next =~ /^\s*while\b/) {
4752 # If the statement carries leading newlines,
4753 # then count those as offsets.
4754 my ($whitespace) =
4755 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4756 my $offset =
4757 statement_rawlines($whitespace) - 1;
4758
4759 $suppress_whiletrailers{$line_nr_next +
4760 $offset} = 1;
4761 }
4762 }
4763 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004764 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004765 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004766 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004767
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004768 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004769 ERROR("ASSIGN_IN_IF",
4770 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004771 }
4772
4773 # Find out what is on the end of the line after the
4774 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004775 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004776 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004777 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004778 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4779 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004780 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004781 # Find out how long the conditional actually is.
4782 my @newlines = ($c =~ /\n/gs);
4783 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004784 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004785
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004786 $stat_real = raw_line($linenr, $cond_lines)
4787 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004788 if (defined($stat_real) && $cond_lines > 1) {
4789 $stat_real = "[...]\n$stat_real";
4790 }
4791
Joe Perches000d1cc12011-07-25 17:13:25 -07004792 ERROR("TRAILING_STATEMENTS",
4793 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004794 }
4795 }
4796
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004797# Check for bitwise tests written as boolean
4798 if ($line =~ /
4799 (?:
4800 (?:\[|\(|\&\&|\|\|)
4801 \s*0[xX][0-9]+\s*
4802 (?:\&\&|\|\|)
4803 |
4804 (?:\&\&|\|\|)
4805 \s*0[xX][0-9]+\s*
4806 (?:\&\&|\|\||\)|\])
4807 )/x)
4808 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004809 WARN("HEXADECIMAL_BOOLEAN_TEST",
4810 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004811 }
4812
Andy Whitcroft8905a672007-11-28 16:21:06 -08004813# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004814 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4815 my $s = $1;
4816 $s =~ s/$;//g; # Remove any comments
4817 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004818 ERROR("TRAILING_STATEMENTS",
4819 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004820 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004821 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004822# if should not continue a brace
4823 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004824 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004825 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004826 $herecurr);
4827 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004828# case and default should not have general statements after them
4829 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4830 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004831 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004832 \s*return\s+
4833 )/xg)
4834 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004835 ERROR("TRAILING_STATEMENTS",
4836 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004837 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004838
4839 # Check for }<nl>else {, these must be at the same
4840 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004841 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4842 $previndent == $indent) {
4843 if (ERROR("ELSE_AFTER_BRACE",
4844 "else should follow close brace '}'\n" . $hereprev) &&
4845 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4846 fix_delete_line($fixlinenr - 1, $prevrawline);
4847 fix_delete_line($fixlinenr, $rawline);
4848 my $fixedline = $prevrawline;
4849 $fixedline =~ s/}\s*$//;
4850 if ($fixedline !~ /^\+\s*$/) {
4851 fix_insert_line($fixlinenr, $fixedline);
4852 }
4853 $fixedline = $rawline;
4854 $fixedline =~ s/^(.\s*)else/$1} else/;
4855 fix_insert_line($fixlinenr, $fixedline);
4856 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004857 }
4858
Joe Perches8b8856f2014-08-06 16:11:14 -07004859 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4860 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004861 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4862
4863 # Find out what is on the end of the line after the
4864 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004865 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004866 $s =~ s/\n.*//g;
4867
4868 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004869 if (ERROR("WHILE_AFTER_BRACE",
4870 "while should follow close brace '}'\n" . $hereprev) &&
4871 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4872 fix_delete_line($fixlinenr - 1, $prevrawline);
4873 fix_delete_line($fixlinenr, $rawline);
4874 my $fixedline = $prevrawline;
4875 my $trailing = $rawline;
4876 $trailing =~ s/^\+//;
4877 $trailing = trim($trailing);
4878 $fixedline =~ s/}\s*$/} $trailing/;
4879 fix_insert_line($fixlinenr, $fixedline);
4880 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004881 }
4882 }
4883
Joe Perches95e2c602013-07-03 15:05:20 -07004884#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004885 while ($line =~ m{($Constant|$Lval)}g) {
4886 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004887
4888#gcc binary extension
4889 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004890 if (WARN("GCC_BINARY_CONSTANT",
4891 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4892 $fix) {
4893 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004894 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004895 s/\b$var\b/$hexval/;
4896 }
Joe Perches95e2c602013-07-03 15:05:20 -07004897 }
4898
4899#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004900 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004901 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004902#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004903 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004904#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004905 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4906#Ignore some three character SI units explicitly, like MiB and KHz
4907 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004908 while ($var =~ m{($Ident)}g) {
4909 my $word = $1;
4910 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004911 if ($check) {
4912 seed_camelcase_includes();
4913 if (!$file && !$camelcase_file_seeded) {
4914 seed_camelcase_file($realfile);
4915 $camelcase_file_seeded = 1;
4916 }
4917 }
Joe Perches7e781f62013-09-11 14:23:55 -07004918 if (!defined $camelcase{$word}) {
4919 $camelcase{$word} = 1;
4920 CHK("CAMELCASE",
4921 "Avoid CamelCase: <$word>\n" . $herecurr);
4922 }
Joe Perches34456862013-07-03 15:05:34 -07004923 }
Joe Perches323c1262012-12-17 16:02:07 -08004924 }
4925 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004926
4927#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004928 if ($line =~ /\#\s*define.*\\\s+$/) {
4929 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4930 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4931 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004932 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004933 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004934 }
4935
Fabian Frederick0e212e02015-04-16 12:44:25 -07004936# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4937# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004938 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004939 my $file = "$1.h";
4940 my $checkfile = "include/linux/$file";
4941 if (-f "$root/$checkfile" &&
4942 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004943 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004944 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004945 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4946 if ($asminclude > 0) {
4947 if ($realfile =~ m{^arch/}) {
4948 CHK("ARCH_INCLUDE_LINUX",
4949 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4950 } else {
4951 WARN("INCLUDE_LINUX",
4952 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4953 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004954 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004955 }
4956 }
4957
Andy Whitcroft653d4872007-06-23 17:16:34 -07004958# multi-statement macros should be enclosed in a do while loop, grab the
4959# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004960# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004961 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4962 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004963 my $ln = $linenr;
4964 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004965 my ($off, $dstat, $dcond, $rest);
4966 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004967 my $has_flow_statement = 0;
4968 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004969 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004970 ctx_statement_block($linenr, $realcnt, 0);
4971 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004972 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004973 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004974
Joe Perches08a28432014-10-13 15:51:55 -07004975 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004976 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004977
Joe Perchesf59b64b2016-10-11 13:52:08 -07004978 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4979 my $define_args = $1;
4980 my $define_stmt = $dstat;
4981 my @def_args = ();
4982
4983 if (defined $define_args && $define_args ne "") {
4984 $define_args = substr($define_args, 1, length($define_args) - 2);
4985 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07004986 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07004987 @def_args = split(",", $define_args);
4988 }
4989
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004990 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004991 $dstat =~ s/\\\n.//g;
4992 $dstat =~ s/^\s*//s;
4993 $dstat =~ s/\s*$//s;
4994
4995 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004996 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4997 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004998 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004999 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005000 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005001
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005002 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005003 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5004 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005005 {
5006 }
5007
Joe Perches42e15292016-03-15 14:58:01 -07005008 # Make asm volatile uses seem like a generic function
5009 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5010
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005011 my $exceptions = qr{
5012 $Declare|
5013 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005014 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005015 DECLARE_PER_CPU|
5016 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005017 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005018 union|
5019 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005020 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005021 ^\"|\"$|
5022 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005023 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005024 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005025
5026 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005027 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005028 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005029
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005030 if ($dstat ne '' &&
5031 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5032 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005033 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005034 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005035 $dstat !~ /$exceptions/ &&
5036 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005037 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005038 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005039 $dstat !~ /^for\s*$Constant$/ && # for (...)
5040 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5041 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005042 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005043 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005044 {
Joe Perchese7955562017-05-08 15:55:48 -07005045 if ($dstat =~ /^\s*if\b/) {
5046 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5047 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5048 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005049 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5050 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5051 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005052 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005053 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005054 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005055
5056 }
Joe Perches52076492016-10-11 13:52:14 -07005057
5058 # Make $define_stmt single line, comment-free, etc
5059 my @stmt_array = split('\n', $define_stmt);
5060 my $first = 1;
5061 $define_stmt = "";
5062 foreach my $l (@stmt_array) {
5063 $l =~ s/\\$//;
5064 if ($first) {
5065 $define_stmt = $l;
5066 $first = 0;
5067 } elsif ($l =~ /^[\+ ]/) {
5068 $define_stmt .= substr($l, 1);
5069 }
5070 }
5071 $define_stmt =~ s/$;//g;
5072 $define_stmt =~ s/\s+/ /g;
5073 $define_stmt = trim($define_stmt);
5074
Joe Perchesf59b64b2016-10-11 13:52:08 -07005075# check if any macro arguments are reused (ignore '...' and 'type')
5076 foreach my $arg (@def_args) {
5077 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005078 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005079 my $tmp_stmt = $define_stmt;
5080 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5081 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5082 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005083 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005084 if ($use_cnt > 1) {
5085 CHK("MACRO_ARG_REUSE",
5086 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005087 }
5088# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005089 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005090 ((defined($1) && $1 ne ',') ||
5091 (defined($2) && $2 ne ','))) {
5092 CHK("MACRO_ARG_PRECEDENCE",
5093 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005094 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005095 }
Joe Perches5023d342012-12-17 16:01:47 -08005096
Joe Perches08a28432014-10-13 15:51:55 -07005097# check for macros with flow control, but without ## concatenation
5098# ## concatenation is commonly a macro that defines a function so ignore those
5099 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005100 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005101 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005102
Joe Perches08a28432014-10-13 15:51:55 -07005103 WARN("MACRO_WITH_FLOW_CONTROL",
5104 "Macros with flow control statements should be avoided\n" . "$herectx");
5105 }
5106
Joe Perches481eb482012-12-17 16:01:56 -08005107# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005108
5109 } else {
5110 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005111 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5112 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005113 $line =~ /^\+.*\\$/) {
5114 WARN("LINE_CONTINUATIONS",
5115 "Avoid unnecessary line continuations\n" . $herecurr);
5116 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005117 }
5118
Joe Perchesb13edf72012-07-30 14:41:24 -07005119# do {} while (0) macro tests:
5120# single-statement macros do not need to be enclosed in do while (0) loop,
5121# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005122 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005123 $realfile !~ m@/vmlinux.lds.h$@ &&
5124 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5125 my $ln = $linenr;
5126 my $cnt = $realcnt;
5127 my ($off, $dstat, $dcond, $rest);
5128 my $ctx = '';
5129 ($dstat, $dcond, $ln, $cnt, $off) =
5130 ctx_statement_block($linenr, $realcnt, 0);
5131 $ctx = $dstat;
5132
5133 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005134 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005135
5136 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5137 my $stmts = $2;
5138 my $semis = $3;
5139
5140 $ctx =~ s/\n*$//;
5141 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005142 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005143
Joe Perchesac8e97f2012-08-21 16:15:53 -07005144 if (($stmts =~ tr/;/;/) == 1 &&
5145 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005146 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5147 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5148 }
5149 if (defined $semis && $semis ne "") {
5150 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5151 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5152 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005153 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5154 $ctx =~ s/\n*$//;
5155 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005156 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005157
5158 WARN("TRAILING_SEMICOLON",
5159 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005160 }
5161 }
5162
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005163# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005164 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5165 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005166 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005167 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005168 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5169 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005170 my @allowed = ();
5171 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005172 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005173 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005174 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005175 for my $chunk (@chunks) {
5176 my ($cond, $block) = @{$chunk};
5177
Andy Whitcroft773647a2008-03-28 14:15:58 -07005178 # If the condition carries leading newlines, then count those as offsets.
5179 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5180 my $offset = statement_rawlines($whitespace) - 1;
5181
Joe Perchesaad4f612012-03-23 15:02:19 -07005182 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005183 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5184
5185 # We have looked at and allowed this specific line.
5186 $suppress_ifbraces{$ln + $offset} = 1;
5187
5188 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005189 $ln += statement_rawlines($block) - 1;
5190
Andy Whitcroft773647a2008-03-28 14:15:58 -07005191 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005192
5193 $seen++ if ($block =~ /^\s*{/);
5194
Joe Perchesaad4f612012-03-23 15:02:19 -07005195 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005196 if (statement_lines($cond) > 1) {
5197 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005198 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005199 }
5200 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005201 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005202 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005203 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005204 if (statement_block_size($block) > 1) {
5205 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005206 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005207 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005208 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005209 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005210 if ($seen) {
5211 my $sum_allowed = 0;
5212 foreach (@allowed) {
5213 $sum_allowed += $_;
5214 }
5215 if ($sum_allowed == 0) {
5216 WARN("BRACES",
5217 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5218 } elsif ($sum_allowed != $allow &&
5219 $seen != $allow) {
5220 CHK("BRACES",
5221 "braces {} should be used on all arms of this statement\n" . $herectx);
5222 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005223 }
5224 }
5225 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005226 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005227 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005228 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005229
Andy Whitcroftcf655042008-03-04 14:28:20 -08005230 # Check the pre-context.
5231 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5232 #print "APW: ALLOWED: pre<$1>\n";
5233 $allowed = 1;
5234 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005235
5236 my ($level, $endln, @chunks) =
5237 ctx_statement_full($linenr, $realcnt, $-[0]);
5238
Andy Whitcroftcf655042008-03-04 14:28:20 -08005239 # Check the condition.
5240 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005241 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005242 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005243 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005244 }
5245 if (statement_lines($cond) > 1) {
5246 #print "APW: ALLOWED: cond<$cond>\n";
5247 $allowed = 1;
5248 }
5249 if ($block =~/\b(?:if|for|while)\b/) {
5250 #print "APW: ALLOWED: block<$block>\n";
5251 $allowed = 1;
5252 }
5253 if (statement_block_size($block) > 1) {
5254 #print "APW: ALLOWED: lines block<$block>\n";
5255 $allowed = 1;
5256 }
5257 # Check the post-context.
5258 if (defined $chunks[1]) {
5259 my ($cond, $block) = @{$chunks[1]};
5260 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005261 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005262 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005263 if ($block =~ /^\s*\{/) {
5264 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5265 $allowed = 1;
5266 }
5267 }
5268 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005269 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005270 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005271
Joe Perches000d1cc12011-07-25 17:13:25 -07005272 WARN("BRACES",
5273 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005274 }
5275 }
5276
Joe Perchese4c5bab2017-02-24 15:01:41 -08005277# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005278 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5279 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005280 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5281 }
5282
Joe Perches0979ae62012-12-17 16:01:59 -08005283# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005284 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005285 if (CHK("BRACES",
5286 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5287 $fix && $prevrawline =~ /^\+/) {
5288 fix_delete_line($fixlinenr - 1, $prevrawline);
5289 }
Joe Perches0979ae62012-12-17 16:01:59 -08005290 }
Joe Perches77b9a532013-07-03 15:05:29 -07005291 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005292 if (CHK("BRACES",
5293 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5294 $fix) {
5295 fix_delete_line($fixlinenr, $rawline);
5296 }
Joe Perches0979ae62012-12-17 16:01:59 -08005297 }
5298
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005299# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005300 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5301 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005302 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005303 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005304 }
5305
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005306# Check for user-visible strings broken across lines, which breaks the ability
5307# to grep for the string. Make exceptions when the previous string ends in a
5308# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5309# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005310 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005311 $prevline =~ /"\s*$/ &&
5312 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5313 if (WARN("SPLIT_STRING",
5314 "quoted string split across lines\n" . $hereprev) &&
5315 $fix &&
5316 $prevrawline =~ /^\+.*"\s*$/ &&
5317 $last_coalesced_string_linenr != $linenr - 1) {
5318 my $extracted_string = get_quoted_string($line, $rawline);
5319 my $comma_close = "";
5320 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5321 $comma_close = $1;
5322 }
5323
5324 fix_delete_line($fixlinenr - 1, $prevrawline);
5325 fix_delete_line($fixlinenr, $rawline);
5326 my $fixedline = $prevrawline;
5327 $fixedline =~ s/"\s*$//;
5328 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5329 fix_insert_line($fixlinenr - 1, $fixedline);
5330 $fixedline = $rawline;
5331 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5332 if ($fixedline !~ /\+\s*$/) {
5333 fix_insert_line($fixlinenr, $fixedline);
5334 }
5335 $last_coalesced_string_linenr = $linenr;
5336 }
5337 }
5338
5339# check for missing a space in a string concatenation
5340 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5341 WARN('MISSING_SPACE',
5342 "break quoted strings at a space character\n" . $hereprev);
5343 }
5344
Joe Perchese4b7d302017-05-08 15:55:51 -07005345# check for an embedded function name in a string when the function is known
5346# This does not work very well for -f --file checking as it depends on patch
5347# context providing the function name or a single line form for in-file
5348# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005349 if ($line =~ /^\+.*$String/ &&
5350 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005351 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5352 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005353 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005354 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005355 }
5356
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005357# check for spaces before a quoted newline
5358 if ($rawline =~ /^.*\".*\s\\n/) {
5359 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5360 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5361 $fix) {
5362 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5363 }
5364
5365 }
5366
Joe Perchesf17dba42014-10-13 15:51:51 -07005367# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005368 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5369 if (CHK("CONCATENATED_STRING",
5370 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5371 $fix) {
5372 while ($line =~ /($String)/g) {
5373 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5374 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5375 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5376 }
5377 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005378 }
5379
Joe Perches90ad30e2014-12-10 15:51:59 -08005380# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005381 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005382 if (WARN("STRING_FRAGMENTS",
5383 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5384 $fix) {
5385 while ($line =~ /($String)(?=\s*")/g) {
5386 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5387 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5388 }
5389 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005390 }
5391
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005392# check for non-standard and hex prefixed decimal printf formats
5393 my $show_L = 1; #don't show the same defect twice
5394 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005395 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005396 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005397 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005398 # check for %L
5399 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005400 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005401 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5402 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005403 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005404 # check for %Z
5405 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5406 WARN("PRINTF_Z",
5407 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5408 $show_Z = 0;
5409 }
5410 # check for 0x<decimal>
5411 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5412 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005413 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5414 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005415 }
5416
5417# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005418 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005419 WARN("LINE_CONTINUATIONS",
5420 "Avoid line continuations in quoted strings\n" . $herecurr);
5421 }
5422
Andy Whitcroft00df3442007-06-08 13:47:06 -07005423# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005424 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005425 WARN("IF_0",
5426 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5427 }
5428
5429# warn about #if 1
5430 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5431 WARN("IF_1",
5432 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005433 }
5434
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005435# check for needless "if (<foo>) fn(<foo>)" uses
5436 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005437 my $tested = quotemeta($1);
5438 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5439 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5440 my $func = $1;
5441 if (WARN('NEEDLESS_IF',
5442 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5443 $fix) {
5444 my $do_fix = 1;
5445 my $leading_tabs = "";
5446 my $new_leading_tabs = "";
5447 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5448 $leading_tabs = $1;
5449 } else {
5450 $do_fix = 0;
5451 }
5452 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5453 $new_leading_tabs = $1;
5454 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5455 $do_fix = 0;
5456 }
5457 } else {
5458 $do_fix = 0;
5459 }
5460 if ($do_fix) {
5461 fix_delete_line($fixlinenr - 1, $prevrawline);
5462 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5463 }
5464 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005465 }
5466 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005467
Joe Perchesebfdc402014-08-06 16:10:27 -07005468# check for unnecessary "Out of Memory" messages
5469 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5470 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5471 (defined $1 || defined $3) &&
5472 $linenr > 3) {
5473 my $testval = $2;
5474 my $testline = $lines[$linenr - 3];
5475
5476 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5477# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5478
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005479 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005480 WARN("OOM_MESSAGE",
5481 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5482 }
5483 }
5484
Joe Perchesf78d98f2014-10-13 15:52:01 -07005485# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005486 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005487 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5488 my $level = $1;
5489 if (WARN("UNNECESSARY_KERN_LEVEL",
5490 "Possible unnecessary $level\n" . $herecurr) &&
5491 $fix) {
5492 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5493 }
5494 }
5495
Joe Perches45c55e92017-02-24 15:01:31 -08005496# check for logging continuations
5497 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5498 WARN("LOGGING_CONTINUATION",
5499 "Avoid logging continuation uses where feasible\n" . $herecurr);
5500 }
5501
Joe Perchesabb08a52014-12-10 15:51:46 -08005502# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005503 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005504 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5505 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5506 WARN("MASK_THEN_SHIFT",
5507 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5508 }
5509
Joe Perchesb75ac612014-12-10 15:52:02 -08005510# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005511 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005512 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5513 my $val = $1;
5514 my $equal = "!";
5515 $equal = "" if ($4 eq "!=");
5516 if (CHK("COMPARISON_TO_NULL",
5517 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5518 $fix) {
5519 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5520 }
5521 }
5522 }
5523
Joe Perches8716de32013-09-11 14:24:05 -07005524# check for bad placement of section $InitAttribute (e.g.: __initdata)
5525 if ($line =~ /(\b$InitAttribute\b)/) {
5526 my $attr = $1;
5527 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5528 my $ptr = $1;
5529 my $var = $2;
5530 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5531 ERROR("MISPLACED_INIT",
5532 "$attr should be placed after $var\n" . $herecurr)) ||
5533 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5534 WARN("MISPLACED_INIT",
5535 "$attr should be placed after $var\n" . $herecurr))) &&
5536 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005537 $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 -07005538 }
5539 }
5540 }
5541
Joe Perchese970b8842013-11-12 15:10:10 -08005542# check for $InitAttributeData (ie: __initdata) with const
5543 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5544 my $attr = $1;
5545 $attr =~ /($InitAttributePrefix)(.*)/;
5546 my $attr_prefix = $1;
5547 my $attr_type = $2;
5548 if (ERROR("INIT_ATTRIBUTE",
5549 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5550 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005551 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005552 s/$InitAttributeData/${attr_prefix}initconst/;
5553 }
5554 }
5555
5556# check for $InitAttributeConst (ie: __initconst) without const
5557 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5558 my $attr = $1;
5559 if (ERROR("INIT_ATTRIBUTE",
5560 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5561 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005562 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005563 /(^\+\s*(?:static\s+))/;
5564 $lead = rtrim($1);
5565 $lead = "$lead " if ($lead !~ /^\+$/);
5566 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005567 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005568 }
5569 }
5570
Joe Perchesc17893c2015-04-16 12:44:42 -07005571# check for __read_mostly with const non-pointer (should just be const)
5572 if ($line =~ /\b__read_mostly\b/ &&
5573 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5574 if (ERROR("CONST_READ_MOSTLY",
5575 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5576 $fix) {
5577 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5578 }
5579 }
5580
Joe Perchesfbdb8132014-04-03 14:49:14 -07005581# don't use __constant_<foo> functions outside of include/uapi/
5582 if ($realfile !~ m@^include/uapi/@ &&
5583 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5584 my $constant_func = $1;
5585 my $func = $constant_func;
5586 $func =~ s/^__constant_//;
5587 if (WARN("CONSTANT_CONVERSION",
5588 "$constant_func should be $func\n" . $herecurr) &&
5589 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005590 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005591 }
5592 }
5593
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005594# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005595 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005596 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005597 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005598 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005599 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005600 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5601 }
5602 if ($delay > 2000) {
5603 WARN("LONG_UDELAY",
5604 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005605 }
5606 }
5607
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005608# warn about unexpectedly long msleep's
5609 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5610 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005611 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005612 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005613 }
5614 }
5615
Joe Perches36ec1932013-07-03 15:05:25 -07005616# check for comparisons of jiffies
5617 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5618 WARN("JIFFIES_COMPARISON",
5619 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5620 }
5621
Joe Perches9d7a34a2013-07-03 15:05:26 -07005622# check for comparisons of get_jiffies_64()
5623 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5624 WARN("JIFFIES_COMPARISON",
5625 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5626 }
5627
Andy Whitcroft00df3442007-06-08 13:47:06 -07005628# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005629# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005630# print "#ifdef in C files should be avoided\n";
5631# print "$herecurr";
5632# $clean = 0;
5633# }
5634
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005635# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005636 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005637 if (ERROR("SPACING",
5638 "exactly one space required after that #$1\n" . $herecurr) &&
5639 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005640 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005641 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5642 }
5643
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005644 }
5645
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005646# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005647 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5648 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005649 my $which = $1;
5650 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005651 CHK("UNCOMMENTED_DEFINITION",
5652 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005653 }
5654 }
5655# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005656
5657 my $barriers = qr{
5658 mb|
5659 rmb|
5660 wmb|
5661 read_barrier_depends
5662 }x;
5663 my $barrier_stems = qr{
5664 mb__before_atomic|
5665 mb__after_atomic|
5666 store_release|
5667 load_acquire|
5668 store_mb|
5669 (?:$barriers)
5670 }x;
5671 my $all_barriers = qr{
5672 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005673 smp_(?:$barrier_stems)|
5674 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005675 }x;
5676
5677 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005678 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005679 WARN("MEMORY_BARRIER",
5680 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005681 }
5682 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005683
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005684 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5685
5686 if ($realfile !~ m@^include/asm-generic/@ &&
5687 $realfile !~ m@/barrier\.h$@ &&
5688 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5689 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5690 WARN("MEMORY_BARRIER",
5691 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5692 }
5693
Joe Perchescb426e92015-06-25 15:02:46 -07005694# check for waitqueue_active without a comment.
5695 if ($line =~ /\bwaitqueue_active\s*\(/) {
5696 if (!ctx_has_comment($first_line, $linenr)) {
5697 WARN("WAITQUEUE_ACTIVE",
5698 "waitqueue_active without comment\n" . $herecurr);
5699 }
5700 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005701
Paul E. McKenney91db2592017-11-27 09:37:35 -08005702# check for smp_read_barrier_depends and read_barrier_depends
5703 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5704 WARN("READ_BARRIER_DEPENDS",
5705 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5706 }
5707
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005708# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005709 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005710 CHK("ARCH_DEFINES",
5711 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005712 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005713
Joe Perches596ed452017-07-12 14:37:02 -07005714# check that the storage class is not after a type
5715 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005716 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005717 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5718 }
5719# Check that the storage class is at the beginning of a declaration
5720 if ($line =~ /\b$Storage\b/ &&
5721 $line !~ /^.\s*$Storage/ &&
5722 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5723 $1 !~ /[\,\)]\s*$/) {
5724 WARN("STORAGE_CLASS",
5725 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005726 }
5727
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005728# check the location of the inline attribute, that it is between
5729# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005730 if ($line =~ /\b$Type\s+$Inline\b/ ||
5731 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005732 ERROR("INLINE_LOCATION",
5733 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005734 }
5735
Andy Whitcroft8905a672007-11-28 16:21:06 -08005736# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005737 if ($realfile !~ m@\binclude/uapi/@ &&
5738 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005739 if (WARN("INLINE",
5740 "plain inline is preferred over $1\n" . $herecurr) &&
5741 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005742 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005743
5744 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005745 }
5746
Joe Perches3d130fd2011-01-12 17:00:00 -08005747# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005748 if ($realfile !~ m@\binclude/uapi/@ &&
5749 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005750 WARN("PREFER_PACKED",
5751 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005752 }
5753
Joe Perches39b7e282011-07-25 17:13:24 -07005754# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005755 if ($realfile !~ m@\binclude/uapi/@ &&
5756 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005757 WARN("PREFER_ALIGNED",
5758 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005759 }
5760
Joe Perches5f14d3b2012-01-10 15:09:52 -08005761# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005762 if ($realfile !~ m@\binclude/uapi/@ &&
5763 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005764 if (WARN("PREFER_PRINTF",
5765 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5766 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005767 $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 -07005768
5769 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005770 }
5771
Joe Perches6061d942012-03-23 15:02:16 -07005772# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005773 if ($realfile !~ m@\binclude/uapi/@ &&
5774 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005775 if (WARN("PREFER_SCANF",
5776 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5777 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005778 $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 -07005779 }
Joe Perches6061d942012-03-23 15:02:16 -07005780 }
5781
Joe Perches619a9082014-12-10 15:51:35 -08005782# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005783 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005784 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5785 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5786 $line =~ /\b__weak\b/)) {
5787 ERROR("WEAK_DECLARATION",
5788 "Using weak declarations can have unintended link defects\n" . $herecurr);
5789 }
5790
Tomas Winklerfd39f902016-12-12 16:46:34 -08005791# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005792 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005793 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005794 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5795 my $type = $1;
5796 if ($type =~ /\b($typeC99Typedefs)\b/) {
5797 $type = $1;
5798 my $kernel_type = 'u';
5799 $kernel_type = 's' if ($type =~ /^_*[si]/);
5800 $type =~ /(\d+)/;
5801 $kernel_type .= $1;
5802 if (CHK("PREFER_KERNEL_TYPES",
5803 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5804 $fix) {
5805 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5806 }
5807 }
5808 }
5809
Joe Perches938224b2016-01-20 14:59:15 -08005810# check for cast of C90 native int or longer types constants
5811 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5812 my $cast = $1;
5813 my $const = $2;
5814 if (WARN("TYPECAST_INT_CONSTANT",
5815 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5816 $fix) {
5817 my $suffix = "";
5818 my $newconst = $const;
5819 $newconst =~ s/${Int_type}$//;
5820 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5821 if ($cast =~ /\blong\s+long\b/) {
5822 $suffix .= 'LL';
5823 } elsif ($cast =~ /\blong\b/) {
5824 $suffix .= 'L';
5825 }
5826 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5827 }
5828 }
5829
Joe Perches8f53a9b2010-03-05 13:43:48 -08005830# check for sizeof(&)
5831 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005832 WARN("SIZEOF_ADDRESS",
5833 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005834 }
5835
Joe Perches66c80b62012-07-30 14:41:22 -07005836# check for sizeof without parenthesis
5837 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005838 if (WARN("SIZEOF_PARENTHESIS",
5839 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5840 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005841 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005842 }
Joe Perches66c80b62012-07-30 14:41:22 -07005843 }
5844
Joe Perches88982fe2012-12-17 16:02:00 -08005845# check for struct spinlock declarations
5846 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5847 WARN("USE_SPINLOCK_T",
5848 "struct spinlock should be spinlock_t\n" . $herecurr);
5849 }
5850
Joe Perchesa6962d72013-04-29 16:18:13 -07005851# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005852 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005853 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005854 $fmt =~ s/%%//g;
5855 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005856 if (WARN("PREFER_SEQ_PUTS",
5857 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5858 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005859 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005860 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005861 }
5862 }
5863
Joe Perches478b1792018-04-10 16:33:34 -07005864# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07005865 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07005866 defined $stat &&
5867 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5868 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005869 my $stat_real;
5870
Joe Perches0b523762017-05-08 15:55:36 -07005871 my $lc = $stat =~ tr@\n@@;
5872 $lc = $lc + $linenr;
5873 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07005874 my $specifier;
5875 my $extension;
5876 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07005877 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5878 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005879
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005880 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5881 $specifier = $1;
5882 $extension = $2;
5883 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5884 $bad_specifier = $specifier;
5885 last;
5886 }
5887 if ($extension eq "x" && !defined($stat_real)) {
5888 if (!defined($stat_real)) {
5889 $stat_real = get_stat_real($linenr, $lc);
5890 }
5891 WARN("VSPRINTF_SPECIFIER_PX",
5892 "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");
5893 }
5894 }
5895 if ($bad_specifier ne "") {
5896 my $stat_real = get_stat_real($linenr, $lc);
5897 my $ext_type = "Invalid";
5898 my $use = "";
5899 if ($bad_specifier =~ /p[Ff]/) {
5900 $ext_type = "Deprecated";
5901 $use = " - use %pS instead";
5902 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5903 }
5904
5905 WARN("VSPRINTF_POINTER_EXTENSION",
5906 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5907 }
Joe Perches0b523762017-05-08 15:55:36 -07005908 }
5909 }
5910
Andy Whitcroft554e1652012-01-10 15:09:57 -08005911# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07005912 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005913 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005914 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005915
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005916 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005917 my $ms_val = $7;
5918 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005919
Andy Whitcroft554e1652012-01-10 15:09:57 -08005920 if ($ms_size =~ /^(0x|)0$/i) {
5921 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005922 "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 -08005923 } elsif ($ms_size =~ /^(0x|)1$/i) {
5924 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005925 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5926 }
5927 }
5928
Joe Perches98a9bba2014-01-23 15:54:52 -08005929# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005930# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07005931# defined $stat &&
5932# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5933# if (WARN("PREFER_ETHER_ADDR_COPY",
5934# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5935# $fix) {
5936# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5937# }
5938# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005939
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005940# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005941# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07005942# defined $stat &&
5943# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5944# WARN("PREFER_ETHER_ADDR_EQUAL",
5945# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5946# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005947
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005948# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5949# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07005950# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07005951# defined $stat &&
5952# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5953#
5954# my $ms_val = $7;
5955#
5956# if ($ms_val =~ /^(?:0x|)0+$/i) {
5957# if (WARN("PREFER_ETH_ZERO_ADDR",
5958# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5959# $fix) {
5960# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5961# }
5962# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5963# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5964# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5965# $fix) {
5966# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5967# }
5968# }
5969# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005970
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005971# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07005972 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005973 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005974 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005975 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005976 my $call = $1;
5977 my $cast1 = deparenthesize($2);
5978 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005979 my $cast2 = deparenthesize($7);
5980 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005981 my $cast;
5982
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005983 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005984 $cast = "$cast1 or $cast2";
5985 } elsif ($cast1 ne "") {
5986 $cast = $cast1;
5987 } else {
5988 $cast = $cast2;
5989 }
5990 WARN("MINMAX",
5991 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005992 }
5993 }
5994
Joe Perches4a273192012-07-30 14:41:20 -07005995# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07005996 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07005997 defined $stat &&
5998 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5999 my $min = $1;
6000 my $max = $7;
6001 if ($min eq $max) {
6002 WARN("USLEEP_RANGE",
6003 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6004 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6005 $min > $max) {
6006 WARN("USLEEP_RANGE",
6007 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6008 }
6009 }
6010
Joe Perches823b7942013-11-12 15:10:15 -08006011# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006012 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006013 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006014 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006015 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6016 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6017 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6018 my $lc = $stat =~ tr@\n@@;
6019 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006020 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006021 WARN("NAKED_SSCANF",
6022 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6023 }
6024
Joe Perchesafc819a2014-06-04 16:12:08 -07006025# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006026 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006027 defined $stat &&
6028 $line =~ /\bsscanf\b/) {
6029 my $lc = $stat =~ tr@\n@@;
6030 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006031 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006032 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6033 my $format = $6;
6034 my $count = $format =~ tr@%@%@;
6035 if ($count == 1 &&
6036 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6037 WARN("SSCANF_TO_KSTRTO",
6038 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6039 }
6040 }
6041 }
6042
Joe Perches70dc8a42013-09-11 14:23:58 -07006043# check for new externs in .h files.
6044 if ($realfile =~ /\.h$/ &&
6045 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006046 if (CHK("AVOID_EXTERNS",
6047 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006048 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006049 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006050 }
6051 }
6052
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006053# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006054 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006055 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006056 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006057 my $function_name = $1;
6058 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006059
6060 my $s = $stat;
6061 if (defined $cond) {
6062 substr($s, 0, length($cond), '');
6063 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006064 if ($s =~ /^\s*;/ &&
6065 $function_name ne 'uninitialized_var')
6066 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006067 WARN("AVOID_EXTERNS",
6068 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006069 }
6070
6071 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006072 WARN("FUNCTION_ARGUMENTS",
6073 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006074 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006075
6076 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6077 $stat =~ /^.\s*extern\s+/)
6078 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006079 WARN("AVOID_EXTERNS",
6080 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006081 }
6082
Joe Perchesa0ad7592017-07-10 15:52:19 -07006083# check for function declarations that have arguments without identifier names
6084 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006085 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006086 $1 ne "void") {
6087 my $args = trim($1);
6088 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6089 my $arg = trim($1);
6090 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6091 WARN("FUNCTION_ARGUMENTS",
6092 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6093 }
6094 }
6095 }
6096
Joe Perchesa0ad7592017-07-10 15:52:19 -07006097# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006098 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006099 defined $stat &&
6100 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6101 $context_function = $1;
6102
6103# check for multiline function definition with misplaced open brace
6104 my $ok = 0;
6105 my $cnt = statement_rawlines($stat);
6106 my $herectx = $here . "\n";
6107 for (my $n = 0; $n < $cnt; $n++) {
6108 my $rl = raw_line($linenr, $n);
6109 $herectx .= $rl . "\n";
6110 $ok = 1 if ($rl =~ /^[ \+]\{/);
6111 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6112 last if $rl =~ /^[ \+].*\{/;
6113 }
6114 if (!$ok) {
6115 ERROR("OPEN_BRACE",
6116 "open brace '{' following function definitions go on the next line\n" . $herectx);
6117 }
6118 }
6119
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006120# checks for new __setup's
6121 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6122 my $name = $1;
6123
6124 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006125 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006126 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006127 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006128 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006129
6130# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006131 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006132 WARN("UNNECESSARY_CASTS",
6133 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006134 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006135
Joe Perchesa640d252013-07-03 15:05:21 -07006136# alloc style
6137# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006138 if ($perl_version_ok &&
Joe Perchesa640d252013-07-03 15:05:21 -07006139 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6140 CHK("ALLOC_SIZEOF_STRUCT",
6141 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6142 }
6143
Joe Perches60a55362014-06-04 16:12:07 -07006144# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006145 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006146 defined $stat &&
6147 $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 -07006148 my $oldfunc = $3;
6149 my $a1 = $4;
6150 my $a2 = $10;
6151 my $newfunc = "kmalloc_array";
6152 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006153 my $r1 = $a1;
6154 my $r2 = $a2;
6155 if ($a1 =~ /^sizeof\s*\S/) {
6156 $r1 = $a2;
6157 $r2 = $a1;
6158 }
6159 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6160 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006161 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006162 my $herectx = get_stat_here($linenr, $cnt, $here);
6163
Joe Perches60a55362014-06-04 16:12:07 -07006164 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006165 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6166 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006167 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006168 $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 -07006169 }
6170 }
6171 }
6172
Joe Perches972fdea2013-04-29 16:18:12 -07006173# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006174 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006175 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6176 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006177 WARN("KREALLOC_ARG_REUSE",
6178 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6179 }
6180
Joe Perches5ce59ae2013-02-21 16:44:18 -08006181# check for alloc argument mismatch
6182 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6183 WARN("ALLOC_ARRAY_ARGS",
6184 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6185 }
6186
Joe Perchescaf2a542011-01-12 16:59:56 -08006187# check for multiple semicolons
6188 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006189 if (WARN("ONE_SEMICOLON",
6190 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6191 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006192 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006193 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006194 }
6195
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006196# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6197 if ($realfile !~ m@^include/uapi/@ &&
6198 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006199 my $ull = "";
6200 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6201 if (CHK("BIT_MACRO",
6202 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6203 $fix) {
6204 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6205 }
6206 }
6207
Joe Perches2d632742016-05-20 17:04:00 -07006208# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6209 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6210 my $config = $1;
6211 if (WARN("PREFER_IS_ENABLED",
6212 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6213 $fix) {
6214 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6215 }
6216 }
6217
Joe Perchese81f2392014-08-06 16:11:25 -07006218# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006219 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6220 my $has_break = 0;
6221 my $has_statement = 0;
6222 my $count = 0;
6223 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006224 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006225 $prevline--;
6226 my $rline = $rawlines[$prevline - 1];
6227 my $fline = $lines[$prevline - 1];
6228 last if ($fline =~ /^\@\@/);
6229 next if ($fline =~ /^\-/);
6230 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6231 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6232 next if ($fline =~ /^.[\s$;]*$/);
6233 $has_statement = 1;
6234 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006235 $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 -08006236 }
6237 if (!$has_break && $has_statement) {
6238 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006239 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006240 }
6241 }
6242
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006243# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006244 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006245 defined $stat &&
6246 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006247 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006248 my $herectx = get_stat_here($linenr, $cnt, $here);
6249
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006250 WARN("DEFAULT_NO_BREAK",
6251 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006252 }
6253
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006254# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006255 if ($line =~ /\b__FUNCTION__\b/) {
6256 if (WARN("USE_FUNC",
6257 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6258 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006259 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006260 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006261 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006262
Joe Perches62ec8182015-02-13 14:38:18 -08006263# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6264 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6265 ERROR("DATE_TIME",
6266 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6267 }
6268
Joe Perches2c924882012-03-23 15:02:20 -07006269# check for use of yield()
6270 if ($line =~ /\byield\s*\(\s*\)/) {
6271 WARN("YIELD",
6272 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6273 }
6274
Joe Perches179f8f42013-07-03 15:05:30 -07006275# check for comparisons against true and false
6276 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6277 my $lead = $1;
6278 my $arg = $2;
6279 my $test = $3;
6280 my $otype = $4;
6281 my $trail = $5;
6282 my $op = "!";
6283
6284 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6285
6286 my $type = lc($otype);
6287 if ($type =~ /^(?:true|false)$/) {
6288 if (("$test" eq "==" && "$type" eq "true") ||
6289 ("$test" eq "!=" && "$type" eq "false")) {
6290 $op = "";
6291 }
6292
6293 CHK("BOOL_COMPARISON",
6294 "Using comparison to $otype is error prone\n" . $herecurr);
6295
6296## maybe suggesting a correct construct would better
6297## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6298
6299 }
6300 }
6301
Joe Perches5d430902018-04-10 16:34:25 -07006302# check for bool bitfields
6303 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6304 WARN("BOOL_BITFIELD",
6305 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6306 }
6307
Joe Perchesd7295932018-08-21 21:57:26 -07006308# check for bool use in .h files
6309 if ($realfile =~ /\.h$/ &&
6310 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6311 CHK("BOOL_MEMBER",
6312 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6313 }
6314
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006315# check for semaphores initialized locked
6316 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006317 WARN("CONSIDER_COMPLETION",
6318 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006319 }
Joe Perches6712d852012-03-23 15:02:20 -07006320
Joe Perches67d0a072011-10-31 17:13:10 -07006321# recommend kstrto* over simple_strto* and strict_strto*
6322 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006323 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006324 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006325 }
Joe Perches6712d852012-03-23 15:02:20 -07006326
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006327# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006328 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006329 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006330 "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 -07006331 }
Joe Perches6712d852012-03-23 15:02:20 -07006332
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006333# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006334# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006335 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006336 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006337 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006338 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006339 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006340
6341# use of NR_CPUS is usually wrong
6342# ignore definitions of NR_CPUS and usage to define arrays as likely right
6343 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006344 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6345 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006346 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6347 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6348 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006349 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006350 WARN("NR_CPUS",
6351 "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 -07006352 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006353
Joe Perches52ea8502013-11-12 15:10:09 -08006354# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6355 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6356 ERROR("DEFINE_ARCH_HAS",
6357 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6358 }
6359
Joe Perchesacd93622015-02-13 14:38:38 -08006360# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006361 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006362 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6363 WARN("LIKELY_MISUSE",
6364 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6365 }
6366
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006367# whine mightly about in_atomic
6368 if ($line =~ /\bin_atomic\s*\(/) {
6369 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006370 ERROR("IN_ATOMIC",
6371 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006372 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006373 WARN("IN_ATOMIC",
6374 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006375 }
6376 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006377
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006378# check for mutex_trylock_recursive usage
6379 if ($line =~ /mutex_trylock_recursive/) {
6380 ERROR("LOCKING",
6381 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6382 }
6383
Peter Zijlstra1704f472010-03-19 01:37:42 +01006384# check for lockdep_set_novalidate_class
6385 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6386 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6387 if ($realfile !~ m@^kernel/lockdep@ &&
6388 $realfile !~ m@^include/linux/lockdep@ &&
6389 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006390 ERROR("LOCKDEP",
6391 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006392 }
6393 }
Dave Jones88f88312011-01-12 16:59:59 -08006394
Joe Perchesb392c642015-04-16 12:44:16 -07006395 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6396 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006397 WARN("EXPORTED_WORLD_WRITABLE",
6398 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006399 }
Joe Perches24358802014-04-03 14:49:13 -07006400
Joe Perches00180462018-02-06 15:38:55 -08006401# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6402# and whether or not function naming is typical and if
6403# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006404 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006405 defined $stat &&
6406 $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*\)/) {
6407 my $var = $1;
6408 my $perms = $2;
6409 my $show = $3;
6410 my $store = $4;
6411 my $octal_perms = perms_to_octal($perms);
6412 if ($show =~ /^${var}_show$/ &&
6413 $store =~ /^${var}_store$/ &&
6414 $octal_perms eq "0644") {
6415 if (WARN("DEVICE_ATTR_RW",
6416 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6417 $fix) {
6418 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6419 }
6420 } elsif ($show =~ /^${var}_show$/ &&
6421 $store =~ /^NULL$/ &&
6422 $octal_perms eq "0444") {
6423 if (WARN("DEVICE_ATTR_RO",
6424 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6425 $fix) {
6426 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6427 }
6428 } elsif ($show =~ /^NULL$/ &&
6429 $store =~ /^${var}_store$/ &&
6430 $octal_perms eq "0200") {
6431 if (WARN("DEVICE_ATTR_WO",
6432 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6433 $fix) {
6434 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6435 }
6436 } elsif ($octal_perms eq "0644" ||
6437 $octal_perms eq "0444" ||
6438 $octal_perms eq "0200") {
6439 my $newshow = "$show";
6440 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6441 my $newstore = $store;
6442 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6443 my $rename = "";
6444 if ($show ne $newshow) {
6445 $rename .= " '$show' to '$newshow'";
6446 }
6447 if ($store ne $newstore) {
6448 $rename .= " '$store' to '$newstore'";
6449 }
6450 WARN("DEVICE_ATTR_FUNCTIONS",
6451 "Consider renaming function(s)$rename\n" . $herecurr);
6452 } else {
6453 WARN("DEVICE_ATTR_PERMS",
6454 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6455 }
6456 }
6457
Joe Perches515a2352014-04-03 14:49:24 -07006458# Mode permission misuses where it seems decimal should be octal
6459# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006460# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6461# specific definition of not visible in sysfs.
6462# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6463# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006464 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006465 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006466 $line =~ /$mode_perms_search/) {
6467 foreach my $entry (@mode_permission_funcs) {
6468 my $func = $entry->[0];
6469 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006470
Joe Perches459cf0a2016-10-11 13:52:19 -07006471 my $lc = $stat =~ tr@\n@@;
6472 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006473 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006474
Joe Perches515a2352014-04-03 14:49:24 -07006475 my $skip_args = "";
6476 if ($arg_pos > 1) {
6477 $arg_pos--;
6478 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6479 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006480 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006481 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006482 my $val = $1;
6483 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006484 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6485 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6486 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006487 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006488 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006489 }
6490 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006491 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006492 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006493 }
Joe Perches24358802014-04-03 14:49:13 -07006494 }
6495 }
6496 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006497
Joe Perches459cf0a2016-10-11 13:52:19 -07006498# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006499 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006500 my $oval = $1;
6501 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006502 if (WARN("SYMBOLIC_PERMS",
6503 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6504 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006505 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006506 }
6507 }
6508
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006509# validate content of MODULE_LICENSE against list from include/linux/module.h
6510 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6511 my $extracted_string = get_quoted_string($line, $rawline);
6512 my $valid_licenses = qr{
6513 GPL|
6514 GPL\ v2|
6515 GPL\ and\ additional\ rights|
6516 Dual\ BSD/GPL|
6517 Dual\ MIT/GPL|
6518 Dual\ MPL/GPL|
6519 Proprietary
6520 }x;
6521 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6522 WARN("MODULE_LICENSE",
6523 "unknown module license " . $extracted_string . "\n" . $herecurr);
6524 }
6525 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006526 }
6527
6528 # If we have no input at all, then there is nothing to report on
6529 # so just keep quiet.
6530 if ($#rawlines == -1) {
6531 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006532 }
6533
Andy Whitcroft8905a672007-11-28 16:21:06 -08006534 # In mailback mode only produce a report in the negative, for
6535 # things that appear to be patches.
6536 if ($mailback && ($clean == 1 || !$is_patch)) {
6537 exit(0);
6538 }
6539
6540 # This is not a patch, and we are are in 'no-patch' mode so
6541 # just keep quiet.
6542 if (!$chk_patch && !$is_patch) {
6543 exit(0);
6544 }
6545
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006546 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006547 ERROR("NOT_UNIFIED_DIFF",
6548 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006549 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006550 if ($is_patch && $has_commit_log && $chk_signoff) {
6551 if ($signoff == 0) {
6552 ERROR("MISSING_SIGN_OFF",
6553 "Missing Signed-off-by: line(s)\n");
6554 } elsif (!$authorsignoff) {
6555 WARN("NO_AUTHOR_SIGN_OFF",
6556 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6557 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006558 }
6559
Andy Whitcroft8905a672007-11-28 16:21:06 -08006560 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006561 if ($summary && !($clean == 1 && $quiet == 1)) {
6562 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006563 print "total: $cnt_error errors, $cnt_warn warnings, " .
6564 (($check)? "$cnt_chk checks, " : "") .
6565 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006566 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006567
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006568 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006569 # If there were any defects found and not already fixing them
6570 if (!$clean and !$fix) {
6571 print << "EOM"
6572
6573NOTE: For some of the reported defects, checkpatch may be able to
6574 mechanically convert to the typical style using --fix or --fix-inplace.
6575EOM
6576 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006577 # If there were whitespace errors which cleanpatch can fix
6578 # then suggest that.
6579 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006580 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006581 print << "EOM"
6582
6583NOTE: Whitespace errors detected.
6584 You may wish to use scripts/cleanpatch or scripts/cleanfile
6585EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006586 }
6587 }
6588
Joe Perchesd752fcc2014-08-06 16:11:05 -07006589 if ($clean == 0 && $fix &&
6590 ("@rawlines" ne "@fixed" ||
6591 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006592 my $newfile = $filename;
6593 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006594 my $linecount = 0;
6595 my $f;
6596
Joe Perchesd752fcc2014-08-06 16:11:05 -07006597 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6598
Joe Perches3705ce52013-07-03 15:05:31 -07006599 open($f, '>', $newfile)
6600 or die "$P: Can't open $newfile for write\n";
6601 foreach my $fixed_line (@fixed) {
6602 $linecount++;
6603 if ($file) {
6604 if ($linecount > 3) {
6605 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006606 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006607 }
6608 } else {
6609 print $f $fixed_line . "\n";
6610 }
6611 }
6612 close($f);
6613
6614 if (!$quiet) {
6615 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006616
Joe Perches3705ce52013-07-03 15:05:31 -07006617Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6618
6619Do _NOT_ trust the results written to this file.
6620Do _NOT_ submit these changes without inspecting them for correctness.
6621
6622This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6623No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006624EOM
6625 }
6626 }
6627
Joe Perchesd8469f12015-06-25 15:03:00 -07006628 if ($quiet == 0) {
6629 print "\n";
6630 if ($clean == 1) {
6631 print "$vname has no obvious style problems and is ready for submission.\n";
6632 } else {
6633 print "$vname has style problems, please review.\n";
6634 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006635 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006636 return $clean;
6637}