Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
Joe Hershberger | ba2beb5 | 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 | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 8 | # (c) 2010-2018 Joe Perches <joe@perches.com> |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 9 | |
| 10 | use strict; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 11 | use warnings; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 12 | use POSIX; |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 13 | use File::Basename; |
| 14 | use Cwd 'abs_path'; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 15 | use Term::ANSIColor qw(:constants); |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 16 | use Encode qw(decode encode); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 17 | |
| 18 | my $P = $0; |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 19 | my $D = dirname(abs_path($P)); |
Joe Hershberger | ba2beb5 | 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; |
| 26 | my $tree = 1; |
| 27 | my $chk_signoff = 1; |
| 28 | my $chk_patch = 1; |
| 29 | my $tst_only; |
| 30 | my $emacs = 0; |
| 31 | my $terse = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 32 | my $showfile = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 33 | my $file = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 34 | my $git = 0; |
| 35 | my %git_commits = (); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 36 | my $check = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 37 | my $check_orig = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 38 | my $summary = 1; |
| 39 | my $mailback = 0; |
| 40 | my $summary_file = 0; |
| 41 | my $show_types = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 42 | my $list_types = 0; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 43 | my $fix = 0; |
| 44 | my $fix_inplace = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 45 | my $root; |
| 46 | my %debug; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 47 | my %camelcase = (); |
| 48 | my %use_type = (); |
| 49 | my @use = (); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 50 | my %ignore_type = (); |
| 51 | my @ignore = (); |
| 52 | my $help = 0; |
| 53 | my $configuration_file = ".checkpatch.conf"; |
Simon Glass | a33d982 | 2020-05-22 16:32:35 -0600 | [diff] [blame] | 54 | my $max_line_length = 100; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 55 | my $ignore_perl_version = 0; |
| 56 | my $minimum_perl_version = 5.10.0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 57 | my $min_conf_desc_length = 4; |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 58 | my $spelling_file = "$D/spelling.txt"; |
| 59 | my $codespell = 0; |
| 60 | my $codespellfile = "/usr/share/codespell/dictionary.txt"; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 61 | my $conststructsfile = "$D/const_structs.checkpatch"; |
| 62 | my $typedefsfile = ""; |
Simon Glass | c8f4d09 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 63 | my $u_boot = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 64 | my $color = "auto"; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 65 | my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE |
| 66 | # git output parsing needs US English output, so first set backtick child process LANGUAGE |
| 67 | my $git_command ='export LANGUAGE=en_US.UTF-8; git'; |
| 68 | my $tabsize = 8; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 69 | |
| 70 | sub help { |
| 71 | my ($exitcode) = @_; |
| 72 | |
| 73 | print << "EOM"; |
| 74 | Usage: $P [OPTION]... [FILE]... |
| 75 | Version: $V |
| 76 | |
| 77 | Options: |
| 78 | -q, --quiet quiet |
| 79 | --no-tree run without a kernel tree |
| 80 | --no-signoff do not check for 'Signed-off-by' line |
| 81 | --patch treat FILE as patchfile (default) |
| 82 | --emacs emacs compile window format |
| 83 | --terse one line per report |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 84 | --showfile emit diffed file position, not input file position |
| 85 | -g, --git treat FILE as a single commit or git revision range |
| 86 | single git commit with: |
| 87 | <rev> |
| 88 | <rev>^ |
| 89 | <rev>~n |
| 90 | multiple git commits with: |
| 91 | <rev1>..<rev2> |
| 92 | <rev1>...<rev2> |
| 93 | <rev>-<count> |
| 94 | git merges are ignored |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 95 | -f, --file treat FILE as regular source file |
| 96 | --subjective, --strict enable more subjective tests |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 97 | --list-types list the possible message types |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 98 | --types TYPE(,TYPE2...) show only these comma separated message types |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 99 | --ignore TYPE(,TYPE2...) ignore various comma separated message types |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 100 | --show-types show the specific message type in the output |
Simon Glass | a33d982 | 2020-05-22 16:32:35 -0600 | [diff] [blame] | 101 | --max-line-length=n set the maximum line length, (default $max_line_length) |
| 102 | if exceeded, warn on patches |
| 103 | requires --strict for use with --file |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 104 | --min-conf-desc-length=n set the min description length, if shorter, warn |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 105 | --tab-size=n set the number of spaces for tab (default $tabsize) |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 106 | --root=PATH PATH to the kernel tree root |
| 107 | --no-summary suppress the per-file summary |
| 108 | --mailback only produce a report in case of warnings/errors |
| 109 | --summary-file include the filename in summary |
| 110 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 111 | 'values', 'possible', 'type', and 'attr' (default |
| 112 | is all off) |
| 113 | --test-only=WORD report only warnings/errors containing WORD |
| 114 | literally |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 115 | --fix EXPERIMENTAL - may create horrible results |
| 116 | If correctable single-line errors exist, create |
| 117 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" |
| 118 | with potential errors corrected to the preferred |
| 119 | checkpatch style |
| 120 | --fix-inplace EXPERIMENTAL - may create horrible results |
| 121 | Is the same as --fix, but overwrites the input |
| 122 | file. It's your fault if there's no backup or git |
| 123 | --ignore-perl-version override checking of perl version. expect |
| 124 | runtime errors. |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 125 | --codespell Use the codespell dictionary for spelling/typos |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 126 | (default:/usr/share/codespell/dictionary.txt) |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 127 | --codespellfile Use this codespell dictionary |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 128 | --typedefsfile Read additional types from this file |
| 129 | --color[=WHEN] Use colors 'always', 'never', or only when output |
| 130 | is a terminal ('auto'). Default is 'auto'. |
Simon Glass | c8f4d09 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 131 | --u-boot Run additional checks for U-Boot |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 132 | -h, --help, --version display this help and exit |
| 133 | |
| 134 | When FILE is - read standard input. |
| 135 | EOM |
| 136 | |
| 137 | exit($exitcode); |
| 138 | } |
| 139 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 140 | sub uniq { |
| 141 | my %seen; |
| 142 | return grep { !$seen{$_}++ } @_; |
| 143 | } |
| 144 | |
| 145 | sub list_types { |
| 146 | my ($exitcode) = @_; |
| 147 | |
| 148 | my $count = 0; |
| 149 | |
| 150 | local $/ = undef; |
| 151 | |
| 152 | open(my $script, '<', abs_path($P)) or |
| 153 | die "$P: Can't read '$P' $!\n"; |
| 154 | |
| 155 | my $text = <$script>; |
| 156 | close($script); |
| 157 | |
| 158 | my @types = (); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 159 | # Also catch when type or level is passed through a variable |
| 160 | for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 161 | push (@types, $_); |
| 162 | } |
| 163 | @types = sort(uniq(@types)); |
| 164 | print("#\tMessage type\n\n"); |
| 165 | foreach my $type (@types) { |
| 166 | print(++$count . "\t" . $type . "\n"); |
| 167 | } |
| 168 | |
| 169 | exit($exitcode); |
| 170 | } |
| 171 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 172 | my $conf = which_conf($configuration_file); |
| 173 | if (-f $conf) { |
| 174 | my @conf_args; |
| 175 | open(my $conffile, '<', "$conf") |
| 176 | or warn "$P: Can't find a readable $configuration_file file $!\n"; |
| 177 | |
| 178 | while (<$conffile>) { |
| 179 | my $line = $_; |
| 180 | |
| 181 | $line =~ s/\s*\n?$//g; |
| 182 | $line =~ s/^\s*//g; |
| 183 | $line =~ s/\s+/ /g; |
| 184 | |
| 185 | next if ($line =~ m/^\s*#/); |
| 186 | next if ($line =~ m/^\s*$/); |
| 187 | |
| 188 | my @words = split(" ", $line); |
| 189 | foreach my $word (@words) { |
| 190 | last if ($word =~ m/^#/); |
| 191 | push (@conf_args, $word); |
| 192 | } |
| 193 | } |
| 194 | close($conffile); |
| 195 | unshift(@ARGV, @conf_args) if @conf_args; |
| 196 | } |
| 197 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 198 | # Perl's Getopt::Long allows options to take optional arguments after a space. |
| 199 | # Prevent --color by itself from consuming other arguments |
| 200 | foreach (@ARGV) { |
| 201 | if ($_ eq "--color" || $_ eq "-color") { |
| 202 | $_ = "--color=$color"; |
| 203 | } |
| 204 | } |
| 205 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 206 | GetOptions( |
| 207 | 'q|quiet+' => \$quiet, |
| 208 | 'tree!' => \$tree, |
| 209 | 'signoff!' => \$chk_signoff, |
| 210 | 'patch!' => \$chk_patch, |
| 211 | 'emacs!' => \$emacs, |
| 212 | 'terse!' => \$terse, |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 213 | 'showfile!' => \$showfile, |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 214 | 'f|file!' => \$file, |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 215 | 'g|git!' => \$git, |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 216 | 'subjective!' => \$check, |
| 217 | 'strict!' => \$check, |
| 218 | 'ignore=s' => \@ignore, |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 219 | 'types=s' => \@use, |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 220 | 'show-types!' => \$show_types, |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 221 | 'list-types!' => \$list_types, |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 222 | 'max-line-length=i' => \$max_line_length, |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 223 | 'min-conf-desc-length=i' => \$min_conf_desc_length, |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 224 | 'tab-size=i' => \$tabsize, |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 225 | 'root=s' => \$root, |
| 226 | 'summary!' => \$summary, |
| 227 | 'mailback!' => \$mailback, |
| 228 | 'summary-file!' => \$summary_file, |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 229 | 'fix!' => \$fix, |
| 230 | 'fix-inplace!' => \$fix_inplace, |
| 231 | 'ignore-perl-version!' => \$ignore_perl_version, |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 232 | 'debug=s' => \%debug, |
| 233 | 'test-only=s' => \$tst_only, |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 234 | 'codespell!' => \$codespell, |
| 235 | 'codespellfile=s' => \$codespellfile, |
| 236 | 'typedefsfile=s' => \$typedefsfile, |
Simon Glass | c8f4d09 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 237 | 'u-boot' => \$u_boot, |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 238 | 'color=s' => \$color, |
| 239 | 'no-color' => \$color, #keep old behaviors of -nocolor |
| 240 | 'nocolor' => \$color, #keep old behaviors of -nocolor |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 241 | 'h|help' => \$help, |
| 242 | 'version' => \$help |
| 243 | ) or help(1); |
| 244 | |
| 245 | help(0) if ($help); |
| 246 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 247 | list_types(0) if ($list_types); |
| 248 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 249 | $fix = 1 if ($fix_inplace); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 250 | $check_orig = $check; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 251 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 252 | my $exit = 0; |
| 253 | |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 254 | my $perl_version_ok = 1; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 255 | if ($^V && $^V lt $minimum_perl_version) { |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 256 | $perl_version_ok = 0; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 257 | printf "$P: requires at least perl version %vd\n", $minimum_perl_version; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 258 | exit(1) if (!$ignore_perl_version); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 259 | } |
| 260 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 261 | #if no filenames are given, push '-' to read patch from stdin |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 262 | if ($#ARGV < 0) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 263 | push(@ARGV, '-'); |
| 264 | } |
| 265 | |
| 266 | if ($color =~ /^[01]$/) { |
| 267 | $color = !$color; |
| 268 | } elsif ($color =~ /^always$/i) { |
| 269 | $color = 1; |
| 270 | } elsif ($color =~ /^never$/i) { |
| 271 | $color = 0; |
| 272 | } elsif ($color =~ /^auto$/i) { |
| 273 | $color = (-t STDOUT); |
| 274 | } else { |
| 275 | die "Invalid color mode: $color\n"; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 278 | # skip TAB size 1 to avoid additional checks on $tabsize - 1 |
| 279 | die "Invalid TAB size: $tabsize\n" if ($tabsize < 2); |
| 280 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 281 | sub hash_save_array_words { |
| 282 | my ($hashRef, $arrayRef) = @_; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 283 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 284 | my @array = split(/,/, join(',', @$arrayRef)); |
| 285 | foreach my $word (@array) { |
| 286 | $word =~ s/\s*\n?$//g; |
| 287 | $word =~ s/^\s*//g; |
| 288 | $word =~ s/\s+/ /g; |
| 289 | $word =~ tr/[a-z]/[A-Z]/; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 290 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 291 | next if ($word =~ m/^\s*#/); |
| 292 | next if ($word =~ m/^\s*$/); |
| 293 | |
| 294 | $hashRef->{$word}++; |
| 295 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 298 | sub hash_show_words { |
| 299 | my ($hashRef, $prefix) = @_; |
| 300 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 301 | if (keys %$hashRef) { |
| 302 | print "\nNOTE: $prefix message types:"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 303 | foreach my $word (sort keys %$hashRef) { |
| 304 | print " $word"; |
| 305 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 306 | print "\n"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | hash_save_array_words(\%ignore_type, \@ignore); |
| 311 | hash_save_array_words(\%use_type, \@use); |
| 312 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 313 | my $dbg_values = 0; |
| 314 | my $dbg_possible = 0; |
| 315 | my $dbg_type = 0; |
| 316 | my $dbg_attr = 0; |
| 317 | for my $key (keys %debug) { |
| 318 | ## no critic |
| 319 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 320 | die "$@" if ($@); |
| 321 | } |
| 322 | |
| 323 | my $rpt_cleaners = 0; |
| 324 | |
| 325 | if ($terse) { |
| 326 | $emacs = 1; |
| 327 | $quiet++; |
| 328 | } |
| 329 | |
| 330 | if ($tree) { |
| 331 | if (defined $root) { |
| 332 | if (!top_of_kernel_tree($root)) { |
| 333 | die "$P: $root: --root does not point at a valid tree\n"; |
| 334 | } |
| 335 | } else { |
| 336 | if (top_of_kernel_tree('.')) { |
| 337 | $root = '.'; |
| 338 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 339 | top_of_kernel_tree($1)) { |
| 340 | $root = $1; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (!defined $root) { |
| 345 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 346 | exit(2); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | my $emitted_corrupt = 0; |
| 351 | |
| 352 | our $Ident = qr{ |
| 353 | [A-Za-z_][A-Za-z\d_]* |
| 354 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 355 | }x; |
| 356 | our $Storage = qr{extern|static|asmlinkage}; |
| 357 | our $Sparse = qr{ |
| 358 | __user| |
| 359 | __kernel| |
| 360 | __force| |
| 361 | __iomem| |
| 362 | __must_check| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 363 | __kprobes| |
| 364 | __ref| |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 365 | __refconst| |
| 366 | __refdata| |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 367 | __rcu| |
| 368 | __private |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 369 | }x; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 370 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; |
| 371 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; |
| 372 | our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; |
| 373 | our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; |
| 374 | our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 375 | |
| 376 | # Notes to $Attribute: |
| 377 | # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check |
| 378 | our $Attribute = qr{ |
| 379 | const| |
| 380 | __percpu| |
| 381 | __nocast| |
| 382 | __safe| |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 383 | __bitwise| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 384 | __packed__| |
| 385 | __packed2__| |
| 386 | __naked| |
| 387 | __maybe_unused| |
| 388 | __always_unused| |
| 389 | __noreturn| |
| 390 | __used| |
| 391 | __cold| |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 392 | __pure| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 393 | __noclone| |
| 394 | __deprecated| |
| 395 | __read_mostly| |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 396 | __ro_after_init| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 397 | __kprobes| |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 398 | $InitAttribute| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 399 | ____cacheline_aligned| |
| 400 | ____cacheline_aligned_in_smp| |
| 401 | ____cacheline_internodealigned_in_smp| |
| 402 | __weak |
| 403 | }x; |
| 404 | our $Modifier; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 405 | our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 406 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 407 | our $Lval = qr{$Ident(?:$Member)*}; |
| 408 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 409 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 410 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 411 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 412 | our $Int = qr{[0-9]+$Int_type?}; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 413 | our $Octal = qr{0[0-7]+$Int_type?}; |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 414 | our $String = qr{"[X\t]*"}; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 415 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 416 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 417 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
| 418 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 419 | our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 420 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 421 | our $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 422 | our $Arithmetic = qr{\+|-|\*|\/|%}; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 423 | our $Operators = qr{ |
| 424 | <=|>=|==|!=| |
| 425 | =>|->|<<|>>|<|>|!|~| |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 426 | &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 427 | }x; |
| 428 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 429 | our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; |
| 430 | |
| 431 | our $BasicType; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 432 | our $NonptrType; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 433 | our $NonptrTypeMisordered; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 434 | our $NonptrTypeWithAttr; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 435 | our $Type; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 436 | our $TypeMisordered; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 437 | our $Declare; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 438 | our $DeclareMisordered; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 439 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 440 | our $NON_ASCII_UTF8 = qr{ |
| 441 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 442 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 443 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 444 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 445 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 446 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 447 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 448 | }x; |
| 449 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 450 | our $UTF8 = qr{ |
| 451 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 452 | | $NON_ASCII_UTF8 |
| 453 | }x; |
| 454 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 455 | our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; |
| 456 | our $typeOtherOSTypedefs = qr{(?x: |
| 457 | u_(?:char|short|int|long) | # bsd |
| 458 | u(?:nchar|short|int|long) # sysv |
| 459 | )}; |
| 460 | our $typeKernelTypedefs = qr{(?x: |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 461 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
| 462 | atomic_t |
| 463 | )}; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 464 | our $typeTypedefs = qr{(?x: |
| 465 | $typeC99Typedefs\b| |
| 466 | $typeOtherOSTypedefs\b| |
| 467 | $typeKernelTypedefs\b |
| 468 | )}; |
| 469 | |
| 470 | our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 471 | |
| 472 | our $logFunctions = qr{(?x: |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 473 | printk(?:_ratelimited|_once|_deferred_once|_deferred|)| |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 474 | (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 475 | TP_printk| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 476 | WARN(?:_RATELIMIT|_ONCE|)| |
| 477 | panic| |
James Byrne | a685979 | 2019-11-21 14:32:46 +0000 | [diff] [blame] | 478 | debug| |
| 479 | printf| |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 480 | MODULE_[A-Z_]+| |
| 481 | seq_vprintf|seq_printf|seq_puts |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 482 | )}; |
| 483 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 484 | our $allocFunctions = qr{(?x: |
| 485 | (?:(?:devm_)? |
| 486 | (?:kv|k|v)[czm]alloc(?:_node|_array)? | |
| 487 | kstrdup(?:_const)? | |
| 488 | kmemdup(?:_nul)?) | |
| 489 | (?:\w+)?alloc_skb(?:_ip_align)? | |
| 490 | # dev_alloc_skb/netdev_alloc_skb, et al |
| 491 | dma_alloc_coherent |
| 492 | )}; |
| 493 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 494 | our $signature_tags = qr{(?xi: |
| 495 | Signed-off-by:| |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 496 | Co-developed-by:| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 497 | Acked-by:| |
| 498 | Tested-by:| |
| 499 | Reviewed-by:| |
| 500 | Reported-by:| |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 501 | Suggested-by:| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 502 | To:| |
| 503 | Cc: |
| 504 | )}; |
| 505 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 506 | our @typeListMisordered = ( |
| 507 | qr{char\s+(?:un)?signed}, |
| 508 | qr{int\s+(?:(?:un)?signed\s+)?short\s}, |
| 509 | qr{int\s+short(?:\s+(?:un)?signed)}, |
| 510 | qr{short\s+int(?:\s+(?:un)?signed)}, |
| 511 | qr{(?:un)?signed\s+int\s+short}, |
| 512 | qr{short\s+(?:un)?signed}, |
| 513 | qr{long\s+int\s+(?:un)?signed}, |
| 514 | qr{int\s+long\s+(?:un)?signed}, |
| 515 | qr{long\s+(?:un)?signed\s+int}, |
| 516 | qr{int\s+(?:un)?signed\s+long}, |
| 517 | qr{int\s+(?:un)?signed}, |
| 518 | qr{int\s+long\s+long\s+(?:un)?signed}, |
| 519 | qr{long\s+long\s+int\s+(?:un)?signed}, |
| 520 | qr{long\s+long\s+(?:un)?signed\s+int}, |
| 521 | qr{long\s+long\s+(?:un)?signed}, |
| 522 | qr{long\s+(?:un)?signed}, |
| 523 | ); |
| 524 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 525 | our @typeList = ( |
| 526 | qr{void}, |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 527 | qr{(?:(?:un)?signed\s+)?char}, |
| 528 | qr{(?:(?:un)?signed\s+)?short\s+int}, |
| 529 | qr{(?:(?:un)?signed\s+)?short}, |
| 530 | qr{(?:(?:un)?signed\s+)?int}, |
| 531 | qr{(?:(?:un)?signed\s+)?long\s+int}, |
| 532 | qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, |
| 533 | qr{(?:(?:un)?signed\s+)?long\s+long}, |
| 534 | qr{(?:(?:un)?signed\s+)?long}, |
| 535 | qr{(?:un)?signed}, |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 536 | qr{float}, |
| 537 | qr{double}, |
| 538 | qr{bool}, |
| 539 | qr{struct\s+$Ident}, |
| 540 | qr{union\s+$Ident}, |
| 541 | qr{enum\s+$Ident}, |
| 542 | qr{${Ident}_t}, |
| 543 | qr{${Ident}_handler}, |
| 544 | qr{${Ident}_handler_fn}, |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 545 | @typeListMisordered, |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 546 | ); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 547 | |
| 548 | our $C90_int_types = qr{(?x: |
| 549 | long\s+long\s+int\s+(?:un)?signed| |
| 550 | long\s+long\s+(?:un)?signed\s+int| |
| 551 | long\s+long\s+(?:un)?signed| |
| 552 | (?:(?:un)?signed\s+)?long\s+long\s+int| |
| 553 | (?:(?:un)?signed\s+)?long\s+long| |
| 554 | int\s+long\s+long\s+(?:un)?signed| |
| 555 | int\s+(?:(?:un)?signed\s+)?long\s+long| |
| 556 | |
| 557 | long\s+int\s+(?:un)?signed| |
| 558 | long\s+(?:un)?signed\s+int| |
| 559 | long\s+(?:un)?signed| |
| 560 | (?:(?:un)?signed\s+)?long\s+int| |
| 561 | (?:(?:un)?signed\s+)?long| |
| 562 | int\s+long\s+(?:un)?signed| |
| 563 | int\s+(?:(?:un)?signed\s+)?long| |
| 564 | |
| 565 | int\s+(?:un)?signed| |
| 566 | (?:(?:un)?signed\s+)?int |
| 567 | )}; |
| 568 | |
| 569 | our @typeListFile = (); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 570 | our @typeListWithAttr = ( |
| 571 | @typeList, |
| 572 | qr{struct\s+$InitAttribute\s+$Ident}, |
| 573 | qr{union\s+$InitAttribute\s+$Ident}, |
| 574 | ); |
| 575 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 576 | our @modifierList = ( |
| 577 | qr{fastcall}, |
| 578 | ); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 579 | our @modifierListFile = (); |
| 580 | |
| 581 | our @mode_permission_funcs = ( |
| 582 | ["module_param", 3], |
| 583 | ["module_param_(?:array|named|string)", 4], |
| 584 | ["module_param_array_named", 5], |
| 585 | ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], |
| 586 | ["proc_create(?:_data|)", 2], |
| 587 | ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], |
| 588 | ["IIO_DEV_ATTR_[A-Z_]+", 1], |
| 589 | ["SENSOR_(?:DEVICE_|)ATTR_2", 2], |
| 590 | ["SENSOR_TEMPLATE(?:_2|)", 3], |
| 591 | ["__ATTR", 2], |
| 592 | ); |
| 593 | |
| 594 | #Create a search pattern for all these functions to speed up a loop below |
| 595 | our $mode_perms_search = ""; |
| 596 | foreach my $entry (@mode_permission_funcs) { |
| 597 | $mode_perms_search .= '|' if ($mode_perms_search ne ""); |
| 598 | $mode_perms_search .= $entry->[0]; |
| 599 | } |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 600 | $mode_perms_search = "(?:${mode_perms_search})"; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 601 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 602 | our %deprecated_apis = ( |
| 603 | "synchronize_rcu_bh" => "synchronize_rcu", |
| 604 | "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", |
| 605 | "call_rcu_bh" => "call_rcu", |
| 606 | "rcu_barrier_bh" => "rcu_barrier", |
| 607 | "synchronize_sched" => "synchronize_rcu", |
| 608 | "synchronize_sched_expedited" => "synchronize_rcu_expedited", |
| 609 | "call_rcu_sched" => "call_rcu", |
| 610 | "rcu_barrier_sched" => "rcu_barrier", |
| 611 | "get_state_synchronize_sched" => "get_state_synchronize_rcu", |
| 612 | "cond_synchronize_sched" => "cond_synchronize_rcu", |
| 613 | ); |
| 614 | |
| 615 | #Create a search pattern for all these strings to speed up a loop below |
| 616 | our $deprecated_apis_search = ""; |
| 617 | foreach my $entry (keys %deprecated_apis) { |
| 618 | $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); |
| 619 | $deprecated_apis_search .= $entry; |
| 620 | } |
| 621 | $deprecated_apis_search = "(?:${deprecated_apis_search})"; |
| 622 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 623 | our $mode_perms_world_writable = qr{ |
| 624 | S_IWUGO | |
| 625 | S_IWOTH | |
| 626 | S_IRWXUGO | |
| 627 | S_IALLUGO | |
| 628 | 0[0-7][0-7][2367] |
| 629 | }x; |
| 630 | |
| 631 | our %mode_permission_string_types = ( |
| 632 | "S_IRWXU" => 0700, |
| 633 | "S_IRUSR" => 0400, |
| 634 | "S_IWUSR" => 0200, |
| 635 | "S_IXUSR" => 0100, |
| 636 | "S_IRWXG" => 0070, |
| 637 | "S_IRGRP" => 0040, |
| 638 | "S_IWGRP" => 0020, |
| 639 | "S_IXGRP" => 0010, |
| 640 | "S_IRWXO" => 0007, |
| 641 | "S_IROTH" => 0004, |
| 642 | "S_IWOTH" => 0002, |
| 643 | "S_IXOTH" => 0001, |
| 644 | "S_IRWXUGO" => 0777, |
| 645 | "S_IRUGO" => 0444, |
| 646 | "S_IWUGO" => 0222, |
| 647 | "S_IXUGO" => 0111, |
| 648 | ); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 649 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 650 | #Create a search pattern for all these strings to speed up a loop below |
| 651 | our $mode_perms_string_search = ""; |
| 652 | foreach my $entry (keys %mode_permission_string_types) { |
| 653 | $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); |
| 654 | $mode_perms_string_search .= $entry; |
| 655 | } |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 656 | our $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; |
| 657 | our $multi_mode_perms_string_search = qr{ |
| 658 | ${single_mode_perms_string_search} |
| 659 | (?:\s*\|\s*${single_mode_perms_string_search})* |
| 660 | }x; |
| 661 | |
| 662 | sub perms_to_octal { |
| 663 | my ($string) = @_; |
| 664 | |
| 665 | return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); |
| 666 | |
| 667 | my $val = ""; |
| 668 | my $oval = ""; |
| 669 | my $to = 0; |
| 670 | my $curpos = 0; |
| 671 | my $lastpos = 0; |
| 672 | while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { |
| 673 | $curpos = pos($string); |
| 674 | my $match = $2; |
| 675 | my $omatch = $1; |
| 676 | last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); |
| 677 | $lastpos = $curpos; |
| 678 | $to |= $mode_permission_string_types{$match}; |
| 679 | $val .= '\s*\|\s*' if ($val ne ""); |
| 680 | $val .= $match; |
| 681 | $oval .= $omatch; |
| 682 | } |
| 683 | $oval =~ s/^\s*\|\s*//; |
| 684 | $oval =~ s/\s*\|\s*$//; |
| 685 | return sprintf("%04o", $to); |
| 686 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 687 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 688 | our $allowed_asm_includes = qr{(?x: |
| 689 | irq| |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 690 | memory| |
| 691 | time| |
| 692 | reboot |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 693 | )}; |
| 694 | # memory.h: ARM has a custom one |
| 695 | |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 696 | # Load common spelling mistakes and build regular expression list. |
| 697 | my $misspellings; |
| 698 | my %spelling_fix; |
| 699 | |
| 700 | if (open(my $spelling, '<', $spelling_file)) { |
| 701 | while (<$spelling>) { |
| 702 | my $line = $_; |
| 703 | |
| 704 | $line =~ s/\s*\n?$//g; |
| 705 | $line =~ s/^\s*//g; |
| 706 | |
| 707 | next if ($line =~ m/^\s*#/); |
| 708 | next if ($line =~ m/^\s*$/); |
| 709 | |
| 710 | my ($suspect, $fix) = split(/\|\|/, $line); |
| 711 | |
| 712 | $spelling_fix{$suspect} = $fix; |
| 713 | } |
| 714 | close($spelling); |
| 715 | } else { |
| 716 | warn "No typos will be found - file '$spelling_file': $!\n"; |
| 717 | } |
| 718 | |
| 719 | if ($codespell) { |
| 720 | if (open(my $spelling, '<', $codespellfile)) { |
| 721 | while (<$spelling>) { |
| 722 | my $line = $_; |
| 723 | |
| 724 | $line =~ s/\s*\n?$//g; |
| 725 | $line =~ s/^\s*//g; |
| 726 | |
| 727 | next if ($line =~ m/^\s*#/); |
| 728 | next if ($line =~ m/^\s*$/); |
| 729 | next if ($line =~ m/, disabled/i); |
| 730 | |
| 731 | $line =~ s/,.*$//; |
| 732 | |
| 733 | my ($suspect, $fix) = split(/->/, $line); |
| 734 | |
| 735 | $spelling_fix{$suspect} = $fix; |
| 736 | } |
| 737 | close($spelling); |
| 738 | } else { |
| 739 | warn "No codespell typos will be found - file '$codespellfile': $!\n"; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; |
| 744 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 745 | sub read_words { |
| 746 | my ($wordsRef, $file) = @_; |
| 747 | |
| 748 | if (open(my $words, '<', $file)) { |
| 749 | while (<$words>) { |
| 750 | my $line = $_; |
| 751 | |
| 752 | $line =~ s/\s*\n?$//g; |
| 753 | $line =~ s/^\s*//g; |
| 754 | |
| 755 | next if ($line =~ m/^\s*#/); |
| 756 | next if ($line =~ m/^\s*$/); |
| 757 | if ($line =~ /\s/) { |
| 758 | print("$file: '$line' invalid - ignored\n"); |
| 759 | next; |
| 760 | } |
| 761 | |
| 762 | $$wordsRef .= '|' if ($$wordsRef ne ""); |
| 763 | $$wordsRef .= $line; |
| 764 | } |
| 765 | close($file); |
| 766 | return 1; |
| 767 | } |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | my $const_structs = ""; |
| 773 | read_words(\$const_structs, $conststructsfile) |
| 774 | or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; |
| 775 | |
| 776 | my $typeOtherTypedefs = ""; |
| 777 | if (length($typedefsfile)) { |
| 778 | read_words(\$typeOtherTypedefs, $typedefsfile) |
| 779 | or warn "No additional types will be considered - file '$typedefsfile': $!\n"; |
| 780 | } |
| 781 | $typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne ""); |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 782 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 783 | sub build_types { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 784 | my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; |
| 785 | my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; |
| 786 | my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 787 | my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 788 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 789 | $BasicType = qr{ |
| 790 | (?:$typeTypedefs\b)| |
| 791 | (?:${all}\b) |
| 792 | }x; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 793 | $NonptrType = qr{ |
| 794 | (?:$Modifier\s+|const\s+)* |
| 795 | (?: |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 796 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 797 | (?:$typeTypedefs\b)| |
| 798 | (?:${all}\b) |
| 799 | ) |
| 800 | (?:\s+$Modifier|\s+const)* |
| 801 | }x; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 802 | $NonptrTypeMisordered = qr{ |
| 803 | (?:$Modifier\s+|const\s+)* |
| 804 | (?: |
| 805 | (?:${Misordered}\b) |
| 806 | ) |
| 807 | (?:\s+$Modifier|\s+const)* |
| 808 | }x; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 809 | $NonptrTypeWithAttr = qr{ |
| 810 | (?:$Modifier\s+|const\s+)* |
| 811 | (?: |
| 812 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 813 | (?:$typeTypedefs\b)| |
| 814 | (?:${allWithAttr}\b) |
| 815 | ) |
| 816 | (?:\s+$Modifier|\s+const)* |
| 817 | }x; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 818 | $Type = qr{ |
| 819 | $NonptrType |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 820 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 821 | (?:\s+$Inline|\s+$Modifier)* |
| 822 | }x; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 823 | $TypeMisordered = qr{ |
| 824 | $NonptrTypeMisordered |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 825 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 826 | (?:\s+$Inline|\s+$Modifier)* |
| 827 | }x; |
| 828 | $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; |
| 829 | $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 830 | } |
| 831 | build_types(); |
| 832 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 833 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 834 | |
| 835 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 836 | # requires at least perl version v5.10.0 |
| 837 | # Any use must be runtime checked with $^V |
| 838 | |
| 839 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 840 | our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; |
| 841 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; |
| 842 | |
| 843 | our $declaration_macros = qr{(?x: |
| 844 | (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| |
| 845 | (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 846 | (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(| |
| 847 | (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 848 | )}; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 849 | |
| 850 | sub deparenthesize { |
| 851 | my ($string) = @_; |
| 852 | return "" if (!defined($string)); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 853 | |
| 854 | while ($string =~ /^\s*\(.*\)\s*$/) { |
| 855 | $string =~ s@^\s*\(\s*@@; |
| 856 | $string =~ s@\s*\)\s*$@@; |
| 857 | } |
| 858 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 859 | $string =~ s@\s+@ @g; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 860 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 861 | return $string; |
| 862 | } |
| 863 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 864 | sub seed_camelcase_file { |
| 865 | my ($file) = @_; |
| 866 | |
| 867 | return if (!(-f $file)); |
| 868 | |
| 869 | local $/; |
| 870 | |
| 871 | open(my $include_file, '<', "$file") |
| 872 | or warn "$P: Can't read '$file' $!\n"; |
| 873 | my $text = <$include_file>; |
| 874 | close($include_file); |
| 875 | |
| 876 | my @lines = split('\n', $text); |
| 877 | |
| 878 | foreach my $line (@lines) { |
| 879 | next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); |
| 880 | if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { |
| 881 | $camelcase{$1} = 1; |
| 882 | } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { |
| 883 | $camelcase{$1} = 1; |
| 884 | } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { |
| 885 | $camelcase{$1} = 1; |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 890 | our %maintained_status = (); |
| 891 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 892 | sub is_maintained_obsolete { |
| 893 | my ($filename) = @_; |
| 894 | |
| 895 | return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); |
| 896 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 897 | if (!exists($maintained_status{$filename})) { |
| 898 | $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; |
| 899 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 900 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 901 | return $maintained_status{$filename} =~ /obsolete/i; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 902 | } |
| 903 | |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 904 | sub is_SPDX_License_valid { |
| 905 | my ($license) = @_; |
| 906 | |
| 907 | return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git")); |
| 908 | |
| 909 | my $root_path = abs_path($root); |
| 910 | my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; |
| 911 | return 0 if ($status ne ""); |
| 912 | return 1; |
| 913 | } |
| 914 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 915 | my $camelcase_seeded = 0; |
| 916 | sub seed_camelcase_includes { |
| 917 | return if ($camelcase_seeded); |
| 918 | |
| 919 | my $files; |
| 920 | my $camelcase_cache = ""; |
| 921 | my @include_files = (); |
| 922 | |
| 923 | $camelcase_seeded = 1; |
| 924 | |
| 925 | if (-e ".git") { |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 926 | my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 927 | chomp $git_last_include_commit; |
| 928 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
| 929 | } else { |
| 930 | my $last_mod_date = 0; |
| 931 | $files = `find $root/include -name "*.h"`; |
| 932 | @include_files = split('\n', $files); |
| 933 | foreach my $file (@include_files) { |
| 934 | my $date = POSIX::strftime("%Y%m%d%H%M", |
| 935 | localtime((stat $file)[9])); |
| 936 | $last_mod_date = $date if ($last_mod_date < $date); |
| 937 | } |
| 938 | $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; |
| 939 | } |
| 940 | |
| 941 | if ($camelcase_cache ne "" && -f $camelcase_cache) { |
| 942 | open(my $camelcase_file, '<', "$camelcase_cache") |
| 943 | or warn "$P: Can't read '$camelcase_cache' $!\n"; |
| 944 | while (<$camelcase_file>) { |
| 945 | chomp; |
| 946 | $camelcase{$_} = 1; |
| 947 | } |
| 948 | close($camelcase_file); |
| 949 | |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | if (-e ".git") { |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 954 | $files = `${git_command} ls-files "include/*.h"`; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 955 | @include_files = split('\n', $files); |
| 956 | } |
| 957 | |
| 958 | foreach my $file (@include_files) { |
| 959 | seed_camelcase_file($file); |
| 960 | } |
| 961 | |
| 962 | if ($camelcase_cache ne "") { |
| 963 | unlink glob ".checkpatch-camelcase.*"; |
| 964 | open(my $camelcase_file, '>', "$camelcase_cache") |
| 965 | or warn "$P: Can't write '$camelcase_cache' $!\n"; |
| 966 | foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { |
| 967 | print $camelcase_file ("$_\n"); |
| 968 | } |
| 969 | close($camelcase_file); |
| 970 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | sub git_commit_info { |
| 974 | my ($commit, $id, $desc) = @_; |
| 975 | |
| 976 | return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); |
| 977 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 978 | my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 979 | $output =~ s/^\s*//gm; |
| 980 | my @lines = split("\n", $output); |
| 981 | |
| 982 | return ($id, $desc) if ($#lines < 0); |
| 983 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 984 | if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 985 | # Maybe one day convert this block of bash into something that returns |
| 986 | # all matching commit ids, but it's very slow... |
| 987 | # |
| 988 | # echo "checking commits $1..." |
| 989 | # git rev-list --remotes | grep -i "^$1" | |
| 990 | # while read line ; do |
| 991 | # git log --format='%H %s' -1 $line | |
| 992 | # echo "commit $(cut -c 1-12,41-)" |
| 993 | # done |
| 994 | } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { |
| 995 | $id = undef; |
| 996 | } else { |
| 997 | $id = substr($lines[0], 0, 12); |
| 998 | $desc = substr($lines[0], 41); |
| 999 | } |
| 1000 | |
| 1001 | return ($id, $desc); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1002 | } |
| 1003 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1004 | $chk_signoff = 0 if ($file); |
| 1005 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1006 | my @rawlines = (); |
| 1007 | my @lines = (); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1008 | my @fixed = (); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1009 | my @fixed_inserted = (); |
| 1010 | my @fixed_deleted = (); |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 1011 | my $fixlinenr = -1; |
| 1012 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1013 | # If input is git commits, extract all commits from the commit expressions. |
| 1014 | # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. |
| 1015 | die "$P: No git repository found\n" if ($git && !-e ".git"); |
| 1016 | |
| 1017 | if ($git) { |
| 1018 | my @commits = (); |
| 1019 | foreach my $commit_expr (@ARGV) { |
| 1020 | my $git_range; |
| 1021 | if ($commit_expr =~ m/^(.*)-(\d+)$/) { |
| 1022 | $git_range = "-$2 $1"; |
| 1023 | } elsif ($commit_expr =~ m/\.\./) { |
| 1024 | $git_range = "$commit_expr"; |
| 1025 | } else { |
| 1026 | $git_range = "-1 $commit_expr"; |
| 1027 | } |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1028 | my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1029 | foreach my $line (split(/\n/, $lines)) { |
| 1030 | $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; |
| 1031 | next if (!defined($1) || !defined($2)); |
| 1032 | my $sha1 = $1; |
| 1033 | my $subject = $2; |
| 1034 | unshift(@commits, $sha1); |
| 1035 | $git_commits{$sha1} = $subject; |
| 1036 | } |
| 1037 | } |
| 1038 | die "$P: no git commits after extraction!\n" if (@commits == 0); |
| 1039 | @ARGV = @commits; |
| 1040 | } |
| 1041 | |
| 1042 | my $vname; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1043 | $allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1044 | for my $filename (@ARGV) { |
| 1045 | my $FILE; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1046 | if ($git) { |
| 1047 | open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || |
| 1048 | die "$P: $filename: git format-patch failed - $!\n"; |
| 1049 | } elsif ($file) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1050 | open($FILE, '-|', "diff -u /dev/null $filename") || |
| 1051 | die "$P: $filename: diff failed - $!\n"; |
| 1052 | } elsif ($filename eq '-') { |
| 1053 | open($FILE, '<&STDIN'); |
| 1054 | } else { |
| 1055 | open($FILE, '<', "$filename") || |
| 1056 | die "$P: $filename: open failed - $!\n"; |
| 1057 | } |
| 1058 | if ($filename eq '-') { |
| 1059 | $vname = 'Your patch'; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1060 | } elsif ($git) { |
| 1061 | $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1062 | } else { |
| 1063 | $vname = $filename; |
| 1064 | } |
| 1065 | while (<$FILE>) { |
| 1066 | chomp; |
| 1067 | push(@rawlines, $_); |
| 1068 | } |
| 1069 | close($FILE); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1070 | |
| 1071 | if ($#ARGV > 0 && $quiet == 0) { |
| 1072 | print '-' x length($vname) . "\n"; |
| 1073 | print "$vname\n"; |
| 1074 | print '-' x length($vname) . "\n"; |
| 1075 | } |
| 1076 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1077 | if (!process($filename)) { |
| 1078 | $exit = 1; |
| 1079 | } |
| 1080 | @rawlines = (); |
| 1081 | @lines = (); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1082 | @fixed = (); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1083 | @fixed_inserted = (); |
| 1084 | @fixed_deleted = (); |
| 1085 | $fixlinenr = -1; |
| 1086 | @modifierListFile = (); |
| 1087 | @typeListFile = (); |
| 1088 | build_types(); |
| 1089 | } |
| 1090 | |
| 1091 | if (!$quiet) { |
| 1092 | hash_show_words(\%use_type, "Used"); |
| 1093 | hash_show_words(\%ignore_type, "Ignored"); |
| 1094 | |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 1095 | if (!$perl_version_ok) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1096 | print << "EOM" |
| 1097 | |
| 1098 | NOTE: perl $^V is not modern enough to detect all possible issues. |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 1099 | An upgrade to at least perl $minimum_perl_version is suggested. |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1100 | EOM |
| 1101 | } |
| 1102 | if ($exit) { |
| 1103 | print << "EOM" |
| 1104 | |
| 1105 | NOTE: If any of the errors are false positives, please report |
| 1106 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 1107 | EOM |
| 1108 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | exit($exit); |
| 1112 | |
| 1113 | sub top_of_kernel_tree { |
| 1114 | my ($root) = @_; |
| 1115 | |
| 1116 | my @tree_check = ( |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1117 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1118 | "README", "Documentation", "arch", "include", "drivers", |
| 1119 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 1120 | ); |
| 1121 | |
| 1122 | foreach my $check (@tree_check) { |
| 1123 | if (! -e $root . '/' . $check) { |
| 1124 | return 0; |
| 1125 | } |
| 1126 | } |
| 1127 | return 1; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 1128 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1129 | |
| 1130 | sub parse_email { |
| 1131 | my ($formatted_email) = @_; |
| 1132 | |
| 1133 | my $name = ""; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1134 | my $name_comment = ""; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1135 | my $address = ""; |
| 1136 | my $comment = ""; |
| 1137 | |
| 1138 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 1139 | $name = $1; |
| 1140 | $address = $2; |
| 1141 | $comment = $3 if defined $3; |
| 1142 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 1143 | $address = $1; |
| 1144 | $comment = $2 if defined $2; |
| 1145 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 1146 | $address = $1; |
| 1147 | $comment = $2 if defined $2; |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 1148 | $formatted_email =~ s/\Q$address\E.*$//; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1149 | $name = $formatted_email; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1150 | $name = trim($name); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1151 | $name =~ s/^\"|\"$//g; |
| 1152 | # If there's a name left after stripping spaces and |
| 1153 | # leading quotes, and the address doesn't have both |
| 1154 | # leading and trailing angle brackets, the address |
| 1155 | # is invalid. ie: |
| 1156 | # "joe smith joe@smith.com" bad |
| 1157 | # "joe smith <joe@smith.com" bad |
| 1158 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 1159 | $name = ""; |
| 1160 | $address = ""; |
| 1161 | $comment = ""; |
| 1162 | } |
| 1163 | } |
| 1164 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1165 | $name = trim($name); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1166 | $name =~ s/^\"|\"$//g; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1167 | $name =~ s/(\s*\([^\)]+\))\s*//; |
| 1168 | if (defined($1)) { |
| 1169 | $name_comment = trim($1); |
| 1170 | } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1171 | $address = trim($address); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1172 | $address =~ s/^\<|\>$//g; |
| 1173 | |
| 1174 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 1175 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 1176 | $name = "\"$name\""; |
| 1177 | } |
| 1178 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1179 | return ($name, $name_comment, $address, $comment); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | sub format_email { |
| 1183 | my ($name, $address) = @_; |
| 1184 | |
| 1185 | my $formatted_email; |
| 1186 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1187 | $name = trim($name); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1188 | $name =~ s/^\"|\"$//g; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1189 | $address = trim($address); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1190 | |
| 1191 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 1192 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 1193 | $name = "\"$name\""; |
| 1194 | } |
| 1195 | |
| 1196 | if ("$name" eq "") { |
| 1197 | $formatted_email = "$address"; |
| 1198 | } else { |
| 1199 | $formatted_email = "$name <$address>"; |
| 1200 | } |
| 1201 | |
| 1202 | return $formatted_email; |
| 1203 | } |
| 1204 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1205 | sub reformat_email { |
| 1206 | my ($email) = @_; |
| 1207 | |
| 1208 | my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); |
| 1209 | return format_email($email_name, $email_address); |
| 1210 | } |
| 1211 | |
| 1212 | sub same_email_addresses { |
| 1213 | my ($email1, $email2) = @_; |
| 1214 | |
| 1215 | my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); |
| 1216 | my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); |
| 1217 | |
| 1218 | return $email1_name eq $email2_name && |
| 1219 | $email1_address eq $email2_address; |
| 1220 | } |
| 1221 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1222 | sub which { |
| 1223 | my ($bin) = @_; |
| 1224 | |
| 1225 | foreach my $path (split(/:/, $ENV{PATH})) { |
| 1226 | if (-e "$path/$bin") { |
| 1227 | return "$path/$bin"; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | return ""; |
| 1232 | } |
| 1233 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1234 | sub which_conf { |
| 1235 | my ($conf) = @_; |
| 1236 | |
| 1237 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 1238 | if (-e "$path/$conf") { |
| 1239 | return "$path/$conf"; |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | return ""; |
| 1244 | } |
| 1245 | |
| 1246 | sub expand_tabs { |
| 1247 | my ($str) = @_; |
| 1248 | |
| 1249 | my $res = ''; |
| 1250 | my $n = 0; |
| 1251 | for my $c (split(//, $str)) { |
| 1252 | if ($c eq "\t") { |
| 1253 | $res .= ' '; |
| 1254 | $n++; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 1255 | for (; ($n % $tabsize) != 0; $n++) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1256 | $res .= ' '; |
| 1257 | } |
| 1258 | next; |
| 1259 | } |
| 1260 | $res .= $c; |
| 1261 | $n++; |
| 1262 | } |
| 1263 | |
| 1264 | return $res; |
| 1265 | } |
| 1266 | sub copy_spacing { |
| 1267 | (my $res = shift) =~ tr/\t/ /c; |
| 1268 | return $res; |
| 1269 | } |
| 1270 | |
| 1271 | sub line_stats { |
| 1272 | my ($line) = @_; |
| 1273 | |
| 1274 | # Drop the diff line leader and expand tabs |
| 1275 | $line =~ s/^.//; |
| 1276 | $line = expand_tabs($line); |
| 1277 | |
| 1278 | # Pick the indent from the front of the line. |
| 1279 | my ($white) = ($line =~ /^(\s*)/); |
| 1280 | |
| 1281 | return (length($line), length($white)); |
| 1282 | } |
| 1283 | |
| 1284 | my $sanitise_quote = ''; |
| 1285 | |
| 1286 | sub sanitise_line_reset { |
| 1287 | my ($in_comment) = @_; |
| 1288 | |
| 1289 | if ($in_comment) { |
| 1290 | $sanitise_quote = '*/'; |
| 1291 | } else { |
| 1292 | $sanitise_quote = ''; |
| 1293 | } |
| 1294 | } |
| 1295 | sub sanitise_line { |
| 1296 | my ($line) = @_; |
| 1297 | |
| 1298 | my $res = ''; |
| 1299 | my $l = ''; |
| 1300 | |
| 1301 | my $qlen = 0; |
| 1302 | my $off = 0; |
| 1303 | my $c; |
| 1304 | |
| 1305 | # Always copy over the diff marker. |
| 1306 | $res = substr($line, 0, 1); |
| 1307 | |
| 1308 | for ($off = 1; $off < length($line); $off++) { |
| 1309 | $c = substr($line, $off, 1); |
| 1310 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 1311 | # Comments we are whacking completely including the begin |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1312 | # and end, all to $;. |
| 1313 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 1314 | $sanitise_quote = '*/'; |
| 1315 | |
| 1316 | substr($res, $off, 2, "$;$;"); |
| 1317 | $off++; |
| 1318 | next; |
| 1319 | } |
| 1320 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
| 1321 | $sanitise_quote = ''; |
| 1322 | substr($res, $off, 2, "$;$;"); |
| 1323 | $off++; |
| 1324 | next; |
| 1325 | } |
| 1326 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 1327 | $sanitise_quote = '//'; |
| 1328 | |
| 1329 | substr($res, $off, 2, $sanitise_quote); |
| 1330 | $off++; |
| 1331 | next; |
| 1332 | } |
| 1333 | |
| 1334 | # A \ in a string means ignore the next character. |
| 1335 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 1336 | $c eq "\\") { |
| 1337 | substr($res, $off, 2, 'XX'); |
| 1338 | $off++; |
| 1339 | next; |
| 1340 | } |
| 1341 | # Regular quotes. |
| 1342 | if ($c eq "'" || $c eq '"') { |
| 1343 | if ($sanitise_quote eq '') { |
| 1344 | $sanitise_quote = $c; |
| 1345 | |
| 1346 | substr($res, $off, 1, $c); |
| 1347 | next; |
| 1348 | } elsif ($sanitise_quote eq $c) { |
| 1349 | $sanitise_quote = ''; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | #print "c<$c> SQ<$sanitise_quote>\n"; |
| 1354 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 1355 | substr($res, $off, 1, $;); |
| 1356 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 1357 | substr($res, $off, 1, $;); |
| 1358 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 1359 | substr($res, $off, 1, 'X'); |
| 1360 | } else { |
| 1361 | substr($res, $off, 1, $c); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | if ($sanitise_quote eq '//') { |
| 1366 | $sanitise_quote = ''; |
| 1367 | } |
| 1368 | |
| 1369 | # The pathname on a #include may be surrounded by '<' and '>'. |
| 1370 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
| 1371 | my $clean = 'X' x length($1); |
| 1372 | $res =~ s@\<.*\>@<$clean>@; |
| 1373 | |
| 1374 | # The whole of a #error is a string. |
| 1375 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
| 1376 | my $clean = 'X' x length($1); |
| 1377 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
| 1378 | } |
| 1379 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1380 | if ($allow_c99_comments && $res =~ m@(//.*$)@) { |
| 1381 | my $match = $1; |
| 1382 | $res =~ s/\Q$match\E/"$;" x length($match)/e; |
| 1383 | } |
| 1384 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1385 | return $res; |
| 1386 | } |
| 1387 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1388 | sub get_quoted_string { |
| 1389 | my ($line, $rawline) = @_; |
| 1390 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 1391 | return "" if (!defined($line) || !defined($rawline)); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 1392 | return "" if ($line !~ m/($String)/g); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 1393 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 1394 | } |
| 1395 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1396 | sub ctx_statement_block { |
| 1397 | my ($linenr, $remain, $off) = @_; |
| 1398 | my $line = $linenr - 1; |
| 1399 | my $blk = ''; |
| 1400 | my $soff = $off; |
| 1401 | my $coff = $off - 1; |
| 1402 | my $coff_set = 0; |
| 1403 | |
| 1404 | my $loff = 0; |
| 1405 | |
| 1406 | my $type = ''; |
| 1407 | my $level = 0; |
| 1408 | my @stack = (); |
| 1409 | my $p; |
| 1410 | my $c; |
| 1411 | my $len = 0; |
| 1412 | |
| 1413 | my $remainder; |
| 1414 | while (1) { |
| 1415 | @stack = (['', 0]) if ($#stack == -1); |
| 1416 | |
| 1417 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
| 1418 | # If we are about to drop off the end, pull in more |
| 1419 | # context. |
| 1420 | if ($off >= $len) { |
| 1421 | for (; $remain > 0; $line++) { |
| 1422 | last if (!defined $lines[$line]); |
| 1423 | next if ($lines[$line] =~ /^-/); |
| 1424 | $remain--; |
| 1425 | $loff = $len; |
| 1426 | $blk .= $lines[$line] . "\n"; |
| 1427 | $len = length($blk); |
| 1428 | $line++; |
| 1429 | last; |
| 1430 | } |
| 1431 | # Bail if there is no further context. |
| 1432 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
| 1433 | if ($off >= $len) { |
| 1434 | last; |
| 1435 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 1436 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 1437 | $level++; |
| 1438 | $type = '#'; |
| 1439 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1440 | } |
| 1441 | $p = $c; |
| 1442 | $c = substr($blk, $off, 1); |
| 1443 | $remainder = substr($blk, $off); |
| 1444 | |
| 1445 | #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; |
| 1446 | |
| 1447 | # Handle nested #if/#else. |
| 1448 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 1449 | push(@stack, [ $type, $level ]); |
| 1450 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 1451 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 1452 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 1453 | ($type, $level) = @{pop(@stack)}; |
| 1454 | } |
| 1455 | |
| 1456 | # Statement ends at the ';' or a close '}' at the |
| 1457 | # outermost level. |
| 1458 | if ($level == 0 && $c eq ';') { |
| 1459 | last; |
| 1460 | } |
| 1461 | |
| 1462 | # An else is really a conditional as long as its not else if |
| 1463 | if ($level == 0 && $coff_set == 0 && |
| 1464 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 1465 | $remainder =~ /^(else)(?:\s|{)/ && |
| 1466 | $remainder !~ /^else\s+if\b/) { |
| 1467 | $coff = $off + length($1) - 1; |
| 1468 | $coff_set = 1; |
| 1469 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 1470 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
| 1471 | } |
| 1472 | |
| 1473 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 1474 | $level++; |
| 1475 | $type = '('; |
| 1476 | } |
| 1477 | if ($type eq '(' && $c eq ')') { |
| 1478 | $level--; |
| 1479 | $type = ($level != 0)? '(' : ''; |
| 1480 | |
| 1481 | if ($level == 0 && $coff < $soff) { |
| 1482 | $coff = $off; |
| 1483 | $coff_set = 1; |
| 1484 | #warn "CSB: mark coff<$coff>\n"; |
| 1485 | } |
| 1486 | } |
| 1487 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 1488 | $level++; |
| 1489 | $type = '{'; |
| 1490 | } |
| 1491 | if ($type eq '{' && $c eq '}') { |
| 1492 | $level--; |
| 1493 | $type = ($level != 0)? '{' : ''; |
| 1494 | |
| 1495 | if ($level == 0) { |
| 1496 | if (substr($blk, $off + 1, 1) eq ';') { |
| 1497 | $off++; |
| 1498 | } |
| 1499 | last; |
| 1500 | } |
| 1501 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 1502 | # Preprocessor commands end at the newline unless escaped. |
| 1503 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 1504 | $level--; |
| 1505 | $type = ''; |
| 1506 | $off++; |
| 1507 | last; |
| 1508 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1509 | $off++; |
| 1510 | } |
| 1511 | # We are truly at the end, so shuffle to the next line. |
| 1512 | if ($off == $len) { |
| 1513 | $loff = $len + 1; |
| 1514 | $line++; |
| 1515 | $remain--; |
| 1516 | } |
| 1517 | |
| 1518 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 1519 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 1520 | |
| 1521 | #warn "STATEMENT<$statement>\n"; |
| 1522 | #warn "CONDITION<$condition>\n"; |
| 1523 | |
| 1524 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
| 1525 | |
| 1526 | return ($statement, $condition, |
| 1527 | $line, $remain + 1, $off - $loff + 1, $level); |
| 1528 | } |
| 1529 | |
| 1530 | sub statement_lines { |
| 1531 | my ($stmt) = @_; |
| 1532 | |
| 1533 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 1534 | $stmt =~ s/(^|\n)./$1/g; |
| 1535 | $stmt =~ s/^\s*//; |
| 1536 | $stmt =~ s/\s*$//; |
| 1537 | |
| 1538 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1539 | |
| 1540 | return $#stmt_lines + 2; |
| 1541 | } |
| 1542 | |
| 1543 | sub statement_rawlines { |
| 1544 | my ($stmt) = @_; |
| 1545 | |
| 1546 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1547 | |
| 1548 | return $#stmt_lines + 2; |
| 1549 | } |
| 1550 | |
| 1551 | sub statement_block_size { |
| 1552 | my ($stmt) = @_; |
| 1553 | |
| 1554 | $stmt =~ s/(^|\n)./$1/g; |
| 1555 | $stmt =~ s/^\s*{//; |
| 1556 | $stmt =~ s/}\s*$//; |
| 1557 | $stmt =~ s/^\s*//; |
| 1558 | $stmt =~ s/\s*$//; |
| 1559 | |
| 1560 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1561 | my @stmt_statements = ($stmt =~ /;/g); |
| 1562 | |
| 1563 | my $stmt_lines = $#stmt_lines + 2; |
| 1564 | my $stmt_statements = $#stmt_statements + 1; |
| 1565 | |
| 1566 | if ($stmt_lines > $stmt_statements) { |
| 1567 | return $stmt_lines; |
| 1568 | } else { |
| 1569 | return $stmt_statements; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | sub ctx_statement_full { |
| 1574 | my ($linenr, $remain, $off) = @_; |
| 1575 | my ($statement, $condition, $level); |
| 1576 | |
| 1577 | my (@chunks); |
| 1578 | |
| 1579 | # Grab the first conditional/block pair. |
| 1580 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1581 | ctx_statement_block($linenr, $remain, $off); |
| 1582 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
| 1583 | push(@chunks, [ $condition, $statement ]); |
| 1584 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 1585 | return ($level, $linenr, @chunks); |
| 1586 | } |
| 1587 | |
| 1588 | # Pull in the following conditional/block pairs and see if they |
| 1589 | # could continue the statement. |
| 1590 | for (;;) { |
| 1591 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1592 | ctx_statement_block($linenr, $remain, $off); |
| 1593 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
| 1594 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
| 1595 | #print "C: push\n"; |
| 1596 | push(@chunks, [ $condition, $statement ]); |
| 1597 | } |
| 1598 | |
| 1599 | return ($level, $linenr, @chunks); |
| 1600 | } |
| 1601 | |
| 1602 | sub ctx_block_get { |
| 1603 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
| 1604 | my $line; |
| 1605 | my $start = $linenr - 1; |
| 1606 | my $blk = ''; |
| 1607 | my @o; |
| 1608 | my @c; |
| 1609 | my @res = (); |
| 1610 | |
| 1611 | my $level = 0; |
| 1612 | my @stack = ($level); |
| 1613 | for ($line = $start; $remain > 0; $line++) { |
| 1614 | next if ($rawlines[$line] =~ /^-/); |
| 1615 | $remain--; |
| 1616 | |
| 1617 | $blk .= $rawlines[$line]; |
| 1618 | |
| 1619 | # Handle nested #if/#else. |
| 1620 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
| 1621 | push(@stack, $level); |
| 1622 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
| 1623 | $level = $stack[$#stack - 1]; |
| 1624 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
| 1625 | $level = pop(@stack); |
| 1626 | } |
| 1627 | |
| 1628 | foreach my $c (split(//, $lines[$line])) { |
| 1629 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 1630 | if ($off > 0) { |
| 1631 | $off--; |
| 1632 | next; |
| 1633 | } |
| 1634 | |
| 1635 | if ($c eq $close && $level > 0) { |
| 1636 | $level--; |
| 1637 | last if ($level == 0); |
| 1638 | } elsif ($c eq $open) { |
| 1639 | $level++; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | if (!$outer || $level <= 1) { |
| 1644 | push(@res, $rawlines[$line]); |
| 1645 | } |
| 1646 | |
| 1647 | last if ($level == 0); |
| 1648 | } |
| 1649 | |
| 1650 | return ($level, @res); |
| 1651 | } |
| 1652 | sub ctx_block_outer { |
| 1653 | my ($linenr, $remain) = @_; |
| 1654 | |
| 1655 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 1656 | return @r; |
| 1657 | } |
| 1658 | sub ctx_block { |
| 1659 | my ($linenr, $remain) = @_; |
| 1660 | |
| 1661 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1662 | return @r; |
| 1663 | } |
| 1664 | sub ctx_statement { |
| 1665 | my ($linenr, $remain, $off) = @_; |
| 1666 | |
| 1667 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1668 | return @r; |
| 1669 | } |
| 1670 | sub ctx_block_level { |
| 1671 | my ($linenr, $remain) = @_; |
| 1672 | |
| 1673 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1674 | } |
| 1675 | sub ctx_statement_level { |
| 1676 | my ($linenr, $remain, $off) = @_; |
| 1677 | |
| 1678 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1679 | } |
| 1680 | |
| 1681 | sub ctx_locate_comment { |
| 1682 | my ($first_line, $end_line) = @_; |
| 1683 | |
| 1684 | # Catch a comment on the end of the line itself. |
| 1685 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
| 1686 | return $current_comment if (defined $current_comment); |
| 1687 | |
| 1688 | # Look through the context and try and figure out if there is a |
| 1689 | # comment. |
| 1690 | my $in_comment = 0; |
| 1691 | $current_comment = ''; |
| 1692 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
| 1693 | my $line = $rawlines[$linenr - 1]; |
| 1694 | #warn " $line\n"; |
| 1695 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 1696 | $in_comment = 1; |
| 1697 | } |
| 1698 | if ($line =~ m@/\*@) { |
| 1699 | $in_comment = 1; |
| 1700 | } |
| 1701 | if (!$in_comment && $current_comment ne '') { |
| 1702 | $current_comment = ''; |
| 1703 | } |
| 1704 | $current_comment .= $line . "\n" if ($in_comment); |
| 1705 | if ($line =~ m@\*/@) { |
| 1706 | $in_comment = 0; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | chomp($current_comment); |
| 1711 | return($current_comment); |
| 1712 | } |
| 1713 | sub ctx_has_comment { |
| 1714 | my ($first_line, $end_line) = @_; |
| 1715 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 1716 | |
| 1717 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
| 1718 | ##print "CMMT: $cmt\n"; |
| 1719 | |
| 1720 | return ($cmt ne ''); |
| 1721 | } |
| 1722 | |
| 1723 | sub raw_line { |
| 1724 | my ($linenr, $cnt) = @_; |
| 1725 | |
| 1726 | my $offset = $linenr - 1; |
| 1727 | $cnt++; |
| 1728 | |
| 1729 | my $line; |
| 1730 | while ($cnt) { |
| 1731 | $line = $rawlines[$offset++]; |
| 1732 | next if (defined($line) && $line =~ /^-/); |
| 1733 | $cnt--; |
| 1734 | } |
| 1735 | |
| 1736 | return $line; |
| 1737 | } |
| 1738 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 1739 | sub get_stat_real { |
| 1740 | my ($linenr, $lc) = @_; |
| 1741 | |
| 1742 | my $stat_real = raw_line($linenr, 0); |
| 1743 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 1744 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 1745 | } |
| 1746 | |
| 1747 | return $stat_real; |
| 1748 | } |
| 1749 | |
| 1750 | sub get_stat_here { |
| 1751 | my ($linenr, $cnt, $here) = @_; |
| 1752 | |
| 1753 | my $herectx = $here . "\n"; |
| 1754 | for (my $n = 0; $n < $cnt; $n++) { |
| 1755 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 1756 | } |
| 1757 | |
| 1758 | return $herectx; |
| 1759 | } |
| 1760 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1761 | sub cat_vet { |
| 1762 | my ($vet) = @_; |
| 1763 | my ($res, $coded); |
| 1764 | |
| 1765 | $res = ''; |
| 1766 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 1767 | $res .= $1; |
| 1768 | if ($2 ne '') { |
| 1769 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 1770 | $res .= $coded; |
| 1771 | } |
| 1772 | } |
| 1773 | $res =~ s/$/\$/; |
| 1774 | |
| 1775 | return $res; |
| 1776 | } |
| 1777 | |
| 1778 | my $av_preprocessor = 0; |
| 1779 | my $av_pending; |
| 1780 | my @av_paren_type; |
| 1781 | my $av_pend_colon; |
| 1782 | |
| 1783 | sub annotate_reset { |
| 1784 | $av_preprocessor = 0; |
| 1785 | $av_pending = '_'; |
| 1786 | @av_paren_type = ('E'); |
| 1787 | $av_pend_colon = 'O'; |
| 1788 | } |
| 1789 | |
| 1790 | sub annotate_values { |
| 1791 | my ($stream, $type) = @_; |
| 1792 | |
| 1793 | my $res; |
| 1794 | my $var = '_' x length($stream); |
| 1795 | my $cur = $stream; |
| 1796 | |
| 1797 | print "$stream\n" if ($dbg_values > 1); |
| 1798 | |
| 1799 | while (length($cur)) { |
| 1800 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
| 1801 | print " <" . join('', @av_paren_type) . |
| 1802 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
| 1803 | if ($cur =~ /^(\s+)/o) { |
| 1804 | print "WS($1)\n" if ($dbg_values > 1); |
| 1805 | if ($1 =~ /\n/ && $av_preprocessor) { |
| 1806 | $type = pop(@av_paren_type); |
| 1807 | $av_preprocessor = 0; |
| 1808 | } |
| 1809 | |
| 1810 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
| 1811 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1812 | push(@av_paren_type, $type); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 1813 | $type = 'c'; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 1814 | |
| 1815 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
| 1816 | print "DECLARE($1)\n" if ($dbg_values > 1); |
| 1817 | $type = 'T'; |
| 1818 | |
| 1819 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 1820 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 1821 | $type = 'T'; |
| 1822 | |
| 1823 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
| 1824 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
| 1825 | $av_preprocessor = 1; |
| 1826 | push(@av_paren_type, $type); |
| 1827 | if ($2 ne '') { |
| 1828 | $av_pending = 'N'; |
| 1829 | } |
| 1830 | $type = 'E'; |
| 1831 | |
| 1832 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
| 1833 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 1834 | $av_preprocessor = 1; |
| 1835 | push(@av_paren_type, $type); |
| 1836 | |
| 1837 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
| 1838 | print "PRE_START($1)\n" if ($dbg_values > 1); |
| 1839 | $av_preprocessor = 1; |
| 1840 | |
| 1841 | push(@av_paren_type, $type); |
| 1842 | push(@av_paren_type, $type); |
| 1843 | $type = 'E'; |
| 1844 | |
| 1845 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
| 1846 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 1847 | $av_preprocessor = 1; |
| 1848 | |
| 1849 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 1850 | |
| 1851 | $type = 'E'; |
| 1852 | |
| 1853 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
| 1854 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 1855 | |
| 1856 | $av_preprocessor = 1; |
| 1857 | |
| 1858 | # Assume all arms of the conditional end as this |
| 1859 | # one does, and continue as if the #endif was not here. |
| 1860 | pop(@av_paren_type); |
| 1861 | push(@av_paren_type, $type); |
| 1862 | $type = 'E'; |
| 1863 | |
| 1864 | } elsif ($cur =~ /^(\\\n)/o) { |
| 1865 | print "PRECONT($1)\n" if ($dbg_values > 1); |
| 1866 | |
| 1867 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 1868 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 1869 | $av_pending = $type; |
| 1870 | $type = 'N'; |
| 1871 | |
| 1872 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
| 1873 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
| 1874 | if (defined $2) { |
| 1875 | $av_pending = 'V'; |
| 1876 | } |
| 1877 | $type = 'N'; |
| 1878 | |
| 1879 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
| 1880 | print "COND($1)\n" if ($dbg_values > 1); |
| 1881 | $av_pending = 'E'; |
| 1882 | $type = 'N'; |
| 1883 | |
| 1884 | } elsif ($cur =~/^(case)/o) { |
| 1885 | print "CASE($1)\n" if ($dbg_values > 1); |
| 1886 | $av_pend_colon = 'C'; |
| 1887 | $type = 'N'; |
| 1888 | |
| 1889 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
| 1890 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
| 1891 | $type = 'N'; |
| 1892 | |
| 1893 | } elsif ($cur =~ /^(\()/o) { |
| 1894 | print "PAREN('$1')\n" if ($dbg_values > 1); |
| 1895 | push(@av_paren_type, $av_pending); |
| 1896 | $av_pending = '_'; |
| 1897 | $type = 'N'; |
| 1898 | |
| 1899 | } elsif ($cur =~ /^(\))/o) { |
| 1900 | my $new_type = pop(@av_paren_type); |
| 1901 | if ($new_type ne '_') { |
| 1902 | $type = $new_type; |
| 1903 | print "PAREN('$1') -> $type\n" |
| 1904 | if ($dbg_values > 1); |
| 1905 | } else { |
| 1906 | print "PAREN('$1')\n" if ($dbg_values > 1); |
| 1907 | } |
| 1908 | |
| 1909 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
| 1910 | print "FUNC($1)\n" if ($dbg_values > 1); |
| 1911 | $type = 'V'; |
| 1912 | $av_pending = 'V'; |
| 1913 | |
| 1914 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 1915 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
| 1916 | $av_pend_colon = 'B'; |
| 1917 | } elsif ($type eq 'E') { |
| 1918 | $av_pend_colon = 'L'; |
| 1919 | } |
| 1920 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 1921 | $type = 'V'; |
| 1922 | |
| 1923 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
| 1924 | print "IDENT($1)\n" if ($dbg_values > 1); |
| 1925 | $type = 'V'; |
| 1926 | |
| 1927 | } elsif ($cur =~ /^($Assignment)/o) { |
| 1928 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
| 1929 | $type = 'N'; |
| 1930 | |
| 1931 | } elsif ($cur =~/^(;|{|})/) { |
| 1932 | print "END($1)\n" if ($dbg_values > 1); |
| 1933 | $type = 'E'; |
| 1934 | $av_pend_colon = 'O'; |
| 1935 | |
| 1936 | } elsif ($cur =~/^(,)/) { |
| 1937 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 1938 | $type = 'C'; |
| 1939 | |
| 1940 | } elsif ($cur =~ /^(\?)/o) { |
| 1941 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 1942 | $type = 'N'; |
| 1943 | |
| 1944 | } elsif ($cur =~ /^(:)/o) { |
| 1945 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 1946 | |
| 1947 | substr($var, length($res), 1, $av_pend_colon); |
| 1948 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 1949 | $type = 'E'; |
| 1950 | } else { |
| 1951 | $type = 'N'; |
| 1952 | } |
| 1953 | $av_pend_colon = 'O'; |
| 1954 | |
| 1955 | } elsif ($cur =~ /^(\[)/o) { |
| 1956 | print "CLOSE($1)\n" if ($dbg_values > 1); |
| 1957 | $type = 'N'; |
| 1958 | |
| 1959 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
| 1960 | my $variant; |
| 1961 | |
| 1962 | print "OPV($1)\n" if ($dbg_values > 1); |
| 1963 | if ($type eq 'V') { |
| 1964 | $variant = 'B'; |
| 1965 | } else { |
| 1966 | $variant = 'U'; |
| 1967 | } |
| 1968 | |
| 1969 | substr($var, length($res), 1, $variant); |
| 1970 | $type = 'N'; |
| 1971 | |
| 1972 | } elsif ($cur =~ /^($Operators)/o) { |
| 1973 | print "OP($1)\n" if ($dbg_values > 1); |
| 1974 | if ($1 ne '++' && $1 ne '--') { |
| 1975 | $type = 'N'; |
| 1976 | } |
| 1977 | |
| 1978 | } elsif ($cur =~ /(^.)/o) { |
| 1979 | print "C($1)\n" if ($dbg_values > 1); |
| 1980 | } |
| 1981 | if (defined $1) { |
| 1982 | $cur = substr($cur, length($1)); |
| 1983 | $res .= $type x length($1); |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | return ($res, $var); |
| 1988 | } |
| 1989 | |
| 1990 | sub possible { |
| 1991 | my ($possible, $line) = @_; |
| 1992 | my $notPermitted = qr{(?: |
| 1993 | ^(?: |
| 1994 | $Modifier| |
| 1995 | $Storage| |
| 1996 | $Type| |
| 1997 | DEFINE_\S+ |
| 1998 | )$| |
| 1999 | ^(?: |
| 2000 | goto| |
| 2001 | return| |
| 2002 | case| |
| 2003 | else| |
| 2004 | asm|__asm__| |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2005 | do| |
| 2006 | \#| |
| 2007 | \#\#| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2008 | )(?:\s|$)| |
| 2009 | ^(?:typedef|struct|enum)\b |
| 2010 | )}x; |
| 2011 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 2012 | if ($possible !~ $notPermitted) { |
| 2013 | # Check for modifiers. |
| 2014 | $possible =~ s/\s*$Storage\s*//g; |
| 2015 | $possible =~ s/\s*$Sparse\s*//g; |
| 2016 | if ($possible =~ /^\s*$/) { |
| 2017 | |
| 2018 | } elsif ($possible =~ /\s/) { |
| 2019 | $possible =~ s/\s*$Type\s*//g; |
| 2020 | for my $modifier (split(' ', $possible)) { |
| 2021 | if ($modifier !~ $notPermitted) { |
| 2022 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2023 | push(@modifierListFile, $modifier); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | } else { |
| 2028 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2029 | push(@typeListFile, $possible); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2030 | } |
| 2031 | build_types(); |
| 2032 | } else { |
| 2033 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | my $prefix = ''; |
| 2038 | |
| 2039 | sub show_type { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2040 | my ($type) = @_; |
| 2041 | |
| 2042 | $type =~ tr/[a-z]/[A-Z]/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2043 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2044 | return defined $use_type{$type} if (scalar keys %use_type > 0); |
| 2045 | |
| 2046 | return !defined $ignore_type{$type}; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | sub report { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2050 | my ($level, $type, $msg) = @_; |
| 2051 | |
| 2052 | if (!show_type($type) || |
| 2053 | (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2054 | return 0; |
| 2055 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2056 | my $output = ''; |
| 2057 | if ($color) { |
| 2058 | if ($level eq 'ERROR') { |
| 2059 | $output .= RED; |
| 2060 | } elsif ($level eq 'WARNING') { |
| 2061 | $output .= YELLOW; |
| 2062 | } else { |
| 2063 | $output .= GREEN; |
| 2064 | } |
| 2065 | } |
| 2066 | $output .= $prefix . $level . ':'; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2067 | if ($show_types) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2068 | $output .= BLUE if ($color); |
| 2069 | $output .= "$type:"; |
| 2070 | } |
| 2071 | $output .= RESET if ($color); |
| 2072 | $output .= ' ' . $msg . "\n"; |
| 2073 | |
| 2074 | if ($showfile) { |
| 2075 | my @lines = split("\n", $output, -1); |
| 2076 | splice(@lines, 1, 1); |
| 2077 | $output = join("\n", @lines); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2078 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2079 | $output = (split('\n', $output))[0] . "\n" if ($terse); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2080 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2081 | push(our @report, $output); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2082 | |
| 2083 | return 1; |
| 2084 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2085 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2086 | sub report_dump { |
| 2087 | our @report; |
| 2088 | } |
| 2089 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2090 | sub fixup_current_range { |
| 2091 | my ($lineRef, $offset, $length) = @_; |
| 2092 | |
| 2093 | if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { |
| 2094 | my $o = $1; |
| 2095 | my $l = $2; |
| 2096 | my $no = $o + $offset; |
| 2097 | my $nl = $l + $length; |
| 2098 | $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | sub fix_inserted_deleted_lines { |
| 2103 | my ($linesRef, $insertedRef, $deletedRef) = @_; |
| 2104 | |
| 2105 | my $range_last_linenr = 0; |
| 2106 | my $delta_offset = 0; |
| 2107 | |
| 2108 | my $old_linenr = 0; |
| 2109 | my $new_linenr = 0; |
| 2110 | |
| 2111 | my $next_insert = 0; |
| 2112 | my $next_delete = 0; |
| 2113 | |
| 2114 | my @lines = (); |
| 2115 | |
| 2116 | my $inserted = @{$insertedRef}[$next_insert++]; |
| 2117 | my $deleted = @{$deletedRef}[$next_delete++]; |
| 2118 | |
| 2119 | foreach my $old_line (@{$linesRef}) { |
| 2120 | my $save_line = 1; |
| 2121 | my $line = $old_line; #don't modify the array |
| 2122 | if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename |
| 2123 | $delta_offset = 0; |
| 2124 | } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk |
| 2125 | $range_last_linenr = $new_linenr; |
| 2126 | fixup_current_range(\$line, $delta_offset, 0); |
| 2127 | } |
| 2128 | |
| 2129 | while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { |
| 2130 | $deleted = @{$deletedRef}[$next_delete++]; |
| 2131 | $save_line = 0; |
| 2132 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); |
| 2133 | } |
| 2134 | |
| 2135 | while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { |
| 2136 | push(@lines, ${$inserted}{'LINE'}); |
| 2137 | $inserted = @{$insertedRef}[$next_insert++]; |
| 2138 | $new_linenr++; |
| 2139 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); |
| 2140 | } |
| 2141 | |
| 2142 | if ($save_line) { |
| 2143 | push(@lines, $line); |
| 2144 | $new_linenr++; |
| 2145 | } |
| 2146 | |
| 2147 | $old_linenr++; |
| 2148 | } |
| 2149 | |
| 2150 | return @lines; |
| 2151 | } |
| 2152 | |
| 2153 | sub fix_insert_line { |
| 2154 | my ($linenr, $line) = @_; |
| 2155 | |
| 2156 | my $inserted = { |
| 2157 | LINENR => $linenr, |
| 2158 | LINE => $line, |
| 2159 | }; |
| 2160 | push(@fixed_inserted, $inserted); |
| 2161 | } |
| 2162 | |
| 2163 | sub fix_delete_line { |
| 2164 | my ($linenr, $line) = @_; |
| 2165 | |
| 2166 | my $deleted = { |
| 2167 | LINENR => $linenr, |
| 2168 | LINE => $line, |
| 2169 | }; |
| 2170 | |
| 2171 | push(@fixed_deleted, $deleted); |
| 2172 | } |
| 2173 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2174 | sub ERROR { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2175 | my ($type, $msg) = @_; |
| 2176 | |
| 2177 | if (report("ERROR", $type, $msg)) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2178 | our $clean = 0; |
| 2179 | our $cnt_error++; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2180 | return 1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2181 | } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2182 | return 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2183 | } |
| 2184 | sub WARN { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2185 | my ($type, $msg) = @_; |
| 2186 | |
| 2187 | if (report("WARNING", $type, $msg)) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2188 | our $clean = 0; |
| 2189 | our $cnt_warn++; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2190 | return 1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2191 | } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2192 | return 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2193 | } |
| 2194 | sub CHK { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2195 | my ($type, $msg) = @_; |
| 2196 | |
| 2197 | if ($check && report("CHECK", $type, $msg)) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2198 | our $clean = 0; |
| 2199 | our $cnt_chk++; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2200 | return 1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2201 | } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2202 | return 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | sub check_absolute_file { |
| 2206 | my ($absolute, $herecurr) = @_; |
| 2207 | my $file = $absolute; |
| 2208 | |
| 2209 | ##print "absolute<$absolute>\n"; |
| 2210 | |
| 2211 | # See if any suffix of this path is a path within the tree. |
| 2212 | while ($file =~ s@^[^/]*/@@) { |
| 2213 | if (-f "$root/$file") { |
| 2214 | ##print "file<$file>\n"; |
| 2215 | last; |
| 2216 | } |
| 2217 | } |
| 2218 | if (! -f _) { |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
| 2222 | # It is, so see if the prefix is acceptable. |
| 2223 | my $prefix = $absolute; |
| 2224 | substr($prefix, -length($file)) = ''; |
| 2225 | |
| 2226 | ##print "prefix<$prefix>\n"; |
| 2227 | if ($prefix ne ".../") { |
| 2228 | WARN("USE_RELATIVE_PATH", |
| 2229 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
| 2230 | } |
| 2231 | } |
| 2232 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2233 | sub trim { |
| 2234 | my ($string) = @_; |
| 2235 | |
| 2236 | $string =~ s/^\s+|\s+$//g; |
| 2237 | |
| 2238 | return $string; |
| 2239 | } |
| 2240 | |
| 2241 | sub ltrim { |
| 2242 | my ($string) = @_; |
| 2243 | |
| 2244 | $string =~ s/^\s+//; |
| 2245 | |
| 2246 | return $string; |
| 2247 | } |
| 2248 | |
| 2249 | sub rtrim { |
| 2250 | my ($string) = @_; |
| 2251 | |
| 2252 | $string =~ s/\s+$//; |
| 2253 | |
| 2254 | return $string; |
| 2255 | } |
| 2256 | |
| 2257 | sub string_find_replace { |
| 2258 | my ($string, $find, $replace) = @_; |
| 2259 | |
| 2260 | $string =~ s/$find/$replace/g; |
| 2261 | |
| 2262 | return $string; |
| 2263 | } |
| 2264 | |
| 2265 | sub tabify { |
| 2266 | my ($leading) = @_; |
| 2267 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2268 | my $source_indent = $tabsize; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2269 | my $max_spaces_before_tab = $source_indent - 1; |
| 2270 | my $spaces_to_tab = " " x $source_indent; |
| 2271 | |
| 2272 | #convert leading spaces to tabs |
| 2273 | 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; |
| 2274 | #Remove spaces before a tab |
| 2275 | 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; |
| 2276 | |
| 2277 | return "$leading"; |
| 2278 | } |
| 2279 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2280 | sub pos_last_openparen { |
| 2281 | my ($line) = @_; |
| 2282 | |
| 2283 | my $pos = 0; |
| 2284 | |
| 2285 | my $opens = $line =~ tr/\(/\(/; |
| 2286 | my $closes = $line =~ tr/\)/\)/; |
| 2287 | |
| 2288 | my $last_openparen = 0; |
| 2289 | |
| 2290 | if (($opens == 0) || ($closes >= $opens)) { |
| 2291 | return -1; |
| 2292 | } |
| 2293 | |
| 2294 | my $len = length($line); |
| 2295 | |
| 2296 | for ($pos = 0; $pos < $len; $pos++) { |
| 2297 | my $string = substr($line, $pos); |
| 2298 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 2299 | $pos += length($1) - 1; |
| 2300 | } elsif (substr($line, $pos, 1) eq '(') { |
| 2301 | $last_openparen = $pos; |
| 2302 | } elsif (index($string, '(') == -1) { |
| 2303 | last; |
| 2304 | } |
| 2305 | } |
| 2306 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2307 | return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2310 | sub get_raw_comment { |
| 2311 | my ($line, $rawline) = @_; |
| 2312 | my $comment = ''; |
| 2313 | |
| 2314 | for my $i (0 .. (length($line) - 1)) { |
| 2315 | if (substr($line, $i, 1) eq "$;") { |
| 2316 | $comment .= substr($rawline, $i, 1); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | return $comment; |
| 2321 | } |
| 2322 | |
Simon Glass | c8f4d09 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 2323 | # Checks specific to U-Boot |
| 2324 | sub u_boot_line { |
Simon Glass | f38051e | 2020-07-19 10:16:01 -0600 | [diff] [blame] | 2325 | my ($realfile, $line, $rawline, $herecurr) = @_; |
Simon Glass | 7d919f5 | 2020-05-22 16:32:37 -0600 | [diff] [blame] | 2326 | |
| 2327 | # ask for a test if a new uclass ID is added |
| 2328 | if ($realfile =~ /uclass-id.h/ && $line =~ /^\+/) { |
| 2329 | WARN("NEW_UCLASS", |
| 2330 | "Possible new uclass - make sure to add a sandbox driver, plus a test in test/dm/<name>.c\n" . $herecurr); |
| 2331 | } |
Simon Glass | 3ee0132 | 2020-05-22 16:32:38 -0600 | [diff] [blame] | 2332 | |
| 2333 | # try to get people to use the livetree API |
| 2334 | if ($line =~ /^\+.*fdtdec_/) { |
| 2335 | WARN("LIVETREE", |
| 2336 | "Use the livetree API (dev_read_...)\n" . $herecurr); |
| 2337 | } |
Simon Glass | 35bf3b0 | 2020-05-22 16:32:39 -0600 | [diff] [blame] | 2338 | |
| 2339 | # add tests for new commands |
| 2340 | if ($line =~ /^\+.*do_($Ident)\(struct cmd_tbl.*/) { |
| 2341 | WARN("CMD_TEST", |
| 2342 | "Possible new command - make sure you add a test\n" . $herecurr); |
| 2343 | } |
Simon Glass | 567240d | 2020-05-22 16:32:40 -0600 | [diff] [blame] | 2344 | |
| 2345 | # use if instead of #if |
Simon Glass | 2ec1ed2 | 2020-06-14 10:54:08 -0600 | [diff] [blame] | 2346 | if ($realfile =~ /\.c$/ && $line =~ /^\+#if.*CONFIG.*/) { |
Simon Glass | 567240d | 2020-05-22 16:32:40 -0600 | [diff] [blame] | 2347 | WARN("PREFER_IF", |
| 2348 | "Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef' where possible\n" . $herecurr); |
| 2349 | } |
Tom Rini | d7b4ab5 | 2020-05-26 14:29:02 -0400 | [diff] [blame] | 2350 | |
| 2351 | # use defconfig to manage CONFIG_CMD options |
| 2352 | if ($line =~ /\+\s*#\s*(define|undef)\s+(CONFIG_CMD\w*)\b/) { |
| 2353 | ERROR("DEFINE_CONFIG_CMD", |
| 2354 | "All commands are managed by Kconfig\n" . $herecurr); |
| 2355 | } |
Simon Glass | f38051e | 2020-07-19 10:16:01 -0600 | [diff] [blame] | 2356 | |
| 2357 | # Don't put common.h and dm.h in header files |
| 2358 | if ($realfile =~ /\.h$/ && $rawline =~ /^\+#include\s*<(common|dm)\.h>*/) { |
| 2359 | ERROR("BARRED_INCLUDE_IN_HDR", |
| 2360 | "Avoid including common.h and dm.h in header files\n" . $herecurr); |
| 2361 | } |
Simon Glass | c8f4d09 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 2362 | } |
| 2363 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2364 | sub process { |
| 2365 | my $filename = shift; |
| 2366 | |
| 2367 | my $linenr=0; |
| 2368 | my $prevline=""; |
| 2369 | my $prevrawline=""; |
| 2370 | my $stashline=""; |
| 2371 | my $stashrawline=""; |
| 2372 | |
| 2373 | my $length; |
| 2374 | my $indent; |
| 2375 | my $previndent=0; |
| 2376 | my $stashindent=0; |
| 2377 | |
| 2378 | our $clean = 1; |
| 2379 | my $signoff = 0; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2380 | my $author = ''; |
| 2381 | my $authorsignoff = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2382 | my $is_patch = 0; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2383 | my $is_binding_patch = -1; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2384 | my $in_header_lines = $file ? 0 : 1; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2385 | my $in_commit_log = 0; #Scanning lines before patch |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2386 | my $has_patch_separator = 0; #Found a --- line |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2387 | my $has_commit_log = 0; #Encountered lines before patch |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2388 | my $commit_log_lines = 0; #Number of commit log lines |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2389 | my $commit_log_possible_stack_dump = 0; |
| 2390 | my $commit_log_long_line = 0; |
| 2391 | my $commit_log_has_diff = 0; |
| 2392 | my $reported_maintainer_file = 0; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2393 | my $non_utf8_charset = 0; |
| 2394 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2395 | my $last_blank_line = 0; |
| 2396 | my $last_coalesced_string_linenr = -1; |
| 2397 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2398 | our @report = (); |
| 2399 | our $cnt_lines = 0; |
| 2400 | our $cnt_error = 0; |
| 2401 | our $cnt_warn = 0; |
| 2402 | our $cnt_chk = 0; |
| 2403 | |
| 2404 | # Trace the real file/line as we go. |
| 2405 | my $realfile = ''; |
| 2406 | my $realline = 0; |
| 2407 | my $realcnt = 0; |
| 2408 | my $here = ''; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2409 | my $context_function; #undef'd unless there's a known function |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2410 | my $in_comment = 0; |
| 2411 | my $comment_edge = 0; |
| 2412 | my $first_line = 0; |
| 2413 | my $p1_prefix = ''; |
| 2414 | |
| 2415 | my $prev_values = 'E'; |
| 2416 | |
| 2417 | # suppression flags |
| 2418 | my %suppress_ifbraces; |
| 2419 | my %suppress_whiletrailers; |
| 2420 | my %suppress_export; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2421 | my $suppress_statement = 0; |
| 2422 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2423 | my %signatures = (); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2424 | |
| 2425 | # Pre-scan the patch sanitizing the lines. |
| 2426 | # Pre-scan the patch looking for any __setup documentation. |
| 2427 | # |
| 2428 | my @setup_docs = (); |
| 2429 | my $setup_docs = 0; |
| 2430 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2431 | my $camelcase_file_seeded = 0; |
| 2432 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 2433 | my $checklicenseline = 1; |
| 2434 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2435 | sanitise_line_reset(); |
| 2436 | my $line; |
| 2437 | foreach my $rawline (@rawlines) { |
| 2438 | $linenr++; |
| 2439 | $line = $rawline; |
| 2440 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2441 | push(@fixed, $rawline) if ($fix); |
| 2442 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2443 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
| 2444 | $setup_docs = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2445 | if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2446 | $setup_docs = 1; |
| 2447 | } |
| 2448 | #next; |
| 2449 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2450 | if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2451 | $realline=$1-1; |
| 2452 | if (defined $2) { |
| 2453 | $realcnt=$3+1; |
| 2454 | } else { |
| 2455 | $realcnt=1+1; |
| 2456 | } |
| 2457 | $in_comment = 0; |
| 2458 | |
| 2459 | # Guestimate if this is a continuing comment. Run |
| 2460 | # the context looking for a comment "edge". If this |
| 2461 | # edge is a close comment then we must be in a comment |
| 2462 | # at context start. |
| 2463 | my $edge; |
| 2464 | my $cnt = $realcnt; |
| 2465 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 2466 | next if (defined $rawlines[$ln - 1] && |
| 2467 | $rawlines[$ln - 1] =~ /^-/); |
| 2468 | $cnt--; |
| 2469 | #print "RAW<$rawlines[$ln - 1]>\n"; |
| 2470 | last if (!defined $rawlines[$ln - 1]); |
| 2471 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 2472 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 2473 | ($edge) = $1; |
| 2474 | last; |
| 2475 | } |
| 2476 | } |
| 2477 | if (defined $edge && $edge eq '*/') { |
| 2478 | $in_comment = 1; |
| 2479 | } |
| 2480 | |
| 2481 | # Guestimate if this is a continuing comment. If this |
| 2482 | # is the start of a diff block and this line starts |
| 2483 | # ' *' then it is very likely a comment. |
| 2484 | if (!defined $edge && |
| 2485 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
| 2486 | { |
| 2487 | $in_comment = 1; |
| 2488 | } |
| 2489 | |
| 2490 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 2491 | sanitise_line_reset($in_comment); |
| 2492 | |
| 2493 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
| 2494 | # Standardise the strings and chars within the input to |
| 2495 | # simplify matching -- only bother with positive lines. |
| 2496 | $line = sanitise_line($rawline); |
| 2497 | } |
| 2498 | push(@lines, $line); |
| 2499 | |
| 2500 | if ($realcnt > 1) { |
| 2501 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 2502 | } else { |
| 2503 | $realcnt = 0; |
| 2504 | } |
| 2505 | |
| 2506 | #print "==>$rawline\n"; |
| 2507 | #print "-->$line\n"; |
| 2508 | |
| 2509 | if ($setup_docs && $line =~ /^\+/) { |
| 2510 | push(@setup_docs, $line); |
| 2511 | } |
| 2512 | } |
| 2513 | |
| 2514 | $prefix = ''; |
| 2515 | |
| 2516 | $realcnt = 0; |
| 2517 | $linenr = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2518 | $fixlinenr = -1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2519 | foreach my $line (@lines) { |
| 2520 | $linenr++; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2521 | $fixlinenr++; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2522 | my $sline = $line; #copy of $line |
| 2523 | $sline =~ s/$;/ /g; #with comments as spaces |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2524 | |
| 2525 | my $rawline = $rawlines[$linenr - 1]; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2526 | my $raw_comment = get_raw_comment($line, $rawline); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2527 | |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2528 | # check if it's a mode change, rename or start of a patch |
| 2529 | if (!$in_commit_log && |
| 2530 | ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || |
| 2531 | ($line =~ /^rename (?:from|to) \S+\s*$/ || |
| 2532 | $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { |
| 2533 | $is_patch = 1; |
| 2534 | } |
| 2535 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2536 | #extract the line range in the file after the patch is applied |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2537 | if (!$in_commit_log && |
| 2538 | $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { |
| 2539 | my $context = $4; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2540 | $is_patch = 1; |
| 2541 | $first_line = $linenr + 1; |
| 2542 | $realline=$1-1; |
| 2543 | if (defined $2) { |
| 2544 | $realcnt=$3+1; |
| 2545 | } else { |
| 2546 | $realcnt=1+1; |
| 2547 | } |
| 2548 | annotate_reset(); |
| 2549 | $prev_values = 'E'; |
| 2550 | |
| 2551 | %suppress_ifbraces = (); |
| 2552 | %suppress_whiletrailers = (); |
| 2553 | %suppress_export = (); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2554 | $suppress_statement = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2555 | if ($context =~ /\b(\w+)\s*\(/) { |
| 2556 | $context_function = $1; |
| 2557 | } else { |
| 2558 | undef $context_function; |
| 2559 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2560 | next; |
| 2561 | |
| 2562 | # track the line number as we move through the hunk, note that |
| 2563 | # new versions of GNU diff omit the leading space on completely |
| 2564 | # blank context lines so we need to count that too. |
| 2565 | } elsif ($line =~ /^( |\+|$)/) { |
| 2566 | $realline++; |
| 2567 | $realcnt-- if ($realcnt != 0); |
| 2568 | |
| 2569 | # Measure the line length and indent. |
| 2570 | ($length, $indent) = line_stats($rawline); |
| 2571 | |
| 2572 | # Track the previous line. |
| 2573 | ($prevline, $stashline) = ($stashline, $line); |
| 2574 | ($previndent, $stashindent) = ($stashindent, $indent); |
| 2575 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 2576 | |
| 2577 | #warn "line<$line>\n"; |
| 2578 | |
| 2579 | } elsif ($realcnt == 1) { |
| 2580 | $realcnt--; |
| 2581 | } |
| 2582 | |
| 2583 | my $hunk_line = ($realcnt != 0); |
| 2584 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2585 | $here = "#$linenr: " if (!$file); |
| 2586 | $here = "#$realline: " if ($file); |
| 2587 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2588 | my $found_file = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2589 | # extract the filename as it passes |
| 2590 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 2591 | $realfile = $1; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2592 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2593 | $in_commit_log = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2594 | $found_file = 1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2595 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
| 2596 | $realfile = $1; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2597 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2598 | $in_commit_log = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2599 | |
| 2600 | $p1_prefix = $1; |
| 2601 | if (!$file && $tree && $p1_prefix ne '' && |
| 2602 | -e "$root/$p1_prefix") { |
| 2603 | WARN("PATCH_PREFIX", |
| 2604 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
| 2605 | } |
| 2606 | |
| 2607 | if ($realfile =~ m@^include/asm/@) { |
| 2608 | ERROR("MODIFIED_INCLUDE_ASM", |
| 2609 | "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); |
| 2610 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2611 | $found_file = 1; |
| 2612 | } |
| 2613 | |
| 2614 | #make up the handle for any error we report on this line |
| 2615 | if ($showfile) { |
| 2616 | $prefix = "$realfile:$realline: " |
| 2617 | } elsif ($emacs) { |
| 2618 | if ($file) { |
| 2619 | $prefix = "$filename:$realline: "; |
| 2620 | } else { |
| 2621 | $prefix = "$filename:$linenr: "; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | if ($found_file) { |
| 2626 | if (is_maintained_obsolete($realfile)) { |
| 2627 | WARN("OBSOLETE", |
| 2628 | "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); |
| 2629 | } |
| 2630 | if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { |
| 2631 | $check = 1; |
| 2632 | } else { |
| 2633 | $check = $check_orig; |
| 2634 | } |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 2635 | $checklicenseline = 1; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2636 | |
| 2637 | if ($realfile !~ /^MAINTAINERS/) { |
| 2638 | my $last_binding_patch = $is_binding_patch; |
| 2639 | |
| 2640 | $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; |
| 2641 | |
| 2642 | if (($last_binding_patch != -1) && |
| 2643 | ($last_binding_patch ^ $is_binding_patch)) { |
| 2644 | WARN("DT_SPLIT_BINDING_PATCH", |
| 2645 | "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n"); |
| 2646 | } |
| 2647 | } |
| 2648 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2649 | next; |
| 2650 | } |
| 2651 | |
| 2652 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
| 2653 | |
| 2654 | my $hereline = "$here\n$rawline\n"; |
| 2655 | my $herecurr = "$here\n$rawline\n"; |
| 2656 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
| 2657 | |
| 2658 | $cnt_lines++ if ($realcnt != 0); |
| 2659 | |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2660 | # Verify the existence of a commit log if appropriate |
| 2661 | # 2 is used because a $signature is counted in $commit_log_lines |
| 2662 | if ($in_commit_log) { |
| 2663 | if ($line !~ /^\s*$/) { |
| 2664 | $commit_log_lines++; #could be a $signature |
| 2665 | } |
| 2666 | } elsif ($has_commit_log && $commit_log_lines < 2) { |
| 2667 | WARN("COMMIT_MESSAGE", |
| 2668 | "Missing commit description - Add an appropriate one\n"); |
| 2669 | $commit_log_lines = 2; #warn only once |
| 2670 | } |
| 2671 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2672 | # Check if the commit log has what seems like a diff which can confuse patch |
| 2673 | if ($in_commit_log && !$commit_log_has_diff && |
| 2674 | (($line =~ m@^\s+diff\b.*a/[\w/]+@ && |
| 2675 | $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || |
| 2676 | $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || |
| 2677 | $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { |
| 2678 | ERROR("DIFF_IN_COMMIT_MSG", |
| 2679 | "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); |
| 2680 | $commit_log_has_diff = 1; |
| 2681 | } |
| 2682 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2683 | # Check for incorrect file permissions |
| 2684 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 2685 | my $permhere = $here . "FILE: $realfile\n"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2686 | if ($realfile !~ m@scripts/@ && |
| 2687 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2688 | ERROR("EXECUTE_PERMISSIONS", |
| 2689 | "do not set execute permissions for source files\n" . $permhere); |
| 2690 | } |
| 2691 | } |
| 2692 | |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2693 | # Check the patch for a From: |
| 2694 | if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { |
| 2695 | $author = $1; |
| 2696 | $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); |
| 2697 | $author =~ s/"//g; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2698 | $author = reformat_email($author); |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2699 | } |
| 2700 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2701 | # Check the patch for a signoff: |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2702 | if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2703 | $signoff++; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2704 | $in_commit_log = 0; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2705 | if ($author ne '') { |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2706 | if (same_email_addresses($1, $author)) { |
| 2707 | $authorsignoff = 1; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 2708 | } |
| 2709 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2712 | # Check for patch separator |
| 2713 | if ($line =~ /^---$/) { |
| 2714 | $has_patch_separator = 1; |
| 2715 | $in_commit_log = 0; |
| 2716 | } |
| 2717 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2718 | # Check if MAINTAINERS is being updated. If so, there's probably no need to |
| 2719 | # emit the "does MAINTAINERS need updating?" message on file add/move/delete |
| 2720 | if ($line =~ /^\s*MAINTAINERS\s*\|/) { |
| 2721 | $reported_maintainer_file = 1; |
| 2722 | } |
| 2723 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2724 | # Check signature styles |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2725 | if (!$in_header_lines && |
| 2726 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2727 | my $space_before = $1; |
| 2728 | my $sign_off = $2; |
| 2729 | my $space_after = $3; |
| 2730 | my $email = $4; |
| 2731 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 2732 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2733 | if ($sign_off !~ /$signature_tags/) { |
| 2734 | WARN("BAD_SIGN_OFF", |
| 2735 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 2736 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2737 | if (defined $space_before && $space_before ne "") { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2738 | if (WARN("BAD_SIGN_OFF", |
| 2739 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && |
| 2740 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2741 | $fixed[$fixlinenr] = |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2742 | "$ucfirst_sign_off $email"; |
| 2743 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2744 | } |
| 2745 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2746 | if (WARN("BAD_SIGN_OFF", |
| 2747 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && |
| 2748 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2749 | $fixed[$fixlinenr] = |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2750 | "$ucfirst_sign_off $email"; |
| 2751 | } |
| 2752 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2753 | } |
| 2754 | if (!defined $space_after || $space_after ne " ") { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2755 | if (WARN("BAD_SIGN_OFF", |
| 2756 | "Use a single space after $ucfirst_sign_off\n" . $herecurr) && |
| 2757 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2758 | $fixed[$fixlinenr] = |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2759 | "$ucfirst_sign_off $email"; |
| 2760 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2763 | my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2764 | my $suggested_email = format_email(($email_name, $email_address)); |
| 2765 | if ($suggested_email eq "") { |
| 2766 | ERROR("BAD_SIGN_OFF", |
| 2767 | "Unrecognized email address: '$email'\n" . $herecurr); |
| 2768 | } else { |
| 2769 | my $dequoted = $suggested_email; |
| 2770 | $dequoted =~ s/^"//; |
| 2771 | $dequoted =~ s/" </ </; |
| 2772 | # Don't force email to have quotes |
| 2773 | # Allow just an angle bracketed address |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2774 | if (!same_email_addresses($email, $suggested_email)) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2775 | WARN("BAD_SIGN_OFF", |
| 2776 | "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); |
| 2777 | } |
| 2778 | } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 2779 | |
| 2780 | # Check for duplicate signatures |
| 2781 | my $sig_nospace = $line; |
| 2782 | $sig_nospace =~ s/\s//g; |
| 2783 | $sig_nospace = lc($sig_nospace); |
| 2784 | if (defined $signatures{$sig_nospace}) { |
| 2785 | WARN("BAD_SIGN_OFF", |
| 2786 | "Duplicate signature\n" . $herecurr); |
| 2787 | } else { |
| 2788 | $signatures{$sig_nospace} = 1; |
| 2789 | } |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2790 | |
| 2791 | # Check Co-developed-by: immediately followed by Signed-off-by: with same name and email |
| 2792 | if ($sign_off =~ /^co-developed-by:$/i) { |
| 2793 | if ($email eq $author) { |
| 2794 | WARN("BAD_SIGN_OFF", |
| 2795 | "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); |
| 2796 | } |
| 2797 | if (!defined $lines[$linenr]) { |
| 2798 | WARN("BAD_SIGN_OFF", |
| 2799 | "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); |
| 2800 | } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { |
| 2801 | WARN("BAD_SIGN_OFF", |
| 2802 | "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); |
| 2803 | } elsif ($1 ne $email) { |
| 2804 | WARN("BAD_SIGN_OFF", |
| 2805 | "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); |
| 2806 | } |
| 2807 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | # Check email subject for common tools that don't need to be mentioned |
| 2811 | if ($in_header_lines && |
| 2812 | $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { |
| 2813 | WARN("EMAIL_SUBJECT", |
| 2814 | "A patch subject line should describe the change not the tool that found it\n" . $herecurr); |
| 2815 | } |
| 2816 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2817 | # Check for Gerrit Change-Ids not in any patch context |
| 2818 | if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2819 | ERROR("GERRIT_CHANGE_ID", |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2820 | "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | # Check if the commit log is in a possible stack dump |
| 2824 | if ($in_commit_log && !$commit_log_possible_stack_dump && |
| 2825 | ($line =~ /^\s*(?:WARNING:|BUG:)/ || |
| 2826 | $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || |
| 2827 | # timestamp |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2828 | $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || |
| 2829 | $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || |
| 2830 | $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { |
| 2831 | # stack dump address styles |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2832 | $commit_log_possible_stack_dump = 1; |
| 2833 | } |
| 2834 | |
| 2835 | # Check for line lengths > 75 in commit log, warn once |
| 2836 | if ($in_commit_log && !$commit_log_long_line && |
| 2837 | length($line) > 75 && |
| 2838 | !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || |
| 2839 | # file delta changes |
| 2840 | $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || |
| 2841 | # filename then : |
| 2842 | $line =~ /^\s*(?:Fixes:|Link:)/i || |
| 2843 | # A Fixes: or Link: line |
| 2844 | $commit_log_possible_stack_dump)) { |
| 2845 | WARN("COMMIT_LOG_LONG_LINE", |
| 2846 | "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); |
| 2847 | $commit_log_long_line = 1; |
| 2848 | } |
| 2849 | |
| 2850 | # Reset possible stack dump if a blank line is found |
| 2851 | if ($in_commit_log && $commit_log_possible_stack_dump && |
| 2852 | $line =~ /^\s*$/) { |
| 2853 | $commit_log_possible_stack_dump = 0; |
| 2854 | } |
| 2855 | |
| 2856 | # Check for git id commit length and improperly formed commit descriptions |
| 2857 | if ($in_commit_log && !$commit_log_possible_stack_dump && |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2858 | $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2859 | $line !~ /^This reverts commit [0-9a-f]{7,40}/ && |
| 2860 | ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || |
| 2861 | ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && |
| 2862 | $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && |
| 2863 | $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { |
| 2864 | my $init_char = "c"; |
| 2865 | my $orig_commit = ""; |
| 2866 | my $short = 1; |
| 2867 | my $long = 0; |
| 2868 | my $case = 1; |
| 2869 | my $space = 1; |
| 2870 | my $hasdesc = 0; |
| 2871 | my $hasparens = 0; |
| 2872 | my $id = '0123456789ab'; |
| 2873 | my $orig_desc = "commit description"; |
| 2874 | my $description = ""; |
| 2875 | |
| 2876 | if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { |
| 2877 | $init_char = $1; |
| 2878 | $orig_commit = lc($2); |
| 2879 | } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { |
| 2880 | $orig_commit = lc($1); |
| 2881 | } |
| 2882 | |
| 2883 | $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); |
| 2884 | $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); |
| 2885 | $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); |
| 2886 | $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); |
| 2887 | if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { |
| 2888 | $orig_desc = $1; |
| 2889 | $hasparens = 1; |
| 2890 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && |
| 2891 | defined $rawlines[$linenr] && |
| 2892 | $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { |
| 2893 | $orig_desc = $1; |
| 2894 | $hasparens = 1; |
| 2895 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && |
| 2896 | defined $rawlines[$linenr] && |
| 2897 | $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { |
| 2898 | $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; |
| 2899 | $orig_desc = $1; |
| 2900 | $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; |
| 2901 | $orig_desc .= " " . $1; |
| 2902 | $hasparens = 1; |
| 2903 | } |
| 2904 | |
| 2905 | ($id, $description) = git_commit_info($orig_commit, |
| 2906 | $id, $orig_desc); |
| 2907 | |
| 2908 | if (defined($id) && |
| 2909 | ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { |
| 2910 | ERROR("GIT_COMMIT_ID", |
| 2911 | "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | # Check for added, moved or deleted files |
| 2916 | if (!$reported_maintainer_file && !$in_commit_log && |
| 2917 | ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || |
| 2918 | $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || |
| 2919 | ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && |
| 2920 | (defined($1) || defined($2))))) { |
| 2921 | $is_patch = 1; |
| 2922 | $reported_maintainer_file = 1; |
| 2923 | WARN("FILE_PATH_CHANGES", |
| 2924 | "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 2927 | # Check for adding new DT bindings not in schema format |
| 2928 | if (!$in_commit_log && |
| 2929 | ($line =~ /^new file mode\s*\d+\s*$/) && |
| 2930 | ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { |
| 2931 | WARN("DT_SCHEMA_BINDING_PATCH", |
| 2932 | "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); |
| 2933 | } |
| 2934 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2935 | # Check for wrappage within a valid hunk of the file |
| 2936 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
| 2937 | ERROR("CORRUPTED_PATCH", |
| 2938 | "patch seems to be corrupt (line wrapped?)\n" . |
| 2939 | $herecurr) if (!$emitted_corrupt++); |
| 2940 | } |
| 2941 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 2942 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 2943 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
| 2944 | $rawline !~ m/^$UTF8*$/) { |
| 2945 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 2946 | |
| 2947 | my $blank = copy_spacing($rawline); |
| 2948 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 2949 | my $hereptr = "$hereline$ptr\n"; |
| 2950 | |
| 2951 | CHK("INVALID_UTF8", |
| 2952 | "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); |
| 2953 | } |
| 2954 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2955 | # Check if it's the start of a commit log |
| 2956 | # (not a header line and we haven't seen the patch filename) |
| 2957 | if ($in_header_lines && $realfile =~ /^$/ && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2958 | !($rawline =~ /^\s+(?:\S|$)/ || |
| 2959 | $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2960 | $in_header_lines = 0; |
| 2961 | $in_commit_log = 1; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2962 | $has_commit_log = 1; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 2966 | # declined it, i.e defined some charset where it is missing. |
| 2967 | if ($in_header_lines && |
| 2968 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 2969 | $1 !~ /utf-8/i) { |
| 2970 | $non_utf8_charset = 1; |
| 2971 | } |
| 2972 | |
| 2973 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
| 2974 | $rawline =~ /$NON_ASCII_UTF8/) { |
| 2975 | WARN("UTF8_BEFORE_PATCH", |
| 2976 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 2977 | } |
| 2978 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 2979 | # Check for absolute kernel paths in commit message |
| 2980 | if ($tree && $in_commit_log) { |
| 2981 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 2982 | my $file = $1; |
| 2983 | |
| 2984 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 2985 | check_absolute_file($1, $herecurr)) { |
| 2986 | # |
| 2987 | } else { |
| 2988 | check_absolute_file($file, $herecurr); |
| 2989 | } |
| 2990 | } |
| 2991 | } |
| 2992 | |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 2993 | # Check for various typo / spelling mistakes |
| 2994 | if (defined($misspellings) && |
| 2995 | ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { |
| 2996 | while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { |
| 2997 | my $typo = $1; |
| 2998 | my $typo_fix = $spelling_fix{lc($typo)}; |
| 2999 | $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); |
| 3000 | $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3001 | my $msg_level = \&WARN; |
| 3002 | $msg_level = \&CHK if ($file); |
| 3003 | if (&{$msg_level}("TYPO_SPELLING", |
| 3004 | "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && |
Dan Murphy | 463f688 | 2017-01-31 14:15:53 -0600 | [diff] [blame] | 3005 | $fix) { |
| 3006 | $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; |
| 3007 | } |
| 3008 | } |
| 3009 | } |
| 3010 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3011 | # check for invalid commit id |
| 3012 | if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { |
| 3013 | my $id; |
| 3014 | my $description; |
| 3015 | ($id, $description) = git_commit_info($2, undef, undef); |
| 3016 | if (!defined($id)) { |
| 3017 | WARN("UNKNOWN_COMMIT_ID", |
| 3018 | "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); |
| 3019 | } |
| 3020 | } |
| 3021 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3022 | # ignore non-hunk lines and lines being removed |
| 3023 | next if (!$hunk_line || $line =~ /^-/); |
| 3024 | |
| 3025 | #trailing whitespace |
| 3026 | if ($line =~ /^\+.*\015/) { |
| 3027 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3028 | if (ERROR("DOS_LINE_ENDINGS", |
| 3029 | "DOS line endings\n" . $herevet) && |
| 3030 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3031 | $fixed[$fixlinenr] =~ s/[\s\015]+$//; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3032 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3033 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 3034 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3035 | if (ERROR("TRAILING_WHITESPACE", |
| 3036 | "trailing whitespace\n" . $herevet) && |
| 3037 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3038 | $fixed[$fixlinenr] =~ s/\s+$//; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3039 | } |
| 3040 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3041 | $rpt_cleaners = 1; |
| 3042 | } |
| 3043 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3044 | # Check for FSF mailing addresses. |
| 3045 | if ($rawline =~ /\bwrite to the Free/i || |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3046 | $rawline =~ /\b675\s+Mass\s+Ave/i || |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3047 | $rawline =~ /\b59\s+Temple\s+Pl/i || |
| 3048 | $rawline =~ /\b51\s+Franklin\s+St/i) { |
| 3049 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3050 | my $msg_level = \&ERROR; |
| 3051 | $msg_level = \&CHK if ($file); |
| 3052 | &{$msg_level}("FSF_MAILING_ADDRESS", |
| 3053 | "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 | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3054 | } |
| 3055 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3056 | # check for Kconfig help text having a real description |
| 3057 | # Only applies when adding the entry originally, after that we do not have |
| 3058 | # sufficient context to determine whether it is indeed long enough. |
| 3059 | if ($realfile =~ /Kconfig/ && |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3060 | # 'choice' is usually the last thing on the line (though |
| 3061 | # Kconfig supports named choices), so use a word boundary |
| 3062 | # (\b) rather than a whitespace character (\s) |
| 3063 | $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3064 | my $length = 0; |
| 3065 | my $cnt = $realcnt; |
| 3066 | my $ln = $linenr + 1; |
| 3067 | my $f; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3068 | my $is_start = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3069 | my $is_end = 0; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3070 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3071 | $f = $lines[$ln - 1]; |
| 3072 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 3073 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3074 | |
| 3075 | next if ($f =~ /^-/); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3076 | last if (!$file && $f =~ /^\@\@/); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3077 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3078 | if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3079 | $is_start = 1; |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3080 | } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { |
| 3081 | if ($lines[$ln - 1] =~ "---help---") { |
| 3082 | WARN("CONFIG_DESCRIPTION", |
| 3083 | "prefer 'help' over '---help---' for new help texts\n" . $herecurr); |
| 3084 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3085 | $length = -1; |
| 3086 | } |
| 3087 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3088 | $f =~ s/^.//; |
| 3089 | $f =~ s/#.*//; |
| 3090 | $f =~ s/^\s+//; |
| 3091 | next if ($f =~ /^$/); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3092 | |
| 3093 | # This only checks context lines in the patch |
| 3094 | # and so hopefully shouldn't trigger false |
| 3095 | # positives, even though some of these are |
| 3096 | # common words in help texts |
| 3097 | if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| |
| 3098 | if|endif|menu|endmenu|source)\b/x) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3099 | $is_end = 1; |
| 3100 | last; |
| 3101 | } |
| 3102 | $length++; |
| 3103 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3104 | if ($is_start && $is_end && $length < $min_conf_desc_length) { |
| 3105 | WARN("CONFIG_DESCRIPTION", |
| 3106 | "please write a paragraph that describes the config symbol fully\n" . $herecurr); |
| 3107 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3108 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
| 3109 | } |
| 3110 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3111 | # check for MAINTAINERS entries that don't have the right form |
| 3112 | if ($realfile =~ /^MAINTAINERS$/ && |
| 3113 | $rawline =~ /^\+[A-Z]:/ && |
| 3114 | $rawline !~ /^\+[A-Z]:\t\S/) { |
| 3115 | if (WARN("MAINTAINERS_STYLE", |
| 3116 | "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && |
| 3117 | $fix) { |
| 3118 | $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | # discourage the use of boolean for type definition attributes of Kconfig options |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3123 | if ($realfile =~ /Kconfig/ && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3124 | $line =~ /^\+\s*\bboolean\b/) { |
| 3125 | WARN("CONFIG_TYPE_BOOLEAN", |
| 3126 | "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3129 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 3130 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 3131 | my $flag = $1; |
| 3132 | my $replacement = { |
| 3133 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 3134 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 3135 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 3136 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 3137 | }; |
| 3138 | |
| 3139 | WARN("DEPRECATED_VARIABLE", |
| 3140 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
| 3141 | } |
| 3142 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3143 | # check for DT compatible documentation |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3144 | if (defined $root && |
| 3145 | (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || |
| 3146 | ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { |
| 3147 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3148 | my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; |
| 3149 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3150 | my $dt_path = $root . "/Documentation/devicetree/bindings/"; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3151 | my $vp_file = $dt_path . "vendor-prefixes.yaml"; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3152 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3153 | foreach my $compat (@compats) { |
| 3154 | my $compat2 = $compat; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3155 | $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; |
| 3156 | my $compat3 = $compat; |
| 3157 | $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; |
| 3158 | `grep -Erq "$compat|$compat2|$compat3" $dt_path`; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3159 | if ( $? >> 8 ) { |
| 3160 | WARN("UNDOCUMENTED_DT_STRING", |
| 3161 | "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); |
| 3162 | } |
| 3163 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3164 | next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; |
| 3165 | my $vendor = $1; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3166 | `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3167 | if ( $? >> 8 ) { |
| 3168 | WARN("UNDOCUMENTED_DT_STRING", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3169 | "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3170 | } |
| 3171 | } |
| 3172 | } |
| 3173 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3174 | # check for using SPDX license tag at beginning of files |
| 3175 | if ($realline == $checklicenseline) { |
| 3176 | if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { |
| 3177 | $checklicenseline = 2; |
| 3178 | } elsif ($rawline =~ /^\+/) { |
| 3179 | my $comment = ""; |
| 3180 | if ($realfile =~ /\.(h|s|S)$/) { |
| 3181 | $comment = '/*'; |
| 3182 | } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { |
| 3183 | $comment = '//'; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3184 | } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3185 | $comment = '#'; |
| 3186 | } elsif ($realfile =~ /\.rst$/) { |
| 3187 | $comment = '..'; |
| 3188 | } |
| 3189 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3190 | # check SPDX comment style for .[chsS] files |
| 3191 | if ($realfile =~ /\.[chsS]$/ && |
| 3192 | $rawline =~ /SPDX-License-Identifier:/ && |
| 3193 | $rawline !~ m@^\+\s*\Q$comment\E\s*@) { |
| 3194 | WARN("SPDX_LICENSE_TAG", |
| 3195 | "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); |
| 3196 | } |
| 3197 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3198 | if ($comment !~ /^$/ && |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3199 | $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { |
| 3200 | WARN("SPDX_LICENSE_TAG", |
| 3201 | "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3202 | } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3203 | my $spdx_license = $1; |
| 3204 | if (!is_SPDX_License_valid($spdx_license)) { |
| 3205 | WARN("SPDX_LICENSE_TAG", |
| 3206 | "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); |
| 3207 | } |
| 3208 | if ($realfile =~ m@^Documentation/devicetree/bindings/@ && |
| 3209 | not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { |
| 3210 | my $msg_level = \&WARN; |
| 3211 | $msg_level = \&CHK if ($file); |
| 3212 | if (&{$msg_level}("SPDX_LICENSE_TAG", |
| 3213 | |
| 3214 | "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && |
| 3215 | $fix) { |
| 3216 | $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; |
| 3217 | } |
| 3218 | } |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3219 | } |
| 3220 | } |
| 3221 | } |
| 3222 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3223 | # check we are in a valid source file if not then ignore this hunk |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3224 | next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3225 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3226 | # check for using SPDX-License-Identifier on the wrong line number |
| 3227 | if ($realline != $checklicenseline && |
| 3228 | $rawline =~ /\bSPDX-License-Identifier:/ && |
| 3229 | substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { |
| 3230 | WARN("SPDX_LICENSE_TAG", |
| 3231 | "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); |
| 3232 | } |
| 3233 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3234 | # line length limit (with some exclusions) |
| 3235 | # |
| 3236 | # There are a few types of lines that may extend beyond $max_line_length: |
| 3237 | # logging functions like pr_info that end in a string |
| 3238 | # lines with a single string |
| 3239 | # #defines that are a single string |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3240 | # lines with an RFC3986 like URL |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3241 | # |
| 3242 | # There are 3 different line length message types: |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3243 | # LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3244 | # LONG_LINE_STRING a string starts before but extends beyond $max_line_length |
| 3245 | # LONG_LINE all other lines longer than $max_line_length |
| 3246 | # |
| 3247 | # if LONG_LINE is ignored, the other 2 types are also ignored |
| 3248 | # |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3249 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3250 | if ($line =~ /^\+/ && $length > $max_line_length) { |
| 3251 | my $msg_type = "LONG_LINE"; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3252 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3253 | # Check the allowed long line types first |
| 3254 | |
| 3255 | # logging functions that end in a string that starts |
| 3256 | # before $max_line_length |
| 3257 | if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && |
| 3258 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3259 | $msg_type = ""; |
| 3260 | |
| 3261 | # lines with only strings (w/ possible termination) |
| 3262 | # #defines with only strings |
| 3263 | } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || |
| 3264 | $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { |
| 3265 | $msg_type = ""; |
| 3266 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3267 | # More special cases |
| 3268 | } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || |
| 3269 | $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { |
| 3270 | $msg_type = ""; |
| 3271 | |
| 3272 | # URL ($rawline is used in case the URL is in a comment) |
| 3273 | } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3274 | $msg_type = ""; |
| 3275 | |
| 3276 | # Otherwise set the alternate message types |
| 3277 | |
| 3278 | # a comment starts before $max_line_length |
| 3279 | } elsif ($line =~ /($;[\s$;]*)$/ && |
| 3280 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3281 | $msg_type = "LONG_LINE_COMMENT" |
| 3282 | |
| 3283 | # a quoted string starts before $max_line_length |
| 3284 | } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && |
| 3285 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3286 | $msg_type = "LONG_LINE_STRING" |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3287 | } |
| 3288 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3289 | if ($msg_type ne "" && |
| 3290 | (show_type("LONG_LINE") || show_type($msg_type))) { |
Simon Glass | a33d982 | 2020-05-22 16:32:35 -0600 | [diff] [blame] | 3291 | my $msg_level = \&WARN; |
| 3292 | $msg_level = \&CHK if ($file); |
| 3293 | &{$msg_level}($msg_type, |
| 3294 | "line length of $length exceeds $max_line_length columns\n" . $herecurr); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3295 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | # check for adding lines without a newline. |
| 3299 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
| 3300 | WARN("MISSING_EOF_NEWLINE", |
| 3301 | "adding a line without newline at end of file\n" . $herecurr); |
| 3302 | } |
| 3303 | |
Simon Glass | c8f4d09 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 3304 | if ($u_boot) { |
Simon Glass | f38051e | 2020-07-19 10:16:01 -0600 | [diff] [blame] | 3305 | u_boot_line($realfile, $line, $rawline, $herecurr); |
Simon Glass | c8f4d09 | 2020-05-22 16:32:36 -0600 | [diff] [blame] | 3306 | } |
| 3307 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3308 | # check we are in a valid source file C or perl if not then ignore this hunk |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3309 | next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3310 | |
| 3311 | # at the beginning of a line any tabs must come first and anything |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3312 | # more than $tabsize must use tabs. |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3313 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 3314 | $rawline =~ /^\+\s* \s*/) { |
| 3315 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3316 | $rpt_cleaners = 1; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3317 | if (ERROR("CODE_INDENT", |
| 3318 | "code indent should use tabs where possible\n" . $herevet) && |
| 3319 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3320 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3321 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | # check for space before tabs. |
| 3325 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 3326 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3327 | if (WARN("SPACE_BEFORE_TAB", |
| 3328 | "please, no space before tabs\n" . $herevet) && |
| 3329 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3330 | while ($fixed[$fixlinenr] =~ |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3331 | s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3332 | while ($fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3333 | s/(^\+.*) +\t/$1\t/) {} |
| 3334 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3337 | # check for assignments on the start of a line |
| 3338 | if ($sline =~ /^\+\s+($Assignment)[^=]/) { |
| 3339 | CHK("ASSIGNMENT_CONTINUATIONS", |
| 3340 | "Assignment operator '$1' should be on the previous line\n" . $hereprev); |
| 3341 | } |
| 3342 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3343 | # check for && or || at the start of a line |
| 3344 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
| 3345 | CHK("LOGICAL_CONTINUATIONS", |
| 3346 | "Logical continuations should be on the previous line\n" . $hereprev); |
| 3347 | } |
| 3348 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3349 | # check indentation starts on a tab stop |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3350 | if ($perl_version_ok && |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3351 | $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3352 | my $indent = length($1); |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3353 | if ($indent % $tabsize) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3354 | if (WARN("TABSTOP", |
| 3355 | "Statements should start on a tabstop\n" . $herecurr) && |
| 3356 | $fix) { |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3357 | $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3358 | } |
| 3359 | } |
| 3360 | } |
| 3361 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3362 | # check multi-line statement indentation matches previous line |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3363 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3364 | $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3365 | $prevline =~ /^\+(\t*)(.*)$/; |
| 3366 | my $oldindent = $1; |
| 3367 | my $rest = $2; |
| 3368 | |
| 3369 | my $pos = pos_last_openparen($rest); |
| 3370 | if ($pos >= 0) { |
| 3371 | $line =~ /^(\+| )([ \t]*)/; |
| 3372 | my $newindent = $2; |
| 3373 | |
| 3374 | my $goodtabindent = $oldindent . |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3375 | "\t" x ($pos / $tabsize) . |
| 3376 | " " x ($pos % $tabsize); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3377 | my $goodspaceindent = $oldindent . " " x $pos; |
| 3378 | |
| 3379 | if ($newindent ne $goodtabindent && |
| 3380 | $newindent ne $goodspaceindent) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3381 | |
| 3382 | if (CHK("PARENTHESIS_ALIGNMENT", |
| 3383 | "Alignment should match open parenthesis\n" . $hereprev) && |
| 3384 | $fix && $line =~ /^\+/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3385 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3386 | s/^\+[ \t]*/\+$goodtabindent/; |
| 3387 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3388 | } |
| 3389 | } |
| 3390 | } |
| 3391 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3392 | # check for space after cast like "(int) foo" or "(struct foo) bar" |
| 3393 | # avoid checking a few false positives: |
| 3394 | # "sizeof(<type>)" or "__alignof__(<type>)" |
| 3395 | # function pointer declarations like "(*foo)(int) = bar;" |
| 3396 | # structure definitions like "(struct foo) { 0 };" |
| 3397 | # multiline macros that define functions |
| 3398 | # known attributes or the __attribute__ keyword |
| 3399 | if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && |
| 3400 | (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3401 | if (CHK("SPACING", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3402 | "No space is necessary after a cast\n" . $herecurr) && |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3403 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3404 | $fixed[$fixlinenr] =~ |
| 3405 | s/(\(\s*$Type\s*\))[ \t]+/$1/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3406 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3409 | # Block comment styles |
| 3410 | # Networking with an initial /* |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3411 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3412 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3413 | $rawline =~ /^\+[ \t]*\*/ && |
| 3414 | $realline > 2) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3415 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 3416 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 3417 | } |
| 3418 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3419 | # Block comments use * on subsequent lines |
| 3420 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment |
| 3421 | $prevrawline =~ /^\+.*?\/\*/ && #starting /* |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3422 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
| 3423 | $rawline =~ /^\+/ && #line is new |
| 3424 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3425 | WARN("BLOCK_COMMENT_STYLE", |
| 3426 | "Block comments use * on subsequent lines\n" . $hereprev); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3427 | } |
| 3428 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3429 | # Block comments use */ on trailing lines |
| 3430 | if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3431 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 3432 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 3433 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3434 | WARN("BLOCK_COMMENT_STYLE", |
| 3435 | "Block comments use a trailing */ on a separate line\n" . $herecurr); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3438 | # Block comment * alignment |
| 3439 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment |
| 3440 | $line =~ /^\+[ \t]*$;/ && #leading comment |
| 3441 | $rawline =~ /^\+[ \t]*\*/ && #leading * |
| 3442 | (($prevrawline =~ /^\+.*?\/\*/ && #leading /* |
| 3443 | $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ |
| 3444 | $prevrawline =~ /^\+[ \t]*\*/)) { #leading * |
| 3445 | my $oldindent; |
| 3446 | $prevrawline =~ m@^\+([ \t]*/?)\*@; |
| 3447 | if (defined($1)) { |
| 3448 | $oldindent = expand_tabs($1); |
| 3449 | } else { |
| 3450 | $prevrawline =~ m@^\+(.*/?)\*@; |
| 3451 | $oldindent = expand_tabs($1); |
| 3452 | } |
| 3453 | $rawline =~ m@^\+([ \t]*)\*@; |
| 3454 | my $newindent = $1; |
| 3455 | $newindent = expand_tabs($newindent); |
| 3456 | if (length($oldindent) ne length($newindent)) { |
| 3457 | WARN("BLOCK_COMMENT_STYLE", |
| 3458 | "Block comments should align the * on each line\n" . $hereprev); |
| 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | # check for missing blank lines after struct/union declarations |
| 3463 | # with exceptions for various attributes and macros |
| 3464 | if ($prevline =~ /^[\+ ]};?\s*$/ && |
| 3465 | $line =~ /^\+/ && |
| 3466 | !($line =~ /^\+\s*$/ || |
| 3467 | $line =~ /^\+\s*EXPORT_SYMBOL/ || |
| 3468 | $line =~ /^\+\s*MODULE_/i || |
| 3469 | $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || |
| 3470 | $line =~ /^\+[a-z_]*init/ || |
| 3471 | $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || |
| 3472 | $line =~ /^\+\s*DECLARE/ || |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3473 | $line =~ /^\+\s*builtin_[\w_]*driver/ || |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3474 | $line =~ /^\+\s*__setup/)) { |
| 3475 | if (CHK("LINE_SPACING", |
| 3476 | "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && |
| 3477 | $fix) { |
| 3478 | fix_insert_line($fixlinenr, "\+"); |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | # check for multiple consecutive blank lines |
| 3483 | if ($prevline =~ /^[\+ ]\s*$/ && |
| 3484 | $line =~ /^\+\s*$/ && |
| 3485 | $last_blank_line != ($linenr - 1)) { |
| 3486 | if (CHK("LINE_SPACING", |
| 3487 | "Please don't use multiple blank lines\n" . $hereprev) && |
| 3488 | $fix) { |
| 3489 | fix_delete_line($fixlinenr, $rawline); |
| 3490 | } |
| 3491 | |
| 3492 | $last_blank_line = $linenr; |
| 3493 | } |
| 3494 | |
| 3495 | # check for missing blank lines after declarations |
| 3496 | if ($sline =~ /^\+\s+\S/ && #Not at char 1 |
| 3497 | # actual declarations |
| 3498 | ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
| 3499 | # function pointer declarations |
| 3500 | $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
| 3501 | # foo bar; where foo is some local typedef or #define |
| 3502 | $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
| 3503 | # known declaration macros |
| 3504 | $prevline =~ /^\+\s+$declaration_macros/) && |
| 3505 | # for "else if" which can look like "$Ident $Ident" |
| 3506 | !($prevline =~ /^\+\s+$c90_Keywords\b/ || |
| 3507 | # other possible extensions of declaration lines |
| 3508 | $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || |
| 3509 | # not starting a section or a macro "\" extended line |
| 3510 | $prevline =~ /(?:\{\s*|\\)$/) && |
| 3511 | # looks like a declaration |
| 3512 | !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
| 3513 | # function pointer declarations |
| 3514 | $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
| 3515 | # foo bar; where foo is some local typedef or #define |
| 3516 | $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
| 3517 | # known declaration macros |
| 3518 | $sline =~ /^\+\s+$declaration_macros/ || |
| 3519 | # start of struct or union or enum |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 3520 | $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3521 | # start or end of block or continuation of declaration |
| 3522 | $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || |
| 3523 | # bitfield continuation |
| 3524 | $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || |
| 3525 | # other possible extensions of declaration lines |
| 3526 | $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && |
| 3527 | # indentation of previous and current line are the same |
| 3528 | (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { |
| 3529 | if (WARN("LINE_SPACING", |
| 3530 | "Missing a blank line after declarations\n" . $hereprev) && |
| 3531 | $fix) { |
| 3532 | fix_insert_line($fixlinenr, "\+"); |
| 3533 | } |
| 3534 | } |
| 3535 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3536 | # check for spaces at the beginning of a line. |
| 3537 | # Exceptions: |
| 3538 | # 1) within comments |
| 3539 | # 2) indented preprocessor commands |
| 3540 | # 3) hanging labels |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3541 | if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3542 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3543 | if (WARN("LEADING_SPACE", |
| 3544 | "please, no spaces at the start of a line\n" . $herevet) && |
| 3545 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3546 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3547 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | # check we are in a valid C source file if not then ignore this hunk |
| 3551 | next if ($realfile !~ /\.(h|c)$/); |
| 3552 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 3553 | # check for unusual line ending [ or ( |
| 3554 | if ($line =~ /^\+.*([\[\(])\s*$/) { |
| 3555 | CHK("OPEN_ENDED_LINE", |
| 3556 | "Lines should not end with a '$1'\n" . $herecurr); |
| 3557 | } |
| 3558 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3559 | # check if this appears to be the start function declaration, save the name |
| 3560 | if ($sline =~ /^\+\{\s*$/ && |
| 3561 | $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { |
| 3562 | $context_function = $1; |
| 3563 | } |
| 3564 | |
| 3565 | # check if this appears to be the end of function declaration |
| 3566 | if ($sline =~ /^\+\}\s*$/) { |
| 3567 | undef $context_function; |
| 3568 | } |
| 3569 | |
| 3570 | # check indentation of any line with a bare else |
| 3571 | # (but not if it is a multiple line "if (foo) return bar; else return baz;") |
| 3572 | # if the previous line is a break or return and is indented 1 tab more... |
| 3573 | if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { |
| 3574 | my $tabs = length($1) + 1; |
| 3575 | if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || |
| 3576 | ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && |
| 3577 | defined $lines[$linenr] && |
| 3578 | $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { |
| 3579 | WARN("UNNECESSARY_ELSE", |
| 3580 | "else is not generally useful after a break or return\n" . $hereprev); |
| 3581 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3582 | } |
| 3583 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3584 | # check indentation of a line with a break; |
| 3585 | # if the previous line is a goto or return and is indented the same # of tabs |
| 3586 | if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { |
| 3587 | my $tabs = $1; |
| 3588 | if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { |
| 3589 | WARN("UNNECESSARY_BREAK", |
| 3590 | "break is not useful after a goto or return\n" . $hereprev); |
| 3591 | } |
| 3592 | } |
| 3593 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3594 | # check for RCS/CVS revision markers |
| 3595 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
| 3596 | WARN("CVS_KEYWORD", |
| 3597 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
| 3598 | } |
| 3599 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3600 | # check for old HOTPLUG __dev<foo> section markings |
| 3601 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 3602 | WARN("HOTPLUG_SECTION", |
| 3603 | "Using $1 is unnecessary\n" . $herecurr); |
| 3604 | } |
| 3605 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3606 | # Check for potential 'bare' types |
| 3607 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 3608 | $realline_next); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3609 | #print "LINE<$line>\n"; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3610 | if ($linenr > $suppress_statement && |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3611 | $realcnt && $sline =~ /.\s*\S/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3612 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 3613 | ctx_statement_block($linenr, $realcnt, 0); |
| 3614 | $stat =~ s/\n./\n /g; |
| 3615 | $cond =~ s/\n./\n /g; |
| 3616 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3617 | #print "linenr<$linenr> <$stat>\n"; |
| 3618 | # If this statement has no statement boundaries within |
| 3619 | # it there is no point in retrying a statement scan |
| 3620 | # until we hit end of it. |
| 3621 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 3622 | if ($frag !~ /(?:{|;)/) { |
| 3623 | #print "skip<$line_nr_next>\n"; |
| 3624 | $suppress_statement = $line_nr_next; |
| 3625 | } |
| 3626 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3627 | # Find the real next line. |
| 3628 | $realline_next = $line_nr_next; |
| 3629 | if (defined $realline_next && |
| 3630 | (!defined $lines[$realline_next - 1] || |
| 3631 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 3632 | $realline_next++; |
| 3633 | } |
| 3634 | |
| 3635 | my $s = $stat; |
| 3636 | $s =~ s/{.*$//s; |
| 3637 | |
| 3638 | # Ignore goto labels. |
| 3639 | if ($s =~ /$Ident:\*$/s) { |
| 3640 | |
| 3641 | # Ignore functions being called |
| 3642 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
| 3643 | |
| 3644 | } elsif ($s =~ /^.\s*else\b/s) { |
| 3645 | |
| 3646 | # declarations always start with types |
| 3647 | } 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) { |
| 3648 | my $type = $1; |
| 3649 | $type =~ s/\s+/ /g; |
| 3650 | possible($type, "A:" . $s); |
| 3651 | |
| 3652 | # definitions in global scope can only start with types |
| 3653 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
| 3654 | possible($1, "B:" . $s); |
| 3655 | } |
| 3656 | |
| 3657 | # any (foo ... *) is a pointer cast, and foo is a type |
| 3658 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
| 3659 | possible($1, "C:" . $s); |
| 3660 | } |
| 3661 | |
| 3662 | # Check for any sort of function declaration. |
| 3663 | # int foo(something bar, other baz); |
| 3664 | # void (*store_gdt)(x86_descr_ptr *); |
| 3665 | if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { |
| 3666 | my ($name_len) = length($1); |
| 3667 | |
| 3668 | my $ctx = $s; |
| 3669 | substr($ctx, 0, $name_len + 1, ''); |
| 3670 | $ctx =~ s/\)[^\)]*$//; |
| 3671 | |
| 3672 | for my $arg (split(/\s*,\s*/, $ctx)) { |
| 3673 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { |
| 3674 | |
| 3675 | possible($1, "D:" . $s); |
| 3676 | } |
| 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | } |
| 3681 | |
| 3682 | # |
| 3683 | # Checks which may be anchored in the context. |
| 3684 | # |
| 3685 | |
| 3686 | # Check for switch () and associated case and default |
| 3687 | # statements should be at the same indent. |
| 3688 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 3689 | my $err = ''; |
| 3690 | my $sep = ''; |
| 3691 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 3692 | shift(@ctx); |
| 3693 | for my $ctx (@ctx) { |
| 3694 | my ($clen, $cindent) = line_stats($ctx); |
| 3695 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 3696 | $indent != $cindent) { |
| 3697 | $err .= "$sep$ctx\n"; |
| 3698 | $sep = ''; |
| 3699 | } else { |
| 3700 | $sep = "[...]\n"; |
| 3701 | } |
| 3702 | } |
| 3703 | if ($err ne '') { |
| 3704 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 3705 | "switch and case should be at the same indent\n$hereline$err"); |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 3710 | # or if that brace on the next line is for something else |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3711 | if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3712 | my $pre_ctx = "$1$2"; |
| 3713 | |
| 3714 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3715 | |
| 3716 | if ($line =~ /^\+\t{6,}/) { |
| 3717 | WARN("DEEP_INDENTATION", |
| 3718 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 3719 | } |
| 3720 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3721 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 3722 | my $ctx = join("\n", @ctx); |
| 3723 | |
| 3724 | my $ctx_ln = $linenr; |
| 3725 | my $ctx_skip = $realcnt; |
| 3726 | |
| 3727 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 3728 | defined $lines[$ctx_ln - 1] && |
| 3729 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 3730 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 3731 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
| 3732 | $ctx_ln++; |
| 3733 | } |
| 3734 | |
| 3735 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 3736 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; |
| 3737 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3738 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3739 | ERROR("OPEN_BRACE", |
| 3740 | "that open brace { should be on the previous line\n" . |
| 3741 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
| 3742 | } |
| 3743 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 3744 | $ctx =~ /\)\s*\;\s*$/ && |
| 3745 | defined $lines[$ctx_ln - 1]) |
| 3746 | { |
| 3747 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 3748 | if ($nindent > $indent) { |
| 3749 | WARN("TRAILING_SEMICOLON", |
| 3750 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
| 3751 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
| 3752 | } |
| 3753 | } |
| 3754 | } |
| 3755 | |
| 3756 | # Check relative indent for conditionals and blocks. |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3757 | if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3758 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 3759 | ctx_statement_block($linenr, $realcnt, 0) |
| 3760 | if (!defined $stat); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3761 | my ($s, $c) = ($stat, $cond); |
| 3762 | |
| 3763 | substr($s, 0, length($c), ''); |
| 3764 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3765 | # remove inline comments |
| 3766 | $s =~ s/$;/ /g; |
| 3767 | $c =~ s/$;/ /g; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3768 | |
| 3769 | # Find out how long the conditional actually is. |
| 3770 | my @newlines = ($c =~ /\n/gs); |
| 3771 | my $cond_lines = 1 + $#newlines; |
| 3772 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3773 | # Make sure we remove the line prefixes as we have |
| 3774 | # none on the first line, and are going to readd them |
| 3775 | # where necessary. |
| 3776 | $s =~ s/\n./\n/gs; |
| 3777 | while ($s =~ /\n\s+\\\n/) { |
| 3778 | $cond_lines += $s =~ s/\n\s+\\\n/\n/g; |
| 3779 | } |
| 3780 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3781 | # We want to check the first line inside the block |
| 3782 | # starting at the end of the conditional, so remove: |
| 3783 | # 1) any blank line termination |
| 3784 | # 2) any opening brace { on end of the line |
| 3785 | # 3) any do (...) { |
| 3786 | my $continuation = 0; |
| 3787 | my $check = 0; |
| 3788 | $s =~ s/^.*\bdo\b//; |
| 3789 | $s =~ s/^\s*{//; |
| 3790 | if ($s =~ s/^\s*\\//) { |
| 3791 | $continuation = 1; |
| 3792 | } |
| 3793 | if ($s =~ s/^\s*?\n//) { |
| 3794 | $check = 1; |
| 3795 | $cond_lines++; |
| 3796 | } |
| 3797 | |
| 3798 | # Also ignore a loop construct at the end of a |
| 3799 | # preprocessor statement. |
| 3800 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 3801 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 3802 | $check = 0; |
| 3803 | } |
| 3804 | |
| 3805 | my $cond_ptr = -1; |
| 3806 | $continuation = 0; |
| 3807 | while ($cond_ptr != $cond_lines) { |
| 3808 | $cond_ptr = $cond_lines; |
| 3809 | |
| 3810 | # If we see an #else/#elif then the code |
| 3811 | # is not linear. |
| 3812 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 3813 | $check = 0; |
| 3814 | } |
| 3815 | |
| 3816 | # Ignore: |
| 3817 | # 1) blank lines, they should be at 0, |
| 3818 | # 2) preprocessor lines, and |
| 3819 | # 3) labels. |
| 3820 | if ($continuation || |
| 3821 | $s =~ /^\s*?\n/ || |
| 3822 | $s =~ /^\s*#\s*?/ || |
| 3823 | $s =~ /^\s*$Ident\s*:/) { |
| 3824 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
| 3825 | if ($s =~ s/^.*?\n//) { |
| 3826 | $cond_lines++; |
| 3827 | } |
| 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | my (undef, $sindent) = line_stats("+" . $s); |
| 3832 | my $stat_real = raw_line($linenr, $cond_lines); |
| 3833 | |
| 3834 | # Check if either of these lines are modified, else |
| 3835 | # this is not this patch's fault. |
| 3836 | if (!defined($stat_real) || |
| 3837 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 3838 | $check = 0; |
| 3839 | } |
| 3840 | if (defined($stat_real) && $cond_lines > 1) { |
| 3841 | $stat_real = "[...]\n$stat_real"; |
| 3842 | } |
| 3843 | |
| 3844 | #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"; |
| 3845 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3846 | if ($check && $s ne '' && |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3847 | (($sindent % $tabsize) != 0 || |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3848 | ($sindent < $indent) || |
| 3849 | ($sindent == $indent && |
| 3850 | ($s !~ /^\s*(?:\}|\{|else\b)/)) || |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 3851 | ($sindent > $indent + $tabsize))) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3852 | WARN("SUSPECT_CODE_INDENT", |
| 3853 | "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | # Track the 'values' across context and added lines. |
| 3858 | my $opline = $line; $opline =~ s/^./ /; |
| 3859 | my ($curr_values, $curr_vars) = |
| 3860 | annotate_values($opline . "\n", $prev_values); |
| 3861 | $curr_values = $prev_values . $curr_values; |
| 3862 | if ($dbg_values) { |
| 3863 | my $outline = $opline; $outline =~ s/\t/ /g; |
| 3864 | print "$linenr > .$outline\n"; |
| 3865 | print "$linenr > $curr_values\n"; |
| 3866 | print "$linenr > $curr_vars\n"; |
| 3867 | } |
| 3868 | $prev_values = substr($curr_values, -1); |
| 3869 | |
| 3870 | #ignore lines not being added |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3871 | next if ($line =~ /^[^\+]/); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3872 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3873 | # check for dereferences that span multiple lines |
| 3874 | if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && |
| 3875 | $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { |
| 3876 | $prevline =~ /($Lval\s*(?:\.|->))\s*$/; |
| 3877 | my $ref = $1; |
| 3878 | $line =~ /^.\s*($Lval)/; |
| 3879 | $ref .= $1; |
| 3880 | $ref =~ s/\s//g; |
| 3881 | WARN("MULTILINE_DEREFERENCE", |
| 3882 | "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); |
| 3883 | } |
| 3884 | |
| 3885 | # check for declarations of signed or unsigned without int |
| 3886 | while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { |
| 3887 | my $type = $1; |
| 3888 | my $var = $2; |
| 3889 | $var = "" if (!defined $var); |
| 3890 | if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { |
| 3891 | my $sign = $1; |
| 3892 | my $pointer = $2; |
| 3893 | |
| 3894 | $pointer = "" if (!defined $pointer); |
| 3895 | |
| 3896 | if (WARN("UNSPECIFIED_INT", |
| 3897 | "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && |
| 3898 | $fix) { |
| 3899 | my $decl = trim($sign) . " int "; |
| 3900 | my $comp_pointer = $pointer; |
| 3901 | $comp_pointer =~ s/\s//g; |
| 3902 | $decl .= $comp_pointer; |
| 3903 | $decl = rtrim($decl) if ($var eq ""); |
| 3904 | $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; |
| 3905 | } |
| 3906 | } |
| 3907 | } |
| 3908 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3909 | # TEST: allow direct testing of the type matcher. |
| 3910 | if ($dbg_type) { |
| 3911 | if ($line =~ /^.\s*$Declare\s*$/) { |
| 3912 | ERROR("TEST_TYPE", |
| 3913 | "TEST: is type\n" . $herecurr); |
| 3914 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
| 3915 | ERROR("TEST_NOT_TYPE", |
| 3916 | "TEST: is not type ($1 is)\n". $herecurr); |
| 3917 | } |
| 3918 | next; |
| 3919 | } |
| 3920 | # TEST: allow direct testing of the attribute matcher. |
| 3921 | if ($dbg_attr) { |
| 3922 | if ($line =~ /^.\s*$Modifier\s*$/) { |
| 3923 | ERROR("TEST_ATTR", |
| 3924 | "TEST: is attr\n" . $herecurr); |
| 3925 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
| 3926 | ERROR("TEST_NOT_ATTR", |
| 3927 | "TEST: is not attr ($1 is)\n". $herecurr); |
| 3928 | } |
| 3929 | next; |
| 3930 | } |
| 3931 | |
| 3932 | # check for initialisation to aggregates open brace on the next line |
| 3933 | if ($line =~ /^.\s*{/ && |
| 3934 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3935 | if (ERROR("OPEN_BRACE", |
| 3936 | "that open brace { should be on the previous line\n" . $hereprev) && |
| 3937 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 3938 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 3939 | fix_delete_line($fixlinenr, $rawline); |
| 3940 | my $fixedline = $prevrawline; |
| 3941 | $fixedline =~ s/\s*=\s*$/ = {/; |
| 3942 | fix_insert_line($fixlinenr, $fixedline); |
| 3943 | $fixedline = $line; |
| 3944 | $fixedline =~ s/^(.\s*)\{\s*/$1/; |
| 3945 | fix_insert_line($fixlinenr, $fixedline); |
| 3946 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | # |
| 3950 | # Checks which are anchored on the added line. |
| 3951 | # |
| 3952 | |
| 3953 | # check for malformed paths in #include statements (uses RAW line) |
| 3954 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
| 3955 | my $path = $1; |
| 3956 | if ($path =~ m{//}) { |
| 3957 | ERROR("MALFORMED_INCLUDE", |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3958 | "malformed #include filename\n" . $herecurr); |
| 3959 | } |
| 3960 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 3961 | ERROR("UAPI_INCLUDE", |
| 3962 | "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3963 | } |
| 3964 | } |
| 3965 | |
| 3966 | # no C99 // comments |
| 3967 | if ($line =~ m{//}) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3968 | if (ERROR("C99_COMMENTS", |
| 3969 | "do not use C99 // comments\n" . $herecurr) && |
| 3970 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3971 | my $line = $fixed[$fixlinenr]; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3972 | if ($line =~ /\/\/(.*)$/) { |
| 3973 | my $comment = trim($1); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 3974 | $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 3975 | } |
| 3976 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3977 | } |
| 3978 | # Remove C99 comments. |
| 3979 | $line =~ s@//.*@@; |
| 3980 | $opline =~ s@//.*@@; |
| 3981 | |
| 3982 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 3983 | # the whole statement. |
| 3984 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 3985 | if (defined $realline_next && |
| 3986 | exists $lines[$realline_next - 1] && |
| 3987 | !defined $suppress_export{$realline_next} && |
| 3988 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 3989 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 3990 | # Handle definitions which produce identifiers with |
| 3991 | # a prefix: |
| 3992 | # XXX(foo); |
| 3993 | # EXPORT_SYMBOL(something_foo); |
| 3994 | my $name = $1; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 3995 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 3996 | $name =~ /^${Ident}_$2/) { |
| 3997 | #print "FOO C name<$name>\n"; |
| 3998 | $suppress_export{$realline_next} = 1; |
| 3999 | |
| 4000 | } elsif ($stat !~ /(?: |
| 4001 | \n.}\s*$| |
| 4002 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 4003 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 4004 | ^.LIST_HEAD\(\Q$name\E\)| |
| 4005 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 4006 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
| 4007 | )/x) { |
| 4008 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 4009 | $suppress_export{$realline_next} = 2; |
| 4010 | } else { |
| 4011 | $suppress_export{$realline_next} = 1; |
| 4012 | } |
| 4013 | } |
| 4014 | if (!defined $suppress_export{$linenr} && |
| 4015 | $prevline =~ /^.\s*$/ && |
| 4016 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 4017 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 4018 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 4019 | $suppress_export{$linenr} = 2; |
| 4020 | } |
| 4021 | if (defined $suppress_export{$linenr} && |
| 4022 | $suppress_export{$linenr} == 2) { |
| 4023 | WARN("EXPORT_SYMBOL", |
| 4024 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
| 4025 | } |
| 4026 | |
| 4027 | # check for global initialisers. |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4028 | if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4029 | if (ERROR("GLOBAL_INITIALISERS", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4030 | "do not initialise globals to $1\n" . $herecurr) && |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4031 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4032 | $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4033 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4034 | } |
| 4035 | # check for static initialisers. |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4036 | if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4037 | if (ERROR("INITIALISED_STATIC", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4038 | "do not initialise statics to $1\n" . |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4039 | $herecurr) && |
| 4040 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4041 | $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4042 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4045 | # check for misordered declarations of char/short/int/long with signed/unsigned |
| 4046 | while ($sline =~ m{(\b$TypeMisordered\b)}g) { |
| 4047 | my $tmp = trim($1); |
| 4048 | WARN("MISORDERED_TYPE", |
| 4049 | "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); |
| 4050 | } |
| 4051 | |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4052 | # check for unnecessary <signed> int declarations of short/long/long long |
| 4053 | while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { |
| 4054 | my $type = trim($1); |
| 4055 | next if ($type !~ /\bint\b/); |
| 4056 | next if ($type !~ /\b(?:short|long\s+long|long)\b/); |
| 4057 | my $new_type = $type; |
| 4058 | $new_type =~ s/\b\s*int\s*\b/ /; |
| 4059 | $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; |
| 4060 | $new_type =~ s/^const\s+//; |
| 4061 | $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); |
| 4062 | $new_type = "const $new_type" if ($type =~ /^const\b/); |
| 4063 | $new_type =~ s/\s+/ /g; |
| 4064 | $new_type = trim($new_type); |
| 4065 | if (WARN("UNNECESSARY_INT", |
| 4066 | "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && |
| 4067 | $fix) { |
| 4068 | $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; |
| 4069 | } |
| 4070 | } |
| 4071 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4072 | # check for static const char * arrays. |
| 4073 | if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { |
| 4074 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 4075 | "static const char * array should probably be static const char * const\n" . |
| 4076 | $herecurr); |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | # check for initialized const char arrays that should be static const |
| 4080 | if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { |
| 4081 | if (WARN("STATIC_CONST_CHAR_ARRAY", |
| 4082 | "const array should probably be static const\n" . $herecurr) && |
| 4083 | $fix) { |
| 4084 | $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; |
| 4085 | } |
| 4086 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4087 | |
| 4088 | # check for static char foo[] = "bar" declarations. |
| 4089 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
| 4090 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 4091 | "static char array declaration should probably be static const char\n" . |
| 4092 | $herecurr); |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4093 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4094 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4095 | # check for const <foo> const where <foo> is not a pointer or array type |
| 4096 | if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { |
| 4097 | my $found = $1; |
| 4098 | if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { |
| 4099 | WARN("CONST_CONST", |
| 4100 | "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); |
| 4101 | } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { |
| 4102 | WARN("CONST_CONST", |
| 4103 | "'const $found const' should probably be 'const $found'\n" . $herecurr); |
| 4104 | } |
| 4105 | } |
| 4106 | |
| 4107 | # check for non-global char *foo[] = {"bar", ...} declarations. |
| 4108 | if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { |
| 4109 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 4110 | "char * array declaration might be better as static const\n" . |
| 4111 | $herecurr); |
| 4112 | } |
| 4113 | |
| 4114 | # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) |
| 4115 | if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { |
| 4116 | my $array = $1; |
| 4117 | if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) { |
| 4118 | my $array_div = $1; |
| 4119 | if (WARN("ARRAY_SIZE", |
| 4120 | "Prefer ARRAY_SIZE($array)\n" . $herecurr) && |
| 4121 | $fix) { |
| 4122 | $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; |
| 4123 | } |
| 4124 | } |
| 4125 | } |
| 4126 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4127 | # check for function declarations without arguments like "int foo()" |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4128 | if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4129 | if (ERROR("FUNCTION_WITHOUT_ARGS", |
| 4130 | "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && |
| 4131 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4132 | $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4133 | } |
| 4134 | } |
| 4135 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4136 | # check for new typedefs, only function parameters and sparse annotations |
| 4137 | # make sense. |
| 4138 | if ($line =~ /\btypedef\s/ && |
| 4139 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
| 4140 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
| 4141 | $line !~ /\b$typeTypedefs\b/ && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4142 | $line !~ /\b__bitwise\b/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4143 | WARN("NEW_TYPEDEFS", |
| 4144 | "do not add new typedefs\n" . $herecurr); |
| 4145 | } |
| 4146 | |
| 4147 | # * goes on variable not on type |
| 4148 | # (char*[ const]) |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4149 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 4150 | #print "AA<$1>\n"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4151 | my ($ident, $from, $to) = ($1, $2, $2); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4152 | |
| 4153 | # Should start with a space. |
| 4154 | $to =~ s/^(\S)/ $1/; |
| 4155 | # Should not end with a space. |
| 4156 | $to =~ s/\s+$//; |
| 4157 | # '*'s should not have spaces between. |
| 4158 | while ($to =~ s/\*\s+\*/\*\*/) { |
| 4159 | } |
| 4160 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4161 | ## print "1: from<$from> to<$to> ident<$ident>\n"; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4162 | if ($from ne $to) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4163 | if (ERROR("POINTER_LOCATION", |
| 4164 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && |
| 4165 | $fix) { |
| 4166 | my $sub_from = $ident; |
| 4167 | my $sub_to = $ident; |
| 4168 | $sub_to =~ s/\Q$from\E/$to/; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4169 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4170 | s@\Q$sub_from\E@$sub_to@; |
| 4171 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4172 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4173 | } |
| 4174 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 4175 | #print "BB<$1>\n"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4176 | my ($match, $from, $to, $ident) = ($1, $2, $2, $3); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4177 | |
| 4178 | # Should start with a space. |
| 4179 | $to =~ s/^(\S)/ $1/; |
| 4180 | # Should not end with a space. |
| 4181 | $to =~ s/\s+$//; |
| 4182 | # '*'s should not have spaces between. |
| 4183 | while ($to =~ s/\*\s+\*/\*\*/) { |
| 4184 | } |
| 4185 | # Modifiers should have spaces. |
| 4186 | $to =~ s/(\b$Modifier$)/$1 /; |
| 4187 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4188 | ## print "2: from<$from> to<$to> ident<$ident>\n"; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4189 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4190 | if (ERROR("POINTER_LOCATION", |
| 4191 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && |
| 4192 | $fix) { |
| 4193 | |
| 4194 | my $sub_from = $match; |
| 4195 | my $sub_to = $match; |
| 4196 | $sub_to =~ s/\Q$from\E/$to/; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4197 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4198 | s@\Q$sub_from\E@$sub_to@; |
| 4199 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4200 | } |
| 4201 | } |
| 4202 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4203 | # avoid BUG() or BUG_ON() |
| 4204 | if ($line =~ /\b(?:BUG|BUG_ON)\b/) { |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4205 | my $msg_level = \&WARN; |
| 4206 | $msg_level = \&CHK if ($file); |
| 4207 | &{$msg_level}("AVOID_BUG", |
| 4208 | "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4209 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4210 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4211 | # avoid LINUX_VERSION_CODE |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4212 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
| 4213 | WARN("LINUX_VERSION_CODE", |
| 4214 | "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
| 4215 | } |
| 4216 | |
| 4217 | # check for uses of printk_ratelimit |
| 4218 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
| 4219 | WARN("PRINTK_RATELIMITED", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4220 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4223 | # printk should use KERN_* levels |
| 4224 | if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { |
| 4225 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 4226 | "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4229 | if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { |
| 4230 | my $orig = $1; |
| 4231 | my $level = lc($orig); |
| 4232 | $level = "warn" if ($level eq "warning"); |
| 4233 | my $level2 = $level; |
| 4234 | $level2 = "dbg" if ($level eq "debug"); |
| 4235 | WARN("PREFER_PR_LEVEL", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4236 | "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4239 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 4240 | my $orig = $1; |
| 4241 | my $level = lc($orig); |
| 4242 | $level = "warn" if ($level eq "warning"); |
| 4243 | $level = "dbg" if ($level eq "debug"); |
| 4244 | WARN("PREFER_DEV_LEVEL", |
| 4245 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 4246 | } |
| 4247 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4248 | # ENOSYS means "bad syscall nr" and nothing else. This will have a small |
| 4249 | # number of false positives, but assembly files are not checked, so at |
| 4250 | # least the arch entry code will not trigger this warning. |
| 4251 | if ($line =~ /\bENOSYS\b/) { |
| 4252 | WARN("ENOSYS", |
| 4253 | "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); |
| 4254 | } |
| 4255 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4256 | # function brace can't be on same line, except for #defines of do while, |
| 4257 | # or if closed on same line |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4258 | if ($perl_version_ok && |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4259 | $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && |
| 4260 | $sline !~ /\#\s*define\b.*do\s*\{/ && |
| 4261 | $sline !~ /}/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4262 | if (ERROR("OPEN_BRACE", |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4263 | "open brace '{' following function definitions go on the next line\n" . $herecurr) && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4264 | $fix) { |
| 4265 | fix_delete_line($fixlinenr, $rawline); |
| 4266 | my $fixed_line = $rawline; |
| 4267 | $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; |
| 4268 | my $line1 = $1; |
| 4269 | my $line2 = $2; |
| 4270 | fix_insert_line($fixlinenr, ltrim($line1)); |
| 4271 | fix_insert_line($fixlinenr, "\+{"); |
| 4272 | if ($line2 !~ /^\s*$/) { |
| 4273 | fix_insert_line($fixlinenr, "\+\t" . trim($line2)); |
| 4274 | } |
| 4275 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | # open braces for enum, union and struct go on the same line. |
| 4279 | if ($line =~ /^.\s*{/ && |
| 4280 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4281 | if (ERROR("OPEN_BRACE", |
| 4282 | "open brace '{' following $1 go on the same line\n" . $hereprev) && |
| 4283 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4284 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4285 | fix_delete_line($fixlinenr, $rawline); |
| 4286 | my $fixedline = rtrim($prevrawline) . " {"; |
| 4287 | fix_insert_line($fixlinenr, $fixedline); |
| 4288 | $fixedline = $rawline; |
| 4289 | $fixedline =~ s/^(.\s*)\{\s*/$1\t/; |
| 4290 | if ($fixedline !~ /^\+\s*$/) { |
| 4291 | fix_insert_line($fixlinenr, $fixedline); |
| 4292 | } |
| 4293 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | # missing space after union, struct or enum definition |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4297 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { |
| 4298 | if (WARN("SPACING", |
| 4299 | "missing space after $1 definition\n" . $herecurr) && |
| 4300 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4301 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4302 | s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | # Function pointer declarations |
| 4307 | # check spacing between type, funcptr, and args |
| 4308 | # canonical declaration is "type (*funcptr)(args...)" |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4309 | if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4310 | my $declare = $1; |
| 4311 | my $pre_pointer_space = $2; |
| 4312 | my $post_pointer_space = $3; |
| 4313 | my $funcname = $4; |
| 4314 | my $post_funcname_space = $5; |
| 4315 | my $pre_args_space = $6; |
| 4316 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4317 | # the $Declare variable will capture all spaces after the type |
| 4318 | # so check it for a missing trailing missing space but pointer return types |
| 4319 | # don't need a space so don't warn for those. |
| 4320 | my $post_declare_space = ""; |
| 4321 | if ($declare =~ /(\s+)$/) { |
| 4322 | $post_declare_space = $1; |
| 4323 | $declare = rtrim($declare); |
| 4324 | } |
| 4325 | if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4326 | WARN("SPACING", |
| 4327 | "missing space after return type\n" . $herecurr); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4328 | $post_declare_space = " "; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | # unnecessary space "type (*funcptr)(args...)" |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4332 | # This test is not currently implemented because these declarations are |
| 4333 | # equivalent to |
| 4334 | # int foo(int bar, ...) |
| 4335 | # and this is form shouldn't/doesn't generate a checkpatch warning. |
| 4336 | # |
| 4337 | # elsif ($declare =~ /\s{2,}$/) { |
| 4338 | # WARN("SPACING", |
| 4339 | # "Multiple spaces after return type\n" . $herecurr); |
| 4340 | # } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4341 | |
| 4342 | # unnecessary space "type ( *funcptr)(args...)" |
| 4343 | if (defined $pre_pointer_space && |
| 4344 | $pre_pointer_space =~ /^\s/) { |
| 4345 | WARN("SPACING", |
| 4346 | "Unnecessary space after function pointer open parenthesis\n" . $herecurr); |
| 4347 | } |
| 4348 | |
| 4349 | # unnecessary space "type (* funcptr)(args...)" |
| 4350 | if (defined $post_pointer_space && |
| 4351 | $post_pointer_space =~ /^\s/) { |
| 4352 | WARN("SPACING", |
| 4353 | "Unnecessary space before function pointer name\n" . $herecurr); |
| 4354 | } |
| 4355 | |
| 4356 | # unnecessary space "type (*funcptr )(args...)" |
| 4357 | if (defined $post_funcname_space && |
| 4358 | $post_funcname_space =~ /^\s/) { |
| 4359 | WARN("SPACING", |
| 4360 | "Unnecessary space after function pointer name\n" . $herecurr); |
| 4361 | } |
| 4362 | |
| 4363 | # unnecessary space "type (*funcptr) (args...)" |
| 4364 | if (defined $pre_args_space && |
| 4365 | $pre_args_space =~ /^\s/) { |
| 4366 | WARN("SPACING", |
| 4367 | "Unnecessary space before function pointer arguments\n" . $herecurr); |
| 4368 | } |
| 4369 | |
| 4370 | if (show_type("SPACING") && $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4371 | $fixed[$fixlinenr] =~ |
| 4372 | s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4373 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | # check for spacing round square brackets; allowed: |
| 4377 | # 1. with a type on the left -- int [] a; |
| 4378 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 4379 | # 3. inside a curly brace -- = { [0...10] = 5 } |
| 4380 | while ($line =~ /(.*?\s)\[/g) { |
| 4381 | my ($where, $prefix) = ($-[1], $1); |
| 4382 | if ($prefix !~ /$Type\s+$/ && |
| 4383 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4384 | $prefix !~ /[{,:]\s+$/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4385 | if (ERROR("BRACKET_SPACE", |
| 4386 | "space prohibited before open square bracket '['\n" . $herecurr) && |
| 4387 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4388 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4389 | s/^(\+.*?)\s+\[/$1\[/; |
| 4390 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | # check for spaces between functions and their parentheses. |
| 4395 | while ($line =~ /($Ident)\s+\(/g) { |
| 4396 | my $name = $1; |
| 4397 | my $ctx_before = substr($line, 0, $-[1]); |
| 4398 | my $ctx = "$ctx_before$name"; |
| 4399 | |
| 4400 | # Ignore those directives where spaces _are_ permitted. |
| 4401 | if ($name =~ /^(?: |
| 4402 | if|for|while|switch|return|case| |
| 4403 | volatile|__volatile__| |
| 4404 | __attribute__|format|__extension__| |
| 4405 | asm|__asm__)$/x) |
| 4406 | { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4407 | # cpp #define statements have non-optional spaces, ie |
| 4408 | # if there is a space between the name and the open |
| 4409 | # parenthesis it is simply not a parameter group. |
| 4410 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
| 4411 | |
| 4412 | # cpp #elif statement condition may start with a ( |
| 4413 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
| 4414 | |
| 4415 | # If this whole things ends with a type its most |
| 4416 | # likely a typedef for a function. |
| 4417 | } elsif ($ctx =~ /$Type$/) { |
| 4418 | |
| 4419 | } else { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4420 | if (WARN("SPACING", |
| 4421 | "space prohibited between function name and open parenthesis '('\n" . $herecurr) && |
| 4422 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4423 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4424 | s/\b$name\s+\(/$name\(/; |
| 4425 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4426 | } |
| 4427 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4428 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4429 | # Check operator spacing. |
| 4430 | if (!($line=~/\#\s*include/)) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4431 | my $fixed_line = ""; |
| 4432 | my $line_fixed = 0; |
| 4433 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4434 | my $ops = qr{ |
| 4435 | <<=|>>=|<=|>=|==|!=| |
| 4436 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 4437 | =>|->|<<|>>|<|>|=|!|~| |
| 4438 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4439 | \?:|\?|: |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4440 | }x; |
| 4441 | my @elements = split(/($ops|;)/, $opline); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4442 | |
| 4443 | ## print("element count: <" . $#elements . ">\n"); |
| 4444 | ## foreach my $el (@elements) { |
| 4445 | ## print("el: <$el>\n"); |
| 4446 | ## } |
| 4447 | |
| 4448 | my @fix_elements = (); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4449 | my $off = 0; |
| 4450 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4451 | foreach my $el (@elements) { |
| 4452 | push(@fix_elements, substr($rawline, $off, length($el))); |
| 4453 | $off += length($el); |
| 4454 | } |
| 4455 | |
| 4456 | $off = 0; |
| 4457 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4458 | my $blank = copy_spacing($opline); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4459 | my $last_after = -1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4460 | |
| 4461 | for (my $n = 0; $n < $#elements; $n += 2) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4462 | |
| 4463 | my $good = $fix_elements[$n] . $fix_elements[$n + 1]; |
| 4464 | |
| 4465 | ## print("n: <$n> good: <$good>\n"); |
| 4466 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4467 | $off += length($elements[$n]); |
| 4468 | |
| 4469 | # Pick up the preceding and succeeding characters. |
| 4470 | my $ca = substr($opline, 0, $off); |
| 4471 | my $cc = ''; |
| 4472 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 4473 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 4474 | } |
| 4475 | my $cb = "$ca$;$cc"; |
| 4476 | |
| 4477 | my $a = ''; |
| 4478 | $a = 'V' if ($elements[$n] ne ''); |
| 4479 | $a = 'W' if ($elements[$n] =~ /\s$/); |
| 4480 | $a = 'C' if ($elements[$n] =~ /$;$/); |
| 4481 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 4482 | $a = 'O' if ($elements[$n] eq ''); |
| 4483 | $a = 'E' if ($ca =~ /^\s*$/); |
| 4484 | |
| 4485 | my $op = $elements[$n + 1]; |
| 4486 | |
| 4487 | my $c = ''; |
| 4488 | if (defined $elements[$n + 2]) { |
| 4489 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 4490 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
| 4491 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
| 4492 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 4493 | $c = 'O' if ($elements[$n + 2] eq ''); |
| 4494 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
| 4495 | } else { |
| 4496 | $c = 'E'; |
| 4497 | } |
| 4498 | |
| 4499 | my $ctx = "${a}x${c}"; |
| 4500 | |
| 4501 | my $at = "(ctx:$ctx)"; |
| 4502 | |
| 4503 | my $ptr = substr($blank, 0, $off) . "^"; |
| 4504 | my $hereptr = "$hereline$ptr\n"; |
| 4505 | |
| 4506 | # Pull out the value of this operator. |
| 4507 | my $op_type = substr($curr_values, $off + 1, 1); |
| 4508 | |
| 4509 | # Get the full operator variant. |
| 4510 | my $opv = $op . substr($curr_vars, $off, 1); |
| 4511 | |
| 4512 | # Ignore operators passed as parameters. |
| 4513 | if ($op_type ne 'V' && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4514 | $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4515 | |
| 4516 | # # Ignore comments |
| 4517 | # } elsif ($op =~ /^$;+$/) { |
| 4518 | |
| 4519 | # ; should have either the end of line or a space or \ after it |
| 4520 | } elsif ($op eq ';') { |
| 4521 | if ($ctx !~ /.x[WEBC]/ && |
| 4522 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4523 | if (ERROR("SPACING", |
| 4524 | "space required after that '$op' $at\n" . $hereptr)) { |
| 4525 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
| 4526 | $line_fixed = 1; |
| 4527 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | # // is a comment |
| 4531 | } elsif ($op eq '//') { |
| 4532 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4533 | # : when part of a bitfield |
| 4534 | } elsif ($opv eq ':B') { |
| 4535 | # skip the bitfield test for now |
| 4536 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4537 | # No spaces for: |
| 4538 | # -> |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4539 | } elsif ($op eq '->') { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4540 | if ($ctx =~ /Wx.|.xW/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4541 | if (ERROR("SPACING", |
| 4542 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
| 4543 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 4544 | if (defined $fix_elements[$n + 2]) { |
| 4545 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4546 | } |
| 4547 | $line_fixed = 1; |
| 4548 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4551 | # , must not have a space before and must have a space on the right. |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4552 | } elsif ($op eq ',') { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4553 | my $rtrim_before = 0; |
| 4554 | my $space_after = 0; |
| 4555 | if ($ctx =~ /Wx./) { |
| 4556 | if (ERROR("SPACING", |
| 4557 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 4558 | $line_fixed = 1; |
| 4559 | $rtrim_before = 1; |
| 4560 | } |
| 4561 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4562 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4563 | if (ERROR("SPACING", |
| 4564 | "space required after that '$op' $at\n" . $hereptr)) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4565 | $line_fixed = 1; |
| 4566 | $last_after = $n; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4567 | $space_after = 1; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4568 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4569 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4570 | if ($rtrim_before || $space_after) { |
| 4571 | if ($rtrim_before) { |
| 4572 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 4573 | } else { |
| 4574 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
| 4575 | } |
| 4576 | if ($space_after) { |
| 4577 | $good .= " "; |
| 4578 | } |
| 4579 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4580 | |
| 4581 | # '*' as part of a type definition -- reported already. |
| 4582 | } elsif ($opv eq '*_') { |
| 4583 | #warn "'*' is part of type\n"; |
| 4584 | |
| 4585 | # unary operators should have a space before and |
| 4586 | # none after. May be left adjacent to another |
| 4587 | # unary operator, or a cast |
| 4588 | } elsif ($op eq '!' || $op eq '~' || |
| 4589 | $opv eq '*U' || $opv eq '-U' || |
| 4590 | $opv eq '&U' || $opv eq '&&U') { |
| 4591 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4592 | if (ERROR("SPACING", |
| 4593 | "space required before that '$op' $at\n" . $hereptr)) { |
| 4594 | if ($n != $last_after + 2) { |
| 4595 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
| 4596 | $line_fixed = 1; |
| 4597 | } |
| 4598 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4599 | } |
| 4600 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
| 4601 | # A unary '*' may be const |
| 4602 | |
| 4603 | } elsif ($ctx =~ /.xW/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4604 | if (ERROR("SPACING", |
| 4605 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
| 4606 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
| 4607 | if (defined $fix_elements[$n + 2]) { |
| 4608 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4609 | } |
| 4610 | $line_fixed = 1; |
| 4611 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | # unary ++ and unary -- are allowed no space on one side. |
| 4615 | } elsif ($op eq '++' or $op eq '--') { |
| 4616 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4617 | if (ERROR("SPACING", |
| 4618 | "space required one side of that '$op' $at\n" . $hereptr)) { |
| 4619 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
| 4620 | $line_fixed = 1; |
| 4621 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4622 | } |
| 4623 | if ($ctx =~ /Wx[BE]/ || |
| 4624 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4625 | if (ERROR("SPACING", |
| 4626 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 4627 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 4628 | $line_fixed = 1; |
| 4629 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4630 | } |
| 4631 | if ($ctx =~ /ExW/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4632 | if (ERROR("SPACING", |
| 4633 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
| 4634 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
| 4635 | if (defined $fix_elements[$n + 2]) { |
| 4636 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4637 | } |
| 4638 | $line_fixed = 1; |
| 4639 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4640 | } |
| 4641 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4642 | # << and >> may either have or not have spaces both sides |
| 4643 | } elsif ($op eq '<<' or $op eq '>>' or |
| 4644 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 4645 | $op eq '+' or $op eq '-' or |
| 4646 | $op eq '*' or $op eq '/' or |
| 4647 | $op eq '%') |
| 4648 | { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4649 | if ($check) { |
| 4650 | if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { |
| 4651 | if (CHK("SPACING", |
| 4652 | "spaces preferred around that '$op' $at\n" . $hereptr)) { |
| 4653 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 4654 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4655 | $line_fixed = 1; |
| 4656 | } |
| 4657 | } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { |
| 4658 | if (CHK("SPACING", |
| 4659 | "space preferred before that '$op' $at\n" . $hereptr)) { |
| 4660 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); |
| 4661 | $line_fixed = 1; |
| 4662 | } |
| 4663 | } |
| 4664 | } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4665 | if (ERROR("SPACING", |
| 4666 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
| 4667 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 4668 | if (defined $fix_elements[$n + 2]) { |
| 4669 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4670 | } |
| 4671 | $line_fixed = 1; |
| 4672 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | # A colon needs no spaces before when it is |
| 4676 | # terminating a case value or a label. |
| 4677 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 4678 | if ($ctx =~ /Wx./) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4679 | if (ERROR("SPACING", |
| 4680 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 4681 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 4682 | $line_fixed = 1; |
| 4683 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | # All the others need spaces both sides. |
| 4687 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
| 4688 | my $ok = 0; |
| 4689 | |
| 4690 | # Ignore email addresses <foo@bar> |
| 4691 | if (($op eq '<' && |
| 4692 | $cc =~ /^\S+\@\S+>/) || |
| 4693 | ($op eq '>' && |
| 4694 | $ca =~ /<\S+\@\S+$/)) |
| 4695 | { |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4696 | $ok = 1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4699 | # for asm volatile statements |
| 4700 | # ignore a colon with another |
| 4701 | # colon immediately before or after |
| 4702 | if (($op eq ':') && |
| 4703 | ($ca =~ /:$/ || $cc =~ /^:/)) { |
| 4704 | $ok = 1; |
| 4705 | } |
| 4706 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4707 | # messages are ERROR, but ?: are CHK |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4708 | if ($ok == 0) { |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4709 | my $msg_level = \&ERROR; |
| 4710 | $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4711 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4712 | if (&{$msg_level}("SPACING", |
| 4713 | "spaces required around that '$op' $at\n" . $hereptr)) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4714 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 4715 | if (defined $fix_elements[$n + 2]) { |
| 4716 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4717 | } |
| 4718 | $line_fixed = 1; |
| 4719 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4720 | } |
| 4721 | } |
| 4722 | $off += length($elements[$n + 1]); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4723 | |
| 4724 | ## print("n: <$n> GOOD: <$good>\n"); |
| 4725 | |
| 4726 | $fixed_line = $fixed_line . $good; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4727 | } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4728 | |
| 4729 | if (($#elements % 2) == 0) { |
| 4730 | $fixed_line = $fixed_line . $fix_elements[$#elements]; |
| 4731 | } |
| 4732 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4733 | if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { |
| 4734 | $fixed[$fixlinenr] = $fixed_line; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4740 | # check for whitespace before a non-naked semicolon |
| 4741 | if ($line =~ /^\+.*\S\s+;\s*$/) { |
| 4742 | if (WARN("SPACING", |
| 4743 | "space prohibited before semicolon\n" . $herecurr) && |
| 4744 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4745 | 1 while $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4746 | s/^(\+.*\S)\s+;/$1;/; |
| 4747 | } |
| 4748 | } |
| 4749 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4750 | # check for multiple assignments |
| 4751 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
| 4752 | CHK("MULTIPLE_ASSIGNMENTS", |
| 4753 | "multiple assignments should be avoided\n" . $herecurr); |
| 4754 | } |
| 4755 | |
| 4756 | ## # check for multiple declarations, allowing for a function declaration |
| 4757 | ## # continuation. |
| 4758 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 4759 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 4760 | ## |
| 4761 | ## # Remove any bracketed sections to ensure we do not |
| 4762 | ## # falsly report the parameters of functions. |
| 4763 | ## my $ln = $line; |
| 4764 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 4765 | ## } |
| 4766 | ## if ($ln =~ /,/) { |
| 4767 | ## WARN("MULTIPLE_DECLARATION", |
| 4768 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
| 4769 | ## } |
| 4770 | ## } |
| 4771 | |
| 4772 | #need space before brace following if, while, etc |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4773 | if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4774 | $line =~ /\b(?:else|do)\{/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4775 | if (ERROR("SPACING", |
| 4776 | "space required before the open brace '{'\n" . $herecurr) && |
| 4777 | $fix) { |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4778 | $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4779 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4782 | ## # check for blank lines before declarations |
| 4783 | ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && |
| 4784 | ## $prevrawline =~ /^.\s*$/) { |
| 4785 | ## WARN("SPACING", |
| 4786 | ## "No blank lines before declarations\n" . $hereprev); |
| 4787 | ## } |
| 4788 | ## |
| 4789 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4790 | # closing brace should have a space following it when it has anything |
| 4791 | # on the line |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 4792 | if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4793 | if (ERROR("SPACING", |
| 4794 | "space required after that close brace '}'\n" . $herecurr) && |
| 4795 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4796 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4797 | s/}((?!(?:,|;|\)))\S)/} $1/; |
| 4798 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | # check spacing on square brackets |
| 4802 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4803 | if (ERROR("SPACING", |
| 4804 | "space prohibited after that open square bracket '['\n" . $herecurr) && |
| 4805 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4806 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4807 | s/\[\s+/\[/; |
| 4808 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4809 | } |
| 4810 | if ($line =~ /\s\]/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4811 | if (ERROR("SPACING", |
| 4812 | "space prohibited before that close square bracket ']'\n" . $herecurr) && |
| 4813 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4814 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4815 | s/\s+\]/\]/; |
| 4816 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
| 4819 | # check spacing on parentheses |
| 4820 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 4821 | $line !~ /for\s*\(\s+;/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4822 | if (ERROR("SPACING", |
| 4823 | "space prohibited after that open parenthesis '('\n" . $herecurr) && |
| 4824 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4825 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4826 | s/\(\s+/\(/; |
| 4827 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4828 | } |
| 4829 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
| 4830 | $line !~ /for\s*\(.*;\s+\)/ && |
| 4831 | $line !~ /:\s+\)/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4832 | if (ERROR("SPACING", |
| 4833 | "space prohibited before that close parenthesis ')'\n" . $herecurr) && |
| 4834 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4835 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4836 | s/\s+\)/\)/; |
| 4837 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4840 | # check unnecessary parentheses around addressof/dereference single $Lvals |
| 4841 | # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar |
| 4842 | |
| 4843 | while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { |
| 4844 | my $var = $1; |
| 4845 | if (CHK("UNNECESSARY_PARENTHESES", |
| 4846 | "Unnecessary parentheses around $var\n" . $herecurr) && |
| 4847 | $fix) { |
| 4848 | $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; |
| 4849 | } |
| 4850 | } |
| 4851 | |
| 4852 | # check for unnecessary parentheses around function pointer uses |
| 4853 | # ie: (foo->bar)(); should be foo->bar(); |
| 4854 | # but not "if (foo->bar) (" to avoid some false positives |
| 4855 | if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { |
| 4856 | my $var = $2; |
| 4857 | if (CHK("UNNECESSARY_PARENTHESES", |
| 4858 | "Unnecessary parentheses around function pointer $var\n" . $herecurr) && |
| 4859 | $fix) { |
| 4860 | my $var2 = deparenthesize($var); |
| 4861 | $var2 =~ s/\s//g; |
| 4862 | $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; |
| 4863 | } |
| 4864 | } |
| 4865 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4866 | # check for unnecessary parentheses around comparisons in if uses |
| 4867 | # when !drivers/staging or command-line uses --strict |
| 4868 | if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4869 | $perl_version_ok && defined($stat) && |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 4870 | $stat =~ /(^.\s*if\s*($balanced_parens))/) { |
| 4871 | my $if_stat = $1; |
| 4872 | my $test = substr($2, 1, -1); |
| 4873 | my $herectx; |
| 4874 | while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { |
| 4875 | my $match = $1; |
| 4876 | # avoid parentheses around potential macro args |
| 4877 | next if ($match =~ /^\s*\w+\s*$/); |
| 4878 | if (!defined($herectx)) { |
| 4879 | $herectx = $here . "\n"; |
| 4880 | my $cnt = statement_rawlines($if_stat); |
| 4881 | for (my $n = 0; $n < $cnt; $n++) { |
| 4882 | my $rl = raw_line($linenr, $n); |
| 4883 | $herectx .= $rl . "\n"; |
| 4884 | last if $rl =~ /^[ \+].*\{/; |
| 4885 | } |
| 4886 | } |
| 4887 | CHK("UNNECESSARY_PARENTHESES", |
| 4888 | "Unnecessary parentheses around '$match'\n" . $herectx); |
| 4889 | } |
| 4890 | } |
| 4891 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4892 | #goto labels aren't indented, allow a single space however |
| 4893 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
| 4894 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4895 | if (WARN("INDENTED_LABEL", |
| 4896 | "labels should not be indented\n" . $herecurr) && |
| 4897 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4898 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4899 | s/^(.)\s+/$1/; |
| 4900 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4903 | # return is not a function |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4904 | if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4905 | my $spacing = $1; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4906 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4907 | $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { |
| 4908 | my $value = $1; |
| 4909 | $value = deparenthesize($value); |
| 4910 | if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { |
| 4911 | ERROR("RETURN_PARENTHESES", |
| 4912 | "return is not a function, parentheses are not required\n" . $herecurr); |
| 4913 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4914 | } elsif ($spacing !~ /\s+/) { |
| 4915 | ERROR("SPACING", |
| 4916 | "space required before the open parenthesis '('\n" . $herecurr); |
| 4917 | } |
| 4918 | } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4919 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4920 | # unnecessary return in a void function |
| 4921 | # at end-of-function, with the previous line a single leading tab, then return; |
| 4922 | # and the line before that not a goto label target like "out:" |
| 4923 | if ($sline =~ /^[ \+]}\s*$/ && |
| 4924 | $prevline =~ /^\+\treturn\s*;\s*$/ && |
| 4925 | $linenr >= 3 && |
| 4926 | $lines[$linenr - 3] =~ /^[ +]/ && |
| 4927 | $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { |
| 4928 | WARN("RETURN_VOID", |
| 4929 | "void function return statements are not generally useful\n" . $hereprev); |
| 4930 | } |
| 4931 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4932 | # if statements using unnecessary parentheses - ie: if ((foo == bar)) |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4933 | if ($perl_version_ok && |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4934 | $line =~ /\bif\s*((?:\(\s*){2,})/) { |
| 4935 | my $openparens = $1; |
| 4936 | my $count = $openparens =~ tr@\(@\(@; |
| 4937 | my $msg = ""; |
| 4938 | if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { |
| 4939 | my $comp = $4; #Not $1 because of $LvalOrFunc |
| 4940 | $msg = " - maybe == should be = ?" if ($comp eq "=="); |
| 4941 | WARN("UNNECESSARY_PARENTHESES", |
| 4942 | "Unnecessary parentheses$msg\n" . $herecurr); |
| 4943 | } |
| 4944 | } |
| 4945 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4946 | # comparisons with a constant or upper case identifier on the left |
| 4947 | # avoid cases like "foo + BAR < baz" |
| 4948 | # only fix matches surrounded by parentheses to avoid incorrect |
| 4949 | # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 4950 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4951 | $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { |
| 4952 | my $lead = $1; |
| 4953 | my $const = $2; |
| 4954 | my $comp = $3; |
| 4955 | my $to = $4; |
| 4956 | my $newcomp = $comp; |
| 4957 | if ($lead !~ /(?:$Operators|\.)\s*$/ && |
| 4958 | $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && |
| 4959 | WARN("CONSTANT_COMPARISON", |
| 4960 | "Comparisons should place the constant on the right side of the test\n" . $herecurr) && |
| 4961 | $fix) { |
| 4962 | if ($comp eq "<") { |
| 4963 | $newcomp = ">"; |
| 4964 | } elsif ($comp eq "<=") { |
| 4965 | $newcomp = ">="; |
| 4966 | } elsif ($comp eq ">") { |
| 4967 | $newcomp = "<"; |
| 4968 | } elsif ($comp eq ">=") { |
| 4969 | $newcomp = "<="; |
| 4970 | } |
| 4971 | $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; |
| 4972 | } |
| 4973 | } |
| 4974 | |
| 4975 | # Return of what appears to be an errno should normally be negative |
| 4976 | if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4977 | my $name = $1; |
| 4978 | if ($name ne 'EOF' && $name ne 'ERROR') { |
| 4979 | WARN("USE_NEGATIVE_ERRNO", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4980 | "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4981 | } |
| 4982 | } |
| 4983 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4984 | # Need a space before open parenthesis after if, while etc |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4985 | if ($line =~ /\b(if|while|for|switch)\(/) { |
| 4986 | if (ERROR("SPACING", |
| 4987 | "space required before the open parenthesis '('\n" . $herecurr) && |
| 4988 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 4989 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 4990 | s/\b(if|while|for|switch)\(/$1 \(/; |
| 4991 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 4992 | } |
| 4993 | |
| 4994 | # Check for illegal assignment in if conditional -- and check for trailing |
| 4995 | # statements after the conditional. |
| 4996 | if ($line =~ /do\s*(?!{)/) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 4997 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 4998 | ctx_statement_block($linenr, $realcnt, 0) |
| 4999 | if (!defined $stat); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5000 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 5001 | $remain_next, $off_next); |
| 5002 | $stat_next =~ s/\n./\n /g; |
| 5003 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 5004 | |
| 5005 | if ($stat_next =~ /^\s*while\b/) { |
| 5006 | # If the statement carries leading newlines, |
| 5007 | # then count those as offsets. |
| 5008 | my ($whitespace) = |
| 5009 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 5010 | my $offset = |
| 5011 | statement_rawlines($whitespace) - 1; |
| 5012 | |
| 5013 | $suppress_whiletrailers{$line_nr_next + |
| 5014 | $offset} = 1; |
| 5015 | } |
| 5016 | } |
| 5017 | if (!defined $suppress_whiletrailers{$linenr} && |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5018 | defined($stat) && defined($cond) && |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5019 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
| 5020 | my ($s, $c) = ($stat, $cond); |
| 5021 | |
| 5022 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
| 5023 | ERROR("ASSIGN_IN_IF", |
| 5024 | "do not use assignment in if condition\n" . $herecurr); |
| 5025 | } |
| 5026 | |
| 5027 | # Find out what is on the end of the line after the |
| 5028 | # conditional. |
| 5029 | substr($s, 0, length($c), ''); |
| 5030 | $s =~ s/\n.*//g; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5031 | $s =~ s/$;//g; # Remove any comments |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5032 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 5033 | $c !~ /}\s*while\s*/) |
| 5034 | { |
| 5035 | # Find out how long the conditional actually is. |
| 5036 | my @newlines = ($c =~ /\n/gs); |
| 5037 | my $cond_lines = 1 + $#newlines; |
| 5038 | my $stat_real = ''; |
| 5039 | |
| 5040 | $stat_real = raw_line($linenr, $cond_lines) |
| 5041 | . "\n" if ($cond_lines); |
| 5042 | if (defined($stat_real) && $cond_lines > 1) { |
| 5043 | $stat_real = "[...]\n$stat_real"; |
| 5044 | } |
| 5045 | |
| 5046 | ERROR("TRAILING_STATEMENTS", |
| 5047 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
| 5048 | } |
| 5049 | } |
| 5050 | |
| 5051 | # Check for bitwise tests written as boolean |
| 5052 | if ($line =~ / |
| 5053 | (?: |
| 5054 | (?:\[|\(|\&\&|\|\|) |
| 5055 | \s*0[xX][0-9]+\s* |
| 5056 | (?:\&\&|\|\|) |
| 5057 | | |
| 5058 | (?:\&\&|\|\|) |
| 5059 | \s*0[xX][0-9]+\s* |
| 5060 | (?:\&\&|\|\||\)|\]) |
| 5061 | )/x) |
| 5062 | { |
| 5063 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 5064 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
| 5065 | } |
| 5066 | |
| 5067 | # if and else should not have general statements after it |
| 5068 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 5069 | my $s = $1; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5070 | $s =~ s/$;//g; # Remove any comments |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5071 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
| 5072 | ERROR("TRAILING_STATEMENTS", |
| 5073 | "trailing statements should be on next line\n" . $herecurr); |
| 5074 | } |
| 5075 | } |
| 5076 | # if should not continue a brace |
| 5077 | if ($line =~ /}\s*if\b/) { |
| 5078 | ERROR("TRAILING_STATEMENTS", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5079 | "trailing statements should be on next line (or did you mean 'else if'?)\n" . |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5080 | $herecurr); |
| 5081 | } |
| 5082 | # case and default should not have general statements after them |
| 5083 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 5084 | $line !~ /\G(?: |
| 5085 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
| 5086 | \s*return\s+ |
| 5087 | )/xg) |
| 5088 | { |
| 5089 | ERROR("TRAILING_STATEMENTS", |
| 5090 | "trailing statements should be on next line\n" . $herecurr); |
| 5091 | } |
| 5092 | |
| 5093 | # Check for }<nl>else {, these must be at the same |
| 5094 | # indent level to be relevant to each other. |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5095 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && |
| 5096 | $previndent == $indent) { |
| 5097 | if (ERROR("ELSE_AFTER_BRACE", |
| 5098 | "else should follow close brace '}'\n" . $hereprev) && |
| 5099 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 5100 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5101 | fix_delete_line($fixlinenr, $rawline); |
| 5102 | my $fixedline = $prevrawline; |
| 5103 | $fixedline =~ s/}\s*$//; |
| 5104 | if ($fixedline !~ /^\+\s*$/) { |
| 5105 | fix_insert_line($fixlinenr, $fixedline); |
| 5106 | } |
| 5107 | $fixedline = $rawline; |
| 5108 | $fixedline =~ s/^(.\s*)else/$1} else/; |
| 5109 | fix_insert_line($fixlinenr, $fixedline); |
| 5110 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5113 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && |
| 5114 | $previndent == $indent) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5115 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 5116 | |
| 5117 | # Find out what is on the end of the line after the |
| 5118 | # conditional. |
| 5119 | substr($s, 0, length($c), ''); |
| 5120 | $s =~ s/\n.*//g; |
| 5121 | |
| 5122 | if ($s =~ /^\s*;/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5123 | if (ERROR("WHILE_AFTER_BRACE", |
| 5124 | "while should follow close brace '}'\n" . $hereprev) && |
| 5125 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 5126 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5127 | fix_delete_line($fixlinenr, $rawline); |
| 5128 | my $fixedline = $prevrawline; |
| 5129 | my $trailing = $rawline; |
| 5130 | $trailing =~ s/^\+//; |
| 5131 | $trailing = trim($trailing); |
| 5132 | $fixedline =~ s/}\s*$/} $trailing/; |
| 5133 | fix_insert_line($fixlinenr, $fixedline); |
| 5134 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5135 | } |
| 5136 | } |
| 5137 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5138 | #Specific variable tests |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5139 | while ($line =~ m{($Constant|$Lval)}g) { |
| 5140 | my $var = $1; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5141 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5142 | #CamelCase |
| 5143 | if ($var !~ /^$Constant$/ && |
| 5144 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
| 5145 | #Ignore Page<foo> variants |
| 5146 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5147 | #Ignore SI style variants like nS, mV and dB |
| 5148 | #(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) |
| 5149 | $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5150 | #Ignore some three character SI units explicitly, like MiB and KHz |
| 5151 | $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5152 | while ($var =~ m{($Ident)}g) { |
| 5153 | my $word = $1; |
| 5154 | next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); |
| 5155 | if ($check) { |
| 5156 | seed_camelcase_includes(); |
| 5157 | if (!$file && !$camelcase_file_seeded) { |
| 5158 | seed_camelcase_file($realfile); |
| 5159 | $camelcase_file_seeded = 1; |
| 5160 | } |
| 5161 | } |
| 5162 | if (!defined $camelcase{$word}) { |
| 5163 | $camelcase{$word} = 1; |
| 5164 | CHK("CAMELCASE", |
| 5165 | "Avoid CamelCase: <$word>\n" . $herecurr); |
| 5166 | } |
| 5167 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5168 | } |
| 5169 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5170 | |
| 5171 | #no spaces allowed after \ in define |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5172 | if ($line =~ /\#\s*define.*\\\s+$/) { |
| 5173 | if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 5174 | "Whitespace after \\ makes next lines useless\n" . $herecurr) && |
| 5175 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5176 | $fixed[$fixlinenr] =~ s/\s+$//; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5177 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5180 | # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes |
| 5181 | # itself <asm/foo.h> (uses RAW line) |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5182 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
| 5183 | my $file = "$1.h"; |
| 5184 | my $checkfile = "include/linux/$file"; |
| 5185 | if (-f "$root/$checkfile" && |
| 5186 | $realfile ne $checkfile && |
| 5187 | $1 !~ /$allowed_asm_includes/) |
| 5188 | { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5189 | my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; |
| 5190 | if ($asminclude > 0) { |
| 5191 | if ($realfile =~ m{^arch/}) { |
| 5192 | CHK("ARCH_INCLUDE_LINUX", |
| 5193 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 5194 | } else { |
| 5195 | WARN("INCLUDE_LINUX", |
| 5196 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 5197 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5198 | } |
| 5199 | } |
| 5200 | } |
| 5201 | |
| 5202 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 5203 | # first statement and ensure its the whole macro if its not enclosed |
| 5204 | # in a known good container |
| 5205 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 5206 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
| 5207 | my $ln = $linenr; |
| 5208 | my $cnt = $realcnt; |
| 5209 | my ($off, $dstat, $dcond, $rest); |
| 5210 | my $ctx = ''; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5211 | my $has_flow_statement = 0; |
| 5212 | my $has_arg_concat = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5213 | ($dstat, $dcond, $ln, $cnt, $off) = |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5214 | ctx_statement_block($linenr, $realcnt, 0); |
| 5215 | $ctx = $dstat; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5216 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
| 5217 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
| 5218 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5219 | $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); |
| 5220 | $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); |
| 5221 | |
| 5222 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; |
| 5223 | my $define_args = $1; |
| 5224 | my $define_stmt = $dstat; |
| 5225 | my @def_args = (); |
| 5226 | |
| 5227 | if (defined $define_args && $define_args ne "") { |
| 5228 | $define_args = substr($define_args, 1, length($define_args) - 2); |
| 5229 | $define_args =~ s/\s*//g; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5230 | $define_args =~ s/\\\+?//g; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5231 | @def_args = split(",", $define_args); |
| 5232 | } |
| 5233 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5234 | $dstat =~ s/$;//g; |
| 5235 | $dstat =~ s/\\\n.//g; |
| 5236 | $dstat =~ s/^\s*//s; |
| 5237 | $dstat =~ s/\s*$//s; |
| 5238 | |
| 5239 | # Flatten any parentheses and braces |
| 5240 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 5241 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5242 | $dstat =~ s/.\[[^\[\]]*\]/1/) |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5243 | { |
| 5244 | } |
| 5245 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5246 | # Flatten any obvious string concatenation. |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5247 | while ($dstat =~ s/($String)\s*$Ident/$1/ || |
| 5248 | $dstat =~ s/$Ident\s*($String)/$1/) |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5249 | { |
| 5250 | } |
| 5251 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5252 | # Make asm volatile uses seem like a generic function |
| 5253 | $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; |
| 5254 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5255 | my $exceptions = qr{ |
| 5256 | $Declare| |
| 5257 | module_param_named| |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5258 | MODULE_PARM_DESC| |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5259 | DECLARE_PER_CPU| |
| 5260 | DEFINE_PER_CPU| |
| 5261 | __typeof__\(| |
| 5262 | union| |
| 5263 | struct| |
| 5264 | \.$Ident\s*=\s*| |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5265 | ^\"|\"$| |
| 5266 | ^\[ |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5267 | }x; |
| 5268 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5269 | |
| 5270 | $ctx =~ s/\n*$//; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5271 | my $stmt_cnt = statement_rawlines($ctx); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5272 | my $herectx = get_stat_here($linenr, $stmt_cnt, $here); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5273 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5274 | if ($dstat ne '' && |
| 5275 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 5276 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5277 | $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5278 | $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5279 | $dstat !~ /$exceptions/ && |
| 5280 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5281 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5282 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
| 5283 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 5284 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 5285 | $dstat !~ /^do\s*{/ && # do {... |
Heiko Schocher | e65be98 | 2016-01-04 09:17:19 +0100 | [diff] [blame] | 5286 | $dstat !~ /^\(\{/ && # ({... |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5287 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5288 | { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5289 | if ($dstat =~ /^\s*if\b/) { |
| 5290 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 5291 | "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); |
| 5292 | } elsif ($dstat =~ /;/) { |
| 5293 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 5294 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 5295 | } else { |
| 5296 | ERROR("COMPLEX_MACRO", |
| 5297 | "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); |
| 5298 | } |
| 5299 | |
| 5300 | } |
| 5301 | |
| 5302 | # Make $define_stmt single line, comment-free, etc |
| 5303 | my @stmt_array = split('\n', $define_stmt); |
| 5304 | my $first = 1; |
| 5305 | $define_stmt = ""; |
| 5306 | foreach my $l (@stmt_array) { |
| 5307 | $l =~ s/\\$//; |
| 5308 | if ($first) { |
| 5309 | $define_stmt = $l; |
| 5310 | $first = 0; |
| 5311 | } elsif ($l =~ /^[\+ ]/) { |
| 5312 | $define_stmt .= substr($l, 1); |
| 5313 | } |
| 5314 | } |
| 5315 | $define_stmt =~ s/$;//g; |
| 5316 | $define_stmt =~ s/\s+/ /g; |
| 5317 | $define_stmt = trim($define_stmt); |
| 5318 | |
| 5319 | # check if any macro arguments are reused (ignore '...' and 'type') |
| 5320 | foreach my $arg (@def_args) { |
| 5321 | next if ($arg =~ /\.\.\./); |
| 5322 | next if ($arg =~ /^type$/i); |
| 5323 | my $tmp_stmt = $define_stmt; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5324 | $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5325 | $tmp_stmt =~ s/\#+\s*$arg\b//g; |
| 5326 | $tmp_stmt =~ s/\b$arg\s*\#\#//g; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5327 | my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5328 | if ($use_cnt > 1) { |
| 5329 | CHK("MACRO_ARG_REUSE", |
| 5330 | "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); |
| 5331 | } |
| 5332 | # check if any macro arguments may have other precedence issues |
| 5333 | if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && |
| 5334 | ((defined($1) && $1 ne ',') || |
| 5335 | (defined($2) && $2 ne ','))) { |
| 5336 | CHK("MACRO_ARG_PRECEDENCE", |
| 5337 | "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); |
| 5338 | } |
| 5339 | } |
| 5340 | |
| 5341 | # check for macros with flow control, but without ## concatenation |
| 5342 | # ## concatenation is commonly a macro that defines a function so ignore those |
| 5343 | if ($has_flow_statement && !$has_arg_concat) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5344 | my $cnt = statement_rawlines($ctx); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5345 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5346 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5347 | WARN("MACRO_WITH_FLOW_CONTROL", |
| 5348 | "Macros with flow control statements should be avoided\n" . "$herectx"); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5349 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5350 | |
| 5351 | # check for line continuations outside of #defines, preprocessor #, and asm |
| 5352 | |
| 5353 | } else { |
| 5354 | if ($prevline !~ /^..*\\$/ && |
| 5355 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 5356 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
| 5357 | $line =~ /^\+.*\\$/) { |
| 5358 | WARN("LINE_CONTINUATIONS", |
| 5359 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 5360 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5363 | # do {} while (0) macro tests: |
| 5364 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 5365 | # macro should not end with a semicolon |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5366 | if ($perl_version_ok && |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5367 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 5368 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 5369 | my $ln = $linenr; |
| 5370 | my $cnt = $realcnt; |
| 5371 | my ($off, $dstat, $dcond, $rest); |
| 5372 | my $ctx = ''; |
| 5373 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 5374 | ctx_statement_block($linenr, $realcnt, 0); |
| 5375 | $ctx = $dstat; |
| 5376 | |
| 5377 | $dstat =~ s/\\\n.//g; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5378 | $dstat =~ s/$;/ /g; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5379 | |
| 5380 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 5381 | my $stmts = $2; |
| 5382 | my $semis = $3; |
| 5383 | |
| 5384 | $ctx =~ s/\n*$//; |
| 5385 | my $cnt = statement_rawlines($ctx); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5386 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5387 | |
| 5388 | if (($stmts =~ tr/;/;/) == 1 && |
| 5389 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
| 5390 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 5391 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 5392 | } |
| 5393 | if (defined $semis && $semis ne "") { |
| 5394 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 5395 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
| 5396 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5397 | } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { |
| 5398 | $ctx =~ s/\n*$//; |
| 5399 | my $cnt = statement_rawlines($ctx); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5400 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5401 | |
| 5402 | WARN("TRAILING_SEMICOLON", |
| 5403 | "macros should not use a trailing semicolon\n" . "$herectx"); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5404 | } |
| 5405 | } |
| 5406 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5407 | # check for redundant bracing round if etc |
| 5408 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 5409 | my ($level, $endln, @chunks) = |
| 5410 | ctx_statement_full($linenr, $realcnt, 1); |
| 5411 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
| 5412 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 5413 | if ($#chunks > 0 && $level == 0) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5414 | my @allowed = (); |
| 5415 | my $allow = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5416 | my $seen = 0; |
| 5417 | my $herectx = $here . "\n"; |
| 5418 | my $ln = $linenr - 1; |
| 5419 | for my $chunk (@chunks) { |
| 5420 | my ($cond, $block) = @{$chunk}; |
| 5421 | |
| 5422 | # If the condition carries leading newlines, then count those as offsets. |
| 5423 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 5424 | my $offset = statement_rawlines($whitespace) - 1; |
| 5425 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5426 | $allowed[$allow] = 0; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5427 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 5428 | |
| 5429 | # We have looked at and allowed this specific line. |
| 5430 | $suppress_ifbraces{$ln + $offset} = 1; |
| 5431 | |
| 5432 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
| 5433 | $ln += statement_rawlines($block) - 1; |
| 5434 | |
| 5435 | substr($block, 0, length($cond), ''); |
| 5436 | |
| 5437 | $seen++ if ($block =~ /^\s*{/); |
| 5438 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5439 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5440 | if (statement_lines($cond) > 1) { |
| 5441 | #print "APW: ALLOWED: cond<$cond>\n"; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5442 | $allowed[$allow] = 1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5443 | } |
| 5444 | if ($block =~/\b(?:if|for|while)\b/) { |
| 5445 | #print "APW: ALLOWED: block<$block>\n"; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5446 | $allowed[$allow] = 1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5447 | } |
| 5448 | if (statement_block_size($block) > 1) { |
| 5449 | #print "APW: ALLOWED: lines block<$block>\n"; |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5450 | $allowed[$allow] = 1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5451 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5452 | $allow++; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5453 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5454 | if ($seen) { |
| 5455 | my $sum_allowed = 0; |
| 5456 | foreach (@allowed) { |
| 5457 | $sum_allowed += $_; |
| 5458 | } |
| 5459 | if ($sum_allowed == 0) { |
| 5460 | WARN("BRACES", |
| 5461 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 5462 | } elsif ($sum_allowed != $allow && |
| 5463 | $seen != $allow) { |
| 5464 | CHK("BRACES", |
| 5465 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 5466 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5467 | } |
| 5468 | } |
| 5469 | } |
| 5470 | if (!defined $suppress_ifbraces{$linenr - 1} && |
| 5471 | $line =~ /\b(if|while|for|else)\b/) { |
| 5472 | my $allowed = 0; |
| 5473 | |
| 5474 | # Check the pre-context. |
| 5475 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 5476 | #print "APW: ALLOWED: pre<$1>\n"; |
| 5477 | $allowed = 1; |
| 5478 | } |
| 5479 | |
| 5480 | my ($level, $endln, @chunks) = |
| 5481 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 5482 | |
| 5483 | # Check the condition. |
| 5484 | my ($cond, $block) = @{$chunks[0]}; |
| 5485 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
| 5486 | if (defined $cond) { |
| 5487 | substr($block, 0, length($cond), ''); |
| 5488 | } |
| 5489 | if (statement_lines($cond) > 1) { |
| 5490 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 5491 | $allowed = 1; |
| 5492 | } |
| 5493 | if ($block =~/\b(?:if|for|while)\b/) { |
| 5494 | #print "APW: ALLOWED: block<$block>\n"; |
| 5495 | $allowed = 1; |
| 5496 | } |
| 5497 | if (statement_block_size($block) > 1) { |
| 5498 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 5499 | $allowed = 1; |
| 5500 | } |
| 5501 | # Check the post-context. |
| 5502 | if (defined $chunks[1]) { |
| 5503 | my ($cond, $block) = @{$chunks[1]}; |
| 5504 | if (defined $cond) { |
| 5505 | substr($block, 0, length($cond), ''); |
| 5506 | } |
| 5507 | if ($block =~ /^\s*\{/) { |
| 5508 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 5509 | $allowed = 1; |
| 5510 | } |
| 5511 | } |
| 5512 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5513 | my $cnt = statement_rawlines($block); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5514 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5515 | |
| 5516 | WARN("BRACES", |
| 5517 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
| 5518 | } |
| 5519 | } |
| 5520 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5521 | # check for single line unbalanced braces |
| 5522 | if ($sline =~ /^.\s*\}\s*else\s*$/ || |
| 5523 | $sline =~ /^.\s*else\s*\{\s*$/) { |
| 5524 | CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); |
| 5525 | } |
| 5526 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5527 | # check for unnecessary blank lines around braces |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5528 | if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5529 | if (CHK("BRACES", |
| 5530 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && |
| 5531 | $fix && $prevrawline =~ /^\+/) { |
| 5532 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5533 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5534 | } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5535 | if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5536 | if (CHK("BRACES", |
| 5537 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && |
| 5538 | $fix) { |
| 5539 | fix_delete_line($fixlinenr, $rawline); |
| 5540 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5541 | } |
| 5542 | |
| 5543 | # no volatiles please |
| 5544 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 5545 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
| 5546 | WARN("VOLATILE", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5547 | "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); |
| 5548 | } |
| 5549 | |
| 5550 | # Check for user-visible strings broken across lines, which breaks the ability |
| 5551 | # to grep for the string. Make exceptions when the previous string ends in a |
| 5552 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' |
| 5553 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value |
| 5554 | if ($line =~ /^\+\s*$String/ && |
| 5555 | $prevline =~ /"\s*$/ && |
| 5556 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { |
| 5557 | if (WARN("SPLIT_STRING", |
| 5558 | "quoted string split across lines\n" . $hereprev) && |
| 5559 | $fix && |
| 5560 | $prevrawline =~ /^\+.*"\s*$/ && |
| 5561 | $last_coalesced_string_linenr != $linenr - 1) { |
| 5562 | my $extracted_string = get_quoted_string($line, $rawline); |
| 5563 | my $comma_close = ""; |
| 5564 | if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { |
| 5565 | $comma_close = $1; |
| 5566 | } |
| 5567 | |
| 5568 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5569 | fix_delete_line($fixlinenr, $rawline); |
| 5570 | my $fixedline = $prevrawline; |
| 5571 | $fixedline =~ s/"\s*$//; |
| 5572 | $fixedline .= substr($extracted_string, 1) . trim($comma_close); |
| 5573 | fix_insert_line($fixlinenr - 1, $fixedline); |
| 5574 | $fixedline = $rawline; |
| 5575 | $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; |
| 5576 | if ($fixedline !~ /\+\s*$/) { |
| 5577 | fix_insert_line($fixlinenr, $fixedline); |
| 5578 | } |
| 5579 | $last_coalesced_string_linenr = $linenr; |
| 5580 | } |
| 5581 | } |
| 5582 | |
| 5583 | # check for missing a space in a string concatenation |
| 5584 | if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { |
| 5585 | WARN('MISSING_SPACE', |
| 5586 | "break quoted strings at a space character\n" . $hereprev); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5587 | } |
| 5588 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5589 | # check for an embedded function name in a string when the function is known |
| 5590 | # This does not work very well for -f --file checking as it depends on patch |
| 5591 | # context providing the function name or a single line form for in-file |
| 5592 | # function declarations |
| 5593 | if ($line =~ /^\+.*$String/ && |
| 5594 | defined($context_function) && |
| 5595 | get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && |
| 5596 | length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { |
| 5597 | WARN("EMBEDDED_FUNCTION_NAME", |
| 5598 | "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); |
| 5599 | } |
| 5600 | |
| 5601 | # check for spaces before a quoted newline |
| 5602 | if ($rawline =~ /^.*\".*\s\\n/) { |
| 5603 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 5604 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && |
| 5605 | $fix) { |
| 5606 | $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; |
| 5607 | } |
| 5608 | |
| 5609 | } |
| 5610 | |
| 5611 | # concatenated string without spaces between elements |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5612 | if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { |
| 5613 | if (CHK("CONCATENATED_STRING", |
| 5614 | "Concatenated strings should use spaces between elements\n" . $herecurr) && |
| 5615 | $fix) { |
| 5616 | while ($line =~ /($String)/g) { |
| 5617 | my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); |
| 5618 | $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; |
| 5619 | $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; |
| 5620 | } |
| 5621 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | # uncoalesced string fragments |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5625 | if ($line =~ /$String\s*"/) { |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5626 | if (WARN("STRING_FRAGMENTS", |
| 5627 | "Consecutive strings are generally better as a single string\n" . $herecurr) && |
| 5628 | $fix) { |
| 5629 | while ($line =~ /($String)(?=\s*")/g) { |
| 5630 | my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); |
| 5631 | $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; |
| 5632 | } |
| 5633 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5634 | } |
| 5635 | |
| 5636 | # check for non-standard and hex prefixed decimal printf formats |
| 5637 | my $show_L = 1; #don't show the same defect twice |
| 5638 | my $show_Z = 1; |
| 5639 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 5640 | my $string = substr($rawline, $-[1], $+[1] - $-[1]); |
| 5641 | $string =~ s/%%/__/g; |
| 5642 | # check for %L |
| 5643 | if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { |
| 5644 | WARN("PRINTF_L", |
| 5645 | "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); |
| 5646 | $show_L = 0; |
| 5647 | } |
| 5648 | # check for %Z |
| 5649 | if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { |
| 5650 | WARN("PRINTF_Z", |
| 5651 | "%Z$1 is non-standard C, use %z$1\n" . $herecurr); |
| 5652 | $show_Z = 0; |
| 5653 | } |
| 5654 | # check for 0x<decimal> |
| 5655 | if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { |
| 5656 | ERROR("PRINTF_0XDECIMAL", |
| 5657 | "Prefixing 0x with decimal output is defective\n" . $herecurr); |
| 5658 | } |
| 5659 | } |
| 5660 | |
| 5661 | # check for line continuations in quoted strings with odd counts of " |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5662 | if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5663 | WARN("LINE_CONTINUATIONS", |
| 5664 | "Avoid line continuations in quoted strings\n" . $herecurr); |
| 5665 | } |
| 5666 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5667 | # warn about #if 0 |
| 5668 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5669 | WARN("IF_0", |
| 5670 | "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); |
| 5671 | } |
| 5672 | |
| 5673 | # warn about #if 1 |
| 5674 | if ($line =~ /^.\s*\#\s*if\s+1\b/) { |
| 5675 | WARN("IF_1", |
| 5676 | "Consider removing the #if 1 and its #endif\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5677 | } |
| 5678 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5679 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 5680 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5681 | my $tested = quotemeta($1); |
| 5682 | my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; |
| 5683 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { |
| 5684 | my $func = $1; |
| 5685 | if (WARN('NEEDLESS_IF', |
| 5686 | "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && |
| 5687 | $fix) { |
| 5688 | my $do_fix = 1; |
| 5689 | my $leading_tabs = ""; |
| 5690 | my $new_leading_tabs = ""; |
| 5691 | if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { |
| 5692 | $leading_tabs = $1; |
| 5693 | } else { |
| 5694 | $do_fix = 0; |
| 5695 | } |
| 5696 | if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { |
| 5697 | $new_leading_tabs = $1; |
| 5698 | if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { |
| 5699 | $do_fix = 0; |
| 5700 | } |
| 5701 | } else { |
| 5702 | $do_fix = 0; |
| 5703 | } |
| 5704 | if ($do_fix) { |
| 5705 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5706 | $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; |
| 5707 | } |
| 5708 | } |
| 5709 | } |
| 5710 | } |
| 5711 | |
| 5712 | # check for unnecessary "Out of Memory" messages |
| 5713 | if ($line =~ /^\+.*\b$logFunctions\s*\(/ && |
| 5714 | $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && |
| 5715 | (defined $1 || defined $3) && |
| 5716 | $linenr > 3) { |
| 5717 | my $testval = $2; |
| 5718 | my $testline = $lines[$linenr - 3]; |
| 5719 | |
| 5720 | my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); |
| 5721 | # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); |
| 5722 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5723 | if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && |
| 5724 | $s !~ /\b__GFP_NOWARN\b/ ) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5725 | WARN("OOM_MESSAGE", |
| 5726 | "Possible unnecessary 'out of memory' message\n" . $hereprev); |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | # check for logging functions with KERN_<LEVEL> |
| 5731 | if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && |
| 5732 | $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { |
| 5733 | my $level = $1; |
| 5734 | if (WARN("UNNECESSARY_KERN_LEVEL", |
| 5735 | "Possible unnecessary $level\n" . $herecurr) && |
| 5736 | $fix) { |
| 5737 | $fixed[$fixlinenr] =~ s/\s*$level\s*//; |
| 5738 | } |
| 5739 | } |
| 5740 | |
| 5741 | # check for logging continuations |
| 5742 | if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { |
| 5743 | WARN("LOGGING_CONTINUATION", |
| 5744 | "Avoid logging continuation uses where feasible\n" . $herecurr); |
| 5745 | } |
| 5746 | |
| 5747 | # check for mask then right shift without a parentheses |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5748 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5749 | $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && |
| 5750 | $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so |
| 5751 | WARN("MASK_THEN_SHIFT", |
| 5752 | "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); |
| 5753 | } |
| 5754 | |
| 5755 | # check for pointer comparisons to NULL |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 5756 | if ($perl_version_ok) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5757 | while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { |
| 5758 | my $val = $1; |
| 5759 | my $equal = "!"; |
| 5760 | $equal = "" if ($4 eq "!="); |
| 5761 | if (CHK("COMPARISON_TO_NULL", |
| 5762 | "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && |
| 5763 | $fix) { |
| 5764 | $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; |
| 5765 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5766 | } |
| 5767 | } |
| 5768 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5769 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
| 5770 | if ($line =~ /(\b$InitAttribute\b)/) { |
| 5771 | my $attr = $1; |
| 5772 | if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { |
| 5773 | my $ptr = $1; |
| 5774 | my $var = $2; |
| 5775 | if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && |
| 5776 | ERROR("MISPLACED_INIT", |
| 5777 | "$attr should be placed after $var\n" . $herecurr)) || |
| 5778 | ($ptr !~ /\b(union|struct)\s+$attr\b/ && |
| 5779 | WARN("MISPLACED_INIT", |
| 5780 | "$attr should be placed after $var\n" . $herecurr))) && |
| 5781 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5782 | $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 | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5783 | } |
| 5784 | } |
| 5785 | } |
| 5786 | |
| 5787 | # check for $InitAttributeData (ie: __initdata) with const |
| 5788 | if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { |
| 5789 | my $attr = $1; |
| 5790 | $attr =~ /($InitAttributePrefix)(.*)/; |
| 5791 | my $attr_prefix = $1; |
| 5792 | my $attr_type = $2; |
| 5793 | if (ERROR("INIT_ATTRIBUTE", |
| 5794 | "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && |
| 5795 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5796 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5797 | s/$InitAttributeData/${attr_prefix}initconst/; |
| 5798 | } |
| 5799 | } |
| 5800 | |
| 5801 | # check for $InitAttributeConst (ie: __initconst) without const |
| 5802 | if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { |
| 5803 | my $attr = $1; |
| 5804 | if (ERROR("INIT_ATTRIBUTE", |
| 5805 | "Use of $attr requires a separate use of const\n" . $herecurr) && |
| 5806 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5807 | my $lead = $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5808 | /(^\+\s*(?:static\s+))/; |
| 5809 | $lead = rtrim($1); |
| 5810 | $lead = "$lead " if ($lead !~ /^\+$/); |
| 5811 | $lead = "${lead}const "; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5812 | $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; |
| 5813 | } |
| 5814 | } |
| 5815 | |
| 5816 | # check for __read_mostly with const non-pointer (should just be const) |
| 5817 | if ($line =~ /\b__read_mostly\b/ && |
| 5818 | $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { |
| 5819 | if (ERROR("CONST_READ_MOSTLY", |
| 5820 | "Invalid use of __read_mostly with const type\n" . $herecurr) && |
| 5821 | $fix) { |
| 5822 | $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; |
| 5823 | } |
| 5824 | } |
| 5825 | |
| 5826 | # don't use __constant_<foo> functions outside of include/uapi/ |
| 5827 | if ($realfile !~ m@^include/uapi/@ && |
| 5828 | $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { |
| 5829 | my $constant_func = $1; |
| 5830 | my $func = $constant_func; |
| 5831 | $func =~ s/^__constant_//; |
| 5832 | if (WARN("CONSTANT_CONVERSION", |
| 5833 | "$constant_func should be $func\n" . $herecurr) && |
| 5834 | $fix) { |
| 5835 | $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5836 | } |
| 5837 | } |
| 5838 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5839 | # prefer usleep_range over udelay |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 5840 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5841 | my $delay = $1; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5842 | # ignore udelay's < 10, however |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5843 | if (! ($delay < 10) ) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5844 | CHK("USLEEP_RANGE", |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5845 | "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5846 | } |
| 5847 | if ($delay > 2000) { |
| 5848 | WARN("LONG_UDELAY", |
| 5849 | "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5850 | } |
| 5851 | } |
| 5852 | |
| 5853 | # warn about unexpectedly long msleep's |
| 5854 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 5855 | if ($1 < 20) { |
| 5856 | WARN("MSLEEP", |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 5857 | "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5861 | # check for comparisons of jiffies |
| 5862 | if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { |
| 5863 | WARN("JIFFIES_COMPARISON", |
| 5864 | "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); |
| 5865 | } |
| 5866 | |
| 5867 | # check for comparisons of get_jiffies_64() |
| 5868 | if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { |
| 5869 | WARN("JIFFIES_COMPARISON", |
| 5870 | "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); |
| 5871 | } |
| 5872 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5873 | # warn about #ifdefs in C files |
| 5874 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
| 5875 | # print "#ifdef in C files should be avoided\n"; |
| 5876 | # print "$herecurr"; |
| 5877 | # $clean = 0; |
| 5878 | # } |
| 5879 | |
| 5880 | # warn about spacing in #ifdefs |
| 5881 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5882 | if (ERROR("SPACING", |
| 5883 | "exactly one space required after that #$1\n" . $herecurr) && |
| 5884 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5885 | $fixed[$fixlinenr] =~ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5886 | s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; |
| 5887 | } |
| 5888 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
| 5891 | # check for spinlock_t definitions without a comment. |
| 5892 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 5893 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
| 5894 | my $which = $1; |
| 5895 | if (!ctx_has_comment($first_line, $linenr)) { |
| 5896 | CHK("UNCOMMENTED_DEFINITION", |
| 5897 | "$1 definition without comment\n" . $herecurr); |
| 5898 | } |
| 5899 | } |
| 5900 | # check for memory barriers without a comment. |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5901 | |
| 5902 | my $barriers = qr{ |
| 5903 | mb| |
| 5904 | rmb| |
| 5905 | wmb| |
| 5906 | read_barrier_depends |
| 5907 | }x; |
| 5908 | my $barrier_stems = qr{ |
| 5909 | mb__before_atomic| |
| 5910 | mb__after_atomic| |
| 5911 | store_release| |
| 5912 | load_acquire| |
| 5913 | store_mb| |
| 5914 | (?:$barriers) |
| 5915 | }x; |
| 5916 | my $all_barriers = qr{ |
| 5917 | (?:$barriers)| |
| 5918 | smp_(?:$barrier_stems)| |
| 5919 | virt_(?:$barrier_stems) |
| 5920 | }x; |
| 5921 | |
| 5922 | if ($line =~ /\b(?:$all_barriers)\s*\(/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5923 | if (!ctx_has_comment($first_line, $linenr)) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5924 | WARN("MEMORY_BARRIER", |
| 5925 | "memory barrier without comment\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5926 | } |
| 5927 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5928 | |
| 5929 | my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; |
| 5930 | |
| 5931 | if ($realfile !~ m@^include/asm-generic/@ && |
| 5932 | $realfile !~ m@/barrier\.h$@ && |
| 5933 | $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && |
| 5934 | $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { |
| 5935 | WARN("MEMORY_BARRIER", |
| 5936 | "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); |
| 5937 | } |
| 5938 | |
| 5939 | # check for waitqueue_active without a comment. |
| 5940 | if ($line =~ /\bwaitqueue_active\s*\(/) { |
| 5941 | if (!ctx_has_comment($first_line, $linenr)) { |
| 5942 | WARN("WAITQUEUE_ACTIVE", |
| 5943 | "waitqueue_active without comment\n" . $herecurr); |
| 5944 | } |
| 5945 | } |
| 5946 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 5947 | # check for smp_read_barrier_depends and read_barrier_depends |
| 5948 | if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) { |
| 5949 | WARN("READ_BARRIER_DEPENDS", |
| 5950 | "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr); |
| 5951 | } |
| 5952 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5953 | # check of hardware specific defines |
| 5954 | if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
| 5955 | CHK("ARCH_DEFINES", |
| 5956 | "architecture specific defines should be avoided\n" . $herecurr); |
| 5957 | } |
| 5958 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5959 | # check that the storage class is not after a type |
| 5960 | if ($line =~ /\b($Type)\s+($Storage)\b/) { |
| 5961 | WARN("STORAGE_CLASS", |
| 5962 | "storage class '$2' should be located before type '$1'\n" . $herecurr); |
| 5963 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5964 | # Check that the storage class is at the beginning of a declaration |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5965 | if ($line =~ /\b$Storage\b/ && |
| 5966 | $line !~ /^.\s*$Storage/ && |
| 5967 | $line =~ /^.\s*(.+?)\$Storage\s/ && |
| 5968 | $1 !~ /[\,\)]\s*$/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5969 | WARN("STORAGE_CLASS", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5970 | "storage class should be at the beginning of the declaration\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5971 | } |
| 5972 | |
| 5973 | # check the location of the inline attribute, that it is between |
| 5974 | # storage class and type. |
| 5975 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 5976 | $line =~ /\b$Inline\s+$Storage\b/) { |
| 5977 | ERROR("INLINE_LOCATION", |
| 5978 | "inline keyword should sit between storage class and type\n" . $herecurr); |
| 5979 | } |
| 5980 | |
| 5981 | # Check for __inline__ and __inline, prefer inline |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5982 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5983 | $line =~ /\b(__inline__|__inline)\b/) { |
| 5984 | if (WARN("INLINE", |
| 5985 | "plain inline is preferred over $1\n" . $herecurr) && |
| 5986 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 5987 | $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5988 | |
| 5989 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5990 | } |
| 5991 | |
| 5992 | # Check for __attribute__ packed, prefer __packed |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 5993 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5994 | $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 5995 | WARN("PREFER_PACKED", |
| 5996 | "__packed is preferred over __attribute__((packed))\n" . $herecurr); |
| 5997 | } |
| 5998 | |
| 5999 | # Check for __attribute__ aligned, prefer __aligned |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6000 | if ($realfile !~ m@\binclude/uapi/@ && |
| 6001 | $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6002 | WARN("PREFER_ALIGNED", |
| 6003 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
| 6004 | } |
| 6005 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6006 | # Check for __attribute__ section, prefer __section |
| 6007 | if ($realfile !~ m@\binclude/uapi/@ && |
| 6008 | $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) { |
| 6009 | my $old = substr($rawline, $-[1], $+[1] - $-[1]); |
| 6010 | my $new = substr($old, 1, -1); |
| 6011 | if (WARN("PREFER_SECTION", |
| 6012 | "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) && |
| 6013 | $fix) { |
| 6014 | $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/; |
| 6015 | } |
| 6016 | } |
| 6017 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6018 | # Check for __attribute__ format(printf, prefer __printf |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6019 | if ($realfile !~ m@\binclude/uapi/@ && |
| 6020 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { |
| 6021 | if (WARN("PREFER_PRINTF", |
| 6022 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && |
| 6023 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6024 | $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6025 | |
| 6026 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6027 | } |
| 6028 | |
| 6029 | # Check for __attribute__ format(scanf, prefer __scanf |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6030 | if ($realfile !~ m@\binclude/uapi/@ && |
| 6031 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { |
| 6032 | if (WARN("PREFER_SCANF", |
| 6033 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && |
| 6034 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6035 | $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; |
| 6036 | } |
| 6037 | } |
| 6038 | |
| 6039 | # Check for __attribute__ weak, or __weak declarations (may have link issues) |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6040 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6041 | $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && |
| 6042 | ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || |
| 6043 | $line =~ /\b__weak\b/)) { |
| 6044 | ERROR("WEAK_DECLARATION", |
| 6045 | "Using weak declarations can have unintended link defects\n" . $herecurr); |
| 6046 | } |
| 6047 | |
| 6048 | # check for c99 types like uint8_t used outside of uapi/ and tools/ |
| 6049 | if ($realfile !~ m@\binclude/uapi/@ && |
| 6050 | $realfile !~ m@\btools/@ && |
| 6051 | $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { |
| 6052 | my $type = $1; |
| 6053 | if ($type =~ /\b($typeC99Typedefs)\b/) { |
| 6054 | $type = $1; |
| 6055 | my $kernel_type = 'u'; |
| 6056 | $kernel_type = 's' if ($type =~ /^_*[si]/); |
| 6057 | $type =~ /(\d+)/; |
| 6058 | $kernel_type .= $1; |
| 6059 | if (CHK("PREFER_KERNEL_TYPES", |
| 6060 | "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && |
| 6061 | $fix) { |
| 6062 | $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; |
| 6063 | } |
| 6064 | } |
| 6065 | } |
| 6066 | |
| 6067 | # check for cast of C90 native int or longer types constants |
| 6068 | if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { |
| 6069 | my $cast = $1; |
| 6070 | my $const = $2; |
| 6071 | if (WARN("TYPECAST_INT_CONSTANT", |
| 6072 | "Unnecessary typecast of c90 int constant\n" . $herecurr) && |
| 6073 | $fix) { |
| 6074 | my $suffix = ""; |
| 6075 | my $newconst = $const; |
| 6076 | $newconst =~ s/${Int_type}$//; |
| 6077 | $suffix .= 'U' if ($cast =~ /\bunsigned\b/); |
| 6078 | if ($cast =~ /\blong\s+long\b/) { |
| 6079 | $suffix .= 'LL'; |
| 6080 | } elsif ($cast =~ /\blong\b/) { |
| 6081 | $suffix .= 'L'; |
| 6082 | } |
| 6083 | $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6084 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6085 | } |
| 6086 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6087 | # check for sizeof(&) |
| 6088 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
| 6089 | WARN("SIZEOF_ADDRESS", |
| 6090 | "sizeof(& should be avoided\n" . $herecurr); |
| 6091 | } |
| 6092 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6093 | # check for sizeof without parenthesis |
| 6094 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6095 | if (WARN("SIZEOF_PARENTHESIS", |
| 6096 | "sizeof $1 should be sizeof($1)\n" . $herecurr) && |
| 6097 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6098 | $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6099 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6100 | } |
| 6101 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6102 | # check for struct spinlock declarations |
| 6103 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 6104 | WARN("USE_SPINLOCK_T", |
| 6105 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 6106 | } |
| 6107 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6108 | # check for seq_printf uses that could be seq_puts |
| 6109 | if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { |
| 6110 | my $fmt = get_quoted_string($line, $rawline); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6111 | $fmt =~ s/%%//g; |
| 6112 | if ($fmt !~ /%/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6113 | if (WARN("PREFER_SEQ_PUTS", |
| 6114 | "Prefer seq_puts to seq_printf\n" . $herecurr) && |
| 6115 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6116 | $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; |
| 6117 | } |
| 6118 | } |
| 6119 | } |
| 6120 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6121 | # check for vsprintf extension %p<foo> misuses |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6122 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6123 | defined $stat && |
| 6124 | $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && |
| 6125 | $1 !~ /^_*volatile_*$/) { |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6126 | my $stat_real; |
| 6127 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6128 | my $lc = $stat =~ tr@\n@@; |
| 6129 | $lc = $lc + $linenr; |
| 6130 | for (my $count = $linenr; $count <= $lc; $count++) { |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6131 | my $specifier; |
| 6132 | my $extension; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6133 | my $qualifier; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6134 | my $bad_specifier = ""; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6135 | my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); |
| 6136 | $fmt =~ s/%%//g; |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6137 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6138 | while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6139 | $specifier = $1; |
| 6140 | $extension = $2; |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6141 | $qualifier = $3; |
| 6142 | if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || |
| 6143 | ($extension eq "f" && |
| 6144 | defined $qualifier && $qualifier !~ /^w/)) { |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6145 | $bad_specifier = $specifier; |
| 6146 | last; |
| 6147 | } |
| 6148 | if ($extension eq "x" && !defined($stat_real)) { |
| 6149 | if (!defined($stat_real)) { |
| 6150 | $stat_real = get_stat_real($linenr, $lc); |
| 6151 | } |
| 6152 | WARN("VSPRINTF_SPECIFIER_PX", |
| 6153 | "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"); |
| 6154 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6155 | } |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6156 | if ($bad_specifier ne "") { |
| 6157 | my $stat_real = get_stat_real($linenr, $lc); |
| 6158 | my $ext_type = "Invalid"; |
| 6159 | my $use = ""; |
| 6160 | if ($bad_specifier =~ /p[Ff]/) { |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6161 | $use = " - use %pS instead"; |
| 6162 | $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); |
| 6163 | } |
| 6164 | |
| 6165 | WARN("VSPRINTF_POINTER_EXTENSION", |
| 6166 | "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6167 | } |
| 6168 | } |
| 6169 | } |
| 6170 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6171 | # Check for misused memsets |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6172 | if ($perl_version_ok && |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6173 | defined $stat && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6174 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6175 | |
| 6176 | my $ms_addr = $2; |
| 6177 | my $ms_val = $7; |
| 6178 | my $ms_size = $12; |
| 6179 | |
| 6180 | if ($ms_size =~ /^(0x|)0$/i) { |
| 6181 | ERROR("MEMSET", |
| 6182 | "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); |
| 6183 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 6184 | WARN("MEMSET", |
| 6185 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 6186 | } |
| 6187 | } |
| 6188 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6189 | # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6190 | # if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6191 | # defined $stat && |
| 6192 | # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 6193 | # if (WARN("PREFER_ETHER_ADDR_COPY", |
| 6194 | # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && |
| 6195 | # $fix) { |
| 6196 | # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; |
| 6197 | # } |
| 6198 | # } |
| 6199 | |
| 6200 | # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6201 | # if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6202 | # defined $stat && |
| 6203 | # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 6204 | # WARN("PREFER_ETHER_ADDR_EQUAL", |
| 6205 | # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") |
| 6206 | # } |
| 6207 | |
| 6208 | # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr |
| 6209 | # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6210 | # if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6211 | # defined $stat && |
| 6212 | # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 6213 | # |
| 6214 | # my $ms_val = $7; |
| 6215 | # |
| 6216 | # if ($ms_val =~ /^(?:0x|)0+$/i) { |
| 6217 | # if (WARN("PREFER_ETH_ZERO_ADDR", |
| 6218 | # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && |
| 6219 | # $fix) { |
| 6220 | # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; |
| 6221 | # } |
| 6222 | # } elsif ($ms_val =~ /^(?:0xff|255)$/i) { |
| 6223 | # if (WARN("PREFER_ETH_BROADCAST_ADDR", |
| 6224 | # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && |
| 6225 | # $fix) { |
| 6226 | # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; |
| 6227 | # } |
| 6228 | # } |
| 6229 | # } |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6230 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6231 | # typecasts on min/max could be min_t/max_t |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6232 | if ($perl_version_ok && |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6233 | defined $stat && |
| 6234 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
| 6235 | if (defined $2 || defined $7) { |
| 6236 | my $call = $1; |
| 6237 | my $cast1 = deparenthesize($2); |
| 6238 | my $arg1 = $3; |
| 6239 | my $cast2 = deparenthesize($7); |
| 6240 | my $arg2 = $8; |
| 6241 | my $cast; |
| 6242 | |
| 6243 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
| 6244 | $cast = "$cast1 or $cast2"; |
| 6245 | } elsif ($cast1 ne "") { |
| 6246 | $cast = $cast1; |
| 6247 | } else { |
| 6248 | $cast = $cast2; |
| 6249 | } |
| 6250 | WARN("MINMAX", |
| 6251 | "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); |
| 6252 | } |
| 6253 | } |
| 6254 | |
| 6255 | # check usleep_range arguments |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6256 | if ($perl_version_ok && |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6257 | defined $stat && |
| 6258 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 6259 | my $min = $1; |
| 6260 | my $max = $7; |
| 6261 | if ($min eq $max) { |
| 6262 | WARN("USLEEP_RANGE", |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6263 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6264 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 6265 | $min > $max) { |
| 6266 | WARN("USLEEP_RANGE", |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6267 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6268 | } |
| 6269 | } |
| 6270 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6271 | # check for naked sscanf |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6272 | if ($perl_version_ok && |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6273 | defined $stat && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6274 | $line =~ /\bsscanf\b/ && |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6275 | ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && |
| 6276 | $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && |
| 6277 | $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { |
| 6278 | my $lc = $stat =~ tr@\n@@; |
| 6279 | $lc = $lc + $linenr; |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6280 | my $stat_real = get_stat_real($linenr, $lc); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6281 | WARN("NAKED_SSCANF", |
| 6282 | "unchecked sscanf return value\n" . "$here\n$stat_real\n"); |
| 6283 | } |
| 6284 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6285 | # check for simple sscanf that should be kstrto<foo> |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6286 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6287 | defined $stat && |
| 6288 | $line =~ /\bsscanf\b/) { |
| 6289 | my $lc = $stat =~ tr@\n@@; |
| 6290 | $lc = $lc + $linenr; |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6291 | my $stat_real = get_stat_real($linenr, $lc); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6292 | if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { |
| 6293 | my $format = $6; |
| 6294 | my $count = $format =~ tr@%@%@; |
| 6295 | if ($count == 1 && |
| 6296 | $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { |
| 6297 | WARN("SSCANF_TO_KSTRTO", |
| 6298 | "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); |
| 6299 | } |
| 6300 | } |
| 6301 | } |
| 6302 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6303 | # check for new externs in .h files. |
| 6304 | if ($realfile =~ /\.h$/ && |
| 6305 | $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { |
| 6306 | if (CHK("AVOID_EXTERNS", |
| 6307 | "extern prototypes should be avoided in .h files\n" . $herecurr) && |
| 6308 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6309 | $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6310 | } |
| 6311 | } |
| 6312 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6313 | # check for new externs in .c files. |
| 6314 | if ($realfile =~ /\.c$/ && defined $stat && |
| 6315 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
| 6316 | { |
| 6317 | my $function_name = $1; |
| 6318 | my $paren_space = $2; |
| 6319 | |
| 6320 | my $s = $stat; |
| 6321 | if (defined $cond) { |
| 6322 | substr($s, 0, length($cond), ''); |
| 6323 | } |
| 6324 | if ($s =~ /^\s*;/ && |
| 6325 | $function_name ne 'uninitialized_var') |
| 6326 | { |
| 6327 | WARN("AVOID_EXTERNS", |
| 6328 | "externs should be avoided in .c files\n" . $herecurr); |
| 6329 | } |
| 6330 | |
| 6331 | if ($paren_space =~ /\n/) { |
| 6332 | WARN("FUNCTION_ARGUMENTS", |
| 6333 | "arguments for function declarations should follow identifier\n" . $herecurr); |
| 6334 | } |
| 6335 | |
| 6336 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 6337 | $stat =~ /^.\s*extern\s+/) |
| 6338 | { |
| 6339 | WARN("AVOID_EXTERNS", |
| 6340 | "externs should be avoided in .c files\n" . $herecurr); |
| 6341 | } |
| 6342 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6343 | # check for function declarations that have arguments without identifier names |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6344 | # while avoiding uninitialized_var(x) |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6345 | if (defined $stat && |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6346 | $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && |
| 6347 | (!defined($1) || |
| 6348 | (defined($1) && $1 ne "uninitialized_var")) && |
| 6349 | $2 ne "void") { |
| 6350 | my $args = trim($2); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6351 | while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { |
| 6352 | my $arg = trim($1); |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6353 | if ($arg =~ /^$Type$/ && |
| 6354 | $arg !~ /enum\s+$Ident$/) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6355 | WARN("FUNCTION_ARGUMENTS", |
| 6356 | "function definition argument '$arg' should also have an identifier name\n" . $herecurr); |
| 6357 | } |
| 6358 | } |
| 6359 | } |
| 6360 | |
| 6361 | # check for function definitions |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6362 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6363 | defined $stat && |
| 6364 | $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { |
| 6365 | $context_function = $1; |
| 6366 | |
| 6367 | # check for multiline function definition with misplaced open brace |
| 6368 | my $ok = 0; |
| 6369 | my $cnt = statement_rawlines($stat); |
| 6370 | my $herectx = $here . "\n"; |
| 6371 | for (my $n = 0; $n < $cnt; $n++) { |
| 6372 | my $rl = raw_line($linenr, $n); |
| 6373 | $herectx .= $rl . "\n"; |
| 6374 | $ok = 1 if ($rl =~ /^[ \+]\{/); |
| 6375 | $ok = 1 if ($rl =~ /\{/ && $n == 0); |
| 6376 | last if $rl =~ /^[ \+].*\{/; |
| 6377 | } |
| 6378 | if (!$ok) { |
| 6379 | ERROR("OPEN_BRACE", |
| 6380 | "open brace '{' following function definitions go on the next line\n" . $herectx); |
| 6381 | } |
| 6382 | } |
| 6383 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6384 | # checks for new __setup's |
| 6385 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 6386 | my $name = $1; |
| 6387 | |
| 6388 | if (!grep(/$name/, @setup_docs)) { |
| 6389 | CHK("UNDOCUMENTED_SETUP", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6390 | "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6391 | } |
| 6392 | } |
| 6393 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6394 | # check for pointless casting of alloc functions |
| 6395 | if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6396 | WARN("UNNECESSARY_CASTS", |
| 6397 | "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
| 6398 | } |
| 6399 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6400 | # alloc style |
| 6401 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6402 | if ($perl_version_ok && |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6403 | $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 | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6404 | CHK("ALLOC_SIZEOF_STRUCT", |
| 6405 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 6406 | } |
| 6407 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6408 | # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6409 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6410 | defined $stat && |
| 6411 | $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { |
| 6412 | my $oldfunc = $3; |
| 6413 | my $a1 = $4; |
| 6414 | my $a2 = $10; |
| 6415 | my $newfunc = "kmalloc_array"; |
| 6416 | $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); |
| 6417 | my $r1 = $a1; |
| 6418 | my $r2 = $a2; |
| 6419 | if ($a1 =~ /^sizeof\s*\S/) { |
| 6420 | $r1 = $a2; |
| 6421 | $r2 = $a1; |
| 6422 | } |
| 6423 | if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && |
| 6424 | !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6425 | my $cnt = statement_rawlines($stat); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6426 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 6427 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6428 | if (WARN("ALLOC_WITH_MULTIPLY", |
| 6429 | "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && |
| 6430 | $cnt == 1 && |
| 6431 | $fix) { |
| 6432 | $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; |
| 6433 | } |
| 6434 | } |
| 6435 | } |
| 6436 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6437 | # check for krealloc arg reuse |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6438 | if ($perl_version_ok && |
| 6439 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && |
| 6440 | $1 eq $3) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6441 | WARN("KREALLOC_ARG_REUSE", |
| 6442 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 6443 | } |
| 6444 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6445 | # check for alloc argument mismatch |
| 6446 | if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { |
| 6447 | WARN("ALLOC_ARRAY_ARGS", |
| 6448 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
| 6449 | } |
| 6450 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6451 | # check for multiple semicolons |
| 6452 | if ($line =~ /;\s*;\s*$/) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6453 | if (WARN("ONE_SEMICOLON", |
| 6454 | "Statements terminations use 1 semicolon\n" . $herecurr) && |
| 6455 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6456 | $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; |
| 6457 | } |
| 6458 | } |
| 6459 | |
| 6460 | # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi |
| 6461 | if ($realfile !~ m@^include/uapi/@ && |
| 6462 | $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { |
| 6463 | my $ull = ""; |
| 6464 | $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); |
| 6465 | if (CHK("BIT_MACRO", |
| 6466 | "Prefer using the BIT$ull macro\n" . $herecurr) && |
| 6467 | $fix) { |
| 6468 | $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6469 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6470 | } |
| 6471 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6472 | # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE |
| 6473 | if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) { |
| 6474 | my $config = $1; |
| 6475 | if (WARN("PREFER_IS_ENABLED", |
| 6476 | "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && |
| 6477 | $fix) { |
| 6478 | $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; |
| 6479 | } |
| 6480 | } |
| 6481 | |
Robert P. J. Day | c5b1e5d | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 6482 | # check for case / default statements not preceded by break/fallthrough/switch |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6483 | if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { |
| 6484 | my $has_break = 0; |
| 6485 | my $has_statement = 0; |
| 6486 | my $count = 0; |
| 6487 | my $prevline = $linenr; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6488 | while ($prevline > 1 && ($file || $count < 3) && !$has_break) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6489 | $prevline--; |
| 6490 | my $rline = $rawlines[$prevline - 1]; |
| 6491 | my $fline = $lines[$prevline - 1]; |
| 6492 | last if ($fline =~ /^\@\@/); |
| 6493 | next if ($fline =~ /^\-/); |
| 6494 | next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); |
| 6495 | $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); |
| 6496 | next if ($fline =~ /^.[\s$;]*$/); |
| 6497 | $has_statement = 1; |
| 6498 | $count++; |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6499 | $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6500 | } |
| 6501 | if (!$has_break && $has_statement) { |
| 6502 | WARN("MISSING_BREAK", |
Robert P. J. Day | c5b1e5d | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 6503 | "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6504 | } |
| 6505 | } |
| 6506 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6507 | # check for /* fallthrough */ like comment, prefer fallthrough; |
| 6508 | my @fallthroughs = ( |
| 6509 | 'fallthrough', |
| 6510 | '@fallthrough@', |
| 6511 | 'lint -fallthrough[ \t]*', |
| 6512 | 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', |
| 6513 | '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', |
| 6514 | 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', |
| 6515 | 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', |
| 6516 | ); |
| 6517 | if ($raw_comment ne '') { |
| 6518 | foreach my $ft (@fallthroughs) { |
| 6519 | if ($raw_comment =~ /$ft/) { |
| 6520 | my $msg_level = \&WARN; |
| 6521 | $msg_level = \&CHK if ($file); |
| 6522 | &{$msg_level}("PREFER_FALLTHROUGH", |
| 6523 | "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); |
| 6524 | last; |
| 6525 | } |
| 6526 | } |
| 6527 | } |
| 6528 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6529 | # check for switch/default statements without a break; |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6530 | if ($perl_version_ok && |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6531 | defined $stat && |
| 6532 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6533 | my $cnt = statement_rawlines($stat); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6534 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 6535 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6536 | WARN("DEFAULT_NO_BREAK", |
| 6537 | "switch default: should use break\n" . $herectx); |
Eric Nelson | 9c7c24f | 2012-05-02 20:32:18 -0700 | [diff] [blame] | 6538 | } |
| 6539 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6540 | # check for gcc specific __FUNCTION__ |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6541 | if ($line =~ /\b__FUNCTION__\b/) { |
| 6542 | if (WARN("USE_FUNC", |
| 6543 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && |
| 6544 | $fix) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6545 | $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6546 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6547 | } |
| 6548 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6549 | # check for uses of __DATE__, __TIME__, __TIMESTAMP__ |
| 6550 | while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { |
| 6551 | ERROR("DATE_TIME", |
| 6552 | "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); |
| 6553 | } |
| 6554 | |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6555 | # check for use of yield() |
| 6556 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 6557 | WARN("YIELD", |
| 6558 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 6559 | } |
| 6560 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6561 | # check for comparisons against true and false |
| 6562 | if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { |
| 6563 | my $lead = $1; |
| 6564 | my $arg = $2; |
| 6565 | my $test = $3; |
| 6566 | my $otype = $4; |
| 6567 | my $trail = $5; |
| 6568 | my $op = "!"; |
| 6569 | |
| 6570 | ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); |
| 6571 | |
| 6572 | my $type = lc($otype); |
| 6573 | if ($type =~ /^(?:true|false)$/) { |
| 6574 | if (("$test" eq "==" && "$type" eq "true") || |
| 6575 | ("$test" eq "!=" && "$type" eq "false")) { |
| 6576 | $op = ""; |
| 6577 | } |
| 6578 | |
| 6579 | CHK("BOOL_COMPARISON", |
| 6580 | "Using comparison to $otype is error prone\n" . $herecurr); |
| 6581 | |
| 6582 | ## maybe suggesting a correct construct would better |
| 6583 | ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); |
| 6584 | |
| 6585 | } |
| 6586 | } |
| 6587 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6588 | # check for semaphores initialized locked |
| 6589 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
| 6590 | WARN("CONSIDER_COMPLETION", |
| 6591 | "consider using a completion\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6592 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6593 | |
| 6594 | # recommend kstrto* over simple_strto* and strict_strto* |
| 6595 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6596 | WARN("CONSIDER_KSTRTO", |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6597 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6598 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6599 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6600 | # check for __initcall(), use device_initcall() explicitly or more appropriate function please |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6601 | if ($line =~ /^.\s*__initcall\s*\(/) { |
| 6602 | WARN("USE_DEVICE_INITCALL", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6603 | "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6604 | } |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6605 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6606 | # check for spin_is_locked(), suggest lockdep instead |
| 6607 | if ($line =~ /\bspin_is_locked\(/) { |
| 6608 | WARN("USE_LOCKDEP", |
| 6609 | "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); |
| 6610 | } |
| 6611 | |
| 6612 | # check for deprecated apis |
| 6613 | if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { |
| 6614 | my $deprecated_api = $1; |
| 6615 | my $new_api = $deprecated_apis{$deprecated_api}; |
| 6616 | WARN("DEPRECATED_API", |
| 6617 | "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); |
| 6618 | } |
| 6619 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6620 | # check for various structs that are normally const (ops, kgdb, device_tree) |
| 6621 | # and avoid what seem like struct definitions 'struct foo {' |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6622 | if ($line !~ /\bconst\b/ && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6623 | $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6624 | WARN("CONST_STRUCT", |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6625 | "struct $1 should normally be const\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | # use of NR_CPUS is usually wrong |
| 6629 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 6630 | if ($line =~ /\bNR_CPUS\b/ && |
| 6631 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 6632 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
| 6633 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 6634 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 6635 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
| 6636 | { |
| 6637 | WARN("NR_CPUS", |
| 6638 | "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); |
| 6639 | } |
| 6640 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6641 | # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. |
| 6642 | if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { |
| 6643 | ERROR("DEFINE_ARCH_HAS", |
| 6644 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); |
| 6645 | } |
| 6646 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6647 | # likely/unlikely comparisons similar to "(likely(foo) > 0)" |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6648 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6649 | $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { |
| 6650 | WARN("LIKELY_MISUSE", |
| 6651 | "Using $1 should generally have parentheses around the comparison\n" . $herecurr); |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6652 | } |
| 6653 | |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6654 | # nested likely/unlikely calls |
| 6655 | if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { |
| 6656 | WARN("LIKELY_MISUSE", |
| 6657 | "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); |
| 6658 | } |
| 6659 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6660 | # whine mightly about in_atomic |
| 6661 | if ($line =~ /\bin_atomic\s*\(/) { |
| 6662 | if ($realfile =~ m@^drivers/@) { |
| 6663 | ERROR("IN_ATOMIC", |
| 6664 | "do not use in_atomic in drivers\n" . $herecurr); |
| 6665 | } elsif ($realfile !~ m@^kernel/@) { |
| 6666 | WARN("IN_ATOMIC", |
| 6667 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
| 6668 | } |
| 6669 | } |
| 6670 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6671 | # check for mutex_trylock_recursive usage |
| 6672 | if ($line =~ /mutex_trylock_recursive/) { |
| 6673 | ERROR("LOCKING", |
| 6674 | "recursive locking is bad, do not use this ever.\n" . $herecurr); |
| 6675 | } |
| 6676 | |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6677 | # check for lockdep_set_novalidate_class |
| 6678 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 6679 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 6680 | if ($realfile !~ m@^kernel/lockdep@ && |
| 6681 | $realfile !~ m@^include/linux/lockdep@ && |
| 6682 | $realfile !~ m@^drivers/base/core@) { |
| 6683 | ERROR("LOCKDEP", |
| 6684 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
| 6685 | } |
| 6686 | } |
| 6687 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6688 | if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || |
| 6689 | $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6690 | WARN("EXPORTED_WORLD_WRITABLE", |
| 6691 | "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); |
| 6692 | } |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6693 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6694 | # check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> |
| 6695 | # and whether or not function naming is typical and if |
| 6696 | # DEVICE_ATTR permissions uses are unusual too |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6697 | if ($perl_version_ok && |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6698 | defined $stat && |
| 6699 | $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*\)/) { |
| 6700 | my $var = $1; |
| 6701 | my $perms = $2; |
| 6702 | my $show = $3; |
| 6703 | my $store = $4; |
| 6704 | my $octal_perms = perms_to_octal($perms); |
| 6705 | if ($show =~ /^${var}_show$/ && |
| 6706 | $store =~ /^${var}_store$/ && |
| 6707 | $octal_perms eq "0644") { |
| 6708 | if (WARN("DEVICE_ATTR_RW", |
| 6709 | "Use DEVICE_ATTR_RW\n" . $herecurr) && |
| 6710 | $fix) { |
| 6711 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/; |
| 6712 | } |
| 6713 | } elsif ($show =~ /^${var}_show$/ && |
| 6714 | $store =~ /^NULL$/ && |
| 6715 | $octal_perms eq "0444") { |
| 6716 | if (WARN("DEVICE_ATTR_RO", |
| 6717 | "Use DEVICE_ATTR_RO\n" . $herecurr) && |
| 6718 | $fix) { |
| 6719 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/; |
| 6720 | } |
| 6721 | } elsif ($show =~ /^NULL$/ && |
| 6722 | $store =~ /^${var}_store$/ && |
| 6723 | $octal_perms eq "0200") { |
| 6724 | if (WARN("DEVICE_ATTR_WO", |
| 6725 | "Use DEVICE_ATTR_WO\n" . $herecurr) && |
| 6726 | $fix) { |
| 6727 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/; |
| 6728 | } |
| 6729 | } elsif ($octal_perms eq "0644" || |
| 6730 | $octal_perms eq "0444" || |
| 6731 | $octal_perms eq "0200") { |
| 6732 | my $newshow = "$show"; |
| 6733 | $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); |
| 6734 | my $newstore = $store; |
| 6735 | $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); |
| 6736 | my $rename = ""; |
| 6737 | if ($show ne $newshow) { |
| 6738 | $rename .= " '$show' to '$newshow'"; |
| 6739 | } |
| 6740 | if ($store ne $newstore) { |
| 6741 | $rename .= " '$store' to '$newstore'"; |
| 6742 | } |
| 6743 | WARN("DEVICE_ATTR_FUNCTIONS", |
| 6744 | "Consider renaming function(s)$rename\n" . $herecurr); |
| 6745 | } else { |
| 6746 | WARN("DEVICE_ATTR_PERMS", |
| 6747 | "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); |
| 6748 | } |
| 6749 | } |
| 6750 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6751 | # Mode permission misuses where it seems decimal should be octal |
| 6752 | # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6753 | # o Ignore module_param*(...) uses with a decimal 0 permission as that has a |
| 6754 | # specific definition of not visible in sysfs. |
| 6755 | # o Ignore proc_create*(...) uses with a decimal 0 permission as that means |
| 6756 | # use the default permissions |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6757 | if ($perl_version_ok && |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6758 | defined $stat && |
| 6759 | $line =~ /$mode_perms_search/) { |
| 6760 | foreach my $entry (@mode_permission_funcs) { |
| 6761 | my $func = $entry->[0]; |
| 6762 | my $arg_pos = $entry->[1]; |
| 6763 | |
| 6764 | my $lc = $stat =~ tr@\n@@; |
| 6765 | $lc = $lc + $linenr; |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6766 | my $stat_real = get_stat_real($linenr, $lc); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6767 | |
| 6768 | my $skip_args = ""; |
| 6769 | if ($arg_pos > 1) { |
| 6770 | $arg_pos--; |
| 6771 | $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; |
| 6772 | } |
| 6773 | my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; |
| 6774 | if ($stat =~ /$test/) { |
| 6775 | my $val = $1; |
| 6776 | $val = $6 if ($skip_args ne ""); |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6777 | if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && |
| 6778 | (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || |
| 6779 | ($val =~ /^$Octal$/ && length($val) ne 4))) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6780 | ERROR("NON_OCTAL_PERMISSIONS", |
| 6781 | "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); |
| 6782 | } |
| 6783 | if ($val =~ /^$Octal$/ && (oct($val) & 02)) { |
| 6784 | ERROR("EXPORTED_WORLD_WRITABLE", |
| 6785 | "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); |
| 6786 | } |
| 6787 | } |
| 6788 | } |
| 6789 | } |
| 6790 | |
| 6791 | # check for uses of S_<PERMS> that could be octal for readability |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6792 | while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { |
| 6793 | my $oval = $1; |
| 6794 | my $octal = perms_to_octal($oval); |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6795 | if (WARN("SYMBOLIC_PERMS", |
| 6796 | "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && |
| 6797 | $fix) { |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6798 | $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6799 | } |
| 6800 | } |
| 6801 | |
| 6802 | # validate content of MODULE_LICENSE against list from include/linux/module.h |
| 6803 | if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { |
| 6804 | my $extracted_string = get_quoted_string($line, $rawline); |
| 6805 | my $valid_licenses = qr{ |
| 6806 | GPL| |
| 6807 | GPL\ v2| |
| 6808 | GPL\ and\ additional\ rights| |
| 6809 | Dual\ BSD/GPL| |
| 6810 | Dual\ MIT/GPL| |
| 6811 | Dual\ MPL/GPL| |
| 6812 | Proprietary |
| 6813 | }x; |
| 6814 | if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { |
| 6815 | WARN("MODULE_LICENSE", |
| 6816 | "unknown module license " . $extracted_string . "\n" . $herecurr); |
| 6817 | } |
| 6818 | } |
Tom Rini | d2e3033 | 2020-06-16 10:29:46 -0400 | [diff] [blame] | 6819 | |
| 6820 | # check for sysctl duplicate constants |
| 6821 | if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { |
| 6822 | WARN("DUPLICATED_SYSCTL_CONST", |
| 6823 | "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); |
| 6824 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6825 | } |
| 6826 | |
| 6827 | # If we have no input at all, then there is nothing to report on |
| 6828 | # so just keep quiet. |
| 6829 | if ($#rawlines == -1) { |
| 6830 | exit(0); |
| 6831 | } |
| 6832 | |
| 6833 | # In mailback mode only produce a report in the negative, for |
| 6834 | # things that appear to be patches. |
| 6835 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 6836 | exit(0); |
| 6837 | } |
| 6838 | |
| 6839 | # This is not a patch, and we are are in 'no-patch' mode so |
| 6840 | # just keep quiet. |
| 6841 | if (!$chk_patch && !$is_patch) { |
| 6842 | exit(0); |
| 6843 | } |
| 6844 | |
Heinrich Schuchardt | c664f0a | 2018-04-04 15:39:20 +0200 | [diff] [blame] | 6845 | if (!$is_patch && $filename !~ /cover-letter\.patch$/) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6846 | ERROR("NOT_UNIFIED_DIFF", |
| 6847 | "Does not appear to be a unified-diff format patch\n"); |
| 6848 | } |
Heinrich Schuchardt | d46efeb | 2019-10-19 09:06:38 +0200 | [diff] [blame] | 6849 | if ($is_patch && $has_commit_log && $chk_signoff) { |
| 6850 | if ($signoff == 0) { |
| 6851 | ERROR("MISSING_SIGN_OFF", |
| 6852 | "Missing Signed-off-by: line(s)\n"); |
| 6853 | } elsif (!$authorsignoff) { |
| 6854 | WARN("NO_AUTHOR_SIGN_OFF", |
| 6855 | "Missing Signed-off-by: line by nominal patch author '$author'\n"); |
| 6856 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6857 | } |
| 6858 | |
| 6859 | print report_dump(); |
| 6860 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 6861 | print "$filename " if ($summary_file); |
| 6862 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 6863 | (($check)? "$cnt_chk checks, " : "") . |
| 6864 | "$cnt_lines lines checked\n"; |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
| 6867 | if ($quiet == 0) { |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6868 | # If there were any defects found and not already fixing them |
| 6869 | if (!$clean and !$fix) { |
| 6870 | print << "EOM" |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6871 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6872 | NOTE: For some of the reported defects, checkpatch may be able to |
| 6873 | mechanically convert to the typical style using --fix or --fix-inplace. |
| 6874 | EOM |
Kim Phillips | 9b7b837 | 2013-02-28 12:53:52 +0000 | [diff] [blame] | 6875 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6876 | # If there were whitespace errors which cleanpatch can fix |
| 6877 | # then suggest that. |
| 6878 | if ($rpt_cleaners) { |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6879 | $rpt_cleaners = 0; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6880 | print << "EOM" |
| 6881 | |
| 6882 | NOTE: Whitespace errors detected. |
| 6883 | You may wish to use scripts/cleanpatch or scripts/cleanfile |
| 6884 | EOM |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6885 | } |
| 6886 | } |
| 6887 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6888 | if ($clean == 0 && $fix && |
| 6889 | ("@rawlines" ne "@fixed" || |
| 6890 | $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6891 | my $newfile = $filename; |
| 6892 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); |
| 6893 | my $linecount = 0; |
| 6894 | my $f; |
| 6895 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6896 | @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); |
| 6897 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6898 | open($f, '>', $newfile) |
| 6899 | or die "$P: Can't open $newfile for write\n"; |
| 6900 | foreach my $fixed_line (@fixed) { |
| 6901 | $linecount++; |
| 6902 | if ($file) { |
| 6903 | if ($linecount > 3) { |
| 6904 | $fixed_line =~ s/^\+//; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6905 | print $f $fixed_line . "\n"; |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6906 | } |
| 6907 | } else { |
| 6908 | print $f $fixed_line . "\n"; |
| 6909 | } |
| 6910 | } |
| 6911 | close($f); |
| 6912 | |
| 6913 | if (!$quiet) { |
| 6914 | print << "EOM"; |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6915 | |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6916 | Wrote EXPERIMENTAL --fix correction(s) to '$newfile' |
| 6917 | |
| 6918 | Do _NOT_ trust the results written to this file. |
| 6919 | Do _NOT_ submit these changes without inspecting them for correctness. |
| 6920 | |
| 6921 | This EXPERIMENTAL file is simply a convenience to help rewrite patches. |
| 6922 | No warranties, expressed or implied... |
Tom Rini | a50f8de | 2014-02-27 08:27:28 -0500 | [diff] [blame] | 6923 | EOM |
| 6924 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6925 | } |
| 6926 | |
Heinrich Schuchardt | 8716f58 | 2017-09-12 09:57:45 +0200 | [diff] [blame] | 6927 | if ($quiet == 0) { |
| 6928 | print "\n"; |
| 6929 | if ($clean == 1) { |
| 6930 | print "$vname has no obvious style problems and is ready for submission.\n"; |
| 6931 | } else { |
| 6932 | print "$vname has style problems, please review.\n"; |
| 6933 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6934 | } |
Joe Hershberger | ba2beb5 | 2011-10-18 10:06:59 +0000 | [diff] [blame] | 6935 | return $clean; |
| 6936 | } |