Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4 | # (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 Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 8 | # (c) 2010-2018 Joe Perches <joe@perches.com> |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 9 | |
| 10 | use strict; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 11 | use warnings; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 12 | use POSIX; |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 13 | use File::Basename; |
| 14 | use Cwd 'abs_path'; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 15 | use Term::ANSIColor qw(:constants); |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 16 | use Encode qw(decode encode); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 17 | |
| 18 | my $P = $0; |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 19 | my $D = dirname(abs_path($P)); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 20 | |
| 21 | my $V = '0.32'; |
| 22 | |
| 23 | use Getopt::Long qw(:config no_auto_abbrev); |
| 24 | |
| 25 | my $quiet = 0; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 26 | my $verbose = 0; |
| 27 | my %verbose_messages = (); |
| 28 | my %verbose_emitted = (); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 29 | my $tree = 1; |
| 30 | my $chk_signoff = 1; |
| 31 | my $chk_patch = 1; |
| 32 | my $tst_only; |
| 33 | my $emacs = 0; |
| 34 | my $terse = 0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 35 | my $showfile = 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 36 | my $file = 0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 37 | my $git = 0; |
| 38 | my %git_commits = (); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 39 | my $check = 0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 40 | my $check_orig = 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 41 | my $summary = 1; |
| 42 | my $mailback = 0; |
| 43 | my $summary_file = 0; |
| 44 | my $show_types = 0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 45 | my $list_types = 0; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 46 | my $fix = 0; |
| 47 | my $fix_inplace = 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 48 | my $root; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 49 | my $gitroot = $ENV{'GIT_DIR'}; |
| 50 | $gitroot = ".git" if !defined($gitroot); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 51 | my %debug; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 52 | my %camelcase = (); |
| 53 | my %use_type = (); |
| 54 | my @use = (); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 55 | my %ignore_type = (); |
| 56 | my @ignore = (); |
| 57 | my $help = 0; |
| 58 | my $configuration_file = ".checkpatch.conf"; |
Simon Glass | 048a648 | 2020-05-22 16:32:35 -0600 | [diff] [blame] | 59 | my $max_line_length = 100; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 60 | my $ignore_perl_version = 0; |
| 61 | my $minimum_perl_version = 5.10.0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 62 | my $min_conf_desc_length = 4; |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 63 | my $spelling_file = "$D/spelling.txt"; |
| 64 | my $codespell = 0; |
| 65 | my $codespellfile = "/usr/share/codespell/dictionary.txt"; |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 66 | my $user_codespellfile = ""; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 67 | my $conststructsfile = "$D/const_structs.checkpatch"; |
Simon Glass | b77df59 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 68 | my $u_boot = 0; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 69 | my $docsfile = "$D/../doc/develop/checkpatch.rst"; |
| 70 | my $typedefsfile; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 71 | my $color = "auto"; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 72 | my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE |
| 73 | # git output parsing needs US English output, so first set backtick child process LANGUAGE |
| 74 | my $git_command ='export LANGUAGE=en_US.UTF-8; git'; |
| 75 | my $tabsize = 8; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 76 | my ${CONFIG_} = "CONFIG_"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 77 | |
| 78 | sub help { |
| 79 | my ($exitcode) = @_; |
| 80 | |
| 81 | print << "EOM"; |
| 82 | Usage: $P [OPTION]... [FILE]... |
| 83 | Version: $V |
| 84 | |
| 85 | Options: |
| 86 | -q, --quiet quiet |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 87 | -v, --verbose verbose mode |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 88 | --no-tree run without a kernel tree |
| 89 | --no-signoff do not check for 'Signed-off-by' line |
| 90 | --patch treat FILE as patchfile (default) |
| 91 | --emacs emacs compile window format |
| 92 | --terse one line per report |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 93 | --showfile emit diffed file position, not input file position |
| 94 | -g, --git treat FILE as a single commit or git revision range |
| 95 | single git commit with: |
| 96 | <rev> |
| 97 | <rev>^ |
| 98 | <rev>~n |
| 99 | multiple git commits with: |
| 100 | <rev1>..<rev2> |
| 101 | <rev1>...<rev2> |
| 102 | <rev>-<count> |
| 103 | git merges are ignored |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 104 | -f, --file treat FILE as regular source file |
| 105 | --subjective, --strict enable more subjective tests |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 106 | --list-types list the possible message types |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 107 | --types TYPE(,TYPE2...) show only these comma separated message types |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 108 | --ignore TYPE(,TYPE2...) ignore various comma separated message types |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 109 | --show-types show the specific message type in the output |
Simon Glass | 048a648 | 2020-05-22 16:32:35 -0600 | [diff] [blame] | 110 | --max-line-length=n set the maximum line length, (default $max_line_length) |
| 111 | if exceeded, warn on patches |
| 112 | requires --strict for use with --file |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 113 | --min-conf-desc-length=n set the min description length, if shorter, warn |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 114 | --tab-size=n set the number of spaces for tab (default $tabsize) |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 115 | --root=PATH PATH to the kernel tree root |
| 116 | --no-summary suppress the per-file summary |
| 117 | --mailback only produce a report in case of warnings/errors |
| 118 | --summary-file include the filename in summary |
| 119 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 120 | 'values', 'possible', 'type', and 'attr' (default |
| 121 | is all off) |
| 122 | --test-only=WORD report only warnings/errors containing WORD |
| 123 | literally |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 124 | --fix EXPERIMENTAL - may create horrible results |
| 125 | If correctable single-line errors exist, create |
| 126 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" |
| 127 | with potential errors corrected to the preferred |
| 128 | checkpatch style |
| 129 | --fix-inplace EXPERIMENTAL - may create horrible results |
| 130 | Is the same as --fix, but overwrites the input |
| 131 | file. It's your fault if there's no backup or git |
| 132 | --ignore-perl-version override checking of perl version. expect |
| 133 | runtime errors. |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 134 | --codespell Use the codespell dictionary for spelling/typos |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 135 | (default:$codespellfile) |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 136 | --codespellfile Use this codespell dictionary |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 137 | --typedefsfile Read additional types from this file |
| 138 | --color[=WHEN] Use colors 'always', 'never', or only when output |
| 139 | is a terminal ('auto'). Default is 'auto'. |
Simon Glass | b77df59 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 140 | --u-boot Run additional checks for U-Boot |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 141 | --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default |
| 142 | ${CONFIG_}) |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 143 | -h, --help, --version display this help and exit |
| 144 | |
| 145 | When FILE is - read standard input. |
| 146 | EOM |
| 147 | |
| 148 | exit($exitcode); |
| 149 | } |
| 150 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 151 | sub uniq { |
| 152 | my %seen; |
| 153 | return grep { !$seen{$_}++ } @_; |
| 154 | } |
| 155 | |
| 156 | sub list_types { |
| 157 | my ($exitcode) = @_; |
| 158 | |
| 159 | my $count = 0; |
| 160 | |
| 161 | local $/ = undef; |
| 162 | |
| 163 | open(my $script, '<', abs_path($P)) or |
| 164 | die "$P: Can't read '$P' $!\n"; |
| 165 | |
| 166 | my $text = <$script>; |
| 167 | close($script); |
| 168 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 169 | my %types = (); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 170 | # Also catch when type or level is passed through a variable |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 171 | while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { |
| 172 | if (defined($1)) { |
| 173 | if (exists($types{$2})) { |
| 174 | $types{$2} .= ",$1" if ($types{$2} ne $1); |
| 175 | } else { |
| 176 | $types{$2} = $1; |
| 177 | } |
| 178 | } else { |
| 179 | $types{$2} = "UNDETERMINED"; |
| 180 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 181 | } |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 182 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 183 | print("#\tMessage type\n\n"); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 184 | if ($color) { |
| 185 | print(" ( Color coding: "); |
| 186 | print(RED . "ERROR" . RESET); |
| 187 | print(" | "); |
| 188 | print(YELLOW . "WARNING" . RESET); |
| 189 | print(" | "); |
| 190 | print(GREEN . "CHECK" . RESET); |
| 191 | print(" | "); |
| 192 | print("Multiple levels / Undetermined"); |
| 193 | print(" )\n\n"); |
| 194 | } |
| 195 | |
| 196 | foreach my $type (sort keys %types) { |
| 197 | my $orig_type = $type; |
| 198 | if ($color) { |
| 199 | my $level = $types{$type}; |
| 200 | if ($level eq "ERROR") { |
| 201 | $type = RED . $type . RESET; |
| 202 | } elsif ($level eq "WARN") { |
| 203 | $type = YELLOW . $type . RESET; |
| 204 | } elsif ($level eq "CHK") { |
| 205 | $type = GREEN . $type . RESET; |
| 206 | } |
| 207 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 208 | print(++$count . "\t" . $type . "\n"); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 209 | if ($verbose && exists($verbose_messages{$orig_type})) { |
| 210 | my $message = $verbose_messages{$orig_type}; |
| 211 | $message =~ s/\n/\n\t/g; |
| 212 | print("\t" . $message . "\n\n"); |
| 213 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | exit($exitcode); |
| 217 | } |
| 218 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 219 | my $conf = which_conf($configuration_file); |
| 220 | if (-f $conf) { |
| 221 | my @conf_args; |
| 222 | open(my $conffile, '<', "$conf") |
| 223 | or warn "$P: Can't find a readable $configuration_file file $!\n"; |
| 224 | |
| 225 | while (<$conffile>) { |
| 226 | my $line = $_; |
| 227 | |
| 228 | $line =~ s/\s*\n?$//g; |
| 229 | $line =~ s/^\s*//g; |
| 230 | $line =~ s/\s+/ /g; |
| 231 | |
| 232 | next if ($line =~ m/^\s*#/); |
| 233 | next if ($line =~ m/^\s*$/); |
| 234 | |
| 235 | my @words = split(" ", $line); |
| 236 | foreach my $word (@words) { |
| 237 | last if ($word =~ m/^#/); |
| 238 | push (@conf_args, $word); |
| 239 | } |
| 240 | } |
| 241 | close($conffile); |
| 242 | unshift(@ARGV, @conf_args) if @conf_args; |
| 243 | } |
| 244 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 245 | sub load_docs { |
| 246 | open(my $docs, '<', "$docsfile") |
| 247 | or warn "$P: Can't read the documentation file $docsfile $!\n"; |
| 248 | |
| 249 | my $type = ''; |
| 250 | my $desc = ''; |
| 251 | my $in_desc = 0; |
| 252 | |
| 253 | while (<$docs>) { |
| 254 | chomp; |
| 255 | my $line = $_; |
| 256 | $line =~ s/\s+$//; |
| 257 | |
| 258 | if ($line =~ /^\s*\*\*(.+)\*\*$/) { |
| 259 | if ($desc ne '') { |
| 260 | $verbose_messages{$type} = trim($desc); |
| 261 | } |
| 262 | $type = $1; |
| 263 | $desc = ''; |
| 264 | $in_desc = 1; |
| 265 | } elsif ($in_desc) { |
| 266 | if ($line =~ /^(?:\s{4,}|$)/) { |
| 267 | $line =~ s/^\s{4}//; |
| 268 | $desc .= $line; |
| 269 | $desc .= "\n"; |
| 270 | } else { |
| 271 | $verbose_messages{$type} = trim($desc); |
| 272 | $type = ''; |
| 273 | $desc = ''; |
| 274 | $in_desc = 0; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if ($desc ne '') { |
| 280 | $verbose_messages{$type} = trim($desc); |
| 281 | } |
| 282 | close($docs); |
| 283 | } |
| 284 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 285 | # Perl's Getopt::Long allows options to take optional arguments after a space. |
| 286 | # Prevent --color by itself from consuming other arguments |
| 287 | foreach (@ARGV) { |
| 288 | if ($_ eq "--color" || $_ eq "-color") { |
| 289 | $_ = "--color=$color"; |
| 290 | } |
| 291 | } |
| 292 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 293 | GetOptions( |
| 294 | 'q|quiet+' => \$quiet, |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 295 | 'v|verbose!' => \$verbose, |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 296 | 'tree!' => \$tree, |
| 297 | 'signoff!' => \$chk_signoff, |
| 298 | 'patch!' => \$chk_patch, |
| 299 | 'emacs!' => \$emacs, |
| 300 | 'terse!' => \$terse, |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 301 | 'showfile!' => \$showfile, |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 302 | 'f|file!' => \$file, |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 303 | 'g|git!' => \$git, |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 304 | 'subjective!' => \$check, |
| 305 | 'strict!' => \$check, |
| 306 | 'ignore=s' => \@ignore, |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 307 | 'types=s' => \@use, |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 308 | 'show-types!' => \$show_types, |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 309 | 'list-types!' => \$list_types, |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 310 | 'max-line-length=i' => \$max_line_length, |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 311 | 'min-conf-desc-length=i' => \$min_conf_desc_length, |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 312 | 'tab-size=i' => \$tabsize, |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 313 | 'root=s' => \$root, |
| 314 | 'summary!' => \$summary, |
| 315 | 'mailback!' => \$mailback, |
| 316 | 'summary-file!' => \$summary_file, |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 317 | 'fix!' => \$fix, |
| 318 | 'fix-inplace!' => \$fix_inplace, |
| 319 | 'ignore-perl-version!' => \$ignore_perl_version, |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 320 | 'debug=s' => \%debug, |
| 321 | 'test-only=s' => \$tst_only, |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 322 | 'codespell!' => \$codespell, |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 323 | 'codespellfile=s' => \$user_codespellfile, |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 324 | 'typedefsfile=s' => \$typedefsfile, |
Simon Glass | b77df59 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 325 | 'u-boot' => \$u_boot, |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 326 | 'color=s' => \$color, |
| 327 | 'no-color' => \$color, #keep old behaviors of -nocolor |
| 328 | 'nocolor' => \$color, #keep old behaviors of -nocolor |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 329 | 'kconfig-prefix=s' => \${CONFIG_}, |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 330 | 'h|help' => \$help, |
| 331 | 'version' => \$help |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 332 | ) or $help = 2; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 333 | |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 334 | if ($user_codespellfile) { |
| 335 | # Use the user provided codespell file unconditionally |
| 336 | $codespellfile = $user_codespellfile; |
| 337 | } elsif (!(-f $codespellfile)) { |
| 338 | # If /usr/share/codespell/dictionary.txt is not present, try to find it |
| 339 | # under codespell's install directory: <codespell_root>/data/dictionary.txt |
| 340 | if (($codespell || $help) && which("codespell") ne "" && which("python") ne "") { |
| 341 | my $python_codespell_dict = << "EOF"; |
| 342 | |
| 343 | import os.path as op |
| 344 | import codespell_lib |
| 345 | codespell_dir = op.dirname(codespell_lib.__file__) |
| 346 | codespell_file = op.join(codespell_dir, 'data', 'dictionary.txt') |
| 347 | print(codespell_file, end='') |
| 348 | EOF |
| 349 | |
| 350 | my $codespell_dict = `python -c "$python_codespell_dict" 2> /dev/null`; |
| 351 | $codespellfile = $codespell_dict if (-f $codespell_dict); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | # $help is 1 if either -h, --help or --version is passed as option - exitcode: 0 |
| 356 | # $help is 2 if invalid option is passed - exitcode: 1 |
| 357 | help($help - 1) if ($help); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 358 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 359 | die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix)); |
| 360 | die "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse); |
| 361 | |
| 362 | if ($color =~ /^[01]$/) { |
| 363 | $color = !$color; |
| 364 | } elsif ($color =~ /^always$/i) { |
| 365 | $color = 1; |
| 366 | } elsif ($color =~ /^never$/i) { |
| 367 | $color = 0; |
| 368 | } elsif ($color =~ /^auto$/i) { |
| 369 | $color = (-t STDOUT); |
| 370 | } else { |
| 371 | die "$P: Invalid color mode: $color\n"; |
| 372 | } |
| 373 | |
| 374 | load_docs() if ($verbose); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 375 | list_types(0) if ($list_types); |
| 376 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 377 | $fix = 1 if ($fix_inplace); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 378 | $check_orig = $check; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 379 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 380 | my $exit = 0; |
| 381 | |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 382 | my $perl_version_ok = 1; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 383 | if ($^V && $^V lt $minimum_perl_version) { |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 384 | $perl_version_ok = 0; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 385 | printf "$P: requires at least perl version %vd\n", $minimum_perl_version; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 386 | exit(1) if (!$ignore_perl_version); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 387 | } |
| 388 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 389 | #if no filenames are given, push '-' to read patch from stdin |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 390 | if ($#ARGV < 0) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 391 | push(@ARGV, '-'); |
| 392 | } |
| 393 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 394 | # skip TAB size 1 to avoid additional checks on $tabsize - 1 |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 395 | die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2); |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 396 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 397 | sub hash_save_array_words { |
| 398 | my ($hashRef, $arrayRef) = @_; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 399 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 400 | my @array = split(/,/, join(',', @$arrayRef)); |
| 401 | foreach my $word (@array) { |
| 402 | $word =~ s/\s*\n?$//g; |
| 403 | $word =~ s/^\s*//g; |
| 404 | $word =~ s/\s+/ /g; |
| 405 | $word =~ tr/[a-z]/[A-Z]/; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 406 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 407 | next if ($word =~ m/^\s*#/); |
| 408 | next if ($word =~ m/^\s*$/); |
| 409 | |
| 410 | $hashRef->{$word}++; |
| 411 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 414 | sub hash_show_words { |
| 415 | my ($hashRef, $prefix) = @_; |
| 416 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 417 | if (keys %$hashRef) { |
| 418 | print "\nNOTE: $prefix message types:"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 419 | foreach my $word (sort keys %$hashRef) { |
| 420 | print " $word"; |
| 421 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 422 | print "\n"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | hash_save_array_words(\%ignore_type, \@ignore); |
| 427 | hash_save_array_words(\%use_type, \@use); |
| 428 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 429 | my $dbg_values = 0; |
| 430 | my $dbg_possible = 0; |
| 431 | my $dbg_type = 0; |
| 432 | my $dbg_attr = 0; |
| 433 | for my $key (keys %debug) { |
| 434 | ## no critic |
| 435 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 436 | die "$@" if ($@); |
| 437 | } |
| 438 | |
| 439 | my $rpt_cleaners = 0; |
| 440 | |
| 441 | if ($terse) { |
| 442 | $emacs = 1; |
| 443 | $quiet++; |
| 444 | } |
| 445 | |
| 446 | if ($tree) { |
| 447 | if (defined $root) { |
| 448 | if (!top_of_kernel_tree($root)) { |
| 449 | die "$P: $root: --root does not point at a valid tree\n"; |
| 450 | } |
| 451 | } else { |
| 452 | if (top_of_kernel_tree('.')) { |
| 453 | $root = '.'; |
| 454 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 455 | top_of_kernel_tree($1)) { |
| 456 | $root = $1; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | if (!defined $root) { |
| 461 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 462 | exit(2); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | my $emitted_corrupt = 0; |
| 467 | |
| 468 | our $Ident = qr{ |
| 469 | [A-Za-z_][A-Za-z\d_]* |
| 470 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 471 | }x; |
| 472 | our $Storage = qr{extern|static|asmlinkage}; |
| 473 | our $Sparse = qr{ |
| 474 | __user| |
| 475 | __kernel| |
| 476 | __force| |
| 477 | __iomem| |
| 478 | __must_check| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 479 | __kprobes| |
| 480 | __ref| |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 481 | __refconst| |
| 482 | __refdata| |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 483 | __rcu| |
| 484 | __private |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 485 | }x; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 486 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; |
| 487 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; |
| 488 | our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; |
| 489 | our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; |
| 490 | our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 491 | |
| 492 | # Notes to $Attribute: |
| 493 | # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check |
| 494 | our $Attribute = qr{ |
| 495 | const| |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 496 | volatile| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 497 | __percpu| |
| 498 | __nocast| |
| 499 | __safe| |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 500 | __bitwise| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 501 | __packed__| |
| 502 | __packed2__| |
| 503 | __naked| |
| 504 | __maybe_unused| |
| 505 | __always_unused| |
| 506 | __noreturn| |
| 507 | __used| |
| 508 | __cold| |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 509 | __pure| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 510 | __noclone| |
| 511 | __deprecated| |
| 512 | __read_mostly| |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 513 | __ro_after_init| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 514 | __kprobes| |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 515 | $InitAttribute| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 516 | ____cacheline_aligned| |
| 517 | ____cacheline_aligned_in_smp| |
| 518 | ____cacheline_internodealigned_in_smp| |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 519 | __weak| |
| 520 | __alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 521 | }x; |
| 522 | our $Modifier; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 523 | our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 524 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 525 | our $Lval = qr{$Ident(?:$Member)*}; |
| 526 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 527 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 528 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 529 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 530 | our $Int = qr{[0-9]+$Int_type?}; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 531 | our $Octal = qr{0[0-7]+$Int_type?}; |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 532 | our $String = qr{(?:\b[Lu])?"[X\t]*"}; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 533 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 534 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 535 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
| 536 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 537 | our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 538 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 539 | our $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 540 | our $Arithmetic = qr{\+|-|\*|\/|%}; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 541 | our $Operators = qr{ |
| 542 | <=|>=|==|!=| |
| 543 | =>|->|<<|>>|<|>|!|~| |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 544 | &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 545 | }x; |
| 546 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 547 | our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; |
| 548 | |
| 549 | our $BasicType; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 550 | our $NonptrType; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 551 | our $NonptrTypeMisordered; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 552 | our $NonptrTypeWithAttr; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 553 | our $Type; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 554 | our $TypeMisordered; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 555 | our $Declare; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 556 | our $DeclareMisordered; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 557 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 558 | our $NON_ASCII_UTF8 = qr{ |
| 559 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 560 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 561 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 562 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 563 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 564 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 565 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 566 | }x; |
| 567 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 568 | our $UTF8 = qr{ |
| 569 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 570 | | $NON_ASCII_UTF8 |
| 571 | }x; |
| 572 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 573 | our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; |
| 574 | our $typeOtherOSTypedefs = qr{(?x: |
| 575 | u_(?:char|short|int|long) | # bsd |
| 576 | u(?:nchar|short|int|long) # sysv |
| 577 | )}; |
| 578 | our $typeKernelTypedefs = qr{(?x: |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 579 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
| 580 | atomic_t |
| 581 | )}; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 582 | our $typeTypedefs = qr{(?x: |
| 583 | $typeC99Typedefs\b| |
| 584 | $typeOtherOSTypedefs\b| |
| 585 | $typeKernelTypedefs\b |
| 586 | )}; |
| 587 | |
| 588 | our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 589 | |
| 590 | our $logFunctions = qr{(?x: |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 591 | printk(?:_ratelimited|_once|_deferred_once|_deferred|)| |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 592 | (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 593 | TP_printk| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 594 | WARN(?:_RATELIMIT|_ONCE|)| |
| 595 | panic| |
James Byrne | 66b3ccc | 2019-11-21 14:32:46 +0000 | [diff] [blame] | 596 | debug| |
| 597 | printf| |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 598 | MODULE_[A-Z_]+| |
| 599 | seq_vprintf|seq_printf|seq_puts |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 600 | )}; |
| 601 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 602 | our $allocFunctions = qr{(?x: |
| 603 | (?:(?:devm_)? |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 604 | (?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 605 | kstrdup(?:_const)? | |
| 606 | kmemdup(?:_nul)?) | |
| 607 | (?:\w+)?alloc_skb(?:_ip_align)? | |
| 608 | # dev_alloc_skb/netdev_alloc_skb, et al |
| 609 | dma_alloc_coherent |
| 610 | )}; |
| 611 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 612 | our $signature_tags = qr{(?xi: |
| 613 | Signed-off-by:| |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 614 | Co-developed-by:| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 615 | Acked-by:| |
| 616 | Tested-by:| |
| 617 | Reviewed-by:| |
| 618 | Reported-by:| |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 619 | Suggested-by:| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 620 | To:| |
| 621 | Cc: |
| 622 | )}; |
| 623 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 624 | our $tracing_logging_tags = qr{(?xi: |
| 625 | [=-]*> | |
| 626 | <[=-]* | |
| 627 | \[ | |
| 628 | \] | |
| 629 | start | |
| 630 | called | |
| 631 | entered | |
| 632 | entry | |
| 633 | enter | |
| 634 | in | |
| 635 | inside | |
| 636 | here | |
| 637 | begin | |
| 638 | exit | |
| 639 | end | |
| 640 | done | |
| 641 | leave | |
| 642 | completed | |
| 643 | out | |
| 644 | return | |
| 645 | [\.\!:\s]* |
| 646 | )}; |
| 647 | |
| 648 | sub edit_distance_min { |
| 649 | my (@arr) = @_; |
| 650 | my $len = scalar @arr; |
| 651 | if ((scalar @arr) < 1) { |
| 652 | # if underflow, return |
| 653 | return; |
| 654 | } |
| 655 | my $min = $arr[0]; |
| 656 | for my $i (0 .. ($len-1)) { |
| 657 | if ($arr[$i] < $min) { |
| 658 | $min = $arr[$i]; |
| 659 | } |
| 660 | } |
| 661 | return $min; |
| 662 | } |
| 663 | |
| 664 | sub get_edit_distance { |
| 665 | my ($str1, $str2) = @_; |
| 666 | $str1 = lc($str1); |
| 667 | $str2 = lc($str2); |
| 668 | $str1 =~ s/-//g; |
| 669 | $str2 =~ s/-//g; |
| 670 | my $len1 = length($str1); |
| 671 | my $len2 = length($str2); |
| 672 | # two dimensional array storing minimum edit distance |
| 673 | my @distance; |
| 674 | for my $i (0 .. $len1) { |
| 675 | for my $j (0 .. $len2) { |
| 676 | if ($i == 0) { |
| 677 | $distance[$i][$j] = $j; |
| 678 | } elsif ($j == 0) { |
| 679 | $distance[$i][$j] = $i; |
| 680 | } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) { |
| 681 | $distance[$i][$j] = $distance[$i - 1][$j - 1]; |
| 682 | } else { |
| 683 | my $dist1 = $distance[$i][$j - 1]; #insert distance |
| 684 | my $dist2 = $distance[$i - 1][$j]; # remove |
| 685 | my $dist3 = $distance[$i - 1][$j - 1]; #replace |
| 686 | $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | return $distance[$len1][$len2]; |
| 691 | } |
| 692 | |
| 693 | sub find_standard_signature { |
| 694 | my ($sign_off) = @_; |
| 695 | my @standard_signature_tags = ( |
| 696 | 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:', |
| 697 | 'Reviewed-by:', 'Reported-by:', 'Suggested-by:' |
| 698 | ); |
| 699 | foreach my $signature (@standard_signature_tags) { |
| 700 | return $signature if (get_edit_distance($sign_off, $signature) <= 2); |
| 701 | } |
| 702 | |
| 703 | return ""; |
| 704 | } |
| 705 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 706 | our @typeListMisordered = ( |
| 707 | qr{char\s+(?:un)?signed}, |
| 708 | qr{int\s+(?:(?:un)?signed\s+)?short\s}, |
| 709 | qr{int\s+short(?:\s+(?:un)?signed)}, |
| 710 | qr{short\s+int(?:\s+(?:un)?signed)}, |
| 711 | qr{(?:un)?signed\s+int\s+short}, |
| 712 | qr{short\s+(?:un)?signed}, |
| 713 | qr{long\s+int\s+(?:un)?signed}, |
| 714 | qr{int\s+long\s+(?:un)?signed}, |
| 715 | qr{long\s+(?:un)?signed\s+int}, |
| 716 | qr{int\s+(?:un)?signed\s+long}, |
| 717 | qr{int\s+(?:un)?signed}, |
| 718 | qr{int\s+long\s+long\s+(?:un)?signed}, |
| 719 | qr{long\s+long\s+int\s+(?:un)?signed}, |
| 720 | qr{long\s+long\s+(?:un)?signed\s+int}, |
| 721 | qr{long\s+long\s+(?:un)?signed}, |
| 722 | qr{long\s+(?:un)?signed}, |
| 723 | ); |
| 724 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 725 | our @typeList = ( |
| 726 | qr{void}, |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 727 | qr{(?:(?:un)?signed\s+)?char}, |
| 728 | qr{(?:(?:un)?signed\s+)?short\s+int}, |
| 729 | qr{(?:(?:un)?signed\s+)?short}, |
| 730 | qr{(?:(?:un)?signed\s+)?int}, |
| 731 | qr{(?:(?:un)?signed\s+)?long\s+int}, |
| 732 | qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, |
| 733 | qr{(?:(?:un)?signed\s+)?long\s+long}, |
| 734 | qr{(?:(?:un)?signed\s+)?long}, |
| 735 | qr{(?:un)?signed}, |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 736 | qr{float}, |
| 737 | qr{double}, |
| 738 | qr{bool}, |
| 739 | qr{struct\s+$Ident}, |
| 740 | qr{union\s+$Ident}, |
| 741 | qr{enum\s+$Ident}, |
| 742 | qr{${Ident}_t}, |
| 743 | qr{${Ident}_handler}, |
| 744 | qr{${Ident}_handler_fn}, |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 745 | @typeListMisordered, |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 746 | ); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 747 | |
| 748 | our $C90_int_types = qr{(?x: |
| 749 | long\s+long\s+int\s+(?:un)?signed| |
| 750 | long\s+long\s+(?:un)?signed\s+int| |
| 751 | long\s+long\s+(?:un)?signed| |
| 752 | (?:(?:un)?signed\s+)?long\s+long\s+int| |
| 753 | (?:(?:un)?signed\s+)?long\s+long| |
| 754 | int\s+long\s+long\s+(?:un)?signed| |
| 755 | int\s+(?:(?:un)?signed\s+)?long\s+long| |
| 756 | |
| 757 | long\s+int\s+(?:un)?signed| |
| 758 | long\s+(?:un)?signed\s+int| |
| 759 | long\s+(?:un)?signed| |
| 760 | (?:(?:un)?signed\s+)?long\s+int| |
| 761 | (?:(?:un)?signed\s+)?long| |
| 762 | int\s+long\s+(?:un)?signed| |
| 763 | int\s+(?:(?:un)?signed\s+)?long| |
| 764 | |
| 765 | int\s+(?:un)?signed| |
| 766 | (?:(?:un)?signed\s+)?int |
| 767 | )}; |
| 768 | |
| 769 | our @typeListFile = (); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 770 | our @typeListWithAttr = ( |
| 771 | @typeList, |
| 772 | qr{struct\s+$InitAttribute\s+$Ident}, |
| 773 | qr{union\s+$InitAttribute\s+$Ident}, |
| 774 | ); |
| 775 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 776 | our @modifierList = ( |
| 777 | qr{fastcall}, |
| 778 | ); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 779 | our @modifierListFile = (); |
| 780 | |
| 781 | our @mode_permission_funcs = ( |
| 782 | ["module_param", 3], |
| 783 | ["module_param_(?:array|named|string)", 4], |
| 784 | ["module_param_array_named", 5], |
| 785 | ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], |
| 786 | ["proc_create(?:_data|)", 2], |
| 787 | ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], |
| 788 | ["IIO_DEV_ATTR_[A-Z_]+", 1], |
| 789 | ["SENSOR_(?:DEVICE_|)ATTR_2", 2], |
| 790 | ["SENSOR_TEMPLATE(?:_2|)", 3], |
| 791 | ["__ATTR", 2], |
| 792 | ); |
| 793 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 794 | my $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; |
| 795 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 796 | #Create a search pattern for all these functions to speed up a loop below |
| 797 | our $mode_perms_search = ""; |
| 798 | foreach my $entry (@mode_permission_funcs) { |
| 799 | $mode_perms_search .= '|' if ($mode_perms_search ne ""); |
| 800 | $mode_perms_search .= $entry->[0]; |
| 801 | } |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 802 | $mode_perms_search = "(?:${mode_perms_search})"; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 803 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 804 | our %deprecated_apis = ( |
| 805 | "synchronize_rcu_bh" => "synchronize_rcu", |
| 806 | "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", |
| 807 | "call_rcu_bh" => "call_rcu", |
| 808 | "rcu_barrier_bh" => "rcu_barrier", |
| 809 | "synchronize_sched" => "synchronize_rcu", |
| 810 | "synchronize_sched_expedited" => "synchronize_rcu_expedited", |
| 811 | "call_rcu_sched" => "call_rcu", |
| 812 | "rcu_barrier_sched" => "rcu_barrier", |
| 813 | "get_state_synchronize_sched" => "get_state_synchronize_rcu", |
| 814 | "cond_synchronize_sched" => "cond_synchronize_rcu", |
| 815 | ); |
| 816 | |
| 817 | #Create a search pattern for all these strings to speed up a loop below |
| 818 | our $deprecated_apis_search = ""; |
| 819 | foreach my $entry (keys %deprecated_apis) { |
| 820 | $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); |
| 821 | $deprecated_apis_search .= $entry; |
| 822 | } |
| 823 | $deprecated_apis_search = "(?:${deprecated_apis_search})"; |
| 824 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 825 | our $mode_perms_world_writable = qr{ |
| 826 | S_IWUGO | |
| 827 | S_IWOTH | |
| 828 | S_IRWXUGO | |
| 829 | S_IALLUGO | |
| 830 | 0[0-7][0-7][2367] |
| 831 | }x; |
| 832 | |
| 833 | our %mode_permission_string_types = ( |
| 834 | "S_IRWXU" => 0700, |
| 835 | "S_IRUSR" => 0400, |
| 836 | "S_IWUSR" => 0200, |
| 837 | "S_IXUSR" => 0100, |
| 838 | "S_IRWXG" => 0070, |
| 839 | "S_IRGRP" => 0040, |
| 840 | "S_IWGRP" => 0020, |
| 841 | "S_IXGRP" => 0010, |
| 842 | "S_IRWXO" => 0007, |
| 843 | "S_IROTH" => 0004, |
| 844 | "S_IWOTH" => 0002, |
| 845 | "S_IXOTH" => 0001, |
| 846 | "S_IRWXUGO" => 0777, |
| 847 | "S_IRUGO" => 0444, |
| 848 | "S_IWUGO" => 0222, |
| 849 | "S_IXUGO" => 0111, |
| 850 | ); |
| 851 | |
| 852 | #Create a search pattern for all these strings to speed up a loop below |
| 853 | our $mode_perms_string_search = ""; |
| 854 | foreach my $entry (keys %mode_permission_string_types) { |
| 855 | $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); |
| 856 | $mode_perms_string_search .= $entry; |
| 857 | } |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 858 | our $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; |
| 859 | our $multi_mode_perms_string_search = qr{ |
| 860 | ${single_mode_perms_string_search} |
| 861 | (?:\s*\|\s*${single_mode_perms_string_search})* |
| 862 | }x; |
| 863 | |
| 864 | sub perms_to_octal { |
| 865 | my ($string) = @_; |
| 866 | |
| 867 | return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); |
| 868 | |
| 869 | my $val = ""; |
| 870 | my $oval = ""; |
| 871 | my $to = 0; |
| 872 | my $curpos = 0; |
| 873 | my $lastpos = 0; |
| 874 | while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { |
| 875 | $curpos = pos($string); |
| 876 | my $match = $2; |
| 877 | my $omatch = $1; |
| 878 | last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); |
| 879 | $lastpos = $curpos; |
| 880 | $to |= $mode_permission_string_types{$match}; |
| 881 | $val .= '\s*\|\s*' if ($val ne ""); |
| 882 | $val .= $match; |
| 883 | $oval .= $omatch; |
| 884 | } |
| 885 | $oval =~ s/^\s*\|\s*//; |
| 886 | $oval =~ s/\s*\|\s*$//; |
| 887 | return sprintf("%04o", $to); |
| 888 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 889 | |
| 890 | our $allowed_asm_includes = qr{(?x: |
| 891 | irq| |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 892 | memory| |
| 893 | time| |
| 894 | reboot |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 895 | )}; |
| 896 | # memory.h: ARM has a custom one |
| 897 | |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 898 | # Load common spelling mistakes and build regular expression list. |
| 899 | my $misspellings; |
| 900 | my %spelling_fix; |
| 901 | |
| 902 | if (open(my $spelling, '<', $spelling_file)) { |
| 903 | while (<$spelling>) { |
| 904 | my $line = $_; |
| 905 | |
| 906 | $line =~ s/\s*\n?$//g; |
| 907 | $line =~ s/^\s*//g; |
| 908 | |
| 909 | next if ($line =~ m/^\s*#/); |
| 910 | next if ($line =~ m/^\s*$/); |
| 911 | |
| 912 | my ($suspect, $fix) = split(/\|\|/, $line); |
| 913 | |
| 914 | $spelling_fix{$suspect} = $fix; |
| 915 | } |
| 916 | close($spelling); |
| 917 | } else { |
| 918 | warn "No typos will be found - file '$spelling_file': $!\n"; |
| 919 | } |
| 920 | |
| 921 | if ($codespell) { |
| 922 | if (open(my $spelling, '<', $codespellfile)) { |
| 923 | while (<$spelling>) { |
| 924 | my $line = $_; |
| 925 | |
| 926 | $line =~ s/\s*\n?$//g; |
| 927 | $line =~ s/^\s*//g; |
| 928 | |
| 929 | next if ($line =~ m/^\s*#/); |
| 930 | next if ($line =~ m/^\s*$/); |
| 931 | next if ($line =~ m/, disabled/i); |
| 932 | |
| 933 | $line =~ s/,.*$//; |
| 934 | |
| 935 | my ($suspect, $fix) = split(/->/, $line); |
| 936 | |
| 937 | $spelling_fix{$suspect} = $fix; |
| 938 | } |
| 939 | close($spelling); |
| 940 | } else { |
| 941 | warn "No codespell typos will be found - file '$codespellfile': $!\n"; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; |
| 946 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 947 | sub read_words { |
| 948 | my ($wordsRef, $file) = @_; |
| 949 | |
| 950 | if (open(my $words, '<', $file)) { |
| 951 | while (<$words>) { |
| 952 | my $line = $_; |
| 953 | |
| 954 | $line =~ s/\s*\n?$//g; |
| 955 | $line =~ s/^\s*//g; |
| 956 | |
| 957 | next if ($line =~ m/^\s*#/); |
| 958 | next if ($line =~ m/^\s*$/); |
| 959 | if ($line =~ /\s/) { |
| 960 | print("$file: '$line' invalid - ignored\n"); |
| 961 | next; |
| 962 | } |
| 963 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 964 | $$wordsRef .= '|' if (defined $$wordsRef); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 965 | $$wordsRef .= $line; |
| 966 | } |
| 967 | close($file); |
| 968 | return 1; |
| 969 | } |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 974 | my $const_structs; |
| 975 | if (show_type("CONST_STRUCT")) { |
| 976 | read_words(\$const_structs, $conststructsfile) |
| 977 | or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; |
| 978 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 979 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 980 | if (defined($typedefsfile)) { |
| 981 | my $typeOtherTypedefs; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 982 | read_words(\$typeOtherTypedefs, $typedefsfile) |
| 983 | or warn "No additional types will be considered - file '$typedefsfile': $!\n"; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 984 | $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 985 | } |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 986 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 987 | sub build_types { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 988 | my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; |
| 989 | my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; |
| 990 | my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 991 | my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 992 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 993 | $BasicType = qr{ |
| 994 | (?:$typeTypedefs\b)| |
| 995 | (?:${all}\b) |
| 996 | }x; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 997 | $NonptrType = qr{ |
| 998 | (?:$Modifier\s+|const\s+)* |
| 999 | (?: |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 1000 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1001 | (?:$typeTypedefs\b)| |
| 1002 | (?:${all}\b) |
| 1003 | ) |
| 1004 | (?:\s+$Modifier|\s+const)* |
| 1005 | }x; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1006 | $NonptrTypeMisordered = qr{ |
| 1007 | (?:$Modifier\s+|const\s+)* |
| 1008 | (?: |
| 1009 | (?:${Misordered}\b) |
| 1010 | ) |
| 1011 | (?:\s+$Modifier|\s+const)* |
| 1012 | }x; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1013 | $NonptrTypeWithAttr = qr{ |
| 1014 | (?:$Modifier\s+|const\s+)* |
| 1015 | (?: |
| 1016 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 1017 | (?:$typeTypedefs\b)| |
| 1018 | (?:${allWithAttr}\b) |
| 1019 | ) |
| 1020 | (?:\s+$Modifier|\s+const)* |
| 1021 | }x; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1022 | $Type = qr{ |
| 1023 | $NonptrType |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1024 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1025 | (?:\s+$Inline|\s+$Modifier)* |
| 1026 | }x; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1027 | $TypeMisordered = qr{ |
| 1028 | $NonptrTypeMisordered |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1029 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1030 | (?:\s+$Inline|\s+$Modifier)* |
| 1031 | }x; |
| 1032 | $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; |
| 1033 | $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1034 | } |
| 1035 | build_types(); |
| 1036 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1037 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 1038 | |
| 1039 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 1040 | # requires at least perl version v5.10.0 |
| 1041 | # Any use must be runtime checked with $^V |
| 1042 | |
| 1043 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1044 | our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; |
| 1045 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; |
| 1046 | |
| 1047 | our $declaration_macros = qr{(?x: |
| 1048 | (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| |
| 1049 | (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 1050 | (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1051 | )}; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1052 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1053 | our %allow_repeated_words = ( |
| 1054 | add => '', |
| 1055 | added => '', |
| 1056 | bad => '', |
| 1057 | be => '', |
| 1058 | ); |
| 1059 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1060 | sub deparenthesize { |
| 1061 | my ($string) = @_; |
| 1062 | return "" if (!defined($string)); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1063 | |
| 1064 | while ($string =~ /^\s*\(.*\)\s*$/) { |
| 1065 | $string =~ s@^\s*\(\s*@@; |
| 1066 | $string =~ s@\s*\)\s*$@@; |
| 1067 | } |
| 1068 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1069 | $string =~ s@\s+@ @g; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1070 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1071 | return $string; |
| 1072 | } |
| 1073 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1074 | sub seed_camelcase_file { |
| 1075 | my ($file) = @_; |
| 1076 | |
| 1077 | return if (!(-f $file)); |
| 1078 | |
| 1079 | local $/; |
| 1080 | |
| 1081 | open(my $include_file, '<', "$file") |
| 1082 | or warn "$P: Can't read '$file' $!\n"; |
| 1083 | my $text = <$include_file>; |
| 1084 | close($include_file); |
| 1085 | |
| 1086 | my @lines = split('\n', $text); |
| 1087 | |
| 1088 | foreach my $line (@lines) { |
| 1089 | next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); |
| 1090 | if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { |
| 1091 | $camelcase{$1} = 1; |
| 1092 | } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { |
| 1093 | $camelcase{$1} = 1; |
| 1094 | } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { |
| 1095 | $camelcase{$1} = 1; |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1100 | our %maintained_status = (); |
| 1101 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1102 | sub is_maintained_obsolete { |
| 1103 | my ($filename) = @_; |
| 1104 | |
| 1105 | return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); |
| 1106 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1107 | if (!exists($maintained_status{$filename})) { |
| 1108 | $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; |
| 1109 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1110 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1111 | return $maintained_status{$filename} =~ /obsolete/i; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 1114 | sub is_SPDX_License_valid { |
| 1115 | my ($license) = @_; |
| 1116 | |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 1117 | return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot")); |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 1118 | |
| 1119 | my $root_path = abs_path($root); |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 1120 | my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 1121 | return 0 if ($status ne ""); |
| 1122 | return 1; |
| 1123 | } |
| 1124 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1125 | my $camelcase_seeded = 0; |
| 1126 | sub seed_camelcase_includes { |
| 1127 | return if ($camelcase_seeded); |
| 1128 | |
| 1129 | my $files; |
| 1130 | my $camelcase_cache = ""; |
| 1131 | my @include_files = (); |
| 1132 | |
| 1133 | $camelcase_seeded = 1; |
| 1134 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1135 | if (-e "$gitroot") { |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1136 | my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1137 | chomp $git_last_include_commit; |
| 1138 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
| 1139 | } else { |
| 1140 | my $last_mod_date = 0; |
| 1141 | $files = `find $root/include -name "*.h"`; |
| 1142 | @include_files = split('\n', $files); |
| 1143 | foreach my $file (@include_files) { |
| 1144 | my $date = POSIX::strftime("%Y%m%d%H%M", |
| 1145 | localtime((stat $file)[9])); |
| 1146 | $last_mod_date = $date if ($last_mod_date < $date); |
| 1147 | } |
| 1148 | $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; |
| 1149 | } |
| 1150 | |
| 1151 | if ($camelcase_cache ne "" && -f $camelcase_cache) { |
| 1152 | open(my $camelcase_file, '<', "$camelcase_cache") |
| 1153 | or warn "$P: Can't read '$camelcase_cache' $!\n"; |
| 1154 | while (<$camelcase_file>) { |
| 1155 | chomp; |
| 1156 | $camelcase{$_} = 1; |
| 1157 | } |
| 1158 | close($camelcase_file); |
| 1159 | |
| 1160 | return; |
| 1161 | } |
| 1162 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1163 | if (-e "$gitroot") { |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1164 | $files = `${git_command} ls-files "include/*.h"`; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1165 | @include_files = split('\n', $files); |
| 1166 | } |
| 1167 | |
| 1168 | foreach my $file (@include_files) { |
| 1169 | seed_camelcase_file($file); |
| 1170 | } |
| 1171 | |
| 1172 | if ($camelcase_cache ne "") { |
| 1173 | unlink glob ".checkpatch-camelcase.*"; |
| 1174 | open(my $camelcase_file, '>', "$camelcase_cache") |
| 1175 | or warn "$P: Can't write '$camelcase_cache' $!\n"; |
| 1176 | foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { |
| 1177 | print $camelcase_file ("$_\n"); |
| 1178 | } |
| 1179 | close($camelcase_file); |
| 1180 | } |
| 1181 | } |
| 1182 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1183 | sub git_is_single_file { |
| 1184 | my ($filename) = @_; |
| 1185 | |
| 1186 | return 0 if ((which("git") eq "") || !(-e "$gitroot")); |
| 1187 | |
| 1188 | my $output = `${git_command} ls-files -- $filename 2>/dev/null`; |
| 1189 | my $count = $output =~ tr/\n//; |
| 1190 | return $count eq 1 && $output =~ m{^${filename}$}; |
| 1191 | } |
| 1192 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1193 | sub git_commit_info { |
| 1194 | my ($commit, $id, $desc) = @_; |
| 1195 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1196 | return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot")); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1197 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1198 | my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1199 | $output =~ s/^\s*//gm; |
| 1200 | my @lines = split("\n", $output); |
| 1201 | |
| 1202 | return ($id, $desc) if ($#lines < 0); |
| 1203 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1204 | if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1205 | # Maybe one day convert this block of bash into something that returns |
| 1206 | # all matching commit ids, but it's very slow... |
| 1207 | # |
| 1208 | # echo "checking commits $1..." |
| 1209 | # git rev-list --remotes | grep -i "^$1" | |
| 1210 | # while read line ; do |
| 1211 | # git log --format='%H %s' -1 $line | |
| 1212 | # echo "commit $(cut -c 1-12,41-)" |
| 1213 | # done |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 1214 | } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ || |
| 1215 | $lines[0] =~ /^fatal: bad object $commit/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1216 | $id = undef; |
| 1217 | } else { |
| 1218 | $id = substr($lines[0], 0, 12); |
| 1219 | $desc = substr($lines[0], 41); |
| 1220 | } |
| 1221 | |
| 1222 | return ($id, $desc); |
| 1223 | } |
| 1224 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1225 | $chk_signoff = 0 if ($file); |
| 1226 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1227 | my @rawlines = (); |
| 1228 | my @lines = (); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1229 | my @fixed = (); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1230 | my @fixed_inserted = (); |
| 1231 | my @fixed_deleted = (); |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 1232 | my $fixlinenr = -1; |
| 1233 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1234 | # If input is git commits, extract all commits from the commit expressions. |
| 1235 | # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1236 | die "$P: No git repository found\n" if ($git && !-e "$gitroot"); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1237 | |
| 1238 | if ($git) { |
| 1239 | my @commits = (); |
| 1240 | foreach my $commit_expr (@ARGV) { |
| 1241 | my $git_range; |
| 1242 | if ($commit_expr =~ m/^(.*)-(\d+)$/) { |
| 1243 | $git_range = "-$2 $1"; |
| 1244 | } elsif ($commit_expr =~ m/\.\./) { |
| 1245 | $git_range = "$commit_expr"; |
| 1246 | } else { |
| 1247 | $git_range = "-1 $commit_expr"; |
| 1248 | } |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1249 | my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1250 | foreach my $line (split(/\n/, $lines)) { |
| 1251 | $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; |
| 1252 | next if (!defined($1) || !defined($2)); |
| 1253 | my $sha1 = $1; |
| 1254 | my $subject = $2; |
| 1255 | unshift(@commits, $sha1); |
| 1256 | $git_commits{$sha1} = $subject; |
| 1257 | } |
| 1258 | } |
| 1259 | die "$P: no git commits after extraction!\n" if (@commits == 0); |
| 1260 | @ARGV = @commits; |
| 1261 | } |
| 1262 | |
| 1263 | my $vname; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1264 | $allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1265 | for my $filename (@ARGV) { |
| 1266 | my $FILE; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1267 | my $is_git_file = git_is_single_file($filename); |
| 1268 | my $oldfile = $file; |
| 1269 | $file = 1 if ($is_git_file); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1270 | if ($git) { |
| 1271 | open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || |
| 1272 | die "$P: $filename: git format-patch failed - $!\n"; |
| 1273 | } elsif ($file) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1274 | open($FILE, '-|', "diff -u /dev/null $filename") || |
| 1275 | die "$P: $filename: diff failed - $!\n"; |
| 1276 | } elsif ($filename eq '-') { |
| 1277 | open($FILE, '<&STDIN'); |
| 1278 | } else { |
| 1279 | open($FILE, '<', "$filename") || |
| 1280 | die "$P: $filename: open failed - $!\n"; |
| 1281 | } |
| 1282 | if ($filename eq '-') { |
| 1283 | $vname = 'Your patch'; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1284 | } elsif ($git) { |
| 1285 | $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1286 | } else { |
| 1287 | $vname = $filename; |
| 1288 | } |
| 1289 | while (<$FILE>) { |
| 1290 | chomp; |
| 1291 | push(@rawlines, $_); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1292 | $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1293 | } |
| 1294 | close($FILE); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1295 | |
| 1296 | if ($#ARGV > 0 && $quiet == 0) { |
| 1297 | print '-' x length($vname) . "\n"; |
| 1298 | print "$vname\n"; |
| 1299 | print '-' x length($vname) . "\n"; |
| 1300 | } |
| 1301 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1302 | if (!process($filename)) { |
| 1303 | $exit = 1; |
| 1304 | } |
| 1305 | @rawlines = (); |
| 1306 | @lines = (); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1307 | @fixed = (); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1308 | @fixed_inserted = (); |
| 1309 | @fixed_deleted = (); |
| 1310 | $fixlinenr = -1; |
| 1311 | @modifierListFile = (); |
| 1312 | @typeListFile = (); |
| 1313 | build_types(); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1314 | $file = $oldfile if ($is_git_file); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | if (!$quiet) { |
| 1318 | hash_show_words(\%use_type, "Used"); |
| 1319 | hash_show_words(\%ignore_type, "Ignored"); |
| 1320 | |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 1321 | if (!$perl_version_ok) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1322 | print << "EOM" |
| 1323 | |
| 1324 | NOTE: perl $^V is not modern enough to detect all possible issues. |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 1325 | An upgrade to at least perl $minimum_perl_version is suggested. |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1326 | EOM |
| 1327 | } |
| 1328 | if ($exit) { |
| 1329 | print << "EOM" |
| 1330 | |
| 1331 | NOTE: If any of the errors are false positives, please report |
| 1332 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 1333 | EOM |
| 1334 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | exit($exit); |
| 1338 | |
| 1339 | sub top_of_kernel_tree { |
| 1340 | my ($root) = @_; |
| 1341 | |
| 1342 | my @tree_check = ( |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1343 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1344 | "README", "Documentation", "arch", "include", "drivers", |
| 1345 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 1346 | ); |
| 1347 | |
| 1348 | foreach my $check (@tree_check) { |
| 1349 | if (! -e $root . '/' . $check) { |
| 1350 | return 0; |
| 1351 | } |
| 1352 | } |
| 1353 | return 1; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 1354 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1355 | |
| 1356 | sub parse_email { |
| 1357 | my ($formatted_email) = @_; |
| 1358 | |
| 1359 | my $name = ""; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1360 | my $quoted = ""; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1361 | my $name_comment = ""; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1362 | my $address = ""; |
| 1363 | my $comment = ""; |
| 1364 | |
| 1365 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 1366 | $name = $1; |
| 1367 | $address = $2; |
| 1368 | $comment = $3 if defined $3; |
| 1369 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 1370 | $address = $1; |
| 1371 | $comment = $2 if defined $2; |
| 1372 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 1373 | $address = $1; |
| 1374 | $comment = $2 if defined $2; |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 1375 | $formatted_email =~ s/\Q$address\E.*$//; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1376 | $name = $formatted_email; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1377 | $name = trim($name); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1378 | $name =~ s/^\"|\"$//g; |
| 1379 | # If there's a name left after stripping spaces and |
| 1380 | # leading quotes, and the address doesn't have both |
| 1381 | # leading and trailing angle brackets, the address |
| 1382 | # is invalid. ie: |
| 1383 | # "joe smith joe@smith.com" bad |
| 1384 | # "joe smith <joe@smith.com" bad |
| 1385 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 1386 | $name = ""; |
| 1387 | $address = ""; |
| 1388 | $comment = ""; |
| 1389 | } |
| 1390 | } |
| 1391 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1392 | # Extract comments from names excluding quoted parts |
| 1393 | # "John D. (Doe)" - Do not extract |
| 1394 | if ($name =~ s/\"(.+)\"//) { |
| 1395 | $quoted = $1; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1396 | } |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1397 | while ($name =~ s/\s*($balanced_parens)\s*/ /) { |
| 1398 | $name_comment .= trim($1); |
| 1399 | } |
| 1400 | $name =~ s/^[ \"]+|[ \"]+$//g; |
| 1401 | $name = trim("$quoted $name"); |
| 1402 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1403 | $address = trim($address); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1404 | $address =~ s/^\<|\>$//g; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1405 | $comment = trim($comment); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1406 | |
| 1407 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 1408 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 1409 | $name = "\"$name\""; |
| 1410 | } |
| 1411 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1412 | return ($name, $name_comment, $address, $comment); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | sub format_email { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1416 | my ($name, $name_comment, $address, $comment) = @_; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1417 | |
| 1418 | my $formatted_email; |
| 1419 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1420 | $name =~ s/^[ \"]+|[ \"]+$//g; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1421 | $address = trim($address); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1422 | $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1423 | |
| 1424 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 1425 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 1426 | $name = "\"$name\""; |
| 1427 | } |
| 1428 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1429 | $name_comment = trim($name_comment); |
| 1430 | $name_comment = " $name_comment" if ($name_comment ne ""); |
| 1431 | $comment = trim($comment); |
| 1432 | $comment = " $comment" if ($comment ne ""); |
| 1433 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1434 | if ("$name" eq "") { |
| 1435 | $formatted_email = "$address"; |
| 1436 | } else { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1437 | $formatted_email = "$name$name_comment <$address>"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1438 | } |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1439 | $formatted_email .= "$comment"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1440 | return $formatted_email; |
| 1441 | } |
| 1442 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1443 | sub reformat_email { |
| 1444 | my ($email) = @_; |
| 1445 | |
| 1446 | my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1447 | return format_email($email_name, $name_comment, $email_address, $comment); |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | sub same_email_addresses { |
| 1451 | my ($email1, $email2) = @_; |
| 1452 | |
| 1453 | my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); |
| 1454 | my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); |
| 1455 | |
| 1456 | return $email1_name eq $email2_name && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1457 | $email1_address eq $email2_address && |
| 1458 | $name1_comment eq $name2_comment && |
| 1459 | $comment1 eq $comment2; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1460 | } |
| 1461 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1462 | sub which { |
| 1463 | my ($bin) = @_; |
| 1464 | |
| 1465 | foreach my $path (split(/:/, $ENV{PATH})) { |
| 1466 | if (-e "$path/$bin") { |
| 1467 | return "$path/$bin"; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | return ""; |
| 1472 | } |
| 1473 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1474 | sub which_conf { |
| 1475 | my ($conf) = @_; |
| 1476 | |
| 1477 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 1478 | if (-e "$path/$conf") { |
| 1479 | return "$path/$conf"; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | return ""; |
| 1484 | } |
| 1485 | |
| 1486 | sub expand_tabs { |
| 1487 | my ($str) = @_; |
| 1488 | |
| 1489 | my $res = ''; |
| 1490 | my $n = 0; |
| 1491 | for my $c (split(//, $str)) { |
| 1492 | if ($c eq "\t") { |
| 1493 | $res .= ' '; |
| 1494 | $n++; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1495 | for (; ($n % $tabsize) != 0; $n++) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1496 | $res .= ' '; |
| 1497 | } |
| 1498 | next; |
| 1499 | } |
| 1500 | $res .= $c; |
| 1501 | $n++; |
| 1502 | } |
| 1503 | |
| 1504 | return $res; |
| 1505 | } |
| 1506 | sub copy_spacing { |
| 1507 | (my $res = shift) =~ tr/\t/ /c; |
| 1508 | return $res; |
| 1509 | } |
| 1510 | |
| 1511 | sub line_stats { |
| 1512 | my ($line) = @_; |
| 1513 | |
| 1514 | # Drop the diff line leader and expand tabs |
| 1515 | $line =~ s/^.//; |
| 1516 | $line = expand_tabs($line); |
| 1517 | |
| 1518 | # Pick the indent from the front of the line. |
| 1519 | my ($white) = ($line =~ /^(\s*)/); |
| 1520 | |
| 1521 | return (length($line), length($white)); |
| 1522 | } |
| 1523 | |
| 1524 | my $sanitise_quote = ''; |
| 1525 | |
| 1526 | sub sanitise_line_reset { |
| 1527 | my ($in_comment) = @_; |
| 1528 | |
| 1529 | if ($in_comment) { |
| 1530 | $sanitise_quote = '*/'; |
| 1531 | } else { |
| 1532 | $sanitise_quote = ''; |
| 1533 | } |
| 1534 | } |
| 1535 | sub sanitise_line { |
| 1536 | my ($line) = @_; |
| 1537 | |
| 1538 | my $res = ''; |
| 1539 | my $l = ''; |
| 1540 | |
| 1541 | my $qlen = 0; |
| 1542 | my $off = 0; |
| 1543 | my $c; |
| 1544 | |
| 1545 | # Always copy over the diff marker. |
| 1546 | $res = substr($line, 0, 1); |
| 1547 | |
| 1548 | for ($off = 1; $off < length($line); $off++) { |
| 1549 | $c = substr($line, $off, 1); |
| 1550 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 1551 | # Comments we are whacking completely including the begin |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1552 | # and end, all to $;. |
| 1553 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 1554 | $sanitise_quote = '*/'; |
| 1555 | |
| 1556 | substr($res, $off, 2, "$;$;"); |
| 1557 | $off++; |
| 1558 | next; |
| 1559 | } |
| 1560 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
| 1561 | $sanitise_quote = ''; |
| 1562 | substr($res, $off, 2, "$;$;"); |
| 1563 | $off++; |
| 1564 | next; |
| 1565 | } |
| 1566 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 1567 | $sanitise_quote = '//'; |
| 1568 | |
| 1569 | substr($res, $off, 2, $sanitise_quote); |
| 1570 | $off++; |
| 1571 | next; |
| 1572 | } |
| 1573 | |
| 1574 | # A \ in a string means ignore the next character. |
| 1575 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 1576 | $c eq "\\") { |
| 1577 | substr($res, $off, 2, 'XX'); |
| 1578 | $off++; |
| 1579 | next; |
| 1580 | } |
| 1581 | # Regular quotes. |
| 1582 | if ($c eq "'" || $c eq '"') { |
| 1583 | if ($sanitise_quote eq '') { |
| 1584 | $sanitise_quote = $c; |
| 1585 | |
| 1586 | substr($res, $off, 1, $c); |
| 1587 | next; |
| 1588 | } elsif ($sanitise_quote eq $c) { |
| 1589 | $sanitise_quote = ''; |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | #print "c<$c> SQ<$sanitise_quote>\n"; |
| 1594 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 1595 | substr($res, $off, 1, $;); |
| 1596 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 1597 | substr($res, $off, 1, $;); |
| 1598 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 1599 | substr($res, $off, 1, 'X'); |
| 1600 | } else { |
| 1601 | substr($res, $off, 1, $c); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | if ($sanitise_quote eq '//') { |
| 1606 | $sanitise_quote = ''; |
| 1607 | } |
| 1608 | |
| 1609 | # The pathname on a #include may be surrounded by '<' and '>'. |
| 1610 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
| 1611 | my $clean = 'X' x length($1); |
| 1612 | $res =~ s@\<.*\>@<$clean>@; |
| 1613 | |
| 1614 | # The whole of a #error is a string. |
| 1615 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
| 1616 | my $clean = 'X' x length($1); |
| 1617 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
| 1618 | } |
| 1619 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1620 | if ($allow_c99_comments && $res =~ m@(//.*$)@) { |
| 1621 | my $match = $1; |
| 1622 | $res =~ s/\Q$match\E/"$;" x length($match)/e; |
| 1623 | } |
| 1624 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1625 | return $res; |
| 1626 | } |
| 1627 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1628 | sub get_quoted_string { |
| 1629 | my ($line, $rawline) = @_; |
| 1630 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 1631 | return "" if (!defined($line) || !defined($rawline)); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1632 | return "" if ($line !~ m/($String)/g); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1633 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 1634 | } |
| 1635 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1636 | sub ctx_statement_block { |
| 1637 | my ($linenr, $remain, $off) = @_; |
| 1638 | my $line = $linenr - 1; |
| 1639 | my $blk = ''; |
| 1640 | my $soff = $off; |
| 1641 | my $coff = $off - 1; |
| 1642 | my $coff_set = 0; |
| 1643 | |
| 1644 | my $loff = 0; |
| 1645 | |
| 1646 | my $type = ''; |
| 1647 | my $level = 0; |
| 1648 | my @stack = (); |
| 1649 | my $p; |
| 1650 | my $c; |
| 1651 | my $len = 0; |
| 1652 | |
| 1653 | my $remainder; |
| 1654 | while (1) { |
| 1655 | @stack = (['', 0]) if ($#stack == -1); |
| 1656 | |
| 1657 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
| 1658 | # If we are about to drop off the end, pull in more |
| 1659 | # context. |
| 1660 | if ($off >= $len) { |
| 1661 | for (; $remain > 0; $line++) { |
| 1662 | last if (!defined $lines[$line]); |
| 1663 | next if ($lines[$line] =~ /^-/); |
| 1664 | $remain--; |
| 1665 | $loff = $len; |
| 1666 | $blk .= $lines[$line] . "\n"; |
| 1667 | $len = length($blk); |
| 1668 | $line++; |
| 1669 | last; |
| 1670 | } |
| 1671 | # Bail if there is no further context. |
| 1672 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
| 1673 | if ($off >= $len) { |
| 1674 | last; |
| 1675 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 1676 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 1677 | $level++; |
| 1678 | $type = '#'; |
| 1679 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1680 | } |
| 1681 | $p = $c; |
| 1682 | $c = substr($blk, $off, 1); |
| 1683 | $remainder = substr($blk, $off); |
| 1684 | |
| 1685 | #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; |
| 1686 | |
| 1687 | # Handle nested #if/#else. |
| 1688 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 1689 | push(@stack, [ $type, $level ]); |
| 1690 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 1691 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 1692 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 1693 | ($type, $level) = @{pop(@stack)}; |
| 1694 | } |
| 1695 | |
| 1696 | # Statement ends at the ';' or a close '}' at the |
| 1697 | # outermost level. |
| 1698 | if ($level == 0 && $c eq ';') { |
| 1699 | last; |
| 1700 | } |
| 1701 | |
| 1702 | # An else is really a conditional as long as its not else if |
| 1703 | if ($level == 0 && $coff_set == 0 && |
| 1704 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 1705 | $remainder =~ /^(else)(?:\s|{)/ && |
| 1706 | $remainder !~ /^else\s+if\b/) { |
| 1707 | $coff = $off + length($1) - 1; |
| 1708 | $coff_set = 1; |
| 1709 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 1710 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
| 1711 | } |
| 1712 | |
| 1713 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 1714 | $level++; |
| 1715 | $type = '('; |
| 1716 | } |
| 1717 | if ($type eq '(' && $c eq ')') { |
| 1718 | $level--; |
| 1719 | $type = ($level != 0)? '(' : ''; |
| 1720 | |
| 1721 | if ($level == 0 && $coff < $soff) { |
| 1722 | $coff = $off; |
| 1723 | $coff_set = 1; |
| 1724 | #warn "CSB: mark coff<$coff>\n"; |
| 1725 | } |
| 1726 | } |
| 1727 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 1728 | $level++; |
| 1729 | $type = '{'; |
| 1730 | } |
| 1731 | if ($type eq '{' && $c eq '}') { |
| 1732 | $level--; |
| 1733 | $type = ($level != 0)? '{' : ''; |
| 1734 | |
| 1735 | if ($level == 0) { |
| 1736 | if (substr($blk, $off + 1, 1) eq ';') { |
| 1737 | $off++; |
| 1738 | } |
| 1739 | last; |
| 1740 | } |
| 1741 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 1742 | # Preprocessor commands end at the newline unless escaped. |
| 1743 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 1744 | $level--; |
| 1745 | $type = ''; |
| 1746 | $off++; |
| 1747 | last; |
| 1748 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1749 | $off++; |
| 1750 | } |
| 1751 | # We are truly at the end, so shuffle to the next line. |
| 1752 | if ($off == $len) { |
| 1753 | $loff = $len + 1; |
| 1754 | $line++; |
| 1755 | $remain--; |
| 1756 | } |
| 1757 | |
| 1758 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 1759 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 1760 | |
| 1761 | #warn "STATEMENT<$statement>\n"; |
| 1762 | #warn "CONDITION<$condition>\n"; |
| 1763 | |
| 1764 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
| 1765 | |
| 1766 | return ($statement, $condition, |
| 1767 | $line, $remain + 1, $off - $loff + 1, $level); |
| 1768 | } |
| 1769 | |
| 1770 | sub statement_lines { |
| 1771 | my ($stmt) = @_; |
| 1772 | |
| 1773 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 1774 | $stmt =~ s/(^|\n)./$1/g; |
| 1775 | $stmt =~ s/^\s*//; |
| 1776 | $stmt =~ s/\s*$//; |
| 1777 | |
| 1778 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1779 | |
| 1780 | return $#stmt_lines + 2; |
| 1781 | } |
| 1782 | |
| 1783 | sub statement_rawlines { |
| 1784 | my ($stmt) = @_; |
| 1785 | |
| 1786 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1787 | |
| 1788 | return $#stmt_lines + 2; |
| 1789 | } |
| 1790 | |
| 1791 | sub statement_block_size { |
| 1792 | my ($stmt) = @_; |
| 1793 | |
| 1794 | $stmt =~ s/(^|\n)./$1/g; |
| 1795 | $stmt =~ s/^\s*{//; |
| 1796 | $stmt =~ s/}\s*$//; |
| 1797 | $stmt =~ s/^\s*//; |
| 1798 | $stmt =~ s/\s*$//; |
| 1799 | |
| 1800 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1801 | my @stmt_statements = ($stmt =~ /;/g); |
| 1802 | |
| 1803 | my $stmt_lines = $#stmt_lines + 2; |
| 1804 | my $stmt_statements = $#stmt_statements + 1; |
| 1805 | |
| 1806 | if ($stmt_lines > $stmt_statements) { |
| 1807 | return $stmt_lines; |
| 1808 | } else { |
| 1809 | return $stmt_statements; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | sub ctx_statement_full { |
| 1814 | my ($linenr, $remain, $off) = @_; |
| 1815 | my ($statement, $condition, $level); |
| 1816 | |
| 1817 | my (@chunks); |
| 1818 | |
| 1819 | # Grab the first conditional/block pair. |
| 1820 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1821 | ctx_statement_block($linenr, $remain, $off); |
| 1822 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
| 1823 | push(@chunks, [ $condition, $statement ]); |
| 1824 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 1825 | return ($level, $linenr, @chunks); |
| 1826 | } |
| 1827 | |
| 1828 | # Pull in the following conditional/block pairs and see if they |
| 1829 | # could continue the statement. |
| 1830 | for (;;) { |
| 1831 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1832 | ctx_statement_block($linenr, $remain, $off); |
| 1833 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
| 1834 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
| 1835 | #print "C: push\n"; |
| 1836 | push(@chunks, [ $condition, $statement ]); |
| 1837 | } |
| 1838 | |
| 1839 | return ($level, $linenr, @chunks); |
| 1840 | } |
| 1841 | |
| 1842 | sub ctx_block_get { |
| 1843 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
| 1844 | my $line; |
| 1845 | my $start = $linenr - 1; |
| 1846 | my $blk = ''; |
| 1847 | my @o; |
| 1848 | my @c; |
| 1849 | my @res = (); |
| 1850 | |
| 1851 | my $level = 0; |
| 1852 | my @stack = ($level); |
| 1853 | for ($line = $start; $remain > 0; $line++) { |
| 1854 | next if ($rawlines[$line] =~ /^-/); |
| 1855 | $remain--; |
| 1856 | |
| 1857 | $blk .= $rawlines[$line]; |
| 1858 | |
| 1859 | # Handle nested #if/#else. |
| 1860 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
| 1861 | push(@stack, $level); |
| 1862 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
| 1863 | $level = $stack[$#stack - 1]; |
| 1864 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
| 1865 | $level = pop(@stack); |
| 1866 | } |
| 1867 | |
| 1868 | foreach my $c (split(//, $lines[$line])) { |
| 1869 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 1870 | if ($off > 0) { |
| 1871 | $off--; |
| 1872 | next; |
| 1873 | } |
| 1874 | |
| 1875 | if ($c eq $close && $level > 0) { |
| 1876 | $level--; |
| 1877 | last if ($level == 0); |
| 1878 | } elsif ($c eq $open) { |
| 1879 | $level++; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | if (!$outer || $level <= 1) { |
| 1884 | push(@res, $rawlines[$line]); |
| 1885 | } |
| 1886 | |
| 1887 | last if ($level == 0); |
| 1888 | } |
| 1889 | |
| 1890 | return ($level, @res); |
| 1891 | } |
| 1892 | sub ctx_block_outer { |
| 1893 | my ($linenr, $remain) = @_; |
| 1894 | |
| 1895 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 1896 | return @r; |
| 1897 | } |
| 1898 | sub ctx_block { |
| 1899 | my ($linenr, $remain) = @_; |
| 1900 | |
| 1901 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1902 | return @r; |
| 1903 | } |
| 1904 | sub ctx_statement { |
| 1905 | my ($linenr, $remain, $off) = @_; |
| 1906 | |
| 1907 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1908 | return @r; |
| 1909 | } |
| 1910 | sub ctx_block_level { |
| 1911 | my ($linenr, $remain) = @_; |
| 1912 | |
| 1913 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1914 | } |
| 1915 | sub ctx_statement_level { |
| 1916 | my ($linenr, $remain, $off) = @_; |
| 1917 | |
| 1918 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1919 | } |
| 1920 | |
| 1921 | sub ctx_locate_comment { |
| 1922 | my ($first_line, $end_line) = @_; |
| 1923 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1924 | # If c99 comment on the current line, or the line before or after |
| 1925 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@); |
| 1926 | return $current_comment if (defined $current_comment); |
| 1927 | ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@); |
| 1928 | return $current_comment if (defined $current_comment); |
| 1929 | ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@); |
| 1930 | return $current_comment if (defined $current_comment); |
| 1931 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1932 | # Catch a comment on the end of the line itself. |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 1933 | ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1934 | return $current_comment if (defined $current_comment); |
| 1935 | |
| 1936 | # Look through the context and try and figure out if there is a |
| 1937 | # comment. |
| 1938 | my $in_comment = 0; |
| 1939 | $current_comment = ''; |
| 1940 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
| 1941 | my $line = $rawlines[$linenr - 1]; |
| 1942 | #warn " $line\n"; |
| 1943 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 1944 | $in_comment = 1; |
| 1945 | } |
| 1946 | if ($line =~ m@/\*@) { |
| 1947 | $in_comment = 1; |
| 1948 | } |
| 1949 | if (!$in_comment && $current_comment ne '') { |
| 1950 | $current_comment = ''; |
| 1951 | } |
| 1952 | $current_comment .= $line . "\n" if ($in_comment); |
| 1953 | if ($line =~ m@\*/@) { |
| 1954 | $in_comment = 0; |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | chomp($current_comment); |
| 1959 | return($current_comment); |
| 1960 | } |
| 1961 | sub ctx_has_comment { |
| 1962 | my ($first_line, $end_line) = @_; |
| 1963 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 1964 | |
| 1965 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
| 1966 | ##print "CMMT: $cmt\n"; |
| 1967 | |
| 1968 | return ($cmt ne ''); |
| 1969 | } |
| 1970 | |
| 1971 | sub raw_line { |
| 1972 | my ($linenr, $cnt) = @_; |
| 1973 | |
| 1974 | my $offset = $linenr - 1; |
| 1975 | $cnt++; |
| 1976 | |
| 1977 | my $line; |
| 1978 | while ($cnt) { |
| 1979 | $line = $rawlines[$offset++]; |
| 1980 | next if (defined($line) && $line =~ /^-/); |
| 1981 | $cnt--; |
| 1982 | } |
| 1983 | |
| 1984 | return $line; |
| 1985 | } |
| 1986 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 1987 | sub get_stat_real { |
| 1988 | my ($linenr, $lc) = @_; |
| 1989 | |
| 1990 | my $stat_real = raw_line($linenr, 0); |
| 1991 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 1992 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 1993 | } |
| 1994 | |
| 1995 | return $stat_real; |
| 1996 | } |
| 1997 | |
| 1998 | sub get_stat_here { |
| 1999 | my ($linenr, $cnt, $here) = @_; |
| 2000 | |
| 2001 | my $herectx = $here . "\n"; |
| 2002 | for (my $n = 0; $n < $cnt; $n++) { |
| 2003 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 2004 | } |
| 2005 | |
| 2006 | return $herectx; |
| 2007 | } |
| 2008 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2009 | sub cat_vet { |
| 2010 | my ($vet) = @_; |
| 2011 | my ($res, $coded); |
| 2012 | |
| 2013 | $res = ''; |
| 2014 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 2015 | $res .= $1; |
| 2016 | if ($2 ne '') { |
| 2017 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 2018 | $res .= $coded; |
| 2019 | } |
| 2020 | } |
| 2021 | $res =~ s/$/\$/; |
| 2022 | |
| 2023 | return $res; |
| 2024 | } |
| 2025 | |
| 2026 | my $av_preprocessor = 0; |
| 2027 | my $av_pending; |
| 2028 | my @av_paren_type; |
| 2029 | my $av_pend_colon; |
| 2030 | |
| 2031 | sub annotate_reset { |
| 2032 | $av_preprocessor = 0; |
| 2033 | $av_pending = '_'; |
| 2034 | @av_paren_type = ('E'); |
| 2035 | $av_pend_colon = 'O'; |
| 2036 | } |
| 2037 | |
| 2038 | sub annotate_values { |
| 2039 | my ($stream, $type) = @_; |
| 2040 | |
| 2041 | my $res; |
| 2042 | my $var = '_' x length($stream); |
| 2043 | my $cur = $stream; |
| 2044 | |
| 2045 | print "$stream\n" if ($dbg_values > 1); |
| 2046 | |
| 2047 | while (length($cur)) { |
| 2048 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
| 2049 | print " <" . join('', @av_paren_type) . |
| 2050 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
| 2051 | if ($cur =~ /^(\s+)/o) { |
| 2052 | print "WS($1)\n" if ($dbg_values > 1); |
| 2053 | if ($1 =~ /\n/ && $av_preprocessor) { |
| 2054 | $type = pop(@av_paren_type); |
| 2055 | $av_preprocessor = 0; |
| 2056 | } |
| 2057 | |
| 2058 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
| 2059 | print "CAST($1)\n" if ($dbg_values > 1); |
| 2060 | push(@av_paren_type, $type); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2061 | $type = 'c'; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2062 | |
| 2063 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
| 2064 | print "DECLARE($1)\n" if ($dbg_values > 1); |
| 2065 | $type = 'T'; |
| 2066 | |
| 2067 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 2068 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 2069 | $type = 'T'; |
| 2070 | |
| 2071 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
| 2072 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
| 2073 | $av_preprocessor = 1; |
| 2074 | push(@av_paren_type, $type); |
| 2075 | if ($2 ne '') { |
| 2076 | $av_pending = 'N'; |
| 2077 | } |
| 2078 | $type = 'E'; |
| 2079 | |
| 2080 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
| 2081 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 2082 | $av_preprocessor = 1; |
| 2083 | push(@av_paren_type, $type); |
| 2084 | |
| 2085 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
| 2086 | print "PRE_START($1)\n" if ($dbg_values > 1); |
| 2087 | $av_preprocessor = 1; |
| 2088 | |
| 2089 | push(@av_paren_type, $type); |
| 2090 | push(@av_paren_type, $type); |
| 2091 | $type = 'E'; |
| 2092 | |
| 2093 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
| 2094 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 2095 | $av_preprocessor = 1; |
| 2096 | |
| 2097 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 2098 | |
| 2099 | $type = 'E'; |
| 2100 | |
| 2101 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
| 2102 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 2103 | |
| 2104 | $av_preprocessor = 1; |
| 2105 | |
| 2106 | # Assume all arms of the conditional end as this |
| 2107 | # one does, and continue as if the #endif was not here. |
| 2108 | pop(@av_paren_type); |
| 2109 | push(@av_paren_type, $type); |
| 2110 | $type = 'E'; |
| 2111 | |
| 2112 | } elsif ($cur =~ /^(\\\n)/o) { |
| 2113 | print "PRECONT($1)\n" if ($dbg_values > 1); |
| 2114 | |
| 2115 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 2116 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 2117 | $av_pending = $type; |
| 2118 | $type = 'N'; |
| 2119 | |
| 2120 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
| 2121 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
| 2122 | if (defined $2) { |
| 2123 | $av_pending = 'V'; |
| 2124 | } |
| 2125 | $type = 'N'; |
| 2126 | |
| 2127 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
| 2128 | print "COND($1)\n" if ($dbg_values > 1); |
| 2129 | $av_pending = 'E'; |
| 2130 | $type = 'N'; |
| 2131 | |
| 2132 | } elsif ($cur =~/^(case)/o) { |
| 2133 | print "CASE($1)\n" if ($dbg_values > 1); |
| 2134 | $av_pend_colon = 'C'; |
| 2135 | $type = 'N'; |
| 2136 | |
| 2137 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
| 2138 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
| 2139 | $type = 'N'; |
| 2140 | |
| 2141 | } elsif ($cur =~ /^(\()/o) { |
| 2142 | print "PAREN('$1')\n" if ($dbg_values > 1); |
| 2143 | push(@av_paren_type, $av_pending); |
| 2144 | $av_pending = '_'; |
| 2145 | $type = 'N'; |
| 2146 | |
| 2147 | } elsif ($cur =~ /^(\))/o) { |
| 2148 | my $new_type = pop(@av_paren_type); |
| 2149 | if ($new_type ne '_') { |
| 2150 | $type = $new_type; |
| 2151 | print "PAREN('$1') -> $type\n" |
| 2152 | if ($dbg_values > 1); |
| 2153 | } else { |
| 2154 | print "PAREN('$1')\n" if ($dbg_values > 1); |
| 2155 | } |
| 2156 | |
| 2157 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
| 2158 | print "FUNC($1)\n" if ($dbg_values > 1); |
| 2159 | $type = 'V'; |
| 2160 | $av_pending = 'V'; |
| 2161 | |
| 2162 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 2163 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
| 2164 | $av_pend_colon = 'B'; |
| 2165 | } elsif ($type eq 'E') { |
| 2166 | $av_pend_colon = 'L'; |
| 2167 | } |
| 2168 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 2169 | $type = 'V'; |
| 2170 | |
| 2171 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
| 2172 | print "IDENT($1)\n" if ($dbg_values > 1); |
| 2173 | $type = 'V'; |
| 2174 | |
| 2175 | } elsif ($cur =~ /^($Assignment)/o) { |
| 2176 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
| 2177 | $type = 'N'; |
| 2178 | |
| 2179 | } elsif ($cur =~/^(;|{|})/) { |
| 2180 | print "END($1)\n" if ($dbg_values > 1); |
| 2181 | $type = 'E'; |
| 2182 | $av_pend_colon = 'O'; |
| 2183 | |
| 2184 | } elsif ($cur =~/^(,)/) { |
| 2185 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 2186 | $type = 'C'; |
| 2187 | |
| 2188 | } elsif ($cur =~ /^(\?)/o) { |
| 2189 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 2190 | $type = 'N'; |
| 2191 | |
| 2192 | } elsif ($cur =~ /^(:)/o) { |
| 2193 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 2194 | |
| 2195 | substr($var, length($res), 1, $av_pend_colon); |
| 2196 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 2197 | $type = 'E'; |
| 2198 | } else { |
| 2199 | $type = 'N'; |
| 2200 | } |
| 2201 | $av_pend_colon = 'O'; |
| 2202 | |
| 2203 | } elsif ($cur =~ /^(\[)/o) { |
| 2204 | print "CLOSE($1)\n" if ($dbg_values > 1); |
| 2205 | $type = 'N'; |
| 2206 | |
| 2207 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
| 2208 | my $variant; |
| 2209 | |
| 2210 | print "OPV($1)\n" if ($dbg_values > 1); |
| 2211 | if ($type eq 'V') { |
| 2212 | $variant = 'B'; |
| 2213 | } else { |
| 2214 | $variant = 'U'; |
| 2215 | } |
| 2216 | |
| 2217 | substr($var, length($res), 1, $variant); |
| 2218 | $type = 'N'; |
| 2219 | |
| 2220 | } elsif ($cur =~ /^($Operators)/o) { |
| 2221 | print "OP($1)\n" if ($dbg_values > 1); |
| 2222 | if ($1 ne '++' && $1 ne '--') { |
| 2223 | $type = 'N'; |
| 2224 | } |
| 2225 | |
| 2226 | } elsif ($cur =~ /(^.)/o) { |
| 2227 | print "C($1)\n" if ($dbg_values > 1); |
| 2228 | } |
| 2229 | if (defined $1) { |
| 2230 | $cur = substr($cur, length($1)); |
| 2231 | $res .= $type x length($1); |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | return ($res, $var); |
| 2236 | } |
| 2237 | |
| 2238 | sub possible { |
| 2239 | my ($possible, $line) = @_; |
| 2240 | my $notPermitted = qr{(?: |
| 2241 | ^(?: |
| 2242 | $Modifier| |
| 2243 | $Storage| |
| 2244 | $Type| |
| 2245 | DEFINE_\S+ |
| 2246 | )$| |
| 2247 | ^(?: |
| 2248 | goto| |
| 2249 | return| |
| 2250 | case| |
| 2251 | else| |
| 2252 | asm|__asm__| |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2253 | do| |
| 2254 | \#| |
| 2255 | \#\#| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2256 | )(?:\s|$)| |
| 2257 | ^(?:typedef|struct|enum)\b |
| 2258 | )}x; |
| 2259 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 2260 | if ($possible !~ $notPermitted) { |
| 2261 | # Check for modifiers. |
| 2262 | $possible =~ s/\s*$Storage\s*//g; |
| 2263 | $possible =~ s/\s*$Sparse\s*//g; |
| 2264 | if ($possible =~ /^\s*$/) { |
| 2265 | |
| 2266 | } elsif ($possible =~ /\s/) { |
| 2267 | $possible =~ s/\s*$Type\s*//g; |
| 2268 | for my $modifier (split(' ', $possible)) { |
| 2269 | if ($modifier !~ $notPermitted) { |
| 2270 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2271 | push(@modifierListFile, $modifier); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | } else { |
| 2276 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2277 | push(@typeListFile, $possible); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2278 | } |
| 2279 | build_types(); |
| 2280 | } else { |
| 2281 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
| 2282 | } |
| 2283 | } |
| 2284 | |
| 2285 | my $prefix = ''; |
| 2286 | |
| 2287 | sub show_type { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2288 | my ($type) = @_; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2289 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2290 | $type =~ tr/[a-z]/[A-Z]/; |
| 2291 | |
| 2292 | return defined $use_type{$type} if (scalar keys %use_type > 0); |
| 2293 | |
| 2294 | return !defined $ignore_type{$type}; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | sub report { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2298 | my ($level, $type, $msg) = @_; |
| 2299 | |
| 2300 | if (!show_type($type) || |
| 2301 | (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2302 | return 0; |
| 2303 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2304 | my $output = ''; |
| 2305 | if ($color) { |
| 2306 | if ($level eq 'ERROR') { |
| 2307 | $output .= RED; |
| 2308 | } elsif ($level eq 'WARNING') { |
| 2309 | $output .= YELLOW; |
| 2310 | } else { |
| 2311 | $output .= GREEN; |
| 2312 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2313 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2314 | $output .= $prefix . $level . ':'; |
| 2315 | if ($show_types) { |
| 2316 | $output .= BLUE if ($color); |
| 2317 | $output .= "$type:"; |
| 2318 | } |
| 2319 | $output .= RESET if ($color); |
| 2320 | $output .= ' ' . $msg . "\n"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2321 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2322 | if ($showfile) { |
| 2323 | my @lines = split("\n", $output, -1); |
| 2324 | splice(@lines, 1, 1); |
| 2325 | $output = join("\n", @lines); |
| 2326 | } |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 2327 | |
| 2328 | if ($terse) { |
| 2329 | $output = (split('\n', $output))[0] . "\n"; |
| 2330 | } |
| 2331 | |
| 2332 | if ($verbose && exists($verbose_messages{$type}) && |
| 2333 | !exists($verbose_emitted{$type})) { |
| 2334 | $output .= $verbose_messages{$type} . "\n\n"; |
| 2335 | $verbose_emitted{$type} = 1; |
| 2336 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2337 | |
| 2338 | push(our @report, $output); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2339 | |
| 2340 | return 1; |
| 2341 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2342 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2343 | sub report_dump { |
| 2344 | our @report; |
| 2345 | } |
| 2346 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2347 | sub fixup_current_range { |
| 2348 | my ($lineRef, $offset, $length) = @_; |
| 2349 | |
| 2350 | if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { |
| 2351 | my $o = $1; |
| 2352 | my $l = $2; |
| 2353 | my $no = $o + $offset; |
| 2354 | my $nl = $l + $length; |
| 2355 | $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | sub fix_inserted_deleted_lines { |
| 2360 | my ($linesRef, $insertedRef, $deletedRef) = @_; |
| 2361 | |
| 2362 | my $range_last_linenr = 0; |
| 2363 | my $delta_offset = 0; |
| 2364 | |
| 2365 | my $old_linenr = 0; |
| 2366 | my $new_linenr = 0; |
| 2367 | |
| 2368 | my $next_insert = 0; |
| 2369 | my $next_delete = 0; |
| 2370 | |
| 2371 | my @lines = (); |
| 2372 | |
| 2373 | my $inserted = @{$insertedRef}[$next_insert++]; |
| 2374 | my $deleted = @{$deletedRef}[$next_delete++]; |
| 2375 | |
| 2376 | foreach my $old_line (@{$linesRef}) { |
| 2377 | my $save_line = 1; |
| 2378 | my $line = $old_line; #don't modify the array |
| 2379 | if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename |
| 2380 | $delta_offset = 0; |
| 2381 | } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk |
| 2382 | $range_last_linenr = $new_linenr; |
| 2383 | fixup_current_range(\$line, $delta_offset, 0); |
| 2384 | } |
| 2385 | |
| 2386 | while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { |
| 2387 | $deleted = @{$deletedRef}[$next_delete++]; |
| 2388 | $save_line = 0; |
| 2389 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); |
| 2390 | } |
| 2391 | |
| 2392 | while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { |
| 2393 | push(@lines, ${$inserted}{'LINE'}); |
| 2394 | $inserted = @{$insertedRef}[$next_insert++]; |
| 2395 | $new_linenr++; |
| 2396 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); |
| 2397 | } |
| 2398 | |
| 2399 | if ($save_line) { |
| 2400 | push(@lines, $line); |
| 2401 | $new_linenr++; |
| 2402 | } |
| 2403 | |
| 2404 | $old_linenr++; |
| 2405 | } |
| 2406 | |
| 2407 | return @lines; |
| 2408 | } |
| 2409 | |
| 2410 | sub fix_insert_line { |
| 2411 | my ($linenr, $line) = @_; |
| 2412 | |
| 2413 | my $inserted = { |
| 2414 | LINENR => $linenr, |
| 2415 | LINE => $line, |
| 2416 | }; |
| 2417 | push(@fixed_inserted, $inserted); |
| 2418 | } |
| 2419 | |
| 2420 | sub fix_delete_line { |
| 2421 | my ($linenr, $line) = @_; |
| 2422 | |
| 2423 | my $deleted = { |
| 2424 | LINENR => $linenr, |
| 2425 | LINE => $line, |
| 2426 | }; |
| 2427 | |
| 2428 | push(@fixed_deleted, $deleted); |
| 2429 | } |
| 2430 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2431 | sub ERROR { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2432 | my ($type, $msg) = @_; |
| 2433 | |
| 2434 | if (report("ERROR", $type, $msg)) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2435 | our $clean = 0; |
| 2436 | our $cnt_error++; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2437 | return 1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2438 | } |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2439 | return 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2440 | } |
| 2441 | sub WARN { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2442 | my ($type, $msg) = @_; |
| 2443 | |
| 2444 | if (report("WARNING", $type, $msg)) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2445 | our $clean = 0; |
| 2446 | our $cnt_warn++; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2447 | return 1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2448 | } |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2449 | return 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2450 | } |
| 2451 | sub CHK { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2452 | my ($type, $msg) = @_; |
| 2453 | |
| 2454 | if ($check && report("CHECK", $type, $msg)) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2455 | our $clean = 0; |
| 2456 | our $cnt_chk++; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2457 | return 1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2458 | } |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2459 | return 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | sub check_absolute_file { |
| 2463 | my ($absolute, $herecurr) = @_; |
| 2464 | my $file = $absolute; |
| 2465 | |
| 2466 | ##print "absolute<$absolute>\n"; |
| 2467 | |
| 2468 | # See if any suffix of this path is a path within the tree. |
| 2469 | while ($file =~ s@^[^/]*/@@) { |
| 2470 | if (-f "$root/$file") { |
| 2471 | ##print "file<$file>\n"; |
| 2472 | last; |
| 2473 | } |
| 2474 | } |
| 2475 | if (! -f _) { |
| 2476 | return 0; |
| 2477 | } |
| 2478 | |
| 2479 | # It is, so see if the prefix is acceptable. |
| 2480 | my $prefix = $absolute; |
| 2481 | substr($prefix, -length($file)) = ''; |
| 2482 | |
| 2483 | ##print "prefix<$prefix>\n"; |
| 2484 | if ($prefix ne ".../") { |
| 2485 | WARN("USE_RELATIVE_PATH", |
| 2486 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
| 2487 | } |
| 2488 | } |
| 2489 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2490 | sub trim { |
| 2491 | my ($string) = @_; |
| 2492 | |
| 2493 | $string =~ s/^\s+|\s+$//g; |
| 2494 | |
| 2495 | return $string; |
| 2496 | } |
| 2497 | |
| 2498 | sub ltrim { |
| 2499 | my ($string) = @_; |
| 2500 | |
| 2501 | $string =~ s/^\s+//; |
| 2502 | |
| 2503 | return $string; |
| 2504 | } |
| 2505 | |
| 2506 | sub rtrim { |
| 2507 | my ($string) = @_; |
| 2508 | |
| 2509 | $string =~ s/\s+$//; |
| 2510 | |
| 2511 | return $string; |
| 2512 | } |
| 2513 | |
| 2514 | sub string_find_replace { |
| 2515 | my ($string, $find, $replace) = @_; |
| 2516 | |
| 2517 | $string =~ s/$find/$replace/g; |
| 2518 | |
| 2519 | return $string; |
| 2520 | } |
| 2521 | |
| 2522 | sub tabify { |
| 2523 | my ($leading) = @_; |
| 2524 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2525 | my $source_indent = $tabsize; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2526 | my $max_spaces_before_tab = $source_indent - 1; |
| 2527 | my $spaces_to_tab = " " x $source_indent; |
| 2528 | |
| 2529 | #convert leading spaces to tabs |
| 2530 | 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; |
| 2531 | #Remove spaces before a tab |
| 2532 | 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; |
| 2533 | |
| 2534 | return "$leading"; |
| 2535 | } |
| 2536 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2537 | sub pos_last_openparen { |
| 2538 | my ($line) = @_; |
| 2539 | |
| 2540 | my $pos = 0; |
| 2541 | |
| 2542 | my $opens = $line =~ tr/\(/\(/; |
| 2543 | my $closes = $line =~ tr/\)/\)/; |
| 2544 | |
| 2545 | my $last_openparen = 0; |
| 2546 | |
| 2547 | if (($opens == 0) || ($closes >= $opens)) { |
| 2548 | return -1; |
| 2549 | } |
| 2550 | |
| 2551 | my $len = length($line); |
| 2552 | |
| 2553 | for ($pos = 0; $pos < $len; $pos++) { |
| 2554 | my $string = substr($line, $pos); |
| 2555 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 2556 | $pos += length($1) - 1; |
| 2557 | } elsif (substr($line, $pos, 1) eq '(') { |
| 2558 | $last_openparen = $pos; |
| 2559 | } elsif (index($string, '(') == -1) { |
| 2560 | last; |
| 2561 | } |
| 2562 | } |
| 2563 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2564 | return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2567 | sub get_raw_comment { |
| 2568 | my ($line, $rawline) = @_; |
| 2569 | my $comment = ''; |
| 2570 | |
| 2571 | for my $i (0 .. (length($line) - 1)) { |
| 2572 | if (substr($line, $i, 1) eq "$;") { |
| 2573 | $comment .= substr($rawline, $i, 1); |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | return $comment; |
| 2578 | } |
| 2579 | |
Simon Glass | b7bbd55 | 2020-12-03 16:55:24 -0700 | [diff] [blame] | 2580 | # Args: |
| 2581 | # line: Patch line to check |
| 2582 | # auto: Auto variable name, e.g. "per_child_auto" |
| 2583 | # suffix: Suffix to expect on member, e.g. "_priv" |
| 2584 | # warning: Warning name, e.g. "PRIV_AUTO" |
| 2585 | sub u_boot_struct_name { |
Evan Benn | ec6db6c | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 2586 | my ($line, $auto, $suffix, $warning, $herecurr) = @_; |
Simon Glass | b7bbd55 | 2020-12-03 16:55:24 -0700 | [diff] [blame] | 2587 | |
| 2588 | # Use _priv as a suffix for the device-private data struct |
| 2589 | if ($line =~ /^\+\s*\.${auto}\s*=\s*sizeof\(struct\((\w+)\).*/) { |
| 2590 | my $struct_name = $1; |
| 2591 | if ($struct_name !~ /^\w+${suffix}/) { |
Evan Benn | ec6db6c | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 2592 | WARN($warning, |
| 2593 | "struct \'$struct_name\' should have a ${suffix} suffix\n" |
| 2594 | . $herecurr); |
Simon Glass | b7bbd55 | 2020-12-03 16:55:24 -0700 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | } |
| 2598 | |
Simon Glass | b77df59 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 2599 | # Checks specific to U-Boot |
| 2600 | sub u_boot_line { |
Simon Glass | 23552ba | 2020-07-19 10:16:01 -0600 | [diff] [blame] | 2601 | my ($realfile, $line, $rawline, $herecurr) = @_; |
Simon Glass | 281236c | 2020-05-22 16:32:37 -0600 | [diff] [blame] | 2602 | |
| 2603 | # ask for a test if a new uclass ID is added |
| 2604 | if ($realfile =~ /uclass-id.h/ && $line =~ /^\+/) { |
| 2605 | WARN("NEW_UCLASS", |
| 2606 | "Possible new uclass - make sure to add a sandbox driver, plus a test in test/dm/<name>.c\n" . $herecurr); |
| 2607 | } |
Simon Glass | 7fc7d24 | 2020-05-22 16:32:38 -0600 | [diff] [blame] | 2608 | |
| 2609 | # try to get people to use the livetree API |
| 2610 | if ($line =~ /^\+.*fdtdec_/) { |
| 2611 | WARN("LIVETREE", |
| 2612 | "Use the livetree API (dev_read_...)\n" . $herecurr); |
| 2613 | } |
Simon Glass | 5897811 | 2020-05-22 16:32:39 -0600 | [diff] [blame] | 2614 | |
| 2615 | # add tests for new commands |
| 2616 | if ($line =~ /^\+.*do_($Ident)\(struct cmd_tbl.*/) { |
| 2617 | WARN("CMD_TEST", |
| 2618 | "Possible new command - make sure you add a test\n" . $herecurr); |
| 2619 | } |
Simon Glass | dd5b0fa | 2020-05-22 16:32:40 -0600 | [diff] [blame] | 2620 | |
| 2621 | # use if instead of #if |
Simon Glass | 8af45b1 | 2020-06-14 10:54:08 -0600 | [diff] [blame] | 2622 | if ($realfile =~ /\.c$/ && $line =~ /^\+#if.*CONFIG.*/) { |
Simon Glass | dd5b0fa | 2020-05-22 16:32:40 -0600 | [diff] [blame] | 2623 | WARN("PREFER_IF", |
| 2624 | "Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef' where possible\n" . $herecurr); |
| 2625 | } |
Tom Rini | f3e2ebe | 2020-05-26 14:29:02 -0400 | [diff] [blame] | 2626 | |
Sean Anderson | d9c3050 | 2021-03-11 00:15:45 -0500 | [diff] [blame] | 2627 | # prefer strl(cpy|cat) over strn(cpy|cat) |
| 2628 | if ($line =~ /\bstrn(cpy|cat)\s*\(/) { |
| 2629 | WARN("STRL", |
| 2630 | "strl$1 is preferred over strn$1 because it always produces a nul-terminated string\n" . $herecurr); |
| 2631 | } |
| 2632 | |
Tom Rini | 2a06da0 | 2022-12-04 10:14:14 -0500 | [diff] [blame] | 2633 | # use Kconfig for all CONFIG symbols |
| 2634 | if ($line =~ /\+\s*#\s*(define|undef)\s+(CONFIG_\w*)\b/) { |
| 2635 | ERROR("DEFINE_CONFIG_SYM", |
| 2636 | "All CONFIG symbols are managed by Kconfig\n" . $herecurr); |
Tom Rini | f3e2ebe | 2020-05-26 14:29:02 -0400 | [diff] [blame] | 2637 | } |
Simon Glass | 23552ba | 2020-07-19 10:16:01 -0600 | [diff] [blame] | 2638 | |
| 2639 | # Don't put common.h and dm.h in header files |
| 2640 | if ($realfile =~ /\.h$/ && $rawline =~ /^\+#include\s*<(common|dm)\.h>*/) { |
| 2641 | ERROR("BARRED_INCLUDE_IN_HDR", |
| 2642 | "Avoid including common.h and dm.h in header files\n" . $herecurr); |
| 2643 | } |
Tom Rini | 12178b5 | 2020-08-20 08:37:49 -0400 | [diff] [blame] | 2644 | |
| 2645 | # Do not disable fdt / initrd relocation |
Hou Zhiqiang | 2f3e8d6 | 2021-12-15 14:23:52 +0800 | [diff] [blame] | 2646 | if ($rawline =~ /^\+.*(fdt|initrd)_high=0xffffffff/) { |
Tom Rini | 12178b5 | 2020-08-20 08:37:49 -0400 | [diff] [blame] | 2647 | ERROR("DISABLE_FDT_OR_INITRD_RELOC", |
| 2648 | "fdt or initrd relocation disabled at boot time\n" . $herecurr); |
| 2649 | } |
Alper Nebi Yasak | b9cca2c | 2020-10-05 09:57:30 +0300 | [diff] [blame] | 2650 | |
Wasim Khan | 402558b | 2021-02-04 15:44:04 +0100 | [diff] [blame] | 2651 | # make sure 'skip_board_fixup' is not |
| 2652 | if ($rawline =~ /.*skip_board_fixup.*/) { |
| 2653 | ERROR("SKIP_BOARD_FIXUP", |
| 2654 | "Avoid setting skip_board_fixup env variable\n" . $herecurr); |
| 2655 | } |
| 2656 | |
Alper Nebi Yasak | b9cca2c | 2020-10-05 09:57:30 +0300 | [diff] [blame] | 2657 | # Do not use CONFIG_ prefix in CONFIG_IS_ENABLED() calls |
| 2658 | if ($line =~ /^\+.*CONFIG_IS_ENABLED\(CONFIG_\w*\).*/) { |
| 2659 | ERROR("CONFIG_IS_ENABLED_CONFIG", |
| 2660 | "CONFIG_IS_ENABLED() takes values without the CONFIG_ prefix\n" . $herecurr); |
| 2661 | } |
Simon Glass | b7bbd55 | 2020-12-03 16:55:24 -0700 | [diff] [blame] | 2662 | |
| 2663 | # Use _priv as a suffix for the device-private data struct |
| 2664 | if ($line =~ /^\+\s*\.priv_auto\s*=\s*sizeof\(struct\((\w+)\).*/) { |
| 2665 | my $struct_name = $1; |
| 2666 | if ($struct_name !~ /^\w+_priv/) { |
| 2667 | WARN("PRIV_AUTO", "struct \'$struct_name\' should have a _priv suffix"); |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | # Check struct names for the 'auto' members of struct driver |
Evan Benn | ec6db6c | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 2672 | u_boot_struct_name($line, "priv_auto", "_priv", "PRIV_AUTO", $herecurr); |
| 2673 | u_boot_struct_name($line, "plat_auto", "_plat", "PLAT_AUTO", $herecurr); |
| 2674 | u_boot_struct_name($line, "per_child_auto", "_priv", "CHILD_PRIV_AUTO", $herecurr); |
Simon Glass | b7bbd55 | 2020-12-03 16:55:24 -0700 | [diff] [blame] | 2675 | u_boot_struct_name($line, "per_child_plat_auto", "_plat", |
Evan Benn | ec6db6c | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 2676 | "CHILD_PLAT_AUTO", $herecurr); |
Simon Glass | b7bbd55 | 2020-12-03 16:55:24 -0700 | [diff] [blame] | 2677 | |
| 2678 | # Now the ones for struct uclass, skipping those in common with above |
| 2679 | u_boot_struct_name($line, "per_device_auto", "_priv", |
Evan Benn | ec6db6c | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 2680 | "DEVICE_PRIV_AUTO", $herecurr); |
Simon Glass | b7bbd55 | 2020-12-03 16:55:24 -0700 | [diff] [blame] | 2681 | u_boot_struct_name($line, "per_device_plat_auto", "_plat", |
Evan Benn | ec6db6c | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 2682 | "DEVICE_PLAT_AUTO", $herecurr); |
Simon Glass | 48be546 | 2023-02-13 08:56:38 -0700 | [diff] [blame] | 2683 | |
| 2684 | # Avoid using the pre-schema driver model tags |
| 2685 | if ($line =~ /^\+.*u-boot,dm-.*/) { |
| 2686 | ERROR("PRE_SCHEMA", |
| 2687 | "Driver model schema uses 'bootph-...' tags now\n" . $herecurr); |
| 2688 | } |
Simon Glass | b77df59 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 2689 | } |
| 2690 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 2691 | sub exclude_global_initialisers { |
| 2692 | my ($realfile) = @_; |
| 2693 | |
| 2694 | # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c). |
| 2695 | return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ || |
| 2696 | $realfile =~ m@^samples/bpf/.*_kern\.c$@ || |
| 2697 | $realfile =~ m@/bpf/.*\.bpf\.c$@; |
| 2698 | } |
| 2699 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2700 | sub process { |
| 2701 | my $filename = shift; |
| 2702 | |
| 2703 | my $linenr=0; |
| 2704 | my $prevline=""; |
| 2705 | my $prevrawline=""; |
| 2706 | my $stashline=""; |
| 2707 | my $stashrawline=""; |
| 2708 | |
| 2709 | my $length; |
| 2710 | my $indent; |
| 2711 | my $previndent=0; |
| 2712 | my $stashindent=0; |
| 2713 | |
| 2714 | our $clean = 1; |
| 2715 | my $signoff = 0; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2716 | my $author = ''; |
| 2717 | my $authorsignoff = 0; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 2718 | my $author_sob = ''; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2719 | my $is_patch = 0; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2720 | my $is_binding_patch = -1; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2721 | my $in_header_lines = $file ? 0 : 1; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2722 | my $in_commit_log = 0; #Scanning lines before patch |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2723 | my $has_patch_separator = 0; #Found a --- line |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2724 | my $has_commit_log = 0; #Encountered lines before patch |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2725 | my $commit_log_lines = 0; #Number of commit log lines |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2726 | my $commit_log_possible_stack_dump = 0; |
| 2727 | my $commit_log_long_line = 0; |
| 2728 | my $commit_log_has_diff = 0; |
| 2729 | my $reported_maintainer_file = 0; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2730 | my $non_utf8_charset = 0; |
| 2731 | |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 2732 | my $last_git_commit_id_linenr = -1; |
| 2733 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2734 | my $last_blank_line = 0; |
| 2735 | my $last_coalesced_string_linenr = -1; |
| 2736 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2737 | our @report = (); |
| 2738 | our $cnt_lines = 0; |
| 2739 | our $cnt_error = 0; |
| 2740 | our $cnt_warn = 0; |
| 2741 | our $cnt_chk = 0; |
| 2742 | |
| 2743 | # Trace the real file/line as we go. |
| 2744 | my $realfile = ''; |
| 2745 | my $realline = 0; |
| 2746 | my $realcnt = 0; |
| 2747 | my $here = ''; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2748 | my $context_function; #undef'd unless there's a known function |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2749 | my $in_comment = 0; |
| 2750 | my $comment_edge = 0; |
| 2751 | my $first_line = 0; |
| 2752 | my $p1_prefix = ''; |
| 2753 | |
| 2754 | my $prev_values = 'E'; |
| 2755 | |
| 2756 | # suppression flags |
| 2757 | my %suppress_ifbraces; |
| 2758 | my %suppress_whiletrailers; |
| 2759 | my %suppress_export; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2760 | my $suppress_statement = 0; |
| 2761 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2762 | my %signatures = (); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2763 | |
| 2764 | # Pre-scan the patch sanitizing the lines. |
| 2765 | # Pre-scan the patch looking for any __setup documentation. |
| 2766 | # |
| 2767 | my @setup_docs = (); |
| 2768 | my $setup_docs = 0; |
| 2769 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2770 | my $camelcase_file_seeded = 0; |
| 2771 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 2772 | my $checklicenseline = 1; |
| 2773 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2774 | sanitise_line_reset(); |
| 2775 | my $line; |
| 2776 | foreach my $rawline (@rawlines) { |
| 2777 | $linenr++; |
| 2778 | $line = $rawline; |
| 2779 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2780 | push(@fixed, $rawline) if ($fix); |
| 2781 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2782 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
| 2783 | $setup_docs = 0; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 2784 | if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2785 | $setup_docs = 1; |
| 2786 | } |
| 2787 | #next; |
| 2788 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2789 | if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2790 | $realline=$1-1; |
| 2791 | if (defined $2) { |
| 2792 | $realcnt=$3+1; |
| 2793 | } else { |
| 2794 | $realcnt=1+1; |
| 2795 | } |
| 2796 | $in_comment = 0; |
| 2797 | |
| 2798 | # Guestimate if this is a continuing comment. Run |
| 2799 | # the context looking for a comment "edge". If this |
| 2800 | # edge is a close comment then we must be in a comment |
| 2801 | # at context start. |
| 2802 | my $edge; |
| 2803 | my $cnt = $realcnt; |
| 2804 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 2805 | next if (defined $rawlines[$ln - 1] && |
| 2806 | $rawlines[$ln - 1] =~ /^-/); |
| 2807 | $cnt--; |
| 2808 | #print "RAW<$rawlines[$ln - 1]>\n"; |
| 2809 | last if (!defined $rawlines[$ln - 1]); |
| 2810 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 2811 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 2812 | ($edge) = $1; |
| 2813 | last; |
| 2814 | } |
| 2815 | } |
| 2816 | if (defined $edge && $edge eq '*/') { |
| 2817 | $in_comment = 1; |
| 2818 | } |
| 2819 | |
| 2820 | # Guestimate if this is a continuing comment. If this |
| 2821 | # is the start of a diff block and this line starts |
| 2822 | # ' *' then it is very likely a comment. |
| 2823 | if (!defined $edge && |
| 2824 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
| 2825 | { |
| 2826 | $in_comment = 1; |
| 2827 | } |
| 2828 | |
| 2829 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 2830 | sanitise_line_reset($in_comment); |
| 2831 | |
| 2832 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
| 2833 | # Standardise the strings and chars within the input to |
| 2834 | # simplify matching -- only bother with positive lines. |
| 2835 | $line = sanitise_line($rawline); |
| 2836 | } |
| 2837 | push(@lines, $line); |
| 2838 | |
| 2839 | if ($realcnt > 1) { |
| 2840 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 2841 | } else { |
| 2842 | $realcnt = 0; |
| 2843 | } |
| 2844 | |
| 2845 | #print "==>$rawline\n"; |
| 2846 | #print "-->$line\n"; |
| 2847 | |
| 2848 | if ($setup_docs && $line =~ /^\+/) { |
| 2849 | push(@setup_docs, $line); |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | $prefix = ''; |
| 2854 | |
| 2855 | $realcnt = 0; |
| 2856 | $linenr = 0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2857 | $fixlinenr = -1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2858 | foreach my $line (@lines) { |
| 2859 | $linenr++; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2860 | $fixlinenr++; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2861 | my $sline = $line; #copy of $line |
| 2862 | $sline =~ s/$;/ /g; #with comments as spaces |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2863 | |
| 2864 | my $rawline = $rawlines[$linenr - 1]; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2865 | my $raw_comment = get_raw_comment($line, $rawline); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2866 | |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2867 | # check if it's a mode change, rename or start of a patch |
| 2868 | if (!$in_commit_log && |
| 2869 | ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || |
| 2870 | ($line =~ /^rename (?:from|to) \S+\s*$/ || |
| 2871 | $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { |
| 2872 | $is_patch = 1; |
| 2873 | } |
| 2874 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2875 | #extract the line range in the file after the patch is applied |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2876 | if (!$in_commit_log && |
| 2877 | $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { |
| 2878 | my $context = $4; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2879 | $is_patch = 1; |
| 2880 | $first_line = $linenr + 1; |
| 2881 | $realline=$1-1; |
| 2882 | if (defined $2) { |
| 2883 | $realcnt=$3+1; |
| 2884 | } else { |
| 2885 | $realcnt=1+1; |
| 2886 | } |
| 2887 | annotate_reset(); |
| 2888 | $prev_values = 'E'; |
| 2889 | |
| 2890 | %suppress_ifbraces = (); |
| 2891 | %suppress_whiletrailers = (); |
| 2892 | %suppress_export = (); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2893 | $suppress_statement = 0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2894 | if ($context =~ /\b(\w+)\s*\(/) { |
| 2895 | $context_function = $1; |
| 2896 | } else { |
| 2897 | undef $context_function; |
| 2898 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2899 | next; |
| 2900 | |
| 2901 | # track the line number as we move through the hunk, note that |
| 2902 | # new versions of GNU diff omit the leading space on completely |
| 2903 | # blank context lines so we need to count that too. |
| 2904 | } elsif ($line =~ /^( |\+|$)/) { |
| 2905 | $realline++; |
| 2906 | $realcnt-- if ($realcnt != 0); |
| 2907 | |
| 2908 | # Measure the line length and indent. |
| 2909 | ($length, $indent) = line_stats($rawline); |
| 2910 | |
| 2911 | # Track the previous line. |
| 2912 | ($prevline, $stashline) = ($stashline, $line); |
| 2913 | ($previndent, $stashindent) = ($stashindent, $indent); |
| 2914 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 2915 | |
| 2916 | #warn "line<$line>\n"; |
| 2917 | |
| 2918 | } elsif ($realcnt == 1) { |
| 2919 | $realcnt--; |
| 2920 | } |
| 2921 | |
| 2922 | my $hunk_line = ($realcnt != 0); |
| 2923 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2924 | $here = "#$linenr: " if (!$file); |
| 2925 | $here = "#$realline: " if ($file); |
| 2926 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2927 | my $found_file = 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2928 | # extract the filename as it passes |
| 2929 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 2930 | $realfile = $1; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2931 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2932 | $in_commit_log = 0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2933 | $found_file = 1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2934 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
| 2935 | $realfile = $1; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2936 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2937 | $in_commit_log = 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2938 | |
| 2939 | $p1_prefix = $1; |
| 2940 | if (!$file && $tree && $p1_prefix ne '' && |
| 2941 | -e "$root/$p1_prefix") { |
| 2942 | WARN("PATCH_PREFIX", |
| 2943 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
| 2944 | } |
| 2945 | |
| 2946 | if ($realfile =~ m@^include/asm/@) { |
| 2947 | ERROR("MODIFIED_INCLUDE_ASM", |
| 2948 | "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); |
| 2949 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2950 | $found_file = 1; |
| 2951 | } |
| 2952 | |
| 2953 | #make up the handle for any error we report on this line |
| 2954 | if ($showfile) { |
| 2955 | $prefix = "$realfile:$realline: " |
| 2956 | } elsif ($emacs) { |
| 2957 | if ($file) { |
| 2958 | $prefix = "$filename:$realline: "; |
| 2959 | } else { |
| 2960 | $prefix = "$filename:$linenr: "; |
| 2961 | } |
| 2962 | } |
| 2963 | |
| 2964 | if ($found_file) { |
| 2965 | if (is_maintained_obsolete($realfile)) { |
| 2966 | WARN("OBSOLETE", |
| 2967 | "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); |
| 2968 | } |
| 2969 | if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { |
| 2970 | $check = 1; |
| 2971 | } else { |
| 2972 | $check = $check_orig; |
| 2973 | } |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 2974 | $checklicenseline = 1; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2975 | |
| 2976 | if ($realfile !~ /^MAINTAINERS/) { |
| 2977 | my $last_binding_patch = $is_binding_patch; |
| 2978 | |
| 2979 | $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; |
| 2980 | |
| 2981 | if (($last_binding_patch != -1) && |
| 2982 | ($last_binding_patch ^ $is_binding_patch)) { |
| 2983 | WARN("DT_SPLIT_BINDING_PATCH", |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 2984 | "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n"); |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2988 | next; |
| 2989 | } |
| 2990 | |
| 2991 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
| 2992 | |
| 2993 | my $hereline = "$here\n$rawline\n"; |
| 2994 | my $herecurr = "$here\n$rawline\n"; |
| 2995 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
| 2996 | |
| 2997 | $cnt_lines++ if ($realcnt != 0); |
| 2998 | |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2999 | # Verify the existence of a commit log if appropriate |
| 3000 | # 2 is used because a $signature is counted in $commit_log_lines |
| 3001 | if ($in_commit_log) { |
| 3002 | if ($line !~ /^\s*$/) { |
| 3003 | $commit_log_lines++; #could be a $signature |
| 3004 | } |
| 3005 | } elsif ($has_commit_log && $commit_log_lines < 2) { |
| 3006 | WARN("COMMIT_MESSAGE", |
| 3007 | "Missing commit description - Add an appropriate one\n"); |
| 3008 | $commit_log_lines = 2; #warn only once |
| 3009 | } |
| 3010 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3011 | # Check if the commit log has what seems like a diff which can confuse patch |
| 3012 | if ($in_commit_log && !$commit_log_has_diff && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3013 | (($line =~ m@^\s+diff\b.*a/([\w/]+)@ && |
| 3014 | $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3015 | $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || |
| 3016 | $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { |
| 3017 | ERROR("DIFF_IN_COMMIT_MSG", |
| 3018 | "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); |
| 3019 | $commit_log_has_diff = 1; |
| 3020 | } |
| 3021 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3022 | # Check for incorrect file permissions |
| 3023 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 3024 | my $permhere = $here . "FILE: $realfile\n"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3025 | if ($realfile !~ m@scripts/@ && |
| 3026 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3027 | ERROR("EXECUTE_PERMISSIONS", |
| 3028 | "do not set execute permissions for source files\n" . $permhere); |
| 3029 | } |
| 3030 | } |
| 3031 | |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3032 | # Check the patch for a From: |
| 3033 | if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { |
| 3034 | $author = $1; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3035 | my $curline = $linenr; |
| 3036 | while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) { |
| 3037 | $author .= $1; |
| 3038 | } |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3039 | $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); |
| 3040 | $author =~ s/"//g; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3041 | $author = reformat_email($author); |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3042 | } |
| 3043 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3044 | # Check the patch for a signoff: |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3045 | if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3046 | $signoff++; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3047 | $in_commit_log = 0; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3048 | if ($author ne '' && $authorsignoff != 1) { |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3049 | if (same_email_addresses($1, $author)) { |
| 3050 | $authorsignoff = 1; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3051 | } else { |
| 3052 | my $ctx = $1; |
| 3053 | my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx); |
| 3054 | my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author); |
| 3055 | |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3056 | if (lc $email_address eq lc $author_address && $email_name eq $author_name) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3057 | $author_sob = $ctx; |
| 3058 | $authorsignoff = 2; |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3059 | } elsif (lc $email_address eq lc $author_address) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3060 | $author_sob = $ctx; |
| 3061 | $authorsignoff = 3; |
| 3062 | } elsif ($email_name eq $author_name) { |
| 3063 | $author_sob = $ctx; |
| 3064 | $authorsignoff = 4; |
| 3065 | |
| 3066 | my $address1 = $email_address; |
| 3067 | my $address2 = $author_address; |
| 3068 | |
| 3069 | if ($address1 =~ /(\S+)\+\S+(\@.*)/) { |
| 3070 | $address1 = "$1$2"; |
| 3071 | } |
| 3072 | if ($address2 =~ /(\S+)\+\S+(\@.*)/) { |
| 3073 | $address2 = "$1$2"; |
| 3074 | } |
| 3075 | if ($address1 eq $address2) { |
| 3076 | $authorsignoff = 5; |
| 3077 | } |
| 3078 | } |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3079 | } |
| 3080 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3083 | # Check for patch separator |
| 3084 | if ($line =~ /^---$/) { |
| 3085 | $has_patch_separator = 1; |
| 3086 | $in_commit_log = 0; |
| 3087 | } |
| 3088 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3089 | # Check if MAINTAINERS is being updated. If so, there's probably no need to |
| 3090 | # emit the "does MAINTAINERS need updating?" message on file add/move/delete |
| 3091 | if ($line =~ /^\s*MAINTAINERS\s*\|/) { |
| 3092 | $reported_maintainer_file = 1; |
| 3093 | } |
| 3094 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3095 | # Check signature styles |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3096 | if (!$in_header_lines && |
| 3097 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3098 | my $space_before = $1; |
| 3099 | my $sign_off = $2; |
| 3100 | my $space_after = $3; |
| 3101 | my $email = $4; |
| 3102 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 3103 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3104 | if ($sign_off !~ /$signature_tags/) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3105 | my $suggested_signature = find_standard_signature($sign_off); |
| 3106 | if ($suggested_signature eq "") { |
| 3107 | WARN("BAD_SIGN_OFF", |
| 3108 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 3109 | } else { |
| 3110 | if (WARN("BAD_SIGN_OFF", |
| 3111 | "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) && |
| 3112 | $fix) { |
| 3113 | $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/; |
| 3114 | } |
| 3115 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3116 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3117 | if (defined $space_before && $space_before ne "") { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3118 | if (WARN("BAD_SIGN_OFF", |
| 3119 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && |
| 3120 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3121 | $fixed[$fixlinenr] = |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3122 | "$ucfirst_sign_off $email"; |
| 3123 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3124 | } |
| 3125 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3126 | if (WARN("BAD_SIGN_OFF", |
| 3127 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && |
| 3128 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3129 | $fixed[$fixlinenr] = |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3130 | "$ucfirst_sign_off $email"; |
| 3131 | } |
| 3132 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3133 | } |
| 3134 | if (!defined $space_after || $space_after ne " ") { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3135 | if (WARN("BAD_SIGN_OFF", |
| 3136 | "Use a single space after $ucfirst_sign_off\n" . $herecurr) && |
| 3137 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3138 | $fixed[$fixlinenr] = |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3139 | "$ucfirst_sign_off $email"; |
| 3140 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3143 | my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3144 | my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment)); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3145 | if ($suggested_email eq "") { |
| 3146 | ERROR("BAD_SIGN_OFF", |
| 3147 | "Unrecognized email address: '$email'\n" . $herecurr); |
| 3148 | } else { |
| 3149 | my $dequoted = $suggested_email; |
| 3150 | $dequoted =~ s/^"//; |
| 3151 | $dequoted =~ s/" </ </; |
| 3152 | # Don't force email to have quotes |
| 3153 | # Allow just an angle bracketed address |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3154 | if (!same_email_addresses($email, $suggested_email)) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3155 | if (WARN("BAD_SIGN_OFF", |
| 3156 | "email address '$email' might be better as '$suggested_email'\n" . $herecurr) && |
| 3157 | $fix) { |
| 3158 | $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/; |
| 3159 | } |
| 3160 | } |
| 3161 | |
| 3162 | # Address part shouldn't have comments |
| 3163 | my $stripped_address = $email_address; |
| 3164 | $stripped_address =~ s/\([^\(\)]*\)//g; |
| 3165 | if ($email_address ne $stripped_address) { |
| 3166 | if (WARN("BAD_SIGN_OFF", |
| 3167 | "address part of email should not have comments: '$email_address'\n" . $herecurr) && |
| 3168 | $fix) { |
| 3169 | $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/; |
| 3170 | } |
| 3171 | } |
| 3172 | |
| 3173 | # Only one name comment should be allowed |
| 3174 | my $comment_count = () = $name_comment =~ /\([^\)]+\)/g; |
| 3175 | if ($comment_count > 1) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3176 | WARN("BAD_SIGN_OFF", |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3177 | "Use a single name comment in email: '$email'\n" . $herecurr); |
| 3178 | } |
| 3179 | |
| 3180 | |
| 3181 | # stable@vger.kernel.org or stable@kernel.org shouldn't |
| 3182 | # have an email name. In addition comments should strictly |
| 3183 | # begin with a # |
| 3184 | if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) { |
| 3185 | if (($comment ne "" && $comment !~ /^#.+/) || |
| 3186 | ($email_name ne "")) { |
| 3187 | my $cur_name = $email_name; |
| 3188 | my $new_comment = $comment; |
| 3189 | $cur_name =~ s/[a-zA-Z\s\-\"]+//g; |
| 3190 | |
| 3191 | # Remove brackets enclosing comment text |
| 3192 | # and # from start of comments to get comment text |
| 3193 | $new_comment =~ s/^\((.*)\)$/$1/; |
| 3194 | $new_comment =~ s/^\[(.*)\]$/$1/; |
| 3195 | $new_comment =~ s/^[\s\#]+|\s+$//g; |
| 3196 | |
| 3197 | $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment); |
| 3198 | $new_comment = " # $new_comment" if ($new_comment ne ""); |
| 3199 | my $new_email = "$email_address$new_comment"; |
| 3200 | |
| 3201 | if (WARN("BAD_STABLE_ADDRESS_STYLE", |
| 3202 | "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) && |
| 3203 | $fix) { |
| 3204 | $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; |
| 3205 | } |
| 3206 | } |
| 3207 | } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) { |
| 3208 | my $new_comment = $comment; |
| 3209 | |
| 3210 | # Extract comment text from within brackets or |
| 3211 | # c89 style /*...*/ comments |
| 3212 | $new_comment =~ s/^\[(.*)\]$/$1/; |
| 3213 | $new_comment =~ s/^\/\*(.*)\*\/$/$1/; |
| 3214 | |
| 3215 | $new_comment = trim($new_comment); |
| 3216 | $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo |
| 3217 | $new_comment = "($new_comment)" if ($new_comment ne ""); |
| 3218 | my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment); |
| 3219 | |
| 3220 | if (WARN("BAD_SIGN_OFF", |
| 3221 | "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) && |
| 3222 | $fix) { |
| 3223 | $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; |
| 3224 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3225 | } |
| 3226 | } |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3227 | |
| 3228 | # Check for duplicate signatures |
| 3229 | my $sig_nospace = $line; |
| 3230 | $sig_nospace =~ s/\s//g; |
| 3231 | $sig_nospace = lc($sig_nospace); |
| 3232 | if (defined $signatures{$sig_nospace}) { |
| 3233 | WARN("BAD_SIGN_OFF", |
| 3234 | "Duplicate signature\n" . $herecurr); |
| 3235 | } else { |
| 3236 | $signatures{$sig_nospace} = 1; |
| 3237 | } |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3238 | |
| 3239 | # Check Co-developed-by: immediately followed by Signed-off-by: with same name and email |
| 3240 | if ($sign_off =~ /^co-developed-by:$/i) { |
| 3241 | if ($email eq $author) { |
| 3242 | WARN("BAD_SIGN_OFF", |
| 3243 | "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); |
| 3244 | } |
| 3245 | if (!defined $lines[$linenr]) { |
| 3246 | WARN("BAD_SIGN_OFF", |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3247 | "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3248 | } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { |
| 3249 | WARN("BAD_SIGN_OFF", |
| 3250 | "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); |
| 3251 | } elsif ($1 ne $email) { |
| 3252 | WARN("BAD_SIGN_OFF", |
| 3253 | "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); |
| 3254 | } |
| 3255 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3258 | # Check email subject for common tools that don't need to be mentioned |
| 3259 | if ($in_header_lines && |
| 3260 | $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { |
| 3261 | WARN("EMAIL_SUBJECT", |
| 3262 | "A patch subject line should describe the change not the tool that found it\n" . $herecurr); |
| 3263 | } |
| 3264 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3265 | # Check for Gerrit Change-Ids not in any patch context |
| 3266 | if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3267 | if (ERROR("GERRIT_CHANGE_ID", |
| 3268 | "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) && |
| 3269 | $fix) { |
| 3270 | fix_delete_line($fixlinenr, $rawline); |
| 3271 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | # Check if the commit log is in a possible stack dump |
| 3275 | if ($in_commit_log && !$commit_log_possible_stack_dump && |
| 3276 | ($line =~ /^\s*(?:WARNING:|BUG:)/ || |
| 3277 | $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || |
| 3278 | # timestamp |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3279 | $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || |
| 3280 | $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || |
| 3281 | $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { |
| 3282 | # stack dump address styles |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3283 | $commit_log_possible_stack_dump = 1; |
| 3284 | } |
| 3285 | |
| 3286 | # Check for line lengths > 75 in commit log, warn once |
| 3287 | if ($in_commit_log && !$commit_log_long_line && |
| 3288 | length($line) > 75 && |
| 3289 | !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || |
| 3290 | # file delta changes |
| 3291 | $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || |
| 3292 | # filename then : |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3293 | $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i || |
| 3294 | # A Fixes: or Link: line or signature tag line |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3295 | $commit_log_possible_stack_dump)) { |
| 3296 | WARN("COMMIT_LOG_LONG_LINE", |
| 3297 | "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); |
| 3298 | $commit_log_long_line = 1; |
| 3299 | } |
| 3300 | |
| 3301 | # Reset possible stack dump if a blank line is found |
| 3302 | if ($in_commit_log && $commit_log_possible_stack_dump && |
| 3303 | $line =~ /^\s*$/) { |
| 3304 | $commit_log_possible_stack_dump = 0; |
| 3305 | } |
| 3306 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3307 | # Check for lines starting with a # |
| 3308 | if ($in_commit_log && $line =~ /^#/) { |
| 3309 | if (WARN("COMMIT_COMMENT_SYMBOL", |
| 3310 | "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) && |
| 3311 | $fix) { |
| 3312 | $fixed[$fixlinenr] =~ s/^/ /; |
| 3313 | } |
| 3314 | } |
| 3315 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3316 | # Check for git id commit length and improperly formed commit descriptions |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3317 | # A correctly formed commit description is: |
| 3318 | # commit <SHA-1 hash length 12+ chars> ("Complete commit subject") |
| 3319 | # with the commit subject '("' prefix and '")' suffix |
| 3320 | # This is a fairly compilicated block as it tests for what appears to be |
| 3321 | # bare SHA-1 hash with minimum length of 5. It also avoids several types of |
| 3322 | # possible SHA-1 matches. |
| 3323 | # A commit match can span multiple lines so this block attempts to find a |
| 3324 | # complete typical commit on a maximum of 3 lines |
| 3325 | if ($perl_version_ok && |
| 3326 | $in_commit_log && !$commit_log_possible_stack_dump && |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3327 | $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3328 | $line !~ /^This reverts commit [0-9a-f]{7,40}/ && |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3329 | (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || |
| 3330 | ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3331 | ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && |
| 3332 | $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && |
| 3333 | $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { |
| 3334 | my $init_char = "c"; |
| 3335 | my $orig_commit = ""; |
| 3336 | my $short = 1; |
| 3337 | my $long = 0; |
| 3338 | my $case = 1; |
| 3339 | my $space = 1; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3340 | my $id = '0123456789ab'; |
| 3341 | my $orig_desc = "commit description"; |
| 3342 | my $description = ""; |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3343 | my $herectx = $herecurr; |
| 3344 | my $has_parens = 0; |
| 3345 | my $has_quotes = 0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3346 | |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3347 | my $input = $line; |
| 3348 | if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) { |
| 3349 | for (my $n = 0; $n < 2; $n++) { |
| 3350 | if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) { |
| 3351 | $orig_desc = $1; |
| 3352 | $has_parens = 1; |
| 3353 | # Always strip leading/trailing parens then double quotes if existing |
| 3354 | $orig_desc = substr($orig_desc, 1, -1); |
| 3355 | if ($orig_desc =~ /^".*"$/) { |
| 3356 | $orig_desc = substr($orig_desc, 1, -1); |
| 3357 | $has_quotes = 1; |
| 3358 | } |
| 3359 | last; |
| 3360 | } |
| 3361 | last if ($#lines < $linenr + $n); |
| 3362 | $input .= " " . trim($rawlines[$linenr + $n]); |
| 3363 | $herectx .= "$rawlines[$linenr + $n]\n"; |
| 3364 | } |
| 3365 | $herectx = $herecurr if (!$has_parens); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3366 | } |
| 3367 | |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3368 | if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { |
| 3369 | $init_char = $1; |
| 3370 | $orig_commit = lc($2); |
| 3371 | $short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i); |
| 3372 | $long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i); |
| 3373 | $space = 0 if ($input =~ /\bcommit [0-9a-f]/i); |
| 3374 | $case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); |
| 3375 | } elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) { |
| 3376 | $orig_commit = lc($1); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | ($id, $description) = git_commit_info($orig_commit, |
| 3380 | $id, $orig_desc); |
| 3381 | |
| 3382 | if (defined($id) && |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3383 | ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) && |
| 3384 | $last_git_commit_id_linenr != $linenr - 1) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3385 | ERROR("GIT_COMMIT_ID", |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3386 | "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3387 | } |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 3388 | #don't report the next line if this line ends in commit and the sha1 hash is the next line |
| 3389 | $last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | # Check for added, moved or deleted files |
| 3393 | if (!$reported_maintainer_file && !$in_commit_log && |
| 3394 | ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || |
| 3395 | $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || |
| 3396 | ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && |
| 3397 | (defined($1) || defined($2))))) { |
| 3398 | $is_patch = 1; |
| 3399 | $reported_maintainer_file = 1; |
| 3400 | WARN("FILE_PATH_CHANGES", |
| 3401 | "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); |
| 3402 | } |
| 3403 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3404 | # Check for adding new DT bindings not in schema format |
| 3405 | if (!$in_commit_log && |
| 3406 | ($line =~ /^new file mode\s*\d+\s*$/) && |
| 3407 | ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { |
| 3408 | WARN("DT_SCHEMA_BINDING_PATCH", |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3409 | "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n"); |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3410 | } |
| 3411 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3412 | # Check for wrappage within a valid hunk of the file |
| 3413 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
| 3414 | ERROR("CORRUPTED_PATCH", |
| 3415 | "patch seems to be corrupt (line wrapped?)\n" . |
| 3416 | $herecurr) if (!$emitted_corrupt++); |
| 3417 | } |
| 3418 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3419 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 3420 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
| 3421 | $rawline !~ m/^$UTF8*$/) { |
| 3422 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 3423 | |
| 3424 | my $blank = copy_spacing($rawline); |
| 3425 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 3426 | my $hereptr = "$hereline$ptr\n"; |
| 3427 | |
| 3428 | CHK("INVALID_UTF8", |
| 3429 | "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); |
| 3430 | } |
| 3431 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3432 | # Check if it's the start of a commit log |
| 3433 | # (not a header line and we haven't seen the patch filename) |
| 3434 | if ($in_header_lines && $realfile =~ /^$/ && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3435 | !($rawline =~ /^\s+(?:\S|$)/ || |
| 3436 | $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3437 | $in_header_lines = 0; |
| 3438 | $in_commit_log = 1; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3439 | $has_commit_log = 1; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 3443 | # declined it, i.e defined some charset where it is missing. |
| 3444 | if ($in_header_lines && |
| 3445 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 3446 | $1 !~ /utf-8/i) { |
| 3447 | $non_utf8_charset = 1; |
| 3448 | } |
| 3449 | |
| 3450 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
| 3451 | $rawline =~ /$NON_ASCII_UTF8/) { |
| 3452 | WARN("UTF8_BEFORE_PATCH", |
| 3453 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 3454 | } |
| 3455 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3456 | # Check for absolute kernel paths in commit message |
| 3457 | if ($tree && $in_commit_log) { |
| 3458 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 3459 | my $file = $1; |
| 3460 | |
| 3461 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 3462 | check_absolute_file($1, $herecurr)) { |
| 3463 | # |
| 3464 | } else { |
| 3465 | check_absolute_file($file, $herecurr); |
| 3466 | } |
| 3467 | } |
| 3468 | } |
| 3469 | |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 3470 | # Check for various typo / spelling mistakes |
| 3471 | if (defined($misspellings) && |
| 3472 | ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3473 | while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) { |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 3474 | my $typo = $1; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3475 | my $blank = copy_spacing($rawline); |
| 3476 | my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo); |
| 3477 | my $hereptr = "$hereline$ptr\n"; |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 3478 | my $typo_fix = $spelling_fix{lc($typo)}; |
| 3479 | $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); |
| 3480 | $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3481 | my $msg_level = \&WARN; |
| 3482 | $msg_level = \&CHK if ($file); |
| 3483 | if (&{$msg_level}("TYPO_SPELLING", |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3484 | "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) && |
Dan Murphy | c10e0f5 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 3485 | $fix) { |
| 3486 | $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; |
| 3487 | } |
| 3488 | } |
| 3489 | } |
| 3490 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3491 | # check for invalid commit id |
| 3492 | if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { |
| 3493 | my $id; |
| 3494 | my $description; |
| 3495 | ($id, $description) = git_commit_info($2, undef, undef); |
| 3496 | if (!defined($id)) { |
| 3497 | WARN("UNKNOWN_COMMIT_ID", |
| 3498 | "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); |
| 3499 | } |
| 3500 | } |
| 3501 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3502 | # check for repeated words separated by a single space |
| 3503 | # avoid false positive from list command eg, '-rw-r--r-- 1 root root' |
| 3504 | if (($rawline =~ /^\+/ || $in_commit_log) && |
| 3505 | $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) { |
| 3506 | pos($rawline) = 1 if (!$in_commit_log); |
| 3507 | while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) { |
| 3508 | |
| 3509 | my $first = $1; |
| 3510 | my $second = $2; |
| 3511 | my $start_pos = $-[1]; |
| 3512 | my $end_pos = $+[2]; |
| 3513 | if ($first =~ /(?:struct|union|enum)/) { |
| 3514 | pos($rawline) += length($first) + length($second) + 1; |
| 3515 | next; |
| 3516 | } |
| 3517 | |
| 3518 | next if (lc($first) ne lc($second)); |
| 3519 | next if ($first eq 'long'); |
| 3520 | |
| 3521 | # check for character before and after the word matches |
| 3522 | my $start_char = ''; |
| 3523 | my $end_char = ''; |
| 3524 | $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1)); |
| 3525 | $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline)); |
| 3526 | |
| 3527 | next if ($start_char =~ /^\S$/); |
| 3528 | next if (index(" \t.,;?!", $end_char) == -1); |
| 3529 | |
| 3530 | # avoid repeating hex occurrences like 'ff ff fe 09 ...' |
| 3531 | if ($first =~ /\b[0-9a-f]{2,}\b/i) { |
| 3532 | next if (!exists($allow_repeated_words{lc($first)})); |
| 3533 | } |
| 3534 | |
| 3535 | if (WARN("REPEATED_WORD", |
| 3536 | "Possible repeated word: '$first'\n" . $herecurr) && |
| 3537 | $fix) { |
| 3538 | $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/; |
| 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | # if it's a repeated word on consecutive lines in a comment block |
| 3543 | if ($prevline =~ /$;+\s*$/ && |
| 3544 | $prevrawline =~ /($word_pattern)\s*$/) { |
| 3545 | my $last_word = $1; |
| 3546 | if ($rawline =~ /^\+\s*\*\s*$last_word /) { |
| 3547 | if (WARN("REPEATED_WORD", |
| 3548 | "Possible repeated word: '$last_word'\n" . $hereprev) && |
| 3549 | $fix) { |
| 3550 | $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/; |
| 3551 | } |
| 3552 | } |
| 3553 | } |
| 3554 | } |
| 3555 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3556 | # ignore non-hunk lines and lines being removed |
| 3557 | next if (!$hunk_line || $line =~ /^-/); |
| 3558 | |
| 3559 | #trailing whitespace |
| 3560 | if ($line =~ /^\+.*\015/) { |
| 3561 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3562 | if (ERROR("DOS_LINE_ENDINGS", |
| 3563 | "DOS line endings\n" . $herevet) && |
| 3564 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3565 | $fixed[$fixlinenr] =~ s/[\s\015]+$//; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3566 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3567 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 3568 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3569 | if (ERROR("TRAILING_WHITESPACE", |
| 3570 | "trailing whitespace\n" . $herevet) && |
| 3571 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3572 | $fixed[$fixlinenr] =~ s/\s+$//; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3573 | } |
| 3574 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3575 | $rpt_cleaners = 1; |
| 3576 | } |
| 3577 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3578 | # Check for FSF mailing addresses. |
| 3579 | if ($rawline =~ /\bwrite to the Free/i || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3580 | $rawline =~ /\b675\s+Mass\s+Ave/i || |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3581 | $rawline =~ /\b59\s+Temple\s+Pl/i || |
| 3582 | $rawline =~ /\b51\s+Franklin\s+St/i) { |
| 3583 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3584 | my $msg_level = \&ERROR; |
| 3585 | $msg_level = \&CHK if ($file); |
| 3586 | &{$msg_level}("FSF_MAILING_ADDRESS", |
| 3587 | "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 Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3588 | } |
| 3589 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3590 | # check for Kconfig help text having a real description |
| 3591 | # Only applies when adding the entry originally, after that we do not have |
| 3592 | # sufficient context to determine whether it is indeed long enough. |
| 3593 | if ($realfile =~ /Kconfig/ && |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3594 | # 'choice' is usually the last thing on the line (though |
| 3595 | # Kconfig supports named choices), so use a word boundary |
| 3596 | # (\b) rather than a whitespace character (\s) |
| 3597 | $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3598 | my $length = 0; |
| 3599 | my $cnt = $realcnt; |
| 3600 | my $ln = $linenr + 1; |
| 3601 | my $f; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3602 | my $is_start = 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3603 | my $is_end = 0; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3604 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3605 | $f = $lines[$ln - 1]; |
| 3606 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 3607 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3608 | |
| 3609 | next if ($f =~ /^-/); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3610 | last if (!$file && $f =~ /^\@\@/); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3611 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3612 | if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3613 | $is_start = 1; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3614 | } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3615 | $length = -1; |
| 3616 | } |
| 3617 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3618 | $f =~ s/^.//; |
| 3619 | $f =~ s/#.*//; |
| 3620 | $f =~ s/^\s+//; |
| 3621 | next if ($f =~ /^$/); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3622 | |
| 3623 | # This only checks context lines in the patch |
| 3624 | # and so hopefully shouldn't trigger false |
| 3625 | # positives, even though some of these are |
| 3626 | # common words in help texts |
| 3627 | if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| |
| 3628 | if|endif|menu|endmenu|source)\b/x) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3629 | $is_end = 1; |
| 3630 | last; |
| 3631 | } |
| 3632 | $length++; |
| 3633 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3634 | if ($is_start && $is_end && $length < $min_conf_desc_length) { |
| 3635 | WARN("CONFIG_DESCRIPTION", |
| 3636 | "please write a paragraph that describes the config symbol fully\n" . $herecurr); |
| 3637 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3638 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
| 3639 | } |
| 3640 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3641 | # check MAINTAINERS entries |
| 3642 | if ($realfile =~ /^MAINTAINERS$/) { |
| 3643 | # check MAINTAINERS entries for the right form |
| 3644 | if ($rawline =~ /^\+[A-Z]:/ && |
| 3645 | $rawline !~ /^\+[A-Z]:\t\S/) { |
| 3646 | if (WARN("MAINTAINERS_STYLE", |
| 3647 | "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && |
| 3648 | $fix) { |
| 3649 | $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; |
| 3650 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3651 | } |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3652 | # check MAINTAINERS entries for the right ordering too |
| 3653 | my $preferred_order = 'MRLSWQBCPTFXNK'; |
| 3654 | if ($rawline =~ /^\+[A-Z]:/ && |
| 3655 | $prevrawline =~ /^[\+ ][A-Z]:/) { |
| 3656 | $rawline =~ /^\+([A-Z]):\s*(.*)/; |
| 3657 | my $cur = $1; |
| 3658 | my $curval = $2; |
| 3659 | $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/; |
| 3660 | my $prev = $1; |
| 3661 | my $prevval = $2; |
| 3662 | my $curindex = index($preferred_order, $cur); |
| 3663 | my $previndex = index($preferred_order, $prev); |
| 3664 | if ($curindex < 0) { |
| 3665 | WARN("MAINTAINERS_STYLE", |
| 3666 | "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr); |
| 3667 | } else { |
| 3668 | if ($previndex >= 0 && $curindex < $previndex) { |
| 3669 | WARN("MAINTAINERS_STYLE", |
| 3670 | "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev); |
| 3671 | } elsif ((($prev eq 'F' && $cur eq 'F') || |
| 3672 | ($prev eq 'X' && $cur eq 'X')) && |
| 3673 | ($prevval cmp $curval) > 0) { |
| 3674 | WARN("MAINTAINERS_STYLE", |
| 3675 | "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev); |
| 3676 | } |
| 3677 | } |
| 3678 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 3682 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 3683 | my $flag = $1; |
| 3684 | my $replacement = { |
| 3685 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 3686 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 3687 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 3688 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 3689 | }; |
| 3690 | |
| 3691 | WARN("DEPRECATED_VARIABLE", |
| 3692 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3695 | # check for DT compatible documentation |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3696 | if (defined $root && |
| 3697 | (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || |
| 3698 | ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { |
| 3699 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3700 | my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; |
| 3701 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3702 | my $dt_path = $root . "/Documentation/devicetree/bindings/"; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3703 | my $vp_file = $dt_path . "vendor-prefixes.yaml"; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3704 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3705 | foreach my $compat (@compats) { |
| 3706 | my $compat2 = $compat; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3707 | $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; |
| 3708 | my $compat3 = $compat; |
| 3709 | $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; |
| 3710 | `grep -Erq "$compat|$compat2|$compat3" $dt_path`; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3711 | if ( $? >> 8 ) { |
| 3712 | WARN("UNDOCUMENTED_DT_STRING", |
| 3713 | "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); |
| 3714 | } |
| 3715 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3716 | next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; |
| 3717 | my $vendor = $1; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3718 | `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3719 | if ( $? >> 8 ) { |
| 3720 | WARN("UNDOCUMENTED_DT_STRING", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3721 | "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3722 | } |
| 3723 | } |
| 3724 | } |
| 3725 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3726 | # check for using SPDX license tag at beginning of files |
| 3727 | if ($realline == $checklicenseline) { |
| 3728 | if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { |
| 3729 | $checklicenseline = 2; |
| 3730 | } elsif ($rawline =~ /^\+/) { |
| 3731 | my $comment = ""; |
| 3732 | if ($realfile =~ /\.(h|s|S)$/) { |
| 3733 | $comment = '/*'; |
| 3734 | } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { |
| 3735 | $comment = '//'; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3736 | } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3737 | $comment = '#'; |
| 3738 | } elsif ($realfile =~ /\.rst$/) { |
| 3739 | $comment = '..'; |
| 3740 | } |
| 3741 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3742 | # check SPDX comment style for .[chsS] files |
| 3743 | if ($realfile =~ /\.[chsS]$/ && |
| 3744 | $rawline =~ /SPDX-License-Identifier:/ && |
| 3745 | $rawline !~ m@^\+\s*\Q$comment\E\s*@) { |
| 3746 | WARN("SPDX_LICENSE_TAG", |
| 3747 | "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); |
| 3748 | } |
| 3749 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3750 | if ($comment !~ /^$/ && |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3751 | $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { |
| 3752 | WARN("SPDX_LICENSE_TAG", |
| 3753 | "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3754 | } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3755 | my $spdx_license = $1; |
| 3756 | if (!is_SPDX_License_valid($spdx_license)) { |
| 3757 | WARN("SPDX_LICENSE_TAG", |
| 3758 | "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); |
| 3759 | } |
| 3760 | if ($realfile =~ m@^Documentation/devicetree/bindings/@ && |
| 3761 | not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { |
| 3762 | my $msg_level = \&WARN; |
| 3763 | $msg_level = \&CHK if ($file); |
| 3764 | if (&{$msg_level}("SPDX_LICENSE_TAG", |
| 3765 | |
| 3766 | "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && |
| 3767 | $fix) { |
| 3768 | $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; |
| 3769 | } |
| 3770 | } |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3771 | } |
| 3772 | } |
| 3773 | } |
| 3774 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3775 | # check for embedded filenames |
| 3776 | if ($rawline =~ /^\+.*\Q$realfile\E/) { |
| 3777 | WARN("EMBEDDED_FILENAME", |
| 3778 | "It's generally not useful to have the filename in the file\n" . $herecurr); |
| 3779 | } |
| 3780 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3781 | # check we are in a valid source file if not then ignore this hunk |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3782 | next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3783 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3784 | # check for using SPDX-License-Identifier on the wrong line number |
| 3785 | if ($realline != $checklicenseline && |
| 3786 | $rawline =~ /\bSPDX-License-Identifier:/ && |
| 3787 | substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { |
| 3788 | WARN("SPDX_LICENSE_TAG", |
| 3789 | "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); |
| 3790 | } |
| 3791 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3792 | # line length limit (with some exclusions) |
| 3793 | # |
| 3794 | # There are a few types of lines that may extend beyond $max_line_length: |
| 3795 | # logging functions like pr_info that end in a string |
| 3796 | # lines with a single string |
| 3797 | # #defines that are a single string |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3798 | # lines with an RFC3986 like URL |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3799 | # |
| 3800 | # There are 3 different line length message types: |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3801 | # LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3802 | # LONG_LINE_STRING a string starts before but extends beyond $max_line_length |
| 3803 | # LONG_LINE all other lines longer than $max_line_length |
| 3804 | # |
| 3805 | # if LONG_LINE is ignored, the other 2 types are also ignored |
| 3806 | # |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3807 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3808 | if ($line =~ /^\+/ && $length > $max_line_length) { |
| 3809 | my $msg_type = "LONG_LINE"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3810 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3811 | # Check the allowed long line types first |
| 3812 | |
| 3813 | # logging functions that end in a string that starts |
| 3814 | # before $max_line_length |
| 3815 | if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && |
| 3816 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3817 | $msg_type = ""; |
| 3818 | |
| 3819 | # lines with only strings (w/ possible termination) |
| 3820 | # #defines with only strings |
| 3821 | } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || |
| 3822 | $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { |
| 3823 | $msg_type = ""; |
| 3824 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3825 | # More special cases |
| 3826 | } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || |
| 3827 | $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { |
| 3828 | $msg_type = ""; |
| 3829 | |
| 3830 | # URL ($rawline is used in case the URL is in a comment) |
| 3831 | } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3832 | $msg_type = ""; |
| 3833 | |
| 3834 | # Otherwise set the alternate message types |
| 3835 | |
| 3836 | # a comment starts before $max_line_length |
| 3837 | } elsif ($line =~ /($;[\s$;]*)$/ && |
| 3838 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3839 | $msg_type = "LONG_LINE_COMMENT" |
| 3840 | |
| 3841 | # a quoted string starts before $max_line_length |
| 3842 | } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && |
| 3843 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3844 | $msg_type = "LONG_LINE_STRING" |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3845 | } |
| 3846 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3847 | if ($msg_type ne "" && |
| 3848 | (show_type("LONG_LINE") || show_type($msg_type))) { |
Simon Glass | 048a648 | 2020-05-22 16:32:35 -0600 | [diff] [blame] | 3849 | my $msg_level = \&WARN; |
| 3850 | $msg_level = \&CHK if ($file); |
| 3851 | &{$msg_level}($msg_type, |
| 3852 | "line length of $length exceeds $max_line_length columns\n" . $herecurr); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3853 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | # check for adding lines without a newline. |
| 3857 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3858 | if (WARN("MISSING_EOF_NEWLINE", |
| 3859 | "adding a line without newline at end of file\n" . $herecurr) && |
| 3860 | $fix) { |
| 3861 | fix_delete_line($fixlinenr+1, "No newline at end of file"); |
| 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | # check for .L prefix local symbols in .S files |
| 3866 | if ($realfile =~ /\.S$/ && |
| 3867 | $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) { |
| 3868 | WARN("AVOID_L_PREFIX", |
| 3869 | "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
Simon Glass | b77df59 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 3872 | if ($u_boot) { |
Simon Glass | 23552ba | 2020-07-19 10:16:01 -0600 | [diff] [blame] | 3873 | u_boot_line($realfile, $line, $rawline, $herecurr); |
Simon Glass | b77df59 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 3874 | } |
| 3875 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3876 | # check we are in a valid source file C or perl if not then ignore this hunk |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3877 | next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3878 | |
| 3879 | # at the beginning of a line any tabs must come first and anything |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3880 | # more than $tabsize must use tabs. |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3881 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 3882 | $rawline =~ /^\+\s* \s*/) { |
| 3883 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3884 | $rpt_cleaners = 1; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3885 | if (ERROR("CODE_INDENT", |
| 3886 | "code indent should use tabs where possible\n" . $herevet) && |
| 3887 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3888 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3889 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | # check for space before tabs. |
| 3893 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 3894 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3895 | if (WARN("SPACE_BEFORE_TAB", |
| 3896 | "please, no space before tabs\n" . $herevet) && |
| 3897 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3898 | while ($fixed[$fixlinenr] =~ |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3899 | s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3900 | while ($fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3901 | s/(^\+.*) +\t/$1\t/) {} |
| 3902 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3905 | # check for assignments on the start of a line |
| 3906 | if ($sline =~ /^\+\s+($Assignment)[^=]/) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3907 | my $operator = $1; |
| 3908 | if (CHK("ASSIGNMENT_CONTINUATIONS", |
| 3909 | "Assignment operator '$1' should be on the previous line\n" . $hereprev) && |
| 3910 | $fix && $prevrawline =~ /^\+/) { |
| 3911 | # add assignment operator to the previous line, remove from current line |
| 3912 | $fixed[$fixlinenr - 1] .= " $operator"; |
| 3913 | $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; |
| 3914 | } |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3915 | } |
| 3916 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3917 | # check for && or || at the start of a line |
| 3918 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3919 | my $operator = $1; |
| 3920 | if (CHK("LOGICAL_CONTINUATIONS", |
| 3921 | "Logical continuations should be on the previous line\n" . $hereprev) && |
| 3922 | $fix && $prevrawline =~ /^\+/) { |
| 3923 | # insert logical operator at last non-comment, non-whitepsace char on previous line |
| 3924 | $prevline =~ /[\s$;]*$/; |
| 3925 | my $line_end = substr($prevrawline, $-[0]); |
| 3926 | $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/; |
| 3927 | $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; |
| 3928 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3931 | # check indentation starts on a tab stop |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3932 | if ($perl_version_ok && |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3933 | $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3934 | my $indent = length($1); |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3935 | if ($indent % $tabsize) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3936 | if (WARN("TABSTOP", |
| 3937 | "Statements should start on a tabstop\n" . $herecurr) && |
| 3938 | $fix) { |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3939 | $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3940 | } |
| 3941 | } |
| 3942 | } |
| 3943 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3944 | # check multi-line statement indentation matches previous line |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3945 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3946 | $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3947 | $prevline =~ /^\+(\t*)(.*)$/; |
| 3948 | my $oldindent = $1; |
| 3949 | my $rest = $2; |
| 3950 | |
| 3951 | my $pos = pos_last_openparen($rest); |
| 3952 | if ($pos >= 0) { |
| 3953 | $line =~ /^(\+| )([ \t]*)/; |
| 3954 | my $newindent = $2; |
| 3955 | |
| 3956 | my $goodtabindent = $oldindent . |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3957 | "\t" x ($pos / $tabsize) . |
| 3958 | " " x ($pos % $tabsize); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3959 | my $goodspaceindent = $oldindent . " " x $pos; |
| 3960 | |
| 3961 | if ($newindent ne $goodtabindent && |
| 3962 | $newindent ne $goodspaceindent) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3963 | |
| 3964 | if (CHK("PARENTHESIS_ALIGNMENT", |
| 3965 | "Alignment should match open parenthesis\n" . $hereprev) && |
| 3966 | $fix && $line =~ /^\+/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3967 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3968 | s/^\+[ \t]*/\+$goodtabindent/; |
| 3969 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3970 | } |
| 3971 | } |
| 3972 | } |
| 3973 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3974 | # check for space after cast like "(int) foo" or "(struct foo) bar" |
| 3975 | # avoid checking a few false positives: |
| 3976 | # "sizeof(<type>)" or "__alignof__(<type>)" |
| 3977 | # function pointer declarations like "(*foo)(int) = bar;" |
| 3978 | # structure definitions like "(struct foo) { 0 };" |
| 3979 | # multiline macros that define functions |
| 3980 | # known attributes or the __attribute__ keyword |
| 3981 | if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && |
| 3982 | (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3983 | if (CHK("SPACING", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3984 | "No space is necessary after a cast\n" . $herecurr) && |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3985 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3986 | $fixed[$fixlinenr] =~ |
| 3987 | s/(\(\s*$Type\s*\))[ \t]+/$1/; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3988 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3991 | # Block comment styles |
| 3992 | # Networking with an initial /* |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3993 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3994 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3995 | $rawline =~ /^\+[ \t]*\*/ && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 3996 | $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3997 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 3998 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 3999 | } |
| 4000 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4001 | # Block comments use * on subsequent lines |
| 4002 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment |
| 4003 | $prevrawline =~ /^\+.*?\/\*/ && #starting /* |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4004 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
| 4005 | $rawline =~ /^\+/ && #line is new |
| 4006 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4007 | WARN("BLOCK_COMMENT_STYLE", |
| 4008 | "Block comments use * on subsequent lines\n" . $hereprev); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4009 | } |
| 4010 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4011 | # Block comments use */ on trailing lines |
| 4012 | if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4013 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 4014 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 4015 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4016 | WARN("BLOCK_COMMENT_STYLE", |
| 4017 | "Block comments use a trailing */ on a separate line\n" . $herecurr); |
| 4018 | } |
| 4019 | |
| 4020 | # Block comment * alignment |
| 4021 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment |
| 4022 | $line =~ /^\+[ \t]*$;/ && #leading comment |
| 4023 | $rawline =~ /^\+[ \t]*\*/ && #leading * |
| 4024 | (($prevrawline =~ /^\+.*?\/\*/ && #leading /* |
| 4025 | $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ |
| 4026 | $prevrawline =~ /^\+[ \t]*\*/)) { #leading * |
| 4027 | my $oldindent; |
| 4028 | $prevrawline =~ m@^\+([ \t]*/?)\*@; |
| 4029 | if (defined($1)) { |
| 4030 | $oldindent = expand_tabs($1); |
| 4031 | } else { |
| 4032 | $prevrawline =~ m@^\+(.*/?)\*@; |
| 4033 | $oldindent = expand_tabs($1); |
| 4034 | } |
| 4035 | $rawline =~ m@^\+([ \t]*)\*@; |
| 4036 | my $newindent = $1; |
| 4037 | $newindent = expand_tabs($newindent); |
| 4038 | if (length($oldindent) ne length($newindent)) { |
| 4039 | WARN("BLOCK_COMMENT_STYLE", |
| 4040 | "Block comments should align the * on each line\n" . $hereprev); |
| 4041 | } |
| 4042 | } |
| 4043 | |
| 4044 | # check for missing blank lines after struct/union declarations |
| 4045 | # with exceptions for various attributes and macros |
| 4046 | if ($prevline =~ /^[\+ ]};?\s*$/ && |
| 4047 | $line =~ /^\+/ && |
| 4048 | !($line =~ /^\+\s*$/ || |
| 4049 | $line =~ /^\+\s*EXPORT_SYMBOL/ || |
| 4050 | $line =~ /^\+\s*MODULE_/i || |
| 4051 | $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || |
| 4052 | $line =~ /^\+[a-z_]*init/ || |
| 4053 | $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || |
| 4054 | $line =~ /^\+\s*DECLARE/ || |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4055 | $line =~ /^\+\s*builtin_[\w_]*driver/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4056 | $line =~ /^\+\s*__setup/)) { |
| 4057 | if (CHK("LINE_SPACING", |
| 4058 | "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && |
| 4059 | $fix) { |
| 4060 | fix_insert_line($fixlinenr, "\+"); |
| 4061 | } |
| 4062 | } |
| 4063 | |
| 4064 | # check for multiple consecutive blank lines |
| 4065 | if ($prevline =~ /^[\+ ]\s*$/ && |
| 4066 | $line =~ /^\+\s*$/ && |
| 4067 | $last_blank_line != ($linenr - 1)) { |
| 4068 | if (CHK("LINE_SPACING", |
| 4069 | "Please don't use multiple blank lines\n" . $hereprev) && |
| 4070 | $fix) { |
| 4071 | fix_delete_line($fixlinenr, $rawline); |
| 4072 | } |
| 4073 | |
| 4074 | $last_blank_line = $linenr; |
| 4075 | } |
| 4076 | |
| 4077 | # check for missing blank lines after declarations |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4078 | # (declarations must have the same indentation and not be at the start of line) |
| 4079 | if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) { |
| 4080 | # use temporaries |
| 4081 | my $sl = $sline; |
| 4082 | my $pl = $prevline; |
| 4083 | # remove $Attribute/$Sparse uses to simplify comparisons |
| 4084 | $sl =~ s/\b(?:$Attribute|$Sparse)\b//g; |
| 4085 | $pl =~ s/\b(?:$Attribute|$Sparse)\b//g; |
| 4086 | if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4087 | # function pointer declarations |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4088 | $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4089 | # foo bar; where foo is some local typedef or #define |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4090 | $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4091 | # known declaration macros |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4092 | $pl =~ /^\+\s+$declaration_macros/) && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4093 | # for "else if" which can look like "$Ident $Ident" |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4094 | !($pl =~ /^\+\s+$c90_Keywords\b/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4095 | # other possible extensions of declaration lines |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4096 | $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4097 | # not starting a section or a macro "\" extended line |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4098 | $pl =~ /(?:\{\s*|\\)$/) && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4099 | # looks like a declaration |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4100 | !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4101 | # function pointer declarations |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4102 | $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4103 | # foo bar; where foo is some local typedef or #define |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4104 | $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4105 | # known declaration macros |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4106 | $sl =~ /^\+\s+$declaration_macros/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4107 | # start of struct or union or enum |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4108 | $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4109 | # start or end of block or continuation of declaration |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4110 | $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4111 | # bitfield continuation |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4112 | $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4113 | # other possible extensions of declaration lines |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4114 | $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) { |
| 4115 | if (WARN("LINE_SPACING", |
| 4116 | "Missing a blank line after declarations\n" . $hereprev) && |
| 4117 | $fix) { |
| 4118 | fix_insert_line($fixlinenr, "\+"); |
| 4119 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4120 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4123 | # check for spaces at the beginning of a line. |
| 4124 | # Exceptions: |
| 4125 | # 1) within comments |
| 4126 | # 2) indented preprocessor commands |
| 4127 | # 3) hanging labels |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4128 | if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4129 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4130 | if (WARN("LEADING_SPACE", |
| 4131 | "please, no spaces at the start of a line\n" . $herevet) && |
| 4132 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4133 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4134 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | # check we are in a valid C source file if not then ignore this hunk |
| 4138 | next if ($realfile !~ /\.(h|c)$/); |
| 4139 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4140 | # check for unusual line ending [ or ( |
| 4141 | if ($line =~ /^\+.*([\[\(])\s*$/) { |
| 4142 | CHK("OPEN_ENDED_LINE", |
| 4143 | "Lines should not end with a '$1'\n" . $herecurr); |
| 4144 | } |
| 4145 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4146 | # check if this appears to be the start function declaration, save the name |
| 4147 | if ($sline =~ /^\+\{\s*$/ && |
| 4148 | $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { |
| 4149 | $context_function = $1; |
| 4150 | } |
| 4151 | |
| 4152 | # check if this appears to be the end of function declaration |
| 4153 | if ($sline =~ /^\+\}\s*$/) { |
| 4154 | undef $context_function; |
| 4155 | } |
| 4156 | |
| 4157 | # check indentation of any line with a bare else |
| 4158 | # (but not if it is a multiple line "if (foo) return bar; else return baz;") |
| 4159 | # if the previous line is a break or return and is indented 1 tab more... |
| 4160 | if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { |
| 4161 | my $tabs = length($1) + 1; |
| 4162 | if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || |
| 4163 | ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && |
| 4164 | defined $lines[$linenr] && |
| 4165 | $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { |
| 4166 | WARN("UNNECESSARY_ELSE", |
| 4167 | "else is not generally useful after a break or return\n" . $hereprev); |
| 4168 | } |
| 4169 | } |
| 4170 | |
| 4171 | # check indentation of a line with a break; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4172 | # if the previous line is a goto, return or break |
| 4173 | # and is indented the same # of tabs |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4174 | if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { |
| 4175 | my $tabs = $1; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4176 | if ($prevline =~ /^\+$tabs(goto|return|break)\b/) { |
| 4177 | if (WARN("UNNECESSARY_BREAK", |
| 4178 | "break is not useful after a $1\n" . $hereprev) && |
| 4179 | $fix) { |
| 4180 | fix_delete_line($fixlinenr, $rawline); |
| 4181 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4182 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4185 | # check for RCS/CVS revision markers |
| 4186 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
| 4187 | WARN("CVS_KEYWORD", |
| 4188 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
| 4189 | } |
| 4190 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4191 | # check for old HOTPLUG __dev<foo> section markings |
| 4192 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 4193 | WARN("HOTPLUG_SECTION", |
| 4194 | "Using $1 is unnecessary\n" . $herecurr); |
| 4195 | } |
| 4196 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4197 | # Check for potential 'bare' types |
| 4198 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 4199 | $realline_next); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4200 | #print "LINE<$line>\n"; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4201 | if ($linenr > $suppress_statement && |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4202 | $realcnt && $sline =~ /.\s*\S/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4203 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 4204 | ctx_statement_block($linenr, $realcnt, 0); |
| 4205 | $stat =~ s/\n./\n /g; |
| 4206 | $cond =~ s/\n./\n /g; |
| 4207 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4208 | #print "linenr<$linenr> <$stat>\n"; |
| 4209 | # If this statement has no statement boundaries within |
| 4210 | # it there is no point in retrying a statement scan |
| 4211 | # until we hit end of it. |
| 4212 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 4213 | if ($frag !~ /(?:{|;)/) { |
| 4214 | #print "skip<$line_nr_next>\n"; |
| 4215 | $suppress_statement = $line_nr_next; |
| 4216 | } |
| 4217 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4218 | # Find the real next line. |
| 4219 | $realline_next = $line_nr_next; |
| 4220 | if (defined $realline_next && |
| 4221 | (!defined $lines[$realline_next - 1] || |
| 4222 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 4223 | $realline_next++; |
| 4224 | } |
| 4225 | |
| 4226 | my $s = $stat; |
| 4227 | $s =~ s/{.*$//s; |
| 4228 | |
| 4229 | # Ignore goto labels. |
| 4230 | if ($s =~ /$Ident:\*$/s) { |
| 4231 | |
| 4232 | # Ignore functions being called |
| 4233 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
| 4234 | |
| 4235 | } elsif ($s =~ /^.\s*else\b/s) { |
| 4236 | |
| 4237 | # declarations always start with types |
| 4238 | } 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) { |
| 4239 | my $type = $1; |
| 4240 | $type =~ s/\s+/ /g; |
| 4241 | possible($type, "A:" . $s); |
| 4242 | |
| 4243 | # definitions in global scope can only start with types |
| 4244 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
| 4245 | possible($1, "B:" . $s); |
| 4246 | } |
| 4247 | |
| 4248 | # any (foo ... *) is a pointer cast, and foo is a type |
| 4249 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
| 4250 | possible($1, "C:" . $s); |
| 4251 | } |
| 4252 | |
| 4253 | # Check for any sort of function declaration. |
| 4254 | # int foo(something bar, other baz); |
| 4255 | # void (*store_gdt)(x86_descr_ptr *); |
| 4256 | if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { |
| 4257 | my ($name_len) = length($1); |
| 4258 | |
| 4259 | my $ctx = $s; |
| 4260 | substr($ctx, 0, $name_len + 1, ''); |
| 4261 | $ctx =~ s/\)[^\)]*$//; |
| 4262 | |
| 4263 | for my $arg (split(/\s*,\s*/, $ctx)) { |
| 4264 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { |
| 4265 | |
| 4266 | possible($1, "D:" . $s); |
| 4267 | } |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | } |
| 4272 | |
| 4273 | # |
| 4274 | # Checks which may be anchored in the context. |
| 4275 | # |
| 4276 | |
| 4277 | # Check for switch () and associated case and default |
| 4278 | # statements should be at the same indent. |
| 4279 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 4280 | my $err = ''; |
| 4281 | my $sep = ''; |
| 4282 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 4283 | shift(@ctx); |
| 4284 | for my $ctx (@ctx) { |
| 4285 | my ($clen, $cindent) = line_stats($ctx); |
| 4286 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 4287 | $indent != $cindent) { |
| 4288 | $err .= "$sep$ctx\n"; |
| 4289 | $sep = ''; |
| 4290 | } else { |
| 4291 | $sep = "[...]\n"; |
| 4292 | } |
| 4293 | } |
| 4294 | if ($err ne '') { |
| 4295 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 4296 | "switch and case should be at the same indent\n$hereline$err"); |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 4301 | # or if that brace on the next line is for something else |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4302 | if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4303 | my $pre_ctx = "$1$2"; |
| 4304 | |
| 4305 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4306 | |
| 4307 | if ($line =~ /^\+\t{6,}/) { |
| 4308 | WARN("DEEP_INDENTATION", |
| 4309 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 4310 | } |
| 4311 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4312 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 4313 | my $ctx = join("\n", @ctx); |
| 4314 | |
| 4315 | my $ctx_ln = $linenr; |
| 4316 | my $ctx_skip = $realcnt; |
| 4317 | |
| 4318 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 4319 | defined $lines[$ctx_ln - 1] && |
| 4320 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 4321 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 4322 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
| 4323 | $ctx_ln++; |
| 4324 | } |
| 4325 | |
| 4326 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 4327 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; |
| 4328 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4329 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4330 | ERROR("OPEN_BRACE", |
| 4331 | "that open brace { should be on the previous line\n" . |
| 4332 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
| 4333 | } |
| 4334 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 4335 | $ctx =~ /\)\s*\;\s*$/ && |
| 4336 | defined $lines[$ctx_ln - 1]) |
| 4337 | { |
| 4338 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 4339 | if ($nindent > $indent) { |
| 4340 | WARN("TRAILING_SEMICOLON", |
| 4341 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
| 4342 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
| 4343 | } |
| 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | # Check relative indent for conditionals and blocks. |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4348 | if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4349 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 4350 | ctx_statement_block($linenr, $realcnt, 0) |
| 4351 | if (!defined $stat); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4352 | my ($s, $c) = ($stat, $cond); |
| 4353 | |
| 4354 | substr($s, 0, length($c), ''); |
| 4355 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4356 | # remove inline comments |
| 4357 | $s =~ s/$;/ /g; |
| 4358 | $c =~ s/$;/ /g; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4359 | |
| 4360 | # Find out how long the conditional actually is. |
| 4361 | my @newlines = ($c =~ /\n/gs); |
| 4362 | my $cond_lines = 1 + $#newlines; |
| 4363 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4364 | # Make sure we remove the line prefixes as we have |
| 4365 | # none on the first line, and are going to readd them |
| 4366 | # where necessary. |
| 4367 | $s =~ s/\n./\n/gs; |
| 4368 | while ($s =~ /\n\s+\\\n/) { |
| 4369 | $cond_lines += $s =~ s/\n\s+\\\n/\n/g; |
| 4370 | } |
| 4371 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4372 | # We want to check the first line inside the block |
| 4373 | # starting at the end of the conditional, so remove: |
| 4374 | # 1) any blank line termination |
| 4375 | # 2) any opening brace { on end of the line |
| 4376 | # 3) any do (...) { |
| 4377 | my $continuation = 0; |
| 4378 | my $check = 0; |
| 4379 | $s =~ s/^.*\bdo\b//; |
| 4380 | $s =~ s/^\s*{//; |
| 4381 | if ($s =~ s/^\s*\\//) { |
| 4382 | $continuation = 1; |
| 4383 | } |
| 4384 | if ($s =~ s/^\s*?\n//) { |
| 4385 | $check = 1; |
| 4386 | $cond_lines++; |
| 4387 | } |
| 4388 | |
| 4389 | # Also ignore a loop construct at the end of a |
| 4390 | # preprocessor statement. |
| 4391 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 4392 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 4393 | $check = 0; |
| 4394 | } |
| 4395 | |
| 4396 | my $cond_ptr = -1; |
| 4397 | $continuation = 0; |
| 4398 | while ($cond_ptr != $cond_lines) { |
| 4399 | $cond_ptr = $cond_lines; |
| 4400 | |
| 4401 | # If we see an #else/#elif then the code |
| 4402 | # is not linear. |
| 4403 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 4404 | $check = 0; |
| 4405 | } |
| 4406 | |
| 4407 | # Ignore: |
| 4408 | # 1) blank lines, they should be at 0, |
| 4409 | # 2) preprocessor lines, and |
| 4410 | # 3) labels. |
| 4411 | if ($continuation || |
| 4412 | $s =~ /^\s*?\n/ || |
| 4413 | $s =~ /^\s*#\s*?/ || |
| 4414 | $s =~ /^\s*$Ident\s*:/) { |
| 4415 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
| 4416 | if ($s =~ s/^.*?\n//) { |
| 4417 | $cond_lines++; |
| 4418 | } |
| 4419 | } |
| 4420 | } |
| 4421 | |
| 4422 | my (undef, $sindent) = line_stats("+" . $s); |
| 4423 | my $stat_real = raw_line($linenr, $cond_lines); |
| 4424 | |
| 4425 | # Check if either of these lines are modified, else |
| 4426 | # this is not this patch's fault. |
| 4427 | if (!defined($stat_real) || |
| 4428 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 4429 | $check = 0; |
| 4430 | } |
| 4431 | if (defined($stat_real) && $cond_lines > 1) { |
| 4432 | $stat_real = "[...]\n$stat_real"; |
| 4433 | } |
| 4434 | |
| 4435 | #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"; |
| 4436 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4437 | if ($check && $s ne '' && |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4438 | (($sindent % $tabsize) != 0 || |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4439 | ($sindent < $indent) || |
| 4440 | ($sindent == $indent && |
| 4441 | ($s !~ /^\s*(?:\}|\{|else\b)/)) || |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4442 | ($sindent > $indent + $tabsize))) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4443 | WARN("SUSPECT_CODE_INDENT", |
| 4444 | "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | # Track the 'values' across context and added lines. |
| 4449 | my $opline = $line; $opline =~ s/^./ /; |
| 4450 | my ($curr_values, $curr_vars) = |
| 4451 | annotate_values($opline . "\n", $prev_values); |
| 4452 | $curr_values = $prev_values . $curr_values; |
| 4453 | if ($dbg_values) { |
| 4454 | my $outline = $opline; $outline =~ s/\t/ /g; |
| 4455 | print "$linenr > .$outline\n"; |
| 4456 | print "$linenr > $curr_values\n"; |
| 4457 | print "$linenr > $curr_vars\n"; |
| 4458 | } |
| 4459 | $prev_values = substr($curr_values, -1); |
| 4460 | |
| 4461 | #ignore lines not being added |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4462 | next if ($line =~ /^[^\+]/); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4463 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4464 | # check for self assignments used to avoid compiler warnings |
| 4465 | # e.g.: int foo = foo, *bar = NULL; |
| 4466 | # struct foo bar = *(&(bar)); |
| 4467 | if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) { |
| 4468 | my $var = $1; |
| 4469 | if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) { |
| 4470 | WARN("SELF_ASSIGNMENT", |
| 4471 | "Do not use self-assignments to avoid compiler warnings\n" . $herecurr); |
| 4472 | } |
| 4473 | } |
| 4474 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4475 | # check for dereferences that span multiple lines |
| 4476 | if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && |
| 4477 | $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { |
| 4478 | $prevline =~ /($Lval\s*(?:\.|->))\s*$/; |
| 4479 | my $ref = $1; |
| 4480 | $line =~ /^.\s*($Lval)/; |
| 4481 | $ref .= $1; |
| 4482 | $ref =~ s/\s//g; |
| 4483 | WARN("MULTILINE_DEREFERENCE", |
| 4484 | "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); |
| 4485 | } |
| 4486 | |
| 4487 | # check for declarations of signed or unsigned without int |
| 4488 | while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { |
| 4489 | my $type = $1; |
| 4490 | my $var = $2; |
| 4491 | $var = "" if (!defined $var); |
| 4492 | if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { |
| 4493 | my $sign = $1; |
| 4494 | my $pointer = $2; |
| 4495 | |
| 4496 | $pointer = "" if (!defined $pointer); |
| 4497 | |
| 4498 | if (WARN("UNSPECIFIED_INT", |
| 4499 | "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && |
| 4500 | $fix) { |
| 4501 | my $decl = trim($sign) . " int "; |
| 4502 | my $comp_pointer = $pointer; |
| 4503 | $comp_pointer =~ s/\s//g; |
| 4504 | $decl .= $comp_pointer; |
| 4505 | $decl = rtrim($decl) if ($var eq ""); |
| 4506 | $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; |
| 4507 | } |
| 4508 | } |
| 4509 | } |
| 4510 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4511 | # TEST: allow direct testing of the type matcher. |
| 4512 | if ($dbg_type) { |
| 4513 | if ($line =~ /^.\s*$Declare\s*$/) { |
| 4514 | ERROR("TEST_TYPE", |
| 4515 | "TEST: is type\n" . $herecurr); |
| 4516 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
| 4517 | ERROR("TEST_NOT_TYPE", |
| 4518 | "TEST: is not type ($1 is)\n". $herecurr); |
| 4519 | } |
| 4520 | next; |
| 4521 | } |
| 4522 | # TEST: allow direct testing of the attribute matcher. |
| 4523 | if ($dbg_attr) { |
| 4524 | if ($line =~ /^.\s*$Modifier\s*$/) { |
| 4525 | ERROR("TEST_ATTR", |
| 4526 | "TEST: is attr\n" . $herecurr); |
| 4527 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
| 4528 | ERROR("TEST_NOT_ATTR", |
| 4529 | "TEST: is not attr ($1 is)\n". $herecurr); |
| 4530 | } |
| 4531 | next; |
| 4532 | } |
| 4533 | |
| 4534 | # check for initialisation to aggregates open brace on the next line |
| 4535 | if ($line =~ /^.\s*{/ && |
| 4536 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4537 | if (ERROR("OPEN_BRACE", |
| 4538 | "that open brace { should be on the previous line\n" . $hereprev) && |
| 4539 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4540 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4541 | fix_delete_line($fixlinenr, $rawline); |
| 4542 | my $fixedline = $prevrawline; |
| 4543 | $fixedline =~ s/\s*=\s*$/ = {/; |
| 4544 | fix_insert_line($fixlinenr, $fixedline); |
| 4545 | $fixedline = $line; |
| 4546 | $fixedline =~ s/^(.\s*)\{\s*/$1/; |
| 4547 | fix_insert_line($fixlinenr, $fixedline); |
| 4548 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | # |
| 4552 | # Checks which are anchored on the added line. |
| 4553 | # |
| 4554 | |
| 4555 | # check for malformed paths in #include statements (uses RAW line) |
| 4556 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
| 4557 | my $path = $1; |
| 4558 | if ($path =~ m{//}) { |
| 4559 | ERROR("MALFORMED_INCLUDE", |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4560 | "malformed #include filename\n" . $herecurr); |
| 4561 | } |
| 4562 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 4563 | ERROR("UAPI_INCLUDE", |
| 4564 | "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4565 | } |
| 4566 | } |
| 4567 | |
| 4568 | # no C99 // comments |
| 4569 | if ($line =~ m{//}) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4570 | if (ERROR("C99_COMMENTS", |
| 4571 | "do not use C99 // comments\n" . $herecurr) && |
| 4572 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4573 | my $line = $fixed[$fixlinenr]; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4574 | if ($line =~ /\/\/(.*)$/) { |
| 4575 | my $comment = trim($1); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4576 | $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4577 | } |
| 4578 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4579 | } |
| 4580 | # Remove C99 comments. |
| 4581 | $line =~ s@//.*@@; |
| 4582 | $opline =~ s@//.*@@; |
| 4583 | |
| 4584 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 4585 | # the whole statement. |
| 4586 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 4587 | if (defined $realline_next && |
| 4588 | exists $lines[$realline_next - 1] && |
| 4589 | !defined $suppress_export{$realline_next} && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4590 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4591 | # Handle definitions which produce identifiers with |
| 4592 | # a prefix: |
| 4593 | # XXX(foo); |
| 4594 | # EXPORT_SYMBOL(something_foo); |
| 4595 | my $name = $1; |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 4596 | $name =~ s/^\s*($Ident).*/$1/; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4597 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4598 | $name =~ /^${Ident}_$2/) { |
| 4599 | #print "FOO C name<$name>\n"; |
| 4600 | $suppress_export{$realline_next} = 1; |
| 4601 | |
| 4602 | } elsif ($stat !~ /(?: |
| 4603 | \n.}\s*$| |
| 4604 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 4605 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 4606 | ^.LIST_HEAD\(\Q$name\E\)| |
| 4607 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 4608 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
| 4609 | )/x) { |
| 4610 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 4611 | $suppress_export{$realline_next} = 2; |
| 4612 | } else { |
| 4613 | $suppress_export{$realline_next} = 1; |
| 4614 | } |
| 4615 | } |
| 4616 | if (!defined $suppress_export{$linenr} && |
| 4617 | $prevline =~ /^.\s*$/ && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4618 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4619 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 4620 | $suppress_export{$linenr} = 2; |
| 4621 | } |
| 4622 | if (defined $suppress_export{$linenr} && |
| 4623 | $suppress_export{$linenr} == 2) { |
| 4624 | WARN("EXPORT_SYMBOL", |
| 4625 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
| 4626 | } |
| 4627 | |
| 4628 | # check for global initialisers. |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4629 | if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ && |
| 4630 | !exclude_global_initialisers($realfile)) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4631 | if (ERROR("GLOBAL_INITIALISERS", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4632 | "do not initialise globals to $1\n" . $herecurr) && |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4633 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4634 | $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4635 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4636 | } |
| 4637 | # check for static initialisers. |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4638 | if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4639 | if (ERROR("INITIALISED_STATIC", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4640 | "do not initialise statics to $1\n" . |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4641 | $herecurr) && |
| 4642 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4643 | $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4644 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4647 | # check for misordered declarations of char/short/int/long with signed/unsigned |
| 4648 | while ($sline =~ m{(\b$TypeMisordered\b)}g) { |
| 4649 | my $tmp = trim($1); |
| 4650 | WARN("MISORDERED_TYPE", |
| 4651 | "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); |
| 4652 | } |
| 4653 | |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4654 | # check for unnecessary <signed> int declarations of short/long/long long |
| 4655 | while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { |
| 4656 | my $type = trim($1); |
| 4657 | next if ($type !~ /\bint\b/); |
| 4658 | next if ($type !~ /\b(?:short|long\s+long|long)\b/); |
| 4659 | my $new_type = $type; |
| 4660 | $new_type =~ s/\b\s*int\s*\b/ /; |
| 4661 | $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; |
| 4662 | $new_type =~ s/^const\s+//; |
| 4663 | $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); |
| 4664 | $new_type = "const $new_type" if ($type =~ /^const\b/); |
| 4665 | $new_type =~ s/\s+/ /g; |
| 4666 | $new_type = trim($new_type); |
| 4667 | if (WARN("UNNECESSARY_INT", |
| 4668 | "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && |
| 4669 | $fix) { |
| 4670 | $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; |
| 4671 | } |
| 4672 | } |
| 4673 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4674 | # check for static const char * arrays. |
| 4675 | if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { |
| 4676 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 4677 | "static const char * array should probably be static const char * const\n" . |
| 4678 | $herecurr); |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | # check for initialized const char arrays that should be static const |
| 4682 | if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { |
| 4683 | if (WARN("STATIC_CONST_CHAR_ARRAY", |
| 4684 | "const array should probably be static const\n" . $herecurr) && |
| 4685 | $fix) { |
| 4686 | $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; |
| 4687 | } |
| 4688 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4689 | |
| 4690 | # check for static char foo[] = "bar" declarations. |
| 4691 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
| 4692 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 4693 | "static char array declaration should probably be static const char\n" . |
| 4694 | $herecurr); |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4695 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4696 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4697 | # check for const <foo> const where <foo> is not a pointer or array type |
| 4698 | if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { |
| 4699 | my $found = $1; |
| 4700 | if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { |
| 4701 | WARN("CONST_CONST", |
| 4702 | "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); |
| 4703 | } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { |
| 4704 | WARN("CONST_CONST", |
| 4705 | "'const $found const' should probably be 'const $found'\n" . $herecurr); |
| 4706 | } |
| 4707 | } |
| 4708 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4709 | # check for const static or static <non ptr type> const declarations |
| 4710 | # prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const' |
| 4711 | if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ || |
| 4712 | $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) { |
| 4713 | if (WARN("STATIC_CONST", |
| 4714 | "Move const after static - use 'static const $1'\n" . $herecurr) && |
| 4715 | $fix) { |
| 4716 | $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/; |
| 4717 | $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/; |
| 4718 | } |
| 4719 | } |
| 4720 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4721 | # check for non-global char *foo[] = {"bar", ...} declarations. |
| 4722 | if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { |
| 4723 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 4724 | "char * array declaration might be better as static const\n" . |
| 4725 | $herecurr); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4726 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4727 | |
| 4728 | # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) |
| 4729 | if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { |
| 4730 | my $array = $1; |
| 4731 | if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) { |
| 4732 | my $array_div = $1; |
| 4733 | if (WARN("ARRAY_SIZE", |
| 4734 | "Prefer ARRAY_SIZE($array)\n" . $herecurr) && |
| 4735 | $fix) { |
| 4736 | $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; |
| 4737 | } |
| 4738 | } |
| 4739 | } |
| 4740 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4741 | # check for function declarations without arguments like "int foo()" |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4742 | if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4743 | if (ERROR("FUNCTION_WITHOUT_ARGS", |
| 4744 | "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && |
| 4745 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4746 | $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4747 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | # check for new typedefs, only function parameters and sparse annotations |
| 4751 | # make sense. |
| 4752 | if ($line =~ /\btypedef\s/ && |
| 4753 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
| 4754 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
| 4755 | $line !~ /\b$typeTypedefs\b/ && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4756 | $line !~ /\b__bitwise\b/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4757 | WARN("NEW_TYPEDEFS", |
| 4758 | "do not add new typedefs\n" . $herecurr); |
| 4759 | } |
| 4760 | |
| 4761 | # * goes on variable not on type |
| 4762 | # (char*[ const]) |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4763 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 4764 | #print "AA<$1>\n"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4765 | my ($ident, $from, $to) = ($1, $2, $2); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4766 | |
| 4767 | # Should start with a space. |
| 4768 | $to =~ s/^(\S)/ $1/; |
| 4769 | # Should not end with a space. |
| 4770 | $to =~ s/\s+$//; |
| 4771 | # '*'s should not have spaces between. |
| 4772 | while ($to =~ s/\*\s+\*/\*\*/) { |
| 4773 | } |
| 4774 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4775 | ## print "1: from<$from> to<$to> ident<$ident>\n"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4776 | if ($from ne $to) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4777 | if (ERROR("POINTER_LOCATION", |
| 4778 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && |
| 4779 | $fix) { |
| 4780 | my $sub_from = $ident; |
| 4781 | my $sub_to = $ident; |
| 4782 | $sub_to =~ s/\Q$from\E/$to/; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4783 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4784 | s@\Q$sub_from\E@$sub_to@; |
| 4785 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4786 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4787 | } |
| 4788 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 4789 | #print "BB<$1>\n"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4790 | my ($match, $from, $to, $ident) = ($1, $2, $2, $3); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4791 | |
| 4792 | # Should start with a space. |
| 4793 | $to =~ s/^(\S)/ $1/; |
| 4794 | # Should not end with a space. |
| 4795 | $to =~ s/\s+$//; |
| 4796 | # '*'s should not have spaces between. |
| 4797 | while ($to =~ s/\*\s+\*/\*\*/) { |
| 4798 | } |
| 4799 | # Modifiers should have spaces. |
| 4800 | $to =~ s/(\b$Modifier$)/$1 /; |
| 4801 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4802 | ## print "2: from<$from> to<$to> ident<$ident>\n"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4803 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4804 | if (ERROR("POINTER_LOCATION", |
| 4805 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && |
| 4806 | $fix) { |
| 4807 | |
| 4808 | my $sub_from = $match; |
| 4809 | my $sub_to = $match; |
| 4810 | $sub_to =~ s/\Q$from\E/$to/; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4811 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4812 | s@\Q$sub_from\E@$sub_to@; |
| 4813 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4814 | } |
| 4815 | } |
| 4816 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4817 | # avoid BUG() or BUG_ON() |
| 4818 | if ($line =~ /\b(?:BUG|BUG_ON)\b/) { |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4819 | my $msg_level = \&WARN; |
| 4820 | $msg_level = \&CHK if ($file); |
| 4821 | &{$msg_level}("AVOID_BUG", |
| 4822 | "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4823 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4824 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4825 | # avoid LINUX_VERSION_CODE |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4826 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
| 4827 | WARN("LINUX_VERSION_CODE", |
| 4828 | "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
| 4829 | } |
| 4830 | |
| 4831 | # check for uses of printk_ratelimit |
| 4832 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
| 4833 | WARN("PRINTK_RATELIMITED", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4834 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4837 | # printk should use KERN_* levels |
| 4838 | if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { |
| 4839 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 4840 | "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4843 | # prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL> |
| 4844 | if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) { |
| 4845 | my $printk = $1; |
| 4846 | my $modifier = $2; |
| 4847 | my $orig = $3; |
| 4848 | $modifier = "" if (!defined($modifier)); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4849 | my $level = lc($orig); |
| 4850 | $level = "warn" if ($level eq "warning"); |
| 4851 | my $level2 = $level; |
| 4852 | $level2 = "dbg" if ($level eq "debug"); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4853 | $level .= $modifier; |
| 4854 | $level2 .= $modifier; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4855 | WARN("PREFER_PR_LEVEL", |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4856 | "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4859 | # prefer dev_<level> to dev_printk(KERN_<LEVEL> |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4860 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 4861 | my $orig = $1; |
| 4862 | my $level = lc($orig); |
| 4863 | $level = "warn" if ($level eq "warning"); |
| 4864 | $level = "dbg" if ($level eq "debug"); |
| 4865 | WARN("PREFER_DEV_LEVEL", |
| 4866 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 4867 | } |
| 4868 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4869 | # trace_printk should not be used in production code. |
| 4870 | if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) { |
| 4871 | WARN("TRACE_PRINTK", |
| 4872 | "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr); |
| 4873 | } |
| 4874 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4875 | # ENOSYS means "bad syscall nr" and nothing else. This will have a small |
| 4876 | # number of false positives, but assembly files are not checked, so at |
| 4877 | # least the arch entry code will not trigger this warning. |
| 4878 | if ($line =~ /\bENOSYS\b/) { |
| 4879 | WARN("ENOSYS", |
| 4880 | "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); |
| 4881 | } |
| 4882 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4883 | # ENOTSUPP is not a standard error code and should be avoided in new patches. |
| 4884 | # Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. |
| 4885 | # Similarly to ENOSYS warning a small number of false positives is expected. |
| 4886 | if (!$file && $line =~ /\bENOTSUPP\b/) { |
| 4887 | if (WARN("ENOTSUPP", |
| 4888 | "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && |
| 4889 | $fix) { |
| 4890 | $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; |
| 4891 | } |
| 4892 | } |
| 4893 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4894 | # function brace can't be on same line, except for #defines of do while, |
| 4895 | # or if closed on same line |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4896 | if ($perl_version_ok && |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4897 | $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && |
| 4898 | $sline !~ /\#\s*define\b.*do\s*\{/ && |
| 4899 | $sline !~ /}/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4900 | if (ERROR("OPEN_BRACE", |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4901 | "open brace '{' following function definitions go on the next line\n" . $herecurr) && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4902 | $fix) { |
| 4903 | fix_delete_line($fixlinenr, $rawline); |
| 4904 | my $fixed_line = $rawline; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 4905 | $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4906 | my $line1 = $1; |
| 4907 | my $line2 = $2; |
| 4908 | fix_insert_line($fixlinenr, ltrim($line1)); |
| 4909 | fix_insert_line($fixlinenr, "\+{"); |
| 4910 | if ($line2 !~ /^\s*$/) { |
| 4911 | fix_insert_line($fixlinenr, "\+\t" . trim($line2)); |
| 4912 | } |
| 4913 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | # open braces for enum, union and struct go on the same line. |
| 4917 | if ($line =~ /^.\s*{/ && |
| 4918 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4919 | if (ERROR("OPEN_BRACE", |
| 4920 | "open brace '{' following $1 go on the same line\n" . $hereprev) && |
| 4921 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4922 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4923 | fix_delete_line($fixlinenr, $rawline); |
| 4924 | my $fixedline = rtrim($prevrawline) . " {"; |
| 4925 | fix_insert_line($fixlinenr, $fixedline); |
| 4926 | $fixedline = $rawline; |
| 4927 | $fixedline =~ s/^(.\s*)\{\s*/$1\t/; |
| 4928 | if ($fixedline !~ /^\+\s*$/) { |
| 4929 | fix_insert_line($fixlinenr, $fixedline); |
| 4930 | } |
| 4931 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | # missing space after union, struct or enum definition |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4935 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { |
| 4936 | if (WARN("SPACING", |
| 4937 | "missing space after $1 definition\n" . $herecurr) && |
| 4938 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4939 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4940 | s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | # Function pointer declarations |
| 4945 | # check spacing between type, funcptr, and args |
| 4946 | # canonical declaration is "type (*funcptr)(args...)" |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4947 | if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4948 | my $declare = $1; |
| 4949 | my $pre_pointer_space = $2; |
| 4950 | my $post_pointer_space = $3; |
| 4951 | my $funcname = $4; |
| 4952 | my $post_funcname_space = $5; |
| 4953 | my $pre_args_space = $6; |
| 4954 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4955 | # the $Declare variable will capture all spaces after the type |
| 4956 | # so check it for a missing trailing missing space but pointer return types |
| 4957 | # don't need a space so don't warn for those. |
| 4958 | my $post_declare_space = ""; |
| 4959 | if ($declare =~ /(\s+)$/) { |
| 4960 | $post_declare_space = $1; |
| 4961 | $declare = rtrim($declare); |
| 4962 | } |
| 4963 | if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4964 | WARN("SPACING", |
| 4965 | "missing space after return type\n" . $herecurr); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4966 | $post_declare_space = " "; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | # unnecessary space "type (*funcptr)(args...)" |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4970 | # This test is not currently implemented because these declarations are |
| 4971 | # equivalent to |
| 4972 | # int foo(int bar, ...) |
| 4973 | # and this is form shouldn't/doesn't generate a checkpatch warning. |
| 4974 | # |
| 4975 | # elsif ($declare =~ /\s{2,}$/) { |
| 4976 | # WARN("SPACING", |
| 4977 | # "Multiple spaces after return type\n" . $herecurr); |
| 4978 | # } |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4979 | |
| 4980 | # unnecessary space "type ( *funcptr)(args...)" |
| 4981 | if (defined $pre_pointer_space && |
| 4982 | $pre_pointer_space =~ /^\s/) { |
| 4983 | WARN("SPACING", |
| 4984 | "Unnecessary space after function pointer open parenthesis\n" . $herecurr); |
| 4985 | } |
| 4986 | |
| 4987 | # unnecessary space "type (* funcptr)(args...)" |
| 4988 | if (defined $post_pointer_space && |
| 4989 | $post_pointer_space =~ /^\s/) { |
| 4990 | WARN("SPACING", |
| 4991 | "Unnecessary space before function pointer name\n" . $herecurr); |
| 4992 | } |
| 4993 | |
| 4994 | # unnecessary space "type (*funcptr )(args...)" |
| 4995 | if (defined $post_funcname_space && |
| 4996 | $post_funcname_space =~ /^\s/) { |
| 4997 | WARN("SPACING", |
| 4998 | "Unnecessary space after function pointer name\n" . $herecurr); |
| 4999 | } |
| 5000 | |
| 5001 | # unnecessary space "type (*funcptr) (args...)" |
| 5002 | if (defined $pre_args_space && |
| 5003 | $pre_args_space =~ /^\s/) { |
| 5004 | WARN("SPACING", |
| 5005 | "Unnecessary space before function pointer arguments\n" . $herecurr); |
| 5006 | } |
| 5007 | |
| 5008 | if (show_type("SPACING") && $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5009 | $fixed[$fixlinenr] =~ |
| 5010 | s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5011 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | # check for spacing round square brackets; allowed: |
| 5015 | # 1. with a type on the left -- int [] a; |
| 5016 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 5017 | # 3. inside a curly brace -- = { [0...10] = 5 } |
| 5018 | while ($line =~ /(.*?\s)\[/g) { |
| 5019 | my ($where, $prefix) = ($-[1], $1); |
| 5020 | if ($prefix !~ /$Type\s+$/ && |
| 5021 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5022 | $prefix !~ /[{,:]\s+$/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5023 | if (ERROR("BRACKET_SPACE", |
| 5024 | "space prohibited before open square bracket '['\n" . $herecurr) && |
| 5025 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5026 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5027 | s/^(\+.*?)\s+\[/$1\[/; |
| 5028 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | # check for spaces between functions and their parentheses. |
| 5033 | while ($line =~ /($Ident)\s+\(/g) { |
| 5034 | my $name = $1; |
| 5035 | my $ctx_before = substr($line, 0, $-[1]); |
| 5036 | my $ctx = "$ctx_before$name"; |
| 5037 | |
| 5038 | # Ignore those directives where spaces _are_ permitted. |
| 5039 | if ($name =~ /^(?: |
| 5040 | if|for|while|switch|return|case| |
| 5041 | volatile|__volatile__| |
| 5042 | __attribute__|format|__extension__| |
| 5043 | asm|__asm__)$/x) |
| 5044 | { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5045 | # cpp #define statements have non-optional spaces, ie |
| 5046 | # if there is a space between the name and the open |
| 5047 | # parenthesis it is simply not a parameter group. |
| 5048 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
| 5049 | |
| 5050 | # cpp #elif statement condition may start with a ( |
| 5051 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
| 5052 | |
| 5053 | # If this whole things ends with a type its most |
| 5054 | # likely a typedef for a function. |
| 5055 | } elsif ($ctx =~ /$Type$/) { |
| 5056 | |
| 5057 | } else { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5058 | if (WARN("SPACING", |
| 5059 | "space prohibited between function name and open parenthesis '('\n" . $herecurr) && |
| 5060 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5061 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5062 | s/\b$name\s+\(/$name\(/; |
| 5063 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5064 | } |
| 5065 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5066 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5067 | # Check operator spacing. |
| 5068 | if (!($line=~/\#\s*include/)) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5069 | my $fixed_line = ""; |
| 5070 | my $line_fixed = 0; |
| 5071 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5072 | my $ops = qr{ |
| 5073 | <<=|>>=|<=|>=|==|!=| |
| 5074 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 5075 | =>|->|<<|>>|<|>|=|!|~| |
| 5076 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5077 | \?:|\?|: |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5078 | }x; |
| 5079 | my @elements = split(/($ops|;)/, $opline); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5080 | |
| 5081 | ## print("element count: <" . $#elements . ">\n"); |
| 5082 | ## foreach my $el (@elements) { |
| 5083 | ## print("el: <$el>\n"); |
| 5084 | ## } |
| 5085 | |
| 5086 | my @fix_elements = (); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5087 | my $off = 0; |
| 5088 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5089 | foreach my $el (@elements) { |
| 5090 | push(@fix_elements, substr($rawline, $off, length($el))); |
| 5091 | $off += length($el); |
| 5092 | } |
| 5093 | |
| 5094 | $off = 0; |
| 5095 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5096 | my $blank = copy_spacing($opline); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5097 | my $last_after = -1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5098 | |
| 5099 | for (my $n = 0; $n < $#elements; $n += 2) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5100 | |
| 5101 | my $good = $fix_elements[$n] . $fix_elements[$n + 1]; |
| 5102 | |
| 5103 | ## print("n: <$n> good: <$good>\n"); |
| 5104 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5105 | $off += length($elements[$n]); |
| 5106 | |
| 5107 | # Pick up the preceding and succeeding characters. |
| 5108 | my $ca = substr($opline, 0, $off); |
| 5109 | my $cc = ''; |
| 5110 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 5111 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 5112 | } |
| 5113 | my $cb = "$ca$;$cc"; |
| 5114 | |
| 5115 | my $a = ''; |
| 5116 | $a = 'V' if ($elements[$n] ne ''); |
| 5117 | $a = 'W' if ($elements[$n] =~ /\s$/); |
| 5118 | $a = 'C' if ($elements[$n] =~ /$;$/); |
| 5119 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 5120 | $a = 'O' if ($elements[$n] eq ''); |
| 5121 | $a = 'E' if ($ca =~ /^\s*$/); |
| 5122 | |
| 5123 | my $op = $elements[$n + 1]; |
| 5124 | |
| 5125 | my $c = ''; |
| 5126 | if (defined $elements[$n + 2]) { |
| 5127 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 5128 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
| 5129 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
| 5130 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 5131 | $c = 'O' if ($elements[$n + 2] eq ''); |
| 5132 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
| 5133 | } else { |
| 5134 | $c = 'E'; |
| 5135 | } |
| 5136 | |
| 5137 | my $ctx = "${a}x${c}"; |
| 5138 | |
| 5139 | my $at = "(ctx:$ctx)"; |
| 5140 | |
| 5141 | my $ptr = substr($blank, 0, $off) . "^"; |
| 5142 | my $hereptr = "$hereline$ptr\n"; |
| 5143 | |
| 5144 | # Pull out the value of this operator. |
| 5145 | my $op_type = substr($curr_values, $off + 1, 1); |
| 5146 | |
| 5147 | # Get the full operator variant. |
| 5148 | my $opv = $op . substr($curr_vars, $off, 1); |
| 5149 | |
| 5150 | # Ignore operators passed as parameters. |
| 5151 | if ($op_type ne 'V' && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5152 | $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5153 | |
| 5154 | # # Ignore comments |
| 5155 | # } elsif ($op =~ /^$;+$/) { |
| 5156 | |
| 5157 | # ; should have either the end of line or a space or \ after it |
| 5158 | } elsif ($op eq ';') { |
| 5159 | if ($ctx !~ /.x[WEBC]/ && |
| 5160 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5161 | if (ERROR("SPACING", |
| 5162 | "space required after that '$op' $at\n" . $hereptr)) { |
| 5163 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
| 5164 | $line_fixed = 1; |
| 5165 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | # // is a comment |
| 5169 | } elsif ($op eq '//') { |
| 5170 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5171 | # : when part of a bitfield |
| 5172 | } elsif ($opv eq ':B') { |
| 5173 | # skip the bitfield test for now |
| 5174 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5175 | # No spaces for: |
| 5176 | # -> |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5177 | } elsif ($op eq '->') { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5178 | if ($ctx =~ /Wx.|.xW/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5179 | if (ERROR("SPACING", |
| 5180 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
| 5181 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 5182 | if (defined $fix_elements[$n + 2]) { |
| 5183 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5184 | } |
| 5185 | $line_fixed = 1; |
| 5186 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5189 | # , must not have a space before and must have a space on the right. |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5190 | } elsif ($op eq ',') { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5191 | my $rtrim_before = 0; |
| 5192 | my $space_after = 0; |
| 5193 | if ($ctx =~ /Wx./) { |
| 5194 | if (ERROR("SPACING", |
| 5195 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 5196 | $line_fixed = 1; |
| 5197 | $rtrim_before = 1; |
| 5198 | } |
| 5199 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5200 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5201 | if (ERROR("SPACING", |
| 5202 | "space required after that '$op' $at\n" . $hereptr)) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5203 | $line_fixed = 1; |
| 5204 | $last_after = $n; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5205 | $space_after = 1; |
| 5206 | } |
| 5207 | } |
| 5208 | if ($rtrim_before || $space_after) { |
| 5209 | if ($rtrim_before) { |
| 5210 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 5211 | } else { |
| 5212 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
| 5213 | } |
| 5214 | if ($space_after) { |
| 5215 | $good .= " "; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5216 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | # '*' as part of a type definition -- reported already. |
| 5220 | } elsif ($opv eq '*_') { |
| 5221 | #warn "'*' is part of type\n"; |
| 5222 | |
| 5223 | # unary operators should have a space before and |
| 5224 | # none after. May be left adjacent to another |
| 5225 | # unary operator, or a cast |
| 5226 | } elsif ($op eq '!' || $op eq '~' || |
| 5227 | $opv eq '*U' || $opv eq '-U' || |
| 5228 | $opv eq '&U' || $opv eq '&&U') { |
| 5229 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5230 | if (ERROR("SPACING", |
| 5231 | "space required before that '$op' $at\n" . $hereptr)) { |
| 5232 | if ($n != $last_after + 2) { |
| 5233 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
| 5234 | $line_fixed = 1; |
| 5235 | } |
| 5236 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5237 | } |
| 5238 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
| 5239 | # A unary '*' may be const |
| 5240 | |
| 5241 | } elsif ($ctx =~ /.xW/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5242 | if (ERROR("SPACING", |
| 5243 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
| 5244 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
| 5245 | if (defined $fix_elements[$n + 2]) { |
| 5246 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5247 | } |
| 5248 | $line_fixed = 1; |
| 5249 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | # unary ++ and unary -- are allowed no space on one side. |
| 5253 | } elsif ($op eq '++' or $op eq '--') { |
| 5254 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5255 | if (ERROR("SPACING", |
| 5256 | "space required one side of that '$op' $at\n" . $hereptr)) { |
| 5257 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
| 5258 | $line_fixed = 1; |
| 5259 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5260 | } |
| 5261 | if ($ctx =~ /Wx[BE]/ || |
| 5262 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5263 | if (ERROR("SPACING", |
| 5264 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 5265 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 5266 | $line_fixed = 1; |
| 5267 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5268 | } |
| 5269 | if ($ctx =~ /ExW/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5270 | if (ERROR("SPACING", |
| 5271 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
| 5272 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
| 5273 | if (defined $fix_elements[$n + 2]) { |
| 5274 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5275 | } |
| 5276 | $line_fixed = 1; |
| 5277 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5278 | } |
| 5279 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5280 | # << and >> may either have or not have spaces both sides |
| 5281 | } elsif ($op eq '<<' or $op eq '>>' or |
| 5282 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 5283 | $op eq '+' or $op eq '-' or |
| 5284 | $op eq '*' or $op eq '/' or |
| 5285 | $op eq '%') |
| 5286 | { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5287 | if ($check) { |
| 5288 | if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { |
| 5289 | if (CHK("SPACING", |
| 5290 | "spaces preferred around that '$op' $at\n" . $hereptr)) { |
| 5291 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 5292 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5293 | $line_fixed = 1; |
| 5294 | } |
| 5295 | } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { |
| 5296 | if (CHK("SPACING", |
| 5297 | "space preferred before that '$op' $at\n" . $hereptr)) { |
| 5298 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); |
| 5299 | $line_fixed = 1; |
| 5300 | } |
| 5301 | } |
| 5302 | } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5303 | if (ERROR("SPACING", |
| 5304 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
| 5305 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 5306 | if (defined $fix_elements[$n + 2]) { |
| 5307 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5308 | } |
| 5309 | $line_fixed = 1; |
| 5310 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
| 5313 | # A colon needs no spaces before when it is |
| 5314 | # terminating a case value or a label. |
| 5315 | } elsif ($opv eq ':C' || $opv eq ':L') { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 5316 | if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5317 | if (ERROR("SPACING", |
| 5318 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 5319 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 5320 | $line_fixed = 1; |
| 5321 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | # All the others need spaces both sides. |
| 5325 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
| 5326 | my $ok = 0; |
| 5327 | |
| 5328 | # Ignore email addresses <foo@bar> |
| 5329 | if (($op eq '<' && |
| 5330 | $cc =~ /^\S+\@\S+>/) || |
| 5331 | ($op eq '>' && |
| 5332 | $ca =~ /<\S+\@\S+$/)) |
| 5333 | { |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5334 | $ok = 1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5335 | } |
| 5336 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5337 | # for asm volatile statements |
| 5338 | # ignore a colon with another |
| 5339 | # colon immediately before or after |
| 5340 | if (($op eq ':') && |
| 5341 | ($ca =~ /:$/ || $cc =~ /^:/)) { |
| 5342 | $ok = 1; |
| 5343 | } |
| 5344 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5345 | # messages are ERROR, but ?: are CHK |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5346 | if ($ok == 0) { |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5347 | my $msg_level = \&ERROR; |
| 5348 | $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5349 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5350 | if (&{$msg_level}("SPACING", |
| 5351 | "spaces required around that '$op' $at\n" . $hereptr)) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5352 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 5353 | if (defined $fix_elements[$n + 2]) { |
| 5354 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5355 | } |
| 5356 | $line_fixed = 1; |
| 5357 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5358 | } |
| 5359 | } |
| 5360 | $off += length($elements[$n + 1]); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5361 | |
| 5362 | ## print("n: <$n> GOOD: <$good>\n"); |
| 5363 | |
| 5364 | $fixed_line = $fixed_line . $good; |
| 5365 | } |
| 5366 | |
| 5367 | if (($#elements % 2) == 0) { |
| 5368 | $fixed_line = $fixed_line . $fix_elements[$#elements]; |
| 5369 | } |
| 5370 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5371 | if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { |
| 5372 | $fixed[$fixlinenr] = $fixed_line; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | |
| 5376 | } |
| 5377 | |
| 5378 | # check for whitespace before a non-naked semicolon |
| 5379 | if ($line =~ /^\+.*\S\s+;\s*$/) { |
| 5380 | if (WARN("SPACING", |
| 5381 | "space prohibited before semicolon\n" . $herecurr) && |
| 5382 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5383 | 1 while $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5384 | s/^(\+.*\S)\s+;/$1;/; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5385 | } |
| 5386 | } |
| 5387 | |
| 5388 | # check for multiple assignments |
| 5389 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
| 5390 | CHK("MULTIPLE_ASSIGNMENTS", |
| 5391 | "multiple assignments should be avoided\n" . $herecurr); |
| 5392 | } |
| 5393 | |
| 5394 | ## # check for multiple declarations, allowing for a function declaration |
| 5395 | ## # continuation. |
| 5396 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 5397 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 5398 | ## |
| 5399 | ## # Remove any bracketed sections to ensure we do not |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 5400 | ## # falsely report the parameters of functions. |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5401 | ## my $ln = $line; |
| 5402 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 5403 | ## } |
| 5404 | ## if ($ln =~ /,/) { |
| 5405 | ## WARN("MULTIPLE_DECLARATION", |
| 5406 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
| 5407 | ## } |
| 5408 | ## } |
| 5409 | |
| 5410 | #need space before brace following if, while, etc |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5411 | if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5412 | $line =~ /\b(?:else|do)\{/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5413 | if (ERROR("SPACING", |
| 5414 | "space required before the open brace '{'\n" . $herecurr) && |
| 5415 | $fix) { |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5416 | $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5417 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5418 | } |
| 5419 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5420 | ## # check for blank lines before declarations |
| 5421 | ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && |
| 5422 | ## $prevrawline =~ /^.\s*$/) { |
| 5423 | ## WARN("SPACING", |
| 5424 | ## "No blank lines before declarations\n" . $hereprev); |
| 5425 | ## } |
| 5426 | ## |
| 5427 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5428 | # closing brace should have a space following it when it has anything |
| 5429 | # on the line |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5430 | if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5431 | if (ERROR("SPACING", |
| 5432 | "space required after that close brace '}'\n" . $herecurr) && |
| 5433 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5434 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5435 | s/}((?!(?:,|;|\)))\S)/} $1/; |
| 5436 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
| 5439 | # check spacing on square brackets |
| 5440 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5441 | if (ERROR("SPACING", |
| 5442 | "space prohibited after that open square bracket '['\n" . $herecurr) && |
| 5443 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5444 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5445 | s/\[\s+/\[/; |
| 5446 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5447 | } |
| 5448 | if ($line =~ /\s\]/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5449 | if (ERROR("SPACING", |
| 5450 | "space prohibited before that close square bracket ']'\n" . $herecurr) && |
| 5451 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5452 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5453 | s/\s+\]/\]/; |
| 5454 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5455 | } |
| 5456 | |
| 5457 | # check spacing on parentheses |
| 5458 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 5459 | $line !~ /for\s*\(\s+;/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5460 | if (ERROR("SPACING", |
| 5461 | "space prohibited after that open parenthesis '('\n" . $herecurr) && |
| 5462 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5463 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5464 | s/\(\s+/\(/; |
| 5465 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5466 | } |
| 5467 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
| 5468 | $line !~ /for\s*\(.*;\s+\)/ && |
| 5469 | $line !~ /:\s+\)/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5470 | if (ERROR("SPACING", |
| 5471 | "space prohibited before that close parenthesis ')'\n" . $herecurr) && |
| 5472 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5473 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5474 | s/\s+\)/\)/; |
| 5475 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5476 | } |
| 5477 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5478 | # check unnecessary parentheses around addressof/dereference single $Lvals |
| 5479 | # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar |
| 5480 | |
| 5481 | while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { |
| 5482 | my $var = $1; |
| 5483 | if (CHK("UNNECESSARY_PARENTHESES", |
| 5484 | "Unnecessary parentheses around $var\n" . $herecurr) && |
| 5485 | $fix) { |
| 5486 | $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; |
| 5487 | } |
| 5488 | } |
| 5489 | |
| 5490 | # check for unnecessary parentheses around function pointer uses |
| 5491 | # ie: (foo->bar)(); should be foo->bar(); |
| 5492 | # but not "if (foo->bar) (" to avoid some false positives |
| 5493 | if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { |
| 5494 | my $var = $2; |
| 5495 | if (CHK("UNNECESSARY_PARENTHESES", |
| 5496 | "Unnecessary parentheses around function pointer $var\n" . $herecurr) && |
| 5497 | $fix) { |
| 5498 | my $var2 = deparenthesize($var); |
| 5499 | $var2 =~ s/\s//g; |
| 5500 | $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; |
| 5501 | } |
| 5502 | } |
| 5503 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5504 | # check for unnecessary parentheses around comparisons in if uses |
| 5505 | # when !drivers/staging or command-line uses --strict |
| 5506 | if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5507 | $perl_version_ok && defined($stat) && |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5508 | $stat =~ /(^.\s*if\s*($balanced_parens))/) { |
| 5509 | my $if_stat = $1; |
| 5510 | my $test = substr($2, 1, -1); |
| 5511 | my $herectx; |
| 5512 | while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { |
| 5513 | my $match = $1; |
| 5514 | # avoid parentheses around potential macro args |
| 5515 | next if ($match =~ /^\s*\w+\s*$/); |
| 5516 | if (!defined($herectx)) { |
| 5517 | $herectx = $here . "\n"; |
| 5518 | my $cnt = statement_rawlines($if_stat); |
| 5519 | for (my $n = 0; $n < $cnt; $n++) { |
| 5520 | my $rl = raw_line($linenr, $n); |
| 5521 | $herectx .= $rl . "\n"; |
| 5522 | last if $rl =~ /^[ \+].*\{/; |
| 5523 | } |
| 5524 | } |
| 5525 | CHK("UNNECESSARY_PARENTHESES", |
| 5526 | "Unnecessary parentheses around '$match'\n" . $herectx); |
| 5527 | } |
| 5528 | } |
| 5529 | |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 5530 | # check that goto labels aren't indented (allow a single space indentation) |
| 5531 | # and ignore bitfield definitions like foo:1 |
| 5532 | # Strictly, labels can have whitespace after the identifier and before the : |
| 5533 | # but this is not allowed here as many ?: uses would appear to be labels |
| 5534 | if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ && |
| 5535 | $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ && |
| 5536 | $sline !~ /^.\s+default:/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5537 | if (WARN("INDENTED_LABEL", |
| 5538 | "labels should not be indented\n" . $herecurr) && |
| 5539 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5540 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5541 | s/^(.)\s+/$1/; |
| 5542 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 5545 | # check if a statement with a comma should be two statements like: |
| 5546 | # foo = bar(), /* comma should be semicolon */ |
| 5547 | # bar = baz(); |
| 5548 | if (defined($stat) && |
| 5549 | $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) { |
| 5550 | my $cnt = statement_rawlines($stat); |
| 5551 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 5552 | WARN("SUSPECT_COMMA_SEMICOLON", |
| 5553 | "Possible comma where semicolon could be used\n" . $herectx); |
| 5554 | } |
| 5555 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5556 | # return is not a function |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5557 | if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5558 | my $spacing = $1; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5559 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5560 | $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { |
| 5561 | my $value = $1; |
| 5562 | $value = deparenthesize($value); |
| 5563 | if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { |
| 5564 | ERROR("RETURN_PARENTHESES", |
| 5565 | "return is not a function, parentheses are not required\n" . $herecurr); |
| 5566 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5567 | } elsif ($spacing !~ /\s+/) { |
| 5568 | ERROR("SPACING", |
| 5569 | "space required before the open parenthesis '('\n" . $herecurr); |
| 5570 | } |
| 5571 | } |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5572 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5573 | # unnecessary return in a void function |
| 5574 | # at end-of-function, with the previous line a single leading tab, then return; |
| 5575 | # and the line before that not a goto label target like "out:" |
| 5576 | if ($sline =~ /^[ \+]}\s*$/ && |
| 5577 | $prevline =~ /^\+\treturn\s*;\s*$/ && |
| 5578 | $linenr >= 3 && |
| 5579 | $lines[$linenr - 3] =~ /^[ +]/ && |
| 5580 | $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { |
| 5581 | WARN("RETURN_VOID", |
| 5582 | "void function return statements are not generally useful\n" . $hereprev); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 5583 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5584 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5585 | # if statements using unnecessary parentheses - ie: if ((foo == bar)) |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5586 | if ($perl_version_ok && |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5587 | $line =~ /\bif\s*((?:\(\s*){2,})/) { |
| 5588 | my $openparens = $1; |
| 5589 | my $count = $openparens =~ tr@\(@\(@; |
| 5590 | my $msg = ""; |
| 5591 | if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { |
| 5592 | my $comp = $4; #Not $1 because of $LvalOrFunc |
| 5593 | $msg = " - maybe == should be = ?" if ($comp eq "=="); |
| 5594 | WARN("UNNECESSARY_PARENTHESES", |
| 5595 | "Unnecessary parentheses$msg\n" . $herecurr); |
| 5596 | } |
| 5597 | } |
| 5598 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5599 | # comparisons with a constant or upper case identifier on the left |
| 5600 | # avoid cases like "foo + BAR < baz" |
| 5601 | # only fix matches surrounded by parentheses to avoid incorrect |
| 5602 | # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5603 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5604 | $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { |
| 5605 | my $lead = $1; |
| 5606 | my $const = $2; |
| 5607 | my $comp = $3; |
| 5608 | my $to = $4; |
| 5609 | my $newcomp = $comp; |
| 5610 | if ($lead !~ /(?:$Operators|\.)\s*$/ && |
| 5611 | $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && |
| 5612 | WARN("CONSTANT_COMPARISON", |
| 5613 | "Comparisons should place the constant on the right side of the test\n" . $herecurr) && |
| 5614 | $fix) { |
| 5615 | if ($comp eq "<") { |
| 5616 | $newcomp = ">"; |
| 5617 | } elsif ($comp eq "<=") { |
| 5618 | $newcomp = ">="; |
| 5619 | } elsif ($comp eq ">") { |
| 5620 | $newcomp = "<"; |
| 5621 | } elsif ($comp eq ">=") { |
| 5622 | $newcomp = "<="; |
| 5623 | } |
| 5624 | $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; |
| 5625 | } |
| 5626 | } |
| 5627 | |
| 5628 | # Return of what appears to be an errno should normally be negative |
| 5629 | if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5630 | my $name = $1; |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 5631 | if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5632 | WARN("USE_NEGATIVE_ERRNO", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5633 | "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5634 | } |
| 5635 | } |
| 5636 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5637 | # Need a space before open parenthesis after if, while etc |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5638 | if ($line =~ /\b(if|while|for|switch)\(/) { |
| 5639 | if (ERROR("SPACING", |
| 5640 | "space required before the open parenthesis '('\n" . $herecurr) && |
| 5641 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5642 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5643 | s/\b(if|while|for|switch)\(/$1 \(/; |
| 5644 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5645 | } |
| 5646 | |
| 5647 | # Check for illegal assignment in if conditional -- and check for trailing |
| 5648 | # statements after the conditional. |
| 5649 | if ($line =~ /do\s*(?!{)/) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5650 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 5651 | ctx_statement_block($linenr, $realcnt, 0) |
| 5652 | if (!defined $stat); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5653 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 5654 | $remain_next, $off_next); |
| 5655 | $stat_next =~ s/\n./\n /g; |
| 5656 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 5657 | |
| 5658 | if ($stat_next =~ /^\s*while\b/) { |
| 5659 | # If the statement carries leading newlines, |
| 5660 | # then count those as offsets. |
| 5661 | my ($whitespace) = |
| 5662 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 5663 | my $offset = |
| 5664 | statement_rawlines($whitespace) - 1; |
| 5665 | |
| 5666 | $suppress_whiletrailers{$line_nr_next + |
| 5667 | $offset} = 1; |
| 5668 | } |
| 5669 | } |
| 5670 | if (!defined $suppress_whiletrailers{$linenr} && |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5671 | defined($stat) && defined($cond) && |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5672 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
| 5673 | my ($s, $c) = ($stat, $cond); |
| 5674 | |
| 5675 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 5676 | if (ERROR("ASSIGN_IN_IF", |
| 5677 | "do not use assignment in if condition\n" . $herecurr) && |
| 5678 | $fix && $perl_version_ok) { |
| 5679 | if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) { |
| 5680 | my $space = $1; |
| 5681 | my $not = $2; |
| 5682 | my $statement = $3; |
| 5683 | my $assigned = $4; |
| 5684 | my $test = $8; |
| 5685 | my $against = $9; |
| 5686 | my $brace = $15; |
| 5687 | fix_delete_line($fixlinenr, $rawline); |
| 5688 | fix_insert_line($fixlinenr, "$space$statement;"); |
| 5689 | my $newline = "${space}if ("; |
| 5690 | $newline .= '!' if defined($not); |
| 5691 | $newline .= '(' if (defined $not && defined($test) && defined($against)); |
| 5692 | $newline .= "$assigned"; |
| 5693 | $newline .= " $test $against" if (defined($test) && defined($against)); |
| 5694 | $newline .= ')' if (defined $not && defined($test) && defined($against)); |
| 5695 | $newline .= ')'; |
| 5696 | $newline .= " {" if (defined($brace)); |
| 5697 | fix_insert_line($fixlinenr + 1, $newline); |
| 5698 | } |
| 5699 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | # Find out what is on the end of the line after the |
| 5703 | # conditional. |
| 5704 | substr($s, 0, length($c), ''); |
| 5705 | $s =~ s/\n.*//g; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5706 | $s =~ s/$;//g; # Remove any comments |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5707 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 5708 | $c !~ /}\s*while\s*/) |
| 5709 | { |
| 5710 | # Find out how long the conditional actually is. |
| 5711 | my @newlines = ($c =~ /\n/gs); |
| 5712 | my $cond_lines = 1 + $#newlines; |
| 5713 | my $stat_real = ''; |
| 5714 | |
| 5715 | $stat_real = raw_line($linenr, $cond_lines) |
| 5716 | . "\n" if ($cond_lines); |
| 5717 | if (defined($stat_real) && $cond_lines > 1) { |
| 5718 | $stat_real = "[...]\n$stat_real"; |
| 5719 | } |
| 5720 | |
| 5721 | ERROR("TRAILING_STATEMENTS", |
| 5722 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
| 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | # Check for bitwise tests written as boolean |
| 5727 | if ($line =~ / |
| 5728 | (?: |
| 5729 | (?:\[|\(|\&\&|\|\|) |
| 5730 | \s*0[xX][0-9]+\s* |
| 5731 | (?:\&\&|\|\|) |
| 5732 | | |
| 5733 | (?:\&\&|\|\|) |
| 5734 | \s*0[xX][0-9]+\s* |
| 5735 | (?:\&\&|\|\||\)|\]) |
| 5736 | )/x) |
| 5737 | { |
| 5738 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 5739 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
| 5740 | } |
| 5741 | |
| 5742 | # if and else should not have general statements after it |
| 5743 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 5744 | my $s = $1; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5745 | $s =~ s/$;//g; # Remove any comments |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5746 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
| 5747 | ERROR("TRAILING_STATEMENTS", |
| 5748 | "trailing statements should be on next line\n" . $herecurr); |
| 5749 | } |
| 5750 | } |
| 5751 | # if should not continue a brace |
| 5752 | if ($line =~ /}\s*if\b/) { |
| 5753 | ERROR("TRAILING_STATEMENTS", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5754 | "trailing statements should be on next line (or did you mean 'else if'?)\n" . |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5755 | $herecurr); |
| 5756 | } |
| 5757 | # case and default should not have general statements after them |
| 5758 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 5759 | $line !~ /\G(?: |
| 5760 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
| 5761 | \s*return\s+ |
| 5762 | )/xg) |
| 5763 | { |
| 5764 | ERROR("TRAILING_STATEMENTS", |
| 5765 | "trailing statements should be on next line\n" . $herecurr); |
| 5766 | } |
| 5767 | |
| 5768 | # Check for }<nl>else {, these must be at the same |
| 5769 | # indent level to be relevant to each other. |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5770 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && |
| 5771 | $previndent == $indent) { |
| 5772 | if (ERROR("ELSE_AFTER_BRACE", |
| 5773 | "else should follow close brace '}'\n" . $hereprev) && |
| 5774 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 5775 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5776 | fix_delete_line($fixlinenr, $rawline); |
| 5777 | my $fixedline = $prevrawline; |
| 5778 | $fixedline =~ s/}\s*$//; |
| 5779 | if ($fixedline !~ /^\+\s*$/) { |
| 5780 | fix_insert_line($fixlinenr, $fixedline); |
| 5781 | } |
| 5782 | $fixedline = $rawline; |
| 5783 | $fixedline =~ s/^(.\s*)else/$1} else/; |
| 5784 | fix_insert_line($fixlinenr, $fixedline); |
| 5785 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5786 | } |
| 5787 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5788 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && |
| 5789 | $previndent == $indent) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5790 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 5791 | |
| 5792 | # Find out what is on the end of the line after the |
| 5793 | # conditional. |
| 5794 | substr($s, 0, length($c), ''); |
| 5795 | $s =~ s/\n.*//g; |
| 5796 | |
| 5797 | if ($s =~ /^\s*;/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5798 | if (ERROR("WHILE_AFTER_BRACE", |
| 5799 | "while should follow close brace '}'\n" . $hereprev) && |
| 5800 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 5801 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5802 | fix_delete_line($fixlinenr, $rawline); |
| 5803 | my $fixedline = $prevrawline; |
| 5804 | my $trailing = $rawline; |
| 5805 | $trailing =~ s/^\+//; |
| 5806 | $trailing = trim($trailing); |
| 5807 | $fixedline =~ s/}\s*$/} $trailing/; |
| 5808 | fix_insert_line($fixlinenr, $fixedline); |
| 5809 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5810 | } |
| 5811 | } |
| 5812 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5813 | #Specific variable tests |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5814 | while ($line =~ m{($Constant|$Lval)}g) { |
| 5815 | my $var = $1; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5816 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5817 | #CamelCase |
| 5818 | if ($var !~ /^$Constant$/ && |
| 5819 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 5820 | #Ignore some autogenerated defines and enum values |
| 5821 | $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ && |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5822 | #Ignore Page<foo> variants |
| 5823 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5824 | #Ignore SI style variants like nS, mV and dB |
| 5825 | #(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) |
| 5826 | $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5827 | #Ignore some three character SI units explicitly, like MiB and KHz |
| 5828 | $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5829 | while ($var =~ m{($Ident)}g) { |
| 5830 | my $word = $1; |
| 5831 | next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); |
| 5832 | if ($check) { |
| 5833 | seed_camelcase_includes(); |
| 5834 | if (!$file && !$camelcase_file_seeded) { |
| 5835 | seed_camelcase_file($realfile); |
| 5836 | $camelcase_file_seeded = 1; |
| 5837 | } |
| 5838 | } |
| 5839 | if (!defined $camelcase{$word}) { |
| 5840 | $camelcase{$word} = 1; |
| 5841 | CHK("CAMELCASE", |
| 5842 | "Avoid CamelCase: <$word>\n" . $herecurr); |
| 5843 | } |
| 5844 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5845 | } |
| 5846 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5847 | |
| 5848 | #no spaces allowed after \ in define |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5849 | if ($line =~ /\#\s*define.*\\\s+$/) { |
| 5850 | if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 5851 | "Whitespace after \\ makes next lines useless\n" . $herecurr) && |
| 5852 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5853 | $fixed[$fixlinenr] =~ s/\s+$//; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5854 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5855 | } |
| 5856 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5857 | # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes |
| 5858 | # itself <asm/foo.h> (uses RAW line) |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5859 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
| 5860 | my $file = "$1.h"; |
| 5861 | my $checkfile = "include/linux/$file"; |
| 5862 | if (-f "$root/$checkfile" && |
| 5863 | $realfile ne $checkfile && |
| 5864 | $1 !~ /$allowed_asm_includes/) |
| 5865 | { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5866 | my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; |
| 5867 | if ($asminclude > 0) { |
| 5868 | if ($realfile =~ m{^arch/}) { |
| 5869 | CHK("ARCH_INCLUDE_LINUX", |
| 5870 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 5871 | } else { |
| 5872 | WARN("INCLUDE_LINUX", |
| 5873 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 5874 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5875 | } |
| 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 5880 | # first statement and ensure its the whole macro if its not enclosed |
| 5881 | # in a known good container |
| 5882 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 5883 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
| 5884 | my $ln = $linenr; |
| 5885 | my $cnt = $realcnt; |
| 5886 | my ($off, $dstat, $dcond, $rest); |
| 5887 | my $ctx = ''; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5888 | my $has_flow_statement = 0; |
| 5889 | my $has_arg_concat = 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5890 | ($dstat, $dcond, $ln, $cnt, $off) = |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5891 | ctx_statement_block($linenr, $realcnt, 0); |
| 5892 | $ctx = $dstat; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5893 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
| 5894 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
| 5895 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5896 | $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); |
| 5897 | $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); |
| 5898 | |
| 5899 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; |
| 5900 | my $define_args = $1; |
| 5901 | my $define_stmt = $dstat; |
| 5902 | my @def_args = (); |
| 5903 | |
| 5904 | if (defined $define_args && $define_args ne "") { |
| 5905 | $define_args = substr($define_args, 1, length($define_args) - 2); |
| 5906 | $define_args =~ s/\s*//g; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5907 | $define_args =~ s/\\\+?//g; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5908 | @def_args = split(",", $define_args); |
| 5909 | } |
| 5910 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5911 | $dstat =~ s/$;//g; |
| 5912 | $dstat =~ s/\\\n.//g; |
| 5913 | $dstat =~ s/^\s*//s; |
| 5914 | $dstat =~ s/\s*$//s; |
| 5915 | |
| 5916 | # Flatten any parentheses and braces |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 5917 | while ($dstat =~ s/\([^\(\)]*\)/1u/ || |
| 5918 | $dstat =~ s/\{[^\{\}]*\}/1u/ || |
| 5919 | $dstat =~ s/.\[[^\[\]]*\]/1u/) |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5920 | { |
| 5921 | } |
| 5922 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5923 | # Flatten any obvious string concatenation. |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5924 | while ($dstat =~ s/($String)\s*$Ident/$1/ || |
| 5925 | $dstat =~ s/$Ident\s*($String)/$1/) |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5926 | { |
| 5927 | } |
| 5928 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5929 | # Make asm volatile uses seem like a generic function |
| 5930 | $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; |
| 5931 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5932 | my $exceptions = qr{ |
| 5933 | $Declare| |
| 5934 | module_param_named| |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5935 | MODULE_PARM_DESC| |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5936 | DECLARE_PER_CPU| |
| 5937 | DEFINE_PER_CPU| |
| 5938 | __typeof__\(| |
| 5939 | union| |
| 5940 | struct| |
| 5941 | \.$Ident\s*=\s*| |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5942 | ^\"|\"$| |
| 5943 | ^\[ |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5944 | }x; |
| 5945 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5946 | |
| 5947 | $ctx =~ s/\n*$//; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5948 | my $stmt_cnt = statement_rawlines($ctx); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5949 | my $herectx = get_stat_here($linenr, $stmt_cnt, $here); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5950 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5951 | if ($dstat ne '' && |
| 5952 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 5953 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5954 | $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5955 | $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5956 | $dstat !~ /$exceptions/ && |
| 5957 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5958 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5959 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 5960 | $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...} |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5961 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 5962 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 5963 | $dstat !~ /^do\s*{/ && # do {... |
Heiko Schocher | d8a1a30 | 2016-01-04 09:17:19 +0100 | [diff] [blame] | 5964 | $dstat !~ /^\(\{/ && # ({... |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5965 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5966 | { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5967 | if ($dstat =~ /^\s*if\b/) { |
| 5968 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 5969 | "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); |
| 5970 | } elsif ($dstat =~ /;/) { |
| 5971 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 5972 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 5973 | } else { |
| 5974 | ERROR("COMPLEX_MACRO", |
| 5975 | "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); |
| 5976 | } |
| 5977 | |
| 5978 | } |
| 5979 | |
| 5980 | # Make $define_stmt single line, comment-free, etc |
| 5981 | my @stmt_array = split('\n', $define_stmt); |
| 5982 | my $first = 1; |
| 5983 | $define_stmt = ""; |
| 5984 | foreach my $l (@stmt_array) { |
| 5985 | $l =~ s/\\$//; |
| 5986 | if ($first) { |
| 5987 | $define_stmt = $l; |
| 5988 | $first = 0; |
| 5989 | } elsif ($l =~ /^[\+ ]/) { |
| 5990 | $define_stmt .= substr($l, 1); |
| 5991 | } |
| 5992 | } |
| 5993 | $define_stmt =~ s/$;//g; |
| 5994 | $define_stmt =~ s/\s+/ /g; |
| 5995 | $define_stmt = trim($define_stmt); |
| 5996 | |
| 5997 | # check if any macro arguments are reused (ignore '...' and 'type') |
| 5998 | foreach my $arg (@def_args) { |
| 5999 | next if ($arg =~ /\.\.\./); |
| 6000 | next if ($arg =~ /^type$/i); |
| 6001 | my $tmp_stmt = $define_stmt; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6002 | $tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6003 | $tmp_stmt =~ s/\#+\s*$arg\b//g; |
| 6004 | $tmp_stmt =~ s/\b$arg\s*\#\#//g; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6005 | my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6006 | if ($use_cnt > 1) { |
| 6007 | CHK("MACRO_ARG_REUSE", |
| 6008 | "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); |
| 6009 | } |
| 6010 | # check if any macro arguments may have other precedence issues |
| 6011 | if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && |
| 6012 | ((defined($1) && $1 ne ',') || |
| 6013 | (defined($2) && $2 ne ','))) { |
| 6014 | CHK("MACRO_ARG_PRECEDENCE", |
| 6015 | "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); |
| 6016 | } |
| 6017 | } |
| 6018 | |
| 6019 | # check for macros with flow control, but without ## concatenation |
| 6020 | # ## concatenation is commonly a macro that defines a function so ignore those |
| 6021 | if ($has_flow_statement && !$has_arg_concat) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6022 | my $cnt = statement_rawlines($ctx); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6023 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6024 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6025 | WARN("MACRO_WITH_FLOW_CONTROL", |
| 6026 | "Macros with flow control statements should be avoided\n" . "$herectx"); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6027 | } |
| 6028 | |
| 6029 | # check for line continuations outside of #defines, preprocessor #, and asm |
| 6030 | |
| 6031 | } else { |
| 6032 | if ($prevline !~ /^..*\\$/ && |
| 6033 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 6034 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
| 6035 | $line =~ /^\+.*\\$/) { |
| 6036 | WARN("LINE_CONTINUATIONS", |
| 6037 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 6038 | } |
| 6039 | } |
| 6040 | |
| 6041 | # do {} while (0) macro tests: |
| 6042 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 6043 | # macro should not end with a semicolon |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6044 | if ($perl_version_ok && |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6045 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 6046 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 6047 | my $ln = $linenr; |
| 6048 | my $cnt = $realcnt; |
| 6049 | my ($off, $dstat, $dcond, $rest); |
| 6050 | my $ctx = ''; |
| 6051 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 6052 | ctx_statement_block($linenr, $realcnt, 0); |
| 6053 | $ctx = $dstat; |
| 6054 | |
| 6055 | $dstat =~ s/\\\n.//g; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6056 | $dstat =~ s/$;/ /g; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6057 | |
| 6058 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 6059 | my $stmts = $2; |
| 6060 | my $semis = $3; |
| 6061 | |
| 6062 | $ctx =~ s/\n*$//; |
| 6063 | my $cnt = statement_rawlines($ctx); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6064 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6065 | |
| 6066 | if (($stmts =~ tr/;/;/) == 1 && |
| 6067 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
| 6068 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 6069 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 6070 | } |
| 6071 | if (defined $semis && $semis ne "") { |
| 6072 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 6073 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6074 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6075 | } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { |
| 6076 | $ctx =~ s/\n*$//; |
| 6077 | my $cnt = statement_rawlines($ctx); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6078 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6079 | |
| 6080 | WARN("TRAILING_SEMICOLON", |
| 6081 | "macros should not use a trailing semicolon\n" . "$herectx"); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6082 | } |
| 6083 | } |
| 6084 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6085 | # check for redundant bracing round if etc |
| 6086 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 6087 | my ($level, $endln, @chunks) = |
| 6088 | ctx_statement_full($linenr, $realcnt, 1); |
| 6089 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
| 6090 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 6091 | if ($#chunks > 0 && $level == 0) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6092 | my @allowed = (); |
| 6093 | my $allow = 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6094 | my $seen = 0; |
| 6095 | my $herectx = $here . "\n"; |
| 6096 | my $ln = $linenr - 1; |
| 6097 | for my $chunk (@chunks) { |
| 6098 | my ($cond, $block) = @{$chunk}; |
| 6099 | |
| 6100 | # If the condition carries leading newlines, then count those as offsets. |
| 6101 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 6102 | my $offset = statement_rawlines($whitespace) - 1; |
| 6103 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6104 | $allowed[$allow] = 0; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6105 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 6106 | |
| 6107 | # We have looked at and allowed this specific line. |
| 6108 | $suppress_ifbraces{$ln + $offset} = 1; |
| 6109 | |
| 6110 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
| 6111 | $ln += statement_rawlines($block) - 1; |
| 6112 | |
| 6113 | substr($block, 0, length($cond), ''); |
| 6114 | |
| 6115 | $seen++ if ($block =~ /^\s*{/); |
| 6116 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6117 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6118 | if (statement_lines($cond) > 1) { |
| 6119 | #print "APW: ALLOWED: cond<$cond>\n"; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6120 | $allowed[$allow] = 1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6121 | } |
| 6122 | if ($block =~/\b(?:if|for|while)\b/) { |
| 6123 | #print "APW: ALLOWED: block<$block>\n"; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6124 | $allowed[$allow] = 1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6125 | } |
| 6126 | if (statement_block_size($block) > 1) { |
| 6127 | #print "APW: ALLOWED: lines block<$block>\n"; |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6128 | $allowed[$allow] = 1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6129 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6130 | $allow++; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6131 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6132 | if ($seen) { |
| 6133 | my $sum_allowed = 0; |
| 6134 | foreach (@allowed) { |
| 6135 | $sum_allowed += $_; |
| 6136 | } |
| 6137 | if ($sum_allowed == 0) { |
| 6138 | WARN("BRACES", |
| 6139 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 6140 | } elsif ($sum_allowed != $allow && |
| 6141 | $seen != $allow) { |
| 6142 | CHK("BRACES", |
| 6143 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 6144 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6145 | } |
| 6146 | } |
| 6147 | } |
| 6148 | if (!defined $suppress_ifbraces{$linenr - 1} && |
| 6149 | $line =~ /\b(if|while|for|else)\b/) { |
| 6150 | my $allowed = 0; |
| 6151 | |
| 6152 | # Check the pre-context. |
| 6153 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 6154 | #print "APW: ALLOWED: pre<$1>\n"; |
| 6155 | $allowed = 1; |
| 6156 | } |
| 6157 | |
| 6158 | my ($level, $endln, @chunks) = |
| 6159 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 6160 | |
| 6161 | # Check the condition. |
| 6162 | my ($cond, $block) = @{$chunks[0]}; |
| 6163 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
| 6164 | if (defined $cond) { |
| 6165 | substr($block, 0, length($cond), ''); |
| 6166 | } |
| 6167 | if (statement_lines($cond) > 1) { |
| 6168 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 6169 | $allowed = 1; |
| 6170 | } |
| 6171 | if ($block =~/\b(?:if|for|while)\b/) { |
| 6172 | #print "APW: ALLOWED: block<$block>\n"; |
| 6173 | $allowed = 1; |
| 6174 | } |
| 6175 | if (statement_block_size($block) > 1) { |
| 6176 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 6177 | $allowed = 1; |
| 6178 | } |
| 6179 | # Check the post-context. |
| 6180 | if (defined $chunks[1]) { |
| 6181 | my ($cond, $block) = @{$chunks[1]}; |
| 6182 | if (defined $cond) { |
| 6183 | substr($block, 0, length($cond), ''); |
| 6184 | } |
| 6185 | if ($block =~ /^\s*\{/) { |
| 6186 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 6187 | $allowed = 1; |
| 6188 | } |
| 6189 | } |
| 6190 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6191 | my $cnt = statement_rawlines($block); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6192 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6193 | |
| 6194 | WARN("BRACES", |
| 6195 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
| 6196 | } |
| 6197 | } |
| 6198 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6199 | # check for single line unbalanced braces |
| 6200 | if ($sline =~ /^.\s*\}\s*else\s*$/ || |
| 6201 | $sline =~ /^.\s*else\s*\{\s*$/) { |
| 6202 | CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); |
| 6203 | } |
| 6204 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6205 | # check for unnecessary blank lines around braces |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6206 | if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6207 | if (CHK("BRACES", |
| 6208 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && |
| 6209 | $fix && $prevrawline =~ /^\+/) { |
| 6210 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 6211 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6212 | } |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6213 | if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6214 | if (CHK("BRACES", |
| 6215 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && |
| 6216 | $fix) { |
| 6217 | fix_delete_line($fixlinenr, $rawline); |
| 6218 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | # no volatiles please |
| 6222 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 6223 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
| 6224 | WARN("VOLATILE", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6225 | "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); |
| 6226 | } |
| 6227 | |
| 6228 | # Check for user-visible strings broken across lines, which breaks the ability |
| 6229 | # to grep for the string. Make exceptions when the previous string ends in a |
| 6230 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' |
| 6231 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value |
| 6232 | if ($line =~ /^\+\s*$String/ && |
| 6233 | $prevline =~ /"\s*$/ && |
| 6234 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { |
| 6235 | if (WARN("SPLIT_STRING", |
| 6236 | "quoted string split across lines\n" . $hereprev) && |
| 6237 | $fix && |
| 6238 | $prevrawline =~ /^\+.*"\s*$/ && |
| 6239 | $last_coalesced_string_linenr != $linenr - 1) { |
| 6240 | my $extracted_string = get_quoted_string($line, $rawline); |
| 6241 | my $comma_close = ""; |
| 6242 | if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { |
| 6243 | $comma_close = $1; |
| 6244 | } |
| 6245 | |
| 6246 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 6247 | fix_delete_line($fixlinenr, $rawline); |
| 6248 | my $fixedline = $prevrawline; |
| 6249 | $fixedline =~ s/"\s*$//; |
| 6250 | $fixedline .= substr($extracted_string, 1) . trim($comma_close); |
| 6251 | fix_insert_line($fixlinenr - 1, $fixedline); |
| 6252 | $fixedline = $rawline; |
| 6253 | $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; |
| 6254 | if ($fixedline !~ /\+\s*$/) { |
| 6255 | fix_insert_line($fixlinenr, $fixedline); |
| 6256 | } |
| 6257 | $last_coalesced_string_linenr = $linenr; |
| 6258 | } |
| 6259 | } |
| 6260 | |
| 6261 | # check for missing a space in a string concatenation |
| 6262 | if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { |
| 6263 | WARN('MISSING_SPACE', |
| 6264 | "break quoted strings at a space character\n" . $hereprev); |
| 6265 | } |
| 6266 | |
| 6267 | # check for an embedded function name in a string when the function is known |
| 6268 | # This does not work very well for -f --file checking as it depends on patch |
| 6269 | # context providing the function name or a single line form for in-file |
| 6270 | # function declarations |
| 6271 | if ($line =~ /^\+.*$String/ && |
| 6272 | defined($context_function) && |
| 6273 | get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && |
| 6274 | length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { |
| 6275 | WARN("EMBEDDED_FUNCTION_NAME", |
| 6276 | "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); |
| 6277 | } |
| 6278 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6279 | # check for unnecessary function tracing like uses |
| 6280 | # This does not use $logFunctions because there are many instances like |
| 6281 | # 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions |
| 6282 | if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) { |
| 6283 | if (WARN("TRACING_LOGGING", |
| 6284 | "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) && |
| 6285 | $fix) { |
| 6286 | fix_delete_line($fixlinenr, $rawline); |
| 6287 | } |
| 6288 | } |
| 6289 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6290 | # check for spaces before a quoted newline |
| 6291 | if ($rawline =~ /^.*\".*\s\\n/) { |
| 6292 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 6293 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && |
| 6294 | $fix) { |
| 6295 | $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; |
| 6296 | } |
| 6297 | |
| 6298 | } |
| 6299 | |
| 6300 | # concatenated string without spaces between elements |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 6301 | if ($line =~ /$String[A-Z_]/ || |
| 6302 | ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) { |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6303 | if (CHK("CONCATENATED_STRING", |
| 6304 | "Concatenated strings should use spaces between elements\n" . $herecurr) && |
| 6305 | $fix) { |
| 6306 | while ($line =~ /($String)/g) { |
| 6307 | my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); |
| 6308 | $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; |
| 6309 | $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; |
| 6310 | } |
| 6311 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6312 | } |
| 6313 | |
| 6314 | # uncoalesced string fragments |
Simon Glass | 587254e | 2022-01-23 12:55:11 -0700 | [diff] [blame] | 6315 | if ($line =~ /$String\s*[Lu]?"/) { |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6316 | if (WARN("STRING_FRAGMENTS", |
| 6317 | "Consecutive strings are generally better as a single string\n" . $herecurr) && |
| 6318 | $fix) { |
| 6319 | while ($line =~ /($String)(?=\s*")/g) { |
| 6320 | my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); |
| 6321 | $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; |
| 6322 | } |
| 6323 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | # check for non-standard and hex prefixed decimal printf formats |
| 6327 | my $show_L = 1; #don't show the same defect twice |
| 6328 | my $show_Z = 1; |
| 6329 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 6330 | my $string = substr($rawline, $-[1], $+[1] - $-[1]); |
| 6331 | $string =~ s/%%/__/g; |
| 6332 | # check for %L |
| 6333 | if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { |
| 6334 | WARN("PRINTF_L", |
| 6335 | "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); |
| 6336 | $show_L = 0; |
| 6337 | } |
| 6338 | # check for %Z |
| 6339 | if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { |
| 6340 | WARN("PRINTF_Z", |
| 6341 | "%Z$1 is non-standard C, use %z$1\n" . $herecurr); |
| 6342 | $show_Z = 0; |
| 6343 | } |
| 6344 | # check for 0x<decimal> |
| 6345 | if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { |
| 6346 | ERROR("PRINTF_0XDECIMAL", |
| 6347 | "Prefixing 0x with decimal output is defective\n" . $herecurr); |
| 6348 | } |
| 6349 | } |
| 6350 | |
| 6351 | # check for line continuations in quoted strings with odd counts of " |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6352 | if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6353 | WARN("LINE_CONTINUATIONS", |
| 6354 | "Avoid line continuations in quoted strings\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
| 6357 | # warn about #if 0 |
| 6358 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6359 | WARN("IF_0", |
| 6360 | "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); |
| 6361 | } |
| 6362 | |
| 6363 | # warn about #if 1 |
| 6364 | if ($line =~ /^.\s*\#\s*if\s+1\b/) { |
| 6365 | WARN("IF_1", |
| 6366 | "Consider removing the #if 1 and its #endif\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6369 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 6370 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6371 | my $tested = quotemeta($1); |
| 6372 | my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; |
| 6373 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { |
| 6374 | my $func = $1; |
| 6375 | if (WARN('NEEDLESS_IF', |
| 6376 | "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && |
| 6377 | $fix) { |
| 6378 | my $do_fix = 1; |
| 6379 | my $leading_tabs = ""; |
| 6380 | my $new_leading_tabs = ""; |
| 6381 | if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { |
| 6382 | $leading_tabs = $1; |
| 6383 | } else { |
| 6384 | $do_fix = 0; |
| 6385 | } |
| 6386 | if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { |
| 6387 | $new_leading_tabs = $1; |
| 6388 | if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { |
| 6389 | $do_fix = 0; |
| 6390 | } |
| 6391 | } else { |
| 6392 | $do_fix = 0; |
| 6393 | } |
| 6394 | if ($do_fix) { |
| 6395 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 6396 | $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; |
| 6397 | } |
| 6398 | } |
| 6399 | } |
| 6400 | } |
| 6401 | |
| 6402 | # check for unnecessary "Out of Memory" messages |
| 6403 | if ($line =~ /^\+.*\b$logFunctions\s*\(/ && |
| 6404 | $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && |
| 6405 | (defined $1 || defined $3) && |
| 6406 | $linenr > 3) { |
| 6407 | my $testval = $2; |
| 6408 | my $testline = $lines[$linenr - 3]; |
| 6409 | |
| 6410 | my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); |
| 6411 | # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); |
| 6412 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6413 | if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && |
| 6414 | $s !~ /\b__GFP_NOWARN\b/ ) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6415 | WARN("OOM_MESSAGE", |
| 6416 | "Possible unnecessary 'out of memory' message\n" . $hereprev); |
| 6417 | } |
| 6418 | } |
| 6419 | |
| 6420 | # check for logging functions with KERN_<LEVEL> |
| 6421 | if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && |
| 6422 | $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { |
| 6423 | my $level = $1; |
| 6424 | if (WARN("UNNECESSARY_KERN_LEVEL", |
| 6425 | "Possible unnecessary $level\n" . $herecurr) && |
| 6426 | $fix) { |
| 6427 | $fixed[$fixlinenr] =~ s/\s*$level\s*//; |
| 6428 | } |
| 6429 | } |
| 6430 | |
| 6431 | # check for logging continuations |
| 6432 | if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { |
| 6433 | WARN("LOGGING_CONTINUATION", |
| 6434 | "Avoid logging continuation uses where feasible\n" . $herecurr); |
| 6435 | } |
| 6436 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6437 | # check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions |
| 6438 | if (defined $stat && |
| 6439 | $line =~ /\b$logFunctions\s*\(/ && |
| 6440 | index($stat, '"') >= 0) { |
| 6441 | my $lc = $stat =~ tr@\n@@; |
| 6442 | $lc = $lc + $linenr; |
| 6443 | my $stat_real = get_stat_real($linenr, $lc); |
| 6444 | pos($stat_real) = index($stat_real, '"'); |
| 6445 | while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) { |
| 6446 | my $pspec = $1; |
| 6447 | my $h = $2; |
| 6448 | my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@; |
| 6449 | if (WARN("UNNECESSARY_MODIFIER", |
| 6450 | "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") && |
| 6451 | $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) { |
| 6452 | my $nspec = $pspec; |
| 6453 | $nspec =~ s/h//g; |
| 6454 | $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/; |
| 6455 | } |
| 6456 | } |
| 6457 | } |
| 6458 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6459 | # check for mask then right shift without a parentheses |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6460 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6461 | $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && |
| 6462 | $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so |
| 6463 | WARN("MASK_THEN_SHIFT", |
| 6464 | "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); |
| 6465 | } |
| 6466 | |
| 6467 | # check for pointer comparisons to NULL |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6468 | if ($perl_version_ok) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6469 | while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { |
| 6470 | my $val = $1; |
| 6471 | my $equal = "!"; |
| 6472 | $equal = "" if ($4 eq "!="); |
| 6473 | if (CHK("COMPARISON_TO_NULL", |
| 6474 | "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && |
| 6475 | $fix) { |
| 6476 | $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; |
| 6477 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6478 | } |
| 6479 | } |
| 6480 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6481 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
| 6482 | if ($line =~ /(\b$InitAttribute\b)/) { |
| 6483 | my $attr = $1; |
| 6484 | if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { |
| 6485 | my $ptr = $1; |
| 6486 | my $var = $2; |
| 6487 | if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && |
| 6488 | ERROR("MISPLACED_INIT", |
| 6489 | "$attr should be placed after $var\n" . $herecurr)) || |
| 6490 | ($ptr !~ /\b(union|struct)\s+$attr\b/ && |
| 6491 | WARN("MISPLACED_INIT", |
| 6492 | "$attr should be placed after $var\n" . $herecurr))) && |
| 6493 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6494 | $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 Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6495 | } |
| 6496 | } |
| 6497 | } |
| 6498 | |
| 6499 | # check for $InitAttributeData (ie: __initdata) with const |
| 6500 | if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { |
| 6501 | my $attr = $1; |
| 6502 | $attr =~ /($InitAttributePrefix)(.*)/; |
| 6503 | my $attr_prefix = $1; |
| 6504 | my $attr_type = $2; |
| 6505 | if (ERROR("INIT_ATTRIBUTE", |
| 6506 | "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && |
| 6507 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6508 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6509 | s/$InitAttributeData/${attr_prefix}initconst/; |
| 6510 | } |
| 6511 | } |
| 6512 | |
| 6513 | # check for $InitAttributeConst (ie: __initconst) without const |
| 6514 | if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { |
| 6515 | my $attr = $1; |
| 6516 | if (ERROR("INIT_ATTRIBUTE", |
| 6517 | "Use of $attr requires a separate use of const\n" . $herecurr) && |
| 6518 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6519 | my $lead = $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6520 | /(^\+\s*(?:static\s+))/; |
| 6521 | $lead = rtrim($1); |
| 6522 | $lead = "$lead " if ($lead !~ /^\+$/); |
| 6523 | $lead = "${lead}const "; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6524 | $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; |
| 6525 | } |
| 6526 | } |
| 6527 | |
| 6528 | # check for __read_mostly with const non-pointer (should just be const) |
| 6529 | if ($line =~ /\b__read_mostly\b/ && |
| 6530 | $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { |
| 6531 | if (ERROR("CONST_READ_MOSTLY", |
| 6532 | "Invalid use of __read_mostly with const type\n" . $herecurr) && |
| 6533 | $fix) { |
| 6534 | $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; |
| 6535 | } |
| 6536 | } |
| 6537 | |
| 6538 | # don't use __constant_<foo> functions outside of include/uapi/ |
| 6539 | if ($realfile !~ m@^include/uapi/@ && |
| 6540 | $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { |
| 6541 | my $constant_func = $1; |
| 6542 | my $func = $constant_func; |
| 6543 | $func =~ s/^__constant_//; |
| 6544 | if (WARN("CONSTANT_CONVERSION", |
| 6545 | "$constant_func should be $func\n" . $herecurr) && |
| 6546 | $fix) { |
| 6547 | $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6548 | } |
| 6549 | } |
| 6550 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6551 | # prefer usleep_range over udelay |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6552 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6553 | my $delay = $1; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6554 | # ignore udelay's < 10, however |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6555 | if (! ($delay < 10) ) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6556 | CHK("USLEEP_RANGE", |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6557 | "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6558 | } |
| 6559 | if ($delay > 2000) { |
| 6560 | WARN("LONG_UDELAY", |
| 6561 | "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6562 | } |
| 6563 | } |
| 6564 | |
| 6565 | # warn about unexpectedly long msleep's |
| 6566 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 6567 | if ($1 < 20) { |
| 6568 | WARN("MSLEEP", |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6569 | "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6570 | } |
| 6571 | } |
| 6572 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6573 | # check for comparisons of jiffies |
| 6574 | if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { |
| 6575 | WARN("JIFFIES_COMPARISON", |
| 6576 | "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); |
| 6577 | } |
| 6578 | |
| 6579 | # check for comparisons of get_jiffies_64() |
| 6580 | if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { |
| 6581 | WARN("JIFFIES_COMPARISON", |
| 6582 | "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); |
| 6583 | } |
| 6584 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6585 | # warn about #ifdefs in C files |
| 6586 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
| 6587 | # print "#ifdef in C files should be avoided\n"; |
| 6588 | # print "$herecurr"; |
| 6589 | # $clean = 0; |
| 6590 | # } |
| 6591 | |
| 6592 | # warn about spacing in #ifdefs |
| 6593 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6594 | if (ERROR("SPACING", |
| 6595 | "exactly one space required after that #$1\n" . $herecurr) && |
| 6596 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6597 | $fixed[$fixlinenr] =~ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6598 | s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; |
| 6599 | } |
| 6600 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6601 | } |
| 6602 | |
| 6603 | # check for spinlock_t definitions without a comment. |
| 6604 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 6605 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
| 6606 | my $which = $1; |
| 6607 | if (!ctx_has_comment($first_line, $linenr)) { |
| 6608 | CHK("UNCOMMENTED_DEFINITION", |
| 6609 | "$1 definition without comment\n" . $herecurr); |
| 6610 | } |
| 6611 | } |
| 6612 | # check for memory barriers without a comment. |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6613 | |
| 6614 | my $barriers = qr{ |
| 6615 | mb| |
| 6616 | rmb| |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6617 | wmb |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6618 | }x; |
| 6619 | my $barrier_stems = qr{ |
| 6620 | mb__before_atomic| |
| 6621 | mb__after_atomic| |
| 6622 | store_release| |
| 6623 | load_acquire| |
| 6624 | store_mb| |
| 6625 | (?:$barriers) |
| 6626 | }x; |
| 6627 | my $all_barriers = qr{ |
| 6628 | (?:$barriers)| |
| 6629 | smp_(?:$barrier_stems)| |
| 6630 | virt_(?:$barrier_stems) |
| 6631 | }x; |
| 6632 | |
| 6633 | if ($line =~ /\b(?:$all_barriers)\s*\(/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6634 | if (!ctx_has_comment($first_line, $linenr)) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6635 | WARN("MEMORY_BARRIER", |
| 6636 | "memory barrier without comment\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6637 | } |
| 6638 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6639 | |
| 6640 | my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; |
| 6641 | |
| 6642 | if ($realfile !~ m@^include/asm-generic/@ && |
| 6643 | $realfile !~ m@/barrier\.h$@ && |
| 6644 | $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && |
| 6645 | $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { |
| 6646 | WARN("MEMORY_BARRIER", |
| 6647 | "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); |
| 6648 | } |
| 6649 | |
| 6650 | # check for waitqueue_active without a comment. |
| 6651 | if ($line =~ /\bwaitqueue_active\s*\(/) { |
| 6652 | if (!ctx_has_comment($first_line, $linenr)) { |
| 6653 | WARN("WAITQUEUE_ACTIVE", |
| 6654 | "waitqueue_active without comment\n" . $herecurr); |
| 6655 | } |
| 6656 | } |
| 6657 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6658 | # check for data_race without a comment. |
| 6659 | if ($line =~ /\bdata_race\s*\(/) { |
| 6660 | if (!ctx_has_comment($first_line, $linenr)) { |
| 6661 | WARN("DATA_RACE", |
| 6662 | "data_race without comment\n" . $herecurr); |
| 6663 | } |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6664 | } |
| 6665 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6666 | # check of hardware specific defines |
| 6667 | if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
| 6668 | CHK("ARCH_DEFINES", |
| 6669 | "architecture specific defines should be avoided\n" . $herecurr); |
| 6670 | } |
| 6671 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6672 | # check that the storage class is not after a type |
| 6673 | if ($line =~ /\b($Type)\s+($Storage)\b/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6674 | WARN("STORAGE_CLASS", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6675 | "storage class '$2' should be located before type '$1'\n" . $herecurr); |
| 6676 | } |
| 6677 | # Check that the storage class is at the beginning of a declaration |
| 6678 | if ($line =~ /\b$Storage\b/ && |
| 6679 | $line !~ /^.\s*$Storage/ && |
| 6680 | $line =~ /^.\s*(.+?)\$Storage\s/ && |
| 6681 | $1 !~ /[\,\)]\s*$/) { |
| 6682 | WARN("STORAGE_CLASS", |
| 6683 | "storage class should be at the beginning of the declaration\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
| 6686 | # check the location of the inline attribute, that it is between |
| 6687 | # storage class and type. |
| 6688 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 6689 | $line =~ /\b$Inline\s+$Storage\b/) { |
| 6690 | ERROR("INLINE_LOCATION", |
| 6691 | "inline keyword should sit between storage class and type\n" . $herecurr); |
| 6692 | } |
| 6693 | |
| 6694 | # Check for __inline__ and __inline, prefer inline |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6695 | if ($realfile !~ m@\binclude/uapi/@ && |
| 6696 | $line =~ /\b(__inline__|__inline)\b/) { |
| 6697 | if (WARN("INLINE", |
| 6698 | "plain inline is preferred over $1\n" . $herecurr) && |
| 6699 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6700 | $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6701 | |
| 6702 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6703 | } |
| 6704 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6705 | # Check for compiler attributes |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6706 | if ($realfile !~ m@\binclude/uapi/@ && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6707 | $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) { |
| 6708 | my $attr = $1; |
| 6709 | $attr =~ s/\s*\(\s*(.*)\)\s*/$1/; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6710 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6711 | my %attr_list = ( |
| 6712 | "alias" => "__alias", |
| 6713 | "aligned" => "__aligned", |
| 6714 | "always_inline" => "__always_inline", |
| 6715 | "assume_aligned" => "__assume_aligned", |
| 6716 | "cold" => "__cold", |
| 6717 | "const" => "__attribute_const__", |
| 6718 | "copy" => "__copy", |
| 6719 | "designated_init" => "__designated_init", |
| 6720 | "externally_visible" => "__visible", |
| 6721 | "format" => "printf|scanf", |
| 6722 | "gnu_inline" => "__gnu_inline", |
| 6723 | "malloc" => "__malloc", |
| 6724 | "mode" => "__mode", |
| 6725 | "no_caller_saved_registers" => "__no_caller_saved_registers", |
| 6726 | "noclone" => "__noclone", |
| 6727 | "noinline" => "noinline", |
| 6728 | "nonstring" => "__nonstring", |
| 6729 | "noreturn" => "__noreturn", |
| 6730 | "packed" => "__packed", |
| 6731 | "pure" => "__pure", |
| 6732 | "section" => "__section", |
| 6733 | "used" => "__used", |
| 6734 | "weak" => "__weak" |
| 6735 | ); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6736 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6737 | while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) { |
| 6738 | my $orig_attr = $1; |
| 6739 | my $params = ''; |
| 6740 | $params = $2 if defined($2); |
| 6741 | my $curr_attr = $orig_attr; |
| 6742 | $curr_attr =~ s/^[\s_]+|[\s_]+$//g; |
| 6743 | if (exists($attr_list{$curr_attr})) { |
| 6744 | my $new = $attr_list{$curr_attr}; |
| 6745 | if ($curr_attr eq "format" && $params) { |
| 6746 | $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/; |
| 6747 | $new = "__$1\($2"; |
| 6748 | } else { |
| 6749 | $new = "$new$params"; |
| 6750 | } |
| 6751 | if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", |
| 6752 | "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) && |
| 6753 | $fix) { |
| 6754 | my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?'; |
| 6755 | $fixed[$fixlinenr] =~ s/$remove//; |
| 6756 | $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/; |
| 6757 | $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/; |
| 6758 | $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//; |
| 6759 | } |
| 6760 | } |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6761 | } |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6762 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6763 | # Check for __attribute__ unused, prefer __always_unused or __maybe_unused |
| 6764 | if ($attr =~ /^_*unused/) { |
| 6765 | WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", |
| 6766 | "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6767 | } |
| 6768 | } |
| 6769 | |
| 6770 | # Check for __attribute__ weak, or __weak declarations (may have link issues) |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6771 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6772 | $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && |
| 6773 | ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || |
| 6774 | $line =~ /\b__weak\b/)) { |
| 6775 | ERROR("WEAK_DECLARATION", |
| 6776 | "Using weak declarations can have unintended link defects\n" . $herecurr); |
| 6777 | } |
| 6778 | |
| 6779 | # check for c99 types like uint8_t used outside of uapi/ and tools/ |
| 6780 | if ($realfile !~ m@\binclude/uapi/@ && |
| 6781 | $realfile !~ m@\btools/@ && |
| 6782 | $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { |
| 6783 | my $type = $1; |
| 6784 | if ($type =~ /\b($typeC99Typedefs)\b/) { |
| 6785 | $type = $1; |
| 6786 | my $kernel_type = 'u'; |
| 6787 | $kernel_type = 's' if ($type =~ /^_*[si]/); |
| 6788 | $type =~ /(\d+)/; |
| 6789 | $kernel_type .= $1; |
| 6790 | if (CHK("PREFER_KERNEL_TYPES", |
| 6791 | "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && |
| 6792 | $fix) { |
| 6793 | $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; |
| 6794 | } |
| 6795 | } |
| 6796 | } |
| 6797 | |
| 6798 | # check for cast of C90 native int or longer types constants |
| 6799 | if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { |
| 6800 | my $cast = $1; |
| 6801 | my $const = $2; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6802 | my $suffix = ""; |
| 6803 | my $newconst = $const; |
| 6804 | $newconst =~ s/${Int_type}$//; |
| 6805 | $suffix .= 'U' if ($cast =~ /\bunsigned\b/); |
| 6806 | if ($cast =~ /\blong\s+long\b/) { |
| 6807 | $suffix .= 'LL'; |
| 6808 | } elsif ($cast =~ /\blong\b/) { |
| 6809 | $suffix .= 'L'; |
| 6810 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6811 | if (WARN("TYPECAST_INT_CONSTANT", |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6812 | "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6813 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6814 | $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6815 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6816 | } |
| 6817 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6818 | # check for sizeof(&) |
| 6819 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
| 6820 | WARN("SIZEOF_ADDRESS", |
| 6821 | "sizeof(& should be avoided\n" . $herecurr); |
| 6822 | } |
| 6823 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6824 | # check for sizeof without parenthesis |
| 6825 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6826 | if (WARN("SIZEOF_PARENTHESIS", |
| 6827 | "sizeof $1 should be sizeof($1)\n" . $herecurr) && |
| 6828 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6829 | $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6830 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6833 | # check for struct spinlock declarations |
| 6834 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 6835 | WARN("USE_SPINLOCK_T", |
| 6836 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 6837 | } |
| 6838 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6839 | # check for seq_printf uses that could be seq_puts |
| 6840 | if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { |
| 6841 | my $fmt = get_quoted_string($line, $rawline); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6842 | $fmt =~ s/%%//g; |
| 6843 | if ($fmt !~ /%/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6844 | if (WARN("PREFER_SEQ_PUTS", |
| 6845 | "Prefer seq_puts to seq_printf\n" . $herecurr) && |
| 6846 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6847 | $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6848 | } |
| 6849 | } |
| 6850 | } |
| 6851 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6852 | # check for vsprintf extension %p<foo> misuses |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6853 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6854 | defined $stat && |
| 6855 | $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && |
| 6856 | $1 !~ /^_*volatile_*$/) { |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6857 | my $stat_real; |
| 6858 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6859 | my $lc = $stat =~ tr@\n@@; |
| 6860 | $lc = $lc + $linenr; |
| 6861 | for (my $count = $linenr; $count <= $lc; $count++) { |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6862 | my $specifier; |
| 6863 | my $extension; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6864 | my $qualifier; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6865 | my $bad_specifier = ""; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6866 | my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); |
| 6867 | $fmt =~ s/%%//g; |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6868 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6869 | while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6870 | $specifier = $1; |
| 6871 | $extension = $2; |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6872 | $qualifier = $3; |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6873 | if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ || |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6874 | ($extension eq "f" && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 6875 | defined $qualifier && $qualifier !~ /^w/) || |
| 6876 | ($extension eq "4" && |
| 6877 | defined $qualifier && $qualifier !~ /^cc/)) { |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6878 | $bad_specifier = $specifier; |
| 6879 | last; |
| 6880 | } |
| 6881 | if ($extension eq "x" && !defined($stat_real)) { |
| 6882 | if (!defined($stat_real)) { |
| 6883 | $stat_real = get_stat_real($linenr, $lc); |
| 6884 | } |
| 6885 | WARN("VSPRINTF_SPECIFIER_PX", |
| 6886 | "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"); |
| 6887 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6888 | } |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6889 | if ($bad_specifier ne "") { |
| 6890 | my $stat_real = get_stat_real($linenr, $lc); |
| 6891 | my $ext_type = "Invalid"; |
| 6892 | my $use = ""; |
| 6893 | if ($bad_specifier =~ /p[Ff]/) { |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6894 | $use = " - use %pS instead"; |
| 6895 | $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); |
| 6896 | } |
| 6897 | |
| 6898 | WARN("VSPRINTF_POINTER_EXTENSION", |
| 6899 | "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6900 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6901 | } |
| 6902 | } |
| 6903 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6904 | # Check for misused memsets |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6905 | if ($perl_version_ok && |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6906 | defined $stat && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6907 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6908 | |
| 6909 | my $ms_addr = $2; |
| 6910 | my $ms_val = $7; |
| 6911 | my $ms_size = $12; |
| 6912 | |
| 6913 | if ($ms_size =~ /^(0x|)0$/i) { |
| 6914 | ERROR("MEMSET", |
| 6915 | "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); |
| 6916 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 6917 | WARN("MEMSET", |
| 6918 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 6919 | } |
| 6920 | } |
| 6921 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6922 | # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6923 | # if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6924 | # defined $stat && |
| 6925 | # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 6926 | # if (WARN("PREFER_ETHER_ADDR_COPY", |
| 6927 | # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && |
| 6928 | # $fix) { |
| 6929 | # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; |
| 6930 | # } |
| 6931 | # } |
| 6932 | |
| 6933 | # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6934 | # if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6935 | # defined $stat && |
| 6936 | # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 6937 | # WARN("PREFER_ETHER_ADDR_EQUAL", |
| 6938 | # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") |
| 6939 | # } |
| 6940 | |
| 6941 | # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr |
| 6942 | # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6943 | # if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6944 | # defined $stat && |
| 6945 | # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 6946 | # |
| 6947 | # my $ms_val = $7; |
| 6948 | # |
| 6949 | # if ($ms_val =~ /^(?:0x|)0+$/i) { |
| 6950 | # if (WARN("PREFER_ETH_ZERO_ADDR", |
| 6951 | # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && |
| 6952 | # $fix) { |
| 6953 | # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; |
| 6954 | # } |
| 6955 | # } elsif ($ms_val =~ /^(?:0xff|255)$/i) { |
| 6956 | # if (WARN("PREFER_ETH_BROADCAST_ADDR", |
| 6957 | # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && |
| 6958 | # $fix) { |
| 6959 | # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; |
| 6960 | # } |
| 6961 | # } |
| 6962 | # } |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6963 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6964 | # typecasts on min/max could be min_t/max_t |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6965 | if ($perl_version_ok && |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6966 | defined $stat && |
| 6967 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
| 6968 | if (defined $2 || defined $7) { |
| 6969 | my $call = $1; |
| 6970 | my $cast1 = deparenthesize($2); |
| 6971 | my $arg1 = $3; |
| 6972 | my $cast2 = deparenthesize($7); |
| 6973 | my $arg2 = $8; |
| 6974 | my $cast; |
| 6975 | |
| 6976 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
| 6977 | $cast = "$cast1 or $cast2"; |
| 6978 | } elsif ($cast1 ne "") { |
| 6979 | $cast = $cast1; |
| 6980 | } else { |
| 6981 | $cast = $cast2; |
| 6982 | } |
| 6983 | WARN("MINMAX", |
| 6984 | "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); |
| 6985 | } |
| 6986 | } |
| 6987 | |
| 6988 | # check usleep_range arguments |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6989 | if ($perl_version_ok && |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6990 | defined $stat && |
| 6991 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 6992 | my $min = $1; |
| 6993 | my $max = $7; |
| 6994 | if ($min eq $max) { |
| 6995 | WARN("USLEEP_RANGE", |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6996 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6997 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 6998 | $min > $max) { |
| 6999 | WARN("USLEEP_RANGE", |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 7000 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7001 | } |
| 7002 | } |
| 7003 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7004 | # check for naked sscanf |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7005 | if ($perl_version_ok && |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7006 | defined $stat && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7007 | $line =~ /\bsscanf\b/ && |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7008 | ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && |
| 7009 | $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && |
| 7010 | $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { |
| 7011 | my $lc = $stat =~ tr@\n@@; |
| 7012 | $lc = $lc + $linenr; |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7013 | my $stat_real = get_stat_real($linenr, $lc); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7014 | WARN("NAKED_SSCANF", |
| 7015 | "unchecked sscanf return value\n" . "$here\n$stat_real\n"); |
| 7016 | } |
| 7017 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7018 | # check for simple sscanf that should be kstrto<foo> |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7019 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7020 | defined $stat && |
| 7021 | $line =~ /\bsscanf\b/) { |
| 7022 | my $lc = $stat =~ tr@\n@@; |
| 7023 | $lc = $lc + $linenr; |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7024 | my $stat_real = get_stat_real($linenr, $lc); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7025 | if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { |
| 7026 | my $format = $6; |
| 7027 | my $count = $format =~ tr@%@%@; |
| 7028 | if ($count == 1 && |
| 7029 | $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { |
| 7030 | WARN("SSCANF_TO_KSTRTO", |
| 7031 | "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); |
| 7032 | } |
| 7033 | } |
| 7034 | } |
| 7035 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7036 | # check for new externs in .h files. |
| 7037 | if ($realfile =~ /\.h$/ && |
| 7038 | $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { |
| 7039 | if (CHK("AVOID_EXTERNS", |
| 7040 | "extern prototypes should be avoided in .h files\n" . $herecurr) && |
| 7041 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7042 | $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7043 | } |
| 7044 | } |
| 7045 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7046 | # check for new externs in .c files. |
| 7047 | if ($realfile =~ /\.c$/ && defined $stat && |
| 7048 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
| 7049 | { |
| 7050 | my $function_name = $1; |
| 7051 | my $paren_space = $2; |
| 7052 | |
| 7053 | my $s = $stat; |
| 7054 | if (defined $cond) { |
| 7055 | substr($s, 0, length($cond), ''); |
| 7056 | } |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7057 | if ($s =~ /^\s*;/) |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7058 | { |
| 7059 | WARN("AVOID_EXTERNS", |
| 7060 | "externs should be avoided in .c files\n" . $herecurr); |
| 7061 | } |
| 7062 | |
| 7063 | if ($paren_space =~ /\n/) { |
| 7064 | WARN("FUNCTION_ARGUMENTS", |
| 7065 | "arguments for function declarations should follow identifier\n" . $herecurr); |
| 7066 | } |
| 7067 | |
| 7068 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 7069 | $stat =~ /^.\s*extern\s+/) |
| 7070 | { |
| 7071 | WARN("AVOID_EXTERNS", |
| 7072 | "externs should be avoided in .c files\n" . $herecurr); |
| 7073 | } |
| 7074 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7075 | # check for function declarations that have arguments without identifier names |
| 7076 | if (defined $stat && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7077 | $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && |
| 7078 | $1 ne "void") { |
| 7079 | my $args = trim($1); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7080 | while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { |
| 7081 | my $arg = trim($1); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7082 | if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7083 | WARN("FUNCTION_ARGUMENTS", |
| 7084 | "function definition argument '$arg' should also have an identifier name\n" . $herecurr); |
| 7085 | } |
| 7086 | } |
| 7087 | } |
| 7088 | |
| 7089 | # check for function definitions |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7090 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7091 | defined $stat && |
| 7092 | $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { |
| 7093 | $context_function = $1; |
| 7094 | |
| 7095 | # check for multiline function definition with misplaced open brace |
| 7096 | my $ok = 0; |
| 7097 | my $cnt = statement_rawlines($stat); |
| 7098 | my $herectx = $here . "\n"; |
| 7099 | for (my $n = 0; $n < $cnt; $n++) { |
| 7100 | my $rl = raw_line($linenr, $n); |
| 7101 | $herectx .= $rl . "\n"; |
| 7102 | $ok = 1 if ($rl =~ /^[ \+]\{/); |
| 7103 | $ok = 1 if ($rl =~ /\{/ && $n == 0); |
| 7104 | last if $rl =~ /^[ \+].*\{/; |
| 7105 | } |
| 7106 | if (!$ok) { |
| 7107 | ERROR("OPEN_BRACE", |
| 7108 | "open brace '{' following function definitions go on the next line\n" . $herectx); |
| 7109 | } |
| 7110 | } |
| 7111 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7112 | # checks for new __setup's |
| 7113 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 7114 | my $name = $1; |
| 7115 | |
| 7116 | if (!grep(/$name/, @setup_docs)) { |
| 7117 | CHK("UNDOCUMENTED_SETUP", |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7118 | "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7119 | } |
| 7120 | } |
| 7121 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 7122 | # check for pointless casting of alloc functions |
| 7123 | if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7124 | WARN("UNNECESSARY_CASTS", |
| 7125 | "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
| 7126 | } |
| 7127 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7128 | # alloc style |
| 7129 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7130 | if ($perl_version_ok && |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 7131 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7132 | CHK("ALLOC_SIZEOF_STRUCT", |
| 7133 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 7134 | } |
| 7135 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7136 | # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7137 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7138 | defined $stat && |
| 7139 | $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { |
| 7140 | my $oldfunc = $3; |
| 7141 | my $a1 = $4; |
| 7142 | my $a2 = $10; |
| 7143 | my $newfunc = "kmalloc_array"; |
| 7144 | $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); |
| 7145 | my $r1 = $a1; |
| 7146 | my $r2 = $a2; |
| 7147 | if ($a1 =~ /^sizeof\s*\S/) { |
| 7148 | $r1 = $a2; |
| 7149 | $r2 = $a1; |
| 7150 | } |
| 7151 | if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && |
| 7152 | !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7153 | my $cnt = statement_rawlines($stat); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7154 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 7155 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7156 | if (WARN("ALLOC_WITH_MULTIPLY", |
| 7157 | "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && |
| 7158 | $cnt == 1 && |
| 7159 | $fix) { |
| 7160 | $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; |
| 7161 | } |
| 7162 | } |
| 7163 | } |
| 7164 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7165 | # check for krealloc arg reuse |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7166 | if ($perl_version_ok && |
| 7167 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && |
| 7168 | $1 eq $3) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7169 | WARN("KREALLOC_ARG_REUSE", |
| 7170 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 7171 | } |
| 7172 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7173 | # check for alloc argument mismatch |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7174 | if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7175 | WARN("ALLOC_ARRAY_ARGS", |
| 7176 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7177 | } |
| 7178 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7179 | # check for multiple semicolons |
| 7180 | if ($line =~ /;\s*;\s*$/) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7181 | if (WARN("ONE_SEMICOLON", |
| 7182 | "Statements terminations use 1 semicolon\n" . $herecurr) && |
| 7183 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7184 | $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; |
| 7185 | } |
| 7186 | } |
| 7187 | |
| 7188 | # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi |
| 7189 | if ($realfile !~ m@^include/uapi/@ && |
| 7190 | $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { |
| 7191 | my $ull = ""; |
| 7192 | $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); |
| 7193 | if (CHK("BIT_MACRO", |
| 7194 | "Prefer using the BIT$ull macro\n" . $herecurr) && |
| 7195 | $fix) { |
| 7196 | $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; |
| 7197 | } |
| 7198 | } |
| 7199 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7200 | # check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too) |
| 7201 | if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) { |
| 7202 | WARN("IS_ENABLED_CONFIG", |
| 7203 | "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr); |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7204 | } |
| 7205 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7206 | # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE |
| 7207 | if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) { |
| 7208 | my $config = $1; |
| 7209 | if (WARN("PREFER_IS_ENABLED", |
| 7210 | "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) && |
| 7211 | $fix) { |
| 7212 | $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7213 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 7216 | # check for /* fallthrough */ like comment, prefer fallthrough; |
| 7217 | my @fallthroughs = ( |
| 7218 | 'fallthrough', |
| 7219 | '@fallthrough@', |
| 7220 | 'lint -fallthrough[ \t]*', |
| 7221 | 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', |
| 7222 | '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', |
| 7223 | 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', |
| 7224 | 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', |
| 7225 | ); |
| 7226 | if ($raw_comment ne '') { |
| 7227 | foreach my $ft (@fallthroughs) { |
| 7228 | if ($raw_comment =~ /$ft/) { |
| 7229 | my $msg_level = \&WARN; |
| 7230 | $msg_level = \&CHK if ($file); |
| 7231 | &{$msg_level}("PREFER_FALLTHROUGH", |
| 7232 | "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); |
| 7233 | last; |
| 7234 | } |
| 7235 | } |
| 7236 | } |
| 7237 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7238 | # check for switch/default statements without a break; |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7239 | if ($perl_version_ok && |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7240 | defined $stat && |
| 7241 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7242 | my $cnt = statement_rawlines($stat); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7243 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 7244 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7245 | WARN("DEFAULT_NO_BREAK", |
| 7246 | "switch default: should use break\n" . $herectx); |
Eric Nelson | 172a3a8 | 2012-05-02 20:32:18 -0700 | [diff] [blame] | 7247 | } |
| 7248 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7249 | # check for gcc specific __FUNCTION__ |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7250 | if ($line =~ /\b__FUNCTION__\b/) { |
| 7251 | if (WARN("USE_FUNC", |
| 7252 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && |
| 7253 | $fix) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7254 | $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7255 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7256 | } |
| 7257 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7258 | # check for uses of __DATE__, __TIME__, __TIMESTAMP__ |
| 7259 | while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { |
| 7260 | ERROR("DATE_TIME", |
| 7261 | "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); |
| 7262 | } |
| 7263 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7264 | # check for use of yield() |
| 7265 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 7266 | WARN("YIELD", |
| 7267 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 7268 | } |
| 7269 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7270 | # check for comparisons against true and false |
| 7271 | if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { |
| 7272 | my $lead = $1; |
| 7273 | my $arg = $2; |
| 7274 | my $test = $3; |
| 7275 | my $otype = $4; |
| 7276 | my $trail = $5; |
| 7277 | my $op = "!"; |
| 7278 | |
| 7279 | ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); |
| 7280 | |
| 7281 | my $type = lc($otype); |
| 7282 | if ($type =~ /^(?:true|false)$/) { |
| 7283 | if (("$test" eq "==" && "$type" eq "true") || |
| 7284 | ("$test" eq "!=" && "$type" eq "false")) { |
| 7285 | $op = ""; |
| 7286 | } |
| 7287 | |
| 7288 | CHK("BOOL_COMPARISON", |
| 7289 | "Using comparison to $otype is error prone\n" . $herecurr); |
| 7290 | |
| 7291 | ## maybe suggesting a correct construct would better |
| 7292 | ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); |
| 7293 | |
| 7294 | } |
| 7295 | } |
| 7296 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7297 | # check for semaphores initialized locked |
| 7298 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
| 7299 | WARN("CONSIDER_COMPLETION", |
| 7300 | "consider using a completion\n" . $herecurr); |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7301 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7302 | |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7303 | # recommend kstrto* over simple_strto* and strict_strto* |
| 7304 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7305 | WARN("CONSIDER_KSTRTO", |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7306 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7307 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7308 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7309 | # check for __initcall(), use device_initcall() explicitly or more appropriate function please |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7310 | if ($line =~ /^.\s*__initcall\s*\(/) { |
| 7311 | WARN("USE_DEVICE_INITCALL", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7312 | "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7313 | } |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7314 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 7315 | # check for spin_is_locked(), suggest lockdep instead |
| 7316 | if ($line =~ /\bspin_is_locked\(/) { |
| 7317 | WARN("USE_LOCKDEP", |
| 7318 | "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); |
| 7319 | } |
| 7320 | |
| 7321 | # check for deprecated apis |
| 7322 | if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { |
| 7323 | my $deprecated_api = $1; |
| 7324 | my $new_api = $deprecated_apis{$deprecated_api}; |
| 7325 | WARN("DEPRECATED_API", |
| 7326 | "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); |
| 7327 | } |
| 7328 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7329 | # check for various structs that are normally const (ops, kgdb, device_tree) |
| 7330 | # and avoid what seem like struct definitions 'struct foo {' |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7331 | if (defined($const_structs) && |
| 7332 | $line !~ /\bconst\b/ && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7333 | $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7334 | WARN("CONST_STRUCT", |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7335 | "struct $1 should normally be const\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | # use of NR_CPUS is usually wrong |
| 7339 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7340 | # ignore designated initializers using NR_CPUS |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7341 | if ($line =~ /\bNR_CPUS\b/ && |
| 7342 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 7343 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
| 7344 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 7345 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7346 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ && |
| 7347 | $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/) |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7348 | { |
| 7349 | WARN("NR_CPUS", |
| 7350 | "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); |
| 7351 | } |
| 7352 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7353 | # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. |
| 7354 | if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { |
| 7355 | ERROR("DEFINE_ARCH_HAS", |
| 7356 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); |
| 7357 | } |
| 7358 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7359 | # likely/unlikely comparisons similar to "(likely(foo) > 0)" |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7360 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7361 | $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { |
| 7362 | WARN("LIKELY_MISUSE", |
| 7363 | "Using $1 should generally have parentheses around the comparison\n" . $herecurr); |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7364 | } |
| 7365 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7366 | # return sysfs_emit(foo, fmt, ...) fmt without newline |
| 7367 | if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ && |
| 7368 | substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) { |
| 7369 | my $offset = $+[6] - 1; |
| 7370 | if (WARN("SYSFS_EMIT", |
| 7371 | "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) && |
| 7372 | $fix) { |
| 7373 | substr($fixed[$fixlinenr], $offset, 0) = '\\n'; |
| 7374 | } |
| 7375 | } |
| 7376 | |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 7377 | # nested likely/unlikely calls |
| 7378 | if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { |
| 7379 | WARN("LIKELY_MISUSE", |
| 7380 | "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); |
| 7381 | } |
| 7382 | |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7383 | # whine mightly about in_atomic |
| 7384 | if ($line =~ /\bin_atomic\s*\(/) { |
| 7385 | if ($realfile =~ m@^drivers/@) { |
| 7386 | ERROR("IN_ATOMIC", |
| 7387 | "do not use in_atomic in drivers\n" . $herecurr); |
| 7388 | } elsif ($realfile !~ m@^kernel/@) { |
| 7389 | WARN("IN_ATOMIC", |
| 7390 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
| 7391 | } |
| 7392 | } |
| 7393 | |
| 7394 | # check for lockdep_set_novalidate_class |
| 7395 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 7396 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 7397 | if ($realfile !~ m@^kernel/lockdep@ && |
| 7398 | $realfile !~ m@^include/linux/lockdep@ && |
| 7399 | $realfile !~ m@^drivers/base/core@) { |
| 7400 | ERROR("LOCKDEP", |
| 7401 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
| 7402 | } |
| 7403 | } |
| 7404 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7405 | if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || |
| 7406 | $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7407 | WARN("EXPORTED_WORLD_WRITABLE", |
| 7408 | "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); |
| 7409 | } |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7410 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7411 | # check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> |
| 7412 | # and whether or not function naming is typical and if |
| 7413 | # DEVICE_ATTR permissions uses are unusual too |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7414 | if ($perl_version_ok && |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7415 | defined $stat && |
| 7416 | $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*\)/) { |
| 7417 | my $var = $1; |
| 7418 | my $perms = $2; |
| 7419 | my $show = $3; |
| 7420 | my $store = $4; |
| 7421 | my $octal_perms = perms_to_octal($perms); |
| 7422 | if ($show =~ /^${var}_show$/ && |
| 7423 | $store =~ /^${var}_store$/ && |
| 7424 | $octal_perms eq "0644") { |
| 7425 | if (WARN("DEVICE_ATTR_RW", |
| 7426 | "Use DEVICE_ATTR_RW\n" . $herecurr) && |
| 7427 | $fix) { |
| 7428 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/; |
| 7429 | } |
| 7430 | } elsif ($show =~ /^${var}_show$/ && |
| 7431 | $store =~ /^NULL$/ && |
| 7432 | $octal_perms eq "0444") { |
| 7433 | if (WARN("DEVICE_ATTR_RO", |
| 7434 | "Use DEVICE_ATTR_RO\n" . $herecurr) && |
| 7435 | $fix) { |
| 7436 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/; |
| 7437 | } |
| 7438 | } elsif ($show =~ /^NULL$/ && |
| 7439 | $store =~ /^${var}_store$/ && |
| 7440 | $octal_perms eq "0200") { |
| 7441 | if (WARN("DEVICE_ATTR_WO", |
| 7442 | "Use DEVICE_ATTR_WO\n" . $herecurr) && |
| 7443 | $fix) { |
| 7444 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/; |
| 7445 | } |
| 7446 | } elsif ($octal_perms eq "0644" || |
| 7447 | $octal_perms eq "0444" || |
| 7448 | $octal_perms eq "0200") { |
| 7449 | my $newshow = "$show"; |
| 7450 | $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); |
| 7451 | my $newstore = $store; |
| 7452 | $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); |
| 7453 | my $rename = ""; |
| 7454 | if ($show ne $newshow) { |
| 7455 | $rename .= " '$show' to '$newshow'"; |
| 7456 | } |
| 7457 | if ($store ne $newstore) { |
| 7458 | $rename .= " '$store' to '$newstore'"; |
| 7459 | } |
| 7460 | WARN("DEVICE_ATTR_FUNCTIONS", |
| 7461 | "Consider renaming function(s)$rename\n" . $herecurr); |
| 7462 | } else { |
| 7463 | WARN("DEVICE_ATTR_PERMS", |
| 7464 | "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); |
| 7465 | } |
| 7466 | } |
| 7467 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7468 | # Mode permission misuses where it seems decimal should be octal |
| 7469 | # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7470 | # o Ignore module_param*(...) uses with a decimal 0 permission as that has a |
| 7471 | # specific definition of not visible in sysfs. |
| 7472 | # o Ignore proc_create*(...) uses with a decimal 0 permission as that means |
| 7473 | # use the default permissions |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7474 | if ($perl_version_ok && |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7475 | defined $stat && |
| 7476 | $line =~ /$mode_perms_search/) { |
| 7477 | foreach my $entry (@mode_permission_funcs) { |
| 7478 | my $func = $entry->[0]; |
| 7479 | my $arg_pos = $entry->[1]; |
| 7480 | |
| 7481 | my $lc = $stat =~ tr@\n@@; |
| 7482 | $lc = $lc + $linenr; |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7483 | my $stat_real = get_stat_real($linenr, $lc); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7484 | |
| 7485 | my $skip_args = ""; |
| 7486 | if ($arg_pos > 1) { |
| 7487 | $arg_pos--; |
| 7488 | $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; |
| 7489 | } |
| 7490 | my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; |
| 7491 | if ($stat =~ /$test/) { |
| 7492 | my $val = $1; |
| 7493 | $val = $6 if ($skip_args ne ""); |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7494 | if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && |
| 7495 | (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || |
| 7496 | ($val =~ /^$Octal$/ && length($val) ne 4))) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7497 | ERROR("NON_OCTAL_PERMISSIONS", |
| 7498 | "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); |
| 7499 | } |
| 7500 | if ($val =~ /^$Octal$/ && (oct($val) & 02)) { |
| 7501 | ERROR("EXPORTED_WORLD_WRITABLE", |
| 7502 | "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); |
| 7503 | } |
| 7504 | } |
| 7505 | } |
| 7506 | } |
| 7507 | |
| 7508 | # check for uses of S_<PERMS> that could be octal for readability |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7509 | while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { |
| 7510 | my $oval = $1; |
| 7511 | my $octal = perms_to_octal($oval); |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7512 | if (WARN("SYMBOLIC_PERMS", |
| 7513 | "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && |
| 7514 | $fix) { |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7515 | $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7516 | } |
| 7517 | } |
| 7518 | |
| 7519 | # validate content of MODULE_LICENSE against list from include/linux/module.h |
| 7520 | if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { |
| 7521 | my $extracted_string = get_quoted_string($line, $rawline); |
| 7522 | my $valid_licenses = qr{ |
| 7523 | GPL| |
| 7524 | GPL\ v2| |
| 7525 | GPL\ and\ additional\ rights| |
| 7526 | Dual\ BSD/GPL| |
| 7527 | Dual\ MIT/GPL| |
| 7528 | Dual\ MPL/GPL| |
| 7529 | Proprietary |
| 7530 | }x; |
| 7531 | if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { |
| 7532 | WARN("MODULE_LICENSE", |
| 7533 | "unknown module license " . $extracted_string . "\n" . $herecurr); |
| 7534 | } |
| 7535 | } |
Tom Rini | c57383b | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 7536 | |
| 7537 | # check for sysctl duplicate constants |
| 7538 | if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { |
| 7539 | WARN("DUPLICATED_SYSCTL_CONST", |
| 7540 | "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); |
| 7541 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7542 | } |
| 7543 | |
| 7544 | # If we have no input at all, then there is nothing to report on |
| 7545 | # so just keep quiet. |
| 7546 | if ($#rawlines == -1) { |
| 7547 | exit(0); |
| 7548 | } |
| 7549 | |
| 7550 | # In mailback mode only produce a report in the negative, for |
| 7551 | # things that appear to be patches. |
| 7552 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 7553 | exit(0); |
| 7554 | } |
| 7555 | |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7556 | # This is not a patch, and we are in 'no-patch' mode so |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7557 | # just keep quiet. |
| 7558 | if (!$chk_patch && !$is_patch) { |
| 7559 | exit(0); |
| 7560 | } |
| 7561 | |
Heinrich Schuchardt | c398f2d | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 7562 | if (!$is_patch && $filename !~ /cover-letter\.patch$/) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7563 | ERROR("NOT_UNIFIED_DIFF", |
| 7564 | "Does not appear to be a unified-diff format patch\n"); |
| 7565 | } |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7566 | if ($is_patch && $has_commit_log && $chk_signoff) { |
| 7567 | if ($signoff == 0) { |
| 7568 | ERROR("MISSING_SIGN_OFF", |
| 7569 | "Missing Signed-off-by: line(s)\n"); |
Tom Rini | e199fb3 | 2021-08-03 08:31:56 -0400 | [diff] [blame] | 7570 | } elsif ($authorsignoff != 1) { |
| 7571 | # authorsignoff values: |
| 7572 | # 0 -> missing sign off |
| 7573 | # 1 -> sign off identical |
| 7574 | # 2 -> names and addresses match, comments mismatch |
| 7575 | # 3 -> addresses match, names different |
| 7576 | # 4 -> names match, addresses different |
| 7577 | # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match |
| 7578 | |
| 7579 | my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'"; |
| 7580 | |
| 7581 | if ($authorsignoff == 0) { |
| 7582 | ERROR("NO_AUTHOR_SIGN_OFF", |
| 7583 | "Missing Signed-off-by: line by nominal patch author '$author'\n"); |
| 7584 | } elsif ($authorsignoff == 2) { |
| 7585 | CHK("FROM_SIGN_OFF_MISMATCH", |
| 7586 | "From:/Signed-off-by: email comments mismatch: $sob_msg\n"); |
| 7587 | } elsif ($authorsignoff == 3) { |
| 7588 | WARN("FROM_SIGN_OFF_MISMATCH", |
| 7589 | "From:/Signed-off-by: email name mismatch: $sob_msg\n"); |
| 7590 | } elsif ($authorsignoff == 4) { |
| 7591 | WARN("FROM_SIGN_OFF_MISMATCH", |
| 7592 | "From:/Signed-off-by: email address mismatch: $sob_msg\n"); |
| 7593 | } elsif ($authorsignoff == 5) { |
| 7594 | WARN("FROM_SIGN_OFF_MISMATCH", |
| 7595 | "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n"); |
| 7596 | } |
Heinrich Schuchardt | c261fef | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 7597 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7598 | } |
| 7599 | |
| 7600 | print report_dump(); |
| 7601 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 7602 | print "$filename " if ($summary_file); |
| 7603 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 7604 | (($check)? "$cnt_chk checks, " : "") . |
| 7605 | "$cnt_lines lines checked\n"; |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7606 | } |
| 7607 | |
| 7608 | if ($quiet == 0) { |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7609 | # If there were any defects found and not already fixing them |
| 7610 | if (!$clean and !$fix) { |
| 7611 | print << "EOM" |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7612 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7613 | NOTE: For some of the reported defects, checkpatch may be able to |
| 7614 | mechanically convert to the typical style using --fix or --fix-inplace. |
| 7615 | EOM |
Kim Phillips | d45a6ae | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 7616 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7617 | # If there were whitespace errors which cleanpatch can fix |
| 7618 | # then suggest that. |
| 7619 | if ($rpt_cleaners) { |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7620 | $rpt_cleaners = 0; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7621 | print << "EOM" |
| 7622 | |
| 7623 | NOTE: Whitespace errors detected. |
| 7624 | You may wish to use scripts/cleanpatch or scripts/cleanfile |
| 7625 | EOM |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7626 | } |
| 7627 | } |
| 7628 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7629 | if ($clean == 0 && $fix && |
| 7630 | ("@rawlines" ne "@fixed" || |
| 7631 | $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7632 | my $newfile = $filename; |
| 7633 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); |
| 7634 | my $linecount = 0; |
| 7635 | my $f; |
| 7636 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7637 | @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); |
| 7638 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7639 | open($f, '>', $newfile) |
| 7640 | or die "$P: Can't open $newfile for write\n"; |
| 7641 | foreach my $fixed_line (@fixed) { |
| 7642 | $linecount++; |
| 7643 | if ($file) { |
| 7644 | if ($linecount > 3) { |
| 7645 | $fixed_line =~ s/^\+//; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7646 | print $f $fixed_line . "\n"; |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7647 | } |
| 7648 | } else { |
| 7649 | print $f $fixed_line . "\n"; |
| 7650 | } |
| 7651 | } |
| 7652 | close($f); |
| 7653 | |
| 7654 | if (!$quiet) { |
| 7655 | print << "EOM"; |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7656 | |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7657 | Wrote EXPERIMENTAL --fix correction(s) to '$newfile' |
| 7658 | |
| 7659 | Do _NOT_ trust the results written to this file. |
| 7660 | Do _NOT_ submit these changes without inspecting them for correctness. |
| 7661 | |
| 7662 | This EXPERIMENTAL file is simply a convenience to help rewrite patches. |
| 7663 | No warranties, expressed or implied... |
Tom Rini | 6b9709d | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 7664 | EOM |
| 7665 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7666 | } |
| 7667 | |
Heinrich Schuchardt | 6305db9 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 7668 | if ($quiet == 0) { |
| 7669 | print "\n"; |
| 7670 | if ($clean == 1) { |
| 7671 | print "$vname has no obvious style problems and is ready for submission.\n"; |
| 7672 | } else { |
| 7673 | print "$vname has style problems, please review.\n"; |
| 7674 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7675 | } |
Joe Hershberger | 0562219 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 7676 | return $clean; |
| 7677 | } |