Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2 | # (c) 2001, Dave Jones. (the file handling bit) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3 | # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) |
| 4 | # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite) |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5 | # (c) 2008-2010 Andy Whitcroft <apw@canonical.com> |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6 | # Licensed under the terms of the GNU GPL License version 2 |
| 7 | |
| 8 | use strict; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 9 | use warnings; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 10 | use POSIX; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 11 | use File::Basename; |
| 12 | use Cwd 'abs_path'; |
| 13 | use Term::ANSIColor qw(:constants); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 14 | |
| 15 | my $P = $0; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 16 | my $D = dirname(abs_path($P)); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 17 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 18 | my $V = '0.32'; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 19 | |
| 20 | use Getopt::Long qw(:config no_auto_abbrev); |
| 21 | |
| 22 | my $quiet = 0; |
| 23 | my $tree = 1; |
| 24 | my $chk_signoff = 1; |
| 25 | my $chk_patch = 1; |
| 26 | my $tst_only; |
| 27 | my $emacs = 0; |
| 28 | my $terse = 0; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 29 | my $showfile = 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 30 | my $file = 0; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 31 | my $git = 0; |
| 32 | my %git_commits = (); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 33 | my $check = 0; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 34 | my $check_orig = 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 35 | my $summary = 1; |
| 36 | my $mailback = 0; |
| 37 | my $summary_file = 0; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 38 | my $show_types = 0; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 39 | my $list_types = 0; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 40 | my $fix = 0; |
| 41 | my $fix_inplace = 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 42 | my $root; |
| 43 | my %debug; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 44 | my %camelcase = (); |
| 45 | my %use_type = (); |
| 46 | my @use = (); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 47 | my %ignore_type = (); |
| 48 | my @ignore = (); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 49 | my $help = 0; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 50 | my $configuration_file = ".checkpatch.conf"; |
| 51 | my $max_line_length = 80; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 52 | my $ignore_perl_version = 0; |
| 53 | my $minimum_perl_version = 5.10.0; |
Vadim Bendebury | 74ef8d5 | 2014-08-29 19:41:57 -0700 | [diff] [blame] | 54 | my $min_conf_desc_length = 4; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 55 | my $spelling_file = "$D/spelling.txt"; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 56 | my $codespell = 0; |
| 57 | my $codespellfile = "/usr/share/codespell/dictionary.txt"; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 58 | my $conststructsfile = "$D/const_structs.checkpatch"; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 59 | my $typedefsfile = ""; |
| 60 | my $color = "auto"; |
Vadim Bendebury | 76942b8 | 2019-01-11 13:41:12 -0800 | [diff] [blame] | 61 | my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 62 | |
| 63 | sub help { |
| 64 | my ($exitcode) = @_; |
| 65 | |
| 66 | print << "EOM"; |
| 67 | Usage: $P [OPTION]... [FILE]... |
| 68 | Version: $V |
| 69 | |
| 70 | Options: |
| 71 | -q, --quiet quiet |
| 72 | --no-tree run without a kernel tree |
| 73 | --no-signoff do not check for 'Signed-off-by' line |
| 74 | --patch treat FILE as patchfile (default) |
| 75 | --emacs emacs compile window format |
| 76 | --terse one line per report |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 77 | --showfile emit diffed file position, not input file position |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 78 | -g, --git treat FILE as a single commit or git revision range |
| 79 | single git commit with: |
| 80 | <rev> |
| 81 | <rev>^ |
| 82 | <rev>~n |
| 83 | multiple git commits with: |
| 84 | <rev1>..<rev2> |
| 85 | <rev1>...<rev2> |
| 86 | <rev>-<count> |
| 87 | git merges are ignored |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 88 | -f, --file treat FILE as regular source file |
| 89 | --subjective, --strict enable more subjective tests |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 90 | --list-types list the possible message types |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 91 | --types TYPE(,TYPE2...) show only these comma separated message types |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 92 | --ignore TYPE(,TYPE2...) ignore various comma separated message types |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 93 | --show-types show the specific message type in the output |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 94 | --max-line-length=n set the maximum line length, if exceeded, warn |
Vadim Bendebury | 74ef8d5 | 2014-08-29 19:41:57 -0700 | [diff] [blame] | 95 | --min-conf-desc-length=n set the min description length, if shorter, warn |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 96 | --root=PATH PATH to the kernel tree root |
| 97 | --no-summary suppress the per-file summary |
| 98 | --mailback only produce a report in case of warnings/errors |
| 99 | --summary-file include the filename in summary |
| 100 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 101 | 'values', 'possible', 'type', and 'attr' (default |
| 102 | is all off) |
| 103 | --test-only=WORD report only warnings/errors containing WORD |
| 104 | literally |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 105 | --fix EXPERIMENTAL - may create horrible results |
| 106 | If correctable single-line errors exist, create |
| 107 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" |
| 108 | with potential errors corrected to the preferred |
| 109 | checkpatch style |
| 110 | --fix-inplace EXPERIMENTAL - may create horrible results |
| 111 | Is the same as --fix, but overwrites the input |
| 112 | file. It's your fault if there's no backup or git |
| 113 | --ignore-perl-version override checking of perl version. expect |
| 114 | runtime errors. |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 115 | --codespell Use the codespell dictionary for spelling/typos |
| 116 | (default:/usr/share/codespell/dictionary.txt) |
| 117 | --codespellfile Use this codespell dictionary |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 118 | --typedefsfile Read additional types from this file |
| 119 | --color[=WHEN] Use colors 'always', 'never', or only when output |
| 120 | is a terminal ('auto'). Default is 'auto'. |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 121 | -h, --help, --version display this help and exit |
| 122 | |
| 123 | When FILE is - read standard input. |
| 124 | EOM |
| 125 | |
| 126 | exit($exitcode); |
| 127 | } |
| 128 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 129 | sub uniq { |
| 130 | my %seen; |
| 131 | return grep { !$seen{$_}++ } @_; |
| 132 | } |
| 133 | |
| 134 | sub list_types { |
| 135 | my ($exitcode) = @_; |
| 136 | |
| 137 | my $count = 0; |
| 138 | |
| 139 | local $/ = undef; |
| 140 | |
| 141 | open(my $script, '<', abs_path($P)) or |
| 142 | die "$P: Can't read '$P' $!\n"; |
| 143 | |
| 144 | my $text = <$script>; |
| 145 | close($script); |
| 146 | |
| 147 | my @types = (); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 148 | # Also catch when type or level is passed through a variable |
| 149 | for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 150 | push (@types, $_); |
| 151 | } |
| 152 | @types = sort(uniq(@types)); |
| 153 | print("#\tMessage type\n\n"); |
| 154 | foreach my $type (@types) { |
| 155 | print(++$count . "\t" . $type . "\n"); |
| 156 | } |
| 157 | |
| 158 | exit($exitcode); |
| 159 | } |
| 160 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 161 | my $conf = which_conf($configuration_file); |
| 162 | if (-f $conf) { |
| 163 | my @conf_args; |
| 164 | open(my $conffile, '<', "$conf") |
| 165 | or warn "$P: Can't find a readable $configuration_file file $!\n"; |
| 166 | |
| 167 | while (<$conffile>) { |
| 168 | my $line = $_; |
| 169 | |
| 170 | $line =~ s/\s*\n?$//g; |
| 171 | $line =~ s/^\s*//g; |
| 172 | $line =~ s/\s+/ /g; |
| 173 | |
| 174 | next if ($line =~ m/^\s*#/); |
| 175 | next if ($line =~ m/^\s*$/); |
| 176 | |
| 177 | my @words = split(" ", $line); |
| 178 | foreach my $word (@words) { |
| 179 | last if ($word =~ m/^#/); |
| 180 | push (@conf_args, $word); |
| 181 | } |
| 182 | } |
| 183 | close($conffile); |
| 184 | unshift(@ARGV, @conf_args) if @conf_args; |
| 185 | } |
| 186 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 187 | # Perl's Getopt::Long allows options to take optional arguments after a space. |
| 188 | # Prevent --color by itself from consuming other arguments |
| 189 | foreach (@ARGV) { |
| 190 | if ($_ eq "--color" || $_ eq "-color") { |
| 191 | $_ = "--color=$color"; |
| 192 | } |
| 193 | } |
| 194 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 195 | GetOptions( |
| 196 | 'q|quiet+' => \$quiet, |
| 197 | 'tree!' => \$tree, |
| 198 | 'signoff!' => \$chk_signoff, |
| 199 | 'patch!' => \$chk_patch, |
| 200 | 'emacs!' => \$emacs, |
| 201 | 'terse!' => \$terse, |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 202 | 'showfile!' => \$showfile, |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 203 | 'f|file!' => \$file, |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 204 | 'g|git!' => \$git, |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 205 | 'subjective!' => \$check, |
| 206 | 'strict!' => \$check, |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 207 | 'ignore=s' => \@ignore, |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 208 | 'types=s' => \@use, |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 209 | 'show-types!' => \$show_types, |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 210 | 'list-types!' => \$list_types, |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 211 | 'max-line-length=i' => \$max_line_length, |
Vadim Bendebury | 74ef8d5 | 2014-08-29 19:41:57 -0700 | [diff] [blame] | 212 | 'min-conf-desc-length=i' => \$min_conf_desc_length, |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 213 | 'root=s' => \$root, |
| 214 | 'summary!' => \$summary, |
| 215 | 'mailback!' => \$mailback, |
| 216 | 'summary-file!' => \$summary_file, |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 217 | 'fix!' => \$fix, |
| 218 | 'fix-inplace!' => \$fix_inplace, |
| 219 | 'ignore-perl-version!' => \$ignore_perl_version, |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 220 | 'debug=s' => \%debug, |
| 221 | 'test-only=s' => \$tst_only, |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 222 | 'codespell!' => \$codespell, |
| 223 | 'codespellfile=s' => \$codespellfile, |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 224 | 'typedefsfile=s' => \$typedefsfile, |
| 225 | 'color=s' => \$color, |
| 226 | 'no-color' => \$color, #keep old behaviors of -nocolor |
| 227 | 'nocolor' => \$color, #keep old behaviors of -nocolor |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 228 | 'h|help' => \$help, |
| 229 | 'version' => \$help |
| 230 | ) or help(1); |
| 231 | |
| 232 | help(0) if ($help); |
| 233 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 234 | list_types(0) if ($list_types); |
| 235 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 236 | $fix = 1 if ($fix_inplace); |
| 237 | $check_orig = $check; |
| 238 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 239 | my $exit = 0; |
| 240 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 241 | if ($^V && $^V lt $minimum_perl_version) { |
| 242 | printf "$P: requires at least perl version %vd\n", $minimum_perl_version; |
| 243 | if (!$ignore_perl_version) { |
| 244 | exit(1); |
| 245 | } |
| 246 | } |
| 247 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 248 | #if no filenames are given, push '-' to read patch from stdin |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 249 | if ($#ARGV < 0) { |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 250 | push(@ARGV, '-'); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 253 | if ($color =~ /^[01]$/) { |
| 254 | $color = !$color; |
| 255 | } elsif ($color =~ /^always$/i) { |
| 256 | $color = 1; |
| 257 | } elsif ($color =~ /^never$/i) { |
| 258 | $color = 0; |
| 259 | } elsif ($color =~ /^auto$/i) { |
| 260 | $color = (-t STDOUT); |
| 261 | } else { |
| 262 | die "Invalid color mode: $color\n"; |
| 263 | } |
| 264 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 265 | sub hash_save_array_words { |
| 266 | my ($hashRef, $arrayRef) = @_; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 267 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 268 | my @array = split(/,/, join(',', @$arrayRef)); |
| 269 | foreach my $word (@array) { |
| 270 | $word =~ s/\s*\n?$//g; |
| 271 | $word =~ s/^\s*//g; |
| 272 | $word =~ s/\s+/ /g; |
| 273 | $word =~ tr/[a-z]/[A-Z]/; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 274 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 275 | next if ($word =~ m/^\s*#/); |
| 276 | next if ($word =~ m/^\s*$/); |
| 277 | |
| 278 | $hashRef->{$word}++; |
| 279 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 282 | sub hash_show_words { |
| 283 | my ($hashRef, $prefix) = @_; |
| 284 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 285 | if (keys %$hashRef) { |
| 286 | print "\nNOTE: $prefix message types:"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 287 | foreach my $word (sort keys %$hashRef) { |
| 288 | print " $word"; |
| 289 | } |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 290 | print "\n"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | hash_save_array_words(\%ignore_type, \@ignore); |
| 295 | hash_save_array_words(\%use_type, \@use); |
| 296 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 297 | my $dbg_values = 0; |
| 298 | my $dbg_possible = 0; |
| 299 | my $dbg_type = 0; |
| 300 | my $dbg_attr = 0; |
| 301 | for my $key (keys %debug) { |
| 302 | ## no critic |
| 303 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 304 | die "$@" if ($@); |
| 305 | } |
| 306 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 307 | my $rpt_cleaners = 0; |
| 308 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 309 | if ($terse) { |
| 310 | $emacs = 1; |
| 311 | $quiet++; |
| 312 | } |
| 313 | |
| 314 | if ($tree) { |
| 315 | if (defined $root) { |
| 316 | if (!top_of_kernel_tree($root)) { |
| 317 | die "$P: $root: --root does not point at a valid tree\n"; |
| 318 | } |
| 319 | } else { |
| 320 | if (top_of_kernel_tree('.')) { |
| 321 | $root = '.'; |
| 322 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 323 | top_of_kernel_tree($1)) { |
| 324 | $root = $1; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (!defined $root) { |
| 329 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 330 | exit(2); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | my $emitted_corrupt = 0; |
| 335 | |
| 336 | our $Ident = qr{ |
| 337 | [A-Za-z_][A-Za-z\d_]* |
| 338 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 339 | }x; |
| 340 | our $Storage = qr{extern|static|asmlinkage}; |
| 341 | our $Sparse = qr{ |
| 342 | __user| |
| 343 | __kernel| |
| 344 | __force| |
| 345 | __iomem| |
| 346 | __must_check| |
| 347 | __init_refok| |
| 348 | __kprobes| |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 349 | __ref| |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 350 | __rcu| |
| 351 | __private |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 352 | }x; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 353 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; |
| 354 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; |
| 355 | our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; |
| 356 | our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; |
| 357 | our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 358 | |
| 359 | # Notes to $Attribute: |
| 360 | # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 361 | our $Attribute = qr{ |
| 362 | const| |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 363 | __percpu| |
| 364 | __nocast| |
| 365 | __safe| |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 366 | __bitwise| |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 367 | __packed__| |
| 368 | __packed2__| |
| 369 | __naked| |
| 370 | __maybe_unused| |
| 371 | __always_unused| |
| 372 | __noreturn| |
| 373 | __used| |
| 374 | __cold| |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 375 | __pure| |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 376 | __noclone| |
| 377 | __deprecated| |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 378 | __read_mostly| |
| 379 | __kprobes| |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 380 | $InitAttribute| |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 381 | ____cacheline_aligned| |
| 382 | ____cacheline_aligned_in_smp| |
| 383 | ____cacheline_internodealigned_in_smp| |
| 384 | __weak |
| 385 | }x; |
| 386 | our $Modifier; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 387 | our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 388 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 389 | our $Lval = qr{$Ident(?:$Member)*}; |
| 390 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 391 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 392 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 393 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 394 | our $Int = qr{[0-9]+$Int_type?}; |
| 395 | our $Octal = qr{0[0-7]+$Int_type?}; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 396 | our $String = qr{"[X\t]*"}; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 397 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 398 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 399 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
| 400 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 401 | our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 402 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 403 | our $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; |
| 404 | our $Arithmetic = qr{\+|-|\*|\/|%}; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 405 | our $Operators = qr{ |
| 406 | <=|>=|==|!=| |
| 407 | =>|->|<<|>>|<|>|!|~| |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 408 | &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 409 | }x; |
| 410 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 411 | our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; |
| 412 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 413 | our $BasicType; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 414 | our $NonptrType; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 415 | our $NonptrTypeMisordered; |
| 416 | our $NonptrTypeWithAttr; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 417 | our $Type; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 418 | our $TypeMisordered; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 419 | our $Declare; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 420 | our $DeclareMisordered; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 421 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 422 | our $NON_ASCII_UTF8 = qr{ |
| 423 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 424 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 425 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 426 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 427 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 428 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 429 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 430 | }x; |
| 431 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 432 | our $UTF8 = qr{ |
| 433 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 434 | | $NON_ASCII_UTF8 |
| 435 | }x; |
| 436 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 437 | our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; |
| 438 | our $typeOtherOSTypedefs = qr{(?x: |
| 439 | u_(?:char|short|int|long) | # bsd |
| 440 | u(?:nchar|short|int|long) # sysv |
| 441 | )}; |
| 442 | our $typeKernelTypedefs = qr{(?x: |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 443 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
| 444 | atomic_t |
| 445 | )}; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 446 | our $typeTypedefs = qr{(?x: |
| 447 | $typeC99Typedefs\b| |
| 448 | $typeOtherOSTypedefs\b| |
| 449 | $typeKernelTypedefs\b |
| 450 | )}; |
| 451 | |
| 452 | our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 453 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 454 | our $logFunctions = qr{(?x: |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 455 | printk(?:_ratelimited|_once|_deferred_once|_deferred|)| |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 456 | (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 457 | TP_printk| |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 458 | WARN(?:_RATELIMIT|_ONCE|)| |
| 459 | panic| |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 460 | MODULE_[A-Z_]+| |
| 461 | seq_vprintf|seq_printf|seq_puts |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 462 | )}; |
| 463 | |
| 464 | our $signature_tags = qr{(?xi: |
| 465 | Signed-off-by:| |
| 466 | Acked-by:| |
| 467 | Tested-by:| |
| 468 | Reviewed-by:| |
| 469 | Reported-by:| |
| 470 | Suggested-by:| |
| 471 | To:| |
| 472 | Cc: |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 473 | )}; |
| 474 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 475 | our @typeListMisordered = ( |
| 476 | qr{char\s+(?:un)?signed}, |
| 477 | qr{int\s+(?:(?:un)?signed\s+)?short\s}, |
| 478 | qr{int\s+short(?:\s+(?:un)?signed)}, |
| 479 | qr{short\s+int(?:\s+(?:un)?signed)}, |
| 480 | qr{(?:un)?signed\s+int\s+short}, |
| 481 | qr{short\s+(?:un)?signed}, |
| 482 | qr{long\s+int\s+(?:un)?signed}, |
| 483 | qr{int\s+long\s+(?:un)?signed}, |
| 484 | qr{long\s+(?:un)?signed\s+int}, |
| 485 | qr{int\s+(?:un)?signed\s+long}, |
| 486 | qr{int\s+(?:un)?signed}, |
| 487 | qr{int\s+long\s+long\s+(?:un)?signed}, |
| 488 | qr{long\s+long\s+int\s+(?:un)?signed}, |
| 489 | qr{long\s+long\s+(?:un)?signed\s+int}, |
| 490 | qr{long\s+long\s+(?:un)?signed}, |
| 491 | qr{long\s+(?:un)?signed}, |
| 492 | ); |
| 493 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 494 | our @typeList = ( |
| 495 | qr{void}, |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 496 | qr{(?:(?:un)?signed\s+)?char}, |
| 497 | qr{(?:(?:un)?signed\s+)?short\s+int}, |
| 498 | qr{(?:(?:un)?signed\s+)?short}, |
| 499 | qr{(?:(?:un)?signed\s+)?int}, |
| 500 | qr{(?:(?:un)?signed\s+)?long\s+int}, |
| 501 | qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, |
| 502 | qr{(?:(?:un)?signed\s+)?long\s+long}, |
| 503 | qr{(?:(?:un)?signed\s+)?long}, |
| 504 | qr{(?:un)?signed}, |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 505 | qr{float}, |
| 506 | qr{double}, |
| 507 | qr{bool}, |
| 508 | qr{struct\s+$Ident}, |
| 509 | qr{union\s+$Ident}, |
| 510 | qr{enum\s+$Ident}, |
| 511 | qr{${Ident}_t}, |
| 512 | qr{${Ident}_handler}, |
| 513 | qr{${Ident}_handler_fn}, |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 514 | @typeListMisordered, |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 515 | ); |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 516 | |
| 517 | our $C90_int_types = qr{(?x: |
| 518 | long\s+long\s+int\s+(?:un)?signed| |
| 519 | long\s+long\s+(?:un)?signed\s+int| |
| 520 | long\s+long\s+(?:un)?signed| |
| 521 | (?:(?:un)?signed\s+)?long\s+long\s+int| |
| 522 | (?:(?:un)?signed\s+)?long\s+long| |
| 523 | int\s+long\s+long\s+(?:un)?signed| |
| 524 | int\s+(?:(?:un)?signed\s+)?long\s+long| |
| 525 | |
| 526 | long\s+int\s+(?:un)?signed| |
| 527 | long\s+(?:un)?signed\s+int| |
| 528 | long\s+(?:un)?signed| |
| 529 | (?:(?:un)?signed\s+)?long\s+int| |
| 530 | (?:(?:un)?signed\s+)?long| |
| 531 | int\s+long\s+(?:un)?signed| |
| 532 | int\s+(?:(?:un)?signed\s+)?long| |
| 533 | |
| 534 | int\s+(?:un)?signed| |
| 535 | (?:(?:un)?signed\s+)?int |
| 536 | )}; |
| 537 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 538 | our @typeListFile = (); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 539 | our @typeListWithAttr = ( |
| 540 | @typeList, |
| 541 | qr{struct\s+$InitAttribute\s+$Ident}, |
| 542 | qr{union\s+$InitAttribute\s+$Ident}, |
| 543 | ); |
| 544 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 545 | our @modifierList = ( |
| 546 | qr{fastcall}, |
| 547 | ); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 548 | our @modifierListFile = (); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 549 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 550 | our @mode_permission_funcs = ( |
| 551 | ["module_param", 3], |
| 552 | ["module_param_(?:array|named|string)", 4], |
| 553 | ["module_param_array_named", 5], |
| 554 | ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], |
| 555 | ["proc_create(?:_data|)", 2], |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 556 | ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], |
| 557 | ["IIO_DEV_ATTR_[A-Z_]+", 1], |
| 558 | ["SENSOR_(?:DEVICE_|)ATTR_2", 2], |
| 559 | ["SENSOR_TEMPLATE(?:_2|)", 3], |
| 560 | ["__ATTR", 2], |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 561 | ); |
| 562 | |
| 563 | #Create a search pattern for all these functions to speed up a loop below |
| 564 | our $mode_perms_search = ""; |
| 565 | foreach my $entry (@mode_permission_funcs) { |
| 566 | $mode_perms_search .= '|' if ($mode_perms_search ne ""); |
| 567 | $mode_perms_search .= $entry->[0]; |
| 568 | } |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 569 | $mode_perms_search = "(?:${mode_perms_search})"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 570 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 571 | our $mode_perms_world_writable = qr{ |
| 572 | S_IWUGO | |
| 573 | S_IWOTH | |
| 574 | S_IRWXUGO | |
| 575 | S_IALLUGO | |
| 576 | 0[0-7][0-7][2367] |
| 577 | }x; |
| 578 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 579 | our %mode_permission_string_types = ( |
| 580 | "S_IRWXU" => 0700, |
| 581 | "S_IRUSR" => 0400, |
| 582 | "S_IWUSR" => 0200, |
| 583 | "S_IXUSR" => 0100, |
| 584 | "S_IRWXG" => 0070, |
| 585 | "S_IRGRP" => 0040, |
| 586 | "S_IWGRP" => 0020, |
| 587 | "S_IXGRP" => 0010, |
| 588 | "S_IRWXO" => 0007, |
| 589 | "S_IROTH" => 0004, |
| 590 | "S_IWOTH" => 0002, |
| 591 | "S_IXOTH" => 0001, |
| 592 | "S_IRWXUGO" => 0777, |
| 593 | "S_IRUGO" => 0444, |
| 594 | "S_IWUGO" => 0222, |
| 595 | "S_IXUGO" => 0111, |
| 596 | ); |
| 597 | |
| 598 | #Create a search pattern for all these strings to speed up a loop below |
| 599 | our $mode_perms_string_search = ""; |
| 600 | foreach my $entry (keys %mode_permission_string_types) { |
| 601 | $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); |
| 602 | $mode_perms_string_search .= $entry; |
| 603 | } |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 604 | our $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; |
| 605 | our $multi_mode_perms_string_search = qr{ |
| 606 | ${single_mode_perms_string_search} |
| 607 | (?:\s*\|\s*${single_mode_perms_string_search})* |
| 608 | }x; |
| 609 | |
| 610 | sub perms_to_octal { |
| 611 | my ($string) = @_; |
| 612 | |
| 613 | return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); |
| 614 | |
| 615 | my $val = ""; |
| 616 | my $oval = ""; |
| 617 | my $to = 0; |
| 618 | my $curpos = 0; |
| 619 | my $lastpos = 0; |
| 620 | while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { |
| 621 | $curpos = pos($string); |
| 622 | my $match = $2; |
| 623 | my $omatch = $1; |
| 624 | last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); |
| 625 | $lastpos = $curpos; |
| 626 | $to |= $mode_permission_string_types{$match}; |
| 627 | $val .= '\s*\|\s*' if ($val ne ""); |
| 628 | $val .= $match; |
| 629 | $oval .= $omatch; |
| 630 | } |
| 631 | $oval =~ s/^\s*\|\s*//; |
| 632 | $oval =~ s/\s*\|\s*$//; |
| 633 | return sprintf("%04o", $to); |
| 634 | } |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 635 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 636 | our $allowed_asm_includes = qr{(?x: |
| 637 | irq| |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 638 | memory| |
| 639 | time| |
| 640 | reboot |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 641 | )}; |
| 642 | # memory.h: ARM has a custom one |
| 643 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 644 | # Load common spelling mistakes and build regular expression list. |
| 645 | my $misspellings; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 646 | my %spelling_fix; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 647 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 648 | if (open(my $spelling, '<', $spelling_file)) { |
| 649 | while (<$spelling>) { |
| 650 | my $line = $_; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 651 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 652 | $line =~ s/\s*\n?$//g; |
| 653 | $line =~ s/^\s*//g; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 654 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 655 | next if ($line =~ m/^\s*#/); |
| 656 | next if ($line =~ m/^\s*$/); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 657 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 658 | my ($suspect, $fix) = split(/\|\|/, $line); |
| 659 | |
| 660 | $spelling_fix{$suspect} = $fix; |
| 661 | } |
| 662 | close($spelling); |
| 663 | } else { |
| 664 | warn "No typos will be found - file '$spelling_file': $!\n"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 665 | } |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 666 | |
| 667 | if ($codespell) { |
| 668 | if (open(my $spelling, '<', $codespellfile)) { |
| 669 | while (<$spelling>) { |
| 670 | my $line = $_; |
| 671 | |
| 672 | $line =~ s/\s*\n?$//g; |
| 673 | $line =~ s/^\s*//g; |
| 674 | |
| 675 | next if ($line =~ m/^\s*#/); |
| 676 | next if ($line =~ m/^\s*$/); |
| 677 | next if ($line =~ m/, disabled/i); |
| 678 | |
| 679 | $line =~ s/,.*$//; |
| 680 | |
| 681 | my ($suspect, $fix) = split(/->/, $line); |
| 682 | |
| 683 | $spelling_fix{$suspect} = $fix; |
| 684 | } |
| 685 | close($spelling); |
| 686 | } else { |
| 687 | warn "No codespell typos will be found - file '$codespellfile': $!\n"; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 692 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 693 | sub read_words { |
| 694 | my ($wordsRef, $file) = @_; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 695 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 696 | if (open(my $words, '<', $file)) { |
| 697 | while (<$words>) { |
| 698 | my $line = $_; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 699 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 700 | $line =~ s/\s*\n?$//g; |
| 701 | $line =~ s/^\s*//g; |
| 702 | |
| 703 | next if ($line =~ m/^\s*#/); |
| 704 | next if ($line =~ m/^\s*$/); |
| 705 | if ($line =~ /\s/) { |
| 706 | print("$file: '$line' invalid - ignored\n"); |
| 707 | next; |
| 708 | } |
| 709 | |
| 710 | $$wordsRef .= '|' if ($$wordsRef ne ""); |
| 711 | $$wordsRef .= $line; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 712 | } |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 713 | close($file); |
| 714 | return 1; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 715 | } |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 716 | |
| 717 | return 0; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 720 | my $const_structs = ""; |
| 721 | read_words(\$const_structs, $conststructsfile) |
| 722 | or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; |
| 723 | |
| 724 | my $typeOtherTypedefs = ""; |
| 725 | if (length($typedefsfile)) { |
| 726 | read_words(\$typeOtherTypedefs, $typedefsfile) |
| 727 | or warn "No additional types will be considered - file '$typedefsfile': $!\n"; |
| 728 | } |
| 729 | $typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne ""); |
| 730 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 731 | sub build_types { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 732 | my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; |
| 733 | my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 734 | my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; |
| 735 | my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 736 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 737 | $BasicType = qr{ |
| 738 | (?:$typeTypedefs\b)| |
| 739 | (?:${all}\b) |
| 740 | }x; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 741 | $NonptrType = qr{ |
| 742 | (?:$Modifier\s+|const\s+)* |
| 743 | (?: |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 744 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 745 | (?:$typeTypedefs\b)| |
| 746 | (?:${all}\b) |
| 747 | ) |
| 748 | (?:\s+$Modifier|\s+const)* |
| 749 | }x; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 750 | $NonptrTypeMisordered = qr{ |
| 751 | (?:$Modifier\s+|const\s+)* |
| 752 | (?: |
| 753 | (?:${Misordered}\b) |
| 754 | ) |
| 755 | (?:\s+$Modifier|\s+const)* |
| 756 | }x; |
| 757 | $NonptrTypeWithAttr = qr{ |
| 758 | (?:$Modifier\s+|const\s+)* |
| 759 | (?: |
| 760 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 761 | (?:$typeTypedefs\b)| |
| 762 | (?:${allWithAttr}\b) |
| 763 | ) |
| 764 | (?:\s+$Modifier|\s+const)* |
| 765 | }x; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 766 | $Type = qr{ |
| 767 | $NonptrType |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 768 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 769 | (?:\s+$Inline|\s+$Modifier)* |
| 770 | }x; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 771 | $TypeMisordered = qr{ |
| 772 | $NonptrTypeMisordered |
| 773 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? |
| 774 | (?:\s+$Inline|\s+$Modifier)* |
| 775 | }x; |
| 776 | $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; |
| 777 | $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 778 | } |
| 779 | build_types(); |
| 780 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 781 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 782 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 783 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 784 | # requires at least perl version v5.10.0 |
| 785 | # Any use must be runtime checked with $^V |
| 786 | |
| 787 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 788 | our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 789 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 790 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 791 | our $declaration_macros = qr{(?x: |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 792 | (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 793 | (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| |
| 794 | (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(| |
| 795 | (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 796 | )}; |
| 797 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 798 | sub deparenthesize { |
| 799 | my ($string) = @_; |
| 800 | return "" if (!defined($string)); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 801 | |
| 802 | while ($string =~ /^\s*\(.*\)\s*$/) { |
| 803 | $string =~ s@^\s*\(\s*@@; |
| 804 | $string =~ s@\s*\)\s*$@@; |
| 805 | } |
| 806 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 807 | $string =~ s@\s+@ @g; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 808 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 809 | return $string; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 812 | sub seed_camelcase_file { |
| 813 | my ($file) = @_; |
| 814 | |
| 815 | return if (!(-f $file)); |
| 816 | |
| 817 | local $/; |
| 818 | |
| 819 | open(my $include_file, '<', "$file") |
| 820 | or warn "$P: Can't read '$file' $!\n"; |
| 821 | my $text = <$include_file>; |
| 822 | close($include_file); |
| 823 | |
| 824 | my @lines = split('\n', $text); |
| 825 | |
| 826 | foreach my $line (@lines) { |
| 827 | next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); |
| 828 | if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { |
| 829 | $camelcase{$1} = 1; |
| 830 | } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { |
| 831 | $camelcase{$1} = 1; |
| 832 | } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { |
| 833 | $camelcase{$1} = 1; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 838 | sub is_maintained_obsolete { |
| 839 | my ($filename) = @_; |
| 840 | |
| 841 | return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); |
| 842 | |
| 843 | my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; |
| 844 | |
| 845 | return $status =~ /obsolete/i; |
| 846 | } |
| 847 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 848 | my $camelcase_seeded = 0; |
| 849 | sub seed_camelcase_includes { |
| 850 | return if ($camelcase_seeded); |
| 851 | |
| 852 | my $files; |
| 853 | my $camelcase_cache = ""; |
| 854 | my @include_files = (); |
| 855 | |
| 856 | $camelcase_seeded = 1; |
| 857 | |
| 858 | if (-e ".git") { |
| 859 | my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; |
| 860 | chomp $git_last_include_commit; |
| 861 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
| 862 | } else { |
| 863 | my $last_mod_date = 0; |
| 864 | $files = `find $root/include -name "*.h"`; |
| 865 | @include_files = split('\n', $files); |
| 866 | foreach my $file (@include_files) { |
| 867 | my $date = POSIX::strftime("%Y%m%d%H%M", |
| 868 | localtime((stat $file)[9])); |
| 869 | $last_mod_date = $date if ($last_mod_date < $date); |
| 870 | } |
| 871 | $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; |
| 872 | } |
| 873 | |
| 874 | if ($camelcase_cache ne "" && -f $camelcase_cache) { |
| 875 | open(my $camelcase_file, '<', "$camelcase_cache") |
| 876 | or warn "$P: Can't read '$camelcase_cache' $!\n"; |
| 877 | while (<$camelcase_file>) { |
| 878 | chomp; |
| 879 | $camelcase{$_} = 1; |
| 880 | } |
| 881 | close($camelcase_file); |
| 882 | |
| 883 | return; |
| 884 | } |
| 885 | |
| 886 | if (-e ".git") { |
| 887 | $files = `git ls-files "include/*.h"`; |
| 888 | @include_files = split('\n', $files); |
| 889 | } |
| 890 | |
| 891 | foreach my $file (@include_files) { |
| 892 | seed_camelcase_file($file); |
| 893 | } |
| 894 | |
| 895 | if ($camelcase_cache ne "") { |
| 896 | unlink glob ".checkpatch-camelcase.*"; |
| 897 | open(my $camelcase_file, '>', "$camelcase_cache") |
| 898 | or warn "$P: Can't write '$camelcase_cache' $!\n"; |
| 899 | foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { |
| 900 | print $camelcase_file ("$_\n"); |
| 901 | } |
| 902 | close($camelcase_file); |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | sub git_commit_info { |
| 907 | my ($commit, $id, $desc) = @_; |
| 908 | |
| 909 | return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); |
| 910 | |
| 911 | my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; |
| 912 | $output =~ s/^\s*//gm; |
| 913 | my @lines = split("\n", $output); |
| 914 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 915 | return ($id, $desc) if ($#lines < 0); |
| 916 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 917 | if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { |
| 918 | # Maybe one day convert this block of bash into something that returns |
| 919 | # all matching commit ids, but it's very slow... |
| 920 | # |
| 921 | # echo "checking commits $1..." |
| 922 | # git rev-list --remotes | grep -i "^$1" | |
| 923 | # while read line ; do |
| 924 | # git log --format='%H %s' -1 $line | |
| 925 | # echo "commit $(cut -c 1-12,41-)" |
| 926 | # done |
| 927 | } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 928 | $id = undef; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 929 | } else { |
| 930 | $id = substr($lines[0], 0, 12); |
| 931 | $desc = substr($lines[0], 41); |
| 932 | } |
| 933 | |
| 934 | return ($id, $desc); |
| 935 | } |
| 936 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 937 | $chk_signoff = 0 if ($file); |
| 938 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 939 | my @rawlines = (); |
| 940 | my @lines = (); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 941 | my @fixed = (); |
| 942 | my @fixed_inserted = (); |
| 943 | my @fixed_deleted = (); |
| 944 | my $fixlinenr = -1; |
| 945 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 946 | # If input is git commits, extract all commits from the commit expressions. |
| 947 | # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. |
| 948 | die "$P: No git repository found\n" if ($git && !-e ".git"); |
| 949 | |
| 950 | if ($git) { |
| 951 | my @commits = (); |
| 952 | foreach my $commit_expr (@ARGV) { |
| 953 | my $git_range; |
| 954 | if ($commit_expr =~ m/^(.*)-(\d+)$/) { |
| 955 | $git_range = "-$2 $1"; |
| 956 | } elsif ($commit_expr =~ m/\.\./) { |
| 957 | $git_range = "$commit_expr"; |
| 958 | } else { |
| 959 | $git_range = "-1 $commit_expr"; |
| 960 | } |
| 961 | my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`; |
| 962 | foreach my $line (split(/\n/, $lines)) { |
| 963 | $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; |
| 964 | next if (!defined($1) || !defined($2)); |
| 965 | my $sha1 = $1; |
| 966 | my $subject = $2; |
| 967 | unshift(@commits, $sha1); |
| 968 | $git_commits{$sha1} = $subject; |
| 969 | } |
| 970 | } |
| 971 | die "$P: no git commits after extraction!\n" if (@commits == 0); |
| 972 | @ARGV = @commits; |
| 973 | } |
| 974 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 975 | my $vname; |
Vadim Bendebury | 76942b8 | 2019-01-11 13:41:12 -0800 | [diff] [blame] | 976 | $allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 977 | for my $filename (@ARGV) { |
| 978 | my $FILE; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 979 | if ($git) { |
| 980 | open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || |
| 981 | die "$P: $filename: git format-patch failed - $!\n"; |
| 982 | } elsif ($file) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 983 | open($FILE, '-|', "diff -u /dev/null $filename") || |
| 984 | die "$P: $filename: diff failed - $!\n"; |
| 985 | } elsif ($filename eq '-') { |
| 986 | open($FILE, '<&STDIN'); |
| 987 | } else { |
| 988 | open($FILE, '<', "$filename") || |
| 989 | die "$P: $filename: open failed - $!\n"; |
| 990 | } |
| 991 | if ($filename eq '-') { |
| 992 | $vname = 'Your patch'; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 993 | } elsif ($git) { |
| 994 | $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 995 | } else { |
| 996 | $vname = $filename; |
| 997 | } |
| 998 | while (<$FILE>) { |
| 999 | chomp; |
| 1000 | push(@rawlines, $_); |
| 1001 | } |
| 1002 | close($FILE); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1003 | |
| 1004 | if ($#ARGV > 0 && $quiet == 0) { |
| 1005 | print '-' x length($vname) . "\n"; |
| 1006 | print "$vname\n"; |
| 1007 | print '-' x length($vname) . "\n"; |
| 1008 | } |
| 1009 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1010 | if (!process($filename)) { |
| 1011 | $exit = 1; |
| 1012 | } |
| 1013 | @rawlines = (); |
| 1014 | @lines = (); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1015 | @fixed = (); |
| 1016 | @fixed_inserted = (); |
| 1017 | @fixed_deleted = (); |
| 1018 | $fixlinenr = -1; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1019 | @modifierListFile = (); |
| 1020 | @typeListFile = (); |
| 1021 | build_types(); |
| 1022 | } |
| 1023 | |
| 1024 | if (!$quiet) { |
| 1025 | hash_show_words(\%use_type, "Used"); |
| 1026 | hash_show_words(\%ignore_type, "Ignored"); |
| 1027 | |
| 1028 | if ($^V lt 5.10.0) { |
| 1029 | print << "EOM" |
| 1030 | |
| 1031 | NOTE: perl $^V is not modern enough to detect all possible issues. |
| 1032 | An upgrade to at least perl v5.10.0 is suggested. |
| 1033 | EOM |
| 1034 | } |
| 1035 | if ($exit) { |
| 1036 | print << "EOM" |
| 1037 | |
| 1038 | NOTE: If any of the errors are false positives, please report |
| 1039 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 1040 | EOM |
| 1041 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | exit($exit); |
| 1045 | |
| 1046 | sub top_of_kernel_tree { |
| 1047 | my ($root) = @_; |
| 1048 | |
| 1049 | my @tree_check = ( |
| 1050 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 1051 | "README", "Documentation", "arch", "include", "drivers", |
| 1052 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 1053 | ); |
| 1054 | |
| 1055 | foreach my $check (@tree_check) { |
| 1056 | if (! -e $root . '/' . $check) { |
| 1057 | return 0; |
| 1058 | } |
| 1059 | } |
| 1060 | return 1; |
| 1061 | } |
| 1062 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1063 | sub parse_email { |
| 1064 | my ($formatted_email) = @_; |
| 1065 | |
| 1066 | my $name = ""; |
| 1067 | my $address = ""; |
| 1068 | my $comment = ""; |
| 1069 | |
| 1070 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 1071 | $name = $1; |
| 1072 | $address = $2; |
| 1073 | $comment = $3 if defined $3; |
| 1074 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 1075 | $address = $1; |
| 1076 | $comment = $2 if defined $2; |
| 1077 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 1078 | $address = $1; |
| 1079 | $comment = $2 if defined $2; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 1080 | $formatted_email =~ s/\Q$address\E.*$//; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1081 | $name = $formatted_email; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1082 | $name = trim($name); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1083 | $name =~ s/^\"|\"$//g; |
| 1084 | # If there's a name left after stripping spaces and |
| 1085 | # leading quotes, and the address doesn't have both |
| 1086 | # leading and trailing angle brackets, the address |
| 1087 | # is invalid. ie: |
| 1088 | # "joe smith joe@smith.com" bad |
| 1089 | # "joe smith <joe@smith.com" bad |
| 1090 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 1091 | $name = ""; |
| 1092 | $address = ""; |
| 1093 | $comment = ""; |
| 1094 | } |
| 1095 | } |
| 1096 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1097 | $name = trim($name); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1098 | $name =~ s/^\"|\"$//g; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1099 | $address = trim($address); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1100 | $address =~ s/^\<|\>$//g; |
| 1101 | |
| 1102 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 1103 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 1104 | $name = "\"$name\""; |
| 1105 | } |
| 1106 | |
| 1107 | return ($name, $address, $comment); |
| 1108 | } |
| 1109 | |
| 1110 | sub format_email { |
| 1111 | my ($name, $address) = @_; |
| 1112 | |
| 1113 | my $formatted_email; |
| 1114 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1115 | $name = trim($name); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1116 | $name =~ s/^\"|\"$//g; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1117 | $address = trim($address); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1118 | |
| 1119 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 1120 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 1121 | $name = "\"$name\""; |
| 1122 | } |
| 1123 | |
| 1124 | if ("$name" eq "") { |
| 1125 | $formatted_email = "$address"; |
| 1126 | } else { |
| 1127 | $formatted_email = "$name <$address>"; |
| 1128 | } |
| 1129 | |
| 1130 | return $formatted_email; |
| 1131 | } |
| 1132 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1133 | sub which { |
| 1134 | my ($bin) = @_; |
| 1135 | |
| 1136 | foreach my $path (split(/:/, $ENV{PATH})) { |
| 1137 | if (-e "$path/$bin") { |
| 1138 | return "$path/$bin"; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | return ""; |
| 1143 | } |
| 1144 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1145 | sub which_conf { |
| 1146 | my ($conf) = @_; |
| 1147 | |
| 1148 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 1149 | if (-e "$path/$conf") { |
| 1150 | return "$path/$conf"; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | return ""; |
| 1155 | } |
| 1156 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1157 | sub expand_tabs { |
| 1158 | my ($str) = @_; |
| 1159 | |
| 1160 | my $res = ''; |
| 1161 | my $n = 0; |
| 1162 | for my $c (split(//, $str)) { |
| 1163 | if ($c eq "\t") { |
| 1164 | $res .= ' '; |
| 1165 | $n++; |
| 1166 | for (; ($n % 8) != 0; $n++) { |
| 1167 | $res .= ' '; |
| 1168 | } |
| 1169 | next; |
| 1170 | } |
| 1171 | $res .= $c; |
| 1172 | $n++; |
| 1173 | } |
| 1174 | |
| 1175 | return $res; |
| 1176 | } |
| 1177 | sub copy_spacing { |
| 1178 | (my $res = shift) =~ tr/\t/ /c; |
| 1179 | return $res; |
| 1180 | } |
| 1181 | |
| 1182 | sub line_stats { |
| 1183 | my ($line) = @_; |
| 1184 | |
| 1185 | # Drop the diff line leader and expand tabs |
| 1186 | $line =~ s/^.//; |
| 1187 | $line = expand_tabs($line); |
| 1188 | |
| 1189 | # Pick the indent from the front of the line. |
| 1190 | my ($white) = ($line =~ /^(\s*)/); |
| 1191 | |
| 1192 | return (length($line), length($white)); |
| 1193 | } |
| 1194 | |
| 1195 | my $sanitise_quote = ''; |
| 1196 | |
| 1197 | sub sanitise_line_reset { |
| 1198 | my ($in_comment) = @_; |
| 1199 | |
| 1200 | if ($in_comment) { |
| 1201 | $sanitise_quote = '*/'; |
| 1202 | } else { |
| 1203 | $sanitise_quote = ''; |
| 1204 | } |
| 1205 | } |
| 1206 | sub sanitise_line { |
| 1207 | my ($line) = @_; |
| 1208 | |
| 1209 | my $res = ''; |
| 1210 | my $l = ''; |
| 1211 | |
| 1212 | my $qlen = 0; |
| 1213 | my $off = 0; |
| 1214 | my $c; |
| 1215 | |
| 1216 | # Always copy over the diff marker. |
| 1217 | $res = substr($line, 0, 1); |
| 1218 | |
| 1219 | for ($off = 1; $off < length($line); $off++) { |
| 1220 | $c = substr($line, $off, 1); |
| 1221 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 1222 | # Comments we are whacking completely including the begin |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1223 | # and end, all to $;. |
| 1224 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 1225 | $sanitise_quote = '*/'; |
| 1226 | |
| 1227 | substr($res, $off, 2, "$;$;"); |
| 1228 | $off++; |
| 1229 | next; |
| 1230 | } |
| 1231 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
| 1232 | $sanitise_quote = ''; |
| 1233 | substr($res, $off, 2, "$;$;"); |
| 1234 | $off++; |
| 1235 | next; |
| 1236 | } |
| 1237 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 1238 | $sanitise_quote = '//'; |
| 1239 | |
| 1240 | substr($res, $off, 2, $sanitise_quote); |
| 1241 | $off++; |
| 1242 | next; |
| 1243 | } |
| 1244 | |
| 1245 | # A \ in a string means ignore the next character. |
| 1246 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 1247 | $c eq "\\") { |
| 1248 | substr($res, $off, 2, 'XX'); |
| 1249 | $off++; |
| 1250 | next; |
| 1251 | } |
| 1252 | # Regular quotes. |
| 1253 | if ($c eq "'" || $c eq '"') { |
| 1254 | if ($sanitise_quote eq '') { |
| 1255 | $sanitise_quote = $c; |
| 1256 | |
| 1257 | substr($res, $off, 1, $c); |
| 1258 | next; |
| 1259 | } elsif ($sanitise_quote eq $c) { |
| 1260 | $sanitise_quote = ''; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | #print "c<$c> SQ<$sanitise_quote>\n"; |
| 1265 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 1266 | substr($res, $off, 1, $;); |
| 1267 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 1268 | substr($res, $off, 1, $;); |
| 1269 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 1270 | substr($res, $off, 1, 'X'); |
| 1271 | } else { |
| 1272 | substr($res, $off, 1, $c); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | if ($sanitise_quote eq '//') { |
| 1277 | $sanitise_quote = ''; |
| 1278 | } |
| 1279 | |
| 1280 | # The pathname on a #include may be surrounded by '<' and '>'. |
| 1281 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
| 1282 | my $clean = 'X' x length($1); |
| 1283 | $res =~ s@\<.*\>@<$clean>@; |
| 1284 | |
| 1285 | # The whole of a #error is a string. |
| 1286 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
| 1287 | my $clean = 'X' x length($1); |
| 1288 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
| 1289 | } |
| 1290 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 1291 | if ($allow_c99_comments && $res =~ m@(//.*$)@) { |
| 1292 | my $match = $1; |
| 1293 | $res =~ s/\Q$match\E/"$;" x length($match)/e; |
| 1294 | } |
| 1295 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1296 | return $res; |
| 1297 | } |
| 1298 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1299 | sub get_quoted_string { |
| 1300 | my ($line, $rawline) = @_; |
| 1301 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 1302 | return "" if (!defined($line) || !defined($rawline)); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1303 | return "" if ($line !~ m/($String)/g); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1304 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 1305 | } |
| 1306 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1307 | sub ctx_statement_block { |
| 1308 | my ($linenr, $remain, $off) = @_; |
| 1309 | my $line = $linenr - 1; |
| 1310 | my $blk = ''; |
| 1311 | my $soff = $off; |
| 1312 | my $coff = $off - 1; |
| 1313 | my $coff_set = 0; |
| 1314 | |
| 1315 | my $loff = 0; |
| 1316 | |
| 1317 | my $type = ''; |
| 1318 | my $level = 0; |
| 1319 | my @stack = (); |
| 1320 | my $p; |
| 1321 | my $c; |
| 1322 | my $len = 0; |
| 1323 | |
| 1324 | my $remainder; |
| 1325 | while (1) { |
| 1326 | @stack = (['', 0]) if ($#stack == -1); |
| 1327 | |
| 1328 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
| 1329 | # If we are about to drop off the end, pull in more |
| 1330 | # context. |
| 1331 | if ($off >= $len) { |
| 1332 | for (; $remain > 0; $line++) { |
| 1333 | last if (!defined $lines[$line]); |
| 1334 | next if ($lines[$line] =~ /^-/); |
| 1335 | $remain--; |
| 1336 | $loff = $len; |
| 1337 | $blk .= $lines[$line] . "\n"; |
| 1338 | $len = length($blk); |
| 1339 | $line++; |
| 1340 | last; |
| 1341 | } |
| 1342 | # Bail if there is no further context. |
| 1343 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
| 1344 | if ($off >= $len) { |
| 1345 | last; |
| 1346 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1347 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 1348 | $level++; |
| 1349 | $type = '#'; |
| 1350 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1351 | } |
| 1352 | $p = $c; |
| 1353 | $c = substr($blk, $off, 1); |
| 1354 | $remainder = substr($blk, $off); |
| 1355 | |
| 1356 | #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; |
| 1357 | |
| 1358 | # Handle nested #if/#else. |
| 1359 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 1360 | push(@stack, [ $type, $level ]); |
| 1361 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 1362 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 1363 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 1364 | ($type, $level) = @{pop(@stack)}; |
| 1365 | } |
| 1366 | |
| 1367 | # Statement ends at the ';' or a close '}' at the |
| 1368 | # outermost level. |
| 1369 | if ($level == 0 && $c eq ';') { |
| 1370 | last; |
| 1371 | } |
| 1372 | |
| 1373 | # An else is really a conditional as long as its not else if |
| 1374 | if ($level == 0 && $coff_set == 0 && |
| 1375 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 1376 | $remainder =~ /^(else)(?:\s|{)/ && |
| 1377 | $remainder !~ /^else\s+if\b/) { |
| 1378 | $coff = $off + length($1) - 1; |
| 1379 | $coff_set = 1; |
| 1380 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 1381 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
| 1382 | } |
| 1383 | |
| 1384 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 1385 | $level++; |
| 1386 | $type = '('; |
| 1387 | } |
| 1388 | if ($type eq '(' && $c eq ')') { |
| 1389 | $level--; |
| 1390 | $type = ($level != 0)? '(' : ''; |
| 1391 | |
| 1392 | if ($level == 0 && $coff < $soff) { |
| 1393 | $coff = $off; |
| 1394 | $coff_set = 1; |
| 1395 | #warn "CSB: mark coff<$coff>\n"; |
| 1396 | } |
| 1397 | } |
| 1398 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 1399 | $level++; |
| 1400 | $type = '{'; |
| 1401 | } |
| 1402 | if ($type eq '{' && $c eq '}') { |
| 1403 | $level--; |
| 1404 | $type = ($level != 0)? '{' : ''; |
| 1405 | |
| 1406 | if ($level == 0) { |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 1407 | if (substr($blk, $off + 1, 1) eq ';') { |
| 1408 | $off++; |
| 1409 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1410 | last; |
| 1411 | } |
| 1412 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1413 | # Preprocessor commands end at the newline unless escaped. |
| 1414 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 1415 | $level--; |
| 1416 | $type = ''; |
| 1417 | $off++; |
| 1418 | last; |
| 1419 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1420 | $off++; |
| 1421 | } |
| 1422 | # We are truly at the end, so shuffle to the next line. |
| 1423 | if ($off == $len) { |
| 1424 | $loff = $len + 1; |
| 1425 | $line++; |
| 1426 | $remain--; |
| 1427 | } |
| 1428 | |
| 1429 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 1430 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 1431 | |
| 1432 | #warn "STATEMENT<$statement>\n"; |
| 1433 | #warn "CONDITION<$condition>\n"; |
| 1434 | |
| 1435 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
| 1436 | |
| 1437 | return ($statement, $condition, |
| 1438 | $line, $remain + 1, $off - $loff + 1, $level); |
| 1439 | } |
| 1440 | |
| 1441 | sub statement_lines { |
| 1442 | my ($stmt) = @_; |
| 1443 | |
| 1444 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 1445 | $stmt =~ s/(^|\n)./$1/g; |
| 1446 | $stmt =~ s/^\s*//; |
| 1447 | $stmt =~ s/\s*$//; |
| 1448 | |
| 1449 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1450 | |
| 1451 | return $#stmt_lines + 2; |
| 1452 | } |
| 1453 | |
| 1454 | sub statement_rawlines { |
| 1455 | my ($stmt) = @_; |
| 1456 | |
| 1457 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1458 | |
| 1459 | return $#stmt_lines + 2; |
| 1460 | } |
| 1461 | |
| 1462 | sub statement_block_size { |
| 1463 | my ($stmt) = @_; |
| 1464 | |
| 1465 | $stmt =~ s/(^|\n)./$1/g; |
| 1466 | $stmt =~ s/^\s*{//; |
| 1467 | $stmt =~ s/}\s*$//; |
| 1468 | $stmt =~ s/^\s*//; |
| 1469 | $stmt =~ s/\s*$//; |
| 1470 | |
| 1471 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1472 | my @stmt_statements = ($stmt =~ /;/g); |
| 1473 | |
| 1474 | my $stmt_lines = $#stmt_lines + 2; |
| 1475 | my $stmt_statements = $#stmt_statements + 1; |
| 1476 | |
| 1477 | if ($stmt_lines > $stmt_statements) { |
| 1478 | return $stmt_lines; |
| 1479 | } else { |
| 1480 | return $stmt_statements; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | sub ctx_statement_full { |
| 1485 | my ($linenr, $remain, $off) = @_; |
| 1486 | my ($statement, $condition, $level); |
| 1487 | |
| 1488 | my (@chunks); |
| 1489 | |
| 1490 | # Grab the first conditional/block pair. |
| 1491 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1492 | ctx_statement_block($linenr, $remain, $off); |
| 1493 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
| 1494 | push(@chunks, [ $condition, $statement ]); |
| 1495 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 1496 | return ($level, $linenr, @chunks); |
| 1497 | } |
| 1498 | |
| 1499 | # Pull in the following conditional/block pairs and see if they |
| 1500 | # could continue the statement. |
| 1501 | for (;;) { |
| 1502 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1503 | ctx_statement_block($linenr, $remain, $off); |
| 1504 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
| 1505 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
| 1506 | #print "C: push\n"; |
| 1507 | push(@chunks, [ $condition, $statement ]); |
| 1508 | } |
| 1509 | |
| 1510 | return ($level, $linenr, @chunks); |
| 1511 | } |
| 1512 | |
| 1513 | sub ctx_block_get { |
| 1514 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
| 1515 | my $line; |
| 1516 | my $start = $linenr - 1; |
| 1517 | my $blk = ''; |
| 1518 | my @o; |
| 1519 | my @c; |
| 1520 | my @res = (); |
| 1521 | |
| 1522 | my $level = 0; |
| 1523 | my @stack = ($level); |
| 1524 | for ($line = $start; $remain > 0; $line++) { |
| 1525 | next if ($rawlines[$line] =~ /^-/); |
| 1526 | $remain--; |
| 1527 | |
| 1528 | $blk .= $rawlines[$line]; |
| 1529 | |
| 1530 | # Handle nested #if/#else. |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 1531 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1532 | push(@stack, $level); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 1533 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1534 | $level = $stack[$#stack - 1]; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 1535 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1536 | $level = pop(@stack); |
| 1537 | } |
| 1538 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 1539 | foreach my $c (split(//, $lines[$line])) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1540 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 1541 | if ($off > 0) { |
| 1542 | $off--; |
| 1543 | next; |
| 1544 | } |
| 1545 | |
| 1546 | if ($c eq $close && $level > 0) { |
| 1547 | $level--; |
| 1548 | last if ($level == 0); |
| 1549 | } elsif ($c eq $open) { |
| 1550 | $level++; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | if (!$outer || $level <= 1) { |
| 1555 | push(@res, $rawlines[$line]); |
| 1556 | } |
| 1557 | |
| 1558 | last if ($level == 0); |
| 1559 | } |
| 1560 | |
| 1561 | return ($level, @res); |
| 1562 | } |
| 1563 | sub ctx_block_outer { |
| 1564 | my ($linenr, $remain) = @_; |
| 1565 | |
| 1566 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 1567 | return @r; |
| 1568 | } |
| 1569 | sub ctx_block { |
| 1570 | my ($linenr, $remain) = @_; |
| 1571 | |
| 1572 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1573 | return @r; |
| 1574 | } |
| 1575 | sub ctx_statement { |
| 1576 | my ($linenr, $remain, $off) = @_; |
| 1577 | |
| 1578 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1579 | return @r; |
| 1580 | } |
| 1581 | sub ctx_block_level { |
| 1582 | my ($linenr, $remain) = @_; |
| 1583 | |
| 1584 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1585 | } |
| 1586 | sub ctx_statement_level { |
| 1587 | my ($linenr, $remain, $off) = @_; |
| 1588 | |
| 1589 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1590 | } |
| 1591 | |
| 1592 | sub ctx_locate_comment { |
| 1593 | my ($first_line, $end_line) = @_; |
| 1594 | |
| 1595 | # Catch a comment on the end of the line itself. |
| 1596 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
| 1597 | return $current_comment if (defined $current_comment); |
| 1598 | |
| 1599 | # Look through the context and try and figure out if there is a |
| 1600 | # comment. |
| 1601 | my $in_comment = 0; |
| 1602 | $current_comment = ''; |
| 1603 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
| 1604 | my $line = $rawlines[$linenr - 1]; |
| 1605 | #warn " $line\n"; |
| 1606 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 1607 | $in_comment = 1; |
| 1608 | } |
| 1609 | if ($line =~ m@/\*@) { |
| 1610 | $in_comment = 1; |
| 1611 | } |
| 1612 | if (!$in_comment && $current_comment ne '') { |
| 1613 | $current_comment = ''; |
| 1614 | } |
| 1615 | $current_comment .= $line . "\n" if ($in_comment); |
| 1616 | if ($line =~ m@\*/@) { |
| 1617 | $in_comment = 0; |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | chomp($current_comment); |
| 1622 | return($current_comment); |
| 1623 | } |
| 1624 | sub ctx_has_comment { |
| 1625 | my ($first_line, $end_line) = @_; |
| 1626 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 1627 | |
| 1628 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
| 1629 | ##print "CMMT: $cmt\n"; |
| 1630 | |
| 1631 | return ($cmt ne ''); |
| 1632 | } |
| 1633 | |
| 1634 | sub raw_line { |
| 1635 | my ($linenr, $cnt) = @_; |
| 1636 | |
| 1637 | my $offset = $linenr - 1; |
| 1638 | $cnt++; |
| 1639 | |
| 1640 | my $line; |
| 1641 | while ($cnt) { |
| 1642 | $line = $rawlines[$offset++]; |
| 1643 | next if (defined($line) && $line =~ /^-/); |
| 1644 | $cnt--; |
| 1645 | } |
| 1646 | |
| 1647 | return $line; |
| 1648 | } |
| 1649 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 1650 | sub get_stat_real { |
| 1651 | my ($linenr, $lc) = @_; |
| 1652 | |
| 1653 | my $stat_real = raw_line($linenr, 0); |
| 1654 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 1655 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 1656 | } |
| 1657 | |
| 1658 | return $stat_real; |
| 1659 | } |
| 1660 | |
| 1661 | sub get_stat_here { |
| 1662 | my ($linenr, $cnt, $here) = @_; |
| 1663 | |
| 1664 | my $herectx = $here . "\n"; |
| 1665 | for (my $n = 0; $n < $cnt; $n++) { |
| 1666 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 1667 | } |
| 1668 | |
| 1669 | return $herectx; |
| 1670 | } |
| 1671 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1672 | sub cat_vet { |
| 1673 | my ($vet) = @_; |
| 1674 | my ($res, $coded); |
| 1675 | |
| 1676 | $res = ''; |
| 1677 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 1678 | $res .= $1; |
| 1679 | if ($2 ne '') { |
| 1680 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 1681 | $res .= $coded; |
| 1682 | } |
| 1683 | } |
| 1684 | $res =~ s/$/\$/; |
| 1685 | |
| 1686 | return $res; |
| 1687 | } |
| 1688 | |
| 1689 | my $av_preprocessor = 0; |
| 1690 | my $av_pending; |
| 1691 | my @av_paren_type; |
| 1692 | my $av_pend_colon; |
| 1693 | |
| 1694 | sub annotate_reset { |
| 1695 | $av_preprocessor = 0; |
| 1696 | $av_pending = '_'; |
| 1697 | @av_paren_type = ('E'); |
| 1698 | $av_pend_colon = 'O'; |
| 1699 | } |
| 1700 | |
| 1701 | sub annotate_values { |
| 1702 | my ($stream, $type) = @_; |
| 1703 | |
| 1704 | my $res; |
| 1705 | my $var = '_' x length($stream); |
| 1706 | my $cur = $stream; |
| 1707 | |
| 1708 | print "$stream\n" if ($dbg_values > 1); |
| 1709 | |
| 1710 | while (length($cur)) { |
| 1711 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
| 1712 | print " <" . join('', @av_paren_type) . |
| 1713 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
| 1714 | if ($cur =~ /^(\s+)/o) { |
| 1715 | print "WS($1)\n" if ($dbg_values > 1); |
| 1716 | if ($1 =~ /\n/ && $av_preprocessor) { |
| 1717 | $type = pop(@av_paren_type); |
| 1718 | $av_preprocessor = 0; |
| 1719 | } |
| 1720 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 1721 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
| 1722 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1723 | push(@av_paren_type, $type); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1724 | $type = 'c'; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 1725 | |
| 1726 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1727 | print "DECLARE($1)\n" if ($dbg_values > 1); |
| 1728 | $type = 'T'; |
| 1729 | |
| 1730 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 1731 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 1732 | $type = 'T'; |
| 1733 | |
| 1734 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
| 1735 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
| 1736 | $av_preprocessor = 1; |
| 1737 | push(@av_paren_type, $type); |
| 1738 | if ($2 ne '') { |
| 1739 | $av_pending = 'N'; |
| 1740 | } |
| 1741 | $type = 'E'; |
| 1742 | |
| 1743 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
| 1744 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 1745 | $av_preprocessor = 1; |
| 1746 | push(@av_paren_type, $type); |
| 1747 | |
| 1748 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
| 1749 | print "PRE_START($1)\n" if ($dbg_values > 1); |
| 1750 | $av_preprocessor = 1; |
| 1751 | |
| 1752 | push(@av_paren_type, $type); |
| 1753 | push(@av_paren_type, $type); |
| 1754 | $type = 'E'; |
| 1755 | |
| 1756 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
| 1757 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 1758 | $av_preprocessor = 1; |
| 1759 | |
| 1760 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 1761 | |
| 1762 | $type = 'E'; |
| 1763 | |
| 1764 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
| 1765 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 1766 | |
| 1767 | $av_preprocessor = 1; |
| 1768 | |
| 1769 | # Assume all arms of the conditional end as this |
| 1770 | # one does, and continue as if the #endif was not here. |
| 1771 | pop(@av_paren_type); |
| 1772 | push(@av_paren_type, $type); |
| 1773 | $type = 'E'; |
| 1774 | |
| 1775 | } elsif ($cur =~ /^(\\\n)/o) { |
| 1776 | print "PRECONT($1)\n" if ($dbg_values > 1); |
| 1777 | |
| 1778 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 1779 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 1780 | $av_pending = $type; |
| 1781 | $type = 'N'; |
| 1782 | |
| 1783 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
| 1784 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
| 1785 | if (defined $2) { |
| 1786 | $av_pending = 'V'; |
| 1787 | } |
| 1788 | $type = 'N'; |
| 1789 | |
| 1790 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
| 1791 | print "COND($1)\n" if ($dbg_values > 1); |
| 1792 | $av_pending = 'E'; |
| 1793 | $type = 'N'; |
| 1794 | |
| 1795 | } elsif ($cur =~/^(case)/o) { |
| 1796 | print "CASE($1)\n" if ($dbg_values > 1); |
| 1797 | $av_pend_colon = 'C'; |
| 1798 | $type = 'N'; |
| 1799 | |
| 1800 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
| 1801 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
| 1802 | $type = 'N'; |
| 1803 | |
| 1804 | } elsif ($cur =~ /^(\()/o) { |
| 1805 | print "PAREN('$1')\n" if ($dbg_values > 1); |
| 1806 | push(@av_paren_type, $av_pending); |
| 1807 | $av_pending = '_'; |
| 1808 | $type = 'N'; |
| 1809 | |
| 1810 | } elsif ($cur =~ /^(\))/o) { |
| 1811 | my $new_type = pop(@av_paren_type); |
| 1812 | if ($new_type ne '_') { |
| 1813 | $type = $new_type; |
| 1814 | print "PAREN('$1') -> $type\n" |
| 1815 | if ($dbg_values > 1); |
| 1816 | } else { |
| 1817 | print "PAREN('$1')\n" if ($dbg_values > 1); |
| 1818 | } |
| 1819 | |
| 1820 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
| 1821 | print "FUNC($1)\n" if ($dbg_values > 1); |
| 1822 | $type = 'V'; |
| 1823 | $av_pending = 'V'; |
| 1824 | |
| 1825 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 1826 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
| 1827 | $av_pend_colon = 'B'; |
| 1828 | } elsif ($type eq 'E') { |
| 1829 | $av_pend_colon = 'L'; |
| 1830 | } |
| 1831 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 1832 | $type = 'V'; |
| 1833 | |
| 1834 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
| 1835 | print "IDENT($1)\n" if ($dbg_values > 1); |
| 1836 | $type = 'V'; |
| 1837 | |
| 1838 | } elsif ($cur =~ /^($Assignment)/o) { |
| 1839 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
| 1840 | $type = 'N'; |
| 1841 | |
| 1842 | } elsif ($cur =~/^(;|{|})/) { |
| 1843 | print "END($1)\n" if ($dbg_values > 1); |
| 1844 | $type = 'E'; |
| 1845 | $av_pend_colon = 'O'; |
| 1846 | |
| 1847 | } elsif ($cur =~/^(,)/) { |
| 1848 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 1849 | $type = 'C'; |
| 1850 | |
| 1851 | } elsif ($cur =~ /^(\?)/o) { |
| 1852 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 1853 | $type = 'N'; |
| 1854 | |
| 1855 | } elsif ($cur =~ /^(:)/o) { |
| 1856 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 1857 | |
| 1858 | substr($var, length($res), 1, $av_pend_colon); |
| 1859 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 1860 | $type = 'E'; |
| 1861 | } else { |
| 1862 | $type = 'N'; |
| 1863 | } |
| 1864 | $av_pend_colon = 'O'; |
| 1865 | |
| 1866 | } elsif ($cur =~ /^(\[)/o) { |
| 1867 | print "CLOSE($1)\n" if ($dbg_values > 1); |
| 1868 | $type = 'N'; |
| 1869 | |
| 1870 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
| 1871 | my $variant; |
| 1872 | |
| 1873 | print "OPV($1)\n" if ($dbg_values > 1); |
| 1874 | if ($type eq 'V') { |
| 1875 | $variant = 'B'; |
| 1876 | } else { |
| 1877 | $variant = 'U'; |
| 1878 | } |
| 1879 | |
| 1880 | substr($var, length($res), 1, $variant); |
| 1881 | $type = 'N'; |
| 1882 | |
| 1883 | } elsif ($cur =~ /^($Operators)/o) { |
| 1884 | print "OP($1)\n" if ($dbg_values > 1); |
| 1885 | if ($1 ne '++' && $1 ne '--') { |
| 1886 | $type = 'N'; |
| 1887 | } |
| 1888 | |
| 1889 | } elsif ($cur =~ /(^.)/o) { |
| 1890 | print "C($1)\n" if ($dbg_values > 1); |
| 1891 | } |
| 1892 | if (defined $1) { |
| 1893 | $cur = substr($cur, length($1)); |
| 1894 | $res .= $type x length($1); |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | return ($res, $var); |
| 1899 | } |
| 1900 | |
| 1901 | sub possible { |
| 1902 | my ($possible, $line) = @_; |
| 1903 | my $notPermitted = qr{(?: |
| 1904 | ^(?: |
| 1905 | $Modifier| |
| 1906 | $Storage| |
| 1907 | $Type| |
| 1908 | DEFINE_\S+ |
| 1909 | )$| |
| 1910 | ^(?: |
| 1911 | goto| |
| 1912 | return| |
| 1913 | case| |
| 1914 | else| |
| 1915 | asm|__asm__| |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1916 | do| |
| 1917 | \#| |
| 1918 | \#\#| |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1919 | )(?:\s|$)| |
| 1920 | ^(?:typedef|struct|enum)\b |
| 1921 | )}x; |
| 1922 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 1923 | if ($possible !~ $notPermitted) { |
| 1924 | # Check for modifiers. |
| 1925 | $possible =~ s/\s*$Storage\s*//g; |
| 1926 | $possible =~ s/\s*$Sparse\s*//g; |
| 1927 | if ($possible =~ /^\s*$/) { |
| 1928 | |
| 1929 | } elsif ($possible =~ /\s/) { |
| 1930 | $possible =~ s/\s*$Type\s*//g; |
| 1931 | for my $modifier (split(' ', $possible)) { |
| 1932 | if ($modifier !~ $notPermitted) { |
| 1933 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1934 | push(@modifierListFile, $modifier); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | } else { |
| 1939 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1940 | push(@typeListFile, $possible); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1941 | } |
| 1942 | build_types(); |
| 1943 | } else { |
| 1944 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | my $prefix = ''; |
| 1949 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1950 | sub show_type { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1951 | my ($type) = @_; |
| 1952 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 1953 | $type =~ tr/[a-z]/[A-Z]/; |
| 1954 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1955 | return defined $use_type{$type} if (scalar keys %use_type > 0); |
| 1956 | |
| 1957 | return !defined $ignore_type{$type}; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1960 | sub report { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1961 | my ($level, $type, $msg) = @_; |
| 1962 | |
| 1963 | if (!show_type($type) || |
| 1964 | (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1965 | return 0; |
| 1966 | } |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1967 | my $output = ''; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 1968 | if ($color) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1969 | if ($level eq 'ERROR') { |
| 1970 | $output .= RED; |
| 1971 | } elsif ($level eq 'WARNING') { |
| 1972 | $output .= YELLOW; |
| 1973 | } else { |
| 1974 | $output .= GREEN; |
| 1975 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 1976 | } |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1977 | $output .= $prefix . $level . ':'; |
| 1978 | if ($show_types) { |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 1979 | $output .= BLUE if ($color); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1980 | $output .= "$type:"; |
| 1981 | } |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 1982 | $output .= RESET if ($color); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1983 | $output .= ' ' . $msg . "\n"; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1984 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 1985 | if ($showfile) { |
| 1986 | my @lines = split("\n", $output, -1); |
| 1987 | splice(@lines, 1, 1); |
| 1988 | $output = join("\n", @lines); |
| 1989 | } |
| 1990 | $output = (split('\n', $output))[0] . "\n" if ($terse); |
| 1991 | |
| 1992 | push(our @report, $output); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1993 | |
| 1994 | return 1; |
| 1995 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 1996 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1997 | sub report_dump { |
| 1998 | our @report; |
| 1999 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2000 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2001 | sub fixup_current_range { |
| 2002 | my ($lineRef, $offset, $length) = @_; |
| 2003 | |
| 2004 | if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { |
| 2005 | my $o = $1; |
| 2006 | my $l = $2; |
| 2007 | my $no = $o + $offset; |
| 2008 | my $nl = $l + $length; |
| 2009 | $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | sub fix_inserted_deleted_lines { |
| 2014 | my ($linesRef, $insertedRef, $deletedRef) = @_; |
| 2015 | |
| 2016 | my $range_last_linenr = 0; |
| 2017 | my $delta_offset = 0; |
| 2018 | |
| 2019 | my $old_linenr = 0; |
| 2020 | my $new_linenr = 0; |
| 2021 | |
| 2022 | my $next_insert = 0; |
| 2023 | my $next_delete = 0; |
| 2024 | |
| 2025 | my @lines = (); |
| 2026 | |
| 2027 | my $inserted = @{$insertedRef}[$next_insert++]; |
| 2028 | my $deleted = @{$deletedRef}[$next_delete++]; |
| 2029 | |
| 2030 | foreach my $old_line (@{$linesRef}) { |
| 2031 | my $save_line = 1; |
| 2032 | my $line = $old_line; #don't modify the array |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2033 | if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2034 | $delta_offset = 0; |
| 2035 | } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk |
| 2036 | $range_last_linenr = $new_linenr; |
| 2037 | fixup_current_range(\$line, $delta_offset, 0); |
| 2038 | } |
| 2039 | |
| 2040 | while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { |
| 2041 | $deleted = @{$deletedRef}[$next_delete++]; |
| 2042 | $save_line = 0; |
| 2043 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); |
| 2044 | } |
| 2045 | |
| 2046 | while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { |
| 2047 | push(@lines, ${$inserted}{'LINE'}); |
| 2048 | $inserted = @{$insertedRef}[$next_insert++]; |
| 2049 | $new_linenr++; |
| 2050 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); |
| 2051 | } |
| 2052 | |
| 2053 | if ($save_line) { |
| 2054 | push(@lines, $line); |
| 2055 | $new_linenr++; |
| 2056 | } |
| 2057 | |
| 2058 | $old_linenr++; |
| 2059 | } |
| 2060 | |
| 2061 | return @lines; |
| 2062 | } |
| 2063 | |
| 2064 | sub fix_insert_line { |
| 2065 | my ($linenr, $line) = @_; |
| 2066 | |
| 2067 | my $inserted = { |
| 2068 | LINENR => $linenr, |
| 2069 | LINE => $line, |
| 2070 | }; |
| 2071 | push(@fixed_inserted, $inserted); |
| 2072 | } |
| 2073 | |
| 2074 | sub fix_delete_line { |
| 2075 | my ($linenr, $line) = @_; |
| 2076 | |
| 2077 | my $deleted = { |
| 2078 | LINENR => $linenr, |
| 2079 | LINE => $line, |
| 2080 | }; |
| 2081 | |
| 2082 | push(@fixed_deleted, $deleted); |
| 2083 | } |
| 2084 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2085 | sub ERROR { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2086 | my ($type, $msg) = @_; |
| 2087 | |
| 2088 | if (report("ERROR", $type, $msg)) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2089 | our $clean = 0; |
| 2090 | our $cnt_error++; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2091 | return 1; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2092 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2093 | return 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2094 | } |
| 2095 | sub WARN { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2096 | my ($type, $msg) = @_; |
| 2097 | |
| 2098 | if (report("WARNING", $type, $msg)) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2099 | our $clean = 0; |
| 2100 | our $cnt_warn++; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2101 | return 1; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2102 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2103 | return 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2104 | } |
| 2105 | sub CHK { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2106 | my ($type, $msg) = @_; |
| 2107 | |
| 2108 | if ($check && report("CHECK", $type, $msg)) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2109 | our $clean = 0; |
| 2110 | our $cnt_chk++; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2111 | return 1; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2112 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2113 | return 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | sub check_absolute_file { |
| 2117 | my ($absolute, $herecurr) = @_; |
| 2118 | my $file = $absolute; |
| 2119 | |
| 2120 | ##print "absolute<$absolute>\n"; |
| 2121 | |
| 2122 | # See if any suffix of this path is a path within the tree. |
| 2123 | while ($file =~ s@^[^/]*/@@) { |
| 2124 | if (-f "$root/$file") { |
| 2125 | ##print "file<$file>\n"; |
| 2126 | last; |
| 2127 | } |
| 2128 | } |
| 2129 | if (! -f _) { |
| 2130 | return 0; |
| 2131 | } |
| 2132 | |
| 2133 | # It is, so see if the prefix is acceptable. |
| 2134 | my $prefix = $absolute; |
| 2135 | substr($prefix, -length($file)) = ''; |
| 2136 | |
| 2137 | ##print "prefix<$prefix>\n"; |
| 2138 | if ($prefix ne ".../") { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2139 | WARN("USE_RELATIVE_PATH", |
| 2140 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2144 | sub trim { |
| 2145 | my ($string) = @_; |
| 2146 | |
| 2147 | $string =~ s/^\s+|\s+$//g; |
| 2148 | |
| 2149 | return $string; |
| 2150 | } |
| 2151 | |
| 2152 | sub ltrim { |
| 2153 | my ($string) = @_; |
| 2154 | |
| 2155 | $string =~ s/^\s+//; |
| 2156 | |
| 2157 | return $string; |
| 2158 | } |
| 2159 | |
| 2160 | sub rtrim { |
| 2161 | my ($string) = @_; |
| 2162 | |
| 2163 | $string =~ s/\s+$//; |
| 2164 | |
| 2165 | return $string; |
| 2166 | } |
| 2167 | |
| 2168 | sub string_find_replace { |
| 2169 | my ($string, $find, $replace) = @_; |
| 2170 | |
| 2171 | $string =~ s/$find/$replace/g; |
| 2172 | |
| 2173 | return $string; |
| 2174 | } |
| 2175 | |
| 2176 | sub tabify { |
| 2177 | my ($leading) = @_; |
| 2178 | |
| 2179 | my $source_indent = 8; |
| 2180 | my $max_spaces_before_tab = $source_indent - 1; |
| 2181 | my $spaces_to_tab = " " x $source_indent; |
| 2182 | |
| 2183 | #convert leading spaces to tabs |
| 2184 | 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; |
| 2185 | #Remove spaces before a tab |
| 2186 | 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; |
| 2187 | |
| 2188 | return "$leading"; |
| 2189 | } |
| 2190 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2191 | sub pos_last_openparen { |
| 2192 | my ($line) = @_; |
| 2193 | |
| 2194 | my $pos = 0; |
| 2195 | |
| 2196 | my $opens = $line =~ tr/\(/\(/; |
| 2197 | my $closes = $line =~ tr/\)/\)/; |
| 2198 | |
| 2199 | my $last_openparen = 0; |
| 2200 | |
| 2201 | if (($opens == 0) || ($closes >= $opens)) { |
| 2202 | return -1; |
| 2203 | } |
| 2204 | |
| 2205 | my $len = length($line); |
| 2206 | |
| 2207 | for ($pos = 0; $pos < $len; $pos++) { |
| 2208 | my $string = substr($line, $pos); |
| 2209 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 2210 | $pos += length($1) - 1; |
| 2211 | } elsif (substr($line, $pos, 1) eq '(') { |
| 2212 | $last_openparen = $pos; |
| 2213 | } elsif (index($string, '(') == -1) { |
| 2214 | last; |
| 2215 | } |
| 2216 | } |
| 2217 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2218 | return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2221 | sub process { |
| 2222 | my $filename = shift; |
| 2223 | |
| 2224 | my $linenr=0; |
| 2225 | my $prevline=""; |
| 2226 | my $prevrawline=""; |
| 2227 | my $stashline=""; |
| 2228 | my $stashrawline=""; |
| 2229 | |
| 2230 | my $length; |
| 2231 | my $indent; |
| 2232 | my $previndent=0; |
| 2233 | my $stashindent=0; |
| 2234 | |
| 2235 | our $clean = 1; |
| 2236 | my $signoff = 0; |
| 2237 | my $is_patch = 0; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2238 | my $in_header_lines = $file ? 0 : 1; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2239 | my $in_commit_log = 0; #Scanning lines before patch |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2240 | my $has_commit_log = 0; #Encountered lines before patch |
| 2241 | my $commit_log_possible_stack_dump = 0; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2242 | my $commit_log_long_line = 0; |
| 2243 | my $commit_log_has_diff = 0; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2244 | my $reported_maintainer_file = 0; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2245 | my $non_utf8_charset = 0; |
| 2246 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2247 | my $last_blank_line = 0; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2248 | my $last_coalesced_string_linenr = -1; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2249 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2250 | our @report = (); |
| 2251 | our $cnt_lines = 0; |
| 2252 | our $cnt_error = 0; |
| 2253 | our $cnt_warn = 0; |
| 2254 | our $cnt_chk = 0; |
| 2255 | |
| 2256 | # Trace the real file/line as we go. |
| 2257 | my $realfile = ''; |
| 2258 | my $realline = 0; |
| 2259 | my $realcnt = 0; |
| 2260 | my $here = ''; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2261 | my $context_function; #undef'd unless there's a known function |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2262 | my $in_comment = 0; |
| 2263 | my $comment_edge = 0; |
| 2264 | my $first_line = 0; |
| 2265 | my $p1_prefix = ''; |
| 2266 | |
| 2267 | my $prev_values = 'E'; |
| 2268 | |
| 2269 | # suppression flags |
| 2270 | my %suppress_ifbraces; |
| 2271 | my %suppress_whiletrailers; |
| 2272 | my %suppress_export; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2273 | my $suppress_statement = 0; |
| 2274 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2275 | my %signatures = (); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2276 | |
| 2277 | # Pre-scan the patch sanitizing the lines. |
| 2278 | # Pre-scan the patch looking for any __setup documentation. |
| 2279 | # |
| 2280 | my @setup_docs = (); |
| 2281 | my $setup_docs = 0; |
| 2282 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2283 | my $camelcase_file_seeded = 0; |
| 2284 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2285 | my $checklicenseline = 1; |
| 2286 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2287 | sanitise_line_reset(); |
| 2288 | my $line; |
| 2289 | foreach my $rawline (@rawlines) { |
| 2290 | $linenr++; |
| 2291 | $line = $rawline; |
| 2292 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2293 | push(@fixed, $rawline) if ($fix); |
| 2294 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2295 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
| 2296 | $setup_docs = 0; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2297 | if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2298 | $setup_docs = 1; |
| 2299 | } |
| 2300 | #next; |
| 2301 | } |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2302 | if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2303 | $realline=$1-1; |
| 2304 | if (defined $2) { |
| 2305 | $realcnt=$3+1; |
| 2306 | } else { |
| 2307 | $realcnt=1+1; |
| 2308 | } |
| 2309 | $in_comment = 0; |
| 2310 | |
| 2311 | # Guestimate if this is a continuing comment. Run |
| 2312 | # the context looking for a comment "edge". If this |
| 2313 | # edge is a close comment then we must be in a comment |
| 2314 | # at context start. |
| 2315 | my $edge; |
| 2316 | my $cnt = $realcnt; |
| 2317 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 2318 | next if (defined $rawlines[$ln - 1] && |
| 2319 | $rawlines[$ln - 1] =~ /^-/); |
| 2320 | $cnt--; |
| 2321 | #print "RAW<$rawlines[$ln - 1]>\n"; |
| 2322 | last if (!defined $rawlines[$ln - 1]); |
| 2323 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 2324 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 2325 | ($edge) = $1; |
| 2326 | last; |
| 2327 | } |
| 2328 | } |
| 2329 | if (defined $edge && $edge eq '*/') { |
| 2330 | $in_comment = 1; |
| 2331 | } |
| 2332 | |
| 2333 | # Guestimate if this is a continuing comment. If this |
| 2334 | # is the start of a diff block and this line starts |
| 2335 | # ' *' then it is very likely a comment. |
| 2336 | if (!defined $edge && |
| 2337 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
| 2338 | { |
| 2339 | $in_comment = 1; |
| 2340 | } |
| 2341 | |
| 2342 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 2343 | sanitise_line_reset($in_comment); |
| 2344 | |
| 2345 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
| 2346 | # Standardise the strings and chars within the input to |
| 2347 | # simplify matching -- only bother with positive lines. |
| 2348 | $line = sanitise_line($rawline); |
| 2349 | } |
| 2350 | push(@lines, $line); |
| 2351 | |
| 2352 | if ($realcnt > 1) { |
| 2353 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 2354 | } else { |
| 2355 | $realcnt = 0; |
| 2356 | } |
| 2357 | |
| 2358 | #print "==>$rawline\n"; |
| 2359 | #print "-->$line\n"; |
| 2360 | |
| 2361 | if ($setup_docs && $line =~ /^\+/) { |
| 2362 | push(@setup_docs, $line); |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | $prefix = ''; |
| 2367 | |
| 2368 | $realcnt = 0; |
| 2369 | $linenr = 0; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2370 | $fixlinenr = -1; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2371 | foreach my $line (@lines) { |
| 2372 | $linenr++; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2373 | $fixlinenr++; |
| 2374 | my $sline = $line; #copy of $line |
| 2375 | $sline =~ s/$;/ /g; #with comments as spaces |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2376 | |
| 2377 | my $rawline = $rawlines[$linenr - 1]; |
| 2378 | |
| 2379 | #extract the line range in the file after the patch is applied |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2380 | if (!$in_commit_log && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2381 | $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { |
| 2382 | my $context = $4; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2383 | $is_patch = 1; |
| 2384 | $first_line = $linenr + 1; |
| 2385 | $realline=$1-1; |
| 2386 | if (defined $2) { |
| 2387 | $realcnt=$3+1; |
| 2388 | } else { |
| 2389 | $realcnt=1+1; |
| 2390 | } |
| 2391 | annotate_reset(); |
| 2392 | $prev_values = 'E'; |
| 2393 | |
| 2394 | %suppress_ifbraces = (); |
| 2395 | %suppress_whiletrailers = (); |
| 2396 | %suppress_export = (); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2397 | $suppress_statement = 0; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2398 | if ($context =~ /\b(\w+)\s*\(/) { |
| 2399 | $context_function = $1; |
| 2400 | } else { |
| 2401 | undef $context_function; |
| 2402 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2403 | next; |
| 2404 | |
| 2405 | # track the line number as we move through the hunk, note that |
| 2406 | # new versions of GNU diff omit the leading space on completely |
| 2407 | # blank context lines so we need to count that too. |
| 2408 | } elsif ($line =~ /^( |\+|$)/) { |
| 2409 | $realline++; |
| 2410 | $realcnt-- if ($realcnt != 0); |
| 2411 | |
| 2412 | # Measure the line length and indent. |
| 2413 | ($length, $indent) = line_stats($rawline); |
| 2414 | |
| 2415 | # Track the previous line. |
| 2416 | ($prevline, $stashline) = ($stashline, $line); |
| 2417 | ($previndent, $stashindent) = ($stashindent, $indent); |
| 2418 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 2419 | |
| 2420 | #warn "line<$line>\n"; |
| 2421 | |
| 2422 | } elsif ($realcnt == 1) { |
| 2423 | $realcnt--; |
| 2424 | } |
| 2425 | |
| 2426 | my $hunk_line = ($realcnt != 0); |
| 2427 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2428 | $here = "#$linenr: " if (!$file); |
| 2429 | $here = "#$realline: " if ($file); |
| 2430 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2431 | my $found_file = 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2432 | # extract the filename as it passes |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2433 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 2434 | $realfile = $1; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2435 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2436 | $in_commit_log = 0; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2437 | $found_file = 1; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2438 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2439 | $realfile = $1; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2440 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2441 | $in_commit_log = 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2442 | |
| 2443 | $p1_prefix = $1; |
| 2444 | if (!$file && $tree && $p1_prefix ne '' && |
| 2445 | -e "$root/$p1_prefix") { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2446 | WARN("PATCH_PREFIX", |
| 2447 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | if ($realfile =~ m@^include/asm/@) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2451 | ERROR("MODIFIED_INCLUDE_ASM", |
| 2452 | "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2453 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2454 | $found_file = 1; |
| 2455 | } |
| 2456 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2457 | #make up the handle for any error we report on this line |
| 2458 | if ($showfile) { |
| 2459 | $prefix = "$realfile:$realline: " |
| 2460 | } elsif ($emacs) { |
| 2461 | if ($file) { |
| 2462 | $prefix = "$filename:$realline: "; |
| 2463 | } else { |
| 2464 | $prefix = "$filename:$linenr: "; |
| 2465 | } |
| 2466 | } |
| 2467 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2468 | if ($found_file) { |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2469 | if (is_maintained_obsolete($realfile)) { |
| 2470 | WARN("OBSOLETE", |
| 2471 | "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); |
| 2472 | } |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2473 | if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2474 | $check = 1; |
| 2475 | } else { |
| 2476 | $check = $check_orig; |
| 2477 | } |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2478 | $checklicenseline = 1; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2479 | next; |
| 2480 | } |
| 2481 | |
| 2482 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
| 2483 | |
| 2484 | my $hereline = "$here\n$rawline\n"; |
| 2485 | my $herecurr = "$here\n$rawline\n"; |
| 2486 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
| 2487 | |
| 2488 | $cnt_lines++ if ($realcnt != 0); |
| 2489 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2490 | # Check if the commit log has what seems like a diff which can confuse patch |
| 2491 | if ($in_commit_log && !$commit_log_has_diff && |
| 2492 | (($line =~ m@^\s+diff\b.*a/[\w/]+@ && |
| 2493 | $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || |
| 2494 | $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || |
| 2495 | $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { |
| 2496 | ERROR("DIFF_IN_COMMIT_MSG", |
| 2497 | "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); |
| 2498 | $commit_log_has_diff = 1; |
| 2499 | } |
| 2500 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2501 | # Check for incorrect file permissions |
| 2502 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 2503 | my $permhere = $here . "FILE: $realfile\n"; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2504 | if ($realfile !~ m@scripts/@ && |
| 2505 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
| 2506 | ERROR("EXECUTE_PERMISSIONS", |
| 2507 | "do not set execute permissions for source files\n" . $permhere); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2508 | } |
| 2509 | } |
| 2510 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2511 | # Check the patch for a signoff: |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2512 | if ($line =~ /^\s*signed-off-by:/i) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2513 | $signoff++; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2514 | $in_commit_log = 0; |
| 2515 | } |
| 2516 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2517 | # Check if MAINTAINERS is being updated. If so, there's probably no need to |
| 2518 | # emit the "does MAINTAINERS need updating?" message on file add/move/delete |
| 2519 | if ($line =~ /^\s*MAINTAINERS\s*\|/) { |
| 2520 | $reported_maintainer_file = 1; |
| 2521 | } |
| 2522 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2523 | # Check signature styles |
| 2524 | if (!$in_header_lines && |
| 2525 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
| 2526 | my $space_before = $1; |
| 2527 | my $sign_off = $2; |
| 2528 | my $space_after = $3; |
| 2529 | my $email = $4; |
| 2530 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 2531 | |
| 2532 | if ($sign_off !~ /$signature_tags/) { |
| 2533 | WARN("BAD_SIGN_OFF", |
| 2534 | "Non-standard signature: $sign_off\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2535 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2536 | if (defined $space_before && $space_before ne "") { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2537 | if (WARN("BAD_SIGN_OFF", |
| 2538 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && |
| 2539 | $fix) { |
| 2540 | $fixed[$fixlinenr] = |
| 2541 | "$ucfirst_sign_off $email"; |
| 2542 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2543 | } |
| 2544 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2545 | if (WARN("BAD_SIGN_OFF", |
| 2546 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && |
| 2547 | $fix) { |
| 2548 | $fixed[$fixlinenr] = |
| 2549 | "$ucfirst_sign_off $email"; |
| 2550 | } |
| 2551 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2552 | } |
| 2553 | if (!defined $space_after || $space_after ne " ") { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2554 | if (WARN("BAD_SIGN_OFF", |
| 2555 | "Use a single space after $ucfirst_sign_off\n" . $herecurr) && |
| 2556 | $fix) { |
| 2557 | $fixed[$fixlinenr] = |
| 2558 | "$ucfirst_sign_off $email"; |
| 2559 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | my ($email_name, $email_address, $comment) = parse_email($email); |
| 2563 | my $suggested_email = format_email(($email_name, $email_address)); |
| 2564 | if ($suggested_email eq "") { |
| 2565 | ERROR("BAD_SIGN_OFF", |
| 2566 | "Unrecognized email address: '$email'\n" . $herecurr); |
| 2567 | } else { |
| 2568 | my $dequoted = $suggested_email; |
| 2569 | $dequoted =~ s/^"//; |
| 2570 | $dequoted =~ s/" </ </; |
| 2571 | # Don't force email to have quotes |
| 2572 | # Allow just an angle bracketed address |
| 2573 | if ("$dequoted$comment" ne $email && |
| 2574 | "<$email_address>$comment" ne $email && |
| 2575 | "$suggested_email$comment" ne $email) { |
| 2576 | WARN("BAD_SIGN_OFF", |
| 2577 | "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); |
| 2578 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2579 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2580 | |
| 2581 | # Check for duplicate signatures |
| 2582 | my $sig_nospace = $line; |
| 2583 | $sig_nospace =~ s/\s//g; |
| 2584 | $sig_nospace = lc($sig_nospace); |
| 2585 | if (defined $signatures{$sig_nospace}) { |
| 2586 | WARN("BAD_SIGN_OFF", |
| 2587 | "Duplicate signature\n" . $herecurr); |
| 2588 | } else { |
| 2589 | $signatures{$sig_nospace} = 1; |
| 2590 | } |
| 2591 | } |
| 2592 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2593 | # Check email subject for common tools that don't need to be mentioned |
| 2594 | if ($in_header_lines && |
| 2595 | $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { |
| 2596 | WARN("EMAIL_SUBJECT", |
| 2597 | "A patch subject line should describe the change not the tool that found it\n" . $herecurr); |
| 2598 | } |
| 2599 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2600 | # Check for old stable address |
| 2601 | if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) { |
| 2602 | ERROR("STABLE_ADDRESS", |
| 2603 | "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr); |
| 2604 | } |
| 2605 | |
| 2606 | # Check for unwanted Gerrit info |
| 2607 | if ($in_commit_log && $line =~ /^\s*change-id:/i) { |
| 2608 | ERROR("GERRIT_CHANGE_ID", |
| 2609 | "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); |
| 2610 | } |
| 2611 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2612 | # Check if the commit log is in a possible stack dump |
| 2613 | if ($in_commit_log && !$commit_log_possible_stack_dump && |
| 2614 | ($line =~ /^\s*(?:WARNING:|BUG:)/ || |
| 2615 | $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || |
| 2616 | # timestamp |
| 2617 | $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) { |
| 2618 | # stack dump address |
| 2619 | $commit_log_possible_stack_dump = 1; |
| 2620 | } |
| 2621 | |
| 2622 | # Check for line lengths > 75 in commit log, warn once |
| 2623 | if ($in_commit_log && !$commit_log_long_line && |
| 2624 | length($line) > 75 && |
| 2625 | !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || |
| 2626 | # file delta changes |
| 2627 | $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || |
| 2628 | # filename then : |
| 2629 | $line =~ /^\s*(?:Fixes:|Link:)/i || |
| 2630 | # A Fixes: or Link: line |
| 2631 | $commit_log_possible_stack_dump)) { |
| 2632 | WARN("COMMIT_LOG_LONG_LINE", |
| 2633 | "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); |
| 2634 | $commit_log_long_line = 1; |
| 2635 | } |
| 2636 | |
| 2637 | # Reset possible stack dump if a blank line is found |
| 2638 | if ($in_commit_log && $commit_log_possible_stack_dump && |
| 2639 | $line =~ /^\s*$/) { |
| 2640 | $commit_log_possible_stack_dump = 0; |
| 2641 | } |
| 2642 | |
| 2643 | # Check for git id commit length and improperly formed commit descriptions |
| 2644 | if ($in_commit_log && !$commit_log_possible_stack_dump && |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2645 | $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2646 | $line !~ /^This reverts commit [0-9a-f]{7,40}/ && |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2647 | ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2648 | ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2649 | $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && |
| 2650 | $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { |
| 2651 | my $init_char = "c"; |
| 2652 | my $orig_commit = ""; |
| 2653 | my $short = 1; |
| 2654 | my $long = 0; |
| 2655 | my $case = 1; |
| 2656 | my $space = 1; |
| 2657 | my $hasdesc = 0; |
| 2658 | my $hasparens = 0; |
| 2659 | my $id = '0123456789ab'; |
| 2660 | my $orig_desc = "commit description"; |
| 2661 | my $description = ""; |
| 2662 | |
| 2663 | if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { |
| 2664 | $init_char = $1; |
| 2665 | $orig_commit = lc($2); |
| 2666 | } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { |
| 2667 | $orig_commit = lc($1); |
| 2668 | } |
| 2669 | |
| 2670 | $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); |
| 2671 | $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); |
| 2672 | $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); |
| 2673 | $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); |
| 2674 | if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { |
| 2675 | $orig_desc = $1; |
| 2676 | $hasparens = 1; |
| 2677 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && |
| 2678 | defined $rawlines[$linenr] && |
| 2679 | $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { |
| 2680 | $orig_desc = $1; |
| 2681 | $hasparens = 1; |
| 2682 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && |
| 2683 | defined $rawlines[$linenr] && |
| 2684 | $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { |
| 2685 | $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; |
| 2686 | $orig_desc = $1; |
| 2687 | $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; |
| 2688 | $orig_desc .= " " . $1; |
| 2689 | $hasparens = 1; |
| 2690 | } |
| 2691 | |
| 2692 | ($id, $description) = git_commit_info($orig_commit, |
| 2693 | $id, $orig_desc); |
| 2694 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2695 | if (defined($id) && |
| 2696 | ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2697 | ERROR("GIT_COMMIT_ID", |
| 2698 | "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); |
| 2699 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | # Check for added, moved or deleted files |
| 2703 | if (!$reported_maintainer_file && !$in_commit_log && |
| 2704 | ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || |
| 2705 | $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || |
| 2706 | ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && |
| 2707 | (defined($1) || defined($2))))) { |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2708 | $is_patch = 1; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2709 | $reported_maintainer_file = 1; |
| 2710 | WARN("FILE_PATH_CHANGES", |
| 2711 | "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | # Check for wrappage within a valid hunk of the file |
| 2715 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2716 | ERROR("CORRUPTED_PATCH", |
| 2717 | "patch seems to be corrupt (line wrapped?)\n" . |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2718 | $herecurr) if (!$emitted_corrupt++); |
| 2719 | } |
| 2720 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2721 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 2722 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
| 2723 | $rawline !~ m/^$UTF8*$/) { |
| 2724 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 2725 | |
| 2726 | my $blank = copy_spacing($rawline); |
| 2727 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 2728 | my $hereptr = "$hereline$ptr\n"; |
| 2729 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2730 | CHK("INVALID_UTF8", |
| 2731 | "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); |
| 2732 | } |
| 2733 | |
| 2734 | # Check if it's the start of a commit log |
| 2735 | # (not a header line and we haven't seen the patch filename) |
| 2736 | if ($in_header_lines && $realfile =~ /^$/ && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2737 | !($rawline =~ /^\s+(?:\S|$)/ || |
| 2738 | $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2739 | $in_header_lines = 0; |
| 2740 | $in_commit_log = 1; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2741 | $has_commit_log = 1; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 2745 | # declined it, i.e defined some charset where it is missing. |
| 2746 | if ($in_header_lines && |
| 2747 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 2748 | $1 !~ /utf-8/i) { |
| 2749 | $non_utf8_charset = 1; |
| 2750 | } |
| 2751 | |
| 2752 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
| 2753 | $rawline =~ /$NON_ASCII_UTF8/) { |
| 2754 | WARN("UTF8_BEFORE_PATCH", |
| 2755 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2756 | } |
| 2757 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2758 | # Check for absolute kernel paths in commit message |
| 2759 | if ($tree && $in_commit_log) { |
| 2760 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 2761 | my $file = $1; |
| 2762 | |
| 2763 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 2764 | check_absolute_file($1, $herecurr)) { |
| 2765 | # |
| 2766 | } else { |
| 2767 | check_absolute_file($file, $herecurr); |
| 2768 | } |
| 2769 | } |
| 2770 | } |
| 2771 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2772 | # Check for various typo / spelling mistakes |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2773 | if (defined($misspellings) && |
| 2774 | ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { |
| 2775 | while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2776 | my $typo = $1; |
| 2777 | my $typo_fix = $spelling_fix{lc($typo)}; |
| 2778 | $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); |
| 2779 | $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2780 | my $msg_level = \&WARN; |
| 2781 | $msg_level = \&CHK if ($file); |
| 2782 | if (&{$msg_level}("TYPO_SPELLING", |
| 2783 | "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2784 | $fix) { |
| 2785 | $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; |
| 2786 | } |
| 2787 | } |
| 2788 | } |
| 2789 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2790 | # ignore non-hunk lines and lines being removed |
| 2791 | next if (!$hunk_line || $line =~ /^-/); |
| 2792 | |
| 2793 | #trailing whitespace |
| 2794 | if ($line =~ /^\+.*\015/) { |
| 2795 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2796 | if (ERROR("DOS_LINE_ENDINGS", |
| 2797 | "DOS line endings\n" . $herevet) && |
| 2798 | $fix) { |
| 2799 | $fixed[$fixlinenr] =~ s/[\s\015]+$//; |
| 2800 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2801 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 2802 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2803 | if (ERROR("TRAILING_WHITESPACE", |
| 2804 | "trailing whitespace\n" . $herevet) && |
| 2805 | $fix) { |
| 2806 | $fixed[$fixlinenr] =~ s/\s+$//; |
| 2807 | } |
| 2808 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2809 | $rpt_cleaners = 1; |
| 2810 | } |
| 2811 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2812 | # Check for FSF mailing addresses. |
| 2813 | if ($rawline =~ /\bwrite to the Free/i || |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2814 | $rawline =~ /\b675\s+Mass\s+Ave/i || |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2815 | $rawline =~ /\b59\s+Temple\s+Pl/i || |
| 2816 | $rawline =~ /\b51\s+Franklin\s+St/i) { |
| 2817 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2818 | my $msg_level = \&ERROR; |
| 2819 | $msg_level = \&CHK if ($file); |
| 2820 | &{$msg_level}("FSF_MAILING_ADDRESS", |
| 2821 | "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) |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2822 | } |
| 2823 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2824 | # check for Kconfig help text having a real description |
| 2825 | # Only applies when adding the entry originally, after that we do not have |
| 2826 | # sufficient context to determine whether it is indeed long enough. |
| 2827 | if ($realfile =~ /Kconfig/ && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2828 | # 'choice' is usually the last thing on the line (though |
| 2829 | # Kconfig supports named choices), so use a word boundary |
| 2830 | # (\b) rather than a whitespace character (\s) |
| 2831 | $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2832 | my $length = 0; |
| 2833 | my $cnt = $realcnt; |
| 2834 | my $ln = $linenr + 1; |
| 2835 | my $f; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2836 | my $is_start = 0; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2837 | my $is_end = 0; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2838 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2839 | $f = $lines[$ln - 1]; |
| 2840 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 2841 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2842 | |
| 2843 | next if ($f =~ /^-/); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2844 | last if (!$file && $f =~ /^\@\@/); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2845 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2846 | if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2847 | $is_start = 1; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2848 | } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { |
| 2849 | if ($lines[$ln - 1] =~ "---help---") { |
| 2850 | WARN("CONFIG_DESCRIPTION", |
| 2851 | "prefer 'help' over '---help---' for new help texts\n" . $herecurr); |
| 2852 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2853 | $length = -1; |
| 2854 | } |
| 2855 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2856 | $f =~ s/^.//; |
| 2857 | $f =~ s/#.*//; |
| 2858 | $f =~ s/^\s+//; |
| 2859 | next if ($f =~ /^$/); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2860 | |
| 2861 | # This only checks context lines in the patch |
| 2862 | # and so hopefully shouldn't trigger false |
| 2863 | # positives, even though some of these are |
| 2864 | # common words in help texts |
| 2865 | if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| |
| 2866 | if|endif|menu|endmenu|source)\b/x) { |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 2867 | $is_end = 1; |
| 2868 | last; |
| 2869 | } |
| 2870 | $length++; |
| 2871 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2872 | if ($is_start && $is_end && $length < $min_conf_desc_length) { |
| 2873 | WARN("CONFIG_DESCRIPTION", |
| 2874 | "please write a paragraph that describes the config symbol fully\n" . $herecurr); |
| 2875 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2876 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
| 2877 | } |
| 2878 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2879 | # check for MAINTAINERS entries that don't have the right form |
| 2880 | if ($realfile =~ /^MAINTAINERS$/ && |
| 2881 | $rawline =~ /^\+[A-Z]:/ && |
| 2882 | $rawline !~ /^\+[A-Z]:\t\S/) { |
| 2883 | if (WARN("MAINTAINERS_STYLE", |
| 2884 | "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && |
| 2885 | $fix) { |
| 2886 | $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; |
| 2887 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2890 | # discourage the use of boolean for type definition attributes of Kconfig options |
| 2891 | if ($realfile =~ /Kconfig/ && |
| 2892 | $line =~ /^\+\s*\bboolean\b/) { |
| 2893 | WARN("CONFIG_TYPE_BOOLEAN", |
| 2894 | "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); |
| 2895 | } |
| 2896 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2897 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 2898 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 2899 | my $flag = $1; |
| 2900 | my $replacement = { |
| 2901 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 2902 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 2903 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 2904 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 2905 | }; |
| 2906 | |
| 2907 | WARN("DEPRECATED_VARIABLE", |
| 2908 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2911 | # check for DT compatible documentation |
| 2912 | if (defined $root && |
| 2913 | (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || |
| 2914 | ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { |
| 2915 | |
| 2916 | my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; |
| 2917 | |
| 2918 | my $dt_path = $root . "/Documentation/devicetree/bindings/"; |
| 2919 | my $vp_file = $dt_path . "vendor-prefixes.txt"; |
| 2920 | |
| 2921 | foreach my $compat (@compats) { |
| 2922 | my $compat2 = $compat; |
| 2923 | $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; |
| 2924 | my $compat3 = $compat; |
| 2925 | $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; |
| 2926 | `grep -Erq "$compat|$compat2|$compat3" $dt_path`; |
| 2927 | if ( $? >> 8 ) { |
| 2928 | WARN("UNDOCUMENTED_DT_STRING", |
| 2929 | "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); |
| 2930 | } |
| 2931 | |
| 2932 | next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; |
| 2933 | my $vendor = $1; |
| 2934 | `grep -Eq "^$vendor\\b" $vp_file`; |
| 2935 | if ( $? >> 8 ) { |
| 2936 | WARN("UNDOCUMENTED_DT_STRING", |
| 2937 | "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); |
| 2938 | } |
| 2939 | } |
| 2940 | } |
| 2941 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2942 | # check for using SPDX license tag at beginning of files |
| 2943 | if ($realline == $checklicenseline) { |
| 2944 | if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { |
| 2945 | $checklicenseline = 2; |
| 2946 | } elsif ($rawline =~ /^\+/) { |
| 2947 | my $comment = ""; |
| 2948 | if ($realfile =~ /\.(h|s|S)$/) { |
| 2949 | $comment = '/*'; |
| 2950 | } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { |
| 2951 | $comment = '//'; |
| 2952 | } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) { |
| 2953 | $comment = '#'; |
| 2954 | } elsif ($realfile =~ /\.rst$/) { |
| 2955 | $comment = '..'; |
| 2956 | } |
| 2957 | |
| 2958 | if ($comment !~ /^$/ && |
| 2959 | $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) { |
| 2960 | WARN("SPDX_LICENSE_TAG", |
| 2961 | "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); |
| 2962 | } |
| 2963 | } |
| 2964 | } |
| 2965 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2966 | # check we are in a valid source file if not then ignore this hunk |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 2967 | next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2968 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2969 | # line length limit (with some exclusions) |
| 2970 | # |
| 2971 | # There are a few types of lines that may extend beyond $max_line_length: |
| 2972 | # logging functions like pr_info that end in a string |
| 2973 | # lines with a single string |
| 2974 | # #defines that are a single string |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2975 | # lines with an RFC3986 like URL |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2976 | # |
| 2977 | # There are 3 different line length message types: |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 2978 | # LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2979 | # LONG_LINE_STRING a string starts before but extends beyond $max_line_length |
| 2980 | # LONG_LINE all other lines longer than $max_line_length |
| 2981 | # |
| 2982 | # if LONG_LINE is ignored, the other 2 types are also ignored |
| 2983 | # |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 2984 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2985 | if ($line =~ /^\+/ && $length > $max_line_length) { |
| 2986 | my $msg_type = "LONG_LINE"; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 2987 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2988 | # Check the allowed long line types first |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 2989 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 2990 | # logging functions that end in a string that starts |
| 2991 | # before $max_line_length |
| 2992 | if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && |
| 2993 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 2994 | $msg_type = ""; |
| 2995 | |
| 2996 | # lines with only strings (w/ possible termination) |
| 2997 | # #defines with only strings |
| 2998 | } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || |
| 2999 | $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { |
| 3000 | $msg_type = ""; |
| 3001 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3002 | # More special cases |
| 3003 | } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || |
| 3004 | $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { |
| 3005 | $msg_type = ""; |
| 3006 | |
| 3007 | # URL ($rawline is used in case the URL is in a comment) |
| 3008 | } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 3009 | $msg_type = ""; |
| 3010 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3011 | # Otherwise set the alternate message types |
| 3012 | |
| 3013 | # a comment starts before $max_line_length |
| 3014 | } elsif ($line =~ /($;[\s$;]*)$/ && |
| 3015 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3016 | $msg_type = "LONG_LINE_COMMENT" |
| 3017 | |
| 3018 | # a quoted string starts before $max_line_length |
| 3019 | } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && |
| 3020 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3021 | $msg_type = "LONG_LINE_STRING" |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3022 | } |
| 3023 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3024 | if ($msg_type ne "" && |
| 3025 | (show_type("LONG_LINE") || show_type($msg_type))) { |
| 3026 | WARN($msg_type, |
| 3027 | "line over $max_line_length characters\n" . $herecurr); |
| 3028 | } |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3031 | # check for adding lines without a newline. |
| 3032 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3033 | WARN("MISSING_EOF_NEWLINE", |
| 3034 | "adding a line without newline at end of file\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3037 | # check we are in a valid source file C or perl if not then ignore this hunk |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3038 | next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3039 | |
| 3040 | # at the beginning of a line any tabs must come first and anything |
| 3041 | # more than 8 must use tabs. |
| 3042 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 3043 | $rawline =~ /^\+\s* \s*/) { |
| 3044 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3045 | $rpt_cleaners = 1; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3046 | if (ERROR("CODE_INDENT", |
| 3047 | "code indent should use tabs where possible\n" . $herevet) && |
| 3048 | $fix) { |
| 3049 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
| 3050 | } |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | # check for space before tabs. |
| 3054 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 3055 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3056 | if (WARN("SPACE_BEFORE_TAB", |
| 3057 | "please, no space before tabs\n" . $herevet) && |
| 3058 | $fix) { |
| 3059 | while ($fixed[$fixlinenr] =~ |
| 3060 | s/(^\+.*) {8,8}\t/$1\t\t/) {} |
| 3061 | while ($fixed[$fixlinenr] =~ |
| 3062 | s/(^\+.*) +\t/$1\t/) {} |
| 3063 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3064 | } |
| 3065 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3066 | # check for assignments on the start of a line |
| 3067 | if ($sline =~ /^\+\s+($Assignment)[^=]/) { |
| 3068 | CHK("ASSIGNMENT_CONTINUATIONS", |
| 3069 | "Assignment operator '$1' should be on the previous line\n" . $hereprev); |
| 3070 | } |
| 3071 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3072 | # check for && or || at the start of a line |
| 3073 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
| 3074 | CHK("LOGICAL_CONTINUATIONS", |
| 3075 | "Logical continuations should be on the previous line\n" . $hereprev); |
| 3076 | } |
| 3077 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 3078 | # check indentation starts on a tab stop |
| 3079 | if ($^V && $^V ge 5.10.0 && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3080 | $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 3081 | my $indent = length($1); |
| 3082 | if ($indent % 8) { |
| 3083 | if (WARN("TABSTOP", |
| 3084 | "Statements should start on a tabstop\n" . $herecurr) && |
| 3085 | $fix) { |
| 3086 | $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e; |
| 3087 | } |
| 3088 | } |
| 3089 | } |
| 3090 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3091 | # check multi-line statement indentation matches previous line |
| 3092 | if ($^V && $^V ge 5.10.0 && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3093 | $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3094 | $prevline =~ /^\+(\t*)(.*)$/; |
| 3095 | my $oldindent = $1; |
| 3096 | my $rest = $2; |
| 3097 | |
| 3098 | my $pos = pos_last_openparen($rest); |
| 3099 | if ($pos >= 0) { |
| 3100 | $line =~ /^(\+| )([ \t]*)/; |
| 3101 | my $newindent = $2; |
| 3102 | |
| 3103 | my $goodtabindent = $oldindent . |
| 3104 | "\t" x ($pos / 8) . |
| 3105 | " " x ($pos % 8); |
| 3106 | my $goodspaceindent = $oldindent . " " x $pos; |
| 3107 | |
| 3108 | if ($newindent ne $goodtabindent && |
| 3109 | $newindent ne $goodspaceindent) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3110 | |
| 3111 | if (CHK("PARENTHESIS_ALIGNMENT", |
| 3112 | "Alignment should match open parenthesis\n" . $hereprev) && |
| 3113 | $fix && $line =~ /^\+/) { |
| 3114 | $fixed[$fixlinenr] =~ |
| 3115 | s/^\+[ \t]*/\+$goodtabindent/; |
| 3116 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3117 | } |
| 3118 | } |
| 3119 | } |
| 3120 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3121 | # check for space after cast like "(int) foo" or "(struct foo) bar" |
| 3122 | # avoid checking a few false positives: |
| 3123 | # "sizeof(<type>)" or "__alignof__(<type>)" |
| 3124 | # function pointer declarations like "(*foo)(int) = bar;" |
| 3125 | # structure definitions like "(struct foo) { 0 };" |
| 3126 | # multiline macros that define functions |
| 3127 | # known attributes or the __attribute__ keyword |
| 3128 | if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && |
| 3129 | (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3130 | if (CHK("SPACING", |
| 3131 | "No space is necessary after a cast\n" . $herecurr) && |
| 3132 | $fix) { |
| 3133 | $fixed[$fixlinenr] =~ |
| 3134 | s/(\(\s*$Type\s*\))[ \t]+/$1/; |
| 3135 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3136 | } |
| 3137 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3138 | # Block comment styles |
| 3139 | # Networking with an initial /* |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3140 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3141 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
| 3142 | $rawline =~ /^\+[ \t]*\*/ && |
| 3143 | $realline > 2) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3144 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 3145 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 3146 | } |
| 3147 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3148 | # Block comments use * on subsequent lines |
| 3149 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment |
| 3150 | $prevrawline =~ /^\+.*?\/\*/ && #starting /* |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3151 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
| 3152 | $rawline =~ /^\+/ && #line is new |
| 3153 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3154 | WARN("BLOCK_COMMENT_STYLE", |
| 3155 | "Block comments use * on subsequent lines\n" . $hereprev); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3156 | } |
| 3157 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3158 | # Block comments use */ on trailing lines |
| 3159 | if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3160 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 3161 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 3162 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3163 | WARN("BLOCK_COMMENT_STYLE", |
| 3164 | "Block comments use a trailing */ on a separate line\n" . $herecurr); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3165 | } |
| 3166 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 3167 | # Block comment * alignment |
| 3168 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment |
| 3169 | $line =~ /^\+[ \t]*$;/ && #leading comment |
| 3170 | $rawline =~ /^\+[ \t]*\*/ && #leading * |
| 3171 | (($prevrawline =~ /^\+.*?\/\*/ && #leading /* |
| 3172 | $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ |
| 3173 | $prevrawline =~ /^\+[ \t]*\*/)) { #leading * |
| 3174 | my $oldindent; |
| 3175 | $prevrawline =~ m@^\+([ \t]*/?)\*@; |
| 3176 | if (defined($1)) { |
| 3177 | $oldindent = expand_tabs($1); |
| 3178 | } else { |
| 3179 | $prevrawline =~ m@^\+(.*/?)\*@; |
| 3180 | $oldindent = expand_tabs($1); |
| 3181 | } |
| 3182 | $rawline =~ m@^\+([ \t]*)\*@; |
| 3183 | my $newindent = $1; |
| 3184 | $newindent = expand_tabs($newindent); |
| 3185 | if (length($oldindent) ne length($newindent)) { |
| 3186 | WARN("BLOCK_COMMENT_STYLE", |
| 3187 | "Block comments should align the * on each line\n" . $hereprev); |
| 3188 | } |
| 3189 | } |
| 3190 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3191 | # check for missing blank lines after struct/union declarations |
| 3192 | # with exceptions for various attributes and macros |
| 3193 | if ($prevline =~ /^[\+ ]};?\s*$/ && |
| 3194 | $line =~ /^\+/ && |
| 3195 | !($line =~ /^\+\s*$/ || |
| 3196 | $line =~ /^\+\s*EXPORT_SYMBOL/ || |
| 3197 | $line =~ /^\+\s*MODULE_/i || |
| 3198 | $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || |
| 3199 | $line =~ /^\+[a-z_]*init/ || |
| 3200 | $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || |
| 3201 | $line =~ /^\+\s*DECLARE/ || |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3202 | $line =~ /^\+\s*builtin_[\w_]*driver/ || |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3203 | $line =~ /^\+\s*__setup/)) { |
| 3204 | if (CHK("LINE_SPACING", |
| 3205 | "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && |
| 3206 | $fix) { |
| 3207 | fix_insert_line($fixlinenr, "\+"); |
| 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | # check for multiple consecutive blank lines |
| 3212 | if ($prevline =~ /^[\+ ]\s*$/ && |
| 3213 | $line =~ /^\+\s*$/ && |
| 3214 | $last_blank_line != ($linenr - 1)) { |
| 3215 | if (CHK("LINE_SPACING", |
| 3216 | "Please don't use multiple blank lines\n" . $hereprev) && |
| 3217 | $fix) { |
| 3218 | fix_delete_line($fixlinenr, $rawline); |
| 3219 | } |
| 3220 | |
| 3221 | $last_blank_line = $linenr; |
| 3222 | } |
| 3223 | |
| 3224 | # check for missing blank lines after declarations |
| 3225 | if ($sline =~ /^\+\s+\S/ && #Not at char 1 |
| 3226 | # actual declarations |
| 3227 | ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
| 3228 | # function pointer declarations |
| 3229 | $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
| 3230 | # foo bar; where foo is some local typedef or #define |
| 3231 | $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
| 3232 | # known declaration macros |
| 3233 | $prevline =~ /^\+\s+$declaration_macros/) && |
| 3234 | # for "else if" which can look like "$Ident $Ident" |
| 3235 | !($prevline =~ /^\+\s+$c90_Keywords\b/ || |
| 3236 | # other possible extensions of declaration lines |
| 3237 | $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || |
| 3238 | # not starting a section or a macro "\" extended line |
| 3239 | $prevline =~ /(?:\{\s*|\\)$/) && |
| 3240 | # looks like a declaration |
| 3241 | !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
| 3242 | # function pointer declarations |
| 3243 | $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
| 3244 | # foo bar; where foo is some local typedef or #define |
| 3245 | $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
| 3246 | # known declaration macros |
| 3247 | $sline =~ /^\+\s+$declaration_macros/ || |
| 3248 | # start of struct or union or enum |
| 3249 | $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || |
| 3250 | # start or end of block or continuation of declaration |
| 3251 | $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || |
| 3252 | # bitfield continuation |
| 3253 | $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || |
| 3254 | # other possible extensions of declaration lines |
| 3255 | $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && |
| 3256 | # indentation of previous and current line are the same |
| 3257 | (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { |
| 3258 | if (WARN("LINE_SPACING", |
| 3259 | "Missing a blank line after declarations\n" . $hereprev) && |
| 3260 | $fix) { |
| 3261 | fix_insert_line($fixlinenr, "\+"); |
| 3262 | } |
| 3263 | } |
| 3264 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3265 | # check for spaces at the beginning of a line. |
| 3266 | # Exceptions: |
| 3267 | # 1) within comments |
| 3268 | # 2) indented preprocessor commands |
| 3269 | # 3) hanging labels |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3270 | if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3271 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3272 | if (WARN("LEADING_SPACE", |
| 3273 | "please, no spaces at the start of a line\n" . $herevet) && |
| 3274 | $fix) { |
| 3275 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
| 3276 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | # check we are in a valid C source file if not then ignore this hunk |
| 3280 | next if ($realfile !~ /\.(h|c)$/); |
| 3281 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3282 | # check for unusual line ending [ or ( |
| 3283 | if ($line =~ /^\+.*([\[\(])\s*$/) { |
| 3284 | CHK("OPEN_ENDED_LINE", |
| 3285 | "Lines should not end with a '$1'\n" . $herecurr); |
| 3286 | } |
| 3287 | |
| 3288 | # check if this appears to be the start function declaration, save the name |
| 3289 | if ($sline =~ /^\+\{\s*$/ && |
| 3290 | $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { |
| 3291 | $context_function = $1; |
| 3292 | } |
| 3293 | |
| 3294 | # check if this appears to be the end of function declaration |
| 3295 | if ($sline =~ /^\+\}\s*$/) { |
| 3296 | undef $context_function; |
| 3297 | } |
| 3298 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3299 | # check indentation of any line with a bare else |
| 3300 | # (but not if it is a multiple line "if (foo) return bar; else return baz;") |
| 3301 | # if the previous line is a break or return and is indented 1 tab more... |
| 3302 | if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { |
| 3303 | my $tabs = length($1) + 1; |
| 3304 | if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || |
| 3305 | ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && |
| 3306 | defined $lines[$linenr] && |
| 3307 | $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { |
| 3308 | WARN("UNNECESSARY_ELSE", |
| 3309 | "else is not generally useful after a break or return\n" . $hereprev); |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | # check indentation of a line with a break; |
| 3314 | # if the previous line is a goto or return and is indented the same # of tabs |
| 3315 | if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { |
| 3316 | my $tabs = $1; |
| 3317 | if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { |
| 3318 | WARN("UNNECESSARY_BREAK", |
| 3319 | "break is not useful after a goto or return\n" . $hereprev); |
| 3320 | } |
| 3321 | } |
| 3322 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3323 | # check for RCS/CVS revision markers |
| 3324 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3325 | WARN("CVS_KEYWORD", |
| 3326 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3327 | } |
| 3328 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3329 | # check for old HOTPLUG __dev<foo> section markings |
| 3330 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 3331 | WARN("HOTPLUG_SECTION", |
| 3332 | "Using $1 is unnecessary\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | # Check for potential 'bare' types |
| 3336 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 3337 | $realline_next); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3338 | #print "LINE<$line>\n"; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3339 | if ($linenr > $suppress_statement && |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3340 | $realcnt && $sline =~ /.\s*\S/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3341 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 3342 | ctx_statement_block($linenr, $realcnt, 0); |
| 3343 | $stat =~ s/\n./\n /g; |
| 3344 | $cond =~ s/\n./\n /g; |
| 3345 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3346 | #print "linenr<$linenr> <$stat>\n"; |
| 3347 | # If this statement has no statement boundaries within |
| 3348 | # it there is no point in retrying a statement scan |
| 3349 | # until we hit end of it. |
| 3350 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 3351 | if ($frag !~ /(?:{|;)/) { |
| 3352 | #print "skip<$line_nr_next>\n"; |
| 3353 | $suppress_statement = $line_nr_next; |
| 3354 | } |
| 3355 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3356 | # Find the real next line. |
| 3357 | $realline_next = $line_nr_next; |
| 3358 | if (defined $realline_next && |
| 3359 | (!defined $lines[$realline_next - 1] || |
| 3360 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 3361 | $realline_next++; |
| 3362 | } |
| 3363 | |
| 3364 | my $s = $stat; |
| 3365 | $s =~ s/{.*$//s; |
| 3366 | |
| 3367 | # Ignore goto labels. |
| 3368 | if ($s =~ /$Ident:\*$/s) { |
| 3369 | |
| 3370 | # Ignore functions being called |
| 3371 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
| 3372 | |
| 3373 | } elsif ($s =~ /^.\s*else\b/s) { |
| 3374 | |
| 3375 | # declarations always start with types |
| 3376 | } 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) { |
| 3377 | my $type = $1; |
| 3378 | $type =~ s/\s+/ /g; |
| 3379 | possible($type, "A:" . $s); |
| 3380 | |
| 3381 | # definitions in global scope can only start with types |
| 3382 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
| 3383 | possible($1, "B:" . $s); |
| 3384 | } |
| 3385 | |
| 3386 | # any (foo ... *) is a pointer cast, and foo is a type |
| 3387 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
| 3388 | possible($1, "C:" . $s); |
| 3389 | } |
| 3390 | |
| 3391 | # Check for any sort of function declaration. |
| 3392 | # int foo(something bar, other baz); |
| 3393 | # void (*store_gdt)(x86_descr_ptr *); |
| 3394 | if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { |
| 3395 | my ($name_len) = length($1); |
| 3396 | |
| 3397 | my $ctx = $s; |
| 3398 | substr($ctx, 0, $name_len + 1, ''); |
| 3399 | $ctx =~ s/\)[^\)]*$//; |
| 3400 | |
| 3401 | for my $arg (split(/\s*,\s*/, $ctx)) { |
| 3402 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { |
| 3403 | |
| 3404 | possible($1, "D:" . $s); |
| 3405 | } |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | } |
| 3410 | |
| 3411 | # |
| 3412 | # Checks which may be anchored in the context. |
| 3413 | # |
| 3414 | |
| 3415 | # Check for switch () and associated case and default |
| 3416 | # statements should be at the same indent. |
| 3417 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 3418 | my $err = ''; |
| 3419 | my $sep = ''; |
| 3420 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 3421 | shift(@ctx); |
| 3422 | for my $ctx (@ctx) { |
| 3423 | my ($clen, $cindent) = line_stats($ctx); |
| 3424 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 3425 | $indent != $cindent) { |
| 3426 | $err .= "$sep$ctx\n"; |
| 3427 | $sep = ''; |
| 3428 | } else { |
| 3429 | $sep = "[...]\n"; |
| 3430 | } |
| 3431 | } |
| 3432 | if ($err ne '') { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3433 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 3434 | "switch and case should be at the same indent\n$hereline$err"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 3439 | # or if that brace on the next line is for something else |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3440 | if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3441 | my $pre_ctx = "$1$2"; |
| 3442 | |
| 3443 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3444 | |
| 3445 | if ($line =~ /^\+\t{6,}/) { |
| 3446 | WARN("DEEP_INDENTATION", |
| 3447 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 3448 | } |
| 3449 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3450 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 3451 | my $ctx = join("\n", @ctx); |
| 3452 | |
| 3453 | my $ctx_ln = $linenr; |
| 3454 | my $ctx_skip = $realcnt; |
| 3455 | |
| 3456 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 3457 | defined $lines[$ctx_ln - 1] && |
| 3458 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 3459 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 3460 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
| 3461 | $ctx_ln++; |
| 3462 | } |
| 3463 | |
| 3464 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 3465 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; |
| 3466 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3467 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3468 | ERROR("OPEN_BRACE", |
| 3469 | "that open brace { should be on the previous line\n" . |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3470 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3471 | } |
| 3472 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 3473 | $ctx =~ /\)\s*\;\s*$/ && |
| 3474 | defined $lines[$ctx_ln - 1]) |
| 3475 | { |
| 3476 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 3477 | if ($nindent > $indent) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3478 | WARN("TRAILING_SEMICOLON", |
| 3479 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3480 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | # Check relative indent for conditionals and blocks. |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3486 | if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3487 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 3488 | ctx_statement_block($linenr, $realcnt, 0) |
| 3489 | if (!defined $stat); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3490 | my ($s, $c) = ($stat, $cond); |
| 3491 | |
| 3492 | substr($s, 0, length($c), ''); |
| 3493 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3494 | # remove inline comments |
| 3495 | $s =~ s/$;/ /g; |
| 3496 | $c =~ s/$;/ /g; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3497 | |
| 3498 | # Find out how long the conditional actually is. |
| 3499 | my @newlines = ($c =~ /\n/gs); |
| 3500 | my $cond_lines = 1 + $#newlines; |
| 3501 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3502 | # Make sure we remove the line prefixes as we have |
| 3503 | # none on the first line, and are going to readd them |
| 3504 | # where necessary. |
| 3505 | $s =~ s/\n./\n/gs; |
| 3506 | while ($s =~ /\n\s+\\\n/) { |
| 3507 | $cond_lines += $s =~ s/\n\s+\\\n/\n/g; |
| 3508 | } |
| 3509 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3510 | # We want to check the first line inside the block |
| 3511 | # starting at the end of the conditional, so remove: |
| 3512 | # 1) any blank line termination |
| 3513 | # 2) any opening brace { on end of the line |
| 3514 | # 3) any do (...) { |
| 3515 | my $continuation = 0; |
| 3516 | my $check = 0; |
| 3517 | $s =~ s/^.*\bdo\b//; |
| 3518 | $s =~ s/^\s*{//; |
| 3519 | if ($s =~ s/^\s*\\//) { |
| 3520 | $continuation = 1; |
| 3521 | } |
| 3522 | if ($s =~ s/^\s*?\n//) { |
| 3523 | $check = 1; |
| 3524 | $cond_lines++; |
| 3525 | } |
| 3526 | |
| 3527 | # Also ignore a loop construct at the end of a |
| 3528 | # preprocessor statement. |
| 3529 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 3530 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 3531 | $check = 0; |
| 3532 | } |
| 3533 | |
| 3534 | my $cond_ptr = -1; |
| 3535 | $continuation = 0; |
| 3536 | while ($cond_ptr != $cond_lines) { |
| 3537 | $cond_ptr = $cond_lines; |
| 3538 | |
| 3539 | # If we see an #else/#elif then the code |
| 3540 | # is not linear. |
| 3541 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 3542 | $check = 0; |
| 3543 | } |
| 3544 | |
| 3545 | # Ignore: |
| 3546 | # 1) blank lines, they should be at 0, |
| 3547 | # 2) preprocessor lines, and |
| 3548 | # 3) labels. |
| 3549 | if ($continuation || |
| 3550 | $s =~ /^\s*?\n/ || |
| 3551 | $s =~ /^\s*#\s*?/ || |
| 3552 | $s =~ /^\s*$Ident\s*:/) { |
| 3553 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
| 3554 | if ($s =~ s/^.*?\n//) { |
| 3555 | $cond_lines++; |
| 3556 | } |
| 3557 | } |
| 3558 | } |
| 3559 | |
| 3560 | my (undef, $sindent) = line_stats("+" . $s); |
| 3561 | my $stat_real = raw_line($linenr, $cond_lines); |
| 3562 | |
| 3563 | # Check if either of these lines are modified, else |
| 3564 | # this is not this patch's fault. |
| 3565 | if (!defined($stat_real) || |
| 3566 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 3567 | $check = 0; |
| 3568 | } |
| 3569 | if (defined($stat_real) && $cond_lines > 1) { |
| 3570 | $stat_real = "[...]\n$stat_real"; |
| 3571 | } |
| 3572 | |
| 3573 | #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"; |
| 3574 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3575 | if ($check && $s ne '' && |
| 3576 | (($sindent % 8) != 0 || |
| 3577 | ($sindent < $indent) || |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3578 | ($sindent == $indent && |
| 3579 | ($s !~ /^\s*(?:\}|\{|else\b)/)) || |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3580 | ($sindent > $indent + 8))) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3581 | WARN("SUSPECT_CODE_INDENT", |
| 3582 | "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3583 | } |
| 3584 | } |
| 3585 | |
| 3586 | # Track the 'values' across context and added lines. |
| 3587 | my $opline = $line; $opline =~ s/^./ /; |
| 3588 | my ($curr_values, $curr_vars) = |
| 3589 | annotate_values($opline . "\n", $prev_values); |
| 3590 | $curr_values = $prev_values . $curr_values; |
| 3591 | if ($dbg_values) { |
| 3592 | my $outline = $opline; $outline =~ s/\t/ /g; |
| 3593 | print "$linenr > .$outline\n"; |
| 3594 | print "$linenr > $curr_values\n"; |
| 3595 | print "$linenr > $curr_vars\n"; |
| 3596 | } |
| 3597 | $prev_values = substr($curr_values, -1); |
| 3598 | |
| 3599 | #ignore lines not being added |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3600 | next if ($line =~ /^[^\+]/); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3601 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 3602 | # check for dereferences that span multiple lines |
| 3603 | if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && |
| 3604 | $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { |
| 3605 | $prevline =~ /($Lval\s*(?:\.|->))\s*$/; |
| 3606 | my $ref = $1; |
| 3607 | $line =~ /^.\s*($Lval)/; |
| 3608 | $ref .= $1; |
| 3609 | $ref =~ s/\s//g; |
| 3610 | WARN("MULTILINE_DEREFERENCE", |
| 3611 | "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); |
| 3612 | } |
| 3613 | |
| 3614 | # check for declarations of signed or unsigned without int |
| 3615 | while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { |
| 3616 | my $type = $1; |
| 3617 | my $var = $2; |
| 3618 | $var = "" if (!defined $var); |
| 3619 | if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { |
| 3620 | my $sign = $1; |
| 3621 | my $pointer = $2; |
| 3622 | |
| 3623 | $pointer = "" if (!defined $pointer); |
| 3624 | |
| 3625 | if (WARN("UNSPECIFIED_INT", |
| 3626 | "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && |
| 3627 | $fix) { |
| 3628 | my $decl = trim($sign) . " int "; |
| 3629 | my $comp_pointer = $pointer; |
| 3630 | $comp_pointer =~ s/\s//g; |
| 3631 | $decl .= $comp_pointer; |
| 3632 | $decl = rtrim($decl) if ($var eq ""); |
| 3633 | $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; |
| 3634 | } |
| 3635 | } |
| 3636 | } |
| 3637 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3638 | # TEST: allow direct testing of the type matcher. |
| 3639 | if ($dbg_type) { |
| 3640 | if ($line =~ /^.\s*$Declare\s*$/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3641 | ERROR("TEST_TYPE", |
| 3642 | "TEST: is type\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3643 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3644 | ERROR("TEST_NOT_TYPE", |
| 3645 | "TEST: is not type ($1 is)\n". $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3646 | } |
| 3647 | next; |
| 3648 | } |
| 3649 | # TEST: allow direct testing of the attribute matcher. |
| 3650 | if ($dbg_attr) { |
| 3651 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3652 | ERROR("TEST_ATTR", |
| 3653 | "TEST: is attr\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3654 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3655 | ERROR("TEST_NOT_ATTR", |
| 3656 | "TEST: is not attr ($1 is)\n". $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3657 | } |
| 3658 | next; |
| 3659 | } |
| 3660 | |
| 3661 | # check for initialisation to aggregates open brace on the next line |
| 3662 | if ($line =~ /^.\s*{/ && |
| 3663 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3664 | if (ERROR("OPEN_BRACE", |
| 3665 | "that open brace { should be on the previous line\n" . $hereprev) && |
| 3666 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 3667 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 3668 | fix_delete_line($fixlinenr, $rawline); |
| 3669 | my $fixedline = $prevrawline; |
| 3670 | $fixedline =~ s/\s*=\s*$/ = {/; |
| 3671 | fix_insert_line($fixlinenr, $fixedline); |
| 3672 | $fixedline = $line; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3673 | $fixedline =~ s/^(.\s*)\{\s*/$1/; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3674 | fix_insert_line($fixlinenr, $fixedline); |
| 3675 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | # |
| 3679 | # Checks which are anchored on the added line. |
| 3680 | # |
| 3681 | |
| 3682 | # check for malformed paths in #include statements (uses RAW line) |
| 3683 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
| 3684 | my $path = $1; |
| 3685 | if ($path =~ m{//}) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3686 | ERROR("MALFORMED_INCLUDE", |
| 3687 | "malformed #include filename\n" . $herecurr); |
| 3688 | } |
| 3689 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 3690 | ERROR("UAPI_INCLUDE", |
| 3691 | "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3692 | } |
| 3693 | } |
| 3694 | |
| 3695 | # no C99 // comments |
| 3696 | if ($line =~ m{//}) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3697 | if (ERROR("C99_COMMENTS", |
| 3698 | "do not use C99 // comments\n" . $herecurr) && |
| 3699 | $fix) { |
| 3700 | my $line = $fixed[$fixlinenr]; |
| 3701 | if ($line =~ /\/\/(.*)$/) { |
| 3702 | my $comment = trim($1); |
| 3703 | $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; |
| 3704 | } |
| 3705 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3706 | } |
| 3707 | # Remove C99 comments. |
| 3708 | $line =~ s@//.*@@; |
| 3709 | $opline =~ s@//.*@@; |
| 3710 | |
| 3711 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 3712 | # the whole statement. |
| 3713 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 3714 | if (defined $realline_next && |
| 3715 | exists $lines[$realline_next - 1] && |
| 3716 | !defined $suppress_export{$realline_next} && |
| 3717 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 3718 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3719 | # Handle definitions which produce identifiers with |
| 3720 | # a prefix: |
| 3721 | # XXX(foo); |
| 3722 | # EXPORT_SYMBOL(something_foo); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3723 | my $name = $1; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3724 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3725 | $name =~ /^${Ident}_$2/) { |
| 3726 | #print "FOO C name<$name>\n"; |
| 3727 | $suppress_export{$realline_next} = 1; |
| 3728 | |
| 3729 | } elsif ($stat !~ /(?: |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3730 | \n.}\s*$| |
| 3731 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 3732 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 3733 | ^.LIST_HEAD\(\Q$name\E\)| |
| 3734 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 3735 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
| 3736 | )/x) { |
| 3737 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 3738 | $suppress_export{$realline_next} = 2; |
| 3739 | } else { |
| 3740 | $suppress_export{$realline_next} = 1; |
| 3741 | } |
| 3742 | } |
| 3743 | if (!defined $suppress_export{$linenr} && |
| 3744 | $prevline =~ /^.\s*$/ && |
| 3745 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 3746 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 3747 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 3748 | $suppress_export{$linenr} = 2; |
| 3749 | } |
| 3750 | if (defined $suppress_export{$linenr} && |
| 3751 | $suppress_export{$linenr} == 2) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3752 | WARN("EXPORT_SYMBOL", |
| 3753 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3754 | } |
| 3755 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3756 | # check for global initialisers. |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3757 | if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3758 | if (ERROR("GLOBAL_INITIALISERS", |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3759 | "do not initialise globals to $1\n" . $herecurr) && |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3760 | $fix) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3761 | $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3762 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3763 | } |
| 3764 | # check for static initialisers. |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3765 | if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3766 | if (ERROR("INITIALISED_STATIC", |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3767 | "do not initialise statics to $1\n" . |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3768 | $herecurr) && |
| 3769 | $fix) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3770 | $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3771 | } |
| 3772 | } |
| 3773 | |
| 3774 | # check for misordered declarations of char/short/int/long with signed/unsigned |
| 3775 | while ($sline =~ m{(\b$TypeMisordered\b)}g) { |
| 3776 | my $tmp = trim($1); |
| 3777 | WARN("MISORDERED_TYPE", |
| 3778 | "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3779 | } |
| 3780 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3781 | # check for static const char * arrays. |
| 3782 | if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3783 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 3784 | "static const char * array should probably be static const char * const\n" . |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3785 | $herecurr); |
| 3786 | } |
| 3787 | |
| 3788 | # check for static char foo[] = "bar" declarations. |
| 3789 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3790 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 3791 | "static char array declaration should probably be static const char\n" . |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 3792 | $herecurr); |
| 3793 | } |
| 3794 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3795 | # check for const <foo> const where <foo> is not a pointer or array type |
| 3796 | if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { |
| 3797 | my $found = $1; |
| 3798 | if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { |
| 3799 | WARN("CONST_CONST", |
| 3800 | "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); |
| 3801 | } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { |
| 3802 | WARN("CONST_CONST", |
| 3803 | "'const $found const' should probably be 'const $found'\n" . $herecurr); |
| 3804 | } |
| 3805 | } |
| 3806 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3807 | # check for non-global char *foo[] = {"bar", ...} declarations. |
| 3808 | if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { |
| 3809 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 3810 | "char * array declaration might be better as static const\n" . |
| 3811 | $herecurr); |
| 3812 | } |
| 3813 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3814 | # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) |
| 3815 | if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { |
| 3816 | my $array = $1; |
| 3817 | if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) { |
| 3818 | my $array_div = $1; |
| 3819 | if (WARN("ARRAY_SIZE", |
| 3820 | "Prefer ARRAY_SIZE($array)\n" . $herecurr) && |
| 3821 | $fix) { |
| 3822 | $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; |
| 3823 | } |
| 3824 | } |
| 3825 | } |
| 3826 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3827 | # check for function declarations without arguments like "int foo()" |
| 3828 | if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { |
| 3829 | if (ERROR("FUNCTION_WITHOUT_ARGS", |
| 3830 | "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && |
| 3831 | $fix) { |
| 3832 | $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; |
| 3833 | } |
| 3834 | } |
| 3835 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3836 | # check for new typedefs, only function parameters and sparse annotations |
| 3837 | # make sense. |
| 3838 | if ($line =~ /\btypedef\s/ && |
| 3839 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
| 3840 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
| 3841 | $line !~ /\b$typeTypedefs\b/ && |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 3842 | $line !~ /\b__bitwise\b/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3843 | WARN("NEW_TYPEDEFS", |
| 3844 | "do not add new typedefs\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | # * goes on variable not on type |
| 3848 | # (char*[ const]) |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3849 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 3850 | #print "AA<$1>\n"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3851 | my ($ident, $from, $to) = ($1, $2, $2); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3852 | |
| 3853 | # Should start with a space. |
| 3854 | $to =~ s/^(\S)/ $1/; |
| 3855 | # Should not end with a space. |
| 3856 | $to =~ s/\s+$//; |
| 3857 | # '*'s should not have spaces between. |
| 3858 | while ($to =~ s/\*\s+\*/\*\*/) { |
| 3859 | } |
| 3860 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3861 | ## print "1: from<$from> to<$to> ident<$ident>\n"; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3862 | if ($from ne $to) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3863 | if (ERROR("POINTER_LOCATION", |
| 3864 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && |
| 3865 | $fix) { |
| 3866 | my $sub_from = $ident; |
| 3867 | my $sub_to = $ident; |
| 3868 | $sub_to =~ s/\Q$from\E/$to/; |
| 3869 | $fixed[$fixlinenr] =~ |
| 3870 | s@\Q$sub_from\E@$sub_to@; |
| 3871 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3872 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3873 | } |
| 3874 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 3875 | #print "BB<$1>\n"; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3876 | my ($match, $from, $to, $ident) = ($1, $2, $2, $3); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3877 | |
| 3878 | # Should start with a space. |
| 3879 | $to =~ s/^(\S)/ $1/; |
| 3880 | # Should not end with a space. |
| 3881 | $to =~ s/\s+$//; |
| 3882 | # '*'s should not have spaces between. |
| 3883 | while ($to =~ s/\*\s+\*/\*\*/) { |
| 3884 | } |
| 3885 | # Modifiers should have spaces. |
| 3886 | $to =~ s/(\b$Modifier$)/$1 /; |
| 3887 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3888 | ## print "2: from<$from> to<$to> ident<$ident>\n"; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3889 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3890 | if (ERROR("POINTER_LOCATION", |
| 3891 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && |
| 3892 | $fix) { |
| 3893 | |
| 3894 | my $sub_from = $match; |
| 3895 | my $sub_to = $match; |
| 3896 | $sub_to =~ s/\Q$from\E/$to/; |
| 3897 | $fixed[$fixlinenr] =~ |
| 3898 | s@\Q$sub_from\E@$sub_to@; |
| 3899 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3900 | } |
| 3901 | } |
| 3902 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3903 | # avoid BUG() or BUG_ON() |
| 3904 | if ($line =~ /\b(?:BUG|BUG_ON)\b/) { |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3905 | my $msg_level = \&WARN; |
| 3906 | $msg_level = \&CHK if ($file); |
| 3907 | &{$msg_level}("AVOID_BUG", |
| 3908 | "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3909 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3910 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3911 | # avoid LINUX_VERSION_CODE |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3912 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3913 | WARN("LINUX_VERSION_CODE", |
| 3914 | "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
| 3915 | } |
| 3916 | |
| 3917 | # check for uses of printk_ratelimit |
| 3918 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
| 3919 | WARN("PRINTK_RATELIMITED", |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3920 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3921 | } |
| 3922 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3923 | # printk should use KERN_* levels |
| 3924 | if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { |
| 3925 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 3926 | "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3927 | } |
| 3928 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3929 | if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { |
| 3930 | my $orig = $1; |
| 3931 | my $level = lc($orig); |
| 3932 | $level = "warn" if ($level eq "warning"); |
| 3933 | my $level2 = $level; |
| 3934 | $level2 = "dbg" if ($level eq "debug"); |
| 3935 | WARN("PREFER_PR_LEVEL", |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3936 | "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | if ($line =~ /\bpr_warning\s*\(/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3940 | if (WARN("PREFER_PR_LEVEL", |
| 3941 | "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && |
| 3942 | $fix) { |
| 3943 | $fixed[$fixlinenr] =~ |
| 3944 | s/\bpr_warning\b/pr_warn/; |
| 3945 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 3949 | my $orig = $1; |
| 3950 | my $level = lc($orig); |
| 3951 | $level = "warn" if ($level eq "warning"); |
| 3952 | $level = "dbg" if ($level eq "debug"); |
| 3953 | WARN("PREFER_DEV_LEVEL", |
| 3954 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 3955 | } |
| 3956 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 3957 | # ENOSYS means "bad syscall nr" and nothing else. This will have a small |
| 3958 | # number of false positives, but assembly files are not checked, so at |
| 3959 | # least the arch entry code will not trigger this warning. |
| 3960 | if ($line =~ /\bENOSYS\b/) { |
| 3961 | WARN("ENOSYS", |
| 3962 | "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); |
| 3963 | } |
| 3964 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3965 | # function brace can't be on same line, except for #defines of do while, |
| 3966 | # or if closed on same line |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3967 | if ($^V && $^V ge 5.10.0 && |
| 3968 | $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && |
| 3969 | $sline !~ /\#\s*define\b.*do\s*\{/ && |
| 3970 | $sline !~ /}/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3971 | if (ERROR("OPEN_BRACE", |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3972 | "open brace '{' following function definitions go on the next line\n" . $herecurr) && |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3973 | $fix) { |
| 3974 | fix_delete_line($fixlinenr, $rawline); |
| 3975 | my $fixed_line = $rawline; |
| 3976 | $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; |
| 3977 | my $line1 = $1; |
| 3978 | my $line2 = $2; |
| 3979 | fix_insert_line($fixlinenr, ltrim($line1)); |
| 3980 | fix_insert_line($fixlinenr, "\+{"); |
| 3981 | if ($line2 !~ /^\s*$/) { |
| 3982 | fix_insert_line($fixlinenr, "\+\t" . trim($line2)); |
| 3983 | } |
| 3984 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3985 | } |
| 3986 | |
| 3987 | # open braces for enum, union and struct go on the same line. |
| 3988 | if ($line =~ /^.\s*{/ && |
| 3989 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3990 | if (ERROR("OPEN_BRACE", |
| 3991 | "open brace '{' following $1 go on the same line\n" . $hereprev) && |
| 3992 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 3993 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 3994 | fix_delete_line($fixlinenr, $rawline); |
| 3995 | my $fixedline = rtrim($prevrawline) . " {"; |
| 3996 | fix_insert_line($fixlinenr, $fixedline); |
| 3997 | $fixedline = $rawline; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 3998 | $fixedline =~ s/^(.\s*)\{\s*/$1\t/; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 3999 | if ($fixedline !~ /^\+\s*$/) { |
| 4000 | fix_insert_line($fixlinenr, $fixedline); |
| 4001 | } |
| 4002 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4003 | } |
| 4004 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 4005 | # missing space after union, struct or enum definition |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4006 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { |
| 4007 | if (WARN("SPACING", |
| 4008 | "missing space after $1 definition\n" . $herecurr) && |
| 4009 | $fix) { |
| 4010 | $fixed[$fixlinenr] =~ |
| 4011 | s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | # Function pointer declarations |
| 4016 | # check spacing between type, funcptr, and args |
| 4017 | # canonical declaration is "type (*funcptr)(args...)" |
| 4018 | if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { |
| 4019 | my $declare = $1; |
| 4020 | my $pre_pointer_space = $2; |
| 4021 | my $post_pointer_space = $3; |
| 4022 | my $funcname = $4; |
| 4023 | my $post_funcname_space = $5; |
| 4024 | my $pre_args_space = $6; |
| 4025 | |
| 4026 | # the $Declare variable will capture all spaces after the type |
| 4027 | # so check it for a missing trailing missing space but pointer return types |
| 4028 | # don't need a space so don't warn for those. |
| 4029 | my $post_declare_space = ""; |
| 4030 | if ($declare =~ /(\s+)$/) { |
| 4031 | $post_declare_space = $1; |
| 4032 | $declare = rtrim($declare); |
| 4033 | } |
| 4034 | if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { |
| 4035 | WARN("SPACING", |
| 4036 | "missing space after return type\n" . $herecurr); |
| 4037 | $post_declare_space = " "; |
| 4038 | } |
| 4039 | |
| 4040 | # unnecessary space "type (*funcptr)(args...)" |
| 4041 | # This test is not currently implemented because these declarations are |
| 4042 | # equivalent to |
| 4043 | # int foo(int bar, ...) |
| 4044 | # and this is form shouldn't/doesn't generate a checkpatch warning. |
| 4045 | # |
| 4046 | # elsif ($declare =~ /\s{2,}$/) { |
| 4047 | # WARN("SPACING", |
| 4048 | # "Multiple spaces after return type\n" . $herecurr); |
| 4049 | # } |
| 4050 | |
| 4051 | # unnecessary space "type ( *funcptr)(args...)" |
| 4052 | if (defined $pre_pointer_space && |
| 4053 | $pre_pointer_space =~ /^\s/) { |
| 4054 | WARN("SPACING", |
| 4055 | "Unnecessary space after function pointer open parenthesis\n" . $herecurr); |
| 4056 | } |
| 4057 | |
| 4058 | # unnecessary space "type (* funcptr)(args...)" |
| 4059 | if (defined $post_pointer_space && |
| 4060 | $post_pointer_space =~ /^\s/) { |
| 4061 | WARN("SPACING", |
| 4062 | "Unnecessary space before function pointer name\n" . $herecurr); |
| 4063 | } |
| 4064 | |
| 4065 | # unnecessary space "type (*funcptr )(args...)" |
| 4066 | if (defined $post_funcname_space && |
| 4067 | $post_funcname_space =~ /^\s/) { |
| 4068 | WARN("SPACING", |
| 4069 | "Unnecessary space after function pointer name\n" . $herecurr); |
| 4070 | } |
| 4071 | |
| 4072 | # unnecessary space "type (*funcptr) (args...)" |
| 4073 | if (defined $pre_args_space && |
| 4074 | $pre_args_space =~ /^\s/) { |
| 4075 | WARN("SPACING", |
| 4076 | "Unnecessary space before function pointer arguments\n" . $herecurr); |
| 4077 | } |
| 4078 | |
| 4079 | if (show_type("SPACING") && $fix) { |
| 4080 | $fixed[$fixlinenr] =~ |
| 4081 | s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; |
| 4082 | } |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4085 | # check for spacing round square brackets; allowed: |
| 4086 | # 1. with a type on the left -- int [] a; |
| 4087 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 4088 | # 3. inside a curly brace -- = { [0...10] = 5 } |
| 4089 | while ($line =~ /(.*?\s)\[/g) { |
| 4090 | my ($where, $prefix) = ($-[1], $1); |
| 4091 | if ($prefix !~ /$Type\s+$/ && |
| 4092 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 4093 | $prefix !~ /[{,:]\s+$/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4094 | if (ERROR("BRACKET_SPACE", |
| 4095 | "space prohibited before open square bracket '['\n" . $herecurr) && |
| 4096 | $fix) { |
| 4097 | $fixed[$fixlinenr] =~ |
| 4098 | s/^(\+.*?)\s+\[/$1\[/; |
| 4099 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4100 | } |
| 4101 | } |
| 4102 | |
| 4103 | # check for spaces between functions and their parentheses. |
| 4104 | while ($line =~ /($Ident)\s+\(/g) { |
| 4105 | my $name = $1; |
| 4106 | my $ctx_before = substr($line, 0, $-[1]); |
| 4107 | my $ctx = "$ctx_before$name"; |
| 4108 | |
| 4109 | # Ignore those directives where spaces _are_ permitted. |
| 4110 | if ($name =~ /^(?: |
| 4111 | if|for|while|switch|return|case| |
| 4112 | volatile|__volatile__| |
| 4113 | __attribute__|format|__extension__| |
| 4114 | asm|__asm__)$/x) |
| 4115 | { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4116 | # cpp #define statements have non-optional spaces, ie |
| 4117 | # if there is a space between the name and the open |
| 4118 | # parenthesis it is simply not a parameter group. |
| 4119 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
| 4120 | |
| 4121 | # cpp #elif statement condition may start with a ( |
| 4122 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
| 4123 | |
| 4124 | # If this whole things ends with a type its most |
| 4125 | # likely a typedef for a function. |
| 4126 | } elsif ($ctx =~ /$Type$/) { |
| 4127 | |
| 4128 | } else { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4129 | if (WARN("SPACING", |
| 4130 | "space prohibited between function name and open parenthesis '('\n" . $herecurr) && |
| 4131 | $fix) { |
| 4132 | $fixed[$fixlinenr] =~ |
| 4133 | s/\b$name\s+\(/$name\(/; |
| 4134 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4135 | } |
| 4136 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4137 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4138 | # Check operator spacing. |
| 4139 | if (!($line=~/\#\s*include/)) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4140 | my $fixed_line = ""; |
| 4141 | my $line_fixed = 0; |
| 4142 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4143 | my $ops = qr{ |
| 4144 | <<=|>>=|<=|>=|==|!=| |
| 4145 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 4146 | =>|->|<<|>>|<|>|=|!|~| |
| 4147 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4148 | \?:|\?|: |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4149 | }x; |
| 4150 | my @elements = split(/($ops|;)/, $opline); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4151 | |
| 4152 | ## print("element count: <" . $#elements . ">\n"); |
| 4153 | ## foreach my $el (@elements) { |
| 4154 | ## print("el: <$el>\n"); |
| 4155 | ## } |
| 4156 | |
| 4157 | my @fix_elements = (); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4158 | my $off = 0; |
| 4159 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4160 | foreach my $el (@elements) { |
| 4161 | push(@fix_elements, substr($rawline, $off, length($el))); |
| 4162 | $off += length($el); |
| 4163 | } |
| 4164 | |
| 4165 | $off = 0; |
| 4166 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4167 | my $blank = copy_spacing($opline); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4168 | my $last_after = -1; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4169 | |
| 4170 | for (my $n = 0; $n < $#elements; $n += 2) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4171 | |
| 4172 | my $good = $fix_elements[$n] . $fix_elements[$n + 1]; |
| 4173 | |
| 4174 | ## print("n: <$n> good: <$good>\n"); |
| 4175 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4176 | $off += length($elements[$n]); |
| 4177 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4178 | # Pick up the preceding and succeeding characters. |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4179 | my $ca = substr($opline, 0, $off); |
| 4180 | my $cc = ''; |
| 4181 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 4182 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 4183 | } |
| 4184 | my $cb = "$ca$;$cc"; |
| 4185 | |
| 4186 | my $a = ''; |
| 4187 | $a = 'V' if ($elements[$n] ne ''); |
| 4188 | $a = 'W' if ($elements[$n] =~ /\s$/); |
| 4189 | $a = 'C' if ($elements[$n] =~ /$;$/); |
| 4190 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 4191 | $a = 'O' if ($elements[$n] eq ''); |
| 4192 | $a = 'E' if ($ca =~ /^\s*$/); |
| 4193 | |
| 4194 | my $op = $elements[$n + 1]; |
| 4195 | |
| 4196 | my $c = ''; |
| 4197 | if (defined $elements[$n + 2]) { |
| 4198 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 4199 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
| 4200 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
| 4201 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 4202 | $c = 'O' if ($elements[$n + 2] eq ''); |
| 4203 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
| 4204 | } else { |
| 4205 | $c = 'E'; |
| 4206 | } |
| 4207 | |
| 4208 | my $ctx = "${a}x${c}"; |
| 4209 | |
| 4210 | my $at = "(ctx:$ctx)"; |
| 4211 | |
| 4212 | my $ptr = substr($blank, 0, $off) . "^"; |
| 4213 | my $hereptr = "$hereline$ptr\n"; |
| 4214 | |
| 4215 | # Pull out the value of this operator. |
| 4216 | my $op_type = substr($curr_values, $off + 1, 1); |
| 4217 | |
| 4218 | # Get the full operator variant. |
| 4219 | my $opv = $op . substr($curr_vars, $off, 1); |
| 4220 | |
| 4221 | # Ignore operators passed as parameters. |
| 4222 | if ($op_type ne 'V' && |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4223 | $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4224 | |
| 4225 | # # Ignore comments |
| 4226 | # } elsif ($op =~ /^$;+$/) { |
| 4227 | |
| 4228 | # ; should have either the end of line or a space or \ after it |
| 4229 | } elsif ($op eq ';') { |
| 4230 | if ($ctx !~ /.x[WEBC]/ && |
| 4231 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4232 | if (ERROR("SPACING", |
| 4233 | "space required after that '$op' $at\n" . $hereptr)) { |
| 4234 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
| 4235 | $line_fixed = 1; |
| 4236 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | # // is a comment |
| 4240 | } elsif ($op eq '//') { |
| 4241 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4242 | # : when part of a bitfield |
| 4243 | } elsif ($opv eq ':B') { |
| 4244 | # skip the bitfield test for now |
| 4245 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4246 | # No spaces for: |
| 4247 | # -> |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4248 | } elsif ($op eq '->') { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4249 | if ($ctx =~ /Wx.|.xW/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4250 | if (ERROR("SPACING", |
| 4251 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
| 4252 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 4253 | if (defined $fix_elements[$n + 2]) { |
| 4254 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4255 | } |
| 4256 | $line_fixed = 1; |
| 4257 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4260 | # , must not have a space before and must have a space on the right. |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4261 | } elsif ($op eq ',') { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4262 | my $rtrim_before = 0; |
| 4263 | my $space_after = 0; |
| 4264 | if ($ctx =~ /Wx./) { |
| 4265 | if (ERROR("SPACING", |
| 4266 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 4267 | $line_fixed = 1; |
| 4268 | $rtrim_before = 1; |
| 4269 | } |
| 4270 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4271 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4272 | if (ERROR("SPACING", |
| 4273 | "space required after that '$op' $at\n" . $hereptr)) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4274 | $line_fixed = 1; |
| 4275 | $last_after = $n; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4276 | $space_after = 1; |
| 4277 | } |
| 4278 | } |
| 4279 | if ($rtrim_before || $space_after) { |
| 4280 | if ($rtrim_before) { |
| 4281 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 4282 | } else { |
| 4283 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
| 4284 | } |
| 4285 | if ($space_after) { |
| 4286 | $good .= " "; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4287 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | # '*' as part of a type definition -- reported already. |
| 4291 | } elsif ($opv eq '*_') { |
| 4292 | #warn "'*' is part of type\n"; |
| 4293 | |
| 4294 | # unary operators should have a space before and |
| 4295 | # none after. May be left adjacent to another |
| 4296 | # unary operator, or a cast |
| 4297 | } elsif ($op eq '!' || $op eq '~' || |
| 4298 | $opv eq '*U' || $opv eq '-U' || |
| 4299 | $opv eq '&U' || $opv eq '&&U') { |
| 4300 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4301 | if (ERROR("SPACING", |
| 4302 | "space required before that '$op' $at\n" . $hereptr)) { |
| 4303 | if ($n != $last_after + 2) { |
| 4304 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
| 4305 | $line_fixed = 1; |
| 4306 | } |
| 4307 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4308 | } |
| 4309 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
| 4310 | # A unary '*' may be const |
| 4311 | |
| 4312 | } elsif ($ctx =~ /.xW/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4313 | if (ERROR("SPACING", |
| 4314 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
| 4315 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
| 4316 | if (defined $fix_elements[$n + 2]) { |
| 4317 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4318 | } |
| 4319 | $line_fixed = 1; |
| 4320 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | # unary ++ and unary -- are allowed no space on one side. |
| 4324 | } elsif ($op eq '++' or $op eq '--') { |
| 4325 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4326 | if (ERROR("SPACING", |
| 4327 | "space required one side of that '$op' $at\n" . $hereptr)) { |
| 4328 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
| 4329 | $line_fixed = 1; |
| 4330 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4331 | } |
| 4332 | if ($ctx =~ /Wx[BE]/ || |
| 4333 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4334 | if (ERROR("SPACING", |
| 4335 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 4336 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 4337 | $line_fixed = 1; |
| 4338 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4339 | } |
| 4340 | if ($ctx =~ /ExW/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4341 | if (ERROR("SPACING", |
| 4342 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
| 4343 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
| 4344 | if (defined $fix_elements[$n + 2]) { |
| 4345 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4346 | } |
| 4347 | $line_fixed = 1; |
| 4348 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4349 | } |
| 4350 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4351 | # << and >> may either have or not have spaces both sides |
| 4352 | } elsif ($op eq '<<' or $op eq '>>' or |
| 4353 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 4354 | $op eq '+' or $op eq '-' or |
| 4355 | $op eq '*' or $op eq '/' or |
| 4356 | $op eq '%') |
| 4357 | { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4358 | if ($check) { |
| 4359 | if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { |
| 4360 | if (CHK("SPACING", |
| 4361 | "spaces preferred around that '$op' $at\n" . $hereptr)) { |
| 4362 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 4363 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4364 | $line_fixed = 1; |
| 4365 | } |
| 4366 | } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { |
| 4367 | if (CHK("SPACING", |
| 4368 | "space preferred before that '$op' $at\n" . $hereptr)) { |
| 4369 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); |
| 4370 | $line_fixed = 1; |
| 4371 | } |
| 4372 | } |
| 4373 | } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4374 | if (ERROR("SPACING", |
| 4375 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
| 4376 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 4377 | if (defined $fix_elements[$n + 2]) { |
| 4378 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4379 | } |
| 4380 | $line_fixed = 1; |
| 4381 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | # A colon needs no spaces before when it is |
| 4385 | # terminating a case value or a label. |
| 4386 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 4387 | if ($ctx =~ /Wx./) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4388 | if (ERROR("SPACING", |
| 4389 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 4390 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 4391 | $line_fixed = 1; |
| 4392 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | # All the others need spaces both sides. |
| 4396 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
| 4397 | my $ok = 0; |
| 4398 | |
| 4399 | # Ignore email addresses <foo@bar> |
| 4400 | if (($op eq '<' && |
| 4401 | $cc =~ /^\S+\@\S+>/) || |
| 4402 | ($op eq '>' && |
| 4403 | $ca =~ /<\S+\@\S+$/)) |
| 4404 | { |
| 4405 | $ok = 1; |
| 4406 | } |
| 4407 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4408 | # for asm volatile statements |
| 4409 | # ignore a colon with another |
| 4410 | # colon immediately before or after |
| 4411 | if (($op eq ':') && |
| 4412 | ($ca =~ /:$/ || $cc =~ /^:/)) { |
| 4413 | $ok = 1; |
| 4414 | } |
| 4415 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4416 | # messages are ERROR, but ?: are CHK |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4417 | if ($ok == 0) { |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 4418 | my $msg_level = \&ERROR; |
| 4419 | $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4420 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 4421 | if (&{$msg_level}("SPACING", |
| 4422 | "spaces required around that '$op' $at\n" . $hereptr)) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4423 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 4424 | if (defined $fix_elements[$n + 2]) { |
| 4425 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4426 | } |
| 4427 | $line_fixed = 1; |
| 4428 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4429 | } |
| 4430 | } |
| 4431 | $off += length($elements[$n + 1]); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4432 | |
| 4433 | ## print("n: <$n> GOOD: <$good>\n"); |
| 4434 | |
| 4435 | $fixed_line = $fixed_line . $good; |
| 4436 | } |
| 4437 | |
| 4438 | if (($#elements % 2) == 0) { |
| 4439 | $fixed_line = $fixed_line . $fix_elements[$#elements]; |
| 4440 | } |
| 4441 | |
| 4442 | if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { |
| 4443 | $fixed[$fixlinenr] = $fixed_line; |
| 4444 | } |
| 4445 | |
| 4446 | |
| 4447 | } |
| 4448 | |
| 4449 | # check for whitespace before a non-naked semicolon |
| 4450 | if ($line =~ /^\+.*\S\s+;\s*$/) { |
| 4451 | if (WARN("SPACING", |
| 4452 | "space prohibited before semicolon\n" . $herecurr) && |
| 4453 | $fix) { |
| 4454 | 1 while $fixed[$fixlinenr] =~ |
| 4455 | s/^(\+.*\S)\s+;/$1;/; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4456 | } |
| 4457 | } |
| 4458 | |
| 4459 | # check for multiple assignments |
| 4460 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4461 | CHK("MULTIPLE_ASSIGNMENTS", |
| 4462 | "multiple assignments should be avoided\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | ## # check for multiple declarations, allowing for a function declaration |
| 4466 | ## # continuation. |
| 4467 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 4468 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 4469 | ## |
| 4470 | ## # Remove any bracketed sections to ensure we do not |
| 4471 | ## # falsly report the parameters of functions. |
| 4472 | ## my $ln = $line; |
| 4473 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 4474 | ## } |
| 4475 | ## if ($ln =~ /,/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4476 | ## WARN("MULTIPLE_DECLARATION", |
| 4477 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4478 | ## } |
| 4479 | ## } |
| 4480 | |
| 4481 | #need space before brace following if, while, etc |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4482 | if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4483 | $line =~ /do\{/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4484 | if (ERROR("SPACING", |
| 4485 | "space required before the open brace '{'\n" . $herecurr) && |
| 4486 | $fix) { |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 4487 | $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4488 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4489 | } |
| 4490 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4491 | ## # check for blank lines before declarations |
| 4492 | ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && |
| 4493 | ## $prevrawline =~ /^.\s*$/) { |
| 4494 | ## WARN("SPACING", |
| 4495 | ## "No blank lines before declarations\n" . $hereprev); |
| 4496 | ## } |
| 4497 | ## |
| 4498 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4499 | # closing brace should have a space following it when it has anything |
| 4500 | # on the line |
| 4501 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4502 | if (ERROR("SPACING", |
| 4503 | "space required after that close brace '}'\n" . $herecurr) && |
| 4504 | $fix) { |
| 4505 | $fixed[$fixlinenr] =~ |
| 4506 | s/}((?!(?:,|;|\)))\S)/} $1/; |
| 4507 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | # check spacing on square brackets |
| 4511 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4512 | if (ERROR("SPACING", |
| 4513 | "space prohibited after that open square bracket '['\n" . $herecurr) && |
| 4514 | $fix) { |
| 4515 | $fixed[$fixlinenr] =~ |
| 4516 | s/\[\s+/\[/; |
| 4517 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4518 | } |
| 4519 | if ($line =~ /\s\]/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4520 | if (ERROR("SPACING", |
| 4521 | "space prohibited before that close square bracket ']'\n" . $herecurr) && |
| 4522 | $fix) { |
| 4523 | $fixed[$fixlinenr] =~ |
| 4524 | s/\s+\]/\]/; |
| 4525 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | # check spacing on parentheses |
| 4529 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 4530 | $line !~ /for\s*\(\s+;/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4531 | if (ERROR("SPACING", |
| 4532 | "space prohibited after that open parenthesis '('\n" . $herecurr) && |
| 4533 | $fix) { |
| 4534 | $fixed[$fixlinenr] =~ |
| 4535 | s/\(\s+/\(/; |
| 4536 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4537 | } |
| 4538 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
| 4539 | $line !~ /for\s*\(.*;\s+\)/ && |
| 4540 | $line !~ /:\s+\)/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4541 | if (ERROR("SPACING", |
| 4542 | "space prohibited before that close parenthesis ')'\n" . $herecurr) && |
| 4543 | $fix) { |
| 4544 | $fixed[$fixlinenr] =~ |
| 4545 | s/\s+\)/\)/; |
| 4546 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4547 | } |
| 4548 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4549 | # check unnecessary parentheses around addressof/dereference single $Lvals |
| 4550 | # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar |
| 4551 | |
| 4552 | while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4553 | my $var = $1; |
| 4554 | if (CHK("UNNECESSARY_PARENTHESES", |
| 4555 | "Unnecessary parentheses around $var\n" . $herecurr) && |
| 4556 | $fix) { |
| 4557 | $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; |
| 4558 | } |
| 4559 | } |
| 4560 | |
| 4561 | # check for unnecessary parentheses around function pointer uses |
| 4562 | # ie: (foo->bar)(); should be foo->bar(); |
| 4563 | # but not "if (foo->bar) (" to avoid some false positives |
| 4564 | if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { |
| 4565 | my $var = $2; |
| 4566 | if (CHK("UNNECESSARY_PARENTHESES", |
| 4567 | "Unnecessary parentheses around function pointer $var\n" . $herecurr) && |
| 4568 | $fix) { |
| 4569 | my $var2 = deparenthesize($var); |
| 4570 | $var2 =~ s/\s//g; |
| 4571 | $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; |
| 4572 | } |
| 4573 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4574 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 4575 | # check for unnecessary parentheses around comparisons in if uses |
| 4576 | # when !drivers/staging or command-line uses --strict |
| 4577 | if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && |
| 4578 | $^V && $^V ge 5.10.0 && defined($stat) && |
| 4579 | $stat =~ /(^.\s*if\s*($balanced_parens))/) { |
| 4580 | my $if_stat = $1; |
| 4581 | my $test = substr($2, 1, -1); |
| 4582 | my $herectx; |
| 4583 | while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { |
| 4584 | my $match = $1; |
| 4585 | # avoid parentheses around potential macro args |
| 4586 | next if ($match =~ /^\s*\w+\s*$/); |
| 4587 | if (!defined($herectx)) { |
| 4588 | $herectx = $here . "\n"; |
| 4589 | my $cnt = statement_rawlines($if_stat); |
| 4590 | for (my $n = 0; $n < $cnt; $n++) { |
| 4591 | my $rl = raw_line($linenr, $n); |
| 4592 | $herectx .= $rl . "\n"; |
| 4593 | last if $rl =~ /^[ \+].*\{/; |
| 4594 | } |
| 4595 | } |
| 4596 | CHK("UNNECESSARY_PARENTHESES", |
| 4597 | "Unnecessary parentheses around '$match'\n" . $herectx); |
| 4598 | } |
| 4599 | } |
| 4600 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4601 | #goto labels aren't indented, allow a single space however |
| 4602 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
| 4603 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4604 | if (WARN("INDENTED_LABEL", |
| 4605 | "labels should not be indented\n" . $herecurr) && |
| 4606 | $fix) { |
| 4607 | $fixed[$fixlinenr] =~ |
| 4608 | s/^(.)\s+/$1/; |
| 4609 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4610 | } |
| 4611 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4612 | # return is not a function |
| 4613 | if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4614 | my $spacing = $1; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4615 | if ($^V && $^V ge 5.10.0 && |
| 4616 | $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { |
| 4617 | my $value = $1; |
| 4618 | $value = deparenthesize($value); |
| 4619 | if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { |
| 4620 | ERROR("RETURN_PARENTHESES", |
| 4621 | "return is not a function, parentheses are not required\n" . $herecurr); |
| 4622 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4623 | } elsif ($spacing !~ /\s+/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4624 | ERROR("SPACING", |
| 4625 | "space required before the open parenthesis '('\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4626 | } |
| 4627 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4628 | |
| 4629 | # unnecessary return in a void function |
| 4630 | # at end-of-function, with the previous line a single leading tab, then return; |
| 4631 | # and the line before that not a goto label target like "out:" |
| 4632 | if ($sline =~ /^[ \+]}\s*$/ && |
| 4633 | $prevline =~ /^\+\treturn\s*;\s*$/ && |
| 4634 | $linenr >= 3 && |
| 4635 | $lines[$linenr - 3] =~ /^[ +]/ && |
| 4636 | $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { |
| 4637 | WARN("RETURN_VOID", |
| 4638 | "void function return statements are not generally useful\n" . $hereprev); |
| 4639 | } |
| 4640 | |
| 4641 | # if statements using unnecessary parentheses - ie: if ((foo == bar)) |
| 4642 | if ($^V && $^V ge 5.10.0 && |
| 4643 | $line =~ /\bif\s*((?:\(\s*){2,})/) { |
| 4644 | my $openparens = $1; |
| 4645 | my $count = $openparens =~ tr@\(@\(@; |
| 4646 | my $msg = ""; |
| 4647 | if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { |
| 4648 | my $comp = $4; #Not $1 because of $LvalOrFunc |
| 4649 | $msg = " - maybe == should be = ?" if ($comp eq "=="); |
| 4650 | WARN("UNNECESSARY_PARENTHESES", |
| 4651 | "Unnecessary parentheses$msg\n" . $herecurr); |
| 4652 | } |
| 4653 | } |
| 4654 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4655 | # comparisons with a constant or upper case identifier on the left |
| 4656 | # avoid cases like "foo + BAR < baz" |
| 4657 | # only fix matches surrounded by parentheses to avoid incorrect |
| 4658 | # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" |
| 4659 | if ($^V && $^V ge 5.10.0 && |
| 4660 | $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { |
| 4661 | my $lead = $1; |
| 4662 | my $const = $2; |
| 4663 | my $comp = $3; |
| 4664 | my $to = $4; |
| 4665 | my $newcomp = $comp; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4666 | if ($lead !~ /(?:$Operators|\.)\s*$/ && |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4667 | $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && |
| 4668 | WARN("CONSTANT_COMPARISON", |
| 4669 | "Comparisons should place the constant on the right side of the test\n" . $herecurr) && |
| 4670 | $fix) { |
| 4671 | if ($comp eq "<") { |
| 4672 | $newcomp = ">"; |
| 4673 | } elsif ($comp eq "<=") { |
| 4674 | $newcomp = ">="; |
| 4675 | } elsif ($comp eq ">") { |
| 4676 | $newcomp = "<"; |
| 4677 | } elsif ($comp eq ">=") { |
| 4678 | $newcomp = "<="; |
| 4679 | } |
| 4680 | $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; |
| 4681 | } |
| 4682 | } |
| 4683 | |
| 4684 | # Return of what appears to be an errno should normally be negative |
| 4685 | if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 4686 | my $name = $1; |
| 4687 | if ($name ne 'EOF' && $name ne 'ERROR') { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4688 | WARN("USE_NEGATIVE_ERRNO", |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4689 | "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 4690 | } |
| 4691 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4692 | |
| 4693 | # Need a space before open parenthesis after if, while etc |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4694 | if ($line =~ /\b(if|while|for|switch)\(/) { |
| 4695 | if (ERROR("SPACING", |
| 4696 | "space required before the open parenthesis '('\n" . $herecurr) && |
| 4697 | $fix) { |
| 4698 | $fixed[$fixlinenr] =~ |
| 4699 | s/\b(if|while|for|switch)\(/$1 \(/; |
| 4700 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | # Check for illegal assignment in if conditional -- and check for trailing |
| 4704 | # statements after the conditional. |
| 4705 | if ($line =~ /do\s*(?!{)/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4706 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 4707 | ctx_statement_block($linenr, $realcnt, 0) |
| 4708 | if (!defined $stat); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4709 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 4710 | $remain_next, $off_next); |
| 4711 | $stat_next =~ s/\n./\n /g; |
| 4712 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 4713 | |
| 4714 | if ($stat_next =~ /^\s*while\b/) { |
| 4715 | # If the statement carries leading newlines, |
| 4716 | # then count those as offsets. |
| 4717 | my ($whitespace) = |
| 4718 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 4719 | my $offset = |
| 4720 | statement_rawlines($whitespace) - 1; |
| 4721 | |
| 4722 | $suppress_whiletrailers{$line_nr_next + |
| 4723 | $offset} = 1; |
| 4724 | } |
| 4725 | } |
| 4726 | if (!defined $suppress_whiletrailers{$linenr} && |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4727 | defined($stat) && defined($cond) && |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4728 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
| 4729 | my ($s, $c) = ($stat, $cond); |
| 4730 | |
| 4731 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4732 | ERROR("ASSIGN_IN_IF", |
| 4733 | "do not use assignment in if condition\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | # Find out what is on the end of the line after the |
| 4737 | # conditional. |
| 4738 | substr($s, 0, length($c), ''); |
| 4739 | $s =~ s/\n.*//g; |
| 4740 | $s =~ s/$;//g; # Remove any comments |
| 4741 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 4742 | $c !~ /}\s*while\s*/) |
| 4743 | { |
| 4744 | # Find out how long the conditional actually is. |
| 4745 | my @newlines = ($c =~ /\n/gs); |
| 4746 | my $cond_lines = 1 + $#newlines; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 4747 | my $stat_real = ''; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4748 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 4749 | $stat_real = raw_line($linenr, $cond_lines) |
| 4750 | . "\n" if ($cond_lines); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4751 | if (defined($stat_real) && $cond_lines > 1) { |
| 4752 | $stat_real = "[...]\n$stat_real"; |
| 4753 | } |
| 4754 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4755 | ERROR("TRAILING_STATEMENTS", |
| 4756 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4757 | } |
| 4758 | } |
| 4759 | |
| 4760 | # Check for bitwise tests written as boolean |
| 4761 | if ($line =~ / |
| 4762 | (?: |
| 4763 | (?:\[|\(|\&\&|\|\|) |
| 4764 | \s*0[xX][0-9]+\s* |
| 4765 | (?:\&\&|\|\|) |
| 4766 | | |
| 4767 | (?:\&\&|\|\|) |
| 4768 | \s*0[xX][0-9]+\s* |
| 4769 | (?:\&\&|\|\||\)|\]) |
| 4770 | )/x) |
| 4771 | { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4772 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 4773 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | # if and else should not have general statements after it |
| 4777 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 4778 | my $s = $1; |
| 4779 | $s =~ s/$;//g; # Remove any comments |
| 4780 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4781 | ERROR("TRAILING_STATEMENTS", |
| 4782 | "trailing statements should be on next line\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4783 | } |
| 4784 | } |
| 4785 | # if should not continue a brace |
| 4786 | if ($line =~ /}\s*if\b/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4787 | ERROR("TRAILING_STATEMENTS", |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4788 | "trailing statements should be on next line (or did you mean 'else if'?)\n" . |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4789 | $herecurr); |
| 4790 | } |
| 4791 | # case and default should not have general statements after them |
| 4792 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 4793 | $line !~ /\G(?: |
| 4794 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
| 4795 | \s*return\s+ |
| 4796 | )/xg) |
| 4797 | { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4798 | ERROR("TRAILING_STATEMENTS", |
| 4799 | "trailing statements should be on next line\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | # Check for }<nl>else {, these must be at the same |
| 4803 | # indent level to be relevant to each other. |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4804 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && |
| 4805 | $previndent == $indent) { |
| 4806 | if (ERROR("ELSE_AFTER_BRACE", |
| 4807 | "else should follow close brace '}'\n" . $hereprev) && |
| 4808 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4809 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4810 | fix_delete_line($fixlinenr, $rawline); |
| 4811 | my $fixedline = $prevrawline; |
| 4812 | $fixedline =~ s/}\s*$//; |
| 4813 | if ($fixedline !~ /^\+\s*$/) { |
| 4814 | fix_insert_line($fixlinenr, $fixedline); |
| 4815 | } |
| 4816 | $fixedline = $rawline; |
| 4817 | $fixedline =~ s/^(.\s*)else/$1} else/; |
| 4818 | fix_insert_line($fixlinenr, $fixedline); |
| 4819 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4822 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && |
| 4823 | $previndent == $indent) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4824 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 4825 | |
| 4826 | # Find out what is on the end of the line after the |
| 4827 | # conditional. |
| 4828 | substr($s, 0, length($c), ''); |
| 4829 | $s =~ s/\n.*//g; |
| 4830 | |
| 4831 | if ($s =~ /^\s*;/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4832 | if (ERROR("WHILE_AFTER_BRACE", |
| 4833 | "while should follow close brace '}'\n" . $hereprev) && |
| 4834 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4835 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4836 | fix_delete_line($fixlinenr, $rawline); |
| 4837 | my $fixedline = $prevrawline; |
| 4838 | my $trailing = $rawline; |
| 4839 | $trailing =~ s/^\+//; |
| 4840 | $trailing = trim($trailing); |
| 4841 | $fixedline =~ s/}\s*$/} $trailing/; |
| 4842 | fix_insert_line($fixlinenr, $fixedline); |
| 4843 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4844 | } |
| 4845 | } |
| 4846 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4847 | #Specific variable tests |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4848 | while ($line =~ m{($Constant|$Lval)}g) { |
| 4849 | my $var = $1; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4850 | |
| 4851 | #gcc binary extension |
| 4852 | if ($var =~ /^$Binary$/) { |
| 4853 | if (WARN("GCC_BINARY_CONSTANT", |
| 4854 | "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && |
| 4855 | $fix) { |
| 4856 | my $hexval = sprintf("0x%x", oct($var)); |
| 4857 | $fixed[$fixlinenr] =~ |
| 4858 | s/\b$var\b/$hexval/; |
| 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | #CamelCase |
| 4863 | if ($var !~ /^$Constant$/ && |
| 4864 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
Julius Werner | 8b99197 | 2014-11-20 13:31:44 -0800 | [diff] [blame] | 4865 | #Ignore Page<foo> variants |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4866 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Julius Werner | 8b99197 | 2014-11-20 13:31:44 -0800 | [diff] [blame] | 4867 | #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4868 | $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && |
| 4869 | #Ignore some three character SI units explicitly, like MiB and KHz |
| 4870 | $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4871 | while ($var =~ m{($Ident)}g) { |
| 4872 | my $word = $1; |
| 4873 | next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); |
| 4874 | if ($check) { |
| 4875 | seed_camelcase_includes(); |
| 4876 | if (!$file && !$camelcase_file_seeded) { |
| 4877 | seed_camelcase_file($realfile); |
| 4878 | $camelcase_file_seeded = 1; |
| 4879 | } |
| 4880 | } |
| 4881 | if (!defined $camelcase{$word}) { |
| 4882 | $camelcase{$word} = 1; |
| 4883 | CHK("CAMELCASE", |
| 4884 | "Avoid CamelCase: <$word>\n" . $herecurr); |
| 4885 | } |
| 4886 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4887 | } |
| 4888 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4889 | |
| 4890 | #no spaces allowed after \ in define |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4891 | if ($line =~ /\#\s*define.*\\\s+$/) { |
| 4892 | if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 4893 | "Whitespace after \\ makes next lines useless\n" . $herecurr) && |
| 4894 | $fix) { |
| 4895 | $fixed[$fixlinenr] =~ s/\s+$//; |
| 4896 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4897 | } |
| 4898 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4899 | # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes |
| 4900 | # itself <asm/foo.h> (uses RAW line) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4901 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
| 4902 | my $file = "$1.h"; |
| 4903 | my $checkfile = "include/linux/$file"; |
| 4904 | if (-f "$root/$checkfile" && |
| 4905 | $realfile ne $checkfile && |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 4906 | $1 !~ /$allowed_asm_includes/) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4907 | { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4908 | my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; |
| 4909 | if ($asminclude > 0) { |
| 4910 | if ($realfile =~ m{^arch/}) { |
| 4911 | CHK("ARCH_INCLUDE_LINUX", |
| 4912 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 4913 | } else { |
| 4914 | WARN("INCLUDE_LINUX", |
| 4915 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 4916 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4917 | } |
| 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 4922 | # first statement and ensure its the whole macro if its not enclosed |
| 4923 | # in a known good container |
| 4924 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 4925 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
| 4926 | my $ln = $linenr; |
| 4927 | my $cnt = $realcnt; |
| 4928 | my ($off, $dstat, $dcond, $rest); |
| 4929 | my $ctx = ''; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4930 | my $has_flow_statement = 0; |
| 4931 | my $has_arg_concat = 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4932 | ($dstat, $dcond, $ln, $cnt, $off) = |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4933 | ctx_statement_block($linenr, $realcnt, 0); |
| 4934 | $ctx = $dstat; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4935 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
| 4936 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
| 4937 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4938 | $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4939 | $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4940 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4941 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; |
| 4942 | my $define_args = $1; |
| 4943 | my $define_stmt = $dstat; |
| 4944 | my @def_args = (); |
| 4945 | |
| 4946 | if (defined $define_args && $define_args ne "") { |
| 4947 | $define_args = substr($define_args, 1, length($define_args) - 2); |
| 4948 | $define_args =~ s/\s*//g; |
| 4949 | @def_args = split(",", $define_args); |
| 4950 | } |
| 4951 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4952 | $dstat =~ s/$;//g; |
| 4953 | $dstat =~ s/\\\n.//g; |
| 4954 | $dstat =~ s/^\s*//s; |
| 4955 | $dstat =~ s/\s*$//s; |
| 4956 | |
| 4957 | # Flatten any parentheses and braces |
| 4958 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 4959 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4960 | $dstat =~ s/.\[[^\[\]]*\]/1/) |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4961 | { |
| 4962 | } |
| 4963 | |
| 4964 | # Flatten any obvious string concatentation. |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 4965 | while ($dstat =~ s/($String)\s*$Ident/$1/ || |
| 4966 | $dstat =~ s/$Ident\s*($String)/$1/) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4967 | { |
| 4968 | } |
| 4969 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4970 | # Make asm volatile uses seem like a generic function |
| 4971 | $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; |
| 4972 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4973 | my $exceptions = qr{ |
| 4974 | $Declare| |
| 4975 | module_param_named| |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4976 | MODULE_PARM_DESC| |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4977 | DECLARE_PER_CPU| |
| 4978 | DEFINE_PER_CPU| |
| 4979 | __typeof__\(| |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 4980 | union| |
| 4981 | struct| |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4982 | \.$Ident\s*=\s*| |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4983 | ^\"|\"$| |
| 4984 | ^\[ |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 4985 | }x; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 4986 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4987 | |
| 4988 | $ctx =~ s/\n*$//; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4989 | my $stmt_cnt = statement_rawlines($ctx); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 4990 | my $herectx = get_stat_here($linenr, $stmt_cnt, $here); |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 4991 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4992 | if ($dstat ne '' && |
| 4993 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 4994 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 4995 | $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz |
| 4996 | $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 4997 | $dstat !~ /$exceptions/ && |
| 4998 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
| 4999 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
| 5000 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
| 5001 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 5002 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 5003 | $dstat !~ /^do\s*{/ && # do {... |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5004 | $dstat !~ /^\(\{/ && # ({... |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5005 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5006 | { |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5007 | if ($dstat =~ /^\s*if\b/) { |
| 5008 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 5009 | "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); |
| 5010 | } elsif ($dstat =~ /;/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5011 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 5012 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 5013 | } else { |
| 5014 | ERROR("COMPLEX_MACRO", |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5015 | "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5016 | } |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5017 | |
| 5018 | } |
| 5019 | |
| 5020 | # Make $define_stmt single line, comment-free, etc |
| 5021 | my @stmt_array = split('\n', $define_stmt); |
| 5022 | my $first = 1; |
| 5023 | $define_stmt = ""; |
| 5024 | foreach my $l (@stmt_array) { |
| 5025 | $l =~ s/\\$//; |
| 5026 | if ($first) { |
| 5027 | $define_stmt = $l; |
| 5028 | $first = 0; |
| 5029 | } elsif ($l =~ /^[\+ ]/) { |
| 5030 | $define_stmt .= substr($l, 1); |
| 5031 | } |
| 5032 | } |
| 5033 | $define_stmt =~ s/$;//g; |
| 5034 | $define_stmt =~ s/\s+/ /g; |
| 5035 | $define_stmt = trim($define_stmt); |
| 5036 | |
| 5037 | # check if any macro arguments are reused (ignore '...' and 'type') |
| 5038 | foreach my $arg (@def_args) { |
| 5039 | next if ($arg =~ /\.\.\./); |
| 5040 | next if ($arg =~ /^type$/i); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5041 | my $tmp_stmt = $define_stmt; |
| 5042 | $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; |
| 5043 | $tmp_stmt =~ s/\#+\s*$arg\b//g; |
| 5044 | $tmp_stmt =~ s/\b$arg\s*\#\#//g; |
| 5045 | my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5046 | if ($use_cnt > 1) { |
| 5047 | CHK("MACRO_ARG_REUSE", |
| 5048 | "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); |
| 5049 | } |
| 5050 | # check if any macro arguments may have other precedence issues |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5051 | if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5052 | ((defined($1) && $1 ne ',') || |
| 5053 | (defined($2) && $2 ne ','))) { |
| 5054 | CHK("MACRO_ARG_PRECEDENCE", |
| 5055 | "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); |
| 5056 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5057 | } |
| 5058 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5059 | # check for macros with flow control, but without ## concatenation |
| 5060 | # ## concatenation is commonly a macro that defines a function so ignore those |
| 5061 | if ($has_flow_statement && !$has_arg_concat) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5062 | my $cnt = statement_rawlines($ctx); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5063 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5064 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5065 | WARN("MACRO_WITH_FLOW_CONTROL", |
| 5066 | "Macros with flow control statements should be avoided\n" . "$herectx"); |
| 5067 | } |
| 5068 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5069 | # check for line continuations outside of #defines, preprocessor #, and asm |
| 5070 | |
| 5071 | } else { |
| 5072 | if ($prevline !~ /^..*\\$/ && |
| 5073 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 5074 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
| 5075 | $line =~ /^\+.*\\$/) { |
| 5076 | WARN("LINE_CONTINUATIONS", |
| 5077 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 5078 | } |
| 5079 | } |
| 5080 | |
| 5081 | # do {} while (0) macro tests: |
| 5082 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 5083 | # macro should not end with a semicolon |
| 5084 | if ($^V && $^V ge 5.10.0 && |
| 5085 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 5086 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 5087 | my $ln = $linenr; |
| 5088 | my $cnt = $realcnt; |
| 5089 | my ($off, $dstat, $dcond, $rest); |
| 5090 | my $ctx = ''; |
| 5091 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 5092 | ctx_statement_block($linenr, $realcnt, 0); |
| 5093 | $ctx = $dstat; |
| 5094 | |
| 5095 | $dstat =~ s/\\\n.//g; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5096 | $dstat =~ s/$;/ /g; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5097 | |
| 5098 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 5099 | my $stmts = $2; |
| 5100 | my $semis = $3; |
| 5101 | |
| 5102 | $ctx =~ s/\n*$//; |
| 5103 | my $cnt = statement_rawlines($ctx); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5104 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5105 | |
| 5106 | if (($stmts =~ tr/;/;/) == 1 && |
| 5107 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
| 5108 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 5109 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 5110 | } |
| 5111 | if (defined $semis && $semis ne "") { |
| 5112 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 5113 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5114 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5115 | } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { |
| 5116 | $ctx =~ s/\n*$//; |
| 5117 | my $cnt = statement_rawlines($ctx); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5118 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5119 | |
| 5120 | WARN("TRAILING_SEMICOLON", |
| 5121 | "macros should not use a trailing semicolon\n" . "$herectx"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 5126 | # all assignments may have only one of the following with an assignment: |
| 5127 | # . |
| 5128 | # ALIGN(...) |
| 5129 | # VMLINUX_SYMBOL(...) |
| 5130 | if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5131 | WARN("MISSING_VMLINUX_SYMBOL", |
| 5132 | "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | # check for redundant bracing round if etc |
| 5136 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 5137 | my ($level, $endln, @chunks) = |
| 5138 | ctx_statement_full($linenr, $realcnt, 1); |
| 5139 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
| 5140 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 5141 | if ($#chunks > 0 && $level == 0) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5142 | my @allowed = (); |
| 5143 | my $allow = 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5144 | my $seen = 0; |
| 5145 | my $herectx = $here . "\n"; |
| 5146 | my $ln = $linenr - 1; |
| 5147 | for my $chunk (@chunks) { |
| 5148 | my ($cond, $block) = @{$chunk}; |
| 5149 | |
| 5150 | # If the condition carries leading newlines, then count those as offsets. |
| 5151 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 5152 | my $offset = statement_rawlines($whitespace) - 1; |
| 5153 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5154 | $allowed[$allow] = 0; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5155 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 5156 | |
| 5157 | # We have looked at and allowed this specific line. |
| 5158 | $suppress_ifbraces{$ln + $offset} = 1; |
| 5159 | |
| 5160 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
| 5161 | $ln += statement_rawlines($block) - 1; |
| 5162 | |
| 5163 | substr($block, 0, length($cond), ''); |
| 5164 | |
| 5165 | $seen++ if ($block =~ /^\s*{/); |
| 5166 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5167 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5168 | if (statement_lines($cond) > 1) { |
| 5169 | #print "APW: ALLOWED: cond<$cond>\n"; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5170 | $allowed[$allow] = 1; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5171 | } |
| 5172 | if ($block =~/\b(?:if|for|while)\b/) { |
| 5173 | #print "APW: ALLOWED: block<$block>\n"; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5174 | $allowed[$allow] = 1; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5175 | } |
| 5176 | if (statement_block_size($block) > 1) { |
| 5177 | #print "APW: ALLOWED: lines block<$block>\n"; |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5178 | $allowed[$allow] = 1; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5179 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5180 | $allow++; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5181 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5182 | if ($seen) { |
| 5183 | my $sum_allowed = 0; |
| 5184 | foreach (@allowed) { |
| 5185 | $sum_allowed += $_; |
| 5186 | } |
| 5187 | if ($sum_allowed == 0) { |
| 5188 | WARN("BRACES", |
| 5189 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 5190 | } elsif ($sum_allowed != $allow && |
| 5191 | $seen != $allow) { |
| 5192 | CHK("BRACES", |
| 5193 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 5194 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5195 | } |
| 5196 | } |
| 5197 | } |
| 5198 | if (!defined $suppress_ifbraces{$linenr - 1} && |
| 5199 | $line =~ /\b(if|while|for|else)\b/) { |
| 5200 | my $allowed = 0; |
| 5201 | |
| 5202 | # Check the pre-context. |
| 5203 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 5204 | #print "APW: ALLOWED: pre<$1>\n"; |
| 5205 | $allowed = 1; |
| 5206 | } |
| 5207 | |
| 5208 | my ($level, $endln, @chunks) = |
| 5209 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 5210 | |
| 5211 | # Check the condition. |
| 5212 | my ($cond, $block) = @{$chunks[0]}; |
| 5213 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
| 5214 | if (defined $cond) { |
| 5215 | substr($block, 0, length($cond), ''); |
| 5216 | } |
| 5217 | if (statement_lines($cond) > 1) { |
| 5218 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 5219 | $allowed = 1; |
| 5220 | } |
| 5221 | if ($block =~/\b(?:if|for|while)\b/) { |
| 5222 | #print "APW: ALLOWED: block<$block>\n"; |
| 5223 | $allowed = 1; |
| 5224 | } |
| 5225 | if (statement_block_size($block) > 1) { |
| 5226 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 5227 | $allowed = 1; |
| 5228 | } |
| 5229 | # Check the post-context. |
| 5230 | if (defined $chunks[1]) { |
| 5231 | my ($cond, $block) = @{$chunks[1]}; |
| 5232 | if (defined $cond) { |
| 5233 | substr($block, 0, length($cond), ''); |
| 5234 | } |
| 5235 | if ($block =~ /^\s*\{/) { |
| 5236 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 5237 | $allowed = 1; |
| 5238 | } |
| 5239 | } |
| 5240 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5241 | my $cnt = statement_rawlines($block); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5242 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5243 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5244 | WARN("BRACES", |
| 5245 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5246 | } |
| 5247 | } |
| 5248 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5249 | # check for single line unbalanced braces |
| 5250 | if ($sline =~ /^.\s*\}\s*else\s*$/ || |
| 5251 | $sline =~ /^.\s*else\s*\{\s*$/) { |
| 5252 | CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); |
| 5253 | } |
| 5254 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5255 | # check for unnecessary blank lines around braces |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5256 | if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5257 | if (CHK("BRACES", |
| 5258 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && |
| 5259 | $fix && $prevrawline =~ /^\+/) { |
| 5260 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5261 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5262 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5263 | if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5264 | if (CHK("BRACES", |
| 5265 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && |
| 5266 | $fix) { |
| 5267 | fix_delete_line($fixlinenr, $rawline); |
| 5268 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | # no volatiles please |
| 5272 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 5273 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5274 | WARN("VOLATILE", |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5275 | "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5276 | } |
| 5277 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5278 | # Check for user-visible strings broken across lines, which breaks the ability |
| 5279 | # to grep for the string. Make exceptions when the previous string ends in a |
| 5280 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' |
| 5281 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value |
| 5282 | if ($line =~ /^\+\s*$String/ && |
| 5283 | $prevline =~ /"\s*$/ && |
| 5284 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { |
| 5285 | if (WARN("SPLIT_STRING", |
| 5286 | "quoted string split across lines\n" . $hereprev) && |
| 5287 | $fix && |
| 5288 | $prevrawline =~ /^\+.*"\s*$/ && |
| 5289 | $last_coalesced_string_linenr != $linenr - 1) { |
| 5290 | my $extracted_string = get_quoted_string($line, $rawline); |
| 5291 | my $comma_close = ""; |
| 5292 | if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { |
| 5293 | $comma_close = $1; |
| 5294 | } |
| 5295 | |
| 5296 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5297 | fix_delete_line($fixlinenr, $rawline); |
| 5298 | my $fixedline = $prevrawline; |
| 5299 | $fixedline =~ s/"\s*$//; |
| 5300 | $fixedline .= substr($extracted_string, 1) . trim($comma_close); |
| 5301 | fix_insert_line($fixlinenr - 1, $fixedline); |
| 5302 | $fixedline = $rawline; |
| 5303 | $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; |
| 5304 | if ($fixedline !~ /\+\s*$/) { |
| 5305 | fix_insert_line($fixlinenr, $fixedline); |
| 5306 | } |
| 5307 | $last_coalesced_string_linenr = $linenr; |
| 5308 | } |
| 5309 | } |
| 5310 | |
| 5311 | # check for missing a space in a string concatenation |
| 5312 | if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { |
| 5313 | WARN('MISSING_SPACE', |
| 5314 | "break quoted strings at a space character\n" . $hereprev); |
| 5315 | } |
| 5316 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5317 | # check for an embedded function name in a string when the function is known |
| 5318 | # This does not work very well for -f --file checking as it depends on patch |
| 5319 | # context providing the function name or a single line form for in-file |
| 5320 | # function declarations |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5321 | if ($line =~ /^\+.*$String/ && |
| 5322 | defined($context_function) && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5323 | get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && |
| 5324 | length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5325 | WARN("EMBEDDED_FUNCTION_NAME", |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5326 | "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5327 | } |
| 5328 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5329 | # check for spaces before a quoted newline |
| 5330 | if ($rawline =~ /^.*\".*\s\\n/) { |
| 5331 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 5332 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && |
| 5333 | $fix) { |
| 5334 | $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; |
| 5335 | } |
| 5336 | |
| 5337 | } |
| 5338 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5339 | # concatenated string without spaces between elements |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5340 | if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5341 | CHK("CONCATENATED_STRING", |
| 5342 | "Concatenated strings should use spaces between elements\n" . $herecurr); |
| 5343 | } |
| 5344 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5345 | # uncoalesced string fragments |
| 5346 | if ($line =~ /$String\s*"/) { |
| 5347 | WARN("STRING_FRAGMENTS", |
| 5348 | "Consecutive strings are generally better as a single string\n" . $herecurr); |
| 5349 | } |
| 5350 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5351 | # check for non-standard and hex prefixed decimal printf formats |
| 5352 | my $show_L = 1; #don't show the same defect twice |
| 5353 | my $show_Z = 1; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5354 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5355 | my $string = substr($rawline, $-[1], $+[1] - $-[1]); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5356 | $string =~ s/%%/__/g; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5357 | # check for %L |
| 5358 | if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5359 | WARN("PRINTF_L", |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5360 | "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); |
| 5361 | $show_L = 0; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5362 | } |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5363 | # check for %Z |
| 5364 | if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { |
| 5365 | WARN("PRINTF_Z", |
| 5366 | "%Z$1 is non-standard C, use %z$1\n" . $herecurr); |
| 5367 | $show_Z = 0; |
| 5368 | } |
| 5369 | # check for 0x<decimal> |
| 5370 | if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { |
| 5371 | ERROR("PRINTF_0XDECIMAL", |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5372 | "Prefixing 0x with decimal output is defective\n" . $herecurr); |
| 5373 | } |
| 5374 | } |
| 5375 | |
| 5376 | # check for line continuations in quoted strings with odd counts of " |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5377 | if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5378 | WARN("LINE_CONTINUATIONS", |
| 5379 | "Avoid line continuations in quoted strings\n" . $herecurr); |
| 5380 | } |
| 5381 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5382 | # warn about #if 0 |
| 5383 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5384 | CHK("REDUNDANT_CODE", |
| 5385 | "if this code is redundant consider removing it\n" . |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5386 | $herecurr); |
| 5387 | } |
| 5388 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5389 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 5390 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5391 | my $tested = quotemeta($1); |
| 5392 | my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; |
| 5393 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { |
| 5394 | my $func = $1; |
| 5395 | if (WARN('NEEDLESS_IF', |
| 5396 | "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && |
| 5397 | $fix) { |
| 5398 | my $do_fix = 1; |
| 5399 | my $leading_tabs = ""; |
| 5400 | my $new_leading_tabs = ""; |
| 5401 | if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { |
| 5402 | $leading_tabs = $1; |
| 5403 | } else { |
| 5404 | $do_fix = 0; |
| 5405 | } |
| 5406 | if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { |
| 5407 | $new_leading_tabs = $1; |
| 5408 | if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { |
| 5409 | $do_fix = 0; |
| 5410 | } |
| 5411 | } else { |
| 5412 | $do_fix = 0; |
| 5413 | } |
| 5414 | if ($do_fix) { |
| 5415 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5416 | $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; |
| 5417 | } |
| 5418 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5419 | } |
| 5420 | } |
| 5421 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5422 | # check for unnecessary "Out of Memory" messages |
| 5423 | if ($line =~ /^\+.*\b$logFunctions\s*\(/ && |
| 5424 | $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && |
| 5425 | (defined $1 || defined $3) && |
| 5426 | $linenr > 3) { |
| 5427 | my $testval = $2; |
| 5428 | my $testline = $lines[$linenr - 3]; |
| 5429 | |
| 5430 | my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); |
| 5431 | # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); |
| 5432 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5433 | if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5434 | WARN("OOM_MESSAGE", |
| 5435 | "Possible unnecessary 'out of memory' message\n" . $hereprev); |
| 5436 | } |
| 5437 | } |
| 5438 | |
| 5439 | # check for logging functions with KERN_<LEVEL> |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5440 | if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5441 | $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { |
| 5442 | my $level = $1; |
| 5443 | if (WARN("UNNECESSARY_KERN_LEVEL", |
| 5444 | "Possible unnecessary $level\n" . $herecurr) && |
| 5445 | $fix) { |
| 5446 | $fixed[$fixlinenr] =~ s/\s*$level\s*//; |
| 5447 | } |
| 5448 | } |
| 5449 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5450 | # check for logging continuations |
| 5451 | if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { |
| 5452 | WARN("LOGGING_CONTINUATION", |
| 5453 | "Avoid logging continuation uses where feasible\n" . $herecurr); |
| 5454 | } |
| 5455 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5456 | # check for mask then right shift without a parentheses |
| 5457 | if ($^V && $^V ge 5.10.0 && |
| 5458 | $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && |
| 5459 | $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so |
| 5460 | WARN("MASK_THEN_SHIFT", |
| 5461 | "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); |
| 5462 | } |
| 5463 | |
| 5464 | # check for pointer comparisons to NULL |
| 5465 | if ($^V && $^V ge 5.10.0) { |
| 5466 | while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { |
| 5467 | my $val = $1; |
| 5468 | my $equal = "!"; |
| 5469 | $equal = "" if ($4 eq "!="); |
| 5470 | if (CHK("COMPARISON_TO_NULL", |
| 5471 | "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && |
| 5472 | $fix) { |
| 5473 | $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; |
| 5474 | } |
| 5475 | } |
| 5476 | } |
| 5477 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5478 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
| 5479 | if ($line =~ /(\b$InitAttribute\b)/) { |
| 5480 | my $attr = $1; |
| 5481 | if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { |
| 5482 | my $ptr = $1; |
| 5483 | my $var = $2; |
| 5484 | if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && |
| 5485 | ERROR("MISPLACED_INIT", |
| 5486 | "$attr should be placed after $var\n" . $herecurr)) || |
| 5487 | ($ptr !~ /\b(union|struct)\s+$attr\b/ && |
| 5488 | WARN("MISPLACED_INIT", |
| 5489 | "$attr should be placed after $var\n" . $herecurr))) && |
| 5490 | $fix) { |
| 5491 | $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; |
| 5492 | } |
| 5493 | } |
| 5494 | } |
| 5495 | |
| 5496 | # check for $InitAttributeData (ie: __initdata) with const |
| 5497 | if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { |
| 5498 | my $attr = $1; |
| 5499 | $attr =~ /($InitAttributePrefix)(.*)/; |
| 5500 | my $attr_prefix = $1; |
| 5501 | my $attr_type = $2; |
| 5502 | if (ERROR("INIT_ATTRIBUTE", |
| 5503 | "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && |
| 5504 | $fix) { |
| 5505 | $fixed[$fixlinenr] =~ |
| 5506 | s/$InitAttributeData/${attr_prefix}initconst/; |
| 5507 | } |
| 5508 | } |
| 5509 | |
| 5510 | # check for $InitAttributeConst (ie: __initconst) without const |
| 5511 | if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { |
| 5512 | my $attr = $1; |
| 5513 | if (ERROR("INIT_ATTRIBUTE", |
| 5514 | "Use of $attr requires a separate use of const\n" . $herecurr) && |
| 5515 | $fix) { |
| 5516 | my $lead = $fixed[$fixlinenr] =~ |
| 5517 | /(^\+\s*(?:static\s+))/; |
| 5518 | $lead = rtrim($1); |
| 5519 | $lead = "$lead " if ($lead !~ /^\+$/); |
| 5520 | $lead = "${lead}const "; |
| 5521 | $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; |
| 5522 | } |
| 5523 | } |
| 5524 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5525 | # check for __read_mostly with const non-pointer (should just be const) |
| 5526 | if ($line =~ /\b__read_mostly\b/ && |
| 5527 | $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { |
| 5528 | if (ERROR("CONST_READ_MOSTLY", |
| 5529 | "Invalid use of __read_mostly with const type\n" . $herecurr) && |
| 5530 | $fix) { |
| 5531 | $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; |
| 5532 | } |
| 5533 | } |
| 5534 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5535 | # don't use __constant_<foo> functions outside of include/uapi/ |
| 5536 | if ($realfile !~ m@^include/uapi/@ && |
| 5537 | $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { |
| 5538 | my $constant_func = $1; |
| 5539 | my $func = $constant_func; |
| 5540 | $func =~ s/^__constant_//; |
| 5541 | if (WARN("CONSTANT_CONVERSION", |
| 5542 | "$constant_func should be $func\n" . $herecurr) && |
| 5543 | $fix) { |
| 5544 | $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; |
| 5545 | } |
| 5546 | } |
| 5547 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5548 | # prefer usleep_range over udelay |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5549 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5550 | my $delay = $1; |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5551 | # ignore udelay's < 10, however |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5552 | if (! ($delay < 10) ) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5553 | CHK("USLEEP_RANGE", |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5554 | "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); |
| 5555 | } |
| 5556 | if ($delay > 2000) { |
| 5557 | WARN("LONG_UDELAY", |
| 5558 | "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5559 | } |
| 5560 | } |
| 5561 | |
| 5562 | # warn about unexpectedly long msleep's |
| 5563 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 5564 | if ($1 < 20) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5565 | WARN("MSLEEP", |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5566 | "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5567 | } |
| 5568 | } |
| 5569 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5570 | # check for comparisons of jiffies |
| 5571 | if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { |
| 5572 | WARN("JIFFIES_COMPARISON", |
| 5573 | "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); |
| 5574 | } |
| 5575 | |
| 5576 | # check for comparisons of get_jiffies_64() |
| 5577 | if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { |
| 5578 | WARN("JIFFIES_COMPARISON", |
| 5579 | "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); |
| 5580 | } |
| 5581 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5582 | # warn about #ifdefs in C files |
| 5583 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
| 5584 | # print "#ifdef in C files should be avoided\n"; |
| 5585 | # print "$herecurr"; |
| 5586 | # $clean = 0; |
| 5587 | # } |
| 5588 | |
| 5589 | # warn about spacing in #ifdefs |
| 5590 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5591 | if (ERROR("SPACING", |
| 5592 | "exactly one space required after that #$1\n" . $herecurr) && |
| 5593 | $fix) { |
| 5594 | $fixed[$fixlinenr] =~ |
| 5595 | s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; |
| 5596 | } |
| 5597 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5598 | } |
| 5599 | |
| 5600 | # check for spinlock_t definitions without a comment. |
| 5601 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 5602 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
| 5603 | my $which = $1; |
| 5604 | if (!ctx_has_comment($first_line, $linenr)) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5605 | CHK("UNCOMMENTED_DEFINITION", |
| 5606 | "$1 definition without comment\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5607 | } |
| 5608 | } |
| 5609 | # check for memory barriers without a comment. |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5610 | |
| 5611 | my $barriers = qr{ |
| 5612 | mb| |
| 5613 | rmb| |
| 5614 | wmb| |
| 5615 | read_barrier_depends |
| 5616 | }x; |
| 5617 | my $barrier_stems = qr{ |
| 5618 | mb__before_atomic| |
| 5619 | mb__after_atomic| |
| 5620 | store_release| |
| 5621 | load_acquire| |
| 5622 | store_mb| |
| 5623 | (?:$barriers) |
| 5624 | }x; |
| 5625 | my $all_barriers = qr{ |
| 5626 | (?:$barriers)| |
| 5627 | smp_(?:$barrier_stems)| |
| 5628 | virt_(?:$barrier_stems) |
| 5629 | }x; |
| 5630 | |
| 5631 | if ($line =~ /\b(?:$all_barriers)\s*\(/) { |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5632 | if (!ctx_has_comment($first_line, $linenr)) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5633 | WARN("MEMORY_BARRIER", |
| 5634 | "memory barrier without comment\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5635 | } |
| 5636 | } |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5637 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5638 | my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; |
| 5639 | |
| 5640 | if ($realfile !~ m@^include/asm-generic/@ && |
| 5641 | $realfile !~ m@/barrier\.h$@ && |
| 5642 | $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && |
| 5643 | $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { |
| 5644 | WARN("MEMORY_BARRIER", |
| 5645 | "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); |
| 5646 | } |
| 5647 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5648 | # check for waitqueue_active without a comment. |
| 5649 | if ($line =~ /\bwaitqueue_active\s*\(/) { |
| 5650 | if (!ctx_has_comment($first_line, $linenr)) { |
| 5651 | WARN("WAITQUEUE_ACTIVE", |
| 5652 | "waitqueue_active without comment\n" . $herecurr); |
| 5653 | } |
| 5654 | } |
| 5655 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5656 | # check for smp_read_barrier_depends and read_barrier_depends |
| 5657 | if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) { |
| 5658 | WARN("READ_BARRIER_DEPENDS", |
| 5659 | "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5660 | } |
| 5661 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5662 | # check of hardware specific defines |
| 5663 | if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5664 | CHK("ARCH_DEFINES", |
| 5665 | "architecture specific defines should be avoided\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5666 | } |
| 5667 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5668 | # check that the storage class is not after a type |
| 5669 | if ($line =~ /\b($Type)\s+($Storage)\b/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5670 | WARN("STORAGE_CLASS", |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5671 | "storage class '$2' should be located before type '$1'\n" . $herecurr); |
| 5672 | } |
| 5673 | # Check that the storage class is at the beginning of a declaration |
| 5674 | if ($line =~ /\b$Storage\b/ && |
| 5675 | $line !~ /^.\s*$Storage/ && |
| 5676 | $line =~ /^.\s*(.+?)\$Storage\s/ && |
| 5677 | $1 !~ /[\,\)]\s*$/) { |
| 5678 | WARN("STORAGE_CLASS", |
| 5679 | "storage class should be at the beginning of the declaration\n" . $herecurr); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5680 | } |
| 5681 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5682 | # check the location of the inline attribute, that it is between |
| 5683 | # storage class and type. |
| 5684 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 5685 | $line =~ /\b$Inline\s+$Storage\b/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5686 | ERROR("INLINE_LOCATION", |
| 5687 | "inline keyword should sit between storage class and type\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | # Check for __inline__ and __inline, prefer inline |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5691 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5692 | $line =~ /\b(__inline__|__inline)\b/) { |
| 5693 | if (WARN("INLINE", |
| 5694 | "plain inline is preferred over $1\n" . $herecurr) && |
| 5695 | $fix) { |
| 5696 | $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; |
| 5697 | |
| 5698 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 5699 | } |
| 5700 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5701 | # Check for __attribute__ packed, prefer __packed |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5702 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5703 | $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5704 | WARN("PREFER_PACKED", |
| 5705 | "__packed is preferred over __attribute__((packed))\n" . $herecurr); |
| 5706 | } |
| 5707 | |
| 5708 | # Check for __attribute__ aligned, prefer __aligned |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5709 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5710 | $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5711 | WARN("PREFER_ALIGNED", |
| 5712 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
| 5713 | } |
| 5714 | |
| 5715 | # Check for __attribute__ format(printf, prefer __printf |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5716 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5717 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { |
| 5718 | if (WARN("PREFER_PRINTF", |
| 5719 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && |
| 5720 | $fix) { |
| 5721 | $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; |
| 5722 | |
| 5723 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5724 | } |
| 5725 | |
| 5726 | # Check for __attribute__ format(scanf, prefer __scanf |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5727 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5728 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { |
| 5729 | if (WARN("PREFER_SCANF", |
| 5730 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && |
| 5731 | $fix) { |
| 5732 | $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; |
| 5733 | } |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5734 | } |
| 5735 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5736 | # Check for __attribute__ weak, or __weak declarations (may have link issues) |
| 5737 | if ($^V && $^V ge 5.10.0 && |
| 5738 | $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && |
| 5739 | ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || |
| 5740 | $line =~ /\b__weak\b/)) { |
| 5741 | ERROR("WEAK_DECLARATION", |
| 5742 | "Using weak declarations can have unintended link defects\n" . $herecurr); |
| 5743 | } |
| 5744 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5745 | # check for c99 types like uint8_t used outside of uapi/ and tools/ |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5746 | if ($realfile !~ m@\binclude/uapi/@ && |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5747 | $realfile !~ m@\btools/@ && |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5748 | $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { |
| 5749 | my $type = $1; |
| 5750 | if ($type =~ /\b($typeC99Typedefs)\b/) { |
| 5751 | $type = $1; |
| 5752 | my $kernel_type = 'u'; |
| 5753 | $kernel_type = 's' if ($type =~ /^_*[si]/); |
| 5754 | $type =~ /(\d+)/; |
| 5755 | $kernel_type .= $1; |
| 5756 | if (CHK("PREFER_KERNEL_TYPES", |
| 5757 | "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && |
| 5758 | $fix) { |
| 5759 | $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; |
| 5760 | } |
| 5761 | } |
| 5762 | } |
| 5763 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5764 | # check for cast of C90 native int or longer types constants |
| 5765 | if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { |
| 5766 | my $cast = $1; |
| 5767 | my $const = $2; |
| 5768 | if (WARN("TYPECAST_INT_CONSTANT", |
| 5769 | "Unnecessary typecast of c90 int constant\n" . $herecurr) && |
| 5770 | $fix) { |
| 5771 | my $suffix = ""; |
| 5772 | my $newconst = $const; |
| 5773 | $newconst =~ s/${Int_type}$//; |
| 5774 | $suffix .= 'U' if ($cast =~ /\bunsigned\b/); |
| 5775 | if ($cast =~ /\blong\s+long\b/) { |
| 5776 | $suffix .= 'LL'; |
| 5777 | } elsif ($cast =~ /\blong\b/) { |
| 5778 | $suffix .= 'L'; |
| 5779 | } |
| 5780 | $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; |
| 5781 | } |
| 5782 | } |
| 5783 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5784 | # check for sizeof(&) |
| 5785 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5786 | WARN("SIZEOF_ADDRESS", |
| 5787 | "sizeof(& should be avoided\n" . $herecurr); |
| 5788 | } |
| 5789 | |
| 5790 | # check for sizeof without parenthesis |
| 5791 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5792 | if (WARN("SIZEOF_PARENTHESIS", |
| 5793 | "sizeof $1 should be sizeof($1)\n" . $herecurr) && |
| 5794 | $fix) { |
| 5795 | $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; |
| 5796 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5797 | } |
| 5798 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5799 | # check for struct spinlock declarations |
| 5800 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 5801 | WARN("USE_SPINLOCK_T", |
| 5802 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 5803 | } |
| 5804 | |
| 5805 | # check for seq_printf uses that could be seq_puts |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5806 | if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5807 | my $fmt = get_quoted_string($line, $rawline); |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5808 | $fmt =~ s/%%//g; |
| 5809 | if ($fmt !~ /%/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5810 | if (WARN("PREFER_SEQ_PUTS", |
| 5811 | "Prefer seq_puts to seq_printf\n" . $herecurr) && |
| 5812 | $fix) { |
| 5813 | $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; |
| 5814 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5815 | } |
| 5816 | } |
| 5817 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5818 | # check for vsprintf extension %p<foo> misuses |
| 5819 | if ($^V && $^V ge 5.10.0 && |
| 5820 | defined $stat && |
| 5821 | $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && |
| 5822 | $1 !~ /^_*volatile_*$/) { |
| 5823 | my $specifier; |
| 5824 | my $extension; |
| 5825 | my $bad_specifier = ""; |
| 5826 | my $stat_real; |
| 5827 | |
| 5828 | my $lc = $stat =~ tr@\n@@; |
| 5829 | $lc = $lc + $linenr; |
| 5830 | for (my $count = $linenr; $count <= $lc; $count++) { |
| 5831 | my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); |
| 5832 | $fmt =~ s/%%//g; |
| 5833 | |
| 5834 | while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) { |
| 5835 | $specifier = $1; |
| 5836 | $extension = $2; |
| 5837 | if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) { |
| 5838 | $bad_specifier = $specifier; |
| 5839 | last; |
| 5840 | } |
| 5841 | if ($extension eq "x" && !defined($stat_real)) { |
| 5842 | if (!defined($stat_real)) { |
| 5843 | $stat_real = get_stat_real($linenr, $lc); |
| 5844 | } |
| 5845 | WARN("VSPRINTF_SPECIFIER_PX", |
| 5846 | "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"); |
| 5847 | } |
| 5848 | } |
| 5849 | if ($bad_specifier ne "") { |
| 5850 | my $stat_real = get_stat_real($linenr, $lc); |
| 5851 | my $ext_type = "Invalid"; |
| 5852 | my $use = ""; |
| 5853 | if ($bad_specifier =~ /p[Ff]/) { |
| 5854 | $ext_type = "Deprecated"; |
| 5855 | $use = " - use %pS instead"; |
| 5856 | $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); |
| 5857 | } |
| 5858 | |
| 5859 | WARN("VSPRINTF_POINTER_EXTENSION", |
| 5860 | "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); |
| 5861 | } |
| 5862 | } |
| 5863 | } |
| 5864 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5865 | # Check for misused memsets |
| 5866 | if ($^V && $^V ge 5.10.0 && |
| 5867 | defined $stat && |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5868 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5869 | |
| 5870 | my $ms_addr = $2; |
| 5871 | my $ms_val = $7; |
| 5872 | my $ms_size = $12; |
| 5873 | |
| 5874 | if ($ms_size =~ /^(0x|)0$/i) { |
| 5875 | ERROR("MEMSET", |
| 5876 | "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); |
| 5877 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 5878 | WARN("MEMSET", |
| 5879 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 5880 | } |
| 5881 | } |
| 5882 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5883 | # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5884 | # if ($^V && $^V ge 5.10.0 && |
| 5885 | # defined $stat && |
| 5886 | # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 5887 | # if (WARN("PREFER_ETHER_ADDR_COPY", |
| 5888 | # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && |
| 5889 | # $fix) { |
| 5890 | # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; |
| 5891 | # } |
| 5892 | # } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5893 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5894 | # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5895 | # if ($^V && $^V ge 5.10.0 && |
| 5896 | # defined $stat && |
| 5897 | # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 5898 | # WARN("PREFER_ETHER_ADDR_EQUAL", |
| 5899 | # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") |
| 5900 | # } |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5901 | |
| 5902 | # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr |
| 5903 | # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 5904 | # if ($^V && $^V ge 5.10.0 && |
| 5905 | # defined $stat && |
| 5906 | # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 5907 | # |
| 5908 | # my $ms_val = $7; |
| 5909 | # |
| 5910 | # if ($ms_val =~ /^(?:0x|)0+$/i) { |
| 5911 | # if (WARN("PREFER_ETH_ZERO_ADDR", |
| 5912 | # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && |
| 5913 | # $fix) { |
| 5914 | # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; |
| 5915 | # } |
| 5916 | # } elsif ($ms_val =~ /^(?:0xff|255)$/i) { |
| 5917 | # if (WARN("PREFER_ETH_BROADCAST_ADDR", |
| 5918 | # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && |
| 5919 | # $fix) { |
| 5920 | # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; |
| 5921 | # } |
| 5922 | # } |
| 5923 | # } |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 5924 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 5925 | # typecasts on min/max could be min_t/max_t |
| 5926 | if ($^V && $^V ge 5.10.0 && |
| 5927 | defined $stat && |
| 5928 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
| 5929 | if (defined $2 || defined $7) { |
| 5930 | my $call = $1; |
| 5931 | my $cast1 = deparenthesize($2); |
| 5932 | my $arg1 = $3; |
| 5933 | my $cast2 = deparenthesize($7); |
| 5934 | my $arg2 = $8; |
| 5935 | my $cast; |
| 5936 | |
| 5937 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
| 5938 | $cast = "$cast1 or $cast2"; |
| 5939 | } elsif ($cast1 ne "") { |
| 5940 | $cast = $cast1; |
| 5941 | } else { |
| 5942 | $cast = $cast2; |
| 5943 | } |
| 5944 | WARN("MINMAX", |
| 5945 | "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); |
| 5946 | } |
| 5947 | } |
| 5948 | |
| 5949 | # check usleep_range arguments |
| 5950 | if ($^V && $^V ge 5.10.0 && |
| 5951 | defined $stat && |
| 5952 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 5953 | my $min = $1; |
| 5954 | my $max = $7; |
| 5955 | if ($min eq $max) { |
| 5956 | WARN("USLEEP_RANGE", |
| 5957 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 5958 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 5959 | $min > $max) { |
| 5960 | WARN("USLEEP_RANGE", |
| 5961 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 5962 | } |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 5963 | } |
| 5964 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5965 | # check for naked sscanf |
| 5966 | if ($^V && $^V ge 5.10.0 && |
| 5967 | defined $stat && |
| 5968 | $line =~ /\bsscanf\b/ && |
| 5969 | ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && |
| 5970 | $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && |
| 5971 | $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { |
| 5972 | my $lc = $stat =~ tr@\n@@; |
| 5973 | $lc = $lc + $linenr; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5974 | my $stat_real = get_stat_real($linenr, $lc); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5975 | WARN("NAKED_SSCANF", |
| 5976 | "unchecked sscanf return value\n" . "$here\n$stat_real\n"); |
| 5977 | } |
| 5978 | |
| 5979 | # check for simple sscanf that should be kstrto<foo> |
| 5980 | if ($^V && $^V ge 5.10.0 && |
| 5981 | defined $stat && |
| 5982 | $line =~ /\bsscanf\b/) { |
| 5983 | my $lc = $stat =~ tr@\n@@; |
| 5984 | $lc = $lc + $linenr; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 5985 | my $stat_real = get_stat_real($linenr, $lc); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 5986 | if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { |
| 5987 | my $format = $6; |
| 5988 | my $count = $format =~ tr@%@%@; |
| 5989 | if ($count == 1 && |
| 5990 | $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { |
| 5991 | WARN("SSCANF_TO_KSTRTO", |
| 5992 | "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); |
| 5993 | } |
| 5994 | } |
| 5995 | } |
| 5996 | |
| 5997 | # check for new externs in .h files. |
| 5998 | if ($realfile =~ /\.h$/ && |
| 5999 | $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { |
| 6000 | if (CHK("AVOID_EXTERNS", |
| 6001 | "extern prototypes should be avoided in .h files\n" . $herecurr) && |
| 6002 | $fix) { |
| 6003 | $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; |
| 6004 | } |
| 6005 | } |
| 6006 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6007 | # check for new externs in .c files. |
| 6008 | if ($realfile =~ /\.c$/ && defined $stat && |
| 6009 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
| 6010 | { |
| 6011 | my $function_name = $1; |
| 6012 | my $paren_space = $2; |
| 6013 | |
| 6014 | my $s = $stat; |
| 6015 | if (defined $cond) { |
| 6016 | substr($s, 0, length($cond), ''); |
| 6017 | } |
| 6018 | if ($s =~ /^\s*;/ && |
| 6019 | $function_name ne 'uninitialized_var') |
| 6020 | { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6021 | WARN("AVOID_EXTERNS", |
| 6022 | "externs should be avoided in .c files\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6023 | } |
| 6024 | |
| 6025 | if ($paren_space =~ /\n/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6026 | WARN("FUNCTION_ARGUMENTS", |
| 6027 | "arguments for function declarations should follow identifier\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6028 | } |
| 6029 | |
| 6030 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 6031 | $stat =~ /^.\s*extern\s+/) |
| 6032 | { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6033 | WARN("AVOID_EXTERNS", |
| 6034 | "externs should be avoided in .c files\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6035 | } |
| 6036 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6037 | # check for function declarations that have arguments without identifier names |
| 6038 | if (defined $stat && |
| 6039 | $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6040 | $1 ne "void") { |
| 6041 | my $args = trim($1); |
| 6042 | while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { |
| 6043 | my $arg = trim($1); |
| 6044 | if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { |
| 6045 | WARN("FUNCTION_ARGUMENTS", |
| 6046 | "function definition argument '$arg' should also have an identifier name\n" . $herecurr); |
| 6047 | } |
| 6048 | } |
| 6049 | } |
| 6050 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6051 | # check for function definitions |
| 6052 | if ($^V && $^V ge 5.10.0 && |
| 6053 | defined $stat && |
| 6054 | $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { |
| 6055 | $context_function = $1; |
| 6056 | |
| 6057 | # check for multiline function definition with misplaced open brace |
| 6058 | my $ok = 0; |
| 6059 | my $cnt = statement_rawlines($stat); |
| 6060 | my $herectx = $here . "\n"; |
| 6061 | for (my $n = 0; $n < $cnt; $n++) { |
| 6062 | my $rl = raw_line($linenr, $n); |
| 6063 | $herectx .= $rl . "\n"; |
| 6064 | $ok = 1 if ($rl =~ /^[ \+]\{/); |
| 6065 | $ok = 1 if ($rl =~ /\{/ && $n == 0); |
| 6066 | last if $rl =~ /^[ \+].*\{/; |
| 6067 | } |
| 6068 | if (!$ok) { |
| 6069 | ERROR("OPEN_BRACE", |
| 6070 | "open brace '{' following function definitions go on the next line\n" . $herectx); |
| 6071 | } |
| 6072 | } |
| 6073 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6074 | # checks for new __setup's |
| 6075 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 6076 | my $name = $1; |
| 6077 | |
| 6078 | if (!grep(/$name/, @setup_docs)) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6079 | CHK("UNDOCUMENTED_SETUP", |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6080 | "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6081 | } |
| 6082 | } |
| 6083 | |
| 6084 | # check for pointless casting of kmalloc return |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6085 | if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6086 | WARN("UNNECESSARY_CASTS", |
| 6087 | "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
| 6088 | } |
| 6089 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6090 | # alloc style |
| 6091 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
| 6092 | if ($^V && $^V ge 5.10.0 && |
| 6093 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { |
| 6094 | CHK("ALLOC_SIZEOF_STRUCT", |
| 6095 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 6096 | } |
| 6097 | |
| 6098 | # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc |
| 6099 | if ($^V && $^V ge 5.10.0 && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6100 | defined $stat && |
| 6101 | $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6102 | my $oldfunc = $3; |
| 6103 | my $a1 = $4; |
| 6104 | my $a2 = $10; |
| 6105 | my $newfunc = "kmalloc_array"; |
| 6106 | $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); |
| 6107 | my $r1 = $a1; |
| 6108 | my $r2 = $a2; |
| 6109 | if ($a1 =~ /^sizeof\s*\S/) { |
| 6110 | $r1 = $a2; |
| 6111 | $r2 = $a1; |
| 6112 | } |
| 6113 | if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && |
| 6114 | !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6115 | my $cnt = statement_rawlines($stat); |
| 6116 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 6117 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6118 | if (WARN("ALLOC_WITH_MULTIPLY", |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6119 | "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && |
| 6120 | $cnt == 1 && |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6121 | $fix) { |
| 6122 | $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; |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6123 | } |
| 6124 | } |
| 6125 | } |
| 6126 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6127 | # check for krealloc arg reuse |
| 6128 | if ($^V && $^V ge 5.10.0 && |
| 6129 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { |
| 6130 | WARN("KREALLOC_ARG_REUSE", |
| 6131 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 6132 | } |
| 6133 | |
| 6134 | # check for alloc argument mismatch |
| 6135 | if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { |
| 6136 | WARN("ALLOC_ARRAY_ARGS", |
| 6137 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6138 | } |
| 6139 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6140 | # check for multiple semicolons |
| 6141 | if ($line =~ /;\s*;\s*$/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6142 | if (WARN("ONE_SEMICOLON", |
| 6143 | "Statements terminations use 1 semicolon\n" . $herecurr) && |
| 6144 | $fix) { |
| 6145 | $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; |
| 6146 | } |
| 6147 | } |
| 6148 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6149 | # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi |
| 6150 | if ($realfile !~ m@^include/uapi/@ && |
| 6151 | $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6152 | my $ull = ""; |
| 6153 | $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); |
| 6154 | if (CHK("BIT_MACRO", |
| 6155 | "Prefer using the BIT$ull macro\n" . $herecurr) && |
| 6156 | $fix) { |
| 6157 | $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; |
| 6158 | } |
| 6159 | } |
| 6160 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6161 | # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE |
| 6162 | if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) { |
| 6163 | my $config = $1; |
| 6164 | if (WARN("PREFER_IS_ENABLED", |
| 6165 | "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && |
| 6166 | $fix) { |
| 6167 | $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; |
| 6168 | } |
| 6169 | } |
| 6170 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6171 | # check for case / default statements not preceded by break/fallthrough/switch |
| 6172 | if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { |
| 6173 | my $has_break = 0; |
| 6174 | my $has_statement = 0; |
| 6175 | my $count = 0; |
| 6176 | my $prevline = $linenr; |
| 6177 | while ($prevline > 1 && ($file || $count < 3) && !$has_break) { |
| 6178 | $prevline--; |
| 6179 | my $rline = $rawlines[$prevline - 1]; |
| 6180 | my $fline = $lines[$prevline - 1]; |
| 6181 | last if ($fline =~ /^\@\@/); |
| 6182 | next if ($fline =~ /^\-/); |
| 6183 | next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); |
| 6184 | $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); |
| 6185 | next if ($fline =~ /^.[\s$;]*$/); |
| 6186 | $has_statement = 1; |
| 6187 | $count++; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6188 | $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6189 | } |
| 6190 | if (!$has_break && $has_statement) { |
| 6191 | WARN("MISSING_BREAK", |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6192 | "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6193 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6194 | } |
| 6195 | |
| 6196 | # check for switch/default statements without a break; |
| 6197 | if ($^V && $^V ge 5.10.0 && |
| 6198 | defined $stat && |
| 6199 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6200 | my $cnt = statement_rawlines($stat); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6201 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 6202 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6203 | WARN("DEFAULT_NO_BREAK", |
| 6204 | "switch default: should use break\n" . $herectx); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6205 | } |
| 6206 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6207 | # check for gcc specific __FUNCTION__ |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6208 | if ($line =~ /\b__FUNCTION__\b/) { |
| 6209 | if (WARN("USE_FUNC", |
| 6210 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && |
| 6211 | $fix) { |
| 6212 | $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; |
| 6213 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6214 | } |
| 6215 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6216 | # check for uses of __DATE__, __TIME__, __TIMESTAMP__ |
| 6217 | while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { |
| 6218 | ERROR("DATE_TIME", |
| 6219 | "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); |
| 6220 | } |
| 6221 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6222 | # check for use of yield() |
| 6223 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 6224 | WARN("YIELD", |
| 6225 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6226 | } |
| 6227 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6228 | # check for comparisons against true and false |
| 6229 | if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { |
| 6230 | my $lead = $1; |
| 6231 | my $arg = $2; |
| 6232 | my $test = $3; |
| 6233 | my $otype = $4; |
| 6234 | my $trail = $5; |
| 6235 | my $op = "!"; |
| 6236 | |
| 6237 | ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); |
| 6238 | |
| 6239 | my $type = lc($otype); |
| 6240 | if ($type =~ /^(?:true|false)$/) { |
| 6241 | if (("$test" eq "==" && "$type" eq "true") || |
| 6242 | ("$test" eq "!=" && "$type" eq "false")) { |
| 6243 | $op = ""; |
| 6244 | } |
| 6245 | |
| 6246 | CHK("BOOL_COMPARISON", |
| 6247 | "Using comparison to $otype is error prone\n" . $herecurr); |
| 6248 | |
| 6249 | ## maybe suggesting a correct construct would better |
| 6250 | ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); |
| 6251 | |
| 6252 | } |
| 6253 | } |
| 6254 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6255 | # check for bool bitfields |
| 6256 | if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) { |
| 6257 | WARN("BOOL_BITFIELD", |
| 6258 | "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr); |
| 6259 | } |
| 6260 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6261 | # check for semaphores initialized locked |
| 6262 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6263 | WARN("CONSIDER_COMPLETION", |
| 6264 | "consider using a completion\n" . $herecurr); |
| 6265 | } |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6266 | |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6267 | # recommend kstrto* over simple_strto* and strict_strto* |
| 6268 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
| 6269 | WARN("CONSIDER_KSTRTO", |
| 6270 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6271 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6272 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6273 | # check for __initcall(), use device_initcall() explicitly or more appropriate function please |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6274 | if ($line =~ /^.\s*__initcall\s*\(/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6275 | WARN("USE_DEVICE_INITCALL", |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6276 | "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6277 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6278 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6279 | # check for various structs that are normally const (ops, kgdb, device_tree) |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6280 | # and avoid what seem like struct definitions 'struct foo {' |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6281 | if ($line !~ /\bconst\b/ && |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6282 | $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6283 | WARN("CONST_STRUCT", |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6284 | "struct $1 should normally be const\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | # use of NR_CPUS is usually wrong |
| 6288 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 6289 | if ($line =~ /\bNR_CPUS\b/ && |
| 6290 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 6291 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
| 6292 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 6293 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 6294 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
| 6295 | { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6296 | WARN("NR_CPUS", |
| 6297 | "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6298 | } |
| 6299 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6300 | # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. |
| 6301 | if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { |
| 6302 | ERROR("DEFINE_ARCH_HAS", |
| 6303 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); |
| 6304 | } |
| 6305 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6306 | # likely/unlikely comparisons similar to "(likely(foo) > 0)" |
| 6307 | if ($^V && $^V ge 5.10.0 && |
| 6308 | $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { |
| 6309 | WARN("LIKELY_MISUSE", |
| 6310 | "Using $1 should generally have parentheses around the comparison\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6311 | } |
| 6312 | |
| 6313 | # whine mightly about in_atomic |
| 6314 | if ($line =~ /\bin_atomic\s*\(/) { |
| 6315 | if ($realfile =~ m@^drivers/@) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6316 | ERROR("IN_ATOMIC", |
| 6317 | "do not use in_atomic in drivers\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6318 | } elsif ($realfile !~ m@^kernel/@) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6319 | WARN("IN_ATOMIC", |
| 6320 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6321 | } |
| 6322 | } |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6323 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6324 | # check for mutex_trylock_recursive usage |
| 6325 | if ($line =~ /mutex_trylock_recursive/) { |
| 6326 | ERROR("LOCKING", |
| 6327 | "recursive locking is bad, do not use this ever.\n" . $herecurr); |
| 6328 | } |
| 6329 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6330 | # check for lockdep_set_novalidate_class |
| 6331 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 6332 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 6333 | if ($realfile !~ m@^kernel/lockdep@ && |
| 6334 | $realfile !~ m@^include/linux/lockdep@ && |
| 6335 | $realfile !~ m@^drivers/base/core@) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6336 | ERROR("LOCKDEP", |
| 6337 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6338 | } |
| 6339 | } |
| 6340 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6341 | if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || |
| 6342 | $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6343 | WARN("EXPORTED_WORLD_WRITABLE", |
| 6344 | "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6345 | } |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6346 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6347 | # check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> |
| 6348 | # and whether or not function naming is typical and if |
| 6349 | # DEVICE_ATTR permissions uses are unusual too |
| 6350 | if ($^V && $^V ge 5.10.0 && |
| 6351 | defined $stat && |
| 6352 | $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*\)/) { |
| 6353 | my $var = $1; |
| 6354 | my $perms = $2; |
| 6355 | my $show = $3; |
| 6356 | my $store = $4; |
| 6357 | my $octal_perms = perms_to_octal($perms); |
| 6358 | if ($show =~ /^${var}_show$/ && |
| 6359 | $store =~ /^${var}_store$/ && |
| 6360 | $octal_perms eq "0644") { |
| 6361 | if (WARN("DEVICE_ATTR_RW", |
| 6362 | "Use DEVICE_ATTR_RW\n" . $herecurr) && |
| 6363 | $fix) { |
| 6364 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/; |
| 6365 | } |
| 6366 | } elsif ($show =~ /^${var}_show$/ && |
| 6367 | $store =~ /^NULL$/ && |
| 6368 | $octal_perms eq "0444") { |
| 6369 | if (WARN("DEVICE_ATTR_RO", |
| 6370 | "Use DEVICE_ATTR_RO\n" . $herecurr) && |
| 6371 | $fix) { |
| 6372 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/; |
| 6373 | } |
| 6374 | } elsif ($show =~ /^NULL$/ && |
| 6375 | $store =~ /^${var}_store$/ && |
| 6376 | $octal_perms eq "0200") { |
| 6377 | if (WARN("DEVICE_ATTR_WO", |
| 6378 | "Use DEVICE_ATTR_WO\n" . $herecurr) && |
| 6379 | $fix) { |
| 6380 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/; |
| 6381 | } |
| 6382 | } elsif ($octal_perms eq "0644" || |
| 6383 | $octal_perms eq "0444" || |
| 6384 | $octal_perms eq "0200") { |
| 6385 | my $newshow = "$show"; |
| 6386 | $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); |
| 6387 | my $newstore = $store; |
| 6388 | $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); |
| 6389 | my $rename = ""; |
| 6390 | if ($show ne $newshow) { |
| 6391 | $rename .= " '$show' to '$newshow'"; |
| 6392 | } |
| 6393 | if ($store ne $newstore) { |
| 6394 | $rename .= " '$store' to '$newstore'"; |
| 6395 | } |
| 6396 | WARN("DEVICE_ATTR_FUNCTIONS", |
| 6397 | "Consider renaming function(s)$rename\n" . $herecurr); |
| 6398 | } else { |
| 6399 | WARN("DEVICE_ATTR_PERMS", |
| 6400 | "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); |
| 6401 | } |
| 6402 | } |
| 6403 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6404 | # Mode permission misuses where it seems decimal should be octal |
| 6405 | # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6406 | # o Ignore module_param*(...) uses with a decimal 0 permission as that has a |
| 6407 | # specific definition of not visible in sysfs. |
| 6408 | # o Ignore proc_create*(...) uses with a decimal 0 permission as that means |
| 6409 | # use the default permissions |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6410 | if ($^V && $^V ge 5.10.0 && |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6411 | defined $stat && |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6412 | $line =~ /$mode_perms_search/) { |
| 6413 | foreach my $entry (@mode_permission_funcs) { |
| 6414 | my $func = $entry->[0]; |
| 6415 | my $arg_pos = $entry->[1]; |
| 6416 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6417 | my $lc = $stat =~ tr@\n@@; |
| 6418 | $lc = $lc + $linenr; |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6419 | my $stat_real = get_stat_real($linenr, $lc); |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6420 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6421 | my $skip_args = ""; |
| 6422 | if ($arg_pos > 1) { |
| 6423 | $arg_pos--; |
| 6424 | $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; |
| 6425 | } |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6426 | my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; |
| 6427 | if ($stat =~ /$test/) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6428 | my $val = $1; |
| 6429 | $val = $6 if ($skip_args ne ""); |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6430 | if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && |
| 6431 | (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || |
| 6432 | ($val =~ /^$Octal$/ && length($val) ne 4))) { |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6433 | ERROR("NON_OCTAL_PERMISSIONS", |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6434 | "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); |
| 6435 | } |
| 6436 | if ($val =~ /^$Octal$/ && (oct($val) & 02)) { |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6437 | ERROR("EXPORTED_WORLD_WRITABLE", |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6438 | "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6439 | } |
| 6440 | } |
| 6441 | } |
| 6442 | } |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6443 | |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6444 | # check for uses of S_<PERMS> that could be octal for readability |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6445 | while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { |
| 6446 | my $oval = $1; |
| 6447 | my $octal = perms_to_octal($oval); |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6448 | if (WARN("SYMBOLIC_PERMS", |
| 6449 | "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && |
| 6450 | $fix) { |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6451 | $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6452 | } |
| 6453 | } |
| 6454 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6455 | # validate content of MODULE_LICENSE against list from include/linux/module.h |
| 6456 | if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { |
| 6457 | my $extracted_string = get_quoted_string($line, $rawline); |
| 6458 | my $valid_licenses = qr{ |
| 6459 | GPL| |
| 6460 | GPL\ v2| |
| 6461 | GPL\ and\ additional\ rights| |
| 6462 | Dual\ BSD/GPL| |
| 6463 | Dual\ MIT/GPL| |
| 6464 | Dual\ MPL/GPL| |
| 6465 | Proprietary |
| 6466 | }x; |
| 6467 | if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { |
| 6468 | WARN("MODULE_LICENSE", |
| 6469 | "unknown module license " . $extracted_string . "\n" . $herecurr); |
| 6470 | } |
| 6471 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6472 | } |
| 6473 | |
| 6474 | # If we have no input at all, then there is nothing to report on |
| 6475 | # so just keep quiet. |
| 6476 | if ($#rawlines == -1) { |
| 6477 | exit(0); |
| 6478 | } |
| 6479 | |
| 6480 | # In mailback mode only produce a report in the negative, for |
| 6481 | # things that appear to be patches. |
| 6482 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 6483 | exit(0); |
| 6484 | } |
| 6485 | |
| 6486 | # This is not a patch, and we are are in 'no-patch' mode so |
| 6487 | # just keep quiet. |
| 6488 | if (!$chk_patch && !$is_patch) { |
| 6489 | exit(0); |
| 6490 | } |
| 6491 | |
Kuo-Hsin Yang | bb06fc9 | 2018-06-08 15:53:01 +0800 | [diff] [blame] | 6492 | if (!$is_patch && $filename !~ /cover-letter\.patch$/) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6493 | ERROR("NOT_UNIFIED_DIFF", |
| 6494 | "Does not appear to be a unified-diff format patch\n"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6495 | } |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6496 | if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6497 | ERROR("MISSING_SIGN_OFF", |
| 6498 | "Missing Signed-off-by: line(s)\n"); |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | print report_dump(); |
| 6502 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 6503 | print "$filename " if ($summary_file); |
| 6504 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 6505 | (($check)? "$cnt_chk checks, " : "") . |
| 6506 | "$cnt_lines lines checked\n"; |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6507 | } |
| 6508 | |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6509 | if ($quiet == 0) { |
Shawn Nematbakhsh | aa22740 | 2017-07-07 09:14:52 -0700 | [diff] [blame] | 6510 | # If there were any defects found and not already fixing them |
| 6511 | if (!$clean and !$fix) { |
| 6512 | print << "EOM" |
| 6513 | |
| 6514 | NOTE: For some of the reported defects, checkpatch may be able to |
| 6515 | mechanically convert to the typical style using --fix or --fix-inplace. |
| 6516 | EOM |
| 6517 | } |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6518 | # If there were whitespace errors which cleanpatch can fix |
| 6519 | # then suggest that. |
| 6520 | if ($rpt_cleaners) { |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6521 | $rpt_cleaners = 0; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6522 | print << "EOM" |
| 6523 | |
| 6524 | NOTE: Whitespace errors detected. |
| 6525 | You may wish to use scripts/cleanpatch or scripts/cleanfile |
| 6526 | EOM |
Doug Anderson | 26e4e21 | 2011-05-02 09:49:48 -0700 | [diff] [blame] | 6527 | } |
| 6528 | } |
| 6529 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6530 | if ($clean == 0 && $fix && |
| 6531 | ("@rawlines" ne "@fixed" || |
| 6532 | $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { |
| 6533 | my $newfile = $filename; |
| 6534 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); |
| 6535 | my $linecount = 0; |
| 6536 | my $f; |
| 6537 | |
| 6538 | @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); |
| 6539 | |
| 6540 | open($f, '>', $newfile) |
| 6541 | or die "$P: Can't open $newfile for write\n"; |
| 6542 | foreach my $fixed_line (@fixed) { |
| 6543 | $linecount++; |
| 6544 | if ($file) { |
| 6545 | if ($linecount > 3) { |
| 6546 | $fixed_line =~ s/^\+//; |
| 6547 | print $f $fixed_line . "\n"; |
| 6548 | } |
| 6549 | } else { |
| 6550 | print $f $fixed_line . "\n"; |
| 6551 | } |
| 6552 | } |
| 6553 | close($f); |
| 6554 | |
| 6555 | if (!$quiet) { |
| 6556 | print << "EOM"; |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6557 | |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6558 | Wrote EXPERIMENTAL --fix correction(s) to '$newfile' |
| 6559 | |
| 6560 | Do _NOT_ trust the results written to this file. |
| 6561 | Do _NOT_ submit these changes without inspecting them for correctness. |
| 6562 | |
| 6563 | This EXPERIMENTAL file is simply a convenience to help rewrite patches. |
| 6564 | No warranties, expressed or implied... |
Filipe Brandenburger | 5084c0b | 2015-10-06 16:09:05 -0700 | [diff] [blame] | 6565 | EOM |
| 6566 | } |
Doug Anderson | 77086ba | 2013-06-25 17:21:30 -0700 | [diff] [blame] | 6567 | } |
| 6568 | |
Brian Norris | 722b5fb | 2016-06-14 14:22:11 -0700 | [diff] [blame] | 6569 | if ($quiet == 0) { |
| 6570 | print "\n"; |
| 6571 | if ($clean == 1) { |
| 6572 | print "$vname has no obvious style problems and is ready for submission.\n"; |
| 6573 | } else { |
| 6574 | print "$vname has style problems, please review.\n"; |
| 6575 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6576 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6577 | return $clean; |
| 6578 | } |