blob: ede55afaadf0b2c43f880b6607156ead207a720d [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";
Vadim Bendebury98005e82019-03-07 16:28:38 -080064my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070065# git output parsing needs US English output, so first set backtick child process LANGUAGE
66my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Hannes Eder77f5b102009-09-21 17:04:37 -070067
68sub help {
69 my ($exitcode) = @_;
70
71 print << "EOM";
72Usage: $P [OPTION]... [FILE]...
73Version: $V
74
75Options:
76 -q, --quiet quiet
77 --no-tree run without a kernel tree
78 --no-signoff do not check for 'Signed-off-by' line
79 --patch treat FILE as patchfile (default)
80 --emacs emacs compile window format
81 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070082 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070083 -g, --git treat FILE as a single commit or git revision range
84 single git commit with:
85 <rev>
86 <rev>^
87 <rev>~n
88 multiple git commits with:
89 <rev1>..<rev2>
90 <rev1>...<rev2>
91 <rev>-<count>
92 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070093 -f, --file treat FILE as regular source file
94 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070095 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070096 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070097 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070098 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080099 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700100 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700101 --root=PATH PATH to the kernel tree root
102 --no-summary suppress the per-file summary
103 --mailback only produce a report in case of warnings/errors
104 --summary-file include the filename in summary
105 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
106 'values', 'possible', 'type', and 'attr' (default
107 is all off)
108 --test-only=WORD report only warnings/errors containing WORD
109 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700110 --fix EXPERIMENTAL - may create horrible results
111 If correctable single-line errors exist, create
112 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
113 with potential errors corrected to the preferred
114 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800115 --fix-inplace EXPERIMENTAL - may create horrible results
116 Is the same as --fix, but overwrites the input
117 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700118 --ignore-perl-version override checking of perl version. expect
119 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700120 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700121 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700122 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700123 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700124 --color[=WHEN] Use colors 'always', 'never', or only when output
125 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700126 -h, --help, --version display this help and exit
127
128When FILE is - read standard input.
129EOM
130
131 exit($exitcode);
132}
133
Joe Perches3beb42e2016-05-20 17:04:14 -0700134sub uniq {
135 my %seen;
136 return grep { !$seen{$_}++ } @_;
137}
138
139sub list_types {
140 my ($exitcode) = @_;
141
142 my $count = 0;
143
144 local $/ = undef;
145
146 open(my $script, '<', abs_path($P)) or
147 die "$P: Can't read '$P' $!\n";
148
149 my $text = <$script>;
150 close($script);
151
152 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700153 # Also catch when type or level is passed through a variable
154 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700155 push (@types, $_);
156 }
157 @types = sort(uniq(@types));
158 print("#\tMessage type\n\n");
159 foreach my $type (@types) {
160 print(++$count . "\t" . $type . "\n");
161 }
162
163 exit($exitcode);
164}
165
Joe Perches000d1cc12011-07-25 17:13:25 -0700166my $conf = which_conf($configuration_file);
167if (-f $conf) {
168 my @conf_args;
169 open(my $conffile, '<', "$conf")
170 or warn "$P: Can't find a readable $configuration_file file $!\n";
171
172 while (<$conffile>) {
173 my $line = $_;
174
175 $line =~ s/\s*\n?$//g;
176 $line =~ s/^\s*//g;
177 $line =~ s/\s+/ /g;
178
179 next if ($line =~ m/^\s*#/);
180 next if ($line =~ m/^\s*$/);
181
182 my @words = split(" ", $line);
183 foreach my $word (@words) {
184 last if ($word =~ m/^#/);
185 push (@conf_args, $word);
186 }
187 }
188 close($conffile);
189 unshift(@ARGV, @conf_args) if @conf_args;
190}
191
John Brooks737c0762017-07-10 15:52:24 -0700192# Perl's Getopt::Long allows options to take optional arguments after a space.
193# Prevent --color by itself from consuming other arguments
194foreach (@ARGV) {
195 if ($_ eq "--color" || $_ eq "-color") {
196 $_ = "--color=$color";
197 }
198}
199
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700200GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700201 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700202 'tree!' => \$tree,
203 'signoff!' => \$chk_signoff,
204 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700205 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800206 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700207 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700208 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700209 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210 'subjective!' => \$check,
211 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700212 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700213 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700214 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700215 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800216 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700217 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700218 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800219 'summary!' => \$summary,
220 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800221 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700222 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800223 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700224 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800225 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700226 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700227 'codespell!' => \$codespell,
228 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700229 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700230 'color=s' => \$color,
231 'no-color' => \$color, #keep old behaviors of -nocolor
232 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700233 'h|help' => \$help,
234 'version' => \$help
235) or help(1);
236
237help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700238
Joe Perches3beb42e2016-05-20 17:04:14 -0700239list_types(0) if ($list_types);
240
Joe Perches9624b8d2014-01-23 15:54:44 -0800241$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700242$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800243
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700244my $exit = 0;
245
Joe Perches5b579802018-08-21 21:57:33 -0700246my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700247if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700248 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700249 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700250 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700251}
252
Allen Hubbe45107ff2016-08-02 14:04:47 -0700253#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700254if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700255 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700256}
257
John Brooks737c0762017-07-10 15:52:24 -0700258if ($color =~ /^[01]$/) {
259 $color = !$color;
260} elsif ($color =~ /^always$/i) {
261 $color = 1;
262} elsif ($color =~ /^never$/i) {
263 $color = 0;
264} elsif ($color =~ /^auto$/i) {
265 $color = (-t STDOUT);
266} else {
267 die "Invalid color mode: $color\n";
268}
269
Joe Perches91bfe482013-09-11 14:23:59 -0700270sub hash_save_array_words {
271 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700272
Joe Perches91bfe482013-09-11 14:23:59 -0700273 my @array = split(/,/, join(',', @$arrayRef));
274 foreach my $word (@array) {
275 $word =~ s/\s*\n?$//g;
276 $word =~ s/^\s*//g;
277 $word =~ s/\s+/ /g;
278 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700279
Joe Perches91bfe482013-09-11 14:23:59 -0700280 next if ($word =~ m/^\s*#/);
281 next if ($word =~ m/^\s*$/);
282
283 $hashRef->{$word}++;
284 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700285}
286
Joe Perches91bfe482013-09-11 14:23:59 -0700287sub hash_show_words {
288 my ($hashRef, $prefix) = @_;
289
Joe Perches3c816e42015-06-25 15:03:29 -0700290 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700291 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700292 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700293 print " $word";
294 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700295 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700296 }
297}
298
299hash_save_array_words(\%ignore_type, \@ignore);
300hash_save_array_words(\%use_type, \@use);
301
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800302my $dbg_values = 0;
303my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700304my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700305my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800306for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800307 ## no critic
308 eval "\${dbg_$key} = '$debug{$key}';";
309 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800310}
311
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700312my $rpt_cleaners = 0;
313
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314if ($terse) {
315 $emacs = 1;
316 $quiet++;
317}
318
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700319if ($tree) {
320 if (defined $root) {
321 if (!top_of_kernel_tree($root)) {
322 die "$P: $root: --root does not point at a valid tree\n";
323 }
324 } else {
325 if (top_of_kernel_tree('.')) {
326 $root = '.';
327 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
328 top_of_kernel_tree($1)) {
329 $root = $1;
330 }
331 }
332
333 if (!defined $root) {
334 print "Must be run from the top-level dir. of a kernel tree\n";
335 exit(2);
336 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700337}
338
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700339my $emitted_corrupt = 0;
340
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700341our $Ident = qr{
342 [A-Za-z_][A-Za-z\d_]*
343 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
344 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700345our $Storage = qr{extern|static|asmlinkage};
346our $Sparse = qr{
347 __user|
348 __kernel|
349 __force|
350 __iomem|
351 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800352 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700353 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700354 __refconst|
355 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800356 __rcu|
357 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700358 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800359our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
360our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
361our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
362our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
363our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700364
Wolfram Sang52131292010-03-05 13:43:51 -0800365# Notes to $Attribute:
366# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700367our $Attribute = qr{
368 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700369 __percpu|
370 __nocast|
371 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200372 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700373 __packed__|
374 __packed2__|
375 __naked|
376 __maybe_unused|
377 __always_unused|
378 __noreturn|
379 __used|
380 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800381 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700382 __noclone|
383 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700384 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700385 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700386 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700387 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700388 ____cacheline_aligned|
389 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800390 ____cacheline_internodealigned_in_smp|
391 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700393our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700394our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700395our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
396our $Lval = qr{$Ident(?:$Member)*};
397
Joe Perches95e2c602013-07-03 15:05:20 -0700398our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
399our $Binary = qr{(?i)0b[01]+$Int_type?};
400our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
401our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700402our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800403our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800404our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
405our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
406our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800407our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700408our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800409our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700410our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700411our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700412our $Operators = qr{
413 <=|>=|==|!=|
414 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700415 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700416 }x;
417
Joe Perches91cb5192014-04-03 14:49:32 -0700418our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
419
Joe Perchesab7e23f2015-04-16 12:44:22 -0700420our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800421our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700422our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700423our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800424our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700425our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800426our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700427our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800428
Joe Perches15662b32011-10-31 17:13:12 -0700429our $NON_ASCII_UTF8 = qr{
430 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700431 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
432 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
433 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
434 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
435 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
436 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
437}x;
438
Joe Perches15662b32011-10-31 17:13:12 -0700439our $UTF8 = qr{
440 [\x09\x0A\x0D\x20-\x7E] # ASCII
441 | $NON_ASCII_UTF8
442}x;
443
Joe Perchese6176fa2015-06-25 15:02:49 -0700444our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800445our $typeOtherOSTypedefs = qr{(?x:
446 u_(?:char|short|int|long) | # bsd
447 u(?:nchar|short|int|long) # sysv
448)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700449our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700450 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700451 atomic_t
452)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700453our $typeTypedefs = qr{(?x:
454 $typeC99Typedefs\b|
455 $typeOtherOSTypedefs\b|
456 $typeKernelTypedefs\b
457)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700458
Joe Perches6d32f7a2015-11-06 16:31:37 -0800459our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
460
Joe Perches691e6692010-03-05 13:43:51 -0800461our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800462 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700463 (?:[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 -0800464 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700465 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700466 panic|
Joe Perches06668722013-11-12 15:10:07 -0800467 MODULE_[A-Z_]+|
468 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800469)};
470
Joe Perchese29a70f2019-03-07 16:28:35 -0800471our $allocFunctions = qr{(?x:
472 (?:(?:devm_)?
473 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
474 kstrdup(?:_const)? |
475 kmemdup(?:_nul)?) |
476 (?:\w+)?alloc_skb(?:ip_align)? |
477 # dev_alloc_skb/netdev_alloc_skb, et al
478 dma_alloc_coherent
479)};
480
Joe Perches20112472011-07-25 17:13:23 -0700481our $signature_tags = qr{(?xi:
482 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800483 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700484 Acked-by:|
485 Tested-by:|
486 Reviewed-by:|
487 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700488 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700489 To:|
490 Cc:
491)};
492
Joe Perches18130872014-08-06 16:11:22 -0700493our @typeListMisordered = (
494 qr{char\s+(?:un)?signed},
495 qr{int\s+(?:(?:un)?signed\s+)?short\s},
496 qr{int\s+short(?:\s+(?:un)?signed)},
497 qr{short\s+int(?:\s+(?:un)?signed)},
498 qr{(?:un)?signed\s+int\s+short},
499 qr{short\s+(?:un)?signed},
500 qr{long\s+int\s+(?:un)?signed},
501 qr{int\s+long\s+(?:un)?signed},
502 qr{long\s+(?:un)?signed\s+int},
503 qr{int\s+(?:un)?signed\s+long},
504 qr{int\s+(?:un)?signed},
505 qr{int\s+long\s+long\s+(?:un)?signed},
506 qr{long\s+long\s+int\s+(?:un)?signed},
507 qr{long\s+long\s+(?:un)?signed\s+int},
508 qr{long\s+long\s+(?:un)?signed},
509 qr{long\s+(?:un)?signed},
510);
511
Andy Whitcroft8905a672007-11-28 16:21:06 -0800512our @typeList = (
513 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700514 qr{(?:(?:un)?signed\s+)?char},
515 qr{(?:(?:un)?signed\s+)?short\s+int},
516 qr{(?:(?:un)?signed\s+)?short},
517 qr{(?:(?:un)?signed\s+)?int},
518 qr{(?:(?:un)?signed\s+)?long\s+int},
519 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
520 qr{(?:(?:un)?signed\s+)?long\s+long},
521 qr{(?:(?:un)?signed\s+)?long},
522 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523 qr{float},
524 qr{double},
525 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800526 qr{struct\s+$Ident},
527 qr{union\s+$Ident},
528 qr{enum\s+$Ident},
529 qr{${Ident}_t},
530 qr{${Ident}_handler},
531 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700532 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800533);
Joe Perches938224b2016-01-20 14:59:15 -0800534
535our $C90_int_types = qr{(?x:
536 long\s+long\s+int\s+(?:un)?signed|
537 long\s+long\s+(?:un)?signed\s+int|
538 long\s+long\s+(?:un)?signed|
539 (?:(?:un)?signed\s+)?long\s+long\s+int|
540 (?:(?:un)?signed\s+)?long\s+long|
541 int\s+long\s+long\s+(?:un)?signed|
542 int\s+(?:(?:un)?signed\s+)?long\s+long|
543
544 long\s+int\s+(?:un)?signed|
545 long\s+(?:un)?signed\s+int|
546 long\s+(?:un)?signed|
547 (?:(?:un)?signed\s+)?long\s+int|
548 (?:(?:un)?signed\s+)?long|
549 int\s+long\s+(?:un)?signed|
550 int\s+(?:(?:un)?signed\s+)?long|
551
552 int\s+(?:un)?signed|
553 (?:(?:un)?signed\s+)?int
554)};
555
Alex Dowad485ff232015-06-25 15:02:52 -0700556our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700557our @typeListWithAttr = (
558 @typeList,
559 qr{struct\s+$InitAttribute\s+$Ident},
560 qr{union\s+$InitAttribute\s+$Ident},
561);
562
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700563our @modifierList = (
564 qr{fastcall},
565);
Alex Dowad485ff232015-06-25 15:02:52 -0700566our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800567
Joe Perches24358802014-04-03 14:49:13 -0700568our @mode_permission_funcs = (
569 ["module_param", 3],
570 ["module_param_(?:array|named|string)", 4],
571 ["module_param_array_named", 5],
572 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
573 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700574 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
575 ["IIO_DEV_ATTR_[A-Z_]+", 1],
576 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
577 ["SENSOR_TEMPLATE(?:_2|)", 3],
578 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700579);
580
Joe Perches515a2352014-04-03 14:49:24 -0700581#Create a search pattern for all these functions to speed up a loop below
582our $mode_perms_search = "";
583foreach my $entry (@mode_permission_funcs) {
584 $mode_perms_search .= '|' if ($mode_perms_search ne "");
585 $mode_perms_search .= $entry->[0];
586}
Joe Perches00180462018-02-06 15:38:55 -0800587$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700588
Joe Perches9189c7e2018-09-07 15:26:18 -0700589our %deprecated_apis = (
590 "synchronize_rcu_bh" => "synchronize_rcu",
591 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
592 "call_rcu_bh" => "call_rcu",
593 "rcu_barrier_bh" => "rcu_barrier",
594 "synchronize_sched" => "synchronize_rcu",
595 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
596 "call_rcu_sched" => "call_rcu",
597 "rcu_barrier_sched" => "rcu_barrier",
598 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
599 "cond_synchronize_sched" => "cond_synchronize_rcu",
600);
601
602#Create a search pattern for all these strings to speed up a loop below
603our $deprecated_apis_search = "";
604foreach my $entry (keys %deprecated_apis) {
605 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
606 $deprecated_apis_search .= $entry;
607}
608$deprecated_apis_search = "(?:${deprecated_apis_search})";
609
Joe Perchesb392c642015-04-16 12:44:16 -0700610our $mode_perms_world_writable = qr{
611 S_IWUGO |
612 S_IWOTH |
613 S_IRWXUGO |
614 S_IALLUGO |
615 0[0-7][0-7][2367]
616}x;
617
Joe Perchesf90774e2016-10-11 13:51:47 -0700618our %mode_permission_string_types = (
619 "S_IRWXU" => 0700,
620 "S_IRUSR" => 0400,
621 "S_IWUSR" => 0200,
622 "S_IXUSR" => 0100,
623 "S_IRWXG" => 0070,
624 "S_IRGRP" => 0040,
625 "S_IWGRP" => 0020,
626 "S_IXGRP" => 0010,
627 "S_IRWXO" => 0007,
628 "S_IROTH" => 0004,
629 "S_IWOTH" => 0002,
630 "S_IXOTH" => 0001,
631 "S_IRWXUGO" => 0777,
632 "S_IRUGO" => 0444,
633 "S_IWUGO" => 0222,
634 "S_IXUGO" => 0111,
635);
636
637#Create a search pattern for all these strings to speed up a loop below
638our $mode_perms_string_search = "";
639foreach my $entry (keys %mode_permission_string_types) {
640 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
641 $mode_perms_string_search .= $entry;
642}
Joe Perches00180462018-02-06 15:38:55 -0800643our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
644our $multi_mode_perms_string_search = qr{
645 ${single_mode_perms_string_search}
646 (?:\s*\|\s*${single_mode_perms_string_search})*
647}x;
648
649sub perms_to_octal {
650 my ($string) = @_;
651
652 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
653
654 my $val = "";
655 my $oval = "";
656 my $to = 0;
657 my $curpos = 0;
658 my $lastpos = 0;
659 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
660 $curpos = pos($string);
661 my $match = $2;
662 my $omatch = $1;
663 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
664 $lastpos = $curpos;
665 $to |= $mode_permission_string_types{$match};
666 $val .= '\s*\|\s*' if ($val ne "");
667 $val .= $match;
668 $oval .= $omatch;
669 }
670 $oval =~ s/^\s*\|\s*//;
671 $oval =~ s/\s*\|\s*$//;
672 return sprintf("%04o", $to);
673}
Joe Perchesf90774e2016-10-11 13:51:47 -0700674
Wolfram Sang7840a942010-08-09 17:20:57 -0700675our $allowed_asm_includes = qr{(?x:
676 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700677 memory|
678 time|
679 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700680)};
681# memory.h: ARM has a custom one
682
Kees Cook66b47b42014-10-13 15:51:57 -0700683# Load common spelling mistakes and build regular expression list.
684my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700685my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700686
Joe Perches36061e32014-12-10 15:51:43 -0800687if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800688 while (<$spelling>) {
689 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700690
Joe Perches36061e32014-12-10 15:51:43 -0800691 $line =~ s/\s*\n?$//g;
692 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700693
Joe Perches36061e32014-12-10 15:51:43 -0800694 next if ($line =~ m/^\s*#/);
695 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700696
Joe Perches36061e32014-12-10 15:51:43 -0800697 my ($suspect, $fix) = split(/\|\|/, $line);
698
Joe Perches36061e32014-12-10 15:51:43 -0800699 $spelling_fix{$suspect} = $fix;
700 }
701 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800702} else {
703 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700704}
Kees Cook66b47b42014-10-13 15:51:57 -0700705
Joe Perchesebfd7d62015-04-16 12:44:14 -0700706if ($codespell) {
707 if (open(my $spelling, '<', $codespellfile)) {
708 while (<$spelling>) {
709 my $line = $_;
710
711 $line =~ s/\s*\n?$//g;
712 $line =~ s/^\s*//g;
713
714 next if ($line =~ m/^\s*#/);
715 next if ($line =~ m/^\s*$/);
716 next if ($line =~ m/, disabled/i);
717
718 $line =~ s/,.*$//;
719
720 my ($suspect, $fix) = split(/->/, $line);
721
722 $spelling_fix{$suspect} = $fix;
723 }
724 close($spelling);
725 } else {
726 warn "No codespell typos will be found - file '$codespellfile': $!\n";
727 }
728}
729
730$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
731
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700732sub read_words {
733 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700734
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700735 if (open(my $words, '<', $file)) {
736 while (<$words>) {
737 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700738
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700739 $line =~ s/\s*\n?$//g;
740 $line =~ s/^\s*//g;
741
742 next if ($line =~ m/^\s*#/);
743 next if ($line =~ m/^\s*$/);
744 if ($line =~ /\s/) {
745 print("$file: '$line' invalid - ignored\n");
746 next;
747 }
748
749 $$wordsRef .= '|' if ($$wordsRef ne "");
750 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700751 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700752 close($file);
753 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700754 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700755
756 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700757}
758
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700759my $const_structs = "";
760read_words(\$const_structs, $conststructsfile)
761 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
762
763my $typeOtherTypedefs = "";
764if (length($typedefsfile)) {
765 read_words(\$typeOtherTypedefs, $typedefsfile)
766 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
767}
768$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
769
Andy Whitcroft8905a672007-11-28 16:21:06 -0800770sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700771 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
772 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700773 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700774 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700775 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700776 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700777 (?:$typeTypedefs\b)|
778 (?:${all}\b)
779 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800780 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700781 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800782 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800783 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700784 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700785 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800786 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700787 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800788 }x;
Joe Perches18130872014-08-06 16:11:22 -0700789 $NonptrTypeMisordered = qr{
790 (?:$Modifier\s+|const\s+)*
791 (?:
792 (?:${Misordered}\b)
793 )
794 (?:\s+$Modifier|\s+const)*
795 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700796 $NonptrTypeWithAttr = qr{
797 (?:$Modifier\s+|const\s+)*
798 (?:
799 (?:typeof|__typeof__)\s*\([^\)]*\)|
800 (?:$typeTypedefs\b)|
801 (?:${allWithAttr}\b)
802 )
803 (?:\s+$Modifier|\s+const)*
804 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800805 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700806 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700807 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700808 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800809 }x;
Joe Perches18130872014-08-06 16:11:22 -0700810 $TypeMisordered = qr{
811 $NonptrTypeMisordered
812 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
813 (?:\s+$Inline|\s+$Modifier)*
814 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700815 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700816 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800817}
818build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700819
Joe Perches7d2367a2011-07-25 17:13:22 -0700820our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700821
822# Using $balanced_parens, $LvalOrFunc, or $FuncArg
823# requires at least perl version v5.10.0
824# Any use must be runtime checked with $^V
825
826our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700827our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800828our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700829
Joe Perchesf8422302014-08-06 16:11:31 -0700830our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700831 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700832 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700833 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
834 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700835)};
836
Joe Perches7d2367a2011-07-25 17:13:22 -0700837sub deparenthesize {
838 my ($string) = @_;
839 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700840
841 while ($string =~ /^\s*\(.*\)\s*$/) {
842 $string =~ s@^\s*\(\s*@@;
843 $string =~ s@\s*\)\s*$@@;
844 }
845
Joe Perches7d2367a2011-07-25 17:13:22 -0700846 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700847
Joe Perches7d2367a2011-07-25 17:13:22 -0700848 return $string;
849}
850
Joe Perches34456862013-07-03 15:05:34 -0700851sub seed_camelcase_file {
852 my ($file) = @_;
853
854 return if (!(-f $file));
855
856 local $/;
857
858 open(my $include_file, '<', "$file")
859 or warn "$P: Can't read '$file' $!\n";
860 my $text = <$include_file>;
861 close($include_file);
862
863 my @lines = split('\n', $text);
864
865 foreach my $line (@lines) {
866 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
867 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
868 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800869 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
870 $camelcase{$1} = 1;
871 } 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 -0700872 $camelcase{$1} = 1;
873 }
874 }
875}
876
Joe Perchescd28b112019-12-04 16:52:09 -0800877our %maintained_status = ();
878
Joe Perches85b0ee12016-10-11 13:51:44 -0700879sub is_maintained_obsolete {
880 my ($filename) = @_;
881
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800882 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700883
Joe Perchescd28b112019-12-04 16:52:09 -0800884 if (!exists($maintained_status{$filename})) {
885 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
886 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700887
Joe Perchescd28b112019-12-04 16:52:09 -0800888 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700889}
890
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700891sub is_SPDX_License_valid {
892 my ($license) = @_;
893
Joe Perches56294112018-08-21 21:58:04 -0700894 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700895
Joe Perches56294112018-08-21 21:58:04 -0700896 my $root_path = abs_path($root);
897 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700898 return 0 if ($status ne "");
899 return 1;
900}
901
Joe Perches34456862013-07-03 15:05:34 -0700902my $camelcase_seeded = 0;
903sub seed_camelcase_includes {
904 return if ($camelcase_seeded);
905
906 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700907 my $camelcase_cache = "";
908 my @include_files = ();
909
910 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700911
Richard Genoud3645e322014-02-10 14:25:32 -0800912 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700913 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700914 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700915 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700916 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700917 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700918 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700919 @include_files = split('\n', $files);
920 foreach my $file (@include_files) {
921 my $date = POSIX::strftime("%Y%m%d%H%M",
922 localtime((stat $file)[9]));
923 $last_mod_date = $date if ($last_mod_date < $date);
924 }
925 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700926 }
Joe Perchesc707a812013-07-08 16:00:43 -0700927
928 if ($camelcase_cache ne "" && -f $camelcase_cache) {
929 open(my $camelcase_file, '<', "$camelcase_cache")
930 or warn "$P: Can't read '$camelcase_cache' $!\n";
931 while (<$camelcase_file>) {
932 chomp;
933 $camelcase{$_} = 1;
934 }
935 close($camelcase_file);
936
937 return;
938 }
939
Richard Genoud3645e322014-02-10 14:25:32 -0800940 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700941 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700942 @include_files = split('\n', $files);
943 }
944
Joe Perches34456862013-07-03 15:05:34 -0700945 foreach my $file (@include_files) {
946 seed_camelcase_file($file);
947 }
Joe Perches351b2a12013-07-03 15:05:36 -0700948
Joe Perchesc707a812013-07-08 16:00:43 -0700949 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700950 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700951 open(my $camelcase_file, '>', "$camelcase_cache")
952 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700953 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
954 print $camelcase_file ("$_\n");
955 }
956 close($camelcase_file);
957 }
Joe Perches34456862013-07-03 15:05:34 -0700958}
959
Joe Perchesd311cd42014-08-06 16:10:57 -0700960sub git_commit_info {
961 my ($commit, $id, $desc) = @_;
962
963 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
964
Joe Perchesdbbf8692019-09-25 16:46:52 -0700965 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700966 $output =~ s/^\s*//gm;
967 my @lines = split("\n", $output);
968
Joe Perches0d7835f2015-02-13 14:38:35 -0800969 return ($id, $desc) if ($#lines < 0);
970
Sean Christopherson5a7f4452019-09-25 16:46:49 -0700971 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -0700972# Maybe one day convert this block of bash into something that returns
973# all matching commit ids, but it's very slow...
974#
975# echo "checking commits $1..."
976# git rev-list --remotes | grep -i "^$1" |
977# while read line ; do
978# git log --format='%H %s' -1 $line |
979# echo "commit $(cut -c 1-12,41-)"
980# done
981 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700982 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700983 } else {
984 $id = substr($lines[0], 0, 12);
985 $desc = substr($lines[0], 41);
986 }
987
988 return ($id, $desc);
989}
990
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700991$chk_signoff = 0 if ($file);
992
Andy Whitcroft00df3442007-06-08 13:47:06 -0700993my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800994my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700995my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700996my @fixed_inserted = ();
997my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700998my $fixlinenr = -1;
999
Du, Changbin4a593c32016-05-20 17:04:16 -07001000# If input is git commits, extract all commits from the commit expressions.
1001# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1002die "$P: No git repository found\n" if ($git && !-e ".git");
1003
1004if ($git) {
1005 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001006 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001007 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001008 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1009 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001010 } elsif ($commit_expr =~ m/\.\./) {
1011 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001012 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001013 $git_range = "-1 $commit_expr";
1014 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001015 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001016 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001017 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1018 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001019 my $sha1 = $1;
1020 my $subject = $2;
1021 unshift(@commits, $sha1);
1022 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001023 }
1024 }
1025 die "$P: no git commits after extraction!\n" if (@commits == 0);
1026 @ARGV = @commits;
1027}
1028
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001029my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001030$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001031for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001032 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001033 if ($git) {
1034 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1035 die "$P: $filename: git format-patch failed - $!\n";
1036 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001037 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001038 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001039 } elsif ($filename eq '-') {
1040 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001041 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001042 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001043 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001044 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001045 if ($filename eq '-') {
1046 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001047 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001048 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001049 } else {
1050 $vname = $filename;
1051 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001052 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001053 chomp;
1054 push(@rawlines, $_);
1055 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001056 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001057
1058 if ($#ARGV > 0 && $quiet == 0) {
1059 print '-' x length($vname) . "\n";
1060 print "$vname\n";
1061 print '-' x length($vname) . "\n";
1062 }
1063
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001064 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001065 $exit = 1;
1066 }
1067 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001068 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001069 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001070 @fixed_inserted = ();
1071 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001072 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001073 @modifierListFile = ();
1074 @typeListFile = ();
1075 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001076}
1077
Joe Perchesd8469f12015-06-25 15:03:00 -07001078if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001079 hash_show_words(\%use_type, "Used");
1080 hash_show_words(\%ignore_type, "Ignored");
1081
Joe Perches5b579802018-08-21 21:57:33 -07001082 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001083 print << "EOM"
1084
1085NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001086 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001087EOM
1088 }
1089 if ($exit) {
1090 print << "EOM"
1091
1092NOTE: If any of the errors are false positives, please report
1093 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1094EOM
1095 }
1096}
1097
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001098exit($exit);
1099
1100sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001101 my ($root) = @_;
1102
1103 my @tree_check = (
1104 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1105 "README", "Documentation", "arch", "include", "drivers",
1106 "fs", "init", "ipc", "kernel", "lib", "scripts",
1107 );
1108
1109 foreach my $check (@tree_check) {
1110 if (! -e $root . '/' . $check) {
1111 return 0;
1112 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001113 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001114 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001115}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001116
Joe Perches20112472011-07-25 17:13:23 -07001117sub parse_email {
1118 my ($formatted_email) = @_;
1119
1120 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001121 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001122 my $address = "";
1123 my $comment = "";
1124
1125 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1126 $name = $1;
1127 $address = $2;
1128 $comment = $3 if defined $3;
1129 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1130 $address = $1;
1131 $comment = $2 if defined $2;
1132 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1133 $address = $1;
1134 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001135 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001136 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001137 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001138 $name =~ s/^\"|\"$//g;
1139 # If there's a name left after stripping spaces and
1140 # leading quotes, and the address doesn't have both
1141 # leading and trailing angle brackets, the address
1142 # is invalid. ie:
1143 # "joe smith joe@smith.com" bad
1144 # "joe smith <joe@smith.com" bad
1145 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1146 $name = "";
1147 $address = "";
1148 $comment = "";
1149 }
1150 }
1151
Joe Perches3705ce52013-07-03 15:05:31 -07001152 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001153 $name =~ s/^\"|\"$//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001154 $name =~ s/(\s*\([^\)]+\))\s*//;
1155 if (defined($1)) {
1156 $name_comment = trim($1);
1157 }
Joe Perches3705ce52013-07-03 15:05:31 -07001158 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001159 $address =~ s/^\<|\>$//g;
1160
1161 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1162 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1163 $name = "\"$name\"";
1164 }
1165
Joe Perchesdfa05c22020-04-06 20:10:48 -07001166 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001167}
1168
1169sub format_email {
1170 my ($name, $address) = @_;
1171
1172 my $formatted_email;
1173
Joe Perches3705ce52013-07-03 15:05:31 -07001174 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001175 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001176 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001177
1178 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1179 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1180 $name = "\"$name\"";
1181 }
1182
1183 if ("$name" eq "") {
1184 $formatted_email = "$address";
1185 } else {
1186 $formatted_email = "$name <$address>";
1187 }
1188
1189 return $formatted_email;
1190}
1191
Joe Perchesdfa05c22020-04-06 20:10:48 -07001192sub reformat_email {
1193 my ($email) = @_;
1194
1195 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1196 return format_email($email_name, $email_address);
1197}
1198
1199sub same_email_addresses {
1200 my ($email1, $email2) = @_;
1201
1202 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1203 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1204
1205 return $email1_name eq $email2_name &&
1206 $email1_address eq $email2_address;
1207}
1208
Joe Perchesd311cd42014-08-06 16:10:57 -07001209sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001210 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001211
Joe Perchesbd474ca2014-08-06 16:11:10 -07001212 foreach my $path (split(/:/, $ENV{PATH})) {
1213 if (-e "$path/$bin") {
1214 return "$path/$bin";
1215 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001216 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001217
Joe Perchesbd474ca2014-08-06 16:11:10 -07001218 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001219}
1220
Joe Perches000d1cc12011-07-25 17:13:25 -07001221sub which_conf {
1222 my ($conf) = @_;
1223
1224 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1225 if (-e "$path/$conf") {
1226 return "$path/$conf";
1227 }
1228 }
1229
1230 return "";
1231}
1232
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001233sub expand_tabs {
1234 my ($str) = @_;
1235
1236 my $res = '';
1237 my $n = 0;
1238 for my $c (split(//, $str)) {
1239 if ($c eq "\t") {
1240 $res .= ' ';
1241 $n++;
1242 for (; ($n % 8) != 0; $n++) {
1243 $res .= ' ';
1244 }
1245 next;
1246 }
1247 $res .= $c;
1248 $n++;
1249 }
1250
1251 return $res;
1252}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001253sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001254 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001255 return $res;
1256}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001257
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001258sub line_stats {
1259 my ($line) = @_;
1260
1261 # Drop the diff line leader and expand tabs
1262 $line =~ s/^.//;
1263 $line = expand_tabs($line);
1264
1265 # Pick the indent from the front of the line.
1266 my ($white) = ($line =~ /^(\s*)/);
1267
1268 return (length($line), length($white));
1269}
1270
Andy Whitcroft773647a2008-03-28 14:15:58 -07001271my $sanitise_quote = '';
1272
1273sub sanitise_line_reset {
1274 my ($in_comment) = @_;
1275
1276 if ($in_comment) {
1277 $sanitise_quote = '*/';
1278 } else {
1279 $sanitise_quote = '';
1280 }
1281}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001282sub sanitise_line {
1283 my ($line) = @_;
1284
1285 my $res = '';
1286 my $l = '';
1287
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001288 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001289 my $off = 0;
1290 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001291
Andy Whitcroft773647a2008-03-28 14:15:58 -07001292 # Always copy over the diff marker.
1293 $res = substr($line, 0, 1);
1294
1295 for ($off = 1; $off < length($line); $off++) {
1296 $c = substr($line, $off, 1);
1297
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001298 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001299 # and end, all to $;.
1300 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1301 $sanitise_quote = '*/';
1302
1303 substr($res, $off, 2, "$;$;");
1304 $off++;
1305 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001306 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001307 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001308 $sanitise_quote = '';
1309 substr($res, $off, 2, "$;$;");
1310 $off++;
1311 next;
1312 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001313 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1314 $sanitise_quote = '//';
1315
1316 substr($res, $off, 2, $sanitise_quote);
1317 $off++;
1318 next;
1319 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001320
1321 # A \ in a string means ignore the next character.
1322 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1323 $c eq "\\") {
1324 substr($res, $off, 2, 'XX');
1325 $off++;
1326 next;
1327 }
1328 # Regular quotes.
1329 if ($c eq "'" || $c eq '"') {
1330 if ($sanitise_quote eq '') {
1331 $sanitise_quote = $c;
1332
1333 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001334 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001335 } elsif ($sanitise_quote eq $c) {
1336 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001337 }
1338 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001339
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001340 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001341 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1342 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001343 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1344 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001345 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1346 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001347 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001348 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001349 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001350 }
1351
Daniel Walker113f04a2009-09-21 17:04:35 -07001352 if ($sanitise_quote eq '//') {
1353 $sanitise_quote = '';
1354 }
1355
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001356 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001357 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001358 my $clean = 'X' x length($1);
1359 $res =~ s@\<.*\>@<$clean>@;
1360
1361 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001362 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001363 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001364 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001365 }
1366
Joe Perchesdadf6802016-08-02 14:04:33 -07001367 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1368 my $match = $1;
1369 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1370 }
1371
Andy Whitcroft00df3442007-06-08 13:47:06 -07001372 return $res;
1373}
1374
Joe Perchesa6962d72013-04-29 16:18:13 -07001375sub get_quoted_string {
1376 my ($line, $rawline) = @_;
1377
Joe Perches478b1792018-04-10 16:33:34 -07001378 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001379 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001380 return substr($rawline, $-[0], $+[0] - $-[0]);
1381}
1382
Andy Whitcroft8905a672007-11-28 16:21:06 -08001383sub ctx_statement_block {
1384 my ($linenr, $remain, $off) = @_;
1385 my $line = $linenr - 1;
1386 my $blk = '';
1387 my $soff = $off;
1388 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001389 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001390
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001391 my $loff = 0;
1392
Andy Whitcroft8905a672007-11-28 16:21:06 -08001393 my $type = '';
1394 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001395 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001396 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001397 my $c;
1398 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001399
1400 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001401 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001402 @stack = (['', 0]) if ($#stack == -1);
1403
Andy Whitcroft773647a2008-03-28 14:15:58 -07001404 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001405 # If we are about to drop off the end, pull in more
1406 # context.
1407 if ($off >= $len) {
1408 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001409 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001410 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001411 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001412 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001413 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001414 $len = length($blk);
1415 $line++;
1416 last;
1417 }
1418 # Bail if there is no further context.
1419 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001420 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001421 last;
1422 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001423 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1424 $level++;
1425 $type = '#';
1426 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001427 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001428 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001429 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001430 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001431
Andy Whitcroft773647a2008-03-28 14:15:58 -07001432 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001433
1434 # Handle nested #if/#else.
1435 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1436 push(@stack, [ $type, $level ]);
1437 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1438 ($type, $level) = @{$stack[$#stack - 1]};
1439 } elsif ($remainder =~ /^#\s*endif\b/) {
1440 ($type, $level) = @{pop(@stack)};
1441 }
1442
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443 # Statement ends at the ';' or a close '}' at the
1444 # outermost level.
1445 if ($level == 0 && $c eq ';') {
1446 last;
1447 }
1448
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001449 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001450 if ($level == 0 && $coff_set == 0 &&
1451 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1452 $remainder =~ /^(else)(?:\s|{)/ &&
1453 $remainder !~ /^else\s+if\b/) {
1454 $coff = $off + length($1) - 1;
1455 $coff_set = 1;
1456 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1457 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001458 }
1459
Andy Whitcroft8905a672007-11-28 16:21:06 -08001460 if (($type eq '' || $type eq '(') && $c eq '(') {
1461 $level++;
1462 $type = '(';
1463 }
1464 if ($type eq '(' && $c eq ')') {
1465 $level--;
1466 $type = ($level != 0)? '(' : '';
1467
1468 if ($level == 0 && $coff < $soff) {
1469 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001470 $coff_set = 1;
1471 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001472 }
1473 }
1474 if (($type eq '' || $type eq '{') && $c eq '{') {
1475 $level++;
1476 $type = '{';
1477 }
1478 if ($type eq '{' && $c eq '}') {
1479 $level--;
1480 $type = ($level != 0)? '{' : '';
1481
1482 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001483 if (substr($blk, $off + 1, 1) eq ';') {
1484 $off++;
1485 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001486 last;
1487 }
1488 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001489 # Preprocessor commands end at the newline unless escaped.
1490 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1491 $level--;
1492 $type = '';
1493 $off++;
1494 last;
1495 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001496 $off++;
1497 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001498 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001499 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001500 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001501 $line++;
1502 $remain--;
1503 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001504
1505 my $statement = substr($blk, $soff, $off - $soff + 1);
1506 my $condition = substr($blk, $soff, $coff - $soff + 1);
1507
1508 #warn "STATEMENT<$statement>\n";
1509 #warn "CONDITION<$condition>\n";
1510
Andy Whitcroft773647a2008-03-28 14:15:58 -07001511 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001512
1513 return ($statement, $condition,
1514 $line, $remain + 1, $off - $loff + 1, $level);
1515}
1516
Andy Whitcroftcf655042008-03-04 14:28:20 -08001517sub statement_lines {
1518 my ($stmt) = @_;
1519
1520 # Strip the diff line prefixes and rip blank lines at start and end.
1521 $stmt =~ s/(^|\n)./$1/g;
1522 $stmt =~ s/^\s*//;
1523 $stmt =~ s/\s*$//;
1524
1525 my @stmt_lines = ($stmt =~ /\n/g);
1526
1527 return $#stmt_lines + 2;
1528}
1529
1530sub statement_rawlines {
1531 my ($stmt) = @_;
1532
1533 my @stmt_lines = ($stmt =~ /\n/g);
1534
1535 return $#stmt_lines + 2;
1536}
1537
1538sub statement_block_size {
1539 my ($stmt) = @_;
1540
1541 $stmt =~ s/(^|\n)./$1/g;
1542 $stmt =~ s/^\s*{//;
1543 $stmt =~ s/}\s*$//;
1544 $stmt =~ s/^\s*//;
1545 $stmt =~ s/\s*$//;
1546
1547 my @stmt_lines = ($stmt =~ /\n/g);
1548 my @stmt_statements = ($stmt =~ /;/g);
1549
1550 my $stmt_lines = $#stmt_lines + 2;
1551 my $stmt_statements = $#stmt_statements + 1;
1552
1553 if ($stmt_lines > $stmt_statements) {
1554 return $stmt_lines;
1555 } else {
1556 return $stmt_statements;
1557 }
1558}
1559
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001560sub ctx_statement_full {
1561 my ($linenr, $remain, $off) = @_;
1562 my ($statement, $condition, $level);
1563
1564 my (@chunks);
1565
Andy Whitcroftcf655042008-03-04 14:28:20 -08001566 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001567 ($statement, $condition, $linenr, $remain, $off, $level) =
1568 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001569 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001570 push(@chunks, [ $condition, $statement ]);
1571 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1572 return ($level, $linenr, @chunks);
1573 }
1574
1575 # Pull in the following conditional/block pairs and see if they
1576 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001577 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001578 ($statement, $condition, $linenr, $remain, $off, $level) =
1579 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001580 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001581 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001582 #print "C: push\n";
1583 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001584 }
1585
1586 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001587}
1588
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001589sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001590 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001591 my $line;
1592 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001593 my $blk = '';
1594 my @o;
1595 my @c;
1596 my @res = ();
1597
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001598 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001599 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001600 for ($line = $start; $remain > 0; $line++) {
1601 next if ($rawlines[$line] =~ /^-/);
1602 $remain--;
1603
1604 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001605
1606 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001607 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001608 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001609 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001610 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001611 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001612 $level = pop(@stack);
1613 }
1614
Andy Whitcroft01464f32010-10-26 14:23:19 -07001615 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001616 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1617 if ($off > 0) {
1618 $off--;
1619 next;
1620 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001621
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001622 if ($c eq $close && $level > 0) {
1623 $level--;
1624 last if ($level == 0);
1625 } elsif ($c eq $open) {
1626 $level++;
1627 }
1628 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001629
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001630 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001631 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001632 }
1633
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001634 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001635 }
1636
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001637 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001638}
1639sub ctx_block_outer {
1640 my ($linenr, $remain) = @_;
1641
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001642 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1643 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001644}
1645sub ctx_block {
1646 my ($linenr, $remain) = @_;
1647
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001648 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1649 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001650}
1651sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001652 my ($linenr, $remain, $off) = @_;
1653
1654 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1655 return @r;
1656}
1657sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001658 my ($linenr, $remain) = @_;
1659
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001660 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001661}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001662sub ctx_statement_level {
1663 my ($linenr, $remain, $off) = @_;
1664
1665 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1666}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001667
1668sub ctx_locate_comment {
1669 my ($first_line, $end_line) = @_;
1670
1671 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001672 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001673 return $current_comment if (defined $current_comment);
1674
1675 # Look through the context and try and figure out if there is a
1676 # comment.
1677 my $in_comment = 0;
1678 $current_comment = '';
1679 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001680 my $line = $rawlines[$linenr - 1];
1681 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001682 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1683 $in_comment = 1;
1684 }
1685 if ($line =~ m@/\*@) {
1686 $in_comment = 1;
1687 }
1688 if (!$in_comment && $current_comment ne '') {
1689 $current_comment = '';
1690 }
1691 $current_comment .= $line . "\n" if ($in_comment);
1692 if ($line =~ m@\*/@) {
1693 $in_comment = 0;
1694 }
1695 }
1696
1697 chomp($current_comment);
1698 return($current_comment);
1699}
1700sub ctx_has_comment {
1701 my ($first_line, $end_line) = @_;
1702 my $cmt = ctx_locate_comment($first_line, $end_line);
1703
Andy Whitcroft00df3442007-06-08 13:47:06 -07001704 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001705 ##print "CMMT: $cmt\n";
1706
1707 return ($cmt ne '');
1708}
1709
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001710sub raw_line {
1711 my ($linenr, $cnt) = @_;
1712
1713 my $offset = $linenr - 1;
1714 $cnt++;
1715
1716 my $line;
1717 while ($cnt) {
1718 $line = $rawlines[$offset++];
1719 next if (defined($line) && $line =~ /^-/);
1720 $cnt--;
1721 }
1722
1723 return $line;
1724}
1725
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001726sub get_stat_real {
1727 my ($linenr, $lc) = @_;
1728
1729 my $stat_real = raw_line($linenr, 0);
1730 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1731 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1732 }
1733
1734 return $stat_real;
1735}
1736
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001737sub get_stat_here {
1738 my ($linenr, $cnt, $here) = @_;
1739
1740 my $herectx = $here . "\n";
1741 for (my $n = 0; $n < $cnt; $n++) {
1742 $herectx .= raw_line($linenr, $n) . "\n";
1743 }
1744
1745 return $herectx;
1746}
1747
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001748sub cat_vet {
1749 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001750 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001751
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001752 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001753 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1754 $res .= $1;
1755 if ($2 ne '') {
1756 $coded = sprintf("^%c", unpack('C', $2) + 64);
1757 $res .= $coded;
1758 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001759 }
1760 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001761
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001762 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001763}
1764
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001765my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001766my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001767my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001768my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001769
1770sub annotate_reset {
1771 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001772 $av_pending = '_';
1773 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001774 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001775}
1776
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001777sub annotate_values {
1778 my ($stream, $type) = @_;
1779
1780 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001781 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001782 my $cur = $stream;
1783
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001784 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001785
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001786 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001787 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001788 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001789 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001790 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001791 print "WS($1)\n" if ($dbg_values > 1);
1792 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001793 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001794 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001795 }
1796
Florian Micklerc023e4732011-01-12 16:59:58 -08001797 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001798 print "CAST($1)\n" if ($dbg_values > 1);
1799 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001800 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001801
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001802 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001803 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001804 $type = 'T';
1805
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001806 } elsif ($cur =~ /^($Modifier)\s*/) {
1807 print "MODIFIER($1)\n" if ($dbg_values > 1);
1808 $type = 'T';
1809
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001810 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001811 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001812 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001813 push(@av_paren_type, $type);
1814 if ($2 ne '') {
1815 $av_pending = 'N';
1816 }
1817 $type = 'E';
1818
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001819 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001820 print "UNDEF($1)\n" if ($dbg_values > 1);
1821 $av_preprocessor = 1;
1822 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001823
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001824 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001825 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001826 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001827
1828 push(@av_paren_type, $type);
1829 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001830 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001831
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001832 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001833 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1834 $av_preprocessor = 1;
1835
1836 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1837
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001838 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001839
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001840 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001841 print "PRE_END($1)\n" if ($dbg_values > 1);
1842
1843 $av_preprocessor = 1;
1844
1845 # Assume all arms of the conditional end as this
1846 # one does, and continue as if the #endif was not here.
1847 pop(@av_paren_type);
1848 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001849 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001850
1851 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001852 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001853
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001854 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1855 print "ATTR($1)\n" if ($dbg_values > 1);
1856 $av_pending = $type;
1857 $type = 'N';
1858
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001859 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001860 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001861 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001862 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001863 }
1864 $type = 'N';
1865
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001866 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001867 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001868 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001869 $type = 'N';
1870
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001871 } elsif ($cur =~/^(case)/o) {
1872 print "CASE($1)\n" if ($dbg_values > 1);
1873 $av_pend_colon = 'C';
1874 $type = 'N';
1875
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001876 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001877 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001878 $type = 'N';
1879
1880 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001881 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001882 push(@av_paren_type, $av_pending);
1883 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001884 $type = 'N';
1885
1886 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001887 my $new_type = pop(@av_paren_type);
1888 if ($new_type ne '_') {
1889 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001890 print "PAREN('$1') -> $type\n"
1891 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001892 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001893 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001894 }
1895
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001896 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001897 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001898 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001899 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001900
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001901 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1902 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001903 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001904 } elsif ($type eq 'E') {
1905 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001906 }
1907 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1908 $type = 'V';
1909
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001910 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001911 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001912 $type = 'V';
1913
1914 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001915 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001916 $type = 'N';
1917
Andy Whitcroftcf655042008-03-04 14:28:20 -08001918 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001919 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001920 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001921 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001922
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001923 } elsif ($cur =~/^(,)/) {
1924 print "COMMA($1)\n" if ($dbg_values > 1);
1925 $type = 'C';
1926
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001927 } elsif ($cur =~ /^(\?)/o) {
1928 print "QUESTION($1)\n" if ($dbg_values > 1);
1929 $type = 'N';
1930
1931 } elsif ($cur =~ /^(:)/o) {
1932 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1933
1934 substr($var, length($res), 1, $av_pend_colon);
1935 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1936 $type = 'E';
1937 } else {
1938 $type = 'N';
1939 }
1940 $av_pend_colon = 'O';
1941
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001942 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001943 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001944 $type = 'N';
1945
Andy Whitcroft0d413862008-10-15 22:02:16 -07001946 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001947 my $variant;
1948
1949 print "OPV($1)\n" if ($dbg_values > 1);
1950 if ($type eq 'V') {
1951 $variant = 'B';
1952 } else {
1953 $variant = 'U';
1954 }
1955
1956 substr($var, length($res), 1, $variant);
1957 $type = 'N';
1958
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001959 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001960 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001961 if ($1 ne '++' && $1 ne '--') {
1962 $type = 'N';
1963 }
1964
1965 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001966 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001967 }
1968 if (defined $1) {
1969 $cur = substr($cur, length($1));
1970 $res .= $type x length($1);
1971 }
1972 }
1973
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001974 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001975}
1976
Andy Whitcroft8905a672007-11-28 16:21:06 -08001977sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001978 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001979 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001980 ^(?:
1981 $Modifier|
1982 $Storage|
1983 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001984 DEFINE_\S+
1985 )$|
1986 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001987 goto|
1988 return|
1989 case|
1990 else|
1991 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001992 do|
1993 \#|
1994 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001995 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001996 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001997 )}x;
1998 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1999 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002000 # Check for modifiers.
2001 $possible =~ s/\s*$Storage\s*//g;
2002 $possible =~ s/\s*$Sparse\s*//g;
2003 if ($possible =~ /^\s*$/) {
2004
2005 } elsif ($possible =~ /\s/) {
2006 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002007 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002008 if ($modifier !~ $notPermitted) {
2009 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002010 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002011 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002012 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002013
2014 } else {
2015 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002016 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002017 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002018 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002019 } else {
2020 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002021 }
2022}
2023
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002024my $prefix = '';
2025
Joe Perches000d1cc12011-07-25 17:13:25 -07002026sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002027 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002028
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002029 $type =~ tr/[a-z]/[A-Z]/;
2030
Joe Perchescbec18a2014-04-03 14:49:19 -07002031 return defined $use_type{$type} if (scalar keys %use_type > 0);
2032
2033 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002034}
2035
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002036sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002037 my ($level, $type, $msg) = @_;
2038
2039 if (!show_type($type) ||
2040 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002041 return 0;
2042 }
Joe Perches57230292015-06-25 15:03:03 -07002043 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002044 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002045 if ($level eq 'ERROR') {
2046 $output .= RED;
2047 } elsif ($level eq 'WARNING') {
2048 $output .= YELLOW;
2049 } else {
2050 $output .= GREEN;
2051 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002052 }
Joe Perches57230292015-06-25 15:03:03 -07002053 $output .= $prefix . $level . ':';
2054 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002055 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002056 $output .= "$type:";
2057 }
John Brooks737c0762017-07-10 15:52:24 -07002058 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002059 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002060
2061 if ($showfile) {
2062 my @lines = split("\n", $output, -1);
2063 splice(@lines, 1, 1);
2064 $output = join("\n", @lines);
2065 }
Joe Perches57230292015-06-25 15:03:03 -07002066 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002067
Joe Perches57230292015-06-25 15:03:03 -07002068 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002069
2070 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002071}
Joe Perchescbec18a2014-04-03 14:49:19 -07002072
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002073sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002074 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002075}
Joe Perches000d1cc12011-07-25 17:13:25 -07002076
Joe Perchesd752fcc2014-08-06 16:11:05 -07002077sub fixup_current_range {
2078 my ($lineRef, $offset, $length) = @_;
2079
2080 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2081 my $o = $1;
2082 my $l = $2;
2083 my $no = $o + $offset;
2084 my $nl = $l + $length;
2085 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2086 }
2087}
2088
2089sub fix_inserted_deleted_lines {
2090 my ($linesRef, $insertedRef, $deletedRef) = @_;
2091
2092 my $range_last_linenr = 0;
2093 my $delta_offset = 0;
2094
2095 my $old_linenr = 0;
2096 my $new_linenr = 0;
2097
2098 my $next_insert = 0;
2099 my $next_delete = 0;
2100
2101 my @lines = ();
2102
2103 my $inserted = @{$insertedRef}[$next_insert++];
2104 my $deleted = @{$deletedRef}[$next_delete++];
2105
2106 foreach my $old_line (@{$linesRef}) {
2107 my $save_line = 1;
2108 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002109 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002110 $delta_offset = 0;
2111 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2112 $range_last_linenr = $new_linenr;
2113 fixup_current_range(\$line, $delta_offset, 0);
2114 }
2115
2116 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2117 $deleted = @{$deletedRef}[$next_delete++];
2118 $save_line = 0;
2119 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2120 }
2121
2122 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2123 push(@lines, ${$inserted}{'LINE'});
2124 $inserted = @{$insertedRef}[$next_insert++];
2125 $new_linenr++;
2126 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2127 }
2128
2129 if ($save_line) {
2130 push(@lines, $line);
2131 $new_linenr++;
2132 }
2133
2134 $old_linenr++;
2135 }
2136
2137 return @lines;
2138}
2139
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002140sub fix_insert_line {
2141 my ($linenr, $line) = @_;
2142
2143 my $inserted = {
2144 LINENR => $linenr,
2145 LINE => $line,
2146 };
2147 push(@fixed_inserted, $inserted);
2148}
2149
2150sub fix_delete_line {
2151 my ($linenr, $line) = @_;
2152
2153 my $deleted = {
2154 LINENR => $linenr,
2155 LINE => $line,
2156 };
2157
2158 push(@fixed_deleted, $deleted);
2159}
2160
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002161sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002162 my ($type, $msg) = @_;
2163
2164 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002165 our $clean = 0;
2166 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002167 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002168 }
Joe Perches3705ce52013-07-03 15:05:31 -07002169 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002170}
2171sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002172 my ($type, $msg) = @_;
2173
2174 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002175 our $clean = 0;
2176 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002177 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002178 }
Joe Perches3705ce52013-07-03 15:05:31 -07002179 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002180}
2181sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002182 my ($type, $msg) = @_;
2183
2184 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002185 our $clean = 0;
2186 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002187 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002188 }
Joe Perches3705ce52013-07-03 15:05:31 -07002189 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002190}
2191
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002192sub check_absolute_file {
2193 my ($absolute, $herecurr) = @_;
2194 my $file = $absolute;
2195
2196 ##print "absolute<$absolute>\n";
2197
2198 # See if any suffix of this path is a path within the tree.
2199 while ($file =~ s@^[^/]*/@@) {
2200 if (-f "$root/$file") {
2201 ##print "file<$file>\n";
2202 last;
2203 }
2204 }
2205 if (! -f _) {
2206 return 0;
2207 }
2208
2209 # It is, so see if the prefix is acceptable.
2210 my $prefix = $absolute;
2211 substr($prefix, -length($file)) = '';
2212
2213 ##print "prefix<$prefix>\n";
2214 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002215 WARN("USE_RELATIVE_PATH",
2216 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002217 }
2218}
2219
Joe Perches3705ce52013-07-03 15:05:31 -07002220sub trim {
2221 my ($string) = @_;
2222
Joe Perchesb34c6482013-09-11 14:24:01 -07002223 $string =~ s/^\s+|\s+$//g;
2224
2225 return $string;
2226}
2227
2228sub ltrim {
2229 my ($string) = @_;
2230
2231 $string =~ s/^\s+//;
2232
2233 return $string;
2234}
2235
2236sub rtrim {
2237 my ($string) = @_;
2238
2239 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002240
2241 return $string;
2242}
2243
Joe Perches52ea8502013-11-12 15:10:09 -08002244sub string_find_replace {
2245 my ($string, $find, $replace) = @_;
2246
2247 $string =~ s/$find/$replace/g;
2248
2249 return $string;
2250}
2251
Joe Perches3705ce52013-07-03 15:05:31 -07002252sub tabify {
2253 my ($leading) = @_;
2254
2255 my $source_indent = 8;
2256 my $max_spaces_before_tab = $source_indent - 1;
2257 my $spaces_to_tab = " " x $source_indent;
2258
2259 #convert leading spaces to tabs
2260 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2261 #Remove spaces before a tab
2262 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2263
2264 return "$leading";
2265}
2266
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002267sub pos_last_openparen {
2268 my ($line) = @_;
2269
2270 my $pos = 0;
2271
2272 my $opens = $line =~ tr/\(/\(/;
2273 my $closes = $line =~ tr/\)/\)/;
2274
2275 my $last_openparen = 0;
2276
2277 if (($opens == 0) || ($closes >= $opens)) {
2278 return -1;
2279 }
2280
2281 my $len = length($line);
2282
2283 for ($pos = 0; $pos < $len; $pos++) {
2284 my $string = substr($line, $pos);
2285 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2286 $pos += length($1) - 1;
2287 } elsif (substr($line, $pos, 1) eq '(') {
2288 $last_openparen = $pos;
2289 } elsif (index($string, '(') == -1) {
2290 last;
2291 }
2292 }
2293
Joe Perches91cb5192014-04-03 14:49:32 -07002294 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002295}
2296
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002297sub process {
2298 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002299
2300 my $linenr=0;
2301 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002302 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002303 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002304 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002305
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002306 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002307 my $indent;
2308 my $previndent=0;
2309 my $stashindent=0;
2310
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002311 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002312 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002313 my $author = '';
2314 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002315 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002316 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002317 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002318 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002319 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002320 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002321 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002322 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002323 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002324 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002325 my $non_utf8_charset = 0;
2326
Joe Perches365dd4e2014-08-06 16:10:42 -07002327 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002328 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002329
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002330 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002331 our $cnt_lines = 0;
2332 our $cnt_error = 0;
2333 our $cnt_warn = 0;
2334 our $cnt_chk = 0;
2335
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002336 # Trace the real file/line as we go.
2337 my $realfile = '';
2338 my $realline = 0;
2339 my $realcnt = 0;
2340 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002341 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002342 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002343 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002344 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002345 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002346
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002347 my $prev_values = 'E';
2348
2349 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002350 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002351 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002352 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002353 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002354
Joe Perches7e51f192013-09-11 14:23:57 -07002355 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002356
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002357 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002358 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002359 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002360 my @setup_docs = ();
2361 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002362
Joe Perchesd8b07712013-11-12 15:10:06 -08002363 my $camelcase_file_seeded = 0;
2364
Rob Herring9f3a8992018-04-10 16:33:13 -07002365 my $checklicenseline = 1;
2366
Andy Whitcroft773647a2008-03-28 14:15:58 -07002367 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002368 my $line;
2369 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002370 $linenr++;
2371 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002372
Joe Perches3705ce52013-07-03 15:05:31 -07002373 push(@fixed, $rawline) if ($fix);
2374
Andy Whitcroft773647a2008-03-28 14:15:58 -07002375 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002376 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002377 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002378 $setup_docs = 1;
2379 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002380 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002381 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002382 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002383 $realline=$1-1;
2384 if (defined $2) {
2385 $realcnt=$3+1;
2386 } else {
2387 $realcnt=1+1;
2388 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002389 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002390
2391 # Guestimate if this is a continuing comment. Run
2392 # the context looking for a comment "edge". If this
2393 # edge is a close comment then we must be in a comment
2394 # at context start.
2395 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002396 my $cnt = $realcnt;
2397 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2398 next if (defined $rawlines[$ln - 1] &&
2399 $rawlines[$ln - 1] =~ /^-/);
2400 $cnt--;
2401 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002402 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002403 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2404 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2405 ($edge) = $1;
2406 last;
2407 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002408 }
2409 if (defined $edge && $edge eq '*/') {
2410 $in_comment = 1;
2411 }
2412
2413 # Guestimate if this is a continuing comment. If this
2414 # is the start of a diff block and this line starts
2415 # ' *' then it is very likely a comment.
2416 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002417 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002418 {
2419 $in_comment = 1;
2420 }
2421
2422 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2423 sanitise_line_reset($in_comment);
2424
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002425 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002426 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002427 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002428 $line = sanitise_line($rawline);
2429 }
2430 push(@lines, $line);
2431
2432 if ($realcnt > 1) {
2433 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2434 } else {
2435 $realcnt = 0;
2436 }
2437
2438 #print "==>$rawline\n";
2439 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002440
2441 if ($setup_docs && $line =~ /^\+/) {
2442 push(@setup_docs, $line);
2443 }
2444 }
2445
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002446 $prefix = '';
2447
Andy Whitcroft773647a2008-03-28 14:15:58 -07002448 $realcnt = 0;
2449 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002450 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002451 foreach my $line (@lines) {
2452 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002453 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002454 my $sline = $line; #copy of $line
2455 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002456
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002457 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002458
Joe Perches12c253a2018-06-07 17:10:58 -07002459# check if it's a mode change, rename or start of a patch
2460 if (!$in_commit_log &&
2461 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2462 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2463 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2464 $is_patch = 1;
2465 }
2466
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002467#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002468 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002469 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2470 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002471 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002472 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002473 $realline=$1-1;
2474 if (defined $2) {
2475 $realcnt=$3+1;
2476 } else {
2477 $realcnt=1+1;
2478 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002479 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002480 $prev_values = 'E';
2481
Andy Whitcroft773647a2008-03-28 14:15:58 -07002482 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002483 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002484 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002485 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002486 if ($context =~ /\b(\w+)\s*\(/) {
2487 $context_function = $1;
2488 } else {
2489 undef $context_function;
2490 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002491 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002492
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002493# track the line number as we move through the hunk, note that
2494# new versions of GNU diff omit the leading space on completely
2495# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002496 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002497 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002498 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002499
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002500 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002501 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002502
2503 # Track the previous line.
2504 ($prevline, $stashline) = ($stashline, $line);
2505 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002506 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2507
Andy Whitcroft773647a2008-03-28 14:15:58 -07002508 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002509
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002510 } elsif ($realcnt == 1) {
2511 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002512 }
2513
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002514 my $hunk_line = ($realcnt != 0);
2515
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002516 $here = "#$linenr: " if (!$file);
2517 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002518
Joe Perches2ac73b42014-06-04 16:12:05 -07002519 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002520 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002521 if ($line =~ /^diff --git.*?(\S+)$/) {
2522 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002523 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002524 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002525 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002526 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002527 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002528 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002529 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002530
2531 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002532 if (!$file && $tree && $p1_prefix ne '' &&
2533 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002534 WARN("PATCH_PREFIX",
2535 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002536 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002537
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002538 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002539 ERROR("MODIFIED_INCLUDE_ASM",
2540 "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 -07002541 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002542 $found_file = 1;
2543 }
2544
Joe Perches34d88152015-06-25 15:03:05 -07002545#make up the handle for any error we report on this line
2546 if ($showfile) {
2547 $prefix = "$realfile:$realline: "
2548 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002549 if ($file) {
2550 $prefix = "$filename:$realline: ";
2551 } else {
2552 $prefix = "$filename:$linenr: ";
2553 }
Joe Perches34d88152015-06-25 15:03:05 -07002554 }
2555
Joe Perches2ac73b42014-06-04 16:12:05 -07002556 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002557 if (is_maintained_obsolete($realfile)) {
2558 WARN("OBSOLETE",
2559 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2560 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002561 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002562 $check = 1;
2563 } else {
2564 $check = $check_orig;
2565 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002566 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002567
2568 if ($realfile !~ /^MAINTAINERS/) {
2569 my $last_binding_patch = $is_binding_patch;
2570
2571 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2572
2573 if (($last_binding_patch != -1) &&
2574 ($last_binding_patch ^ $is_binding_patch)) {
2575 WARN("DT_SPLIT_BINDING_PATCH",
2576 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2577 }
2578 }
2579
Andy Whitcroft773647a2008-03-28 14:15:58 -07002580 next;
2581 }
2582
Randy Dunlap389834b2007-06-08 13:47:03 -07002583 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002584
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002585 my $hereline = "$here\n$rawline\n";
2586 my $herecurr = "$here\n$rawline\n";
2587 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002588
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002589 $cnt_lines++ if ($realcnt != 0);
2590
Joe Perches490b2922018-08-21 21:58:01 -07002591# Verify the existence of a commit log if appropriate
2592# 2 is used because a $signature is counted in $commit_log_lines
2593 if ($in_commit_log) {
2594 if ($line !~ /^\s*$/) {
2595 $commit_log_lines++; #could be a $signature
2596 }
2597 } elsif ($has_commit_log && $commit_log_lines < 2) {
2598 WARN("COMMIT_MESSAGE",
2599 "Missing commit description - Add an appropriate one\n");
2600 $commit_log_lines = 2; #warn only once
2601 }
2602
Joe Perchese518e9a2015-06-25 15:03:27 -07002603# Check if the commit log has what seems like a diff which can confuse patch
2604 if ($in_commit_log && !$commit_log_has_diff &&
2605 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2606 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2607 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2608 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2609 ERROR("DIFF_IN_COMMIT_MSG",
2610 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2611 $commit_log_has_diff = 1;
2612 }
2613
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002614# Check for incorrect file permissions
2615 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2616 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002617 if ($realfile !~ m@scripts/@ &&
2618 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002619 ERROR("EXECUTE_PERMISSIONS",
2620 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002621 }
2622 }
2623
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002624# Check the patch for a From:
2625 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2626 $author = $1;
2627 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2628 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002629 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002630 }
2631
Joe Perches20112472011-07-25 17:13:23 -07002632# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002633 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002634 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002635 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002636 if ($author ne '') {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002637 if (same_email_addresses($1, $author)) {
2638 $authorsignoff = 1;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002639 }
2640 }
Joe Perches20112472011-07-25 17:13:23 -07002641 }
2642
Joe Perchese0d975b2014-12-10 15:51:49 -08002643# Check if MAINTAINERS is being updated. If so, there's probably no need to
2644# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2645 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2646 $reported_maintainer_file = 1;
2647 }
2648
Joe Perches20112472011-07-25 17:13:23 -07002649# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002650 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002651 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002652 my $space_before = $1;
2653 my $sign_off = $2;
2654 my $space_after = $3;
2655 my $email = $4;
2656 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2657
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002658 if ($sign_off !~ /$signature_tags/) {
2659 WARN("BAD_SIGN_OFF",
2660 "Non-standard signature: $sign_off\n" . $herecurr);
2661 }
Joe Perches20112472011-07-25 17:13:23 -07002662 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002663 if (WARN("BAD_SIGN_OFF",
2664 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2665 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002666 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002667 "$ucfirst_sign_off $email";
2668 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002669 }
Joe Perches20112472011-07-25 17:13:23 -07002670 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002671 if (WARN("BAD_SIGN_OFF",
2672 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2673 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002674 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002675 "$ucfirst_sign_off $email";
2676 }
2677
Joe Perches20112472011-07-25 17:13:23 -07002678 }
2679 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002680 if (WARN("BAD_SIGN_OFF",
2681 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2682 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002683 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002684 "$ucfirst_sign_off $email";
2685 }
Joe Perches20112472011-07-25 17:13:23 -07002686 }
2687
Joe Perchesdfa05c22020-04-06 20:10:48 -07002688 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Perches20112472011-07-25 17:13:23 -07002689 my $suggested_email = format_email(($email_name, $email_address));
2690 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002691 ERROR("BAD_SIGN_OFF",
2692 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002693 } else {
2694 my $dequoted = $suggested_email;
2695 $dequoted =~ s/^"//;
2696 $dequoted =~ s/" </ </;
2697 # Don't force email to have quotes
2698 # Allow just an angle bracketed address
Joe Perchesdfa05c22020-04-06 20:10:48 -07002699 if (!same_email_addresses($email, $suggested_email)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002700 WARN("BAD_SIGN_OFF",
2701 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002702 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002703 }
Joe Perches7e51f192013-09-11 14:23:57 -07002704
2705# Check for duplicate signatures
2706 my $sig_nospace = $line;
2707 $sig_nospace =~ s/\s//g;
2708 $sig_nospace = lc($sig_nospace);
2709 if (defined $signatures{$sig_nospace}) {
2710 WARN("BAD_SIGN_OFF",
2711 "Duplicate signature\n" . $herecurr);
2712 } else {
2713 $signatures{$sig_nospace} = 1;
2714 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002715
2716# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2717 if ($sign_off =~ /^co-developed-by:$/i) {
2718 if ($email eq $author) {
2719 WARN("BAD_SIGN_OFF",
2720 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2721 }
2722 if (!defined $lines[$linenr]) {
2723 WARN("BAD_SIGN_OFF",
2724 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2725 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2726 WARN("BAD_SIGN_OFF",
2727 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2728 } elsif ($1 ne $email) {
2729 WARN("BAD_SIGN_OFF",
2730 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2731 }
2732 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002733 }
2734
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002735# Check email subject for common tools that don't need to be mentioned
2736 if ($in_header_lines &&
2737 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2738 WARN("EMAIL_SUBJECT",
2739 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2740 }
2741
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002742# Check for unwanted Gerrit info
2743 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2744 ERROR("GERRIT_CHANGE_ID",
2745 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2746 }
2747
Joe Perches369c8dd2015-11-06 16:31:34 -08002748# Check if the commit log is in a possible stack dump
2749 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2750 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2751 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2752 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002753 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2754 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2755 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2756 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002757 $commit_log_possible_stack_dump = 1;
2758 }
2759
Joe Perches2a076f42015-04-16 12:44:28 -07002760# Check for line lengths > 75 in commit log, warn once
2761 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002762 length($line) > 75 &&
2763 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2764 # file delta changes
2765 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2766 # filename then :
2767 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2768 # A Fixes: or Link: line
2769 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002770 WARN("COMMIT_LOG_LONG_LINE",
2771 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2772 $commit_log_long_line = 1;
2773 }
2774
Joe Perchesbf4daf12015-09-09 15:37:50 -07002775# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002776 if ($in_commit_log && $commit_log_possible_stack_dump &&
2777 $line =~ /^\s*$/) {
2778 $commit_log_possible_stack_dump = 0;
2779 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002780
Joe Perches0d7835f2015-02-13 14:38:35 -08002781# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002782 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002783 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002784 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002785 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002786 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002787 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2788 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002789 my $init_char = "c";
2790 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002791 my $short = 1;
2792 my $long = 0;
2793 my $case = 1;
2794 my $space = 1;
2795 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002796 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002797 my $id = '0123456789ab';
2798 my $orig_desc = "commit description";
2799 my $description = "";
2800
Joe Perchesfe043ea2015-09-09 15:37:25 -07002801 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2802 $init_char = $1;
2803 $orig_commit = lc($2);
2804 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2805 $orig_commit = lc($1);
2806 }
2807
Joe Perches0d7835f2015-02-13 14:38:35 -08002808 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2809 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2810 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2811 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2812 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2813 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002814 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002815 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2816 defined $rawlines[$linenr] &&
2817 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2818 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002819 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002820 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2821 defined $rawlines[$linenr] &&
2822 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2823 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2824 $orig_desc = $1;
2825 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2826 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002827 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002828 }
2829
2830 ($id, $description) = git_commit_info($orig_commit,
2831 $id, $orig_desc);
2832
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002833 if (defined($id) &&
2834 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002835 ERROR("GIT_COMMIT_ID",
2836 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2837 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002838 }
2839
Joe Perches13f19372014-08-06 16:10:59 -07002840# Check for added, moved or deleted files
2841 if (!$reported_maintainer_file && !$in_commit_log &&
2842 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2843 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2844 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2845 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002846 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002847 $reported_maintainer_file = 1;
2848 WARN("FILE_PATH_CHANGES",
2849 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2850 }
2851
Rob Herringe400edb2019-09-12 15:18:20 +01002852# Check for adding new DT bindings not in schema format
2853 if (!$in_commit_log &&
2854 ($line =~ /^new file mode\s*\d+\s*$/) &&
2855 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2856 WARN("DT_SCHEMA_BINDING_PATCH",
2857 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2858 }
2859
Andy Whitcroft00df3442007-06-08 13:47:06 -07002860# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002861 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002862 ERROR("CORRUPTED_PATCH",
2863 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002864 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002865 }
2866
2867# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2868 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002869 $rawline !~ m/^$UTF8*$/) {
2870 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2871
2872 my $blank = copy_spacing($rawline);
2873 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2874 my $hereptr = "$hereline$ptr\n";
2875
Joe Perches34d99212011-07-25 17:13:26 -07002876 CHK("INVALID_UTF8",
2877 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002878 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002879
Joe Perches15662b32011-10-31 17:13:12 -07002880# Check if it's the start of a commit log
2881# (not a header line and we haven't seen the patch filename)
2882 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002883 !($rawline =~ /^\s+(?:\S|$)/ ||
2884 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002885 $in_header_lines = 0;
2886 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002887 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002888 }
2889
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002890# Check if there is UTF-8 in a commit log when a mail header has explicitly
2891# declined it, i.e defined some charset where it is missing.
2892 if ($in_header_lines &&
2893 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2894 $1 !~ /utf-8/i) {
2895 $non_utf8_charset = 1;
2896 }
2897
2898 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002899 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002900 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002901 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2902 }
2903
Joe Perchesd6430f72016-12-12 16:46:28 -08002904# Check for absolute kernel paths in commit message
2905 if ($tree && $in_commit_log) {
2906 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2907 my $file = $1;
2908
2909 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2910 check_absolute_file($1, $herecurr)) {
2911 #
2912 } else {
2913 check_absolute_file($file, $herecurr);
2914 }
2915 }
2916 }
2917
Kees Cook66b47b42014-10-13 15:51:57 -07002918# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002919 if (defined($misspellings) &&
2920 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002921 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002922 my $typo = $1;
2923 my $typo_fix = $spelling_fix{lc($typo)};
2924 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2925 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002926 my $msg_level = \&WARN;
2927 $msg_level = \&CHK if ($file);
2928 if (&{$msg_level}("TYPO_SPELLING",
2929 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002930 $fix) {
2931 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2932 }
2933 }
2934 }
2935
Matteo Crocea8dd86b2019-09-25 16:46:38 -07002936# check for invalid commit id
2937 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2938 my $id;
2939 my $description;
2940 ($id, $description) = git_commit_info($2, undef, undef);
2941 if (!defined($id)) {
2942 WARN("UNKNOWN_COMMIT_ID",
2943 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2944 }
2945 }
2946
Andy Whitcroft306708542008-10-15 22:02:28 -07002947# ignore non-hunk lines and lines being removed
2948 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002949
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002950#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002951 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002952 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002953 if (ERROR("DOS_LINE_ENDINGS",
2954 "DOS line endings\n" . $herevet) &&
2955 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002956 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002957 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002958 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2959 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002960 if (ERROR("TRAILING_WHITESPACE",
2961 "trailing whitespace\n" . $herevet) &&
2962 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002963 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002964 }
2965
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002966 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002967 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002968
Josh Triplett4783f892013-11-12 15:10:12 -08002969# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002970 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002971 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002972 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2973 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002974 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002975 my $msg_level = \&ERROR;
2976 $msg_level = \&CHK if ($file);
2977 &{$msg_level}("FSF_MAILING_ADDRESS",
2978 "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 -08002979 }
2980
Andi Kleen33549572010-05-24 14:33:29 -07002981# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002982# Only applies when adding the entry originally, after that we do not have
2983# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002984 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002985 # 'choice' is usually the last thing on the line (though
2986 # Kconfig supports named choices), so use a word boundary
2987 # (\b) rather than a whitespace character (\s)
2988 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002989 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002990 my $cnt = $realcnt;
2991 my $ln = $linenr + 1;
2992 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002993 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002994 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002995 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002996 $f = $lines[$ln - 1];
2997 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2998 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002999
3000 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003001 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003002
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003003 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003004 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01003005 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3006 if ($lines[$ln - 1] =~ "---help---") {
3007 WARN("CONFIG_DESCRIPTION",
3008 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3009 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003010 $length = -1;
3011 }
3012
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003013 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003014 $f =~ s/#.*//;
3015 $f =~ s/^\s+//;
3016 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003017
3018 # This only checks context lines in the patch
3019 # and so hopefully shouldn't trigger false
3020 # positives, even though some of these are
3021 # common words in help texts
3022 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3023 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003024 $is_end = 1;
3025 last;
3026 }
Andi Kleen33549572010-05-24 14:33:29 -07003027 $length++;
3028 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003029 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3030 WARN("CONFIG_DESCRIPTION",
3031 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3032 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003033 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003034 }
3035
Joe Perches628f91a2017-07-10 15:52:07 -07003036# check for MAINTAINERS entries that don't have the right form
3037 if ($realfile =~ /^MAINTAINERS$/ &&
3038 $rawline =~ /^\+[A-Z]:/ &&
3039 $rawline !~ /^\+[A-Z]:\t\S/) {
3040 if (WARN("MAINTAINERS_STYLE",
3041 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3042 $fix) {
3043 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3044 }
3045 }
3046
Christoph Jaeger327953e2015-02-13 14:38:29 -08003047# discourage the use of boolean for type definition attributes of Kconfig options
3048 if ($realfile =~ /Kconfig/ &&
3049 $line =~ /^\+\s*\bboolean\b/) {
3050 WARN("CONFIG_TYPE_BOOLEAN",
3051 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3052 }
3053
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003054 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3055 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3056 my $flag = $1;
3057 my $replacement = {
3058 'EXTRA_AFLAGS' => 'asflags-y',
3059 'EXTRA_CFLAGS' => 'ccflags-y',
3060 'EXTRA_CPPFLAGS' => 'cppflags-y',
3061 'EXTRA_LDFLAGS' => 'ldflags-y',
3062 };
3063
3064 WARN("DEPRECATED_VARIABLE",
3065 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3066 }
3067
Rob Herringbff5da42014-01-23 15:54:51 -08003068# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003069 if (defined $root &&
3070 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3071 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3072
Rob Herringbff5da42014-01-23 15:54:51 -08003073 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3074
Florian Vaussardcc933192014-04-03 14:49:27 -07003075 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003076 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003077
Rob Herringbff5da42014-01-23 15:54:51 -08003078 foreach my $compat (@compats) {
3079 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003080 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3081 my $compat3 = $compat;
3082 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3083 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003084 if ( $? >> 8 ) {
3085 WARN("UNDOCUMENTED_DT_STRING",
3086 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3087 }
3088
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003089 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3090 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003091 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003092 if ( $? >> 8 ) {
3093 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003094 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003095 }
3096 }
3097 }
3098
Rob Herring9f3a8992018-04-10 16:33:13 -07003099# check for using SPDX license tag at beginning of files
3100 if ($realline == $checklicenseline) {
3101 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3102 $checklicenseline = 2;
3103 } elsif ($rawline =~ /^\+/) {
3104 my $comment = "";
3105 if ($realfile =~ /\.(h|s|S)$/) {
3106 $comment = '/*';
3107 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3108 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003109 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003110 $comment = '#';
3111 } elsif ($realfile =~ /\.rst$/) {
3112 $comment = '..';
3113 }
3114
Joe Perchesfdf13692019-03-07 16:28:32 -08003115# check SPDX comment style for .[chsS] files
3116 if ($realfile =~ /\.[chsS]$/ &&
3117 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003118 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003119 WARN("SPDX_LICENSE_TAG",
3120 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3121 }
3122
Rob Herring9f3a8992018-04-10 16:33:13 -07003123 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003124 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3125 WARN("SPDX_LICENSE_TAG",
3126 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003127 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003128 my $spdx_license = $1;
3129 if (!is_SPDX_License_valid($spdx_license)) {
3130 WARN("SPDX_LICENSE_TAG",
3131 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3132 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003133 }
3134 }
3135 }
3136
Andy Whitcroft5368df202008-10-15 22:02:27 -07003137# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003138 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003139
Joe Perchesa8da38a2019-03-07 16:28:42 -08003140# check for using SPDX-License-Identifier on the wrong line number
3141 if ($realline != $checklicenseline &&
3142 $rawline =~ /\bSPDX-License-Identifier:/ &&
3143 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3144 WARN("SPDX_LICENSE_TAG",
3145 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3146 }
3147
Joe Perches47e0c882015-06-25 15:02:57 -07003148# line length limit (with some exclusions)
3149#
3150# There are a few types of lines that may extend beyond $max_line_length:
3151# logging functions like pr_info that end in a string
3152# lines with a single string
3153# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003154# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003155#
3156# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003157# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003158# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3159# LONG_LINE all other lines longer than $max_line_length
3160#
3161# if LONG_LINE is ignored, the other 2 types are also ignored
3162#
3163
Joe Perchesb4749e92015-07-17 16:24:01 -07003164 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003165 my $msg_type = "LONG_LINE";
3166
3167 # Check the allowed long line types first
3168
3169 # logging functions that end in a string that starts
3170 # before $max_line_length
3171 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3172 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3173 $msg_type = "";
3174
3175 # lines with only strings (w/ possible termination)
3176 # #defines with only strings
3177 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3178 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3179 $msg_type = "";
3180
Joe Perchescc147502017-11-17 15:28:44 -08003181 # More special cases
3182 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3183 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003184 $msg_type = "";
3185
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003186 # URL ($rawline is used in case the URL is in a comment)
3187 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3188 $msg_type = "";
3189
Joe Perches47e0c882015-06-25 15:02:57 -07003190 # Otherwise set the alternate message types
3191
3192 # a comment starts before $max_line_length
3193 } elsif ($line =~ /($;[\s$;]*)$/ &&
3194 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3195 $msg_type = "LONG_LINE_COMMENT"
3196
3197 # a quoted string starts before $max_line_length
3198 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3199 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3200 $msg_type = "LONG_LINE_STRING"
3201 }
3202
3203 if ($msg_type ne "" &&
3204 (show_type("LONG_LINE") || show_type($msg_type))) {
3205 WARN($msg_type,
3206 "line over $max_line_length characters\n" . $herecurr);
3207 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003208 }
3209
Andy Whitcroft8905a672007-11-28 16:21:06 -08003210# check for adding lines without a newline.
3211 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003212 WARN("MISSING_EOF_NEWLINE",
3213 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003214 }
3215
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003216# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003217 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003218
3219# at the beginning of a line any tabs must come first and anything
3220# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003221 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3222 $rawline =~ /^\+\s* \s*/) {
3223 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003224 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003225 if (ERROR("CODE_INDENT",
3226 "code indent should use tabs where possible\n" . $herevet) &&
3227 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003228 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003229 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003230 }
3231
Alberto Panizzo08e44362010-03-05 13:43:54 -08003232# check for space before tabs.
3233 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3234 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003235 if (WARN("SPACE_BEFORE_TAB",
3236 "please, no space before tabs\n" . $herevet) &&
3237 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003238 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003239 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003240 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003241 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003242 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003243 }
3244
Joe Perches6a487212018-04-10 16:34:04 -07003245# check for assignments on the start of a line
3246 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3247 CHK("ASSIGNMENT_CONTINUATIONS",
3248 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3249 }
3250
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003251# check for && or || at the start of a line
3252 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3253 CHK("LOGICAL_CONTINUATIONS",
3254 "Logical continuations should be on the previous line\n" . $hereprev);
3255 }
3256
Joe Perchesa91e8992016-05-20 17:04:05 -07003257# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003258 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003259 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003260 my $indent = length($1);
3261 if ($indent % 8) {
3262 if (WARN("TABSTOP",
3263 "Statements should start on a tabstop\n" . $herecurr) &&
3264 $fix) {
3265 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3266 }
3267 }
3268 }
3269
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003270# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003271 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003272 $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 -07003273 $prevline =~ /^\+(\t*)(.*)$/;
3274 my $oldindent = $1;
3275 my $rest = $2;
3276
3277 my $pos = pos_last_openparen($rest);
3278 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003279 $line =~ /^(\+| )([ \t]*)/;
3280 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003281
3282 my $goodtabindent = $oldindent .
3283 "\t" x ($pos / 8) .
3284 " " x ($pos % 8);
3285 my $goodspaceindent = $oldindent . " " x $pos;
3286
3287 if ($newindent ne $goodtabindent &&
3288 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003289
3290 if (CHK("PARENTHESIS_ALIGNMENT",
3291 "Alignment should match open parenthesis\n" . $hereprev) &&
3292 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003293 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003294 s/^\+[ \t]*/\+$goodtabindent/;
3295 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003296 }
3297 }
3298 }
3299
Joe Perches6ab3a972015-04-16 12:44:05 -07003300# check for space after cast like "(int) foo" or "(struct foo) bar"
3301# avoid checking a few false positives:
3302# "sizeof(<type>)" or "__alignof__(<type>)"
3303# function pointer declarations like "(*foo)(int) = bar;"
3304# structure definitions like "(struct foo) { 0 };"
3305# multiline macros that define functions
3306# known attributes or the __attribute__ keyword
3307 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3308 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003309 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003310 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003311 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003312 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003313 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003314 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003315 }
3316
Joe Perches86406b12015-09-09 15:37:41 -07003317# Block comment styles
3318# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003319 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003320 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003321 $rawline =~ /^\+[ \t]*\*/ &&
3322 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003323 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3324 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3325 }
3326
Joe Perches86406b12015-09-09 15:37:41 -07003327# Block comments use * on subsequent lines
3328 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3329 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003330 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003331 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003332 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003333 WARN("BLOCK_COMMENT_STYLE",
3334 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003335 }
3336
Joe Perches86406b12015-09-09 15:37:41 -07003337# Block comments use */ on trailing lines
3338 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003339 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3340 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3341 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003342 WARN("BLOCK_COMMENT_STYLE",
3343 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003344 }
3345
Joe Perches08eb9b82016-10-11 13:51:50 -07003346# Block comment * alignment
3347 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003348 $line =~ /^\+[ \t]*$;/ && #leading comment
3349 $rawline =~ /^\+[ \t]*\*/ && #leading *
3350 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003351 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003352 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3353 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003354 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003355 if (defined($1)) {
3356 $oldindent = expand_tabs($1);
3357 } else {
3358 $prevrawline =~ m@^\+(.*/?)\*@;
3359 $oldindent = expand_tabs($1);
3360 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003361 $rawline =~ m@^\+([ \t]*)\*@;
3362 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003363 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003364 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003365 WARN("BLOCK_COMMENT_STYLE",
3366 "Block comments should align the * on each line\n" . $hereprev);
3367 }
3368 }
3369
Joe Perches7f619192014-08-06 16:10:39 -07003370# check for missing blank lines after struct/union declarations
3371# with exceptions for various attributes and macros
3372 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3373 $line =~ /^\+/ &&
3374 !($line =~ /^\+\s*$/ ||
3375 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3376 $line =~ /^\+\s*MODULE_/i ||
3377 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3378 $line =~ /^\+[a-z_]*init/ ||
3379 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3380 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003381 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003382 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003383 if (CHK("LINE_SPACING",
3384 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3385 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003386 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003387 }
Joe Perches7f619192014-08-06 16:10:39 -07003388 }
3389
Joe Perches365dd4e2014-08-06 16:10:42 -07003390# check for multiple consecutive blank lines
3391 if ($prevline =~ /^[\+ ]\s*$/ &&
3392 $line =~ /^\+\s*$/ &&
3393 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003394 if (CHK("LINE_SPACING",
3395 "Please don't use multiple blank lines\n" . $hereprev) &&
3396 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003397 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003398 }
3399
Joe Perches365dd4e2014-08-06 16:10:42 -07003400 $last_blank_line = $linenr;
3401 }
3402
Joe Perches3b617e32014-04-03 14:49:28 -07003403# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003404 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3405 # actual declarations
3406 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003407 # function pointer declarations
3408 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003409 # foo bar; where foo is some local typedef or #define
3410 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3411 # known declaration macros
3412 $prevline =~ /^\+\s+$declaration_macros/) &&
3413 # for "else if" which can look like "$Ident $Ident"
3414 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3415 # other possible extensions of declaration lines
3416 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3417 # not starting a section or a macro "\" extended line
3418 $prevline =~ /(?:\{\s*|\\)$/) &&
3419 # looks like a declaration
3420 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003421 # function pointer declarations
3422 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003423 # foo bar; where foo is some local typedef or #define
3424 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3425 # known declaration macros
3426 $sline =~ /^\+\s+$declaration_macros/ ||
3427 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003428 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003429 # start or end of block or continuation of declaration
3430 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3431 # bitfield continuation
3432 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3433 # other possible extensions of declaration lines
3434 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3435 # indentation of previous and current line are the same
3436 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003437 if (WARN("LINE_SPACING",
3438 "Missing a blank line after declarations\n" . $hereprev) &&
3439 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003440 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003441 }
Joe Perches3b617e32014-04-03 14:49:28 -07003442 }
3443
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003444# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003445# Exceptions:
3446# 1) within comments
3447# 2) indented preprocessor commands
3448# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003449 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003450 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003451 if (WARN("LEADING_SPACE",
3452 "please, no spaces at the start of a line\n" . $herevet) &&
3453 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003454 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003455 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003456 }
3457
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003458# check we are in a valid C source file if not then ignore this hunk
3459 next if ($realfile !~ /\.(h|c)$/);
3460
Joe Perches5751a242017-11-17 15:28:52 -08003461# check for unusual line ending [ or (
3462 if ($line =~ /^\+.*([\[\(])\s*$/) {
3463 CHK("OPEN_ENDED_LINE",
3464 "Lines should not end with a '$1'\n" . $herecurr);
3465 }
3466
Joe Perches4dbed762017-05-08 15:55:39 -07003467# check if this appears to be the start function declaration, save the name
3468 if ($sline =~ /^\+\{\s*$/ &&
3469 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3470 $context_function = $1;
3471 }
3472
3473# check if this appears to be the end of function declaration
3474 if ($sline =~ /^\+\}\s*$/) {
3475 undef $context_function;
3476 }
3477
Joe Perches032a4c02014-08-06 16:10:29 -07003478# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003479# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003480# if the previous line is a break or return and is indented 1 tab more...
3481 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3482 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003483 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3484 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3485 defined $lines[$linenr] &&
3486 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003487 WARN("UNNECESSARY_ELSE",
3488 "else is not generally useful after a break or return\n" . $hereprev);
3489 }
3490 }
3491
Joe Perchesc00df192014-08-06 16:11:01 -07003492# check indentation of a line with a break;
3493# if the previous line is a goto or return and is indented the same # of tabs
3494 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3495 my $tabs = $1;
3496 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3497 WARN("UNNECESSARY_BREAK",
3498 "break is not useful after a goto or return\n" . $hereprev);
3499 }
3500 }
3501
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003502# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003503 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003504 WARN("CVS_KEYWORD",
3505 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003506 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003507
Joe Perches56e77d72013-02-21 16:44:14 -08003508# check for old HOTPLUG __dev<foo> section markings
3509 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3510 WARN("HOTPLUG_SECTION",
3511 "Using $1 is unnecessary\n" . $herecurr);
3512 }
3513
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003514# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003515 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3516 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003517#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003518 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003519 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003520 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003521 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003522 $stat =~ s/\n./\n /g;
3523 $cond =~ s/\n./\n /g;
3524
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003525#print "linenr<$linenr> <$stat>\n";
3526 # If this statement has no statement boundaries within
3527 # it there is no point in retrying a statement scan
3528 # until we hit end of it.
3529 my $frag = $stat; $frag =~ s/;+\s*$//;
3530 if ($frag !~ /(?:{|;)/) {
3531#print "skip<$line_nr_next>\n";
3532 $suppress_statement = $line_nr_next;
3533 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003534
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003535 # Find the real next line.
3536 $realline_next = $line_nr_next;
3537 if (defined $realline_next &&
3538 (!defined $lines[$realline_next - 1] ||
3539 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3540 $realline_next++;
3541 }
3542
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003543 my $s = $stat;
3544 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003545
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003546 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003547 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003548
3549 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003550 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003551
Andy Whitcroft463f2862009-09-21 17:04:34 -07003552 } elsif ($s =~ /^.\s*else\b/s) {
3553
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003554 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003555 } 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 -07003556 my $type = $1;
3557 $type =~ s/\s+/ /g;
3558 possible($type, "A:" . $s);
3559
Andy Whitcroft8905a672007-11-28 16:21:06 -08003560 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003561 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003562 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003563 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003564
3565 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003566 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003567 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003568 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003569
3570 # Check for any sort of function declaration.
3571 # int foo(something bar, other baz);
3572 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003573 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 -08003574 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003575
Andy Whitcroftcf655042008-03-04 14:28:20 -08003576 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003577 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003578 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003579
Andy Whitcroft8905a672007-11-28 16:21:06 -08003580 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003581 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003582
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003583 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003584 }
3585 }
3586 }
3587
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003588 }
3589
Andy Whitcroft653d4872007-06-23 17:16:34 -07003590#
3591# Checks which may be anchored in the context.
3592#
3593
3594# Check for switch () and associated case and default
3595# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003596 if ($line=~/\bswitch\s*\(.*\)/) {
3597 my $err = '';
3598 my $sep = '';
3599 my @ctx = ctx_block_outer($linenr, $realcnt);
3600 shift(@ctx);
3601 for my $ctx (@ctx) {
3602 my ($clen, $cindent) = line_stats($ctx);
3603 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3604 $indent != $cindent) {
3605 $err .= "$sep$ctx\n";
3606 $sep = '';
3607 } else {
3608 $sep = "[...]\n";
3609 }
3610 }
3611 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003612 ERROR("SWITCH_CASE_INDENT_LEVEL",
3613 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003614 }
3615 }
3616
3617# if/while/etc brace do not go on next line, unless defining a do while loop,
3618# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003619 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 -07003620 my $pre_ctx = "$1$2";
3621
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003622 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003623
3624 if ($line =~ /^\+\t{6,}/) {
3625 WARN("DEEP_INDENTATION",
3626 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3627 }
3628
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003629 my $ctx_cnt = $realcnt - $#ctx - 1;
3630 my $ctx = join("\n", @ctx);
3631
Andy Whitcroft548596d2008-07-23 21:29:01 -07003632 my $ctx_ln = $linenr;
3633 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003634
Andy Whitcroft548596d2008-07-23 21:29:01 -07003635 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3636 defined $lines[$ctx_ln - 1] &&
3637 $lines[$ctx_ln - 1] =~ /^-/)) {
3638 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3639 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003640 $ctx_ln++;
3641 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003642
Andy Whitcroft53210162008-07-23 21:29:03 -07003643 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3644 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003645
Joe Perchesd752fcc2014-08-06 16:11:05 -07003646 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003647 ERROR("OPEN_BRACE",
3648 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003649 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003650 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003651 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3652 $ctx =~ /\)\s*\;\s*$/ &&
3653 defined $lines[$ctx_ln - 1])
3654 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003655 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3656 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003657 WARN("TRAILING_SEMICOLON",
3658 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003659 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003660 }
3661 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003662 }
3663
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003664# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003665 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 -08003666 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3667 ctx_statement_block($linenr, $realcnt, 0)
3668 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003669 my ($s, $c) = ($stat, $cond);
3670
3671 substr($s, 0, length($c), '');
3672
Joe Perches9f5af482015-09-09 15:37:30 -07003673 # remove inline comments
3674 $s =~ s/$;/ /g;
3675 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003676
3677 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003678 my @newlines = ($c =~ /\n/gs);
3679 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003680
Joe Perches9f5af482015-09-09 15:37:30 -07003681 # Make sure we remove the line prefixes as we have
3682 # none on the first line, and are going to readd them
3683 # where necessary.
3684 $s =~ s/\n./\n/gs;
3685 while ($s =~ /\n\s+\\\n/) {
3686 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3687 }
3688
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003689 # We want to check the first line inside the block
3690 # starting at the end of the conditional, so remove:
3691 # 1) any blank line termination
3692 # 2) any opening brace { on end of the line
3693 # 3) any do (...) {
3694 my $continuation = 0;
3695 my $check = 0;
3696 $s =~ s/^.*\bdo\b//;
3697 $s =~ s/^\s*{//;
3698 if ($s =~ s/^\s*\\//) {
3699 $continuation = 1;
3700 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003701 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003702 $check = 1;
3703 $cond_lines++;
3704 }
3705
3706 # Also ignore a loop construct at the end of a
3707 # preprocessor statement.
3708 if (($prevline =~ /^.\s*#\s*define\s/ ||
3709 $prevline =~ /\\\s*$/) && $continuation == 0) {
3710 $check = 0;
3711 }
3712
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003713 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003714 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003715 while ($cond_ptr != $cond_lines) {
3716 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003717
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003718 # If we see an #else/#elif then the code
3719 # is not linear.
3720 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3721 $check = 0;
3722 }
3723
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003724 # Ignore:
3725 # 1) blank lines, they should be at 0,
3726 # 2) preprocessor lines, and
3727 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003728 if ($continuation ||
3729 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003730 $s =~ /^\s*#\s*?/ ||
3731 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003732 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003733 if ($s =~ s/^.*?\n//) {
3734 $cond_lines++;
3735 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003736 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003737 }
3738
3739 my (undef, $sindent) = line_stats("+" . $s);
3740 my $stat_real = raw_line($linenr, $cond_lines);
3741
3742 # Check if either of these lines are modified, else
3743 # this is not this patch's fault.
3744 if (!defined($stat_real) ||
3745 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3746 $check = 0;
3747 }
3748 if (defined($stat_real) && $cond_lines > 1) {
3749 $stat_real = "[...]\n$stat_real";
3750 }
3751
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003752 #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 -07003753
Joe Perches9f5af482015-09-09 15:37:30 -07003754 if ($check && $s ne '' &&
3755 (($sindent % 8) != 0 ||
3756 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003757 ($sindent == $indent &&
3758 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003759 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003760 WARN("SUSPECT_CODE_INDENT",
3761 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003762 }
3763 }
3764
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003765 # Track the 'values' across context and added lines.
3766 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003767 my ($curr_values, $curr_vars) =
3768 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003769 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003770 if ($dbg_values) {
3771 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003772 print "$linenr > .$outline\n";
3773 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003774 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003775 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003776 $prev_values = substr($curr_values, -1);
3777
Andy Whitcroft00df3442007-06-08 13:47:06 -07003778#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003779 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003780
Joe Perches11ca40a2016-12-12 16:46:31 -08003781# check for dereferences that span multiple lines
3782 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3783 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3784 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3785 my $ref = $1;
3786 $line =~ /^.\s*($Lval)/;
3787 $ref .= $1;
3788 $ref =~ s/\s//g;
3789 WARN("MULTILINE_DEREFERENCE",
3790 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3791 }
3792
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003793# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003794 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 -07003795 my $type = $1;
3796 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003797 $var = "" if (!defined $var);
3798 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003799 my $sign = $1;
3800 my $pointer = $2;
3801
3802 $pointer = "" if (!defined $pointer);
3803
3804 if (WARN("UNSPECIFIED_INT",
3805 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3806 $fix) {
3807 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003808 my $comp_pointer = $pointer;
3809 $comp_pointer =~ s/\s//g;
3810 $decl .= $comp_pointer;
3811 $decl = rtrim($decl) if ($var eq "");
3812 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003813 }
3814 }
3815 }
3816
Andy Whitcroft653d4872007-06-23 17:16:34 -07003817# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003818 if ($dbg_type) {
3819 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003820 ERROR("TEST_TYPE",
3821 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003822 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003823 ERROR("TEST_NOT_TYPE",
3824 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003825 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003826 next;
3827 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003828# TEST: allow direct testing of the attribute matcher.
3829 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003830 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003831 ERROR("TEST_ATTR",
3832 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003833 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003834 ERROR("TEST_NOT_ATTR",
3835 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003836 }
3837 next;
3838 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003839
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003840# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003841 if ($line =~ /^.\s*{/ &&
3842 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003843 if (ERROR("OPEN_BRACE",
3844 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003845 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3846 fix_delete_line($fixlinenr - 1, $prevrawline);
3847 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003848 my $fixedline = $prevrawline;
3849 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003850 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003851 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003852 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003853 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003854 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003855 }
3856
Andy Whitcroft653d4872007-06-23 17:16:34 -07003857#
3858# Checks which are anchored on the added line.
3859#
3860
3861# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003862 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003863 my $path = $1;
3864 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003865 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003866 "malformed #include filename\n" . $herecurr);
3867 }
3868 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3869 ERROR("UAPI_INCLUDE",
3870 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003871 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003872 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003873
3874# no C99 // comments
3875 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003876 if (ERROR("C99_COMMENTS",
3877 "do not use C99 // comments\n" . $herecurr) &&
3878 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003879 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003880 if ($line =~ /\/\/(.*)$/) {
3881 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003882 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003883 }
3884 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003885 }
3886 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003887 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003888 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003889
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003890# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3891# the whole statement.
3892#print "APW <$lines[$realline_next - 1]>\n";
3893 if (defined $realline_next &&
3894 exists $lines[$realline_next - 1] &&
3895 !defined $suppress_export{$realline_next} &&
3896 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3897 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003898 # Handle definitions which produce identifiers with
3899 # a prefix:
3900 # XXX(foo);
3901 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003902 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003903 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003904 $name =~ /^${Ident}_$2/) {
3905#print "FOO C name<$name>\n";
3906 $suppress_export{$realline_next} = 1;
3907
3908 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003909 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003910 ^.DEFINE_$Ident\(\Q$name\E\)|
3911 ^.DECLARE_$Ident\(\Q$name\E\)|
3912 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003913 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3914 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003915 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003916#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3917 $suppress_export{$realline_next} = 2;
3918 } else {
3919 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003920 }
3921 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003922 if (!defined $suppress_export{$linenr} &&
3923 $prevline =~ /^.\s*$/ &&
3924 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3925 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3926#print "FOO B <$lines[$linenr - 1]>\n";
3927 $suppress_export{$linenr} = 2;
3928 }
3929 if (defined $suppress_export{$linenr} &&
3930 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003931 WARN("EXPORT_SYMBOL",
3932 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003933 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003934
Joe Eloff5150bda2010-08-09 17:21:00 -07003935# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003936 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003937 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003938 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003939 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003940 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003941 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003942 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003943# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003944 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003945 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003946 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003947 $herecurr) &&
3948 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003949 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003950 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003951 }
3952
Joe Perches18130872014-08-06 16:11:22 -07003953# check for misordered declarations of char/short/int/long with signed/unsigned
3954 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3955 my $tmp = trim($1);
3956 WARN("MISORDERED_TYPE",
3957 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3958 }
3959
Joe Perches809e0822018-08-21 21:58:12 -07003960# check for unnecessary <signed> int declarations of short/long/long long
3961 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3962 my $type = trim($1);
3963 next if ($type !~ /\bint\b/);
3964 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3965 my $new_type = $type;
3966 $new_type =~ s/\b\s*int\s*\b/ /;
3967 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3968 $new_type =~ s/^const\s+//;
3969 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3970 $new_type = "const $new_type" if ($type =~ /^const\b/);
3971 $new_type =~ s/\s+/ /g;
3972 $new_type = trim($new_type);
3973 if (WARN("UNNECESSARY_INT",
3974 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3975 $fix) {
3976 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3977 }
3978 }
3979
Joe Perchescb710ec2010-10-26 14:23:20 -07003980# check for static const char * arrays.
3981 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003982 WARN("STATIC_CONST_CHAR_ARRAY",
3983 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003984 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003985 }
3986
3987# check for initialized const char arrays that should be static const
3988 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
3989 if (WARN("STATIC_CONST_CHAR_ARRAY",
3990 "const array should probably be static const\n" . $herecurr) &&
3991 $fix) {
3992 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
3993 }
3994 }
Joe Perchescb710ec2010-10-26 14:23:20 -07003995
3996# check for static char foo[] = "bar" declarations.
3997 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003998 WARN("STATIC_CONST_CHAR_ARRAY",
3999 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004000 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004001 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004002
Joe Perchesab7e23f2015-04-16 12:44:22 -07004003# check for const <foo> const where <foo> is not a pointer or array type
4004 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4005 my $found = $1;
4006 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4007 WARN("CONST_CONST",
4008 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4009 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4010 WARN("CONST_CONST",
4011 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4012 }
4013 }
4014
Joe Perches9b0fa602014-04-03 14:49:18 -07004015# check for non-global char *foo[] = {"bar", ...} declarations.
4016 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4017 WARN("STATIC_CONST_CHAR_ARRAY",
4018 "char * array declaration might be better as static const\n" .
4019 $herecurr);
4020 }
4021
Joe Perchesb598b672015-04-16 12:44:36 -07004022# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4023 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4024 my $array = $1;
4025 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4026 my $array_div = $1;
4027 if (WARN("ARRAY_SIZE",
4028 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4029 $fix) {
4030 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4031 }
4032 }
4033 }
4034
Joe Perchesb36190c2014-01-27 17:07:18 -08004035# check for function declarations without arguments like "int foo()"
4036 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
4037 if (ERROR("FUNCTION_WITHOUT_ARGS",
4038 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4039 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004040 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004041 }
4042 }
4043
Andy Whitcroft653d4872007-06-23 17:16:34 -07004044# check for new typedefs, only function parameters and sparse annotations
4045# make sense.
4046 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004047 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004048 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004049 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004050 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004051 WARN("NEW_TYPEDEFS",
4052 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004053 }
4054
4055# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004056 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004057 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4058 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004059 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004060
Andy Whitcroft65863862009-01-06 14:41:21 -08004061 # Should start with a space.
4062 $to =~ s/^(\S)/ $1/;
4063 # Should not end with a space.
4064 $to =~ s/\s+$//;
4065 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004066 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004067 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004068
Joe Perches3705ce52013-07-03 15:05:31 -07004069## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004070 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004071 if (ERROR("POINTER_LOCATION",
4072 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4073 $fix) {
4074 my $sub_from = $ident;
4075 my $sub_to = $ident;
4076 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004077 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004078 s@\Q$sub_from\E@$sub_to@;
4079 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004080 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004081 }
4082 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4083 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004084 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004085
Andy Whitcroft65863862009-01-06 14:41:21 -08004086 # Should start with a space.
4087 $to =~ s/^(\S)/ $1/;
4088 # Should not end with a space.
4089 $to =~ s/\s+$//;
4090 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004091 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004092 }
4093 # Modifiers should have spaces.
4094 $to =~ s/(\b$Modifier$)/$1 /;
4095
Joe Perches3705ce52013-07-03 15:05:31 -07004096## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004097 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004098 if (ERROR("POINTER_LOCATION",
4099 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4100 $fix) {
4101
4102 my $sub_from = $match;
4103 my $sub_to = $match;
4104 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004105 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004106 s@\Q$sub_from\E@$sub_to@;
4107 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004108 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004109 }
4110
Joe Perches9d3e3c72015-09-09 15:37:27 -07004111# avoid BUG() or BUG_ON()
4112 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004113 my $msg_level = \&WARN;
4114 $msg_level = \&CHK if ($file);
4115 &{$msg_level}("AVOID_BUG",
4116 "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 -07004117 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004118
Joe Perches9d3e3c72015-09-09 15:37:27 -07004119# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004120 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004121 WARN("LINUX_VERSION_CODE",
4122 "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 -08004123 }
4124
Joe Perches17441222011-06-15 15:08:17 -07004125# check for uses of printk_ratelimit
4126 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004127 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004128 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004129 }
4130
Joe Percheseeef5732017-11-17 15:28:41 -08004131# printk should use KERN_* levels
4132 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4133 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4134 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004135 }
4136
Joe Perches243f3802012-05-31 16:26:09 -07004137 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4138 my $orig = $1;
4139 my $level = lc($orig);
4140 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004141 my $level2 = $level;
4142 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004143 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004144 "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 -07004145 }
4146
Joe Perchesdc139312013-02-21 16:44:13 -08004147 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4148 my $orig = $1;
4149 my $level = lc($orig);
4150 $level = "warn" if ($level eq "warning");
4151 $level = "dbg" if ($level eq "debug");
4152 WARN("PREFER_DEV_LEVEL",
4153 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4154 }
4155
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004156# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4157# number of false positives, but assembly files are not checked, so at
4158# least the arch entry code will not trigger this warning.
4159 if ($line =~ /\bENOSYS\b/) {
4160 WARN("ENOSYS",
4161 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4162 }
4163
Andy Whitcroft653d4872007-06-23 17:16:34 -07004164# function brace can't be on same line, except for #defines of do while,
4165# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004166 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004167 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4168 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4169 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004170 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004171 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004172 $fix) {
4173 fix_delete_line($fixlinenr, $rawline);
4174 my $fixed_line = $rawline;
4175 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4176 my $line1 = $1;
4177 my $line2 = $2;
4178 fix_insert_line($fixlinenr, ltrim($line1));
4179 fix_insert_line($fixlinenr, "\+{");
4180 if ($line2 !~ /^\s*$/) {
4181 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4182 }
4183 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004184 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004185
Andy Whitcroft8905a672007-11-28 16:21:06 -08004186# open braces for enum, union and struct go on the same line.
4187 if ($line =~ /^.\s*{/ &&
4188 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004189 if (ERROR("OPEN_BRACE",
4190 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4191 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4192 fix_delete_line($fixlinenr - 1, $prevrawline);
4193 fix_delete_line($fixlinenr, $rawline);
4194 my $fixedline = rtrim($prevrawline) . " {";
4195 fix_insert_line($fixlinenr, $fixedline);
4196 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004197 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004198 if ($fixedline !~ /^\+\s*$/) {
4199 fix_insert_line($fixlinenr, $fixedline);
4200 }
4201 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004202 }
4203
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004204# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004205 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4206 if (WARN("SPACING",
4207 "missing space after $1 definition\n" . $herecurr) &&
4208 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004209 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004210 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4211 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004212 }
4213
Joe Perches31070b52014-01-23 15:54:49 -08004214# Function pointer declarations
4215# check spacing between type, funcptr, and args
4216# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004217 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004218 my $declare = $1;
4219 my $pre_pointer_space = $2;
4220 my $post_pointer_space = $3;
4221 my $funcname = $4;
4222 my $post_funcname_space = $5;
4223 my $pre_args_space = $6;
4224
Joe Perches91f72e92014-04-03 14:49:12 -07004225# the $Declare variable will capture all spaces after the type
4226# so check it for a missing trailing missing space but pointer return types
4227# don't need a space so don't warn for those.
4228 my $post_declare_space = "";
4229 if ($declare =~ /(\s+)$/) {
4230 $post_declare_space = $1;
4231 $declare = rtrim($declare);
4232 }
4233 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004234 WARN("SPACING",
4235 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004236 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004237 }
4238
4239# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004240# This test is not currently implemented because these declarations are
4241# equivalent to
4242# int foo(int bar, ...)
4243# and this is form shouldn't/doesn't generate a checkpatch warning.
4244#
4245# elsif ($declare =~ /\s{2,}$/) {
4246# WARN("SPACING",
4247# "Multiple spaces after return type\n" . $herecurr);
4248# }
Joe Perches31070b52014-01-23 15:54:49 -08004249
4250# unnecessary space "type ( *funcptr)(args...)"
4251 if (defined $pre_pointer_space &&
4252 $pre_pointer_space =~ /^\s/) {
4253 WARN("SPACING",
4254 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4255 }
4256
4257# unnecessary space "type (* funcptr)(args...)"
4258 if (defined $post_pointer_space &&
4259 $post_pointer_space =~ /^\s/) {
4260 WARN("SPACING",
4261 "Unnecessary space before function pointer name\n" . $herecurr);
4262 }
4263
4264# unnecessary space "type (*funcptr )(args...)"
4265 if (defined $post_funcname_space &&
4266 $post_funcname_space =~ /^\s/) {
4267 WARN("SPACING",
4268 "Unnecessary space after function pointer name\n" . $herecurr);
4269 }
4270
4271# unnecessary space "type (*funcptr) (args...)"
4272 if (defined $pre_args_space &&
4273 $pre_args_space =~ /^\s/) {
4274 WARN("SPACING",
4275 "Unnecessary space before function pointer arguments\n" . $herecurr);
4276 }
4277
4278 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004279 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004280 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004281 }
4282 }
4283
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004284# check for spacing round square brackets; allowed:
4285# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004286# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4287# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004288 while ($line =~ /(.*?\s)\[/g) {
4289 my ($where, $prefix) = ($-[1], $1);
4290 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004291 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004292 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004293 if (ERROR("BRACKET_SPACE",
4294 "space prohibited before open square bracket '['\n" . $herecurr) &&
4295 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004296 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004297 s/^(\+.*?)\s+\[/$1\[/;
4298 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004299 }
4300 }
4301
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004302# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004303 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004304 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004305 my $ctx_before = substr($line, 0, $-[1]);
4306 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004307
4308 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004309 if ($name =~ /^(?:
4310 if|for|while|switch|return|case|
4311 volatile|__volatile__|
4312 __attribute__|format|__extension__|
4313 asm|__asm__)$/x)
4314 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004315 # cpp #define statements have non-optional spaces, ie
4316 # if there is a space between the name and the open
4317 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004318 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004319
4320 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004321 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004322
4323 # If this whole things ends with a type its most
4324 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004325 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004326
4327 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004328 if (WARN("SPACING",
4329 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4330 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004331 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004332 s/\b$name\s+\(/$name\(/;
4333 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004334 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004335 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004336
Andy Whitcroft653d4872007-06-23 17:16:34 -07004337# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004338 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004339 my $fixed_line = "";
4340 my $line_fixed = 0;
4341
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004342 my $ops = qr{
4343 <<=|>>=|<=|>=|==|!=|
4344 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4345 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004346 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004347 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004348 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004349 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004350
4351## print("element count: <" . $#elements . ">\n");
4352## foreach my $el (@elements) {
4353## print("el: <$el>\n");
4354## }
4355
4356 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004357 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004358
Joe Perches3705ce52013-07-03 15:05:31 -07004359 foreach my $el (@elements) {
4360 push(@fix_elements, substr($rawline, $off, length($el)));
4361 $off += length($el);
4362 }
4363
4364 $off = 0;
4365
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004366 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004367 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004368
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004369 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004370
4371 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4372
4373## print("n: <$n> good: <$good>\n");
4374
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004375 $off += length($elements[$n]);
4376
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004377 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004378 my $ca = substr($opline, 0, $off);
4379 my $cc = '';
4380 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4381 $cc = substr($opline, $off + length($elements[$n + 1]));
4382 }
4383 my $cb = "$ca$;$cc";
4384
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004385 my $a = '';
4386 $a = 'V' if ($elements[$n] ne '');
4387 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004388 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004389 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4390 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004391 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004392
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004393 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004394
4395 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004396 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004397 $c = 'V' if ($elements[$n + 2] ne '');
4398 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004399 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004400 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4401 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004402 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004403 } else {
4404 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004405 }
4406
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004407 my $ctx = "${a}x${c}";
4408
4409 my $at = "(ctx:$ctx)";
4410
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004411 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004412 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004413
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004414 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004415 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004416
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004417 # Get the full operator variant.
4418 my $opv = $op . substr($curr_vars, $off, 1);
4419
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004420 # Ignore operators passed as parameters.
4421 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004422 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004423
Andy Whitcroftcf655042008-03-04 14:28:20 -08004424# # Ignore comments
4425# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004426
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004427 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004428 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004429 if ($ctx !~ /.x[WEBC]/ &&
4430 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004431 if (ERROR("SPACING",
4432 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004433 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004434 $line_fixed = 1;
4435 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004436 }
4437
4438 # // is a comment
4439 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004440
Joe Perchesb00e4812014-04-03 14:49:33 -07004441 # : when part of a bitfield
4442 } elsif ($opv eq ':B') {
4443 # skip the bitfield test for now
4444
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004445 # No spaces for:
4446 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004447 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004448 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004449 if (ERROR("SPACING",
4450 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004451 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004452 if (defined $fix_elements[$n + 2]) {
4453 $fix_elements[$n + 2] =~ s/^\s+//;
4454 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004455 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004456 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004457 }
4458
Joe Perches23810972014-12-10 15:51:32 -08004459 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004460 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004461 my $rtrim_before = 0;
4462 my $space_after = 0;
4463 if ($ctx =~ /Wx./) {
4464 if (ERROR("SPACING",
4465 "space prohibited before that '$op' $at\n" . $hereptr)) {
4466 $line_fixed = 1;
4467 $rtrim_before = 1;
4468 }
4469 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004470 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004471 if (ERROR("SPACING",
4472 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004473 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004474 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004475 $space_after = 1;
4476 }
4477 }
4478 if ($rtrim_before || $space_after) {
4479 if ($rtrim_before) {
4480 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4481 } else {
4482 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4483 }
4484 if ($space_after) {
4485 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004486 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004487 }
4488
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004489 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004490 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004491 #warn "'*' is part of type\n";
4492
4493 # unary operators should have a space before and
4494 # none after. May be left adjacent to another
4495 # unary operator, or a cast
4496 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004497 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004498 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004499 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004500 if (ERROR("SPACING",
4501 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004502 if ($n != $last_after + 2) {
4503 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4504 $line_fixed = 1;
4505 }
Joe Perches3705ce52013-07-03 15:05:31 -07004506 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004507 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004508 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004509 # A unary '*' may be const
4510
4511 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004512 if (ERROR("SPACING",
4513 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004514 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004515 if (defined $fix_elements[$n + 2]) {
4516 $fix_elements[$n + 2] =~ s/^\s+//;
4517 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004518 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004519 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004520 }
4521
4522 # unary ++ and unary -- are allowed no space on one side.
4523 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004524 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004525 if (ERROR("SPACING",
4526 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004527 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004528 $line_fixed = 1;
4529 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004530 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004531 if ($ctx =~ /Wx[BE]/ ||
4532 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004533 if (ERROR("SPACING",
4534 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004535 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004536 $line_fixed = 1;
4537 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004538 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004539 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004540 if (ERROR("SPACING",
4541 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004542 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004543 if (defined $fix_elements[$n + 2]) {
4544 $fix_elements[$n + 2] =~ s/^\s+//;
4545 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004546 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004547 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004548 }
4549
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004550 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004551 } elsif ($op eq '<<' or $op eq '>>' or
4552 $op eq '&' or $op eq '^' or $op eq '|' or
4553 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004554 $op eq '*' or $op eq '/' or
4555 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004556 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004557 if ($check) {
4558 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4559 if (CHK("SPACING",
4560 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4561 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4562 $fix_elements[$n + 2] =~ s/^\s+//;
4563 $line_fixed = 1;
4564 }
4565 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4566 if (CHK("SPACING",
4567 "space preferred before that '$op' $at\n" . $hereptr)) {
4568 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4569 $line_fixed = 1;
4570 }
4571 }
4572 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004573 if (ERROR("SPACING",
4574 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004575 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4576 if (defined $fix_elements[$n + 2]) {
4577 $fix_elements[$n + 2] =~ s/^\s+//;
4578 }
Joe Perches3705ce52013-07-03 15:05:31 -07004579 $line_fixed = 1;
4580 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004581 }
4582
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004583 # A colon needs no spaces before when it is
4584 # terminating a case value or a label.
4585 } elsif ($opv eq ':C' || $opv eq ':L') {
4586 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004587 if (ERROR("SPACING",
4588 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004589 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004590 $line_fixed = 1;
4591 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004592 }
4593
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004594 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004595 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004596 my $ok = 0;
4597
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004598 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004599 if (($op eq '<' &&
4600 $cc =~ /^\S+\@\S+>/) ||
4601 ($op eq '>' &&
4602 $ca =~ /<\S+\@\S+$/))
4603 {
4604 $ok = 1;
4605 }
4606
Joe Perchese0df7e12015-04-16 12:44:53 -07004607 # for asm volatile statements
4608 # ignore a colon with another
4609 # colon immediately before or after
4610 if (($op eq ':') &&
4611 ($ca =~ /:$/ || $cc =~ /^:/)) {
4612 $ok = 1;
4613 }
4614
Joe Perches84731622013-11-12 15:10:05 -08004615 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004616 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004617 my $msg_level = \&ERROR;
4618 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004619
Jean Delvare0675a8f2017-09-08 16:16:07 -07004620 if (&{$msg_level}("SPACING",
4621 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004622 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4623 if (defined $fix_elements[$n + 2]) {
4624 $fix_elements[$n + 2] =~ s/^\s+//;
4625 }
Joe Perches3705ce52013-07-03 15:05:31 -07004626 $line_fixed = 1;
4627 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004628 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004629 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004630 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004631
4632## print("n: <$n> GOOD: <$good>\n");
4633
4634 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004635 }
Joe Perches3705ce52013-07-03 15:05:31 -07004636
4637 if (($#elements % 2) == 0) {
4638 $fixed_line = $fixed_line . $fix_elements[$#elements];
4639 }
4640
Joe Perches194f66f2014-08-06 16:11:03 -07004641 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4642 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004643 }
4644
4645
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004646 }
4647
Joe Perches786b6322013-07-03 15:05:32 -07004648# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004649 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004650 if (WARN("SPACING",
4651 "space prohibited before semicolon\n" . $herecurr) &&
4652 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004653 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004654 s/^(\+.*\S)\s+;/$1;/;
4655 }
4656 }
4657
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004658# check for multiple assignments
4659 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004660 CHK("MULTIPLE_ASSIGNMENTS",
4661 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004662 }
4663
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004664## # check for multiple declarations, allowing for a function declaration
4665## # continuation.
4666## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4667## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4668##
4669## # Remove any bracketed sections to ensure we do not
4670## # falsly report the parameters of functions.
4671## my $ln = $line;
4672## while ($ln =~ s/\([^\(\)]*\)//g) {
4673## }
4674## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004675## WARN("MULTIPLE_DECLARATION",
4676## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004677## }
4678## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004679
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004680#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004681 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004682 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004683 if (ERROR("SPACING",
4684 "space required before the open brace '{'\n" . $herecurr) &&
4685 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004686 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004687 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004688 }
4689
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004690## # check for blank lines before declarations
4691## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4692## $prevrawline =~ /^.\s*$/) {
4693## WARN("SPACING",
4694## "No blank lines before declarations\n" . $hereprev);
4695## }
4696##
4697
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004698# closing brace should have a space following it when it has anything
4699# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004700 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004701 if (ERROR("SPACING",
4702 "space required after that close brace '}'\n" . $herecurr) &&
4703 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004704 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004705 s/}((?!(?:,|;|\)))\S)/} $1/;
4706 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004707 }
4708
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004709# check spacing on square brackets
4710 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004711 if (ERROR("SPACING",
4712 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4713 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004714 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004715 s/\[\s+/\[/;
4716 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004717 }
4718 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004719 if (ERROR("SPACING",
4720 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4721 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004722 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004723 s/\s+\]/\]/;
4724 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004725 }
4726
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004727# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004728 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4729 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004730 if (ERROR("SPACING",
4731 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4732 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004733 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004734 s/\(\s+/\(/;
4735 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004736 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004737 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004738 $line !~ /for\s*\(.*;\s+\)/ &&
4739 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004740 if (ERROR("SPACING",
4741 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4742 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004743 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004744 s/\s+\)/\)/;
4745 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004746 }
4747
Joe Perchese2826fd2014-08-06 16:10:48 -07004748# check unnecessary parentheses around addressof/dereference single $Lvals
4749# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4750
4751 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004752 my $var = $1;
4753 if (CHK("UNNECESSARY_PARENTHESES",
4754 "Unnecessary parentheses around $var\n" . $herecurr) &&
4755 $fix) {
4756 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4757 }
4758 }
4759
4760# check for unnecessary parentheses around function pointer uses
4761# ie: (foo->bar)(); should be foo->bar();
4762# but not "if (foo->bar) (" to avoid some false positives
4763 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4764 my $var = $2;
4765 if (CHK("UNNECESSARY_PARENTHESES",
4766 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4767 $fix) {
4768 my $var2 = deparenthesize($var);
4769 $var2 =~ s/\s//g;
4770 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4771 }
4772 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004773
Joe Perches63b7c732017-09-08 16:16:01 -07004774# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004775# when !drivers/staging or command-line uses --strict
4776 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004777 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004778 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4779 my $if_stat = $1;
4780 my $test = substr($2, 1, -1);
4781 my $herectx;
4782 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4783 my $match = $1;
4784 # avoid parentheses around potential macro args
4785 next if ($match =~ /^\s*\w+\s*$/);
4786 if (!defined($herectx)) {
4787 $herectx = $here . "\n";
4788 my $cnt = statement_rawlines($if_stat);
4789 for (my $n = 0; $n < $cnt; $n++) {
4790 my $rl = raw_line($linenr, $n);
4791 $herectx .= $rl . "\n";
4792 last if $rl =~ /^[ \+].*\{/;
4793 }
4794 }
4795 CHK("UNNECESSARY_PARENTHESES",
4796 "Unnecessary parentheses around '$match'\n" . $herectx);
4797 }
4798 }
4799
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004800#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004801 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004802 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004803 if (WARN("INDENTED_LABEL",
4804 "labels should not be indented\n" . $herecurr) &&
4805 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004806 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004807 s/^(.)\s+/$1/;
4808 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004809 }
4810
Joe Perches5b9553a2014-04-03 14:49:21 -07004811# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004812 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004813 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004814 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004815 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4816 my $value = $1;
4817 $value = deparenthesize($value);
4818 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4819 ERROR("RETURN_PARENTHESES",
4820 "return is not a function, parentheses are not required\n" . $herecurr);
4821 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004822 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004823 ERROR("SPACING",
4824 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004825 }
4826 }
Joe Perches507e5142013-11-12 15:10:13 -08004827
Joe Perchesb43ae212014-06-23 13:22:07 -07004828# unnecessary return in a void function
4829# at end-of-function, with the previous line a single leading tab, then return;
4830# and the line before that not a goto label target like "out:"
4831 if ($sline =~ /^[ \+]}\s*$/ &&
4832 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4833 $linenr >= 3 &&
4834 $lines[$linenr - 3] =~ /^[ +]/ &&
4835 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004836 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004837 "void function return statements are not generally useful\n" . $hereprev);
4838 }
Joe Perches9819cf22014-06-04 16:12:09 -07004839
Joe Perches189248d2014-01-23 15:54:47 -08004840# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004841 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004842 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4843 my $openparens = $1;
4844 my $count = $openparens =~ tr@\(@\(@;
4845 my $msg = "";
4846 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4847 my $comp = $4; #Not $1 because of $LvalOrFunc
4848 $msg = " - maybe == should be = ?" if ($comp eq "==");
4849 WARN("UNNECESSARY_PARENTHESES",
4850 "Unnecessary parentheses$msg\n" . $herecurr);
4851 }
4852 }
4853
Joe Perchesc5595fa2015-09-09 15:37:58 -07004854# comparisons with a constant or upper case identifier on the left
4855# avoid cases like "foo + BAR < baz"
4856# only fix matches surrounded by parentheses to avoid incorrect
4857# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004858 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004859 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4860 my $lead = $1;
4861 my $const = $2;
4862 my $comp = $3;
4863 my $to = $4;
4864 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004865 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004866 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4867 WARN("CONSTANT_COMPARISON",
4868 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4869 $fix) {
4870 if ($comp eq "<") {
4871 $newcomp = ">";
4872 } elsif ($comp eq "<=") {
4873 $newcomp = ">=";
4874 } elsif ($comp eq ">") {
4875 $newcomp = "<";
4876 } elsif ($comp eq ">=") {
4877 $newcomp = "<=";
4878 }
4879 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4880 }
4881 }
4882
Joe Perchesf34e4a42015-04-16 12:44:19 -07004883# Return of what appears to be an errno should normally be negative
4884 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004885 my $name = $1;
4886 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004887 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004888 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004889 }
4890 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004891
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004892# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004893 if ($line =~ /\b(if|while|for|switch)\(/) {
4894 if (ERROR("SPACING",
4895 "space required before the open parenthesis '('\n" . $herecurr) &&
4896 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004897 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004898 s/\b(if|while|for|switch)\(/$1 \(/;
4899 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004900 }
4901
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004902# Check for illegal assignment in if conditional -- and check for trailing
4903# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004904 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004905 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4906 ctx_statement_block($linenr, $realcnt, 0)
4907 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004908 my ($stat_next) = ctx_statement_block($line_nr_next,
4909 $remain_next, $off_next);
4910 $stat_next =~ s/\n./\n /g;
4911 ##print "stat<$stat> stat_next<$stat_next>\n";
4912
4913 if ($stat_next =~ /^\s*while\b/) {
4914 # If the statement carries leading newlines,
4915 # then count those as offsets.
4916 my ($whitespace) =
4917 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4918 my $offset =
4919 statement_rawlines($whitespace) - 1;
4920
4921 $suppress_whiletrailers{$line_nr_next +
4922 $offset} = 1;
4923 }
4924 }
4925 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004926 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004927 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004928 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004929
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004930 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004931 ERROR("ASSIGN_IN_IF",
4932 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004933 }
4934
4935 # Find out what is on the end of the line after the
4936 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004937 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004938 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004939 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004940 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4941 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004942 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004943 # Find out how long the conditional actually is.
4944 my @newlines = ($c =~ /\n/gs);
4945 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004946 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004947
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004948 $stat_real = raw_line($linenr, $cond_lines)
4949 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004950 if (defined($stat_real) && $cond_lines > 1) {
4951 $stat_real = "[...]\n$stat_real";
4952 }
4953
Joe Perches000d1cc12011-07-25 17:13:25 -07004954 ERROR("TRAILING_STATEMENTS",
4955 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004956 }
4957 }
4958
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004959# Check for bitwise tests written as boolean
4960 if ($line =~ /
4961 (?:
4962 (?:\[|\(|\&\&|\|\|)
4963 \s*0[xX][0-9]+\s*
4964 (?:\&\&|\|\|)
4965 |
4966 (?:\&\&|\|\|)
4967 \s*0[xX][0-9]+\s*
4968 (?:\&\&|\|\||\)|\])
4969 )/x)
4970 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004971 WARN("HEXADECIMAL_BOOLEAN_TEST",
4972 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004973 }
4974
Andy Whitcroft8905a672007-11-28 16:21:06 -08004975# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004976 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4977 my $s = $1;
4978 $s =~ s/$;//g; # Remove any comments
4979 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004980 ERROR("TRAILING_STATEMENTS",
4981 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004982 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004983 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004984# if should not continue a brace
4985 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004986 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004987 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004988 $herecurr);
4989 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004990# case and default should not have general statements after them
4991 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4992 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004993 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004994 \s*return\s+
4995 )/xg)
4996 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004997 ERROR("TRAILING_STATEMENTS",
4998 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004999 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005000
5001 # Check for }<nl>else {, these must be at the same
5002 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005003 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5004 $previndent == $indent) {
5005 if (ERROR("ELSE_AFTER_BRACE",
5006 "else should follow close brace '}'\n" . $hereprev) &&
5007 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5008 fix_delete_line($fixlinenr - 1, $prevrawline);
5009 fix_delete_line($fixlinenr, $rawline);
5010 my $fixedline = $prevrawline;
5011 $fixedline =~ s/}\s*$//;
5012 if ($fixedline !~ /^\+\s*$/) {
5013 fix_insert_line($fixlinenr, $fixedline);
5014 }
5015 $fixedline = $rawline;
5016 $fixedline =~ s/^(.\s*)else/$1} else/;
5017 fix_insert_line($fixlinenr, $fixedline);
5018 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005019 }
5020
Joe Perches8b8856f2014-08-06 16:11:14 -07005021 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5022 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005023 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5024
5025 # Find out what is on the end of the line after the
5026 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005027 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005028 $s =~ s/\n.*//g;
5029
5030 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005031 if (ERROR("WHILE_AFTER_BRACE",
5032 "while should follow close brace '}'\n" . $hereprev) &&
5033 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5034 fix_delete_line($fixlinenr - 1, $prevrawline);
5035 fix_delete_line($fixlinenr, $rawline);
5036 my $fixedline = $prevrawline;
5037 my $trailing = $rawline;
5038 $trailing =~ s/^\+//;
5039 $trailing = trim($trailing);
5040 $fixedline =~ s/}\s*$/} $trailing/;
5041 fix_insert_line($fixlinenr, $fixedline);
5042 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005043 }
5044 }
5045
Joe Perches95e2c602013-07-03 15:05:20 -07005046#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005047 while ($line =~ m{($Constant|$Lval)}g) {
5048 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005049
Joe Perches95e2c602013-07-03 15:05:20 -07005050#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005051 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005052 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005053#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005054 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005055#Ignore SI style variants like nS, mV and dB
5056#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5057 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005058#Ignore some three character SI units explicitly, like MiB and KHz
5059 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005060 while ($var =~ m{($Ident)}g) {
5061 my $word = $1;
5062 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005063 if ($check) {
5064 seed_camelcase_includes();
5065 if (!$file && !$camelcase_file_seeded) {
5066 seed_camelcase_file($realfile);
5067 $camelcase_file_seeded = 1;
5068 }
5069 }
Joe Perches7e781f62013-09-11 14:23:55 -07005070 if (!defined $camelcase{$word}) {
5071 $camelcase{$word} = 1;
5072 CHK("CAMELCASE",
5073 "Avoid CamelCase: <$word>\n" . $herecurr);
5074 }
Joe Perches34456862013-07-03 15:05:34 -07005075 }
Joe Perches323c1262012-12-17 16:02:07 -08005076 }
5077 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005078
5079#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005080 if ($line =~ /\#\s*define.*\\\s+$/) {
5081 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5082 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5083 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005084 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005085 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005086 }
5087
Fabian Frederick0e212e02015-04-16 12:44:25 -07005088# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5089# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005090 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005091 my $file = "$1.h";
5092 my $checkfile = "include/linux/$file";
5093 if (-f "$root/$checkfile" &&
5094 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005095 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005096 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005097 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5098 if ($asminclude > 0) {
5099 if ($realfile =~ m{^arch/}) {
5100 CHK("ARCH_INCLUDE_LINUX",
5101 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5102 } else {
5103 WARN("INCLUDE_LINUX",
5104 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5105 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005106 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005107 }
5108 }
5109
Andy Whitcroft653d4872007-06-23 17:16:34 -07005110# multi-statement macros should be enclosed in a do while loop, grab the
5111# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005112# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07005113 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5114 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005115 my $ln = $linenr;
5116 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005117 my ($off, $dstat, $dcond, $rest);
5118 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005119 my $has_flow_statement = 0;
5120 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005121 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005122 ctx_statement_block($linenr, $realcnt, 0);
5123 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005124 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005125 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005126
Joe Perches08a28432014-10-13 15:51:55 -07005127 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005128 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005129
Joe Perchesf59b64b2016-10-11 13:52:08 -07005130 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5131 my $define_args = $1;
5132 my $define_stmt = $dstat;
5133 my @def_args = ();
5134
5135 if (defined $define_args && $define_args ne "") {
5136 $define_args = substr($define_args, 1, length($define_args) - 2);
5137 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005138 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005139 @def_args = split(",", $define_args);
5140 }
5141
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005142 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005143 $dstat =~ s/\\\n.//g;
5144 $dstat =~ s/^\s*//s;
5145 $dstat =~ s/\s*$//s;
5146
5147 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005148 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5149 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005150 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005151 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005152 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005153
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005154 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005155 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5156 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005157 {
5158 }
5159
Joe Perches42e15292016-03-15 14:58:01 -07005160 # Make asm volatile uses seem like a generic function
5161 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5162
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005163 my $exceptions = qr{
5164 $Declare|
5165 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005166 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005167 DECLARE_PER_CPU|
5168 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005169 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005170 union|
5171 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005172 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005173 ^\"|\"$|
5174 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005175 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005176 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005177
5178 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005179 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005180 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005181
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005182 if ($dstat ne '' &&
5183 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5184 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005185 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005186 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005187 $dstat !~ /$exceptions/ &&
5188 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005189 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005190 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005191 $dstat !~ /^for\s*$Constant$/ && # for (...)
5192 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5193 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005194 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005195 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005196 {
Joe Perchese7955562017-05-08 15:55:48 -07005197 if ($dstat =~ /^\s*if\b/) {
5198 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5199 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5200 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005201 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5202 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5203 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005204 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005205 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005206 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005207
5208 }
Joe Perches52076492016-10-11 13:52:14 -07005209
5210 # Make $define_stmt single line, comment-free, etc
5211 my @stmt_array = split('\n', $define_stmt);
5212 my $first = 1;
5213 $define_stmt = "";
5214 foreach my $l (@stmt_array) {
5215 $l =~ s/\\$//;
5216 if ($first) {
5217 $define_stmt = $l;
5218 $first = 0;
5219 } elsif ($l =~ /^[\+ ]/) {
5220 $define_stmt .= substr($l, 1);
5221 }
5222 }
5223 $define_stmt =~ s/$;//g;
5224 $define_stmt =~ s/\s+/ /g;
5225 $define_stmt = trim($define_stmt);
5226
Joe Perchesf59b64b2016-10-11 13:52:08 -07005227# check if any macro arguments are reused (ignore '...' and 'type')
5228 foreach my $arg (@def_args) {
5229 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005230 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005231 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005232 $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Joe Perches7fe528a22017-07-10 15:52:27 -07005233 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5234 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005235 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005236 if ($use_cnt > 1) {
5237 CHK("MACRO_ARG_REUSE",
5238 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005239 }
5240# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005241 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005242 ((defined($1) && $1 ne ',') ||
5243 (defined($2) && $2 ne ','))) {
5244 CHK("MACRO_ARG_PRECEDENCE",
5245 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005246 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005247 }
Joe Perches5023d342012-12-17 16:01:47 -08005248
Joe Perches08a28432014-10-13 15:51:55 -07005249# check for macros with flow control, but without ## concatenation
5250# ## concatenation is commonly a macro that defines a function so ignore those
5251 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005252 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005253 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005254
Joe Perches08a28432014-10-13 15:51:55 -07005255 WARN("MACRO_WITH_FLOW_CONTROL",
5256 "Macros with flow control statements should be avoided\n" . "$herectx");
5257 }
5258
Joe Perches481eb482012-12-17 16:01:56 -08005259# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005260
5261 } else {
5262 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005263 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5264 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005265 $line =~ /^\+.*\\$/) {
5266 WARN("LINE_CONTINUATIONS",
5267 "Avoid unnecessary line continuations\n" . $herecurr);
5268 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005269 }
5270
Joe Perchesb13edf72012-07-30 14:41:24 -07005271# do {} while (0) macro tests:
5272# single-statement macros do not need to be enclosed in do while (0) loop,
5273# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005274 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005275 $realfile !~ m@/vmlinux.lds.h$@ &&
5276 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5277 my $ln = $linenr;
5278 my $cnt = $realcnt;
5279 my ($off, $dstat, $dcond, $rest);
5280 my $ctx = '';
5281 ($dstat, $dcond, $ln, $cnt, $off) =
5282 ctx_statement_block($linenr, $realcnt, 0);
5283 $ctx = $dstat;
5284
5285 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005286 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005287
5288 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5289 my $stmts = $2;
5290 my $semis = $3;
5291
5292 $ctx =~ s/\n*$//;
5293 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005294 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005295
Joe Perchesac8e97f2012-08-21 16:15:53 -07005296 if (($stmts =~ tr/;/;/) == 1 &&
5297 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005298 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5299 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5300 }
5301 if (defined $semis && $semis ne "") {
5302 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5303 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5304 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005305 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5306 $ctx =~ s/\n*$//;
5307 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005308 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005309
5310 WARN("TRAILING_SEMICOLON",
5311 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005312 }
5313 }
5314
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005315# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005316 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5317 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005318 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005319 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005320 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5321 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005322 my @allowed = ();
5323 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005324 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005325 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005326 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005327 for my $chunk (@chunks) {
5328 my ($cond, $block) = @{$chunk};
5329
Andy Whitcroft773647a2008-03-28 14:15:58 -07005330 # If the condition carries leading newlines, then count those as offsets.
5331 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5332 my $offset = statement_rawlines($whitespace) - 1;
5333
Joe Perchesaad4f612012-03-23 15:02:19 -07005334 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005335 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5336
5337 # We have looked at and allowed this specific line.
5338 $suppress_ifbraces{$ln + $offset} = 1;
5339
5340 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005341 $ln += statement_rawlines($block) - 1;
5342
Andy Whitcroft773647a2008-03-28 14:15:58 -07005343 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005344
5345 $seen++ if ($block =~ /^\s*{/);
5346
Joe Perchesaad4f612012-03-23 15:02:19 -07005347 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005348 if (statement_lines($cond) > 1) {
5349 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005350 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005351 }
5352 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005353 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005354 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005355 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005356 if (statement_block_size($block) > 1) {
5357 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005358 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005359 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005360 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005361 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005362 if ($seen) {
5363 my $sum_allowed = 0;
5364 foreach (@allowed) {
5365 $sum_allowed += $_;
5366 }
5367 if ($sum_allowed == 0) {
5368 WARN("BRACES",
5369 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5370 } elsif ($sum_allowed != $allow &&
5371 $seen != $allow) {
5372 CHK("BRACES",
5373 "braces {} should be used on all arms of this statement\n" . $herectx);
5374 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005375 }
5376 }
5377 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005378 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005379 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005380 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005381
Andy Whitcroftcf655042008-03-04 14:28:20 -08005382 # Check the pre-context.
5383 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5384 #print "APW: ALLOWED: pre<$1>\n";
5385 $allowed = 1;
5386 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005387
5388 my ($level, $endln, @chunks) =
5389 ctx_statement_full($linenr, $realcnt, $-[0]);
5390
Andy Whitcroftcf655042008-03-04 14:28:20 -08005391 # Check the condition.
5392 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005393 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005394 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005395 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005396 }
5397 if (statement_lines($cond) > 1) {
5398 #print "APW: ALLOWED: cond<$cond>\n";
5399 $allowed = 1;
5400 }
5401 if ($block =~/\b(?:if|for|while)\b/) {
5402 #print "APW: ALLOWED: block<$block>\n";
5403 $allowed = 1;
5404 }
5405 if (statement_block_size($block) > 1) {
5406 #print "APW: ALLOWED: lines block<$block>\n";
5407 $allowed = 1;
5408 }
5409 # Check the post-context.
5410 if (defined $chunks[1]) {
5411 my ($cond, $block) = @{$chunks[1]};
5412 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005413 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005414 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005415 if ($block =~ /^\s*\{/) {
5416 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5417 $allowed = 1;
5418 }
5419 }
5420 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005421 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005422 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005423
Joe Perches000d1cc12011-07-25 17:13:25 -07005424 WARN("BRACES",
5425 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005426 }
5427 }
5428
Joe Perchese4c5bab2017-02-24 15:01:41 -08005429# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005430 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5431 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005432 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5433 }
5434
Joe Perches0979ae62012-12-17 16:01:59 -08005435# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005436 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005437 if (CHK("BRACES",
5438 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5439 $fix && $prevrawline =~ /^\+/) {
5440 fix_delete_line($fixlinenr - 1, $prevrawline);
5441 }
Joe Perches0979ae62012-12-17 16:01:59 -08005442 }
Joe Perches77b9a532013-07-03 15:05:29 -07005443 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005444 if (CHK("BRACES",
5445 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5446 $fix) {
5447 fix_delete_line($fixlinenr, $rawline);
5448 }
Joe Perches0979ae62012-12-17 16:01:59 -08005449 }
5450
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005451# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005452 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5453 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005454 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005455 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005456 }
5457
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005458# Check for user-visible strings broken across lines, which breaks the ability
5459# to grep for the string. Make exceptions when the previous string ends in a
5460# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5461# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005462 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005463 $prevline =~ /"\s*$/ &&
5464 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5465 if (WARN("SPLIT_STRING",
5466 "quoted string split across lines\n" . $hereprev) &&
5467 $fix &&
5468 $prevrawline =~ /^\+.*"\s*$/ &&
5469 $last_coalesced_string_linenr != $linenr - 1) {
5470 my $extracted_string = get_quoted_string($line, $rawline);
5471 my $comma_close = "";
5472 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5473 $comma_close = $1;
5474 }
5475
5476 fix_delete_line($fixlinenr - 1, $prevrawline);
5477 fix_delete_line($fixlinenr, $rawline);
5478 my $fixedline = $prevrawline;
5479 $fixedline =~ s/"\s*$//;
5480 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5481 fix_insert_line($fixlinenr - 1, $fixedline);
5482 $fixedline = $rawline;
5483 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5484 if ($fixedline !~ /\+\s*$/) {
5485 fix_insert_line($fixlinenr, $fixedline);
5486 }
5487 $last_coalesced_string_linenr = $linenr;
5488 }
5489 }
5490
5491# check for missing a space in a string concatenation
5492 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5493 WARN('MISSING_SPACE',
5494 "break quoted strings at a space character\n" . $hereprev);
5495 }
5496
Joe Perchese4b7d302017-05-08 15:55:51 -07005497# check for an embedded function name in a string when the function is known
5498# This does not work very well for -f --file checking as it depends on patch
5499# context providing the function name or a single line form for in-file
5500# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005501 if ($line =~ /^\+.*$String/ &&
5502 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005503 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5504 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005505 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005506 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005507 }
5508
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005509# check for spaces before a quoted newline
5510 if ($rawline =~ /^.*\".*\s\\n/) {
5511 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5512 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5513 $fix) {
5514 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5515 }
5516
5517 }
5518
Joe Perchesf17dba42014-10-13 15:51:51 -07005519# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005520 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5521 if (CHK("CONCATENATED_STRING",
5522 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5523 $fix) {
5524 while ($line =~ /($String)/g) {
5525 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5526 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5527 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5528 }
5529 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005530 }
5531
Joe Perches90ad30e2014-12-10 15:51:59 -08005532# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005533 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005534 if (WARN("STRING_FRAGMENTS",
5535 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5536 $fix) {
5537 while ($line =~ /($String)(?=\s*")/g) {
5538 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5539 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5540 }
5541 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005542 }
5543
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005544# check for non-standard and hex prefixed decimal printf formats
5545 my $show_L = 1; #don't show the same defect twice
5546 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005547 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005548 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005549 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005550 # check for %L
5551 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005552 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005553 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5554 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005555 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005556 # check for %Z
5557 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5558 WARN("PRINTF_Z",
5559 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5560 $show_Z = 0;
5561 }
5562 # check for 0x<decimal>
5563 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5564 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005565 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5566 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005567 }
5568
5569# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005570 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005571 WARN("LINE_CONTINUATIONS",
5572 "Avoid line continuations in quoted strings\n" . $herecurr);
5573 }
5574
Andy Whitcroft00df3442007-06-08 13:47:06 -07005575# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005576 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005577 WARN("IF_0",
5578 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5579 }
5580
5581# warn about #if 1
5582 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5583 WARN("IF_1",
5584 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005585 }
5586
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005587# check for needless "if (<foo>) fn(<foo>)" uses
5588 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005589 my $tested = quotemeta($1);
5590 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5591 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5592 my $func = $1;
5593 if (WARN('NEEDLESS_IF',
5594 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5595 $fix) {
5596 my $do_fix = 1;
5597 my $leading_tabs = "";
5598 my $new_leading_tabs = "";
5599 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5600 $leading_tabs = $1;
5601 } else {
5602 $do_fix = 0;
5603 }
5604 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5605 $new_leading_tabs = $1;
5606 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5607 $do_fix = 0;
5608 }
5609 } else {
5610 $do_fix = 0;
5611 }
5612 if ($do_fix) {
5613 fix_delete_line($fixlinenr - 1, $prevrawline);
5614 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5615 }
5616 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005617 }
5618 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005619
Joe Perchesebfdc402014-08-06 16:10:27 -07005620# check for unnecessary "Out of Memory" messages
5621 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5622 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5623 (defined $1 || defined $3) &&
5624 $linenr > 3) {
5625 my $testval = $2;
5626 my $testline = $lines[$linenr - 3];
5627
5628 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5629# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5630
Joe Perchese29a70f2019-03-07 16:28:35 -08005631 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5632 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005633 WARN("OOM_MESSAGE",
5634 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5635 }
5636 }
5637
Joe Perchesf78d98f2014-10-13 15:52:01 -07005638# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005639 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005640 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5641 my $level = $1;
5642 if (WARN("UNNECESSARY_KERN_LEVEL",
5643 "Possible unnecessary $level\n" . $herecurr) &&
5644 $fix) {
5645 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5646 }
5647 }
5648
Joe Perches45c55e92017-02-24 15:01:31 -08005649# check for logging continuations
5650 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5651 WARN("LOGGING_CONTINUATION",
5652 "Avoid logging continuation uses where feasible\n" . $herecurr);
5653 }
5654
Joe Perchesabb08a52014-12-10 15:51:46 -08005655# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005656 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005657 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5658 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5659 WARN("MASK_THEN_SHIFT",
5660 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5661 }
5662
Joe Perchesb75ac612014-12-10 15:52:02 -08005663# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005664 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005665 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5666 my $val = $1;
5667 my $equal = "!";
5668 $equal = "" if ($4 eq "!=");
5669 if (CHK("COMPARISON_TO_NULL",
5670 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5671 $fix) {
5672 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5673 }
5674 }
5675 }
5676
Joe Perches8716de32013-09-11 14:24:05 -07005677# check for bad placement of section $InitAttribute (e.g.: __initdata)
5678 if ($line =~ /(\b$InitAttribute\b)/) {
5679 my $attr = $1;
5680 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5681 my $ptr = $1;
5682 my $var = $2;
5683 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5684 ERROR("MISPLACED_INIT",
5685 "$attr should be placed after $var\n" . $herecurr)) ||
5686 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5687 WARN("MISPLACED_INIT",
5688 "$attr should be placed after $var\n" . $herecurr))) &&
5689 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005690 $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 -07005691 }
5692 }
5693 }
5694
Joe Perchese970b8842013-11-12 15:10:10 -08005695# check for $InitAttributeData (ie: __initdata) with const
5696 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5697 my $attr = $1;
5698 $attr =~ /($InitAttributePrefix)(.*)/;
5699 my $attr_prefix = $1;
5700 my $attr_type = $2;
5701 if (ERROR("INIT_ATTRIBUTE",
5702 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5703 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005704 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005705 s/$InitAttributeData/${attr_prefix}initconst/;
5706 }
5707 }
5708
5709# check for $InitAttributeConst (ie: __initconst) without const
5710 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5711 my $attr = $1;
5712 if (ERROR("INIT_ATTRIBUTE",
5713 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5714 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005715 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005716 /(^\+\s*(?:static\s+))/;
5717 $lead = rtrim($1);
5718 $lead = "$lead " if ($lead !~ /^\+$/);
5719 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005720 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005721 }
5722 }
5723
Joe Perchesc17893c2015-04-16 12:44:42 -07005724# check for __read_mostly with const non-pointer (should just be const)
5725 if ($line =~ /\b__read_mostly\b/ &&
5726 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5727 if (ERROR("CONST_READ_MOSTLY",
5728 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5729 $fix) {
5730 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5731 }
5732 }
5733
Joe Perchesfbdb8132014-04-03 14:49:14 -07005734# don't use __constant_<foo> functions outside of include/uapi/
5735 if ($realfile !~ m@^include/uapi/@ &&
5736 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5737 my $constant_func = $1;
5738 my $func = $constant_func;
5739 $func =~ s/^__constant_//;
5740 if (WARN("CONSTANT_CONVERSION",
5741 "$constant_func should be $func\n" . $herecurr) &&
5742 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005743 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005744 }
5745 }
5746
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005747# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005748 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005749 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005750 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005751 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005752 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005753 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005754 }
5755 if ($delay > 2000) {
5756 WARN("LONG_UDELAY",
5757 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005758 }
5759 }
5760
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005761# warn about unexpectedly long msleep's
5762 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5763 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005764 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005765 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005766 }
5767 }
5768
Joe Perches36ec1932013-07-03 15:05:25 -07005769# check for comparisons of jiffies
5770 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5771 WARN("JIFFIES_COMPARISON",
5772 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5773 }
5774
Joe Perches9d7a34a2013-07-03 15:05:26 -07005775# check for comparisons of get_jiffies_64()
5776 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5777 WARN("JIFFIES_COMPARISON",
5778 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5779 }
5780
Andy Whitcroft00df3442007-06-08 13:47:06 -07005781# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005782# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005783# print "#ifdef in C files should be avoided\n";
5784# print "$herecurr";
5785# $clean = 0;
5786# }
5787
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005788# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005789 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005790 if (ERROR("SPACING",
5791 "exactly one space required after that #$1\n" . $herecurr) &&
5792 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005793 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005794 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5795 }
5796
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005797 }
5798
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005799# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005800 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5801 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005802 my $which = $1;
5803 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005804 CHK("UNCOMMENTED_DEFINITION",
5805 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005806 }
5807 }
5808# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005809
5810 my $barriers = qr{
5811 mb|
5812 rmb|
5813 wmb|
5814 read_barrier_depends
5815 }x;
5816 my $barrier_stems = qr{
5817 mb__before_atomic|
5818 mb__after_atomic|
5819 store_release|
5820 load_acquire|
5821 store_mb|
5822 (?:$barriers)
5823 }x;
5824 my $all_barriers = qr{
5825 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005826 smp_(?:$barrier_stems)|
5827 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005828 }x;
5829
5830 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005831 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005832 WARN("MEMORY_BARRIER",
5833 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005834 }
5835 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005836
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005837 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5838
5839 if ($realfile !~ m@^include/asm-generic/@ &&
5840 $realfile !~ m@/barrier\.h$@ &&
5841 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5842 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5843 WARN("MEMORY_BARRIER",
5844 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5845 }
5846
Joe Perchescb426e92015-06-25 15:02:46 -07005847# check for waitqueue_active without a comment.
5848 if ($line =~ /\bwaitqueue_active\s*\(/) {
5849 if (!ctx_has_comment($first_line, $linenr)) {
5850 WARN("WAITQUEUE_ACTIVE",
5851 "waitqueue_active without comment\n" . $herecurr);
5852 }
5853 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005854
Paul E. McKenney91db2592017-11-27 09:37:35 -08005855# check for smp_read_barrier_depends and read_barrier_depends
5856 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5857 WARN("READ_BARRIER_DEPENDS",
5858 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5859 }
5860
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005861# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005862 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005863 CHK("ARCH_DEFINES",
5864 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005865 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005866
Joe Perches596ed452017-07-12 14:37:02 -07005867# check that the storage class is not after a type
5868 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005869 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005870 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5871 }
5872# Check that the storage class is at the beginning of a declaration
5873 if ($line =~ /\b$Storage\b/ &&
5874 $line !~ /^.\s*$Storage/ &&
5875 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5876 $1 !~ /[\,\)]\s*$/) {
5877 WARN("STORAGE_CLASS",
5878 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005879 }
5880
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005881# check the location of the inline attribute, that it is between
5882# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005883 if ($line =~ /\b$Type\s+$Inline\b/ ||
5884 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005885 ERROR("INLINE_LOCATION",
5886 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005887 }
5888
Andy Whitcroft8905a672007-11-28 16:21:06 -08005889# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005890 if ($realfile !~ m@\binclude/uapi/@ &&
5891 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005892 if (WARN("INLINE",
5893 "plain inline is preferred over $1\n" . $herecurr) &&
5894 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005895 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005896
5897 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005898 }
5899
Joe Perches3d130fd2011-01-12 17:00:00 -08005900# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005901 if ($realfile !~ m@\binclude/uapi/@ &&
5902 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005903 WARN("PREFER_PACKED",
5904 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005905 }
5906
Joe Perches39b7e282011-07-25 17:13:24 -07005907# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005908 if ($realfile !~ m@\binclude/uapi/@ &&
5909 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005910 WARN("PREFER_ALIGNED",
5911 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005912 }
5913
Joe Perches462811d2019-09-25 16:46:44 -07005914# Check for __attribute__ section, prefer __section
5915 if ($realfile !~ m@\binclude/uapi/@ &&
5916 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5917 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
5918 my $new = substr($old, 1, -1);
5919 if (WARN("PREFER_SECTION",
5920 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
5921 $fix) {
5922 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
5923 }
5924 }
5925
Joe Perches5f14d3b2012-01-10 15:09:52 -08005926# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005927 if ($realfile !~ m@\binclude/uapi/@ &&
5928 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005929 if (WARN("PREFER_PRINTF",
5930 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5931 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005932 $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 -07005933
5934 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005935 }
5936
Joe Perches6061d942012-03-23 15:02:16 -07005937# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005938 if ($realfile !~ m@\binclude/uapi/@ &&
5939 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005940 if (WARN("PREFER_SCANF",
5941 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5942 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005943 $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 -07005944 }
Joe Perches6061d942012-03-23 15:02:16 -07005945 }
5946
Joe Perches619a9082014-12-10 15:51:35 -08005947# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005948 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005949 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5950 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5951 $line =~ /\b__weak\b/)) {
5952 ERROR("WEAK_DECLARATION",
5953 "Using weak declarations can have unintended link defects\n" . $herecurr);
5954 }
5955
Tomas Winklerfd39f902016-12-12 16:46:34 -08005956# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005957 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005958 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005959 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5960 my $type = $1;
5961 if ($type =~ /\b($typeC99Typedefs)\b/) {
5962 $type = $1;
5963 my $kernel_type = 'u';
5964 $kernel_type = 's' if ($type =~ /^_*[si]/);
5965 $type =~ /(\d+)/;
5966 $kernel_type .= $1;
5967 if (CHK("PREFER_KERNEL_TYPES",
5968 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5969 $fix) {
5970 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5971 }
5972 }
5973 }
5974
Joe Perches938224b2016-01-20 14:59:15 -08005975# check for cast of C90 native int or longer types constants
5976 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5977 my $cast = $1;
5978 my $const = $2;
5979 if (WARN("TYPECAST_INT_CONSTANT",
5980 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5981 $fix) {
5982 my $suffix = "";
5983 my $newconst = $const;
5984 $newconst =~ s/${Int_type}$//;
5985 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5986 if ($cast =~ /\blong\s+long\b/) {
5987 $suffix .= 'LL';
5988 } elsif ($cast =~ /\blong\b/) {
5989 $suffix .= 'L';
5990 }
5991 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5992 }
5993 }
5994
Joe Perches8f53a9b2010-03-05 13:43:48 -08005995# check for sizeof(&)
5996 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005997 WARN("SIZEOF_ADDRESS",
5998 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005999 }
6000
Joe Perches66c80b62012-07-30 14:41:22 -07006001# check for sizeof without parenthesis
6002 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006003 if (WARN("SIZEOF_PARENTHESIS",
6004 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6005 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006006 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006007 }
Joe Perches66c80b62012-07-30 14:41:22 -07006008 }
6009
Joe Perches88982fe2012-12-17 16:02:00 -08006010# check for struct spinlock declarations
6011 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6012 WARN("USE_SPINLOCK_T",
6013 "struct spinlock should be spinlock_t\n" . $herecurr);
6014 }
6015
Joe Perchesa6962d72013-04-29 16:18:13 -07006016# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006017 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006018 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006019 $fmt =~ s/%%//g;
6020 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006021 if (WARN("PREFER_SEQ_PUTS",
6022 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6023 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006024 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006025 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006026 }
6027 }
6028
Joe Perches478b1792018-04-10 16:33:34 -07006029# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006030 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006031 defined $stat &&
6032 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6033 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006034 my $stat_real;
6035
Joe Perches0b523762017-05-08 15:55:36 -07006036 my $lc = $stat =~ tr@\n@@;
6037 $lc = $lc + $linenr;
6038 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006039 my $specifier;
6040 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006041 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006042 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006043 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6044 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006045
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006046 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006047 $specifier = $1;
6048 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006049 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006050 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006051 ($extension eq "f" &&
6052 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006053 $bad_specifier = $specifier;
6054 last;
6055 }
6056 if ($extension eq "x" && !defined($stat_real)) {
6057 if (!defined($stat_real)) {
6058 $stat_real = get_stat_real($linenr, $lc);
6059 }
6060 WARN("VSPRINTF_SPECIFIER_PX",
6061 "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");
6062 }
6063 }
6064 if ($bad_specifier ne "") {
6065 my $stat_real = get_stat_real($linenr, $lc);
6066 my $ext_type = "Invalid";
6067 my $use = "";
6068 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006069 $use = " - use %pS instead";
6070 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6071 }
6072
6073 WARN("VSPRINTF_POINTER_EXTENSION",
6074 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6075 }
Joe Perches0b523762017-05-08 15:55:36 -07006076 }
6077 }
6078
Andy Whitcroft554e1652012-01-10 15:09:57 -08006079# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006080 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006081 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006082 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006083
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006084 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006085 my $ms_val = $7;
6086 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006087
Andy Whitcroft554e1652012-01-10 15:09:57 -08006088 if ($ms_size =~ /^(0x|)0$/i) {
6089 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006090 "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 -08006091 } elsif ($ms_size =~ /^(0x|)1$/i) {
6092 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006093 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6094 }
6095 }
6096
Joe Perches98a9bba2014-01-23 15:54:52 -08006097# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006098# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006099# defined $stat &&
6100# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6101# if (WARN("PREFER_ETHER_ADDR_COPY",
6102# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6103# $fix) {
6104# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6105# }
6106# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006107
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006108# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006109# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006110# defined $stat &&
6111# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6112# WARN("PREFER_ETHER_ADDR_EQUAL",
6113# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6114# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006115
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006116# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6117# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006118# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07006119# defined $stat &&
6120# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6121#
6122# my $ms_val = $7;
6123#
6124# if ($ms_val =~ /^(?:0x|)0+$/i) {
6125# if (WARN("PREFER_ETH_ZERO_ADDR",
6126# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6127# $fix) {
6128# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6129# }
6130# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6131# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6132# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6133# $fix) {
6134# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6135# }
6136# }
6137# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006138
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006139# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006140 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006141 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006142 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006143 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006144 my $call = $1;
6145 my $cast1 = deparenthesize($2);
6146 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006147 my $cast2 = deparenthesize($7);
6148 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006149 my $cast;
6150
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006151 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006152 $cast = "$cast1 or $cast2";
6153 } elsif ($cast1 ne "") {
6154 $cast = $cast1;
6155 } else {
6156 $cast = $cast2;
6157 }
6158 WARN("MINMAX",
6159 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006160 }
6161 }
6162
Joe Perches4a273192012-07-30 14:41:20 -07006163# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006164 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006165 defined $stat &&
6166 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6167 my $min = $1;
6168 my $max = $7;
6169 if ($min eq $max) {
6170 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006171 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Joe Perches4a273192012-07-30 14:41:20 -07006172 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6173 $min > $max) {
6174 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006175 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Joe Perches4a273192012-07-30 14:41:20 -07006176 }
6177 }
6178
Joe Perches823b7942013-11-12 15:10:15 -08006179# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006180 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006181 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006182 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006183 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6184 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6185 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6186 my $lc = $stat =~ tr@\n@@;
6187 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006188 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006189 WARN("NAKED_SSCANF",
6190 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6191 }
6192
Joe Perchesafc819a2014-06-04 16:12:08 -07006193# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006194 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006195 defined $stat &&
6196 $line =~ /\bsscanf\b/) {
6197 my $lc = $stat =~ tr@\n@@;
6198 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006199 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006200 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6201 my $format = $6;
6202 my $count = $format =~ tr@%@%@;
6203 if ($count == 1 &&
6204 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6205 WARN("SSCANF_TO_KSTRTO",
6206 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6207 }
6208 }
6209 }
6210
Joe Perches70dc8a42013-09-11 14:23:58 -07006211# check for new externs in .h files.
6212 if ($realfile =~ /\.h$/ &&
6213 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006214 if (CHK("AVOID_EXTERNS",
6215 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006216 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006217 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006218 }
6219 }
6220
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006221# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006222 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006223 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006224 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006225 my $function_name = $1;
6226 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006227
6228 my $s = $stat;
6229 if (defined $cond) {
6230 substr($s, 0, length($cond), '');
6231 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006232 if ($s =~ /^\s*;/ &&
6233 $function_name ne 'uninitialized_var')
6234 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006235 WARN("AVOID_EXTERNS",
6236 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006237 }
6238
6239 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006240 WARN("FUNCTION_ARGUMENTS",
6241 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006242 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006243
6244 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6245 $stat =~ /^.\s*extern\s+/)
6246 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006247 WARN("AVOID_EXTERNS",
6248 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006249 }
6250
Joe Perchesa0ad7592017-07-10 15:52:19 -07006251# check for function declarations that have arguments without identifier names
6252 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006253 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006254 $1 ne "void") {
6255 my $args = trim($1);
6256 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6257 my $arg = trim($1);
6258 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6259 WARN("FUNCTION_ARGUMENTS",
6260 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6261 }
6262 }
6263 }
6264
Joe Perchesa0ad7592017-07-10 15:52:19 -07006265# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006266 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006267 defined $stat &&
6268 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6269 $context_function = $1;
6270
6271# check for multiline function definition with misplaced open brace
6272 my $ok = 0;
6273 my $cnt = statement_rawlines($stat);
6274 my $herectx = $here . "\n";
6275 for (my $n = 0; $n < $cnt; $n++) {
6276 my $rl = raw_line($linenr, $n);
6277 $herectx .= $rl . "\n";
6278 $ok = 1 if ($rl =~ /^[ \+]\{/);
6279 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6280 last if $rl =~ /^[ \+].*\{/;
6281 }
6282 if (!$ok) {
6283 ERROR("OPEN_BRACE",
6284 "open brace '{' following function definitions go on the next line\n" . $herectx);
6285 }
6286 }
6287
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006288# checks for new __setup's
6289 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6290 my $name = $1;
6291
6292 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006293 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006294 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006295 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006296 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006297
Joe Perchese29a70f2019-03-07 16:28:35 -08006298# check for pointless casting of alloc functions
6299 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006300 WARN("UNNECESSARY_CASTS",
6301 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006302 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006303
Joe Perchesa640d252013-07-03 15:05:21 -07006304# alloc style
6305# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006306 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006307 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Joe Perchesa640d252013-07-03 15:05:21 -07006308 CHK("ALLOC_SIZEOF_STRUCT",
6309 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6310 }
6311
Joe Perches60a55362014-06-04 16:12:07 -07006312# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006313 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006314 defined $stat &&
6315 $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 -07006316 my $oldfunc = $3;
6317 my $a1 = $4;
6318 my $a2 = $10;
6319 my $newfunc = "kmalloc_array";
6320 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006321 my $r1 = $a1;
6322 my $r2 = $a2;
6323 if ($a1 =~ /^sizeof\s*\S/) {
6324 $r1 = $a2;
6325 $r2 = $a1;
6326 }
6327 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6328 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006329 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006330 my $herectx = get_stat_here($linenr, $cnt, $here);
6331
Joe Perches60a55362014-06-04 16:12:07 -07006332 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006333 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6334 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006335 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006336 $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 -07006337 }
6338 }
6339 }
6340
Joe Perches972fdea2013-04-29 16:18:12 -07006341# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006342 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006343 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6344 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006345 WARN("KREALLOC_ARG_REUSE",
6346 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6347 }
6348
Joe Perches5ce59ae2013-02-21 16:44:18 -08006349# check for alloc argument mismatch
6350 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6351 WARN("ALLOC_ARRAY_ARGS",
6352 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6353 }
6354
Joe Perchescaf2a542011-01-12 16:59:56 -08006355# check for multiple semicolons
6356 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006357 if (WARN("ONE_SEMICOLON",
6358 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6359 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006360 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006361 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006362 }
6363
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006364# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6365 if ($realfile !~ m@^include/uapi/@ &&
6366 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006367 my $ull = "";
6368 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6369 if (CHK("BIT_MACRO",
6370 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6371 $fix) {
6372 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6373 }
6374 }
6375
Joe Perches2d632742016-05-20 17:04:00 -07006376# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6377 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6378 my $config = $1;
6379 if (WARN("PREFER_IS_ENABLED",
6380 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6381 $fix) {
6382 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6383 }
6384 }
6385
Joe Perchese81f2392014-08-06 16:11:25 -07006386# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006387 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6388 my $has_break = 0;
6389 my $has_statement = 0;
6390 my $count = 0;
6391 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006392 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006393 $prevline--;
6394 my $rline = $rawlines[$prevline - 1];
6395 my $fline = $lines[$prevline - 1];
6396 last if ($fline =~ /^\@\@/);
6397 next if ($fline =~ /^\-/);
6398 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6399 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6400 next if ($fline =~ /^.[\s$;]*$/);
6401 $has_statement = 1;
6402 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006403 $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 -08006404 }
6405 if (!$has_break && $has_statement) {
6406 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006407 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006408 }
6409 }
6410
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006411# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006412 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006413 defined $stat &&
6414 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006415 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006416 my $herectx = get_stat_here($linenr, $cnt, $here);
6417
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006418 WARN("DEFAULT_NO_BREAK",
6419 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006420 }
6421
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006422# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006423 if ($line =~ /\b__FUNCTION__\b/) {
6424 if (WARN("USE_FUNC",
6425 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6426 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006427 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006428 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006429 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006430
Joe Perches62ec8182015-02-13 14:38:18 -08006431# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6432 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6433 ERROR("DATE_TIME",
6434 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6435 }
6436
Joe Perches2c924882012-03-23 15:02:20 -07006437# check for use of yield()
6438 if ($line =~ /\byield\s*\(\s*\)/) {
6439 WARN("YIELD",
6440 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6441 }
6442
Joe Perches179f8f42013-07-03 15:05:30 -07006443# check for comparisons against true and false
6444 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6445 my $lead = $1;
6446 my $arg = $2;
6447 my $test = $3;
6448 my $otype = $4;
6449 my $trail = $5;
6450 my $op = "!";
6451
6452 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6453
6454 my $type = lc($otype);
6455 if ($type =~ /^(?:true|false)$/) {
6456 if (("$test" eq "==" && "$type" eq "true") ||
6457 ("$test" eq "!=" && "$type" eq "false")) {
6458 $op = "";
6459 }
6460
6461 CHK("BOOL_COMPARISON",
6462 "Using comparison to $otype is error prone\n" . $herecurr);
6463
6464## maybe suggesting a correct construct would better
6465## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6466
6467 }
6468 }
6469
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006470# check for semaphores initialized locked
6471 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006472 WARN("CONSIDER_COMPLETION",
6473 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006474 }
Joe Perches6712d852012-03-23 15:02:20 -07006475
Joe Perches67d0a072011-10-31 17:13:10 -07006476# recommend kstrto* over simple_strto* and strict_strto*
6477 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006478 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006479 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006480 }
Joe Perches6712d852012-03-23 15:02:20 -07006481
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006482# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006483 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006484 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006485 "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 -07006486 }
Joe Perches6712d852012-03-23 15:02:20 -07006487
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006488# check for spin_is_locked(), suggest lockdep instead
6489 if ($line =~ /\bspin_is_locked\(/) {
6490 WARN("USE_LOCKDEP",
6491 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6492 }
6493
Joe Perches9189c7e2018-09-07 15:26:18 -07006494# check for deprecated apis
6495 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6496 my $deprecated_api = $1;
6497 my $new_api = $deprecated_apis{$deprecated_api};
6498 WARN("DEPRECATED_API",
6499 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6500 }
6501
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006502# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006503# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006504 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006505 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006506 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006507 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006508 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006509
6510# use of NR_CPUS is usually wrong
6511# ignore definitions of NR_CPUS and usage to define arrays as likely right
6512 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006513 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6514 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006515 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6516 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6517 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006518 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006519 WARN("NR_CPUS",
6520 "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 -07006521 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006522
Joe Perches52ea8502013-11-12 15:10:09 -08006523# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6524 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6525 ERROR("DEFINE_ARCH_HAS",
6526 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6527 }
6528
Joe Perchesacd93622015-02-13 14:38:38 -08006529# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006530 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006531 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6532 WARN("LIKELY_MISUSE",
6533 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6534 }
6535
Denis Efremovde3f1862019-09-25 16:49:25 -07006536# nested likely/unlikely calls
6537 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6538 WARN("LIKELY_MISUSE",
6539 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6540 }
6541
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006542# whine mightly about in_atomic
6543 if ($line =~ /\bin_atomic\s*\(/) {
6544 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006545 ERROR("IN_ATOMIC",
6546 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006547 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006548 WARN("IN_ATOMIC",
6549 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006550 }
6551 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006552
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006553# check for mutex_trylock_recursive usage
6554 if ($line =~ /mutex_trylock_recursive/) {
6555 ERROR("LOCKING",
6556 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6557 }
6558
Peter Zijlstra1704f472010-03-19 01:37:42 +01006559# check for lockdep_set_novalidate_class
6560 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6561 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6562 if ($realfile !~ m@^kernel/lockdep@ &&
6563 $realfile !~ m@^include/linux/lockdep@ &&
6564 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006565 ERROR("LOCKDEP",
6566 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006567 }
6568 }
Dave Jones88f88312011-01-12 16:59:59 -08006569
Joe Perchesb392c642015-04-16 12:44:16 -07006570 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6571 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006572 WARN("EXPORTED_WORLD_WRITABLE",
6573 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006574 }
Joe Perches24358802014-04-03 14:49:13 -07006575
Joe Perches00180462018-02-06 15:38:55 -08006576# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6577# and whether or not function naming is typical and if
6578# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006579 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006580 defined $stat &&
6581 $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*\)/) {
6582 my $var = $1;
6583 my $perms = $2;
6584 my $show = $3;
6585 my $store = $4;
6586 my $octal_perms = perms_to_octal($perms);
6587 if ($show =~ /^${var}_show$/ &&
6588 $store =~ /^${var}_store$/ &&
6589 $octal_perms eq "0644") {
6590 if (WARN("DEVICE_ATTR_RW",
6591 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6592 $fix) {
6593 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6594 }
6595 } elsif ($show =~ /^${var}_show$/ &&
6596 $store =~ /^NULL$/ &&
6597 $octal_perms eq "0444") {
6598 if (WARN("DEVICE_ATTR_RO",
6599 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6600 $fix) {
6601 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6602 }
6603 } elsif ($show =~ /^NULL$/ &&
6604 $store =~ /^${var}_store$/ &&
6605 $octal_perms eq "0200") {
6606 if (WARN("DEVICE_ATTR_WO",
6607 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6608 $fix) {
6609 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6610 }
6611 } elsif ($octal_perms eq "0644" ||
6612 $octal_perms eq "0444" ||
6613 $octal_perms eq "0200") {
6614 my $newshow = "$show";
6615 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6616 my $newstore = $store;
6617 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6618 my $rename = "";
6619 if ($show ne $newshow) {
6620 $rename .= " '$show' to '$newshow'";
6621 }
6622 if ($store ne $newstore) {
6623 $rename .= " '$store' to '$newstore'";
6624 }
6625 WARN("DEVICE_ATTR_FUNCTIONS",
6626 "Consider renaming function(s)$rename\n" . $herecurr);
6627 } else {
6628 WARN("DEVICE_ATTR_PERMS",
6629 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6630 }
6631 }
6632
Joe Perches515a2352014-04-03 14:49:24 -07006633# Mode permission misuses where it seems decimal should be octal
6634# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006635# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6636# specific definition of not visible in sysfs.
6637# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6638# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006639 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006640 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006641 $line =~ /$mode_perms_search/) {
6642 foreach my $entry (@mode_permission_funcs) {
6643 my $func = $entry->[0];
6644 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006645
Joe Perches459cf0a2016-10-11 13:52:19 -07006646 my $lc = $stat =~ tr@\n@@;
6647 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006648 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006649
Joe Perches515a2352014-04-03 14:49:24 -07006650 my $skip_args = "";
6651 if ($arg_pos > 1) {
6652 $arg_pos--;
6653 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6654 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006655 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006656 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006657 my $val = $1;
6658 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006659 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6660 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6661 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006662 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006663 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006664 }
6665 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006666 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006667 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006668 }
Joe Perches24358802014-04-03 14:49:13 -07006669 }
6670 }
6671 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006672
Joe Perches459cf0a2016-10-11 13:52:19 -07006673# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006674 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006675 my $oval = $1;
6676 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006677 if (WARN("SYMBOLIC_PERMS",
6678 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6679 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006680 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006681 }
6682 }
6683
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006684# validate content of MODULE_LICENSE against list from include/linux/module.h
6685 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6686 my $extracted_string = get_quoted_string($line, $rawline);
6687 my $valid_licenses = qr{
6688 GPL|
6689 GPL\ v2|
6690 GPL\ and\ additional\ rights|
6691 Dual\ BSD/GPL|
6692 Dual\ MIT/GPL|
6693 Dual\ MPL/GPL|
6694 Proprietary
6695 }x;
6696 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6697 WARN("MODULE_LICENSE",
6698 "unknown module license " . $extracted_string . "\n" . $herecurr);
6699 }
6700 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006701
6702# check for sysctl duplicate constants
6703 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6704 WARN("DUPLICATED_SYSCTL_CONST",
6705 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6706 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006707 }
6708
6709 # If we have no input at all, then there is nothing to report on
6710 # so just keep quiet.
6711 if ($#rawlines == -1) {
6712 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006713 }
6714
Andy Whitcroft8905a672007-11-28 16:21:06 -08006715 # In mailback mode only produce a report in the negative, for
6716 # things that appear to be patches.
6717 if ($mailback && ($clean == 1 || !$is_patch)) {
6718 exit(0);
6719 }
6720
6721 # This is not a patch, and we are are in 'no-patch' mode so
6722 # just keep quiet.
6723 if (!$chk_patch && !$is_patch) {
6724 exit(0);
6725 }
6726
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006727 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006728 ERROR("NOT_UNIFIED_DIFF",
6729 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006730 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006731 if ($is_patch && $has_commit_log && $chk_signoff) {
6732 if ($signoff == 0) {
6733 ERROR("MISSING_SIGN_OFF",
6734 "Missing Signed-off-by: line(s)\n");
6735 } elsif (!$authorsignoff) {
6736 WARN("NO_AUTHOR_SIGN_OFF",
6737 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6738 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006739 }
6740
Andy Whitcroft8905a672007-11-28 16:21:06 -08006741 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006742 if ($summary && !($clean == 1 && $quiet == 1)) {
6743 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006744 print "total: $cnt_error errors, $cnt_warn warnings, " .
6745 (($check)? "$cnt_chk checks, " : "") .
6746 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006747 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006748
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006749 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006750 # If there were any defects found and not already fixing them
6751 if (!$clean and !$fix) {
6752 print << "EOM"
6753
6754NOTE: For some of the reported defects, checkpatch may be able to
6755 mechanically convert to the typical style using --fix or --fix-inplace.
6756EOM
6757 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006758 # If there were whitespace errors which cleanpatch can fix
6759 # then suggest that.
6760 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006761 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006762 print << "EOM"
6763
6764NOTE: Whitespace errors detected.
6765 You may wish to use scripts/cleanpatch or scripts/cleanfile
6766EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006767 }
6768 }
6769
Joe Perchesd752fcc2014-08-06 16:11:05 -07006770 if ($clean == 0 && $fix &&
6771 ("@rawlines" ne "@fixed" ||
6772 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006773 my $newfile = $filename;
6774 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006775 my $linecount = 0;
6776 my $f;
6777
Joe Perchesd752fcc2014-08-06 16:11:05 -07006778 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6779
Joe Perches3705ce52013-07-03 15:05:31 -07006780 open($f, '>', $newfile)
6781 or die "$P: Can't open $newfile for write\n";
6782 foreach my $fixed_line (@fixed) {
6783 $linecount++;
6784 if ($file) {
6785 if ($linecount > 3) {
6786 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006787 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006788 }
6789 } else {
6790 print $f $fixed_line . "\n";
6791 }
6792 }
6793 close($f);
6794
6795 if (!$quiet) {
6796 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006797
Joe Perches3705ce52013-07-03 15:05:31 -07006798Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6799
6800Do _NOT_ trust the results written to this file.
6801Do _NOT_ submit these changes without inspecting them for correctness.
6802
6803This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6804No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006805EOM
6806 }
6807 }
6808
Joe Perchesd8469f12015-06-25 15:03:00 -07006809 if ($quiet == 0) {
6810 print "\n";
6811 if ($clean == 1) {
6812 print "$vname has no obvious style problems and is ready for submission.\n";
6813 } else {
6814 print "$vname has style problems, please review.\n";
6815 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006816 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006817 return $clean;
6818}