blob: 4e047586a68228a5005c6693bf18166e54a4f649 [file] [log] [blame]
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001#!/usr/bin/env perl
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002# SPDX-License-Identifier: GPL-2.0
3#
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004# (c) 2001, Dave Jones. (the file handling bit)
5# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
6# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
7# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02008# (c) 2010-2018 Joe Perches <joe@perches.com>
Joe Hershbergerba2beb52011-10-18 10:06:59 +00009
10use strict;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020011use warnings;
Tom Rinia50f8de2014-02-27 08:27:28 -050012use POSIX;
Dan Murphy463f6882017-01-31 14:15:53 -060013use File::Basename;
14use Cwd 'abs_path';
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020015use Term::ANSIColor qw(:constants);
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +020016use Encode qw(decode encode);
Joe Hershbergerba2beb52011-10-18 10:06:59 +000017
18my $P = $0;
Dan Murphy463f6882017-01-31 14:15:53 -060019my $D = dirname(abs_path($P));
Joe Hershbergerba2beb52011-10-18 10:06:59 +000020
21my $V = '0.32';
22
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
29my $tst_only;
30my $emacs = 0;
31my $terse = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020032my $showfile = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +000033my $file = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020034my $git = 0;
35my %git_commits = ();
Joe Hershbergerba2beb52011-10-18 10:06:59 +000036my $check = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020037my $check_orig = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +000038my $summary = 1;
39my $mailback = 0;
40my $summary_file = 0;
41my $show_types = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020042my $list_types = 0;
Tom Rinia50f8de2014-02-27 08:27:28 -050043my $fix = 0;
44my $fix_inplace = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +000045my $root;
46my %debug;
Tom Rinia50f8de2014-02-27 08:27:28 -050047my %camelcase = ();
48my %use_type = ();
49my @use = ();
Joe Hershbergerba2beb52011-10-18 10:06:59 +000050my %ignore_type = ();
51my @ignore = ();
52my $help = 0;
53my $configuration_file = ".checkpatch.conf";
Simon Glassa33d9822020-05-22 16:32:35 -060054my $max_line_length = 100;
Tom Rinia50f8de2014-02-27 08:27:28 -050055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020057my $min_conf_desc_length = 4;
Dan Murphy463f6882017-01-31 14:15:53 -060058my $spelling_file = "$D/spelling.txt";
59my $codespell = 0;
60my $codespellfile = "/usr/share/codespell/dictionary.txt";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020061my $conststructsfile = "$D/const_structs.checkpatch";
62my $typedefsfile = "";
Simon Glassc8f4d092020-05-22 16:32:36 -060063my $u_boot = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020064my $color = "auto";
Tom Rinid2e30332020-06-16 10:29:46 -040065my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
66# git output parsing needs US English output, so first set backtick child process LANGUAGE
67my $git_command ='export LANGUAGE=en_US.UTF-8; git';
68my $tabsize = 8;
Joe Hershbergerba2beb52011-10-18 10:06:59 +000069
70sub help {
71 my ($exitcode) = @_;
72
73 print << "EOM";
74Usage: $P [OPTION]... [FILE]...
75Version: $V
76
77Options:
78 -q, --quiet quiet
79 --no-tree run without a kernel tree
80 --no-signoff do not check for 'Signed-off-by' line
81 --patch treat FILE as patchfile (default)
82 --emacs emacs compile window format
83 --terse one line per report
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020084 --showfile emit diffed file position, not input file position
85 -g, --git treat FILE as a single commit or git revision range
86 single git commit with:
87 <rev>
88 <rev>^
89 <rev>~n
90 multiple git commits with:
91 <rev1>..<rev2>
92 <rev1>...<rev2>
93 <rev>-<count>
94 git merges are ignored
Joe Hershbergerba2beb52011-10-18 10:06:59 +000095 -f, --file treat FILE as regular source file
96 --subjective, --strict enable more subjective tests
Heinrich Schuchardt8716f582017-09-12 09:57:45 +020097 --list-types list the possible message types
Tom Rinia50f8de2014-02-27 08:27:28 -050098 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Hershbergerba2beb52011-10-18 10:06:59 +000099 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200100 --show-types show the specific message type in the output
Simon Glassa33d9822020-05-22 16:32:35 -0600101 --max-line-length=n set the maximum line length, (default $max_line_length)
102 if exceeded, warn on patches
103 requires --strict for use with --file
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200104 --min-conf-desc-length=n set the min description length, if shorter, warn
Tom Rinid2e30332020-06-16 10:29:46 -0400105 --tab-size=n set the number of spaces for tab (default $tabsize)
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000106 --root=PATH PATH to the kernel tree root
107 --no-summary suppress the per-file summary
108 --mailback only produce a report in case of warnings/errors
109 --summary-file include the filename in summary
110 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
111 'values', 'possible', 'type', and 'attr' (default
112 is all off)
113 --test-only=WORD report only warnings/errors containing WORD
114 literally
Tom Rinia50f8de2014-02-27 08:27:28 -0500115 --fix EXPERIMENTAL - may create horrible results
116 If correctable single-line errors exist, create
117 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
118 with potential errors corrected to the preferred
119 checkpatch style
120 --fix-inplace EXPERIMENTAL - may create horrible results
121 Is the same as --fix, but overwrites the input
122 file. It's your fault if there's no backup or git
123 --ignore-perl-version override checking of perl version. expect
124 runtime errors.
Dan Murphy463f6882017-01-31 14:15:53 -0600125 --codespell Use the codespell dictionary for spelling/typos
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200126 (default:/usr/share/codespell/dictionary.txt)
Dan Murphy463f6882017-01-31 14:15:53 -0600127 --codespellfile Use this codespell dictionary
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200128 --typedefsfile Read additional types from this file
129 --color[=WHEN] Use colors 'always', 'never', or only when output
130 is a terminal ('auto'). Default is 'auto'.
Simon Glassc8f4d092020-05-22 16:32:36 -0600131 --u-boot Run additional checks for U-Boot
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000132 -h, --help, --version display this help and exit
133
134When FILE is - read standard input.
135EOM
136
137 exit($exitcode);
138}
139
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200140sub uniq {
141 my %seen;
142 return grep { !$seen{$_}++ } @_;
143}
144
145sub list_types {
146 my ($exitcode) = @_;
147
148 my $count = 0;
149
150 local $/ = undef;
151
152 open(my $script, '<', abs_path($P)) or
153 die "$P: Can't read '$P' $!\n";
154
155 my $text = <$script>;
156 close($script);
157
158 my @types = ();
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200159 # Also catch when type or level is passed through a variable
160 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200161 push (@types, $_);
162 }
163 @types = sort(uniq(@types));
164 print("#\tMessage type\n\n");
165 foreach my $type (@types) {
166 print(++$count . "\t" . $type . "\n");
167 }
168
169 exit($exitcode);
170}
171
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000172my $conf = which_conf($configuration_file);
173if (-f $conf) {
174 my @conf_args;
175 open(my $conffile, '<', "$conf")
176 or warn "$P: Can't find a readable $configuration_file file $!\n";
177
178 while (<$conffile>) {
179 my $line = $_;
180
181 $line =~ s/\s*\n?$//g;
182 $line =~ s/^\s*//g;
183 $line =~ s/\s+/ /g;
184
185 next if ($line =~ m/^\s*#/);
186 next if ($line =~ m/^\s*$/);
187
188 my @words = split(" ", $line);
189 foreach my $word (@words) {
190 last if ($word =~ m/^#/);
191 push (@conf_args, $word);
192 }
193 }
194 close($conffile);
195 unshift(@ARGV, @conf_args) if @conf_args;
196}
197
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200198# Perl's Getopt::Long allows options to take optional arguments after a space.
199# Prevent --color by itself from consuming other arguments
200foreach (@ARGV) {
201 if ($_ eq "--color" || $_ eq "-color") {
202 $_ = "--color=$color";
203 }
204}
205
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000206GetOptions(
207 'q|quiet+' => \$quiet,
208 'tree!' => \$tree,
209 'signoff!' => \$chk_signoff,
210 'patch!' => \$chk_patch,
211 'emacs!' => \$emacs,
212 'terse!' => \$terse,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200213 'showfile!' => \$showfile,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000214 'f|file!' => \$file,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200215 'g|git!' => \$git,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000216 'subjective!' => \$check,
217 'strict!' => \$check,
218 'ignore=s' => \@ignore,
Tom Rinia50f8de2014-02-27 08:27:28 -0500219 'types=s' => \@use,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000220 'show-types!' => \$show_types,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200221 'list-types!' => \$list_types,
Kim Phillips9b7b8372013-02-28 12:53:52 +0000222 'max-line-length=i' => \$max_line_length,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200223 'min-conf-desc-length=i' => \$min_conf_desc_length,
Tom Rinid2e30332020-06-16 10:29:46 -0400224 'tab-size=i' => \$tabsize,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000225 'root=s' => \$root,
226 'summary!' => \$summary,
227 'mailback!' => \$mailback,
228 'summary-file!' => \$summary_file,
Tom Rinia50f8de2014-02-27 08:27:28 -0500229 'fix!' => \$fix,
230 'fix-inplace!' => \$fix_inplace,
231 'ignore-perl-version!' => \$ignore_perl_version,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000232 'debug=s' => \%debug,
233 'test-only=s' => \$tst_only,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200234 'codespell!' => \$codespell,
235 'codespellfile=s' => \$codespellfile,
236 'typedefsfile=s' => \$typedefsfile,
Simon Glassc8f4d092020-05-22 16:32:36 -0600237 'u-boot' => \$u_boot,
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200238 'color=s' => \$color,
239 'no-color' => \$color, #keep old behaviors of -nocolor
240 'nocolor' => \$color, #keep old behaviors of -nocolor
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000241 'h|help' => \$help,
242 'version' => \$help
243) or help(1);
244
245help(0) if ($help);
246
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200247list_types(0) if ($list_types);
248
Tom Rinia50f8de2014-02-27 08:27:28 -0500249$fix = 1 if ($fix_inplace);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200250$check_orig = $check;
Tom Rinia50f8de2014-02-27 08:27:28 -0500251
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000252my $exit = 0;
253
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200254my $perl_version_ok = 1;
Tom Rinia50f8de2014-02-27 08:27:28 -0500255if ($^V && $^V lt $minimum_perl_version) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200256 $perl_version_ok = 0;
Tom Rinia50f8de2014-02-27 08:27:28 -0500257 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200258 exit(1) if (!$ignore_perl_version);
Tom Rinia50f8de2014-02-27 08:27:28 -0500259}
260
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200261#if no filenames are given, push '-' to read patch from stdin
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000262if ($#ARGV < 0) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200263 push(@ARGV, '-');
264}
265
266if ($color =~ /^[01]$/) {
267 $color = !$color;
268} elsif ($color =~ /^always$/i) {
269 $color = 1;
270} elsif ($color =~ /^never$/i) {
271 $color = 0;
272} elsif ($color =~ /^auto$/i) {
273 $color = (-t STDOUT);
274} else {
275 die "Invalid color mode: $color\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000276}
277
Tom Rinid2e30332020-06-16 10:29:46 -0400278# skip TAB size 1 to avoid additional checks on $tabsize - 1
279die "Invalid TAB size: $tabsize\n" if ($tabsize < 2);
280
Tom Rinia50f8de2014-02-27 08:27:28 -0500281sub hash_save_array_words {
282 my ($hashRef, $arrayRef) = @_;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000283
Tom Rinia50f8de2014-02-27 08:27:28 -0500284 my @array = split(/,/, join(',', @$arrayRef));
285 foreach my $word (@array) {
286 $word =~ s/\s*\n?$//g;
287 $word =~ s/^\s*//g;
288 $word =~ s/\s+/ /g;
289 $word =~ tr/[a-z]/[A-Z]/;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000290
Tom Rinia50f8de2014-02-27 08:27:28 -0500291 next if ($word =~ m/^\s*#/);
292 next if ($word =~ m/^\s*$/);
293
294 $hashRef->{$word}++;
295 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000296}
297
Tom Rinia50f8de2014-02-27 08:27:28 -0500298sub hash_show_words {
299 my ($hashRef, $prefix) = @_;
300
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200301 if (keys %$hashRef) {
302 print "\nNOTE: $prefix message types:";
Tom Rinia50f8de2014-02-27 08:27:28 -0500303 foreach my $word (sort keys %$hashRef) {
304 print " $word";
305 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200306 print "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -0500307 }
308}
309
310hash_save_array_words(\%ignore_type, \@ignore);
311hash_save_array_words(\%use_type, \@use);
312
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000313my $dbg_values = 0;
314my $dbg_possible = 0;
315my $dbg_type = 0;
316my $dbg_attr = 0;
317for my $key (keys %debug) {
318 ## no critic
319 eval "\${dbg_$key} = '$debug{$key}';";
320 die "$@" if ($@);
321}
322
323my $rpt_cleaners = 0;
324
325if ($terse) {
326 $emacs = 1;
327 $quiet++;
328}
329
330if ($tree) {
331 if (defined $root) {
332 if (!top_of_kernel_tree($root)) {
333 die "$P: $root: --root does not point at a valid tree\n";
334 }
335 } else {
336 if (top_of_kernel_tree('.')) {
337 $root = '.';
338 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
339 top_of_kernel_tree($1)) {
340 $root = $1;
341 }
342 }
343
344 if (!defined $root) {
345 print "Must be run from the top-level dir. of a kernel tree\n";
346 exit(2);
347 }
348}
349
350my $emitted_corrupt = 0;
351
352our $Ident = qr{
353 [A-Za-z_][A-Za-z\d_]*
354 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
355 }x;
356our $Storage = qr{extern|static|asmlinkage};
357our $Sparse = qr{
358 __user|
359 __kernel|
360 __force|
361 __iomem|
362 __must_check|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000363 __kprobes|
364 __ref|
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200365 __refconst|
366 __refdata|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200367 __rcu|
368 __private
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000369 }x;
Tom Rinia50f8de2014-02-27 08:27:28 -0500370our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
371our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
372our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
373our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
374our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000375
376# Notes to $Attribute:
377# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
378our $Attribute = qr{
379 const|
380 __percpu|
381 __nocast|
382 __safe|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200383 __bitwise|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000384 __packed__|
385 __packed2__|
386 __naked|
387 __maybe_unused|
388 __always_unused|
389 __noreturn|
390 __used|
391 __cold|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200392 __pure|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000393 __noclone|
394 __deprecated|
395 __read_mostly|
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200396 __ro_after_init|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000397 __kprobes|
Tom Rinia50f8de2014-02-27 08:27:28 -0500398 $InitAttribute|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000399 ____cacheline_aligned|
400 ____cacheline_aligned_in_smp|
401 ____cacheline_internodealigned_in_smp|
402 __weak
403 }x;
404our $Modifier;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200405our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000406our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
407our $Lval = qr{$Ident(?:$Member)*};
408
Tom Rinia50f8de2014-02-27 08:27:28 -0500409our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
410our $Binary = qr{(?i)0b[01]+$Int_type?};
411our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
412our $Int = qr{[0-9]+$Int_type?};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200413our $Octal = qr{0[0-7]+$Int_type?};
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200414our $String = qr{"[X\t]*"};
Kim Phillips9b7b8372013-02-28 12:53:52 +0000415our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
416our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
417our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
418our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200419our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Kim Phillips9b7b8372013-02-28 12:53:52 +0000420our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200421our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Tom Rinia50f8de2014-02-27 08:27:28 -0500422our $Arithmetic = qr{\+|-|\*|\/|%};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000423our $Operators = qr{
424 <=|>=|==|!=|
425 =>|->|<<|>>|<|>|!|~|
Tom Rinia50f8de2014-02-27 08:27:28 -0500426 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000427 }x;
428
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200429our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
430
431our $BasicType;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000432our $NonptrType;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200433our $NonptrTypeMisordered;
Tom Rinia50f8de2014-02-27 08:27:28 -0500434our $NonptrTypeWithAttr;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000435our $Type;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200436our $TypeMisordered;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000437our $Declare;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200438our $DeclareMisordered;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000439
Kim Phillips9b7b8372013-02-28 12:53:52 +0000440our $NON_ASCII_UTF8 = qr{
441 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000442 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
443 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
444 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
445 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
446 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
447 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
448}x;
449
Kim Phillips9b7b8372013-02-28 12:53:52 +0000450our $UTF8 = qr{
451 [\x09\x0A\x0D\x20-\x7E] # ASCII
452 | $NON_ASCII_UTF8
453}x;
454
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200455our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
456our $typeOtherOSTypedefs = qr{(?x:
457 u_(?:char|short|int|long) | # bsd
458 u(?:nchar|short|int|long) # sysv
459)};
460our $typeKernelTypedefs = qr{(?x:
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000461 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
462 atomic_t
463)};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200464our $typeTypedefs = qr{(?x:
465 $typeC99Typedefs\b|
466 $typeOtherOSTypedefs\b|
467 $typeKernelTypedefs\b
468)};
469
470our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000471
472our $logFunctions = qr{(?x:
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200473 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Tom Rinia50f8de2014-02-27 08:27:28 -0500474 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200475 TP_printk|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000476 WARN(?:_RATELIMIT|_ONCE|)|
477 panic|
James Byrnea6859792019-11-21 14:32:46 +0000478 debug|
479 printf|
Tom Rinia50f8de2014-02-27 08:27:28 -0500480 MODULE_[A-Z_]+|
481 seq_vprintf|seq_printf|seq_puts
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000482)};
483
Tom Rinid2e30332020-06-16 10:29:46 -0400484our $allocFunctions = qr{(?x:
485 (?:(?:devm_)?
486 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
487 kstrdup(?:_const)? |
488 kmemdup(?:_nul)?) |
489 (?:\w+)?alloc_skb(?:_ip_align)? |
490 # dev_alloc_skb/netdev_alloc_skb, et al
491 dma_alloc_coherent
492)};
493
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000494our $signature_tags = qr{(?xi:
495 Signed-off-by:|
Tom Rinid2e30332020-06-16 10:29:46 -0400496 Co-developed-by:|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000497 Acked-by:|
498 Tested-by:|
499 Reviewed-by:|
500 Reported-by:|
Tom Rinia50f8de2014-02-27 08:27:28 -0500501 Suggested-by:|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000502 To:|
503 Cc:
504)};
505
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200506our @typeListMisordered = (
507 qr{char\s+(?:un)?signed},
508 qr{int\s+(?:(?:un)?signed\s+)?short\s},
509 qr{int\s+short(?:\s+(?:un)?signed)},
510 qr{short\s+int(?:\s+(?:un)?signed)},
511 qr{(?:un)?signed\s+int\s+short},
512 qr{short\s+(?:un)?signed},
513 qr{long\s+int\s+(?:un)?signed},
514 qr{int\s+long\s+(?:un)?signed},
515 qr{long\s+(?:un)?signed\s+int},
516 qr{int\s+(?:un)?signed\s+long},
517 qr{int\s+(?:un)?signed},
518 qr{int\s+long\s+long\s+(?:un)?signed},
519 qr{long\s+long\s+int\s+(?:un)?signed},
520 qr{long\s+long\s+(?:un)?signed\s+int},
521 qr{long\s+long\s+(?:un)?signed},
522 qr{long\s+(?:un)?signed},
523);
524
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000525our @typeList = (
526 qr{void},
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200527 qr{(?:(?:un)?signed\s+)?char},
528 qr{(?:(?:un)?signed\s+)?short\s+int},
529 qr{(?:(?:un)?signed\s+)?short},
530 qr{(?:(?:un)?signed\s+)?int},
531 qr{(?:(?:un)?signed\s+)?long\s+int},
532 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
533 qr{(?:(?:un)?signed\s+)?long\s+long},
534 qr{(?:(?:un)?signed\s+)?long},
535 qr{(?:un)?signed},
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000536 qr{float},
537 qr{double},
538 qr{bool},
539 qr{struct\s+$Ident},
540 qr{union\s+$Ident},
541 qr{enum\s+$Ident},
542 qr{${Ident}_t},
543 qr{${Ident}_handler},
544 qr{${Ident}_handler_fn},
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200545 @typeListMisordered,
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000546);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200547
548our $C90_int_types = qr{(?x:
549 long\s+long\s+int\s+(?:un)?signed|
550 long\s+long\s+(?:un)?signed\s+int|
551 long\s+long\s+(?:un)?signed|
552 (?:(?:un)?signed\s+)?long\s+long\s+int|
553 (?:(?:un)?signed\s+)?long\s+long|
554 int\s+long\s+long\s+(?:un)?signed|
555 int\s+(?:(?:un)?signed\s+)?long\s+long|
556
557 long\s+int\s+(?:un)?signed|
558 long\s+(?:un)?signed\s+int|
559 long\s+(?:un)?signed|
560 (?:(?:un)?signed\s+)?long\s+int|
561 (?:(?:un)?signed\s+)?long|
562 int\s+long\s+(?:un)?signed|
563 int\s+(?:(?:un)?signed\s+)?long|
564
565 int\s+(?:un)?signed|
566 (?:(?:un)?signed\s+)?int
567)};
568
569our @typeListFile = ();
Tom Rinia50f8de2014-02-27 08:27:28 -0500570our @typeListWithAttr = (
571 @typeList,
572 qr{struct\s+$InitAttribute\s+$Ident},
573 qr{union\s+$InitAttribute\s+$Ident},
574);
575
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000576our @modifierList = (
577 qr{fastcall},
578);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200579our @modifierListFile = ();
580
581our @mode_permission_funcs = (
582 ["module_param", 3],
583 ["module_param_(?:array|named|string)", 4],
584 ["module_param_array_named", 5],
585 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
586 ["proc_create(?:_data|)", 2],
587 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
588 ["IIO_DEV_ATTR_[A-Z_]+", 1],
589 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
590 ["SENSOR_TEMPLATE(?:_2|)", 3],
591 ["__ATTR", 2],
592);
593
594#Create a search pattern for all these functions to speed up a loop below
595our $mode_perms_search = "";
596foreach my $entry (@mode_permission_funcs) {
597 $mode_perms_search .= '|' if ($mode_perms_search ne "");
598 $mode_perms_search .= $entry->[0];
599}
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200600$mode_perms_search = "(?:${mode_perms_search})";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200601
Tom Rinid2e30332020-06-16 10:29:46 -0400602our %deprecated_apis = (
603 "synchronize_rcu_bh" => "synchronize_rcu",
604 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
605 "call_rcu_bh" => "call_rcu",
606 "rcu_barrier_bh" => "rcu_barrier",
607 "synchronize_sched" => "synchronize_rcu",
608 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
609 "call_rcu_sched" => "call_rcu",
610 "rcu_barrier_sched" => "rcu_barrier",
611 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
612 "cond_synchronize_sched" => "cond_synchronize_rcu",
613);
614
615#Create a search pattern for all these strings to speed up a loop below
616our $deprecated_apis_search = "";
617foreach my $entry (keys %deprecated_apis) {
618 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
619 $deprecated_apis_search .= $entry;
620}
621$deprecated_apis_search = "(?:${deprecated_apis_search})";
622
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200623our $mode_perms_world_writable = qr{
624 S_IWUGO |
625 S_IWOTH |
626 S_IRWXUGO |
627 S_IALLUGO |
628 0[0-7][0-7][2367]
629}x;
630
631our %mode_permission_string_types = (
632 "S_IRWXU" => 0700,
633 "S_IRUSR" => 0400,
634 "S_IWUSR" => 0200,
635 "S_IXUSR" => 0100,
636 "S_IRWXG" => 0070,
637 "S_IRGRP" => 0040,
638 "S_IWGRP" => 0020,
639 "S_IXGRP" => 0010,
640 "S_IRWXO" => 0007,
641 "S_IROTH" => 0004,
642 "S_IWOTH" => 0002,
643 "S_IXOTH" => 0001,
644 "S_IRWXUGO" => 0777,
645 "S_IRUGO" => 0444,
646 "S_IWUGO" => 0222,
647 "S_IXUGO" => 0111,
648);
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000649
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200650#Create a search pattern for all these strings to speed up a loop below
651our $mode_perms_string_search = "";
652foreach my $entry (keys %mode_permission_string_types) {
653 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
654 $mode_perms_string_search .= $entry;
655}
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200656our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
657our $multi_mode_perms_string_search = qr{
658 ${single_mode_perms_string_search}
659 (?:\s*\|\s*${single_mode_perms_string_search})*
660}x;
661
662sub perms_to_octal {
663 my ($string) = @_;
664
665 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
666
667 my $val = "";
668 my $oval = "";
669 my $to = 0;
670 my $curpos = 0;
671 my $lastpos = 0;
672 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
673 $curpos = pos($string);
674 my $match = $2;
675 my $omatch = $1;
676 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
677 $lastpos = $curpos;
678 $to |= $mode_permission_string_types{$match};
679 $val .= '\s*\|\s*' if ($val ne "");
680 $val .= $match;
681 $oval .= $omatch;
682 }
683 $oval =~ s/^\s*\|\s*//;
684 $oval =~ s/\s*\|\s*$//;
685 return sprintf("%04o", $to);
686}
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200687
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000688our $allowed_asm_includes = qr{(?x:
689 irq|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200690 memory|
691 time|
692 reboot
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000693)};
694# memory.h: ARM has a custom one
695
Dan Murphy463f6882017-01-31 14:15:53 -0600696# Load common spelling mistakes and build regular expression list.
697my $misspellings;
698my %spelling_fix;
699
700if (open(my $spelling, '<', $spelling_file)) {
701 while (<$spelling>) {
702 my $line = $_;
703
704 $line =~ s/\s*\n?$//g;
705 $line =~ s/^\s*//g;
706
707 next if ($line =~ m/^\s*#/);
708 next if ($line =~ m/^\s*$/);
709
710 my ($suspect, $fix) = split(/\|\|/, $line);
711
712 $spelling_fix{$suspect} = $fix;
713 }
714 close($spelling);
715} else {
716 warn "No typos will be found - file '$spelling_file': $!\n";
717}
718
719if ($codespell) {
720 if (open(my $spelling, '<', $codespellfile)) {
721 while (<$spelling>) {
722 my $line = $_;
723
724 $line =~ s/\s*\n?$//g;
725 $line =~ s/^\s*//g;
726
727 next if ($line =~ m/^\s*#/);
728 next if ($line =~ m/^\s*$/);
729 next if ($line =~ m/, disabled/i);
730
731 $line =~ s/,.*$//;
732
733 my ($suspect, $fix) = split(/->/, $line);
734
735 $spelling_fix{$suspect} = $fix;
736 }
737 close($spelling);
738 } else {
739 warn "No codespell typos will be found - file '$codespellfile': $!\n";
740 }
741}
742
743$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
744
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200745sub read_words {
746 my ($wordsRef, $file) = @_;
747
748 if (open(my $words, '<', $file)) {
749 while (<$words>) {
750 my $line = $_;
751
752 $line =~ s/\s*\n?$//g;
753 $line =~ s/^\s*//g;
754
755 next if ($line =~ m/^\s*#/);
756 next if ($line =~ m/^\s*$/);
757 if ($line =~ /\s/) {
758 print("$file: '$line' invalid - ignored\n");
759 next;
760 }
761
762 $$wordsRef .= '|' if ($$wordsRef ne "");
763 $$wordsRef .= $line;
764 }
765 close($file);
766 return 1;
767 }
768
769 return 0;
770}
771
772my $const_structs = "";
773read_words(\$const_structs, $conststructsfile)
774 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
775
776my $typeOtherTypedefs = "";
777if (length($typedefsfile)) {
778 read_words(\$typeOtherTypedefs, $typedefsfile)
779 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
780}
781$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
Dan Murphy463f6882017-01-31 14:15:53 -0600782
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000783sub build_types {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200784 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
785 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
786 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Tom Rinia50f8de2014-02-27 08:27:28 -0500787 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000788 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200789 $BasicType = qr{
790 (?:$typeTypedefs\b)|
791 (?:${all}\b)
792 }x;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000793 $NonptrType = qr{
794 (?:$Modifier\s+|const\s+)*
795 (?:
Kim Phillips9b7b8372013-02-28 12:53:52 +0000796 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000797 (?:$typeTypedefs\b)|
798 (?:${all}\b)
799 )
800 (?:\s+$Modifier|\s+const)*
801 }x;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200802 $NonptrTypeMisordered = qr{
803 (?:$Modifier\s+|const\s+)*
804 (?:
805 (?:${Misordered}\b)
806 )
807 (?:\s+$Modifier|\s+const)*
808 }x;
Tom Rinia50f8de2014-02-27 08:27:28 -0500809 $NonptrTypeWithAttr = qr{
810 (?:$Modifier\s+|const\s+)*
811 (?:
812 (?:typeof|__typeof__)\s*\([^\)]*\)|
813 (?:$typeTypedefs\b)|
814 (?:${allWithAttr}\b)
815 )
816 (?:\s+$Modifier|\s+const)*
817 }x;
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000818 $Type = qr{
819 $NonptrType
Tom Rinid2e30332020-06-16 10:29:46 -0400820 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000821 (?:\s+$Inline|\s+$Modifier)*
822 }x;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200823 $TypeMisordered = qr{
824 $NonptrTypeMisordered
Tom Rinid2e30332020-06-16 10:29:46 -0400825 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200826 (?:\s+$Inline|\s+$Modifier)*
827 }x;
828 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
829 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000830}
831build_types();
832
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000833our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Kim Phillips9b7b8372013-02-28 12:53:52 +0000834
835# Using $balanced_parens, $LvalOrFunc, or $FuncArg
836# requires at least perl version v5.10.0
837# Any use must be runtime checked with $^V
838
839our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200840our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
841our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
842
843our $declaration_macros = qr{(?x:
844 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
845 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +0200846 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
847 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200848)};
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000849
850sub deparenthesize {
851 my ($string) = @_;
852 return "" if (!defined($string));
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200853
854 while ($string =~ /^\s*\(.*\)\s*$/) {
855 $string =~ s@^\s*\(\s*@@;
856 $string =~ s@\s*\)\s*$@@;
857 }
858
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000859 $string =~ s@\s+@ @g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200860
Joe Hershbergerba2beb52011-10-18 10:06:59 +0000861 return $string;
862}
863
Tom Rinia50f8de2014-02-27 08:27:28 -0500864sub seed_camelcase_file {
865 my ($file) = @_;
866
867 return if (!(-f $file));
868
869 local $/;
870
871 open(my $include_file, '<', "$file")
872 or warn "$P: Can't read '$file' $!\n";
873 my $text = <$include_file>;
874 close($include_file);
875
876 my @lines = split('\n', $text);
877
878 foreach my $line (@lines) {
879 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
880 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
881 $camelcase{$1} = 1;
882 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
883 $camelcase{$1} = 1;
884 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
885 $camelcase{$1} = 1;
886 }
887 }
888}
889
Tom Rinid2e30332020-06-16 10:29:46 -0400890our %maintained_status = ();
891
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200892sub is_maintained_obsolete {
893 my ($filename) = @_;
894
895 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
896
Tom Rinid2e30332020-06-16 10:29:46 -0400897 if (!exists($maintained_status{$filename})) {
898 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
899 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200900
Tom Rinid2e30332020-06-16 10:29:46 -0400901 return $maintained_status{$filename} =~ /obsolete/i;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200902}
903
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +0200904sub is_SPDX_License_valid {
905 my ($license) = @_;
906
907 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
908
909 my $root_path = abs_path($root);
910 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
911 return 0 if ($status ne "");
912 return 1;
913}
914
Tom Rinia50f8de2014-02-27 08:27:28 -0500915my $camelcase_seeded = 0;
916sub seed_camelcase_includes {
917 return if ($camelcase_seeded);
918
919 my $files;
920 my $camelcase_cache = "";
921 my @include_files = ();
922
923 $camelcase_seeded = 1;
924
925 if (-e ".git") {
Tom Rinid2e30332020-06-16 10:29:46 -0400926 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Tom Rinia50f8de2014-02-27 08:27:28 -0500927 chomp $git_last_include_commit;
928 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
929 } else {
930 my $last_mod_date = 0;
931 $files = `find $root/include -name "*.h"`;
932 @include_files = split('\n', $files);
933 foreach my $file (@include_files) {
934 my $date = POSIX::strftime("%Y%m%d%H%M",
935 localtime((stat $file)[9]));
936 $last_mod_date = $date if ($last_mod_date < $date);
937 }
938 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
939 }
940
941 if ($camelcase_cache ne "" && -f $camelcase_cache) {
942 open(my $camelcase_file, '<', "$camelcase_cache")
943 or warn "$P: Can't read '$camelcase_cache' $!\n";
944 while (<$camelcase_file>) {
945 chomp;
946 $camelcase{$_} = 1;
947 }
948 close($camelcase_file);
949
950 return;
951 }
952
953 if (-e ".git") {
Tom Rinid2e30332020-06-16 10:29:46 -0400954 $files = `${git_command} ls-files "include/*.h"`;
Tom Rinia50f8de2014-02-27 08:27:28 -0500955 @include_files = split('\n', $files);
956 }
957
958 foreach my $file (@include_files) {
959 seed_camelcase_file($file);
960 }
961
962 if ($camelcase_cache ne "") {
963 unlink glob ".checkpatch-camelcase.*";
964 open(my $camelcase_file, '>', "$camelcase_cache")
965 or warn "$P: Can't write '$camelcase_cache' $!\n";
966 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
967 print $camelcase_file ("$_\n");
968 }
969 close($camelcase_file);
970 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200971}
972
973sub git_commit_info {
974 my ($commit, $id, $desc) = @_;
975
976 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
977
Tom Rinid2e30332020-06-16 10:29:46 -0400978 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200979 $output =~ s/^\s*//gm;
980 my @lines = split("\n", $output);
981
982 return ($id, $desc) if ($#lines < 0);
983
Tom Rinid2e30332020-06-16 10:29:46 -0400984 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +0200985# Maybe one day convert this block of bash into something that returns
986# all matching commit ids, but it's very slow...
987#
988# echo "checking commits $1..."
989# git rev-list --remotes | grep -i "^$1" |
990# while read line ; do
991# git log --format='%H %s' -1 $line |
992# echo "commit $(cut -c 1-12,41-)"
993# done
994 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
995 $id = undef;
996 } else {
997 $id = substr($lines[0], 0, 12);
998 $desc = substr($lines[0], 41);
999 }
1000
1001 return ($id, $desc);
Tom Rinia50f8de2014-02-27 08:27:28 -05001002}
1003
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001004$chk_signoff = 0 if ($file);
1005
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001006my @rawlines = ();
1007my @lines = ();
Tom Rinia50f8de2014-02-27 08:27:28 -05001008my @fixed = ();
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001009my @fixed_inserted = ();
1010my @fixed_deleted = ();
Dan Murphy463f6882017-01-31 14:15:53 -06001011my $fixlinenr = -1;
1012
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001013# If input is git commits, extract all commits from the commit expressions.
1014# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1015die "$P: No git repository found\n" if ($git && !-e ".git");
1016
1017if ($git) {
1018 my @commits = ();
1019 foreach my $commit_expr (@ARGV) {
1020 my $git_range;
1021 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1022 $git_range = "-$2 $1";
1023 } elsif ($commit_expr =~ m/\.\./) {
1024 $git_range = "$commit_expr";
1025 } else {
1026 $git_range = "-1 $commit_expr";
1027 }
Tom Rinid2e30332020-06-16 10:29:46 -04001028 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001029 foreach my $line (split(/\n/, $lines)) {
1030 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1031 next if (!defined($1) || !defined($2));
1032 my $sha1 = $1;
1033 my $subject = $2;
1034 unshift(@commits, $sha1);
1035 $git_commits{$sha1} = $subject;
1036 }
1037 }
1038 die "$P: no git commits after extraction!\n" if (@commits == 0);
1039 @ARGV = @commits;
1040}
1041
1042my $vname;
Tom Rinid2e30332020-06-16 10:29:46 -04001043$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001044for my $filename (@ARGV) {
1045 my $FILE;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001046 if ($git) {
1047 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1048 die "$P: $filename: git format-patch failed - $!\n";
1049 } elsif ($file) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001050 open($FILE, '-|', "diff -u /dev/null $filename") ||
1051 die "$P: $filename: diff failed - $!\n";
1052 } elsif ($filename eq '-') {
1053 open($FILE, '<&STDIN');
1054 } else {
1055 open($FILE, '<', "$filename") ||
1056 die "$P: $filename: open failed - $!\n";
1057 }
1058 if ($filename eq '-') {
1059 $vname = 'Your patch';
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001060 } elsif ($git) {
1061 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001062 } else {
1063 $vname = $filename;
1064 }
1065 while (<$FILE>) {
1066 chomp;
1067 push(@rawlines, $_);
1068 }
1069 close($FILE);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001070
1071 if ($#ARGV > 0 && $quiet == 0) {
1072 print '-' x length($vname) . "\n";
1073 print "$vname\n";
1074 print '-' x length($vname) . "\n";
1075 }
1076
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001077 if (!process($filename)) {
1078 $exit = 1;
1079 }
1080 @rawlines = ();
1081 @lines = ();
Tom Rinia50f8de2014-02-27 08:27:28 -05001082 @fixed = ();
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001083 @fixed_inserted = ();
1084 @fixed_deleted = ();
1085 $fixlinenr = -1;
1086 @modifierListFile = ();
1087 @typeListFile = ();
1088 build_types();
1089}
1090
1091if (!$quiet) {
1092 hash_show_words(\%use_type, "Used");
1093 hash_show_words(\%ignore_type, "Ignored");
1094
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02001095 if (!$perl_version_ok) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001096 print << "EOM"
1097
1098NOTE: perl $^V is not modern enough to detect all possible issues.
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02001099 An upgrade to at least perl $minimum_perl_version is suggested.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001100EOM
1101 }
1102 if ($exit) {
1103 print << "EOM"
1104
1105NOTE: If any of the errors are false positives, please report
1106 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1107EOM
1108 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001109}
1110
1111exit($exit);
1112
1113sub top_of_kernel_tree {
1114 my ($root) = @_;
1115
1116 my @tree_check = (
Tom Rinia50f8de2014-02-27 08:27:28 -05001117 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001118 "README", "Documentation", "arch", "include", "drivers",
1119 "fs", "init", "ipc", "kernel", "lib", "scripts",
1120 );
1121
1122 foreach my $check (@tree_check) {
1123 if (! -e $root . '/' . $check) {
1124 return 0;
1125 }
1126 }
1127 return 1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00001128}
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001129
1130sub parse_email {
1131 my ($formatted_email) = @_;
1132
1133 my $name = "";
Tom Rinid2e30332020-06-16 10:29:46 -04001134 my $name_comment = "";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001135 my $address = "";
1136 my $comment = "";
1137
1138 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1139 $name = $1;
1140 $address = $2;
1141 $comment = $3 if defined $3;
1142 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1143 $address = $1;
1144 $comment = $2 if defined $2;
1145 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1146 $address = $1;
1147 $comment = $2 if defined $2;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02001148 $formatted_email =~ s/\Q$address\E.*$//;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001149 $name = $formatted_email;
Tom Rinia50f8de2014-02-27 08:27:28 -05001150 $name = trim($name);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001151 $name =~ s/^\"|\"$//g;
1152 # If there's a name left after stripping spaces and
1153 # leading quotes, and the address doesn't have both
1154 # leading and trailing angle brackets, the address
1155 # is invalid. ie:
1156 # "joe smith joe@smith.com" bad
1157 # "joe smith <joe@smith.com" bad
1158 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1159 $name = "";
1160 $address = "";
1161 $comment = "";
1162 }
1163 }
1164
Tom Rinia50f8de2014-02-27 08:27:28 -05001165 $name = trim($name);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001166 $name =~ s/^\"|\"$//g;
Tom Rinid2e30332020-06-16 10:29:46 -04001167 $name =~ s/(\s*\([^\)]+\))\s*//;
1168 if (defined($1)) {
1169 $name_comment = trim($1);
1170 }
Tom Rinia50f8de2014-02-27 08:27:28 -05001171 $address = trim($address);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001172 $address =~ s/^\<|\>$//g;
1173
1174 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1175 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1176 $name = "\"$name\"";
1177 }
1178
Tom Rinid2e30332020-06-16 10:29:46 -04001179 return ($name, $name_comment, $address, $comment);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001180}
1181
1182sub format_email {
1183 my ($name, $address) = @_;
1184
1185 my $formatted_email;
1186
Tom Rinia50f8de2014-02-27 08:27:28 -05001187 $name = trim($name);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001188 $name =~ s/^\"|\"$//g;
Tom Rinia50f8de2014-02-27 08:27:28 -05001189 $address = trim($address);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001190
1191 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1192 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1193 $name = "\"$name\"";
1194 }
1195
1196 if ("$name" eq "") {
1197 $formatted_email = "$address";
1198 } else {
1199 $formatted_email = "$name <$address>";
1200 }
1201
1202 return $formatted_email;
1203}
1204
Tom Rinid2e30332020-06-16 10:29:46 -04001205sub reformat_email {
1206 my ($email) = @_;
1207
1208 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1209 return format_email($email_name, $email_address);
1210}
1211
1212sub same_email_addresses {
1213 my ($email1, $email2) = @_;
1214
1215 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1216 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1217
1218 return $email1_name eq $email2_name &&
1219 $email1_address eq $email2_address;
1220}
1221
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001222sub which {
1223 my ($bin) = @_;
1224
1225 foreach my $path (split(/:/, $ENV{PATH})) {
1226 if (-e "$path/$bin") {
1227 return "$path/$bin";
1228 }
1229 }
1230
1231 return "";
1232}
1233
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001234sub which_conf {
1235 my ($conf) = @_;
1236
1237 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1238 if (-e "$path/$conf") {
1239 return "$path/$conf";
1240 }
1241 }
1242
1243 return "";
1244}
1245
1246sub expand_tabs {
1247 my ($str) = @_;
1248
1249 my $res = '';
1250 my $n = 0;
1251 for my $c (split(//, $str)) {
1252 if ($c eq "\t") {
1253 $res .= ' ';
1254 $n++;
Tom Rinid2e30332020-06-16 10:29:46 -04001255 for (; ($n % $tabsize) != 0; $n++) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001256 $res .= ' ';
1257 }
1258 next;
1259 }
1260 $res .= $c;
1261 $n++;
1262 }
1263
1264 return $res;
1265}
1266sub copy_spacing {
1267 (my $res = shift) =~ tr/\t/ /c;
1268 return $res;
1269}
1270
1271sub line_stats {
1272 my ($line) = @_;
1273
1274 # Drop the diff line leader and expand tabs
1275 $line =~ s/^.//;
1276 $line = expand_tabs($line);
1277
1278 # Pick the indent from the front of the line.
1279 my ($white) = ($line =~ /^(\s*)/);
1280
1281 return (length($line), length($white));
1282}
1283
1284my $sanitise_quote = '';
1285
1286sub sanitise_line_reset {
1287 my ($in_comment) = @_;
1288
1289 if ($in_comment) {
1290 $sanitise_quote = '*/';
1291 } else {
1292 $sanitise_quote = '';
1293 }
1294}
1295sub sanitise_line {
1296 my ($line) = @_;
1297
1298 my $res = '';
1299 my $l = '';
1300
1301 my $qlen = 0;
1302 my $off = 0;
1303 my $c;
1304
1305 # Always copy over the diff marker.
1306 $res = substr($line, 0, 1);
1307
1308 for ($off = 1; $off < length($line); $off++) {
1309 $c = substr($line, $off, 1);
1310
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02001311 # Comments we are whacking completely including the begin
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001312 # and end, all to $;.
1313 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1314 $sanitise_quote = '*/';
1315
1316 substr($res, $off, 2, "$;$;");
1317 $off++;
1318 next;
1319 }
1320 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1321 $sanitise_quote = '';
1322 substr($res, $off, 2, "$;$;");
1323 $off++;
1324 next;
1325 }
1326 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1327 $sanitise_quote = '//';
1328
1329 substr($res, $off, 2, $sanitise_quote);
1330 $off++;
1331 next;
1332 }
1333
1334 # A \ in a string means ignore the next character.
1335 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1336 $c eq "\\") {
1337 substr($res, $off, 2, 'XX');
1338 $off++;
1339 next;
1340 }
1341 # Regular quotes.
1342 if ($c eq "'" || $c eq '"') {
1343 if ($sanitise_quote eq '') {
1344 $sanitise_quote = $c;
1345
1346 substr($res, $off, 1, $c);
1347 next;
1348 } elsif ($sanitise_quote eq $c) {
1349 $sanitise_quote = '';
1350 }
1351 }
1352
1353 #print "c<$c> SQ<$sanitise_quote>\n";
1354 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1355 substr($res, $off, 1, $;);
1356 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1357 substr($res, $off, 1, $;);
1358 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1359 substr($res, $off, 1, 'X');
1360 } else {
1361 substr($res, $off, 1, $c);
1362 }
1363 }
1364
1365 if ($sanitise_quote eq '//') {
1366 $sanitise_quote = '';
1367 }
1368
1369 # The pathname on a #include may be surrounded by '<' and '>'.
1370 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1371 my $clean = 'X' x length($1);
1372 $res =~ s@\<.*\>@<$clean>@;
1373
1374 # The whole of a #error is a string.
1375 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1376 my $clean = 'X' x length($1);
1377 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1378 }
1379
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001380 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1381 my $match = $1;
1382 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1383 }
1384
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001385 return $res;
1386}
1387
Tom Rinia50f8de2014-02-27 08:27:28 -05001388sub get_quoted_string {
1389 my ($line, $rawline) = @_;
1390
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02001391 return "" if (!defined($line) || !defined($rawline));
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02001392 return "" if ($line !~ m/($String)/g);
Tom Rinia50f8de2014-02-27 08:27:28 -05001393 return substr($rawline, $-[0], $+[0] - $-[0]);
1394}
1395
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001396sub ctx_statement_block {
1397 my ($linenr, $remain, $off) = @_;
1398 my $line = $linenr - 1;
1399 my $blk = '';
1400 my $soff = $off;
1401 my $coff = $off - 1;
1402 my $coff_set = 0;
1403
1404 my $loff = 0;
1405
1406 my $type = '';
1407 my $level = 0;
1408 my @stack = ();
1409 my $p;
1410 my $c;
1411 my $len = 0;
1412
1413 my $remainder;
1414 while (1) {
1415 @stack = (['', 0]) if ($#stack == -1);
1416
1417 #warn "CSB: blk<$blk> remain<$remain>\n";
1418 # If we are about to drop off the end, pull in more
1419 # context.
1420 if ($off >= $len) {
1421 for (; $remain > 0; $line++) {
1422 last if (!defined $lines[$line]);
1423 next if ($lines[$line] =~ /^-/);
1424 $remain--;
1425 $loff = $len;
1426 $blk .= $lines[$line] . "\n";
1427 $len = length($blk);
1428 $line++;
1429 last;
1430 }
1431 # Bail if there is no further context.
1432 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1433 if ($off >= $len) {
1434 last;
1435 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00001436 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1437 $level++;
1438 $type = '#';
1439 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001440 }
1441 $p = $c;
1442 $c = substr($blk, $off, 1);
1443 $remainder = substr($blk, $off);
1444
1445 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1446
1447 # Handle nested #if/#else.
1448 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1449 push(@stack, [ $type, $level ]);
1450 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1451 ($type, $level) = @{$stack[$#stack - 1]};
1452 } elsif ($remainder =~ /^#\s*endif\b/) {
1453 ($type, $level) = @{pop(@stack)};
1454 }
1455
1456 # Statement ends at the ';' or a close '}' at the
1457 # outermost level.
1458 if ($level == 0 && $c eq ';') {
1459 last;
1460 }
1461
1462 # An else is really a conditional as long as its not else if
1463 if ($level == 0 && $coff_set == 0 &&
1464 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1465 $remainder =~ /^(else)(?:\s|{)/ &&
1466 $remainder !~ /^else\s+if\b/) {
1467 $coff = $off + length($1) - 1;
1468 $coff_set = 1;
1469 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1470 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1471 }
1472
1473 if (($type eq '' || $type eq '(') && $c eq '(') {
1474 $level++;
1475 $type = '(';
1476 }
1477 if ($type eq '(' && $c eq ')') {
1478 $level--;
1479 $type = ($level != 0)? '(' : '';
1480
1481 if ($level == 0 && $coff < $soff) {
1482 $coff = $off;
1483 $coff_set = 1;
1484 #warn "CSB: mark coff<$coff>\n";
1485 }
1486 }
1487 if (($type eq '' || $type eq '{') && $c eq '{') {
1488 $level++;
1489 $type = '{';
1490 }
1491 if ($type eq '{' && $c eq '}') {
1492 $level--;
1493 $type = ($level != 0)? '{' : '';
1494
1495 if ($level == 0) {
1496 if (substr($blk, $off + 1, 1) eq ';') {
1497 $off++;
1498 }
1499 last;
1500 }
1501 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00001502 # Preprocessor commands end at the newline unless escaped.
1503 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1504 $level--;
1505 $type = '';
1506 $off++;
1507 last;
1508 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001509 $off++;
1510 }
1511 # We are truly at the end, so shuffle to the next line.
1512 if ($off == $len) {
1513 $loff = $len + 1;
1514 $line++;
1515 $remain--;
1516 }
1517
1518 my $statement = substr($blk, $soff, $off - $soff + 1);
1519 my $condition = substr($blk, $soff, $coff - $soff + 1);
1520
1521 #warn "STATEMENT<$statement>\n";
1522 #warn "CONDITION<$condition>\n";
1523
1524 #print "coff<$coff> soff<$off> loff<$loff>\n";
1525
1526 return ($statement, $condition,
1527 $line, $remain + 1, $off - $loff + 1, $level);
1528}
1529
1530sub statement_lines {
1531 my ($stmt) = @_;
1532
1533 # Strip the diff line prefixes and rip blank lines at start and end.
1534 $stmt =~ s/(^|\n)./$1/g;
1535 $stmt =~ s/^\s*//;
1536 $stmt =~ s/\s*$//;
1537
1538 my @stmt_lines = ($stmt =~ /\n/g);
1539
1540 return $#stmt_lines + 2;
1541}
1542
1543sub statement_rawlines {
1544 my ($stmt) = @_;
1545
1546 my @stmt_lines = ($stmt =~ /\n/g);
1547
1548 return $#stmt_lines + 2;
1549}
1550
1551sub statement_block_size {
1552 my ($stmt) = @_;
1553
1554 $stmt =~ s/(^|\n)./$1/g;
1555 $stmt =~ s/^\s*{//;
1556 $stmt =~ s/}\s*$//;
1557 $stmt =~ s/^\s*//;
1558 $stmt =~ s/\s*$//;
1559
1560 my @stmt_lines = ($stmt =~ /\n/g);
1561 my @stmt_statements = ($stmt =~ /;/g);
1562
1563 my $stmt_lines = $#stmt_lines + 2;
1564 my $stmt_statements = $#stmt_statements + 1;
1565
1566 if ($stmt_lines > $stmt_statements) {
1567 return $stmt_lines;
1568 } else {
1569 return $stmt_statements;
1570 }
1571}
1572
1573sub ctx_statement_full {
1574 my ($linenr, $remain, $off) = @_;
1575 my ($statement, $condition, $level);
1576
1577 my (@chunks);
1578
1579 # Grab the first conditional/block pair.
1580 ($statement, $condition, $linenr, $remain, $off, $level) =
1581 ctx_statement_block($linenr, $remain, $off);
1582 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1583 push(@chunks, [ $condition, $statement ]);
1584 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1585 return ($level, $linenr, @chunks);
1586 }
1587
1588 # Pull in the following conditional/block pairs and see if they
1589 # could continue the statement.
1590 for (;;) {
1591 ($statement, $condition, $linenr, $remain, $off, $level) =
1592 ctx_statement_block($linenr, $remain, $off);
1593 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1594 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1595 #print "C: push\n";
1596 push(@chunks, [ $condition, $statement ]);
1597 }
1598
1599 return ($level, $linenr, @chunks);
1600}
1601
1602sub ctx_block_get {
1603 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1604 my $line;
1605 my $start = $linenr - 1;
1606 my $blk = '';
1607 my @o;
1608 my @c;
1609 my @res = ();
1610
1611 my $level = 0;
1612 my @stack = ($level);
1613 for ($line = $start; $remain > 0; $line++) {
1614 next if ($rawlines[$line] =~ /^-/);
1615 $remain--;
1616
1617 $blk .= $rawlines[$line];
1618
1619 # Handle nested #if/#else.
1620 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1621 push(@stack, $level);
1622 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1623 $level = $stack[$#stack - 1];
1624 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1625 $level = pop(@stack);
1626 }
1627
1628 foreach my $c (split(//, $lines[$line])) {
1629 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1630 if ($off > 0) {
1631 $off--;
1632 next;
1633 }
1634
1635 if ($c eq $close && $level > 0) {
1636 $level--;
1637 last if ($level == 0);
1638 } elsif ($c eq $open) {
1639 $level++;
1640 }
1641 }
1642
1643 if (!$outer || $level <= 1) {
1644 push(@res, $rawlines[$line]);
1645 }
1646
1647 last if ($level == 0);
1648 }
1649
1650 return ($level, @res);
1651}
1652sub ctx_block_outer {
1653 my ($linenr, $remain) = @_;
1654
1655 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1656 return @r;
1657}
1658sub ctx_block {
1659 my ($linenr, $remain) = @_;
1660
1661 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1662 return @r;
1663}
1664sub ctx_statement {
1665 my ($linenr, $remain, $off) = @_;
1666
1667 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1668 return @r;
1669}
1670sub ctx_block_level {
1671 my ($linenr, $remain) = @_;
1672
1673 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1674}
1675sub ctx_statement_level {
1676 my ($linenr, $remain, $off) = @_;
1677
1678 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1679}
1680
1681sub ctx_locate_comment {
1682 my ($first_line, $end_line) = @_;
1683
1684 # Catch a comment on the end of the line itself.
1685 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1686 return $current_comment if (defined $current_comment);
1687
1688 # Look through the context and try and figure out if there is a
1689 # comment.
1690 my $in_comment = 0;
1691 $current_comment = '';
1692 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1693 my $line = $rawlines[$linenr - 1];
1694 #warn " $line\n";
1695 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1696 $in_comment = 1;
1697 }
1698 if ($line =~ m@/\*@) {
1699 $in_comment = 1;
1700 }
1701 if (!$in_comment && $current_comment ne '') {
1702 $current_comment = '';
1703 }
1704 $current_comment .= $line . "\n" if ($in_comment);
1705 if ($line =~ m@\*/@) {
1706 $in_comment = 0;
1707 }
1708 }
1709
1710 chomp($current_comment);
1711 return($current_comment);
1712}
1713sub ctx_has_comment {
1714 my ($first_line, $end_line) = @_;
1715 my $cmt = ctx_locate_comment($first_line, $end_line);
1716
1717 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1718 ##print "CMMT: $cmt\n";
1719
1720 return ($cmt ne '');
1721}
1722
1723sub raw_line {
1724 my ($linenr, $cnt) = @_;
1725
1726 my $offset = $linenr - 1;
1727 $cnt++;
1728
1729 my $line;
1730 while ($cnt) {
1731 $line = $rawlines[$offset++];
1732 next if (defined($line) && $line =~ /^-/);
1733 $cnt--;
1734 }
1735
1736 return $line;
1737}
1738
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02001739sub get_stat_real {
1740 my ($linenr, $lc) = @_;
1741
1742 my $stat_real = raw_line($linenr, 0);
1743 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1744 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1745 }
1746
1747 return $stat_real;
1748}
1749
1750sub get_stat_here {
1751 my ($linenr, $cnt, $here) = @_;
1752
1753 my $herectx = $here . "\n";
1754 for (my $n = 0; $n < $cnt; $n++) {
1755 $herectx .= raw_line($linenr, $n) . "\n";
1756 }
1757
1758 return $herectx;
1759}
1760
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001761sub cat_vet {
1762 my ($vet) = @_;
1763 my ($res, $coded);
1764
1765 $res = '';
1766 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1767 $res .= $1;
1768 if ($2 ne '') {
1769 $coded = sprintf("^%c", unpack('C', $2) + 64);
1770 $res .= $coded;
1771 }
1772 }
1773 $res =~ s/$/\$/;
1774
1775 return $res;
1776}
1777
1778my $av_preprocessor = 0;
1779my $av_pending;
1780my @av_paren_type;
1781my $av_pend_colon;
1782
1783sub annotate_reset {
1784 $av_preprocessor = 0;
1785 $av_pending = '_';
1786 @av_paren_type = ('E');
1787 $av_pend_colon = 'O';
1788}
1789
1790sub annotate_values {
1791 my ($stream, $type) = @_;
1792
1793 my $res;
1794 my $var = '_' x length($stream);
1795 my $cur = $stream;
1796
1797 print "$stream\n" if ($dbg_values > 1);
1798
1799 while (length($cur)) {
1800 @av_paren_type = ('E') if ($#av_paren_type < 0);
1801 print " <" . join('', @av_paren_type) .
1802 "> <$type> <$av_pending>" if ($dbg_values > 1);
1803 if ($cur =~ /^(\s+)/o) {
1804 print "WS($1)\n" if ($dbg_values > 1);
1805 if ($1 =~ /\n/ && $av_preprocessor) {
1806 $type = pop(@av_paren_type);
1807 $av_preprocessor = 0;
1808 }
1809
1810 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1811 print "CAST($1)\n" if ($dbg_values > 1);
1812 push(@av_paren_type, $type);
Kim Phillips9b7b8372013-02-28 12:53:52 +00001813 $type = 'c';
Joe Hershbergerba2beb52011-10-18 10:06:59 +00001814
1815 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1816 print "DECLARE($1)\n" if ($dbg_values > 1);
1817 $type = 'T';
1818
1819 } elsif ($cur =~ /^($Modifier)\s*/) {
1820 print "MODIFIER($1)\n" if ($dbg_values > 1);
1821 $type = 'T';
1822
1823 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1824 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1825 $av_preprocessor = 1;
1826 push(@av_paren_type, $type);
1827 if ($2 ne '') {
1828 $av_pending = 'N';
1829 }
1830 $type = 'E';
1831
1832 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1833 print "UNDEF($1)\n" if ($dbg_values > 1);
1834 $av_preprocessor = 1;
1835 push(@av_paren_type, $type);
1836
1837 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1838 print "PRE_START($1)\n" if ($dbg_values > 1);
1839 $av_preprocessor = 1;
1840
1841 push(@av_paren_type, $type);
1842 push(@av_paren_type, $type);
1843 $type = 'E';
1844
1845 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1846 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1847 $av_preprocessor = 1;
1848
1849 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1850
1851 $type = 'E';
1852
1853 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1854 print "PRE_END($1)\n" if ($dbg_values > 1);
1855
1856 $av_preprocessor = 1;
1857
1858 # Assume all arms of the conditional end as this
1859 # one does, and continue as if the #endif was not here.
1860 pop(@av_paren_type);
1861 push(@av_paren_type, $type);
1862 $type = 'E';
1863
1864 } elsif ($cur =~ /^(\\\n)/o) {
1865 print "PRECONT($1)\n" if ($dbg_values > 1);
1866
1867 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1868 print "ATTR($1)\n" if ($dbg_values > 1);
1869 $av_pending = $type;
1870 $type = 'N';
1871
1872 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1873 print "SIZEOF($1)\n" if ($dbg_values > 1);
1874 if (defined $2) {
1875 $av_pending = 'V';
1876 }
1877 $type = 'N';
1878
1879 } elsif ($cur =~ /^(if|while|for)\b/o) {
1880 print "COND($1)\n" if ($dbg_values > 1);
1881 $av_pending = 'E';
1882 $type = 'N';
1883
1884 } elsif ($cur =~/^(case)/o) {
1885 print "CASE($1)\n" if ($dbg_values > 1);
1886 $av_pend_colon = 'C';
1887 $type = 'N';
1888
1889 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1890 print "KEYWORD($1)\n" if ($dbg_values > 1);
1891 $type = 'N';
1892
1893 } elsif ($cur =~ /^(\()/o) {
1894 print "PAREN('$1')\n" if ($dbg_values > 1);
1895 push(@av_paren_type, $av_pending);
1896 $av_pending = '_';
1897 $type = 'N';
1898
1899 } elsif ($cur =~ /^(\))/o) {
1900 my $new_type = pop(@av_paren_type);
1901 if ($new_type ne '_') {
1902 $type = $new_type;
1903 print "PAREN('$1') -> $type\n"
1904 if ($dbg_values > 1);
1905 } else {
1906 print "PAREN('$1')\n" if ($dbg_values > 1);
1907 }
1908
1909 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1910 print "FUNC($1)\n" if ($dbg_values > 1);
1911 $type = 'V';
1912 $av_pending = 'V';
1913
1914 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1915 if (defined $2 && $type eq 'C' || $type eq 'T') {
1916 $av_pend_colon = 'B';
1917 } elsif ($type eq 'E') {
1918 $av_pend_colon = 'L';
1919 }
1920 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1921 $type = 'V';
1922
1923 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1924 print "IDENT($1)\n" if ($dbg_values > 1);
1925 $type = 'V';
1926
1927 } elsif ($cur =~ /^($Assignment)/o) {
1928 print "ASSIGN($1)\n" if ($dbg_values > 1);
1929 $type = 'N';
1930
1931 } elsif ($cur =~/^(;|{|})/) {
1932 print "END($1)\n" if ($dbg_values > 1);
1933 $type = 'E';
1934 $av_pend_colon = 'O';
1935
1936 } elsif ($cur =~/^(,)/) {
1937 print "COMMA($1)\n" if ($dbg_values > 1);
1938 $type = 'C';
1939
1940 } elsif ($cur =~ /^(\?)/o) {
1941 print "QUESTION($1)\n" if ($dbg_values > 1);
1942 $type = 'N';
1943
1944 } elsif ($cur =~ /^(:)/o) {
1945 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1946
1947 substr($var, length($res), 1, $av_pend_colon);
1948 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1949 $type = 'E';
1950 } else {
1951 $type = 'N';
1952 }
1953 $av_pend_colon = 'O';
1954
1955 } elsif ($cur =~ /^(\[)/o) {
1956 print "CLOSE($1)\n" if ($dbg_values > 1);
1957 $type = 'N';
1958
1959 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1960 my $variant;
1961
1962 print "OPV($1)\n" if ($dbg_values > 1);
1963 if ($type eq 'V') {
1964 $variant = 'B';
1965 } else {
1966 $variant = 'U';
1967 }
1968
1969 substr($var, length($res), 1, $variant);
1970 $type = 'N';
1971
1972 } elsif ($cur =~ /^($Operators)/o) {
1973 print "OP($1)\n" if ($dbg_values > 1);
1974 if ($1 ne '++' && $1 ne '--') {
1975 $type = 'N';
1976 }
1977
1978 } elsif ($cur =~ /(^.)/o) {
1979 print "C($1)\n" if ($dbg_values > 1);
1980 }
1981 if (defined $1) {
1982 $cur = substr($cur, length($1));
1983 $res .= $type x length($1);
1984 }
1985 }
1986
1987 return ($res, $var);
1988}
1989
1990sub possible {
1991 my ($possible, $line) = @_;
1992 my $notPermitted = qr{(?:
1993 ^(?:
1994 $Modifier|
1995 $Storage|
1996 $Type|
1997 DEFINE_\S+
1998 )$|
1999 ^(?:
2000 goto|
2001 return|
2002 case|
2003 else|
2004 asm|__asm__|
Kim Phillips9b7b8372013-02-28 12:53:52 +00002005 do|
2006 \#|
2007 \#\#|
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002008 )(?:\s|$)|
2009 ^(?:typedef|struct|enum)\b
2010 )}x;
2011 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2012 if ($possible !~ $notPermitted) {
2013 # Check for modifiers.
2014 $possible =~ s/\s*$Storage\s*//g;
2015 $possible =~ s/\s*$Sparse\s*//g;
2016 if ($possible =~ /^\s*$/) {
2017
2018 } elsif ($possible =~ /\s/) {
2019 $possible =~ s/\s*$Type\s*//g;
2020 for my $modifier (split(' ', $possible)) {
2021 if ($modifier !~ $notPermitted) {
2022 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002023 push(@modifierListFile, $modifier);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002024 }
2025 }
2026
2027 } else {
2028 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002029 push(@typeListFile, $possible);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002030 }
2031 build_types();
2032 } else {
2033 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
2034 }
2035}
2036
2037my $prefix = '';
2038
2039sub show_type {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002040 my ($type) = @_;
2041
2042 $type =~ tr/[a-z]/[A-Z]/;
Tom Rinia50f8de2014-02-27 08:27:28 -05002043
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002044 return defined $use_type{$type} if (scalar keys %use_type > 0);
2045
2046 return !defined $ignore_type{$type};
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002047}
2048
2049sub report {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002050 my ($level, $type, $msg) = @_;
2051
2052 if (!show_type($type) ||
2053 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002054 return 0;
2055 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002056 my $output = '';
2057 if ($color) {
2058 if ($level eq 'ERROR') {
2059 $output .= RED;
2060 } elsif ($level eq 'WARNING') {
2061 $output .= YELLOW;
2062 } else {
2063 $output .= GREEN;
2064 }
2065 }
2066 $output .= $prefix . $level . ':';
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002067 if ($show_types) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002068 $output .= BLUE if ($color);
2069 $output .= "$type:";
2070 }
2071 $output .= RESET if ($color);
2072 $output .= ' ' . $msg . "\n";
2073
2074 if ($showfile) {
2075 my @lines = split("\n", $output, -1);
2076 splice(@lines, 1, 1);
2077 $output = join("\n", @lines);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002078 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002079 $output = (split('\n', $output))[0] . "\n" if ($terse);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002080
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002081 push(our @report, $output);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002082
2083 return 1;
2084}
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002085
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002086sub report_dump {
2087 our @report;
2088}
2089
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002090sub fixup_current_range {
2091 my ($lineRef, $offset, $length) = @_;
2092
2093 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2094 my $o = $1;
2095 my $l = $2;
2096 my $no = $o + $offset;
2097 my $nl = $l + $length;
2098 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2099 }
2100}
2101
2102sub fix_inserted_deleted_lines {
2103 my ($linesRef, $insertedRef, $deletedRef) = @_;
2104
2105 my $range_last_linenr = 0;
2106 my $delta_offset = 0;
2107
2108 my $old_linenr = 0;
2109 my $new_linenr = 0;
2110
2111 my $next_insert = 0;
2112 my $next_delete = 0;
2113
2114 my @lines = ();
2115
2116 my $inserted = @{$insertedRef}[$next_insert++];
2117 my $deleted = @{$deletedRef}[$next_delete++];
2118
2119 foreach my $old_line (@{$linesRef}) {
2120 my $save_line = 1;
2121 my $line = $old_line; #don't modify the array
2122 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
2123 $delta_offset = 0;
2124 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2125 $range_last_linenr = $new_linenr;
2126 fixup_current_range(\$line, $delta_offset, 0);
2127 }
2128
2129 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2130 $deleted = @{$deletedRef}[$next_delete++];
2131 $save_line = 0;
2132 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2133 }
2134
2135 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2136 push(@lines, ${$inserted}{'LINE'});
2137 $inserted = @{$insertedRef}[$next_insert++];
2138 $new_linenr++;
2139 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2140 }
2141
2142 if ($save_line) {
2143 push(@lines, $line);
2144 $new_linenr++;
2145 }
2146
2147 $old_linenr++;
2148 }
2149
2150 return @lines;
2151}
2152
2153sub fix_insert_line {
2154 my ($linenr, $line) = @_;
2155
2156 my $inserted = {
2157 LINENR => $linenr,
2158 LINE => $line,
2159 };
2160 push(@fixed_inserted, $inserted);
2161}
2162
2163sub fix_delete_line {
2164 my ($linenr, $line) = @_;
2165
2166 my $deleted = {
2167 LINENR => $linenr,
2168 LINE => $line,
2169 };
2170
2171 push(@fixed_deleted, $deleted);
2172}
2173
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002174sub ERROR {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002175 my ($type, $msg) = @_;
2176
2177 if (report("ERROR", $type, $msg)) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002178 our $clean = 0;
2179 our $cnt_error++;
Tom Rinia50f8de2014-02-27 08:27:28 -05002180 return 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002181 }
Tom Rinia50f8de2014-02-27 08:27:28 -05002182 return 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002183}
2184sub WARN {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002185 my ($type, $msg) = @_;
2186
2187 if (report("WARNING", $type, $msg)) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002188 our $clean = 0;
2189 our $cnt_warn++;
Tom Rinia50f8de2014-02-27 08:27:28 -05002190 return 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002191 }
Tom Rinia50f8de2014-02-27 08:27:28 -05002192 return 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002193}
2194sub CHK {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002195 my ($type, $msg) = @_;
2196
2197 if ($check && report("CHECK", $type, $msg)) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002198 our $clean = 0;
2199 our $cnt_chk++;
Tom Rinia50f8de2014-02-27 08:27:28 -05002200 return 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002201 }
Tom Rinia50f8de2014-02-27 08:27:28 -05002202 return 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002203}
2204
2205sub check_absolute_file {
2206 my ($absolute, $herecurr) = @_;
2207 my $file = $absolute;
2208
2209 ##print "absolute<$absolute>\n";
2210
2211 # See if any suffix of this path is a path within the tree.
2212 while ($file =~ s@^[^/]*/@@) {
2213 if (-f "$root/$file") {
2214 ##print "file<$file>\n";
2215 last;
2216 }
2217 }
2218 if (! -f _) {
2219 return 0;
2220 }
2221
2222 # It is, so see if the prefix is acceptable.
2223 my $prefix = $absolute;
2224 substr($prefix, -length($file)) = '';
2225
2226 ##print "prefix<$prefix>\n";
2227 if ($prefix ne ".../") {
2228 WARN("USE_RELATIVE_PATH",
2229 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2230 }
2231}
2232
Tom Rinia50f8de2014-02-27 08:27:28 -05002233sub trim {
2234 my ($string) = @_;
2235
2236 $string =~ s/^\s+|\s+$//g;
2237
2238 return $string;
2239}
2240
2241sub ltrim {
2242 my ($string) = @_;
2243
2244 $string =~ s/^\s+//;
2245
2246 return $string;
2247}
2248
2249sub rtrim {
2250 my ($string) = @_;
2251
2252 $string =~ s/\s+$//;
2253
2254 return $string;
2255}
2256
2257sub string_find_replace {
2258 my ($string, $find, $replace) = @_;
2259
2260 $string =~ s/$find/$replace/g;
2261
2262 return $string;
2263}
2264
2265sub tabify {
2266 my ($leading) = @_;
2267
Tom Rinid2e30332020-06-16 10:29:46 -04002268 my $source_indent = $tabsize;
Tom Rinia50f8de2014-02-27 08:27:28 -05002269 my $max_spaces_before_tab = $source_indent - 1;
2270 my $spaces_to_tab = " " x $source_indent;
2271
2272 #convert leading spaces to tabs
2273 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2274 #Remove spaces before a tab
2275 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2276
2277 return "$leading";
2278}
2279
Kim Phillips9b7b8372013-02-28 12:53:52 +00002280sub pos_last_openparen {
2281 my ($line) = @_;
2282
2283 my $pos = 0;
2284
2285 my $opens = $line =~ tr/\(/\(/;
2286 my $closes = $line =~ tr/\)/\)/;
2287
2288 my $last_openparen = 0;
2289
2290 if (($opens == 0) || ($closes >= $opens)) {
2291 return -1;
2292 }
2293
2294 my $len = length($line);
2295
2296 for ($pos = 0; $pos < $len; $pos++) {
2297 my $string = substr($line, $pos);
2298 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2299 $pos += length($1) - 1;
2300 } elsif (substr($line, $pos, 1) eq '(') {
2301 $last_openparen = $pos;
2302 } elsif (index($string, '(') == -1) {
2303 last;
2304 }
2305 }
2306
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002307 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002308}
2309
Tom Rinid2e30332020-06-16 10:29:46 -04002310sub get_raw_comment {
2311 my ($line, $rawline) = @_;
2312 my $comment = '';
2313
2314 for my $i (0 .. (length($line) - 1)) {
2315 if (substr($line, $i, 1) eq "$;") {
2316 $comment .= substr($rawline, $i, 1);
2317 }
2318 }
2319
2320 return $comment;
2321}
2322
Simon Glass2d848112020-12-03 16:55:24 -07002323# Args:
2324# line: Patch line to check
2325# auto: Auto variable name, e.g. "per_child_auto"
2326# suffix: Suffix to expect on member, e.g. "_priv"
2327# warning: Warning name, e.g. "PRIV_AUTO"
2328sub u_boot_struct_name {
2329 my ($line, $auto, $suffix, $warning) = @_;
2330
2331 # Use _priv as a suffix for the device-private data struct
2332 if ($line =~ /^\+\s*\.${auto}\s*=\s*sizeof\(struct\((\w+)\).*/) {
2333 my $struct_name = $1;
2334 if ($struct_name !~ /^\w+${suffix}/) {
2335 WARN($warning, "struct \'$struct_name\' should have a ${suffix} suffix");
2336 }
2337 }
2338}
2339
Simon Glassc8f4d092020-05-22 16:32:36 -06002340# Checks specific to U-Boot
2341sub u_boot_line {
Simon Glassf38051e2020-07-19 10:16:01 -06002342 my ($realfile, $line, $rawline, $herecurr) = @_;
Simon Glass7d919f52020-05-22 16:32:37 -06002343
2344 # ask for a test if a new uclass ID is added
2345 if ($realfile =~ /uclass-id.h/ && $line =~ /^\+/) {
2346 WARN("NEW_UCLASS",
2347 "Possible new uclass - make sure to add a sandbox driver, plus a test in test/dm/<name>.c\n" . $herecurr);
2348 }
Simon Glass3ee01322020-05-22 16:32:38 -06002349
2350 # try to get people to use the livetree API
2351 if ($line =~ /^\+.*fdtdec_/) {
2352 WARN("LIVETREE",
2353 "Use the livetree API (dev_read_...)\n" . $herecurr);
2354 }
Simon Glass35bf3b02020-05-22 16:32:39 -06002355
2356 # add tests for new commands
2357 if ($line =~ /^\+.*do_($Ident)\(struct cmd_tbl.*/) {
2358 WARN("CMD_TEST",
2359 "Possible new command - make sure you add a test\n" . $herecurr);
2360 }
Simon Glass567240d2020-05-22 16:32:40 -06002361
2362 # use if instead of #if
Simon Glass2ec1ed22020-06-14 10:54:08 -06002363 if ($realfile =~ /\.c$/ && $line =~ /^\+#if.*CONFIG.*/) {
Simon Glass567240d2020-05-22 16:32:40 -06002364 WARN("PREFER_IF",
2365 "Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef' where possible\n" . $herecurr);
2366 }
Tom Rinid7b4ab52020-05-26 14:29:02 -04002367
Sean Anderson50fe43f2021-03-11 00:15:45 -05002368 # prefer strl(cpy|cat) over strn(cpy|cat)
2369 if ($line =~ /\bstrn(cpy|cat)\s*\(/) {
2370 WARN("STRL",
2371 "strl$1 is preferred over strn$1 because it always produces a nul-terminated string\n" . $herecurr);
2372 }
2373
Tom Rinid7b4ab52020-05-26 14:29:02 -04002374 # use defconfig to manage CONFIG_CMD options
2375 if ($line =~ /\+\s*#\s*(define|undef)\s+(CONFIG_CMD\w*)\b/) {
2376 ERROR("DEFINE_CONFIG_CMD",
2377 "All commands are managed by Kconfig\n" . $herecurr);
2378 }
Simon Glassf38051e2020-07-19 10:16:01 -06002379
2380 # Don't put common.h and dm.h in header files
2381 if ($realfile =~ /\.h$/ && $rawline =~ /^\+#include\s*<(common|dm)\.h>*/) {
2382 ERROR("BARRED_INCLUDE_IN_HDR",
2383 "Avoid including common.h and dm.h in header files\n" . $herecurr);
2384 }
Tom Rini46b3e0b2020-08-20 08:37:49 -04002385
2386 # Do not disable fdt / initrd relocation
2387 if ($rawline =~ /.*(fdt|initrd)_high=0xffffffff/) {
2388 ERROR("DISABLE_FDT_OR_INITRD_RELOC",
2389 "fdt or initrd relocation disabled at boot time\n" . $herecurr);
2390 }
Alper Nebi Yasak7c1a3022020-10-05 09:57:30 +03002391
Wasim Khan6e5aad82021-02-04 15:44:04 +01002392 # make sure 'skip_board_fixup' is not
2393 if ($rawline =~ /.*skip_board_fixup.*/) {
2394 ERROR("SKIP_BOARD_FIXUP",
2395 "Avoid setting skip_board_fixup env variable\n" . $herecurr);
2396 }
2397
Alper Nebi Yasak7c1a3022020-10-05 09:57:30 +03002398 # Do not use CONFIG_ prefix in CONFIG_IS_ENABLED() calls
2399 if ($line =~ /^\+.*CONFIG_IS_ENABLED\(CONFIG_\w*\).*/) {
2400 ERROR("CONFIG_IS_ENABLED_CONFIG",
2401 "CONFIG_IS_ENABLED() takes values without the CONFIG_ prefix\n" . $herecurr);
2402 }
Simon Glass2d848112020-12-03 16:55:24 -07002403
2404 # Use _priv as a suffix for the device-private data struct
2405 if ($line =~ /^\+\s*\.priv_auto\s*=\s*sizeof\(struct\((\w+)\).*/) {
2406 my $struct_name = $1;
2407 if ($struct_name !~ /^\w+_priv/) {
2408 WARN("PRIV_AUTO", "struct \'$struct_name\' should have a _priv suffix");
2409 }
2410 }
2411
2412 # Check struct names for the 'auto' members of struct driver
2413 u_boot_struct_name($line, "priv_auto", "_priv", "PRIV_AUTO");
2414 u_boot_struct_name($line, "plat_auto", "_plat", "PLAT_AUTO");
2415 u_boot_struct_name($line, "per_child_auto", "_priv", "CHILD_PRIV_AUTO");
2416 u_boot_struct_name($line, "per_child_plat_auto", "_plat",
2417 "CHILD_PLAT_AUTO");
2418
2419 # Now the ones for struct uclass, skipping those in common with above
2420 u_boot_struct_name($line, "per_device_auto", "_priv",
2421 "DEVICE_PRIV_AUTO");
2422 u_boot_struct_name($line, "per_device_plat_auto", "_plat",
2423 "DEVICE_PLAT_AUTO");
Simon Glassc8f4d092020-05-22 16:32:36 -06002424}
2425
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002426sub process {
2427 my $filename = shift;
2428
2429 my $linenr=0;
2430 my $prevline="";
2431 my $prevrawline="";
2432 my $stashline="";
2433 my $stashrawline="";
2434
2435 my $length;
2436 my $indent;
2437 my $previndent=0;
2438 my $stashindent=0;
2439
2440 our $clean = 1;
2441 my $signoff = 0;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002442 my $author = '';
2443 my $authorsignoff = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002444 my $is_patch = 0;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002445 my $is_binding_patch = -1;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002446 my $in_header_lines = $file ? 0 : 1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002447 my $in_commit_log = 0; #Scanning lines before patch
Tom Rinid2e30332020-06-16 10:29:46 -04002448 my $has_patch_separator = 0; #Found a --- line
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002449 my $has_commit_log = 0; #Encountered lines before patch
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002450 my $commit_log_lines = 0; #Number of commit log lines
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002451 my $commit_log_possible_stack_dump = 0;
2452 my $commit_log_long_line = 0;
2453 my $commit_log_has_diff = 0;
2454 my $reported_maintainer_file = 0;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002455 my $non_utf8_charset = 0;
2456
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002457 my $last_blank_line = 0;
2458 my $last_coalesced_string_linenr = -1;
2459
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002460 our @report = ();
2461 our $cnt_lines = 0;
2462 our $cnt_error = 0;
2463 our $cnt_warn = 0;
2464 our $cnt_chk = 0;
2465
2466 # Trace the real file/line as we go.
2467 my $realfile = '';
2468 my $realline = 0;
2469 my $realcnt = 0;
2470 my $here = '';
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002471 my $context_function; #undef'd unless there's a known function
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002472 my $in_comment = 0;
2473 my $comment_edge = 0;
2474 my $first_line = 0;
2475 my $p1_prefix = '';
2476
2477 my $prev_values = 'E';
2478
2479 # suppression flags
2480 my %suppress_ifbraces;
2481 my %suppress_whiletrailers;
2482 my %suppress_export;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002483 my $suppress_statement = 0;
2484
Tom Rinia50f8de2014-02-27 08:27:28 -05002485 my %signatures = ();
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002486
2487 # Pre-scan the patch sanitizing the lines.
2488 # Pre-scan the patch looking for any __setup documentation.
2489 #
2490 my @setup_docs = ();
2491 my $setup_docs = 0;
2492
Tom Rinia50f8de2014-02-27 08:27:28 -05002493 my $camelcase_file_seeded = 0;
2494
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002495 my $checklicenseline = 1;
2496
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002497 sanitise_line_reset();
2498 my $line;
2499 foreach my $rawline (@rawlines) {
2500 $linenr++;
2501 $line = $rawline;
2502
Tom Rinia50f8de2014-02-27 08:27:28 -05002503 push(@fixed, $rawline) if ($fix);
2504
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002505 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2506 $setup_docs = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002507 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002508 $setup_docs = 1;
2509 }
2510 #next;
2511 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002512 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002513 $realline=$1-1;
2514 if (defined $2) {
2515 $realcnt=$3+1;
2516 } else {
2517 $realcnt=1+1;
2518 }
2519 $in_comment = 0;
2520
2521 # Guestimate if this is a continuing comment. Run
2522 # the context looking for a comment "edge". If this
2523 # edge is a close comment then we must be in a comment
2524 # at context start.
2525 my $edge;
2526 my $cnt = $realcnt;
2527 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2528 next if (defined $rawlines[$ln - 1] &&
2529 $rawlines[$ln - 1] =~ /^-/);
2530 $cnt--;
2531 #print "RAW<$rawlines[$ln - 1]>\n";
2532 last if (!defined $rawlines[$ln - 1]);
2533 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2534 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2535 ($edge) = $1;
2536 last;
2537 }
2538 }
2539 if (defined $edge && $edge eq '*/') {
2540 $in_comment = 1;
2541 }
2542
2543 # Guestimate if this is a continuing comment. If this
2544 # is the start of a diff block and this line starts
2545 # ' *' then it is very likely a comment.
2546 if (!defined $edge &&
2547 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2548 {
2549 $in_comment = 1;
2550 }
2551
2552 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2553 sanitise_line_reset($in_comment);
2554
2555 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2556 # Standardise the strings and chars within the input to
2557 # simplify matching -- only bother with positive lines.
2558 $line = sanitise_line($rawline);
2559 }
2560 push(@lines, $line);
2561
2562 if ($realcnt > 1) {
2563 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2564 } else {
2565 $realcnt = 0;
2566 }
2567
2568 #print "==>$rawline\n";
2569 #print "-->$line\n";
2570
2571 if ($setup_docs && $line =~ /^\+/) {
2572 push(@setup_docs, $line);
2573 }
2574 }
2575
2576 $prefix = '';
2577
2578 $realcnt = 0;
2579 $linenr = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002580 $fixlinenr = -1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002581 foreach my $line (@lines) {
2582 $linenr++;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002583 $fixlinenr++;
Tom Rinia50f8de2014-02-27 08:27:28 -05002584 my $sline = $line; #copy of $line
2585 $sline =~ s/$;/ /g; #with comments as spaces
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002586
2587 my $rawline = $rawlines[$linenr - 1];
Tom Rinid2e30332020-06-16 10:29:46 -04002588 my $raw_comment = get_raw_comment($line, $rawline);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002589
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002590# check if it's a mode change, rename or start of a patch
2591 if (!$in_commit_log &&
2592 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2593 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2594 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2595 $is_patch = 1;
2596 }
2597
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002598#extract the line range in the file after the patch is applied
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002599 if (!$in_commit_log &&
2600 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2601 my $context = $4;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002602 $is_patch = 1;
2603 $first_line = $linenr + 1;
2604 $realline=$1-1;
2605 if (defined $2) {
2606 $realcnt=$3+1;
2607 } else {
2608 $realcnt=1+1;
2609 }
2610 annotate_reset();
2611 $prev_values = 'E';
2612
2613 %suppress_ifbraces = ();
2614 %suppress_whiletrailers = ();
2615 %suppress_export = ();
Kim Phillips9b7b8372013-02-28 12:53:52 +00002616 $suppress_statement = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002617 if ($context =~ /\b(\w+)\s*\(/) {
2618 $context_function = $1;
2619 } else {
2620 undef $context_function;
2621 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002622 next;
2623
2624# track the line number as we move through the hunk, note that
2625# new versions of GNU diff omit the leading space on completely
2626# blank context lines so we need to count that too.
2627 } elsif ($line =~ /^( |\+|$)/) {
2628 $realline++;
2629 $realcnt-- if ($realcnt != 0);
2630
2631 # Measure the line length and indent.
2632 ($length, $indent) = line_stats($rawline);
2633
2634 # Track the previous line.
2635 ($prevline, $stashline) = ($stashline, $line);
2636 ($previndent, $stashindent) = ($stashindent, $indent);
2637 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2638
2639 #warn "line<$line>\n";
2640
2641 } elsif ($realcnt == 1) {
2642 $realcnt--;
2643 }
2644
2645 my $hunk_line = ($realcnt != 0);
2646
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002647 $here = "#$linenr: " if (!$file);
2648 $here = "#$realline: " if ($file);
2649
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002650 my $found_file = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002651 # extract the filename as it passes
2652 if ($line =~ /^diff --git.*?(\S+)$/) {
2653 $realfile = $1;
Tom Rinia50f8de2014-02-27 08:27:28 -05002654 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillips9b7b8372013-02-28 12:53:52 +00002655 $in_commit_log = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002656 $found_file = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002657 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2658 $realfile = $1;
Tom Rinia50f8de2014-02-27 08:27:28 -05002659 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillips9b7b8372013-02-28 12:53:52 +00002660 $in_commit_log = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002661
2662 $p1_prefix = $1;
2663 if (!$file && $tree && $p1_prefix ne '' &&
2664 -e "$root/$p1_prefix") {
2665 WARN("PATCH_PREFIX",
2666 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2667 }
2668
2669 if ($realfile =~ m@^include/asm/@) {
2670 ERROR("MODIFIED_INCLUDE_ASM",
2671 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2672 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002673 $found_file = 1;
2674 }
2675
2676#make up the handle for any error we report on this line
2677 if ($showfile) {
2678 $prefix = "$realfile:$realline: "
2679 } elsif ($emacs) {
2680 if ($file) {
2681 $prefix = "$filename:$realline: ";
2682 } else {
2683 $prefix = "$filename:$linenr: ";
2684 }
2685 }
2686
2687 if ($found_file) {
2688 if (is_maintained_obsolete($realfile)) {
2689 WARN("OBSOLETE",
2690 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2691 }
2692 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2693 $check = 1;
2694 } else {
2695 $check = $check_orig;
2696 }
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02002697 $checklicenseline = 1;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002698
2699 if ($realfile !~ /^MAINTAINERS/) {
2700 my $last_binding_patch = $is_binding_patch;
2701
2702 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2703
2704 if (($last_binding_patch != -1) &&
2705 ($last_binding_patch ^ $is_binding_patch)) {
2706 WARN("DT_SPLIT_BINDING_PATCH",
2707 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2708 }
2709 }
2710
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002711 next;
2712 }
2713
2714 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2715
2716 my $hereline = "$here\n$rawline\n";
2717 my $herecurr = "$here\n$rawline\n";
2718 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2719
2720 $cnt_lines++ if ($realcnt != 0);
2721
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002722# Verify the existence of a commit log if appropriate
2723# 2 is used because a $signature is counted in $commit_log_lines
2724 if ($in_commit_log) {
2725 if ($line !~ /^\s*$/) {
2726 $commit_log_lines++; #could be a $signature
2727 }
2728 } elsif ($has_commit_log && $commit_log_lines < 2) {
2729 WARN("COMMIT_MESSAGE",
2730 "Missing commit description - Add an appropriate one\n");
2731 $commit_log_lines = 2; #warn only once
2732 }
2733
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002734# Check if the commit log has what seems like a diff which can confuse patch
2735 if ($in_commit_log && !$commit_log_has_diff &&
2736 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2737 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2738 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2739 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2740 ERROR("DIFF_IN_COMMIT_MSG",
2741 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2742 $commit_log_has_diff = 1;
2743 }
2744
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002745# Check for incorrect file permissions
2746 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2747 my $permhere = $here . "FILE: $realfile\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05002748 if ($realfile !~ m@scripts/@ &&
2749 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002750 ERROR("EXECUTE_PERMISSIONS",
2751 "do not set execute permissions for source files\n" . $permhere);
2752 }
2753 }
2754
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002755# Check the patch for a From:
2756 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2757 $author = $1;
2758 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2759 $author =~ s/"//g;
Tom Rinid2e30332020-06-16 10:29:46 -04002760 $author = reformat_email($author);
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002761 }
2762
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002763# Check the patch for a signoff:
Tom Rinid2e30332020-06-16 10:29:46 -04002764 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002765 $signoff++;
Kim Phillips9b7b8372013-02-28 12:53:52 +00002766 $in_commit_log = 0;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002767 if ($author ne '') {
Tom Rinid2e30332020-06-16 10:29:46 -04002768 if (same_email_addresses($1, $author)) {
2769 $authorsignoff = 1;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02002770 }
2771 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002772 }
2773
Tom Rinid2e30332020-06-16 10:29:46 -04002774# Check for patch separator
2775 if ($line =~ /^---$/) {
2776 $has_patch_separator = 1;
2777 $in_commit_log = 0;
2778 }
2779
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002780# Check if MAINTAINERS is being updated. If so, there's probably no need to
2781# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2782 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2783 $reported_maintainer_file = 1;
2784 }
2785
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002786# Check signature styles
Kim Phillips9b7b8372013-02-28 12:53:52 +00002787 if (!$in_header_lines &&
2788 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002789 my $space_before = $1;
2790 my $sign_off = $2;
2791 my $space_after = $3;
2792 my $email = $4;
2793 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2794
Kim Phillips9b7b8372013-02-28 12:53:52 +00002795 if ($sign_off !~ /$signature_tags/) {
2796 WARN("BAD_SIGN_OFF",
2797 "Non-standard signature: $sign_off\n" . $herecurr);
2798 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002799 if (defined $space_before && $space_before ne "") {
Tom Rinia50f8de2014-02-27 08:27:28 -05002800 if (WARN("BAD_SIGN_OFF",
2801 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2802 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002803 $fixed[$fixlinenr] =
Tom Rinia50f8de2014-02-27 08:27:28 -05002804 "$ucfirst_sign_off $email";
2805 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002806 }
2807 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Tom Rinia50f8de2014-02-27 08:27:28 -05002808 if (WARN("BAD_SIGN_OFF",
2809 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2810 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002811 $fixed[$fixlinenr] =
Tom Rinia50f8de2014-02-27 08:27:28 -05002812 "$ucfirst_sign_off $email";
2813 }
2814
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002815 }
2816 if (!defined $space_after || $space_after ne " ") {
Tom Rinia50f8de2014-02-27 08:27:28 -05002817 if (WARN("BAD_SIGN_OFF",
2818 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2819 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002820 $fixed[$fixlinenr] =
Tom Rinia50f8de2014-02-27 08:27:28 -05002821 "$ucfirst_sign_off $email";
2822 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002823 }
2824
Tom Rinid2e30332020-06-16 10:29:46 -04002825 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002826 my $suggested_email = format_email(($email_name, $email_address));
2827 if ($suggested_email eq "") {
2828 ERROR("BAD_SIGN_OFF",
2829 "Unrecognized email address: '$email'\n" . $herecurr);
2830 } else {
2831 my $dequoted = $suggested_email;
2832 $dequoted =~ s/^"//;
2833 $dequoted =~ s/" </ </;
2834 # Don't force email to have quotes
2835 # Allow just an angle bracketed address
Tom Rinid2e30332020-06-16 10:29:46 -04002836 if (!same_email_addresses($email, $suggested_email)) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002837 WARN("BAD_SIGN_OFF",
2838 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2839 }
2840 }
Tom Rinia50f8de2014-02-27 08:27:28 -05002841
2842# Check for duplicate signatures
2843 my $sig_nospace = $line;
2844 $sig_nospace =~ s/\s//g;
2845 $sig_nospace = lc($sig_nospace);
2846 if (defined $signatures{$sig_nospace}) {
2847 WARN("BAD_SIGN_OFF",
2848 "Duplicate signature\n" . $herecurr);
2849 } else {
2850 $signatures{$sig_nospace} = 1;
2851 }
Tom Rinid2e30332020-06-16 10:29:46 -04002852
2853# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2854 if ($sign_off =~ /^co-developed-by:$/i) {
2855 if ($email eq $author) {
2856 WARN("BAD_SIGN_OFF",
2857 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2858 }
2859 if (!defined $lines[$linenr]) {
2860 WARN("BAD_SIGN_OFF",
2861 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2862 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2863 WARN("BAD_SIGN_OFF",
2864 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2865 } elsif ($1 ne $email) {
2866 WARN("BAD_SIGN_OFF",
2867 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2868 }
2869 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002870 }
2871
2872# Check email subject for common tools that don't need to be mentioned
2873 if ($in_header_lines &&
2874 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2875 WARN("EMAIL_SUBJECT",
2876 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2877 }
2878
Tom Rinid2e30332020-06-16 10:29:46 -04002879# Check for Gerrit Change-Ids not in any patch context
2880 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002881 ERROR("GERRIT_CHANGE_ID",
Tom Rinid2e30332020-06-16 10:29:46 -04002882 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002883 }
2884
2885# Check if the commit log is in a possible stack dump
2886 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2887 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2888 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2889 # timestamp
Tom Rinid2e30332020-06-16 10:29:46 -04002890 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2891 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2892 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2893 # stack dump address styles
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002894 $commit_log_possible_stack_dump = 1;
2895 }
2896
2897# Check for line lengths > 75 in commit log, warn once
2898 if ($in_commit_log && !$commit_log_long_line &&
2899 length($line) > 75 &&
2900 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2901 # file delta changes
2902 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2903 # filename then :
2904 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2905 # A Fixes: or Link: line
2906 $commit_log_possible_stack_dump)) {
2907 WARN("COMMIT_LOG_LONG_LINE",
2908 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2909 $commit_log_long_line = 1;
2910 }
2911
2912# Reset possible stack dump if a blank line is found
2913 if ($in_commit_log && $commit_log_possible_stack_dump &&
2914 $line =~ /^\s*$/) {
2915 $commit_log_possible_stack_dump = 0;
2916 }
2917
2918# Check for git id commit length and improperly formed commit descriptions
2919 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Tom Rinid2e30332020-06-16 10:29:46 -04002920 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02002921 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2922 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2923 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2924 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2925 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2926 my $init_char = "c";
2927 my $orig_commit = "";
2928 my $short = 1;
2929 my $long = 0;
2930 my $case = 1;
2931 my $space = 1;
2932 my $hasdesc = 0;
2933 my $hasparens = 0;
2934 my $id = '0123456789ab';
2935 my $orig_desc = "commit description";
2936 my $description = "";
2937
2938 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2939 $init_char = $1;
2940 $orig_commit = lc($2);
2941 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2942 $orig_commit = lc($1);
2943 }
2944
2945 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2946 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2947 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2948 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2949 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2950 $orig_desc = $1;
2951 $hasparens = 1;
2952 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2953 defined $rawlines[$linenr] &&
2954 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2955 $orig_desc = $1;
2956 $hasparens = 1;
2957 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2958 defined $rawlines[$linenr] &&
2959 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2960 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2961 $orig_desc = $1;
2962 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2963 $orig_desc .= " " . $1;
2964 $hasparens = 1;
2965 }
2966
2967 ($id, $description) = git_commit_info($orig_commit,
2968 $id, $orig_desc);
2969
2970 if (defined($id) &&
2971 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2972 ERROR("GIT_COMMIT_ID",
2973 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2974 }
2975 }
2976
2977# Check for added, moved or deleted files
2978 if (!$reported_maintainer_file && !$in_commit_log &&
2979 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2980 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2981 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2982 (defined($1) || defined($2))))) {
2983 $is_patch = 1;
2984 $reported_maintainer_file = 1;
2985 WARN("FILE_PATH_CHANGES",
2986 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002987 }
2988
Tom Rinid2e30332020-06-16 10:29:46 -04002989# Check for adding new DT bindings not in schema format
2990 if (!$in_commit_log &&
2991 ($line =~ /^new file mode\s*\d+\s*$/) &&
2992 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2993 WARN("DT_SCHEMA_BINDING_PATCH",
2994 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2995 }
2996
Joe Hershbergerba2beb52011-10-18 10:06:59 +00002997# Check for wrappage within a valid hunk of the file
2998 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2999 ERROR("CORRUPTED_PATCH",
3000 "patch seems to be corrupt (line wrapped?)\n" .
3001 $herecurr) if (!$emitted_corrupt++);
3002 }
3003
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003004# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3005 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3006 $rawline !~ m/^$UTF8*$/) {
3007 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3008
3009 my $blank = copy_spacing($rawline);
3010 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3011 my $hereptr = "$hereline$ptr\n";
3012
3013 CHK("INVALID_UTF8",
3014 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
3015 }
3016
Kim Phillips9b7b8372013-02-28 12:53:52 +00003017# Check if it's the start of a commit log
3018# (not a header line and we haven't seen the patch filename)
3019 if ($in_header_lines && $realfile =~ /^$/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003020 !($rawline =~ /^\s+(?:\S|$)/ ||
3021 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00003022 $in_header_lines = 0;
3023 $in_commit_log = 1;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003024 $has_commit_log = 1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00003025 }
3026
3027# Check if there is UTF-8 in a commit log when a mail header has explicitly
3028# declined it, i.e defined some charset where it is missing.
3029 if ($in_header_lines &&
3030 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3031 $1 !~ /utf-8/i) {
3032 $non_utf8_charset = 1;
3033 }
3034
3035 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
3036 $rawline =~ /$NON_ASCII_UTF8/) {
3037 WARN("UTF8_BEFORE_PATCH",
3038 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3039 }
3040
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003041# Check for absolute kernel paths in commit message
3042 if ($tree && $in_commit_log) {
3043 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3044 my $file = $1;
3045
3046 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3047 check_absolute_file($1, $herecurr)) {
3048 #
3049 } else {
3050 check_absolute_file($file, $herecurr);
3051 }
3052 }
3053 }
3054
Dan Murphy463f6882017-01-31 14:15:53 -06003055# Check for various typo / spelling mistakes
3056 if (defined($misspellings) &&
3057 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
3058 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
3059 my $typo = $1;
3060 my $typo_fix = $spelling_fix{lc($typo)};
3061 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3062 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003063 my $msg_level = \&WARN;
3064 $msg_level = \&CHK if ($file);
3065 if (&{$msg_level}("TYPO_SPELLING",
3066 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Dan Murphy463f6882017-01-31 14:15:53 -06003067 $fix) {
3068 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3069 }
3070 }
3071 }
3072
Tom Rinid2e30332020-06-16 10:29:46 -04003073# check for invalid commit id
3074 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3075 my $id;
3076 my $description;
3077 ($id, $description) = git_commit_info($2, undef, undef);
3078 if (!defined($id)) {
3079 WARN("UNKNOWN_COMMIT_ID",
3080 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3081 }
3082 }
3083
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003084# ignore non-hunk lines and lines being removed
3085 next if (!$hunk_line || $line =~ /^-/);
3086
3087#trailing whitespace
3088 if ($line =~ /^\+.*\015/) {
3089 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05003090 if (ERROR("DOS_LINE_ENDINGS",
3091 "DOS line endings\n" . $herevet) &&
3092 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003093 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Tom Rinia50f8de2014-02-27 08:27:28 -05003094 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003095 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3096 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05003097 if (ERROR("TRAILING_WHITESPACE",
3098 "trailing whitespace\n" . $herevet) &&
3099 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003100 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rinia50f8de2014-02-27 08:27:28 -05003101 }
3102
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003103 $rpt_cleaners = 1;
3104 }
3105
Tom Rinia50f8de2014-02-27 08:27:28 -05003106# Check for FSF mailing addresses.
3107 if ($rawline =~ /\bwrite to the Free/i ||
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003108 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Tom Rinia50f8de2014-02-27 08:27:28 -05003109 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3110 $rawline =~ /\b51\s+Franklin\s+St/i) {
3111 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003112 my $msg_level = \&ERROR;
3113 $msg_level = \&CHK if ($file);
3114 &{$msg_level}("FSF_MAILING_ADDRESS",
3115 "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)
Tom Rinia50f8de2014-02-27 08:27:28 -05003116 }
3117
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003118# check for Kconfig help text having a real description
3119# Only applies when adding the entry originally, after that we do not have
3120# sufficient context to determine whether it is indeed long enough.
3121 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003122 # 'choice' is usually the last thing on the line (though
3123 # Kconfig supports named choices), so use a word boundary
3124 # (\b) rather than a whitespace character (\s)
3125 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003126 my $length = 0;
3127 my $cnt = $realcnt;
3128 my $ln = $linenr + 1;
3129 my $f;
Kim Phillips9b7b8372013-02-28 12:53:52 +00003130 my $is_start = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003131 my $is_end = 0;
Kim Phillips9b7b8372013-02-28 12:53:52 +00003132 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003133 $f = $lines[$ln - 1];
3134 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3135 $is_end = $lines[$ln - 1] =~ /^\+/;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003136
3137 next if ($f =~ /^-/);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003138 last if (!$file && $f =~ /^\@\@/);
Kim Phillips9b7b8372013-02-28 12:53:52 +00003139
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003140 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00003141 $is_start = 1;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003142 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3143 if ($lines[$ln - 1] =~ "---help---") {
3144 WARN("CONFIG_DESCRIPTION",
3145 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3146 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00003147 $length = -1;
3148 }
3149
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003150 $f =~ s/^.//;
3151 $f =~ s/#.*//;
3152 $f =~ s/^\s+//;
3153 next if ($f =~ /^$/);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003154
3155 # This only checks context lines in the patch
3156 # and so hopefully shouldn't trigger false
3157 # positives, even though some of these are
3158 # common words in help texts
3159 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3160 if|endif|menu|endmenu|source)\b/x) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003161 $is_end = 1;
3162 last;
3163 }
3164 $length++;
3165 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003166 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3167 WARN("CONFIG_DESCRIPTION",
3168 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3169 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00003170 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
3171 }
3172
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003173# check for MAINTAINERS entries that don't have the right form
3174 if ($realfile =~ /^MAINTAINERS$/ &&
3175 $rawline =~ /^\+[A-Z]:/ &&
3176 $rawline !~ /^\+[A-Z]:\t\S/) {
3177 if (WARN("MAINTAINERS_STYLE",
3178 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3179 $fix) {
3180 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3181 }
3182 }
3183
3184# discourage the use of boolean for type definition attributes of Kconfig options
Kim Phillips9b7b8372013-02-28 12:53:52 +00003185 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003186 $line =~ /^\+\s*\bboolean\b/) {
3187 WARN("CONFIG_TYPE_BOOLEAN",
3188 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003189 }
3190
Kim Phillips9b7b8372013-02-28 12:53:52 +00003191 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3192 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3193 my $flag = $1;
3194 my $replacement = {
3195 'EXTRA_AFLAGS' => 'asflags-y',
3196 'EXTRA_CFLAGS' => 'ccflags-y',
3197 'EXTRA_CPPFLAGS' => 'cppflags-y',
3198 'EXTRA_LDFLAGS' => 'ldflags-y',
3199 };
3200
3201 WARN("DEPRECATED_VARIABLE",
3202 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3203 }
3204
Tom Rinia50f8de2014-02-27 08:27:28 -05003205# check for DT compatible documentation
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003206 if (defined $root &&
3207 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3208 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3209
Tom Rinia50f8de2014-02-27 08:27:28 -05003210 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3211
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003212 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Tom Rinid2e30332020-06-16 10:29:46 -04003213 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003214
Tom Rinia50f8de2014-02-27 08:27:28 -05003215 foreach my $compat (@compats) {
3216 my $compat2 = $compat;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003217 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3218 my $compat3 = $compat;
3219 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3220 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Tom Rinia50f8de2014-02-27 08:27:28 -05003221 if ( $? >> 8 ) {
3222 WARN("UNDOCUMENTED_DT_STRING",
3223 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3224 }
3225
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003226 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3227 my $vendor = $1;
Tom Rinid2e30332020-06-16 10:29:46 -04003228 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Tom Rinia50f8de2014-02-27 08:27:28 -05003229 if ( $? >> 8 ) {
3230 WARN("UNDOCUMENTED_DT_STRING",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003231 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Tom Rinia50f8de2014-02-27 08:27:28 -05003232 }
3233 }
3234 }
3235
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003236# check for using SPDX license tag at beginning of files
3237 if ($realline == $checklicenseline) {
3238 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3239 $checklicenseline = 2;
3240 } elsif ($rawline =~ /^\+/) {
3241 my $comment = "";
3242 if ($realfile =~ /\.(h|s|S)$/) {
3243 $comment = '/*';
3244 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3245 $comment = '//';
Tom Rinid2e30332020-06-16 10:29:46 -04003246 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003247 $comment = '#';
3248 } elsif ($realfile =~ /\.rst$/) {
3249 $comment = '..';
3250 }
3251
Tom Rinid2e30332020-06-16 10:29:46 -04003252# check SPDX comment style for .[chsS] files
3253 if ($realfile =~ /\.[chsS]$/ &&
3254 $rawline =~ /SPDX-License-Identifier:/ &&
3255 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3256 WARN("SPDX_LICENSE_TAG",
3257 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3258 }
3259
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003260 if ($comment !~ /^$/ &&
Tom Rinid2e30332020-06-16 10:29:46 -04003261 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3262 WARN("SPDX_LICENSE_TAG",
3263 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003264 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Tom Rinid2e30332020-06-16 10:29:46 -04003265 my $spdx_license = $1;
3266 if (!is_SPDX_License_valid($spdx_license)) {
3267 WARN("SPDX_LICENSE_TAG",
3268 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3269 }
3270 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3271 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3272 my $msg_level = \&WARN;
3273 $msg_level = \&CHK if ($file);
3274 if (&{$msg_level}("SPDX_LICENSE_TAG",
3275
3276 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3277 $fix) {
3278 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3279 }
3280 }
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003281 }
3282 }
3283 }
3284
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003285# check we are in a valid source file if not then ignore this hunk
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003286 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003287
Tom Rinid2e30332020-06-16 10:29:46 -04003288# check for using SPDX-License-Identifier on the wrong line number
3289 if ($realline != $checklicenseline &&
3290 $rawline =~ /\bSPDX-License-Identifier:/ &&
3291 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3292 WARN("SPDX_LICENSE_TAG",
3293 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3294 }
3295
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003296# line length limit (with some exclusions)
3297#
3298# There are a few types of lines that may extend beyond $max_line_length:
3299# logging functions like pr_info that end in a string
3300# lines with a single string
3301# #defines that are a single string
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003302# lines with an RFC3986 like URL
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003303#
3304# There are 3 different line length message types:
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003305# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003306# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3307# LONG_LINE all other lines longer than $max_line_length
3308#
3309# if LONG_LINE is ignored, the other 2 types are also ignored
3310#
Kim Phillips9b7b8372013-02-28 12:53:52 +00003311
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003312 if ($line =~ /^\+/ && $length > $max_line_length) {
3313 my $msg_type = "LONG_LINE";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003314
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003315 # Check the allowed long line types first
3316
3317 # logging functions that end in a string that starts
3318 # before $max_line_length
3319 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3320 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3321 $msg_type = "";
3322
3323 # lines with only strings (w/ possible termination)
3324 # #defines with only strings
3325 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3326 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3327 $msg_type = "";
3328
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003329 # More special cases
3330 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3331 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3332 $msg_type = "";
3333
3334 # URL ($rawline is used in case the URL is in a comment)
3335 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003336 $msg_type = "";
3337
3338 # Otherwise set the alternate message types
3339
3340 # a comment starts before $max_line_length
3341 } elsif ($line =~ /($;[\s$;]*)$/ &&
3342 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3343 $msg_type = "LONG_LINE_COMMENT"
3344
3345 # a quoted string starts before $max_line_length
3346 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3347 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3348 $msg_type = "LONG_LINE_STRING"
Tom Rinia50f8de2014-02-27 08:27:28 -05003349 }
3350
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003351 if ($msg_type ne "" &&
3352 (show_type("LONG_LINE") || show_type($msg_type))) {
Simon Glassa33d9822020-05-22 16:32:35 -06003353 my $msg_level = \&WARN;
3354 $msg_level = \&CHK if ($file);
3355 &{$msg_level}($msg_type,
3356 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003357 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003358 }
3359
3360# check for adding lines without a newline.
3361 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3362 WARN("MISSING_EOF_NEWLINE",
3363 "adding a line without newline at end of file\n" . $herecurr);
3364 }
3365
Simon Glassc8f4d092020-05-22 16:32:36 -06003366 if ($u_boot) {
Simon Glassf38051e2020-07-19 10:16:01 -06003367 u_boot_line($realfile, $line, $rawline, $herecurr);
Simon Glassc8f4d092020-05-22 16:32:36 -06003368 }
3369
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003370# check we are in a valid source file C or perl if not then ignore this hunk
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003371 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003372
3373# at the beginning of a line any tabs must come first and anything
Tom Rinid2e30332020-06-16 10:29:46 -04003374# more than $tabsize must use tabs.
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003375 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3376 $rawline =~ /^\+\s* \s*/) {
3377 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003378 $rpt_cleaners = 1;
Tom Rinia50f8de2014-02-27 08:27:28 -05003379 if (ERROR("CODE_INDENT",
3380 "code indent should use tabs where possible\n" . $herevet) &&
3381 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003382 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rinia50f8de2014-02-27 08:27:28 -05003383 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003384 }
3385
3386# check for space before tabs.
3387 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3388 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05003389 if (WARN("SPACE_BEFORE_TAB",
3390 "please, no space before tabs\n" . $herevet) &&
3391 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003392 while ($fixed[$fixlinenr] =~
Tom Rinid2e30332020-06-16 10:29:46 -04003393 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003394 while ($fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05003395 s/(^\+.*) +\t/$1\t/) {}
3396 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003397 }
3398
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003399# check for assignments on the start of a line
3400 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3401 CHK("ASSIGNMENT_CONTINUATIONS",
3402 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3403 }
3404
Kim Phillips9b7b8372013-02-28 12:53:52 +00003405# check for && or || at the start of a line
3406 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3407 CHK("LOGICAL_CONTINUATIONS",
3408 "Logical continuations should be on the previous line\n" . $hereprev);
3409 }
3410
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003411# check indentation starts on a tab stop
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003412 if ($perl_version_ok &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003413 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003414 my $indent = length($1);
Tom Rinid2e30332020-06-16 10:29:46 -04003415 if ($indent % $tabsize) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003416 if (WARN("TABSTOP",
3417 "Statements should start on a tabstop\n" . $herecurr) &&
3418 $fix) {
Tom Rinid2e30332020-06-16 10:29:46 -04003419 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003420 }
3421 }
3422 }
3423
Kim Phillips9b7b8372013-02-28 12:53:52 +00003424# check multi-line statement indentation matches previous line
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003425 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003426 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00003427 $prevline =~ /^\+(\t*)(.*)$/;
3428 my $oldindent = $1;
3429 my $rest = $2;
3430
3431 my $pos = pos_last_openparen($rest);
3432 if ($pos >= 0) {
3433 $line =~ /^(\+| )([ \t]*)/;
3434 my $newindent = $2;
3435
3436 my $goodtabindent = $oldindent .
Tom Rinid2e30332020-06-16 10:29:46 -04003437 "\t" x ($pos / $tabsize) .
3438 " " x ($pos % $tabsize);
Kim Phillips9b7b8372013-02-28 12:53:52 +00003439 my $goodspaceindent = $oldindent . " " x $pos;
3440
3441 if ($newindent ne $goodtabindent &&
3442 $newindent ne $goodspaceindent) {
Tom Rinia50f8de2014-02-27 08:27:28 -05003443
3444 if (CHK("PARENTHESIS_ALIGNMENT",
3445 "Alignment should match open parenthesis\n" . $hereprev) &&
3446 $fix && $line =~ /^\+/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003447 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05003448 s/^\+[ \t]*/\+$goodtabindent/;
3449 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00003450 }
3451 }
3452 }
3453
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003454# check for space after cast like "(int) foo" or "(struct foo) bar"
3455# avoid checking a few false positives:
3456# "sizeof(<type>)" or "__alignof__(<type>)"
3457# function pointer declarations like "(*foo)(int) = bar;"
3458# structure definitions like "(struct foo) { 0 };"
3459# multiline macros that define functions
3460# known attributes or the __attribute__ keyword
3461 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3462 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05003463 if (CHK("SPACING",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003464 "No space is necessary after a cast\n" . $herecurr) &&
Tom Rinia50f8de2014-02-27 08:27:28 -05003465 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003466 $fixed[$fixlinenr] =~
3467 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Tom Rinia50f8de2014-02-27 08:27:28 -05003468 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00003469 }
3470
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003471# Block comment styles
3472# Networking with an initial /*
Kim Phillips9b7b8372013-02-28 12:53:52 +00003473 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Tom Rinia50f8de2014-02-27 08:27:28 -05003474 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003475 $rawline =~ /^\+[ \t]*\*/ &&
3476 $realline > 2) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00003477 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3478 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3479 }
3480
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003481# Block comments use * on subsequent lines
3482 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3483 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Tom Rinia50f8de2014-02-27 08:27:28 -05003484 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3485 $rawline =~ /^\+/ && #line is new
3486 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003487 WARN("BLOCK_COMMENT_STYLE",
3488 "Block comments use * on subsequent lines\n" . $hereprev);
Tom Rinia50f8de2014-02-27 08:27:28 -05003489 }
3490
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003491# Block comments use */ on trailing lines
3492 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Kim Phillips9b7b8372013-02-28 12:53:52 +00003493 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3494 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3495 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003496 WARN("BLOCK_COMMENT_STYLE",
3497 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Kim Phillips9b7b8372013-02-28 12:53:52 +00003498 }
3499
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003500# Block comment * alignment
3501 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3502 $line =~ /^\+[ \t]*$;/ && #leading comment
3503 $rawline =~ /^\+[ \t]*\*/ && #leading *
3504 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3505 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3506 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3507 my $oldindent;
3508 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3509 if (defined($1)) {
3510 $oldindent = expand_tabs($1);
3511 } else {
3512 $prevrawline =~ m@^\+(.*/?)\*@;
3513 $oldindent = expand_tabs($1);
3514 }
3515 $rawline =~ m@^\+([ \t]*)\*@;
3516 my $newindent = $1;
3517 $newindent = expand_tabs($newindent);
3518 if (length($oldindent) ne length($newindent)) {
3519 WARN("BLOCK_COMMENT_STYLE",
3520 "Block comments should align the * on each line\n" . $hereprev);
3521 }
3522 }
3523
3524# check for missing blank lines after struct/union declarations
3525# with exceptions for various attributes and macros
3526 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3527 $line =~ /^\+/ &&
3528 !($line =~ /^\+\s*$/ ||
3529 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3530 $line =~ /^\+\s*MODULE_/i ||
3531 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3532 $line =~ /^\+[a-z_]*init/ ||
3533 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3534 $line =~ /^\+\s*DECLARE/ ||
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003535 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003536 $line =~ /^\+\s*__setup/)) {
3537 if (CHK("LINE_SPACING",
3538 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3539 $fix) {
3540 fix_insert_line($fixlinenr, "\+");
3541 }
3542 }
3543
3544# check for multiple consecutive blank lines
3545 if ($prevline =~ /^[\+ ]\s*$/ &&
3546 $line =~ /^\+\s*$/ &&
3547 $last_blank_line != ($linenr - 1)) {
3548 if (CHK("LINE_SPACING",
3549 "Please don't use multiple blank lines\n" . $hereprev) &&
3550 $fix) {
3551 fix_delete_line($fixlinenr, $rawline);
3552 }
3553
3554 $last_blank_line = $linenr;
3555 }
3556
3557# check for missing blank lines after declarations
3558 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3559 # actual declarations
3560 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3561 # function pointer declarations
3562 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3563 # foo bar; where foo is some local typedef or #define
3564 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3565 # known declaration macros
3566 $prevline =~ /^\+\s+$declaration_macros/) &&
3567 # for "else if" which can look like "$Ident $Ident"
3568 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3569 # other possible extensions of declaration lines
3570 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3571 # not starting a section or a macro "\" extended line
3572 $prevline =~ /(?:\{\s*|\\)$/) &&
3573 # looks like a declaration
3574 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3575 # function pointer declarations
3576 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3577 # foo bar; where foo is some local typedef or #define
3578 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3579 # known declaration macros
3580 $sline =~ /^\+\s+$declaration_macros/ ||
3581 # start of struct or union or enum
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02003582 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003583 # start or end of block or continuation of declaration
3584 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3585 # bitfield continuation
3586 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3587 # other possible extensions of declaration lines
3588 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3589 # indentation of previous and current line are the same
3590 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3591 if (WARN("LINE_SPACING",
3592 "Missing a blank line after declarations\n" . $hereprev) &&
3593 $fix) {
3594 fix_insert_line($fixlinenr, "\+");
3595 }
3596 }
3597
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003598# check for spaces at the beginning of a line.
3599# Exceptions:
3600# 1) within comments
3601# 2) indented preprocessor commands
3602# 3) hanging labels
Tom Rinia50f8de2014-02-27 08:27:28 -05003603 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003604 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05003605 if (WARN("LEADING_SPACE",
3606 "please, no spaces at the start of a line\n" . $herevet) &&
3607 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003608 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rinia50f8de2014-02-27 08:27:28 -05003609 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003610 }
3611
3612# check we are in a valid C source file if not then ignore this hunk
3613 next if ($realfile !~ /\.(h|c)$/);
3614
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02003615# check for unusual line ending [ or (
3616 if ($line =~ /^\+.*([\[\(])\s*$/) {
3617 CHK("OPEN_ENDED_LINE",
3618 "Lines should not end with a '$1'\n" . $herecurr);
3619 }
3620
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003621# check if this appears to be the start function declaration, save the name
3622 if ($sline =~ /^\+\{\s*$/ &&
3623 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3624 $context_function = $1;
3625 }
3626
3627# check if this appears to be the end of function declaration
3628 if ($sline =~ /^\+\}\s*$/) {
3629 undef $context_function;
3630 }
3631
3632# check indentation of any line with a bare else
3633# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3634# if the previous line is a break or return and is indented 1 tab more...
3635 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3636 my $tabs = length($1) + 1;
3637 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3638 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3639 defined $lines[$linenr] &&
3640 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3641 WARN("UNNECESSARY_ELSE",
3642 "else is not generally useful after a break or return\n" . $hereprev);
3643 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00003644 }
3645
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003646# check indentation of a line with a break;
3647# if the previous line is a goto or return and is indented the same # of tabs
3648 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3649 my $tabs = $1;
3650 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3651 WARN("UNNECESSARY_BREAK",
3652 "break is not useful after a goto or return\n" . $hereprev);
3653 }
3654 }
3655
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003656# check for RCS/CVS revision markers
3657 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3658 WARN("CVS_KEYWORD",
3659 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3660 }
3661
Kim Phillips9b7b8372013-02-28 12:53:52 +00003662# check for old HOTPLUG __dev<foo> section markings
3663 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3664 WARN("HOTPLUG_SECTION",
3665 "Using $1 is unnecessary\n" . $herecurr);
3666 }
3667
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003668# Check for potential 'bare' types
3669 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3670 $realline_next);
Kim Phillips9b7b8372013-02-28 12:53:52 +00003671#print "LINE<$line>\n";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003672 if ($linenr > $suppress_statement &&
Tom Rinia50f8de2014-02-27 08:27:28 -05003673 $realcnt && $sline =~ /.\s*\S/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003674 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3675 ctx_statement_block($linenr, $realcnt, 0);
3676 $stat =~ s/\n./\n /g;
3677 $cond =~ s/\n./\n /g;
3678
Kim Phillips9b7b8372013-02-28 12:53:52 +00003679#print "linenr<$linenr> <$stat>\n";
3680 # If this statement has no statement boundaries within
3681 # it there is no point in retrying a statement scan
3682 # until we hit end of it.
3683 my $frag = $stat; $frag =~ s/;+\s*$//;
3684 if ($frag !~ /(?:{|;)/) {
3685#print "skip<$line_nr_next>\n";
3686 $suppress_statement = $line_nr_next;
3687 }
3688
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003689 # Find the real next line.
3690 $realline_next = $line_nr_next;
3691 if (defined $realline_next &&
3692 (!defined $lines[$realline_next - 1] ||
3693 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3694 $realline_next++;
3695 }
3696
3697 my $s = $stat;
3698 $s =~ s/{.*$//s;
3699
3700 # Ignore goto labels.
3701 if ($s =~ /$Ident:\*$/s) {
3702
3703 # Ignore functions being called
3704 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3705
3706 } elsif ($s =~ /^.\s*else\b/s) {
3707
3708 # declarations always start with types
3709 } 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) {
3710 my $type = $1;
3711 $type =~ s/\s+/ /g;
3712 possible($type, "A:" . $s);
3713
3714 # definitions in global scope can only start with types
3715 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3716 possible($1, "B:" . $s);
3717 }
3718
3719 # any (foo ... *) is a pointer cast, and foo is a type
3720 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3721 possible($1, "C:" . $s);
3722 }
3723
3724 # Check for any sort of function declaration.
3725 # int foo(something bar, other baz);
3726 # void (*store_gdt)(x86_descr_ptr *);
3727 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3728 my ($name_len) = length($1);
3729
3730 my $ctx = $s;
3731 substr($ctx, 0, $name_len + 1, '');
3732 $ctx =~ s/\)[^\)]*$//;
3733
3734 for my $arg (split(/\s*,\s*/, $ctx)) {
3735 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3736
3737 possible($1, "D:" . $s);
3738 }
3739 }
3740 }
3741
3742 }
3743
3744#
3745# Checks which may be anchored in the context.
3746#
3747
3748# Check for switch () and associated case and default
3749# statements should be at the same indent.
3750 if ($line=~/\bswitch\s*\(.*\)/) {
3751 my $err = '';
3752 my $sep = '';
3753 my @ctx = ctx_block_outer($linenr, $realcnt);
3754 shift(@ctx);
3755 for my $ctx (@ctx) {
3756 my ($clen, $cindent) = line_stats($ctx);
3757 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3758 $indent != $cindent) {
3759 $err .= "$sep$ctx\n";
3760 $sep = '';
3761 } else {
3762 $sep = "[...]\n";
3763 }
3764 }
3765 if ($err ne '') {
3766 ERROR("SWITCH_CASE_INDENT_LEVEL",
3767 "switch and case should be at the same indent\n$hereline$err");
3768 }
3769 }
3770
3771# if/while/etc brace do not go on next line, unless defining a do while loop,
3772# or if that brace on the next line is for something else
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003773 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003774 my $pre_ctx = "$1$2";
3775
3776 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Kim Phillips9b7b8372013-02-28 12:53:52 +00003777
3778 if ($line =~ /^\+\t{6,}/) {
3779 WARN("DEEP_INDENTATION",
3780 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3781 }
3782
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003783 my $ctx_cnt = $realcnt - $#ctx - 1;
3784 my $ctx = join("\n", @ctx);
3785
3786 my $ctx_ln = $linenr;
3787 my $ctx_skip = $realcnt;
3788
3789 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3790 defined $lines[$ctx_ln - 1] &&
3791 $lines[$ctx_ln - 1] =~ /^-/)) {
3792 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3793 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3794 $ctx_ln++;
3795 }
3796
3797 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3798 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3799
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003800 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003801 ERROR("OPEN_BRACE",
3802 "that open brace { should be on the previous line\n" .
3803 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3804 }
3805 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3806 $ctx =~ /\)\s*\;\s*$/ &&
3807 defined $lines[$ctx_ln - 1])
3808 {
3809 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3810 if ($nindent > $indent) {
3811 WARN("TRAILING_SEMICOLON",
3812 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3813 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3814 }
3815 }
3816 }
3817
3818# Check relative indent for conditionals and blocks.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003819 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00003820 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3821 ctx_statement_block($linenr, $realcnt, 0)
3822 if (!defined $stat);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003823 my ($s, $c) = ($stat, $cond);
3824
3825 substr($s, 0, length($c), '');
3826
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003827 # remove inline comments
3828 $s =~ s/$;/ /g;
3829 $c =~ s/$;/ /g;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003830
3831 # Find out how long the conditional actually is.
3832 my @newlines = ($c =~ /\n/gs);
3833 my $cond_lines = 1 + $#newlines;
3834
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003835 # Make sure we remove the line prefixes as we have
3836 # none on the first line, and are going to readd them
3837 # where necessary.
3838 $s =~ s/\n./\n/gs;
3839 while ($s =~ /\n\s+\\\n/) {
3840 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3841 }
3842
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003843 # We want to check the first line inside the block
3844 # starting at the end of the conditional, so remove:
3845 # 1) any blank line termination
3846 # 2) any opening brace { on end of the line
3847 # 3) any do (...) {
3848 my $continuation = 0;
3849 my $check = 0;
3850 $s =~ s/^.*\bdo\b//;
3851 $s =~ s/^\s*{//;
3852 if ($s =~ s/^\s*\\//) {
3853 $continuation = 1;
3854 }
3855 if ($s =~ s/^\s*?\n//) {
3856 $check = 1;
3857 $cond_lines++;
3858 }
3859
3860 # Also ignore a loop construct at the end of a
3861 # preprocessor statement.
3862 if (($prevline =~ /^.\s*#\s*define\s/ ||
3863 $prevline =~ /\\\s*$/) && $continuation == 0) {
3864 $check = 0;
3865 }
3866
3867 my $cond_ptr = -1;
3868 $continuation = 0;
3869 while ($cond_ptr != $cond_lines) {
3870 $cond_ptr = $cond_lines;
3871
3872 # If we see an #else/#elif then the code
3873 # is not linear.
3874 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3875 $check = 0;
3876 }
3877
3878 # Ignore:
3879 # 1) blank lines, they should be at 0,
3880 # 2) preprocessor lines, and
3881 # 3) labels.
3882 if ($continuation ||
3883 $s =~ /^\s*?\n/ ||
3884 $s =~ /^\s*#\s*?/ ||
3885 $s =~ /^\s*$Ident\s*:/) {
3886 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3887 if ($s =~ s/^.*?\n//) {
3888 $cond_lines++;
3889 }
3890 }
3891 }
3892
3893 my (undef, $sindent) = line_stats("+" . $s);
3894 my $stat_real = raw_line($linenr, $cond_lines);
3895
3896 # Check if either of these lines are modified, else
3897 # this is not this patch's fault.
3898 if (!defined($stat_real) ||
3899 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3900 $check = 0;
3901 }
3902 if (defined($stat_real) && $cond_lines > 1) {
3903 $stat_real = "[...]\n$stat_real";
3904 }
3905
3906 #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";
3907
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003908 if ($check && $s ne '' &&
Tom Rinid2e30332020-06-16 10:29:46 -04003909 (($sindent % $tabsize) != 0 ||
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003910 ($sindent < $indent) ||
3911 ($sindent == $indent &&
3912 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Tom Rinid2e30332020-06-16 10:29:46 -04003913 ($sindent > $indent + $tabsize))) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003914 WARN("SUSPECT_CODE_INDENT",
3915 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3916 }
3917 }
3918
3919 # Track the 'values' across context and added lines.
3920 my $opline = $line; $opline =~ s/^./ /;
3921 my ($curr_values, $curr_vars) =
3922 annotate_values($opline . "\n", $prev_values);
3923 $curr_values = $prev_values . $curr_values;
3924 if ($dbg_values) {
3925 my $outline = $opline; $outline =~ s/\t/ /g;
3926 print "$linenr > .$outline\n";
3927 print "$linenr > $curr_values\n";
3928 print "$linenr > $curr_vars\n";
3929 }
3930 $prev_values = substr($curr_values, -1);
3931
3932#ignore lines not being added
Tom Rinia50f8de2014-02-27 08:27:28 -05003933 next if ($line =~ /^[^\+]/);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003934
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003935# check for dereferences that span multiple lines
3936 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3937 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3938 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3939 my $ref = $1;
3940 $line =~ /^.\s*($Lval)/;
3941 $ref .= $1;
3942 $ref =~ s/\s//g;
3943 WARN("MULTILINE_DEREFERENCE",
3944 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3945 }
3946
3947# check for declarations of signed or unsigned without int
3948 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3949 my $type = $1;
3950 my $var = $2;
3951 $var = "" if (!defined $var);
3952 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3953 my $sign = $1;
3954 my $pointer = $2;
3955
3956 $pointer = "" if (!defined $pointer);
3957
3958 if (WARN("UNSPECIFIED_INT",
3959 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3960 $fix) {
3961 my $decl = trim($sign) . " int ";
3962 my $comp_pointer = $pointer;
3963 $comp_pointer =~ s/\s//g;
3964 $decl .= $comp_pointer;
3965 $decl = rtrim($decl) if ($var eq "");
3966 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3967 }
3968 }
3969 }
3970
Joe Hershbergerba2beb52011-10-18 10:06:59 +00003971# TEST: allow direct testing of the type matcher.
3972 if ($dbg_type) {
3973 if ($line =~ /^.\s*$Declare\s*$/) {
3974 ERROR("TEST_TYPE",
3975 "TEST: is type\n" . $herecurr);
3976 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3977 ERROR("TEST_NOT_TYPE",
3978 "TEST: is not type ($1 is)\n". $herecurr);
3979 }
3980 next;
3981 }
3982# TEST: allow direct testing of the attribute matcher.
3983 if ($dbg_attr) {
3984 if ($line =~ /^.\s*$Modifier\s*$/) {
3985 ERROR("TEST_ATTR",
3986 "TEST: is attr\n" . $herecurr);
3987 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3988 ERROR("TEST_NOT_ATTR",
3989 "TEST: is not attr ($1 is)\n". $herecurr);
3990 }
3991 next;
3992 }
3993
3994# check for initialisation to aggregates open brace on the next line
3995 if ($line =~ /^.\s*{/ &&
3996 $prevline =~ /(?:^|[^=])=\s*$/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02003997 if (ERROR("OPEN_BRACE",
3998 "that open brace { should be on the previous line\n" . $hereprev) &&
3999 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4000 fix_delete_line($fixlinenr - 1, $prevrawline);
4001 fix_delete_line($fixlinenr, $rawline);
4002 my $fixedline = $prevrawline;
4003 $fixedline =~ s/\s*=\s*$/ = {/;
4004 fix_insert_line($fixlinenr, $fixedline);
4005 $fixedline = $line;
4006 $fixedline =~ s/^(.\s*)\{\s*/$1/;
4007 fix_insert_line($fixlinenr, $fixedline);
4008 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004009 }
4010
4011#
4012# Checks which are anchored on the added line.
4013#
4014
4015# check for malformed paths in #include statements (uses RAW line)
4016 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4017 my $path = $1;
4018 if ($path =~ m{//}) {
4019 ERROR("MALFORMED_INCLUDE",
Kim Phillips9b7b8372013-02-28 12:53:52 +00004020 "malformed #include filename\n" . $herecurr);
4021 }
4022 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4023 ERROR("UAPI_INCLUDE",
4024 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004025 }
4026 }
4027
4028# no C99 // comments
4029 if ($line =~ m{//}) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004030 if (ERROR("C99_COMMENTS",
4031 "do not use C99 // comments\n" . $herecurr) &&
4032 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004033 my $line = $fixed[$fixlinenr];
Tom Rinia50f8de2014-02-27 08:27:28 -05004034 if ($line =~ /\/\/(.*)$/) {
4035 my $comment = trim($1);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004036 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Tom Rinia50f8de2014-02-27 08:27:28 -05004037 }
4038 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004039 }
4040 # Remove C99 comments.
4041 $line =~ s@//.*@@;
4042 $opline =~ s@//.*@@;
4043
4044# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4045# the whole statement.
4046#print "APW <$lines[$realline_next - 1]>\n";
4047 if (defined $realline_next &&
4048 exists $lines[$realline_next - 1] &&
4049 !defined $suppress_export{$realline_next} &&
4050 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4051 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4052 # Handle definitions which produce identifiers with
4053 # a prefix:
4054 # XXX(foo);
4055 # EXPORT_SYMBOL(something_foo);
4056 my $name = $1;
Kim Phillips9b7b8372013-02-28 12:53:52 +00004057 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004058 $name =~ /^${Ident}_$2/) {
4059#print "FOO C name<$name>\n";
4060 $suppress_export{$realline_next} = 1;
4061
4062 } elsif ($stat !~ /(?:
4063 \n.}\s*$|
4064 ^.DEFINE_$Ident\(\Q$name\E\)|
4065 ^.DECLARE_$Ident\(\Q$name\E\)|
4066 ^.LIST_HEAD\(\Q$name\E\)|
4067 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4068 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
4069 )/x) {
4070#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4071 $suppress_export{$realline_next} = 2;
4072 } else {
4073 $suppress_export{$realline_next} = 1;
4074 }
4075 }
4076 if (!defined $suppress_export{$linenr} &&
4077 $prevline =~ /^.\s*$/ &&
4078 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4079 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4080#print "FOO B <$lines[$linenr - 1]>\n";
4081 $suppress_export{$linenr} = 2;
4082 }
4083 if (defined $suppress_export{$linenr} &&
4084 $suppress_export{$linenr} == 2) {
4085 WARN("EXPORT_SYMBOL",
4086 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
4087 }
4088
4089# check for global initialisers.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004090 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004091 if (ERROR("GLOBAL_INITIALISERS",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004092 "do not initialise globals to $1\n" . $herecurr) &&
Tom Rinia50f8de2014-02-27 08:27:28 -05004093 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004094 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rinia50f8de2014-02-27 08:27:28 -05004095 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004096 }
4097# check for static initialisers.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004098 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004099 if (ERROR("INITIALISED_STATIC",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004100 "do not initialise statics to $1\n" .
Tom Rinia50f8de2014-02-27 08:27:28 -05004101 $herecurr) &&
4102 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004103 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rinia50f8de2014-02-27 08:27:28 -05004104 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004105 }
4106
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004107# check for misordered declarations of char/short/int/long with signed/unsigned
4108 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4109 my $tmp = trim($1);
4110 WARN("MISORDERED_TYPE",
4111 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4112 }
4113
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004114# check for unnecessary <signed> int declarations of short/long/long long
4115 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4116 my $type = trim($1);
4117 next if ($type !~ /\bint\b/);
4118 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4119 my $new_type = $type;
4120 $new_type =~ s/\b\s*int\s*\b/ /;
4121 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4122 $new_type =~ s/^const\s+//;
4123 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4124 $new_type = "const $new_type" if ($type =~ /^const\b/);
4125 $new_type =~ s/\s+/ /g;
4126 $new_type = trim($new_type);
4127 if (WARN("UNNECESSARY_INT",
4128 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4129 $fix) {
4130 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4131 }
4132 }
4133
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004134# check for static const char * arrays.
4135 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4136 WARN("STATIC_CONST_CHAR_ARRAY",
4137 "static const char * array should probably be static const char * const\n" .
4138 $herecurr);
Tom Rinid2e30332020-06-16 10:29:46 -04004139 }
4140
4141# check for initialized const char arrays that should be static const
4142 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4143 if (WARN("STATIC_CONST_CHAR_ARRAY",
4144 "const array should probably be static const\n" . $herecurr) &&
4145 $fix) {
4146 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4147 }
4148 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004149
4150# check for static char foo[] = "bar" declarations.
4151 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4152 WARN("STATIC_CONST_CHAR_ARRAY",
4153 "static char array declaration should probably be static const char\n" .
4154 $herecurr);
Tom Rinid2e30332020-06-16 10:29:46 -04004155 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004156
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004157# check for const <foo> const where <foo> is not a pointer or array type
4158 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4159 my $found = $1;
4160 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4161 WARN("CONST_CONST",
4162 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4163 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4164 WARN("CONST_CONST",
4165 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4166 }
4167 }
4168
4169# check for non-global char *foo[] = {"bar", ...} declarations.
4170 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4171 WARN("STATIC_CONST_CHAR_ARRAY",
4172 "char * array declaration might be better as static const\n" .
4173 $herecurr);
4174 }
4175
4176# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4177 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4178 my $array = $1;
4179 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4180 my $array_div = $1;
4181 if (WARN("ARRAY_SIZE",
4182 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4183 $fix) {
4184 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4185 }
4186 }
4187 }
4188
Tom Rinia50f8de2014-02-27 08:27:28 -05004189# check for function declarations without arguments like "int foo()"
Tom Rinid2e30332020-06-16 10:29:46 -04004190 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004191 if (ERROR("FUNCTION_WITHOUT_ARGS",
4192 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4193 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004194 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Tom Rinia50f8de2014-02-27 08:27:28 -05004195 }
4196 }
4197
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004198# check for new typedefs, only function parameters and sparse annotations
4199# make sense.
4200 if ($line =~ /\btypedef\s/ &&
4201 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4202 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
4203 $line !~ /\b$typeTypedefs\b/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004204 $line !~ /\b__bitwise\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004205 WARN("NEW_TYPEDEFS",
4206 "do not add new typedefs\n" . $herecurr);
4207 }
4208
4209# * goes on variable not on type
4210 # (char*[ const])
Kim Phillips9b7b8372013-02-28 12:53:52 +00004211 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4212 #print "AA<$1>\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05004213 my ($ident, $from, $to) = ($1, $2, $2);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004214
4215 # Should start with a space.
4216 $to =~ s/^(\S)/ $1/;
4217 # Should not end with a space.
4218 $to =~ s/\s+$//;
4219 # '*'s should not have spaces between.
4220 while ($to =~ s/\*\s+\*/\*\*/) {
4221 }
4222
Tom Rinia50f8de2014-02-27 08:27:28 -05004223## print "1: from<$from> to<$to> ident<$ident>\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004224 if ($from ne $to) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004225 if (ERROR("POINTER_LOCATION",
4226 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4227 $fix) {
4228 my $sub_from = $ident;
4229 my $sub_to = $ident;
4230 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004231 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004232 s@\Q$sub_from\E@$sub_to@;
4233 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004234 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00004235 }
4236 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4237 #print "BB<$1>\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05004238 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004239
4240 # Should start with a space.
4241 $to =~ s/^(\S)/ $1/;
4242 # Should not end with a space.
4243 $to =~ s/\s+$//;
4244 # '*'s should not have spaces between.
4245 while ($to =~ s/\*\s+\*/\*\*/) {
4246 }
4247 # Modifiers should have spaces.
4248 $to =~ s/(\b$Modifier$)/$1 /;
4249
Tom Rinia50f8de2014-02-27 08:27:28 -05004250## print "2: from<$from> to<$to> ident<$ident>\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004251 if ($from ne $to && $ident !~ /^$Modifier$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004252 if (ERROR("POINTER_LOCATION",
4253 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4254 $fix) {
4255
4256 my $sub_from = $match;
4257 my $sub_to = $match;
4258 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004259 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004260 s@\Q$sub_from\E@$sub_to@;
4261 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004262 }
4263 }
4264
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004265# avoid BUG() or BUG_ON()
4266 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004267 my $msg_level = \&WARN;
4268 $msg_level = \&CHK if ($file);
4269 &{$msg_level}("AVOID_BUG",
4270 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004271 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004272
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004273# avoid LINUX_VERSION_CODE
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004274 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4275 WARN("LINUX_VERSION_CODE",
4276 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4277 }
4278
4279# check for uses of printk_ratelimit
4280 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4281 WARN("PRINTK_RATELIMITED",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004282 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004283 }
4284
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004285# printk should use KERN_* levels
4286 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4287 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4288 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004289 }
4290
Kim Phillips9b7b8372013-02-28 12:53:52 +00004291 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4292 my $orig = $1;
4293 my $level = lc($orig);
4294 $level = "warn" if ($level eq "warning");
4295 my $level2 = $level;
4296 $level2 = "dbg" if ($level eq "debug");
4297 WARN("PREFER_PR_LEVEL",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004298 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Kim Phillips9b7b8372013-02-28 12:53:52 +00004299 }
4300
Kim Phillips9b7b8372013-02-28 12:53:52 +00004301 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4302 my $orig = $1;
4303 my $level = lc($orig);
4304 $level = "warn" if ($level eq "warning");
4305 $level = "dbg" if ($level eq "debug");
4306 WARN("PREFER_DEV_LEVEL",
4307 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4308 }
4309
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004310# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4311# number of false positives, but assembly files are not checked, so at
4312# least the arch entry code will not trigger this warning.
4313 if ($line =~ /\bENOSYS\b/) {
4314 WARN("ENOSYS",
4315 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4316 }
4317
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004318# function brace can't be on same line, except for #defines of do while,
4319# or if closed on same line
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004320 if ($perl_version_ok &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004321 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4322 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4323 $sline !~ /}/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004324 if (ERROR("OPEN_BRACE",
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004325 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004326 $fix) {
4327 fix_delete_line($fixlinenr, $rawline);
4328 my $fixed_line = $rawline;
4329 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4330 my $line1 = $1;
4331 my $line2 = $2;
4332 fix_insert_line($fixlinenr, ltrim($line1));
4333 fix_insert_line($fixlinenr, "\+{");
4334 if ($line2 !~ /^\s*$/) {
4335 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4336 }
4337 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004338 }
4339
4340# open braces for enum, union and struct go on the same line.
4341 if ($line =~ /^.\s*{/ &&
4342 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004343 if (ERROR("OPEN_BRACE",
4344 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4345 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4346 fix_delete_line($fixlinenr - 1, $prevrawline);
4347 fix_delete_line($fixlinenr, $rawline);
4348 my $fixedline = rtrim($prevrawline) . " {";
4349 fix_insert_line($fixlinenr, $fixedline);
4350 $fixedline = $rawline;
4351 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4352 if ($fixedline !~ /^\+\s*$/) {
4353 fix_insert_line($fixlinenr, $fixedline);
4354 }
4355 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004356 }
4357
4358# missing space after union, struct or enum definition
Tom Rinia50f8de2014-02-27 08:27:28 -05004359 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4360 if (WARN("SPACING",
4361 "missing space after $1 definition\n" . $herecurr) &&
4362 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004363 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004364 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4365 }
4366 }
4367
4368# Function pointer declarations
4369# check spacing between type, funcptr, and args
4370# canonical declaration is "type (*funcptr)(args...)"
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004371 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004372 my $declare = $1;
4373 my $pre_pointer_space = $2;
4374 my $post_pointer_space = $3;
4375 my $funcname = $4;
4376 my $post_funcname_space = $5;
4377 my $pre_args_space = $6;
4378
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004379# the $Declare variable will capture all spaces after the type
4380# so check it for a missing trailing missing space but pointer return types
4381# don't need a space so don't warn for those.
4382 my $post_declare_space = "";
4383 if ($declare =~ /(\s+)$/) {
4384 $post_declare_space = $1;
4385 $declare = rtrim($declare);
4386 }
4387 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004388 WARN("SPACING",
4389 "missing space after return type\n" . $herecurr);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004390 $post_declare_space = " ";
Tom Rinia50f8de2014-02-27 08:27:28 -05004391 }
4392
4393# unnecessary space "type (*funcptr)(args...)"
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004394# This test is not currently implemented because these declarations are
4395# equivalent to
4396# int foo(int bar, ...)
4397# and this is form shouldn't/doesn't generate a checkpatch warning.
4398#
4399# elsif ($declare =~ /\s{2,}$/) {
4400# WARN("SPACING",
4401# "Multiple spaces after return type\n" . $herecurr);
4402# }
Tom Rinia50f8de2014-02-27 08:27:28 -05004403
4404# unnecessary space "type ( *funcptr)(args...)"
4405 if (defined $pre_pointer_space &&
4406 $pre_pointer_space =~ /^\s/) {
4407 WARN("SPACING",
4408 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4409 }
4410
4411# unnecessary space "type (* funcptr)(args...)"
4412 if (defined $post_pointer_space &&
4413 $post_pointer_space =~ /^\s/) {
4414 WARN("SPACING",
4415 "Unnecessary space before function pointer name\n" . $herecurr);
4416 }
4417
4418# unnecessary space "type (*funcptr )(args...)"
4419 if (defined $post_funcname_space &&
4420 $post_funcname_space =~ /^\s/) {
4421 WARN("SPACING",
4422 "Unnecessary space after function pointer name\n" . $herecurr);
4423 }
4424
4425# unnecessary space "type (*funcptr) (args...)"
4426 if (defined $pre_args_space &&
4427 $pre_args_space =~ /^\s/) {
4428 WARN("SPACING",
4429 "Unnecessary space before function pointer arguments\n" . $herecurr);
4430 }
4431
4432 if (show_type("SPACING") && $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004433 $fixed[$fixlinenr] =~
4434 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Tom Rinia50f8de2014-02-27 08:27:28 -05004435 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004436 }
4437
4438# check for spacing round square brackets; allowed:
4439# 1. with a type on the left -- int [] a;
4440# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4441# 3. inside a curly brace -- = { [0...10] = 5 }
4442 while ($line =~ /(.*?\s)\[/g) {
4443 my ($where, $prefix) = ($-[1], $1);
4444 if ($prefix !~ /$Type\s+$/ &&
4445 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004446 $prefix !~ /[{,:]\s+$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004447 if (ERROR("BRACKET_SPACE",
4448 "space prohibited before open square bracket '['\n" . $herecurr) &&
4449 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004450 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004451 s/^(\+.*?)\s+\[/$1\[/;
4452 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004453 }
4454 }
4455
4456# check for spaces between functions and their parentheses.
4457 while ($line =~ /($Ident)\s+\(/g) {
4458 my $name = $1;
4459 my $ctx_before = substr($line, 0, $-[1]);
4460 my $ctx = "$ctx_before$name";
4461
4462 # Ignore those directives where spaces _are_ permitted.
4463 if ($name =~ /^(?:
4464 if|for|while|switch|return|case|
4465 volatile|__volatile__|
4466 __attribute__|format|__extension__|
4467 asm|__asm__)$/x)
4468 {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004469 # cpp #define statements have non-optional spaces, ie
4470 # if there is a space between the name and the open
4471 # parenthesis it is simply not a parameter group.
4472 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4473
4474 # cpp #elif statement condition may start with a (
4475 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4476
4477 # If this whole things ends with a type its most
4478 # likely a typedef for a function.
4479 } elsif ($ctx =~ /$Type$/) {
4480
4481 } else {
Tom Rinia50f8de2014-02-27 08:27:28 -05004482 if (WARN("SPACING",
4483 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4484 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004485 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004486 s/\b$name\s+\(/$name\(/;
4487 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004488 }
4489 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00004490
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004491# Check operator spacing.
4492 if (!($line=~/\#\s*include/)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004493 my $fixed_line = "";
4494 my $line_fixed = 0;
4495
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004496 my $ops = qr{
4497 <<=|>>=|<=|>=|==|!=|
4498 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4499 =>|->|<<|>>|<|>|=|!|~|
4500 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Tom Rinia50f8de2014-02-27 08:27:28 -05004501 \?:|\?|:
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004502 }x;
4503 my @elements = split(/($ops|;)/, $opline);
Tom Rinia50f8de2014-02-27 08:27:28 -05004504
4505## print("element count: <" . $#elements . ">\n");
4506## foreach my $el (@elements) {
4507## print("el: <$el>\n");
4508## }
4509
4510 my @fix_elements = ();
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004511 my $off = 0;
4512
Tom Rinia50f8de2014-02-27 08:27:28 -05004513 foreach my $el (@elements) {
4514 push(@fix_elements, substr($rawline, $off, length($el)));
4515 $off += length($el);
4516 }
4517
4518 $off = 0;
4519
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004520 my $blank = copy_spacing($opline);
Tom Rinia50f8de2014-02-27 08:27:28 -05004521 my $last_after = -1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004522
4523 for (my $n = 0; $n < $#elements; $n += 2) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004524
4525 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4526
4527## print("n: <$n> good: <$good>\n");
4528
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004529 $off += length($elements[$n]);
4530
4531 # Pick up the preceding and succeeding characters.
4532 my $ca = substr($opline, 0, $off);
4533 my $cc = '';
4534 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4535 $cc = substr($opline, $off + length($elements[$n + 1]));
4536 }
4537 my $cb = "$ca$;$cc";
4538
4539 my $a = '';
4540 $a = 'V' if ($elements[$n] ne '');
4541 $a = 'W' if ($elements[$n] =~ /\s$/);
4542 $a = 'C' if ($elements[$n] =~ /$;$/);
4543 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4544 $a = 'O' if ($elements[$n] eq '');
4545 $a = 'E' if ($ca =~ /^\s*$/);
4546
4547 my $op = $elements[$n + 1];
4548
4549 my $c = '';
4550 if (defined $elements[$n + 2]) {
4551 $c = 'V' if ($elements[$n + 2] ne '');
4552 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4553 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4554 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4555 $c = 'O' if ($elements[$n + 2] eq '');
4556 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4557 } else {
4558 $c = 'E';
4559 }
4560
4561 my $ctx = "${a}x${c}";
4562
4563 my $at = "(ctx:$ctx)";
4564
4565 my $ptr = substr($blank, 0, $off) . "^";
4566 my $hereptr = "$hereline$ptr\n";
4567
4568 # Pull out the value of this operator.
4569 my $op_type = substr($curr_values, $off + 1, 1);
4570
4571 # Get the full operator variant.
4572 my $opv = $op . substr($curr_vars, $off, 1);
4573
4574 # Ignore operators passed as parameters.
4575 if ($op_type ne 'V' &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004576 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004577
4578# # Ignore comments
4579# } elsif ($op =~ /^$;+$/) {
4580
4581 # ; should have either the end of line or a space or \ after it
4582 } elsif ($op eq ';') {
4583 if ($ctx !~ /.x[WEBC]/ &&
4584 $cc !~ /^\\/ && $cc !~ /^;/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004585 if (ERROR("SPACING",
4586 "space required after that '$op' $at\n" . $hereptr)) {
4587 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4588 $line_fixed = 1;
4589 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004590 }
4591
4592 # // is a comment
4593 } elsif ($op eq '//') {
4594
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004595 # : when part of a bitfield
4596 } elsif ($opv eq ':B') {
4597 # skip the bitfield test for now
4598
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004599 # No spaces for:
4600 # ->
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004601 } elsif ($op eq '->') {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004602 if ($ctx =~ /Wx.|.xW/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004603 if (ERROR("SPACING",
4604 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4605 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4606 if (defined $fix_elements[$n + 2]) {
4607 $fix_elements[$n + 2] =~ s/^\s+//;
4608 }
4609 $line_fixed = 1;
4610 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004611 }
4612
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004613 # , must not have a space before and must have a space on the right.
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004614 } elsif ($op eq ',') {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004615 my $rtrim_before = 0;
4616 my $space_after = 0;
4617 if ($ctx =~ /Wx./) {
4618 if (ERROR("SPACING",
4619 "space prohibited before that '$op' $at\n" . $hereptr)) {
4620 $line_fixed = 1;
4621 $rtrim_before = 1;
4622 }
4623 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004624 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004625 if (ERROR("SPACING",
4626 "space required after that '$op' $at\n" . $hereptr)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004627 $line_fixed = 1;
4628 $last_after = $n;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004629 $space_after = 1;
Tom Rinia50f8de2014-02-27 08:27:28 -05004630 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004631 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004632 if ($rtrim_before || $space_after) {
4633 if ($rtrim_before) {
4634 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4635 } else {
4636 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4637 }
4638 if ($space_after) {
4639 $good .= " ";
4640 }
4641 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004642
4643 # '*' as part of a type definition -- reported already.
4644 } elsif ($opv eq '*_') {
4645 #warn "'*' is part of type\n";
4646
4647 # unary operators should have a space before and
4648 # none after. May be left adjacent to another
4649 # unary operator, or a cast
4650 } elsif ($op eq '!' || $op eq '~' ||
4651 $opv eq '*U' || $opv eq '-U' ||
4652 $opv eq '&U' || $opv eq '&&U') {
4653 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004654 if (ERROR("SPACING",
4655 "space required before that '$op' $at\n" . $hereptr)) {
4656 if ($n != $last_after + 2) {
4657 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4658 $line_fixed = 1;
4659 }
4660 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004661 }
4662 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4663 # A unary '*' may be const
4664
4665 } elsif ($ctx =~ /.xW/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004666 if (ERROR("SPACING",
4667 "space prohibited after that '$op' $at\n" . $hereptr)) {
4668 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4669 if (defined $fix_elements[$n + 2]) {
4670 $fix_elements[$n + 2] =~ s/^\s+//;
4671 }
4672 $line_fixed = 1;
4673 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004674 }
4675
4676 # unary ++ and unary -- are allowed no space on one side.
4677 } elsif ($op eq '++' or $op eq '--') {
4678 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004679 if (ERROR("SPACING",
4680 "space required one side of that '$op' $at\n" . $hereptr)) {
4681 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4682 $line_fixed = 1;
4683 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004684 }
4685 if ($ctx =~ /Wx[BE]/ ||
4686 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004687 if (ERROR("SPACING",
4688 "space prohibited before that '$op' $at\n" . $hereptr)) {
4689 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4690 $line_fixed = 1;
4691 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004692 }
4693 if ($ctx =~ /ExW/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004694 if (ERROR("SPACING",
4695 "space prohibited after that '$op' $at\n" . $hereptr)) {
4696 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4697 if (defined $fix_elements[$n + 2]) {
4698 $fix_elements[$n + 2] =~ s/^\s+//;
4699 }
4700 $line_fixed = 1;
4701 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004702 }
4703
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004704 # << and >> may either have or not have spaces both sides
4705 } elsif ($op eq '<<' or $op eq '>>' or
4706 $op eq '&' or $op eq '^' or $op eq '|' or
4707 $op eq '+' or $op eq '-' or
4708 $op eq '*' or $op eq '/' or
4709 $op eq '%')
4710 {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004711 if ($check) {
4712 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4713 if (CHK("SPACING",
4714 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4715 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4716 $fix_elements[$n + 2] =~ s/^\s+//;
4717 $line_fixed = 1;
4718 }
4719 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4720 if (CHK("SPACING",
4721 "space preferred before that '$op' $at\n" . $hereptr)) {
4722 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4723 $line_fixed = 1;
4724 }
4725 }
4726 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004727 if (ERROR("SPACING",
4728 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4729 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4730 if (defined $fix_elements[$n + 2]) {
4731 $fix_elements[$n + 2] =~ s/^\s+//;
4732 }
4733 $line_fixed = 1;
4734 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004735 }
4736
4737 # A colon needs no spaces before when it is
4738 # terminating a case value or a label.
4739 } elsif ($opv eq ':C' || $opv eq ':L') {
4740 if ($ctx =~ /Wx./) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004741 if (ERROR("SPACING",
4742 "space prohibited before that '$op' $at\n" . $hereptr)) {
4743 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4744 $line_fixed = 1;
4745 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004746 }
4747
4748 # All the others need spaces both sides.
4749 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4750 my $ok = 0;
4751
4752 # Ignore email addresses <foo@bar>
4753 if (($op eq '<' &&
4754 $cc =~ /^\S+\@\S+>/) ||
4755 ($op eq '>' &&
4756 $ca =~ /<\S+\@\S+$/))
4757 {
Tom Rinid2e30332020-06-16 10:29:46 -04004758 $ok = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004759 }
4760
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004761 # for asm volatile statements
4762 # ignore a colon with another
4763 # colon immediately before or after
4764 if (($op eq ':') &&
4765 ($ca =~ /:$/ || $cc =~ /^:/)) {
4766 $ok = 1;
4767 }
4768
Tom Rinia50f8de2014-02-27 08:27:28 -05004769 # messages are ERROR, but ?: are CHK
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004770 if ($ok == 0) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004771 my $msg_level = \&ERROR;
4772 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Tom Rinia50f8de2014-02-27 08:27:28 -05004773
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004774 if (&{$msg_level}("SPACING",
4775 "spaces required around that '$op' $at\n" . $hereptr)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004776 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4777 if (defined $fix_elements[$n + 2]) {
4778 $fix_elements[$n + 2] =~ s/^\s+//;
4779 }
4780 $line_fixed = 1;
4781 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004782 }
4783 }
4784 $off += length($elements[$n + 1]);
Tom Rinia50f8de2014-02-27 08:27:28 -05004785
4786## print("n: <$n> GOOD: <$good>\n");
4787
4788 $fixed_line = $fixed_line . $good;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004789 }
Tom Rinia50f8de2014-02-27 08:27:28 -05004790
4791 if (($#elements % 2) == 0) {
4792 $fixed_line = $fixed_line . $fix_elements[$#elements];
4793 }
4794
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004795 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4796 $fixed[$fixlinenr] = $fixed_line;
Tom Rinia50f8de2014-02-27 08:27:28 -05004797 }
4798
4799
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004800 }
4801
Tom Rinia50f8de2014-02-27 08:27:28 -05004802# check for whitespace before a non-naked semicolon
4803 if ($line =~ /^\+.*\S\s+;\s*$/) {
4804 if (WARN("SPACING",
4805 "space prohibited before semicolon\n" . $herecurr) &&
4806 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004807 1 while $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004808 s/^(\+.*\S)\s+;/$1;/;
4809 }
4810 }
4811
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004812# check for multiple assignments
4813 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4814 CHK("MULTIPLE_ASSIGNMENTS",
4815 "multiple assignments should be avoided\n" . $herecurr);
4816 }
4817
4818## # check for multiple declarations, allowing for a function declaration
4819## # continuation.
4820## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4821## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4822##
4823## # Remove any bracketed sections to ensure we do not
4824## # falsly report the parameters of functions.
4825## my $ln = $line;
4826## while ($ln =~ s/\([^\(\)]*\)//g) {
4827## }
4828## if ($ln =~ /,/) {
4829## WARN("MULTIPLE_DECLARATION",
4830## "declaring multiple variables together should be avoided\n" . $herecurr);
4831## }
4832## }
4833
4834#need space before brace following if, while, etc
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004835 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004836 $line =~ /\b(?:else|do)\{/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004837 if (ERROR("SPACING",
4838 "space required before the open brace '{'\n" . $herecurr) &&
4839 $fix) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004840 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Tom Rinia50f8de2014-02-27 08:27:28 -05004841 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004842 }
4843
Tom Rinia50f8de2014-02-27 08:27:28 -05004844## # check for blank lines before declarations
4845## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4846## $prevrawline =~ /^.\s*$/) {
4847## WARN("SPACING",
4848## "No blank lines before declarations\n" . $hereprev);
4849## }
4850##
4851
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004852# closing brace should have a space following it when it has anything
4853# on the line
Tom Rinid2e30332020-06-16 10:29:46 -04004854 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004855 if (ERROR("SPACING",
4856 "space required after that close brace '}'\n" . $herecurr) &&
4857 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004858 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004859 s/}((?!(?:,|;|\)))\S)/} $1/;
4860 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004861 }
4862
4863# check spacing on square brackets
4864 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004865 if (ERROR("SPACING",
4866 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4867 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004868 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004869 s/\[\s+/\[/;
4870 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004871 }
4872 if ($line =~ /\s\]/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004873 if (ERROR("SPACING",
4874 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4875 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004876 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004877 s/\s+\]/\]/;
4878 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004879 }
4880
4881# check spacing on parentheses
4882 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4883 $line !~ /for\s*\(\s+;/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004884 if (ERROR("SPACING",
4885 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4886 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004887 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004888 s/\(\s+/\(/;
4889 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004890 }
4891 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4892 $line !~ /for\s*\(.*;\s+\)/ &&
4893 $line !~ /:\s+\)/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004894 if (ERROR("SPACING",
4895 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4896 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004897 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004898 s/\s+\)/\)/;
4899 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004900 }
4901
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004902# check unnecessary parentheses around addressof/dereference single $Lvals
4903# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4904
4905 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4906 my $var = $1;
4907 if (CHK("UNNECESSARY_PARENTHESES",
4908 "Unnecessary parentheses around $var\n" . $herecurr) &&
4909 $fix) {
4910 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4911 }
4912 }
4913
4914# check for unnecessary parentheses around function pointer uses
4915# ie: (foo->bar)(); should be foo->bar();
4916# but not "if (foo->bar) (" to avoid some false positives
4917 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4918 my $var = $2;
4919 if (CHK("UNNECESSARY_PARENTHESES",
4920 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4921 $fix) {
4922 my $var2 = deparenthesize($var);
4923 $var2 =~ s/\s//g;
4924 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4925 }
4926 }
4927
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004928# check for unnecessary parentheses around comparisons in if uses
4929# when !drivers/staging or command-line uses --strict
4930 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004931 $perl_version_ok && defined($stat) &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02004932 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4933 my $if_stat = $1;
4934 my $test = substr($2, 1, -1);
4935 my $herectx;
4936 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4937 my $match = $1;
4938 # avoid parentheses around potential macro args
4939 next if ($match =~ /^\s*\w+\s*$/);
4940 if (!defined($herectx)) {
4941 $herectx = $here . "\n";
4942 my $cnt = statement_rawlines($if_stat);
4943 for (my $n = 0; $n < $cnt; $n++) {
4944 my $rl = raw_line($linenr, $n);
4945 $herectx .= $rl . "\n";
4946 last if $rl =~ /^[ \+].*\{/;
4947 }
4948 }
4949 CHK("UNNECESSARY_PARENTHESES",
4950 "Unnecessary parentheses around '$match'\n" . $herectx);
4951 }
4952 }
4953
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004954#goto labels aren't indented, allow a single space however
4955 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4956 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05004957 if (WARN("INDENTED_LABEL",
4958 "labels should not be indented\n" . $herecurr) &&
4959 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004960 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05004961 s/^(.)\s+/$1/;
4962 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004963 }
4964
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004965# return is not a function
Tom Rinia50f8de2014-02-27 08:27:28 -05004966 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004967 my $spacing = $1;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004968 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004969 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4970 my $value = $1;
4971 $value = deparenthesize($value);
4972 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4973 ERROR("RETURN_PARENTHESES",
4974 "return is not a function, parentheses are not required\n" . $herecurr);
4975 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00004976 } elsif ($spacing !~ /\s+/) {
4977 ERROR("SPACING",
4978 "space required before the open parenthesis '('\n" . $herecurr);
4979 }
4980 }
Tom Rinia50f8de2014-02-27 08:27:28 -05004981
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02004982# unnecessary return in a void function
4983# at end-of-function, with the previous line a single leading tab, then return;
4984# and the line before that not a goto label target like "out:"
4985 if ($sline =~ /^[ \+]}\s*$/ &&
4986 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4987 $linenr >= 3 &&
4988 $lines[$linenr - 3] =~ /^[ +]/ &&
4989 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4990 WARN("RETURN_VOID",
4991 "void function return statements are not generally useful\n" . $hereprev);
4992 }
4993
Tom Rinia50f8de2014-02-27 08:27:28 -05004994# if statements using unnecessary parentheses - ie: if ((foo == bar))
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02004995 if ($perl_version_ok &&
Tom Rinia50f8de2014-02-27 08:27:28 -05004996 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4997 my $openparens = $1;
4998 my $count = $openparens =~ tr@\(@\(@;
4999 my $msg = "";
5000 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5001 my $comp = $4; #Not $1 because of $LvalOrFunc
5002 $msg = " - maybe == should be = ?" if ($comp eq "==");
5003 WARN("UNNECESSARY_PARENTHESES",
5004 "Unnecessary parentheses$msg\n" . $herecurr);
5005 }
5006 }
5007
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005008# comparisons with a constant or upper case identifier on the left
5009# avoid cases like "foo + BAR < baz"
5010# only fix matches surrounded by parentheses to avoid incorrect
5011# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005012 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005013 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5014 my $lead = $1;
5015 my $const = $2;
5016 my $comp = $3;
5017 my $to = $4;
5018 my $newcomp = $comp;
5019 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5020 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5021 WARN("CONSTANT_COMPARISON",
5022 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5023 $fix) {
5024 if ($comp eq "<") {
5025 $newcomp = ">";
5026 } elsif ($comp eq "<=") {
5027 $newcomp = ">=";
5028 } elsif ($comp eq ">") {
5029 $newcomp = "<";
5030 } elsif ($comp eq ">=") {
5031 $newcomp = "<=";
5032 }
5033 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5034 }
5035 }
5036
5037# Return of what appears to be an errno should normally be negative
5038 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005039 my $name = $1;
5040 if ($name ne 'EOF' && $name ne 'ERROR') {
5041 WARN("USE_NEGATIVE_ERRNO",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005042 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005043 }
5044 }
5045
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005046# Need a space before open parenthesis after if, while etc
Tom Rinia50f8de2014-02-27 08:27:28 -05005047 if ($line =~ /\b(if|while|for|switch)\(/) {
5048 if (ERROR("SPACING",
5049 "space required before the open parenthesis '('\n" . $herecurr) &&
5050 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005051 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05005052 s/\b(if|while|for|switch)\(/$1 \(/;
5053 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005054 }
5055
5056# Check for illegal assignment in if conditional -- and check for trailing
5057# statements after the conditional.
5058 if ($line =~ /do\s*(?!{)/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00005059 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5060 ctx_statement_block($linenr, $realcnt, 0)
5061 if (!defined $stat);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005062 my ($stat_next) = ctx_statement_block($line_nr_next,
5063 $remain_next, $off_next);
5064 $stat_next =~ s/\n./\n /g;
5065 ##print "stat<$stat> stat_next<$stat_next>\n";
5066
5067 if ($stat_next =~ /^\s*while\b/) {
5068 # If the statement carries leading newlines,
5069 # then count those as offsets.
5070 my ($whitespace) =
5071 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5072 my $offset =
5073 statement_rawlines($whitespace) - 1;
5074
5075 $suppress_whiletrailers{$line_nr_next +
5076 $offset} = 1;
5077 }
5078 }
5079 if (!defined $suppress_whiletrailers{$linenr} &&
Tom Rinia50f8de2014-02-27 08:27:28 -05005080 defined($stat) && defined($cond) &&
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005081 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5082 my ($s, $c) = ($stat, $cond);
5083
5084 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
5085 ERROR("ASSIGN_IN_IF",
5086 "do not use assignment in if condition\n" . $herecurr);
5087 }
5088
5089 # Find out what is on the end of the line after the
5090 # conditional.
5091 substr($s, 0, length($c), '');
5092 $s =~ s/\n.*//g;
Tom Rinid2e30332020-06-16 10:29:46 -04005093 $s =~ s/$;//g; # Remove any comments
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005094 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5095 $c !~ /}\s*while\s*/)
5096 {
5097 # Find out how long the conditional actually is.
5098 my @newlines = ($c =~ /\n/gs);
5099 my $cond_lines = 1 + $#newlines;
5100 my $stat_real = '';
5101
5102 $stat_real = raw_line($linenr, $cond_lines)
5103 . "\n" if ($cond_lines);
5104 if (defined($stat_real) && $cond_lines > 1) {
5105 $stat_real = "[...]\n$stat_real";
5106 }
5107
5108 ERROR("TRAILING_STATEMENTS",
5109 "trailing statements should be on next line\n" . $herecurr . $stat_real);
5110 }
5111 }
5112
5113# Check for bitwise tests written as boolean
5114 if ($line =~ /
5115 (?:
5116 (?:\[|\(|\&\&|\|\|)
5117 \s*0[xX][0-9]+\s*
5118 (?:\&\&|\|\|)
5119 |
5120 (?:\&\&|\|\|)
5121 \s*0[xX][0-9]+\s*
5122 (?:\&\&|\|\||\)|\])
5123 )/x)
5124 {
5125 WARN("HEXADECIMAL_BOOLEAN_TEST",
5126 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
5127 }
5128
5129# if and else should not have general statements after it
5130 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5131 my $s = $1;
Tom Rinid2e30332020-06-16 10:29:46 -04005132 $s =~ s/$;//g; # Remove any comments
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005133 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5134 ERROR("TRAILING_STATEMENTS",
5135 "trailing statements should be on next line\n" . $herecurr);
5136 }
5137 }
5138# if should not continue a brace
5139 if ($line =~ /}\s*if\b/) {
5140 ERROR("TRAILING_STATEMENTS",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005141 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005142 $herecurr);
5143 }
5144# case and default should not have general statements after them
5145 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5146 $line !~ /\G(?:
5147 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5148 \s*return\s+
5149 )/xg)
5150 {
5151 ERROR("TRAILING_STATEMENTS",
5152 "trailing statements should be on next line\n" . $herecurr);
5153 }
5154
5155 # Check for }<nl>else {, these must be at the same
5156 # indent level to be relevant to each other.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005157 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5158 $previndent == $indent) {
5159 if (ERROR("ELSE_AFTER_BRACE",
5160 "else should follow close brace '}'\n" . $hereprev) &&
5161 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5162 fix_delete_line($fixlinenr - 1, $prevrawline);
5163 fix_delete_line($fixlinenr, $rawline);
5164 my $fixedline = $prevrawline;
5165 $fixedline =~ s/}\s*$//;
5166 if ($fixedline !~ /^\+\s*$/) {
5167 fix_insert_line($fixlinenr, $fixedline);
5168 }
5169 $fixedline = $rawline;
5170 $fixedline =~ s/^(.\s*)else/$1} else/;
5171 fix_insert_line($fixlinenr, $fixedline);
5172 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005173 }
5174
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005175 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5176 $previndent == $indent) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005177 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5178
5179 # Find out what is on the end of the line after the
5180 # conditional.
5181 substr($s, 0, length($c), '');
5182 $s =~ s/\n.*//g;
5183
5184 if ($s =~ /^\s*;/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005185 if (ERROR("WHILE_AFTER_BRACE",
5186 "while should follow close brace '}'\n" . $hereprev) &&
5187 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5188 fix_delete_line($fixlinenr - 1, $prevrawline);
5189 fix_delete_line($fixlinenr, $rawline);
5190 my $fixedline = $prevrawline;
5191 my $trailing = $rawline;
5192 $trailing =~ s/^\+//;
5193 $trailing = trim($trailing);
5194 $fixedline =~ s/}\s*$/} $trailing/;
5195 fix_insert_line($fixlinenr, $fixedline);
5196 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005197 }
5198 }
5199
Tom Rinia50f8de2014-02-27 08:27:28 -05005200#Specific variable tests
Kim Phillips9b7b8372013-02-28 12:53:52 +00005201 while ($line =~ m{($Constant|$Lval)}g) {
5202 my $var = $1;
Tom Rinia50f8de2014-02-27 08:27:28 -05005203
Tom Rinia50f8de2014-02-27 08:27:28 -05005204#CamelCase
5205 if ($var !~ /^$Constant$/ &&
5206 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
5207#Ignore Page<foo> variants
5208 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Tom Rinid2e30332020-06-16 10:29:46 -04005209#Ignore SI style variants like nS, mV and dB
5210#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5211 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005212#Ignore some three character SI units explicitly, like MiB and KHz
5213 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05005214 while ($var =~ m{($Ident)}g) {
5215 my $word = $1;
5216 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5217 if ($check) {
5218 seed_camelcase_includes();
5219 if (!$file && !$camelcase_file_seeded) {
5220 seed_camelcase_file($realfile);
5221 $camelcase_file_seeded = 1;
5222 }
5223 }
5224 if (!defined $camelcase{$word}) {
5225 $camelcase{$word} = 1;
5226 CHK("CAMELCASE",
5227 "Avoid CamelCase: <$word>\n" . $herecurr);
5228 }
5229 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005230 }
5231 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005232
5233#no spaces allowed after \ in define
Tom Rinia50f8de2014-02-27 08:27:28 -05005234 if ($line =~ /\#\s*define.*\\\s+$/) {
5235 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5236 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5237 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005238 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rinia50f8de2014-02-27 08:27:28 -05005239 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005240 }
5241
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005242# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5243# itself <asm/foo.h> (uses RAW line)
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005244 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5245 my $file = "$1.h";
5246 my $checkfile = "include/linux/$file";
5247 if (-f "$root/$checkfile" &&
5248 $realfile ne $checkfile &&
5249 $1 !~ /$allowed_asm_includes/)
5250 {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005251 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5252 if ($asminclude > 0) {
5253 if ($realfile =~ m{^arch/}) {
5254 CHK("ARCH_INCLUDE_LINUX",
5255 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5256 } else {
5257 WARN("INCLUDE_LINUX",
5258 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5259 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005260 }
5261 }
5262 }
5263
5264# multi-statement macros should be enclosed in a do while loop, grab the
5265# first statement and ensure its the whole macro if its not enclosed
5266# in a known good container
5267 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5268 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5269 my $ln = $linenr;
5270 my $cnt = $realcnt;
5271 my ($off, $dstat, $dcond, $rest);
5272 my $ctx = '';
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005273 my $has_flow_statement = 0;
5274 my $has_arg_concat = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005275 ($dstat, $dcond, $ln, $cnt, $off) =
Kim Phillips9b7b8372013-02-28 12:53:52 +00005276 ctx_statement_block($linenr, $realcnt, 0);
5277 $ctx = $dstat;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005278 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5279 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5280
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005281 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5282 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5283
5284 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5285 my $define_args = $1;
5286 my $define_stmt = $dstat;
5287 my @def_args = ();
5288
5289 if (defined $define_args && $define_args ne "") {
5290 $define_args = substr($define_args, 1, length($define_args) - 2);
5291 $define_args =~ s/\s*//g;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005292 $define_args =~ s/\\\+?//g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005293 @def_args = split(",", $define_args);
5294 }
5295
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005296 $dstat =~ s/$;//g;
5297 $dstat =~ s/\\\n.//g;
5298 $dstat =~ s/^\s*//s;
5299 $dstat =~ s/\s*$//s;
5300
5301 # Flatten any parentheses and braces
5302 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5303 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005304 $dstat =~ s/.\[[^\[\]]*\]/1/)
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005305 {
5306 }
5307
Tom Rinid2e30332020-06-16 10:29:46 -04005308 # Flatten any obvious string concatenation.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005309 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5310 $dstat =~ s/$Ident\s*($String)/$1/)
Kim Phillips9b7b8372013-02-28 12:53:52 +00005311 {
5312 }
5313
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005314 # Make asm volatile uses seem like a generic function
5315 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5316
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005317 my $exceptions = qr{
5318 $Declare|
5319 module_param_named|
Kim Phillips9b7b8372013-02-28 12:53:52 +00005320 MODULE_PARM_DESC|
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005321 DECLARE_PER_CPU|
5322 DEFINE_PER_CPU|
5323 __typeof__\(|
5324 union|
5325 struct|
5326 \.$Ident\s*=\s*|
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005327 ^\"|\"$|
5328 ^\[
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005329 }x;
5330 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005331
5332 $ctx =~ s/\n*$//;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005333 my $stmt_cnt = statement_rawlines($ctx);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005334 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005335
Kim Phillips9b7b8372013-02-28 12:53:52 +00005336 if ($dstat ne '' &&
5337 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5338 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Tom Rinia50f8de2014-02-27 08:27:28 -05005339 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005340 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Kim Phillips9b7b8372013-02-28 12:53:52 +00005341 $dstat !~ /$exceptions/ &&
5342 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Tom Rinia50f8de2014-02-27 08:27:28 -05005343 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Kim Phillips9b7b8372013-02-28 12:53:52 +00005344 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
5345 $dstat !~ /^for\s*$Constant$/ && # for (...)
5346 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5347 $dstat !~ /^do\s*{/ && # do {...
Heiko Schochere65be982016-01-04 09:17:19 +01005348 $dstat !~ /^\(\{/ && # ({...
Tom Rinia50f8de2014-02-27 08:27:28 -05005349 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Kim Phillips9b7b8372013-02-28 12:53:52 +00005350 {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005351 if ($dstat =~ /^\s*if\b/) {
5352 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5353 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5354 } elsif ($dstat =~ /;/) {
5355 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5356 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5357 } else {
5358 ERROR("COMPLEX_MACRO",
5359 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5360 }
5361
5362 }
5363
5364 # Make $define_stmt single line, comment-free, etc
5365 my @stmt_array = split('\n', $define_stmt);
5366 my $first = 1;
5367 $define_stmt = "";
5368 foreach my $l (@stmt_array) {
5369 $l =~ s/\\$//;
5370 if ($first) {
5371 $define_stmt = $l;
5372 $first = 0;
5373 } elsif ($l =~ /^[\+ ]/) {
5374 $define_stmt .= substr($l, 1);
5375 }
5376 }
5377 $define_stmt =~ s/$;//g;
5378 $define_stmt =~ s/\s+/ /g;
5379 $define_stmt = trim($define_stmt);
5380
5381# check if any macro arguments are reused (ignore '...' and 'type')
5382 foreach my $arg (@def_args) {
5383 next if ($arg =~ /\.\.\./);
5384 next if ($arg =~ /^type$/i);
5385 my $tmp_stmt = $define_stmt;
Tom Rinid2e30332020-06-16 10:29:46 -04005386 $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005387 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5388 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005389 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005390 if ($use_cnt > 1) {
5391 CHK("MACRO_ARG_REUSE",
5392 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5393 }
5394# check if any macro arguments may have other precedence issues
5395 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5396 ((defined($1) && $1 ne ',') ||
5397 (defined($2) && $2 ne ','))) {
5398 CHK("MACRO_ARG_PRECEDENCE",
5399 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5400 }
5401 }
5402
5403# check for macros with flow control, but without ## concatenation
5404# ## concatenation is commonly a macro that defines a function so ignore those
5405 if ($has_flow_statement && !$has_arg_concat) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00005406 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005407 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillips9b7b8372013-02-28 12:53:52 +00005408
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005409 WARN("MACRO_WITH_FLOW_CONTROL",
5410 "Macros with flow control statements should be avoided\n" . "$herectx");
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005411 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005412
5413# check for line continuations outside of #defines, preprocessor #, and asm
5414
5415 } else {
5416 if ($prevline !~ /^..*\\$/ &&
5417 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5418 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5419 $line =~ /^\+.*\\$/) {
5420 WARN("LINE_CONTINUATIONS",
5421 "Avoid unnecessary line continuations\n" . $herecurr);
5422 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005423 }
5424
Kim Phillips9b7b8372013-02-28 12:53:52 +00005425# do {} while (0) macro tests:
5426# single-statement macros do not need to be enclosed in do while (0) loop,
5427# macro should not end with a semicolon
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005428 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00005429 $realfile !~ m@/vmlinux.lds.h$@ &&
5430 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5431 my $ln = $linenr;
5432 my $cnt = $realcnt;
5433 my ($off, $dstat, $dcond, $rest);
5434 my $ctx = '';
5435 ($dstat, $dcond, $ln, $cnt, $off) =
5436 ctx_statement_block($linenr, $realcnt, 0);
5437 $ctx = $dstat;
5438
5439 $dstat =~ s/\\\n.//g;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005440 $dstat =~ s/$;/ /g;
Kim Phillips9b7b8372013-02-28 12:53:52 +00005441
5442 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5443 my $stmts = $2;
5444 my $semis = $3;
5445
5446 $ctx =~ s/\n*$//;
5447 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005448 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillips9b7b8372013-02-28 12:53:52 +00005449
5450 if (($stmts =~ tr/;/;/) == 1 &&
5451 $stmts !~ /^\s*(if|while|for|switch)\b/) {
5452 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5453 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5454 }
5455 if (defined $semis && $semis ne "") {
5456 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5457 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5458 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005459 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5460 $ctx =~ s/\n*$//;
5461 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005462 my $herectx = get_stat_here($linenr, $cnt, $here);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005463
5464 WARN("TRAILING_SEMICOLON",
5465 "macros should not use a trailing semicolon\n" . "$herectx");
Kim Phillips9b7b8372013-02-28 12:53:52 +00005466 }
5467 }
5468
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005469# check for redundant bracing round if etc
5470 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5471 my ($level, $endln, @chunks) =
5472 ctx_statement_full($linenr, $realcnt, 1);
5473 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5474 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5475 if ($#chunks > 0 && $level == 0) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00005476 my @allowed = ();
5477 my $allow = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005478 my $seen = 0;
5479 my $herectx = $here . "\n";
5480 my $ln = $linenr - 1;
5481 for my $chunk (@chunks) {
5482 my ($cond, $block) = @{$chunk};
5483
5484 # If the condition carries leading newlines, then count those as offsets.
5485 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5486 my $offset = statement_rawlines($whitespace) - 1;
5487
Kim Phillips9b7b8372013-02-28 12:53:52 +00005488 $allowed[$allow] = 0;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005489 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5490
5491 # We have looked at and allowed this specific line.
5492 $suppress_ifbraces{$ln + $offset} = 1;
5493
5494 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5495 $ln += statement_rawlines($block) - 1;
5496
5497 substr($block, 0, length($cond), '');
5498
5499 $seen++ if ($block =~ /^\s*{/);
5500
Kim Phillips9b7b8372013-02-28 12:53:52 +00005501 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005502 if (statement_lines($cond) > 1) {
5503 #print "APW: ALLOWED: cond<$cond>\n";
Kim Phillips9b7b8372013-02-28 12:53:52 +00005504 $allowed[$allow] = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005505 }
5506 if ($block =~/\b(?:if|for|while)\b/) {
5507 #print "APW: ALLOWED: block<$block>\n";
Kim Phillips9b7b8372013-02-28 12:53:52 +00005508 $allowed[$allow] = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005509 }
5510 if (statement_block_size($block) > 1) {
5511 #print "APW: ALLOWED: lines block<$block>\n";
Kim Phillips9b7b8372013-02-28 12:53:52 +00005512 $allowed[$allow] = 1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005513 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005514 $allow++;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005515 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00005516 if ($seen) {
5517 my $sum_allowed = 0;
5518 foreach (@allowed) {
5519 $sum_allowed += $_;
5520 }
5521 if ($sum_allowed == 0) {
5522 WARN("BRACES",
5523 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5524 } elsif ($sum_allowed != $allow &&
5525 $seen != $allow) {
5526 CHK("BRACES",
5527 "braces {} should be used on all arms of this statement\n" . $herectx);
5528 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005529 }
5530 }
5531 }
5532 if (!defined $suppress_ifbraces{$linenr - 1} &&
5533 $line =~ /\b(if|while|for|else)\b/) {
5534 my $allowed = 0;
5535
5536 # Check the pre-context.
5537 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5538 #print "APW: ALLOWED: pre<$1>\n";
5539 $allowed = 1;
5540 }
5541
5542 my ($level, $endln, @chunks) =
5543 ctx_statement_full($linenr, $realcnt, $-[0]);
5544
5545 # Check the condition.
5546 my ($cond, $block) = @{$chunks[0]};
5547 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5548 if (defined $cond) {
5549 substr($block, 0, length($cond), '');
5550 }
5551 if (statement_lines($cond) > 1) {
5552 #print "APW: ALLOWED: cond<$cond>\n";
5553 $allowed = 1;
5554 }
5555 if ($block =~/\b(?:if|for|while)\b/) {
5556 #print "APW: ALLOWED: block<$block>\n";
5557 $allowed = 1;
5558 }
5559 if (statement_block_size($block) > 1) {
5560 #print "APW: ALLOWED: lines block<$block>\n";
5561 $allowed = 1;
5562 }
5563 # Check the post-context.
5564 if (defined $chunks[1]) {
5565 my ($cond, $block) = @{$chunks[1]};
5566 if (defined $cond) {
5567 substr($block, 0, length($cond), '');
5568 }
5569 if ($block =~ /^\s*\{/) {
5570 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5571 $allowed = 1;
5572 }
5573 }
5574 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005575 my $cnt = statement_rawlines($block);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005576 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005577
5578 WARN("BRACES",
5579 "braces {} are not necessary for single statement blocks\n" . $herectx);
5580 }
5581 }
5582
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005583# check for single line unbalanced braces
5584 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5585 $sline =~ /^.\s*else\s*\{\s*$/) {
5586 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5587 }
5588
Kim Phillips9b7b8372013-02-28 12:53:52 +00005589# check for unnecessary blank lines around braces
Tom Rinia50f8de2014-02-27 08:27:28 -05005590 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005591 if (CHK("BRACES",
5592 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5593 $fix && $prevrawline =~ /^\+/) {
5594 fix_delete_line($fixlinenr - 1, $prevrawline);
5595 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005596 }
Tom Rinia50f8de2014-02-27 08:27:28 -05005597 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005598 if (CHK("BRACES",
5599 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5600 $fix) {
5601 fix_delete_line($fixlinenr, $rawline);
5602 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005603 }
5604
5605# no volatiles please
5606 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5607 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5608 WARN("VOLATILE",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005609 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
5610 }
5611
5612# Check for user-visible strings broken across lines, which breaks the ability
5613# to grep for the string. Make exceptions when the previous string ends in a
5614# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5615# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5616 if ($line =~ /^\+\s*$String/ &&
5617 $prevline =~ /"\s*$/ &&
5618 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5619 if (WARN("SPLIT_STRING",
5620 "quoted string split across lines\n" . $hereprev) &&
5621 $fix &&
5622 $prevrawline =~ /^\+.*"\s*$/ &&
5623 $last_coalesced_string_linenr != $linenr - 1) {
5624 my $extracted_string = get_quoted_string($line, $rawline);
5625 my $comma_close = "";
5626 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5627 $comma_close = $1;
5628 }
5629
5630 fix_delete_line($fixlinenr - 1, $prevrawline);
5631 fix_delete_line($fixlinenr, $rawline);
5632 my $fixedline = $prevrawline;
5633 $fixedline =~ s/"\s*$//;
5634 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5635 fix_insert_line($fixlinenr - 1, $fixedline);
5636 $fixedline = $rawline;
5637 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5638 if ($fixedline !~ /\+\s*$/) {
5639 fix_insert_line($fixlinenr, $fixedline);
5640 }
5641 $last_coalesced_string_linenr = $linenr;
5642 }
5643 }
5644
5645# check for missing a space in a string concatenation
5646 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5647 WARN('MISSING_SPACE',
5648 "break quoted strings at a space character\n" . $hereprev);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005649 }
5650
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005651# check for an embedded function name in a string when the function is known
5652# This does not work very well for -f --file checking as it depends on patch
5653# context providing the function name or a single line form for in-file
5654# function declarations
5655 if ($line =~ /^\+.*$String/ &&
5656 defined($context_function) &&
5657 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5658 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
5659 WARN("EMBEDDED_FUNCTION_NAME",
5660 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
5661 }
5662
5663# check for spaces before a quoted newline
5664 if ($rawline =~ /^.*\".*\s\\n/) {
5665 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5666 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5667 $fix) {
5668 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5669 }
5670
5671 }
5672
5673# concatenated string without spaces between elements
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005674 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5675 if (CHK("CONCATENATED_STRING",
5676 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5677 $fix) {
5678 while ($line =~ /($String)/g) {
5679 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5680 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5681 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5682 }
5683 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005684 }
5685
5686# uncoalesced string fragments
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005687 if ($line =~ /$String\s*"/) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005688 if (WARN("STRING_FRAGMENTS",
5689 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5690 $fix) {
5691 while ($line =~ /($String)(?=\s*")/g) {
5692 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5693 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5694 }
5695 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005696 }
5697
5698# check for non-standard and hex prefixed decimal printf formats
5699 my $show_L = 1; #don't show the same defect twice
5700 my $show_Z = 1;
5701 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5702 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5703 $string =~ s/%%/__/g;
5704 # check for %L
5705 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5706 WARN("PRINTF_L",
5707 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5708 $show_L = 0;
5709 }
5710 # check for %Z
5711 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5712 WARN("PRINTF_Z",
5713 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5714 $show_Z = 0;
5715 }
5716 # check for 0x<decimal>
5717 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5718 ERROR("PRINTF_0XDECIMAL",
5719 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5720 }
5721 }
5722
5723# check for line continuations in quoted strings with odd counts of "
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02005724 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005725 WARN("LINE_CONTINUATIONS",
5726 "Avoid line continuations in quoted strings\n" . $herecurr);
5727 }
5728
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005729# warn about #if 0
5730 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005731 WARN("IF_0",
5732 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5733 }
5734
5735# warn about #if 1
5736 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5737 WARN("IF_1",
5738 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005739 }
5740
Kim Phillips9b7b8372013-02-28 12:53:52 +00005741# check for needless "if (<foo>) fn(<foo>)" uses
5742 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005743 my $tested = quotemeta($1);
5744 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5745 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5746 my $func = $1;
5747 if (WARN('NEEDLESS_IF',
5748 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5749 $fix) {
5750 my $do_fix = 1;
5751 my $leading_tabs = "";
5752 my $new_leading_tabs = "";
5753 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5754 $leading_tabs = $1;
5755 } else {
5756 $do_fix = 0;
5757 }
5758 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5759 $new_leading_tabs = $1;
5760 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5761 $do_fix = 0;
5762 }
5763 } else {
5764 $do_fix = 0;
5765 }
5766 if ($do_fix) {
5767 fix_delete_line($fixlinenr - 1, $prevrawline);
5768 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5769 }
5770 }
5771 }
5772 }
5773
5774# check for unnecessary "Out of Memory" messages
5775 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5776 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5777 (defined $1 || defined $3) &&
5778 $linenr > 3) {
5779 my $testval = $2;
5780 my $testline = $lines[$linenr - 3];
5781
5782 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5783# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5784
Tom Rinid2e30332020-06-16 10:29:46 -04005785 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5786 $s !~ /\b__GFP_NOWARN\b/ ) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005787 WARN("OOM_MESSAGE",
5788 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5789 }
5790 }
5791
5792# check for logging functions with KERN_<LEVEL>
5793 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5794 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5795 my $level = $1;
5796 if (WARN("UNNECESSARY_KERN_LEVEL",
5797 "Possible unnecessary $level\n" . $herecurr) &&
5798 $fix) {
5799 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5800 }
5801 }
5802
5803# check for logging continuations
5804 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5805 WARN("LOGGING_CONTINUATION",
5806 "Avoid logging continuation uses where feasible\n" . $herecurr);
5807 }
5808
5809# check for mask then right shift without a parentheses
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005810 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005811 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5812 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5813 WARN("MASK_THEN_SHIFT",
5814 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5815 }
5816
5817# check for pointer comparisons to NULL
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02005818 if ($perl_version_ok) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005819 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5820 my $val = $1;
5821 my $equal = "!";
5822 $equal = "" if ($4 eq "!=");
5823 if (CHK("COMPARISON_TO_NULL",
5824 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5825 $fix) {
5826 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5827 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005828 }
5829 }
5830
Tom Rinia50f8de2014-02-27 08:27:28 -05005831# check for bad placement of section $InitAttribute (e.g.: __initdata)
5832 if ($line =~ /(\b$InitAttribute\b)/) {
5833 my $attr = $1;
5834 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5835 my $ptr = $1;
5836 my $var = $2;
5837 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5838 ERROR("MISPLACED_INIT",
5839 "$attr should be placed after $var\n" . $herecurr)) ||
5840 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5841 WARN("MISPLACED_INIT",
5842 "$attr should be placed after $var\n" . $herecurr))) &&
5843 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005844 $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;
Tom Rinia50f8de2014-02-27 08:27:28 -05005845 }
5846 }
5847 }
5848
5849# check for $InitAttributeData (ie: __initdata) with const
5850 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5851 my $attr = $1;
5852 $attr =~ /($InitAttributePrefix)(.*)/;
5853 my $attr_prefix = $1;
5854 my $attr_type = $2;
5855 if (ERROR("INIT_ATTRIBUTE",
5856 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5857 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005858 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05005859 s/$InitAttributeData/${attr_prefix}initconst/;
5860 }
5861 }
5862
5863# check for $InitAttributeConst (ie: __initconst) without const
5864 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5865 my $attr = $1;
5866 if (ERROR("INIT_ATTRIBUTE",
5867 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5868 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005869 my $lead = $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05005870 /(^\+\s*(?:static\s+))/;
5871 $lead = rtrim($1);
5872 $lead = "$lead " if ($lead !~ /^\+$/);
5873 $lead = "${lead}const ";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005874 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5875 }
5876 }
5877
5878# check for __read_mostly with const non-pointer (should just be const)
5879 if ($line =~ /\b__read_mostly\b/ &&
5880 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5881 if (ERROR("CONST_READ_MOSTLY",
5882 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5883 $fix) {
5884 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5885 }
5886 }
5887
5888# don't use __constant_<foo> functions outside of include/uapi/
5889 if ($realfile !~ m@^include/uapi/@ &&
5890 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5891 my $constant_func = $1;
5892 my $func = $constant_func;
5893 $func =~ s/^__constant_//;
5894 if (WARN("CONSTANT_CONVERSION",
5895 "$constant_func should be $func\n" . $herecurr) &&
5896 $fix) {
5897 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Tom Rinia50f8de2014-02-27 08:27:28 -05005898 }
5899 }
5900
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005901# prefer usleep_range over udelay
Kim Phillips9b7b8372013-02-28 12:53:52 +00005902 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005903 my $delay = $1;
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005904 # ignore udelay's < 10, however
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005905 if (! ($delay < 10) ) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005906 CHK("USLEEP_RANGE",
Tom Rinid2e30332020-06-16 10:29:46 -04005907 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005908 }
5909 if ($delay > 2000) {
5910 WARN("LONG_UDELAY",
5911 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005912 }
5913 }
5914
5915# warn about unexpectedly long msleep's
5916 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5917 if ($1 < 20) {
5918 WARN("MSLEEP",
Tom Rinid2e30332020-06-16 10:29:46 -04005919 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005920 }
5921 }
5922
Tom Rinia50f8de2014-02-27 08:27:28 -05005923# check for comparisons of jiffies
5924 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5925 WARN("JIFFIES_COMPARISON",
5926 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5927 }
5928
5929# check for comparisons of get_jiffies_64()
5930 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5931 WARN("JIFFIES_COMPARISON",
5932 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5933 }
5934
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005935# warn about #ifdefs in C files
5936# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5937# print "#ifdef in C files should be avoided\n";
5938# print "$herecurr";
5939# $clean = 0;
5940# }
5941
5942# warn about spacing in #ifdefs
5943 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05005944 if (ERROR("SPACING",
5945 "exactly one space required after that #$1\n" . $herecurr) &&
5946 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005947 $fixed[$fixlinenr] =~
Tom Rinia50f8de2014-02-27 08:27:28 -05005948 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5949 }
5950
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005951 }
5952
5953# check for spinlock_t definitions without a comment.
5954 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5955 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5956 my $which = $1;
5957 if (!ctx_has_comment($first_line, $linenr)) {
5958 CHK("UNCOMMENTED_DEFINITION",
5959 "$1 definition without comment\n" . $herecurr);
5960 }
5961 }
5962# check for memory barriers without a comment.
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005963
5964 my $barriers = qr{
5965 mb|
5966 rmb|
5967 wmb|
5968 read_barrier_depends
5969 }x;
5970 my $barrier_stems = qr{
5971 mb__before_atomic|
5972 mb__after_atomic|
5973 store_release|
5974 load_acquire|
5975 store_mb|
5976 (?:$barriers)
5977 }x;
5978 my $all_barriers = qr{
5979 (?:$barriers)|
5980 smp_(?:$barrier_stems)|
5981 virt_(?:$barrier_stems)
5982 }x;
5983
5984 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005985 if (!ctx_has_comment($first_line, $linenr)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05005986 WARN("MEMORY_BARRIER",
5987 "memory barrier without comment\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00005988 }
5989 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02005990
5991 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5992
5993 if ($realfile !~ m@^include/asm-generic/@ &&
5994 $realfile !~ m@/barrier\.h$@ &&
5995 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5996 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5997 WARN("MEMORY_BARRIER",
5998 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5999 }
6000
6001# check for waitqueue_active without a comment.
6002 if ($line =~ /\bwaitqueue_active\s*\(/) {
6003 if (!ctx_has_comment($first_line, $linenr)) {
6004 WARN("WAITQUEUE_ACTIVE",
6005 "waitqueue_active without comment\n" . $herecurr);
6006 }
6007 }
6008
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006009# check for smp_read_barrier_depends and read_barrier_depends
6010 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
6011 WARN("READ_BARRIER_DEPENDS",
6012 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
6013 }
6014
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006015# check of hardware specific defines
6016 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6017 CHK("ARCH_DEFINES",
6018 "architecture specific defines should be avoided\n" . $herecurr);
6019 }
6020
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006021# check that the storage class is not after a type
6022 if ($line =~ /\b($Type)\s+($Storage)\b/) {
6023 WARN("STORAGE_CLASS",
6024 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6025 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006026# Check that the storage class is at the beginning of a declaration
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006027 if ($line =~ /\b$Storage\b/ &&
6028 $line !~ /^.\s*$Storage/ &&
6029 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6030 $1 !~ /[\,\)]\s*$/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006031 WARN("STORAGE_CLASS",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006032 "storage class should be at the beginning of the declaration\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006033 }
6034
6035# check the location of the inline attribute, that it is between
6036# storage class and type.
6037 if ($line =~ /\b$Type\s+$Inline\b/ ||
6038 $line =~ /\b$Inline\s+$Storage\b/) {
6039 ERROR("INLINE_LOCATION",
6040 "inline keyword should sit between storage class and type\n" . $herecurr);
6041 }
6042
6043# Check for __inline__ and __inline, prefer inline
Tom Rinia50f8de2014-02-27 08:27:28 -05006044 if ($realfile !~ m@\binclude/uapi/@ &&
6045 $line =~ /\b(__inline__|__inline)\b/) {
6046 if (WARN("INLINE",
6047 "plain inline is preferred over $1\n" . $herecurr) &&
6048 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006049 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Tom Rinia50f8de2014-02-27 08:27:28 -05006050
6051 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006052 }
6053
6054# Check for __attribute__ packed, prefer __packed
Tom Rinia50f8de2014-02-27 08:27:28 -05006055 if ($realfile !~ m@\binclude/uapi/@ &&
6056 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006057 WARN("PREFER_PACKED",
6058 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
6059 }
6060
6061# Check for __attribute__ aligned, prefer __aligned
Tom Rinia50f8de2014-02-27 08:27:28 -05006062 if ($realfile !~ m@\binclude/uapi/@ &&
6063 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006064 WARN("PREFER_ALIGNED",
6065 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
6066 }
6067
Tom Rinid2e30332020-06-16 10:29:46 -04006068# Check for __attribute__ section, prefer __section
6069 if ($realfile !~ m@\binclude/uapi/@ &&
6070 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6071 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6072 my $new = substr($old, 1, -1);
6073 if (WARN("PREFER_SECTION",
6074 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6075 $fix) {
6076 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6077 }
6078 }
6079
Kim Phillips9b7b8372013-02-28 12:53:52 +00006080# Check for __attribute__ format(printf, prefer __printf
Tom Rinia50f8de2014-02-27 08:27:28 -05006081 if ($realfile !~ m@\binclude/uapi/@ &&
6082 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
6083 if (WARN("PREFER_PRINTF",
6084 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6085 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006086 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Tom Rinia50f8de2014-02-27 08:27:28 -05006087
6088 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00006089 }
6090
6091# Check for __attribute__ format(scanf, prefer __scanf
Tom Rinia50f8de2014-02-27 08:27:28 -05006092 if ($realfile !~ m@\binclude/uapi/@ &&
6093 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
6094 if (WARN("PREFER_SCANF",
6095 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6096 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006097 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
6098 }
6099 }
6100
6101# Check for __attribute__ weak, or __weak declarations (may have link issues)
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006102 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006103 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6104 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6105 $line =~ /\b__weak\b/)) {
6106 ERROR("WEAK_DECLARATION",
6107 "Using weak declarations can have unintended link defects\n" . $herecurr);
6108 }
6109
6110# check for c99 types like uint8_t used outside of uapi/ and tools/
6111 if ($realfile !~ m@\binclude/uapi/@ &&
6112 $realfile !~ m@\btools/@ &&
6113 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6114 my $type = $1;
6115 if ($type =~ /\b($typeC99Typedefs)\b/) {
6116 $type = $1;
6117 my $kernel_type = 'u';
6118 $kernel_type = 's' if ($type =~ /^_*[si]/);
6119 $type =~ /(\d+)/;
6120 $kernel_type .= $1;
6121 if (CHK("PREFER_KERNEL_TYPES",
6122 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6123 $fix) {
6124 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6125 }
6126 }
6127 }
6128
6129# check for cast of C90 native int or longer types constants
6130 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6131 my $cast = $1;
6132 my $const = $2;
6133 if (WARN("TYPECAST_INT_CONSTANT",
6134 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6135 $fix) {
6136 my $suffix = "";
6137 my $newconst = $const;
6138 $newconst =~ s/${Int_type}$//;
6139 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6140 if ($cast =~ /\blong\s+long\b/) {
6141 $suffix .= 'LL';
6142 } elsif ($cast =~ /\blong\b/) {
6143 $suffix .= 'L';
6144 }
6145 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
Tom Rinia50f8de2014-02-27 08:27:28 -05006146 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00006147 }
6148
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006149# check for sizeof(&)
6150 if ($line =~ /\bsizeof\s*\(\s*\&/) {
6151 WARN("SIZEOF_ADDRESS",
6152 "sizeof(& should be avoided\n" . $herecurr);
6153 }
6154
Kim Phillips9b7b8372013-02-28 12:53:52 +00006155# check for sizeof without parenthesis
6156 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006157 if (WARN("SIZEOF_PARENTHESIS",
6158 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6159 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006160 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Tom Rinia50f8de2014-02-27 08:27:28 -05006161 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00006162 }
6163
Kim Phillips9b7b8372013-02-28 12:53:52 +00006164# check for struct spinlock declarations
6165 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6166 WARN("USE_SPINLOCK_T",
6167 "struct spinlock should be spinlock_t\n" . $herecurr);
6168 }
6169
Tom Rinia50f8de2014-02-27 08:27:28 -05006170# check for seq_printf uses that could be seq_puts
6171 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6172 my $fmt = get_quoted_string($line, $rawline);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006173 $fmt =~ s/%%//g;
6174 if ($fmt !~ /%/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006175 if (WARN("PREFER_SEQ_PUTS",
6176 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6177 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006178 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6179 }
6180 }
6181 }
6182
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006183# check for vsprintf extension %p<foo> misuses
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006184 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006185 defined $stat &&
6186 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6187 $1 !~ /^_*volatile_*$/) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006188 my $stat_real;
6189
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006190 my $lc = $stat =~ tr@\n@@;
6191 $lc = $lc + $linenr;
6192 for (my $count = $linenr; $count <= $lc; $count++) {
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006193 my $specifier;
6194 my $extension;
Tom Rinid2e30332020-06-16 10:29:46 -04006195 my $qualifier;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006196 my $bad_specifier = "";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006197 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6198 $fmt =~ s/%%//g;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006199
Tom Rinid2e30332020-06-16 10:29:46 -04006200 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006201 $specifier = $1;
6202 $extension = $2;
Tom Rinid2e30332020-06-16 10:29:46 -04006203 $qualifier = $3;
6204 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
6205 ($extension eq "f" &&
6206 defined $qualifier && $qualifier !~ /^w/)) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006207 $bad_specifier = $specifier;
6208 last;
6209 }
6210 if ($extension eq "x" && !defined($stat_real)) {
6211 if (!defined($stat_real)) {
6212 $stat_real = get_stat_real($linenr, $lc);
6213 }
6214 WARN("VSPRINTF_SPECIFIER_PX",
6215 "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");
6216 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006217 }
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006218 if ($bad_specifier ne "") {
6219 my $stat_real = get_stat_real($linenr, $lc);
6220 my $ext_type = "Invalid";
6221 my $use = "";
6222 if ($bad_specifier =~ /p[Ff]/) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006223 $use = " - use %pS instead";
6224 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6225 }
6226
6227 WARN("VSPRINTF_POINTER_EXTENSION",
6228 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
Tom Rinia50f8de2014-02-27 08:27:28 -05006229 }
6230 }
6231 }
6232
Kim Phillips9b7b8372013-02-28 12:53:52 +00006233# Check for misused memsets
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006234 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00006235 defined $stat &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006236 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00006237
6238 my $ms_addr = $2;
6239 my $ms_val = $7;
6240 my $ms_size = $12;
6241
6242 if ($ms_size =~ /^(0x|)0$/i) {
6243 ERROR("MEMSET",
6244 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6245 } elsif ($ms_size =~ /^(0x|)1$/i) {
6246 WARN("MEMSET",
6247 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6248 }
6249 }
6250
Tom Rinia50f8de2014-02-27 08:27:28 -05006251# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006252# if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006253# defined $stat &&
6254# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6255# if (WARN("PREFER_ETHER_ADDR_COPY",
6256# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6257# $fix) {
6258# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6259# }
6260# }
6261
6262# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006263# if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006264# defined $stat &&
6265# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6266# WARN("PREFER_ETHER_ADDR_EQUAL",
6267# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6268# }
6269
6270# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6271# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006272# if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006273# defined $stat &&
6274# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6275#
6276# my $ms_val = $7;
6277#
6278# if ($ms_val =~ /^(?:0x|)0+$/i) {
6279# if (WARN("PREFER_ETH_ZERO_ADDR",
6280# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6281# $fix) {
6282# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6283# }
6284# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6285# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6286# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6287# $fix) {
6288# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6289# }
6290# }
6291# }
Tom Rinia50f8de2014-02-27 08:27:28 -05006292
Kim Phillips9b7b8372013-02-28 12:53:52 +00006293# typecasts on min/max could be min_t/max_t
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006294 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00006295 defined $stat &&
6296 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6297 if (defined $2 || defined $7) {
6298 my $call = $1;
6299 my $cast1 = deparenthesize($2);
6300 my $arg1 = $3;
6301 my $cast2 = deparenthesize($7);
6302 my $arg2 = $8;
6303 my $cast;
6304
6305 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6306 $cast = "$cast1 or $cast2";
6307 } elsif ($cast1 ne "") {
6308 $cast = $cast1;
6309 } else {
6310 $cast = $cast2;
6311 }
6312 WARN("MINMAX",
6313 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6314 }
6315 }
6316
6317# check usleep_range arguments
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006318 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00006319 defined $stat &&
6320 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6321 my $min = $1;
6322 my $max = $7;
6323 if ($min eq $max) {
6324 WARN("USLEEP_RANGE",
Tom Rinid2e30332020-06-16 10:29:46 -04006325 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Kim Phillips9b7b8372013-02-28 12:53:52 +00006326 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6327 $min > $max) {
6328 WARN("USLEEP_RANGE",
Tom Rinid2e30332020-06-16 10:29:46 -04006329 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Kim Phillips9b7b8372013-02-28 12:53:52 +00006330 }
6331 }
6332
Tom Rinia50f8de2014-02-27 08:27:28 -05006333# check for naked sscanf
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006334 if ($perl_version_ok &&
Tom Rinia50f8de2014-02-27 08:27:28 -05006335 defined $stat &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006336 $line =~ /\bsscanf\b/ &&
Tom Rinia50f8de2014-02-27 08:27:28 -05006337 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6338 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6339 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6340 my $lc = $stat =~ tr@\n@@;
6341 $lc = $lc + $linenr;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006342 my $stat_real = get_stat_real($linenr, $lc);
Tom Rinia50f8de2014-02-27 08:27:28 -05006343 WARN("NAKED_SSCANF",
6344 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6345 }
6346
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006347# check for simple sscanf that should be kstrto<foo>
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006348 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006349 defined $stat &&
6350 $line =~ /\bsscanf\b/) {
6351 my $lc = $stat =~ tr@\n@@;
6352 $lc = $lc + $linenr;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006353 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006354 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6355 my $format = $6;
6356 my $count = $format =~ tr@%@%@;
6357 if ($count == 1 &&
6358 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6359 WARN("SSCANF_TO_KSTRTO",
6360 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6361 }
6362 }
6363 }
6364
Tom Rinia50f8de2014-02-27 08:27:28 -05006365# check for new externs in .h files.
6366 if ($realfile =~ /\.h$/ &&
6367 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6368 if (CHK("AVOID_EXTERNS",
6369 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6370 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006371 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Tom Rinia50f8de2014-02-27 08:27:28 -05006372 }
6373 }
6374
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006375# check for new externs in .c files.
6376 if ($realfile =~ /\.c$/ && defined $stat &&
6377 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6378 {
6379 my $function_name = $1;
6380 my $paren_space = $2;
6381
6382 my $s = $stat;
6383 if (defined $cond) {
6384 substr($s, 0, length($cond), '');
6385 }
6386 if ($s =~ /^\s*;/ &&
6387 $function_name ne 'uninitialized_var')
6388 {
6389 WARN("AVOID_EXTERNS",
6390 "externs should be avoided in .c files\n" . $herecurr);
6391 }
6392
6393 if ($paren_space =~ /\n/) {
6394 WARN("FUNCTION_ARGUMENTS",
6395 "arguments for function declarations should follow identifier\n" . $herecurr);
6396 }
6397
6398 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6399 $stat =~ /^.\s*extern\s+/)
6400 {
6401 WARN("AVOID_EXTERNS",
6402 "externs should be avoided in .c files\n" . $herecurr);
6403 }
6404
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006405# check for function declarations that have arguments without identifier names
Tom Rinid2e30332020-06-16 10:29:46 -04006406# while avoiding uninitialized_var(x)
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006407 if (defined $stat &&
Tom Rinid2e30332020-06-16 10:29:46 -04006408 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6409 (!defined($1) ||
6410 (defined($1) && $1 ne "uninitialized_var")) &&
6411 $2 ne "void") {
6412 my $args = trim($2);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006413 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6414 my $arg = trim($1);
Tom Rinid2e30332020-06-16 10:29:46 -04006415 if ($arg =~ /^$Type$/ &&
6416 $arg !~ /enum\s+$Ident$/) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006417 WARN("FUNCTION_ARGUMENTS",
6418 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6419 }
6420 }
6421 }
6422
6423# check for function definitions
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006424 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006425 defined $stat &&
6426 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6427 $context_function = $1;
6428
6429# check for multiline function definition with misplaced open brace
6430 my $ok = 0;
6431 my $cnt = statement_rawlines($stat);
6432 my $herectx = $here . "\n";
6433 for (my $n = 0; $n < $cnt; $n++) {
6434 my $rl = raw_line($linenr, $n);
6435 $herectx .= $rl . "\n";
6436 $ok = 1 if ($rl =~ /^[ \+]\{/);
6437 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6438 last if $rl =~ /^[ \+].*\{/;
6439 }
6440 if (!$ok) {
6441 ERROR("OPEN_BRACE",
6442 "open brace '{' following function definitions go on the next line\n" . $herectx);
6443 }
6444 }
6445
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006446# checks for new __setup's
6447 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6448 my $name = $1;
6449
6450 if (!grep(/$name/, @setup_docs)) {
6451 CHK("UNDOCUMENTED_SETUP",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006452 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006453 }
6454 }
6455
Tom Rinid2e30332020-06-16 10:29:46 -04006456# check for pointless casting of alloc functions
6457 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006458 WARN("UNNECESSARY_CASTS",
6459 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6460 }
6461
Tom Rinia50f8de2014-02-27 08:27:28 -05006462# alloc style
6463# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006464 if ($perl_version_ok &&
Tom Rinid2e30332020-06-16 10:29:46 -04006465 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006466 CHK("ALLOC_SIZEOF_STRUCT",
6467 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6468 }
6469
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006470# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006471 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006472 defined $stat &&
6473 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
6474 my $oldfunc = $3;
6475 my $a1 = $4;
6476 my $a2 = $10;
6477 my $newfunc = "kmalloc_array";
6478 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
6479 my $r1 = $a1;
6480 my $r2 = $a2;
6481 if ($a1 =~ /^sizeof\s*\S/) {
6482 $r1 = $a2;
6483 $r2 = $a1;
6484 }
6485 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6486 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006487 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006488 my $herectx = get_stat_here($linenr, $cnt, $here);
6489
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006490 if (WARN("ALLOC_WITH_MULTIPLY",
6491 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6492 $cnt == 1 &&
6493 $fix) {
6494 $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;
6495 }
6496 }
6497 }
6498
Tom Rinia50f8de2014-02-27 08:27:28 -05006499# check for krealloc arg reuse
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006500 if ($perl_version_ok &&
6501 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6502 $1 eq $3) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006503 WARN("KREALLOC_ARG_REUSE",
6504 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6505 }
6506
Kim Phillips9b7b8372013-02-28 12:53:52 +00006507# check for alloc argument mismatch
6508 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6509 WARN("ALLOC_ARRAY_ARGS",
6510 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6511 }
6512
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006513# check for multiple semicolons
6514 if ($line =~ /;\s*;\s*$/) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006515 if (WARN("ONE_SEMICOLON",
6516 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6517 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006518 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6519 }
6520 }
6521
6522# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6523 if ($realfile !~ m@^include/uapi/@ &&
6524 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
6525 my $ull = "";
6526 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6527 if (CHK("BIT_MACRO",
6528 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6529 $fix) {
6530 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
Tom Rinia50f8de2014-02-27 08:27:28 -05006531 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006532 }
6533
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006534# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6535 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6536 my $config = $1;
6537 if (WARN("PREFER_IS_ENABLED",
6538 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6539 $fix) {
6540 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6541 }
6542 }
6543
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04006544# check for case / default statements not preceded by break/fallthrough/switch
Tom Rinia50f8de2014-02-27 08:27:28 -05006545 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6546 my $has_break = 0;
6547 my $has_statement = 0;
6548 my $count = 0;
6549 my $prevline = $linenr;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006550 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006551 $prevline--;
6552 my $rline = $rawlines[$prevline - 1];
6553 my $fline = $lines[$prevline - 1];
6554 last if ($fline =~ /^\@\@/);
6555 next if ($fline =~ /^\-/);
6556 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6557 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6558 next if ($fline =~ /^.[\s$;]*$/);
6559 $has_statement = 1;
6560 $count++;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006561 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
Tom Rinia50f8de2014-02-27 08:27:28 -05006562 }
6563 if (!$has_break && $has_statement) {
6564 WARN("MISSING_BREAK",
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -04006565 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Tom Rinia50f8de2014-02-27 08:27:28 -05006566 }
6567 }
6568
Tom Rinid2e30332020-06-16 10:29:46 -04006569# check for /* fallthrough */ like comment, prefer fallthrough;
6570 my @fallthroughs = (
6571 'fallthrough',
6572 '@fallthrough@',
6573 'lint -fallthrough[ \t]*',
6574 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6575 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6576 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6577 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6578 );
6579 if ($raw_comment ne '') {
6580 foreach my $ft (@fallthroughs) {
6581 if ($raw_comment =~ /$ft/) {
6582 my $msg_level = \&WARN;
6583 $msg_level = \&CHK if ($file);
6584 &{$msg_level}("PREFER_FALLTHROUGH",
6585 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6586 last;
6587 }
6588 }
6589 }
6590
Kim Phillips9b7b8372013-02-28 12:53:52 +00006591# check for switch/default statements without a break;
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006592 if ($perl_version_ok &&
Kim Phillips9b7b8372013-02-28 12:53:52 +00006593 defined $stat &&
6594 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Kim Phillips9b7b8372013-02-28 12:53:52 +00006595 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006596 my $herectx = get_stat_here($linenr, $cnt, $here);
6597
Kim Phillips9b7b8372013-02-28 12:53:52 +00006598 WARN("DEFAULT_NO_BREAK",
6599 "switch default: should use break\n" . $herectx);
Eric Nelson9c7c24f2012-05-02 20:32:18 -07006600 }
6601
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006602# check for gcc specific __FUNCTION__
Tom Rinia50f8de2014-02-27 08:27:28 -05006603 if ($line =~ /\b__FUNCTION__\b/) {
6604 if (WARN("USE_FUNC",
6605 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6606 $fix) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006607 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Tom Rinia50f8de2014-02-27 08:27:28 -05006608 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006609 }
6610
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006611# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6612 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6613 ERROR("DATE_TIME",
6614 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6615 }
6616
Kim Phillips9b7b8372013-02-28 12:53:52 +00006617# check for use of yield()
6618 if ($line =~ /\byield\s*\(\s*\)/) {
6619 WARN("YIELD",
6620 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6621 }
6622
Tom Rinia50f8de2014-02-27 08:27:28 -05006623# check for comparisons against true and false
6624 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6625 my $lead = $1;
6626 my $arg = $2;
6627 my $test = $3;
6628 my $otype = $4;
6629 my $trail = $5;
6630 my $op = "!";
6631
6632 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6633
6634 my $type = lc($otype);
6635 if ($type =~ /^(?:true|false)$/) {
6636 if (("$test" eq "==" && "$type" eq "true") ||
6637 ("$test" eq "!=" && "$type" eq "false")) {
6638 $op = "";
6639 }
6640
6641 CHK("BOOL_COMPARISON",
6642 "Using comparison to $otype is error prone\n" . $herecurr);
6643
6644## maybe suggesting a correct construct would better
6645## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6646
6647 }
6648 }
6649
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006650# check for semaphores initialized locked
6651 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6652 WARN("CONSIDER_COMPLETION",
6653 "consider using a completion\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006654 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00006655
6656# recommend kstrto* over simple_strto* and strict_strto*
6657 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006658 WARN("CONSIDER_KSTRTO",
Kim Phillips9b7b8372013-02-28 12:53:52 +00006659 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006660 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00006661
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006662# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006663 if ($line =~ /^.\s*__initcall\s*\(/) {
6664 WARN("USE_DEVICE_INITCALL",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006665 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006666 }
Kim Phillips9b7b8372013-02-28 12:53:52 +00006667
Tom Rinid2e30332020-06-16 10:29:46 -04006668# check for spin_is_locked(), suggest lockdep instead
6669 if ($line =~ /\bspin_is_locked\(/) {
6670 WARN("USE_LOCKDEP",
6671 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6672 }
6673
6674# check for deprecated apis
6675 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6676 my $deprecated_api = $1;
6677 my $new_api = $deprecated_apis{$deprecated_api};
6678 WARN("DEPRECATED_API",
6679 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6680 }
6681
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006682# check for various structs that are normally const (ops, kgdb, device_tree)
6683# and avoid what seem like struct definitions 'struct foo {'
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006684 if ($line !~ /\bconst\b/ &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006685 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006686 WARN("CONST_STRUCT",
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006687 "struct $1 should normally be const\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006688 }
6689
6690# use of NR_CPUS is usually wrong
6691# ignore definitions of NR_CPUS and usage to define arrays as likely right
6692 if ($line =~ /\bNR_CPUS\b/ &&
6693 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6694 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6695 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6696 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6697 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6698 {
6699 WARN("NR_CPUS",
6700 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6701 }
6702
Tom Rinia50f8de2014-02-27 08:27:28 -05006703# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6704 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6705 ERROR("DEFINE_ARCH_HAS",
6706 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6707 }
6708
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006709# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006710 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006711 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6712 WARN("LIKELY_MISUSE",
6713 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006714 }
6715
Tom Rinid2e30332020-06-16 10:29:46 -04006716# nested likely/unlikely calls
6717 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6718 WARN("LIKELY_MISUSE",
6719 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6720 }
6721
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006722# whine mightly about in_atomic
6723 if ($line =~ /\bin_atomic\s*\(/) {
6724 if ($realfile =~ m@^drivers/@) {
6725 ERROR("IN_ATOMIC",
6726 "do not use in_atomic in drivers\n" . $herecurr);
6727 } elsif ($realfile !~ m@^kernel/@) {
6728 WARN("IN_ATOMIC",
6729 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6730 }
6731 }
6732
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006733# check for mutex_trylock_recursive usage
6734 if ($line =~ /mutex_trylock_recursive/) {
6735 ERROR("LOCKING",
6736 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6737 }
6738
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006739# check for lockdep_set_novalidate_class
6740 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6741 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6742 if ($realfile !~ m@^kernel/lockdep@ &&
6743 $realfile !~ m@^include/linux/lockdep@ &&
6744 $realfile !~ m@^drivers/base/core@) {
6745 ERROR("LOCKDEP",
6746 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6747 }
6748 }
6749
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006750 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6751 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006752 WARN("EXPORTED_WORLD_WRITABLE",
6753 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6754 }
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006755
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006756# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6757# and whether or not function naming is typical and if
6758# DEVICE_ATTR permissions uses are unusual too
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006759 if ($perl_version_ok &&
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006760 defined $stat &&
6761 $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*\)/) {
6762 my $var = $1;
6763 my $perms = $2;
6764 my $show = $3;
6765 my $store = $4;
6766 my $octal_perms = perms_to_octal($perms);
6767 if ($show =~ /^${var}_show$/ &&
6768 $store =~ /^${var}_store$/ &&
6769 $octal_perms eq "0644") {
6770 if (WARN("DEVICE_ATTR_RW",
6771 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6772 $fix) {
6773 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6774 }
6775 } elsif ($show =~ /^${var}_show$/ &&
6776 $store =~ /^NULL$/ &&
6777 $octal_perms eq "0444") {
6778 if (WARN("DEVICE_ATTR_RO",
6779 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6780 $fix) {
6781 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6782 }
6783 } elsif ($show =~ /^NULL$/ &&
6784 $store =~ /^${var}_store$/ &&
6785 $octal_perms eq "0200") {
6786 if (WARN("DEVICE_ATTR_WO",
6787 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6788 $fix) {
6789 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6790 }
6791 } elsif ($octal_perms eq "0644" ||
6792 $octal_perms eq "0444" ||
6793 $octal_perms eq "0200") {
6794 my $newshow = "$show";
6795 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6796 my $newstore = $store;
6797 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6798 my $rename = "";
6799 if ($show ne $newshow) {
6800 $rename .= " '$show' to '$newshow'";
6801 }
6802 if ($store ne $newstore) {
6803 $rename .= " '$store' to '$newstore'";
6804 }
6805 WARN("DEVICE_ATTR_FUNCTIONS",
6806 "Consider renaming function(s)$rename\n" . $herecurr);
6807 } else {
6808 WARN("DEVICE_ATTR_PERMS",
6809 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6810 }
6811 }
6812
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006813# Mode permission misuses where it seems decimal should be octal
6814# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006815# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6816# specific definition of not visible in sysfs.
6817# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6818# use the default permissions
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006819 if ($perl_version_ok &&
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006820 defined $stat &&
6821 $line =~ /$mode_perms_search/) {
6822 foreach my $entry (@mode_permission_funcs) {
6823 my $func = $entry->[0];
6824 my $arg_pos = $entry->[1];
6825
6826 my $lc = $stat =~ tr@\n@@;
6827 $lc = $lc + $linenr;
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006828 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006829
6830 my $skip_args = "";
6831 if ($arg_pos > 1) {
6832 $arg_pos--;
6833 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6834 }
6835 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6836 if ($stat =~ /$test/) {
6837 my $val = $1;
6838 $val = $6 if ($skip_args ne "");
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006839 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6840 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6841 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006842 ERROR("NON_OCTAL_PERMISSIONS",
6843 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6844 }
6845 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6846 ERROR("EXPORTED_WORLD_WRITABLE",
6847 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6848 }
6849 }
6850 }
6851 }
6852
6853# check for uses of S_<PERMS> that could be octal for readability
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006854 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
6855 my $oval = $1;
6856 my $octal = perms_to_octal($oval);
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006857 if (WARN("SYMBOLIC_PERMS",
6858 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6859 $fix) {
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006860 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006861 }
6862 }
6863
6864# validate content of MODULE_LICENSE against list from include/linux/module.h
6865 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6866 my $extracted_string = get_quoted_string($line, $rawline);
6867 my $valid_licenses = qr{
6868 GPL|
6869 GPL\ v2|
6870 GPL\ and\ additional\ rights|
6871 Dual\ BSD/GPL|
6872 Dual\ MIT/GPL|
6873 Dual\ MPL/GPL|
6874 Proprietary
6875 }x;
6876 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6877 WARN("MODULE_LICENSE",
6878 "unknown module license " . $extracted_string . "\n" . $herecurr);
6879 }
6880 }
Tom Rinid2e30332020-06-16 10:29:46 -04006881
6882# check for sysctl duplicate constants
6883 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6884 WARN("DUPLICATED_SYSCTL_CONST",
6885 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6886 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006887 }
6888
6889 # If we have no input at all, then there is nothing to report on
6890 # so just keep quiet.
6891 if ($#rawlines == -1) {
6892 exit(0);
6893 }
6894
6895 # In mailback mode only produce a report in the negative, for
6896 # things that appear to be patches.
6897 if ($mailback && ($clean == 1 || !$is_patch)) {
6898 exit(0);
6899 }
6900
6901 # This is not a patch, and we are are in 'no-patch' mode so
6902 # just keep quiet.
6903 if (!$chk_patch && !$is_patch) {
6904 exit(0);
6905 }
6906
Heinrich Schuchardtc664f0a2018-04-04 15:39:20 +02006907 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006908 ERROR("NOT_UNIFIED_DIFF",
6909 "Does not appear to be a unified-diff format patch\n");
6910 }
Heinrich Schuchardtd46efeb2019-10-19 09:06:38 +02006911 if ($is_patch && $has_commit_log && $chk_signoff) {
6912 if ($signoff == 0) {
6913 ERROR("MISSING_SIGN_OFF",
6914 "Missing Signed-off-by: line(s)\n");
6915 } elsif (!$authorsignoff) {
6916 WARN("NO_AUTHOR_SIGN_OFF",
6917 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6918 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006919 }
6920
6921 print report_dump();
6922 if ($summary && !($clean == 1 && $quiet == 1)) {
6923 print "$filename " if ($summary_file);
6924 print "total: $cnt_error errors, $cnt_warn warnings, " .
6925 (($check)? "$cnt_chk checks, " : "") .
6926 "$cnt_lines lines checked\n";
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006927 }
6928
6929 if ($quiet == 0) {
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006930 # If there were any defects found and not already fixing them
6931 if (!$clean and !$fix) {
6932 print << "EOM"
Kim Phillips9b7b8372013-02-28 12:53:52 +00006933
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006934NOTE: For some of the reported defects, checkpatch may be able to
6935 mechanically convert to the typical style using --fix or --fix-inplace.
6936EOM
Kim Phillips9b7b8372013-02-28 12:53:52 +00006937 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006938 # If there were whitespace errors which cleanpatch can fix
6939 # then suggest that.
6940 if ($rpt_cleaners) {
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006941 $rpt_cleaners = 0;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006942 print << "EOM"
6943
6944NOTE: Whitespace errors detected.
6945 You may wish to use scripts/cleanpatch or scripts/cleanfile
6946EOM
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006947 }
6948 }
6949
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006950 if ($clean == 0 && $fix &&
6951 ("@rawlines" ne "@fixed" ||
6952 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Tom Rinia50f8de2014-02-27 08:27:28 -05006953 my $newfile = $filename;
6954 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6955 my $linecount = 0;
6956 my $f;
6957
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006958 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6959
Tom Rinia50f8de2014-02-27 08:27:28 -05006960 open($f, '>', $newfile)
6961 or die "$P: Can't open $newfile for write\n";
6962 foreach my $fixed_line (@fixed) {
6963 $linecount++;
6964 if ($file) {
6965 if ($linecount > 3) {
6966 $fixed_line =~ s/^\+//;
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006967 print $f $fixed_line . "\n";
Tom Rinia50f8de2014-02-27 08:27:28 -05006968 }
6969 } else {
6970 print $f $fixed_line . "\n";
6971 }
6972 }
6973 close($f);
6974
6975 if (!$quiet) {
6976 print << "EOM";
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006977
Tom Rinia50f8de2014-02-27 08:27:28 -05006978Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6979
6980Do _NOT_ trust the results written to this file.
6981Do _NOT_ submit these changes without inspecting them for correctness.
6982
6983This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6984No warranties, expressed or implied...
Tom Rinia50f8de2014-02-27 08:27:28 -05006985EOM
6986 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006987 }
6988
Heinrich Schuchardt8716f582017-09-12 09:57:45 +02006989 if ($quiet == 0) {
6990 print "\n";
6991 if ($clean == 1) {
6992 print "$vname has no obvious style problems and is ready for submission.\n";
6993 } else {
6994 print "$vname has style problems, please review.\n";
6995 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006996 }
Joe Hershbergerba2beb52011-10-18 10:06:59 +00006997 return $clean;
6998}