blob: 238f12cb469db068b2d3df3c471cde57e1f972d6 [file] [log] [blame]
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001#!/usr/bin/env perl
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002# SPDX-License-Identifier: GPL-2.0
3#
Joe Hershberger05622192011-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 Schuchardtc261fef2019-10-19 09:06:38 +02008# (c) 2010-2018 Joe Perches <joe@perches.com>
Joe Hershberger05622192011-10-18 10:06:59 +00009
10use strict;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020011use warnings;
Tom Rini6b9709d2014-02-27 08:27:28 -050012use POSIX;
Dan Murphyc10e0f52017-01-31 14:15:53 -060013use File::Basename;
14use Cwd 'abs_path';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020015use Term::ANSIColor qw(:constants);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +020016use Encode qw(decode encode);
Joe Hershberger05622192011-10-18 10:06:59 +000017
18my $P = $0;
Dan Murphyc10e0f52017-01-31 14:15:53 -060019my $D = dirname(abs_path($P));
Joe Hershberger05622192011-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 Schuchardt6305db92017-09-12 09:57:45 +020032my $showfile = 0;
Joe Hershberger05622192011-10-18 10:06:59 +000033my $file = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020034my $git = 0;
35my %git_commits = ();
Joe Hershberger05622192011-10-18 10:06:59 +000036my $check = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020037my $check_orig = 0;
Joe Hershberger05622192011-10-18 10:06:59 +000038my $summary = 1;
39my $mailback = 0;
40my $summary_file = 0;
41my $show_types = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020042my $list_types = 0;
Tom Rini6b9709d2014-02-27 08:27:28 -050043my $fix = 0;
44my $fix_inplace = 0;
Joe Hershberger05622192011-10-18 10:06:59 +000045my $root;
46my %debug;
Tom Rini6b9709d2014-02-27 08:27:28 -050047my %camelcase = ();
48my %use_type = ();
49my @use = ();
Joe Hershberger05622192011-10-18 10:06:59 +000050my %ignore_type = ();
51my @ignore = ();
52my $help = 0;
53my $configuration_file = ".checkpatch.conf";
Simon Glass048a6482020-05-22 16:32:35 -060054my $max_line_length = 100;
Tom Rini6b9709d2014-02-27 08:27:28 -050055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020057my $min_conf_desc_length = 4;
Dan Murphyc10e0f52017-01-31 14:15:53 -060058my $spelling_file = "$D/spelling.txt";
59my $codespell = 0;
60my $codespellfile = "/usr/share/codespell/dictionary.txt";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020061my $conststructsfile = "$D/const_structs.checkpatch";
62my $typedefsfile = "";
Simon Glassb77df592020-05-22 16:32:36 -060063my $u_boot = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020064my $color = "auto";
Tom Rinic57383b2020-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 Hershberger05622192011-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 Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +000095 -f, --file treat FILE as regular source file
96 --subjective, --strict enable more subjective tests
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020097 --list-types list the possible message types
Tom Rini6b9709d2014-02-27 08:27:28 -050098 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Hershberger05622192011-10-18 10:06:59 +000099 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200100 --show-types show the specific message type in the output
Simon Glass048a6482020-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 Schuchardt6305db92017-09-12 09:57:45 +0200104 --min-conf-desc-length=n set the min description length, if shorter, warn
Tom Rinic57383b2020-06-16 10:29:46 -0400105 --tab-size=n set the number of spaces for tab (default $tabsize)
Joe Hershberger05622192011-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 Rini6b9709d2014-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 Murphyc10e0f52017-01-31 14:15:53 -0600125 --codespell Use the codespell dictionary for spelling/typos
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200126 (default:/usr/share/codespell/dictionary.txt)
Dan Murphyc10e0f52017-01-31 14:15:53 -0600127 --codespellfile Use this codespell dictionary
Heinrich Schuchardt6305db92017-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 Glassb77df592020-05-22 16:32:36 -0600131 --u-boot Run additional checks for U-Boot
Joe Hershberger05622192011-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 Schuchardt6305db92017-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 Schuchardtc398f2d2018-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 Schuchardt6305db92017-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 Hershberger05622192011-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 Schuchardt6305db92017-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 Hershberger05622192011-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 Schuchardt6305db92017-09-12 09:57:45 +0200213 'showfile!' => \$showfile,
Joe Hershberger05622192011-10-18 10:06:59 +0000214 'f|file!' => \$file,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200215 'g|git!' => \$git,
Joe Hershberger05622192011-10-18 10:06:59 +0000216 'subjective!' => \$check,
217 'strict!' => \$check,
218 'ignore=s' => \@ignore,
Tom Rini6b9709d2014-02-27 08:27:28 -0500219 'types=s' => \@use,
Joe Hershberger05622192011-10-18 10:06:59 +0000220 'show-types!' => \$show_types,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200221 'list-types!' => \$list_types,
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000222 'max-line-length=i' => \$max_line_length,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200223 'min-conf-desc-length=i' => \$min_conf_desc_length,
Tom Rinic57383b2020-06-16 10:29:46 -0400224 'tab-size=i' => \$tabsize,
Joe Hershberger05622192011-10-18 10:06:59 +0000225 'root=s' => \$root,
226 'summary!' => \$summary,
227 'mailback!' => \$mailback,
228 'summary-file!' => \$summary_file,
Tom Rini6b9709d2014-02-27 08:27:28 -0500229 'fix!' => \$fix,
230 'fix-inplace!' => \$fix_inplace,
231 'ignore-perl-version!' => \$ignore_perl_version,
Joe Hershberger05622192011-10-18 10:06:59 +0000232 'debug=s' => \%debug,
233 'test-only=s' => \$tst_only,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200234 'codespell!' => \$codespell,
235 'codespellfile=s' => \$codespellfile,
236 'typedefsfile=s' => \$typedefsfile,
Simon Glassb77df592020-05-22 16:32:36 -0600237 'u-boot' => \$u_boot,
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +0000241 'h|help' => \$help,
242 'version' => \$help
243) or help(1);
244
245help(0) if ($help);
246
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200247list_types(0) if ($list_types);
248
Tom Rini6b9709d2014-02-27 08:27:28 -0500249$fix = 1 if ($fix_inplace);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200250$check_orig = $check;
Tom Rini6b9709d2014-02-27 08:27:28 -0500251
Joe Hershberger05622192011-10-18 10:06:59 +0000252my $exit = 0;
253
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200254my $perl_version_ok = 1;
Tom Rini6b9709d2014-02-27 08:27:28 -0500255if ($^V && $^V lt $minimum_perl_version) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200256 $perl_version_ok = 0;
Tom Rini6b9709d2014-02-27 08:27:28 -0500257 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200258 exit(1) if (!$ignore_perl_version);
Tom Rini6b9709d2014-02-27 08:27:28 -0500259}
260
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200261#if no filenames are given, push '-' to read patch from stdin
Joe Hershberger05622192011-10-18 10:06:59 +0000262if ($#ARGV < 0) {
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +0000276}
277
Tom Rinic57383b2020-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 Rini6b9709d2014-02-27 08:27:28 -0500281sub hash_save_array_words {
282 my ($hashRef, $arrayRef) = @_;
Joe Hershberger05622192011-10-18 10:06:59 +0000283
Tom Rini6b9709d2014-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 Hershberger05622192011-10-18 10:06:59 +0000290
Tom Rini6b9709d2014-02-27 08:27:28 -0500291 next if ($word =~ m/^\s*#/);
292 next if ($word =~ m/^\s*$/);
293
294 $hashRef->{$word}++;
295 }
Joe Hershberger05622192011-10-18 10:06:59 +0000296}
297
Tom Rini6b9709d2014-02-27 08:27:28 -0500298sub hash_show_words {
299 my ($hashRef, $prefix) = @_;
300
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200301 if (keys %$hashRef) {
302 print "\nNOTE: $prefix message types:";
Tom Rini6b9709d2014-02-27 08:27:28 -0500303 foreach my $word (sort keys %$hashRef) {
304 print " $word";
305 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200306 print "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -0500307 }
308}
309
310hash_save_array_words(\%ignore_type, \@ignore);
311hash_save_array_words(\%use_type, \@use);
312
Joe Hershberger05622192011-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 Hershberger05622192011-10-18 10:06:59 +0000363 __kprobes|
364 __ref|
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200365 __refconst|
366 __refdata|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200367 __rcu|
368 __private
Joe Hershberger05622192011-10-18 10:06:59 +0000369 }x;
Tom Rini6b9709d2014-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 Hershberger05622192011-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 Schuchardt6305db92017-09-12 09:57:45 +0200383 __bitwise|
Joe Hershberger05622192011-10-18 10:06:59 +0000384 __packed__|
385 __packed2__|
386 __naked|
387 __maybe_unused|
388 __always_unused|
389 __noreturn|
390 __used|
391 __cold|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200392 __pure|
Joe Hershberger05622192011-10-18 10:06:59 +0000393 __noclone|
394 __deprecated|
395 __read_mostly|
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200396 __ro_after_init|
Joe Hershberger05622192011-10-18 10:06:59 +0000397 __kprobes|
Tom Rini6b9709d2014-02-27 08:27:28 -0500398 $InitAttribute|
Joe Hershberger05622192011-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 Schuchardt6305db92017-09-12 09:57:45 +0200405our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Joe Hershberger05622192011-10-18 10:06:59 +0000406our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
407our $Lval = qr{$Ident(?:$Member)*};
408
Tom Rini6b9709d2014-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 Schuchardt6305db92017-09-12 09:57:45 +0200413our $Octal = qr{0[0-7]+$Int_type?};
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200414our $String = qr{"[X\t]*"};
Kim Phillipsd45a6ae2013-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 Schuchardt6305db92017-09-12 09:57:45 +0200419our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000420our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200421our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Tom Rini6b9709d2014-02-27 08:27:28 -0500422our $Arithmetic = qr{\+|-|\*|\/|%};
Joe Hershberger05622192011-10-18 10:06:59 +0000423our $Operators = qr{
424 <=|>=|==|!=|
425 =>|->|<<|>>|<|>|!|~|
Tom Rini6b9709d2014-02-27 08:27:28 -0500426 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Joe Hershberger05622192011-10-18 10:06:59 +0000427 }x;
428
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +0000432our $NonptrType;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200433our $NonptrTypeMisordered;
Tom Rini6b9709d2014-02-27 08:27:28 -0500434our $NonptrTypeWithAttr;
Joe Hershberger05622192011-10-18 10:06:59 +0000435our $Type;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200436our $TypeMisordered;
Joe Hershberger05622192011-10-18 10:06:59 +0000437our $Declare;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200438our $DeclareMisordered;
Joe Hershberger05622192011-10-18 10:06:59 +0000439
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000440our $NON_ASCII_UTF8 = qr{
441 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Joe Hershberger05622192011-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 Phillipsd45a6ae2013-02-28 12:53:52 +0000450our $UTF8 = qr{
451 [\x09\x0A\x0D\x20-\x7E] # ASCII
452 | $NON_ASCII_UTF8
453}x;
454
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +0000461 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
462 atomic_t
463)};
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +0000471
472our $logFunctions = qr{(?x:
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200473 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Tom Rini6b9709d2014-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 Schuchardtc398f2d2018-04-04 15:39:20 +0200475 TP_printk|
Joe Hershberger05622192011-10-18 10:06:59 +0000476 WARN(?:_RATELIMIT|_ONCE|)|
477 panic|
James Byrne66b3ccc2019-11-21 14:32:46 +0000478 debug|
479 printf|
Tom Rini6b9709d2014-02-27 08:27:28 -0500480 MODULE_[A-Z_]+|
481 seq_vprintf|seq_printf|seq_puts
Joe Hershberger05622192011-10-18 10:06:59 +0000482)};
483
Tom Rinic57383b2020-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 Hershberger05622192011-10-18 10:06:59 +0000494our $signature_tags = qr{(?xi:
495 Signed-off-by:|
Tom Rinic57383b2020-06-16 10:29:46 -0400496 Co-developed-by:|
Joe Hershberger05622192011-10-18 10:06:59 +0000497 Acked-by:|
498 Tested-by:|
499 Reviewed-by:|
500 Reported-by:|
Tom Rini6b9709d2014-02-27 08:27:28 -0500501 Suggested-by:|
Joe Hershberger05622192011-10-18 10:06:59 +0000502 To:|
503 Cc:
504)};
505
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +0000525our @typeList = (
526 qr{void},
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-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 Schuchardt6305db92017-09-12 09:57:45 +0200545 @typeListMisordered,
Joe Hershberger05622192011-10-18 10:06:59 +0000546);
Heinrich Schuchardt6305db92017-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 Rini6b9709d2014-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 Hershberger05622192011-10-18 10:06:59 +0000576our @modifierList = (
577 qr{fastcall},
578);
Heinrich Schuchardt6305db92017-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 Schuchardtc398f2d2018-04-04 15:39:20 +0200600$mode_perms_search = "(?:${mode_perms_search})";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200601
Tom Rinic57383b2020-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 Schuchardt6305db92017-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);
649
650#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 Schuchardtc398f2d2018-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}
Joe Hershberger05622192011-10-18 10:06:59 +0000687
688our $allowed_asm_includes = qr{(?x:
689 irq|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200690 memory|
691 time|
692 reboot
Joe Hershberger05622192011-10-18 10:06:59 +0000693)};
694# memory.h: ARM has a custom one
695
Dan Murphyc10e0f52017-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 Schuchardt6305db92017-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 Murphyc10e0f52017-01-31 14:15:53 -0600782
Joe Hershberger05622192011-10-18 10:06:59 +0000783sub build_types {
Heinrich Schuchardt6305db92017-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 Rini6b9709d2014-02-27 08:27:28 -0500787 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Joe Hershberger05622192011-10-18 10:06:59 +0000788 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200789 $BasicType = qr{
790 (?:$typeTypedefs\b)|
791 (?:${all}\b)
792 }x;
Joe Hershberger05622192011-10-18 10:06:59 +0000793 $NonptrType = qr{
794 (?:$Modifier\s+|const\s+)*
795 (?:
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000796 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Hershberger05622192011-10-18 10:06:59 +0000797 (?:$typeTypedefs\b)|
798 (?:${all}\b)
799 )
800 (?:\s+$Modifier|\s+const)*
801 }x;
Heinrich Schuchardt6305db92017-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 Rini6b9709d2014-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 Hershberger05622192011-10-18 10:06:59 +0000818 $Type = qr{
819 $NonptrType
Tom Rinic57383b2020-06-16 10:29:46 -0400820 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Hershberger05622192011-10-18 10:06:59 +0000821 (?:\s+$Inline|\s+$Modifier)*
822 }x;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200823 $TypeMisordered = qr{
824 $NonptrTypeMisordered
Tom Rinic57383b2020-06-16 10:29:46 -0400825 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +0000830}
831build_types();
832
Joe Hershberger05622192011-10-18 10:06:59 +0000833our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Kim Phillipsd45a6ae2013-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 Schuchardt6305db92017-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 Schuchardtc398f2d2018-04-04 15:39:20 +0200846 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
847 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200848)};
Joe Hershberger05622192011-10-18 10:06:59 +0000849
850sub deparenthesize {
851 my ($string) = @_;
852 return "" if (!defined($string));
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200853
854 while ($string =~ /^\s*\(.*\)\s*$/) {
855 $string =~ s@^\s*\(\s*@@;
856 $string =~ s@\s*\)\s*$@@;
857 }
858
Joe Hershberger05622192011-10-18 10:06:59 +0000859 $string =~ s@\s+@ @g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200860
Joe Hershberger05622192011-10-18 10:06:59 +0000861 return $string;
862}
863
Tom Rini6b9709d2014-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 Rinic57383b2020-06-16 10:29:46 -0400890our %maintained_status = ();
891
Heinrich Schuchardt6305db92017-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 Rinic57383b2020-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 Schuchardt6305db92017-09-12 09:57:45 +0200900
Tom Rinic57383b2020-06-16 10:29:46 -0400901 return $maintained_status{$filename} =~ /obsolete/i;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200902}
903
Heinrich Schuchardtc261fef2019-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 Rini6b9709d2014-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 Rinic57383b2020-06-16 10:29:46 -0400926 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Tom Rini6b9709d2014-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 Rinic57383b2020-06-16 10:29:46 -0400954 $files = `${git_command} ls-files "include/*.h"`;
Tom Rini6b9709d2014-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 }
971}
972
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200973sub git_commit_info {
974 my ($commit, $id, $desc) = @_;
975
976 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
977
Tom Rinic57383b2020-06-16 10:29:46 -0400978 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Heinrich Schuchardt6305db92017-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 Rinic57383b2020-06-16 10:29:46 -0400984 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Heinrich Schuchardt6305db92017-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);
1002}
1003
Joe Hershberger05622192011-10-18 10:06:59 +00001004$chk_signoff = 0 if ($file);
1005
Joe Hershberger05622192011-10-18 10:06:59 +00001006my @rawlines = ();
1007my @lines = ();
Tom Rini6b9709d2014-02-27 08:27:28 -05001008my @fixed = ();
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001009my @fixed_inserted = ();
1010my @fixed_deleted = ();
Dan Murphyc10e0f52017-01-31 14:15:53 -06001011my $fixlinenr = -1;
1012
Heinrich Schuchardt6305db92017-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 Rinic57383b2020-06-16 10:29:46 -04001028 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Heinrich Schuchardt6305db92017-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 Rinic57383b2020-06-16 10:29:46 -04001043$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Joe Hershberger05622192011-10-18 10:06:59 +00001044for my $filename (@ARGV) {
1045 my $FILE;
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-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 Schuchardt6305db92017-09-12 09:57:45 +02001060 } elsif ($git) {
1061 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Joe Hershberger05622192011-10-18 10:06:59 +00001062 } else {
1063 $vname = $filename;
1064 }
1065 while (<$FILE>) {
1066 chomp;
1067 push(@rawlines, $_);
1068 }
1069 close($FILE);
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +00001077 if (!process($filename)) {
1078 $exit = 1;
1079 }
1080 @rawlines = ();
1081 @lines = ();
Tom Rini6b9709d2014-02-27 08:27:28 -05001082 @fixed = ();
Heinrich Schuchardt6305db92017-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 Schuchardtc261fef2019-10-19 09:06:38 +02001095 if (!$perl_version_ok) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001096 print << "EOM"
1097
1098NOTE: perl $^V is not modern enough to detect all possible issues.
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001099 An upgrade to at least perl $minimum_perl_version is suggested.
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +00001109}
1110
1111exit($exit);
1112
1113sub top_of_kernel_tree {
1114 my ($root) = @_;
1115
1116 my @tree_check = (
Tom Rini6b9709d2014-02-27 08:27:28 -05001117 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
Joe Hershberger05622192011-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 Phillipsd45a6ae2013-02-28 12:53:52 +00001128}
Joe Hershberger05622192011-10-18 10:06:59 +00001129
1130sub parse_email {
1131 my ($formatted_email) = @_;
1132
1133 my $name = "";
Tom Rinic57383b2020-06-16 10:29:46 -04001134 my $name_comment = "";
Joe Hershberger05622192011-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 Schuchardtc398f2d2018-04-04 15:39:20 +02001148 $formatted_email =~ s/\Q$address\E.*$//;
Joe Hershberger05622192011-10-18 10:06:59 +00001149 $name = $formatted_email;
Tom Rini6b9709d2014-02-27 08:27:28 -05001150 $name = trim($name);
Joe Hershberger05622192011-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 Rini6b9709d2014-02-27 08:27:28 -05001165 $name = trim($name);
Joe Hershberger05622192011-10-18 10:06:59 +00001166 $name =~ s/^\"|\"$//g;
Tom Rinic57383b2020-06-16 10:29:46 -04001167 $name =~ s/(\s*\([^\)]+\))\s*//;
1168 if (defined($1)) {
1169 $name_comment = trim($1);
1170 }
Tom Rini6b9709d2014-02-27 08:27:28 -05001171 $address = trim($address);
Joe Hershberger05622192011-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 Rinic57383b2020-06-16 10:29:46 -04001179 return ($name, $name_comment, $address, $comment);
Joe Hershberger05622192011-10-18 10:06:59 +00001180}
1181
1182sub format_email {
1183 my ($name, $address) = @_;
1184
1185 my $formatted_email;
1186
Tom Rini6b9709d2014-02-27 08:27:28 -05001187 $name = trim($name);
Joe Hershberger05622192011-10-18 10:06:59 +00001188 $name =~ s/^\"|\"$//g;
Tom Rini6b9709d2014-02-27 08:27:28 -05001189 $address = trim($address);
Joe Hershberger05622192011-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 Rinic57383b2020-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 Schuchardt6305db92017-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 Hershberger05622192011-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 Rinic57383b2020-06-16 10:29:46 -04001255 for (; ($n % $tabsize) != 0; $n++) {
Joe Hershberger05622192011-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 Schuchardtc398f2d2018-04-04 15:39:20 +02001311 # Comments we are whacking completely including the begin
Joe Hershberger05622192011-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 Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +00001385 return $res;
1386}
1387
Tom Rini6b9709d2014-02-27 08:27:28 -05001388sub get_quoted_string {
1389 my ($line, $rawline) = @_;
1390
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001391 return "" if (!defined($line) || !defined($rawline));
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001392 return "" if ($line !~ m/($String)/g);
Tom Rini6b9709d2014-02-27 08:27:28 -05001393 return substr($rawline, $-[0], $+[0] - $-[0]);
1394}
1395
Joe Hershberger05622192011-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 Phillipsd45a6ae2013-02-28 12:53:52 +00001436 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1437 $level++;
1438 $type = '#';
1439 }
Joe Hershberger05622192011-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 Phillipsd45a6ae2013-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 Hershberger05622192011-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 Schuchardtc398f2d2018-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 Hershberger05622192011-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 Phillipsd45a6ae2013-02-28 12:53:52 +00001813 $type = 'c';
Joe Hershberger05622192011-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 Phillipsd45a6ae2013-02-28 12:53:52 +00002005 do|
2006 \#|
2007 \#\#|
Joe Hershberger05622192011-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 Schuchardt6305db92017-09-12 09:57:45 +02002023 push(@modifierListFile, $modifier);
Joe Hershberger05622192011-10-18 10:06:59 +00002024 }
2025 }
2026
2027 } else {
2028 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002029 push(@typeListFile, $possible);
Joe Hershberger05622192011-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 Schuchardt6305db92017-09-12 09:57:45 +02002040 my ($type) = @_;
Tom Rini6b9709d2014-02-27 08:27:28 -05002041
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002042 $type =~ tr/[a-z]/[A-Z]/;
2043
2044 return defined $use_type{$type} if (scalar keys %use_type > 0);
2045
2046 return !defined $ignore_type{$type};
Joe Hershberger05622192011-10-18 10:06:59 +00002047}
2048
2049sub report {
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +00002054 return 0;
2055 }
Heinrich Schuchardt6305db92017-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 }
Joe Hershberger05622192011-10-18 10:06:59 +00002065 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002066 $output .= $prefix . $level . ':';
2067 if ($show_types) {
2068 $output .= BLUE if ($color);
2069 $output .= "$type:";
2070 }
2071 $output .= RESET if ($color);
2072 $output .= ' ' . $msg . "\n";
Joe Hershberger05622192011-10-18 10:06:59 +00002073
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002074 if ($showfile) {
2075 my @lines = split("\n", $output, -1);
2076 splice(@lines, 1, 1);
2077 $output = join("\n", @lines);
2078 }
2079 $output = (split('\n', $output))[0] . "\n" if ($terse);
2080
2081 push(our @report, $output);
Joe Hershberger05622192011-10-18 10:06:59 +00002082
2083 return 1;
2084}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002085
Joe Hershberger05622192011-10-18 10:06:59 +00002086sub report_dump {
2087 our @report;
2088}
2089
Heinrich Schuchardt6305db92017-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 Hershberger05622192011-10-18 10:06:59 +00002174sub ERROR {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002175 my ($type, $msg) = @_;
2176
2177 if (report("ERROR", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002178 our $clean = 0;
2179 our $cnt_error++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002180 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002181 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002182 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002183}
2184sub WARN {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002185 my ($type, $msg) = @_;
2186
2187 if (report("WARNING", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002188 our $clean = 0;
2189 our $cnt_warn++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002190 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002191 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002192 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002193}
2194sub CHK {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002195 my ($type, $msg) = @_;
2196
2197 if ($check && report("CHECK", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002198 our $clean = 0;
2199 our $cnt_chk++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002200 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002201 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002202 return 0;
Joe Hershberger05622192011-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 Rini6b9709d2014-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 Rinic57383b2020-06-16 10:29:46 -04002268 my $source_indent = $tabsize;
Tom Rini6b9709d2014-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 Phillipsd45a6ae2013-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 Schuchardt6305db92017-09-12 09:57:45 +02002307 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002308}
2309
Tom Rinic57383b2020-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 Glassb77df592020-05-22 16:32:36 -06002323# Checks specific to U-Boot
2324sub u_boot_line {
2325 my ($realfile, $line, $herecurr) = @_;
Simon Glass281236c2020-05-22 16:32:37 -06002326
2327 # ask for a test if a new uclass ID is added
2328 if ($realfile =~ /uclass-id.h/ && $line =~ /^\+/) {
2329 WARN("NEW_UCLASS",
2330 "Possible new uclass - make sure to add a sandbox driver, plus a test in test/dm/<name>.c\n" . $herecurr);
2331 }
Simon Glass7fc7d242020-05-22 16:32:38 -06002332
2333 # try to get people to use the livetree API
2334 if ($line =~ /^\+.*fdtdec_/) {
2335 WARN("LIVETREE",
2336 "Use the livetree API (dev_read_...)\n" . $herecurr);
2337 }
Simon Glass58978112020-05-22 16:32:39 -06002338
2339 # add tests for new commands
2340 if ($line =~ /^\+.*do_($Ident)\(struct cmd_tbl.*/) {
2341 WARN("CMD_TEST",
2342 "Possible new command - make sure you add a test\n" . $herecurr);
2343 }
Simon Glassdd5b0fa2020-05-22 16:32:40 -06002344
2345 # use if instead of #if
Simon Glass8af45b12020-06-14 10:54:08 -06002346 if ($realfile =~ /\.c$/ && $line =~ /^\+#if.*CONFIG.*/) {
Simon Glassdd5b0fa2020-05-22 16:32:40 -06002347 WARN("PREFER_IF",
2348 "Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef' where possible\n" . $herecurr);
2349 }
Tom Rinif3e2ebe2020-05-26 14:29:02 -04002350
2351 # use defconfig to manage CONFIG_CMD options
2352 if ($line =~ /\+\s*#\s*(define|undef)\s+(CONFIG_CMD\w*)\b/) {
2353 ERROR("DEFINE_CONFIG_CMD",
2354 "All commands are managed by Kconfig\n" . $herecurr);
2355 }
Simon Glassb77df592020-05-22 16:32:36 -06002356}
2357
Joe Hershberger05622192011-10-18 10:06:59 +00002358sub process {
2359 my $filename = shift;
2360
2361 my $linenr=0;
2362 my $prevline="";
2363 my $prevrawline="";
2364 my $stashline="";
2365 my $stashrawline="";
2366
2367 my $length;
2368 my $indent;
2369 my $previndent=0;
2370 my $stashindent=0;
2371
2372 our $clean = 1;
2373 my $signoff = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002374 my $author = '';
2375 my $authorsignoff = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002376 my $is_patch = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002377 my $is_binding_patch = -1;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002378 my $in_header_lines = $file ? 0 : 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002379 my $in_commit_log = 0; #Scanning lines before patch
Tom Rinic57383b2020-06-16 10:29:46 -04002380 my $has_patch_separator = 0; #Found a --- line
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002381 my $has_commit_log = 0; #Encountered lines before patch
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002382 my $commit_log_lines = 0; #Number of commit log lines
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002383 my $commit_log_possible_stack_dump = 0;
2384 my $commit_log_long_line = 0;
2385 my $commit_log_has_diff = 0;
2386 my $reported_maintainer_file = 0;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002387 my $non_utf8_charset = 0;
2388
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002389 my $last_blank_line = 0;
2390 my $last_coalesced_string_linenr = -1;
2391
Joe Hershberger05622192011-10-18 10:06:59 +00002392 our @report = ();
2393 our $cnt_lines = 0;
2394 our $cnt_error = 0;
2395 our $cnt_warn = 0;
2396 our $cnt_chk = 0;
2397
2398 # Trace the real file/line as we go.
2399 my $realfile = '';
2400 my $realline = 0;
2401 my $realcnt = 0;
2402 my $here = '';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002403 my $context_function; #undef'd unless there's a known function
Joe Hershberger05622192011-10-18 10:06:59 +00002404 my $in_comment = 0;
2405 my $comment_edge = 0;
2406 my $first_line = 0;
2407 my $p1_prefix = '';
2408
2409 my $prev_values = 'E';
2410
2411 # suppression flags
2412 my %suppress_ifbraces;
2413 my %suppress_whiletrailers;
2414 my %suppress_export;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002415 my $suppress_statement = 0;
2416
Tom Rini6b9709d2014-02-27 08:27:28 -05002417 my %signatures = ();
Joe Hershberger05622192011-10-18 10:06:59 +00002418
2419 # Pre-scan the patch sanitizing the lines.
2420 # Pre-scan the patch looking for any __setup documentation.
2421 #
2422 my @setup_docs = ();
2423 my $setup_docs = 0;
2424
Tom Rini6b9709d2014-02-27 08:27:28 -05002425 my $camelcase_file_seeded = 0;
2426
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002427 my $checklicenseline = 1;
2428
Joe Hershberger05622192011-10-18 10:06:59 +00002429 sanitise_line_reset();
2430 my $line;
2431 foreach my $rawline (@rawlines) {
2432 $linenr++;
2433 $line = $rawline;
2434
Tom Rini6b9709d2014-02-27 08:27:28 -05002435 push(@fixed, $rawline) if ($fix);
2436
Joe Hershberger05622192011-10-18 10:06:59 +00002437 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2438 $setup_docs = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002439 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Joe Hershberger05622192011-10-18 10:06:59 +00002440 $setup_docs = 1;
2441 }
2442 #next;
2443 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002444 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Joe Hershberger05622192011-10-18 10:06:59 +00002445 $realline=$1-1;
2446 if (defined $2) {
2447 $realcnt=$3+1;
2448 } else {
2449 $realcnt=1+1;
2450 }
2451 $in_comment = 0;
2452
2453 # Guestimate if this is a continuing comment. Run
2454 # the context looking for a comment "edge". If this
2455 # edge is a close comment then we must be in a comment
2456 # at context start.
2457 my $edge;
2458 my $cnt = $realcnt;
2459 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2460 next if (defined $rawlines[$ln - 1] &&
2461 $rawlines[$ln - 1] =~ /^-/);
2462 $cnt--;
2463 #print "RAW<$rawlines[$ln - 1]>\n";
2464 last if (!defined $rawlines[$ln - 1]);
2465 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2466 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2467 ($edge) = $1;
2468 last;
2469 }
2470 }
2471 if (defined $edge && $edge eq '*/') {
2472 $in_comment = 1;
2473 }
2474
2475 # Guestimate if this is a continuing comment. If this
2476 # is the start of a diff block and this line starts
2477 # ' *' then it is very likely a comment.
2478 if (!defined $edge &&
2479 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2480 {
2481 $in_comment = 1;
2482 }
2483
2484 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2485 sanitise_line_reset($in_comment);
2486
2487 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2488 # Standardise the strings and chars within the input to
2489 # simplify matching -- only bother with positive lines.
2490 $line = sanitise_line($rawline);
2491 }
2492 push(@lines, $line);
2493
2494 if ($realcnt > 1) {
2495 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2496 } else {
2497 $realcnt = 0;
2498 }
2499
2500 #print "==>$rawline\n";
2501 #print "-->$line\n";
2502
2503 if ($setup_docs && $line =~ /^\+/) {
2504 push(@setup_docs, $line);
2505 }
2506 }
2507
2508 $prefix = '';
2509
2510 $realcnt = 0;
2511 $linenr = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002512 $fixlinenr = -1;
Joe Hershberger05622192011-10-18 10:06:59 +00002513 foreach my $line (@lines) {
2514 $linenr++;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002515 $fixlinenr++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002516 my $sline = $line; #copy of $line
2517 $sline =~ s/$;/ /g; #with comments as spaces
Joe Hershberger05622192011-10-18 10:06:59 +00002518
2519 my $rawline = $rawlines[$linenr - 1];
Tom Rinic57383b2020-06-16 10:29:46 -04002520 my $raw_comment = get_raw_comment($line, $rawline);
Joe Hershberger05622192011-10-18 10:06:59 +00002521
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002522# check if it's a mode change, rename or start of a patch
2523 if (!$in_commit_log &&
2524 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2525 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2526 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2527 $is_patch = 1;
2528 }
2529
Joe Hershberger05622192011-10-18 10:06:59 +00002530#extract the line range in the file after the patch is applied
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002531 if (!$in_commit_log &&
2532 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2533 my $context = $4;
Joe Hershberger05622192011-10-18 10:06:59 +00002534 $is_patch = 1;
2535 $first_line = $linenr + 1;
2536 $realline=$1-1;
2537 if (defined $2) {
2538 $realcnt=$3+1;
2539 } else {
2540 $realcnt=1+1;
2541 }
2542 annotate_reset();
2543 $prev_values = 'E';
2544
2545 %suppress_ifbraces = ();
2546 %suppress_whiletrailers = ();
2547 %suppress_export = ();
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002548 $suppress_statement = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002549 if ($context =~ /\b(\w+)\s*\(/) {
2550 $context_function = $1;
2551 } else {
2552 undef $context_function;
2553 }
Joe Hershberger05622192011-10-18 10:06:59 +00002554 next;
2555
2556# track the line number as we move through the hunk, note that
2557# new versions of GNU diff omit the leading space on completely
2558# blank context lines so we need to count that too.
2559 } elsif ($line =~ /^( |\+|$)/) {
2560 $realline++;
2561 $realcnt-- if ($realcnt != 0);
2562
2563 # Measure the line length and indent.
2564 ($length, $indent) = line_stats($rawline);
2565
2566 # Track the previous line.
2567 ($prevline, $stashline) = ($stashline, $line);
2568 ($previndent, $stashindent) = ($stashindent, $indent);
2569 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2570
2571 #warn "line<$line>\n";
2572
2573 } elsif ($realcnt == 1) {
2574 $realcnt--;
2575 }
2576
2577 my $hunk_line = ($realcnt != 0);
2578
Joe Hershberger05622192011-10-18 10:06:59 +00002579 $here = "#$linenr: " if (!$file);
2580 $here = "#$realline: " if ($file);
2581
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002582 my $found_file = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002583 # extract the filename as it passes
2584 if ($line =~ /^diff --git.*?(\S+)$/) {
2585 $realfile = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05002586 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002587 $in_commit_log = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002588 $found_file = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002589 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2590 $realfile = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05002591 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002592 $in_commit_log = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002593
2594 $p1_prefix = $1;
2595 if (!$file && $tree && $p1_prefix ne '' &&
2596 -e "$root/$p1_prefix") {
2597 WARN("PATCH_PREFIX",
2598 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2599 }
2600
2601 if ($realfile =~ m@^include/asm/@) {
2602 ERROR("MODIFIED_INCLUDE_ASM",
2603 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2604 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002605 $found_file = 1;
2606 }
2607
2608#make up the handle for any error we report on this line
2609 if ($showfile) {
2610 $prefix = "$realfile:$realline: "
2611 } elsif ($emacs) {
2612 if ($file) {
2613 $prefix = "$filename:$realline: ";
2614 } else {
2615 $prefix = "$filename:$linenr: ";
2616 }
2617 }
2618
2619 if ($found_file) {
2620 if (is_maintained_obsolete($realfile)) {
2621 WARN("OBSOLETE",
2622 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2623 }
2624 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2625 $check = 1;
2626 } else {
2627 $check = $check_orig;
2628 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002629 $checklicenseline = 1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002630
2631 if ($realfile !~ /^MAINTAINERS/) {
2632 my $last_binding_patch = $is_binding_patch;
2633
2634 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2635
2636 if (($last_binding_patch != -1) &&
2637 ($last_binding_patch ^ $is_binding_patch)) {
2638 WARN("DT_SPLIT_BINDING_PATCH",
2639 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2640 }
2641 }
2642
Joe Hershberger05622192011-10-18 10:06:59 +00002643 next;
2644 }
2645
2646 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2647
2648 my $hereline = "$here\n$rawline\n";
2649 my $herecurr = "$here\n$rawline\n";
2650 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2651
2652 $cnt_lines++ if ($realcnt != 0);
2653
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002654# Verify the existence of a commit log if appropriate
2655# 2 is used because a $signature is counted in $commit_log_lines
2656 if ($in_commit_log) {
2657 if ($line !~ /^\s*$/) {
2658 $commit_log_lines++; #could be a $signature
2659 }
2660 } elsif ($has_commit_log && $commit_log_lines < 2) {
2661 WARN("COMMIT_MESSAGE",
2662 "Missing commit description - Add an appropriate one\n");
2663 $commit_log_lines = 2; #warn only once
2664 }
2665
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002666# Check if the commit log has what seems like a diff which can confuse patch
2667 if ($in_commit_log && !$commit_log_has_diff &&
2668 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2669 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2670 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2671 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2672 ERROR("DIFF_IN_COMMIT_MSG",
2673 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2674 $commit_log_has_diff = 1;
2675 }
2676
Joe Hershberger05622192011-10-18 10:06:59 +00002677# Check for incorrect file permissions
2678 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2679 my $permhere = $here . "FILE: $realfile\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05002680 if ($realfile !~ m@scripts/@ &&
2681 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Hershberger05622192011-10-18 10:06:59 +00002682 ERROR("EXECUTE_PERMISSIONS",
2683 "do not set execute permissions for source files\n" . $permhere);
2684 }
2685 }
2686
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002687# Check the patch for a From:
2688 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2689 $author = $1;
2690 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2691 $author =~ s/"//g;
Tom Rinic57383b2020-06-16 10:29:46 -04002692 $author = reformat_email($author);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002693 }
2694
Joe Hershberger05622192011-10-18 10:06:59 +00002695# Check the patch for a signoff:
Tom Rinic57383b2020-06-16 10:29:46 -04002696 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Joe Hershberger05622192011-10-18 10:06:59 +00002697 $signoff++;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002698 $in_commit_log = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002699 if ($author ne '') {
Tom Rinic57383b2020-06-16 10:29:46 -04002700 if (same_email_addresses($1, $author)) {
2701 $authorsignoff = 1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002702 }
2703 }
Joe Hershberger05622192011-10-18 10:06:59 +00002704 }
2705
Tom Rinic57383b2020-06-16 10:29:46 -04002706# Check for patch separator
2707 if ($line =~ /^---$/) {
2708 $has_patch_separator = 1;
2709 $in_commit_log = 0;
2710 }
2711
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002712# Check if MAINTAINERS is being updated. If so, there's probably no need to
2713# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2714 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2715 $reported_maintainer_file = 1;
2716 }
2717
Joe Hershberger05622192011-10-18 10:06:59 +00002718# Check signature styles
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002719 if (!$in_header_lines &&
2720 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Hershberger05622192011-10-18 10:06:59 +00002721 my $space_before = $1;
2722 my $sign_off = $2;
2723 my $space_after = $3;
2724 my $email = $4;
2725 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2726
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002727 if ($sign_off !~ /$signature_tags/) {
2728 WARN("BAD_SIGN_OFF",
2729 "Non-standard signature: $sign_off\n" . $herecurr);
2730 }
Joe Hershberger05622192011-10-18 10:06:59 +00002731 if (defined $space_before && $space_before ne "") {
Tom Rini6b9709d2014-02-27 08:27:28 -05002732 if (WARN("BAD_SIGN_OFF",
2733 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2734 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002735 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05002736 "$ucfirst_sign_off $email";
2737 }
Joe Hershberger05622192011-10-18 10:06:59 +00002738 }
2739 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Tom Rini6b9709d2014-02-27 08:27:28 -05002740 if (WARN("BAD_SIGN_OFF",
2741 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2742 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002743 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05002744 "$ucfirst_sign_off $email";
2745 }
2746
Joe Hershberger05622192011-10-18 10:06:59 +00002747 }
2748 if (!defined $space_after || $space_after ne " ") {
Tom Rini6b9709d2014-02-27 08:27:28 -05002749 if (WARN("BAD_SIGN_OFF",
2750 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2751 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002752 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05002753 "$ucfirst_sign_off $email";
2754 }
Joe Hershberger05622192011-10-18 10:06:59 +00002755 }
2756
Tom Rinic57383b2020-06-16 10:29:46 -04002757 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Hershberger05622192011-10-18 10:06:59 +00002758 my $suggested_email = format_email(($email_name, $email_address));
2759 if ($suggested_email eq "") {
2760 ERROR("BAD_SIGN_OFF",
2761 "Unrecognized email address: '$email'\n" . $herecurr);
2762 } else {
2763 my $dequoted = $suggested_email;
2764 $dequoted =~ s/^"//;
2765 $dequoted =~ s/" </ </;
2766 # Don't force email to have quotes
2767 # Allow just an angle bracketed address
Tom Rinic57383b2020-06-16 10:29:46 -04002768 if (!same_email_addresses($email, $suggested_email)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002769 WARN("BAD_SIGN_OFF",
2770 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2771 }
2772 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002773
2774# Check for duplicate signatures
2775 my $sig_nospace = $line;
2776 $sig_nospace =~ s/\s//g;
2777 $sig_nospace = lc($sig_nospace);
2778 if (defined $signatures{$sig_nospace}) {
2779 WARN("BAD_SIGN_OFF",
2780 "Duplicate signature\n" . $herecurr);
2781 } else {
2782 $signatures{$sig_nospace} = 1;
2783 }
Tom Rinic57383b2020-06-16 10:29:46 -04002784
2785# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2786 if ($sign_off =~ /^co-developed-by:$/i) {
2787 if ($email eq $author) {
2788 WARN("BAD_SIGN_OFF",
2789 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2790 }
2791 if (!defined $lines[$linenr]) {
2792 WARN("BAD_SIGN_OFF",
2793 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2794 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2795 WARN("BAD_SIGN_OFF",
2796 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2797 } elsif ($1 ne $email) {
2798 WARN("BAD_SIGN_OFF",
2799 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2800 }
2801 }
Joe Hershberger05622192011-10-18 10:06:59 +00002802 }
2803
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002804# Check email subject for common tools that don't need to be mentioned
2805 if ($in_header_lines &&
2806 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2807 WARN("EMAIL_SUBJECT",
2808 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2809 }
2810
Tom Rinic57383b2020-06-16 10:29:46 -04002811# Check for Gerrit Change-Ids not in any patch context
2812 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002813 ERROR("GERRIT_CHANGE_ID",
Tom Rinic57383b2020-06-16 10:29:46 -04002814 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002815 }
2816
2817# Check if the commit log is in a possible stack dump
2818 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2819 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2820 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2821 # timestamp
Tom Rinic57383b2020-06-16 10:29:46 -04002822 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2823 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2824 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2825 # stack dump address styles
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002826 $commit_log_possible_stack_dump = 1;
2827 }
2828
2829# Check for line lengths > 75 in commit log, warn once
2830 if ($in_commit_log && !$commit_log_long_line &&
2831 length($line) > 75 &&
2832 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2833 # file delta changes
2834 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2835 # filename then :
2836 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2837 # A Fixes: or Link: line
2838 $commit_log_possible_stack_dump)) {
2839 WARN("COMMIT_LOG_LONG_LINE",
2840 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2841 $commit_log_long_line = 1;
2842 }
2843
2844# Reset possible stack dump if a blank line is found
2845 if ($in_commit_log && $commit_log_possible_stack_dump &&
2846 $line =~ /^\s*$/) {
2847 $commit_log_possible_stack_dump = 0;
2848 }
2849
2850# Check for git id commit length and improperly formed commit descriptions
2851 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Tom Rinic57383b2020-06-16 10:29:46 -04002852 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002853 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2854 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2855 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2856 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2857 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2858 my $init_char = "c";
2859 my $orig_commit = "";
2860 my $short = 1;
2861 my $long = 0;
2862 my $case = 1;
2863 my $space = 1;
2864 my $hasdesc = 0;
2865 my $hasparens = 0;
2866 my $id = '0123456789ab';
2867 my $orig_desc = "commit description";
2868 my $description = "";
2869
2870 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2871 $init_char = $1;
2872 $orig_commit = lc($2);
2873 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2874 $orig_commit = lc($1);
2875 }
2876
2877 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2878 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2879 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2880 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2881 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2882 $orig_desc = $1;
2883 $hasparens = 1;
2884 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2885 defined $rawlines[$linenr] &&
2886 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2887 $orig_desc = $1;
2888 $hasparens = 1;
2889 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2890 defined $rawlines[$linenr] &&
2891 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2892 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2893 $orig_desc = $1;
2894 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2895 $orig_desc .= " " . $1;
2896 $hasparens = 1;
2897 }
2898
2899 ($id, $description) = git_commit_info($orig_commit,
2900 $id, $orig_desc);
2901
2902 if (defined($id) &&
2903 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2904 ERROR("GIT_COMMIT_ID",
2905 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2906 }
2907 }
2908
2909# Check for added, moved or deleted files
2910 if (!$reported_maintainer_file && !$in_commit_log &&
2911 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2912 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2913 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2914 (defined($1) || defined($2))))) {
2915 $is_patch = 1;
2916 $reported_maintainer_file = 1;
2917 WARN("FILE_PATH_CHANGES",
2918 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2919 }
2920
Tom Rinic57383b2020-06-16 10:29:46 -04002921# Check for adding new DT bindings not in schema format
2922 if (!$in_commit_log &&
2923 ($line =~ /^new file mode\s*\d+\s*$/) &&
2924 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2925 WARN("DT_SCHEMA_BINDING_PATCH",
2926 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2927 }
2928
Joe Hershberger05622192011-10-18 10:06:59 +00002929# Check for wrappage within a valid hunk of the file
2930 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2931 ERROR("CORRUPTED_PATCH",
2932 "patch seems to be corrupt (line wrapped?)\n" .
2933 $herecurr) if (!$emitted_corrupt++);
2934 }
2935
Joe Hershberger05622192011-10-18 10:06:59 +00002936# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2937 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2938 $rawline !~ m/^$UTF8*$/) {
2939 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2940
2941 my $blank = copy_spacing($rawline);
2942 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2943 my $hereptr = "$hereline$ptr\n";
2944
2945 CHK("INVALID_UTF8",
2946 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2947 }
2948
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002949# Check if it's the start of a commit log
2950# (not a header line and we haven't seen the patch filename)
2951 if ($in_header_lines && $realfile =~ /^$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002952 !($rawline =~ /^\s+(?:\S|$)/ ||
2953 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002954 $in_header_lines = 0;
2955 $in_commit_log = 1;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002956 $has_commit_log = 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002957 }
2958
2959# Check if there is UTF-8 in a commit log when a mail header has explicitly
2960# declined it, i.e defined some charset where it is missing.
2961 if ($in_header_lines &&
2962 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2963 $1 !~ /utf-8/i) {
2964 $non_utf8_charset = 1;
2965 }
2966
2967 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2968 $rawline =~ /$NON_ASCII_UTF8/) {
2969 WARN("UTF8_BEFORE_PATCH",
2970 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2971 }
2972
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002973# Check for absolute kernel paths in commit message
2974 if ($tree && $in_commit_log) {
2975 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2976 my $file = $1;
2977
2978 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2979 check_absolute_file($1, $herecurr)) {
2980 #
2981 } else {
2982 check_absolute_file($file, $herecurr);
2983 }
2984 }
2985 }
2986
Dan Murphyc10e0f52017-01-31 14:15:53 -06002987# Check for various typo / spelling mistakes
2988 if (defined($misspellings) &&
2989 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2990 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2991 my $typo = $1;
2992 my $typo_fix = $spelling_fix{lc($typo)};
2993 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2994 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002995 my $msg_level = \&WARN;
2996 $msg_level = \&CHK if ($file);
2997 if (&{$msg_level}("TYPO_SPELLING",
2998 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Dan Murphyc10e0f52017-01-31 14:15:53 -06002999 $fix) {
3000 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3001 }
3002 }
3003 }
3004
Tom Rinic57383b2020-06-16 10:29:46 -04003005# check for invalid commit id
3006 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3007 my $id;
3008 my $description;
3009 ($id, $description) = git_commit_info($2, undef, undef);
3010 if (!defined($id)) {
3011 WARN("UNKNOWN_COMMIT_ID",
3012 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3013 }
3014 }
3015
Joe Hershberger05622192011-10-18 10:06:59 +00003016# ignore non-hunk lines and lines being removed
3017 next if (!$hunk_line || $line =~ /^-/);
3018
3019#trailing whitespace
3020 if ($line =~ /^\+.*\015/) {
3021 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003022 if (ERROR("DOS_LINE_ENDINGS",
3023 "DOS line endings\n" . $herevet) &&
3024 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003025 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05003026 }
Joe Hershberger05622192011-10-18 10:06:59 +00003027 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3028 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003029 if (ERROR("TRAILING_WHITESPACE",
3030 "trailing whitespace\n" . $herevet) &&
3031 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003032 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05003033 }
3034
Joe Hershberger05622192011-10-18 10:06:59 +00003035 $rpt_cleaners = 1;
3036 }
3037
Tom Rini6b9709d2014-02-27 08:27:28 -05003038# Check for FSF mailing addresses.
3039 if ($rawline =~ /\bwrite to the Free/i ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003040 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Tom Rini6b9709d2014-02-27 08:27:28 -05003041 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3042 $rawline =~ /\b51\s+Franklin\s+St/i) {
3043 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003044 my $msg_level = \&ERROR;
3045 $msg_level = \&CHK if ($file);
3046 &{$msg_level}("FSF_MAILING_ADDRESS",
3047 "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 Rini6b9709d2014-02-27 08:27:28 -05003048 }
3049
Joe Hershberger05622192011-10-18 10:06:59 +00003050# check for Kconfig help text having a real description
3051# Only applies when adding the entry originally, after that we do not have
3052# sufficient context to determine whether it is indeed long enough.
3053 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003054 # 'choice' is usually the last thing on the line (though
3055 # Kconfig supports named choices), so use a word boundary
3056 # (\b) rather than a whitespace character (\s)
3057 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003058 my $length = 0;
3059 my $cnt = $realcnt;
3060 my $ln = $linenr + 1;
3061 my $f;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003062 my $is_start = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00003063 my $is_end = 0;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003064 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Joe Hershberger05622192011-10-18 10:06:59 +00003065 $f = $lines[$ln - 1];
3066 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3067 $is_end = $lines[$ln - 1] =~ /^\+/;
Joe Hershberger05622192011-10-18 10:06:59 +00003068
3069 next if ($f =~ /^-/);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003070 last if (!$file && $f =~ /^\@\@/);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003071
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003072 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003073 $is_start = 1;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003074 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3075 if ($lines[$ln - 1] =~ "---help---") {
3076 WARN("CONFIG_DESCRIPTION",
3077 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3078 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003079 $length = -1;
3080 }
3081
Joe Hershberger05622192011-10-18 10:06:59 +00003082 $f =~ s/^.//;
3083 $f =~ s/#.*//;
3084 $f =~ s/^\s+//;
3085 next if ($f =~ /^$/);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003086
3087 # This only checks context lines in the patch
3088 # and so hopefully shouldn't trigger false
3089 # positives, even though some of these are
3090 # common words in help texts
3091 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3092 if|endif|menu|endmenu|source)\b/x) {
Joe Hershberger05622192011-10-18 10:06:59 +00003093 $is_end = 1;
3094 last;
3095 }
3096 $length++;
3097 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003098 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3099 WARN("CONFIG_DESCRIPTION",
3100 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3101 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003102 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
3103 }
3104
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003105# check for MAINTAINERS entries that don't have the right form
3106 if ($realfile =~ /^MAINTAINERS$/ &&
3107 $rawline =~ /^\+[A-Z]:/ &&
3108 $rawline !~ /^\+[A-Z]:\t\S/) {
3109 if (WARN("MAINTAINERS_STYLE",
3110 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3111 $fix) {
3112 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3113 }
3114 }
3115
3116# discourage the use of boolean for type definition attributes of Kconfig options
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003117 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003118 $line =~ /^\+\s*\bboolean\b/) {
3119 WARN("CONFIG_TYPE_BOOLEAN",
3120 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003121 }
3122
3123 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3124 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3125 my $flag = $1;
3126 my $replacement = {
3127 'EXTRA_AFLAGS' => 'asflags-y',
3128 'EXTRA_CFLAGS' => 'ccflags-y',
3129 'EXTRA_CPPFLAGS' => 'cppflags-y',
3130 'EXTRA_LDFLAGS' => 'ldflags-y',
3131 };
3132
3133 WARN("DEPRECATED_VARIABLE",
3134 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
Joe Hershberger05622192011-10-18 10:06:59 +00003135 }
3136
Tom Rini6b9709d2014-02-27 08:27:28 -05003137# check for DT compatible documentation
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003138 if (defined $root &&
3139 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3140 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3141
Tom Rini6b9709d2014-02-27 08:27:28 -05003142 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3143
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003144 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Tom Rinic57383b2020-06-16 10:29:46 -04003145 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003146
Tom Rini6b9709d2014-02-27 08:27:28 -05003147 foreach my $compat (@compats) {
3148 my $compat2 = $compat;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003149 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3150 my $compat3 = $compat;
3151 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3152 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Tom Rini6b9709d2014-02-27 08:27:28 -05003153 if ( $? >> 8 ) {
3154 WARN("UNDOCUMENTED_DT_STRING",
3155 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3156 }
3157
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003158 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3159 my $vendor = $1;
Tom Rinic57383b2020-06-16 10:29:46 -04003160 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Tom Rini6b9709d2014-02-27 08:27:28 -05003161 if ( $? >> 8 ) {
3162 WARN("UNDOCUMENTED_DT_STRING",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003163 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Tom Rini6b9709d2014-02-27 08:27:28 -05003164 }
3165 }
3166 }
3167
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003168# check for using SPDX license tag at beginning of files
3169 if ($realline == $checklicenseline) {
3170 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3171 $checklicenseline = 2;
3172 } elsif ($rawline =~ /^\+/) {
3173 my $comment = "";
3174 if ($realfile =~ /\.(h|s|S)$/) {
3175 $comment = '/*';
3176 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3177 $comment = '//';
Tom Rinic57383b2020-06-16 10:29:46 -04003178 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003179 $comment = '#';
3180 } elsif ($realfile =~ /\.rst$/) {
3181 $comment = '..';
3182 }
3183
Tom Rinic57383b2020-06-16 10:29:46 -04003184# check SPDX comment style for .[chsS] files
3185 if ($realfile =~ /\.[chsS]$/ &&
3186 $rawline =~ /SPDX-License-Identifier:/ &&
3187 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3188 WARN("SPDX_LICENSE_TAG",
3189 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3190 }
3191
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003192 if ($comment !~ /^$/ &&
Tom Rinic57383b2020-06-16 10:29:46 -04003193 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3194 WARN("SPDX_LICENSE_TAG",
3195 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003196 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Tom Rinic57383b2020-06-16 10:29:46 -04003197 my $spdx_license = $1;
3198 if (!is_SPDX_License_valid($spdx_license)) {
3199 WARN("SPDX_LICENSE_TAG",
3200 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3201 }
3202 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3203 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3204 my $msg_level = \&WARN;
3205 $msg_level = \&CHK if ($file);
3206 if (&{$msg_level}("SPDX_LICENSE_TAG",
3207
3208 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3209 $fix) {
3210 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3211 }
3212 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003213 }
3214 }
3215 }
3216
Joe Hershberger05622192011-10-18 10:06:59 +00003217# check we are in a valid source file if not then ignore this hunk
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003218 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Joe Hershberger05622192011-10-18 10:06:59 +00003219
Tom Rinic57383b2020-06-16 10:29:46 -04003220# check for using SPDX-License-Identifier on the wrong line number
3221 if ($realline != $checklicenseline &&
3222 $rawline =~ /\bSPDX-License-Identifier:/ &&
3223 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3224 WARN("SPDX_LICENSE_TAG",
3225 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3226 }
3227
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003228# line length limit (with some exclusions)
3229#
3230# There are a few types of lines that may extend beyond $max_line_length:
3231# logging functions like pr_info that end in a string
3232# lines with a single string
3233# #defines that are a single string
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003234# lines with an RFC3986 like URL
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003235#
3236# There are 3 different line length message types:
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003237# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003238# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3239# LONG_LINE all other lines longer than $max_line_length
3240#
3241# if LONG_LINE is ignored, the other 2 types are also ignored
3242#
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003243
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003244 if ($line =~ /^\+/ && $length > $max_line_length) {
3245 my $msg_type = "LONG_LINE";
Joe Hershberger05622192011-10-18 10:06:59 +00003246
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003247 # Check the allowed long line types first
3248
3249 # logging functions that end in a string that starts
3250 # before $max_line_length
3251 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3252 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3253 $msg_type = "";
3254
3255 # lines with only strings (w/ possible termination)
3256 # #defines with only strings
3257 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3258 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3259 $msg_type = "";
3260
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003261 # More special cases
3262 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3263 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3264 $msg_type = "";
3265
3266 # URL ($rawline is used in case the URL is in a comment)
3267 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003268 $msg_type = "";
3269
3270 # Otherwise set the alternate message types
3271
3272 # a comment starts before $max_line_length
3273 } elsif ($line =~ /($;[\s$;]*)$/ &&
3274 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3275 $msg_type = "LONG_LINE_COMMENT"
3276
3277 # a quoted string starts before $max_line_length
3278 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3279 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3280 $msg_type = "LONG_LINE_STRING"
Tom Rini6b9709d2014-02-27 08:27:28 -05003281 }
3282
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003283 if ($msg_type ne "" &&
3284 (show_type("LONG_LINE") || show_type($msg_type))) {
Simon Glass048a6482020-05-22 16:32:35 -06003285 my $msg_level = \&WARN;
3286 $msg_level = \&CHK if ($file);
3287 &{$msg_level}($msg_type,
3288 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003289 }
Joe Hershberger05622192011-10-18 10:06:59 +00003290 }
3291
3292# check for adding lines without a newline.
3293 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3294 WARN("MISSING_EOF_NEWLINE",
3295 "adding a line without newline at end of file\n" . $herecurr);
3296 }
3297
Simon Glassb77df592020-05-22 16:32:36 -06003298 if ($u_boot) {
3299 u_boot_line($realfile, $line, $herecurr);
3300 }
3301
Joe Hershberger05622192011-10-18 10:06:59 +00003302# check we are in a valid source file C or perl if not then ignore this hunk
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003303 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Joe Hershberger05622192011-10-18 10:06:59 +00003304
3305# at the beginning of a line any tabs must come first and anything
Tom Rinic57383b2020-06-16 10:29:46 -04003306# more than $tabsize must use tabs.
Joe Hershberger05622192011-10-18 10:06:59 +00003307 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3308 $rawline =~ /^\+\s* \s*/) {
3309 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Hershberger05622192011-10-18 10:06:59 +00003310 $rpt_cleaners = 1;
Tom Rini6b9709d2014-02-27 08:27:28 -05003311 if (ERROR("CODE_INDENT",
3312 "code indent should use tabs where possible\n" . $herevet) &&
3313 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003314 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rini6b9709d2014-02-27 08:27:28 -05003315 }
Joe Hershberger05622192011-10-18 10:06:59 +00003316 }
3317
3318# check for space before tabs.
3319 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3320 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003321 if (WARN("SPACE_BEFORE_TAB",
3322 "please, no space before tabs\n" . $herevet) &&
3323 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003324 while ($fixed[$fixlinenr] =~
Tom Rinic57383b2020-06-16 10:29:46 -04003325 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003326 while ($fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05003327 s/(^\+.*) +\t/$1\t/) {}
3328 }
Joe Hershberger05622192011-10-18 10:06:59 +00003329 }
3330
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003331# check for assignments on the start of a line
3332 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3333 CHK("ASSIGNMENT_CONTINUATIONS",
3334 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3335 }
3336
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003337# check for && or || at the start of a line
3338 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3339 CHK("LOGICAL_CONTINUATIONS",
3340 "Logical continuations should be on the previous line\n" . $hereprev);
3341 }
3342
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003343# check indentation starts on a tab stop
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003344 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003345 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003346 my $indent = length($1);
Tom Rinic57383b2020-06-16 10:29:46 -04003347 if ($indent % $tabsize) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003348 if (WARN("TABSTOP",
3349 "Statements should start on a tabstop\n" . $herecurr) &&
3350 $fix) {
Tom Rinic57383b2020-06-16 10:29:46 -04003351 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003352 }
3353 }
3354 }
3355
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003356# check multi-line statement indentation matches previous line
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003357 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003358 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003359 $prevline =~ /^\+(\t*)(.*)$/;
3360 my $oldindent = $1;
3361 my $rest = $2;
3362
3363 my $pos = pos_last_openparen($rest);
3364 if ($pos >= 0) {
3365 $line =~ /^(\+| )([ \t]*)/;
3366 my $newindent = $2;
3367
3368 my $goodtabindent = $oldindent .
Tom Rinic57383b2020-06-16 10:29:46 -04003369 "\t" x ($pos / $tabsize) .
3370 " " x ($pos % $tabsize);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003371 my $goodspaceindent = $oldindent . " " x $pos;
3372
3373 if ($newindent ne $goodtabindent &&
3374 $newindent ne $goodspaceindent) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003375
3376 if (CHK("PARENTHESIS_ALIGNMENT",
3377 "Alignment should match open parenthesis\n" . $hereprev) &&
3378 $fix && $line =~ /^\+/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003379 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05003380 s/^\+[ \t]*/\+$goodtabindent/;
3381 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003382 }
3383 }
3384 }
3385
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003386# check for space after cast like "(int) foo" or "(struct foo) bar"
3387# avoid checking a few false positives:
3388# "sizeof(<type>)" or "__alignof__(<type>)"
3389# function pointer declarations like "(*foo)(int) = bar;"
3390# structure definitions like "(struct foo) { 0 };"
3391# multiline macros that define functions
3392# known attributes or the __attribute__ keyword
3393 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3394 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003395 if (CHK("SPACING",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003396 "No space is necessary after a cast\n" . $herecurr) &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003397 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003398 $fixed[$fixlinenr] =~
3399 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Tom Rini6b9709d2014-02-27 08:27:28 -05003400 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003401 }
3402
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003403# Block comment styles
3404# Networking with an initial /*
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003405 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003406 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003407 $rawline =~ /^\+[ \t]*\*/ &&
3408 $realline > 2) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003409 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3410 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3411 }
3412
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003413# Block comments use * on subsequent lines
3414 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3415 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Tom Rini6b9709d2014-02-27 08:27:28 -05003416 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3417 $rawline =~ /^\+/ && #line is new
3418 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003419 WARN("BLOCK_COMMENT_STYLE",
3420 "Block comments use * on subsequent lines\n" . $hereprev);
Tom Rini6b9709d2014-02-27 08:27:28 -05003421 }
3422
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003423# Block comments use */ on trailing lines
3424 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003425 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3426 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3427 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003428 WARN("BLOCK_COMMENT_STYLE",
3429 "Block comments use a trailing */ on a separate line\n" . $herecurr);
3430 }
3431
3432# Block comment * alignment
3433 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3434 $line =~ /^\+[ \t]*$;/ && #leading comment
3435 $rawline =~ /^\+[ \t]*\*/ && #leading *
3436 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3437 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3438 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3439 my $oldindent;
3440 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3441 if (defined($1)) {
3442 $oldindent = expand_tabs($1);
3443 } else {
3444 $prevrawline =~ m@^\+(.*/?)\*@;
3445 $oldindent = expand_tabs($1);
3446 }
3447 $rawline =~ m@^\+([ \t]*)\*@;
3448 my $newindent = $1;
3449 $newindent = expand_tabs($newindent);
3450 if (length($oldindent) ne length($newindent)) {
3451 WARN("BLOCK_COMMENT_STYLE",
3452 "Block comments should align the * on each line\n" . $hereprev);
3453 }
3454 }
3455
3456# check for missing blank lines after struct/union declarations
3457# with exceptions for various attributes and macros
3458 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3459 $line =~ /^\+/ &&
3460 !($line =~ /^\+\s*$/ ||
3461 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3462 $line =~ /^\+\s*MODULE_/i ||
3463 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3464 $line =~ /^\+[a-z_]*init/ ||
3465 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3466 $line =~ /^\+\s*DECLARE/ ||
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003467 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003468 $line =~ /^\+\s*__setup/)) {
3469 if (CHK("LINE_SPACING",
3470 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3471 $fix) {
3472 fix_insert_line($fixlinenr, "\+");
3473 }
3474 }
3475
3476# check for multiple consecutive blank lines
3477 if ($prevline =~ /^[\+ ]\s*$/ &&
3478 $line =~ /^\+\s*$/ &&
3479 $last_blank_line != ($linenr - 1)) {
3480 if (CHK("LINE_SPACING",
3481 "Please don't use multiple blank lines\n" . $hereprev) &&
3482 $fix) {
3483 fix_delete_line($fixlinenr, $rawline);
3484 }
3485
3486 $last_blank_line = $linenr;
3487 }
3488
3489# check for missing blank lines after declarations
3490 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3491 # actual declarations
3492 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3493 # function pointer declarations
3494 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3495 # foo bar; where foo is some local typedef or #define
3496 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3497 # known declaration macros
3498 $prevline =~ /^\+\s+$declaration_macros/) &&
3499 # for "else if" which can look like "$Ident $Ident"
3500 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3501 # other possible extensions of declaration lines
3502 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3503 # not starting a section or a macro "\" extended line
3504 $prevline =~ /(?:\{\s*|\\)$/) &&
3505 # looks like a declaration
3506 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3507 # function pointer declarations
3508 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3509 # foo bar; where foo is some local typedef or #define
3510 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3511 # known declaration macros
3512 $sline =~ /^\+\s+$declaration_macros/ ||
3513 # start of struct or union or enum
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003514 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003515 # start or end of block or continuation of declaration
3516 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3517 # bitfield continuation
3518 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3519 # other possible extensions of declaration lines
3520 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3521 # indentation of previous and current line are the same
3522 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3523 if (WARN("LINE_SPACING",
3524 "Missing a blank line after declarations\n" . $hereprev) &&
3525 $fix) {
3526 fix_insert_line($fixlinenr, "\+");
3527 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003528 }
3529
Joe Hershberger05622192011-10-18 10:06:59 +00003530# check for spaces at the beginning of a line.
3531# Exceptions:
3532# 1) within comments
3533# 2) indented preprocessor commands
3534# 3) hanging labels
Tom Rini6b9709d2014-02-27 08:27:28 -05003535 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003536 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003537 if (WARN("LEADING_SPACE",
3538 "please, no spaces at the start of a line\n" . $herevet) &&
3539 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003540 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rini6b9709d2014-02-27 08:27:28 -05003541 }
Joe Hershberger05622192011-10-18 10:06:59 +00003542 }
3543
3544# check we are in a valid C source file if not then ignore this hunk
3545 next if ($realfile !~ /\.(h|c)$/);
3546
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003547# check for unusual line ending [ or (
3548 if ($line =~ /^\+.*([\[\(])\s*$/) {
3549 CHK("OPEN_ENDED_LINE",
3550 "Lines should not end with a '$1'\n" . $herecurr);
3551 }
3552
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003553# check if this appears to be the start function declaration, save the name
3554 if ($sline =~ /^\+\{\s*$/ &&
3555 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3556 $context_function = $1;
3557 }
3558
3559# check if this appears to be the end of function declaration
3560 if ($sline =~ /^\+\}\s*$/) {
3561 undef $context_function;
3562 }
3563
3564# check indentation of any line with a bare else
3565# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3566# if the previous line is a break or return and is indented 1 tab more...
3567 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3568 my $tabs = length($1) + 1;
3569 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3570 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3571 defined $lines[$linenr] &&
3572 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3573 WARN("UNNECESSARY_ELSE",
3574 "else is not generally useful after a break or return\n" . $hereprev);
3575 }
3576 }
3577
3578# check indentation of a line with a break;
3579# if the previous line is a goto or return and is indented the same # of tabs
3580 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3581 my $tabs = $1;
3582 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3583 WARN("UNNECESSARY_BREAK",
3584 "break is not useful after a goto or return\n" . $hereprev);
3585 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003586 }
3587
Joe Hershberger05622192011-10-18 10:06:59 +00003588# check for RCS/CVS revision markers
3589 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3590 WARN("CVS_KEYWORD",
3591 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3592 }
3593
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003594# check for old HOTPLUG __dev<foo> section markings
3595 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3596 WARN("HOTPLUG_SECTION",
3597 "Using $1 is unnecessary\n" . $herecurr);
3598 }
3599
Joe Hershberger05622192011-10-18 10:06:59 +00003600# Check for potential 'bare' types
3601 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3602 $realline_next);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003603#print "LINE<$line>\n";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003604 if ($linenr > $suppress_statement &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003605 $realcnt && $sline =~ /.\s*\S/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003606 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3607 ctx_statement_block($linenr, $realcnt, 0);
3608 $stat =~ s/\n./\n /g;
3609 $cond =~ s/\n./\n /g;
3610
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003611#print "linenr<$linenr> <$stat>\n";
3612 # If this statement has no statement boundaries within
3613 # it there is no point in retrying a statement scan
3614 # until we hit end of it.
3615 my $frag = $stat; $frag =~ s/;+\s*$//;
3616 if ($frag !~ /(?:{|;)/) {
3617#print "skip<$line_nr_next>\n";
3618 $suppress_statement = $line_nr_next;
3619 }
3620
Joe Hershberger05622192011-10-18 10:06:59 +00003621 # Find the real next line.
3622 $realline_next = $line_nr_next;
3623 if (defined $realline_next &&
3624 (!defined $lines[$realline_next - 1] ||
3625 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3626 $realline_next++;
3627 }
3628
3629 my $s = $stat;
3630 $s =~ s/{.*$//s;
3631
3632 # Ignore goto labels.
3633 if ($s =~ /$Ident:\*$/s) {
3634
3635 # Ignore functions being called
3636 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3637
3638 } elsif ($s =~ /^.\s*else\b/s) {
3639
3640 # declarations always start with types
3641 } 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) {
3642 my $type = $1;
3643 $type =~ s/\s+/ /g;
3644 possible($type, "A:" . $s);
3645
3646 # definitions in global scope can only start with types
3647 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3648 possible($1, "B:" . $s);
3649 }
3650
3651 # any (foo ... *) is a pointer cast, and foo is a type
3652 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3653 possible($1, "C:" . $s);
3654 }
3655
3656 # Check for any sort of function declaration.
3657 # int foo(something bar, other baz);
3658 # void (*store_gdt)(x86_descr_ptr *);
3659 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3660 my ($name_len) = length($1);
3661
3662 my $ctx = $s;
3663 substr($ctx, 0, $name_len + 1, '');
3664 $ctx =~ s/\)[^\)]*$//;
3665
3666 for my $arg (split(/\s*,\s*/, $ctx)) {
3667 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3668
3669 possible($1, "D:" . $s);
3670 }
3671 }
3672 }
3673
3674 }
3675
3676#
3677# Checks which may be anchored in the context.
3678#
3679
3680# Check for switch () and associated case and default
3681# statements should be at the same indent.
3682 if ($line=~/\bswitch\s*\(.*\)/) {
3683 my $err = '';
3684 my $sep = '';
3685 my @ctx = ctx_block_outer($linenr, $realcnt);
3686 shift(@ctx);
3687 for my $ctx (@ctx) {
3688 my ($clen, $cindent) = line_stats($ctx);
3689 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3690 $indent != $cindent) {
3691 $err .= "$sep$ctx\n";
3692 $sep = '';
3693 } else {
3694 $sep = "[...]\n";
3695 }
3696 }
3697 if ($err ne '') {
3698 ERROR("SWITCH_CASE_INDENT_LEVEL",
3699 "switch and case should be at the same indent\n$hereline$err");
3700 }
3701 }
3702
3703# if/while/etc brace do not go on next line, unless defining a do while loop,
3704# or if that brace on the next line is for something else
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003705 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003706 my $pre_ctx = "$1$2";
3707
3708 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003709
3710 if ($line =~ /^\+\t{6,}/) {
3711 WARN("DEEP_INDENTATION",
3712 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3713 }
3714
Joe Hershberger05622192011-10-18 10:06:59 +00003715 my $ctx_cnt = $realcnt - $#ctx - 1;
3716 my $ctx = join("\n", @ctx);
3717
3718 my $ctx_ln = $linenr;
3719 my $ctx_skip = $realcnt;
3720
3721 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3722 defined $lines[$ctx_ln - 1] &&
3723 $lines[$ctx_ln - 1] =~ /^-/)) {
3724 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3725 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3726 $ctx_ln++;
3727 }
3728
3729 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3730 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3731
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003732 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003733 ERROR("OPEN_BRACE",
3734 "that open brace { should be on the previous line\n" .
3735 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3736 }
3737 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3738 $ctx =~ /\)\s*\;\s*$/ &&
3739 defined $lines[$ctx_ln - 1])
3740 {
3741 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3742 if ($nindent > $indent) {
3743 WARN("TRAILING_SEMICOLON",
3744 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3745 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3746 }
3747 }
3748 }
3749
3750# Check relative indent for conditionals and blocks.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003751 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003752 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3753 ctx_statement_block($linenr, $realcnt, 0)
3754 if (!defined $stat);
Joe Hershberger05622192011-10-18 10:06:59 +00003755 my ($s, $c) = ($stat, $cond);
3756
3757 substr($s, 0, length($c), '');
3758
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003759 # remove inline comments
3760 $s =~ s/$;/ /g;
3761 $c =~ s/$;/ /g;
Joe Hershberger05622192011-10-18 10:06:59 +00003762
3763 # Find out how long the conditional actually is.
3764 my @newlines = ($c =~ /\n/gs);
3765 my $cond_lines = 1 + $#newlines;
3766
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003767 # Make sure we remove the line prefixes as we have
3768 # none on the first line, and are going to readd them
3769 # where necessary.
3770 $s =~ s/\n./\n/gs;
3771 while ($s =~ /\n\s+\\\n/) {
3772 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3773 }
3774
Joe Hershberger05622192011-10-18 10:06:59 +00003775 # We want to check the first line inside the block
3776 # starting at the end of the conditional, so remove:
3777 # 1) any blank line termination
3778 # 2) any opening brace { on end of the line
3779 # 3) any do (...) {
3780 my $continuation = 0;
3781 my $check = 0;
3782 $s =~ s/^.*\bdo\b//;
3783 $s =~ s/^\s*{//;
3784 if ($s =~ s/^\s*\\//) {
3785 $continuation = 1;
3786 }
3787 if ($s =~ s/^\s*?\n//) {
3788 $check = 1;
3789 $cond_lines++;
3790 }
3791
3792 # Also ignore a loop construct at the end of a
3793 # preprocessor statement.
3794 if (($prevline =~ /^.\s*#\s*define\s/ ||
3795 $prevline =~ /\\\s*$/) && $continuation == 0) {
3796 $check = 0;
3797 }
3798
3799 my $cond_ptr = -1;
3800 $continuation = 0;
3801 while ($cond_ptr != $cond_lines) {
3802 $cond_ptr = $cond_lines;
3803
3804 # If we see an #else/#elif then the code
3805 # is not linear.
3806 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3807 $check = 0;
3808 }
3809
3810 # Ignore:
3811 # 1) blank lines, they should be at 0,
3812 # 2) preprocessor lines, and
3813 # 3) labels.
3814 if ($continuation ||
3815 $s =~ /^\s*?\n/ ||
3816 $s =~ /^\s*#\s*?/ ||
3817 $s =~ /^\s*$Ident\s*:/) {
3818 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3819 if ($s =~ s/^.*?\n//) {
3820 $cond_lines++;
3821 }
3822 }
3823 }
3824
3825 my (undef, $sindent) = line_stats("+" . $s);
3826 my $stat_real = raw_line($linenr, $cond_lines);
3827
3828 # Check if either of these lines are modified, else
3829 # this is not this patch's fault.
3830 if (!defined($stat_real) ||
3831 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3832 $check = 0;
3833 }
3834 if (defined($stat_real) && $cond_lines > 1) {
3835 $stat_real = "[...]\n$stat_real";
3836 }
3837
3838 #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";
3839
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003840 if ($check && $s ne '' &&
Tom Rinic57383b2020-06-16 10:29:46 -04003841 (($sindent % $tabsize) != 0 ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003842 ($sindent < $indent) ||
3843 ($sindent == $indent &&
3844 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Tom Rinic57383b2020-06-16 10:29:46 -04003845 ($sindent > $indent + $tabsize))) {
Joe Hershberger05622192011-10-18 10:06:59 +00003846 WARN("SUSPECT_CODE_INDENT",
3847 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3848 }
3849 }
3850
3851 # Track the 'values' across context and added lines.
3852 my $opline = $line; $opline =~ s/^./ /;
3853 my ($curr_values, $curr_vars) =
3854 annotate_values($opline . "\n", $prev_values);
3855 $curr_values = $prev_values . $curr_values;
3856 if ($dbg_values) {
3857 my $outline = $opline; $outline =~ s/\t/ /g;
3858 print "$linenr > .$outline\n";
3859 print "$linenr > $curr_values\n";
3860 print "$linenr > $curr_vars\n";
3861 }
3862 $prev_values = substr($curr_values, -1);
3863
3864#ignore lines not being added
Tom Rini6b9709d2014-02-27 08:27:28 -05003865 next if ($line =~ /^[^\+]/);
Joe Hershberger05622192011-10-18 10:06:59 +00003866
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003867# check for dereferences that span multiple lines
3868 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3869 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3870 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3871 my $ref = $1;
3872 $line =~ /^.\s*($Lval)/;
3873 $ref .= $1;
3874 $ref =~ s/\s//g;
3875 WARN("MULTILINE_DEREFERENCE",
3876 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3877 }
3878
3879# check for declarations of signed or unsigned without int
3880 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3881 my $type = $1;
3882 my $var = $2;
3883 $var = "" if (!defined $var);
3884 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3885 my $sign = $1;
3886 my $pointer = $2;
3887
3888 $pointer = "" if (!defined $pointer);
3889
3890 if (WARN("UNSPECIFIED_INT",
3891 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3892 $fix) {
3893 my $decl = trim($sign) . " int ";
3894 my $comp_pointer = $pointer;
3895 $comp_pointer =~ s/\s//g;
3896 $decl .= $comp_pointer;
3897 $decl = rtrim($decl) if ($var eq "");
3898 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3899 }
3900 }
3901 }
3902
Joe Hershberger05622192011-10-18 10:06:59 +00003903# TEST: allow direct testing of the type matcher.
3904 if ($dbg_type) {
3905 if ($line =~ /^.\s*$Declare\s*$/) {
3906 ERROR("TEST_TYPE",
3907 "TEST: is type\n" . $herecurr);
3908 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3909 ERROR("TEST_NOT_TYPE",
3910 "TEST: is not type ($1 is)\n". $herecurr);
3911 }
3912 next;
3913 }
3914# TEST: allow direct testing of the attribute matcher.
3915 if ($dbg_attr) {
3916 if ($line =~ /^.\s*$Modifier\s*$/) {
3917 ERROR("TEST_ATTR",
3918 "TEST: is attr\n" . $herecurr);
3919 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3920 ERROR("TEST_NOT_ATTR",
3921 "TEST: is not attr ($1 is)\n". $herecurr);
3922 }
3923 next;
3924 }
3925
3926# check for initialisation to aggregates open brace on the next line
3927 if ($line =~ /^.\s*{/ &&
3928 $prevline =~ /(?:^|[^=])=\s*$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003929 if (ERROR("OPEN_BRACE",
3930 "that open brace { should be on the previous line\n" . $hereprev) &&
3931 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3932 fix_delete_line($fixlinenr - 1, $prevrawline);
3933 fix_delete_line($fixlinenr, $rawline);
3934 my $fixedline = $prevrawline;
3935 $fixedline =~ s/\s*=\s*$/ = {/;
3936 fix_insert_line($fixlinenr, $fixedline);
3937 $fixedline = $line;
3938 $fixedline =~ s/^(.\s*)\{\s*/$1/;
3939 fix_insert_line($fixlinenr, $fixedline);
3940 }
Joe Hershberger05622192011-10-18 10:06:59 +00003941 }
3942
3943#
3944# Checks which are anchored on the added line.
3945#
3946
3947# check for malformed paths in #include statements (uses RAW line)
3948 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3949 my $path = $1;
3950 if ($path =~ m{//}) {
3951 ERROR("MALFORMED_INCLUDE",
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003952 "malformed #include filename\n" . $herecurr);
3953 }
3954 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3955 ERROR("UAPI_INCLUDE",
3956 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00003957 }
3958 }
3959
3960# no C99 // comments
3961 if ($line =~ m{//}) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003962 if (ERROR("C99_COMMENTS",
3963 "do not use C99 // comments\n" . $herecurr) &&
3964 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003965 my $line = $fixed[$fixlinenr];
Tom Rini6b9709d2014-02-27 08:27:28 -05003966 if ($line =~ /\/\/(.*)$/) {
3967 my $comment = trim($1);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003968 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Tom Rini6b9709d2014-02-27 08:27:28 -05003969 }
3970 }
Joe Hershberger05622192011-10-18 10:06:59 +00003971 }
3972 # Remove C99 comments.
3973 $line =~ s@//.*@@;
3974 $opline =~ s@//.*@@;
3975
3976# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3977# the whole statement.
3978#print "APW <$lines[$realline_next - 1]>\n";
3979 if (defined $realline_next &&
3980 exists $lines[$realline_next - 1] &&
3981 !defined $suppress_export{$realline_next} &&
3982 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3983 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3984 # Handle definitions which produce identifiers with
3985 # a prefix:
3986 # XXX(foo);
3987 # EXPORT_SYMBOL(something_foo);
3988 my $name = $1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003989 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Joe Hershberger05622192011-10-18 10:06:59 +00003990 $name =~ /^${Ident}_$2/) {
3991#print "FOO C name<$name>\n";
3992 $suppress_export{$realline_next} = 1;
3993
3994 } elsif ($stat !~ /(?:
3995 \n.}\s*$|
3996 ^.DEFINE_$Ident\(\Q$name\E\)|
3997 ^.DECLARE_$Ident\(\Q$name\E\)|
3998 ^.LIST_HEAD\(\Q$name\E\)|
3999 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4000 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
4001 )/x) {
4002#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4003 $suppress_export{$realline_next} = 2;
4004 } else {
4005 $suppress_export{$realline_next} = 1;
4006 }
4007 }
4008 if (!defined $suppress_export{$linenr} &&
4009 $prevline =~ /^.\s*$/ &&
4010 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4011 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4012#print "FOO B <$lines[$linenr - 1]>\n";
4013 $suppress_export{$linenr} = 2;
4014 }
4015 if (defined $suppress_export{$linenr} &&
4016 $suppress_export{$linenr} == 2) {
4017 WARN("EXPORT_SYMBOL",
4018 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
4019 }
4020
4021# check for global initialisers.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004022 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004023 if (ERROR("GLOBAL_INITIALISERS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004024 "do not initialise globals to $1\n" . $herecurr) &&
Tom Rini6b9709d2014-02-27 08:27:28 -05004025 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004026 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004027 }
Joe Hershberger05622192011-10-18 10:06:59 +00004028 }
4029# check for static initialisers.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004030 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004031 if (ERROR("INITIALISED_STATIC",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004032 "do not initialise statics to $1\n" .
Tom Rini6b9709d2014-02-27 08:27:28 -05004033 $herecurr) &&
4034 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004035 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004036 }
Joe Hershberger05622192011-10-18 10:06:59 +00004037 }
4038
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004039# check for misordered declarations of char/short/int/long with signed/unsigned
4040 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4041 my $tmp = trim($1);
4042 WARN("MISORDERED_TYPE",
4043 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4044 }
4045
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004046# check for unnecessary <signed> int declarations of short/long/long long
4047 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4048 my $type = trim($1);
4049 next if ($type !~ /\bint\b/);
4050 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4051 my $new_type = $type;
4052 $new_type =~ s/\b\s*int\s*\b/ /;
4053 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4054 $new_type =~ s/^const\s+//;
4055 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4056 $new_type = "const $new_type" if ($type =~ /^const\b/);
4057 $new_type =~ s/\s+/ /g;
4058 $new_type = trim($new_type);
4059 if (WARN("UNNECESSARY_INT",
4060 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4061 $fix) {
4062 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4063 }
4064 }
4065
Joe Hershberger05622192011-10-18 10:06:59 +00004066# check for static const char * arrays.
4067 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4068 WARN("STATIC_CONST_CHAR_ARRAY",
4069 "static const char * array should probably be static const char * const\n" .
4070 $herecurr);
Tom Rinic57383b2020-06-16 10:29:46 -04004071 }
4072
4073# check for initialized const char arrays that should be static const
4074 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4075 if (WARN("STATIC_CONST_CHAR_ARRAY",
4076 "const array should probably be static const\n" . $herecurr) &&
4077 $fix) {
4078 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4079 }
4080 }
Joe Hershberger05622192011-10-18 10:06:59 +00004081
4082# check for static char foo[] = "bar" declarations.
4083 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4084 WARN("STATIC_CONST_CHAR_ARRAY",
4085 "static char array declaration should probably be static const char\n" .
4086 $herecurr);
Tom Rinic57383b2020-06-16 10:29:46 -04004087 }
Joe Hershberger05622192011-10-18 10:06:59 +00004088
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004089# check for const <foo> const where <foo> is not a pointer or array type
4090 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4091 my $found = $1;
4092 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4093 WARN("CONST_CONST",
4094 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4095 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4096 WARN("CONST_CONST",
4097 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4098 }
4099 }
4100
4101# check for non-global char *foo[] = {"bar", ...} declarations.
4102 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4103 WARN("STATIC_CONST_CHAR_ARRAY",
4104 "char * array declaration might be better as static const\n" .
4105 $herecurr);
4106 }
4107
4108# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4109 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4110 my $array = $1;
4111 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4112 my $array_div = $1;
4113 if (WARN("ARRAY_SIZE",
4114 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4115 $fix) {
4116 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4117 }
4118 }
4119 }
4120
Tom Rini6b9709d2014-02-27 08:27:28 -05004121# check for function declarations without arguments like "int foo()"
Tom Rinic57383b2020-06-16 10:29:46 -04004122 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004123 if (ERROR("FUNCTION_WITHOUT_ARGS",
4124 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4125 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004126 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004127 }
Joe Hershberger05622192011-10-18 10:06:59 +00004128 }
4129
4130# check for new typedefs, only function parameters and sparse annotations
4131# make sense.
4132 if ($line =~ /\btypedef\s/ &&
4133 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4134 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
4135 $line !~ /\b$typeTypedefs\b/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004136 $line !~ /\b__bitwise\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004137 WARN("NEW_TYPEDEFS",
4138 "do not add new typedefs\n" . $herecurr);
4139 }
4140
4141# * goes on variable not on type
4142 # (char*[ const])
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004143 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4144 #print "AA<$1>\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05004145 my ($ident, $from, $to) = ($1, $2, $2);
Joe Hershberger05622192011-10-18 10:06:59 +00004146
4147 # Should start with a space.
4148 $to =~ s/^(\S)/ $1/;
4149 # Should not end with a space.
4150 $to =~ s/\s+$//;
4151 # '*'s should not have spaces between.
4152 while ($to =~ s/\*\s+\*/\*\*/) {
4153 }
4154
Tom Rini6b9709d2014-02-27 08:27:28 -05004155## print "1: from<$from> to<$to> ident<$ident>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00004156 if ($from ne $to) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004157 if (ERROR("POINTER_LOCATION",
4158 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4159 $fix) {
4160 my $sub_from = $ident;
4161 my $sub_to = $ident;
4162 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004163 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004164 s@\Q$sub_from\E@$sub_to@;
4165 }
Joe Hershberger05622192011-10-18 10:06:59 +00004166 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004167 }
4168 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4169 #print "BB<$1>\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05004170 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Joe Hershberger05622192011-10-18 10:06:59 +00004171
4172 # Should start with a space.
4173 $to =~ s/^(\S)/ $1/;
4174 # Should not end with a space.
4175 $to =~ s/\s+$//;
4176 # '*'s should not have spaces between.
4177 while ($to =~ s/\*\s+\*/\*\*/) {
4178 }
4179 # Modifiers should have spaces.
4180 $to =~ s/(\b$Modifier$)/$1 /;
4181
Tom Rini6b9709d2014-02-27 08:27:28 -05004182## print "2: from<$from> to<$to> ident<$ident>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00004183 if ($from ne $to && $ident !~ /^$Modifier$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004184 if (ERROR("POINTER_LOCATION",
4185 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4186 $fix) {
4187
4188 my $sub_from = $match;
4189 my $sub_to = $match;
4190 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004191 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004192 s@\Q$sub_from\E@$sub_to@;
4193 }
Joe Hershberger05622192011-10-18 10:06:59 +00004194 }
4195 }
4196
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004197# avoid BUG() or BUG_ON()
4198 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004199 my $msg_level = \&WARN;
4200 $msg_level = \&CHK if ($file);
4201 &{$msg_level}("AVOID_BUG",
4202 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004203 }
Joe Hershberger05622192011-10-18 10:06:59 +00004204
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004205# avoid LINUX_VERSION_CODE
Joe Hershberger05622192011-10-18 10:06:59 +00004206 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4207 WARN("LINUX_VERSION_CODE",
4208 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4209 }
4210
4211# check for uses of printk_ratelimit
4212 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4213 WARN("PRINTK_RATELIMITED",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004214 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004215 }
4216
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004217# printk should use KERN_* levels
4218 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4219 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4220 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004221 }
4222
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004223 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4224 my $orig = $1;
4225 my $level = lc($orig);
4226 $level = "warn" if ($level eq "warning");
4227 my $level2 = $level;
4228 $level2 = "dbg" if ($level eq "debug");
4229 WARN("PREFER_PR_LEVEL",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004230 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004231 }
4232
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004233 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4234 my $orig = $1;
4235 my $level = lc($orig);
4236 $level = "warn" if ($level eq "warning");
4237 $level = "dbg" if ($level eq "debug");
4238 WARN("PREFER_DEV_LEVEL",
4239 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4240 }
4241
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004242# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4243# number of false positives, but assembly files are not checked, so at
4244# least the arch entry code will not trigger this warning.
4245 if ($line =~ /\bENOSYS\b/) {
4246 WARN("ENOSYS",
4247 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4248 }
4249
Joe Hershberger05622192011-10-18 10:06:59 +00004250# function brace can't be on same line, except for #defines of do while,
4251# or if closed on same line
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004252 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004253 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4254 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4255 $sline !~ /}/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004256 if (ERROR("OPEN_BRACE",
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004257 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004258 $fix) {
4259 fix_delete_line($fixlinenr, $rawline);
4260 my $fixed_line = $rawline;
4261 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4262 my $line1 = $1;
4263 my $line2 = $2;
4264 fix_insert_line($fixlinenr, ltrim($line1));
4265 fix_insert_line($fixlinenr, "\+{");
4266 if ($line2 !~ /^\s*$/) {
4267 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4268 }
4269 }
Joe Hershberger05622192011-10-18 10:06:59 +00004270 }
4271
4272# open braces for enum, union and struct go on the same line.
4273 if ($line =~ /^.\s*{/ &&
4274 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004275 if (ERROR("OPEN_BRACE",
4276 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4277 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4278 fix_delete_line($fixlinenr - 1, $prevrawline);
4279 fix_delete_line($fixlinenr, $rawline);
4280 my $fixedline = rtrim($prevrawline) . " {";
4281 fix_insert_line($fixlinenr, $fixedline);
4282 $fixedline = $rawline;
4283 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4284 if ($fixedline !~ /^\+\s*$/) {
4285 fix_insert_line($fixlinenr, $fixedline);
4286 }
4287 }
Joe Hershberger05622192011-10-18 10:06:59 +00004288 }
4289
4290# missing space after union, struct or enum definition
Tom Rini6b9709d2014-02-27 08:27:28 -05004291 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4292 if (WARN("SPACING",
4293 "missing space after $1 definition\n" . $herecurr) &&
4294 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004295 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004296 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4297 }
4298 }
4299
4300# Function pointer declarations
4301# check spacing between type, funcptr, and args
4302# canonical declaration is "type (*funcptr)(args...)"
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004303 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004304 my $declare = $1;
4305 my $pre_pointer_space = $2;
4306 my $post_pointer_space = $3;
4307 my $funcname = $4;
4308 my $post_funcname_space = $5;
4309 my $pre_args_space = $6;
4310
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004311# the $Declare variable will capture all spaces after the type
4312# so check it for a missing trailing missing space but pointer return types
4313# don't need a space so don't warn for those.
4314 my $post_declare_space = "";
4315 if ($declare =~ /(\s+)$/) {
4316 $post_declare_space = $1;
4317 $declare = rtrim($declare);
4318 }
4319 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004320 WARN("SPACING",
4321 "missing space after return type\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004322 $post_declare_space = " ";
Tom Rini6b9709d2014-02-27 08:27:28 -05004323 }
4324
4325# unnecessary space "type (*funcptr)(args...)"
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004326# This test is not currently implemented because these declarations are
4327# equivalent to
4328# int foo(int bar, ...)
4329# and this is form shouldn't/doesn't generate a checkpatch warning.
4330#
4331# elsif ($declare =~ /\s{2,}$/) {
4332# WARN("SPACING",
4333# "Multiple spaces after return type\n" . $herecurr);
4334# }
Tom Rini6b9709d2014-02-27 08:27:28 -05004335
4336# unnecessary space "type ( *funcptr)(args...)"
4337 if (defined $pre_pointer_space &&
4338 $pre_pointer_space =~ /^\s/) {
4339 WARN("SPACING",
4340 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4341 }
4342
4343# unnecessary space "type (* funcptr)(args...)"
4344 if (defined $post_pointer_space &&
4345 $post_pointer_space =~ /^\s/) {
4346 WARN("SPACING",
4347 "Unnecessary space before function pointer name\n" . $herecurr);
4348 }
4349
4350# unnecessary space "type (*funcptr )(args...)"
4351 if (defined $post_funcname_space &&
4352 $post_funcname_space =~ /^\s/) {
4353 WARN("SPACING",
4354 "Unnecessary space after function pointer name\n" . $herecurr);
4355 }
4356
4357# unnecessary space "type (*funcptr) (args...)"
4358 if (defined $pre_args_space &&
4359 $pre_args_space =~ /^\s/) {
4360 WARN("SPACING",
4361 "Unnecessary space before function pointer arguments\n" . $herecurr);
4362 }
4363
4364 if (show_type("SPACING") && $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004365 $fixed[$fixlinenr] =~
4366 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05004367 }
Joe Hershberger05622192011-10-18 10:06:59 +00004368 }
4369
4370# check for spacing round square brackets; allowed:
4371# 1. with a type on the left -- int [] a;
4372# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4373# 3. inside a curly brace -- = { [0...10] = 5 }
4374 while ($line =~ /(.*?\s)\[/g) {
4375 my ($where, $prefix) = ($-[1], $1);
4376 if ($prefix !~ /$Type\s+$/ &&
4377 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004378 $prefix !~ /[{,:]\s+$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004379 if (ERROR("BRACKET_SPACE",
4380 "space prohibited before open square bracket '['\n" . $herecurr) &&
4381 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004382 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004383 s/^(\+.*?)\s+\[/$1\[/;
4384 }
Joe Hershberger05622192011-10-18 10:06:59 +00004385 }
4386 }
4387
4388# check for spaces between functions and their parentheses.
4389 while ($line =~ /($Ident)\s+\(/g) {
4390 my $name = $1;
4391 my $ctx_before = substr($line, 0, $-[1]);
4392 my $ctx = "$ctx_before$name";
4393
4394 # Ignore those directives where spaces _are_ permitted.
4395 if ($name =~ /^(?:
4396 if|for|while|switch|return|case|
4397 volatile|__volatile__|
4398 __attribute__|format|__extension__|
4399 asm|__asm__)$/x)
4400 {
Joe Hershberger05622192011-10-18 10:06:59 +00004401 # cpp #define statements have non-optional spaces, ie
4402 # if there is a space between the name and the open
4403 # parenthesis it is simply not a parameter group.
4404 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4405
4406 # cpp #elif statement condition may start with a (
4407 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4408
4409 # If this whole things ends with a type its most
4410 # likely a typedef for a function.
4411 } elsif ($ctx =~ /$Type$/) {
4412
4413 } else {
Tom Rini6b9709d2014-02-27 08:27:28 -05004414 if (WARN("SPACING",
4415 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4416 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004417 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004418 s/\b$name\s+\(/$name\(/;
4419 }
Joe Hershberger05622192011-10-18 10:06:59 +00004420 }
4421 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004422
Joe Hershberger05622192011-10-18 10:06:59 +00004423# Check operator spacing.
4424 if (!($line=~/\#\s*include/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004425 my $fixed_line = "";
4426 my $line_fixed = 0;
4427
Joe Hershberger05622192011-10-18 10:06:59 +00004428 my $ops = qr{
4429 <<=|>>=|<=|>=|==|!=|
4430 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4431 =>|->|<<|>>|<|>|=|!|~|
4432 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Tom Rini6b9709d2014-02-27 08:27:28 -05004433 \?:|\?|:
Joe Hershberger05622192011-10-18 10:06:59 +00004434 }x;
4435 my @elements = split(/($ops|;)/, $opline);
Tom Rini6b9709d2014-02-27 08:27:28 -05004436
4437## print("element count: <" . $#elements . ">\n");
4438## foreach my $el (@elements) {
4439## print("el: <$el>\n");
4440## }
4441
4442 my @fix_elements = ();
Joe Hershberger05622192011-10-18 10:06:59 +00004443 my $off = 0;
4444
Tom Rini6b9709d2014-02-27 08:27:28 -05004445 foreach my $el (@elements) {
4446 push(@fix_elements, substr($rawline, $off, length($el)));
4447 $off += length($el);
4448 }
4449
4450 $off = 0;
4451
Joe Hershberger05622192011-10-18 10:06:59 +00004452 my $blank = copy_spacing($opline);
Tom Rini6b9709d2014-02-27 08:27:28 -05004453 my $last_after = -1;
Joe Hershberger05622192011-10-18 10:06:59 +00004454
4455 for (my $n = 0; $n < $#elements; $n += 2) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004456
4457 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4458
4459## print("n: <$n> good: <$good>\n");
4460
Joe Hershberger05622192011-10-18 10:06:59 +00004461 $off += length($elements[$n]);
4462
4463 # Pick up the preceding and succeeding characters.
4464 my $ca = substr($opline, 0, $off);
4465 my $cc = '';
4466 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4467 $cc = substr($opline, $off + length($elements[$n + 1]));
4468 }
4469 my $cb = "$ca$;$cc";
4470
4471 my $a = '';
4472 $a = 'V' if ($elements[$n] ne '');
4473 $a = 'W' if ($elements[$n] =~ /\s$/);
4474 $a = 'C' if ($elements[$n] =~ /$;$/);
4475 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4476 $a = 'O' if ($elements[$n] eq '');
4477 $a = 'E' if ($ca =~ /^\s*$/);
4478
4479 my $op = $elements[$n + 1];
4480
4481 my $c = '';
4482 if (defined $elements[$n + 2]) {
4483 $c = 'V' if ($elements[$n + 2] ne '');
4484 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4485 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4486 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4487 $c = 'O' if ($elements[$n + 2] eq '');
4488 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4489 } else {
4490 $c = 'E';
4491 }
4492
4493 my $ctx = "${a}x${c}";
4494
4495 my $at = "(ctx:$ctx)";
4496
4497 my $ptr = substr($blank, 0, $off) . "^";
4498 my $hereptr = "$hereline$ptr\n";
4499
4500 # Pull out the value of this operator.
4501 my $op_type = substr($curr_values, $off + 1, 1);
4502
4503 # Get the full operator variant.
4504 my $opv = $op . substr($curr_vars, $off, 1);
4505
4506 # Ignore operators passed as parameters.
4507 if ($op_type ne 'V' &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004508 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004509
4510# # Ignore comments
4511# } elsif ($op =~ /^$;+$/) {
4512
4513 # ; should have either the end of line or a space or \ after it
4514 } elsif ($op eq ';') {
4515 if ($ctx !~ /.x[WEBC]/ &&
4516 $cc !~ /^\\/ && $cc !~ /^;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004517 if (ERROR("SPACING",
4518 "space required after that '$op' $at\n" . $hereptr)) {
4519 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4520 $line_fixed = 1;
4521 }
Joe Hershberger05622192011-10-18 10:06:59 +00004522 }
4523
4524 # // is a comment
4525 } elsif ($op eq '//') {
4526
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004527 # : when part of a bitfield
4528 } elsif ($opv eq ':B') {
4529 # skip the bitfield test for now
4530
Joe Hershberger05622192011-10-18 10:06:59 +00004531 # No spaces for:
4532 # ->
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004533 } elsif ($op eq '->') {
Joe Hershberger05622192011-10-18 10:06:59 +00004534 if ($ctx =~ /Wx.|.xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004535 if (ERROR("SPACING",
4536 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4537 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4538 if (defined $fix_elements[$n + 2]) {
4539 $fix_elements[$n + 2] =~ s/^\s+//;
4540 }
4541 $line_fixed = 1;
4542 }
Joe Hershberger05622192011-10-18 10:06:59 +00004543 }
4544
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004545 # , must not have a space before and must have a space on the right.
Joe Hershberger05622192011-10-18 10:06:59 +00004546 } elsif ($op eq ',') {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004547 my $rtrim_before = 0;
4548 my $space_after = 0;
4549 if ($ctx =~ /Wx./) {
4550 if (ERROR("SPACING",
4551 "space prohibited before that '$op' $at\n" . $hereptr)) {
4552 $line_fixed = 1;
4553 $rtrim_before = 1;
4554 }
4555 }
Joe Hershberger05622192011-10-18 10:06:59 +00004556 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004557 if (ERROR("SPACING",
4558 "space required after that '$op' $at\n" . $hereptr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004559 $line_fixed = 1;
4560 $last_after = $n;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004561 $space_after = 1;
4562 }
4563 }
4564 if ($rtrim_before || $space_after) {
4565 if ($rtrim_before) {
4566 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4567 } else {
4568 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4569 }
4570 if ($space_after) {
4571 $good .= " ";
Tom Rini6b9709d2014-02-27 08:27:28 -05004572 }
Joe Hershberger05622192011-10-18 10:06:59 +00004573 }
4574
4575 # '*' as part of a type definition -- reported already.
4576 } elsif ($opv eq '*_') {
4577 #warn "'*' is part of type\n";
4578
4579 # unary operators should have a space before and
4580 # none after. May be left adjacent to another
4581 # unary operator, or a cast
4582 } elsif ($op eq '!' || $op eq '~' ||
4583 $opv eq '*U' || $opv eq '-U' ||
4584 $opv eq '&U' || $opv eq '&&U') {
4585 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004586 if (ERROR("SPACING",
4587 "space required before that '$op' $at\n" . $hereptr)) {
4588 if ($n != $last_after + 2) {
4589 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4590 $line_fixed = 1;
4591 }
4592 }
Joe Hershberger05622192011-10-18 10:06:59 +00004593 }
4594 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4595 # A unary '*' may be const
4596
4597 } elsif ($ctx =~ /.xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004598 if (ERROR("SPACING",
4599 "space prohibited after that '$op' $at\n" . $hereptr)) {
4600 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4601 if (defined $fix_elements[$n + 2]) {
4602 $fix_elements[$n + 2] =~ s/^\s+//;
4603 }
4604 $line_fixed = 1;
4605 }
Joe Hershberger05622192011-10-18 10:06:59 +00004606 }
4607
4608 # unary ++ and unary -- are allowed no space on one side.
4609 } elsif ($op eq '++' or $op eq '--') {
4610 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004611 if (ERROR("SPACING",
4612 "space required one side of that '$op' $at\n" . $hereptr)) {
4613 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4614 $line_fixed = 1;
4615 }
Joe Hershberger05622192011-10-18 10:06:59 +00004616 }
4617 if ($ctx =~ /Wx[BE]/ ||
4618 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004619 if (ERROR("SPACING",
4620 "space prohibited before that '$op' $at\n" . $hereptr)) {
4621 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4622 $line_fixed = 1;
4623 }
Joe Hershberger05622192011-10-18 10:06:59 +00004624 }
4625 if ($ctx =~ /ExW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004626 if (ERROR("SPACING",
4627 "space prohibited after that '$op' $at\n" . $hereptr)) {
4628 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4629 if (defined $fix_elements[$n + 2]) {
4630 $fix_elements[$n + 2] =~ s/^\s+//;
4631 }
4632 $line_fixed = 1;
4633 }
Joe Hershberger05622192011-10-18 10:06:59 +00004634 }
4635
Joe Hershberger05622192011-10-18 10:06:59 +00004636 # << and >> may either have or not have spaces both sides
4637 } elsif ($op eq '<<' or $op eq '>>' or
4638 $op eq '&' or $op eq '^' or $op eq '|' or
4639 $op eq '+' or $op eq '-' or
4640 $op eq '*' or $op eq '/' or
4641 $op eq '%')
4642 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004643 if ($check) {
4644 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4645 if (CHK("SPACING",
4646 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4647 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4648 $fix_elements[$n + 2] =~ s/^\s+//;
4649 $line_fixed = 1;
4650 }
4651 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4652 if (CHK("SPACING",
4653 "space preferred before that '$op' $at\n" . $hereptr)) {
4654 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4655 $line_fixed = 1;
4656 }
4657 }
4658 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004659 if (ERROR("SPACING",
4660 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4661 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4662 if (defined $fix_elements[$n + 2]) {
4663 $fix_elements[$n + 2] =~ s/^\s+//;
4664 }
4665 $line_fixed = 1;
4666 }
Joe Hershberger05622192011-10-18 10:06:59 +00004667 }
4668
4669 # A colon needs no spaces before when it is
4670 # terminating a case value or a label.
4671 } elsif ($opv eq ':C' || $opv eq ':L') {
4672 if ($ctx =~ /Wx./) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004673 if (ERROR("SPACING",
4674 "space prohibited before that '$op' $at\n" . $hereptr)) {
4675 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4676 $line_fixed = 1;
4677 }
Joe Hershberger05622192011-10-18 10:06:59 +00004678 }
4679
4680 # All the others need spaces both sides.
4681 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4682 my $ok = 0;
4683
4684 # Ignore email addresses <foo@bar>
4685 if (($op eq '<' &&
4686 $cc =~ /^\S+\@\S+>/) ||
4687 ($op eq '>' &&
4688 $ca =~ /<\S+\@\S+$/))
4689 {
Tom Rinic57383b2020-06-16 10:29:46 -04004690 $ok = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00004691 }
4692
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004693 # for asm volatile statements
4694 # ignore a colon with another
4695 # colon immediately before or after
4696 if (($op eq ':') &&
4697 ($ca =~ /:$/ || $cc =~ /^:/)) {
4698 $ok = 1;
4699 }
4700
Tom Rini6b9709d2014-02-27 08:27:28 -05004701 # messages are ERROR, but ?: are CHK
Joe Hershberger05622192011-10-18 10:06:59 +00004702 if ($ok == 0) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004703 my $msg_level = \&ERROR;
4704 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Tom Rini6b9709d2014-02-27 08:27:28 -05004705
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004706 if (&{$msg_level}("SPACING",
4707 "spaces required around that '$op' $at\n" . $hereptr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004708 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4709 if (defined $fix_elements[$n + 2]) {
4710 $fix_elements[$n + 2] =~ s/^\s+//;
4711 }
4712 $line_fixed = 1;
4713 }
Joe Hershberger05622192011-10-18 10:06:59 +00004714 }
4715 }
4716 $off += length($elements[$n + 1]);
Tom Rini6b9709d2014-02-27 08:27:28 -05004717
4718## print("n: <$n> GOOD: <$good>\n");
4719
4720 $fixed_line = $fixed_line . $good;
4721 }
4722
4723 if (($#elements % 2) == 0) {
4724 $fixed_line = $fixed_line . $fix_elements[$#elements];
4725 }
4726
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004727 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4728 $fixed[$fixlinenr] = $fixed_line;
Tom Rini6b9709d2014-02-27 08:27:28 -05004729 }
4730
4731
4732 }
4733
4734# check for whitespace before a non-naked semicolon
4735 if ($line =~ /^\+.*\S\s+;\s*$/) {
4736 if (WARN("SPACING",
4737 "space prohibited before semicolon\n" . $herecurr) &&
4738 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004739 1 while $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004740 s/^(\+.*\S)\s+;/$1;/;
Joe Hershberger05622192011-10-18 10:06:59 +00004741 }
4742 }
4743
4744# check for multiple assignments
4745 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4746 CHK("MULTIPLE_ASSIGNMENTS",
4747 "multiple assignments should be avoided\n" . $herecurr);
4748 }
4749
4750## # check for multiple declarations, allowing for a function declaration
4751## # continuation.
4752## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4753## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4754##
4755## # Remove any bracketed sections to ensure we do not
4756## # falsly report the parameters of functions.
4757## my $ln = $line;
4758## while ($ln =~ s/\([^\(\)]*\)//g) {
4759## }
4760## if ($ln =~ /,/) {
4761## WARN("MULTIPLE_DECLARATION",
4762## "declaring multiple variables together should be avoided\n" . $herecurr);
4763## }
4764## }
4765
4766#need space before brace following if, while, etc
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004767 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004768 $line =~ /\b(?:else|do)\{/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004769 if (ERROR("SPACING",
4770 "space required before the open brace '{'\n" . $herecurr) &&
4771 $fix) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004772 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004773 }
Joe Hershberger05622192011-10-18 10:06:59 +00004774 }
4775
Tom Rini6b9709d2014-02-27 08:27:28 -05004776## # check for blank lines before declarations
4777## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4778## $prevrawline =~ /^.\s*$/) {
4779## WARN("SPACING",
4780## "No blank lines before declarations\n" . $hereprev);
4781## }
4782##
4783
Joe Hershberger05622192011-10-18 10:06:59 +00004784# closing brace should have a space following it when it has anything
4785# on the line
Tom Rinic57383b2020-06-16 10:29:46 -04004786 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004787 if (ERROR("SPACING",
4788 "space required after that close brace '}'\n" . $herecurr) &&
4789 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004790 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004791 s/}((?!(?:,|;|\)))\S)/} $1/;
4792 }
Joe Hershberger05622192011-10-18 10:06:59 +00004793 }
4794
4795# check spacing on square brackets
4796 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004797 if (ERROR("SPACING",
4798 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4799 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004800 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004801 s/\[\s+/\[/;
4802 }
Joe Hershberger05622192011-10-18 10:06:59 +00004803 }
4804 if ($line =~ /\s\]/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004805 if (ERROR("SPACING",
4806 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4807 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004808 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004809 s/\s+\]/\]/;
4810 }
Joe Hershberger05622192011-10-18 10:06:59 +00004811 }
4812
4813# check spacing on parentheses
4814 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4815 $line !~ /for\s*\(\s+;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004816 if (ERROR("SPACING",
4817 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4818 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004819 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004820 s/\(\s+/\(/;
4821 }
Joe Hershberger05622192011-10-18 10:06:59 +00004822 }
4823 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4824 $line !~ /for\s*\(.*;\s+\)/ &&
4825 $line !~ /:\s+\)/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004826 if (ERROR("SPACING",
4827 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4828 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004829 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004830 s/\s+\)/\)/;
4831 }
Joe Hershberger05622192011-10-18 10:06:59 +00004832 }
4833
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004834# check unnecessary parentheses around addressof/dereference single $Lvals
4835# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4836
4837 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4838 my $var = $1;
4839 if (CHK("UNNECESSARY_PARENTHESES",
4840 "Unnecessary parentheses around $var\n" . $herecurr) &&
4841 $fix) {
4842 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4843 }
4844 }
4845
4846# check for unnecessary parentheses around function pointer uses
4847# ie: (foo->bar)(); should be foo->bar();
4848# but not "if (foo->bar) (" to avoid some false positives
4849 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4850 my $var = $2;
4851 if (CHK("UNNECESSARY_PARENTHESES",
4852 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4853 $fix) {
4854 my $var2 = deparenthesize($var);
4855 $var2 =~ s/\s//g;
4856 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4857 }
4858 }
4859
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004860# check for unnecessary parentheses around comparisons in if uses
4861# when !drivers/staging or command-line uses --strict
4862 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004863 $perl_version_ok && defined($stat) &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004864 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4865 my $if_stat = $1;
4866 my $test = substr($2, 1, -1);
4867 my $herectx;
4868 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4869 my $match = $1;
4870 # avoid parentheses around potential macro args
4871 next if ($match =~ /^\s*\w+\s*$/);
4872 if (!defined($herectx)) {
4873 $herectx = $here . "\n";
4874 my $cnt = statement_rawlines($if_stat);
4875 for (my $n = 0; $n < $cnt; $n++) {
4876 my $rl = raw_line($linenr, $n);
4877 $herectx .= $rl . "\n";
4878 last if $rl =~ /^[ \+].*\{/;
4879 }
4880 }
4881 CHK("UNNECESSARY_PARENTHESES",
4882 "Unnecessary parentheses around '$match'\n" . $herectx);
4883 }
4884 }
4885
Joe Hershberger05622192011-10-18 10:06:59 +00004886#goto labels aren't indented, allow a single space however
4887 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4888 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004889 if (WARN("INDENTED_LABEL",
4890 "labels should not be indented\n" . $herecurr) &&
4891 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004892 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004893 s/^(.)\s+/$1/;
4894 }
Joe Hershberger05622192011-10-18 10:06:59 +00004895 }
4896
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004897# return is not a function
Tom Rini6b9709d2014-02-27 08:27:28 -05004898 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Joe Hershberger05622192011-10-18 10:06:59 +00004899 my $spacing = $1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004900 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004901 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4902 my $value = $1;
4903 $value = deparenthesize($value);
4904 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4905 ERROR("RETURN_PARENTHESES",
4906 "return is not a function, parentheses are not required\n" . $herecurr);
4907 }
Joe Hershberger05622192011-10-18 10:06:59 +00004908 } elsif ($spacing !~ /\s+/) {
4909 ERROR("SPACING",
4910 "space required before the open parenthesis '('\n" . $herecurr);
4911 }
4912 }
Tom Rini6b9709d2014-02-27 08:27:28 -05004913
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004914# unnecessary return in a void function
4915# at end-of-function, with the previous line a single leading tab, then return;
4916# and the line before that not a goto label target like "out:"
4917 if ($sline =~ /^[ \+]}\s*$/ &&
4918 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4919 $linenr >= 3 &&
4920 $lines[$linenr - 3] =~ /^[ +]/ &&
4921 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4922 WARN("RETURN_VOID",
4923 "void function return statements are not generally useful\n" . $hereprev);
4924 }
4925
Tom Rini6b9709d2014-02-27 08:27:28 -05004926# if statements using unnecessary parentheses - ie: if ((foo == bar))
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004927 if ($perl_version_ok &&
Tom Rini6b9709d2014-02-27 08:27:28 -05004928 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4929 my $openparens = $1;
4930 my $count = $openparens =~ tr@\(@\(@;
4931 my $msg = "";
4932 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4933 my $comp = $4; #Not $1 because of $LvalOrFunc
4934 $msg = " - maybe == should be = ?" if ($comp eq "==");
4935 WARN("UNNECESSARY_PARENTHESES",
4936 "Unnecessary parentheses$msg\n" . $herecurr);
4937 }
4938 }
4939
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004940# comparisons with a constant or upper case identifier on the left
4941# avoid cases like "foo + BAR < baz"
4942# only fix matches surrounded by parentheses to avoid incorrect
4943# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004944 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004945 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4946 my $lead = $1;
4947 my $const = $2;
4948 my $comp = $3;
4949 my $to = $4;
4950 my $newcomp = $comp;
4951 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4952 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4953 WARN("CONSTANT_COMPARISON",
4954 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4955 $fix) {
4956 if ($comp eq "<") {
4957 $newcomp = ">";
4958 } elsif ($comp eq "<=") {
4959 $newcomp = ">=";
4960 } elsif ($comp eq ">") {
4961 $newcomp = "<";
4962 } elsif ($comp eq ">=") {
4963 $newcomp = "<=";
4964 }
4965 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4966 }
4967 }
4968
4969# Return of what appears to be an errno should normally be negative
4970 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004971 my $name = $1;
4972 if ($name ne 'EOF' && $name ne 'ERROR') {
4973 WARN("USE_NEGATIVE_ERRNO",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004974 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004975 }
4976 }
4977
Joe Hershberger05622192011-10-18 10:06:59 +00004978# Need a space before open parenthesis after if, while etc
Tom Rini6b9709d2014-02-27 08:27:28 -05004979 if ($line =~ /\b(if|while|for|switch)\(/) {
4980 if (ERROR("SPACING",
4981 "space required before the open parenthesis '('\n" . $herecurr) &&
4982 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004983 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004984 s/\b(if|while|for|switch)\(/$1 \(/;
4985 }
Joe Hershberger05622192011-10-18 10:06:59 +00004986 }
4987
4988# Check for illegal assignment in if conditional -- and check for trailing
4989# statements after the conditional.
4990 if ($line =~ /do\s*(?!{)/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004991 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4992 ctx_statement_block($linenr, $realcnt, 0)
4993 if (!defined $stat);
Joe Hershberger05622192011-10-18 10:06:59 +00004994 my ($stat_next) = ctx_statement_block($line_nr_next,
4995 $remain_next, $off_next);
4996 $stat_next =~ s/\n./\n /g;
4997 ##print "stat<$stat> stat_next<$stat_next>\n";
4998
4999 if ($stat_next =~ /^\s*while\b/) {
5000 # If the statement carries leading newlines,
5001 # then count those as offsets.
5002 my ($whitespace) =
5003 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5004 my $offset =
5005 statement_rawlines($whitespace) - 1;
5006
5007 $suppress_whiletrailers{$line_nr_next +
5008 $offset} = 1;
5009 }
5010 }
5011 if (!defined $suppress_whiletrailers{$linenr} &&
Tom Rini6b9709d2014-02-27 08:27:28 -05005012 defined($stat) && defined($cond) &&
Joe Hershberger05622192011-10-18 10:06:59 +00005013 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5014 my ($s, $c) = ($stat, $cond);
5015
5016 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
5017 ERROR("ASSIGN_IN_IF",
5018 "do not use assignment in if condition\n" . $herecurr);
5019 }
5020
5021 # Find out what is on the end of the line after the
5022 # conditional.
5023 substr($s, 0, length($c), '');
5024 $s =~ s/\n.*//g;
Tom Rinic57383b2020-06-16 10:29:46 -04005025 $s =~ s/$;//g; # Remove any comments
Joe Hershberger05622192011-10-18 10:06:59 +00005026 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5027 $c !~ /}\s*while\s*/)
5028 {
5029 # Find out how long the conditional actually is.
5030 my @newlines = ($c =~ /\n/gs);
5031 my $cond_lines = 1 + $#newlines;
5032 my $stat_real = '';
5033
5034 $stat_real = raw_line($linenr, $cond_lines)
5035 . "\n" if ($cond_lines);
5036 if (defined($stat_real) && $cond_lines > 1) {
5037 $stat_real = "[...]\n$stat_real";
5038 }
5039
5040 ERROR("TRAILING_STATEMENTS",
5041 "trailing statements should be on next line\n" . $herecurr . $stat_real);
5042 }
5043 }
5044
5045# Check for bitwise tests written as boolean
5046 if ($line =~ /
5047 (?:
5048 (?:\[|\(|\&\&|\|\|)
5049 \s*0[xX][0-9]+\s*
5050 (?:\&\&|\|\|)
5051 |
5052 (?:\&\&|\|\|)
5053 \s*0[xX][0-9]+\s*
5054 (?:\&\&|\|\||\)|\])
5055 )/x)
5056 {
5057 WARN("HEXADECIMAL_BOOLEAN_TEST",
5058 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
5059 }
5060
5061# if and else should not have general statements after it
5062 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5063 my $s = $1;
Tom Rinic57383b2020-06-16 10:29:46 -04005064 $s =~ s/$;//g; # Remove any comments
Joe Hershberger05622192011-10-18 10:06:59 +00005065 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5066 ERROR("TRAILING_STATEMENTS",
5067 "trailing statements should be on next line\n" . $herecurr);
5068 }
5069 }
5070# if should not continue a brace
5071 if ($line =~ /}\s*if\b/) {
5072 ERROR("TRAILING_STATEMENTS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005073 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Joe Hershberger05622192011-10-18 10:06:59 +00005074 $herecurr);
5075 }
5076# case and default should not have general statements after them
5077 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5078 $line !~ /\G(?:
5079 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5080 \s*return\s+
5081 )/xg)
5082 {
5083 ERROR("TRAILING_STATEMENTS",
5084 "trailing statements should be on next line\n" . $herecurr);
5085 }
5086
5087 # Check for }<nl>else {, these must be at the same
5088 # indent level to be relevant to each other.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005089 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5090 $previndent == $indent) {
5091 if (ERROR("ELSE_AFTER_BRACE",
5092 "else should follow close brace '}'\n" . $hereprev) &&
5093 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5094 fix_delete_line($fixlinenr - 1, $prevrawline);
5095 fix_delete_line($fixlinenr, $rawline);
5096 my $fixedline = $prevrawline;
5097 $fixedline =~ s/}\s*$//;
5098 if ($fixedline !~ /^\+\s*$/) {
5099 fix_insert_line($fixlinenr, $fixedline);
5100 }
5101 $fixedline = $rawline;
5102 $fixedline =~ s/^(.\s*)else/$1} else/;
5103 fix_insert_line($fixlinenr, $fixedline);
5104 }
Joe Hershberger05622192011-10-18 10:06:59 +00005105 }
5106
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005107 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5108 $previndent == $indent) {
Joe Hershberger05622192011-10-18 10:06:59 +00005109 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5110
5111 # Find out what is on the end of the line after the
5112 # conditional.
5113 substr($s, 0, length($c), '');
5114 $s =~ s/\n.*//g;
5115
5116 if ($s =~ /^\s*;/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005117 if (ERROR("WHILE_AFTER_BRACE",
5118 "while should follow close brace '}'\n" . $hereprev) &&
5119 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5120 fix_delete_line($fixlinenr - 1, $prevrawline);
5121 fix_delete_line($fixlinenr, $rawline);
5122 my $fixedline = $prevrawline;
5123 my $trailing = $rawline;
5124 $trailing =~ s/^\+//;
5125 $trailing = trim($trailing);
5126 $fixedline =~ s/}\s*$/} $trailing/;
5127 fix_insert_line($fixlinenr, $fixedline);
5128 }
Joe Hershberger05622192011-10-18 10:06:59 +00005129 }
5130 }
5131
Tom Rini6b9709d2014-02-27 08:27:28 -05005132#Specific variable tests
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005133 while ($line =~ m{($Constant|$Lval)}g) {
5134 my $var = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05005135
Tom Rini6b9709d2014-02-27 08:27:28 -05005136#CamelCase
5137 if ($var !~ /^$Constant$/ &&
5138 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
5139#Ignore Page<foo> variants
5140 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Tom Rinic57383b2020-06-16 10:29:46 -04005141#Ignore SI style variants like nS, mV and dB
5142#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5143 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005144#Ignore some three character SI units explicitly, like MiB and KHz
5145 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005146 while ($var =~ m{($Ident)}g) {
5147 my $word = $1;
5148 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5149 if ($check) {
5150 seed_camelcase_includes();
5151 if (!$file && !$camelcase_file_seeded) {
5152 seed_camelcase_file($realfile);
5153 $camelcase_file_seeded = 1;
5154 }
5155 }
5156 if (!defined $camelcase{$word}) {
5157 $camelcase{$word} = 1;
5158 CHK("CAMELCASE",
5159 "Avoid CamelCase: <$word>\n" . $herecurr);
5160 }
5161 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005162 }
5163 }
Joe Hershberger05622192011-10-18 10:06:59 +00005164
5165#no spaces allowed after \ in define
Tom Rini6b9709d2014-02-27 08:27:28 -05005166 if ($line =~ /\#\s*define.*\\\s+$/) {
5167 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5168 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5169 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005170 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05005171 }
Joe Hershberger05622192011-10-18 10:06:59 +00005172 }
5173
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005174# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5175# itself <asm/foo.h> (uses RAW line)
Joe Hershberger05622192011-10-18 10:06:59 +00005176 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5177 my $file = "$1.h";
5178 my $checkfile = "include/linux/$file";
5179 if (-f "$root/$checkfile" &&
5180 $realfile ne $checkfile &&
5181 $1 !~ /$allowed_asm_includes/)
5182 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005183 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5184 if ($asminclude > 0) {
5185 if ($realfile =~ m{^arch/}) {
5186 CHK("ARCH_INCLUDE_LINUX",
5187 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5188 } else {
5189 WARN("INCLUDE_LINUX",
5190 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5191 }
Joe Hershberger05622192011-10-18 10:06:59 +00005192 }
5193 }
5194 }
5195
5196# multi-statement macros should be enclosed in a do while loop, grab the
5197# first statement and ensure its the whole macro if its not enclosed
5198# in a known good container
5199 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5200 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5201 my $ln = $linenr;
5202 my $cnt = $realcnt;
5203 my ($off, $dstat, $dcond, $rest);
5204 my $ctx = '';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005205 my $has_flow_statement = 0;
5206 my $has_arg_concat = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005207 ($dstat, $dcond, $ln, $cnt, $off) =
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005208 ctx_statement_block($linenr, $realcnt, 0);
5209 $ctx = $dstat;
Joe Hershberger05622192011-10-18 10:06:59 +00005210 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5211 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5212
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005213 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5214 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5215
5216 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5217 my $define_args = $1;
5218 my $define_stmt = $dstat;
5219 my @def_args = ();
5220
5221 if (defined $define_args && $define_args ne "") {
5222 $define_args = substr($define_args, 1, length($define_args) - 2);
5223 $define_args =~ s/\s*//g;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005224 $define_args =~ s/\\\+?//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005225 @def_args = split(",", $define_args);
5226 }
5227
Joe Hershberger05622192011-10-18 10:06:59 +00005228 $dstat =~ s/$;//g;
5229 $dstat =~ s/\\\n.//g;
5230 $dstat =~ s/^\s*//s;
5231 $dstat =~ s/\s*$//s;
5232
5233 # Flatten any parentheses and braces
5234 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5235 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005236 $dstat =~ s/.\[[^\[\]]*\]/1/)
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005237 {
5238 }
5239
Tom Rinic57383b2020-06-16 10:29:46 -04005240 # Flatten any obvious string concatenation.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005241 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5242 $dstat =~ s/$Ident\s*($String)/$1/)
Joe Hershberger05622192011-10-18 10:06:59 +00005243 {
5244 }
5245
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005246 # Make asm volatile uses seem like a generic function
5247 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5248
Joe Hershberger05622192011-10-18 10:06:59 +00005249 my $exceptions = qr{
5250 $Declare|
5251 module_param_named|
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005252 MODULE_PARM_DESC|
Joe Hershberger05622192011-10-18 10:06:59 +00005253 DECLARE_PER_CPU|
5254 DEFINE_PER_CPU|
5255 __typeof__\(|
5256 union|
5257 struct|
5258 \.$Ident\s*=\s*|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005259 ^\"|\"$|
5260 ^\[
Joe Hershberger05622192011-10-18 10:06:59 +00005261 }x;
5262 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005263
5264 $ctx =~ s/\n*$//;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005265 my $stmt_cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005266 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005267
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005268 if ($dstat ne '' &&
5269 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5270 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Tom Rini6b9709d2014-02-27 08:27:28 -05005271 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005272 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005273 $dstat !~ /$exceptions/ &&
5274 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Tom Rini6b9709d2014-02-27 08:27:28 -05005275 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005276 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
5277 $dstat !~ /^for\s*$Constant$/ && # for (...)
5278 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5279 $dstat !~ /^do\s*{/ && # do {...
Heiko Schocherd8a1a302016-01-04 09:17:19 +01005280 $dstat !~ /^\(\{/ && # ({...
Tom Rini6b9709d2014-02-27 08:27:28 -05005281 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005282 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005283 if ($dstat =~ /^\s*if\b/) {
5284 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5285 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5286 } elsif ($dstat =~ /;/) {
5287 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5288 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5289 } else {
5290 ERROR("COMPLEX_MACRO",
5291 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5292 }
5293
5294 }
5295
5296 # Make $define_stmt single line, comment-free, etc
5297 my @stmt_array = split('\n', $define_stmt);
5298 my $first = 1;
5299 $define_stmt = "";
5300 foreach my $l (@stmt_array) {
5301 $l =~ s/\\$//;
5302 if ($first) {
5303 $define_stmt = $l;
5304 $first = 0;
5305 } elsif ($l =~ /^[\+ ]/) {
5306 $define_stmt .= substr($l, 1);
5307 }
5308 }
5309 $define_stmt =~ s/$;//g;
5310 $define_stmt =~ s/\s+/ /g;
5311 $define_stmt = trim($define_stmt);
5312
5313# check if any macro arguments are reused (ignore '...' and 'type')
5314 foreach my $arg (@def_args) {
5315 next if ($arg =~ /\.\.\./);
5316 next if ($arg =~ /^type$/i);
5317 my $tmp_stmt = $define_stmt;
Tom Rinic57383b2020-06-16 10:29:46 -04005318 $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005319 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5320 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005321 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005322 if ($use_cnt > 1) {
5323 CHK("MACRO_ARG_REUSE",
5324 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5325 }
5326# check if any macro arguments may have other precedence issues
5327 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5328 ((defined($1) && $1 ne ',') ||
5329 (defined($2) && $2 ne ','))) {
5330 CHK("MACRO_ARG_PRECEDENCE",
5331 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5332 }
5333 }
5334
5335# check for macros with flow control, but without ## concatenation
5336# ## concatenation is commonly a macro that defines a function so ignore those
5337 if ($has_flow_statement && !$has_arg_concat) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005338 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005339 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005340
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005341 WARN("MACRO_WITH_FLOW_CONTROL",
5342 "Macros with flow control statements should be avoided\n" . "$herectx");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005343 }
5344
5345# check for line continuations outside of #defines, preprocessor #, and asm
5346
5347 } else {
5348 if ($prevline !~ /^..*\\$/ &&
5349 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5350 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5351 $line =~ /^\+.*\\$/) {
5352 WARN("LINE_CONTINUATIONS",
5353 "Avoid unnecessary line continuations\n" . $herecurr);
5354 }
5355 }
5356
5357# do {} while (0) macro tests:
5358# single-statement macros do not need to be enclosed in do while (0) loop,
5359# macro should not end with a semicolon
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005360 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005361 $realfile !~ m@/vmlinux.lds.h$@ &&
5362 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5363 my $ln = $linenr;
5364 my $cnt = $realcnt;
5365 my ($off, $dstat, $dcond, $rest);
5366 my $ctx = '';
5367 ($dstat, $dcond, $ln, $cnt, $off) =
5368 ctx_statement_block($linenr, $realcnt, 0);
5369 $ctx = $dstat;
5370
5371 $dstat =~ s/\\\n.//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005372 $dstat =~ s/$;/ /g;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005373
5374 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5375 my $stmts = $2;
5376 my $semis = $3;
5377
5378 $ctx =~ s/\n*$//;
5379 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005380 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005381
5382 if (($stmts =~ tr/;/;/) == 1 &&
5383 $stmts !~ /^\s*(if|while|for|switch)\b/) {
5384 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5385 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5386 }
5387 if (defined $semis && $semis ne "") {
5388 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5389 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
Joe Hershberger05622192011-10-18 10:06:59 +00005390 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005391 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5392 $ctx =~ s/\n*$//;
5393 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005394 my $herectx = get_stat_here($linenr, $cnt, $here);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005395
5396 WARN("TRAILING_SEMICOLON",
5397 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Hershberger05622192011-10-18 10:06:59 +00005398 }
5399 }
5400
Joe Hershberger05622192011-10-18 10:06:59 +00005401# check for redundant bracing round if etc
5402 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5403 my ($level, $endln, @chunks) =
5404 ctx_statement_full($linenr, $realcnt, 1);
5405 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5406 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5407 if ($#chunks > 0 && $level == 0) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005408 my @allowed = ();
5409 my $allow = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005410 my $seen = 0;
5411 my $herectx = $here . "\n";
5412 my $ln = $linenr - 1;
5413 for my $chunk (@chunks) {
5414 my ($cond, $block) = @{$chunk};
5415
5416 # If the condition carries leading newlines, then count those as offsets.
5417 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5418 my $offset = statement_rawlines($whitespace) - 1;
5419
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005420 $allowed[$allow] = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005421 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5422
5423 # We have looked at and allowed this specific line.
5424 $suppress_ifbraces{$ln + $offset} = 1;
5425
5426 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5427 $ln += statement_rawlines($block) - 1;
5428
5429 substr($block, 0, length($cond), '');
5430
5431 $seen++ if ($block =~ /^\s*{/);
5432
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005433 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00005434 if (statement_lines($cond) > 1) {
5435 #print "APW: ALLOWED: cond<$cond>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005436 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005437 }
5438 if ($block =~/\b(?:if|for|while)\b/) {
5439 #print "APW: ALLOWED: block<$block>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005440 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005441 }
5442 if (statement_block_size($block) > 1) {
5443 #print "APW: ALLOWED: lines block<$block>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005444 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005445 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005446 $allow++;
Joe Hershberger05622192011-10-18 10:06:59 +00005447 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005448 if ($seen) {
5449 my $sum_allowed = 0;
5450 foreach (@allowed) {
5451 $sum_allowed += $_;
5452 }
5453 if ($sum_allowed == 0) {
5454 WARN("BRACES",
5455 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5456 } elsif ($sum_allowed != $allow &&
5457 $seen != $allow) {
5458 CHK("BRACES",
5459 "braces {} should be used on all arms of this statement\n" . $herectx);
5460 }
Joe Hershberger05622192011-10-18 10:06:59 +00005461 }
5462 }
5463 }
5464 if (!defined $suppress_ifbraces{$linenr - 1} &&
5465 $line =~ /\b(if|while|for|else)\b/) {
5466 my $allowed = 0;
5467
5468 # Check the pre-context.
5469 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5470 #print "APW: ALLOWED: pre<$1>\n";
5471 $allowed = 1;
5472 }
5473
5474 my ($level, $endln, @chunks) =
5475 ctx_statement_full($linenr, $realcnt, $-[0]);
5476
5477 # Check the condition.
5478 my ($cond, $block) = @{$chunks[0]};
5479 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5480 if (defined $cond) {
5481 substr($block, 0, length($cond), '');
5482 }
5483 if (statement_lines($cond) > 1) {
5484 #print "APW: ALLOWED: cond<$cond>\n";
5485 $allowed = 1;
5486 }
5487 if ($block =~/\b(?:if|for|while)\b/) {
5488 #print "APW: ALLOWED: block<$block>\n";
5489 $allowed = 1;
5490 }
5491 if (statement_block_size($block) > 1) {
5492 #print "APW: ALLOWED: lines block<$block>\n";
5493 $allowed = 1;
5494 }
5495 # Check the post-context.
5496 if (defined $chunks[1]) {
5497 my ($cond, $block) = @{$chunks[1]};
5498 if (defined $cond) {
5499 substr($block, 0, length($cond), '');
5500 }
5501 if ($block =~ /^\s*\{/) {
5502 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5503 $allowed = 1;
5504 }
5505 }
5506 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Joe Hershberger05622192011-10-18 10:06:59 +00005507 my $cnt = statement_rawlines($block);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005508 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Hershberger05622192011-10-18 10:06:59 +00005509
5510 WARN("BRACES",
5511 "braces {} are not necessary for single statement blocks\n" . $herectx);
5512 }
5513 }
5514
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005515# check for single line unbalanced braces
5516 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5517 $sline =~ /^.\s*else\s*\{\s*$/) {
5518 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5519 }
5520
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005521# check for unnecessary blank lines around braces
Tom Rini6b9709d2014-02-27 08:27:28 -05005522 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005523 if (CHK("BRACES",
5524 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5525 $fix && $prevrawline =~ /^\+/) {
5526 fix_delete_line($fixlinenr - 1, $prevrawline);
5527 }
Joe Hershberger05622192011-10-18 10:06:59 +00005528 }
Tom Rini6b9709d2014-02-27 08:27:28 -05005529 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005530 if (CHK("BRACES",
5531 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5532 $fix) {
5533 fix_delete_line($fixlinenr, $rawline);
5534 }
Joe Hershberger05622192011-10-18 10:06:59 +00005535 }
5536
5537# no volatiles please
5538 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5539 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5540 WARN("VOLATILE",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005541 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
5542 }
5543
5544# Check for user-visible strings broken across lines, which breaks the ability
5545# to grep for the string. Make exceptions when the previous string ends in a
5546# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5547# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5548 if ($line =~ /^\+\s*$String/ &&
5549 $prevline =~ /"\s*$/ &&
5550 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5551 if (WARN("SPLIT_STRING",
5552 "quoted string split across lines\n" . $hereprev) &&
5553 $fix &&
5554 $prevrawline =~ /^\+.*"\s*$/ &&
5555 $last_coalesced_string_linenr != $linenr - 1) {
5556 my $extracted_string = get_quoted_string($line, $rawline);
5557 my $comma_close = "";
5558 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5559 $comma_close = $1;
5560 }
5561
5562 fix_delete_line($fixlinenr - 1, $prevrawline);
5563 fix_delete_line($fixlinenr, $rawline);
5564 my $fixedline = $prevrawline;
5565 $fixedline =~ s/"\s*$//;
5566 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5567 fix_insert_line($fixlinenr - 1, $fixedline);
5568 $fixedline = $rawline;
5569 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5570 if ($fixedline !~ /\+\s*$/) {
5571 fix_insert_line($fixlinenr, $fixedline);
5572 }
5573 $last_coalesced_string_linenr = $linenr;
5574 }
5575 }
5576
5577# check for missing a space in a string concatenation
5578 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5579 WARN('MISSING_SPACE',
5580 "break quoted strings at a space character\n" . $hereprev);
5581 }
5582
5583# check for an embedded function name in a string when the function is known
5584# This does not work very well for -f --file checking as it depends on patch
5585# context providing the function name or a single line form for in-file
5586# function declarations
5587 if ($line =~ /^\+.*$String/ &&
5588 defined($context_function) &&
5589 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5590 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
5591 WARN("EMBEDDED_FUNCTION_NAME",
5592 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
5593 }
5594
5595# check for spaces before a quoted newline
5596 if ($rawline =~ /^.*\".*\s\\n/) {
5597 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5598 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5599 $fix) {
5600 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5601 }
5602
5603 }
5604
5605# concatenated string without spaces between elements
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005606 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5607 if (CHK("CONCATENATED_STRING",
5608 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5609 $fix) {
5610 while ($line =~ /($String)/g) {
5611 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5612 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5613 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5614 }
5615 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005616 }
5617
5618# uncoalesced string fragments
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005619 if ($line =~ /$String\s*"/) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005620 if (WARN("STRING_FRAGMENTS",
5621 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5622 $fix) {
5623 while ($line =~ /($String)(?=\s*")/g) {
5624 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5625 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5626 }
5627 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005628 }
5629
5630# check for non-standard and hex prefixed decimal printf formats
5631 my $show_L = 1; #don't show the same defect twice
5632 my $show_Z = 1;
5633 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5634 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5635 $string =~ s/%%/__/g;
5636 # check for %L
5637 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5638 WARN("PRINTF_L",
5639 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5640 $show_L = 0;
5641 }
5642 # check for %Z
5643 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5644 WARN("PRINTF_Z",
5645 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5646 $show_Z = 0;
5647 }
5648 # check for 0x<decimal>
5649 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5650 ERROR("PRINTF_0XDECIMAL",
5651 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5652 }
5653 }
5654
5655# check for line continuations in quoted strings with odd counts of "
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005656 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005657 WARN("LINE_CONTINUATIONS",
5658 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005659 }
5660
5661# warn about #if 0
5662 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005663 WARN("IF_0",
5664 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5665 }
5666
5667# warn about #if 1
5668 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5669 WARN("IF_1",
5670 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005671 }
5672
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005673# check for needless "if (<foo>) fn(<foo>)" uses
5674 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005675 my $tested = quotemeta($1);
5676 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5677 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5678 my $func = $1;
5679 if (WARN('NEEDLESS_IF',
5680 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5681 $fix) {
5682 my $do_fix = 1;
5683 my $leading_tabs = "";
5684 my $new_leading_tabs = "";
5685 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5686 $leading_tabs = $1;
5687 } else {
5688 $do_fix = 0;
5689 }
5690 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5691 $new_leading_tabs = $1;
5692 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5693 $do_fix = 0;
5694 }
5695 } else {
5696 $do_fix = 0;
5697 }
5698 if ($do_fix) {
5699 fix_delete_line($fixlinenr - 1, $prevrawline);
5700 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5701 }
5702 }
5703 }
5704 }
5705
5706# check for unnecessary "Out of Memory" messages
5707 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5708 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5709 (defined $1 || defined $3) &&
5710 $linenr > 3) {
5711 my $testval = $2;
5712 my $testline = $lines[$linenr - 3];
5713
5714 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5715# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5716
Tom Rinic57383b2020-06-16 10:29:46 -04005717 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5718 $s !~ /\b__GFP_NOWARN\b/ ) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005719 WARN("OOM_MESSAGE",
5720 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5721 }
5722 }
5723
5724# check for logging functions with KERN_<LEVEL>
5725 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5726 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5727 my $level = $1;
5728 if (WARN("UNNECESSARY_KERN_LEVEL",
5729 "Possible unnecessary $level\n" . $herecurr) &&
5730 $fix) {
5731 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5732 }
5733 }
5734
5735# check for logging continuations
5736 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5737 WARN("LOGGING_CONTINUATION",
5738 "Avoid logging continuation uses where feasible\n" . $herecurr);
5739 }
5740
5741# check for mask then right shift without a parentheses
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005742 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005743 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5744 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5745 WARN("MASK_THEN_SHIFT",
5746 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5747 }
5748
5749# check for pointer comparisons to NULL
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005750 if ($perl_version_ok) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005751 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5752 my $val = $1;
5753 my $equal = "!";
5754 $equal = "" if ($4 eq "!=");
5755 if (CHK("COMPARISON_TO_NULL",
5756 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5757 $fix) {
5758 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5759 }
Joe Hershberger05622192011-10-18 10:06:59 +00005760 }
5761 }
5762
Tom Rini6b9709d2014-02-27 08:27:28 -05005763# check for bad placement of section $InitAttribute (e.g.: __initdata)
5764 if ($line =~ /(\b$InitAttribute\b)/) {
5765 my $attr = $1;
5766 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5767 my $ptr = $1;
5768 my $var = $2;
5769 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5770 ERROR("MISPLACED_INIT",
5771 "$attr should be placed after $var\n" . $herecurr)) ||
5772 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5773 WARN("MISPLACED_INIT",
5774 "$attr should be placed after $var\n" . $herecurr))) &&
5775 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005776 $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 Rini6b9709d2014-02-27 08:27:28 -05005777 }
5778 }
5779 }
5780
5781# check for $InitAttributeData (ie: __initdata) with const
5782 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5783 my $attr = $1;
5784 $attr =~ /($InitAttributePrefix)(.*)/;
5785 my $attr_prefix = $1;
5786 my $attr_type = $2;
5787 if (ERROR("INIT_ATTRIBUTE",
5788 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5789 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005790 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005791 s/$InitAttributeData/${attr_prefix}initconst/;
5792 }
5793 }
5794
5795# check for $InitAttributeConst (ie: __initconst) without const
5796 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5797 my $attr = $1;
5798 if (ERROR("INIT_ATTRIBUTE",
5799 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5800 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005801 my $lead = $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005802 /(^\+\s*(?:static\s+))/;
5803 $lead = rtrim($1);
5804 $lead = "$lead " if ($lead !~ /^\+$/);
5805 $lead = "${lead}const ";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005806 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5807 }
5808 }
5809
5810# check for __read_mostly with const non-pointer (should just be const)
5811 if ($line =~ /\b__read_mostly\b/ &&
5812 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5813 if (ERROR("CONST_READ_MOSTLY",
5814 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5815 $fix) {
5816 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5817 }
5818 }
5819
5820# don't use __constant_<foo> functions outside of include/uapi/
5821 if ($realfile !~ m@^include/uapi/@ &&
5822 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5823 my $constant_func = $1;
5824 my $func = $constant_func;
5825 $func =~ s/^__constant_//;
5826 if (WARN("CONSTANT_CONVERSION",
5827 "$constant_func should be $func\n" . $herecurr) &&
5828 $fix) {
5829 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Tom Rini6b9709d2014-02-27 08:27:28 -05005830 }
5831 }
5832
Joe Hershberger05622192011-10-18 10:06:59 +00005833# prefer usleep_range over udelay
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005834 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005835 my $delay = $1;
Joe Hershberger05622192011-10-18 10:06:59 +00005836 # ignore udelay's < 10, however
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005837 if (! ($delay < 10) ) {
Joe Hershberger05622192011-10-18 10:06:59 +00005838 CHK("USLEEP_RANGE",
Tom Rinic57383b2020-06-16 10:29:46 -04005839 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005840 }
5841 if ($delay > 2000) {
5842 WARN("LONG_UDELAY",
5843 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005844 }
5845 }
5846
5847# warn about unexpectedly long msleep's
5848 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5849 if ($1 < 20) {
5850 WARN("MSLEEP",
Tom Rinic57383b2020-06-16 10:29:46 -04005851 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005852 }
5853 }
5854
Tom Rini6b9709d2014-02-27 08:27:28 -05005855# check for comparisons of jiffies
5856 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5857 WARN("JIFFIES_COMPARISON",
5858 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5859 }
5860
5861# check for comparisons of get_jiffies_64()
5862 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5863 WARN("JIFFIES_COMPARISON",
5864 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5865 }
5866
Joe Hershberger05622192011-10-18 10:06:59 +00005867# warn about #ifdefs in C files
5868# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5869# print "#ifdef in C files should be avoided\n";
5870# print "$herecurr";
5871# $clean = 0;
5872# }
5873
5874# warn about spacing in #ifdefs
5875 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005876 if (ERROR("SPACING",
5877 "exactly one space required after that #$1\n" . $herecurr) &&
5878 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005879 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005880 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5881 }
5882
Joe Hershberger05622192011-10-18 10:06:59 +00005883 }
5884
5885# check for spinlock_t definitions without a comment.
5886 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5887 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5888 my $which = $1;
5889 if (!ctx_has_comment($first_line, $linenr)) {
5890 CHK("UNCOMMENTED_DEFINITION",
5891 "$1 definition without comment\n" . $herecurr);
5892 }
5893 }
5894# check for memory barriers without a comment.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005895
5896 my $barriers = qr{
5897 mb|
5898 rmb|
5899 wmb|
5900 read_barrier_depends
5901 }x;
5902 my $barrier_stems = qr{
5903 mb__before_atomic|
5904 mb__after_atomic|
5905 store_release|
5906 load_acquire|
5907 store_mb|
5908 (?:$barriers)
5909 }x;
5910 my $all_barriers = qr{
5911 (?:$barriers)|
5912 smp_(?:$barrier_stems)|
5913 virt_(?:$barrier_stems)
5914 }x;
5915
5916 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005917 if (!ctx_has_comment($first_line, $linenr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005918 WARN("MEMORY_BARRIER",
5919 "memory barrier without comment\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005920 }
5921 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005922
5923 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5924
5925 if ($realfile !~ m@^include/asm-generic/@ &&
5926 $realfile !~ m@/barrier\.h$@ &&
5927 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5928 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5929 WARN("MEMORY_BARRIER",
5930 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5931 }
5932
5933# check for waitqueue_active without a comment.
5934 if ($line =~ /\bwaitqueue_active\s*\(/) {
5935 if (!ctx_has_comment($first_line, $linenr)) {
5936 WARN("WAITQUEUE_ACTIVE",
5937 "waitqueue_active without comment\n" . $herecurr);
5938 }
5939 }
5940
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005941# check for smp_read_barrier_depends and read_barrier_depends
5942 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5943 WARN("READ_BARRIER_DEPENDS",
5944 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5945 }
5946
Joe Hershberger05622192011-10-18 10:06:59 +00005947# check of hardware specific defines
5948 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5949 CHK("ARCH_DEFINES",
5950 "architecture specific defines should be avoided\n" . $herecurr);
5951 }
5952
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005953# check that the storage class is not after a type
5954 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005955 WARN("STORAGE_CLASS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005956 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5957 }
5958# Check that the storage class is at the beginning of a declaration
5959 if ($line =~ /\b$Storage\b/ &&
5960 $line !~ /^.\s*$Storage/ &&
5961 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5962 $1 !~ /[\,\)]\s*$/) {
5963 WARN("STORAGE_CLASS",
5964 "storage class should be at the beginning of the declaration\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005965 }
5966
5967# check the location of the inline attribute, that it is between
5968# storage class and type.
5969 if ($line =~ /\b$Type\s+$Inline\b/ ||
5970 $line =~ /\b$Inline\s+$Storage\b/) {
5971 ERROR("INLINE_LOCATION",
5972 "inline keyword should sit between storage class and type\n" . $herecurr);
5973 }
5974
5975# Check for __inline__ and __inline, prefer inline
Tom Rini6b9709d2014-02-27 08:27:28 -05005976 if ($realfile !~ m@\binclude/uapi/@ &&
5977 $line =~ /\b(__inline__|__inline)\b/) {
5978 if (WARN("INLINE",
5979 "plain inline is preferred over $1\n" . $herecurr) &&
5980 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005981 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Tom Rini6b9709d2014-02-27 08:27:28 -05005982
5983 }
Joe Hershberger05622192011-10-18 10:06:59 +00005984 }
5985
5986# Check for __attribute__ packed, prefer __packed
Tom Rini6b9709d2014-02-27 08:27:28 -05005987 if ($realfile !~ m@\binclude/uapi/@ &&
5988 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005989 WARN("PREFER_PACKED",
5990 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
5991 }
5992
5993# Check for __attribute__ aligned, prefer __aligned
Tom Rini6b9709d2014-02-27 08:27:28 -05005994 if ($realfile !~ m@\binclude/uapi/@ &&
5995 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005996 WARN("PREFER_ALIGNED",
5997 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
5998 }
5999
Tom Rinic57383b2020-06-16 10:29:46 -04006000# Check for __attribute__ section, prefer __section
6001 if ($realfile !~ m@\binclude/uapi/@ &&
6002 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6003 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6004 my $new = substr($old, 1, -1);
6005 if (WARN("PREFER_SECTION",
6006 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6007 $fix) {
6008 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6009 }
6010 }
6011
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006012# Check for __attribute__ format(printf, prefer __printf
Tom Rini6b9709d2014-02-27 08:27:28 -05006013 if ($realfile !~ m@\binclude/uapi/@ &&
6014 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
6015 if (WARN("PREFER_PRINTF",
6016 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6017 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006018 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05006019
6020 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006021 }
6022
6023# Check for __attribute__ format(scanf, prefer __scanf
Tom Rini6b9709d2014-02-27 08:27:28 -05006024 if ($realfile !~ m@\binclude/uapi/@ &&
6025 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
6026 if (WARN("PREFER_SCANF",
6027 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6028 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006029 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
6030 }
6031 }
6032
6033# Check for __attribute__ weak, or __weak declarations (may have link issues)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006034 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006035 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6036 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6037 $line =~ /\b__weak\b/)) {
6038 ERROR("WEAK_DECLARATION",
6039 "Using weak declarations can have unintended link defects\n" . $herecurr);
6040 }
6041
6042# check for c99 types like uint8_t used outside of uapi/ and tools/
6043 if ($realfile !~ m@\binclude/uapi/@ &&
6044 $realfile !~ m@\btools/@ &&
6045 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6046 my $type = $1;
6047 if ($type =~ /\b($typeC99Typedefs)\b/) {
6048 $type = $1;
6049 my $kernel_type = 'u';
6050 $kernel_type = 's' if ($type =~ /^_*[si]/);
6051 $type =~ /(\d+)/;
6052 $kernel_type .= $1;
6053 if (CHK("PREFER_KERNEL_TYPES",
6054 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6055 $fix) {
6056 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6057 }
6058 }
6059 }
6060
6061# check for cast of C90 native int or longer types constants
6062 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6063 my $cast = $1;
6064 my $const = $2;
6065 if (WARN("TYPECAST_INT_CONSTANT",
6066 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6067 $fix) {
6068 my $suffix = "";
6069 my $newconst = $const;
6070 $newconst =~ s/${Int_type}$//;
6071 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6072 if ($cast =~ /\blong\s+long\b/) {
6073 $suffix .= 'LL';
6074 } elsif ($cast =~ /\blong\b/) {
6075 $suffix .= 'L';
6076 }
6077 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006078 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006079 }
6080
Joe Hershberger05622192011-10-18 10:06:59 +00006081# check for sizeof(&)
6082 if ($line =~ /\bsizeof\s*\(\s*\&/) {
6083 WARN("SIZEOF_ADDRESS",
6084 "sizeof(& should be avoided\n" . $herecurr);
6085 }
6086
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006087# check for sizeof without parenthesis
6088 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006089 if (WARN("SIZEOF_PARENTHESIS",
6090 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6091 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006092 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05006093 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006094 }
6095
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006096# check for struct spinlock declarations
6097 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6098 WARN("USE_SPINLOCK_T",
6099 "struct spinlock should be spinlock_t\n" . $herecurr);
6100 }
6101
Tom Rini6b9709d2014-02-27 08:27:28 -05006102# check for seq_printf uses that could be seq_puts
6103 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6104 my $fmt = get_quoted_string($line, $rawline);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006105 $fmt =~ s/%%//g;
6106 if ($fmt !~ /%/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006107 if (WARN("PREFER_SEQ_PUTS",
6108 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6109 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006110 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006111 }
6112 }
6113 }
6114
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006115# check for vsprintf extension %p<foo> misuses
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006116 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006117 defined $stat &&
6118 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6119 $1 !~ /^_*volatile_*$/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006120 my $stat_real;
6121
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006122 my $lc = $stat =~ tr@\n@@;
6123 $lc = $lc + $linenr;
6124 for (my $count = $linenr; $count <= $lc; $count++) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006125 my $specifier;
6126 my $extension;
Tom Rinic57383b2020-06-16 10:29:46 -04006127 my $qualifier;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006128 my $bad_specifier = "";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006129 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6130 $fmt =~ s/%%//g;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006131
Tom Rinic57383b2020-06-16 10:29:46 -04006132 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006133 $specifier = $1;
6134 $extension = $2;
Tom Rinic57383b2020-06-16 10:29:46 -04006135 $qualifier = $3;
6136 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
6137 ($extension eq "f" &&
6138 defined $qualifier && $qualifier !~ /^w/)) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006139 $bad_specifier = $specifier;
6140 last;
6141 }
6142 if ($extension eq "x" && !defined($stat_real)) {
6143 if (!defined($stat_real)) {
6144 $stat_real = get_stat_real($linenr, $lc);
6145 }
6146 WARN("VSPRINTF_SPECIFIER_PX",
6147 "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");
6148 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006149 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006150 if ($bad_specifier ne "") {
6151 my $stat_real = get_stat_real($linenr, $lc);
6152 my $ext_type = "Invalid";
6153 my $use = "";
6154 if ($bad_specifier =~ /p[Ff]/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006155 $use = " - use %pS instead";
6156 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6157 }
6158
6159 WARN("VSPRINTF_POINTER_EXTENSION",
6160 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006161 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006162 }
6163 }
6164
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006165# Check for misused memsets
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006166 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006167 defined $stat &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006168 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006169
6170 my $ms_addr = $2;
6171 my $ms_val = $7;
6172 my $ms_size = $12;
6173
6174 if ($ms_size =~ /^(0x|)0$/i) {
6175 ERROR("MEMSET",
6176 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6177 } elsif ($ms_size =~ /^(0x|)1$/i) {
6178 WARN("MEMSET",
6179 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6180 }
6181 }
6182
Tom Rini6b9709d2014-02-27 08:27:28 -05006183# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006184# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006185# defined $stat &&
6186# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6187# if (WARN("PREFER_ETHER_ADDR_COPY",
6188# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6189# $fix) {
6190# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6191# }
6192# }
6193
6194# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006195# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006196# defined $stat &&
6197# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6198# WARN("PREFER_ETHER_ADDR_EQUAL",
6199# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6200# }
6201
6202# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6203# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006204# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006205# defined $stat &&
6206# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6207#
6208# my $ms_val = $7;
6209#
6210# if ($ms_val =~ /^(?:0x|)0+$/i) {
6211# if (WARN("PREFER_ETH_ZERO_ADDR",
6212# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6213# $fix) {
6214# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6215# }
6216# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6217# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6218# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6219# $fix) {
6220# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6221# }
6222# }
6223# }
Tom Rini6b9709d2014-02-27 08:27:28 -05006224
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006225# typecasts on min/max could be min_t/max_t
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006226 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006227 defined $stat &&
6228 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6229 if (defined $2 || defined $7) {
6230 my $call = $1;
6231 my $cast1 = deparenthesize($2);
6232 my $arg1 = $3;
6233 my $cast2 = deparenthesize($7);
6234 my $arg2 = $8;
6235 my $cast;
6236
6237 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6238 $cast = "$cast1 or $cast2";
6239 } elsif ($cast1 ne "") {
6240 $cast = $cast1;
6241 } else {
6242 $cast = $cast2;
6243 }
6244 WARN("MINMAX",
6245 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6246 }
6247 }
6248
6249# check usleep_range arguments
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006250 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006251 defined $stat &&
6252 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6253 my $min = $1;
6254 my $max = $7;
6255 if ($min eq $max) {
6256 WARN("USLEEP_RANGE",
Tom Rinic57383b2020-06-16 10:29:46 -04006257 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006258 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6259 $min > $max) {
6260 WARN("USLEEP_RANGE",
Tom Rinic57383b2020-06-16 10:29:46 -04006261 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006262 }
6263 }
6264
Tom Rini6b9709d2014-02-27 08:27:28 -05006265# check for naked sscanf
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006266 if ($perl_version_ok &&
Tom Rini6b9709d2014-02-27 08:27:28 -05006267 defined $stat &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006268 $line =~ /\bsscanf\b/ &&
Tom Rini6b9709d2014-02-27 08:27:28 -05006269 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6270 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6271 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6272 my $lc = $stat =~ tr@\n@@;
6273 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006274 my $stat_real = get_stat_real($linenr, $lc);
Tom Rini6b9709d2014-02-27 08:27:28 -05006275 WARN("NAKED_SSCANF",
6276 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6277 }
6278
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006279# check for simple sscanf that should be kstrto<foo>
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006280 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006281 defined $stat &&
6282 $line =~ /\bsscanf\b/) {
6283 my $lc = $stat =~ tr@\n@@;
6284 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006285 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006286 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6287 my $format = $6;
6288 my $count = $format =~ tr@%@%@;
6289 if ($count == 1 &&
6290 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6291 WARN("SSCANF_TO_KSTRTO",
6292 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6293 }
6294 }
6295 }
6296
Tom Rini6b9709d2014-02-27 08:27:28 -05006297# check for new externs in .h files.
6298 if ($realfile =~ /\.h$/ &&
6299 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6300 if (CHK("AVOID_EXTERNS",
6301 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6302 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006303 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006304 }
6305 }
6306
Joe Hershberger05622192011-10-18 10:06:59 +00006307# check for new externs in .c files.
6308 if ($realfile =~ /\.c$/ && defined $stat &&
6309 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6310 {
6311 my $function_name = $1;
6312 my $paren_space = $2;
6313
6314 my $s = $stat;
6315 if (defined $cond) {
6316 substr($s, 0, length($cond), '');
6317 }
6318 if ($s =~ /^\s*;/ &&
6319 $function_name ne 'uninitialized_var')
6320 {
6321 WARN("AVOID_EXTERNS",
6322 "externs should be avoided in .c files\n" . $herecurr);
6323 }
6324
6325 if ($paren_space =~ /\n/) {
6326 WARN("FUNCTION_ARGUMENTS",
6327 "arguments for function declarations should follow identifier\n" . $herecurr);
6328 }
6329
6330 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6331 $stat =~ /^.\s*extern\s+/)
6332 {
6333 WARN("AVOID_EXTERNS",
6334 "externs should be avoided in .c files\n" . $herecurr);
6335 }
6336
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006337# check for function declarations that have arguments without identifier names
Tom Rinic57383b2020-06-16 10:29:46 -04006338# while avoiding uninitialized_var(x)
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006339 if (defined $stat &&
Tom Rinic57383b2020-06-16 10:29:46 -04006340 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6341 (!defined($1) ||
6342 (defined($1) && $1 ne "uninitialized_var")) &&
6343 $2 ne "void") {
6344 my $args = trim($2);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006345 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6346 my $arg = trim($1);
Tom Rinic57383b2020-06-16 10:29:46 -04006347 if ($arg =~ /^$Type$/ &&
6348 $arg !~ /enum\s+$Ident$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006349 WARN("FUNCTION_ARGUMENTS",
6350 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6351 }
6352 }
6353 }
6354
6355# check for function definitions
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006356 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006357 defined $stat &&
6358 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6359 $context_function = $1;
6360
6361# check for multiline function definition with misplaced open brace
6362 my $ok = 0;
6363 my $cnt = statement_rawlines($stat);
6364 my $herectx = $here . "\n";
6365 for (my $n = 0; $n < $cnt; $n++) {
6366 my $rl = raw_line($linenr, $n);
6367 $herectx .= $rl . "\n";
6368 $ok = 1 if ($rl =~ /^[ \+]\{/);
6369 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6370 last if $rl =~ /^[ \+].*\{/;
6371 }
6372 if (!$ok) {
6373 ERROR("OPEN_BRACE",
6374 "open brace '{' following function definitions go on the next line\n" . $herectx);
6375 }
6376 }
6377
Joe Hershberger05622192011-10-18 10:06:59 +00006378# checks for new __setup's
6379 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6380 my $name = $1;
6381
6382 if (!grep(/$name/, @setup_docs)) {
6383 CHK("UNDOCUMENTED_SETUP",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006384 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006385 }
6386 }
6387
Tom Rinic57383b2020-06-16 10:29:46 -04006388# check for pointless casting of alloc functions
6389 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006390 WARN("UNNECESSARY_CASTS",
6391 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6392 }
6393
Tom Rini6b9709d2014-02-27 08:27:28 -05006394# alloc style
6395# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006396 if ($perl_version_ok &&
Tom Rinic57383b2020-06-16 10:29:46 -04006397 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006398 CHK("ALLOC_SIZEOF_STRUCT",
6399 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6400 }
6401
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006402# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006403 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006404 defined $stat &&
6405 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
6406 my $oldfunc = $3;
6407 my $a1 = $4;
6408 my $a2 = $10;
6409 my $newfunc = "kmalloc_array";
6410 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
6411 my $r1 = $a1;
6412 my $r2 = $a2;
6413 if ($a1 =~ /^sizeof\s*\S/) {
6414 $r1 = $a2;
6415 $r2 = $a1;
6416 }
6417 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6418 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006419 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006420 my $herectx = get_stat_here($linenr, $cnt, $here);
6421
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006422 if (WARN("ALLOC_WITH_MULTIPLY",
6423 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6424 $cnt == 1 &&
6425 $fix) {
6426 $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;
6427 }
6428 }
6429 }
6430
Tom Rini6b9709d2014-02-27 08:27:28 -05006431# check for krealloc arg reuse
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006432 if ($perl_version_ok &&
6433 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6434 $1 eq $3) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006435 WARN("KREALLOC_ARG_REUSE",
6436 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6437 }
6438
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006439# check for alloc argument mismatch
6440 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6441 WARN("ALLOC_ARRAY_ARGS",
6442 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006443 }
6444
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006445# check for multiple semicolons
6446 if ($line =~ /;\s*;\s*$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006447 if (WARN("ONE_SEMICOLON",
6448 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6449 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006450 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6451 }
6452 }
6453
6454# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6455 if ($realfile !~ m@^include/uapi/@ &&
6456 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
6457 my $ull = "";
6458 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6459 if (CHK("BIT_MACRO",
6460 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6461 $fix) {
6462 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6463 }
6464 }
6465
6466# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6467 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6468 my $config = $1;
6469 if (WARN("PREFER_IS_ENABLED",
6470 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6471 $fix) {
6472 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
Tom Rini6b9709d2014-02-27 08:27:28 -05006473 }
6474 }
6475
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -04006476# check for case / default statements not preceded by break/fallthrough/switch
Tom Rini6b9709d2014-02-27 08:27:28 -05006477 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6478 my $has_break = 0;
6479 my $has_statement = 0;
6480 my $count = 0;
6481 my $prevline = $linenr;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006482 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006483 $prevline--;
6484 my $rline = $rawlines[$prevline - 1];
6485 my $fline = $lines[$prevline - 1];
6486 last if ($fline =~ /^\@\@/);
6487 next if ($fline =~ /^\-/);
6488 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6489 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6490 next if ($fline =~ /^.[\s$;]*$/);
6491 $has_statement = 1;
6492 $count++;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006493 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
Tom Rini6b9709d2014-02-27 08:27:28 -05006494 }
6495 if (!$has_break && $has_statement) {
6496 WARN("MISSING_BREAK",
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -04006497 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Tom Rini6b9709d2014-02-27 08:27:28 -05006498 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006499 }
6500
Tom Rinic57383b2020-06-16 10:29:46 -04006501# check for /* fallthrough */ like comment, prefer fallthrough;
6502 my @fallthroughs = (
6503 'fallthrough',
6504 '@fallthrough@',
6505 'lint -fallthrough[ \t]*',
6506 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6507 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6508 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6509 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6510 );
6511 if ($raw_comment ne '') {
6512 foreach my $ft (@fallthroughs) {
6513 if ($raw_comment =~ /$ft/) {
6514 my $msg_level = \&WARN;
6515 $msg_level = \&CHK if ($file);
6516 &{$msg_level}("PREFER_FALLTHROUGH",
6517 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6518 last;
6519 }
6520 }
6521 }
6522
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006523# check for switch/default statements without a break;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006524 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006525 defined $stat &&
6526 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006527 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006528 my $herectx = get_stat_here($linenr, $cnt, $here);
6529
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006530 WARN("DEFAULT_NO_BREAK",
6531 "switch default: should use break\n" . $herectx);
Eric Nelson172a3a82012-05-02 20:32:18 -07006532 }
6533
Joe Hershberger05622192011-10-18 10:06:59 +00006534# check for gcc specific __FUNCTION__
Tom Rini6b9709d2014-02-27 08:27:28 -05006535 if ($line =~ /\b__FUNCTION__\b/) {
6536 if (WARN("USE_FUNC",
6537 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6538 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006539 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Tom Rini6b9709d2014-02-27 08:27:28 -05006540 }
Joe Hershberger05622192011-10-18 10:06:59 +00006541 }
6542
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006543# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6544 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6545 ERROR("DATE_TIME",
6546 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6547 }
6548
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006549# check for use of yield()
6550 if ($line =~ /\byield\s*\(\s*\)/) {
6551 WARN("YIELD",
6552 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6553 }
6554
Tom Rini6b9709d2014-02-27 08:27:28 -05006555# check for comparisons against true and false
6556 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6557 my $lead = $1;
6558 my $arg = $2;
6559 my $test = $3;
6560 my $otype = $4;
6561 my $trail = $5;
6562 my $op = "!";
6563
6564 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6565
6566 my $type = lc($otype);
6567 if ($type =~ /^(?:true|false)$/) {
6568 if (("$test" eq "==" && "$type" eq "true") ||
6569 ("$test" eq "!=" && "$type" eq "false")) {
6570 $op = "";
6571 }
6572
6573 CHK("BOOL_COMPARISON",
6574 "Using comparison to $otype is error prone\n" . $herecurr);
6575
6576## maybe suggesting a correct construct would better
6577## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6578
6579 }
6580 }
6581
Joe Hershberger05622192011-10-18 10:06:59 +00006582# check for semaphores initialized locked
6583 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6584 WARN("CONSIDER_COMPLETION",
6585 "consider using a completion\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006586 }
Joe Hershberger05622192011-10-18 10:06:59 +00006587
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006588# recommend kstrto* over simple_strto* and strict_strto*
6589 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006590 WARN("CONSIDER_KSTRTO",
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006591 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006592 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006593
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006594# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Joe Hershberger05622192011-10-18 10:06:59 +00006595 if ($line =~ /^.\s*__initcall\s*\(/) {
6596 WARN("USE_DEVICE_INITCALL",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006597 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006598 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006599
Tom Rinic57383b2020-06-16 10:29:46 -04006600# check for spin_is_locked(), suggest lockdep instead
6601 if ($line =~ /\bspin_is_locked\(/) {
6602 WARN("USE_LOCKDEP",
6603 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6604 }
6605
6606# check for deprecated apis
6607 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6608 my $deprecated_api = $1;
6609 my $new_api = $deprecated_apis{$deprecated_api};
6610 WARN("DEPRECATED_API",
6611 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6612 }
6613
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006614# check for various structs that are normally const (ops, kgdb, device_tree)
6615# and avoid what seem like struct definitions 'struct foo {'
Joe Hershberger05622192011-10-18 10:06:59 +00006616 if ($line !~ /\bconst\b/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006617 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006618 WARN("CONST_STRUCT",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006619 "struct $1 should normally be const\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006620 }
6621
6622# use of NR_CPUS is usually wrong
6623# ignore definitions of NR_CPUS and usage to define arrays as likely right
6624 if ($line =~ /\bNR_CPUS\b/ &&
6625 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6626 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6627 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6628 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6629 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6630 {
6631 WARN("NR_CPUS",
6632 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6633 }
6634
Tom Rini6b9709d2014-02-27 08:27:28 -05006635# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6636 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6637 ERROR("DEFINE_ARCH_HAS",
6638 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6639 }
6640
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006641# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006642 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006643 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6644 WARN("LIKELY_MISUSE",
6645 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006646 }
6647
Tom Rinic57383b2020-06-16 10:29:46 -04006648# nested likely/unlikely calls
6649 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6650 WARN("LIKELY_MISUSE",
6651 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6652 }
6653
Joe Hershberger05622192011-10-18 10:06:59 +00006654# whine mightly about in_atomic
6655 if ($line =~ /\bin_atomic\s*\(/) {
6656 if ($realfile =~ m@^drivers/@) {
6657 ERROR("IN_ATOMIC",
6658 "do not use in_atomic in drivers\n" . $herecurr);
6659 } elsif ($realfile !~ m@^kernel/@) {
6660 WARN("IN_ATOMIC",
6661 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6662 }
6663 }
6664
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006665# check for mutex_trylock_recursive usage
6666 if ($line =~ /mutex_trylock_recursive/) {
6667 ERROR("LOCKING",
6668 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6669 }
6670
Joe Hershberger05622192011-10-18 10:06:59 +00006671# check for lockdep_set_novalidate_class
6672 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6673 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6674 if ($realfile !~ m@^kernel/lockdep@ &&
6675 $realfile !~ m@^include/linux/lockdep@ &&
6676 $realfile !~ m@^drivers/base/core@) {
6677 ERROR("LOCKDEP",
6678 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6679 }
6680 }
6681
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006682 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6683 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006684 WARN("EXPORTED_WORLD_WRITABLE",
6685 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6686 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006687
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006688# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6689# and whether or not function naming is typical and if
6690# DEVICE_ATTR permissions uses are unusual too
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006691 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006692 defined $stat &&
6693 $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*\)/) {
6694 my $var = $1;
6695 my $perms = $2;
6696 my $show = $3;
6697 my $store = $4;
6698 my $octal_perms = perms_to_octal($perms);
6699 if ($show =~ /^${var}_show$/ &&
6700 $store =~ /^${var}_store$/ &&
6701 $octal_perms eq "0644") {
6702 if (WARN("DEVICE_ATTR_RW",
6703 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6704 $fix) {
6705 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6706 }
6707 } elsif ($show =~ /^${var}_show$/ &&
6708 $store =~ /^NULL$/ &&
6709 $octal_perms eq "0444") {
6710 if (WARN("DEVICE_ATTR_RO",
6711 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6712 $fix) {
6713 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6714 }
6715 } elsif ($show =~ /^NULL$/ &&
6716 $store =~ /^${var}_store$/ &&
6717 $octal_perms eq "0200") {
6718 if (WARN("DEVICE_ATTR_WO",
6719 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6720 $fix) {
6721 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6722 }
6723 } elsif ($octal_perms eq "0644" ||
6724 $octal_perms eq "0444" ||
6725 $octal_perms eq "0200") {
6726 my $newshow = "$show";
6727 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6728 my $newstore = $store;
6729 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6730 my $rename = "";
6731 if ($show ne $newshow) {
6732 $rename .= " '$show' to '$newshow'";
6733 }
6734 if ($store ne $newstore) {
6735 $rename .= " '$store' to '$newstore'";
6736 }
6737 WARN("DEVICE_ATTR_FUNCTIONS",
6738 "Consider renaming function(s)$rename\n" . $herecurr);
6739 } else {
6740 WARN("DEVICE_ATTR_PERMS",
6741 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6742 }
6743 }
6744
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006745# Mode permission misuses where it seems decimal should be octal
6746# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006747# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6748# specific definition of not visible in sysfs.
6749# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6750# use the default permissions
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006751 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006752 defined $stat &&
6753 $line =~ /$mode_perms_search/) {
6754 foreach my $entry (@mode_permission_funcs) {
6755 my $func = $entry->[0];
6756 my $arg_pos = $entry->[1];
6757
6758 my $lc = $stat =~ tr@\n@@;
6759 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006760 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006761
6762 my $skip_args = "";
6763 if ($arg_pos > 1) {
6764 $arg_pos--;
6765 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6766 }
6767 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6768 if ($stat =~ /$test/) {
6769 my $val = $1;
6770 $val = $6 if ($skip_args ne "");
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006771 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6772 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6773 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006774 ERROR("NON_OCTAL_PERMISSIONS",
6775 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6776 }
6777 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6778 ERROR("EXPORTED_WORLD_WRITABLE",
6779 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6780 }
6781 }
6782 }
6783 }
6784
6785# check for uses of S_<PERMS> that could be octal for readability
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006786 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
6787 my $oval = $1;
6788 my $octal = perms_to_octal($oval);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006789 if (WARN("SYMBOLIC_PERMS",
6790 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6791 $fix) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006792 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006793 }
6794 }
6795
6796# validate content of MODULE_LICENSE against list from include/linux/module.h
6797 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6798 my $extracted_string = get_quoted_string($line, $rawline);
6799 my $valid_licenses = qr{
6800 GPL|
6801 GPL\ v2|
6802 GPL\ and\ additional\ rights|
6803 Dual\ BSD/GPL|
6804 Dual\ MIT/GPL|
6805 Dual\ MPL/GPL|
6806 Proprietary
6807 }x;
6808 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6809 WARN("MODULE_LICENSE",
6810 "unknown module license " . $extracted_string . "\n" . $herecurr);
6811 }
6812 }
Tom Rinic57383b2020-06-16 10:29:46 -04006813
6814# check for sysctl duplicate constants
6815 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6816 WARN("DUPLICATED_SYSCTL_CONST",
6817 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6818 }
Joe Hershberger05622192011-10-18 10:06:59 +00006819 }
6820
6821 # If we have no input at all, then there is nothing to report on
6822 # so just keep quiet.
6823 if ($#rawlines == -1) {
6824 exit(0);
6825 }
6826
6827 # In mailback mode only produce a report in the negative, for
6828 # things that appear to be patches.
6829 if ($mailback && ($clean == 1 || !$is_patch)) {
6830 exit(0);
6831 }
6832
6833 # This is not a patch, and we are are in 'no-patch' mode so
6834 # just keep quiet.
6835 if (!$chk_patch && !$is_patch) {
6836 exit(0);
6837 }
6838
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006839 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006840 ERROR("NOT_UNIFIED_DIFF",
6841 "Does not appear to be a unified-diff format patch\n");
6842 }
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006843 if ($is_patch && $has_commit_log && $chk_signoff) {
6844 if ($signoff == 0) {
6845 ERROR("MISSING_SIGN_OFF",
6846 "Missing Signed-off-by: line(s)\n");
6847 } elsif (!$authorsignoff) {
6848 WARN("NO_AUTHOR_SIGN_OFF",
6849 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6850 }
Joe Hershberger05622192011-10-18 10:06:59 +00006851 }
6852
6853 print report_dump();
6854 if ($summary && !($clean == 1 && $quiet == 1)) {
6855 print "$filename " if ($summary_file);
6856 print "total: $cnt_error errors, $cnt_warn warnings, " .
6857 (($check)? "$cnt_chk checks, " : "") .
6858 "$cnt_lines lines checked\n";
Joe Hershberger05622192011-10-18 10:06:59 +00006859 }
6860
6861 if ($quiet == 0) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006862 # If there were any defects found and not already fixing them
6863 if (!$clean and !$fix) {
6864 print << "EOM"
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006865
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006866NOTE: For some of the reported defects, checkpatch may be able to
6867 mechanically convert to the typical style using --fix or --fix-inplace.
6868EOM
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006869 }
Joe Hershberger05622192011-10-18 10:06:59 +00006870 # If there were whitespace errors which cleanpatch can fix
6871 # then suggest that.
6872 if ($rpt_cleaners) {
Joe Hershberger05622192011-10-18 10:06:59 +00006873 $rpt_cleaners = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006874 print << "EOM"
6875
6876NOTE: Whitespace errors detected.
6877 You may wish to use scripts/cleanpatch or scripts/cleanfile
6878EOM
Joe Hershberger05622192011-10-18 10:06:59 +00006879 }
6880 }
6881
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006882 if ($clean == 0 && $fix &&
6883 ("@rawlines" ne "@fixed" ||
6884 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006885 my $newfile = $filename;
6886 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6887 my $linecount = 0;
6888 my $f;
6889
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006890 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6891
Tom Rini6b9709d2014-02-27 08:27:28 -05006892 open($f, '>', $newfile)
6893 or die "$P: Can't open $newfile for write\n";
6894 foreach my $fixed_line (@fixed) {
6895 $linecount++;
6896 if ($file) {
6897 if ($linecount > 3) {
6898 $fixed_line =~ s/^\+//;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006899 print $f $fixed_line . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05006900 }
6901 } else {
6902 print $f $fixed_line . "\n";
6903 }
6904 }
6905 close($f);
6906
6907 if (!$quiet) {
6908 print << "EOM";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006909
Tom Rini6b9709d2014-02-27 08:27:28 -05006910Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6911
6912Do _NOT_ trust the results written to this file.
6913Do _NOT_ submit these changes without inspecting them for correctness.
6914
6915This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6916No warranties, expressed or implied...
Tom Rini6b9709d2014-02-27 08:27:28 -05006917EOM
6918 }
Joe Hershberger05622192011-10-18 10:06:59 +00006919 }
6920
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006921 if ($quiet == 0) {
6922 print "\n";
6923 if ($clean == 1) {
6924 print "$vname has no obvious style problems and is ready for submission.\n";
6925 } else {
6926 print "$vname has style problems, please review.\n";
6927 }
Joe Hershberger05622192011-10-18 10:06:59 +00006928 }
Joe Hershberger05622192011-10-18 10:06:59 +00006929 return $clean;
6930}