Kamil Rytarowski | cb77f0d | 2017-05-07 23:25:26 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Joe Perches | 882ea1d | 2018-06-07 17:04:33 -0700 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
Dave Jones | dbf004d | 2010-01-12 16:59:52 -0500 | [diff] [blame] | 4 | # (c) 2001, Dave Jones. (the file handling bit) |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 5 | # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) |
Andy Whitcroft | 2a5a2c2 | 2009-01-06 14:41:23 -0800 | [diff] [blame] | 6 | # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite) |
Andy Whitcroft | 015830be | 2010-10-26 14:23:17 -0700 | [diff] [blame] | 7 | # (c) 2008-2010 Andy Whitcroft <apw@canonical.com> |
Joe Perches | 882ea1d | 2018-06-07 17:04:33 -0700 | [diff] [blame] | 8 | # (c) 2010-2018 Joe Perches <joe@perches.com> |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 9 | |
| 10 | use strict; |
Kamil Rytarowski | cb77f0d | 2017-05-07 23:25:26 +0200 | [diff] [blame] | 11 | use warnings; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 12 | use POSIX; |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 13 | use File::Basename; |
| 14 | use Cwd 'abs_path'; |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 15 | use Term::ANSIColor qw(:constants); |
Geert Uytterhoeven | cd26149 | 2018-08-21 21:57:40 -0700 | [diff] [blame] | 16 | use Encode qw(decode encode); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 17 | |
| 18 | my $P = $0; |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 19 | my $D = dirname(abs_path($P)); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 20 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 21 | my $V = '0.32'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 22 | |
| 23 | use Getopt::Long qw(:config no_auto_abbrev); |
| 24 | |
| 25 | my $quiet = 0; |
| 26 | my $tree = 1; |
| 27 | my $chk_signoff = 1; |
| 28 | my $chk_patch = 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 29 | my $tst_only; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 30 | my $emacs = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 31 | my $terse = 0; |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 32 | my $showfile = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 33 | my $file = 0; |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 34 | my $git = 0; |
Joe Perches | 0dea9f1e | 2016-05-20 17:04:19 -0700 | [diff] [blame] | 35 | my %git_commits = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 36 | my $check = 0; |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 37 | my $check_orig = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 38 | my $summary = 1; |
| 39 | my $mailback = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 40 | my $summary_file = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 41 | my $show_types = 0; |
Joe Perches | 3beb42e | 2016-05-20 17:04:14 -0700 | [diff] [blame] | 42 | my $list_types = 0; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 43 | my $fix = 0; |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 44 | my $fix_inplace = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 45 | my $root; |
Joe Perches | 0f7f635 | 2020-10-24 16:59:04 -0700 | [diff] [blame] | 46 | my $gitroot = $ENV{'GIT_DIR'}; |
| 47 | $gitroot = ".git" if !defined($gitroot); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 48 | my %debug; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 49 | my %camelcase = (); |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 50 | my %use_type = (); |
| 51 | my @use = (); |
| 52 | my %ignore_type = (); |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 53 | my @ignore = (); |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 54 | my $help = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 55 | my $configuration_file = ".checkpatch.conf"; |
Joe Perches | bdc48fa | 2020-05-29 16:12:21 -0700 | [diff] [blame] | 56 | my $max_line_length = 100; |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 57 | my $ignore_perl_version = 0; |
| 58 | my $minimum_perl_version = 5.10.0; |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 59 | my $min_conf_desc_length = 4; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 60 | my $spelling_file = "$D/spelling.txt"; |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 61 | my $codespell = 0; |
Maxim Uvarov | f1a6367 | 2015-06-25 15:03:08 -0700 | [diff] [blame] | 62 | my $codespellfile = "/usr/share/codespell/dictionary.txt"; |
Joe Perches | bf1fa1d | 2016-10-11 13:51:56 -0700 | [diff] [blame] | 63 | my $conststructsfile = "$D/const_structs.checkpatch"; |
Quentin Monnet | ced69da1 | 2020-08-11 18:35:13 -0700 | [diff] [blame] | 64 | my $typedefsfile; |
John Brooks | 737c076 | 2017-07-10 15:52:24 -0700 | [diff] [blame] | 65 | my $color = "auto"; |
Vadim Bendebury | 98005e8 | 2019-03-07 16:28:38 -0800 | [diff] [blame] | 66 | my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE |
Joe Perches | dbbf869 | 2019-09-25 16:46:52 -0700 | [diff] [blame] | 67 | # git output parsing needs US English output, so first set backtick child process LANGUAGE |
| 68 | my $git_command ='export LANGUAGE=en_US.UTF-8; git'; |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 69 | my $tabsize = 8; |
Jerome Forissier | 3e89ad8 | 2020-10-15 20:11:49 -0700 | [diff] [blame] | 70 | my ${CONFIG_} = "CONFIG_"; |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 71 | |
| 72 | sub help { |
| 73 | my ($exitcode) = @_; |
| 74 | |
| 75 | print << "EOM"; |
| 76 | Usage: $P [OPTION]... [FILE]... |
| 77 | Version: $V |
| 78 | |
| 79 | Options: |
| 80 | -q, --quiet quiet |
| 81 | --no-tree run without a kernel tree |
| 82 | --no-signoff do not check for 'Signed-off-by' line |
| 83 | --patch treat FILE as patchfile (default) |
| 84 | --emacs emacs compile window format |
| 85 | --terse one line per report |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 86 | --showfile emit diffed file position, not input file position |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 87 | -g, --git treat FILE as a single commit or git revision range |
| 88 | single git commit with: |
| 89 | <rev> |
| 90 | <rev>^ |
| 91 | <rev>~n |
| 92 | multiple git commits with: |
| 93 | <rev1>..<rev2> |
| 94 | <rev1>...<rev2> |
| 95 | <rev>-<count> |
| 96 | git merges are ignored |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 97 | -f, --file treat FILE as regular source file |
| 98 | --subjective, --strict enable more subjective tests |
Joe Perches | 3beb42e | 2016-05-20 17:04:14 -0700 | [diff] [blame] | 99 | --list-types list the possible message types |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 100 | --types TYPE(,TYPE2...) show only these comma separated message types |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 101 | --ignore TYPE(,TYPE2...) ignore various comma separated message types |
Joe Perches | 3beb42e | 2016-05-20 17:04:14 -0700 | [diff] [blame] | 102 | --show-types show the specific message type in the output |
Joe Perches | bdc48fa | 2020-05-29 16:12:21 -0700 | [diff] [blame] | 103 | --max-line-length=n set the maximum line length, (default $max_line_length) |
| 104 | if exceeded, warn on patches |
| 105 | requires --strict for use with --file |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 106 | --min-conf-desc-length=n set the min description length, if shorter, warn |
Joe Perches | bdc48fa | 2020-05-29 16:12:21 -0700 | [diff] [blame] | 107 | --tab-size=n set the number of spaces for tab (default $tabsize) |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 108 | --root=PATH PATH to the kernel tree root |
| 109 | --no-summary suppress the per-file summary |
| 110 | --mailback only produce a report in case of warnings/errors |
| 111 | --summary-file include the filename in summary |
| 112 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 113 | 'values', 'possible', 'type', and 'attr' (default |
| 114 | is all off) |
| 115 | --test-only=WORD report only warnings/errors containing WORD |
| 116 | literally |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 117 | --fix EXPERIMENTAL - may create horrible results |
| 118 | If correctable single-line errors exist, create |
| 119 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" |
| 120 | with potential errors corrected to the preferred |
| 121 | checkpatch style |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 122 | --fix-inplace EXPERIMENTAL - may create horrible results |
| 123 | Is the same as --fix, but overwrites the input |
| 124 | file. It's your fault if there's no backup or git |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 125 | --ignore-perl-version override checking of perl version. expect |
| 126 | runtime errors. |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 127 | --codespell Use the codespell dictionary for spelling/typos |
Maxim Uvarov | f1a6367 | 2015-06-25 15:03:08 -0700 | [diff] [blame] | 128 | (default:/usr/share/codespell/dictionary.txt) |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 129 | --codespellfile Use this codespell dictionary |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 130 | --typedefsfile Read additional types from this file |
John Brooks | 737c076 | 2017-07-10 15:52:24 -0700 | [diff] [blame] | 131 | --color[=WHEN] Use colors 'always', 'never', or only when output |
| 132 | is a terminal ('auto'). Default is 'auto'. |
Jerome Forissier | 3e89ad8 | 2020-10-15 20:11:49 -0700 | [diff] [blame] | 133 | --kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default |
| 134 | ${CONFIG_}) |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 135 | -h, --help, --version display this help and exit |
| 136 | |
| 137 | When FILE is - read standard input. |
| 138 | EOM |
| 139 | |
| 140 | exit($exitcode); |
| 141 | } |
| 142 | |
Joe Perches | 3beb42e | 2016-05-20 17:04:14 -0700 | [diff] [blame] | 143 | sub uniq { |
| 144 | my %seen; |
| 145 | return grep { !$seen{$_}++ } @_; |
| 146 | } |
| 147 | |
| 148 | sub list_types { |
| 149 | my ($exitcode) = @_; |
| 150 | |
| 151 | my $count = 0; |
| 152 | |
| 153 | local $/ = undef; |
| 154 | |
| 155 | open(my $script, '<', abs_path($P)) or |
| 156 | die "$P: Can't read '$P' $!\n"; |
| 157 | |
| 158 | my $text = <$script>; |
| 159 | close($script); |
| 160 | |
| 161 | my @types = (); |
Jean Delvare | 0547fa5 | 2017-09-08 16:16:11 -0700 | [diff] [blame] | 162 | # Also catch when type or level is passed through a variable |
| 163 | for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) { |
Joe Perches | 3beb42e | 2016-05-20 17:04:14 -0700 | [diff] [blame] | 164 | push (@types, $_); |
| 165 | } |
| 166 | @types = sort(uniq(@types)); |
| 167 | print("#\tMessage type\n\n"); |
| 168 | foreach my $type (@types) { |
| 169 | print(++$count . "\t" . $type . "\n"); |
| 170 | } |
| 171 | |
| 172 | exit($exitcode); |
| 173 | } |
| 174 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 175 | my $conf = which_conf($configuration_file); |
| 176 | if (-f $conf) { |
| 177 | my @conf_args; |
| 178 | open(my $conffile, '<', "$conf") |
| 179 | or warn "$P: Can't find a readable $configuration_file file $!\n"; |
| 180 | |
| 181 | while (<$conffile>) { |
| 182 | my $line = $_; |
| 183 | |
| 184 | $line =~ s/\s*\n?$//g; |
| 185 | $line =~ s/^\s*//g; |
| 186 | $line =~ s/\s+/ /g; |
| 187 | |
| 188 | next if ($line =~ m/^\s*#/); |
| 189 | next if ($line =~ m/^\s*$/); |
| 190 | |
| 191 | my @words = split(" ", $line); |
| 192 | foreach my $word (@words) { |
| 193 | last if ($word =~ m/^#/); |
| 194 | push (@conf_args, $word); |
| 195 | } |
| 196 | } |
| 197 | close($conffile); |
| 198 | unshift(@ARGV, @conf_args) if @conf_args; |
| 199 | } |
| 200 | |
John Brooks | 737c076 | 2017-07-10 15:52:24 -0700 | [diff] [blame] | 201 | # Perl's Getopt::Long allows options to take optional arguments after a space. |
| 202 | # Prevent --color by itself from consuming other arguments |
| 203 | foreach (@ARGV) { |
| 204 | if ($_ eq "--color" || $_ eq "-color") { |
| 205 | $_ = "--color=$color"; |
| 206 | } |
| 207 | } |
| 208 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 209 | GetOptions( |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 210 | 'q|quiet+' => \$quiet, |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 211 | 'tree!' => \$tree, |
| 212 | 'signoff!' => \$chk_signoff, |
| 213 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 214 | 'emacs!' => \$emacs, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 215 | 'terse!' => \$terse, |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 216 | 'showfile!' => \$showfile, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 217 | 'f|file!' => \$file, |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 218 | 'g|git!' => \$git, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 219 | 'subjective!' => \$check, |
| 220 | 'strict!' => \$check, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 221 | 'ignore=s' => \@ignore, |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 222 | 'types=s' => \@use, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 223 | 'show-types!' => \$show_types, |
Joe Perches | 3beb42e | 2016-05-20 17:04:14 -0700 | [diff] [blame] | 224 | 'list-types!' => \$list_types, |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 225 | 'max-line-length=i' => \$max_line_length, |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 226 | 'min-conf-desc-length=i' => \$min_conf_desc_length, |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 227 | 'tab-size=i' => \$tabsize, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 228 | 'root=s' => \$root, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 229 | 'summary!' => \$summary, |
| 230 | 'mailback!' => \$mailback, |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 231 | 'summary-file!' => \$summary_file, |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 232 | 'fix!' => \$fix, |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 233 | 'fix-inplace!' => \$fix_inplace, |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 234 | 'ignore-perl-version!' => \$ignore_perl_version, |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 235 | 'debug=s' => \%debug, |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 236 | 'test-only=s' => \$tst_only, |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 237 | 'codespell!' => \$codespell, |
| 238 | 'codespellfile=s' => \$codespellfile, |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 239 | 'typedefsfile=s' => \$typedefsfile, |
John Brooks | 737c076 | 2017-07-10 15:52:24 -0700 | [diff] [blame] | 240 | 'color=s' => \$color, |
| 241 | 'no-color' => \$color, #keep old behaviors of -nocolor |
| 242 | 'nocolor' => \$color, #keep old behaviors of -nocolor |
Jerome Forissier | 3e89ad8 | 2020-10-15 20:11:49 -0700 | [diff] [blame] | 243 | 'kconfig-prefix=s' => \${CONFIG_}, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 244 | 'h|help' => \$help, |
| 245 | 'version' => \$help |
| 246 | ) or help(1); |
| 247 | |
| 248 | help(0) if ($help); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 249 | |
Joe Perches | 3beb42e | 2016-05-20 17:04:14 -0700 | [diff] [blame] | 250 | list_types(0) if ($list_types); |
| 251 | |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 252 | $fix = 1 if ($fix_inplace); |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 253 | $check_orig = $check; |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 254 | |
Joe Perches | 32f30ca | 2020-06-04 16:50:40 -0700 | [diff] [blame] | 255 | die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix)); |
| 256 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 257 | my $exit = 0; |
| 258 | |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 259 | my $perl_version_ok = 1; |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 260 | if ($^V && $^V lt $minimum_perl_version) { |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 261 | $perl_version_ok = 0; |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 262 | printf "$P: requires at least perl version %vd\n", $minimum_perl_version; |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 263 | exit(1) if (!$ignore_perl_version); |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Allen Hubbe | 45107ff | 2016-08-02 14:04:47 -0700 | [diff] [blame] | 266 | #if no filenames are given, push '-' to read patch from stdin |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 267 | if ($#ARGV < 0) { |
Allen Hubbe | 45107ff | 2016-08-02 14:04:47 -0700 | [diff] [blame] | 268 | push(@ARGV, '-'); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 269 | } |
| 270 | |
John Brooks | 737c076 | 2017-07-10 15:52:24 -0700 | [diff] [blame] | 271 | if ($color =~ /^[01]$/) { |
| 272 | $color = !$color; |
| 273 | } elsif ($color =~ /^always$/i) { |
| 274 | $color = 1; |
| 275 | } elsif ($color =~ /^never$/i) { |
| 276 | $color = 0; |
| 277 | } elsif ($color =~ /^auto$/i) { |
| 278 | $color = (-t STDOUT); |
| 279 | } else { |
Joe Perches | 32f30ca | 2020-06-04 16:50:40 -0700 | [diff] [blame] | 280 | die "$P: Invalid color mode: $color\n"; |
John Brooks | 737c076 | 2017-07-10 15:52:24 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 283 | # skip TAB size 1 to avoid additional checks on $tabsize - 1 |
Joe Perches | 32f30ca | 2020-06-04 16:50:40 -0700 | [diff] [blame] | 284 | die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2); |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 285 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 286 | sub hash_save_array_words { |
| 287 | my ($hashRef, $arrayRef) = @_; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 288 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 289 | my @array = split(/,/, join(',', @$arrayRef)); |
| 290 | foreach my $word (@array) { |
| 291 | $word =~ s/\s*\n?$//g; |
| 292 | $word =~ s/^\s*//g; |
| 293 | $word =~ s/\s+/ /g; |
| 294 | $word =~ tr/[a-z]/[A-Z]/; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 295 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 296 | next if ($word =~ m/^\s*#/); |
| 297 | next if ($word =~ m/^\s*$/); |
| 298 | |
| 299 | $hashRef->{$word}++; |
| 300 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 303 | sub hash_show_words { |
| 304 | my ($hashRef, $prefix) = @_; |
| 305 | |
Joe Perches | 3c816e4 | 2015-06-25 15:03:29 -0700 | [diff] [blame] | 306 | if (keys %$hashRef) { |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 307 | print "\nNOTE: $prefix message types:"; |
Joe Perches | 58cb3cf | 2013-09-11 14:24:04 -0700 | [diff] [blame] | 308 | foreach my $word (sort keys %$hashRef) { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 309 | print " $word"; |
| 310 | } |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 311 | print "\n"; |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | hash_save_array_words(\%ignore_type, \@ignore); |
| 316 | hash_save_array_words(\%use_type, \@use); |
| 317 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 318 | my $dbg_values = 0; |
| 319 | my $dbg_possible = 0; |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 320 | my $dbg_type = 0; |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 321 | my $dbg_attr = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 322 | for my $key (keys %debug) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 323 | ## no critic |
| 324 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 325 | die "$@" if ($@); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 328 | my $rpt_cleaners = 0; |
| 329 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 330 | if ($terse) { |
| 331 | $emacs = 1; |
| 332 | $quiet++; |
| 333 | } |
| 334 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 335 | if ($tree) { |
| 336 | if (defined $root) { |
| 337 | if (!top_of_kernel_tree($root)) { |
| 338 | die "$P: $root: --root does not point at a valid tree\n"; |
| 339 | } |
| 340 | } else { |
| 341 | if (top_of_kernel_tree('.')) { |
| 342 | $root = '.'; |
| 343 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 344 | top_of_kernel_tree($1)) { |
| 345 | $root = $1; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if (!defined $root) { |
| 350 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 351 | exit(2); |
| 352 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 355 | my $emitted_corrupt = 0; |
| 356 | |
Andy Whitcroft | 2ceb532 | 2009-10-26 16:50:14 -0700 | [diff] [blame] | 357 | our $Ident = qr{ |
| 358 | [A-Za-z_][A-Za-z\d_]* |
| 359 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 360 | }x; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 361 | our $Storage = qr{extern|static|asmlinkage}; |
| 362 | our $Sparse = qr{ |
| 363 | __user| |
| 364 | __kernel| |
| 365 | __force| |
| 366 | __iomem| |
| 367 | __must_check| |
Andy Whitcroft | 417495e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 368 | __kprobes| |
Sven Eckelmann | 165e72a | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 369 | __ref| |
Geert Uytterhoeven | 33aa459 | 2018-08-21 21:57:36 -0700 | [diff] [blame] | 370 | __refconst| |
| 371 | __refdata| |
Boqun Feng | ad31545 | 2015-12-29 12:18:46 +0800 | [diff] [blame] | 372 | __rcu| |
| 373 | __private |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 374 | }x; |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 375 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; |
| 376 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; |
| 377 | our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; |
| 378 | our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; |
| 379 | our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 380 | |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 381 | # Notes to $Attribute: |
| 382 | # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 383 | our $Attribute = qr{ |
| 384 | const| |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 385 | volatile| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 386 | __percpu| |
| 387 | __nocast| |
| 388 | __safe| |
Michael S. Tsirkin | 46d832f | 2016-12-11 06:29:58 +0200 | [diff] [blame] | 389 | __bitwise| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 390 | __packed__| |
| 391 | __packed2__| |
| 392 | __naked| |
| 393 | __maybe_unused| |
| 394 | __always_unused| |
| 395 | __noreturn| |
| 396 | __used| |
| 397 | __cold| |
Joe Perches | e23ef1f | 2015-02-13 14:38:24 -0800 | [diff] [blame] | 398 | __pure| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 399 | __noclone| |
| 400 | __deprecated| |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 401 | __read_mostly| |
Joe Perches | c5967e9 | 2018-09-04 15:46:20 -0700 | [diff] [blame] | 402 | __ro_after_init| |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 403 | __kprobes| |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 404 | $InitAttribute| |
Andy Whitcroft | 24e1d81 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 405 | ____cacheline_aligned| |
| 406 | ____cacheline_aligned_in_smp| |
Andy Whitcroft | 5fe3af1 | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 407 | ____cacheline_internodealigned_in_smp| |
| 408 | __weak |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 409 | }x; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 410 | our $Modifier; |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 411 | our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 412 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 413 | our $Lval = qr{$Ident(?:$Member)*}; |
| 414 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 415 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 416 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 417 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 418 | our $Int = qr{[0-9]+$Int_type?}; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 419 | our $Octal = qr{0[0-7]+$Int_type?}; |
Joe Perches | c0a5c89 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 420 | our $String = qr{"[X\t]*"}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 421 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 422 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 423 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
Joe Perches | 74349bc | 2012-12-17 16:02:05 -0800 | [diff] [blame] | 424 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 425 | our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 426 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Joe Perches | 447432f | 2014-04-03 14:49:17 -0700 | [diff] [blame] | 427 | our $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 428 | our $Arithmetic = qr{\+|-|\*|\/|%}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 429 | our $Operators = qr{ |
| 430 | <=|>=|==|!=| |
| 431 | =>|->|<<|>>|<|>|!|~| |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 432 | &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 433 | }x; |
| 434 | |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 435 | our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; |
| 436 | |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 437 | our $BasicType; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 438 | our $NonptrType; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 439 | our $NonptrTypeMisordered; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 440 | our $NonptrTypeWithAttr; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 441 | our $Type; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 442 | our $TypeMisordered; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 443 | our $Declare; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 444 | our $DeclareMisordered; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 445 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 446 | our $NON_ASCII_UTF8 = qr{ |
| 447 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 448 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 449 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 450 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 451 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 452 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 453 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 454 | }x; |
| 455 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 456 | our $UTF8 = qr{ |
| 457 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 458 | | $NON_ASCII_UTF8 |
| 459 | }x; |
| 460 | |
Joe Perches | e6176fa | 2015-06-25 15:02:49 -0700 | [diff] [blame] | 461 | our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; |
Joe Perches | 021158b | 2015-02-13 14:38:43 -0800 | [diff] [blame] | 462 | our $typeOtherOSTypedefs = qr{(?x: |
| 463 | u_(?:char|short|int|long) | # bsd |
| 464 | u(?:nchar|short|int|long) # sysv |
| 465 | )}; |
Joe Perches | e6176fa | 2015-06-25 15:02:49 -0700 | [diff] [blame] | 466 | our $typeKernelTypedefs = qr{(?x: |
Andy Whitcroft | fb9e909 | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 467 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 468 | atomic_t |
| 469 | )}; |
Joe Perches | e6176fa | 2015-06-25 15:02:49 -0700 | [diff] [blame] | 470 | our $typeTypedefs = qr{(?x: |
| 471 | $typeC99Typedefs\b| |
| 472 | $typeOtherOSTypedefs\b| |
| 473 | $typeKernelTypedefs\b |
| 474 | )}; |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 475 | |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 476 | our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; |
| 477 | |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 478 | our $logFunctions = qr{(?x: |
Miles Chen | 758d7aa | 2017-02-24 15:01:34 -0800 | [diff] [blame] | 479 | printk(?:_ratelimited|_once|_deferred_once|_deferred|)| |
Jacob Keller | 7d0b659 | 2013-07-03 15:05:35 -0700 | [diff] [blame] | 480 | (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| |
Joe Perches | 87bd499 | 2017-11-17 15:28:48 -0800 | [diff] [blame] | 481 | TP_printk| |
Joe Perches | 6e60c02 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 482 | WARN(?:_RATELIMIT|_ONCE|)| |
Joe Perches | b053172 | 2011-05-24 17:13:40 -0700 | [diff] [blame] | 483 | panic| |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 484 | MODULE_[A-Z_]+| |
| 485 | seq_vprintf|seq_printf|seq_puts |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 486 | )}; |
| 487 | |
Joe Perches | e29a70f | 2019-03-07 16:28:35 -0800 | [diff] [blame] | 488 | our $allocFunctions = qr{(?x: |
| 489 | (?:(?:devm_)? |
Joe Perches | 58f0226 | 2021-02-25 17:22:01 -0800 | [diff] [blame] | 490 | (?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? | |
Joe Perches | e29a70f | 2019-03-07 16:28:35 -0800 | [diff] [blame] | 491 | kstrdup(?:_const)? | |
| 492 | kmemdup(?:_nul)?) | |
Christophe JAILLET | 461e156 | 2020-04-20 18:13:58 -0700 | [diff] [blame] | 493 | (?:\w+)?alloc_skb(?:_ip_align)? | |
Joe Perches | e29a70f | 2019-03-07 16:28:35 -0800 | [diff] [blame] | 494 | # dev_alloc_skb/netdev_alloc_skb, et al |
| 495 | dma_alloc_coherent |
| 496 | )}; |
| 497 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 498 | our $signature_tags = qr{(?xi: |
| 499 | Signed-off-by:| |
Jorge Ramirez-Ortiz | d499480 | 2019-01-03 15:29:12 -0800 | [diff] [blame] | 500 | Co-developed-by:| |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 501 | Acked-by:| |
| 502 | Tested-by:| |
| 503 | Reviewed-by:| |
| 504 | Reported-by:| |
Mugunthan V N | 8543ae1 | 2013-04-29 16:18:17 -0700 | [diff] [blame] | 505 | Suggested-by:| |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 506 | To:| |
| 507 | Cc: |
| 508 | )}; |
| 509 | |
Joe Perches | adb2da8 | 2021-02-25 17:21:50 -0800 | [diff] [blame] | 510 | our $tracing_logging_tags = qr{(?xi: |
| 511 | [=-]*> | |
| 512 | <[=-]* | |
| 513 | \[ | |
| 514 | \] | |
| 515 | start | |
| 516 | called | |
| 517 | entered | |
| 518 | entry | |
| 519 | enter | |
| 520 | in | |
| 521 | inside | |
| 522 | here | |
| 523 | begin | |
| 524 | exit | |
| 525 | end | |
| 526 | done | |
| 527 | leave | |
| 528 | completed | |
| 529 | out | |
| 530 | return | |
| 531 | [\.\!:\s]* |
| 532 | )}; |
| 533 | |
Aditya Srivastava | 831242a | 2020-12-15 20:45:12 -0800 | [diff] [blame] | 534 | sub edit_distance_min { |
| 535 | my (@arr) = @_; |
| 536 | my $len = scalar @arr; |
| 537 | if ((scalar @arr) < 1) { |
| 538 | # if underflow, return |
| 539 | return; |
| 540 | } |
| 541 | my $min = $arr[0]; |
| 542 | for my $i (0 .. ($len-1)) { |
| 543 | if ($arr[$i] < $min) { |
| 544 | $min = $arr[$i]; |
| 545 | } |
| 546 | } |
| 547 | return $min; |
| 548 | } |
| 549 | |
| 550 | sub get_edit_distance { |
| 551 | my ($str1, $str2) = @_; |
| 552 | $str1 = lc($str1); |
| 553 | $str2 = lc($str2); |
| 554 | $str1 =~ s/-//g; |
| 555 | $str2 =~ s/-//g; |
| 556 | my $len1 = length($str1); |
| 557 | my $len2 = length($str2); |
| 558 | # two dimensional array storing minimum edit distance |
| 559 | my @distance; |
| 560 | for my $i (0 .. $len1) { |
| 561 | for my $j (0 .. $len2) { |
| 562 | if ($i == 0) { |
| 563 | $distance[$i][$j] = $j; |
| 564 | } elsif ($j == 0) { |
| 565 | $distance[$i][$j] = $i; |
| 566 | } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) { |
| 567 | $distance[$i][$j] = $distance[$i - 1][$j - 1]; |
| 568 | } else { |
| 569 | my $dist1 = $distance[$i][$j - 1]; #insert distance |
| 570 | my $dist2 = $distance[$i - 1][$j]; # remove |
| 571 | my $dist3 = $distance[$i - 1][$j - 1]; #replace |
| 572 | $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | return $distance[$len1][$len2]; |
| 577 | } |
| 578 | |
| 579 | sub find_standard_signature { |
| 580 | my ($sign_off) = @_; |
| 581 | my @standard_signature_tags = ( |
| 582 | 'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:', |
| 583 | 'Reviewed-by:', 'Reported-by:', 'Suggested-by:' |
| 584 | ); |
| 585 | foreach my $signature (@standard_signature_tags) { |
| 586 | return $signature if (get_edit_distance($sign_off, $signature) <= 2); |
| 587 | } |
| 588 | |
| 589 | return ""; |
| 590 | } |
| 591 | |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 592 | our @typeListMisordered = ( |
| 593 | qr{char\s+(?:un)?signed}, |
| 594 | qr{int\s+(?:(?:un)?signed\s+)?short\s}, |
| 595 | qr{int\s+short(?:\s+(?:un)?signed)}, |
| 596 | qr{short\s+int(?:\s+(?:un)?signed)}, |
| 597 | qr{(?:un)?signed\s+int\s+short}, |
| 598 | qr{short\s+(?:un)?signed}, |
| 599 | qr{long\s+int\s+(?:un)?signed}, |
| 600 | qr{int\s+long\s+(?:un)?signed}, |
| 601 | qr{long\s+(?:un)?signed\s+int}, |
| 602 | qr{int\s+(?:un)?signed\s+long}, |
| 603 | qr{int\s+(?:un)?signed}, |
| 604 | qr{int\s+long\s+long\s+(?:un)?signed}, |
| 605 | qr{long\s+long\s+int\s+(?:un)?signed}, |
| 606 | qr{long\s+long\s+(?:un)?signed\s+int}, |
| 607 | qr{long\s+long\s+(?:un)?signed}, |
| 608 | qr{long\s+(?:un)?signed}, |
| 609 | ); |
| 610 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 611 | our @typeList = ( |
| 612 | qr{void}, |
Joe Perches | 0c773d9 | 2014-08-06 16:11:20 -0700 | [diff] [blame] | 613 | qr{(?:(?:un)?signed\s+)?char}, |
| 614 | qr{(?:(?:un)?signed\s+)?short\s+int}, |
| 615 | qr{(?:(?:un)?signed\s+)?short}, |
| 616 | qr{(?:(?:un)?signed\s+)?int}, |
| 617 | qr{(?:(?:un)?signed\s+)?long\s+int}, |
| 618 | qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, |
| 619 | qr{(?:(?:un)?signed\s+)?long\s+long}, |
| 620 | qr{(?:(?:un)?signed\s+)?long}, |
| 621 | qr{(?:un)?signed}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 622 | qr{float}, |
| 623 | qr{double}, |
| 624 | qr{bool}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 625 | qr{struct\s+$Ident}, |
| 626 | qr{union\s+$Ident}, |
| 627 | qr{enum\s+$Ident}, |
| 628 | qr{${Ident}_t}, |
| 629 | qr{${Ident}_handler}, |
| 630 | qr{${Ident}_handler_fn}, |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 631 | @typeListMisordered, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 632 | ); |
Joe Perches | 938224b | 2016-01-20 14:59:15 -0800 | [diff] [blame] | 633 | |
| 634 | our $C90_int_types = qr{(?x: |
| 635 | long\s+long\s+int\s+(?:un)?signed| |
| 636 | long\s+long\s+(?:un)?signed\s+int| |
| 637 | long\s+long\s+(?:un)?signed| |
| 638 | (?:(?:un)?signed\s+)?long\s+long\s+int| |
| 639 | (?:(?:un)?signed\s+)?long\s+long| |
| 640 | int\s+long\s+long\s+(?:un)?signed| |
| 641 | int\s+(?:(?:un)?signed\s+)?long\s+long| |
| 642 | |
| 643 | long\s+int\s+(?:un)?signed| |
| 644 | long\s+(?:un)?signed\s+int| |
| 645 | long\s+(?:un)?signed| |
| 646 | (?:(?:un)?signed\s+)?long\s+int| |
| 647 | (?:(?:un)?signed\s+)?long| |
| 648 | int\s+long\s+(?:un)?signed| |
| 649 | int\s+(?:(?:un)?signed\s+)?long| |
| 650 | |
| 651 | int\s+(?:un)?signed| |
| 652 | (?:(?:un)?signed\s+)?int |
| 653 | )}; |
| 654 | |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 655 | our @typeListFile = (); |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 656 | our @typeListWithAttr = ( |
| 657 | @typeList, |
| 658 | qr{struct\s+$InitAttribute\s+$Ident}, |
| 659 | qr{union\s+$InitAttribute\s+$Ident}, |
| 660 | ); |
| 661 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 662 | our @modifierList = ( |
| 663 | qr{fastcall}, |
| 664 | ); |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 665 | our @modifierListFile = (); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 666 | |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 667 | our @mode_permission_funcs = ( |
| 668 | ["module_param", 3], |
| 669 | ["module_param_(?:array|named|string)", 4], |
| 670 | ["module_param_array_named", 5], |
| 671 | ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], |
| 672 | ["proc_create(?:_data|)", 2], |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 673 | ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2], |
| 674 | ["IIO_DEV_ATTR_[A-Z_]+", 1], |
| 675 | ["SENSOR_(?:DEVICE_|)ATTR_2", 2], |
| 676 | ["SENSOR_TEMPLATE(?:_2|)", 3], |
| 677 | ["__ATTR", 2], |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 678 | ); |
| 679 | |
Joe Perches | 1a3dcf2 | 2020-08-11 18:35:16 -0700 | [diff] [blame] | 680 | my $word_pattern = '\b[A-Z]?[a-z]{2,}\b'; |
| 681 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 682 | #Create a search pattern for all these functions to speed up a loop below |
| 683 | our $mode_perms_search = ""; |
| 684 | foreach my $entry (@mode_permission_funcs) { |
| 685 | $mode_perms_search .= '|' if ($mode_perms_search ne ""); |
| 686 | $mode_perms_search .= $entry->[0]; |
| 687 | } |
Joe Perches | 0018046 | 2018-02-06 15:38:55 -0800 | [diff] [blame] | 688 | $mode_perms_search = "(?:${mode_perms_search})"; |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 689 | |
Joe Perches | 9189c7e | 2018-09-07 15:26:18 -0700 | [diff] [blame] | 690 | our %deprecated_apis = ( |
| 691 | "synchronize_rcu_bh" => "synchronize_rcu", |
| 692 | "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited", |
| 693 | "call_rcu_bh" => "call_rcu", |
| 694 | "rcu_barrier_bh" => "rcu_barrier", |
| 695 | "synchronize_sched" => "synchronize_rcu", |
| 696 | "synchronize_sched_expedited" => "synchronize_rcu_expedited", |
| 697 | "call_rcu_sched" => "call_rcu", |
| 698 | "rcu_barrier_sched" => "rcu_barrier", |
| 699 | "get_state_synchronize_sched" => "get_state_synchronize_rcu", |
| 700 | "cond_synchronize_sched" => "cond_synchronize_rcu", |
| 701 | ); |
| 702 | |
| 703 | #Create a search pattern for all these strings to speed up a loop below |
| 704 | our $deprecated_apis_search = ""; |
| 705 | foreach my $entry (keys %deprecated_apis) { |
| 706 | $deprecated_apis_search .= '|' if ($deprecated_apis_search ne ""); |
| 707 | $deprecated_apis_search .= $entry; |
| 708 | } |
| 709 | $deprecated_apis_search = "(?:${deprecated_apis_search})"; |
| 710 | |
Joe Perches | b392c64 | 2015-04-16 12:44:16 -0700 | [diff] [blame] | 711 | our $mode_perms_world_writable = qr{ |
| 712 | S_IWUGO | |
| 713 | S_IWOTH | |
| 714 | S_IRWXUGO | |
| 715 | S_IALLUGO | |
| 716 | 0[0-7][0-7][2367] |
| 717 | }x; |
| 718 | |
Joe Perches | f90774e | 2016-10-11 13:51:47 -0700 | [diff] [blame] | 719 | our %mode_permission_string_types = ( |
| 720 | "S_IRWXU" => 0700, |
| 721 | "S_IRUSR" => 0400, |
| 722 | "S_IWUSR" => 0200, |
| 723 | "S_IXUSR" => 0100, |
| 724 | "S_IRWXG" => 0070, |
| 725 | "S_IRGRP" => 0040, |
| 726 | "S_IWGRP" => 0020, |
| 727 | "S_IXGRP" => 0010, |
| 728 | "S_IRWXO" => 0007, |
| 729 | "S_IROTH" => 0004, |
| 730 | "S_IWOTH" => 0002, |
| 731 | "S_IXOTH" => 0001, |
| 732 | "S_IRWXUGO" => 0777, |
| 733 | "S_IRUGO" => 0444, |
| 734 | "S_IWUGO" => 0222, |
| 735 | "S_IXUGO" => 0111, |
| 736 | ); |
| 737 | |
| 738 | #Create a search pattern for all these strings to speed up a loop below |
| 739 | our $mode_perms_string_search = ""; |
| 740 | foreach my $entry (keys %mode_permission_string_types) { |
| 741 | $mode_perms_string_search .= '|' if ($mode_perms_string_search ne ""); |
| 742 | $mode_perms_string_search .= $entry; |
| 743 | } |
Joe Perches | 0018046 | 2018-02-06 15:38:55 -0800 | [diff] [blame] | 744 | our $single_mode_perms_string_search = "(?:${mode_perms_string_search})"; |
| 745 | our $multi_mode_perms_string_search = qr{ |
| 746 | ${single_mode_perms_string_search} |
| 747 | (?:\s*\|\s*${single_mode_perms_string_search})* |
| 748 | }x; |
| 749 | |
| 750 | sub perms_to_octal { |
| 751 | my ($string) = @_; |
| 752 | |
| 753 | return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/); |
| 754 | |
| 755 | my $val = ""; |
| 756 | my $oval = ""; |
| 757 | my $to = 0; |
| 758 | my $curpos = 0; |
| 759 | my $lastpos = 0; |
| 760 | while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) { |
| 761 | $curpos = pos($string); |
| 762 | my $match = $2; |
| 763 | my $omatch = $1; |
| 764 | last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos)); |
| 765 | $lastpos = $curpos; |
| 766 | $to |= $mode_permission_string_types{$match}; |
| 767 | $val .= '\s*\|\s*' if ($val ne ""); |
| 768 | $val .= $match; |
| 769 | $oval .= $omatch; |
| 770 | } |
| 771 | $oval =~ s/^\s*\|\s*//; |
| 772 | $oval =~ s/\s*\|\s*$//; |
| 773 | return sprintf("%04o", $to); |
| 774 | } |
Joe Perches | f90774e | 2016-10-11 13:51:47 -0700 | [diff] [blame] | 775 | |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 776 | our $allowed_asm_includes = qr{(?x: |
| 777 | irq| |
Sergey Ryazanov | cdcee68 | 2014-10-13 15:51:44 -0700 | [diff] [blame] | 778 | memory| |
| 779 | time| |
| 780 | reboot |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 781 | )}; |
| 782 | # memory.h: ARM has a custom one |
| 783 | |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 784 | # Load common spelling mistakes and build regular expression list. |
| 785 | my $misspellings; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 786 | my %spelling_fix; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 787 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 788 | if (open(my $spelling, '<', $spelling_file)) { |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 789 | while (<$spelling>) { |
| 790 | my $line = $_; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 791 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 792 | $line =~ s/\s*\n?$//g; |
| 793 | $line =~ s/^\s*//g; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 794 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 795 | next if ($line =~ m/^\s*#/); |
| 796 | next if ($line =~ m/^\s*$/); |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 797 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 798 | my ($suspect, $fix) = split(/\|\|/, $line); |
| 799 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 800 | $spelling_fix{$suspect} = $fix; |
| 801 | } |
| 802 | close($spelling); |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 803 | } else { |
| 804 | warn "No typos will be found - file '$spelling_file': $!\n"; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 805 | } |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 806 | |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 807 | if ($codespell) { |
| 808 | if (open(my $spelling, '<', $codespellfile)) { |
| 809 | while (<$spelling>) { |
| 810 | my $line = $_; |
| 811 | |
| 812 | $line =~ s/\s*\n?$//g; |
| 813 | $line =~ s/^\s*//g; |
| 814 | |
| 815 | next if ($line =~ m/^\s*#/); |
| 816 | next if ($line =~ m/^\s*$/); |
| 817 | next if ($line =~ m/, disabled/i); |
| 818 | |
| 819 | $line =~ s/,.*$//; |
| 820 | |
| 821 | my ($suspect, $fix) = split(/->/, $line); |
| 822 | |
| 823 | $spelling_fix{$suspect} = $fix; |
| 824 | } |
| 825 | close($spelling); |
| 826 | } else { |
| 827 | warn "No codespell typos will be found - file '$codespellfile': $!\n"; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; |
| 832 | |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 833 | sub read_words { |
| 834 | my ($wordsRef, $file) = @_; |
Joe Perches | bf1fa1d | 2016-10-11 13:51:56 -0700 | [diff] [blame] | 835 | |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 836 | if (open(my $words, '<', $file)) { |
| 837 | while (<$words>) { |
| 838 | my $line = $_; |
Joe Perches | bf1fa1d | 2016-10-11 13:51:56 -0700 | [diff] [blame] | 839 | |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 840 | $line =~ s/\s*\n?$//g; |
| 841 | $line =~ s/^\s*//g; |
| 842 | |
| 843 | next if ($line =~ m/^\s*#/); |
| 844 | next if ($line =~ m/^\s*$/); |
| 845 | if ($line =~ /\s/) { |
| 846 | print("$file: '$line' invalid - ignored\n"); |
| 847 | next; |
| 848 | } |
| 849 | |
Quentin Monnet | ced69da1 | 2020-08-11 18:35:13 -0700 | [diff] [blame] | 850 | $$wordsRef .= '|' if (defined $$wordsRef); |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 851 | $$wordsRef .= $line; |
Joe Perches | bf1fa1d | 2016-10-11 13:51:56 -0700 | [diff] [blame] | 852 | } |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 853 | close($file); |
| 854 | return 1; |
Joe Perches | bf1fa1d | 2016-10-11 13:51:56 -0700 | [diff] [blame] | 855 | } |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 856 | |
| 857 | return 0; |
Joe Perches | bf1fa1d | 2016-10-11 13:51:56 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Quentin Monnet | ced69da1 | 2020-08-11 18:35:13 -0700 | [diff] [blame] | 860 | my $const_structs; |
| 861 | if (show_type("CONST_STRUCT")) { |
| 862 | read_words(\$const_structs, $conststructsfile) |
| 863 | or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; |
| 864 | } |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 865 | |
Quentin Monnet | ced69da1 | 2020-08-11 18:35:13 -0700 | [diff] [blame] | 866 | if (defined($typedefsfile)) { |
| 867 | my $typeOtherTypedefs; |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 868 | read_words(\$typeOtherTypedefs, $typedefsfile) |
| 869 | or warn "No additional types will be considered - file '$typedefsfile': $!\n"; |
Quentin Monnet | ced69da1 | 2020-08-11 18:35:13 -0700 | [diff] [blame] | 870 | $typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs); |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 871 | } |
Jerome Forissier | 75ad8c5 | 2017-05-08 15:56:00 -0700 | [diff] [blame] | 872 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 873 | sub build_types { |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 874 | my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; |
| 875 | my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 876 | my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 877 | my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 878 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 879 | $BasicType = qr{ |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 880 | (?:$typeTypedefs\b)| |
| 881 | (?:${all}\b) |
| 882 | }x; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 883 | $NonptrType = qr{ |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 884 | (?:$Modifier\s+|const\s+)* |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 885 | (?: |
Andy Whitcroft | 6b48db2 | 2012-01-10 15:10:13 -0800 | [diff] [blame] | 886 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 887 | (?:$typeTypedefs\b)| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 888 | (?:${all}\b) |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 889 | ) |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 890 | (?:\s+$Modifier|\s+const)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 891 | }x; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 892 | $NonptrTypeMisordered = qr{ |
| 893 | (?:$Modifier\s+|const\s+)* |
| 894 | (?: |
| 895 | (?:${Misordered}\b) |
| 896 | ) |
| 897 | (?:\s+$Modifier|\s+const)* |
| 898 | }x; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 899 | $NonptrTypeWithAttr = qr{ |
| 900 | (?:$Modifier\s+|const\s+)* |
| 901 | (?: |
| 902 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 903 | (?:$typeTypedefs\b)| |
| 904 | (?:${allWithAttr}\b) |
| 905 | ) |
| 906 | (?:\s+$Modifier|\s+const)* |
| 907 | }x; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 908 | $Type = qr{ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 909 | $NonptrType |
Antonio Borneo | 7b18496 | 2020-04-06 20:11:04 -0700 | [diff] [blame] | 910 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 911 | (?:\s+$Inline|\s+$Modifier)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 912 | }x; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 913 | $TypeMisordered = qr{ |
| 914 | $NonptrTypeMisordered |
Antonio Borneo | 7b18496 | 2020-04-06 20:11:04 -0700 | [diff] [blame] | 915 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4} |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 916 | (?:\s+$Inline|\s+$Modifier)* |
| 917 | }x; |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 918 | $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 919 | $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 920 | } |
| 921 | build_types(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 922 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 923 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 924 | |
| 925 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 926 | # requires at least perl version v5.10.0 |
| 927 | # Any use must be runtime checked with $^V |
| 928 | |
| 929 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 930 | our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; |
Joe Perches | c0a5c89 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 931 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 932 | |
Joe Perches | f842230 | 2014-08-06 16:11:31 -0700 | [diff] [blame] | 933 | our $declaration_macros = qr{(?x: |
Joe Perches | 3e838b6 | 2015-09-09 15:37:33 -0700 | [diff] [blame] | 934 | (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| |
Steffen Maier | fe658f9 | 2017-07-10 15:52:10 -0700 | [diff] [blame] | 935 | (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(| |
Gilad Ben-Yossef | 3d102fc | 2018-04-10 16:33:17 -0700 | [diff] [blame] | 936 | (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\( |
Joe Perches | f842230 | 2014-08-06 16:11:31 -0700 | [diff] [blame] | 937 | )}; |
| 938 | |
Aditya Srivastava | 8d0325c | 2020-12-15 20:44:24 -0800 | [diff] [blame] | 939 | our %allow_repeated_words = ( |
| 940 | add => '', |
| 941 | added => '', |
| 942 | bad => '', |
| 943 | be => '', |
| 944 | ); |
| 945 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 946 | sub deparenthesize { |
| 947 | my ($string) = @_; |
| 948 | return "" if (!defined($string)); |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 949 | |
| 950 | while ($string =~ /^\s*\(.*\)\s*$/) { |
| 951 | $string =~ s@^\s*\(\s*@@; |
| 952 | $string =~ s@\s*\)\s*$@@; |
| 953 | } |
| 954 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 955 | $string =~ s@\s+@ @g; |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 956 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 957 | return $string; |
| 958 | } |
| 959 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 960 | sub seed_camelcase_file { |
| 961 | my ($file) = @_; |
| 962 | |
| 963 | return if (!(-f $file)); |
| 964 | |
| 965 | local $/; |
| 966 | |
| 967 | open(my $include_file, '<', "$file") |
| 968 | or warn "$P: Can't read '$file' $!\n"; |
| 969 | my $text = <$include_file>; |
| 970 | close($include_file); |
| 971 | |
| 972 | my @lines = split('\n', $text); |
| 973 | |
| 974 | foreach my $line (@lines) { |
| 975 | next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); |
| 976 | if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { |
| 977 | $camelcase{$1} = 1; |
Joe Perches | 11ea516 | 2013-11-12 15:10:08 -0800 | [diff] [blame] | 978 | } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { |
| 979 | $camelcase{$1} = 1; |
| 980 | } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 981 | $camelcase{$1} = 1; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | |
Joe Perches | cd28b11 | 2019-12-04 16:52:09 -0800 | [diff] [blame] | 986 | our %maintained_status = (); |
| 987 | |
Joe Perches | 85b0ee1 | 2016-10-11 13:51:44 -0700 | [diff] [blame] | 988 | sub is_maintained_obsolete { |
| 989 | my ($filename) = @_; |
| 990 | |
Jerome Forissier | f2c19c2 | 2016-12-12 16:46:23 -0800 | [diff] [blame] | 991 | return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl")); |
Joe Perches | 85b0ee1 | 2016-10-11 13:51:44 -0700 | [diff] [blame] | 992 | |
Joe Perches | cd28b11 | 2019-12-04 16:52:09 -0800 | [diff] [blame] | 993 | if (!exists($maintained_status{$filename})) { |
| 994 | $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`; |
| 995 | } |
Joe Perches | 85b0ee1 | 2016-10-11 13:51:44 -0700 | [diff] [blame] | 996 | |
Joe Perches | cd28b11 | 2019-12-04 16:52:09 -0800 | [diff] [blame] | 997 | return $maintained_status{$filename} =~ /obsolete/i; |
Joe Perches | 85b0ee1 | 2016-10-11 13:51:44 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Joe Perches | 3b6e8ac | 2018-08-21 21:57:47 -0700 | [diff] [blame] | 1000 | sub is_SPDX_License_valid { |
| 1001 | my ($license) = @_; |
| 1002 | |
Joe Perches | 0f7f635 | 2020-10-24 16:59:04 -0700 | [diff] [blame] | 1003 | return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$gitroot")); |
Joe Perches | 3b6e8ac | 2018-08-21 21:57:47 -0700 | [diff] [blame] | 1004 | |
Joe Perches | 5629411 | 2018-08-21 21:58:04 -0700 | [diff] [blame] | 1005 | my $root_path = abs_path($root); |
| 1006 | my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`; |
Joe Perches | 3b6e8ac | 2018-08-21 21:57:47 -0700 | [diff] [blame] | 1007 | return 0 if ($status ne ""); |
| 1008 | return 1; |
| 1009 | } |
| 1010 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 1011 | my $camelcase_seeded = 0; |
| 1012 | sub seed_camelcase_includes { |
| 1013 | return if ($camelcase_seeded); |
| 1014 | |
| 1015 | my $files; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 1016 | my $camelcase_cache = ""; |
| 1017 | my @include_files = (); |
| 1018 | |
| 1019 | $camelcase_seeded = 1; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 1020 | |
Joe Perches | 0f7f635 | 2020-10-24 16:59:04 -0700 | [diff] [blame] | 1021 | if (-e "$gitroot") { |
Joe Perches | dbbf869 | 2019-09-25 16:46:52 -0700 | [diff] [blame] | 1022 | my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 1023 | chomp $git_last_include_commit; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 1024 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 1025 | } else { |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 1026 | my $last_mod_date = 0; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 1027 | $files = `find $root/include -name "*.h"`; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 1028 | @include_files = split('\n', $files); |
| 1029 | foreach my $file (@include_files) { |
| 1030 | my $date = POSIX::strftime("%Y%m%d%H%M", |
| 1031 | localtime((stat $file)[9])); |
| 1032 | $last_mod_date = $date if ($last_mod_date < $date); |
| 1033 | } |
| 1034 | $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 1035 | } |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 1036 | |
| 1037 | if ($camelcase_cache ne "" && -f $camelcase_cache) { |
| 1038 | open(my $camelcase_file, '<', "$camelcase_cache") |
| 1039 | or warn "$P: Can't read '$camelcase_cache' $!\n"; |
| 1040 | while (<$camelcase_file>) { |
| 1041 | chomp; |
| 1042 | $camelcase{$_} = 1; |
| 1043 | } |
| 1044 | close($camelcase_file); |
| 1045 | |
| 1046 | return; |
| 1047 | } |
| 1048 | |
Joe Perches | 0f7f635 | 2020-10-24 16:59:04 -0700 | [diff] [blame] | 1049 | if (-e "$gitroot") { |
Joe Perches | dbbf869 | 2019-09-25 16:46:52 -0700 | [diff] [blame] | 1050 | $files = `${git_command} ls-files "include/*.h"`; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 1051 | @include_files = split('\n', $files); |
| 1052 | } |
| 1053 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 1054 | foreach my $file (@include_files) { |
| 1055 | seed_camelcase_file($file); |
| 1056 | } |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 1057 | |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 1058 | if ($camelcase_cache ne "") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 1059 | unlink glob ".checkpatch-camelcase.*"; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 1060 | open(my $camelcase_file, '>', "$camelcase_cache") |
| 1061 | or warn "$P: Can't write '$camelcase_cache' $!\n"; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 1062 | foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { |
| 1063 | print $camelcase_file ("$_\n"); |
| 1064 | } |
| 1065 | close($camelcase_file); |
| 1066 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Joe Perches | f5f6132 | 2020-10-15 20:12:12 -0700 | [diff] [blame] | 1069 | sub git_is_single_file { |
| 1070 | my ($filename) = @_; |
| 1071 | |
| 1072 | return 0 if ((which("git") eq "") || !(-e "$gitroot")); |
| 1073 | |
| 1074 | my $output = `${git_command} ls-files -- $filename 2>/dev/null`; |
| 1075 | my $count = $output =~ tr/\n//; |
| 1076 | return $count eq 1 && $output =~ m{^${filename}$}; |
| 1077 | } |
| 1078 | |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1079 | sub git_commit_info { |
| 1080 | my ($commit, $id, $desc) = @_; |
| 1081 | |
Joe Perches | 0f7f635 | 2020-10-24 16:59:04 -0700 | [diff] [blame] | 1082 | return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot")); |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1083 | |
Joe Perches | dbbf869 | 2019-09-25 16:46:52 -0700 | [diff] [blame] | 1084 | my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`; |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1085 | $output =~ s/^\s*//gm; |
| 1086 | my @lines = split("\n", $output); |
| 1087 | |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 1088 | return ($id, $desc) if ($#lines < 0); |
| 1089 | |
Sean Christopherson | 5a7f445 | 2019-09-25 16:46:49 -0700 | [diff] [blame] | 1090 | if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) { |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1091 | # Maybe one day convert this block of bash into something that returns |
| 1092 | # all matching commit ids, but it's very slow... |
| 1093 | # |
| 1094 | # echo "checking commits $1..." |
| 1095 | # git rev-list --remotes | grep -i "^$1" | |
| 1096 | # while read line ; do |
| 1097 | # git log --format='%H %s' -1 $line | |
| 1098 | # echo "commit $(cut -c 1-12,41-)" |
| 1099 | # done |
| 1100 | } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { |
Heinrich Schuchardt | 948b133 | 2017-07-10 15:52:16 -0700 | [diff] [blame] | 1101 | $id = undef; |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1102 | } else { |
| 1103 | $id = substr($lines[0], 0, 12); |
| 1104 | $desc = substr($lines[0], 41); |
| 1105 | } |
| 1106 | |
| 1107 | return ($id, $desc); |
| 1108 | } |
| 1109 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1110 | $chk_signoff = 0 if ($file); |
| 1111 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1112 | my @rawlines = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1113 | my @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1114 | my @fixed = (); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 1115 | my @fixed_inserted = (); |
| 1116 | my @fixed_deleted = (); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 1117 | my $fixlinenr = -1; |
| 1118 | |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 1119 | # If input is git commits, extract all commits from the commit expressions. |
| 1120 | # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. |
Joe Perches | 0f7f635 | 2020-10-24 16:59:04 -0700 | [diff] [blame] | 1121 | die "$P: No git repository found\n" if ($git && !-e "$gitroot"); |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 1122 | |
| 1123 | if ($git) { |
| 1124 | my @commits = (); |
Joe Perches | 0dea9f1e | 2016-05-20 17:04:19 -0700 | [diff] [blame] | 1125 | foreach my $commit_expr (@ARGV) { |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 1126 | my $git_range; |
Joe Perches | 28898fd | 2016-05-20 17:04:22 -0700 | [diff] [blame] | 1127 | if ($commit_expr =~ m/^(.*)-(\d+)$/) { |
| 1128 | $git_range = "-$2 $1"; |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 1129 | } elsif ($commit_expr =~ m/\.\./) { |
| 1130 | $git_range = "$commit_expr"; |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 1131 | } else { |
Joe Perches | 0dea9f1e | 2016-05-20 17:04:19 -0700 | [diff] [blame] | 1132 | $git_range = "-1 $commit_expr"; |
| 1133 | } |
Joe Perches | dbbf869 | 2019-09-25 16:46:52 -0700 | [diff] [blame] | 1134 | my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`; |
Joe Perches | 0dea9f1e | 2016-05-20 17:04:19 -0700 | [diff] [blame] | 1135 | foreach my $line (split(/\n/, $lines)) { |
Joe Perches | 28898fd | 2016-05-20 17:04:22 -0700 | [diff] [blame] | 1136 | $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; |
| 1137 | next if (!defined($1) || !defined($2)); |
Joe Perches | 0dea9f1e | 2016-05-20 17:04:19 -0700 | [diff] [blame] | 1138 | my $sha1 = $1; |
| 1139 | my $subject = $2; |
| 1140 | unshift(@commits, $sha1); |
| 1141 | $git_commits{$sha1} = $subject; |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | die "$P: no git commits after extraction!\n" if (@commits == 0); |
| 1145 | @ARGV = @commits; |
| 1146 | } |
| 1147 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1148 | my $vname; |
Vadim Bendebury | 98005e8 | 2019-03-07 16:28:38 -0800 | [diff] [blame] | 1149 | $allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1150 | for my $filename (@ARGV) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 1151 | my $FILE; |
Joe Perches | f5f6132 | 2020-10-15 20:12:12 -0700 | [diff] [blame] | 1152 | my $is_git_file = git_is_single_file($filename); |
| 1153 | my $oldfile = $file; |
| 1154 | $file = 1 if ($is_git_file); |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 1155 | if ($git) { |
| 1156 | open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || |
| 1157 | die "$P: $filename: git format-patch failed - $!\n"; |
| 1158 | } elsif ($file) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 1159 | open($FILE, '-|', "diff -u /dev/null $filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1160 | die "$P: $filename: diff failed - $!\n"; |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 1161 | } elsif ($filename eq '-') { |
| 1162 | open($FILE, '<&STDIN'); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1163 | } else { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 1164 | open($FILE, '<', "$filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1165 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1166 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1167 | if ($filename eq '-') { |
| 1168 | $vname = 'Your patch'; |
Du, Changbin | 4a593c3 | 2016-05-20 17:04:16 -0700 | [diff] [blame] | 1169 | } elsif ($git) { |
Joe Perches | 0dea9f1e | 2016-05-20 17:04:19 -0700 | [diff] [blame] | 1170 | $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1171 | } else { |
| 1172 | $vname = $filename; |
| 1173 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 1174 | while (<$FILE>) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1175 | chomp; |
| 1176 | push(@rawlines, $_); |
Geert Uytterhoeven | c7f574d | 2020-06-04 16:50:43 -0700 | [diff] [blame] | 1177 | $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1178 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 1179 | close($FILE); |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 1180 | |
| 1181 | if ($#ARGV > 0 && $quiet == 0) { |
| 1182 | print '-' x length($vname) . "\n"; |
| 1183 | print "$vname\n"; |
| 1184 | print '-' x length($vname) . "\n"; |
| 1185 | } |
| 1186 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1187 | if (!process($filename)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1188 | $exit = 1; |
| 1189 | } |
| 1190 | @rawlines = (); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1191 | @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1192 | @fixed = (); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 1193 | @fixed_inserted = (); |
| 1194 | @fixed_deleted = (); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 1195 | $fixlinenr = -1; |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 1196 | @modifierListFile = (); |
| 1197 | @typeListFile = (); |
| 1198 | build_types(); |
Joe Perches | f5f6132 | 2020-10-15 20:12:12 -0700 | [diff] [blame] | 1199 | $file = $oldfile if ($is_git_file); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 1202 | if (!$quiet) { |
Joe Perches | 3c816e4 | 2015-06-25 15:03:29 -0700 | [diff] [blame] | 1203 | hash_show_words(\%use_type, "Used"); |
| 1204 | hash_show_words(\%ignore_type, "Ignored"); |
| 1205 | |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 1206 | if (!$perl_version_ok) { |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 1207 | print << "EOM" |
| 1208 | |
| 1209 | NOTE: perl $^V is not modern enough to detect all possible issues. |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 1210 | An upgrade to at least perl $minimum_perl_version is suggested. |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 1211 | EOM |
| 1212 | } |
| 1213 | if ($exit) { |
| 1214 | print << "EOM" |
| 1215 | |
| 1216 | NOTE: If any of the errors are false positives, please report |
| 1217 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 1218 | EOM |
| 1219 | } |
| 1220 | } |
| 1221 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1222 | exit($exit); |
| 1223 | |
| 1224 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1225 | my ($root) = @_; |
| 1226 | |
| 1227 | my @tree_check = ( |
| 1228 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 1229 | "README", "Documentation", "arch", "include", "drivers", |
| 1230 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 1231 | ); |
| 1232 | |
| 1233 | foreach my $check (@tree_check) { |
| 1234 | if (! -e $root . '/' . $check) { |
| 1235 | return 0; |
| 1236 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1237 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1238 | return 1; |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 1239 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1240 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1241 | sub parse_email { |
| 1242 | my ($formatted_email) = @_; |
| 1243 | |
| 1244 | my $name = ""; |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 1245 | my $quoted = ""; |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 1246 | my $name_comment = ""; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1247 | my $address = ""; |
| 1248 | my $comment = ""; |
| 1249 | |
| 1250 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 1251 | $name = $1; |
| 1252 | $address = $2; |
| 1253 | $comment = $3 if defined $3; |
| 1254 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 1255 | $address = $1; |
| 1256 | $comment = $2 if defined $2; |
| 1257 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 1258 | $address = $1; |
| 1259 | $comment = $2 if defined $2; |
Joe Perches | 85e1206 | 2018-04-10 16:33:09 -0700 | [diff] [blame] | 1260 | $formatted_email =~ s/\Q$address\E.*$//; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1261 | $name = $formatted_email; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1262 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1263 | $name =~ s/^\"|\"$//g; |
| 1264 | # If there's a name left after stripping spaces and |
| 1265 | # leading quotes, and the address doesn't have both |
| 1266 | # leading and trailing angle brackets, the address |
| 1267 | # is invalid. ie: |
| 1268 | # "joe smith joe@smith.com" bad |
| 1269 | # "joe smith <joe@smith.com" bad |
| 1270 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 1271 | $name = ""; |
| 1272 | $address = ""; |
| 1273 | $comment = ""; |
| 1274 | } |
| 1275 | } |
| 1276 | |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 1277 | # Extract comments from names excluding quoted parts |
| 1278 | # "John D. (Doe)" - Do not extract |
| 1279 | if ($name =~ s/\"(.+)\"//) { |
| 1280 | $quoted = $1; |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 1281 | } |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 1282 | while ($name =~ s/\s*($balanced_parens)\s*/ /) { |
| 1283 | $name_comment .= trim($1); |
| 1284 | } |
| 1285 | $name =~ s/^[ \"]+|[ \"]+$//g; |
| 1286 | $name = trim("$quoted $name"); |
| 1287 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1288 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1289 | $address =~ s/^\<|\>$//g; |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 1290 | $comment = trim($comment); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1291 | |
| 1292 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 1293 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 1294 | $name = "\"$name\""; |
| 1295 | } |
| 1296 | |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 1297 | return ($name, $name_comment, $address, $comment); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | sub format_email { |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 1301 | my ($name, $name_comment, $address, $comment) = @_; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1302 | |
| 1303 | my $formatted_email; |
| 1304 | |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 1305 | $name =~ s/^[ \"]+|[ \"]+$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1306 | $address = trim($address); |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 1307 | $address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1308 | |
| 1309 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 1310 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 1311 | $name = "\"$name\""; |
| 1312 | } |
| 1313 | |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 1314 | $name_comment = trim($name_comment); |
| 1315 | $name_comment = " $name_comment" if ($name_comment ne ""); |
| 1316 | $comment = trim($comment); |
| 1317 | $comment = " $comment" if ($comment ne ""); |
| 1318 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1319 | if ("$name" eq "") { |
| 1320 | $formatted_email = "$address"; |
| 1321 | } else { |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 1322 | $formatted_email = "$name$name_comment <$address>"; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1323 | } |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 1324 | $formatted_email .= "$comment"; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1325 | return $formatted_email; |
| 1326 | } |
| 1327 | |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 1328 | sub reformat_email { |
| 1329 | my ($email) = @_; |
| 1330 | |
| 1331 | my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 1332 | return format_email($email_name, $name_comment, $email_address, $comment); |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | sub same_email_addresses { |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 1336 | my ($email1, $email2) = @_; |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 1337 | |
| 1338 | my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1); |
| 1339 | my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2); |
| 1340 | |
| 1341 | return $email1_name eq $email2_name && |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 1342 | $email1_address eq $email2_address && |
| 1343 | $name1_comment eq $name2_comment && |
| 1344 | $comment1 eq $comment2; |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1347 | sub which { |
Joe Perches | bd474ca | 2014-08-06 16:11:10 -0700 | [diff] [blame] | 1348 | my ($bin) = @_; |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1349 | |
Joe Perches | bd474ca | 2014-08-06 16:11:10 -0700 | [diff] [blame] | 1350 | foreach my $path (split(/:/, $ENV{PATH})) { |
| 1351 | if (-e "$path/$bin") { |
| 1352 | return "$path/$bin"; |
| 1353 | } |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1354 | } |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1355 | |
Joe Perches | bd474ca | 2014-08-06 16:11:10 -0700 | [diff] [blame] | 1356 | return ""; |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1359 | sub which_conf { |
| 1360 | my ($conf) = @_; |
| 1361 | |
| 1362 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 1363 | if (-e "$path/$conf") { |
| 1364 | return "$path/$conf"; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | return ""; |
| 1369 | } |
| 1370 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1371 | sub expand_tabs { |
| 1372 | my ($str) = @_; |
| 1373 | |
| 1374 | my $res = ''; |
| 1375 | my $n = 0; |
| 1376 | for my $c (split(//, $str)) { |
| 1377 | if ($c eq "\t") { |
| 1378 | $res .= ' '; |
| 1379 | $n++; |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 1380 | for (; ($n % $tabsize) != 0; $n++) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1381 | $res .= ' '; |
| 1382 | } |
| 1383 | next; |
| 1384 | } |
| 1385 | $res .= $c; |
| 1386 | $n++; |
| 1387 | } |
| 1388 | |
| 1389 | return $res; |
| 1390 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1391 | sub copy_spacing { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1392 | (my $res = shift) =~ tr/\t/ /c; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1393 | return $res; |
| 1394 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1395 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1396 | sub line_stats { |
| 1397 | my ($line) = @_; |
| 1398 | |
| 1399 | # Drop the diff line leader and expand tabs |
| 1400 | $line =~ s/^.//; |
| 1401 | $line = expand_tabs($line); |
| 1402 | |
| 1403 | # Pick the indent from the front of the line. |
| 1404 | my ($white) = ($line =~ /^(\s*)/); |
| 1405 | |
| 1406 | return (length($line), length($white)); |
| 1407 | } |
| 1408 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1409 | my $sanitise_quote = ''; |
| 1410 | |
| 1411 | sub sanitise_line_reset { |
| 1412 | my ($in_comment) = @_; |
| 1413 | |
| 1414 | if ($in_comment) { |
| 1415 | $sanitise_quote = '*/'; |
| 1416 | } else { |
| 1417 | $sanitise_quote = ''; |
| 1418 | } |
| 1419 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1420 | sub sanitise_line { |
| 1421 | my ($line) = @_; |
| 1422 | |
| 1423 | my $res = ''; |
| 1424 | my $l = ''; |
| 1425 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1426 | my $qlen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1427 | my $off = 0; |
| 1428 | my $c; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1429 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1430 | # Always copy over the diff marker. |
| 1431 | $res = substr($line, 0, 1); |
| 1432 | |
| 1433 | for ($off = 1; $off < length($line); $off++) { |
| 1434 | $c = substr($line, $off, 1); |
| 1435 | |
Claudio Fontana | 8d2e11b | 2018-04-10 16:33:42 -0700 | [diff] [blame] | 1436 | # Comments we are whacking completely including the begin |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1437 | # and end, all to $;. |
| 1438 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 1439 | $sanitise_quote = '*/'; |
| 1440 | |
| 1441 | substr($res, $off, 2, "$;$;"); |
| 1442 | $off++; |
| 1443 | next; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1444 | } |
Andy Whitcroft | 81bc0e0 | 2008-10-15 22:02:26 -0700 | [diff] [blame] | 1445 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1446 | $sanitise_quote = ''; |
| 1447 | substr($res, $off, 2, "$;$;"); |
| 1448 | $off++; |
| 1449 | next; |
| 1450 | } |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 1451 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 1452 | $sanitise_quote = '//'; |
| 1453 | |
| 1454 | substr($res, $off, 2, $sanitise_quote); |
| 1455 | $off++; |
| 1456 | next; |
| 1457 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1458 | |
| 1459 | # A \ in a string means ignore the next character. |
| 1460 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 1461 | $c eq "\\") { |
| 1462 | substr($res, $off, 2, 'XX'); |
| 1463 | $off++; |
| 1464 | next; |
| 1465 | } |
| 1466 | # Regular quotes. |
| 1467 | if ($c eq "'" || $c eq '"') { |
| 1468 | if ($sanitise_quote eq '') { |
| 1469 | $sanitise_quote = $c; |
| 1470 | |
| 1471 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1472 | next; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1473 | } elsif ($sanitise_quote eq $c) { |
| 1474 | $sanitise_quote = ''; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1475 | } |
| 1476 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1477 | |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 1478 | #print "c<$c> SQ<$sanitise_quote>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1479 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 1480 | substr($res, $off, 1, $;); |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 1481 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 1482 | substr($res, $off, 1, $;); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1483 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 1484 | substr($res, $off, 1, 'X'); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1485 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1486 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1487 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1488 | } |
| 1489 | |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 1490 | if ($sanitise_quote eq '//') { |
| 1491 | $sanitise_quote = ''; |
| 1492 | } |
| 1493 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1494 | # The pathname on a #include may be surrounded by '<' and '>'. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1495 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1496 | my $clean = 'X' x length($1); |
| 1497 | $res =~ s@\<.*\>@<$clean>@; |
| 1498 | |
| 1499 | # The whole of a #error is a string. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1500 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1501 | my $clean = 'X' x length($1); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1502 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
Joe Perches | dadf680 | 2016-08-02 14:04:33 -0700 | [diff] [blame] | 1505 | if ($allow_c99_comments && $res =~ m@(//.*$)@) { |
| 1506 | my $match = $1; |
| 1507 | $res =~ s/\Q$match\E/"$;" x length($match)/e; |
| 1508 | } |
| 1509 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1510 | return $res; |
| 1511 | } |
| 1512 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 1513 | sub get_quoted_string { |
| 1514 | my ($line, $rawline) = @_; |
| 1515 | |
Joe Perches | 478b179 | 2018-04-10 16:33:34 -0700 | [diff] [blame] | 1516 | return "" if (!defined($line) || !defined($rawline)); |
Joe Perches | 33acb54 | 2015-06-25 15:02:54 -0700 | [diff] [blame] | 1517 | return "" if ($line !~ m/($String)/g); |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 1518 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 1519 | } |
| 1520 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1521 | sub ctx_statement_block { |
| 1522 | my ($linenr, $remain, $off) = @_; |
| 1523 | my $line = $linenr - 1; |
| 1524 | my $blk = ''; |
| 1525 | my $soff = $off; |
| 1526 | my $coff = $off - 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1527 | my $coff_set = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1528 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1529 | my $loff = 0; |
| 1530 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1531 | my $type = ''; |
| 1532 | my $level = 0; |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 1533 | my @stack = (); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1534 | my $p; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1535 | my $c; |
| 1536 | my $len = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1537 | |
| 1538 | my $remainder; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1539 | while (1) { |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 1540 | @stack = (['', 0]) if ($#stack == -1); |
| 1541 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1542 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1543 | # If we are about to drop off the end, pull in more |
| 1544 | # context. |
| 1545 | if ($off >= $len) { |
| 1546 | for (; $remain > 0; $line++) { |
Andy Whitcroft | dea3349 | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 1547 | last if (!defined $lines[$line]); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1548 | next if ($lines[$line] =~ /^-/); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1549 | $remain--; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1550 | $loff = $len; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1551 | $blk .= $lines[$line] . "\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1552 | $len = length($blk); |
| 1553 | $line++; |
| 1554 | last; |
| 1555 | } |
| 1556 | # Bail if there is no further context. |
| 1557 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1558 | if ($off >= $len) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1559 | last; |
| 1560 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 1561 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 1562 | $level++; |
| 1563 | $type = '#'; |
| 1564 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1565 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1566 | $p = $c; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1567 | $c = substr($blk, $off, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1568 | $remainder = substr($blk, $off); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1569 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1570 | #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1571 | |
| 1572 | # Handle nested #if/#else. |
| 1573 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 1574 | push(@stack, [ $type, $level ]); |
| 1575 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 1576 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 1577 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 1578 | ($type, $level) = @{pop(@stack)}; |
| 1579 | } |
| 1580 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1581 | # Statement ends at the ';' or a close '}' at the |
| 1582 | # outermost level. |
| 1583 | if ($level == 0 && $c eq ';') { |
| 1584 | last; |
| 1585 | } |
| 1586 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1587 | # An else is really a conditional as long as its not else if |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1588 | if ($level == 0 && $coff_set == 0 && |
| 1589 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 1590 | $remainder =~ /^(else)(?:\s|{)/ && |
| 1591 | $remainder !~ /^else\s+if\b/) { |
| 1592 | $coff = $off + length($1) - 1; |
| 1593 | $coff_set = 1; |
| 1594 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 1595 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1598 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 1599 | $level++; |
| 1600 | $type = '('; |
| 1601 | } |
| 1602 | if ($type eq '(' && $c eq ')') { |
| 1603 | $level--; |
| 1604 | $type = ($level != 0)? '(' : ''; |
| 1605 | |
| 1606 | if ($level == 0 && $coff < $soff) { |
| 1607 | $coff = $off; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1608 | $coff_set = 1; |
| 1609 | #warn "CSB: mark coff<$coff>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1610 | } |
| 1611 | } |
| 1612 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 1613 | $level++; |
| 1614 | $type = '{'; |
| 1615 | } |
| 1616 | if ($type eq '{' && $c eq '}') { |
| 1617 | $level--; |
| 1618 | $type = ($level != 0)? '{' : ''; |
| 1619 | |
| 1620 | if ($level == 0) { |
Patrick Pannuto | b998e00 | 2010-08-09 17:21:03 -0700 | [diff] [blame] | 1621 | if (substr($blk, $off + 1, 1) eq ';') { |
| 1622 | $off++; |
| 1623 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1624 | last; |
| 1625 | } |
| 1626 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 1627 | # Preprocessor commands end at the newline unless escaped. |
| 1628 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 1629 | $level--; |
| 1630 | $type = ''; |
| 1631 | $off++; |
| 1632 | last; |
| 1633 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1634 | $off++; |
| 1635 | } |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 1636 | # We are truly at the end, so shuffle to the next line. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1637 | if ($off == $len) { |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 1638 | $loff = $len + 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1639 | $line++; |
| 1640 | $remain--; |
| 1641 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1642 | |
| 1643 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 1644 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 1645 | |
| 1646 | #warn "STATEMENT<$statement>\n"; |
| 1647 | #warn "CONDITION<$condition>\n"; |
| 1648 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1649 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1650 | |
| 1651 | return ($statement, $condition, |
| 1652 | $line, $remain + 1, $off - $loff + 1, $level); |
| 1653 | } |
| 1654 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1655 | sub statement_lines { |
| 1656 | my ($stmt) = @_; |
| 1657 | |
| 1658 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 1659 | $stmt =~ s/(^|\n)./$1/g; |
| 1660 | $stmt =~ s/^\s*//; |
| 1661 | $stmt =~ s/\s*$//; |
| 1662 | |
| 1663 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1664 | |
| 1665 | return $#stmt_lines + 2; |
| 1666 | } |
| 1667 | |
| 1668 | sub statement_rawlines { |
| 1669 | my ($stmt) = @_; |
| 1670 | |
| 1671 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1672 | |
| 1673 | return $#stmt_lines + 2; |
| 1674 | } |
| 1675 | |
| 1676 | sub statement_block_size { |
| 1677 | my ($stmt) = @_; |
| 1678 | |
| 1679 | $stmt =~ s/(^|\n)./$1/g; |
| 1680 | $stmt =~ s/^\s*{//; |
| 1681 | $stmt =~ s/}\s*$//; |
| 1682 | $stmt =~ s/^\s*//; |
| 1683 | $stmt =~ s/\s*$//; |
| 1684 | |
| 1685 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1686 | my @stmt_statements = ($stmt =~ /;/g); |
| 1687 | |
| 1688 | my $stmt_lines = $#stmt_lines + 2; |
| 1689 | my $stmt_statements = $#stmt_statements + 1; |
| 1690 | |
| 1691 | if ($stmt_lines > $stmt_statements) { |
| 1692 | return $stmt_lines; |
| 1693 | } else { |
| 1694 | return $stmt_statements; |
| 1695 | } |
| 1696 | } |
| 1697 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1698 | sub ctx_statement_full { |
| 1699 | my ($linenr, $remain, $off) = @_; |
| 1700 | my ($statement, $condition, $level); |
| 1701 | |
| 1702 | my (@chunks); |
| 1703 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1704 | # Grab the first conditional/block pair. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1705 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1706 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1707 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1708 | push(@chunks, [ $condition, $statement ]); |
| 1709 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 1710 | return ($level, $linenr, @chunks); |
| 1711 | } |
| 1712 | |
| 1713 | # Pull in the following conditional/block pairs and see if they |
| 1714 | # could continue the statement. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1715 | for (;;) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1716 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1717 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1718 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1719 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1720 | #print "C: push\n"; |
| 1721 | push(@chunks, [ $condition, $statement ]); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | return ($level, $linenr, @chunks); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1727 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1728 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1729 | my $line; |
| 1730 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1731 | my $blk = ''; |
| 1732 | my @o; |
| 1733 | my @c; |
| 1734 | my @res = (); |
| 1735 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1736 | my $level = 0; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1737 | my @stack = ($level); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1738 | for ($line = $start; $remain > 0; $line++) { |
| 1739 | next if ($rawlines[$line] =~ /^-/); |
| 1740 | $remain--; |
| 1741 | |
| 1742 | $blk .= $rawlines[$line]; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1743 | |
| 1744 | # Handle nested #if/#else. |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1745 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1746 | push(@stack, $level); |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1747 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1748 | $level = $stack[$#stack - 1]; |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1749 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1750 | $level = pop(@stack); |
| 1751 | } |
| 1752 | |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1753 | foreach my $c (split(//, $lines[$line])) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1754 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 1755 | if ($off > 0) { |
| 1756 | $off--; |
| 1757 | next; |
| 1758 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1759 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1760 | if ($c eq $close && $level > 0) { |
| 1761 | $level--; |
| 1762 | last if ($level == 0); |
| 1763 | } elsif ($c eq $open) { |
| 1764 | $level++; |
| 1765 | } |
| 1766 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1767 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1768 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1769 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1772 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1775 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1776 | } |
| 1777 | sub ctx_block_outer { |
| 1778 | my ($linenr, $remain) = @_; |
| 1779 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1780 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 1781 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1782 | } |
| 1783 | sub ctx_block { |
| 1784 | my ($linenr, $remain) = @_; |
| 1785 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1786 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1787 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1788 | } |
| 1789 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1790 | my ($linenr, $remain, $off) = @_; |
| 1791 | |
| 1792 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1793 | return @r; |
| 1794 | } |
| 1795 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1796 | my ($linenr, $remain) = @_; |
| 1797 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1798 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1799 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1800 | sub ctx_statement_level { |
| 1801 | my ($linenr, $remain, $off) = @_; |
| 1802 | |
| 1803 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1804 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1805 | |
| 1806 | sub ctx_locate_comment { |
| 1807 | my ($first_line, $end_line) = @_; |
| 1808 | |
Joe Perches | a55ee0c | 2020-06-04 16:50:36 -0700 | [diff] [blame] | 1809 | # If c99 comment on the current line, or the line before or after |
| 1810 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@); |
| 1811 | return $current_comment if (defined $current_comment); |
| 1812 | ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@); |
| 1813 | return $current_comment if (defined $current_comment); |
| 1814 | ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@); |
| 1815 | return $current_comment if (defined $current_comment); |
| 1816 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1817 | # Catch a comment on the end of the line itself. |
Joe Perches | a55ee0c | 2020-06-04 16:50:36 -0700 | [diff] [blame] | 1818 | ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1819 | return $current_comment if (defined $current_comment); |
| 1820 | |
| 1821 | # Look through the context and try and figure out if there is a |
| 1822 | # comment. |
| 1823 | my $in_comment = 0; |
| 1824 | $current_comment = ''; |
| 1825 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1826 | my $line = $rawlines[$linenr - 1]; |
| 1827 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1828 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 1829 | $in_comment = 1; |
| 1830 | } |
| 1831 | if ($line =~ m@/\*@) { |
| 1832 | $in_comment = 1; |
| 1833 | } |
| 1834 | if (!$in_comment && $current_comment ne '') { |
| 1835 | $current_comment = ''; |
| 1836 | } |
| 1837 | $current_comment .= $line . "\n" if ($in_comment); |
| 1838 | if ($line =~ m@\*/@) { |
| 1839 | $in_comment = 0; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | chomp($current_comment); |
| 1844 | return($current_comment); |
| 1845 | } |
| 1846 | sub ctx_has_comment { |
| 1847 | my ($first_line, $end_line) = @_; |
| 1848 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 1849 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1850 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1851 | ##print "CMMT: $cmt\n"; |
| 1852 | |
| 1853 | return ($cmt ne ''); |
| 1854 | } |
| 1855 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1856 | sub raw_line { |
| 1857 | my ($linenr, $cnt) = @_; |
| 1858 | |
| 1859 | my $offset = $linenr - 1; |
| 1860 | $cnt++; |
| 1861 | |
| 1862 | my $line; |
| 1863 | while ($cnt) { |
| 1864 | $line = $rawlines[$offset++]; |
| 1865 | next if (defined($line) && $line =~ /^-/); |
| 1866 | $cnt--; |
| 1867 | } |
| 1868 | |
| 1869 | return $line; |
| 1870 | } |
| 1871 | |
Tobin C. Harding | 2a9f9d8 | 2018-04-10 16:33:20 -0700 | [diff] [blame] | 1872 | sub get_stat_real { |
| 1873 | my ($linenr, $lc) = @_; |
| 1874 | |
| 1875 | my $stat_real = raw_line($linenr, 0); |
| 1876 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 1877 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 1878 | } |
| 1879 | |
| 1880 | return $stat_real; |
| 1881 | } |
| 1882 | |
Tobin C. Harding | e3d95a2 | 2018-04-10 16:33:27 -0700 | [diff] [blame] | 1883 | sub get_stat_here { |
| 1884 | my ($linenr, $cnt, $here) = @_; |
| 1885 | |
| 1886 | my $herectx = $here . "\n"; |
| 1887 | for (my $n = 0; $n < $cnt; $n++) { |
| 1888 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 1889 | } |
| 1890 | |
| 1891 | return $herectx; |
| 1892 | } |
| 1893 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1894 | sub cat_vet { |
| 1895 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1896 | my ($res, $coded); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1897 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1898 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1899 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 1900 | $res .= $1; |
| 1901 | if ($2 ne '') { |
| 1902 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 1903 | $res .= $coded; |
| 1904 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1905 | } |
| 1906 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1907 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1908 | return $res; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1911 | my $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1912 | my $av_pending; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1913 | my @av_paren_type; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1914 | my $av_pend_colon; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1915 | |
| 1916 | sub annotate_reset { |
| 1917 | $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1918 | $av_pending = '_'; |
| 1919 | @av_paren_type = ('E'); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1920 | $av_pend_colon = 'O'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1921 | } |
| 1922 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1923 | sub annotate_values { |
| 1924 | my ($stream, $type) = @_; |
| 1925 | |
| 1926 | my $res; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1927 | my $var = '_' x length($stream); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1928 | my $cur = $stream; |
| 1929 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1930 | print "$stream\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1931 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1932 | while (length($cur)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1933 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1934 | print " <" . join('', @av_paren_type) . |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1935 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1936 | if ($cur =~ /^(\s+)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1937 | print "WS($1)\n" if ($dbg_values > 1); |
| 1938 | if ($1 =~ /\n/ && $av_preprocessor) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1939 | $type = pop(@av_paren_type); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1940 | $av_preprocessor = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
Florian Mickler | c023e473 | 2011-01-12 16:59:58 -0800 | [diff] [blame] | 1943 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1944 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1945 | push(@av_paren_type, $type); |
Andy Whitcroft | addcdce | 2012-01-10 15:10:11 -0800 | [diff] [blame] | 1946 | $type = 'c'; |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1947 | |
Andy Whitcroft | e91b6e2 | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1948 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1949 | print "DECLARE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1950 | $type = 'T'; |
| 1951 | |
Andy Whitcroft | 389a2fe | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 1952 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 1953 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 1954 | $type = 'T'; |
| 1955 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1956 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1957 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1958 | $av_preprocessor = 1; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1959 | push(@av_paren_type, $type); |
| 1960 | if ($2 ne '') { |
| 1961 | $av_pending = 'N'; |
| 1962 | } |
| 1963 | $type = 'E'; |
| 1964 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1965 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1966 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 1967 | $av_preprocessor = 1; |
| 1968 | push(@av_paren_type, $type); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1969 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1970 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1971 | print "PRE_START($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1972 | $av_preprocessor = 1; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1973 | |
| 1974 | push(@av_paren_type, $type); |
| 1975 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1976 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1977 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1978 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1979 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 1980 | $av_preprocessor = 1; |
| 1981 | |
| 1982 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 1983 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1984 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1985 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1986 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1987 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 1988 | |
| 1989 | $av_preprocessor = 1; |
| 1990 | |
| 1991 | # Assume all arms of the conditional end as this |
| 1992 | # one does, and continue as if the #endif was not here. |
| 1993 | pop(@av_paren_type); |
| 1994 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1995 | $type = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1996 | |
| 1997 | } elsif ($cur =~ /^(\\\n)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1998 | print "PRECONT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1999 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2000 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 2001 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 2002 | $av_pending = $type; |
| 2003 | $type = 'N'; |
| 2004 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2005 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2006 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2007 | if (defined $2) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2008 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2009 | } |
| 2010 | $type = 'N'; |
| 2011 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 2012 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2013 | print "COND($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 2014 | $av_pending = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2015 | $type = 'N'; |
| 2016 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2017 | } elsif ($cur =~/^(case)/o) { |
| 2018 | print "CASE($1)\n" if ($dbg_values > 1); |
| 2019 | $av_pend_colon = 'C'; |
| 2020 | $type = 'N'; |
| 2021 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 2022 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2023 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2024 | $type = 'N'; |
| 2025 | |
| 2026 | } elsif ($cur =~ /^(\()/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2027 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2028 | push(@av_paren_type, $av_pending); |
| 2029 | $av_pending = '_'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2030 | $type = 'N'; |
| 2031 | |
| 2032 | } elsif ($cur =~ /^(\))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2033 | my $new_type = pop(@av_paren_type); |
| 2034 | if ($new_type ne '_') { |
| 2035 | $type = $new_type; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2036 | print "PAREN('$1') -> $type\n" |
| 2037 | if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2038 | } else { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2039 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2040 | } |
| 2041 | |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 2042 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2043 | print "FUNC($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 2044 | $type = 'V'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2045 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2046 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 2047 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 2048 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2049 | $av_pend_colon = 'B'; |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 2050 | } elsif ($type eq 'E') { |
| 2051 | $av_pend_colon = 'L'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2052 | } |
| 2053 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 2054 | $type = 'V'; |
| 2055 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2056 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2057 | print "IDENT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2058 | $type = 'V'; |
| 2059 | |
| 2060 | } elsif ($cur =~ /^($Assignment)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2061 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2062 | $type = 'N'; |
| 2063 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2064 | } elsif ($cur =~/^(;|{|})/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2065 | print "END($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2066 | $type = 'E'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2067 | $av_pend_colon = 'O'; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2068 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 2069 | } elsif ($cur =~/^(,)/) { |
| 2070 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 2071 | $type = 'C'; |
| 2072 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2073 | } elsif ($cur =~ /^(\?)/o) { |
| 2074 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 2075 | $type = 'N'; |
| 2076 | |
| 2077 | } elsif ($cur =~ /^(:)/o) { |
| 2078 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 2079 | |
| 2080 | substr($var, length($res), 1, $av_pend_colon); |
| 2081 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 2082 | $type = 'E'; |
| 2083 | } else { |
| 2084 | $type = 'N'; |
| 2085 | } |
| 2086 | $av_pend_colon = 'O'; |
| 2087 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 2088 | } elsif ($cur =~ /^(\[)/o) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2089 | print "CLOSE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2090 | $type = 'N'; |
| 2091 | |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 2092 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2093 | my $variant; |
| 2094 | |
| 2095 | print "OPV($1)\n" if ($dbg_values > 1); |
| 2096 | if ($type eq 'V') { |
| 2097 | $variant = 'B'; |
| 2098 | } else { |
| 2099 | $variant = 'U'; |
| 2100 | } |
| 2101 | |
| 2102 | substr($var, length($res), 1, $variant); |
| 2103 | $type = 'N'; |
| 2104 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2105 | } elsif ($cur =~ /^($Operators)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2106 | print "OP($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2107 | if ($1 ne '++' && $1 ne '--') { |
| 2108 | $type = 'N'; |
| 2109 | } |
| 2110 | |
| 2111 | } elsif ($cur =~ /(^.)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2112 | print "C($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2113 | } |
| 2114 | if (defined $1) { |
| 2115 | $cur = substr($cur, length($1)); |
| 2116 | $res .= $type x length($1); |
| 2117 | } |
| 2118 | } |
| 2119 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2120 | return ($res, $var); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2123 | sub possible { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2124 | my ($possible, $line) = @_; |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 2125 | my $notPermitted = qr{(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 2126 | ^(?: |
| 2127 | $Modifier| |
| 2128 | $Storage| |
| 2129 | $Type| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 2130 | DEFINE_\S+ |
| 2131 | )$| |
| 2132 | ^(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 2133 | goto| |
| 2134 | return| |
| 2135 | case| |
| 2136 | else| |
| 2137 | asm|__asm__| |
Andy Whitcroft | 89a8835 | 2012-01-10 15:10:00 -0800 | [diff] [blame] | 2138 | do| |
| 2139 | \#| |
| 2140 | \#\#| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 2141 | )(?:\s|$)| |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 2142 | ^(?:typedef|struct|enum)\b |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 2143 | )}x; |
| 2144 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 2145 | if ($possible !~ $notPermitted) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2146 | # Check for modifiers. |
| 2147 | $possible =~ s/\s*$Storage\s*//g; |
| 2148 | $possible =~ s/\s*$Sparse\s*//g; |
| 2149 | if ($possible =~ /^\s*$/) { |
| 2150 | |
| 2151 | } elsif ($possible =~ /\s/) { |
| 2152 | $possible =~ s/\s*$Type\s*//g; |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 2153 | for my $modifier (split(' ', $possible)) { |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 2154 | if ($modifier !~ $notPermitted) { |
| 2155 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 2156 | push(@modifierListFile, $modifier); |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 2157 | } |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 2158 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2159 | |
| 2160 | } else { |
| 2161 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 2162 | push(@typeListFile, $possible); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2163 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2164 | build_types(); |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 2165 | } else { |
| 2166 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2170 | my $prefix = ''; |
| 2171 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2172 | sub show_type { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 2173 | my ($type) = @_; |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 2174 | |
Alexey Dobriyan | 522b837 | 2017-02-27 14:30:05 -0800 | [diff] [blame] | 2175 | $type =~ tr/[a-z]/[A-Z]/; |
| 2176 | |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 2177 | return defined $use_type{$type} if (scalar keys %use_type > 0); |
| 2178 | |
| 2179 | return !defined $ignore_type{$type}; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2182 | sub report { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 2183 | my ($level, $type, $msg) = @_; |
| 2184 | |
| 2185 | if (!show_type($type) || |
| 2186 | (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2187 | return 0; |
| 2188 | } |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 2189 | my $output = ''; |
John Brooks | 737c076 | 2017-07-10 15:52:24 -0700 | [diff] [blame] | 2190 | if ($color) { |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 2191 | if ($level eq 'ERROR') { |
| 2192 | $output .= RED; |
| 2193 | } elsif ($level eq 'WARNING') { |
| 2194 | $output .= YELLOW; |
| 2195 | } else { |
| 2196 | $output .= GREEN; |
| 2197 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2198 | } |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 2199 | $output .= $prefix . $level . ':'; |
| 2200 | if ($show_types) { |
John Brooks | 737c076 | 2017-07-10 15:52:24 -0700 | [diff] [blame] | 2201 | $output .= BLUE if ($color); |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 2202 | $output .= "$type:"; |
| 2203 | } |
John Brooks | 737c076 | 2017-07-10 15:52:24 -0700 | [diff] [blame] | 2204 | $output .= RESET if ($color); |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 2205 | $output .= ' ' . $msg . "\n"; |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 2206 | |
| 2207 | if ($showfile) { |
| 2208 | my @lines = split("\n", $output, -1); |
| 2209 | splice(@lines, 1, 1); |
| 2210 | $output = join("\n", @lines); |
| 2211 | } |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 2212 | $output = (split('\n', $output))[0] . "\n" if ($terse); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2213 | |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 2214 | push(our @report, $output); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2215 | |
| 2216 | return 1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2217 | } |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 2218 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2219 | sub report_dump { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2220 | our @report; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2221 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2222 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2223 | sub fixup_current_range { |
| 2224 | my ($lineRef, $offset, $length) = @_; |
| 2225 | |
| 2226 | if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { |
| 2227 | my $o = $1; |
| 2228 | my $l = $2; |
| 2229 | my $no = $o + $offset; |
| 2230 | my $nl = $l + $length; |
| 2231 | $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | sub fix_inserted_deleted_lines { |
| 2236 | my ($linesRef, $insertedRef, $deletedRef) = @_; |
| 2237 | |
| 2238 | my $range_last_linenr = 0; |
| 2239 | my $delta_offset = 0; |
| 2240 | |
| 2241 | my $old_linenr = 0; |
| 2242 | my $new_linenr = 0; |
| 2243 | |
| 2244 | my $next_insert = 0; |
| 2245 | my $next_delete = 0; |
| 2246 | |
| 2247 | my @lines = (); |
| 2248 | |
| 2249 | my $inserted = @{$insertedRef}[$next_insert++]; |
| 2250 | my $deleted = @{$deletedRef}[$next_delete++]; |
| 2251 | |
| 2252 | foreach my $old_line (@{$linesRef}) { |
| 2253 | my $save_line = 1; |
| 2254 | my $line = $old_line; #don't modify the array |
Joe Perches | 323b267 | 2015-04-16 12:44:50 -0700 | [diff] [blame] | 2255 | if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2256 | $delta_offset = 0; |
| 2257 | } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk |
| 2258 | $range_last_linenr = $new_linenr; |
| 2259 | fixup_current_range(\$line, $delta_offset, 0); |
| 2260 | } |
| 2261 | |
| 2262 | while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { |
| 2263 | $deleted = @{$deletedRef}[$next_delete++]; |
| 2264 | $save_line = 0; |
| 2265 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); |
| 2266 | } |
| 2267 | |
| 2268 | while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { |
| 2269 | push(@lines, ${$inserted}{'LINE'}); |
| 2270 | $inserted = @{$insertedRef}[$next_insert++]; |
| 2271 | $new_linenr++; |
| 2272 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); |
| 2273 | } |
| 2274 | |
| 2275 | if ($save_line) { |
| 2276 | push(@lines, $line); |
| 2277 | $new_linenr++; |
| 2278 | } |
| 2279 | |
| 2280 | $old_linenr++; |
| 2281 | } |
| 2282 | |
| 2283 | return @lines; |
| 2284 | } |
| 2285 | |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 2286 | sub fix_insert_line { |
| 2287 | my ($linenr, $line) = @_; |
| 2288 | |
| 2289 | my $inserted = { |
| 2290 | LINENR => $linenr, |
| 2291 | LINE => $line, |
| 2292 | }; |
| 2293 | push(@fixed_inserted, $inserted); |
| 2294 | } |
| 2295 | |
| 2296 | sub fix_delete_line { |
| 2297 | my ($linenr, $line) = @_; |
| 2298 | |
| 2299 | my $deleted = { |
| 2300 | LINENR => $linenr, |
| 2301 | LINE => $line, |
| 2302 | }; |
| 2303 | |
| 2304 | push(@fixed_deleted, $deleted); |
| 2305 | } |
| 2306 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2307 | sub ERROR { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 2308 | my ($type, $msg) = @_; |
| 2309 | |
| 2310 | if (report("ERROR", $type, $msg)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2311 | our $clean = 0; |
| 2312 | our $cnt_error++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2313 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2314 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2315 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2316 | } |
| 2317 | sub WARN { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 2318 | my ($type, $msg) = @_; |
| 2319 | |
| 2320 | if (report("WARNING", $type, $msg)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2321 | our $clean = 0; |
| 2322 | our $cnt_warn++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2323 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2324 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2325 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2326 | } |
| 2327 | sub CHK { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 2328 | my ($type, $msg) = @_; |
| 2329 | |
| 2330 | if ($check && report("CHECK", $type, $msg)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2331 | our $clean = 0; |
| 2332 | our $cnt_chk++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2333 | return 1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2334 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2335 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2336 | } |
| 2337 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2338 | sub check_absolute_file { |
| 2339 | my ($absolute, $herecurr) = @_; |
| 2340 | my $file = $absolute; |
| 2341 | |
| 2342 | ##print "absolute<$absolute>\n"; |
| 2343 | |
| 2344 | # See if any suffix of this path is a path within the tree. |
| 2345 | while ($file =~ s@^[^/]*/@@) { |
| 2346 | if (-f "$root/$file") { |
| 2347 | ##print "file<$file>\n"; |
| 2348 | last; |
| 2349 | } |
| 2350 | } |
| 2351 | if (! -f _) { |
| 2352 | return 0; |
| 2353 | } |
| 2354 | |
| 2355 | # It is, so see if the prefix is acceptable. |
| 2356 | my $prefix = $absolute; |
| 2357 | substr($prefix, -length($file)) = ''; |
| 2358 | |
| 2359 | ##print "prefix<$prefix>\n"; |
| 2360 | if ($prefix ne ".../") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2361 | WARN("USE_RELATIVE_PATH", |
| 2362 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2363 | } |
| 2364 | } |
| 2365 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2366 | sub trim { |
| 2367 | my ($string) = @_; |
| 2368 | |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2369 | $string =~ s/^\s+|\s+$//g; |
| 2370 | |
| 2371 | return $string; |
| 2372 | } |
| 2373 | |
| 2374 | sub ltrim { |
| 2375 | my ($string) = @_; |
| 2376 | |
| 2377 | $string =~ s/^\s+//; |
| 2378 | |
| 2379 | return $string; |
| 2380 | } |
| 2381 | |
| 2382 | sub rtrim { |
| 2383 | my ($string) = @_; |
| 2384 | |
| 2385 | $string =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2386 | |
| 2387 | return $string; |
| 2388 | } |
| 2389 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 2390 | sub string_find_replace { |
| 2391 | my ($string, $find, $replace) = @_; |
| 2392 | |
| 2393 | $string =~ s/$find/$replace/g; |
| 2394 | |
| 2395 | return $string; |
| 2396 | } |
| 2397 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2398 | sub tabify { |
| 2399 | my ($leading) = @_; |
| 2400 | |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 2401 | my $source_indent = $tabsize; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2402 | my $max_spaces_before_tab = $source_indent - 1; |
| 2403 | my $spaces_to_tab = " " x $source_indent; |
| 2404 | |
| 2405 | #convert leading spaces to tabs |
| 2406 | 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; |
| 2407 | #Remove spaces before a tab |
| 2408 | 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; |
| 2409 | |
| 2410 | return "$leading"; |
| 2411 | } |
| 2412 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2413 | sub pos_last_openparen { |
| 2414 | my ($line) = @_; |
| 2415 | |
| 2416 | my $pos = 0; |
| 2417 | |
| 2418 | my $opens = $line =~ tr/\(/\(/; |
| 2419 | my $closes = $line =~ tr/\)/\)/; |
| 2420 | |
| 2421 | my $last_openparen = 0; |
| 2422 | |
| 2423 | if (($opens == 0) || ($closes >= $opens)) { |
| 2424 | return -1; |
| 2425 | } |
| 2426 | |
| 2427 | my $len = length($line); |
| 2428 | |
| 2429 | for ($pos = 0; $pos < $len; $pos++) { |
| 2430 | my $string = substr($line, $pos); |
| 2431 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 2432 | $pos += length($1) - 1; |
| 2433 | } elsif (substr($line, $pos, 1) eq '(') { |
| 2434 | $last_openparen = $pos; |
| 2435 | } elsif (index($string, '(') == -1) { |
| 2436 | last; |
| 2437 | } |
| 2438 | } |
| 2439 | |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 2440 | return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
Joe Perches | f36d3eb | 2020-04-06 20:10:58 -0700 | [diff] [blame] | 2443 | sub get_raw_comment { |
| 2444 | my ($line, $rawline) = @_; |
| 2445 | my $comment = ''; |
| 2446 | |
| 2447 | for my $i (0 .. (length($line) - 1)) { |
| 2448 | if (substr($line, $i, 1) eq "$;") { |
| 2449 | $comment .= substr($rawline, $i, 1); |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | return $comment; |
| 2454 | } |
| 2455 | |
Song Liu | 5b8f82e | 2021-02-25 17:22:08 -0800 | [diff] [blame] | 2456 | sub exclude_global_initialisers { |
| 2457 | my ($realfile) = @_; |
| 2458 | |
| 2459 | # Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c). |
| 2460 | return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ || |
| 2461 | $realfile =~ m@^samples/bpf/.*_kern\.c$@ || |
| 2462 | $realfile =~ m@/bpf/.*\.bpf\.c$@; |
| 2463 | } |
| 2464 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2465 | sub process { |
| 2466 | my $filename = shift; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2467 | |
| 2468 | my $linenr=0; |
| 2469 | my $prevline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2470 | my $prevrawline=""; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2471 | my $stashline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2472 | my $stashrawline=""; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2473 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2474 | my $length; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2475 | my $indent; |
| 2476 | my $previndent=0; |
| 2477 | my $stashindent=0; |
| 2478 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2479 | our $clean = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2480 | my $signoff = 0; |
Geert Uytterhoeven | cd26149 | 2018-08-21 21:57:40 -0700 | [diff] [blame] | 2481 | my $author = ''; |
| 2482 | my $authorsignoff = 0; |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 2483 | my $author_sob = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2484 | my $is_patch = 0; |
Rob Herring | 133712a | 2018-08-21 21:58:16 -0700 | [diff] [blame] | 2485 | my $is_binding_patch = -1; |
Joe Perches | 29ee1b0 | 2014-08-06 16:10:35 -0700 | [diff] [blame] | 2486 | my $in_header_lines = $file ? 0 : 1; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2487 | my $in_commit_log = 0; #Scanning lines before patch |
Joe Perches | 44d303e | 2020-04-06 20:11:10 -0700 | [diff] [blame] | 2488 | my $has_patch_separator = 0; #Found a --- line |
Allen Hubbe | ed43c4e | 2016-08-02 14:04:45 -0700 | [diff] [blame] | 2489 | my $has_commit_log = 0; #Encountered lines before patch |
Joe Perches | 490b292 | 2018-08-21 21:58:01 -0700 | [diff] [blame] | 2490 | my $commit_log_lines = 0; #Number of commit log lines |
Joe Perches | 77cb854 | 2017-02-24 15:01:28 -0800 | [diff] [blame] | 2491 | my $commit_log_possible_stack_dump = 0; |
Joe Perches | 2a076f4 | 2015-04-16 12:44:28 -0700 | [diff] [blame] | 2492 | my $commit_log_long_line = 0; |
Joe Perches | e518e9a | 2015-06-25 15:03:27 -0700 | [diff] [blame] | 2493 | my $commit_log_has_diff = 0; |
Joe Perches | 13f1937 | 2014-08-06 16:10:59 -0700 | [diff] [blame] | 2494 | my $reported_maintainer_file = 0; |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 2495 | my $non_utf8_charset = 0; |
| 2496 | |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 2497 | my $last_blank_line = 0; |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 2498 | my $last_coalesced_string_linenr = -1; |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 2499 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2500 | our @report = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2501 | our $cnt_lines = 0; |
| 2502 | our $cnt_error = 0; |
| 2503 | our $cnt_warn = 0; |
| 2504 | our $cnt_chk = 0; |
| 2505 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2506 | # Trace the real file/line as we go. |
| 2507 | my $realfile = ''; |
| 2508 | my $realline = 0; |
| 2509 | my $realcnt = 0; |
| 2510 | my $here = ''; |
Joe Perches | 77cb854 | 2017-02-24 15:01:28 -0800 | [diff] [blame] | 2511 | my $context_function; #undef'd unless there's a known function |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2512 | my $in_comment = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2513 | my $comment_edge = 0; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2514 | my $first_line = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2515 | my $p1_prefix = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2516 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2517 | my $prev_values = 'E'; |
| 2518 | |
| 2519 | # suppression flags |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2520 | my %suppress_ifbraces; |
Andy Whitcroft | 170d3a22 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2521 | my %suppress_whiletrailers; |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2522 | my %suppress_export; |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2523 | my $suppress_statement = 0; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2524 | |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 2525 | my %signatures = (); |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 2526 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2527 | # Pre-scan the patch sanitizing the lines. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2528 | # Pre-scan the patch looking for any __setup documentation. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2529 | # |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2530 | my @setup_docs = (); |
| 2531 | my $setup_docs = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2532 | |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 2533 | my $camelcase_file_seeded = 0; |
| 2534 | |
Rob Herring | 9f3a899 | 2018-04-10 16:33:13 -0700 | [diff] [blame] | 2535 | my $checklicenseline = 1; |
| 2536 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2537 | sanitise_line_reset(); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2538 | my $line; |
| 2539 | foreach my $rawline (@rawlines) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2540 | $linenr++; |
| 2541 | $line = $rawline; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2542 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2543 | push(@fixed, $rawline) if ($fix); |
| 2544 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2545 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2546 | $setup_docs = 0; |
Tim Froidcoeur | 2581ac7 | 2020-06-10 18:41:38 -0700 | [diff] [blame] | 2547 | if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2548 | $setup_docs = 1; |
| 2549 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2550 | #next; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2551 | } |
Joe Perches | 74fd4f3 | 2017-05-08 15:56:02 -0700 | [diff] [blame] | 2552 | if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2553 | $realline=$1-1; |
| 2554 | if (defined $2) { |
| 2555 | $realcnt=$3+1; |
| 2556 | } else { |
| 2557 | $realcnt=1+1; |
| 2558 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2559 | $in_comment = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2560 | |
| 2561 | # Guestimate if this is a continuing comment. Run |
| 2562 | # the context looking for a comment "edge". If this |
| 2563 | # edge is a close comment then we must be in a comment |
| 2564 | # at context start. |
| 2565 | my $edge; |
Andy Whitcroft | 01fa914 | 2008-10-15 22:02:19 -0700 | [diff] [blame] | 2566 | my $cnt = $realcnt; |
| 2567 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 2568 | next if (defined $rawlines[$ln - 1] && |
| 2569 | $rawlines[$ln - 1] =~ /^-/); |
| 2570 | $cnt--; |
| 2571 | #print "RAW<$rawlines[$ln - 1]>\n"; |
Andy Whitcroft | 721c1cb | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 2572 | last if (!defined $rawlines[$ln - 1]); |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 2573 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 2574 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 2575 | ($edge) = $1; |
| 2576 | last; |
| 2577 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2578 | } |
| 2579 | if (defined $edge && $edge eq '*/') { |
| 2580 | $in_comment = 1; |
| 2581 | } |
| 2582 | |
| 2583 | # Guestimate if this is a continuing comment. If this |
| 2584 | # is the start of a diff block and this line starts |
| 2585 | # ' *' then it is very likely a comment. |
| 2586 | if (!defined $edge && |
Andy Whitcroft | 83242e0 | 2009-01-06 14:41:17 -0800 | [diff] [blame] | 2587 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2588 | { |
| 2589 | $in_comment = 1; |
| 2590 | } |
| 2591 | |
| 2592 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 2593 | sanitise_line_reset($in_comment); |
| 2594 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2595 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2596 | # Standardise the strings and chars within the input to |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2597 | # simplify matching -- only bother with positive lines. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2598 | $line = sanitise_line($rawline); |
| 2599 | } |
| 2600 | push(@lines, $line); |
| 2601 | |
| 2602 | if ($realcnt > 1) { |
| 2603 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 2604 | } else { |
| 2605 | $realcnt = 0; |
| 2606 | } |
| 2607 | |
| 2608 | #print "==>$rawline\n"; |
| 2609 | #print "-->$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2610 | |
| 2611 | if ($setup_docs && $line =~ /^\+/) { |
| 2612 | push(@setup_docs, $line); |
| 2613 | } |
| 2614 | } |
| 2615 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2616 | $prefix = ''; |
| 2617 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2618 | $realcnt = 0; |
| 2619 | $linenr = 0; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2620 | $fixlinenr = -1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2621 | foreach my $line (@lines) { |
| 2622 | $linenr++; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2623 | $fixlinenr++; |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 2624 | my $sline = $line; #copy of $line |
| 2625 | $sline =~ s/$;/ /g; #with comments as spaces |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2626 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2627 | my $rawline = $rawlines[$linenr - 1]; |
Joe Perches | f36d3eb | 2020-04-06 20:10:58 -0700 | [diff] [blame] | 2628 | my $raw_comment = get_raw_comment($line, $rawline); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2629 | |
Joe Perches | 12c253ab | 2018-06-07 17:10:58 -0700 | [diff] [blame] | 2630 | # check if it's a mode change, rename or start of a patch |
| 2631 | if (!$in_commit_log && |
| 2632 | ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ || |
| 2633 | ($line =~ /^rename (?:from|to) \S+\s*$/ || |
| 2634 | $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) { |
| 2635 | $is_patch = 1; |
| 2636 | } |
| 2637 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2638 | #extract the line range in the file after the patch is applied |
Joe Perches | e518e9a | 2015-06-25 15:03:27 -0700 | [diff] [blame] | 2639 | if (!$in_commit_log && |
Joe Perches | 74fd4f3 | 2017-05-08 15:56:02 -0700 | [diff] [blame] | 2640 | $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) { |
| 2641 | my $context = $4; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2642 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2643 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2644 | $realline=$1-1; |
| 2645 | if (defined $2) { |
| 2646 | $realcnt=$3+1; |
| 2647 | } else { |
| 2648 | $realcnt=1+1; |
| 2649 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2650 | annotate_reset(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2651 | $prev_values = 'E'; |
| 2652 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2653 | %suppress_ifbraces = (); |
Andy Whitcroft | 170d3a22 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2654 | %suppress_whiletrailers = (); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2655 | %suppress_export = (); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2656 | $suppress_statement = 0; |
Joe Perches | 74fd4f3 | 2017-05-08 15:56:02 -0700 | [diff] [blame] | 2657 | if ($context =~ /\b(\w+)\s*\(/) { |
| 2658 | $context_function = $1; |
| 2659 | } else { |
| 2660 | undef $context_function; |
| 2661 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2662 | next; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2663 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2664 | # track the line number as we move through the hunk, note that |
| 2665 | # new versions of GNU diff omit the leading space on completely |
| 2666 | # blank context lines so we need to count that too. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2667 | } elsif ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2668 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2669 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2670 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2671 | # Measure the line length and indent. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2672 | ($length, $indent) = line_stats($rawline); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2673 | |
| 2674 | # Track the previous line. |
| 2675 | ($prevline, $stashline) = ($stashline, $line); |
| 2676 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2677 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 2678 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2679 | #warn "line<$line>\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2680 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2681 | } elsif ($realcnt == 1) { |
| 2682 | $realcnt--; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
Andy Whitcroft | cc77cdc | 2009-10-26 16:50:13 -0700 | [diff] [blame] | 2685 | my $hunk_line = ($realcnt != 0); |
| 2686 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2687 | $here = "#$linenr: " if (!$file); |
| 2688 | $here = "#$realline: " if ($file); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2689 | |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2690 | my $found_file = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2691 | # extract the filename as it passes |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2692 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 2693 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 2694 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 2695 | $in_commit_log = 0; |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2696 | $found_file = 1; |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2697 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2698 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 2699 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 2700 | $in_commit_log = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2701 | |
| 2702 | $p1_prefix = $1; |
Andy Whitcroft | e2f7aa4 | 2009-02-27 14:03:06 -0800 | [diff] [blame] | 2703 | if (!$file && $tree && $p1_prefix ne '' && |
| 2704 | -e "$root/$p1_prefix") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2705 | WARN("PATCH_PREFIX", |
| 2706 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2707 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2708 | |
Andy Whitcroft | c1ab332 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2709 | if ($realfile =~ m@^include/asm/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2710 | ERROR("MODIFIED_INCLUDE_ASM", |
| 2711 | "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2712 | } |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2713 | $found_file = 1; |
| 2714 | } |
| 2715 | |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 2716 | #make up the handle for any error we report on this line |
| 2717 | if ($showfile) { |
| 2718 | $prefix = "$realfile:$realline: " |
| 2719 | } elsif ($emacs) { |
Joe Perches | 7d3a9f6 | 2015-09-09 15:37:39 -0700 | [diff] [blame] | 2720 | if ($file) { |
| 2721 | $prefix = "$filename:$realline: "; |
| 2722 | } else { |
| 2723 | $prefix = "$filename:$linenr: "; |
| 2724 | } |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 2725 | } |
| 2726 | |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2727 | if ($found_file) { |
Joe Perches | 85b0ee1 | 2016-10-11 13:51:44 -0700 | [diff] [blame] | 2728 | if (is_maintained_obsolete($realfile)) { |
| 2729 | WARN("OBSOLETE", |
| 2730 | "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n"); |
| 2731 | } |
Joe Perches | 7bd7e48 | 2015-09-09 15:37:44 -0700 | [diff] [blame] | 2732 | if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2733 | $check = 1; |
| 2734 | } else { |
| 2735 | $check = $check_orig; |
| 2736 | } |
Rob Herring | 9f3a899 | 2018-04-10 16:33:13 -0700 | [diff] [blame] | 2737 | $checklicenseline = 1; |
Rob Herring | 133712a | 2018-08-21 21:58:16 -0700 | [diff] [blame] | 2738 | |
| 2739 | if ($realfile !~ /^MAINTAINERS/) { |
| 2740 | my $last_binding_patch = $is_binding_patch; |
| 2741 | |
| 2742 | $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@; |
| 2743 | |
| 2744 | if (($last_binding_patch != -1) && |
| 2745 | ($last_binding_patch ^ $is_binding_patch)) { |
| 2746 | WARN("DT_SPLIT_BINDING_PATCH", |
Mauro Carvalho Chehab | 858e684 | 2020-04-15 16:45:25 +0200 | [diff] [blame] | 2747 | "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n"); |
Rob Herring | 133712a | 2018-08-21 21:58:16 -0700 | [diff] [blame] | 2748 | } |
| 2749 | } |
| 2750 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2751 | next; |
| 2752 | } |
| 2753 | |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 2754 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2755 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2756 | my $hereline = "$here\n$rawline\n"; |
| 2757 | my $herecurr = "$here\n$rawline\n"; |
| 2758 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2759 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2760 | $cnt_lines++ if ($realcnt != 0); |
| 2761 | |
Joe Perches | 490b292 | 2018-08-21 21:58:01 -0700 | [diff] [blame] | 2762 | # Verify the existence of a commit log if appropriate |
| 2763 | # 2 is used because a $signature is counted in $commit_log_lines |
| 2764 | if ($in_commit_log) { |
| 2765 | if ($line !~ /^\s*$/) { |
| 2766 | $commit_log_lines++; #could be a $signature |
| 2767 | } |
| 2768 | } elsif ($has_commit_log && $commit_log_lines < 2) { |
| 2769 | WARN("COMMIT_MESSAGE", |
| 2770 | "Missing commit description - Add an appropriate one\n"); |
| 2771 | $commit_log_lines = 2; #warn only once |
| 2772 | } |
| 2773 | |
Joe Perches | e518e9a | 2015-06-25 15:03:27 -0700 | [diff] [blame] | 2774 | # Check if the commit log has what seems like a diff which can confuse patch |
| 2775 | if ($in_commit_log && !$commit_log_has_diff && |
Mrinal Pandey | 13e4541 | 2020-09-04 16:35:52 -0700 | [diff] [blame] | 2776 | (($line =~ m@^\s+diff\b.*a/([\w/]+)@ && |
| 2777 | $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) || |
Joe Perches | e518e9a | 2015-06-25 15:03:27 -0700 | [diff] [blame] | 2778 | $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || |
| 2779 | $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { |
| 2780 | ERROR("DIFF_IN_COMMIT_MSG", |
| 2781 | "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); |
| 2782 | $commit_log_has_diff = 1; |
| 2783 | } |
| 2784 | |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2785 | # Check for incorrect file permissions |
| 2786 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 2787 | my $permhere = $here . "FILE: $realfile\n"; |
Joe Perches | 04db4d2 | 2013-04-29 16:18:14 -0700 | [diff] [blame] | 2788 | if ($realfile !~ m@scripts/@ && |
| 2789 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2790 | ERROR("EXECUTE_PERMISSIONS", |
| 2791 | "do not set execute permissions for source files\n" . $permhere); |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2792 | } |
| 2793 | } |
| 2794 | |
Geert Uytterhoeven | cd26149 | 2018-08-21 21:57:40 -0700 | [diff] [blame] | 2795 | # Check the patch for a From: |
| 2796 | if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) { |
| 2797 | $author = $1; |
Dwaipayan Ray | e7f929f | 2020-10-15 20:12:15 -0700 | [diff] [blame] | 2798 | my $curline = $linenr; |
| 2799 | while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) { |
| 2800 | $author .= $1; |
| 2801 | } |
Geert Uytterhoeven | cd26149 | 2018-08-21 21:57:40 -0700 | [diff] [blame] | 2802 | $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i); |
| 2803 | $author =~ s/"//g; |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 2804 | $author = reformat_email($author); |
Geert Uytterhoeven | cd26149 | 2018-08-21 21:57:40 -0700 | [diff] [blame] | 2805 | } |
| 2806 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2807 | # Check the patch for a signoff: |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 2808 | if ($line =~ /^\s*signed-off-by:\s*(.*)/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2809 | $signoff++; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2810 | $in_commit_log = 0; |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 2811 | if ($author ne '' && $authorsignoff != 1) { |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 2812 | if (same_email_addresses($1, $author)) { |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 2813 | $authorsignoff = 1; |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 2814 | } else { |
| 2815 | my $ctx = $1; |
| 2816 | my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx); |
| 2817 | my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author); |
| 2818 | |
| 2819 | if ($email_address eq $author_address && $email_name eq $author_name) { |
| 2820 | $author_sob = $ctx; |
| 2821 | $authorsignoff = 2; |
| 2822 | } elsif ($email_address eq $author_address) { |
| 2823 | $author_sob = $ctx; |
| 2824 | $authorsignoff = 3; |
| 2825 | } elsif ($email_name eq $author_name) { |
| 2826 | $author_sob = $ctx; |
| 2827 | $authorsignoff = 4; |
| 2828 | |
| 2829 | my $address1 = $email_address; |
| 2830 | my $address2 = $author_address; |
| 2831 | |
| 2832 | if ($address1 =~ /(\S+)\+\S+(\@.*)/) { |
| 2833 | $address1 = "$1$2"; |
| 2834 | } |
| 2835 | if ($address2 =~ /(\S+)\+\S+(\@.*)/) { |
| 2836 | $address2 = "$1$2"; |
| 2837 | } |
| 2838 | if ($address1 eq $address2) { |
| 2839 | $authorsignoff = 5; |
| 2840 | } |
| 2841 | } |
Geert Uytterhoeven | cd26149 | 2018-08-21 21:57:40 -0700 | [diff] [blame] | 2842 | } |
| 2843 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2844 | } |
| 2845 | |
Joe Perches | 44d303e | 2020-04-06 20:11:10 -0700 | [diff] [blame] | 2846 | # Check for patch separator |
| 2847 | if ($line =~ /^---$/) { |
| 2848 | $has_patch_separator = 1; |
| 2849 | $in_commit_log = 0; |
| 2850 | } |
| 2851 | |
Joe Perches | e0d975b | 2014-12-10 15:51:49 -0800 | [diff] [blame] | 2852 | # Check if MAINTAINERS is being updated. If so, there's probably no need to |
| 2853 | # emit the "does MAINTAINERS need updating?" message on file add/move/delete |
| 2854 | if ($line =~ /^\s*MAINTAINERS\s*\|/) { |
| 2855 | $reported_maintainer_file = 1; |
| 2856 | } |
| 2857 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2858 | # Check signature styles |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 2859 | if (!$in_header_lines && |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 2860 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2861 | my $space_before = $1; |
| 2862 | my $sign_off = $2; |
| 2863 | my $space_after = $3; |
| 2864 | my $email = $4; |
| 2865 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 2866 | |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 2867 | if ($sign_off !~ /$signature_tags/) { |
Aditya Srivastava | 831242a | 2020-12-15 20:45:12 -0800 | [diff] [blame] | 2868 | my $suggested_signature = find_standard_signature($sign_off); |
| 2869 | if ($suggested_signature eq "") { |
| 2870 | WARN("BAD_SIGN_OFF", |
| 2871 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 2872 | } else { |
| 2873 | if (WARN("BAD_SIGN_OFF", |
| 2874 | "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) && |
| 2875 | $fix) { |
| 2876 | $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/; |
| 2877 | } |
| 2878 | } |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 2879 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2880 | if (defined $space_before && $space_before ne "") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2881 | if (WARN("BAD_SIGN_OFF", |
| 2882 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && |
| 2883 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2884 | $fixed[$fixlinenr] = |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2885 | "$ucfirst_sign_off $email"; |
| 2886 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2887 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2888 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2889 | if (WARN("BAD_SIGN_OFF", |
| 2890 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && |
| 2891 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2892 | $fixed[$fixlinenr] = |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2893 | "$ucfirst_sign_off $email"; |
| 2894 | } |
| 2895 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2896 | } |
| 2897 | if (!defined $space_after || $space_after ne " ") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2898 | if (WARN("BAD_SIGN_OFF", |
| 2899 | "Use a single space after $ucfirst_sign_off\n" . $herecurr) && |
| 2900 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2901 | $fixed[$fixlinenr] = |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2902 | "$ucfirst_sign_off $email"; |
| 2903 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2904 | } |
| 2905 | |
Joe Perches | dfa05c2 | 2020-04-06 20:10:48 -0700 | [diff] [blame] | 2906 | my ($email_name, $name_comment, $email_address, $comment) = parse_email($email); |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 2907 | my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment)); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2908 | if ($suggested_email eq "") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2909 | ERROR("BAD_SIGN_OFF", |
| 2910 | "Unrecognized email address: '$email'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2911 | } else { |
| 2912 | my $dequoted = $suggested_email; |
| 2913 | $dequoted =~ s/^"//; |
| 2914 | $dequoted =~ s/" </ </; |
| 2915 | # Don't force email to have quotes |
| 2916 | # Allow just an angle bracketed address |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 2917 | if (!same_email_addresses($email, $suggested_email)) { |
| 2918 | if (WARN("BAD_SIGN_OFF", |
| 2919 | "email address '$email' might be better as '$suggested_email'\n" . $herecurr) && |
| 2920 | $fix) { |
| 2921 | $fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/; |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | # Address part shouldn't have comments |
| 2926 | my $stripped_address = $email_address; |
| 2927 | $stripped_address =~ s/\([^\(\)]*\)//g; |
| 2928 | if ($email_address ne $stripped_address) { |
| 2929 | if (WARN("BAD_SIGN_OFF", |
| 2930 | "address part of email should not have comments: '$email_address'\n" . $herecurr) && |
| 2931 | $fix) { |
| 2932 | $fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/; |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | # Only one name comment should be allowed |
| 2937 | my $comment_count = () = $name_comment =~ /\([^\)]+\)/g; |
| 2938 | if ($comment_count > 1) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2939 | WARN("BAD_SIGN_OFF", |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 2940 | "Use a single name comment in email: '$email'\n" . $herecurr); |
| 2941 | } |
| 2942 | |
| 2943 | |
| 2944 | # stable@vger.kernel.org or stable@kernel.org shouldn't |
Dwaipayan Ray | e73d271 | 2020-12-15 20:44:56 -0800 | [diff] [blame] | 2945 | # have an email name. In addition comments should strictly |
Dwaipayan Ray | fccaebf | 2020-12-15 20:44:53 -0800 | [diff] [blame] | 2946 | # begin with a # |
| 2947 | if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) { |
| 2948 | if (($comment ne "" && $comment !~ /^#.+/) || |
| 2949 | ($email_name ne "")) { |
| 2950 | my $cur_name = $email_name; |
| 2951 | my $new_comment = $comment; |
| 2952 | $cur_name =~ s/[a-zA-Z\s\-\"]+//g; |
| 2953 | |
| 2954 | # Remove brackets enclosing comment text |
| 2955 | # and # from start of comments to get comment text |
| 2956 | $new_comment =~ s/^\((.*)\)$/$1/; |
| 2957 | $new_comment =~ s/^\[(.*)\]$/$1/; |
| 2958 | $new_comment =~ s/^[\s\#]+|\s+$//g; |
| 2959 | |
| 2960 | $new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment); |
| 2961 | $new_comment = " # $new_comment" if ($new_comment ne ""); |
| 2962 | my $new_email = "$email_address$new_comment"; |
| 2963 | |
| 2964 | if (WARN("BAD_STABLE_ADDRESS_STYLE", |
| 2965 | "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) && |
| 2966 | $fix) { |
| 2967 | $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; |
| 2968 | } |
| 2969 | } |
| 2970 | } elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) { |
| 2971 | my $new_comment = $comment; |
| 2972 | |
| 2973 | # Extract comment text from within brackets or |
| 2974 | # c89 style /*...*/ comments |
| 2975 | $new_comment =~ s/^\[(.*)\]$/$1/; |
| 2976 | $new_comment =~ s/^\/\*(.*)\*\/$/$1/; |
| 2977 | |
| 2978 | $new_comment = trim($new_comment); |
| 2979 | $new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo |
| 2980 | $new_comment = "($new_comment)" if ($new_comment ne ""); |
| 2981 | my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment); |
| 2982 | |
| 2983 | if (WARN("BAD_SIGN_OFF", |
| 2984 | "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) && |
| 2985 | $fix) { |
| 2986 | $fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/; |
| 2987 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2988 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2989 | } |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 2990 | |
| 2991 | # Check for duplicate signatures |
| 2992 | my $sig_nospace = $line; |
| 2993 | $sig_nospace =~ s/\s//g; |
| 2994 | $sig_nospace = lc($sig_nospace); |
| 2995 | if (defined $signatures{$sig_nospace}) { |
| 2996 | WARN("BAD_SIGN_OFF", |
| 2997 | "Duplicate signature\n" . $herecurr); |
| 2998 | } else { |
| 2999 | $signatures{$sig_nospace} = 1; |
| 3000 | } |
Sean Christopherson | 6c5d24e | 2019-03-22 14:11:37 -0700 | [diff] [blame] | 3001 | |
| 3002 | # Check Co-developed-by: immediately followed by Signed-off-by: with same name and email |
| 3003 | if ($sign_off =~ /^co-developed-by:$/i) { |
| 3004 | if ($email eq $author) { |
| 3005 | WARN("BAD_SIGN_OFF", |
| 3006 | "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline); |
| 3007 | } |
| 3008 | if (!defined $lines[$linenr]) { |
| 3009 | WARN("BAD_SIGN_OFF", |
Dwaipayan Ray | ea7dbab | 2021-02-25 17:21:47 -0800 | [diff] [blame] | 3010 | "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline); |
Sean Christopherson | 6c5d24e | 2019-03-22 14:11:37 -0700 | [diff] [blame] | 3011 | } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) { |
| 3012 | WARN("BAD_SIGN_OFF", |
| 3013 | "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); |
| 3014 | } elsif ($1 ne $email) { |
| 3015 | WARN("BAD_SIGN_OFF", |
| 3016 | "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]); |
| 3017 | } |
| 3018 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3019 | } |
| 3020 | |
Joe Perches | a2fe16b | 2015-02-13 14:39:02 -0800 | [diff] [blame] | 3021 | # Check email subject for common tools that don't need to be mentioned |
| 3022 | if ($in_header_lines && |
| 3023 | $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { |
| 3024 | WARN("EMAIL_SUBJECT", |
| 3025 | "A patch subject line should describe the change not the tool that found it\n" . $herecurr); |
| 3026 | } |
| 3027 | |
Joe Perches | 44d303e | 2020-04-06 20:11:10 -0700 | [diff] [blame] | 3028 | # Check for Gerrit Change-Ids not in any patch context |
| 3029 | if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) { |
Aditya Srivastava | 7580c5b | 2020-12-15 20:44:47 -0800 | [diff] [blame] | 3030 | if (ERROR("GERRIT_CHANGE_ID", |
| 3031 | "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) && |
| 3032 | $fix) { |
Dwaipayan Ray | ea7dbab | 2021-02-25 17:21:47 -0800 | [diff] [blame] | 3033 | fix_delete_line($fixlinenr, $rawline); |
| 3034 | } |
Christopher Covington | 7ebd05e | 2014-04-03 14:49:31 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 3037 | # Check if the commit log is in a possible stack dump |
| 3038 | if ($in_commit_log && !$commit_log_possible_stack_dump && |
| 3039 | ($line =~ /^\s*(?:WARNING:|BUG:)/ || |
| 3040 | $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || |
| 3041 | # timestamp |
Joe Perches | 634cffc | 2019-09-25 16:46:32 -0700 | [diff] [blame] | 3042 | $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) || |
| 3043 | $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ || |
| 3044 | $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) { |
| 3045 | # stack dump address styles |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 3046 | $commit_log_possible_stack_dump = 1; |
| 3047 | } |
| 3048 | |
Joe Perches | 2a076f4 | 2015-04-16 12:44:28 -0700 | [diff] [blame] | 3049 | # Check for line lengths > 75 in commit log, warn once |
| 3050 | if ($in_commit_log && !$commit_log_long_line && |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 3051 | length($line) > 75 && |
| 3052 | !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || |
| 3053 | # file delta changes |
| 3054 | $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || |
| 3055 | # filename then : |
Aditya Srivastava | 27b379a | 2020-12-15 20:44:59 -0800 | [diff] [blame] | 3056 | $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i || |
| 3057 | # A Fixes: or Link: line or signature tag line |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 3058 | $commit_log_possible_stack_dump)) { |
Joe Perches | 2a076f4 | 2015-04-16 12:44:28 -0700 | [diff] [blame] | 3059 | WARN("COMMIT_LOG_LONG_LINE", |
| 3060 | "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); |
| 3061 | $commit_log_long_line = 1; |
| 3062 | } |
| 3063 | |
Joe Perches | bf4daf1 | 2015-09-09 15:37:50 -0700 | [diff] [blame] | 3064 | # Reset possible stack dump if a blank line is found |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 3065 | if ($in_commit_log && $commit_log_possible_stack_dump && |
| 3066 | $line =~ /^\s*$/) { |
| 3067 | $commit_log_possible_stack_dump = 0; |
| 3068 | } |
Joe Perches | bf4daf1 | 2015-09-09 15:37:50 -0700 | [diff] [blame] | 3069 | |
Dwaipayan Ray | 084a617 | 2020-12-15 20:45:18 -0800 | [diff] [blame] | 3070 | # Check for lines starting with a # |
| 3071 | if ($in_commit_log && $line =~ /^#/) { |
| 3072 | if (WARN("COMMIT_COMMENT_SYMBOL", |
| 3073 | "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) && |
| 3074 | $fix) { |
| 3075 | $fixed[$fixlinenr] =~ s/^/ /; |
| 3076 | } |
| 3077 | } |
| 3078 | |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 3079 | # Check for git id commit length and improperly formed commit descriptions |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 3080 | if ($in_commit_log && !$commit_log_possible_stack_dump && |
John Hubbard | a897257 | 2020-04-06 20:10:55 -0700 | [diff] [blame] | 3081 | $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i && |
Wei Wang | e882dbf | 2017-05-08 15:55:54 -0700 | [diff] [blame] | 3082 | $line !~ /^This reverts commit [0-9a-f]{7,40}/ && |
Joe Perches | fe043ea | 2015-09-09 15:37:25 -0700 | [diff] [blame] | 3083 | ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || |
Joe Perches | aab38f5 | 2016-08-02 14:04:36 -0700 | [diff] [blame] | 3084 | ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i && |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 3085 | $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && |
| 3086 | $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { |
Joe Perches | fe043ea | 2015-09-09 15:37:25 -0700 | [diff] [blame] | 3087 | my $init_char = "c"; |
| 3088 | my $orig_commit = ""; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 3089 | my $short = 1; |
| 3090 | my $long = 0; |
| 3091 | my $case = 1; |
| 3092 | my $space = 1; |
| 3093 | my $hasdesc = 0; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 3094 | my $hasparens = 0; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 3095 | my $id = '0123456789ab'; |
| 3096 | my $orig_desc = "commit description"; |
| 3097 | my $description = ""; |
| 3098 | |
Joe Perches | fe043ea | 2015-09-09 15:37:25 -0700 | [diff] [blame] | 3099 | if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { |
| 3100 | $init_char = $1; |
| 3101 | $orig_commit = lc($2); |
| 3102 | } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { |
| 3103 | $orig_commit = lc($1); |
| 3104 | } |
| 3105 | |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 3106 | $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); |
| 3107 | $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); |
| 3108 | $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); |
| 3109 | $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); |
| 3110 | if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { |
| 3111 | $orig_desc = $1; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 3112 | $hasparens = 1; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 3113 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && |
| 3114 | defined $rawlines[$linenr] && |
| 3115 | $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { |
| 3116 | $orig_desc = $1; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 3117 | $hasparens = 1; |
Joe Perches | b671fde | 2015-02-13 14:38:41 -0800 | [diff] [blame] | 3118 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && |
| 3119 | defined $rawlines[$linenr] && |
| 3120 | $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { |
| 3121 | $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; |
| 3122 | $orig_desc = $1; |
| 3123 | $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; |
| 3124 | $orig_desc .= " " . $1; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 3125 | $hasparens = 1; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | ($id, $description) = git_commit_info($orig_commit, |
| 3129 | $id, $orig_desc); |
| 3130 | |
Heinrich Schuchardt | 948b133 | 2017-07-10 15:52:16 -0700 | [diff] [blame] | 3131 | if (defined($id) && |
| 3132 | ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) { |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 3133 | ERROR("GIT_COMMIT_ID", |
| 3134 | "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); |
| 3135 | } |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 3136 | } |
| 3137 | |
Joe Perches | 13f1937 | 2014-08-06 16:10:59 -0700 | [diff] [blame] | 3138 | # Check for added, moved or deleted files |
| 3139 | if (!$reported_maintainer_file && !$in_commit_log && |
| 3140 | ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || |
| 3141 | $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || |
| 3142 | ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && |
| 3143 | (defined($1) || defined($2))))) { |
Andrew Jeffery | a82603a | 2016-12-12 16:46:37 -0800 | [diff] [blame] | 3144 | $is_patch = 1; |
Joe Perches | 13f1937 | 2014-08-06 16:10:59 -0700 | [diff] [blame] | 3145 | $reported_maintainer_file = 1; |
| 3146 | WARN("FILE_PATH_CHANGES", |
| 3147 | "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); |
| 3148 | } |
| 3149 | |
Rob Herring | e400edb | 2019-09-12 15:18:20 +0100 | [diff] [blame] | 3150 | # Check for adding new DT bindings not in schema format |
| 3151 | if (!$in_commit_log && |
| 3152 | ($line =~ /^new file mode\s*\d+\s*$/) && |
| 3153 | ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) { |
| 3154 | WARN("DT_SCHEMA_BINDING_PATCH", |
| 3155 | "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n"); |
| 3156 | } |
| 3157 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3158 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3159 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3160 | ERROR("CORRUPTED_PATCH", |
| 3161 | "patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3162 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 3166 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3167 | $rawline !~ m/^$UTF8*$/) { |
| 3168 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 3169 | |
| 3170 | my $blank = copy_spacing($rawline); |
| 3171 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 3172 | my $hereptr = "$hereline$ptr\n"; |
| 3173 | |
Joe Perches | 34d9921 | 2011-07-25 17:13:26 -0700 | [diff] [blame] | 3174 | CHK("INVALID_UTF8", |
| 3175 | "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3176 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3177 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 3178 | # Check if it's the start of a commit log |
| 3179 | # (not a header line and we haven't seen the patch filename) |
| 3180 | if ($in_header_lines && $realfile =~ /^$/ && |
Joe Perches | eb3a58d | 2017-05-08 15:55:42 -0700 | [diff] [blame] | 3181 | !($rawline =~ /^\s+(?:\S|$)/ || |
| 3182 | $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) { |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 3183 | $in_header_lines = 0; |
| 3184 | $in_commit_log = 1; |
Allen Hubbe | ed43c4e | 2016-08-02 14:04:45 -0700 | [diff] [blame] | 3185 | $has_commit_log = 1; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 3186 | } |
| 3187 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 3188 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 3189 | # declined it, i.e defined some charset where it is missing. |
| 3190 | if ($in_header_lines && |
| 3191 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 3192 | $1 !~ /utf-8/i) { |
| 3193 | $non_utf8_charset = 1; |
| 3194 | } |
| 3195 | |
| 3196 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 3197 | $rawline =~ /$NON_ASCII_UTF8/) { |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 3198 | WARN("UTF8_BEFORE_PATCH", |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 3199 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 3200 | } |
| 3201 | |
Joe Perches | d6430f7 | 2016-12-12 16:46:28 -0800 | [diff] [blame] | 3202 | # Check for absolute kernel paths in commit message |
| 3203 | if ($tree && $in_commit_log) { |
| 3204 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 3205 | my $file = $1; |
| 3206 | |
| 3207 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 3208 | check_absolute_file($1, $herecurr)) { |
| 3209 | # |
| 3210 | } else { |
| 3211 | check_absolute_file($file, $herecurr); |
| 3212 | } |
| 3213 | } |
| 3214 | } |
| 3215 | |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 3216 | # Check for various typo / spelling mistakes |
Joe Perches | 66d7a38 | 2015-04-16 12:44:08 -0700 | [diff] [blame] | 3217 | if (defined($misspellings) && |
| 3218 | ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { |
Dwaipayan Ray | 7da07c3 | 2020-12-15 20:45:21 -0800 | [diff] [blame] | 3219 | while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) { |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 3220 | my $typo = $1; |
Dwaipayan Ray | 7da07c3 | 2020-12-15 20:45:21 -0800 | [diff] [blame] | 3221 | my $blank = copy_spacing($rawline); |
| 3222 | my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo); |
| 3223 | my $hereptr = "$hereline$ptr\n"; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 3224 | my $typo_fix = $spelling_fix{lc($typo)}; |
| 3225 | $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); |
| 3226 | $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); |
Jean Delvare | 0675a8f | 2017-09-08 16:16:07 -0700 | [diff] [blame] | 3227 | my $msg_level = \&WARN; |
| 3228 | $msg_level = \&CHK if ($file); |
| 3229 | if (&{$msg_level}("TYPO_SPELLING", |
Dwaipayan Ray | 7da07c3 | 2020-12-15 20:45:21 -0800 | [diff] [blame] | 3230 | "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) && |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 3231 | $fix) { |
| 3232 | $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; |
| 3233 | } |
| 3234 | } |
| 3235 | } |
| 3236 | |
Matteo Croce | a8dd86b | 2019-09-25 16:46:38 -0700 | [diff] [blame] | 3237 | # check for invalid commit id |
| 3238 | if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) { |
| 3239 | my $id; |
| 3240 | my $description; |
| 3241 | ($id, $description) = git_commit_info($2, undef, undef); |
| 3242 | if (!defined($id)) { |
| 3243 | WARN("UNKNOWN_COMMIT_ID", |
| 3244 | "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr); |
| 3245 | } |
| 3246 | } |
| 3247 | |
Joe Perches | 310cd06 | 2020-10-15 20:11:52 -0700 | [diff] [blame] | 3248 | # check for repeated words separated by a single space |
Aditya Srivastava | 8d0325c | 2020-12-15 20:44:24 -0800 | [diff] [blame] | 3249 | # avoid false positive from list command eg, '-rw-r--r-- 1 root root' |
| 3250 | if (($rawline =~ /^\+/ || $in_commit_log) && |
| 3251 | $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) { |
Dwaipayan Ray | 1db81a6 | 2020-12-15 20:44:20 -0800 | [diff] [blame] | 3252 | pos($rawline) = 1 if (!$in_commit_log); |
Joe Perches | 310cd06 | 2020-10-15 20:11:52 -0700 | [diff] [blame] | 3253 | while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) { |
| 3254 | |
| 3255 | my $first = $1; |
| 3256 | my $second = $2; |
Dwaipayan Ray | 1db81a6 | 2020-12-15 20:44:20 -0800 | [diff] [blame] | 3257 | my $start_pos = $-[1]; |
| 3258 | my $end_pos = $+[2]; |
Joe Perches | 310cd06 | 2020-10-15 20:11:52 -0700 | [diff] [blame] | 3259 | if ($first =~ /(?:struct|union|enum)/) { |
| 3260 | pos($rawline) += length($first) + length($second) + 1; |
| 3261 | next; |
| 3262 | } |
| 3263 | |
Dwaipayan Ray | 1db81a6 | 2020-12-15 20:44:20 -0800 | [diff] [blame] | 3264 | next if (lc($first) ne lc($second)); |
Joe Perches | 310cd06 | 2020-10-15 20:11:52 -0700 | [diff] [blame] | 3265 | next if ($first eq 'long'); |
| 3266 | |
Dwaipayan Ray | 1db81a6 | 2020-12-15 20:44:20 -0800 | [diff] [blame] | 3267 | # check for character before and after the word matches |
| 3268 | my $start_char = ''; |
| 3269 | my $end_char = ''; |
| 3270 | $start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1)); |
| 3271 | $end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline)); |
| 3272 | |
| 3273 | next if ($start_char =~ /^\S$/); |
| 3274 | next if (index(" \t.,;?!", $end_char) == -1); |
| 3275 | |
Dwaipayan Ray | ea7dbab | 2021-02-25 17:21:47 -0800 | [diff] [blame] | 3276 | # avoid repeating hex occurrences like 'ff ff fe 09 ...' |
| 3277 | if ($first =~ /\b[0-9a-f]{2,}\b/i) { |
| 3278 | next if (!exists($allow_repeated_words{lc($first)})); |
| 3279 | } |
Aditya Srivastava | 8d0325c | 2020-12-15 20:44:24 -0800 | [diff] [blame] | 3280 | |
Joe Perches | 310cd06 | 2020-10-15 20:11:52 -0700 | [diff] [blame] | 3281 | if (WARN("REPEATED_WORD", |
| 3282 | "Possible repeated word: '$first'\n" . $herecurr) && |
| 3283 | $fix) { |
| 3284 | $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/; |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | # if it's a repeated word on consecutive lines in a comment block |
| 3289 | if ($prevline =~ /$;+\s*$/ && |
| 3290 | $prevrawline =~ /($word_pattern)\s*$/) { |
| 3291 | my $last_word = $1; |
| 3292 | if ($rawline =~ /^\+\s*\*\s*$last_word /) { |
| 3293 | if (WARN("REPEATED_WORD", |
| 3294 | "Possible repeated word: '$last_word'\n" . $hereprev) && |
| 3295 | $fix) { |
| 3296 | $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/; |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | } |
| 3301 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 3302 | # ignore non-hunk lines and lines being removed |
| 3303 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3304 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3305 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3306 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3307 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3308 | if (ERROR("DOS_LINE_ENDINGS", |
| 3309 | "DOS line endings\n" . $herevet) && |
| 3310 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3311 | $fixed[$fixlinenr] =~ s/[\s\015]+$//; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3312 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3313 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 3314 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3315 | if (ERROR("TRAILING_WHITESPACE", |
| 3316 | "trailing whitespace\n" . $herevet) && |
| 3317 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3318 | $fixed[$fixlinenr] =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3319 | } |
| 3320 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3321 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3322 | } |
Andy Whitcroft | 5368df20 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 3323 | |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 3324 | # Check for FSF mailing addresses. |
Alexander Duyck | 109d8cb | 2014-01-23 15:54:50 -0800 | [diff] [blame] | 3325 | if ($rawline =~ /\bwrite to the Free/i || |
Matthew Wilcox | 1bde561 | 2017-02-24 15:01:38 -0800 | [diff] [blame] | 3326 | $rawline =~ /\b675\s+Mass\s+Ave/i || |
Joe Perches | 3e2232f | 2014-01-23 15:54:48 -0800 | [diff] [blame] | 3327 | $rawline =~ /\b59\s+Temple\s+Pl/i || |
| 3328 | $rawline =~ /\b51\s+Franklin\s+St/i) { |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 3329 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Jean Delvare | 0675a8f | 2017-09-08 16:16:07 -0700 | [diff] [blame] | 3330 | my $msg_level = \&ERROR; |
| 3331 | $msg_level = \&CHK if ($file); |
| 3332 | &{$msg_level}("FSF_MAILING_ADDRESS", |
| 3333 | "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) |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 3334 | } |
| 3335 | |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 3336 | # check for Kconfig help text having a real description |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3337 | # Only applies when adding the entry originally, after that we do not have |
| 3338 | # sufficient context to determine whether it is indeed long enough. |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 3339 | if ($realfile =~ /Kconfig/ && |
Ulf Magnusson | 678ae16 | 2018-02-16 21:22:54 +0100 | [diff] [blame] | 3340 | # 'choice' is usually the last thing on the line (though |
| 3341 | # Kconfig supports named choices), so use a word boundary |
| 3342 | # (\b) rather than a whitespace character (\s) |
| 3343 | $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 3344 | my $length = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3345 | my $cnt = $realcnt; |
| 3346 | my $ln = $linenr + 1; |
| 3347 | my $f; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 3348 | my $is_start = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3349 | my $is_end = 0; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 3350 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3351 | $f = $lines[$ln - 1]; |
| 3352 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 3353 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3354 | |
| 3355 | next if ($f =~ /^-/); |
Joe Perches | 8d73e0e | 2014-08-06 16:10:46 -0700 | [diff] [blame] | 3356 | last if (!$file && $f =~ /^\@\@/); |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 3357 | |
Ulf Magnusson | 86adf1a | 2018-02-16 21:22:53 +0100 | [diff] [blame] | 3358 | if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 3359 | $is_start = 1; |
Masahiro Yamada | 22a4ac0 | 2020-06-17 12:02:20 +0900 | [diff] [blame] | 3360 | } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 3361 | $length = -1; |
| 3362 | } |
| 3363 | |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3364 | $f =~ s/^.//; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 3365 | $f =~ s/#.*//; |
| 3366 | $f =~ s/^\s+//; |
| 3367 | next if ($f =~ /^$/); |
Ulf Magnusson | 678ae16 | 2018-02-16 21:22:54 +0100 | [diff] [blame] | 3368 | |
| 3369 | # This only checks context lines in the patch |
| 3370 | # and so hopefully shouldn't trigger false |
| 3371 | # positives, even though some of these are |
| 3372 | # common words in help texts |
| 3373 | if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| |
| 3374 | if|endif|menu|endmenu|source)\b/x) { |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3375 | $is_end = 1; |
| 3376 | last; |
| 3377 | } |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 3378 | $length++; |
| 3379 | } |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 3380 | if ($is_start && $is_end && $length < $min_conf_desc_length) { |
| 3381 | WARN("CONFIG_DESCRIPTION", |
| 3382 | "please write a paragraph that describes the config symbol fully\n" . $herecurr); |
| 3383 | } |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 3384 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 3385 | } |
| 3386 | |
Joe Perches | 7ccf41a | 2020-06-04 16:50:33 -0700 | [diff] [blame] | 3387 | # check MAINTAINERS entries |
| 3388 | if ($realfile =~ /^MAINTAINERS$/) { |
| 3389 | # check MAINTAINERS entries for the right form |
| 3390 | if ($rawline =~ /^\+[A-Z]:/ && |
| 3391 | $rawline !~ /^\+[A-Z]:\t\S/) { |
| 3392 | if (WARN("MAINTAINERS_STYLE", |
| 3393 | "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) && |
| 3394 | $fix) { |
| 3395 | $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/; |
| 3396 | } |
| 3397 | } |
| 3398 | # check MAINTAINERS entries for the right ordering too |
| 3399 | my $preferred_order = 'MRLSWQBCPTFXNK'; |
| 3400 | if ($rawline =~ /^\+[A-Z]:/ && |
| 3401 | $prevrawline =~ /^[\+ ][A-Z]:/) { |
| 3402 | $rawline =~ /^\+([A-Z]):\s*(.*)/; |
| 3403 | my $cur = $1; |
| 3404 | my $curval = $2; |
| 3405 | $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/; |
| 3406 | my $prev = $1; |
| 3407 | my $prevval = $2; |
| 3408 | my $curindex = index($preferred_order, $cur); |
| 3409 | my $previndex = index($preferred_order, $prev); |
| 3410 | if ($curindex < 0) { |
| 3411 | WARN("MAINTAINERS_STYLE", |
| 3412 | "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr); |
| 3413 | } else { |
| 3414 | if ($previndex >= 0 && $curindex < $previndex) { |
| 3415 | WARN("MAINTAINERS_STYLE", |
| 3416 | "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev); |
| 3417 | } elsif ((($prev eq 'F' && $cur eq 'F') || |
| 3418 | ($prev eq 'X' && $cur eq 'X')) && |
| 3419 | ($prevval cmp $curval) > 0) { |
| 3420 | WARN("MAINTAINERS_STYLE", |
| 3421 | "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev); |
| 3422 | } |
| 3423 | } |
Joe Perches | 628f91a | 2017-07-10 15:52:07 -0700 | [diff] [blame] | 3424 | } |
| 3425 | } |
| 3426 | |
Arnaud Lacombe | c68e587 | 2011-08-15 01:07:14 -0400 | [diff] [blame] | 3427 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 3428 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 3429 | my $flag = $1; |
| 3430 | my $replacement = { |
| 3431 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 3432 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 3433 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 3434 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 3435 | }; |
| 3436 | |
| 3437 | WARN("DEPRECATED_VARIABLE", |
| 3438 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
| 3439 | } |
| 3440 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 3441 | # check for DT compatible documentation |
Florian Vaussard | 7dd05b3 | 2014-04-03 14:49:26 -0700 | [diff] [blame] | 3442 | if (defined $root && |
| 3443 | (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || |
| 3444 | ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { |
| 3445 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 3446 | my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; |
| 3447 | |
Florian Vaussard | cc93319 | 2014-04-03 14:49:27 -0700 | [diff] [blame] | 3448 | my $dt_path = $root . "/Documentation/devicetree/bindings/"; |
Rob Herring | 852d095 | 2019-05-22 09:55:34 -0500 | [diff] [blame] | 3449 | my $vp_file = $dt_path . "vendor-prefixes.yaml"; |
Florian Vaussard | cc93319 | 2014-04-03 14:49:27 -0700 | [diff] [blame] | 3450 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 3451 | foreach my $compat (@compats) { |
| 3452 | my $compat2 = $compat; |
Rob Herring | 185d566 | 2014-06-04 16:12:03 -0700 | [diff] [blame] | 3453 | $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; |
| 3454 | my $compat3 = $compat; |
| 3455 | $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; |
| 3456 | `grep -Erq "$compat|$compat2|$compat3" $dt_path`; |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 3457 | if ( $? >> 8 ) { |
| 3458 | WARN("UNDOCUMENTED_DT_STRING", |
| 3459 | "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); |
| 3460 | } |
| 3461 | |
Florian Vaussard | 4fbf32a | 2014-04-03 14:49:25 -0700 | [diff] [blame] | 3462 | next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; |
| 3463 | my $vendor = $1; |
Rob Herring | 852d095 | 2019-05-22 09:55:34 -0500 | [diff] [blame] | 3464 | `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`; |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 3465 | if ( $? >> 8 ) { |
| 3466 | WARN("UNDOCUMENTED_DT_STRING", |
Florian Vaussard | cc93319 | 2014-04-03 14:49:27 -0700 | [diff] [blame] | 3467 | "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 3468 | } |
| 3469 | } |
| 3470 | } |
| 3471 | |
Rob Herring | 9f3a899 | 2018-04-10 16:33:13 -0700 | [diff] [blame] | 3472 | # check for using SPDX license tag at beginning of files |
| 3473 | if ($realline == $checklicenseline) { |
| 3474 | if ($rawline =~ /^[ \+]\s*\#\!\s*\//) { |
| 3475 | $checklicenseline = 2; |
| 3476 | } elsif ($rawline =~ /^\+/) { |
| 3477 | my $comment = ""; |
| 3478 | if ($realfile =~ /\.(h|s|S)$/) { |
| 3479 | $comment = '/*'; |
| 3480 | } elsif ($realfile =~ /\.(c|dts|dtsi)$/) { |
| 3481 | $comment = '//'; |
Lubomir Rintel | c8df0ab | 2020-04-06 20:10:51 -0700 | [diff] [blame] | 3482 | } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) { |
Rob Herring | 9f3a899 | 2018-04-10 16:33:13 -0700 | [diff] [blame] | 3483 | $comment = '#'; |
| 3484 | } elsif ($realfile =~ /\.rst$/) { |
| 3485 | $comment = '..'; |
| 3486 | } |
| 3487 | |
Joe Perches | fdf1369 | 2019-03-07 16:28:32 -0800 | [diff] [blame] | 3488 | # check SPDX comment style for .[chsS] files |
| 3489 | if ($realfile =~ /\.[chsS]$/ && |
| 3490 | $rawline =~ /SPDX-License-Identifier:/ && |
Joe Perches | ffbce89 | 2019-09-25 16:46:35 -0700 | [diff] [blame] | 3491 | $rawline !~ m@^\+\s*\Q$comment\E\s*@) { |
Joe Perches | fdf1369 | 2019-03-07 16:28:32 -0800 | [diff] [blame] | 3492 | WARN("SPDX_LICENSE_TAG", |
| 3493 | "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr); |
| 3494 | } |
| 3495 | |
Rob Herring | 9f3a899 | 2018-04-10 16:33:13 -0700 | [diff] [blame] | 3496 | if ($comment !~ /^$/ && |
Joe Perches | ffbce89 | 2019-09-25 16:46:35 -0700 | [diff] [blame] | 3497 | $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) { |
| 3498 | WARN("SPDX_LICENSE_TAG", |
| 3499 | "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr); |
Joe Perches | 3b6e8ac | 2018-08-21 21:57:47 -0700 | [diff] [blame] | 3500 | } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) { |
Joe Perches | ffbce89 | 2019-09-25 16:46:35 -0700 | [diff] [blame] | 3501 | my $spdx_license = $1; |
| 3502 | if (!is_SPDX_License_valid($spdx_license)) { |
| 3503 | WARN("SPDX_LICENSE_TAG", |
| 3504 | "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr); |
| 3505 | } |
Lubomir Rintel | 50c9290 | 2020-04-06 20:11:13 -0700 | [diff] [blame] | 3506 | if ($realfile =~ m@^Documentation/devicetree/bindings/@ && |
| 3507 | not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) { |
| 3508 | my $msg_level = \&WARN; |
| 3509 | $msg_level = \&CHK if ($file); |
| 3510 | if (&{$msg_level}("SPDX_LICENSE_TAG", |
| 3511 | |
| 3512 | "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) && |
| 3513 | $fix) { |
| 3514 | $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/; |
| 3515 | } |
| 3516 | } |
Rob Herring | 9f3a899 | 2018-04-10 16:33:13 -0700 | [diff] [blame] | 3517 | } |
| 3518 | } |
| 3519 | } |
| 3520 | |
Joe Perches | a0154cd | 2020-10-15 20:12:19 -0700 | [diff] [blame] | 3521 | # check for embedded filenames |
| 3522 | if ($rawline =~ /^\+.*\Q$realfile\E/) { |
| 3523 | WARN("EMBEDDED_FILENAME", |
| 3524 | "It's generally not useful to have the filename in the file\n" . $herecurr); |
| 3525 | } |
| 3526 | |
Andy Whitcroft | 5368df20 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 3527 | # check we are in a valid source file if not then ignore this hunk |
Joe Perches | d6430f7 | 2016-12-12 16:46:28 -0800 | [diff] [blame] | 3528 | next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/); |
Andy Whitcroft | 5368df20 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 3529 | |
Joe Perches | a8da38a | 2019-03-07 16:28:42 -0800 | [diff] [blame] | 3530 | # check for using SPDX-License-Identifier on the wrong line number |
| 3531 | if ($realline != $checklicenseline && |
| 3532 | $rawline =~ /\bSPDX-License-Identifier:/ && |
| 3533 | substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) { |
| 3534 | WARN("SPDX_LICENSE_TAG", |
| 3535 | "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr); |
| 3536 | } |
| 3537 | |
Joe Perches | 47e0c88 | 2015-06-25 15:02:57 -0700 | [diff] [blame] | 3538 | # line length limit (with some exclusions) |
| 3539 | # |
| 3540 | # There are a few types of lines that may extend beyond $max_line_length: |
| 3541 | # logging functions like pr_info that end in a string |
| 3542 | # lines with a single string |
| 3543 | # #defines that are a single string |
Andreas Brauchli | 2e4bbbc | 2018-02-06 15:38:45 -0800 | [diff] [blame] | 3544 | # lines with an RFC3986 like URL |
Joe Perches | 47e0c88 | 2015-06-25 15:02:57 -0700 | [diff] [blame] | 3545 | # |
| 3546 | # There are 3 different line length message types: |
Jean Delvare | ab1ecab | 2017-09-08 16:16:04 -0700 | [diff] [blame] | 3547 | # LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length |
Joe Perches | 47e0c88 | 2015-06-25 15:02:57 -0700 | [diff] [blame] | 3548 | # LONG_LINE_STRING a string starts before but extends beyond $max_line_length |
| 3549 | # LONG_LINE all other lines longer than $max_line_length |
| 3550 | # |
| 3551 | # if LONG_LINE is ignored, the other 2 types are also ignored |
| 3552 | # |
| 3553 | |
Joe Perches | b4749e9 | 2015-07-17 16:24:01 -0700 | [diff] [blame] | 3554 | if ($line =~ /^\+/ && $length > $max_line_length) { |
Joe Perches | 47e0c88 | 2015-06-25 15:02:57 -0700 | [diff] [blame] | 3555 | my $msg_type = "LONG_LINE"; |
| 3556 | |
| 3557 | # Check the allowed long line types first |
| 3558 | |
| 3559 | # logging functions that end in a string that starts |
| 3560 | # before $max_line_length |
| 3561 | if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && |
| 3562 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3563 | $msg_type = ""; |
| 3564 | |
| 3565 | # lines with only strings (w/ possible termination) |
| 3566 | # #defines with only strings |
| 3567 | } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || |
| 3568 | $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { |
| 3569 | $msg_type = ""; |
| 3570 | |
Joe Perches | cc14750 | 2017-11-17 15:28:44 -0800 | [diff] [blame] | 3571 | # More special cases |
| 3572 | } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ || |
| 3573 | $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) { |
Joe Perches | d560a5f | 2016-08-02 14:04:31 -0700 | [diff] [blame] | 3574 | $msg_type = ""; |
| 3575 | |
Andreas Brauchli | 2e4bbbc | 2018-02-06 15:38:45 -0800 | [diff] [blame] | 3576 | # URL ($rawline is used in case the URL is in a comment) |
| 3577 | } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) { |
| 3578 | $msg_type = ""; |
| 3579 | |
Joe Perches | 47e0c88 | 2015-06-25 15:02:57 -0700 | [diff] [blame] | 3580 | # Otherwise set the alternate message types |
| 3581 | |
| 3582 | # a comment starts before $max_line_length |
| 3583 | } elsif ($line =~ /($;[\s$;]*)$/ && |
| 3584 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3585 | $msg_type = "LONG_LINE_COMMENT" |
| 3586 | |
| 3587 | # a quoted string starts before $max_line_length |
| 3588 | } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && |
| 3589 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 3590 | $msg_type = "LONG_LINE_STRING" |
| 3591 | } |
| 3592 | |
| 3593 | if ($msg_type ne "" && |
| 3594 | (show_type("LONG_LINE") || show_type($msg_type))) { |
Joe Perches | bdc48fa | 2020-05-29 16:12:21 -0700 | [diff] [blame] | 3595 | my $msg_level = \&WARN; |
| 3596 | $msg_level = \&CHK if ($file); |
| 3597 | &{$msg_level}($msg_type, |
| 3598 | "line length of $length exceeds $max_line_length columns\n" . $herecurr); |
Joe Perches | 47e0c88 | 2015-06-25 15:02:57 -0700 | [diff] [blame] | 3599 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3600 | } |
| 3601 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3602 | # check for adding lines without a newline. |
| 3603 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
Tom Rix | 47ca69b | 2020-12-15 20:44:40 -0800 | [diff] [blame] | 3604 | if (WARN("MISSING_EOF_NEWLINE", |
| 3605 | "adding a line without newline at end of file\n" . $herecurr) && |
| 3606 | $fix) { |
| 3607 | fix_delete_line($fixlinenr+1, "No newline at end of file"); |
| 3608 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3609 | } |
| 3610 | |
Aditya Srivastava | de93245 | 2021-02-25 17:21:57 -0800 | [diff] [blame] | 3611 | # check for .L prefix local symbols in .S files |
| 3612 | if ($realfile =~ /\.S$/ && |
| 3613 | $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) { |
| 3614 | WARN("AVOID_L_PREFIX", |
| 3615 | "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr); |
| 3616 | } |
| 3617 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 3618 | # check we are in a valid source file C or perl if not then ignore this hunk |
Geert Uytterhoeven | de4c924 | 2014-10-13 15:51:46 -0700 | [diff] [blame] | 3619 | next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3620 | |
| 3621 | # at the beginning of a line any tabs must come first and anything |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 3622 | # more than $tabsize must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3623 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 3624 | $rawline =~ /^\+\s* \s*/) { |
| 3625 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 3626 | $rpt_cleaners = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3627 | if (ERROR("CODE_INDENT", |
| 3628 | "code indent should use tabs where possible\n" . $herevet) && |
| 3629 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3630 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3631 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 3634 | # check for space before tabs. |
| 3635 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 3636 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3637 | if (WARN("SPACE_BEFORE_TAB", |
| 3638 | "please, no space before tabs\n" . $herevet) && |
| 3639 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3640 | while ($fixed[$fixlinenr] =~ |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 3641 | s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {} |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3642 | while ($fixed[$fixlinenr] =~ |
Joe Perches | c76f4cb | 2014-01-23 15:54:46 -0800 | [diff] [blame] | 3643 | s/(^\+.*) +\t/$1\t/) {} |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3644 | } |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 3645 | } |
| 3646 | |
Joe Perches | 6a48721 | 2018-04-10 16:34:04 -0700 | [diff] [blame] | 3647 | # check for assignments on the start of a line |
| 3648 | if ($sline =~ /^\+\s+($Assignment)[^=]/) { |
Aditya Srivastava | da7355a | 2020-12-15 20:45:06 -0800 | [diff] [blame] | 3649 | my $operator = $1; |
| 3650 | if (CHK("ASSIGNMENT_CONTINUATIONS", |
| 3651 | "Assignment operator '$1' should be on the previous line\n" . $hereprev) && |
| 3652 | $fix && $prevrawline =~ /^\+/) { |
| 3653 | # add assignment operator to the previous line, remove from current line |
| 3654 | $fixed[$fixlinenr - 1] .= " $operator"; |
| 3655 | $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; |
| 3656 | } |
Joe Perches | 6a48721 | 2018-04-10 16:34:04 -0700 | [diff] [blame] | 3657 | } |
| 3658 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3659 | # check for && or || at the start of a line |
| 3660 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
Aditya Srivastava | 8e08f07 | 2020-12-15 20:45:09 -0800 | [diff] [blame] | 3661 | my $operator = $1; |
| 3662 | if (CHK("LOGICAL_CONTINUATIONS", |
| 3663 | "Logical continuations should be on the previous line\n" . $hereprev) && |
| 3664 | $fix && $prevrawline =~ /^\+/) { |
| 3665 | # insert logical operator at last non-comment, non-whitepsace char on previous line |
| 3666 | $prevline =~ /[\s$;]*$/; |
| 3667 | my $line_end = substr($prevrawline, $-[0]); |
| 3668 | $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/; |
| 3669 | $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//; |
| 3670 | } |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3671 | } |
| 3672 | |
Joe Perches | a91e899 | 2016-05-20 17:04:05 -0700 | [diff] [blame] | 3673 | # check indentation starts on a tab stop |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 3674 | if ($perl_version_ok && |
Joe Perches | bd49111 | 2018-02-06 15:39:06 -0800 | [diff] [blame] | 3675 | $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { |
Joe Perches | a91e899 | 2016-05-20 17:04:05 -0700 | [diff] [blame] | 3676 | my $indent = length($1); |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 3677 | if ($indent % $tabsize) { |
Joe Perches | a91e899 | 2016-05-20 17:04:05 -0700 | [diff] [blame] | 3678 | if (WARN("TABSTOP", |
| 3679 | "Statements should start on a tabstop\n" . $herecurr) && |
| 3680 | $fix) { |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 3681 | $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e; |
Joe Perches | a91e899 | 2016-05-20 17:04:05 -0700 | [diff] [blame] | 3682 | } |
| 3683 | } |
| 3684 | } |
| 3685 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3686 | # check multi-line statement indentation matches previous line |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 3687 | if ($perl_version_ok && |
Joe Perches | fd71f63 | 2017-07-10 15:52:30 -0700 | [diff] [blame] | 3688 | $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3689 | $prevline =~ /^\+(\t*)(.*)$/; |
| 3690 | my $oldindent = $1; |
| 3691 | my $rest = $2; |
| 3692 | |
| 3693 | my $pos = pos_last_openparen($rest); |
| 3694 | if ($pos >= 0) { |
Joe Perches | b34a26f | 2012-07-30 14:41:16 -0700 | [diff] [blame] | 3695 | $line =~ /^(\+| )([ \t]*)/; |
| 3696 | my $newindent = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3697 | |
| 3698 | my $goodtabindent = $oldindent . |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 3699 | "\t" x ($pos / $tabsize) . |
| 3700 | " " x ($pos % $tabsize); |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3701 | my $goodspaceindent = $oldindent . " " x $pos; |
| 3702 | |
| 3703 | if ($newindent ne $goodtabindent && |
| 3704 | $newindent ne $goodspaceindent) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3705 | |
| 3706 | if (CHK("PARENTHESIS_ALIGNMENT", |
| 3707 | "Alignment should match open parenthesis\n" . $hereprev) && |
| 3708 | $fix && $line =~ /^\+/) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3709 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3710 | s/^\+[ \t]*/\+$goodtabindent/; |
| 3711 | } |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 3712 | } |
| 3713 | } |
| 3714 | } |
| 3715 | |
Joe Perches | 6ab3a97 | 2015-04-16 12:44:05 -0700 | [diff] [blame] | 3716 | # check for space after cast like "(int) foo" or "(struct foo) bar" |
| 3717 | # avoid checking a few false positives: |
| 3718 | # "sizeof(<type>)" or "__alignof__(<type>)" |
| 3719 | # function pointer declarations like "(*foo)(int) = bar;" |
| 3720 | # structure definitions like "(struct foo) { 0 };" |
| 3721 | # multiline macros that define functions |
| 3722 | # known attributes or the __attribute__ keyword |
| 3723 | if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && |
| 3724 | (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3725 | if (CHK("SPACING", |
Joe Perches | f27c95d | 2014-08-06 16:10:52 -0700 | [diff] [blame] | 3726 | "No space is necessary after a cast\n" . $herecurr) && |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3727 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3728 | $fixed[$fixlinenr] =~ |
Joe Perches | f27c95d | 2014-08-06 16:10:52 -0700 | [diff] [blame] | 3729 | s/(\(\s*$Type\s*\))[ \t]+/$1/; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3730 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 3733 | # Block comment styles |
| 3734 | # Networking with an initial /* |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 3735 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | fdb4bcd | 2013-07-03 15:05:23 -0700 | [diff] [blame] | 3736 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
Joe Perches | 85ad978 | 2014-04-03 14:49:20 -0700 | [diff] [blame] | 3737 | $rawline =~ /^\+[ \t]*\*/ && |
Łukasz Stelmach | c70735c | 2020-10-15 20:12:25 -0700 | [diff] [blame] | 3738 | $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 3739 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 3740 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 3741 | } |
| 3742 | |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 3743 | # Block comments use * on subsequent lines |
| 3744 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment |
| 3745 | $prevrawline =~ /^\+.*?\/\*/ && #starting /* |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 3746 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
Joe Perches | 61135e9 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 3747 | $rawline =~ /^\+/ && #line is new |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 3748 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 3749 | WARN("BLOCK_COMMENT_STYLE", |
| 3750 | "Block comments use * on subsequent lines\n" . $hereprev); |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 3751 | } |
| 3752 | |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 3753 | # Block comments use */ on trailing lines |
| 3754 | if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
Joe Perches | c24f9f1 | 2012-11-08 15:53:29 -0800 | [diff] [blame] | 3755 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 3756 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 3757 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 3758 | WARN("BLOCK_COMMENT_STYLE", |
| 3759 | "Block comments use a trailing */ on a separate line\n" . $herecurr); |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 3760 | } |
| 3761 | |
Joe Perches | 08eb9b8 | 2016-10-11 13:51:50 -0700 | [diff] [blame] | 3762 | # Block comment * alignment |
| 3763 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment |
Joe Perches | af207524 | 2016-10-11 13:52:05 -0700 | [diff] [blame] | 3764 | $line =~ /^\+[ \t]*$;/ && #leading comment |
| 3765 | $rawline =~ /^\+[ \t]*\*/ && #leading * |
| 3766 | (($prevrawline =~ /^\+.*?\/\*/ && #leading /* |
Joe Perches | 08eb9b8 | 2016-10-11 13:51:50 -0700 | [diff] [blame] | 3767 | $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ |
Joe Perches | af207524 | 2016-10-11 13:52:05 -0700 | [diff] [blame] | 3768 | $prevrawline =~ /^\+[ \t]*\*/)) { #leading * |
| 3769 | my $oldindent; |
Joe Perches | 08eb9b8 | 2016-10-11 13:51:50 -0700 | [diff] [blame] | 3770 | $prevrawline =~ m@^\+([ \t]*/?)\*@; |
Joe Perches | af207524 | 2016-10-11 13:52:05 -0700 | [diff] [blame] | 3771 | if (defined($1)) { |
| 3772 | $oldindent = expand_tabs($1); |
| 3773 | } else { |
| 3774 | $prevrawline =~ m@^\+(.*/?)\*@; |
| 3775 | $oldindent = expand_tabs($1); |
| 3776 | } |
Joe Perches | 08eb9b8 | 2016-10-11 13:51:50 -0700 | [diff] [blame] | 3777 | $rawline =~ m@^\+([ \t]*)\*@; |
| 3778 | my $newindent = $1; |
Joe Perches | 08eb9b8 | 2016-10-11 13:51:50 -0700 | [diff] [blame] | 3779 | $newindent = expand_tabs($newindent); |
Joe Perches | af207524 | 2016-10-11 13:52:05 -0700 | [diff] [blame] | 3780 | if (length($oldindent) ne length($newindent)) { |
Joe Perches | 08eb9b8 | 2016-10-11 13:51:50 -0700 | [diff] [blame] | 3781 | WARN("BLOCK_COMMENT_STYLE", |
| 3782 | "Block comments should align the * on each line\n" . $hereprev); |
| 3783 | } |
| 3784 | } |
| 3785 | |
Joe Perches | 7f61919 | 2014-08-06 16:10:39 -0700 | [diff] [blame] | 3786 | # check for missing blank lines after struct/union declarations |
| 3787 | # with exceptions for various attributes and macros |
| 3788 | if ($prevline =~ /^[\+ ]};?\s*$/ && |
| 3789 | $line =~ /^\+/ && |
| 3790 | !($line =~ /^\+\s*$/ || |
| 3791 | $line =~ /^\+\s*EXPORT_SYMBOL/ || |
| 3792 | $line =~ /^\+\s*MODULE_/i || |
| 3793 | $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || |
| 3794 | $line =~ /^\+[a-z_]*init/ || |
| 3795 | $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || |
| 3796 | $line =~ /^\+\s*DECLARE/ || |
Masahiro Yamada | 0bc989f | 2017-11-17 15:28:55 -0800 | [diff] [blame] | 3797 | $line =~ /^\+\s*builtin_[\w_]*driver/ || |
Joe Perches | 7f61919 | 2014-08-06 16:10:39 -0700 | [diff] [blame] | 3798 | $line =~ /^\+\s*__setup/)) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3799 | if (CHK("LINE_SPACING", |
| 3800 | "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && |
| 3801 | $fix) { |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 3802 | fix_insert_line($fixlinenr, "\+"); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3803 | } |
Joe Perches | 7f61919 | 2014-08-06 16:10:39 -0700 | [diff] [blame] | 3804 | } |
| 3805 | |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 3806 | # check for multiple consecutive blank lines |
| 3807 | if ($prevline =~ /^[\+ ]\s*$/ && |
| 3808 | $line =~ /^\+\s*$/ && |
| 3809 | $last_blank_line != ($linenr - 1)) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3810 | if (CHK("LINE_SPACING", |
| 3811 | "Please don't use multiple blank lines\n" . $hereprev) && |
| 3812 | $fix) { |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 3813 | fix_delete_line($fixlinenr, $rawline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3814 | } |
| 3815 | |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 3816 | $last_blank_line = $linenr; |
| 3817 | } |
| 3818 | |
Joe Perches | 3b617e3 | 2014-04-03 14:49:28 -0700 | [diff] [blame] | 3819 | # check for missing blank lines after declarations |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3820 | # (declarations must have the same indentation and not be at the start of line) |
| 3821 | if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) { |
| 3822 | # use temporaries |
| 3823 | my $sl = $sline; |
| 3824 | my $pl = $prevline; |
| 3825 | # remove $Attribute/$Sparse uses to simplify comparisons |
| 3826 | $sl =~ s/\b(?:$Attribute|$Sparse)\b//g; |
| 3827 | $pl =~ s/\b(?:$Attribute|$Sparse)\b//g; |
| 3828 | if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
Joe Perches | 5a4e1fd | 2014-08-06 16:10:33 -0700 | [diff] [blame] | 3829 | # function pointer declarations |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3830 | $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3831 | # foo bar; where foo is some local typedef or #define |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3832 | $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3833 | # known declaration macros |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3834 | $pl =~ /^\+\s+$declaration_macros/) && |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3835 | # for "else if" which can look like "$Ident $Ident" |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3836 | !($pl =~ /^\+\s+$c90_Keywords\b/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3837 | # other possible extensions of declaration lines |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3838 | $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3839 | # not starting a section or a macro "\" extended line |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3840 | $pl =~ /(?:\{\s*|\\)$/) && |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3841 | # looks like a declaration |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3842 | !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
Joe Perches | 5a4e1fd | 2014-08-06 16:10:33 -0700 | [diff] [blame] | 3843 | # function pointer declarations |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3844 | $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3845 | # foo bar; where foo is some local typedef or #define |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3846 | $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3847 | # known declaration macros |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3848 | $sl =~ /^\+\s+$declaration_macros/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3849 | # start of struct or union or enum |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3850 | $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3851 | # start or end of block or continuation of declaration |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3852 | $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3853 | # bitfield continuation |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3854 | $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 3855 | # other possible extensions of declaration lines |
Joe Perches | b5e8736 | 2021-02-25 17:21:40 -0800 | [diff] [blame] | 3856 | $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) { |
| 3857 | if (WARN("LINE_SPACING", |
| 3858 | "Missing a blank line after declarations\n" . $hereprev) && |
| 3859 | $fix) { |
| 3860 | fix_insert_line($fixlinenr, "\+"); |
| 3861 | } |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3862 | } |
Joe Perches | 3b617e3 | 2014-04-03 14:49:28 -0700 | [diff] [blame] | 3863 | } |
| 3864 | |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 3865 | # check for spaces at the beginning of a line. |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 3866 | # Exceptions: |
| 3867 | # 1) within comments |
| 3868 | # 2) indented preprocessor commands |
| 3869 | # 3) hanging labels |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3870 | if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 3871 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3872 | if (WARN("LEADING_SPACE", |
| 3873 | "please, no spaces at the start of a line\n" . $herevet) && |
| 3874 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3875 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3876 | } |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 3877 | } |
| 3878 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 3879 | # check we are in a valid C source file if not then ignore this hunk |
| 3880 | next if ($realfile !~ /\.(h|c)$/); |
| 3881 | |
Joe Perches | 5751a24 | 2017-11-17 15:28:52 -0800 | [diff] [blame] | 3882 | # check for unusual line ending [ or ( |
| 3883 | if ($line =~ /^\+.*([\[\(])\s*$/) { |
| 3884 | CHK("OPEN_ENDED_LINE", |
| 3885 | "Lines should not end with a '$1'\n" . $herecurr); |
| 3886 | } |
| 3887 | |
Joe Perches | 4dbed76 | 2017-05-08 15:55:39 -0700 | [diff] [blame] | 3888 | # check if this appears to be the start function declaration, save the name |
| 3889 | if ($sline =~ /^\+\{\s*$/ && |
| 3890 | $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) { |
| 3891 | $context_function = $1; |
| 3892 | } |
| 3893 | |
| 3894 | # check if this appears to be the end of function declaration |
| 3895 | if ($sline =~ /^\+\}\s*$/) { |
| 3896 | undef $context_function; |
| 3897 | } |
| 3898 | |
Joe Perches | 032a4c0 | 2014-08-06 16:10:29 -0700 | [diff] [blame] | 3899 | # check indentation of any line with a bare else |
Joe Perches | 840080a | 2014-10-13 15:51:59 -0700 | [diff] [blame] | 3900 | # (but not if it is a multiple line "if (foo) return bar; else return baz;") |
Joe Perches | 032a4c0 | 2014-08-06 16:10:29 -0700 | [diff] [blame] | 3901 | # if the previous line is a break or return and is indented 1 tab more... |
| 3902 | if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { |
| 3903 | my $tabs = length($1) + 1; |
Joe Perches | 840080a | 2014-10-13 15:51:59 -0700 | [diff] [blame] | 3904 | if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || |
| 3905 | ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && |
| 3906 | defined $lines[$linenr] && |
| 3907 | $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { |
Joe Perches | 032a4c0 | 2014-08-06 16:10:29 -0700 | [diff] [blame] | 3908 | WARN("UNNECESSARY_ELSE", |
| 3909 | "else is not generally useful after a break or return\n" . $hereprev); |
| 3910 | } |
| 3911 | } |
| 3912 | |
Joe Perches | c00df19 | 2014-08-06 16:11:01 -0700 | [diff] [blame] | 3913 | # check indentation of a line with a break; |
Joe Perches | dc58bc5 | 2020-12-15 20:44:33 -0800 | [diff] [blame] | 3914 | # if the previous line is a goto, return or break |
| 3915 | # and is indented the same # of tabs |
Joe Perches | c00df19 | 2014-08-06 16:11:01 -0700 | [diff] [blame] | 3916 | if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { |
| 3917 | my $tabs = $1; |
Joe Perches | dc58bc5 | 2020-12-15 20:44:33 -0800 | [diff] [blame] | 3918 | if ($prevline =~ /^\+$tabs(goto|return|break)\b/) { |
| 3919 | if (WARN("UNNECESSARY_BREAK", |
| 3920 | "break is not useful after a $1\n" . $hereprev) && |
| 3921 | $fix) { |
| 3922 | fix_delete_line($fixlinenr, $rawline); |
| 3923 | } |
Joe Perches | c00df19 | 2014-08-06 16:11:01 -0700 | [diff] [blame] | 3924 | } |
| 3925 | } |
| 3926 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3927 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3928 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3929 | WARN("CVS_KEYWORD", |
| 3930 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3931 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3932 | |
Joe Perches | 56e77d7 | 2013-02-21 16:44:14 -0800 | [diff] [blame] | 3933 | # check for old HOTPLUG __dev<foo> section markings |
| 3934 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 3935 | WARN("HOTPLUG_SECTION", |
| 3936 | "Using $1 is unnecessary\n" . $herecurr); |
| 3937 | } |
| 3938 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3939 | # Check for potential 'bare' types |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3940 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 3941 | $realline_next); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 3942 | #print "LINE<$line>\n"; |
Joe Perches | ca81986 | 2017-07-10 15:52:13 -0700 | [diff] [blame] | 3943 | if ($linenr > $suppress_statement && |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 3944 | $realcnt && $sline =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a22 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3945 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 3946 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3947 | $stat =~ s/\n./\n /g; |
| 3948 | $cond =~ s/\n./\n /g; |
| 3949 | |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 3950 | #print "linenr<$linenr> <$stat>\n"; |
| 3951 | # If this statement has no statement boundaries within |
| 3952 | # it there is no point in retrying a statement scan |
| 3953 | # until we hit end of it. |
| 3954 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 3955 | if ($frag !~ /(?:{|;)/) { |
| 3956 | #print "skip<$line_nr_next>\n"; |
| 3957 | $suppress_statement = $line_nr_next; |
| 3958 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3959 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3960 | # Find the real next line. |
| 3961 | $realline_next = $line_nr_next; |
| 3962 | if (defined $realline_next && |
| 3963 | (!defined $lines[$realline_next - 1] || |
| 3964 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 3965 | $realline_next++; |
| 3966 | } |
| 3967 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3968 | my $s = $stat; |
| 3969 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3970 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3971 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3972 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3973 | |
| 3974 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3975 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3976 | |
Andy Whitcroft | 463f286 | 2009-09-21 17:04:34 -0700 | [diff] [blame] | 3977 | } elsif ($s =~ /^.\s*else\b/s) { |
| 3978 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3979 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 3980 | } 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) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3981 | my $type = $1; |
| 3982 | $type =~ s/\s+/ /g; |
| 3983 | possible($type, "A:" . $s); |
| 3984 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3985 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a84062 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3986 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3987 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3988 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3989 | |
| 3990 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3991 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3992 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3993 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3994 | |
| 3995 | # Check for any sort of function declaration. |
| 3996 | # int foo(something bar, other baz); |
| 3997 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3998 | if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3999 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4000 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4001 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4002 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4003 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4004 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4005 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4006 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4007 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4008 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4009 | } |
| 4010 | } |
| 4011 | } |
| 4012 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4013 | } |
| 4014 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4015 | # |
| 4016 | # Checks which may be anchored in the context. |
| 4017 | # |
| 4018 | |
| 4019 | # Check for switch () and associated case and default |
| 4020 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4021 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 4022 | my $err = ''; |
| 4023 | my $sep = ''; |
| 4024 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 4025 | shift(@ctx); |
| 4026 | for my $ctx (@ctx) { |
| 4027 | my ($clen, $cindent) = line_stats($ctx); |
| 4028 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 4029 | $indent != $cindent) { |
| 4030 | $err .= "$sep$ctx\n"; |
| 4031 | $sep = ''; |
| 4032 | } else { |
| 4033 | $sep = "[...]\n"; |
| 4034 | } |
| 4035 | } |
| 4036 | if ($err ne '') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4037 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 4038 | "switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4039 | } |
| 4040 | } |
| 4041 | |
| 4042 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 4043 | # or if that brace on the next line is for something else |
Joe Perches | 0fe3dc2 | 2014-08-06 16:11:16 -0700 | [diff] [blame] | 4044 | if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4045 | my $pre_ctx = "$1$2"; |
| 4046 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4047 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Joe Perches | 8eef05d | 2012-02-03 15:20:39 -0800 | [diff] [blame] | 4048 | |
| 4049 | if ($line =~ /^\+\t{6,}/) { |
| 4050 | WARN("DEEP_INDENTATION", |
| 4051 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 4052 | } |
| 4053 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4054 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 4055 | my $ctx = join("\n", @ctx); |
| 4056 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 4057 | my $ctx_ln = $linenr; |
| 4058 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4059 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 4060 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 4061 | defined $lines[$ctx_ln - 1] && |
| 4062 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 4063 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 4064 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4065 | $ctx_ln++; |
| 4066 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 4067 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 4068 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 4069 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4070 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 4071 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4072 | ERROR("OPEN_BRACE", |
| 4073 | "that open brace { should be on the previous line\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 4074 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4075 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4076 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 4077 | $ctx =~ /\)\s*\;\s*$/ && |
| 4078 | defined $lines[$ctx_ln - 1]) |
| 4079 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4080 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 4081 | if ($nindent > $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4082 | WARN("TRAILING_SEMICOLON", |
| 4083 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 4084 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4085 | } |
| 4086 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4087 | } |
| 4088 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4089 | # Check relative indent for conditionals and blocks. |
Joe Perches | f6950a7 | 2017-05-08 15:56:05 -0700 | [diff] [blame] | 4090 | if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 4091 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 4092 | ctx_statement_block($linenr, $realcnt, 0) |
| 4093 | if (!defined $stat); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4094 | my ($s, $c) = ($stat, $cond); |
| 4095 | |
| 4096 | substr($s, 0, length($c), ''); |
| 4097 | |
Joe Perches | 9f5af48 | 2015-09-09 15:37:30 -0700 | [diff] [blame] | 4098 | # remove inline comments |
| 4099 | $s =~ s/$;/ /g; |
| 4100 | $c =~ s/$;/ /g; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4101 | |
| 4102 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 4103 | my @newlines = ($c =~ /\n/gs); |
| 4104 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4105 | |
Joe Perches | 9f5af48 | 2015-09-09 15:37:30 -0700 | [diff] [blame] | 4106 | # Make sure we remove the line prefixes as we have |
| 4107 | # none on the first line, and are going to readd them |
| 4108 | # where necessary. |
| 4109 | $s =~ s/\n./\n/gs; |
| 4110 | while ($s =~ /\n\s+\\\n/) { |
| 4111 | $cond_lines += $s =~ s/\n\s+\\\n/\n/g; |
| 4112 | } |
| 4113 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4114 | # We want to check the first line inside the block |
| 4115 | # starting at the end of the conditional, so remove: |
| 4116 | # 1) any blank line termination |
| 4117 | # 2) any opening brace { on end of the line |
| 4118 | # 3) any do (...) { |
| 4119 | my $continuation = 0; |
| 4120 | my $check = 0; |
| 4121 | $s =~ s/^.*\bdo\b//; |
| 4122 | $s =~ s/^\s*{//; |
| 4123 | if ($s =~ s/^\s*\\//) { |
| 4124 | $continuation = 1; |
| 4125 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 4126 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4127 | $check = 1; |
| 4128 | $cond_lines++; |
| 4129 | } |
| 4130 | |
| 4131 | # Also ignore a loop construct at the end of a |
| 4132 | # preprocessor statement. |
| 4133 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 4134 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 4135 | $check = 0; |
| 4136 | } |
| 4137 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 4138 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 4139 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 4140 | while ($cond_ptr != $cond_lines) { |
| 4141 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4142 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 4143 | # If we see an #else/#elif then the code |
| 4144 | # is not linear. |
| 4145 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 4146 | $check = 0; |
| 4147 | } |
| 4148 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 4149 | # Ignore: |
| 4150 | # 1) blank lines, they should be at 0, |
| 4151 | # 2) preprocessor lines, and |
| 4152 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 4153 | if ($continuation || |
| 4154 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 4155 | $s =~ /^\s*#\s*?/ || |
| 4156 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 4157 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 30dad6e | 2009-09-21 17:04:36 -0700 | [diff] [blame] | 4158 | if ($s =~ s/^.*?\n//) { |
| 4159 | $cond_lines++; |
| 4160 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 4161 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | my (undef, $sindent) = line_stats("+" . $s); |
| 4165 | my $stat_real = raw_line($linenr, $cond_lines); |
| 4166 | |
| 4167 | # Check if either of these lines are modified, else |
| 4168 | # this is not this patch's fault. |
| 4169 | if (!defined($stat_real) || |
| 4170 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 4171 | $check = 0; |
| 4172 | } |
| 4173 | if (defined($stat_real) && $cond_lines > 1) { |
| 4174 | $stat_real = "[...]\n$stat_real"; |
| 4175 | } |
| 4176 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 4177 | #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"; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4178 | |
Joe Perches | 9f5af48 | 2015-09-09 15:37:30 -0700 | [diff] [blame] | 4179 | if ($check && $s ne '' && |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 4180 | (($sindent % $tabsize) != 0 || |
Joe Perches | 9f5af48 | 2015-09-09 15:37:30 -0700 | [diff] [blame] | 4181 | ($sindent < $indent) || |
Joe Perches | f6950a7 | 2017-05-08 15:56:05 -0700 | [diff] [blame] | 4182 | ($sindent == $indent && |
| 4183 | ($s !~ /^\s*(?:\}|\{|else\b)/)) || |
Antonio Borneo | 713a09d | 2020-04-06 20:11:07 -0700 | [diff] [blame] | 4184 | ($sindent > $indent + $tabsize))) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4185 | WARN("SUSPECT_CODE_INDENT", |
| 4186 | "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 4187 | } |
| 4188 | } |
| 4189 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4190 | # Track the 'values' across context and added lines. |
| 4191 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4192 | my ($curr_values, $curr_vars) = |
| 4193 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4194 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4195 | if ($dbg_values) { |
| 4196 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4197 | print "$linenr > .$outline\n"; |
| 4198 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4199 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4200 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4201 | $prev_values = substr($curr_values, -1); |
| 4202 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4203 | #ignore lines not being added |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4204 | next if ($line =~ /^[^\+]/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4205 | |
Joe Perches | 99ca38c | 2020-10-15 20:12:09 -0700 | [diff] [blame] | 4206 | # check for self assignments used to avoid compiler warnings |
| 4207 | # e.g.: int foo = foo, *bar = NULL; |
| 4208 | # struct foo bar = *(&(bar)); |
| 4209 | if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) { |
| 4210 | my $var = $1; |
| 4211 | if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) { |
| 4212 | WARN("SELF_ASSIGNMENT", |
| 4213 | "Do not use self-assignments to avoid compiler warnings\n" . $herecurr); |
| 4214 | } |
| 4215 | } |
| 4216 | |
Joe Perches | 11ca40a | 2016-12-12 16:46:31 -0800 | [diff] [blame] | 4217 | # check for dereferences that span multiple lines |
| 4218 | if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && |
| 4219 | $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { |
| 4220 | $prevline =~ /($Lval\s*(?:\.|->))\s*$/; |
| 4221 | my $ref = $1; |
| 4222 | $line =~ /^.\s*($Lval)/; |
| 4223 | $ref .= $1; |
| 4224 | $ref =~ s/\s//g; |
| 4225 | WARN("MULTILINE_DEREFERENCE", |
| 4226 | "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev); |
| 4227 | } |
| 4228 | |
Joe Perches | a1ce18e | 2016-03-15 14:58:03 -0700 | [diff] [blame] | 4229 | # check for declarations of signed or unsigned without int |
Joe Perches | c844711 | 2016-08-02 14:04:42 -0700 | [diff] [blame] | 4230 | while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { |
Joe Perches | a1ce18e | 2016-03-15 14:58:03 -0700 | [diff] [blame] | 4231 | my $type = $1; |
| 4232 | my $var = $2; |
Joe Perches | 207a8e8 | 2016-03-15 14:58:06 -0700 | [diff] [blame] | 4233 | $var = "" if (!defined $var); |
| 4234 | if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { |
Joe Perches | a1ce18e | 2016-03-15 14:58:03 -0700 | [diff] [blame] | 4235 | my $sign = $1; |
| 4236 | my $pointer = $2; |
| 4237 | |
| 4238 | $pointer = "" if (!defined $pointer); |
| 4239 | |
| 4240 | if (WARN("UNSPECIFIED_INT", |
| 4241 | "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && |
| 4242 | $fix) { |
| 4243 | my $decl = trim($sign) . " int "; |
Joe Perches | 207a8e8 | 2016-03-15 14:58:06 -0700 | [diff] [blame] | 4244 | my $comp_pointer = $pointer; |
| 4245 | $comp_pointer =~ s/\s//g; |
| 4246 | $decl .= $comp_pointer; |
| 4247 | $decl = rtrim($decl) if ($var eq ""); |
| 4248 | $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; |
Joe Perches | a1ce18e | 2016-03-15 14:58:03 -0700 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | } |
| 4252 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4253 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 4254 | if ($dbg_type) { |
| 4255 | if ($line =~ /^.\s*$Declare\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4256 | ERROR("TEST_TYPE", |
| 4257 | "TEST: is type\n" . $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 4258 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4259 | ERROR("TEST_NOT_TYPE", |
| 4260 | "TEST: is not type ($1 is)\n". $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 4261 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4262 | next; |
| 4263 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 4264 | # TEST: allow direct testing of the attribute matcher. |
| 4265 | if ($dbg_attr) { |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 4266 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4267 | ERROR("TEST_ATTR", |
| 4268 | "TEST: is attr\n" . $herecurr); |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 4269 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4270 | ERROR("TEST_NOT_ATTR", |
| 4271 | "TEST: is not attr ($1 is)\n". $herecurr); |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 4272 | } |
| 4273 | next; |
| 4274 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4275 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4276 | # check for initialisation to aggregates open brace on the next line |
Andy Whitcroft | 99423c2 | 2009-10-26 16:50:15 -0700 | [diff] [blame] | 4277 | if ($line =~ /^.\s*{/ && |
| 4278 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 4279 | if (ERROR("OPEN_BRACE", |
| 4280 | "that open brace { should be on the previous line\n" . $hereprev) && |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 4281 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4282 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4283 | fix_delete_line($fixlinenr, $rawline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 4284 | my $fixedline = $prevrawline; |
| 4285 | $fixedline =~ s/\s*=\s*$/ = {/; |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 4286 | fix_insert_line($fixlinenr, $fixedline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 4287 | $fixedline = $line; |
Cyril Bur | 8d81ae0 | 2017-07-10 15:52:21 -0700 | [diff] [blame] | 4288 | $fixedline =~ s/^(.\s*)\{\s*/$1/; |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 4289 | fix_insert_line($fixlinenr, $fixedline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 4290 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4291 | } |
| 4292 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4293 | # |
| 4294 | # Checks which are anchored on the added line. |
| 4295 | # |
| 4296 | |
| 4297 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4298 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4299 | my $path = $1; |
| 4300 | if ($path =~ m{//}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4301 | ERROR("MALFORMED_INCLUDE", |
Joe Perches | 495e9d8 | 2012-12-20 15:05:37 -0800 | [diff] [blame] | 4302 | "malformed #include filename\n" . $herecurr); |
| 4303 | } |
| 4304 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 4305 | ERROR("UAPI_INCLUDE", |
| 4306 | "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4307 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4308 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4309 | |
| 4310 | # no C99 // comments |
| 4311 | if ($line =~ m{//}) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4312 | if (ERROR("C99_COMMENTS", |
| 4313 | "do not use C99 // comments\n" . $herecurr) && |
| 4314 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4315 | my $line = $fixed[$fixlinenr]; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4316 | if ($line =~ /\/\/(.*)$/) { |
| 4317 | my $comment = trim($1); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4318 | $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4319 | } |
| 4320 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4321 | } |
| 4322 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4323 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4324 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4325 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 4326 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 4327 | # the whole statement. |
| 4328 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 4329 | if (defined $realline_next && |
| 4330 | exists $lines[$realline_next - 1] && |
| 4331 | !defined $suppress_export{$realline_next} && |
Christoph Hellwig | 3679482 | 2021-02-02 13:13:34 +0100 | [diff] [blame] | 4332 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 4333 | # Handle definitions which produce identifiers with |
| 4334 | # a prefix: |
| 4335 | # XXX(foo); |
| 4336 | # EXPORT_SYMBOL(something_foo); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4337 | my $name = $1; |
Andy Whitcroft | 87a5387 | 2012-01-10 15:10:04 -0800 | [diff] [blame] | 4338 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 4339 | $name =~ /^${Ident}_$2/) { |
| 4340 | #print "FOO C name<$name>\n"; |
| 4341 | $suppress_export{$realline_next} = 1; |
| 4342 | |
| 4343 | } elsif ($stat !~ /(?: |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 4344 | \n.}\s*$| |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 4345 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 4346 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 4347 | ^.LIST_HEAD\(\Q$name\E\)| |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 4348 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 4349 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 4350 | )/x) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 4351 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 4352 | $suppress_export{$realline_next} = 2; |
| 4353 | } else { |
| 4354 | $suppress_export{$realline_next} = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4355 | } |
| 4356 | } |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 4357 | if (!defined $suppress_export{$linenr} && |
| 4358 | $prevline =~ /^.\s*$/ && |
Christoph Hellwig | 3679482 | 2021-02-02 13:13:34 +0100 | [diff] [blame] | 4359 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 4360 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 4361 | $suppress_export{$linenr} = 2; |
| 4362 | } |
| 4363 | if (defined $suppress_export{$linenr} && |
| 4364 | $suppress_export{$linenr} == 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4365 | WARN("EXPORT_SYMBOL", |
| 4366 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 4367 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4368 | |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 4369 | # check for global initialisers. |
Song Liu | 5b8f82e | 2021-02-25 17:22:08 -0800 | [diff] [blame] | 4370 | if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ && |
| 4371 | !exclude_global_initialisers($realfile)) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4372 | if (ERROR("GLOBAL_INITIALISERS", |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 4373 | "do not initialise globals to $1\n" . $herecurr) && |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4374 | $fix) { |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 4375 | $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4376 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4377 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4378 | # check for static initialisers. |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 4379 | if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4380 | if (ERROR("INITIALISED_STATIC", |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 4381 | "do not initialise statics to $1\n" . |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4382 | $herecurr) && |
| 4383 | $fix) { |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 4384 | $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4385 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4386 | } |
| 4387 | |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 4388 | # check for misordered declarations of char/short/int/long with signed/unsigned |
| 4389 | while ($sline =~ m{(\b$TypeMisordered\b)}g) { |
| 4390 | my $tmp = trim($1); |
| 4391 | WARN("MISORDERED_TYPE", |
| 4392 | "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); |
| 4393 | } |
| 4394 | |
Joe Perches | 809e082 | 2018-08-21 21:58:12 -0700 | [diff] [blame] | 4395 | # check for unnecessary <signed> int declarations of short/long/long long |
| 4396 | while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) { |
| 4397 | my $type = trim($1); |
| 4398 | next if ($type !~ /\bint\b/); |
| 4399 | next if ($type !~ /\b(?:short|long\s+long|long)\b/); |
| 4400 | my $new_type = $type; |
| 4401 | $new_type =~ s/\b\s*int\s*\b/ /; |
| 4402 | $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /; |
| 4403 | $new_type =~ s/^const\s+//; |
| 4404 | $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/); |
| 4405 | $new_type = "const $new_type" if ($type =~ /^const\b/); |
| 4406 | $new_type =~ s/\s+/ /g; |
| 4407 | $new_type = trim($new_type); |
| 4408 | if (WARN("UNNECESSARY_INT", |
| 4409 | "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) && |
| 4410 | $fix) { |
| 4411 | $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/; |
| 4412 | } |
| 4413 | } |
| 4414 | |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 4415 | # check for static const char * arrays. |
| 4416 | if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4417 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 4418 | "static const char * array should probably be static const char * const\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 4419 | $herecurr); |
Joe Perches | 77b8c0a8 | 2019-01-03 15:26:59 -0800 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | # check for initialized const char arrays that should be static const |
| 4423 | if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) { |
| 4424 | if (WARN("STATIC_CONST_CHAR_ARRAY", |
| 4425 | "const array should probably be static const\n" . $herecurr) && |
| 4426 | $fix) { |
| 4427 | $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/; |
| 4428 | } |
| 4429 | } |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 4430 | |
| 4431 | # check for static char foo[] = "bar" declarations. |
| 4432 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4433 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 4434 | "static char array declaration should probably be static const char\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 4435 | $herecurr); |
Joe Perches | 77b8c0a8 | 2019-01-03 15:26:59 -0800 | [diff] [blame] | 4436 | } |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 4437 | |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 4438 | # check for const <foo> const where <foo> is not a pointer or array type |
| 4439 | if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { |
| 4440 | my $found = $1; |
| 4441 | if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { |
| 4442 | WARN("CONST_CONST", |
| 4443 | "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); |
| 4444 | } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { |
| 4445 | WARN("CONST_CONST", |
| 4446 | "'const $found const' should probably be 'const $found'\n" . $herecurr); |
| 4447 | } |
| 4448 | } |
| 4449 | |
Joe Perches | 7316976 | 2020-12-15 20:44:30 -0800 | [diff] [blame] | 4450 | # check for const static or static <non ptr type> const declarations |
| 4451 | # prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const' |
| 4452 | if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ || |
| 4453 | $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) { |
| 4454 | if (WARN("STATIC_CONST", |
| 4455 | "Move const after static - use 'static const $1'\n" . $herecurr) && |
| 4456 | $fix) { |
| 4457 | $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/; |
| 4458 | $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/; |
| 4459 | } |
| 4460 | } |
| 4461 | |
Joe Perches | 9b0fa60 | 2014-04-03 14:49:18 -0700 | [diff] [blame] | 4462 | # check for non-global char *foo[] = {"bar", ...} declarations. |
| 4463 | if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { |
| 4464 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 4465 | "char * array declaration might be better as static const\n" . |
| 4466 | $herecurr); |
Dwaipayan Ray | ea7dbab | 2021-02-25 17:21:47 -0800 | [diff] [blame] | 4467 | } |
Joe Perches | 9b0fa60 | 2014-04-03 14:49:18 -0700 | [diff] [blame] | 4468 | |
Joe Perches | b598b67 | 2015-04-16 12:44:36 -0700 | [diff] [blame] | 4469 | # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) |
| 4470 | if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { |
| 4471 | my $array = $1; |
| 4472 | if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) { |
| 4473 | my $array_div = $1; |
| 4474 | if (WARN("ARRAY_SIZE", |
| 4475 | "Prefer ARRAY_SIZE($array)\n" . $herecurr) && |
| 4476 | $fix) { |
| 4477 | $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; |
| 4478 | } |
| 4479 | } |
| 4480 | } |
| 4481 | |
Joe Perches | b36190c | 2014-01-27 17:07:18 -0800 | [diff] [blame] | 4482 | # check for function declarations without arguments like "int foo()" |
Joe Perches | 16b7f3c | 2020-04-06 20:11:17 -0700 | [diff] [blame] | 4483 | if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { |
Joe Perches | b36190c | 2014-01-27 17:07:18 -0800 | [diff] [blame] | 4484 | if (ERROR("FUNCTION_WITHOUT_ARGS", |
| 4485 | "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && |
| 4486 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4487 | $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; |
Joe Perches | b36190c | 2014-01-27 17:07:18 -0800 | [diff] [blame] | 4488 | } |
| 4489 | } |
| 4490 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4491 | # check for new typedefs, only function parameters and sparse annotations |
| 4492 | # make sense. |
| 4493 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 8054576 | 2009-01-06 14:41:26 -0800 | [diff] [blame] | 4494 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4495 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 4496 | $line !~ /\b$typeTypedefs\b/ && |
Michael S. Tsirkin | 46d832f | 2016-12-11 06:29:58 +0200 | [diff] [blame] | 4497 | $line !~ /\b__bitwise\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4498 | WARN("NEW_TYPEDEFS", |
| 4499 | "do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 4503 | # (char*[ const]) |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 4504 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 4505 | #print "AA<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4506 | my ($ident, $from, $to) = ($1, $2, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4507 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 4508 | # Should start with a space. |
| 4509 | $to =~ s/^(\S)/ $1/; |
| 4510 | # Should not end with a space. |
| 4511 | $to =~ s/\s+$//; |
| 4512 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d1 | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 4513 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 4514 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4515 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4516 | ## print "1: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 4517 | if ($from ne $to) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4518 | if (ERROR("POINTER_LOCATION", |
| 4519 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && |
| 4520 | $fix) { |
| 4521 | my $sub_from = $ident; |
| 4522 | my $sub_to = $ident; |
| 4523 | $sub_to =~ s/\Q$from\E/$to/; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4524 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4525 | s@\Q$sub_from\E@$sub_to@; |
| 4526 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 4527 | } |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 4528 | } |
| 4529 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 4530 | #print "BB<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4531 | my ($match, $from, $to, $ident) = ($1, $2, $2, $3); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4532 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 4533 | # Should start with a space. |
| 4534 | $to =~ s/^(\S)/ $1/; |
| 4535 | # Should not end with a space. |
| 4536 | $to =~ s/\s+$//; |
| 4537 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d1 | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 4538 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 4539 | } |
| 4540 | # Modifiers should have spaces. |
| 4541 | $to =~ s/(\b$Modifier$)/$1 /; |
| 4542 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4543 | ## print "2: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 667026e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 4544 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4545 | if (ERROR("POINTER_LOCATION", |
| 4546 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && |
| 4547 | $fix) { |
| 4548 | |
| 4549 | my $sub_from = $match; |
| 4550 | my $sub_to = $match; |
| 4551 | $sub_to =~ s/\Q$from\E/$to/; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4552 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4553 | s@\Q$sub_from\E@$sub_to@; |
| 4554 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 4555 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4556 | } |
| 4557 | |
Joe Perches | 9d3e3c7 | 2015-09-09 15:37:27 -0700 | [diff] [blame] | 4558 | # avoid BUG() or BUG_ON() |
| 4559 | if ($line =~ /\b(?:BUG|BUG_ON)\b/) { |
Jean Delvare | 0675a8f | 2017-09-08 16:16:07 -0700 | [diff] [blame] | 4560 | my $msg_level = \&WARN; |
| 4561 | $msg_level = \&CHK if ($file); |
| 4562 | &{$msg_level}("AVOID_BUG", |
| 4563 | "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); |
Joe Perches | 9d3e3c7 | 2015-09-09 15:37:27 -0700 | [diff] [blame] | 4564 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4565 | |
Joe Perches | 9d3e3c7 | 2015-09-09 15:37:27 -0700 | [diff] [blame] | 4566 | # avoid LINUX_VERSION_CODE |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4567 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4568 | WARN("LINUX_VERSION_CODE", |
| 4569 | "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4570 | } |
| 4571 | |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 4572 | # check for uses of printk_ratelimit |
| 4573 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4574 | WARN("PRINTK_RATELIMITED", |
Joe Perches | 101ee68 | 2015-02-13 14:38:54 -0800 | [diff] [blame] | 4575 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 4576 | } |
| 4577 | |
Joe Perches | eeef573 | 2017-11-17 15:28:41 -0800 | [diff] [blame] | 4578 | # printk should use KERN_* levels |
| 4579 | if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) { |
| 4580 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 4581 | "printk() should include KERN_<LEVEL> facility level\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4582 | } |
| 4583 | |
Joe Perches | f5eea3b | 2020-12-15 20:45:24 -0800 | [diff] [blame] | 4584 | # prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL> |
| 4585 | if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) { |
| 4586 | my $printk = $1; |
| 4587 | my $modifier = $2; |
| 4588 | my $orig = $3; |
| 4589 | $modifier = "" if (!defined($modifier)); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 4590 | my $level = lc($orig); |
| 4591 | $level = "warn" if ($level eq "warning"); |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 4592 | my $level2 = $level; |
| 4593 | $level2 = "dbg" if ($level eq "debug"); |
Joe Perches | f5eea3b | 2020-12-15 20:45:24 -0800 | [diff] [blame] | 4594 | $level .= $modifier; |
| 4595 | $level2 .= $modifier; |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 4596 | WARN("PREFER_PR_LEVEL", |
Joe Perches | f5eea3b | 2020-12-15 20:45:24 -0800 | [diff] [blame] | 4597 | "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to $printk(KERN_$orig ...\n" . $herecurr); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 4598 | } |
| 4599 | |
Joe Perches | f5eea3b | 2020-12-15 20:45:24 -0800 | [diff] [blame] | 4600 | # prefer dev_<level> to dev_printk(KERN_<LEVEL> |
Joe Perches | dc13931 | 2013-02-21 16:44:13 -0800 | [diff] [blame] | 4601 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 4602 | my $orig = $1; |
| 4603 | my $level = lc($orig); |
| 4604 | $level = "warn" if ($level eq "warning"); |
| 4605 | $level = "dbg" if ($level eq "debug"); |
| 4606 | WARN("PREFER_DEV_LEVEL", |
| 4607 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 4608 | } |
| 4609 | |
Nicolas Boichat | 8020b25 | 2020-10-15 20:12:02 -0700 | [diff] [blame] | 4610 | # trace_printk should not be used in production code. |
| 4611 | if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) { |
| 4612 | WARN("TRACE_PRINTK", |
| 4613 | "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr); |
| 4614 | } |
| 4615 | |
Andy Lutomirski | 91c9afa | 2015-04-16 12:44:44 -0700 | [diff] [blame] | 4616 | # ENOSYS means "bad syscall nr" and nothing else. This will have a small |
| 4617 | # number of false positives, but assembly files are not checked, so at |
| 4618 | # least the arch entry code will not trigger this warning. |
| 4619 | if ($line =~ /\bENOSYS\b/) { |
| 4620 | WARN("ENOSYS", |
| 4621 | "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); |
| 4622 | } |
| 4623 | |
Jakub Kicinski | 6b9ea5f | 2020-05-11 10:08:07 -0700 | [diff] [blame] | 4624 | # ENOTSUPP is not a standard error code and should be avoided in new patches. |
| 4625 | # Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP. |
| 4626 | # Similarly to ENOSYS warning a small number of false positives is expected. |
| 4627 | if (!$file && $line =~ /\bENOTSUPP\b/) { |
| 4628 | if (WARN("ENOTSUPP", |
| 4629 | "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) && |
| 4630 | $fix) { |
| 4631 | $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/; |
| 4632 | } |
| 4633 | } |
| 4634 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4635 | # function brace can't be on same line, except for #defines of do while, |
| 4636 | # or if closed on same line |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 4637 | if ($perl_version_ok && |
Joe Perches | 2d453e3 | 2018-02-06 15:39:09 -0800 | [diff] [blame] | 4638 | $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && |
| 4639 | $sline !~ /\#\s*define\b.*do\s*\{/ && |
| 4640 | $sline !~ /}/) { |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 4641 | if (ERROR("OPEN_BRACE", |
Joe Perches | 2d453e3 | 2018-02-06 15:39:09 -0800 | [diff] [blame] | 4642 | "open brace '{' following function definitions go on the next line\n" . $herecurr) && |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 4643 | $fix) { |
| 4644 | fix_delete_line($fixlinenr, $rawline); |
| 4645 | my $fixed_line = $rawline; |
Dwaipayan Ray | 03f4935 | 2020-12-15 20:45:02 -0800 | [diff] [blame] | 4646 | $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/; |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 4647 | my $line1 = $1; |
| 4648 | my $line2 = $2; |
| 4649 | fix_insert_line($fixlinenr, ltrim($line1)); |
| 4650 | fix_insert_line($fixlinenr, "\+{"); |
| 4651 | if ($line2 !~ /^\s*$/) { |
| 4652 | fix_insert_line($fixlinenr, "\+\t" . trim($line2)); |
| 4653 | } |
| 4654 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4655 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4656 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4657 | # open braces for enum, union and struct go on the same line. |
| 4658 | if ($line =~ /^.\s*{/ && |
| 4659 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 4660 | if (ERROR("OPEN_BRACE", |
| 4661 | "open brace '{' following $1 go on the same line\n" . $hereprev) && |
| 4662 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4663 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4664 | fix_delete_line($fixlinenr, $rawline); |
| 4665 | my $fixedline = rtrim($prevrawline) . " {"; |
| 4666 | fix_insert_line($fixlinenr, $fixedline); |
| 4667 | $fixedline = $rawline; |
Cyril Bur | 8d81ae0 | 2017-07-10 15:52:21 -0700 | [diff] [blame] | 4668 | $fixedline =~ s/^(.\s*)\{\s*/$1\t/; |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 4669 | if ($fixedline !~ /^\+\s*$/) { |
| 4670 | fix_insert_line($fixlinenr, $fixedline); |
| 4671 | } |
| 4672 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4673 | } |
| 4674 | |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 4675 | # missing space after union, struct or enum definition |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4676 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { |
| 4677 | if (WARN("SPACING", |
| 4678 | "missing space after $1 definition\n" . $herecurr) && |
| 4679 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4680 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4681 | s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; |
| 4682 | } |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 4683 | } |
| 4684 | |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 4685 | # Function pointer declarations |
| 4686 | # check spacing between type, funcptr, and args |
| 4687 | # canonical declaration is "type (*funcptr)(args...)" |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 4688 | if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 4689 | my $declare = $1; |
| 4690 | my $pre_pointer_space = $2; |
| 4691 | my $post_pointer_space = $3; |
| 4692 | my $funcname = $4; |
| 4693 | my $post_funcname_space = $5; |
| 4694 | my $pre_args_space = $6; |
| 4695 | |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 4696 | # the $Declare variable will capture all spaces after the type |
| 4697 | # so check it for a missing trailing missing space but pointer return types |
| 4698 | # don't need a space so don't warn for those. |
| 4699 | my $post_declare_space = ""; |
| 4700 | if ($declare =~ /(\s+)$/) { |
| 4701 | $post_declare_space = $1; |
| 4702 | $declare = rtrim($declare); |
| 4703 | } |
| 4704 | if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 4705 | WARN("SPACING", |
| 4706 | "missing space after return type\n" . $herecurr); |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 4707 | $post_declare_space = " "; |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | # unnecessary space "type (*funcptr)(args...)" |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 4711 | # This test is not currently implemented because these declarations are |
| 4712 | # equivalent to |
| 4713 | # int foo(int bar, ...) |
| 4714 | # and this is form shouldn't/doesn't generate a checkpatch warning. |
| 4715 | # |
| 4716 | # elsif ($declare =~ /\s{2,}$/) { |
| 4717 | # WARN("SPACING", |
| 4718 | # "Multiple spaces after return type\n" . $herecurr); |
| 4719 | # } |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 4720 | |
| 4721 | # unnecessary space "type ( *funcptr)(args...)" |
| 4722 | if (defined $pre_pointer_space && |
| 4723 | $pre_pointer_space =~ /^\s/) { |
| 4724 | WARN("SPACING", |
| 4725 | "Unnecessary space after function pointer open parenthesis\n" . $herecurr); |
| 4726 | } |
| 4727 | |
| 4728 | # unnecessary space "type (* funcptr)(args...)" |
| 4729 | if (defined $post_pointer_space && |
| 4730 | $post_pointer_space =~ /^\s/) { |
| 4731 | WARN("SPACING", |
| 4732 | "Unnecessary space before function pointer name\n" . $herecurr); |
| 4733 | } |
| 4734 | |
| 4735 | # unnecessary space "type (*funcptr )(args...)" |
| 4736 | if (defined $post_funcname_space && |
| 4737 | $post_funcname_space =~ /^\s/) { |
| 4738 | WARN("SPACING", |
| 4739 | "Unnecessary space after function pointer name\n" . $herecurr); |
| 4740 | } |
| 4741 | |
| 4742 | # unnecessary space "type (*funcptr) (args...)" |
| 4743 | if (defined $pre_args_space && |
| 4744 | $pre_args_space =~ /^\s/) { |
| 4745 | WARN("SPACING", |
| 4746 | "Unnecessary space before function pointer arguments\n" . $herecurr); |
| 4747 | } |
| 4748 | |
| 4749 | if (show_type("SPACING") && $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4750 | $fixed[$fixlinenr] =~ |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 4751 | s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 4752 | } |
| 4753 | } |
| 4754 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 4755 | # check for spacing round square brackets; allowed: |
| 4756 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 4757 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 4758 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 4759 | while ($line =~ /(.*?\s)\[/g) { |
| 4760 | my ($where, $prefix) = ($-[1], $1); |
| 4761 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 4762 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Heinrich Schuchardt | 38dca98 | 2018-04-10 16:34:14 -0700 | [diff] [blame] | 4763 | $prefix !~ /[{,:]\s+$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4764 | if (ERROR("BRACKET_SPACE", |
| 4765 | "space prohibited before open square bracket '['\n" . $herecurr) && |
| 4766 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4767 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4768 | s/^(\+.*?)\s+\[/$1\[/; |
| 4769 | } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 4770 | } |
| 4771 | } |
| 4772 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4773 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4774 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4775 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4776 | my $ctx_before = substr($line, 0, $-[1]); |
| 4777 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4778 | |
| 4779 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4780 | if ($name =~ /^(?: |
| 4781 | if|for|while|switch|return|case| |
| 4782 | volatile|__volatile__| |
| 4783 | __attribute__|format|__extension__| |
| 4784 | asm|__asm__)$/x) |
| 4785 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4786 | # cpp #define statements have non-optional spaces, ie |
| 4787 | # if there is a space between the name and the open |
| 4788 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4789 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4790 | |
| 4791 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4792 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4793 | |
| 4794 | # If this whole things ends with a type its most |
| 4795 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4796 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4797 | |
| 4798 | } else { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4799 | if (WARN("SPACING", |
| 4800 | "space prohibited between function name and open parenthesis '('\n" . $herecurr) && |
| 4801 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4802 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4803 | s/\b$name\s+\(/$name\(/; |
| 4804 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4805 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4806 | } |
Eric Nelson | 9a4cad4 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 4807 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4808 | # Check operator spacing. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4809 | if (!($line=~/\#\s*include/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4810 | my $fixed_line = ""; |
| 4811 | my $line_fixed = 0; |
| 4812 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4813 | my $ops = qr{ |
| 4814 | <<=|>>=|<=|>=|==|!=| |
| 4815 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 4816 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4817 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 4818 | \?:|\?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4819 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4820 | my @elements = split(/($ops|;)/, $opline); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4821 | |
| 4822 | ## print("element count: <" . $#elements . ">\n"); |
| 4823 | ## foreach my $el (@elements) { |
| 4824 | ## print("el: <$el>\n"); |
| 4825 | ## } |
| 4826 | |
| 4827 | my @fix_elements = (); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4828 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4829 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4830 | foreach my $el (@elements) { |
| 4831 | push(@fix_elements, substr($rawline, $off, length($el))); |
| 4832 | $off += length($el); |
| 4833 | } |
| 4834 | |
| 4835 | $off = 0; |
| 4836 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4837 | my $blank = copy_spacing($opline); |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4838 | my $last_after = -1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4839 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4840 | for (my $n = 0; $n < $#elements; $n += 2) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4841 | |
| 4842 | my $good = $fix_elements[$n] . $fix_elements[$n + 1]; |
| 4843 | |
| 4844 | ## print("n: <$n> good: <$good>\n"); |
| 4845 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4846 | $off += length($elements[$n]); |
| 4847 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4848 | # Pick up the preceding and succeeding characters. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4849 | my $ca = substr($opline, 0, $off); |
| 4850 | my $cc = ''; |
| 4851 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 4852 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 4853 | } |
| 4854 | my $cb = "$ca$;$cc"; |
| 4855 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4856 | my $a = ''; |
| 4857 | $a = 'V' if ($elements[$n] ne ''); |
| 4858 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4859 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4860 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 4861 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4862 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4863 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4864 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4865 | |
| 4866 | my $c = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4867 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4868 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 4869 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4870 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4871 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 4872 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 8b1b337 | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 4873 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4874 | } else { |
| 4875 | $c = 'E'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4876 | } |
| 4877 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4878 | my $ctx = "${a}x${c}"; |
| 4879 | |
| 4880 | my $at = "(ctx:$ctx)"; |
| 4881 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4882 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4883 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4884 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4885 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4886 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4887 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4888 | # Get the full operator variant. |
| 4889 | my $opv = $op . substr($curr_vars, $off, 1); |
| 4890 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4891 | # Ignore operators passed as parameters. |
| 4892 | if ($op_type ne 'V' && |
Sam Bobroff | d7fe806 | 2015-04-16 12:44:39 -0700 | [diff] [blame] | 4893 | $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4894 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4895 | # # Ignore comments |
| 4896 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4897 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4898 | # ; should have either the end of line or a space or \ after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4899 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4900 | if ($ctx !~ /.x[WEBC]/ && |
| 4901 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4902 | if (ERROR("SPACING", |
| 4903 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4904 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4905 | $line_fixed = 1; |
| 4906 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | # // is a comment |
| 4910 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4911 | |
Joe Perches | b00e481 | 2014-04-03 14:49:33 -0700 | [diff] [blame] | 4912 | # : when part of a bitfield |
| 4913 | } elsif ($opv eq ':B') { |
| 4914 | # skip the bitfield test for now |
| 4915 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4916 | # No spaces for: |
| 4917 | # -> |
Joe Perches | b00e481 | 2014-04-03 14:49:33 -0700 | [diff] [blame] | 4918 | } elsif ($op eq '->') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4919 | if ($ctx =~ /Wx.|.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4920 | if (ERROR("SPACING", |
| 4921 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4922 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4923 | if (defined $fix_elements[$n + 2]) { |
| 4924 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4925 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4926 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4927 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4928 | } |
| 4929 | |
Joe Perches | 2381097 | 2014-12-10 15:51:32 -0800 | [diff] [blame] | 4930 | # , must not have a space before and must have a space on the right. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4931 | } elsif ($op eq ',') { |
Joe Perches | 2381097 | 2014-12-10 15:51:32 -0800 | [diff] [blame] | 4932 | my $rtrim_before = 0; |
| 4933 | my $space_after = 0; |
| 4934 | if ($ctx =~ /Wx./) { |
| 4935 | if (ERROR("SPACING", |
| 4936 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 4937 | $line_fixed = 1; |
| 4938 | $rtrim_before = 1; |
| 4939 | } |
| 4940 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4941 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4942 | if (ERROR("SPACING", |
| 4943 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4944 | $line_fixed = 1; |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4945 | $last_after = $n; |
Joe Perches | 2381097 | 2014-12-10 15:51:32 -0800 | [diff] [blame] | 4946 | $space_after = 1; |
| 4947 | } |
| 4948 | } |
| 4949 | if ($rtrim_before || $space_after) { |
| 4950 | if ($rtrim_before) { |
| 4951 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 4952 | } else { |
| 4953 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
| 4954 | } |
| 4955 | if ($space_after) { |
| 4956 | $good .= " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4957 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4958 | } |
| 4959 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4960 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4961 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4962 | #warn "'*' is part of type\n"; |
| 4963 | |
| 4964 | # unary operators should have a space before and |
| 4965 | # none after. May be left adjacent to another |
| 4966 | # unary operator, or a cast |
| 4967 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4968 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 4969 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4970 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4971 | if (ERROR("SPACING", |
| 4972 | "space required before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4973 | if ($n != $last_after + 2) { |
| 4974 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
| 4975 | $line_fixed = 1; |
| 4976 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4977 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4978 | } |
Andy Whitcroft | a3340b3 | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 4979 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4980 | # A unary '*' may be const |
| 4981 | |
| 4982 | } elsif ($ctx =~ /.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4983 | if (ERROR("SPACING", |
| 4984 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4985 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4986 | if (defined $fix_elements[$n + 2]) { |
| 4987 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4988 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4989 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4990 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | # unary ++ and unary -- are allowed no space on one side. |
| 4994 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4995 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4996 | if (ERROR("SPACING", |
| 4997 | "space required one side of that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4998 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4999 | $line_fixed = 1; |
| 5000 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5001 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5002 | if ($ctx =~ /Wx[BE]/ || |
| 5003 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5004 | if (ERROR("SPACING", |
| 5005 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 5006 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5007 | $line_fixed = 1; |
| 5008 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 5009 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5010 | if ($ctx =~ /ExW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5011 | if (ERROR("SPACING", |
| 5012 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 5013 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5014 | if (defined $fix_elements[$n + 2]) { |
| 5015 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5016 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 5017 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5018 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5019 | } |
| 5020 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5021 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 5022 | } elsif ($op eq '<<' or $op eq '>>' or |
| 5023 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 5024 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 5025 | $op eq '*' or $op eq '/' or |
| 5026 | $op eq '%') |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5027 | { |
Joe Perches | d2e025f | 2015-02-13 14:38:57 -0800 | [diff] [blame] | 5028 | if ($check) { |
| 5029 | if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { |
| 5030 | if (CHK("SPACING", |
| 5031 | "spaces preferred around that '$op' $at\n" . $hereptr)) { |
| 5032 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 5033 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5034 | $line_fixed = 1; |
| 5035 | } |
| 5036 | } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { |
| 5037 | if (CHK("SPACING", |
| 5038 | "space preferred before that '$op' $at\n" . $hereptr)) { |
| 5039 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); |
| 5040 | $line_fixed = 1; |
| 5041 | } |
| 5042 | } |
| 5043 | } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5044 | if (ERROR("SPACING", |
| 5045 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 5046 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 5047 | if (defined $fix_elements[$n + 2]) { |
| 5048 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5049 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5050 | $line_fixed = 1; |
| 5051 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5052 | } |
| 5053 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 5054 | # A colon needs no spaces before when it is |
| 5055 | # terminating a case value or a label. |
| 5056 | } elsif ($opv eq ':C' || $opv eq ':L') { |
Chris Down | 263afd3 | 2021-02-25 17:22:04 -0800 | [diff] [blame] | 5057 | if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5058 | if (ERROR("SPACING", |
| 5059 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 5060 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5061 | $line_fixed = 1; |
| 5062 | } |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 5063 | } |
| 5064 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5065 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5066 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 5067 | my $ok = 0; |
| 5068 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5069 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 5070 | if (($op eq '<' && |
| 5071 | $cc =~ /^\S+\@\S+>/) || |
| 5072 | ($op eq '>' && |
| 5073 | $ca =~ /<\S+\@\S+$/)) |
| 5074 | { |
Antonio Borneo | 342d3d2 | 2020-04-06 20:11:01 -0700 | [diff] [blame] | 5075 | $ok = 1; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 5076 | } |
| 5077 | |
Joe Perches | e0df7e1 | 2015-04-16 12:44:53 -0700 | [diff] [blame] | 5078 | # for asm volatile statements |
| 5079 | # ignore a colon with another |
| 5080 | # colon immediately before or after |
| 5081 | if (($op eq ':') && |
| 5082 | ($ca =~ /:$/ || $cc =~ /^:/)) { |
| 5083 | $ok = 1; |
| 5084 | } |
| 5085 | |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 5086 | # messages are ERROR, but ?: are CHK |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 5087 | if ($ok == 0) { |
Jean Delvare | 0675a8f | 2017-09-08 16:16:07 -0700 | [diff] [blame] | 5088 | my $msg_level = \&ERROR; |
| 5089 | $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 5090 | |
Jean Delvare | 0675a8f | 2017-09-08 16:16:07 -0700 | [diff] [blame] | 5091 | if (&{$msg_level}("SPACING", |
| 5092 | "spaces required around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 5093 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 5094 | if (defined $fix_elements[$n + 2]) { |
| 5095 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 5096 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5097 | $line_fixed = 1; |
| 5098 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5099 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5100 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 5101 | $off += length($elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5102 | |
| 5103 | ## print("n: <$n> GOOD: <$good>\n"); |
| 5104 | |
| 5105 | $fixed_line = $fixed_line . $good; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5106 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5107 | |
| 5108 | if (($#elements % 2) == 0) { |
| 5109 | $fixed_line = $fixed_line . $fix_elements[$#elements]; |
| 5110 | } |
| 5111 | |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5112 | if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { |
| 5113 | $fixed[$fixlinenr] = $fixed_line; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5117 | } |
| 5118 | |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 5119 | # check for whitespace before a non-naked semicolon |
Joe Perches | d2e248e | 2014-01-23 15:54:41 -0800 | [diff] [blame] | 5120 | if ($line =~ /^\+.*\S\s+;\s*$/) { |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 5121 | if (WARN("SPACING", |
| 5122 | "space prohibited before semicolon\n" . $herecurr) && |
| 5123 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5124 | 1 while $fixed[$fixlinenr] =~ |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 5125 | s/^(\+.*\S)\s+;/$1;/; |
| 5126 | } |
| 5127 | } |
| 5128 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 5129 | # check for multiple assignments |
| 5130 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5131 | CHK("MULTIPLE_ASSIGNMENTS", |
| 5132 | "multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 5133 | } |
| 5134 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5135 | ## # check for multiple declarations, allowing for a function declaration |
| 5136 | ## # continuation. |
| 5137 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 5138 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 5139 | ## |
| 5140 | ## # Remove any bracketed sections to ensure we do not |
Dwaipayan Ray | e73d271 | 2020-12-15 20:44:56 -0800 | [diff] [blame] | 5141 | ## # falsely report the parameters of functions. |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5142 | ## my $ln = $line; |
| 5143 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 5144 | ## } |
| 5145 | ## if ($ln =~ /,/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5146 | ## WARN("MULTIPLE_DECLARATION", |
| 5147 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5148 | ## } |
| 5149 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 5150 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5151 | #need space before brace following if, while, etc |
Geyslan G. Bem | 6b8c69e | 2016-03-15 14:58:09 -0700 | [diff] [blame] | 5152 | if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || |
Michal Zylowski | 6ad724e | 2018-08-21 21:58:08 -0700 | [diff] [blame] | 5153 | $line =~ /\b(?:else|do)\{/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5154 | if (ERROR("SPACING", |
| 5155 | "space required before the open brace '{'\n" . $herecurr) && |
| 5156 | $fix) { |
Michal Zylowski | 6ad724e | 2018-08-21 21:58:08 -0700 | [diff] [blame] | 5157 | $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5158 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5159 | } |
| 5160 | |
Joe Perches | c4a62ef | 2013-07-03 15:05:28 -0700 | [diff] [blame] | 5161 | ## # check for blank lines before declarations |
| 5162 | ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && |
| 5163 | ## $prevrawline =~ /^.\s*$/) { |
| 5164 | ## WARN("SPACING", |
| 5165 | ## "No blank lines before declarations\n" . $hereprev); |
| 5166 | ## } |
| 5167 | ## |
| 5168 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5169 | # closing brace should have a space following it when it has anything |
| 5170 | # on the line |
Joe Perches | 94fb984 | 2019-09-25 16:46:47 -0700 | [diff] [blame] | 5171 | if ($line =~ /}(?!(?:,|;|\)|\}))\S/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5172 | if (ERROR("SPACING", |
| 5173 | "space required after that close brace '}'\n" . $herecurr) && |
| 5174 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5175 | $fixed[$fixlinenr] =~ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5176 | s/}((?!(?:,|;|\)))\S)/} $1/; |
| 5177 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5178 | } |
| 5179 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5180 | # check spacing on square brackets |
| 5181 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5182 | if (ERROR("SPACING", |
| 5183 | "space prohibited after that open square bracket '['\n" . $herecurr) && |
| 5184 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5185 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5186 | s/\[\s+/\[/; |
| 5187 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5188 | } |
| 5189 | if ($line =~ /\s\]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5190 | if (ERROR("SPACING", |
| 5191 | "space prohibited before that close square bracket ']'\n" . $herecurr) && |
| 5192 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5193 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5194 | s/\s+\]/\]/; |
| 5195 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5196 | } |
| 5197 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5198 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 5199 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 5200 | $line !~ /for\s*\(\s+;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5201 | if (ERROR("SPACING", |
| 5202 | "space prohibited after that open parenthesis '('\n" . $herecurr) && |
| 5203 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5204 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5205 | s/\(\s+/\(/; |
| 5206 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5207 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5208 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5209 | $line !~ /for\s*\(.*;\s+\)/ && |
| 5210 | $line !~ /:\s+\)/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5211 | if (ERROR("SPACING", |
| 5212 | "space prohibited before that close parenthesis ')'\n" . $herecurr) && |
| 5213 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5214 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5215 | s/\s+\)/\)/; |
| 5216 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5217 | } |
| 5218 | |
Joe Perches | e2826fd | 2014-08-06 16:10:48 -0700 | [diff] [blame] | 5219 | # check unnecessary parentheses around addressof/dereference single $Lvals |
| 5220 | # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar |
| 5221 | |
| 5222 | while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { |
Joe Perches | ea4acbb | 2014-12-10 15:51:51 -0800 | [diff] [blame] | 5223 | my $var = $1; |
| 5224 | if (CHK("UNNECESSARY_PARENTHESES", |
| 5225 | "Unnecessary parentheses around $var\n" . $herecurr) && |
| 5226 | $fix) { |
| 5227 | $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; |
| 5228 | } |
| 5229 | } |
| 5230 | |
| 5231 | # check for unnecessary parentheses around function pointer uses |
| 5232 | # ie: (foo->bar)(); should be foo->bar(); |
| 5233 | # but not "if (foo->bar) (" to avoid some false positives |
| 5234 | if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { |
| 5235 | my $var = $2; |
| 5236 | if (CHK("UNNECESSARY_PARENTHESES", |
| 5237 | "Unnecessary parentheses around function pointer $var\n" . $herecurr) && |
| 5238 | $fix) { |
| 5239 | my $var2 = deparenthesize($var); |
| 5240 | $var2 =~ s/\s//g; |
| 5241 | $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; |
| 5242 | } |
| 5243 | } |
Joe Perches | e2826fd | 2014-08-06 16:10:48 -0700 | [diff] [blame] | 5244 | |
Joe Perches | 63b7c73 | 2017-09-08 16:16:01 -0700 | [diff] [blame] | 5245 | # check for unnecessary parentheses around comparisons in if uses |
Joe Perches | a032aa4 | 2018-02-06 15:39:03 -0800 | [diff] [blame] | 5246 | # when !drivers/staging or command-line uses --strict |
| 5247 | if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) && |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 5248 | $perl_version_ok && defined($stat) && |
Joe Perches | 63b7c73 | 2017-09-08 16:16:01 -0700 | [diff] [blame] | 5249 | $stat =~ /(^.\s*if\s*($balanced_parens))/) { |
| 5250 | my $if_stat = $1; |
| 5251 | my $test = substr($2, 1, -1); |
| 5252 | my $herectx; |
| 5253 | while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) { |
| 5254 | my $match = $1; |
| 5255 | # avoid parentheses around potential macro args |
| 5256 | next if ($match =~ /^\s*\w+\s*$/); |
| 5257 | if (!defined($herectx)) { |
| 5258 | $herectx = $here . "\n"; |
| 5259 | my $cnt = statement_rawlines($if_stat); |
| 5260 | for (my $n = 0; $n < $cnt; $n++) { |
| 5261 | my $rl = raw_line($linenr, $n); |
| 5262 | $herectx .= $rl . "\n"; |
| 5263 | last if $rl =~ /^[ \+].*\{/; |
| 5264 | } |
| 5265 | } |
| 5266 | CHK("UNNECESSARY_PARENTHESES", |
| 5267 | "Unnecessary parentheses around '$match'\n" . $herectx); |
| 5268 | } |
| 5269 | } |
| 5270 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5271 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 5272 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5273 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5274 | if (WARN("INDENTED_LABEL", |
| 5275 | "labels should not be indented\n" . $herecurr) && |
| 5276 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5277 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5278 | s/^(.)\s+/$1/; |
| 5279 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5280 | } |
| 5281 | |
Joe Perches | 40873ab | 2020-10-15 20:11:56 -0700 | [diff] [blame] | 5282 | # check if a statement with a comma should be two statements like: |
| 5283 | # foo = bar(), /* comma should be semicolon */ |
| 5284 | # bar = baz(); |
| 5285 | if (defined($stat) && |
| 5286 | $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) { |
| 5287 | my $cnt = statement_rawlines($stat); |
| 5288 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 5289 | WARN("SUSPECT_COMMA_SEMICOLON", |
| 5290 | "Possible comma where semicolon could be used\n" . $herectx); |
| 5291 | } |
| 5292 | |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 5293 | # return is not a function |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 5294 | if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5295 | my $spacing = $1; |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 5296 | if ($perl_version_ok && |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 5297 | $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { |
| 5298 | my $value = $1; |
| 5299 | $value = deparenthesize($value); |
| 5300 | if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { |
| 5301 | ERROR("RETURN_PARENTHESES", |
| 5302 | "return is not a function, parentheses are not required\n" . $herecurr); |
| 5303 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5304 | } elsif ($spacing !~ /\s+/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5305 | ERROR("SPACING", |
| 5306 | "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5307 | } |
| 5308 | } |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 5309 | |
Joe Perches | b43ae21 | 2014-06-23 13:22:07 -0700 | [diff] [blame] | 5310 | # unnecessary return in a void function |
| 5311 | # at end-of-function, with the previous line a single leading tab, then return; |
| 5312 | # and the line before that not a goto label target like "out:" |
| 5313 | if ($sline =~ /^[ \+]}\s*$/ && |
| 5314 | $prevline =~ /^\+\treturn\s*;\s*$/ && |
| 5315 | $linenr >= 3 && |
| 5316 | $lines[$linenr - 3] =~ /^[ +]/ && |
| 5317 | $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { |
Joe Perches | 9819cf2 | 2014-06-04 16:12:09 -0700 | [diff] [blame] | 5318 | WARN("RETURN_VOID", |
Joe Perches | b43ae21 | 2014-06-23 13:22:07 -0700 | [diff] [blame] | 5319 | "void function return statements are not generally useful\n" . $hereprev); |
Dwaipayan Ray | ea7dbab | 2021-02-25 17:21:47 -0800 | [diff] [blame] | 5320 | } |
Joe Perches | 9819cf2 | 2014-06-04 16:12:09 -0700 | [diff] [blame] | 5321 | |
Joe Perches | 189248d | 2014-01-23 15:54:47 -0800 | [diff] [blame] | 5322 | # if statements using unnecessary parentheses - ie: if ((foo == bar)) |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 5323 | if ($perl_version_ok && |
Joe Perches | 189248d | 2014-01-23 15:54:47 -0800 | [diff] [blame] | 5324 | $line =~ /\bif\s*((?:\(\s*){2,})/) { |
| 5325 | my $openparens = $1; |
| 5326 | my $count = $openparens =~ tr@\(@\(@; |
| 5327 | my $msg = ""; |
| 5328 | if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { |
| 5329 | my $comp = $4; #Not $1 because of $LvalOrFunc |
| 5330 | $msg = " - maybe == should be = ?" if ($comp eq "=="); |
| 5331 | WARN("UNNECESSARY_PARENTHESES", |
| 5332 | "Unnecessary parentheses$msg\n" . $herecurr); |
| 5333 | } |
| 5334 | } |
| 5335 | |
Joe Perches | c5595fa | 2015-09-09 15:37:58 -0700 | [diff] [blame] | 5336 | # comparisons with a constant or upper case identifier on the left |
| 5337 | # avoid cases like "foo + BAR < baz" |
| 5338 | # only fix matches surrounded by parentheses to avoid incorrect |
| 5339 | # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 5340 | if ($perl_version_ok && |
Joe Perches | c5595fa | 2015-09-09 15:37:58 -0700 | [diff] [blame] | 5341 | $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { |
| 5342 | my $lead = $1; |
| 5343 | my $const = $2; |
| 5344 | my $comp = $3; |
| 5345 | my $to = $4; |
| 5346 | my $newcomp = $comp; |
Joe Perches | f39e176 | 2016-05-20 17:04:02 -0700 | [diff] [blame] | 5347 | if ($lead !~ /(?:$Operators|\.)\s*$/ && |
Joe Perches | c5595fa | 2015-09-09 15:37:58 -0700 | [diff] [blame] | 5348 | $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && |
| 5349 | WARN("CONSTANT_COMPARISON", |
| 5350 | "Comparisons should place the constant on the right side of the test\n" . $herecurr) && |
| 5351 | $fix) { |
| 5352 | if ($comp eq "<") { |
| 5353 | $newcomp = ">"; |
| 5354 | } elsif ($comp eq "<=") { |
| 5355 | $newcomp = ">="; |
| 5356 | } elsif ($comp eq ">") { |
| 5357 | $newcomp = "<"; |
| 5358 | } elsif ($comp eq ">=") { |
| 5359 | $newcomp = "<="; |
| 5360 | } |
| 5361 | $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; |
| 5362 | } |
| 5363 | } |
| 5364 | |
Joe Perches | f34e4a4 | 2015-04-16 12:44:19 -0700 | [diff] [blame] | 5365 | # Return of what appears to be an errno should normally be negative |
| 5366 | if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 5367 | my $name = $1; |
| 5368 | if ($name ne 'EOF' && $name ne 'ERROR') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5369 | WARN("USE_NEGATIVE_ERRNO", |
Joe Perches | f34e4a4 | 2015-04-16 12:44:19 -0700 | [diff] [blame] | 5370 | "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 5371 | } |
| 5372 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5373 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5374 | # Need a space before open parenthesis after if, while etc |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5375 | if ($line =~ /\b(if|while|for|switch)\(/) { |
| 5376 | if (ERROR("SPACING", |
| 5377 | "space required before the open parenthesis '('\n" . $herecurr) && |
| 5378 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5379 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5380 | s/\b(if|while|for|switch)\(/$1 \(/; |
| 5381 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5382 | } |
| 5383 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 5384 | # Check for illegal assignment in if conditional -- and check for trailing |
| 5385 | # statements after the conditional. |
Andy Whitcroft | 170d3a22 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 5386 | if ($line =~ /do\s*(?!{)/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 5387 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 5388 | ctx_statement_block($linenr, $realcnt, 0) |
| 5389 | if (!defined $stat); |
Andy Whitcroft | 170d3a22 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 5390 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 5391 | $remain_next, $off_next); |
| 5392 | $stat_next =~ s/\n./\n /g; |
| 5393 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 5394 | |
| 5395 | if ($stat_next =~ /^\s*while\b/) { |
| 5396 | # If the statement carries leading newlines, |
| 5397 | # then count those as offsets. |
| 5398 | my ($whitespace) = |
| 5399 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 5400 | my $offset = |
| 5401 | statement_rawlines($whitespace) - 1; |
| 5402 | |
| 5403 | $suppress_whiletrailers{$line_nr_next + |
| 5404 | $offset} = 1; |
| 5405 | } |
| 5406 | } |
| 5407 | if (!defined $suppress_whiletrailers{$linenr} && |
Joe Perches | c11230f | 2013-11-21 14:31:57 -0800 | [diff] [blame] | 5408 | defined($stat) && defined($cond) && |
Andy Whitcroft | 170d3a22 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 5409 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5410 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5411 | |
Andy Whitcroft | b53c8e1 | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 5412 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Joe Perches | 65b64b3 | 2020-08-11 18:35:10 -0700 | [diff] [blame] | 5413 | if (ERROR("ASSIGN_IN_IF", |
| 5414 | "do not use assignment in if condition\n" . $herecurr) && |
| 5415 | $fix && $perl_version_ok) { |
| 5416 | if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) { |
| 5417 | my $space = $1; |
| 5418 | my $not = $2; |
| 5419 | my $statement = $3; |
| 5420 | my $assigned = $4; |
| 5421 | my $test = $8; |
| 5422 | my $against = $9; |
| 5423 | my $brace = $15; |
| 5424 | fix_delete_line($fixlinenr, $rawline); |
| 5425 | fix_insert_line($fixlinenr, "$space$statement;"); |
| 5426 | my $newline = "${space}if ("; |
| 5427 | $newline .= '!' if defined($not); |
| 5428 | $newline .= '(' if (defined $not && defined($test) && defined($against)); |
| 5429 | $newline .= "$assigned"; |
| 5430 | $newline .= " $test $against" if (defined($test) && defined($against)); |
| 5431 | $newline .= ')' if (defined $not && defined($test) && defined($against)); |
| 5432 | $newline .= ')'; |
| 5433 | $newline .= " {" if (defined($brace)); |
| 5434 | fix_insert_line($fixlinenr + 1, $newline); |
| 5435 | } |
| 5436 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5437 | } |
| 5438 | |
| 5439 | # Find out what is on the end of the line after the |
| 5440 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5441 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5442 | $s =~ s/\n.*//g; |
Antonio Borneo | 342d3d2 | 2020-04-06 20:11:01 -0700 | [diff] [blame] | 5443 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 5444 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 5445 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5446 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 5447 | # Find out how long the conditional actually is. |
| 5448 | my @newlines = ($c =~ /\n/gs); |
| 5449 | my $cond_lines = 1 + $#newlines; |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 5450 | my $stat_real = ''; |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 5451 | |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 5452 | $stat_real = raw_line($linenr, $cond_lines) |
| 5453 | . "\n" if ($cond_lines); |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 5454 | if (defined($stat_real) && $cond_lines > 1) { |
| 5455 | $stat_real = "[...]\n$stat_real"; |
| 5456 | } |
| 5457 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5458 | ERROR("TRAILING_STATEMENTS", |
| 5459 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5460 | } |
| 5461 | } |
| 5462 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5463 | # Check for bitwise tests written as boolean |
| 5464 | if ($line =~ / |
| 5465 | (?: |
| 5466 | (?:\[|\(|\&\&|\|\|) |
| 5467 | \s*0[xX][0-9]+\s* |
| 5468 | (?:\&\&|\|\|) |
| 5469 | | |
| 5470 | (?:\&\&|\|\|) |
| 5471 | \s*0[xX][0-9]+\s* |
| 5472 | (?:\&\&|\|\||\)|\]) |
| 5473 | )/x) |
| 5474 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5475 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 5476 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5477 | } |
| 5478 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5479 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5480 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 5481 | my $s = $1; |
Antonio Borneo | 342d3d2 | 2020-04-06 20:11:01 -0700 | [diff] [blame] | 5482 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5483 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5484 | ERROR("TRAILING_STATEMENTS", |
| 5485 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5486 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5487 | } |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 5488 | # if should not continue a brace |
| 5489 | if ($line =~ /}\s*if\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5490 | ERROR("TRAILING_STATEMENTS", |
Rasmus Villemoes | 048b123 | 2014-08-06 16:10:37 -0700 | [diff] [blame] | 5491 | "trailing statements should be on next line (or did you mean 'else if'?)\n" . |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 5492 | $herecurr); |
| 5493 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 5494 | # case and default should not have general statements after them |
| 5495 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 5496 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 5497 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 5498 | \s*return\s+ |
| 5499 | )/xg) |
| 5500 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5501 | ERROR("TRAILING_STATEMENTS", |
| 5502 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 5503 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5504 | |
| 5505 | # Check for }<nl>else {, these must be at the same |
| 5506 | # indent level to be relevant to each other. |
Joe Perches | 8b8856f | 2014-08-06 16:11:14 -0700 | [diff] [blame] | 5507 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && |
| 5508 | $previndent == $indent) { |
| 5509 | if (ERROR("ELSE_AFTER_BRACE", |
| 5510 | "else should follow close brace '}'\n" . $hereprev) && |
| 5511 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 5512 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5513 | fix_delete_line($fixlinenr, $rawline); |
| 5514 | my $fixedline = $prevrawline; |
| 5515 | $fixedline =~ s/}\s*$//; |
| 5516 | if ($fixedline !~ /^\+\s*$/) { |
| 5517 | fix_insert_line($fixlinenr, $fixedline); |
| 5518 | } |
| 5519 | $fixedline = $rawline; |
| 5520 | $fixedline =~ s/^(.\s*)else/$1} else/; |
| 5521 | fix_insert_line($fixlinenr, $fixedline); |
| 5522 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5523 | } |
| 5524 | |
Joe Perches | 8b8856f | 2014-08-06 16:11:14 -0700 | [diff] [blame] | 5525 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && |
| 5526 | $previndent == $indent) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 5527 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 5528 | |
| 5529 | # Find out what is on the end of the line after the |
| 5530 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5531 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 5532 | $s =~ s/\n.*//g; |
| 5533 | |
| 5534 | if ($s =~ /^\s*;/) { |
Joe Perches | 8b8856f | 2014-08-06 16:11:14 -0700 | [diff] [blame] | 5535 | if (ERROR("WHILE_AFTER_BRACE", |
| 5536 | "while should follow close brace '}'\n" . $hereprev) && |
| 5537 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 5538 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5539 | fix_delete_line($fixlinenr, $rawline); |
| 5540 | my $fixedline = $prevrawline; |
| 5541 | my $trailing = $rawline; |
| 5542 | $trailing =~ s/^\+//; |
| 5543 | $trailing = trim($trailing); |
| 5544 | $fixedline =~ s/}\s*$/} $trailing/; |
| 5545 | fix_insert_line($fixlinenr, $fixedline); |
| 5546 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 5547 | } |
| 5548 | } |
| 5549 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 5550 | #Specific variable tests |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 5551 | while ($line =~ m{($Constant|$Lval)}g) { |
| 5552 | my $var = $1; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 5553 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 5554 | #CamelCase |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 5555 | if ($var !~ /^$Constant$/ && |
Joe Perches | be79794 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 5556 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
Łukasz Stelmach | 4104a20 | 2020-12-15 20:44:27 -0800 | [diff] [blame] | 5557 | #Ignore some autogenerated defines and enum values |
| 5558 | $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 5559 | #Ignore Page<foo> variants |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 5560 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Joe Perches | d439e6a | 2019-12-04 16:52:06 -0800 | [diff] [blame] | 5561 | #Ignore SI style variants like nS, mV and dB |
| 5562 | #(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) |
| 5563 | $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && |
Julius Werner | f512357 | 2014-12-10 15:51:54 -0800 | [diff] [blame] | 5564 | #Ignore some three character SI units explicitly, like MiB and KHz |
| 5565 | $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 5566 | while ($var =~ m{($Ident)}g) { |
| 5567 | my $word = $1; |
| 5568 | next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 5569 | if ($check) { |
| 5570 | seed_camelcase_includes(); |
| 5571 | if (!$file && !$camelcase_file_seeded) { |
| 5572 | seed_camelcase_file($realfile); |
| 5573 | $camelcase_file_seeded = 1; |
| 5574 | } |
| 5575 | } |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 5576 | if (!defined $camelcase{$word}) { |
| 5577 | $camelcase{$word} = 1; |
| 5578 | CHK("CAMELCASE", |
| 5579 | "Avoid CamelCase: <$word>\n" . $herecurr); |
| 5580 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 5581 | } |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 5582 | } |
| 5583 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5584 | |
| 5585 | #no spaces allowed after \ in define |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5586 | if ($line =~ /\#\s*define.*\\\s+$/) { |
| 5587 | if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 5588 | "Whitespace after \\ makes next lines useless\n" . $herecurr) && |
| 5589 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5590 | $fixed[$fixlinenr] =~ s/\s+$//; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5591 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5592 | } |
| 5593 | |
Fabian Frederick | 0e212e0 | 2015-04-16 12:44:25 -0700 | [diff] [blame] | 5594 | # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes |
| 5595 | # itself <asm/foo.h> (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5596 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 5597 | my $file = "$1.h"; |
| 5598 | my $checkfile = "include/linux/$file"; |
| 5599 | if (-f "$root/$checkfile" && |
| 5600 | $realfile ne $checkfile && |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 5601 | $1 !~ /$allowed_asm_includes/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5602 | { |
Fabian Frederick | 0e212e0 | 2015-04-16 12:44:25 -0700 | [diff] [blame] | 5603 | my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; |
| 5604 | if ($asminclude > 0) { |
| 5605 | if ($realfile =~ m{^arch/}) { |
| 5606 | CHK("ARCH_INCLUDE_LINUX", |
| 5607 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 5608 | } else { |
| 5609 | WARN("INCLUDE_LINUX", |
| 5610 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 5611 | } |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 5612 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5613 | } |
| 5614 | } |
| 5615 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 5616 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 5617 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5618 | # in a known good container |
Andy Whitcroft | b8f96a31 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 5619 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 5620 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 5621 | my $ln = $linenr; |
| 5622 | my $cnt = $realcnt; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5623 | my ($off, $dstat, $dcond, $rest); |
| 5624 | my $ctx = ''; |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 5625 | my $has_flow_statement = 0; |
| 5626 | my $has_arg_concat = 0; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5627 | ($dstat, $dcond, $ln, $cnt, $off) = |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 5628 | ctx_statement_block($linenr, $realcnt, 0); |
| 5629 | $ctx = $dstat; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5630 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 5631 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5632 | |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 5633 | $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); |
Joe Perches | 62e15a6 | 2016-01-20 14:59:18 -0800 | [diff] [blame] | 5634 | $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 5635 | |
Joe Perches | f59b64b | 2016-10-11 13:52:08 -0700 | [diff] [blame] | 5636 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//; |
| 5637 | my $define_args = $1; |
| 5638 | my $define_stmt = $dstat; |
| 5639 | my @def_args = (); |
| 5640 | |
| 5641 | if (defined $define_args && $define_args ne "") { |
| 5642 | $define_args = substr($define_args, 1, length($define_args) - 2); |
| 5643 | $define_args =~ s/\s*//g; |
Joe Perches | 8c8c45c | 2018-08-21 21:57:43 -0700 | [diff] [blame] | 5644 | $define_args =~ s/\\\+?//g; |
Joe Perches | f59b64b | 2016-10-11 13:52:08 -0700 | [diff] [blame] | 5645 | @def_args = split(",", $define_args); |
| 5646 | } |
| 5647 | |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 5648 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5649 | $dstat =~ s/\\\n.//g; |
| 5650 | $dstat =~ s/^\s*//s; |
| 5651 | $dstat =~ s/\s*$//s; |
| 5652 | |
| 5653 | # Flatten any parentheses and braces |
Dwaipayan Ray | 2e44e80 | 2020-10-15 20:12:22 -0700 | [diff] [blame] | 5654 | while ($dstat =~ s/\([^\(\)]*\)/1u/ || |
| 5655 | $dstat =~ s/\{[^\{\}]*\}/1u/ || |
| 5656 | $dstat =~ s/.\[[^\[\]]*\]/1u/) |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 5657 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5658 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 5659 | |
Antonio Borneo | 342d3d2 | 2020-04-06 20:11:01 -0700 | [diff] [blame] | 5660 | # Flatten any obvious string concatenation. |
Joe Perches | 33acb54 | 2015-06-25 15:02:54 -0700 | [diff] [blame] | 5661 | while ($dstat =~ s/($String)\s*$Ident/$1/ || |
| 5662 | $dstat =~ s/$Ident\s*($String)/$1/) |
Andy Whitcroft | e45bab8 | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 5663 | { |
| 5664 | } |
| 5665 | |
Joe Perches | 42e1529 | 2016-03-15 14:58:01 -0700 | [diff] [blame] | 5666 | # Make asm volatile uses seem like a generic function |
| 5667 | $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; |
| 5668 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5669 | my $exceptions = qr{ |
| 5670 | $Declare| |
| 5671 | module_param_named| |
Kees Cook | a0a0a7a | 2012-10-04 17:13:38 -0700 | [diff] [blame] | 5672 | MODULE_PARM_DESC| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5673 | DECLARE_PER_CPU| |
| 5674 | DEFINE_PER_CPU| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 5675 | __typeof__\(| |
Stefani Seibold | 22fd2d3 | 2010-03-05 13:43:52 -0800 | [diff] [blame] | 5676 | union| |
| 5677 | struct| |
Andy Whitcroft | ea71a0a | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 5678 | \.$Ident\s*=\s*| |
Vladimir Zapolskiy | 6b10df4 | 2016-01-20 14:59:21 -0800 | [diff] [blame] | 5679 | ^\"|\"$| |
| 5680 | ^\[ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5681 | }x; |
Andy Whitcroft | 5eaa20b | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 5682 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Joe Perches | f59b64b | 2016-10-11 13:52:08 -0700 | [diff] [blame] | 5683 | |
| 5684 | $ctx =~ s/\n*$//; |
Joe Perches | f59b64b | 2016-10-11 13:52:08 -0700 | [diff] [blame] | 5685 | my $stmt_cnt = statement_rawlines($ctx); |
Tobin C. Harding | e3d95a2 | 2018-04-10 16:33:27 -0700 | [diff] [blame] | 5686 | my $herectx = get_stat_here($linenr, $stmt_cnt, $here); |
Joe Perches | f59b64b | 2016-10-11 13:52:08 -0700 | [diff] [blame] | 5687 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 5688 | if ($dstat ne '' && |
| 5689 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 5690 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Joe Perches | 3cc4b1c | 2013-07-03 15:05:27 -0700 | [diff] [blame] | 5691 | $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz |
Joe Perches | 356fd39 | 2014-08-06 16:10:31 -0700 | [diff] [blame] | 5692 | $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 5693 | $dstat !~ /$exceptions/ && |
| 5694 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Joe Perches | e942e2c | 2013-04-17 15:58:26 -0700 | [diff] [blame] | 5695 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
Andy Whitcroft | 72f115f | 2012-01-10 15:10:06 -0800 | [diff] [blame] | 5696 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
Dwaipayan Ray | 2e44e80 | 2020-10-15 20:12:22 -0700 | [diff] [blame] | 5697 | $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ && # while (...) {...} |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 5698 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 5699 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 5700 | $dstat !~ /^do\s*{/ && # do {... |
Eddie Kovsky | 4e5d56b | 2015-09-09 15:37:52 -0700 | [diff] [blame] | 5701 | $dstat !~ /^\(\{/ && # ({... |
Joe Perches | f95a7e6 | 2013-09-11 14:24:00 -0700 | [diff] [blame] | 5702 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 5703 | { |
Joe Perches | e795556 | 2017-05-08 15:55:48 -0700 | [diff] [blame] | 5704 | if ($dstat =~ /^\s*if\b/) { |
| 5705 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 5706 | "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx"); |
| 5707 | } elsif ($dstat =~ /;/) { |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 5708 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 5709 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 5710 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5711 | ERROR("COMPLEX_MACRO", |
Andrew Morton | 388982b | 2014-10-13 15:51:40 -0700 | [diff] [blame] | 5712 | "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 5713 | } |
Joe Perches | f59b64b | 2016-10-11 13:52:08 -0700 | [diff] [blame] | 5714 | |
| 5715 | } |
Joe Perches | 5207649 | 2016-10-11 13:52:14 -0700 | [diff] [blame] | 5716 | |
| 5717 | # Make $define_stmt single line, comment-free, etc |
| 5718 | my @stmt_array = split('\n', $define_stmt); |
| 5719 | my $first = 1; |
| 5720 | $define_stmt = ""; |
| 5721 | foreach my $l (@stmt_array) { |
| 5722 | $l =~ s/\\$//; |
| 5723 | if ($first) { |
| 5724 | $define_stmt = $l; |
| 5725 | $first = 0; |
| 5726 | } elsif ($l =~ /^[\+ ]/) { |
| 5727 | $define_stmt .= substr($l, 1); |
| 5728 | } |
| 5729 | } |
| 5730 | $define_stmt =~ s/$;//g; |
| 5731 | $define_stmt =~ s/\s+/ /g; |
| 5732 | $define_stmt = trim($define_stmt); |
| 5733 | |
Joe Perches | f59b64b | 2016-10-11 13:52:08 -0700 | [diff] [blame] | 5734 | # check if any macro arguments are reused (ignore '...' and 'type') |
| 5735 | foreach my $arg (@def_args) { |
| 5736 | next if ($arg =~ /\.\.\./); |
Joe Perches | 9192d41 | 2016-10-11 13:52:11 -0700 | [diff] [blame] | 5737 | next if ($arg =~ /^type$/i); |
Joe Perches | 7fe528a2 | 2017-07-10 15:52:27 -0700 | [diff] [blame] | 5738 | my $tmp_stmt = $define_stmt; |
Brendan Jackman | 6dba824 | 2019-09-25 16:46:41 -0700 | [diff] [blame] | 5739 | $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g; |
Joe Perches | 7fe528a2 | 2017-07-10 15:52:27 -0700 | [diff] [blame] | 5740 | $tmp_stmt =~ s/\#+\s*$arg\b//g; |
| 5741 | $tmp_stmt =~ s/\b$arg\s*\#\#//g; |
Joe Perches | d41362e | 2018-05-25 14:48:04 -0700 | [diff] [blame] | 5742 | my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g; |
Joe Perches | f59b64b | 2016-10-11 13:52:08 -0700 | [diff] [blame] | 5743 | if ($use_cnt > 1) { |
| 5744 | CHK("MACRO_ARG_REUSE", |
| 5745 | "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx"); |
Joe Perches | 9192d41 | 2016-10-11 13:52:11 -0700 | [diff] [blame] | 5746 | } |
| 5747 | # check if any macro arguments may have other precedence issues |
Joe Perches | 7fe528a2 | 2017-07-10 15:52:27 -0700 | [diff] [blame] | 5748 | if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m && |
Joe Perches | 9192d41 | 2016-10-11 13:52:11 -0700 | [diff] [blame] | 5749 | ((defined($1) && $1 ne ',') || |
| 5750 | (defined($2) && $2 ne ','))) { |
| 5751 | CHK("MACRO_ARG_PRECEDENCE", |
| 5752 | "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx"); |
Joe Perches | f59b64b | 2016-10-11 13:52:08 -0700 | [diff] [blame] | 5753 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 5754 | } |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 5755 | |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 5756 | # check for macros with flow control, but without ## concatenation |
| 5757 | # ## concatenation is commonly a macro that defines a function so ignore those |
| 5758 | if ($has_flow_statement && !$has_arg_concat) { |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 5759 | my $cnt = statement_rawlines($ctx); |
Tobin C. Harding | e3d95a2 | 2018-04-10 16:33:27 -0700 | [diff] [blame] | 5760 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 5761 | |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 5762 | WARN("MACRO_WITH_FLOW_CONTROL", |
| 5763 | "Macros with flow control statements should be avoided\n" . "$herectx"); |
| 5764 | } |
| 5765 | |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 5766 | # check for line continuations outside of #defines, preprocessor #, and asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 5767 | |
| 5768 | } else { |
| 5769 | if ($prevline !~ /^..*\\$/ && |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 5770 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 5771 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 5772 | $line =~ /^\+.*\\$/) { |
| 5773 | WARN("LINE_CONTINUATIONS", |
| 5774 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 5775 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5776 | } |
| 5777 | |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 5778 | # do {} while (0) macro tests: |
| 5779 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 5780 | # macro should not end with a semicolon |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 5781 | if ($perl_version_ok && |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 5782 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 5783 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 5784 | my $ln = $linenr; |
| 5785 | my $cnt = $realcnt; |
| 5786 | my ($off, $dstat, $dcond, $rest); |
| 5787 | my $ctx = ''; |
| 5788 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 5789 | ctx_statement_block($linenr, $realcnt, 0); |
| 5790 | $ctx = $dstat; |
| 5791 | |
| 5792 | $dstat =~ s/\\\n.//g; |
Joe Perches | 1b36b20 | 2015-02-13 14:38:32 -0800 | [diff] [blame] | 5793 | $dstat =~ s/$;/ /g; |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 5794 | |
| 5795 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 5796 | my $stmts = $2; |
| 5797 | my $semis = $3; |
| 5798 | |
| 5799 | $ctx =~ s/\n*$//; |
| 5800 | my $cnt = statement_rawlines($ctx); |
Tobin C. Harding | e3d95a2 | 2018-04-10 16:33:27 -0700 | [diff] [blame] | 5801 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 5802 | |
Joe Perches | ac8e97f | 2012-08-21 16:15:53 -0700 | [diff] [blame] | 5803 | if (($stmts =~ tr/;/;/) == 1 && |
| 5804 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 5805 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 5806 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 5807 | } |
| 5808 | if (defined $semis && $semis ne "") { |
| 5809 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 5810 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
| 5811 | } |
Joe Perches | f5ef95b | 2014-06-04 16:12:06 -0700 | [diff] [blame] | 5812 | } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { |
| 5813 | $ctx =~ s/\n*$//; |
| 5814 | my $cnt = statement_rawlines($ctx); |
Tobin C. Harding | e3d95a2 | 2018-04-10 16:33:27 -0700 | [diff] [blame] | 5815 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Joe Perches | f5ef95b | 2014-06-04 16:12:06 -0700 | [diff] [blame] | 5816 | |
| 5817 | WARN("TRAILING_SEMICOLON", |
| 5818 | "macros should not use a trailing semicolon\n" . "$herectx"); |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 5819 | } |
| 5820 | } |
| 5821 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 5822 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5823 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 5824 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5825 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5826 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5827 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 5828 | if ($#chunks > 0 && $level == 0) { |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 5829 | my @allowed = (); |
| 5830 | my $allow = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5831 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5832 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5833 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5834 | for my $chunk (@chunks) { |
| 5835 | my ($cond, $block) = @{$chunk}; |
| 5836 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5837 | # If the condition carries leading newlines, then count those as offsets. |
| 5838 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 5839 | my $offset = statement_rawlines($whitespace) - 1; |
| 5840 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 5841 | $allowed[$allow] = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5842 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 5843 | |
| 5844 | # We have looked at and allowed this specific line. |
| 5845 | $suppress_ifbraces{$ln + $offset} = 1; |
| 5846 | |
| 5847 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5848 | $ln += statement_rawlines($block) - 1; |
| 5849 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5850 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5851 | |
| 5852 | $seen++ if ($block =~ /^\s*{/); |
| 5853 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 5854 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5855 | if (statement_lines($cond) > 1) { |
| 5856 | #print "APW: ALLOWED: cond<$cond>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 5857 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5858 | } |
| 5859 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5860 | #print "APW: ALLOWED: block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 5861 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5862 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5863 | if (statement_block_size($block) > 1) { |
| 5864 | #print "APW: ALLOWED: lines block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 5865 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5866 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 5867 | $allow++; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5868 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 5869 | if ($seen) { |
| 5870 | my $sum_allowed = 0; |
| 5871 | foreach (@allowed) { |
| 5872 | $sum_allowed += $_; |
| 5873 | } |
| 5874 | if ($sum_allowed == 0) { |
| 5875 | WARN("BRACES", |
| 5876 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 5877 | } elsif ($sum_allowed != $allow && |
| 5878 | $seen != $allow) { |
| 5879 | CHK("BRACES", |
| 5880 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 5881 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5882 | } |
| 5883 | } |
| 5884 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5885 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5886 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5887 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 5888 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5889 | # Check the pre-context. |
| 5890 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 5891 | #print "APW: ALLOWED: pre<$1>\n"; |
| 5892 | $allowed = 1; |
| 5893 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5894 | |
| 5895 | my ($level, $endln, @chunks) = |
| 5896 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 5897 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5898 | # Check the condition. |
| 5899 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5900 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5901 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5902 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5903 | } |
| 5904 | if (statement_lines($cond) > 1) { |
| 5905 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 5906 | $allowed = 1; |
| 5907 | } |
| 5908 | if ($block =~/\b(?:if|for|while)\b/) { |
| 5909 | #print "APW: ALLOWED: block<$block>\n"; |
| 5910 | $allowed = 1; |
| 5911 | } |
| 5912 | if (statement_block_size($block) > 1) { |
| 5913 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 5914 | $allowed = 1; |
| 5915 | } |
| 5916 | # Check the post-context. |
| 5917 | if (defined $chunks[1]) { |
| 5918 | my ($cond, $block) = @{$chunks[1]}; |
| 5919 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5920 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 5921 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5922 | if ($block =~ /^\s*\{/) { |
| 5923 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 5924 | $allowed = 1; |
| 5925 | } |
| 5926 | } |
| 5927 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 5928 | my $cnt = statement_rawlines($block); |
Tobin C. Harding | e3d95a2 | 2018-04-10 16:33:27 -0700 | [diff] [blame] | 5929 | my $herectx = get_stat_here($linenr, $cnt, $here); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 5930 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5931 | WARN("BRACES", |
| 5932 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 5933 | } |
| 5934 | } |
| 5935 | |
Joe Perches | e4c5bab | 2017-02-24 15:01:41 -0800 | [diff] [blame] | 5936 | # check for single line unbalanced braces |
Sven Eckelmann | 9533047 | 2017-02-24 15:01:43 -0800 | [diff] [blame] | 5937 | if ($sline =~ /^.\s*\}\s*else\s*$/ || |
| 5938 | $sline =~ /^.\s*else\s*\{\s*$/) { |
Joe Perches | e4c5bab | 2017-02-24 15:01:41 -0800 | [diff] [blame] | 5939 | CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr); |
| 5940 | } |
| 5941 | |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 5942 | # check for unnecessary blank lines around braces |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 5943 | if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { |
Joe Perches | f8e5821 | 2015-02-13 14:38:46 -0800 | [diff] [blame] | 5944 | if (CHK("BRACES", |
| 5945 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && |
| 5946 | $fix && $prevrawline =~ /^\+/) { |
| 5947 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5948 | } |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 5949 | } |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 5950 | if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
Joe Perches | f8e5821 | 2015-02-13 14:38:46 -0800 | [diff] [blame] | 5951 | if (CHK("BRACES", |
| 5952 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && |
| 5953 | $fix) { |
| 5954 | fix_delete_line($fixlinenr, $rawline); |
| 5955 | } |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 5956 | } |
| 5957 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 5958 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 5959 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 5960 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5961 | WARN("VOLATILE", |
Mauro Carvalho Chehab | 8c27ceff3 | 2016-10-18 10:12:27 -0200 | [diff] [blame] | 5962 | "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 5963 | } |
| 5964 | |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 5965 | # Check for user-visible strings broken across lines, which breaks the ability |
| 5966 | # to grep for the string. Make exceptions when the previous string ends in a |
| 5967 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' |
| 5968 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value |
Joe Perches | 33acb54 | 2015-06-25 15:02:54 -0700 | [diff] [blame] | 5969 | if ($line =~ /^\+\s*$String/ && |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 5970 | $prevline =~ /"\s*$/ && |
| 5971 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { |
| 5972 | if (WARN("SPLIT_STRING", |
| 5973 | "quoted string split across lines\n" . $hereprev) && |
| 5974 | $fix && |
| 5975 | $prevrawline =~ /^\+.*"\s*$/ && |
| 5976 | $last_coalesced_string_linenr != $linenr - 1) { |
| 5977 | my $extracted_string = get_quoted_string($line, $rawline); |
| 5978 | my $comma_close = ""; |
| 5979 | if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { |
| 5980 | $comma_close = $1; |
| 5981 | } |
| 5982 | |
| 5983 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 5984 | fix_delete_line($fixlinenr, $rawline); |
| 5985 | my $fixedline = $prevrawline; |
| 5986 | $fixedline =~ s/"\s*$//; |
| 5987 | $fixedline .= substr($extracted_string, 1) . trim($comma_close); |
| 5988 | fix_insert_line($fixlinenr - 1, $fixedline); |
| 5989 | $fixedline = $rawline; |
| 5990 | $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; |
| 5991 | if ($fixedline !~ /\+\s*$/) { |
| 5992 | fix_insert_line($fixlinenr, $fixedline); |
| 5993 | } |
| 5994 | $last_coalesced_string_linenr = $linenr; |
| 5995 | } |
| 5996 | } |
| 5997 | |
| 5998 | # check for missing a space in a string concatenation |
| 5999 | if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { |
| 6000 | WARN('MISSING_SPACE', |
| 6001 | "break quoted strings at a space character\n" . $hereprev); |
| 6002 | } |
| 6003 | |
Joe Perches | e4b7d30 | 2017-05-08 15:55:51 -0700 | [diff] [blame] | 6004 | # check for an embedded function name in a string when the function is known |
| 6005 | # This does not work very well for -f --file checking as it depends on patch |
| 6006 | # context providing the function name or a single line form for in-file |
| 6007 | # function declarations |
Joe Perches | 77cb854 | 2017-02-24 15:01:28 -0800 | [diff] [blame] | 6008 | if ($line =~ /^\+.*$String/ && |
| 6009 | defined($context_function) && |
Joe Perches | e4b7d30 | 2017-05-08 15:55:51 -0700 | [diff] [blame] | 6010 | get_quoted_string($line, $rawline) =~ /\b$context_function\b/ && |
| 6011 | length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) { |
Joe Perches | 77cb854 | 2017-02-24 15:01:28 -0800 | [diff] [blame] | 6012 | WARN("EMBEDDED_FUNCTION_NAME", |
Joe Perches | e4b7d30 | 2017-05-08 15:55:51 -0700 | [diff] [blame] | 6013 | "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr); |
Joe Perches | 77cb854 | 2017-02-24 15:01:28 -0800 | [diff] [blame] | 6014 | } |
| 6015 | |
Joe Perches | adb2da8 | 2021-02-25 17:21:50 -0800 | [diff] [blame] | 6016 | # check for unnecessary function tracing like uses |
| 6017 | # This does not use $logFunctions because there are many instances like |
| 6018 | # 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions |
| 6019 | if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) { |
| 6020 | if (WARN("TRACING_LOGGING", |
| 6021 | "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) && |
| 6022 | $fix) { |
| 6023 | fix_delete_line($fixlinenr, $rawline); |
| 6024 | } |
| 6025 | } |
| 6026 | |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 6027 | # check for spaces before a quoted newline |
| 6028 | if ($rawline =~ /^.*\".*\s\\n/) { |
| 6029 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 6030 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && |
| 6031 | $fix) { |
| 6032 | $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; |
| 6033 | } |
| 6034 | |
| 6035 | } |
| 6036 | |
Joe Perches | f17dba4 | 2014-10-13 15:51:51 -0700 | [diff] [blame] | 6037 | # concatenated string without spaces between elements |
Joe Perches | 79682c0 | 2018-08-21 21:57:29 -0700 | [diff] [blame] | 6038 | if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) { |
| 6039 | if (CHK("CONCATENATED_STRING", |
| 6040 | "Concatenated strings should use spaces between elements\n" . $herecurr) && |
| 6041 | $fix) { |
| 6042 | while ($line =~ /($String)/g) { |
| 6043 | my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); |
| 6044 | $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/; |
| 6045 | $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/; |
| 6046 | } |
| 6047 | } |
Joe Perches | f17dba4 | 2014-10-13 15:51:51 -0700 | [diff] [blame] | 6048 | } |
| 6049 | |
Joe Perches | 90ad30e | 2014-12-10 15:51:59 -0800 | [diff] [blame] | 6050 | # uncoalesced string fragments |
Joe Perches | 33acb54 | 2015-06-25 15:02:54 -0700 | [diff] [blame] | 6051 | if ($line =~ /$String\s*"/) { |
Joe Perches | 79682c0 | 2018-08-21 21:57:29 -0700 | [diff] [blame] | 6052 | if (WARN("STRING_FRAGMENTS", |
| 6053 | "Consecutive strings are generally better as a single string\n" . $herecurr) && |
| 6054 | $fix) { |
| 6055 | while ($line =~ /($String)(?=\s*")/g) { |
| 6056 | my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]); |
| 6057 | $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e; |
| 6058 | } |
| 6059 | } |
Joe Perches | 90ad30e | 2014-12-10 15:51:59 -0800 | [diff] [blame] | 6060 | } |
| 6061 | |
Alexey Dobriyan | 522b837 | 2017-02-27 14:30:05 -0800 | [diff] [blame] | 6062 | # check for non-standard and hex prefixed decimal printf formats |
| 6063 | my $show_L = 1; #don't show the same defect twice |
| 6064 | my $show_Z = 1; |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 6065 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
Alexey Dobriyan | 522b837 | 2017-02-27 14:30:05 -0800 | [diff] [blame] | 6066 | my $string = substr($rawline, $-[1], $+[1] - $-[1]); |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 6067 | $string =~ s/%%/__/g; |
Alexey Dobriyan | 522b837 | 2017-02-27 14:30:05 -0800 | [diff] [blame] | 6068 | # check for %L |
| 6069 | if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) { |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 6070 | WARN("PRINTF_L", |
Alexey Dobriyan | 522b837 | 2017-02-27 14:30:05 -0800 | [diff] [blame] | 6071 | "\%L$1 is non-standard C, use %ll$1\n" . $herecurr); |
| 6072 | $show_L = 0; |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 6073 | } |
Alexey Dobriyan | 522b837 | 2017-02-27 14:30:05 -0800 | [diff] [blame] | 6074 | # check for %Z |
| 6075 | if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) { |
| 6076 | WARN("PRINTF_Z", |
| 6077 | "%Z$1 is non-standard C, use %z$1\n" . $herecurr); |
| 6078 | $show_Z = 0; |
| 6079 | } |
| 6080 | # check for 0x<decimal> |
| 6081 | if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) { |
| 6082 | ERROR("PRINTF_0XDECIMAL", |
Joe Perches | 6e30075 | 2015-09-09 15:37:47 -0700 | [diff] [blame] | 6083 | "Prefixing 0x with decimal output is defective\n" . $herecurr); |
| 6084 | } |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 6085 | } |
| 6086 | |
| 6087 | # check for line continuations in quoted strings with odd counts of " |
Joe Perches | 3f7f335 | 2018-02-06 15:38:52 -0800 | [diff] [blame] | 6088 | if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) { |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 6089 | WARN("LINE_CONTINUATIONS", |
| 6090 | "Avoid line continuations in quoted strings\n" . $herecurr); |
| 6091 | } |
| 6092 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 6093 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 6094 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Prakruthi Deepak Heragu | 60f8901 | 2018-08-21 21:57:57 -0700 | [diff] [blame] | 6095 | WARN("IF_0", |
| 6096 | "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr); |
| 6097 | } |
| 6098 | |
| 6099 | # warn about #if 1 |
| 6100 | if ($line =~ /^.\s*\#\s*if\s+1\b/) { |
| 6101 | WARN("IF_1", |
| 6102 | "Consider removing the #if 1 and its #endif\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 6103 | } |
| 6104 | |
Andy Whitcroft | 03df4b5 | 2012-12-17 16:01:52 -0800 | [diff] [blame] | 6105 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 6106 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
Joe Perches | 100425d | 2015-09-09 15:37:36 -0700 | [diff] [blame] | 6107 | my $tested = quotemeta($1); |
| 6108 | my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; |
| 6109 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { |
| 6110 | my $func = $1; |
| 6111 | if (WARN('NEEDLESS_IF', |
| 6112 | "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && |
| 6113 | $fix) { |
| 6114 | my $do_fix = 1; |
| 6115 | my $leading_tabs = ""; |
| 6116 | my $new_leading_tabs = ""; |
| 6117 | if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { |
| 6118 | $leading_tabs = $1; |
| 6119 | } else { |
| 6120 | $do_fix = 0; |
| 6121 | } |
| 6122 | if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { |
| 6123 | $new_leading_tabs = $1; |
| 6124 | if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { |
| 6125 | $do_fix = 0; |
| 6126 | } |
| 6127 | } else { |
| 6128 | $do_fix = 0; |
| 6129 | } |
| 6130 | if ($do_fix) { |
| 6131 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 6132 | $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; |
| 6133 | } |
| 6134 | } |
Greg Kroah-Hartman | 4c432a8 | 2008-07-23 21:29:04 -0700 | [diff] [blame] | 6135 | } |
| 6136 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 6137 | |
Joe Perches | ebfdc40 | 2014-08-06 16:10:27 -0700 | [diff] [blame] | 6138 | # check for unnecessary "Out of Memory" messages |
| 6139 | if ($line =~ /^\+.*\b$logFunctions\s*\(/ && |
| 6140 | $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && |
| 6141 | (defined $1 || defined $3) && |
| 6142 | $linenr > 3) { |
| 6143 | my $testval = $2; |
| 6144 | my $testline = $lines[$linenr - 3]; |
| 6145 | |
| 6146 | my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); |
| 6147 | # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); |
| 6148 | |
Joe Perches | e29a70f | 2019-03-07 16:28:35 -0800 | [diff] [blame] | 6149 | if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ && |
| 6150 | $s !~ /\b__GFP_NOWARN\b/ ) { |
Joe Perches | ebfdc40 | 2014-08-06 16:10:27 -0700 | [diff] [blame] | 6151 | WARN("OOM_MESSAGE", |
| 6152 | "Possible unnecessary 'out of memory' message\n" . $hereprev); |
| 6153 | } |
| 6154 | } |
| 6155 | |
Joe Perches | f78d98f | 2014-10-13 15:52:01 -0700 | [diff] [blame] | 6156 | # check for logging functions with KERN_<LEVEL> |
Paolo Bonzini | dcaf112 | 2015-02-13 14:38:26 -0800 | [diff] [blame] | 6157 | if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && |
Joe Perches | f78d98f | 2014-10-13 15:52:01 -0700 | [diff] [blame] | 6158 | $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { |
| 6159 | my $level = $1; |
| 6160 | if (WARN("UNNECESSARY_KERN_LEVEL", |
| 6161 | "Possible unnecessary $level\n" . $herecurr) && |
| 6162 | $fix) { |
| 6163 | $fixed[$fixlinenr] =~ s/\s*$level\s*//; |
| 6164 | } |
| 6165 | } |
| 6166 | |
Joe Perches | 45c55e9 | 2017-02-24 15:01:31 -0800 | [diff] [blame] | 6167 | # check for logging continuations |
| 6168 | if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) { |
| 6169 | WARN("LOGGING_CONTINUATION", |
| 6170 | "Avoid logging continuation uses where feasible\n" . $herecurr); |
| 6171 | } |
| 6172 | |
Dwaipayan Ray | 70eb227 | 2020-12-15 20:45:15 -0800 | [diff] [blame] | 6173 | # check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions |
| 6174 | if (defined $stat && |
| 6175 | $line =~ /\b$logFunctions\s*\(/ && |
| 6176 | index($stat, '"') >= 0) { |
| 6177 | my $lc = $stat =~ tr@\n@@; |
| 6178 | $lc = $lc + $linenr; |
| 6179 | my $stat_real = get_stat_real($linenr, $lc); |
| 6180 | pos($stat_real) = index($stat_real, '"'); |
| 6181 | while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) { |
| 6182 | my $pspec = $1; |
| 6183 | my $h = $2; |
| 6184 | my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@; |
| 6185 | if (WARN("UNNECESSARY_MODIFIER", |
| 6186 | "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") && |
| 6187 | $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) { |
| 6188 | my $nspec = $pspec; |
| 6189 | $nspec =~ s/h//g; |
| 6190 | $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/; |
| 6191 | } |
| 6192 | } |
| 6193 | } |
| 6194 | |
Joe Perches | abb08a5 | 2014-12-10 15:51:46 -0800 | [diff] [blame] | 6195 | # check for mask then right shift without a parentheses |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6196 | if ($perl_version_ok && |
Joe Perches | abb08a5 | 2014-12-10 15:51:46 -0800 | [diff] [blame] | 6197 | $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && |
| 6198 | $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so |
| 6199 | WARN("MASK_THEN_SHIFT", |
| 6200 | "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); |
| 6201 | } |
| 6202 | |
Joe Perches | b75ac61 | 2014-12-10 15:52:02 -0800 | [diff] [blame] | 6203 | # check for pointer comparisons to NULL |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6204 | if ($perl_version_ok) { |
Joe Perches | b75ac61 | 2014-12-10 15:52:02 -0800 | [diff] [blame] | 6205 | while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { |
| 6206 | my $val = $1; |
| 6207 | my $equal = "!"; |
| 6208 | $equal = "" if ($4 eq "!="); |
| 6209 | if (CHK("COMPARISON_TO_NULL", |
| 6210 | "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && |
| 6211 | $fix) { |
| 6212 | $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; |
| 6213 | } |
| 6214 | } |
| 6215 | } |
| 6216 | |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 6217 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
| 6218 | if ($line =~ /(\b$InitAttribute\b)/) { |
| 6219 | my $attr = $1; |
| 6220 | if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { |
| 6221 | my $ptr = $1; |
| 6222 | my $var = $2; |
| 6223 | if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && |
| 6224 | ERROR("MISPLACED_INIT", |
| 6225 | "$attr should be placed after $var\n" . $herecurr)) || |
| 6226 | ($ptr !~ /\b(union|struct)\s+$attr\b/ && |
| 6227 | WARN("MISPLACED_INIT", |
| 6228 | "$attr should be placed after $var\n" . $herecurr))) && |
| 6229 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6230 | $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; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 6231 | } |
| 6232 | } |
| 6233 | } |
| 6234 | |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 6235 | # check for $InitAttributeData (ie: __initdata) with const |
| 6236 | if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { |
| 6237 | my $attr = $1; |
| 6238 | $attr =~ /($InitAttributePrefix)(.*)/; |
| 6239 | my $attr_prefix = $1; |
| 6240 | my $attr_type = $2; |
| 6241 | if (ERROR("INIT_ATTRIBUTE", |
| 6242 | "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && |
| 6243 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6244 | $fixed[$fixlinenr] =~ |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 6245 | s/$InitAttributeData/${attr_prefix}initconst/; |
| 6246 | } |
| 6247 | } |
| 6248 | |
| 6249 | # check for $InitAttributeConst (ie: __initconst) without const |
| 6250 | if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { |
| 6251 | my $attr = $1; |
| 6252 | if (ERROR("INIT_ATTRIBUTE", |
| 6253 | "Use of $attr requires a separate use of const\n" . $herecurr) && |
| 6254 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6255 | my $lead = $fixed[$fixlinenr] =~ |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 6256 | /(^\+\s*(?:static\s+))/; |
| 6257 | $lead = rtrim($1); |
| 6258 | $lead = "$lead " if ($lead !~ /^\+$/); |
| 6259 | $lead = "${lead}const "; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6260 | $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 6261 | } |
| 6262 | } |
| 6263 | |
Joe Perches | c17893c | 2015-04-16 12:44:42 -0700 | [diff] [blame] | 6264 | # check for __read_mostly with const non-pointer (should just be const) |
| 6265 | if ($line =~ /\b__read_mostly\b/ && |
| 6266 | $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { |
| 6267 | if (ERROR("CONST_READ_MOSTLY", |
| 6268 | "Invalid use of __read_mostly with const type\n" . $herecurr) && |
| 6269 | $fix) { |
| 6270 | $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; |
| 6271 | } |
| 6272 | } |
| 6273 | |
Joe Perches | fbdb813 | 2014-04-03 14:49:14 -0700 | [diff] [blame] | 6274 | # don't use __constant_<foo> functions outside of include/uapi/ |
| 6275 | if ($realfile !~ m@^include/uapi/@ && |
| 6276 | $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { |
| 6277 | my $constant_func = $1; |
| 6278 | my $func = $constant_func; |
| 6279 | $func =~ s/^__constant_//; |
| 6280 | if (WARN("CONSTANT_CONVERSION", |
| 6281 | "$constant_func should be $func\n" . $herecurr) && |
| 6282 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6283 | $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; |
Joe Perches | fbdb813 | 2014-04-03 14:49:14 -0700 | [diff] [blame] | 6284 | } |
| 6285 | } |
| 6286 | |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 6287 | # prefer usleep_range over udelay |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 6288 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 6289 | my $delay = $1; |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 6290 | # ignore udelay's < 10, however |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 6291 | if (! ($delay < 10) ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6292 | CHK("USLEEP_RANGE", |
Mauro Carvalho Chehab | 458f69e | 2019-06-12 14:53:00 -0300 | [diff] [blame] | 6293 | "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr); |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 6294 | } |
| 6295 | if ($delay > 2000) { |
| 6296 | WARN("LONG_UDELAY", |
| 6297 | "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 6298 | } |
| 6299 | } |
| 6300 | |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 6301 | # warn about unexpectedly long msleep's |
| 6302 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 6303 | if ($1 < 20) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6304 | WARN("MSLEEP", |
Mauro Carvalho Chehab | 458f69e | 2019-06-12 14:53:00 -0300 | [diff] [blame] | 6305 | "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr); |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 6306 | } |
| 6307 | } |
| 6308 | |
Joe Perches | 36ec193 | 2013-07-03 15:05:25 -0700 | [diff] [blame] | 6309 | # check for comparisons of jiffies |
| 6310 | if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { |
| 6311 | WARN("JIFFIES_COMPARISON", |
| 6312 | "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); |
| 6313 | } |
| 6314 | |
Joe Perches | 9d7a34a | 2013-07-03 15:05:26 -0700 | [diff] [blame] | 6315 | # check for comparisons of get_jiffies_64() |
| 6316 | if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { |
| 6317 | WARN("JIFFIES_COMPARISON", |
| 6318 | "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); |
| 6319 | } |
| 6320 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 6321 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 6322 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 6323 | # print "#ifdef in C files should be avoided\n"; |
| 6324 | # print "$herecurr"; |
| 6325 | # $clean = 0; |
| 6326 | # } |
| 6327 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 6328 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 6329 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 6330 | if (ERROR("SPACING", |
| 6331 | "exactly one space required after that #$1\n" . $herecurr) && |
| 6332 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6333 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 6334 | s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; |
| 6335 | } |
| 6336 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 6337 | } |
| 6338 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 6339 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 6340 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 6341 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 6342 | my $which = $1; |
| 6343 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6344 | CHK("UNCOMMENTED_DEFINITION", |
| 6345 | "$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 6346 | } |
| 6347 | } |
| 6348 | # check for memory barriers without a comment. |
Michael S. Tsirkin | 402c255 | 2016-01-04 09:39:01 +0200 | [diff] [blame] | 6349 | |
| 6350 | my $barriers = qr{ |
| 6351 | mb| |
| 6352 | rmb| |
Will Deacon | ad83ec6 | 2019-11-07 14:49:00 +0000 | [diff] [blame] | 6353 | wmb |
Michael S. Tsirkin | 402c255 | 2016-01-04 09:39:01 +0200 | [diff] [blame] | 6354 | }x; |
| 6355 | my $barrier_stems = qr{ |
| 6356 | mb__before_atomic| |
| 6357 | mb__after_atomic| |
| 6358 | store_release| |
| 6359 | load_acquire| |
| 6360 | store_mb| |
| 6361 | (?:$barriers) |
| 6362 | }x; |
| 6363 | my $all_barriers = qr{ |
| 6364 | (?:$barriers)| |
Michael S. Tsirkin | 43e361f | 2016-01-04 10:00:10 +0200 | [diff] [blame] | 6365 | smp_(?:$barrier_stems)| |
| 6366 | virt_(?:$barrier_stems) |
Michael S. Tsirkin | 402c255 | 2016-01-04 09:39:01 +0200 | [diff] [blame] | 6367 | }x; |
| 6368 | |
| 6369 | if ($line =~ /\b(?:$all_barriers)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 6370 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | c1fd7bb | 2013-11-12 15:10:11 -0800 | [diff] [blame] | 6371 | WARN("MEMORY_BARRIER", |
| 6372 | "memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 6373 | } |
| 6374 | } |
Paul E. McKenney | 3ad8177 | 2015-07-02 11:55:40 -0700 | [diff] [blame] | 6375 | |
Michael S. Tsirkin | f4073b0 | 2016-01-04 09:54:51 +0200 | [diff] [blame] | 6376 | my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; |
| 6377 | |
| 6378 | if ($realfile !~ m@^include/asm-generic/@ && |
| 6379 | $realfile !~ m@/barrier\.h$@ && |
| 6380 | $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && |
| 6381 | $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { |
| 6382 | WARN("MEMORY_BARRIER", |
| 6383 | "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); |
| 6384 | } |
| 6385 | |
Joe Perches | cb426e9 | 2015-06-25 15:02:46 -0700 | [diff] [blame] | 6386 | # check for waitqueue_active without a comment. |
| 6387 | if ($line =~ /\bwaitqueue_active\s*\(/) { |
| 6388 | if (!ctx_has_comment($first_line, $linenr)) { |
| 6389 | WARN("WAITQUEUE_ACTIVE", |
| 6390 | "waitqueue_active without comment\n" . $herecurr); |
| 6391 | } |
| 6392 | } |
Paul E. McKenney | 3ad8177 | 2015-07-02 11:55:40 -0700 | [diff] [blame] | 6393 | |
Marco Elver | 5099a72 | 2020-04-01 12:17:14 +0200 | [diff] [blame] | 6394 | # check for data_race without a comment. |
| 6395 | if ($line =~ /\bdata_race\s*\(/) { |
| 6396 | if (!ctx_has_comment($first_line, $linenr)) { |
| 6397 | WARN("DATA_RACE", |
| 6398 | "data_race without comment\n" . $herecurr); |
| 6399 | } |
| 6400 | } |
| 6401 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 6402 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 6403 | if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6404 | CHK("ARCH_DEFINES", |
| 6405 | "architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 6406 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 6407 | |
Joe Perches | 596ed45 | 2017-07-12 14:37:02 -0700 | [diff] [blame] | 6408 | # check that the storage class is not after a type |
| 6409 | if ($line =~ /\b($Type)\s+($Storage)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6410 | WARN("STORAGE_CLASS", |
Joe Perches | 596ed45 | 2017-07-12 14:37:02 -0700 | [diff] [blame] | 6411 | "storage class '$2' should be located before type '$1'\n" . $herecurr); |
| 6412 | } |
| 6413 | # Check that the storage class is at the beginning of a declaration |
| 6414 | if ($line =~ /\b$Storage\b/ && |
| 6415 | $line !~ /^.\s*$Storage/ && |
| 6416 | $line =~ /^.\s*(.+?)\$Storage\s/ && |
| 6417 | $1 !~ /[\,\)]\s*$/) { |
| 6418 | WARN("STORAGE_CLASS", |
| 6419 | "storage class should be at the beginning of the declaration\n" . $herecurr); |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 6420 | } |
| 6421 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 6422 | # check the location of the inline attribute, that it is between |
| 6423 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 6424 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 6425 | $line =~ /\b$Inline\s+$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6426 | ERROR("INLINE_LOCATION", |
| 6427 | "inline keyword should sit between storage class and type\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 6428 | } |
| 6429 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 6430 | # Check for __inline__ and __inline, prefer inline |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 6431 | if ($realfile !~ m@\binclude/uapi/@ && |
| 6432 | $line =~ /\b(__inline__|__inline)\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6433 | if (WARN("INLINE", |
| 6434 | "plain inline is preferred over $1\n" . $herecurr) && |
| 6435 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6436 | $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6437 | |
| 6438 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 6439 | } |
| 6440 | |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6441 | # Check for compiler attributes |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 6442 | if ($realfile !~ m@\binclude/uapi/@ && |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6443 | $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) { |
| 6444 | my $attr = $1; |
| 6445 | $attr =~ s/\s*\(\s*(.*)\)\s*/$1/; |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 6446 | |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6447 | my %attr_list = ( |
Joe Perches | 0830aab | 2020-12-15 20:44:50 -0800 | [diff] [blame] | 6448 | "alias" => "__alias", |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6449 | "aligned" => "__aligned", |
| 6450 | "always_inline" => "__always_inline", |
| 6451 | "assume_aligned" => "__assume_aligned", |
| 6452 | "cold" => "__cold", |
| 6453 | "const" => "__attribute_const__", |
| 6454 | "copy" => "__copy", |
| 6455 | "designated_init" => "__designated_init", |
| 6456 | "externally_visible" => "__visible", |
| 6457 | "format" => "printf|scanf", |
| 6458 | "gnu_inline" => "__gnu_inline", |
| 6459 | "malloc" => "__malloc", |
| 6460 | "mode" => "__mode", |
| 6461 | "no_caller_saved_registers" => "__no_caller_saved_registers", |
| 6462 | "noclone" => "__noclone", |
| 6463 | "noinline" => "noinline", |
| 6464 | "nonstring" => "__nonstring", |
| 6465 | "noreturn" => "__noreturn", |
| 6466 | "packed" => "__packed", |
| 6467 | "pure" => "__pure", |
Joe Perches | 339f29d | 2020-12-15 20:44:43 -0800 | [diff] [blame] | 6468 | "section" => "__section", |
Joe Perches | 0830aab | 2020-12-15 20:44:50 -0800 | [diff] [blame] | 6469 | "used" => "__used", |
| 6470 | "weak" => "__weak" |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6471 | ); |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 6472 | |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6473 | while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) { |
Joe Perches | 339f29d | 2020-12-15 20:44:43 -0800 | [diff] [blame] | 6474 | my $orig_attr = $1; |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6475 | my $params = ''; |
| 6476 | $params = $2 if defined($2); |
Joe Perches | 339f29d | 2020-12-15 20:44:43 -0800 | [diff] [blame] | 6477 | my $curr_attr = $orig_attr; |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6478 | $curr_attr =~ s/^[\s_]+|[\s_]+$//g; |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6479 | if (exists($attr_list{$curr_attr})) { |
Joe Perches | 339f29d | 2020-12-15 20:44:43 -0800 | [diff] [blame] | 6480 | my $new = $attr_list{$curr_attr}; |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6481 | if ($curr_attr eq "format" && $params) { |
| 6482 | $params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/; |
Joe Perches | 339f29d | 2020-12-15 20:44:43 -0800 | [diff] [blame] | 6483 | $new = "__$1\($2"; |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6484 | } else { |
Joe Perches | 339f29d | 2020-12-15 20:44:43 -0800 | [diff] [blame] | 6485 | $new = "$new$params"; |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6486 | } |
Joe Perches | 339f29d | 2020-12-15 20:44:43 -0800 | [diff] [blame] | 6487 | if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", |
| 6488 | "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) && |
| 6489 | $fix) { |
| 6490 | my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?'; |
| 6491 | $fixed[$fixlinenr] =~ s/$remove//; |
| 6492 | $fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/; |
| 6493 | $fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/; |
| 6494 | $fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//; |
| 6495 | } |
Dwaipayan Ray | 7ebe1d1 | 2020-12-15 20:44:36 -0800 | [diff] [blame] | 6496 | } |
| 6497 | } |
| 6498 | |
| 6499 | # Check for __attribute__ unused, prefer __always_unused or __maybe_unused |
| 6500 | if ($attr =~ /^_*unused/) { |
| 6501 | WARN("PREFER_DEFINED_ATTRIBUTE_MACRO", |
| 6502 | "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr); |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6503 | } |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 6504 | } |
| 6505 | |
Joe Perches | 619a908 | 2014-12-10 15:51:35 -0800 | [diff] [blame] | 6506 | # Check for __attribute__ weak, or __weak declarations (may have link issues) |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6507 | if ($perl_version_ok && |
Joe Perches | 619a908 | 2014-12-10 15:51:35 -0800 | [diff] [blame] | 6508 | $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && |
| 6509 | ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || |
| 6510 | $line =~ /\b__weak\b/)) { |
| 6511 | ERROR("WEAK_DECLARATION", |
| 6512 | "Using weak declarations can have unintended link defects\n" . $herecurr); |
| 6513 | } |
| 6514 | |
Tomas Winkler | fd39f90 | 2016-12-12 16:46:34 -0800 | [diff] [blame] | 6515 | # check for c99 types like uint8_t used outside of uapi/ and tools/ |
Joe Perches | e6176fa | 2015-06-25 15:02:49 -0700 | [diff] [blame] | 6516 | if ($realfile !~ m@\binclude/uapi/@ && |
Tomas Winkler | fd39f90 | 2016-12-12 16:46:34 -0800 | [diff] [blame] | 6517 | $realfile !~ m@\btools/@ && |
Joe Perches | e6176fa | 2015-06-25 15:02:49 -0700 | [diff] [blame] | 6518 | $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { |
| 6519 | my $type = $1; |
| 6520 | if ($type =~ /\b($typeC99Typedefs)\b/) { |
| 6521 | $type = $1; |
| 6522 | my $kernel_type = 'u'; |
| 6523 | $kernel_type = 's' if ($type =~ /^_*[si]/); |
| 6524 | $type =~ /(\d+)/; |
| 6525 | $kernel_type .= $1; |
| 6526 | if (CHK("PREFER_KERNEL_TYPES", |
| 6527 | "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && |
| 6528 | $fix) { |
| 6529 | $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; |
| 6530 | } |
| 6531 | } |
| 6532 | } |
| 6533 | |
Joe Perches | 938224b | 2016-01-20 14:59:15 -0800 | [diff] [blame] | 6534 | # check for cast of C90 native int or longer types constants |
| 6535 | if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { |
| 6536 | my $cast = $1; |
| 6537 | my $const = $2; |
Joe Perches | 0972b8b | 2021-02-25 17:21:54 -0800 | [diff] [blame] | 6538 | my $suffix = ""; |
| 6539 | my $newconst = $const; |
| 6540 | $newconst =~ s/${Int_type}$//; |
| 6541 | $suffix .= 'U' if ($cast =~ /\bunsigned\b/); |
| 6542 | if ($cast =~ /\blong\s+long\b/) { |
| 6543 | $suffix .= 'LL'; |
| 6544 | } elsif ($cast =~ /\blong\b/) { |
| 6545 | $suffix .= 'L'; |
| 6546 | } |
Joe Perches | 938224b | 2016-01-20 14:59:15 -0800 | [diff] [blame] | 6547 | if (WARN("TYPECAST_INT_CONSTANT", |
Joe Perches | 0972b8b | 2021-02-25 17:21:54 -0800 | [diff] [blame] | 6548 | "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) && |
Joe Perches | 938224b | 2016-01-20 14:59:15 -0800 | [diff] [blame] | 6549 | $fix) { |
Joe Perches | 938224b | 2016-01-20 14:59:15 -0800 | [diff] [blame] | 6550 | $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; |
| 6551 | } |
| 6552 | } |
| 6553 | |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 6554 | # check for sizeof(&) |
| 6555 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6556 | WARN("SIZEOF_ADDRESS", |
| 6557 | "sizeof(& should be avoided\n" . $herecurr); |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 6558 | } |
| 6559 | |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 6560 | # check for sizeof without parenthesis |
| 6561 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6562 | if (WARN("SIZEOF_PARENTHESIS", |
| 6563 | "sizeof $1 should be sizeof($1)\n" . $herecurr) && |
| 6564 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6565 | $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6566 | } |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 6567 | } |
| 6568 | |
Joe Perches | 88982fe | 2012-12-17 16:02:00 -0800 | [diff] [blame] | 6569 | # check for struct spinlock declarations |
| 6570 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 6571 | WARN("USE_SPINLOCK_T", |
| 6572 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 6573 | } |
| 6574 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 6575 | # check for seq_printf uses that could be seq_puts |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 6576 | if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 6577 | my $fmt = get_quoted_string($line, $rawline); |
Heba Aamer | caac1d5 | 2015-02-13 14:38:49 -0800 | [diff] [blame] | 6578 | $fmt =~ s/%%//g; |
| 6579 | if ($fmt !~ /%/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6580 | if (WARN("PREFER_SEQ_PUTS", |
| 6581 | "Prefer seq_puts to seq_printf\n" . $herecurr) && |
| 6582 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6583 | $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6584 | } |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 6585 | } |
| 6586 | } |
| 6587 | |
Joe Perches | 478b179 | 2018-04-10 16:33:34 -0700 | [diff] [blame] | 6588 | # check for vsprintf extension %p<foo> misuses |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6589 | if ($perl_version_ok && |
Joe Perches | 0b52376 | 2017-05-08 15:55:36 -0700 | [diff] [blame] | 6590 | defined $stat && |
| 6591 | $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s && |
| 6592 | $1 !~ /^_*volatile_*$/) { |
Tobin C. Harding | e3c6bc9 | 2018-04-10 16:33:31 -0700 | [diff] [blame] | 6593 | my $stat_real; |
| 6594 | |
Joe Perches | 0b52376 | 2017-05-08 15:55:36 -0700 | [diff] [blame] | 6595 | my $lc = $stat =~ tr@\n@@; |
| 6596 | $lc = $lc + $linenr; |
| 6597 | for (my $count = $linenr; $count <= $lc; $count++) { |
Joe Perches | ffe0751 | 2018-07-13 16:59:23 -0700 | [diff] [blame] | 6598 | my $specifier; |
| 6599 | my $extension; |
Sakari Ailus | 3bd32d6 | 2019-10-03 15:32:18 +0300 | [diff] [blame] | 6600 | my $qualifier; |
Joe Perches | ffe0751 | 2018-07-13 16:59:23 -0700 | [diff] [blame] | 6601 | my $bad_specifier = ""; |
Joe Perches | 0b52376 | 2017-05-08 15:55:36 -0700 | [diff] [blame] | 6602 | my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0)); |
| 6603 | $fmt =~ s/%%//g; |
Tobin C. Harding | 2a9f9d8 | 2018-04-10 16:33:20 -0700 | [diff] [blame] | 6604 | |
Sakari Ailus | 3bd32d6 | 2019-10-03 15:32:18 +0300 | [diff] [blame] | 6605 | while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) { |
Tobin C. Harding | e3c6bc9 | 2018-04-10 16:33:31 -0700 | [diff] [blame] | 6606 | $specifier = $1; |
| 6607 | $extension = $2; |
Sakari Ailus | 3bd32d6 | 2019-10-03 15:32:18 +0300 | [diff] [blame] | 6608 | $qualifier = $3; |
Linus Torvalds | 361b0d2 | 2019-11-26 19:45:12 -0800 | [diff] [blame] | 6609 | if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ || |
Sakari Ailus | 3bd32d6 | 2019-10-03 15:32:18 +0300 | [diff] [blame] | 6610 | ($extension eq "f" && |
| 6611 | defined $qualifier && $qualifier !~ /^w/)) { |
Tobin C. Harding | e3c6bc9 | 2018-04-10 16:33:31 -0700 | [diff] [blame] | 6612 | $bad_specifier = $specifier; |
| 6613 | last; |
| 6614 | } |
| 6615 | if ($extension eq "x" && !defined($stat_real)) { |
| 6616 | if (!defined($stat_real)) { |
| 6617 | $stat_real = get_stat_real($linenr, $lc); |
| 6618 | } |
| 6619 | WARN("VSPRINTF_SPECIFIER_PX", |
| 6620 | "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"); |
| 6621 | } |
| 6622 | } |
| 6623 | if ($bad_specifier ne "") { |
| 6624 | my $stat_real = get_stat_real($linenr, $lc); |
| 6625 | my $ext_type = "Invalid"; |
| 6626 | my $use = ""; |
| 6627 | if ($bad_specifier =~ /p[Ff]/) { |
Tobin C. Harding | e3c6bc9 | 2018-04-10 16:33:31 -0700 | [diff] [blame] | 6628 | $use = " - use %pS instead"; |
| 6629 | $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/); |
| 6630 | } |
| 6631 | |
| 6632 | WARN("VSPRINTF_POINTER_EXTENSION", |
| 6633 | "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n"); |
| 6634 | } |
Joe Perches | 0b52376 | 2017-05-08 15:55:36 -0700 | [diff] [blame] | 6635 | } |
| 6636 | } |
| 6637 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 6638 | # Check for misused memsets |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6639 | if ($perl_version_ok && |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 6640 | defined $stat && |
Mateusz Kulikowski | 9e20a85 | 2015-06-25 15:03:16 -0700 | [diff] [blame] | 6641 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 6642 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 6643 | my $ms_addr = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 6644 | my $ms_val = $7; |
| 6645 | my $ms_size = $12; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 6646 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 6647 | if ($ms_size =~ /^(0x|)0$/i) { |
| 6648 | ERROR("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 6649 | "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 6650 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 6651 | WARN("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 6652 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 6653 | } |
| 6654 | } |
| 6655 | |
Joe Perches | 98a9bba | 2014-01-23 15:54:52 -0800 | [diff] [blame] | 6656 | # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6657 | # if ($perl_version_ok && |
Joe Perches | f333195d | 2016-10-11 13:51:53 -0700 | [diff] [blame] | 6658 | # defined $stat && |
| 6659 | # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 6660 | # if (WARN("PREFER_ETHER_ADDR_COPY", |
| 6661 | # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && |
| 6662 | # $fix) { |
| 6663 | # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; |
| 6664 | # } |
| 6665 | # } |
Joe Perches | 98a9bba | 2014-01-23 15:54:52 -0800 | [diff] [blame] | 6666 | |
Mateusz Kulikowski | b6117d1 | 2015-06-25 15:03:13 -0700 | [diff] [blame] | 6667 | # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6668 | # if ($perl_version_ok && |
Joe Perches | f333195d | 2016-10-11 13:51:53 -0700 | [diff] [blame] | 6669 | # defined $stat && |
| 6670 | # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 6671 | # WARN("PREFER_ETHER_ADDR_EQUAL", |
| 6672 | # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") |
| 6673 | # } |
Mateusz Kulikowski | b6117d1 | 2015-06-25 15:03:13 -0700 | [diff] [blame] | 6674 | |
Mateusz Kulikowski | 8617cd0 | 2015-06-25 15:03:19 -0700 | [diff] [blame] | 6675 | # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr |
| 6676 | # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6677 | # if ($perl_version_ok && |
Joe Perches | f333195d | 2016-10-11 13:51:53 -0700 | [diff] [blame] | 6678 | # defined $stat && |
| 6679 | # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 6680 | # |
| 6681 | # my $ms_val = $7; |
| 6682 | # |
| 6683 | # if ($ms_val =~ /^(?:0x|)0+$/i) { |
| 6684 | # if (WARN("PREFER_ETH_ZERO_ADDR", |
| 6685 | # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && |
| 6686 | # $fix) { |
| 6687 | # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; |
| 6688 | # } |
| 6689 | # } elsif ($ms_val =~ /^(?:0xff|255)$/i) { |
| 6690 | # if (WARN("PREFER_ETH_BROADCAST_ADDR", |
| 6691 | # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && |
| 6692 | # $fix) { |
| 6693 | # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; |
| 6694 | # } |
| 6695 | # } |
| 6696 | # } |
Mateusz Kulikowski | 8617cd0 | 2015-06-25 15:03:19 -0700 | [diff] [blame] | 6697 | |
Joe Perches | 5dbdb2d | 2020-12-29 15:14:31 -0800 | [diff] [blame] | 6698 | # strlcpy uses that should likely be strscpy |
| 6699 | if ($line =~ /\bstrlcpy\s*\(/) { |
| 6700 | WARN("STRLCPY", |
| 6701 | "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr); |
| 6702 | } |
| 6703 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 6704 | # typecasts on min/max could be min_t/max_t |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6705 | if ($perl_version_ok && |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 6706 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 6707 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 6708 | if (defined $2 || defined $7) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 6709 | my $call = $1; |
| 6710 | my $cast1 = deparenthesize($2); |
| 6711 | my $arg1 = $3; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 6712 | my $cast2 = deparenthesize($7); |
| 6713 | my $arg2 = $8; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 6714 | my $cast; |
| 6715 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 6716 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 6717 | $cast = "$cast1 or $cast2"; |
| 6718 | } elsif ($cast1 ne "") { |
| 6719 | $cast = $cast1; |
| 6720 | } else { |
| 6721 | $cast = $cast2; |
| 6722 | } |
| 6723 | WARN("MINMAX", |
| 6724 | "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 6725 | } |
| 6726 | } |
| 6727 | |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 6728 | # check usleep_range arguments |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6729 | if ($perl_version_ok && |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 6730 | defined $stat && |
| 6731 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 6732 | my $min = $1; |
| 6733 | my $max = $7; |
| 6734 | if ($min eq $max) { |
| 6735 | WARN("USLEEP_RANGE", |
Mauro Carvalho Chehab | 458f69e | 2019-06-12 14:53:00 -0300 | [diff] [blame] | 6736 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 6737 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 6738 | $min > $max) { |
| 6739 | WARN("USLEEP_RANGE", |
Mauro Carvalho Chehab | 458f69e | 2019-06-12 14:53:00 -0300 | [diff] [blame] | 6740 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n"); |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 6741 | } |
| 6742 | } |
| 6743 | |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 6744 | # check for naked sscanf |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6745 | if ($perl_version_ok && |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 6746 | defined $stat && |
Joe Perches | 6c8bd70 | 2014-04-03 14:49:16 -0700 | [diff] [blame] | 6747 | $line =~ /\bsscanf\b/ && |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 6748 | ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && |
| 6749 | $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && |
| 6750 | $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { |
| 6751 | my $lc = $stat =~ tr@\n@@; |
| 6752 | $lc = $lc + $linenr; |
Tobin C. Harding | 2a9f9d8 | 2018-04-10 16:33:20 -0700 | [diff] [blame] | 6753 | my $stat_real = get_stat_real($linenr, $lc); |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 6754 | WARN("NAKED_SSCANF", |
| 6755 | "unchecked sscanf return value\n" . "$here\n$stat_real\n"); |
| 6756 | } |
| 6757 | |
Joe Perches | afc819a | 2014-06-04 16:12:08 -0700 | [diff] [blame] | 6758 | # check for simple sscanf that should be kstrto<foo> |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6759 | if ($perl_version_ok && |
Joe Perches | afc819a | 2014-06-04 16:12:08 -0700 | [diff] [blame] | 6760 | defined $stat && |
| 6761 | $line =~ /\bsscanf\b/) { |
| 6762 | my $lc = $stat =~ tr@\n@@; |
| 6763 | $lc = $lc + $linenr; |
Tobin C. Harding | 2a9f9d8 | 2018-04-10 16:33:20 -0700 | [diff] [blame] | 6764 | my $stat_real = get_stat_real($linenr, $lc); |
Joe Perches | afc819a | 2014-06-04 16:12:08 -0700 | [diff] [blame] | 6765 | if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { |
| 6766 | my $format = $6; |
| 6767 | my $count = $format =~ tr@%@%@; |
| 6768 | if ($count == 1 && |
| 6769 | $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { |
| 6770 | WARN("SSCANF_TO_KSTRTO", |
| 6771 | "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); |
| 6772 | } |
| 6773 | } |
| 6774 | } |
| 6775 | |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 6776 | # check for new externs in .h files. |
| 6777 | if ($realfile =~ /\.h$/ && |
| 6778 | $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { |
Joe Perches | d1d8578 | 2013-09-24 15:27:46 -0700 | [diff] [blame] | 6779 | if (CHK("AVOID_EXTERNS", |
| 6780 | "extern prototypes should be avoided in .h files\n" . $herecurr) && |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 6781 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6782 | $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 6783 | } |
| 6784 | } |
| 6785 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 6786 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 6787 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 6788 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 6789 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 6790 | my $function_name = $1; |
| 6791 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 6792 | |
| 6793 | my $s = $stat; |
| 6794 | if (defined $cond) { |
| 6795 | substr($s, 0, length($cond), ''); |
| 6796 | } |
Kees Cook | d8b44b5 | 2020-06-03 14:18:09 -0700 | [diff] [blame] | 6797 | if ($s =~ /^\s*;/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 6798 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6799 | WARN("AVOID_EXTERNS", |
| 6800 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | if ($paren_space =~ /\n/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6804 | WARN("FUNCTION_ARGUMENTS", |
| 6805 | "arguments for function declarations should follow identifier\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 6806 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 6807 | |
| 6808 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 6809 | $stat =~ /^.\s*extern\s+/) |
| 6810 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6811 | WARN("AVOID_EXTERNS", |
| 6812 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 6813 | } |
| 6814 | |
Joe Perches | a0ad759 | 2017-07-10 15:52:19 -0700 | [diff] [blame] | 6815 | # check for function declarations that have arguments without identifier names |
| 6816 | if (defined $stat && |
Kees Cook | d8b44b5 | 2020-06-03 14:18:09 -0700 | [diff] [blame] | 6817 | $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s && |
| 6818 | $1 ne "void") { |
| 6819 | my $args = trim($1); |
Joe Perches | ca0d892 | 2016-10-11 13:52:16 -0700 | [diff] [blame] | 6820 | while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) { |
| 6821 | my $arg = trim($1); |
Kees Cook | d8b44b5 | 2020-06-03 14:18:09 -0700 | [diff] [blame] | 6822 | if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) { |
Joe Perches | ca0d892 | 2016-10-11 13:52:16 -0700 | [diff] [blame] | 6823 | WARN("FUNCTION_ARGUMENTS", |
| 6824 | "function definition argument '$arg' should also have an identifier name\n" . $herecurr); |
| 6825 | } |
| 6826 | } |
| 6827 | } |
| 6828 | |
Joe Perches | a0ad759 | 2017-07-10 15:52:19 -0700 | [diff] [blame] | 6829 | # check for function definitions |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6830 | if ($perl_version_ok && |
Joe Perches | a0ad759 | 2017-07-10 15:52:19 -0700 | [diff] [blame] | 6831 | defined $stat && |
| 6832 | $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) { |
| 6833 | $context_function = $1; |
| 6834 | |
| 6835 | # check for multiline function definition with misplaced open brace |
| 6836 | my $ok = 0; |
| 6837 | my $cnt = statement_rawlines($stat); |
| 6838 | my $herectx = $here . "\n"; |
| 6839 | for (my $n = 0; $n < $cnt; $n++) { |
| 6840 | my $rl = raw_line($linenr, $n); |
| 6841 | $herectx .= $rl . "\n"; |
| 6842 | $ok = 1 if ($rl =~ /^[ \+]\{/); |
| 6843 | $ok = 1 if ($rl =~ /\{/ && $n == 0); |
| 6844 | last if $rl =~ /^[ \+].*\{/; |
| 6845 | } |
| 6846 | if (!$ok) { |
| 6847 | ERROR("OPEN_BRACE", |
| 6848 | "open brace '{' following function definitions go on the next line\n" . $herectx); |
| 6849 | } |
| 6850 | } |
| 6851 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 6852 | # checks for new __setup's |
| 6853 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 6854 | my $name = $1; |
| 6855 | |
| 6856 | if (!grep(/$name/, @setup_docs)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6857 | CHK("UNDOCUMENTED_SETUP", |
Tim Froidcoeur | 2581ac7 | 2020-06-10 18:41:38 -0700 | [diff] [blame] | 6858 | "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 6859 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 6860 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 6861 | |
Joe Perches | e29a70f | 2019-03-07 16:28:35 -0800 | [diff] [blame] | 6862 | # check for pointless casting of alloc functions |
| 6863 | if ($line =~ /\*\s*\)\s*$allocFunctions\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 6864 | WARN("UNNECESSARY_CASTS", |
| 6865 | "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 6866 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 6867 | |
Joe Perches | a640d25 | 2013-07-03 15:05:21 -0700 | [diff] [blame] | 6868 | # alloc style |
| 6869 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6870 | if ($perl_version_ok && |
Joe Perches | e29a70f | 2019-03-07 16:28:35 -0800 | [diff] [blame] | 6871 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { |
Joe Perches | a640d25 | 2013-07-03 15:05:21 -0700 | [diff] [blame] | 6872 | CHK("ALLOC_SIZEOF_STRUCT", |
| 6873 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 6874 | } |
| 6875 | |
Joe Perches | 60a5536 | 2014-06-04 16:12:07 -0700 | [diff] [blame] | 6876 | # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6877 | if ($perl_version_ok && |
Joe Perches | 1b4a2ed | 2017-05-08 15:55:57 -0700 | [diff] [blame] | 6878 | defined $stat && |
| 6879 | $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { |
Joe Perches | 60a5536 | 2014-06-04 16:12:07 -0700 | [diff] [blame] | 6880 | my $oldfunc = $3; |
| 6881 | my $a1 = $4; |
| 6882 | my $a2 = $10; |
| 6883 | my $newfunc = "kmalloc_array"; |
| 6884 | $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); |
Joe Perches | e367455 | 2014-08-06 16:10:55 -0700 | [diff] [blame] | 6885 | my $r1 = $a1; |
| 6886 | my $r2 = $a2; |
| 6887 | if ($a1 =~ /^sizeof\s*\S/) { |
| 6888 | $r1 = $a2; |
| 6889 | $r2 = $a1; |
| 6890 | } |
| 6891 | if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && |
| 6892 | !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { |
Joe Perches | 1b4a2ed | 2017-05-08 15:55:57 -0700 | [diff] [blame] | 6893 | my $cnt = statement_rawlines($stat); |
Tobin C. Harding | e3d95a2 | 2018-04-10 16:33:27 -0700 | [diff] [blame] | 6894 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 6895 | |
Joe Perches | 60a5536 | 2014-06-04 16:12:07 -0700 | [diff] [blame] | 6896 | if (WARN("ALLOC_WITH_MULTIPLY", |
Joe Perches | 1b4a2ed | 2017-05-08 15:55:57 -0700 | [diff] [blame] | 6897 | "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && |
| 6898 | $cnt == 1 && |
Joe Perches | 60a5536 | 2014-06-04 16:12:07 -0700 | [diff] [blame] | 6899 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6900 | $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; |
Joe Perches | 60a5536 | 2014-06-04 16:12:07 -0700 | [diff] [blame] | 6901 | } |
| 6902 | } |
| 6903 | } |
| 6904 | |
Joe Perches | 972fdea | 2013-04-29 16:18:12 -0700 | [diff] [blame] | 6905 | # check for krealloc arg reuse |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6906 | if ($perl_version_ok && |
Joe Perches | 4cab63c | 2018-08-21 21:57:50 -0700 | [diff] [blame] | 6907 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ && |
| 6908 | $1 eq $3) { |
Joe Perches | 972fdea | 2013-04-29 16:18:12 -0700 | [diff] [blame] | 6909 | WARN("KREALLOC_ARG_REUSE", |
| 6910 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 6911 | } |
| 6912 | |
Joe Perches | 5ce59ae | 2013-02-21 16:44:18 -0800 | [diff] [blame] | 6913 | # check for alloc argument mismatch |
| 6914 | if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { |
| 6915 | WARN("ALLOC_ARRAY_ARGS", |
| 6916 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
| 6917 | } |
| 6918 | |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 6919 | # check for multiple semicolons |
| 6920 | if ($line =~ /;\s*;\s*$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6921 | if (WARN("ONE_SEMICOLON", |
| 6922 | "Statements terminations use 1 semicolon\n" . $herecurr) && |
| 6923 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6924 | $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6925 | } |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 6926 | } |
| 6927 | |
Tomas Winkler | cec3aaa | 2016-08-02 14:04:39 -0700 | [diff] [blame] | 6928 | # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi |
| 6929 | if ($realfile !~ m@^include/uapi/@ && |
| 6930 | $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { |
Joe Perches | 0ab9019 | 2014-12-10 15:51:57 -0800 | [diff] [blame] | 6931 | my $ull = ""; |
| 6932 | $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); |
| 6933 | if (CHK("BIT_MACRO", |
| 6934 | "Prefer using the BIT$ull macro\n" . $herecurr) && |
| 6935 | $fix) { |
| 6936 | $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; |
| 6937 | } |
| 6938 | } |
| 6939 | |
Joe Perches | 5016126 | 2020-08-11 18:35:07 -0700 | [diff] [blame] | 6940 | # check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too) |
Jerome Forissier | 3e89ad8 | 2020-10-15 20:11:49 -0700 | [diff] [blame] | 6941 | if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) { |
Joe Perches | 5016126 | 2020-08-11 18:35:07 -0700 | [diff] [blame] | 6942 | WARN("IS_ENABLED_CONFIG", |
Jerome Forissier | 3e89ad8 | 2020-10-15 20:11:49 -0700 | [diff] [blame] | 6943 | "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr); |
Joe Perches | 5016126 | 2020-08-11 18:35:07 -0700 | [diff] [blame] | 6944 | } |
| 6945 | |
Joe Perches | 2d63274 | 2016-05-20 17:04:00 -0700 | [diff] [blame] | 6946 | # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE |
Jerome Forissier | 3e89ad8 | 2020-10-15 20:11:49 -0700 | [diff] [blame] | 6947 | if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) { |
Joe Perches | 2d63274 | 2016-05-20 17:04:00 -0700 | [diff] [blame] | 6948 | my $config = $1; |
| 6949 | if (WARN("PREFER_IS_ENABLED", |
Jerome Forissier | 3e89ad8 | 2020-10-15 20:11:49 -0700 | [diff] [blame] | 6950 | "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) && |
Joe Perches | 2d63274 | 2016-05-20 17:04:00 -0700 | [diff] [blame] | 6951 | $fix) { |
| 6952 | $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; |
| 6953 | } |
| 6954 | } |
| 6955 | |
Joe Perches | f36d3eb | 2020-04-06 20:10:58 -0700 | [diff] [blame] | 6956 | # check for /* fallthrough */ like comment, prefer fallthrough; |
| 6957 | my @fallthroughs = ( |
| 6958 | 'fallthrough', |
| 6959 | '@fallthrough@', |
| 6960 | 'lint -fallthrough[ \t]*', |
| 6961 | 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)', |
| 6962 | '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?', |
| 6963 | 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', |
| 6964 | 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?', |
| 6965 | ); |
| 6966 | if ($raw_comment ne '') { |
| 6967 | foreach my $ft (@fallthroughs) { |
| 6968 | if ($raw_comment =~ /$ft/) { |
| 6969 | my $msg_level = \&WARN; |
| 6970 | $msg_level = \&CHK if ($file); |
| 6971 | &{$msg_level}("PREFER_FALLTHROUGH", |
| 6972 | "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr); |
| 6973 | last; |
| 6974 | } |
| 6975 | } |
| 6976 | } |
| 6977 | |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 6978 | # check for switch/default statements without a break; |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 6979 | if ($perl_version_ok && |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 6980 | defined $stat && |
| 6981 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 6982 | my $cnt = statement_rawlines($stat); |
Tobin C. Harding | e3d95a2 | 2018-04-10 16:33:27 -0700 | [diff] [blame] | 6983 | my $herectx = get_stat_here($linenr, $cnt, $here); |
| 6984 | |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 6985 | WARN("DEFAULT_NO_BREAK", |
| 6986 | "switch default: should use break\n" . $herectx); |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 6987 | } |
| 6988 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 6989 | # check for gcc specific __FUNCTION__ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6990 | if ($line =~ /\b__FUNCTION__\b/) { |
| 6991 | if (WARN("USE_FUNC", |
| 6992 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && |
| 6993 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 6994 | $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 6995 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 6996 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 6997 | |
Joe Perches | 62ec818 | 2015-02-13 14:38:18 -0800 | [diff] [blame] | 6998 | # check for uses of __DATE__, __TIME__, __TIMESTAMP__ |
| 6999 | while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { |
| 7000 | ERROR("DATE_TIME", |
| 7001 | "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); |
| 7002 | } |
| 7003 | |
Joe Perches | 2c92488 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 7004 | # check for use of yield() |
| 7005 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 7006 | WARN("YIELD", |
| 7007 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 7008 | } |
| 7009 | |
Joe Perches | 179f8f4 | 2013-07-03 15:05:30 -0700 | [diff] [blame] | 7010 | # check for comparisons against true and false |
| 7011 | if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { |
| 7012 | my $lead = $1; |
| 7013 | my $arg = $2; |
| 7014 | my $test = $3; |
| 7015 | my $otype = $4; |
| 7016 | my $trail = $5; |
| 7017 | my $op = "!"; |
| 7018 | |
| 7019 | ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); |
| 7020 | |
| 7021 | my $type = lc($otype); |
| 7022 | if ($type =~ /^(?:true|false)$/) { |
| 7023 | if (("$test" eq "==" && "$type" eq "true") || |
| 7024 | ("$test" eq "!=" && "$type" eq "false")) { |
| 7025 | $op = ""; |
| 7026 | } |
| 7027 | |
| 7028 | CHK("BOOL_COMPARISON", |
| 7029 | "Using comparison to $otype is error prone\n" . $herecurr); |
| 7030 | |
| 7031 | ## maybe suggesting a correct construct would better |
| 7032 | ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); |
| 7033 | |
| 7034 | } |
| 7035 | } |
| 7036 | |
Thomas Gleixner | 4882720b | 2010-09-07 14:34:01 +0000 | [diff] [blame] | 7037 | # check for semaphores initialized locked |
| 7038 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7039 | WARN("CONSIDER_COMPLETION", |
| 7040 | "consider using a completion\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 7041 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 7042 | |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 7043 | # recommend kstrto* over simple_strto* and strict_strto* |
| 7044 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7045 | WARN("CONSIDER_KSTRTO", |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 7046 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 7047 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 7048 | |
Fabian Frederick | ae3ccc4 | 2014-06-04 16:12:10 -0700 | [diff] [blame] | 7049 | # check for __initcall(), use device_initcall() explicitly or more appropriate function please |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 7050 | if ($line =~ /^.\s*__initcall\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7051 | WARN("USE_DEVICE_INITCALL", |
Fabian Frederick | ae3ccc4 | 2014-06-04 16:12:10 -0700 | [diff] [blame] | 7052 | "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 7053 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 7054 | |
Paul E. McKenney | 3d709ab | 2018-11-11 10:49:10 -0800 | [diff] [blame] | 7055 | # check for spin_is_locked(), suggest lockdep instead |
| 7056 | if ($line =~ /\bspin_is_locked\(/) { |
| 7057 | WARN("USE_LOCKDEP", |
| 7058 | "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr); |
| 7059 | } |
| 7060 | |
Joe Perches | 9189c7e | 2018-09-07 15:26:18 -0700 | [diff] [blame] | 7061 | # check for deprecated apis |
| 7062 | if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) { |
| 7063 | my $deprecated_api = $1; |
| 7064 | my $new_api = $deprecated_apis{$deprecated_api}; |
| 7065 | WARN("DEPRECATED_API", |
| 7066 | "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr); |
| 7067 | } |
| 7068 | |
Joe Perches | 0f3c5aa | 2015-02-13 14:39:05 -0800 | [diff] [blame] | 7069 | # check for various structs that are normally const (ops, kgdb, device_tree) |
Joe Perches | d9190e4 | 2017-05-08 15:55:45 -0700 | [diff] [blame] | 7070 | # and avoid what seem like struct definitions 'struct foo {' |
Quentin Monnet | ced69da1 | 2020-08-11 18:35:13 -0700 | [diff] [blame] | 7071 | if (defined($const_structs) && |
| 7072 | $line !~ /\bconst\b/ && |
Joe Perches | d9190e4 | 2017-05-08 15:55:45 -0700 | [diff] [blame] | 7073 | $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7074 | WARN("CONST_STRUCT", |
Joe Perches | d9190e4 | 2017-05-08 15:55:45 -0700 | [diff] [blame] | 7075 | "struct $1 should normally be const\n" . $herecurr); |
Andy Whitcroft | 2b6db5c | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 7076 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 7077 | |
| 7078 | # use of NR_CPUS is usually wrong |
| 7079 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
Peng Wang | 35cdcbf | 2021-02-25 17:21:44 -0800 | [diff] [blame] | 7080 | # ignore designated initializers using NR_CPUS |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 7081 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 7082 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 7083 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 7084 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 7085 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
Peng Wang | 35cdcbf | 2021-02-25 17:21:44 -0800 | [diff] [blame] | 7086 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ && |
| 7087 | $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 7088 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7089 | WARN("NR_CPUS", |
| 7090 | "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 7091 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 7092 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 7093 | # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. |
| 7094 | if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { |
| 7095 | ERROR("DEFINE_ARCH_HAS", |
| 7096 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); |
| 7097 | } |
| 7098 | |
Joe Perches | acd9362 | 2015-02-13 14:38:38 -0800 | [diff] [blame] | 7099 | # likely/unlikely comparisons similar to "(likely(foo) > 0)" |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 7100 | if ($perl_version_ok && |
Joe Perches | acd9362 | 2015-02-13 14:38:38 -0800 | [diff] [blame] | 7101 | $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { |
| 7102 | WARN("LIKELY_MISUSE", |
| 7103 | "Using $1 should generally have parentheses around the comparison\n" . $herecurr); |
| 7104 | } |
| 7105 | |
Denis Efremov | de3f186 | 2019-09-25 16:49:25 -0700 | [diff] [blame] | 7106 | # nested likely/unlikely calls |
| 7107 | if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) { |
| 7108 | WARN("LIKELY_MISUSE", |
| 7109 | "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr); |
| 7110 | } |
| 7111 | |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 7112 | # whine mightly about in_atomic |
| 7113 | if ($line =~ /\bin_atomic\s*\(/) { |
| 7114 | if ($realfile =~ m@^drivers/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7115 | ERROR("IN_ATOMIC", |
| 7116 | "do not use in_atomic in drivers\n" . $herecurr); |
Andy Whitcroft | f4a8773 | 2009-02-27 14:03:05 -0800 | [diff] [blame] | 7117 | } elsif ($realfile !~ m@^kernel/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7118 | WARN("IN_ATOMIC", |
| 7119 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 7120 | } |
| 7121 | } |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 7122 | |
| 7123 | # check for lockdep_set_novalidate_class |
| 7124 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 7125 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 7126 | if ($realfile !~ m@^kernel/lockdep@ && |
| 7127 | $realfile !~ m@^include/linux/lockdep@ && |
| 7128 | $realfile !~ m@^drivers/base/core@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7129 | ERROR("LOCKDEP", |
| 7130 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 7131 | } |
| 7132 | } |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 7133 | |
Joe Perches | b392c64 | 2015-04-16 12:44:16 -0700 | [diff] [blame] | 7134 | if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || |
| 7135 | $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7136 | WARN("EXPORTED_WORLD_WRITABLE", |
| 7137 | "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 7138 | } |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 7139 | |
Joe Perches | 0018046 | 2018-02-06 15:38:55 -0800 | [diff] [blame] | 7140 | # check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO> |
| 7141 | # and whether or not function naming is typical and if |
| 7142 | # DEVICE_ATTR permissions uses are unusual too |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 7143 | if ($perl_version_ok && |
Joe Perches | 0018046 | 2018-02-06 15:38:55 -0800 | [diff] [blame] | 7144 | defined $stat && |
| 7145 | $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*\)/) { |
| 7146 | my $var = $1; |
| 7147 | my $perms = $2; |
| 7148 | my $show = $3; |
| 7149 | my $store = $4; |
| 7150 | my $octal_perms = perms_to_octal($perms); |
| 7151 | if ($show =~ /^${var}_show$/ && |
| 7152 | $store =~ /^${var}_store$/ && |
| 7153 | $octal_perms eq "0644") { |
| 7154 | if (WARN("DEVICE_ATTR_RW", |
| 7155 | "Use DEVICE_ATTR_RW\n" . $herecurr) && |
| 7156 | $fix) { |
| 7157 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/; |
| 7158 | } |
| 7159 | } elsif ($show =~ /^${var}_show$/ && |
| 7160 | $store =~ /^NULL$/ && |
| 7161 | $octal_perms eq "0444") { |
| 7162 | if (WARN("DEVICE_ATTR_RO", |
| 7163 | "Use DEVICE_ATTR_RO\n" . $herecurr) && |
| 7164 | $fix) { |
| 7165 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/; |
| 7166 | } |
| 7167 | } elsif ($show =~ /^NULL$/ && |
| 7168 | $store =~ /^${var}_store$/ && |
| 7169 | $octal_perms eq "0200") { |
| 7170 | if (WARN("DEVICE_ATTR_WO", |
| 7171 | "Use DEVICE_ATTR_WO\n" . $herecurr) && |
| 7172 | $fix) { |
| 7173 | $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/; |
| 7174 | } |
| 7175 | } elsif ($octal_perms eq "0644" || |
| 7176 | $octal_perms eq "0444" || |
| 7177 | $octal_perms eq "0200") { |
| 7178 | my $newshow = "$show"; |
| 7179 | $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show"); |
| 7180 | my $newstore = $store; |
| 7181 | $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store"); |
| 7182 | my $rename = ""; |
| 7183 | if ($show ne $newshow) { |
| 7184 | $rename .= " '$show' to '$newshow'"; |
| 7185 | } |
| 7186 | if ($store ne $newstore) { |
| 7187 | $rename .= " '$store' to '$newstore'"; |
| 7188 | } |
| 7189 | WARN("DEVICE_ATTR_FUNCTIONS", |
| 7190 | "Consider renaming function(s)$rename\n" . $herecurr); |
| 7191 | } else { |
| 7192 | WARN("DEVICE_ATTR_PERMS", |
| 7193 | "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr); |
| 7194 | } |
| 7195 | } |
| 7196 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 7197 | # Mode permission misuses where it seems decimal should be octal |
| 7198 | # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop |
Joe Perches | 7312153 | 2018-02-06 15:38:49 -0800 | [diff] [blame] | 7199 | # o Ignore module_param*(...) uses with a decimal 0 permission as that has a |
| 7200 | # specific definition of not visible in sysfs. |
| 7201 | # o Ignore proc_create*(...) uses with a decimal 0 permission as that means |
| 7202 | # use the default permissions |
Joe Perches | 5b57980 | 2018-08-21 21:57:33 -0700 | [diff] [blame] | 7203 | if ($perl_version_ok && |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 7204 | defined $stat && |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 7205 | $line =~ /$mode_perms_search/) { |
| 7206 | foreach my $entry (@mode_permission_funcs) { |
| 7207 | my $func = $entry->[0]; |
| 7208 | my $arg_pos = $entry->[1]; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 7209 | |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 7210 | my $lc = $stat =~ tr@\n@@; |
| 7211 | $lc = $lc + $linenr; |
Tobin C. Harding | 2a9f9d8 | 2018-04-10 16:33:20 -0700 | [diff] [blame] | 7212 | my $stat_real = get_stat_real($linenr, $lc); |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 7213 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 7214 | my $skip_args = ""; |
| 7215 | if ($arg_pos > 1) { |
| 7216 | $arg_pos--; |
| 7217 | $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; |
| 7218 | } |
Joe Perches | f90774e | 2016-10-11 13:51:47 -0700 | [diff] [blame] | 7219 | my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]"; |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 7220 | if ($stat =~ /$test/) { |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 7221 | my $val = $1; |
| 7222 | $val = $6 if ($skip_args ne ""); |
Joe Perches | 7312153 | 2018-02-06 15:38:49 -0800 | [diff] [blame] | 7223 | if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") && |
| 7224 | (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || |
| 7225 | ($val =~ /^$Octal$/ && length($val) ne 4))) { |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 7226 | ERROR("NON_OCTAL_PERMISSIONS", |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 7227 | "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real); |
Joe Perches | f90774e | 2016-10-11 13:51:47 -0700 | [diff] [blame] | 7228 | } |
| 7229 | if ($val =~ /^$Octal$/ && (oct($val) & 02)) { |
Joe Perches | c0a5c89 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 7230 | ERROR("EXPORTED_WORLD_WRITABLE", |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 7231 | "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real); |
Joe Perches | f90774e | 2016-10-11 13:51:47 -0700 | [diff] [blame] | 7232 | } |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 7233 | } |
| 7234 | } |
| 7235 | } |
Bjorn Andersson | 5a6d20c | 2015-06-25 15:03:24 -0700 | [diff] [blame] | 7236 | |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 7237 | # check for uses of S_<PERMS> that could be octal for readability |
Joe Perches | bc22d9a | 2018-04-10 16:33:53 -0700 | [diff] [blame] | 7238 | while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) { |
Joe Perches | 0018046 | 2018-02-06 15:38:55 -0800 | [diff] [blame] | 7239 | my $oval = $1; |
| 7240 | my $octal = perms_to_octal($oval); |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 7241 | if (WARN("SYMBOLIC_PERMS", |
| 7242 | "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) && |
| 7243 | $fix) { |
Joe Perches | 0018046 | 2018-02-06 15:38:55 -0800 | [diff] [blame] | 7244 | $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/; |
Joe Perches | 459cf0a | 2016-10-11 13:52:19 -0700 | [diff] [blame] | 7245 | } |
| 7246 | } |
| 7247 | |
Bjorn Andersson | 5a6d20c | 2015-06-25 15:03:24 -0700 | [diff] [blame] | 7248 | # validate content of MODULE_LICENSE against list from include/linux/module.h |
| 7249 | if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { |
| 7250 | my $extracted_string = get_quoted_string($line, $rawline); |
| 7251 | my $valid_licenses = qr{ |
| 7252 | GPL| |
| 7253 | GPL\ v2| |
| 7254 | GPL\ and\ additional\ rights| |
| 7255 | Dual\ BSD/GPL| |
| 7256 | Dual\ MIT/GPL| |
| 7257 | Dual\ MPL/GPL| |
| 7258 | Proprietary |
| 7259 | }x; |
| 7260 | if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { |
| 7261 | WARN("MODULE_LICENSE", |
| 7262 | "unknown module license " . $extracted_string . "\n" . $herecurr); |
| 7263 | } |
| 7264 | } |
Matteo Croce | 6a8d76c | 2019-07-16 16:27:48 -0700 | [diff] [blame] | 7265 | |
| 7266 | # check for sysctl duplicate constants |
| 7267 | if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) { |
| 7268 | WARN("DUPLICATED_SYSCTL_CONST", |
| 7269 | "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr); |
| 7270 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 7271 | } |
| 7272 | |
| 7273 | # If we have no input at all, then there is nothing to report on |
| 7274 | # so just keep quiet. |
| 7275 | if ($#rawlines == -1) { |
| 7276 | exit(0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 7277 | } |
| 7278 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 7279 | # In mailback mode only produce a report in the negative, for |
| 7280 | # things that appear to be patches. |
| 7281 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 7282 | exit(0); |
| 7283 | } |
| 7284 | |
Dwaipayan Ray | e73d271 | 2020-12-15 20:44:56 -0800 | [diff] [blame] | 7285 | # This is not a patch, and we are in 'no-patch' mode so |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 7286 | # just keep quiet. |
| 7287 | if (!$chk_patch && !$is_patch) { |
| 7288 | exit(0); |
| 7289 | } |
| 7290 | |
Stafford Horne | a08ffbe | 2017-10-03 16:16:51 -0700 | [diff] [blame] | 7291 | if (!$is_patch && $filename !~ /cover-letter\.patch$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 7292 | ERROR("NOT_UNIFIED_DIFF", |
| 7293 | "Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 7294 | } |
Geert Uytterhoeven | cd26149 | 2018-08-21 21:57:40 -0700 | [diff] [blame] | 7295 | if ($is_patch && $has_commit_log && $chk_signoff) { |
| 7296 | if ($signoff == 0) { |
| 7297 | ERROR("MISSING_SIGN_OFF", |
| 7298 | "Missing Signed-off-by: line(s)\n"); |
Dwaipayan Ray | 48ca2d8 | 2020-10-15 20:12:28 -0700 | [diff] [blame] | 7299 | } elsif ($authorsignoff != 1) { |
| 7300 | # authorsignoff values: |
| 7301 | # 0 -> missing sign off |
| 7302 | # 1 -> sign off identical |
| 7303 | # 2 -> names and addresses match, comments mismatch |
| 7304 | # 3 -> addresses match, names different |
| 7305 | # 4 -> names match, addresses different |
| 7306 | # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match |
| 7307 | |
| 7308 | my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'"; |
| 7309 | |
| 7310 | if ($authorsignoff == 0) { |
| 7311 | ERROR("NO_AUTHOR_SIGN_OFF", |
| 7312 | "Missing Signed-off-by: line by nominal patch author '$author'\n"); |
| 7313 | } elsif ($authorsignoff == 2) { |
| 7314 | CHK("FROM_SIGN_OFF_MISMATCH", |
| 7315 | "From:/Signed-off-by: email comments mismatch: $sob_msg\n"); |
| 7316 | } elsif ($authorsignoff == 3) { |
| 7317 | WARN("FROM_SIGN_OFF_MISMATCH", |
| 7318 | "From:/Signed-off-by: email name mismatch: $sob_msg\n"); |
| 7319 | } elsif ($authorsignoff == 4) { |
| 7320 | WARN("FROM_SIGN_OFF_MISMATCH", |
| 7321 | "From:/Signed-off-by: email address mismatch: $sob_msg\n"); |
| 7322 | } elsif ($authorsignoff == 5) { |
| 7323 | WARN("FROM_SIGN_OFF_MISMATCH", |
| 7324 | "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n"); |
| 7325 | } |
Geert Uytterhoeven | cd26149 | 2018-08-21 21:57:40 -0700 | [diff] [blame] | 7326 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 7327 | } |
| 7328 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 7329 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 7330 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 7331 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 7332 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 7333 | (($check)? "$cnt_chk checks, " : "") . |
| 7334 | "$cnt_lines lines checked\n"; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 7335 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 7336 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 7337 | if ($quiet == 0) { |
Joe Perches | ef21219 | 2016-05-20 17:04:11 -0700 | [diff] [blame] | 7338 | # If there were any defects found and not already fixing them |
| 7339 | if (!$clean and !$fix) { |
| 7340 | print << "EOM" |
| 7341 | |
| 7342 | NOTE: For some of the reported defects, checkpatch may be able to |
| 7343 | mechanically convert to the typical style using --fix or --fix-inplace. |
| 7344 | EOM |
| 7345 | } |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 7346 | # If there were whitespace errors which cleanpatch can fix |
| 7347 | # then suggest that. |
| 7348 | if ($rpt_cleaners) { |
Mike Frysinger | b078121 | 2011-03-22 16:34:43 -0700 | [diff] [blame] | 7349 | $rpt_cleaners = 0; |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 7350 | print << "EOM" |
| 7351 | |
| 7352 | NOTE: Whitespace errors detected. |
| 7353 | You may wish to use scripts/cleanpatch or scripts/cleanfile |
| 7354 | EOM |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 7355 | } |
| 7356 | } |
| 7357 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 7358 | if ($clean == 0 && $fix && |
| 7359 | ("@rawlines" ne "@fixed" || |
| 7360 | $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 7361 | my $newfile = $filename; |
| 7362 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 7363 | my $linecount = 0; |
| 7364 | my $f; |
| 7365 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 7366 | @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); |
| 7367 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 7368 | open($f, '>', $newfile) |
| 7369 | or die "$P: Can't open $newfile for write\n"; |
| 7370 | foreach my $fixed_line (@fixed) { |
| 7371 | $linecount++; |
| 7372 | if ($file) { |
| 7373 | if ($linecount > 3) { |
| 7374 | $fixed_line =~ s/^\+//; |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 7375 | print $f $fixed_line . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 7376 | } |
| 7377 | } else { |
| 7378 | print $f $fixed_line . "\n"; |
| 7379 | } |
| 7380 | } |
| 7381 | close($f); |
| 7382 | |
| 7383 | if (!$quiet) { |
| 7384 | print << "EOM"; |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 7385 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 7386 | Wrote EXPERIMENTAL --fix correction(s) to '$newfile' |
| 7387 | |
| 7388 | Do _NOT_ trust the results written to this file. |
| 7389 | Do _NOT_ submit these changes without inspecting them for correctness. |
| 7390 | |
| 7391 | This EXPERIMENTAL file is simply a convenience to help rewrite patches. |
| 7392 | No warranties, expressed or implied... |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 7393 | EOM |
| 7394 | } |
| 7395 | } |
| 7396 | |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 7397 | if ($quiet == 0) { |
| 7398 | print "\n"; |
| 7399 | if ($clean == 1) { |
| 7400 | print "$vname has no obvious style problems and is ready for submission.\n"; |
| 7401 | } else { |
| 7402 | print "$vname has style problems, please review.\n"; |
| 7403 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 7404 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 7405 | return $clean; |
| 7406 | } |