Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
Dave Jones | dbf004d | 2010-01-12 16:59:52 -0500 | [diff] [blame] | 2 | # (c) 2001, Dave Jones. (the file handling bit) |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3 | # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) |
Andy Whitcroft | 2a5a2c2 | 2009-01-06 14:41:23 -0800 | [diff] [blame] | 4 | # (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] | 5 | # (c) 2008-2010 Andy Whitcroft <apw@canonical.com> |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 6 | # Licensed under the terms of the GNU GPL License version 2 |
| 7 | |
| 8 | use strict; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 9 | use POSIX; |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 10 | use File::Basename; |
| 11 | use Cwd 'abs_path'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 12 | |
| 13 | my $P = $0; |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 14 | my $D = dirname(abs_path($P)); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 15 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 16 | my $V = '0.32'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 17 | |
| 18 | use Getopt::Long qw(:config no_auto_abbrev); |
| 19 | |
| 20 | my $quiet = 0; |
| 21 | my $tree = 1; |
| 22 | my $chk_signoff = 1; |
| 23 | my $chk_patch = 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 24 | my $tst_only; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 25 | my $emacs = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 26 | my $terse = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 27 | my $file = 0; |
| 28 | my $check = 0; |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 29 | my $check_orig = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 30 | my $summary = 1; |
| 31 | my $mailback = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 32 | my $summary_file = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 33 | my $show_types = 0; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 34 | my $fix = 0; |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 35 | my $fix_inplace = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 36 | my $root; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 37 | my %debug; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 38 | my %camelcase = (); |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 39 | my %use_type = (); |
| 40 | my @use = (); |
| 41 | my %ignore_type = (); |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 42 | my @ignore = (); |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 43 | my $help = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 44 | my $configuration_file = ".checkpatch.conf"; |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 45 | my $max_line_length = 80; |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 46 | my $ignore_perl_version = 0; |
| 47 | my $minimum_perl_version = 5.10.0; |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 48 | my $min_conf_desc_length = 4; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 49 | my $spelling_file = "$D/spelling.txt"; |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 50 | my $codespell = 0; |
| 51 | my $codespellfile = "/usr/local/share/codespell/dictionary.txt"; |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 52 | |
| 53 | sub help { |
| 54 | my ($exitcode) = @_; |
| 55 | |
| 56 | print << "EOM"; |
| 57 | Usage: $P [OPTION]... [FILE]... |
| 58 | Version: $V |
| 59 | |
| 60 | Options: |
| 61 | -q, --quiet quiet |
| 62 | --no-tree run without a kernel tree |
| 63 | --no-signoff do not check for 'Signed-off-by' line |
| 64 | --patch treat FILE as patchfile (default) |
| 65 | --emacs emacs compile window format |
| 66 | --terse one line per report |
| 67 | -f, --file treat FILE as regular source file |
| 68 | --subjective, --strict enable more subjective tests |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 69 | --types TYPE(,TYPE2...) show only these comma separated message types |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 70 | --ignore TYPE(,TYPE2...) ignore various comma separated message types |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 71 | --max-line-length=n set the maximum line length, if exceeded, warn |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 72 | --min-conf-desc-length=n set the min description length, if shorter, warn |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 73 | --show-types show the message "types" in the output |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 74 | --root=PATH PATH to the kernel tree root |
| 75 | --no-summary suppress the per-file summary |
| 76 | --mailback only produce a report in case of warnings/errors |
| 77 | --summary-file include the filename in summary |
| 78 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 79 | 'values', 'possible', 'type', and 'attr' (default |
| 80 | is all off) |
| 81 | --test-only=WORD report only warnings/errors containing WORD |
| 82 | literally |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 83 | --fix EXPERIMENTAL - may create horrible results |
| 84 | If correctable single-line errors exist, create |
| 85 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" |
| 86 | with potential errors corrected to the preferred |
| 87 | checkpatch style |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 88 | --fix-inplace EXPERIMENTAL - may create horrible results |
| 89 | Is the same as --fix, but overwrites the input |
| 90 | file. It's your fault if there's no backup or git |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 91 | --ignore-perl-version override checking of perl version. expect |
| 92 | runtime errors. |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 93 | --codespell Use the codespell dictionary for spelling/typos |
| 94 | (default:/usr/local/share/codespell/dictionary.txt) |
| 95 | --codespellfile Use this codespell dictionary |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 96 | -h, --help, --version display this help and exit |
| 97 | |
| 98 | When FILE is - read standard input. |
| 99 | EOM |
| 100 | |
| 101 | exit($exitcode); |
| 102 | } |
| 103 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 104 | my $conf = which_conf($configuration_file); |
| 105 | if (-f $conf) { |
| 106 | my @conf_args; |
| 107 | open(my $conffile, '<', "$conf") |
| 108 | or warn "$P: Can't find a readable $configuration_file file $!\n"; |
| 109 | |
| 110 | while (<$conffile>) { |
| 111 | my $line = $_; |
| 112 | |
| 113 | $line =~ s/\s*\n?$//g; |
| 114 | $line =~ s/^\s*//g; |
| 115 | $line =~ s/\s+/ /g; |
| 116 | |
| 117 | next if ($line =~ m/^\s*#/); |
| 118 | next if ($line =~ m/^\s*$/); |
| 119 | |
| 120 | my @words = split(" ", $line); |
| 121 | foreach my $word (@words) { |
| 122 | last if ($word =~ m/^#/); |
| 123 | push (@conf_args, $word); |
| 124 | } |
| 125 | } |
| 126 | close($conffile); |
| 127 | unshift(@ARGV, @conf_args) if @conf_args; |
| 128 | } |
| 129 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 130 | GetOptions( |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 131 | 'q|quiet+' => \$quiet, |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 132 | 'tree!' => \$tree, |
| 133 | 'signoff!' => \$chk_signoff, |
| 134 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 135 | 'emacs!' => \$emacs, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 136 | 'terse!' => \$terse, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 137 | 'f|file!' => \$file, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 138 | 'subjective!' => \$check, |
| 139 | 'strict!' => \$check, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 140 | 'ignore=s' => \@ignore, |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 141 | 'types=s' => \@use, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 142 | 'show-types!' => \$show_types, |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 143 | 'max-line-length=i' => \$max_line_length, |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 144 | 'min-conf-desc-length=i' => \$min_conf_desc_length, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 145 | 'root=s' => \$root, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 146 | 'summary!' => \$summary, |
| 147 | 'mailback!' => \$mailback, |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 148 | 'summary-file!' => \$summary_file, |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 149 | 'fix!' => \$fix, |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 150 | 'fix-inplace!' => \$fix_inplace, |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 151 | 'ignore-perl-version!' => \$ignore_perl_version, |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 152 | 'debug=s' => \%debug, |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 153 | 'test-only=s' => \$tst_only, |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 154 | 'codespell!' => \$codespell, |
| 155 | 'codespellfile=s' => \$codespellfile, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 156 | 'h|help' => \$help, |
| 157 | 'version' => \$help |
| 158 | ) or help(1); |
| 159 | |
| 160 | help(0) if ($help); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 161 | |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 162 | $fix = 1 if ($fix_inplace); |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 163 | $check_orig = $check; |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 164 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 165 | my $exit = 0; |
| 166 | |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 167 | if ($^V && $^V lt $minimum_perl_version) { |
| 168 | printf "$P: requires at least perl version %vd\n", $minimum_perl_version; |
| 169 | if (!$ignore_perl_version) { |
| 170 | exit(1); |
| 171 | } |
| 172 | } |
| 173 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 174 | if ($#ARGV < 0) { |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 175 | print "$P: no input files\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 176 | exit(1); |
| 177 | } |
| 178 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 179 | sub hash_save_array_words { |
| 180 | my ($hashRef, $arrayRef) = @_; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 181 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 182 | my @array = split(/,/, join(',', @$arrayRef)); |
| 183 | foreach my $word (@array) { |
| 184 | $word =~ s/\s*\n?$//g; |
| 185 | $word =~ s/^\s*//g; |
| 186 | $word =~ s/\s+/ /g; |
| 187 | $word =~ tr/[a-z]/[A-Z]/; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 188 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 189 | next if ($word =~ m/^\s*#/); |
| 190 | next if ($word =~ m/^\s*$/); |
| 191 | |
| 192 | $hashRef->{$word}++; |
| 193 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 196 | sub hash_show_words { |
| 197 | my ($hashRef, $prefix) = @_; |
| 198 | |
Joe Perches | 58cb3cf | 2013-09-11 14:24:04 -0700 | [diff] [blame] | 199 | if ($quiet == 0 && keys %$hashRef) { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 200 | print "NOTE: $prefix message types:"; |
Joe Perches | 58cb3cf | 2013-09-11 14:24:04 -0700 | [diff] [blame] | 201 | foreach my $word (sort keys %$hashRef) { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 202 | print " $word"; |
| 203 | } |
| 204 | print "\n\n"; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | hash_save_array_words(\%ignore_type, \@ignore); |
| 209 | hash_save_array_words(\%use_type, \@use); |
| 210 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 211 | my $dbg_values = 0; |
| 212 | my $dbg_possible = 0; |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 213 | my $dbg_type = 0; |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 214 | my $dbg_attr = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 215 | for my $key (keys %debug) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 216 | ## no critic |
| 217 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 218 | die "$@" if ($@); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 221 | my $rpt_cleaners = 0; |
| 222 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 223 | if ($terse) { |
| 224 | $emacs = 1; |
| 225 | $quiet++; |
| 226 | } |
| 227 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 228 | if ($tree) { |
| 229 | if (defined $root) { |
| 230 | if (!top_of_kernel_tree($root)) { |
| 231 | die "$P: $root: --root does not point at a valid tree\n"; |
| 232 | } |
| 233 | } else { |
| 234 | if (top_of_kernel_tree('.')) { |
| 235 | $root = '.'; |
| 236 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 237 | top_of_kernel_tree($1)) { |
| 238 | $root = $1; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (!defined $root) { |
| 243 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 244 | exit(2); |
| 245 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 248 | my $emitted_corrupt = 0; |
| 249 | |
Andy Whitcroft | 2ceb532 | 2009-10-26 16:50:14 -0700 | [diff] [blame] | 250 | our $Ident = qr{ |
| 251 | [A-Za-z_][A-Za-z\d_]* |
| 252 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 253 | }x; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 254 | our $Storage = qr{extern|static|asmlinkage}; |
| 255 | our $Sparse = qr{ |
| 256 | __user| |
| 257 | __kernel| |
| 258 | __force| |
| 259 | __iomem| |
| 260 | __must_check| |
| 261 | __init_refok| |
Andy Whitcroft | 417495e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 262 | __kprobes| |
Sven Eckelmann | 165e72a | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 263 | __ref| |
| 264 | __rcu |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 265 | }x; |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 266 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; |
| 267 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; |
| 268 | our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; |
| 269 | our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; |
| 270 | our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 271 | |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 272 | # Notes to $Attribute: |
| 273 | # 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] | 274 | our $Attribute = qr{ |
| 275 | const| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 276 | __percpu| |
| 277 | __nocast| |
| 278 | __safe| |
| 279 | __bitwise__| |
| 280 | __packed__| |
| 281 | __packed2__| |
| 282 | __naked| |
| 283 | __maybe_unused| |
| 284 | __always_unused| |
| 285 | __noreturn| |
| 286 | __used| |
| 287 | __cold| |
Joe Perches | e23ef1f | 2015-02-13 14:38:24 -0800 | [diff] [blame] | 288 | __pure| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 289 | __noclone| |
| 290 | __deprecated| |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 291 | __read_mostly| |
| 292 | __kprobes| |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 293 | $InitAttribute| |
Andy Whitcroft | 24e1d81 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 294 | ____cacheline_aligned| |
| 295 | ____cacheline_aligned_in_smp| |
Andy Whitcroft | 5fe3af1 | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 296 | ____cacheline_internodealigned_in_smp| |
| 297 | __weak |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 298 | }x; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 299 | our $Modifier; |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 300 | our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 301 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 302 | our $Lval = qr{$Ident(?:$Member)*}; |
| 303 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 304 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 305 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 306 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 307 | our $Int = qr{[0-9]+$Int_type?}; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 308 | our $Octal = qr{0[0-7]+$Int_type?}; |
Joe Perches | c0a5c89 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 309 | our $String = qr{"[X\t]*"}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 310 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 311 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 312 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
Joe Perches | 74349bc | 2012-12-17 16:02:05 -0800 | [diff] [blame] | 313 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 314 | our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 315 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Joe Perches | 447432f | 2014-04-03 14:49:17 -0700 | [diff] [blame] | 316 | our $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 317 | our $Arithmetic = qr{\+|-|\*|\/|%}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 318 | our $Operators = qr{ |
| 319 | <=|>=|==|!=| |
| 320 | =>|->|<<|>>|<|>|!|~| |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 321 | &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 322 | }x; |
| 323 | |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 324 | our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; |
| 325 | |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 326 | our $BasicType; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 327 | our $NonptrType; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 328 | our $NonptrTypeMisordered; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 329 | our $NonptrTypeWithAttr; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 330 | our $Type; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 331 | our $TypeMisordered; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 332 | our $Declare; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 333 | our $DeclareMisordered; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 334 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 335 | our $NON_ASCII_UTF8 = qr{ |
| 336 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 337 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 338 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 339 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 340 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 341 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 342 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 343 | }x; |
| 344 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 345 | our $UTF8 = qr{ |
| 346 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 347 | | $NON_ASCII_UTF8 |
| 348 | }x; |
| 349 | |
Joe Perches | 021158b | 2015-02-13 14:38:43 -0800 | [diff] [blame] | 350 | our $typeOtherOSTypedefs = qr{(?x: |
| 351 | u_(?:char|short|int|long) | # bsd |
| 352 | u(?:nchar|short|int|long) # sysv |
| 353 | )}; |
| 354 | |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 355 | our $typeTypedefs = qr{(?x: |
Andy Whitcroft | fb9e909 | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 356 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 357 | atomic_t |
| 358 | )}; |
| 359 | |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 360 | our $logFunctions = qr{(?x: |
Joe Perches | 6e60c02 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 361 | printk(?:_ratelimited|_once|)| |
Jacob Keller | 7d0b659 | 2013-07-03 15:05:35 -0700 | [diff] [blame] | 362 | (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| |
Joe Perches | 6e60c02 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 363 | WARN(?:_RATELIMIT|_ONCE|)| |
Joe Perches | b053172 | 2011-05-24 17:13:40 -0700 | [diff] [blame] | 364 | panic| |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 365 | MODULE_[A-Z_]+| |
| 366 | seq_vprintf|seq_printf|seq_puts |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 367 | )}; |
| 368 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 369 | our $signature_tags = qr{(?xi: |
| 370 | Signed-off-by:| |
| 371 | Acked-by:| |
| 372 | Tested-by:| |
| 373 | Reviewed-by:| |
| 374 | Reported-by:| |
Mugunthan V N | 8543ae1 | 2013-04-29 16:18:17 -0700 | [diff] [blame] | 375 | Suggested-by:| |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 376 | To:| |
| 377 | Cc: |
| 378 | )}; |
| 379 | |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 380 | our @typeListMisordered = ( |
| 381 | qr{char\s+(?:un)?signed}, |
| 382 | qr{int\s+(?:(?:un)?signed\s+)?short\s}, |
| 383 | qr{int\s+short(?:\s+(?:un)?signed)}, |
| 384 | qr{short\s+int(?:\s+(?:un)?signed)}, |
| 385 | qr{(?:un)?signed\s+int\s+short}, |
| 386 | qr{short\s+(?:un)?signed}, |
| 387 | qr{long\s+int\s+(?:un)?signed}, |
| 388 | qr{int\s+long\s+(?:un)?signed}, |
| 389 | qr{long\s+(?:un)?signed\s+int}, |
| 390 | qr{int\s+(?:un)?signed\s+long}, |
| 391 | qr{int\s+(?:un)?signed}, |
| 392 | qr{int\s+long\s+long\s+(?:un)?signed}, |
| 393 | qr{long\s+long\s+int\s+(?:un)?signed}, |
| 394 | qr{long\s+long\s+(?:un)?signed\s+int}, |
| 395 | qr{long\s+long\s+(?:un)?signed}, |
| 396 | qr{long\s+(?:un)?signed}, |
| 397 | ); |
| 398 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 399 | our @typeList = ( |
| 400 | qr{void}, |
Joe Perches | 0c773d9 | 2014-08-06 16:11:20 -0700 | [diff] [blame] | 401 | qr{(?:(?:un)?signed\s+)?char}, |
| 402 | qr{(?:(?:un)?signed\s+)?short\s+int}, |
| 403 | qr{(?:(?:un)?signed\s+)?short}, |
| 404 | qr{(?:(?:un)?signed\s+)?int}, |
| 405 | qr{(?:(?:un)?signed\s+)?long\s+int}, |
| 406 | qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, |
| 407 | qr{(?:(?:un)?signed\s+)?long\s+long}, |
| 408 | qr{(?:(?:un)?signed\s+)?long}, |
| 409 | qr{(?:un)?signed}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 410 | qr{float}, |
| 411 | qr{double}, |
| 412 | qr{bool}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 413 | qr{struct\s+$Ident}, |
| 414 | qr{union\s+$Ident}, |
| 415 | qr{enum\s+$Ident}, |
| 416 | qr{${Ident}_t}, |
| 417 | qr{${Ident}_handler}, |
| 418 | qr{${Ident}_handler_fn}, |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 419 | @typeListMisordered, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 420 | ); |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 421 | our @typeListWithAttr = ( |
| 422 | @typeList, |
| 423 | qr{struct\s+$InitAttribute\s+$Ident}, |
| 424 | qr{union\s+$InitAttribute\s+$Ident}, |
| 425 | ); |
| 426 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 427 | our @modifierList = ( |
| 428 | qr{fastcall}, |
| 429 | ); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 430 | |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 431 | our @mode_permission_funcs = ( |
| 432 | ["module_param", 3], |
| 433 | ["module_param_(?:array|named|string)", 4], |
| 434 | ["module_param_array_named", 5], |
| 435 | ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], |
| 436 | ["proc_create(?:_data|)", 2], |
| 437 | ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2], |
| 438 | ); |
| 439 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 440 | #Create a search pattern for all these functions to speed up a loop below |
| 441 | our $mode_perms_search = ""; |
| 442 | foreach my $entry (@mode_permission_funcs) { |
| 443 | $mode_perms_search .= '|' if ($mode_perms_search ne ""); |
| 444 | $mode_perms_search .= $entry->[0]; |
| 445 | } |
| 446 | |
Joe Perches | b392c64 | 2015-04-16 12:44:16 -0700 | [diff] [blame] | 447 | our $mode_perms_world_writable = qr{ |
| 448 | S_IWUGO | |
| 449 | S_IWOTH | |
| 450 | S_IRWXUGO | |
| 451 | S_IALLUGO | |
| 452 | 0[0-7][0-7][2367] |
| 453 | }x; |
| 454 | |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 455 | our $allowed_asm_includes = qr{(?x: |
| 456 | irq| |
Sergey Ryazanov | cdcee68 | 2014-10-13 15:51:44 -0700 | [diff] [blame] | 457 | memory| |
| 458 | time| |
| 459 | reboot |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 460 | )}; |
| 461 | # memory.h: ARM has a custom one |
| 462 | |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 463 | # Load common spelling mistakes and build regular expression list. |
| 464 | my $misspellings; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 465 | my %spelling_fix; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 466 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 467 | if (open(my $spelling, '<', $spelling_file)) { |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 468 | while (<$spelling>) { |
| 469 | my $line = $_; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 470 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 471 | $line =~ s/\s*\n?$//g; |
| 472 | $line =~ s/^\s*//g; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 473 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 474 | next if ($line =~ m/^\s*#/); |
| 475 | next if ($line =~ m/^\s*$/); |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 476 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 477 | my ($suspect, $fix) = split(/\|\|/, $line); |
| 478 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 479 | $spelling_fix{$suspect} = $fix; |
| 480 | } |
| 481 | close($spelling); |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 482 | } else { |
| 483 | warn "No typos will be found - file '$spelling_file': $!\n"; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 484 | } |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 485 | |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 486 | if ($codespell) { |
| 487 | if (open(my $spelling, '<', $codespellfile)) { |
| 488 | while (<$spelling>) { |
| 489 | my $line = $_; |
| 490 | |
| 491 | $line =~ s/\s*\n?$//g; |
| 492 | $line =~ s/^\s*//g; |
| 493 | |
| 494 | next if ($line =~ m/^\s*#/); |
| 495 | next if ($line =~ m/^\s*$/); |
| 496 | next if ($line =~ m/, disabled/i); |
| 497 | |
| 498 | $line =~ s/,.*$//; |
| 499 | |
| 500 | my ($suspect, $fix) = split(/->/, $line); |
| 501 | |
| 502 | $spelling_fix{$suspect} = $fix; |
| 503 | } |
| 504 | close($spelling); |
| 505 | } else { |
| 506 | warn "No codespell typos will be found - file '$codespellfile': $!\n"; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; |
| 511 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 512 | sub build_types { |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 513 | my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; |
| 514 | my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 515 | my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 516 | my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 517 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 518 | $BasicType = qr{ |
| 519 | (?:$typeOtherOSTypedefs\b)| |
| 520 | (?:$typeTypedefs\b)| |
| 521 | (?:${all}\b) |
| 522 | }x; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 523 | $NonptrType = qr{ |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 524 | (?:$Modifier\s+|const\s+)* |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 525 | (?: |
Andy Whitcroft | 6b48db2 | 2012-01-10 15:10:13 -0800 | [diff] [blame] | 526 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Joe Perches | 021158b | 2015-02-13 14:38:43 -0800 | [diff] [blame] | 527 | (?:$typeOtherOSTypedefs\b)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 528 | (?:$typeTypedefs\b)| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 529 | (?:${all}\b) |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 530 | ) |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 531 | (?:\s+$Modifier|\s+const)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 532 | }x; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 533 | $NonptrTypeMisordered = qr{ |
| 534 | (?:$Modifier\s+|const\s+)* |
| 535 | (?: |
| 536 | (?:${Misordered}\b) |
| 537 | ) |
| 538 | (?:\s+$Modifier|\s+const)* |
| 539 | }x; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 540 | $NonptrTypeWithAttr = qr{ |
| 541 | (?:$Modifier\s+|const\s+)* |
| 542 | (?: |
| 543 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 544 | (?:$typeTypedefs\b)| |
Joe Perches | 021158b | 2015-02-13 14:38:43 -0800 | [diff] [blame] | 545 | (?:$typeOtherOSTypedefs\b)| |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 546 | (?:${allWithAttr}\b) |
| 547 | ) |
| 548 | (?:\s+$Modifier|\s+const)* |
| 549 | }x; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 550 | $Type = qr{ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 551 | $NonptrType |
Joe Perches | 1574a29 | 2014-08-06 16:10:50 -0700 | [diff] [blame] | 552 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 553 | (?:\s+$Inline|\s+$Modifier)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 554 | }x; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 555 | $TypeMisordered = qr{ |
| 556 | $NonptrTypeMisordered |
| 557 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? |
| 558 | (?:\s+$Inline|\s+$Modifier)* |
| 559 | }x; |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 560 | $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 561 | $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 562 | } |
| 563 | build_types(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 564 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 565 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 566 | |
| 567 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 568 | # requires at least perl version v5.10.0 |
| 569 | # Any use must be runtime checked with $^V |
| 570 | |
| 571 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 572 | our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; |
Joe Perches | c0a5c89 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 573 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 574 | |
Joe Perches | f842230 | 2014-08-06 16:11:31 -0700 | [diff] [blame] | 575 | our $declaration_macros = qr{(?x: |
| 576 | (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(| |
| 577 | (?:$Storage\s+)?LIST_HEAD\s*\(| |
| 578 | (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\( |
| 579 | )}; |
| 580 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 581 | sub deparenthesize { |
| 582 | my ($string) = @_; |
| 583 | return "" if (!defined($string)); |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 584 | |
| 585 | while ($string =~ /^\s*\(.*\)\s*$/) { |
| 586 | $string =~ s@^\s*\(\s*@@; |
| 587 | $string =~ s@\s*\)\s*$@@; |
| 588 | } |
| 589 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 590 | $string =~ s@\s+@ @g; |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 591 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 592 | return $string; |
| 593 | } |
| 594 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 595 | sub seed_camelcase_file { |
| 596 | my ($file) = @_; |
| 597 | |
| 598 | return if (!(-f $file)); |
| 599 | |
| 600 | local $/; |
| 601 | |
| 602 | open(my $include_file, '<', "$file") |
| 603 | or warn "$P: Can't read '$file' $!\n"; |
| 604 | my $text = <$include_file>; |
| 605 | close($include_file); |
| 606 | |
| 607 | my @lines = split('\n', $text); |
| 608 | |
| 609 | foreach my $line (@lines) { |
| 610 | next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); |
| 611 | if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { |
| 612 | $camelcase{$1} = 1; |
Joe Perches | 11ea516 | 2013-11-12 15:10:08 -0800 | [diff] [blame] | 613 | } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { |
| 614 | $camelcase{$1} = 1; |
| 615 | } 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] | 616 | $camelcase{$1} = 1; |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | my $camelcase_seeded = 0; |
| 622 | sub seed_camelcase_includes { |
| 623 | return if ($camelcase_seeded); |
| 624 | |
| 625 | my $files; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 626 | my $camelcase_cache = ""; |
| 627 | my @include_files = (); |
| 628 | |
| 629 | $camelcase_seeded = 1; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 630 | |
Richard Genoud | 3645e32 | 2014-02-10 14:25:32 -0800 | [diff] [blame] | 631 | if (-e ".git") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 632 | my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; |
| 633 | chomp $git_last_include_commit; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 634 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 635 | } else { |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 636 | my $last_mod_date = 0; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 637 | $files = `find $root/include -name "*.h"`; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 638 | @include_files = split('\n', $files); |
| 639 | foreach my $file (@include_files) { |
| 640 | my $date = POSIX::strftime("%Y%m%d%H%M", |
| 641 | localtime((stat $file)[9])); |
| 642 | $last_mod_date = $date if ($last_mod_date < $date); |
| 643 | } |
| 644 | $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 645 | } |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 646 | |
| 647 | if ($camelcase_cache ne "" && -f $camelcase_cache) { |
| 648 | open(my $camelcase_file, '<', "$camelcase_cache") |
| 649 | or warn "$P: Can't read '$camelcase_cache' $!\n"; |
| 650 | while (<$camelcase_file>) { |
| 651 | chomp; |
| 652 | $camelcase{$_} = 1; |
| 653 | } |
| 654 | close($camelcase_file); |
| 655 | |
| 656 | return; |
| 657 | } |
| 658 | |
Richard Genoud | 3645e32 | 2014-02-10 14:25:32 -0800 | [diff] [blame] | 659 | if (-e ".git") { |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 660 | $files = `git ls-files "include/*.h"`; |
| 661 | @include_files = split('\n', $files); |
| 662 | } |
| 663 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 664 | foreach my $file (@include_files) { |
| 665 | seed_camelcase_file($file); |
| 666 | } |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 667 | |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 668 | if ($camelcase_cache ne "") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 669 | unlink glob ".checkpatch-camelcase.*"; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 670 | open(my $camelcase_file, '>', "$camelcase_cache") |
| 671 | or warn "$P: Can't write '$camelcase_cache' $!\n"; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 672 | foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { |
| 673 | print $camelcase_file ("$_\n"); |
| 674 | } |
| 675 | close($camelcase_file); |
| 676 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 679 | sub git_commit_info { |
| 680 | my ($commit, $id, $desc) = @_; |
| 681 | |
| 682 | return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); |
| 683 | |
| 684 | my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; |
| 685 | $output =~ s/^\s*//gm; |
| 686 | my @lines = split("\n", $output); |
| 687 | |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 688 | return ($id, $desc) if ($#lines < 0); |
| 689 | |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 690 | if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { |
| 691 | # Maybe one day convert this block of bash into something that returns |
| 692 | # all matching commit ids, but it's very slow... |
| 693 | # |
| 694 | # echo "checking commits $1..." |
| 695 | # git rev-list --remotes | grep -i "^$1" | |
| 696 | # while read line ; do |
| 697 | # git log --format='%H %s' -1 $line | |
| 698 | # echo "commit $(cut -c 1-12,41-)" |
| 699 | # done |
| 700 | } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { |
| 701 | } else { |
| 702 | $id = substr($lines[0], 0, 12); |
| 703 | $desc = substr($lines[0], 41); |
| 704 | } |
| 705 | |
| 706 | return ($id, $desc); |
| 707 | } |
| 708 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 709 | $chk_signoff = 0 if ($file); |
| 710 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 711 | my @rawlines = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 712 | my @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 713 | my @fixed = (); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 714 | my @fixed_inserted = (); |
| 715 | my @fixed_deleted = (); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 716 | my $fixlinenr = -1; |
| 717 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 718 | my $vname; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 719 | for my $filename (@ARGV) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 720 | my $FILE; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 721 | if ($file) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 722 | open($FILE, '-|', "diff -u /dev/null $filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 723 | die "$P: $filename: diff failed - $!\n"; |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 724 | } elsif ($filename eq '-') { |
| 725 | open($FILE, '<&STDIN'); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 726 | } else { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 727 | open($FILE, '<', "$filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 728 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 729 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 730 | if ($filename eq '-') { |
| 731 | $vname = 'Your patch'; |
| 732 | } else { |
| 733 | $vname = $filename; |
| 734 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 735 | while (<$FILE>) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 736 | chomp; |
| 737 | push(@rawlines, $_); |
| 738 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 739 | close($FILE); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 740 | if (!process($filename)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 741 | $exit = 1; |
| 742 | } |
| 743 | @rawlines = (); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 744 | @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 745 | @fixed = (); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 746 | @fixed_inserted = (); |
| 747 | @fixed_deleted = (); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 748 | $fixlinenr = -1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | exit($exit); |
| 752 | |
| 753 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 754 | my ($root) = @_; |
| 755 | |
| 756 | my @tree_check = ( |
| 757 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 758 | "README", "Documentation", "arch", "include", "drivers", |
| 759 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 760 | ); |
| 761 | |
| 762 | foreach my $check (@tree_check) { |
| 763 | if (! -e $root . '/' . $check) { |
| 764 | return 0; |
| 765 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 766 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 767 | return 1; |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 768 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 769 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 770 | sub parse_email { |
| 771 | my ($formatted_email) = @_; |
| 772 | |
| 773 | my $name = ""; |
| 774 | my $address = ""; |
| 775 | my $comment = ""; |
| 776 | |
| 777 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 778 | $name = $1; |
| 779 | $address = $2; |
| 780 | $comment = $3 if defined $3; |
| 781 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 782 | $address = $1; |
| 783 | $comment = $2 if defined $2; |
| 784 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 785 | $address = $1; |
| 786 | $comment = $2 if defined $2; |
| 787 | $formatted_email =~ s/$address.*$//; |
| 788 | $name = $formatted_email; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 789 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 790 | $name =~ s/^\"|\"$//g; |
| 791 | # If there's a name left after stripping spaces and |
| 792 | # leading quotes, and the address doesn't have both |
| 793 | # leading and trailing angle brackets, the address |
| 794 | # is invalid. ie: |
| 795 | # "joe smith joe@smith.com" bad |
| 796 | # "joe smith <joe@smith.com" bad |
| 797 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 798 | $name = ""; |
| 799 | $address = ""; |
| 800 | $comment = ""; |
| 801 | } |
| 802 | } |
| 803 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 804 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 805 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 806 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 807 | $address =~ s/^\<|\>$//g; |
| 808 | |
| 809 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 810 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 811 | $name = "\"$name\""; |
| 812 | } |
| 813 | |
| 814 | return ($name, $address, $comment); |
| 815 | } |
| 816 | |
| 817 | sub format_email { |
| 818 | my ($name, $address) = @_; |
| 819 | |
| 820 | my $formatted_email; |
| 821 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 822 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 823 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 824 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 825 | |
| 826 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 827 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 828 | $name = "\"$name\""; |
| 829 | } |
| 830 | |
| 831 | if ("$name" eq "") { |
| 832 | $formatted_email = "$address"; |
| 833 | } else { |
| 834 | $formatted_email = "$name <$address>"; |
| 835 | } |
| 836 | |
| 837 | return $formatted_email; |
| 838 | } |
| 839 | |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 840 | sub which { |
Joe Perches | bd474ca | 2014-08-06 16:11:10 -0700 | [diff] [blame] | 841 | my ($bin) = @_; |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 842 | |
Joe Perches | bd474ca | 2014-08-06 16:11:10 -0700 | [diff] [blame] | 843 | foreach my $path (split(/:/, $ENV{PATH})) { |
| 844 | if (-e "$path/$bin") { |
| 845 | return "$path/$bin"; |
| 846 | } |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 847 | } |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 848 | |
Joe Perches | bd474ca | 2014-08-06 16:11:10 -0700 | [diff] [blame] | 849 | return ""; |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 852 | sub which_conf { |
| 853 | my ($conf) = @_; |
| 854 | |
| 855 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 856 | if (-e "$path/$conf") { |
| 857 | return "$path/$conf"; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | return ""; |
| 862 | } |
| 863 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 864 | sub expand_tabs { |
| 865 | my ($str) = @_; |
| 866 | |
| 867 | my $res = ''; |
| 868 | my $n = 0; |
| 869 | for my $c (split(//, $str)) { |
| 870 | if ($c eq "\t") { |
| 871 | $res .= ' '; |
| 872 | $n++; |
| 873 | for (; ($n % 8) != 0; $n++) { |
| 874 | $res .= ' '; |
| 875 | } |
| 876 | next; |
| 877 | } |
| 878 | $res .= $c; |
| 879 | $n++; |
| 880 | } |
| 881 | |
| 882 | return $res; |
| 883 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 884 | sub copy_spacing { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 885 | (my $res = shift) =~ tr/\t/ /c; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 886 | return $res; |
| 887 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 888 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 889 | sub line_stats { |
| 890 | my ($line) = @_; |
| 891 | |
| 892 | # Drop the diff line leader and expand tabs |
| 893 | $line =~ s/^.//; |
| 894 | $line = expand_tabs($line); |
| 895 | |
| 896 | # Pick the indent from the front of the line. |
| 897 | my ($white) = ($line =~ /^(\s*)/); |
| 898 | |
| 899 | return (length($line), length($white)); |
| 900 | } |
| 901 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 902 | my $sanitise_quote = ''; |
| 903 | |
| 904 | sub sanitise_line_reset { |
| 905 | my ($in_comment) = @_; |
| 906 | |
| 907 | if ($in_comment) { |
| 908 | $sanitise_quote = '*/'; |
| 909 | } else { |
| 910 | $sanitise_quote = ''; |
| 911 | } |
| 912 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 913 | sub sanitise_line { |
| 914 | my ($line) = @_; |
| 915 | |
| 916 | my $res = ''; |
| 917 | my $l = ''; |
| 918 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 919 | my $qlen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 920 | my $off = 0; |
| 921 | my $c; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 922 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 923 | # Always copy over the diff marker. |
| 924 | $res = substr($line, 0, 1); |
| 925 | |
| 926 | for ($off = 1; $off < length($line); $off++) { |
| 927 | $c = substr($line, $off, 1); |
| 928 | |
| 929 | # Comments we are wacking completly including the begin |
| 930 | # and end, all to $;. |
| 931 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 932 | $sanitise_quote = '*/'; |
| 933 | |
| 934 | substr($res, $off, 2, "$;$;"); |
| 935 | $off++; |
| 936 | next; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 937 | } |
Andy Whitcroft | 81bc0e0 | 2008-10-15 22:02:26 -0700 | [diff] [blame] | 938 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 939 | $sanitise_quote = ''; |
| 940 | substr($res, $off, 2, "$;$;"); |
| 941 | $off++; |
| 942 | next; |
| 943 | } |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 944 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 945 | $sanitise_quote = '//'; |
| 946 | |
| 947 | substr($res, $off, 2, $sanitise_quote); |
| 948 | $off++; |
| 949 | next; |
| 950 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 951 | |
| 952 | # A \ in a string means ignore the next character. |
| 953 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 954 | $c eq "\\") { |
| 955 | substr($res, $off, 2, 'XX'); |
| 956 | $off++; |
| 957 | next; |
| 958 | } |
| 959 | # Regular quotes. |
| 960 | if ($c eq "'" || $c eq '"') { |
| 961 | if ($sanitise_quote eq '') { |
| 962 | $sanitise_quote = $c; |
| 963 | |
| 964 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 965 | next; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 966 | } elsif ($sanitise_quote eq $c) { |
| 967 | $sanitise_quote = ''; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 968 | } |
| 969 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 970 | |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 971 | #print "c<$c> SQ<$sanitise_quote>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 972 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 973 | substr($res, $off, 1, $;); |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 974 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 975 | substr($res, $off, 1, $;); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 976 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 977 | substr($res, $off, 1, 'X'); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 978 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 979 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 980 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 983 | if ($sanitise_quote eq '//') { |
| 984 | $sanitise_quote = ''; |
| 985 | } |
| 986 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 987 | # The pathname on a #include may be surrounded by '<' and '>'. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 988 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 989 | my $clean = 'X' x length($1); |
| 990 | $res =~ s@\<.*\>@<$clean>@; |
| 991 | |
| 992 | # The whole of a #error is a string. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 993 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 994 | my $clean = 'X' x length($1); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 995 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 996 | } |
| 997 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 998 | return $res; |
| 999 | } |
| 1000 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 1001 | sub get_quoted_string { |
| 1002 | my ($line, $rawline) = @_; |
| 1003 | |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 1004 | return "" if ($line !~ m/(\"[X\t]+\")/g); |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 1005 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 1006 | } |
| 1007 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1008 | sub ctx_statement_block { |
| 1009 | my ($linenr, $remain, $off) = @_; |
| 1010 | my $line = $linenr - 1; |
| 1011 | my $blk = ''; |
| 1012 | my $soff = $off; |
| 1013 | my $coff = $off - 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1014 | my $coff_set = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1015 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1016 | my $loff = 0; |
| 1017 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1018 | my $type = ''; |
| 1019 | my $level = 0; |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 1020 | my @stack = (); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1021 | my $p; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1022 | my $c; |
| 1023 | my $len = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1024 | |
| 1025 | my $remainder; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1026 | while (1) { |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 1027 | @stack = (['', 0]) if ($#stack == -1); |
| 1028 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1029 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1030 | # If we are about to drop off the end, pull in more |
| 1031 | # context. |
| 1032 | if ($off >= $len) { |
| 1033 | for (; $remain > 0; $line++) { |
Andy Whitcroft | dea3349 | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 1034 | last if (!defined $lines[$line]); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1035 | next if ($lines[$line] =~ /^-/); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1036 | $remain--; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1037 | $loff = $len; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1038 | $blk .= $lines[$line] . "\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1039 | $len = length($blk); |
| 1040 | $line++; |
| 1041 | last; |
| 1042 | } |
| 1043 | # Bail if there is no further context. |
| 1044 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1045 | if ($off >= $len) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1046 | last; |
| 1047 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 1048 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 1049 | $level++; |
| 1050 | $type = '#'; |
| 1051 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1052 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1053 | $p = $c; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1054 | $c = substr($blk, $off, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1055 | $remainder = substr($blk, $off); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1056 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1057 | #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] | 1058 | |
| 1059 | # Handle nested #if/#else. |
| 1060 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 1061 | push(@stack, [ $type, $level ]); |
| 1062 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 1063 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 1064 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 1065 | ($type, $level) = @{pop(@stack)}; |
| 1066 | } |
| 1067 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1068 | # Statement ends at the ';' or a close '}' at the |
| 1069 | # outermost level. |
| 1070 | if ($level == 0 && $c eq ';') { |
| 1071 | last; |
| 1072 | } |
| 1073 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1074 | # 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] | 1075 | if ($level == 0 && $coff_set == 0 && |
| 1076 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 1077 | $remainder =~ /^(else)(?:\s|{)/ && |
| 1078 | $remainder !~ /^else\s+if\b/) { |
| 1079 | $coff = $off + length($1) - 1; |
| 1080 | $coff_set = 1; |
| 1081 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 1082 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1085 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 1086 | $level++; |
| 1087 | $type = '('; |
| 1088 | } |
| 1089 | if ($type eq '(' && $c eq ')') { |
| 1090 | $level--; |
| 1091 | $type = ($level != 0)? '(' : ''; |
| 1092 | |
| 1093 | if ($level == 0 && $coff < $soff) { |
| 1094 | $coff = $off; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1095 | $coff_set = 1; |
| 1096 | #warn "CSB: mark coff<$coff>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 1100 | $level++; |
| 1101 | $type = '{'; |
| 1102 | } |
| 1103 | if ($type eq '{' && $c eq '}') { |
| 1104 | $level--; |
| 1105 | $type = ($level != 0)? '{' : ''; |
| 1106 | |
| 1107 | if ($level == 0) { |
Patrick Pannuto | b998e00 | 2010-08-09 17:21:03 -0700 | [diff] [blame] | 1108 | if (substr($blk, $off + 1, 1) eq ';') { |
| 1109 | $off++; |
| 1110 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1111 | last; |
| 1112 | } |
| 1113 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 1114 | # Preprocessor commands end at the newline unless escaped. |
| 1115 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 1116 | $level--; |
| 1117 | $type = ''; |
| 1118 | $off++; |
| 1119 | last; |
| 1120 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1121 | $off++; |
| 1122 | } |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 1123 | # We are truly at the end, so shuffle to the next line. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1124 | if ($off == $len) { |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 1125 | $loff = $len + 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1126 | $line++; |
| 1127 | $remain--; |
| 1128 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1129 | |
| 1130 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 1131 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 1132 | |
| 1133 | #warn "STATEMENT<$statement>\n"; |
| 1134 | #warn "CONDITION<$condition>\n"; |
| 1135 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1136 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1137 | |
| 1138 | return ($statement, $condition, |
| 1139 | $line, $remain + 1, $off - $loff + 1, $level); |
| 1140 | } |
| 1141 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1142 | sub statement_lines { |
| 1143 | my ($stmt) = @_; |
| 1144 | |
| 1145 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 1146 | $stmt =~ s/(^|\n)./$1/g; |
| 1147 | $stmt =~ s/^\s*//; |
| 1148 | $stmt =~ s/\s*$//; |
| 1149 | |
| 1150 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1151 | |
| 1152 | return $#stmt_lines + 2; |
| 1153 | } |
| 1154 | |
| 1155 | sub statement_rawlines { |
| 1156 | my ($stmt) = @_; |
| 1157 | |
| 1158 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1159 | |
| 1160 | return $#stmt_lines + 2; |
| 1161 | } |
| 1162 | |
| 1163 | sub statement_block_size { |
| 1164 | my ($stmt) = @_; |
| 1165 | |
| 1166 | $stmt =~ s/(^|\n)./$1/g; |
| 1167 | $stmt =~ s/^\s*{//; |
| 1168 | $stmt =~ s/}\s*$//; |
| 1169 | $stmt =~ s/^\s*//; |
| 1170 | $stmt =~ s/\s*$//; |
| 1171 | |
| 1172 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1173 | my @stmt_statements = ($stmt =~ /;/g); |
| 1174 | |
| 1175 | my $stmt_lines = $#stmt_lines + 2; |
| 1176 | my $stmt_statements = $#stmt_statements + 1; |
| 1177 | |
| 1178 | if ($stmt_lines > $stmt_statements) { |
| 1179 | return $stmt_lines; |
| 1180 | } else { |
| 1181 | return $stmt_statements; |
| 1182 | } |
| 1183 | } |
| 1184 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1185 | sub ctx_statement_full { |
| 1186 | my ($linenr, $remain, $off) = @_; |
| 1187 | my ($statement, $condition, $level); |
| 1188 | |
| 1189 | my (@chunks); |
| 1190 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1191 | # Grab the first conditional/block pair. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1192 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1193 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1194 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1195 | push(@chunks, [ $condition, $statement ]); |
| 1196 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 1197 | return ($level, $linenr, @chunks); |
| 1198 | } |
| 1199 | |
| 1200 | # Pull in the following conditional/block pairs and see if they |
| 1201 | # could continue the statement. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1202 | for (;;) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1203 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1204 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1205 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1206 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1207 | #print "C: push\n"; |
| 1208 | push(@chunks, [ $condition, $statement ]); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | return ($level, $linenr, @chunks); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1214 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1215 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1216 | my $line; |
| 1217 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1218 | my $blk = ''; |
| 1219 | my @o; |
| 1220 | my @c; |
| 1221 | my @res = (); |
| 1222 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1223 | my $level = 0; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1224 | my @stack = ($level); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1225 | for ($line = $start; $remain > 0; $line++) { |
| 1226 | next if ($rawlines[$line] =~ /^-/); |
| 1227 | $remain--; |
| 1228 | |
| 1229 | $blk .= $rawlines[$line]; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1230 | |
| 1231 | # Handle nested #if/#else. |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1232 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1233 | push(@stack, $level); |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1234 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1235 | $level = $stack[$#stack - 1]; |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1236 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1237 | $level = pop(@stack); |
| 1238 | } |
| 1239 | |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1240 | foreach my $c (split(//, $lines[$line])) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1241 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 1242 | if ($off > 0) { |
| 1243 | $off--; |
| 1244 | next; |
| 1245 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1246 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1247 | if ($c eq $close && $level > 0) { |
| 1248 | $level--; |
| 1249 | last if ($level == 0); |
| 1250 | } elsif ($c eq $open) { |
| 1251 | $level++; |
| 1252 | } |
| 1253 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1254 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1255 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1256 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1259 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1262 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1263 | } |
| 1264 | sub ctx_block_outer { |
| 1265 | my ($linenr, $remain) = @_; |
| 1266 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1267 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 1268 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1269 | } |
| 1270 | sub ctx_block { |
| 1271 | my ($linenr, $remain) = @_; |
| 1272 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1273 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1274 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1275 | } |
| 1276 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1277 | my ($linenr, $remain, $off) = @_; |
| 1278 | |
| 1279 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1280 | return @r; |
| 1281 | } |
| 1282 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1283 | my ($linenr, $remain) = @_; |
| 1284 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1285 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1286 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1287 | sub ctx_statement_level { |
| 1288 | my ($linenr, $remain, $off) = @_; |
| 1289 | |
| 1290 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1291 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1292 | |
| 1293 | sub ctx_locate_comment { |
| 1294 | my ($first_line, $end_line) = @_; |
| 1295 | |
| 1296 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | beae633 | 2008-07-23 21:28:59 -0700 | [diff] [blame] | 1297 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1298 | return $current_comment if (defined $current_comment); |
| 1299 | |
| 1300 | # Look through the context and try and figure out if there is a |
| 1301 | # comment. |
| 1302 | my $in_comment = 0; |
| 1303 | $current_comment = ''; |
| 1304 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1305 | my $line = $rawlines[$linenr - 1]; |
| 1306 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1307 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 1308 | $in_comment = 1; |
| 1309 | } |
| 1310 | if ($line =~ m@/\*@) { |
| 1311 | $in_comment = 1; |
| 1312 | } |
| 1313 | if (!$in_comment && $current_comment ne '') { |
| 1314 | $current_comment = ''; |
| 1315 | } |
| 1316 | $current_comment .= $line . "\n" if ($in_comment); |
| 1317 | if ($line =~ m@\*/@) { |
| 1318 | $in_comment = 0; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | chomp($current_comment); |
| 1323 | return($current_comment); |
| 1324 | } |
| 1325 | sub ctx_has_comment { |
| 1326 | my ($first_line, $end_line) = @_; |
| 1327 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 1328 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1329 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1330 | ##print "CMMT: $cmt\n"; |
| 1331 | |
| 1332 | return ($cmt ne ''); |
| 1333 | } |
| 1334 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1335 | sub raw_line { |
| 1336 | my ($linenr, $cnt) = @_; |
| 1337 | |
| 1338 | my $offset = $linenr - 1; |
| 1339 | $cnt++; |
| 1340 | |
| 1341 | my $line; |
| 1342 | while ($cnt) { |
| 1343 | $line = $rawlines[$offset++]; |
| 1344 | next if (defined($line) && $line =~ /^-/); |
| 1345 | $cnt--; |
| 1346 | } |
| 1347 | |
| 1348 | return $line; |
| 1349 | } |
| 1350 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1351 | sub cat_vet { |
| 1352 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1353 | my ($res, $coded); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1354 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1355 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1356 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 1357 | $res .= $1; |
| 1358 | if ($2 ne '') { |
| 1359 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 1360 | $res .= $coded; |
| 1361 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1362 | } |
| 1363 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1364 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1365 | return $res; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1368 | my $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1369 | my $av_pending; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1370 | my @av_paren_type; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1371 | my $av_pend_colon; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1372 | |
| 1373 | sub annotate_reset { |
| 1374 | $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1375 | $av_pending = '_'; |
| 1376 | @av_paren_type = ('E'); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1377 | $av_pend_colon = 'O'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1380 | sub annotate_values { |
| 1381 | my ($stream, $type) = @_; |
| 1382 | |
| 1383 | my $res; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1384 | my $var = '_' x length($stream); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1385 | my $cur = $stream; |
| 1386 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1387 | print "$stream\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1388 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1389 | while (length($cur)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1390 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1391 | print " <" . join('', @av_paren_type) . |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1392 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1393 | if ($cur =~ /^(\s+)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1394 | print "WS($1)\n" if ($dbg_values > 1); |
| 1395 | if ($1 =~ /\n/ && $av_preprocessor) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1396 | $type = pop(@av_paren_type); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1397 | $av_preprocessor = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Florian Mickler | c023e473 | 2011-01-12 16:59:58 -0800 | [diff] [blame] | 1400 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1401 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1402 | push(@av_paren_type, $type); |
Andy Whitcroft | addcdce | 2012-01-10 15:10:11 -0800 | [diff] [blame] | 1403 | $type = 'c'; |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1404 | |
Andy Whitcroft | e91b6e2 | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1405 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1406 | print "DECLARE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1407 | $type = 'T'; |
| 1408 | |
Andy Whitcroft | 389a2fe | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 1409 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 1410 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 1411 | $type = 'T'; |
| 1412 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1413 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1414 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1415 | $av_preprocessor = 1; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1416 | push(@av_paren_type, $type); |
| 1417 | if ($2 ne '') { |
| 1418 | $av_pending = 'N'; |
| 1419 | } |
| 1420 | $type = 'E'; |
| 1421 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1422 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1423 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 1424 | $av_preprocessor = 1; |
| 1425 | push(@av_paren_type, $type); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1426 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1427 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1428 | print "PRE_START($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1429 | $av_preprocessor = 1; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1430 | |
| 1431 | push(@av_paren_type, $type); |
| 1432 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1433 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1434 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1435 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1436 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 1437 | $av_preprocessor = 1; |
| 1438 | |
| 1439 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 1440 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1441 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1442 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1443 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1444 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 1445 | |
| 1446 | $av_preprocessor = 1; |
| 1447 | |
| 1448 | # Assume all arms of the conditional end as this |
| 1449 | # one does, and continue as if the #endif was not here. |
| 1450 | pop(@av_paren_type); |
| 1451 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1452 | $type = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1453 | |
| 1454 | } elsif ($cur =~ /^(\\\n)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1455 | print "PRECONT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1456 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1457 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 1458 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 1459 | $av_pending = $type; |
| 1460 | $type = 'N'; |
| 1461 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1462 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1463 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1464 | if (defined $2) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1465 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1466 | } |
| 1467 | $type = 'N'; |
| 1468 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1469 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1470 | print "COND($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1471 | $av_pending = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1472 | $type = 'N'; |
| 1473 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1474 | } elsif ($cur =~/^(case)/o) { |
| 1475 | print "CASE($1)\n" if ($dbg_values > 1); |
| 1476 | $av_pend_colon = 'C'; |
| 1477 | $type = 'N'; |
| 1478 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1479 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1480 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1481 | $type = 'N'; |
| 1482 | |
| 1483 | } elsif ($cur =~ /^(\()/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1484 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1485 | push(@av_paren_type, $av_pending); |
| 1486 | $av_pending = '_'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1487 | $type = 'N'; |
| 1488 | |
| 1489 | } elsif ($cur =~ /^(\))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1490 | my $new_type = pop(@av_paren_type); |
| 1491 | if ($new_type ne '_') { |
| 1492 | $type = $new_type; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1493 | print "PAREN('$1') -> $type\n" |
| 1494 | if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1495 | } else { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1496 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1499 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1500 | print "FUNC($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1501 | $type = 'V'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1502 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1503 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1504 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 1505 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1506 | $av_pend_colon = 'B'; |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1507 | } elsif ($type eq 'E') { |
| 1508 | $av_pend_colon = 'L'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1509 | } |
| 1510 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 1511 | $type = 'V'; |
| 1512 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1513 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1514 | print "IDENT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1515 | $type = 'V'; |
| 1516 | |
| 1517 | } elsif ($cur =~ /^($Assignment)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1518 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1519 | $type = 'N'; |
| 1520 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1521 | } elsif ($cur =~/^(;|{|})/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1522 | print "END($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1523 | $type = 'E'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1524 | $av_pend_colon = 'O'; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1525 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1526 | } elsif ($cur =~/^(,)/) { |
| 1527 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 1528 | $type = 'C'; |
| 1529 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1530 | } elsif ($cur =~ /^(\?)/o) { |
| 1531 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 1532 | $type = 'N'; |
| 1533 | |
| 1534 | } elsif ($cur =~ /^(:)/o) { |
| 1535 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 1536 | |
| 1537 | substr($var, length($res), 1, $av_pend_colon); |
| 1538 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 1539 | $type = 'E'; |
| 1540 | } else { |
| 1541 | $type = 'N'; |
| 1542 | } |
| 1543 | $av_pend_colon = 'O'; |
| 1544 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1545 | } elsif ($cur =~ /^(\[)/o) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1546 | print "CLOSE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1547 | $type = 'N'; |
| 1548 | |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1549 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1550 | my $variant; |
| 1551 | |
| 1552 | print "OPV($1)\n" if ($dbg_values > 1); |
| 1553 | if ($type eq 'V') { |
| 1554 | $variant = 'B'; |
| 1555 | } else { |
| 1556 | $variant = 'U'; |
| 1557 | } |
| 1558 | |
| 1559 | substr($var, length($res), 1, $variant); |
| 1560 | $type = 'N'; |
| 1561 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1562 | } elsif ($cur =~ /^($Operators)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1563 | print "OP($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1564 | if ($1 ne '++' && $1 ne '--') { |
| 1565 | $type = 'N'; |
| 1566 | } |
| 1567 | |
| 1568 | } elsif ($cur =~ /(^.)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1569 | print "C($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1570 | } |
| 1571 | if (defined $1) { |
| 1572 | $cur = substr($cur, length($1)); |
| 1573 | $res .= $type x length($1); |
| 1574 | } |
| 1575 | } |
| 1576 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1577 | return ($res, $var); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1580 | sub possible { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1581 | my ($possible, $line) = @_; |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1582 | my $notPermitted = qr{(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1583 | ^(?: |
| 1584 | $Modifier| |
| 1585 | $Storage| |
| 1586 | $Type| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1587 | DEFINE_\S+ |
| 1588 | )$| |
| 1589 | ^(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1590 | goto| |
| 1591 | return| |
| 1592 | case| |
| 1593 | else| |
| 1594 | asm|__asm__| |
Andy Whitcroft | 89a8835 | 2012-01-10 15:10:00 -0800 | [diff] [blame] | 1595 | do| |
| 1596 | \#| |
| 1597 | \#\#| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1598 | )(?:\s|$)| |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1599 | ^(?:typedef|struct|enum)\b |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1600 | )}x; |
| 1601 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 1602 | if ($possible !~ $notPermitted) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1603 | # Check for modifiers. |
| 1604 | $possible =~ s/\s*$Storage\s*//g; |
| 1605 | $possible =~ s/\s*$Sparse\s*//g; |
| 1606 | if ($possible =~ /^\s*$/) { |
| 1607 | |
| 1608 | } elsif ($possible =~ /\s/) { |
| 1609 | $possible =~ s/\s*$Type\s*//g; |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1610 | for my $modifier (split(' ', $possible)) { |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1611 | if ($modifier !~ $notPermitted) { |
| 1612 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
| 1613 | push(@modifierList, $modifier); |
| 1614 | } |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1615 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1616 | |
| 1617 | } else { |
| 1618 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
| 1619 | push(@typeList, $possible); |
| 1620 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1621 | build_types(); |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1622 | } else { |
| 1623 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1627 | my $prefix = ''; |
| 1628 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1629 | sub show_type { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1630 | my ($type) = @_; |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 1631 | |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1632 | return defined $use_type{$type} if (scalar keys %use_type > 0); |
| 1633 | |
| 1634 | return !defined $ignore_type{$type}; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1637 | sub report { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1638 | my ($level, $type, $msg) = @_; |
| 1639 | |
| 1640 | if (!show_type($type) || |
| 1641 | (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1642 | return 0; |
| 1643 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1644 | my $line; |
| 1645 | if ($show_types) { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1646 | $line = "$prefix$level:$type: $msg\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1647 | } else { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1648 | $line = "$prefix$level: $msg\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1649 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1650 | $line = (split('\n', $line))[0] . "\n" if ($terse); |
| 1651 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1652 | push(our @report, $line); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1653 | |
| 1654 | return 1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1655 | } |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1656 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1657 | sub report_dump { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1658 | our @report; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1659 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1660 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 1661 | sub fixup_current_range { |
| 1662 | my ($lineRef, $offset, $length) = @_; |
| 1663 | |
| 1664 | if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { |
| 1665 | my $o = $1; |
| 1666 | my $l = $2; |
| 1667 | my $no = $o + $offset; |
| 1668 | my $nl = $l + $length; |
| 1669 | $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | sub fix_inserted_deleted_lines { |
| 1674 | my ($linesRef, $insertedRef, $deletedRef) = @_; |
| 1675 | |
| 1676 | my $range_last_linenr = 0; |
| 1677 | my $delta_offset = 0; |
| 1678 | |
| 1679 | my $old_linenr = 0; |
| 1680 | my $new_linenr = 0; |
| 1681 | |
| 1682 | my $next_insert = 0; |
| 1683 | my $next_delete = 0; |
| 1684 | |
| 1685 | my @lines = (); |
| 1686 | |
| 1687 | my $inserted = @{$insertedRef}[$next_insert++]; |
| 1688 | my $deleted = @{$deletedRef}[$next_delete++]; |
| 1689 | |
| 1690 | foreach my $old_line (@{$linesRef}) { |
| 1691 | my $save_line = 1; |
| 1692 | my $line = $old_line; #don't modify the array |
Joe Perches | 323b267 | 2015-04-16 12:44:50 -0700 | [diff] [blame^] | 1693 | if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 1694 | $delta_offset = 0; |
| 1695 | } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk |
| 1696 | $range_last_linenr = $new_linenr; |
| 1697 | fixup_current_range(\$line, $delta_offset, 0); |
| 1698 | } |
| 1699 | |
| 1700 | while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { |
| 1701 | $deleted = @{$deletedRef}[$next_delete++]; |
| 1702 | $save_line = 0; |
| 1703 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); |
| 1704 | } |
| 1705 | |
| 1706 | while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { |
| 1707 | push(@lines, ${$inserted}{'LINE'}); |
| 1708 | $inserted = @{$insertedRef}[$next_insert++]; |
| 1709 | $new_linenr++; |
| 1710 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); |
| 1711 | } |
| 1712 | |
| 1713 | if ($save_line) { |
| 1714 | push(@lines, $line); |
| 1715 | $new_linenr++; |
| 1716 | } |
| 1717 | |
| 1718 | $old_linenr++; |
| 1719 | } |
| 1720 | |
| 1721 | return @lines; |
| 1722 | } |
| 1723 | |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 1724 | sub fix_insert_line { |
| 1725 | my ($linenr, $line) = @_; |
| 1726 | |
| 1727 | my $inserted = { |
| 1728 | LINENR => $linenr, |
| 1729 | LINE => $line, |
| 1730 | }; |
| 1731 | push(@fixed_inserted, $inserted); |
| 1732 | } |
| 1733 | |
| 1734 | sub fix_delete_line { |
| 1735 | my ($linenr, $line) = @_; |
| 1736 | |
| 1737 | my $deleted = { |
| 1738 | LINENR => $linenr, |
| 1739 | LINE => $line, |
| 1740 | }; |
| 1741 | |
| 1742 | push(@fixed_deleted, $deleted); |
| 1743 | } |
| 1744 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1745 | sub ERROR { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1746 | my ($type, $msg) = @_; |
| 1747 | |
| 1748 | if (report("ERROR", $type, $msg)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1749 | our $clean = 0; |
| 1750 | our $cnt_error++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1751 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1752 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1753 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1754 | } |
| 1755 | sub WARN { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1756 | my ($type, $msg) = @_; |
| 1757 | |
| 1758 | if (report("WARNING", $type, $msg)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1759 | our $clean = 0; |
| 1760 | our $cnt_warn++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1761 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1762 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1763 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1764 | } |
| 1765 | sub CHK { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1766 | my ($type, $msg) = @_; |
| 1767 | |
| 1768 | if ($check && report("CHECK", $type, $msg)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1769 | our $clean = 0; |
| 1770 | our $cnt_chk++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1771 | return 1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1772 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1773 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1776 | sub check_absolute_file { |
| 1777 | my ($absolute, $herecurr) = @_; |
| 1778 | my $file = $absolute; |
| 1779 | |
| 1780 | ##print "absolute<$absolute>\n"; |
| 1781 | |
| 1782 | # See if any suffix of this path is a path within the tree. |
| 1783 | while ($file =~ s@^[^/]*/@@) { |
| 1784 | if (-f "$root/$file") { |
| 1785 | ##print "file<$file>\n"; |
| 1786 | last; |
| 1787 | } |
| 1788 | } |
| 1789 | if (! -f _) { |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | # It is, so see if the prefix is acceptable. |
| 1794 | my $prefix = $absolute; |
| 1795 | substr($prefix, -length($file)) = ''; |
| 1796 | |
| 1797 | ##print "prefix<$prefix>\n"; |
| 1798 | if ($prefix ne ".../") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1799 | WARN("USE_RELATIVE_PATH", |
| 1800 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1804 | sub trim { |
| 1805 | my ($string) = @_; |
| 1806 | |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 1807 | $string =~ s/^\s+|\s+$//g; |
| 1808 | |
| 1809 | return $string; |
| 1810 | } |
| 1811 | |
| 1812 | sub ltrim { |
| 1813 | my ($string) = @_; |
| 1814 | |
| 1815 | $string =~ s/^\s+//; |
| 1816 | |
| 1817 | return $string; |
| 1818 | } |
| 1819 | |
| 1820 | sub rtrim { |
| 1821 | my ($string) = @_; |
| 1822 | |
| 1823 | $string =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1824 | |
| 1825 | return $string; |
| 1826 | } |
| 1827 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 1828 | sub string_find_replace { |
| 1829 | my ($string, $find, $replace) = @_; |
| 1830 | |
| 1831 | $string =~ s/$find/$replace/g; |
| 1832 | |
| 1833 | return $string; |
| 1834 | } |
| 1835 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1836 | sub tabify { |
| 1837 | my ($leading) = @_; |
| 1838 | |
| 1839 | my $source_indent = 8; |
| 1840 | my $max_spaces_before_tab = $source_indent - 1; |
| 1841 | my $spaces_to_tab = " " x $source_indent; |
| 1842 | |
| 1843 | #convert leading spaces to tabs |
| 1844 | 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; |
| 1845 | #Remove spaces before a tab |
| 1846 | 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; |
| 1847 | |
| 1848 | return "$leading"; |
| 1849 | } |
| 1850 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1851 | sub pos_last_openparen { |
| 1852 | my ($line) = @_; |
| 1853 | |
| 1854 | my $pos = 0; |
| 1855 | |
| 1856 | my $opens = $line =~ tr/\(/\(/; |
| 1857 | my $closes = $line =~ tr/\)/\)/; |
| 1858 | |
| 1859 | my $last_openparen = 0; |
| 1860 | |
| 1861 | if (($opens == 0) || ($closes >= $opens)) { |
| 1862 | return -1; |
| 1863 | } |
| 1864 | |
| 1865 | my $len = length($line); |
| 1866 | |
| 1867 | for ($pos = 0; $pos < $len; $pos++) { |
| 1868 | my $string = substr($line, $pos); |
| 1869 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 1870 | $pos += length($1) - 1; |
| 1871 | } elsif (substr($line, $pos, 1) eq '(') { |
| 1872 | $last_openparen = $pos; |
| 1873 | } elsif (index($string, '(') == -1) { |
| 1874 | last; |
| 1875 | } |
| 1876 | } |
| 1877 | |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 1878 | return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1881 | sub process { |
| 1882 | my $filename = shift; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1883 | |
| 1884 | my $linenr=0; |
| 1885 | my $prevline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1886 | my $prevrawline=""; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1887 | my $stashline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1888 | my $stashrawline=""; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1889 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1890 | my $length; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1891 | my $indent; |
| 1892 | my $previndent=0; |
| 1893 | my $stashindent=0; |
| 1894 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1895 | our $clean = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1896 | my $signoff = 0; |
| 1897 | my $is_patch = 0; |
| 1898 | |
Joe Perches | 29ee1b0 | 2014-08-06 16:10:35 -0700 | [diff] [blame] | 1899 | my $in_header_lines = $file ? 0 : 1; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1900 | my $in_commit_log = 0; #Scanning lines before patch |
Joe Perches | 2a076f4 | 2015-04-16 12:44:28 -0700 | [diff] [blame] | 1901 | my $commit_log_long_line = 0; |
Joe Perches | 13f1937 | 2014-08-06 16:10:59 -0700 | [diff] [blame] | 1902 | my $reported_maintainer_file = 0; |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1903 | my $non_utf8_charset = 0; |
| 1904 | |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 1905 | my $last_blank_line = 0; |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 1906 | my $last_coalesced_string_linenr = -1; |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 1907 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1908 | our @report = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1909 | our $cnt_lines = 0; |
| 1910 | our $cnt_error = 0; |
| 1911 | our $cnt_warn = 0; |
| 1912 | our $cnt_chk = 0; |
| 1913 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1914 | # Trace the real file/line as we go. |
| 1915 | my $realfile = ''; |
| 1916 | my $realline = 0; |
| 1917 | my $realcnt = 0; |
| 1918 | my $here = ''; |
| 1919 | my $in_comment = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1920 | my $comment_edge = 0; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1921 | my $first_line = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1922 | my $p1_prefix = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1923 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1924 | my $prev_values = 'E'; |
| 1925 | |
| 1926 | # suppression flags |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1927 | my %suppress_ifbraces; |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1928 | my %suppress_whiletrailers; |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1929 | my %suppress_export; |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1930 | my $suppress_statement = 0; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1931 | |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 1932 | my %signatures = (); |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 1933 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1934 | # Pre-scan the patch sanitizing the lines. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1935 | # Pre-scan the patch looking for any __setup documentation. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1936 | # |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1937 | my @setup_docs = (); |
| 1938 | my $setup_docs = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1939 | |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 1940 | my $camelcase_file_seeded = 0; |
| 1941 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1942 | sanitise_line_reset(); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1943 | my $line; |
| 1944 | foreach my $rawline (@rawlines) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1945 | $linenr++; |
| 1946 | $line = $rawline; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1947 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1948 | push(@fixed, $rawline) if ($fix); |
| 1949 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1950 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1951 | $setup_docs = 0; |
| 1952 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 1953 | $setup_docs = 1; |
| 1954 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1955 | #next; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1956 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1957 | if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
| 1958 | $realline=$1-1; |
| 1959 | if (defined $2) { |
| 1960 | $realcnt=$3+1; |
| 1961 | } else { |
| 1962 | $realcnt=1+1; |
| 1963 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1964 | $in_comment = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1965 | |
| 1966 | # Guestimate if this is a continuing comment. Run |
| 1967 | # the context looking for a comment "edge". If this |
| 1968 | # edge is a close comment then we must be in a comment |
| 1969 | # at context start. |
| 1970 | my $edge; |
Andy Whitcroft | 01fa914 | 2008-10-15 22:02:19 -0700 | [diff] [blame] | 1971 | my $cnt = $realcnt; |
| 1972 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 1973 | next if (defined $rawlines[$ln - 1] && |
| 1974 | $rawlines[$ln - 1] =~ /^-/); |
| 1975 | $cnt--; |
| 1976 | #print "RAW<$rawlines[$ln - 1]>\n"; |
Andy Whitcroft | 721c1cb | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 1977 | last if (!defined $rawlines[$ln - 1]); |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 1978 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 1979 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 1980 | ($edge) = $1; |
| 1981 | last; |
| 1982 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1983 | } |
| 1984 | if (defined $edge && $edge eq '*/') { |
| 1985 | $in_comment = 1; |
| 1986 | } |
| 1987 | |
| 1988 | # Guestimate if this is a continuing comment. If this |
| 1989 | # is the start of a diff block and this line starts |
| 1990 | # ' *' then it is very likely a comment. |
| 1991 | if (!defined $edge && |
Andy Whitcroft | 83242e0 | 2009-01-06 14:41:17 -0800 | [diff] [blame] | 1992 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1993 | { |
| 1994 | $in_comment = 1; |
| 1995 | } |
| 1996 | |
| 1997 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 1998 | sanitise_line_reset($in_comment); |
| 1999 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2000 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2001 | # Standardise the strings and chars within the input to |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2002 | # simplify matching -- only bother with positive lines. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2003 | $line = sanitise_line($rawline); |
| 2004 | } |
| 2005 | push(@lines, $line); |
| 2006 | |
| 2007 | if ($realcnt > 1) { |
| 2008 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 2009 | } else { |
| 2010 | $realcnt = 0; |
| 2011 | } |
| 2012 | |
| 2013 | #print "==>$rawline\n"; |
| 2014 | #print "-->$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2015 | |
| 2016 | if ($setup_docs && $line =~ /^\+/) { |
| 2017 | push(@setup_docs, $line); |
| 2018 | } |
| 2019 | } |
| 2020 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2021 | $prefix = ''; |
| 2022 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2023 | $realcnt = 0; |
| 2024 | $linenr = 0; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2025 | $fixlinenr = -1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2026 | foreach my $line (@lines) { |
| 2027 | $linenr++; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2028 | $fixlinenr++; |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 2029 | my $sline = $line; #copy of $line |
| 2030 | $sline =~ s/$;/ /g; #with comments as spaces |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2031 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2032 | my $rawline = $rawlines[$linenr - 1]; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2033 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2034 | #extract the line range in the file after the patch is applied |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2035 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2036 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2037 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2038 | $realline=$1-1; |
| 2039 | if (defined $2) { |
| 2040 | $realcnt=$3+1; |
| 2041 | } else { |
| 2042 | $realcnt=1+1; |
| 2043 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2044 | annotate_reset(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2045 | $prev_values = 'E'; |
| 2046 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2047 | %suppress_ifbraces = (); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2048 | %suppress_whiletrailers = (); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2049 | %suppress_export = (); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2050 | $suppress_statement = 0; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2051 | next; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2052 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2053 | # track the line number as we move through the hunk, note that |
| 2054 | # new versions of GNU diff omit the leading space on completely |
| 2055 | # blank context lines so we need to count that too. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2056 | } elsif ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2057 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2058 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2059 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2060 | # Measure the line length and indent. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2061 | ($length, $indent) = line_stats($rawline); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2062 | |
| 2063 | # Track the previous line. |
| 2064 | ($prevline, $stashline) = ($stashline, $line); |
| 2065 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2066 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 2067 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2068 | #warn "line<$line>\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2069 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2070 | } elsif ($realcnt == 1) { |
| 2071 | $realcnt--; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
Andy Whitcroft | cc77cdc | 2009-10-26 16:50:13 -0700 | [diff] [blame] | 2074 | my $hunk_line = ($realcnt != 0); |
| 2075 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2076 | #make up the handle for any error we report on this line |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2077 | $prefix = "$filename:$realline: " if ($emacs && $file); |
| 2078 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
| 2079 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2080 | $here = "#$linenr: " if (!$file); |
| 2081 | $here = "#$realline: " if ($file); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2082 | |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2083 | my $found_file = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2084 | # extract the filename as it passes |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2085 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 2086 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 2087 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 2088 | $in_commit_log = 0; |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2089 | $found_file = 1; |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2090 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2091 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 2092 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 2093 | $in_commit_log = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2094 | |
| 2095 | $p1_prefix = $1; |
Andy Whitcroft | e2f7aa4 | 2009-02-27 14:03:06 -0800 | [diff] [blame] | 2096 | if (!$file && $tree && $p1_prefix ne '' && |
| 2097 | -e "$root/$p1_prefix") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2098 | WARN("PATCH_PREFIX", |
| 2099 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2100 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2101 | |
Andy Whitcroft | c1ab332 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2102 | if ($realfile =~ m@^include/asm/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2103 | ERROR("MODIFIED_INCLUDE_ASM", |
| 2104 | "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] | 2105 | } |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2106 | $found_file = 1; |
| 2107 | } |
| 2108 | |
| 2109 | if ($found_file) { |
| 2110 | if ($realfile =~ m@^(drivers/net/|net/)@) { |
| 2111 | $check = 1; |
| 2112 | } else { |
| 2113 | $check = $check_orig; |
| 2114 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2115 | next; |
| 2116 | } |
| 2117 | |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 2118 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2119 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2120 | my $hereline = "$here\n$rawline\n"; |
| 2121 | my $herecurr = "$here\n$rawline\n"; |
| 2122 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2123 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2124 | $cnt_lines++ if ($realcnt != 0); |
| 2125 | |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2126 | # Check for incorrect file permissions |
| 2127 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 2128 | my $permhere = $here . "FILE: $realfile\n"; |
Joe Perches | 04db4d2 | 2013-04-29 16:18:14 -0700 | [diff] [blame] | 2129 | if ($realfile !~ m@scripts/@ && |
| 2130 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2131 | ERROR("EXECUTE_PERMISSIONS", |
| 2132 | "do not set execute permissions for source files\n" . $permhere); |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2136 | # Check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2137 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2138 | $signoff++; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2139 | $in_commit_log = 0; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2140 | } |
| 2141 | |
Joe Perches | e0d975b | 2014-12-10 15:51:49 -0800 | [diff] [blame] | 2142 | # Check if MAINTAINERS is being updated. If so, there's probably no need to |
| 2143 | # emit the "does MAINTAINERS need updating?" message on file add/move/delete |
| 2144 | if ($line =~ /^\s*MAINTAINERS\s*\|/) { |
| 2145 | $reported_maintainer_file = 1; |
| 2146 | } |
| 2147 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2148 | # Check signature styles |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 2149 | if (!$in_header_lines && |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 2150 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2151 | my $space_before = $1; |
| 2152 | my $sign_off = $2; |
| 2153 | my $space_after = $3; |
| 2154 | my $email = $4; |
| 2155 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 2156 | |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 2157 | if ($sign_off !~ /$signature_tags/) { |
| 2158 | WARN("BAD_SIGN_OFF", |
| 2159 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 2160 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2161 | if (defined $space_before && $space_before ne "") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2162 | if (WARN("BAD_SIGN_OFF", |
| 2163 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && |
| 2164 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2165 | $fixed[$fixlinenr] = |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2166 | "$ucfirst_sign_off $email"; |
| 2167 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2168 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2169 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2170 | if (WARN("BAD_SIGN_OFF", |
| 2171 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && |
| 2172 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2173 | $fixed[$fixlinenr] = |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2174 | "$ucfirst_sign_off $email"; |
| 2175 | } |
| 2176 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2177 | } |
| 2178 | if (!defined $space_after || $space_after ne " ") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2179 | if (WARN("BAD_SIGN_OFF", |
| 2180 | "Use a single space after $ucfirst_sign_off\n" . $herecurr) && |
| 2181 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2182 | $fixed[$fixlinenr] = |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2183 | "$ucfirst_sign_off $email"; |
| 2184 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | my ($email_name, $email_address, $comment) = parse_email($email); |
| 2188 | my $suggested_email = format_email(($email_name, $email_address)); |
| 2189 | if ($suggested_email eq "") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2190 | ERROR("BAD_SIGN_OFF", |
| 2191 | "Unrecognized email address: '$email'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2192 | } else { |
| 2193 | my $dequoted = $suggested_email; |
| 2194 | $dequoted =~ s/^"//; |
| 2195 | $dequoted =~ s/" </ </; |
| 2196 | # Don't force email to have quotes |
| 2197 | # Allow just an angle bracketed address |
| 2198 | if ("$dequoted$comment" ne $email && |
| 2199 | "<$email_address>$comment" ne $email && |
| 2200 | "$suggested_email$comment" ne $email) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2201 | WARN("BAD_SIGN_OFF", |
| 2202 | "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2203 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2204 | } |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 2205 | |
| 2206 | # Check for duplicate signatures |
| 2207 | my $sig_nospace = $line; |
| 2208 | $sig_nospace =~ s/\s//g; |
| 2209 | $sig_nospace = lc($sig_nospace); |
| 2210 | if (defined $signatures{$sig_nospace}) { |
| 2211 | WARN("BAD_SIGN_OFF", |
| 2212 | "Duplicate signature\n" . $herecurr); |
| 2213 | } else { |
| 2214 | $signatures{$sig_nospace} = 1; |
| 2215 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Joe Perches | a2fe16b | 2015-02-13 14:39:02 -0800 | [diff] [blame] | 2218 | # Check email subject for common tools that don't need to be mentioned |
| 2219 | if ($in_header_lines && |
| 2220 | $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { |
| 2221 | WARN("EMAIL_SUBJECT", |
| 2222 | "A patch subject line should describe the change not the tool that found it\n" . $herecurr); |
| 2223 | } |
| 2224 | |
Joe Perches | 9b3189e | 2014-06-04 16:12:10 -0700 | [diff] [blame] | 2225 | # Check for old stable address |
| 2226 | if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) { |
| 2227 | ERROR("STABLE_ADDRESS", |
| 2228 | "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr); |
| 2229 | } |
| 2230 | |
Christopher Covington | 7ebd05e | 2014-04-03 14:49:31 -0700 | [diff] [blame] | 2231 | # Check for unwanted Gerrit info |
| 2232 | if ($in_commit_log && $line =~ /^\s*change-id:/i) { |
| 2233 | ERROR("GERRIT_CHANGE_ID", |
| 2234 | "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); |
| 2235 | } |
| 2236 | |
Joe Perches | 2a076f4 | 2015-04-16 12:44:28 -0700 | [diff] [blame] | 2237 | # Check for line lengths > 75 in commit log, warn once |
| 2238 | if ($in_commit_log && !$commit_log_long_line && |
| 2239 | length($line) > 75) { |
| 2240 | WARN("COMMIT_LOG_LONG_LINE", |
| 2241 | "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); |
| 2242 | $commit_log_long_line = 1; |
| 2243 | } |
| 2244 | |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2245 | # Check for git id commit length and improperly formed commit descriptions |
| 2246 | if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) { |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 2247 | my $init_char = $1; |
| 2248 | my $orig_commit = lc($2); |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2249 | my $short = 1; |
| 2250 | my $long = 0; |
| 2251 | my $case = 1; |
| 2252 | my $space = 1; |
| 2253 | my $hasdesc = 0; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2254 | my $hasparens = 0; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2255 | my $id = '0123456789ab'; |
| 2256 | my $orig_desc = "commit description"; |
| 2257 | my $description = ""; |
| 2258 | |
| 2259 | $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); |
| 2260 | $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); |
| 2261 | $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); |
| 2262 | $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); |
| 2263 | if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { |
| 2264 | $orig_desc = $1; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2265 | $hasparens = 1; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2266 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && |
| 2267 | defined $rawlines[$linenr] && |
| 2268 | $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { |
| 2269 | $orig_desc = $1; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2270 | $hasparens = 1; |
Joe Perches | b671fde | 2015-02-13 14:38:41 -0800 | [diff] [blame] | 2271 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && |
| 2272 | defined $rawlines[$linenr] && |
| 2273 | $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { |
| 2274 | $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; |
| 2275 | $orig_desc = $1; |
| 2276 | $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; |
| 2277 | $orig_desc .= " " . $1; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2278 | $hasparens = 1; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | ($id, $description) = git_commit_info($orig_commit, |
| 2282 | $id, $orig_desc); |
| 2283 | |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2284 | if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) { |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2285 | ERROR("GIT_COMMIT_ID", |
| 2286 | "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); |
| 2287 | } |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
Joe Perches | 13f1937 | 2014-08-06 16:10:59 -0700 | [diff] [blame] | 2290 | # Check for added, moved or deleted files |
| 2291 | if (!$reported_maintainer_file && !$in_commit_log && |
| 2292 | ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || |
| 2293 | $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || |
| 2294 | ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && |
| 2295 | (defined($1) || defined($2))))) { |
| 2296 | $reported_maintainer_file = 1; |
| 2297 | WARN("FILE_PATH_CHANGES", |
| 2298 | "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); |
| 2299 | } |
| 2300 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2301 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2302 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2303 | ERROR("CORRUPTED_PATCH", |
| 2304 | "patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2305 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2308 | # Check for absolute kernel paths. |
| 2309 | if ($tree) { |
| 2310 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 2311 | my $file = $1; |
| 2312 | |
| 2313 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 2314 | check_absolute_file($1, $herecurr)) { |
| 2315 | # |
| 2316 | } else { |
| 2317 | check_absolute_file($file, $herecurr); |
| 2318 | } |
| 2319 | } |
| 2320 | } |
| 2321 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2322 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 2323 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2324 | $rawline !~ m/^$UTF8*$/) { |
| 2325 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 2326 | |
| 2327 | my $blank = copy_spacing($rawline); |
| 2328 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 2329 | my $hereptr = "$hereline$ptr\n"; |
| 2330 | |
Joe Perches | 34d9921 | 2011-07-25 17:13:26 -0700 | [diff] [blame] | 2331 | CHK("INVALID_UTF8", |
| 2332 | "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] | 2333 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2334 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2335 | # Check if it's the start of a commit log |
| 2336 | # (not a header line and we haven't seen the patch filename) |
| 2337 | if ($in_header_lines && $realfile =~ /^$/ && |
Joe Perches | 29ee1b0 | 2014-08-06 16:10:35 -0700 | [diff] [blame] | 2338 | !($rawline =~ /^\s+\S/ || |
| 2339 | $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) { |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2340 | $in_header_lines = 0; |
| 2341 | $in_commit_log = 1; |
| 2342 | } |
| 2343 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 2344 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 2345 | # declined it, i.e defined some charset where it is missing. |
| 2346 | if ($in_header_lines && |
| 2347 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 2348 | $1 !~ /utf-8/i) { |
| 2349 | $non_utf8_charset = 1; |
| 2350 | } |
| 2351 | |
| 2352 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2353 | $rawline =~ /$NON_ASCII_UTF8/) { |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 2354 | WARN("UTF8_BEFORE_PATCH", |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2355 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 2356 | } |
| 2357 | |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 2358 | # Check for various typo / spelling mistakes |
Joe Perches | 66d7a38 | 2015-04-16 12:44:08 -0700 | [diff] [blame] | 2359 | if (defined($misspellings) && |
| 2360 | ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 2361 | while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 2362 | my $typo = $1; |
| 2363 | my $typo_fix = $spelling_fix{lc($typo)}; |
| 2364 | $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); |
| 2365 | $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); |
| 2366 | my $msg_type = \&WARN; |
| 2367 | $msg_type = \&CHK if ($file); |
| 2368 | if (&{$msg_type}("TYPO_SPELLING", |
| 2369 | "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && |
| 2370 | $fix) { |
| 2371 | $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; |
| 2372 | } |
| 2373 | } |
| 2374 | } |
| 2375 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 2376 | # ignore non-hunk lines and lines being removed |
| 2377 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2378 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2379 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2380 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2381 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2382 | if (ERROR("DOS_LINE_ENDINGS", |
| 2383 | "DOS line endings\n" . $herevet) && |
| 2384 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2385 | $fixed[$fixlinenr] =~ s/[\s\015]+$//; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2386 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2387 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 2388 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2389 | if (ERROR("TRAILING_WHITESPACE", |
| 2390 | "trailing whitespace\n" . $herevet) && |
| 2391 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2392 | $fixed[$fixlinenr] =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2393 | } |
| 2394 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2395 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2396 | } |
Andy Whitcroft | 5368df20 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2397 | |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 2398 | # Check for FSF mailing addresses. |
Alexander Duyck | 109d8cb | 2014-01-23 15:54:50 -0800 | [diff] [blame] | 2399 | if ($rawline =~ /\bwrite to the Free/i || |
Joe Perches | 3e2232f | 2014-01-23 15:54:48 -0800 | [diff] [blame] | 2400 | $rawline =~ /\b59\s+Temple\s+Pl/i || |
| 2401 | $rawline =~ /\b51\s+Franklin\s+St/i) { |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 2402 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
| 2403 | my $msg_type = \&ERROR; |
| 2404 | $msg_type = \&CHK if ($file); |
| 2405 | &{$msg_type}("FSF_MAILING_ADDRESS", |
Joe Perches | 3e2232f | 2014-01-23 15:54:48 -0800 | [diff] [blame] | 2406 | "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] | 2407 | } |
| 2408 | |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2409 | # check for Kconfig help text having a real description |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2410 | # Only applies when adding the entry originally, after that we do not have |
| 2411 | # sufficient context to determine whether it is indeed long enough. |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2412 | if ($realfile =~ /Kconfig/ && |
Joe Perches | 8d73e0e | 2014-08-06 16:10:46 -0700 | [diff] [blame] | 2413 | $line =~ /^\+\s*config\s+/) { |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2414 | my $length = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2415 | my $cnt = $realcnt; |
| 2416 | my $ln = $linenr + 1; |
| 2417 | my $f; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2418 | my $is_start = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2419 | my $is_end = 0; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2420 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2421 | $f = $lines[$ln - 1]; |
| 2422 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 2423 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2424 | |
| 2425 | next if ($f =~ /^-/); |
Joe Perches | 8d73e0e | 2014-08-06 16:10:46 -0700 | [diff] [blame] | 2426 | last if (!$file && $f =~ /^\@\@/); |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2427 | |
Joe Perches | 8d73e0e | 2014-08-06 16:10:46 -0700 | [diff] [blame] | 2428 | if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) { |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2429 | $is_start = 1; |
Joe Perches | 8d73e0e | 2014-08-06 16:10:46 -0700 | [diff] [blame] | 2430 | } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2431 | $length = -1; |
| 2432 | } |
| 2433 | |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2434 | $f =~ s/^.//; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2435 | $f =~ s/#.*//; |
| 2436 | $f =~ s/^\s+//; |
| 2437 | next if ($f =~ /^$/); |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2438 | if ($f =~ /^\s*config\s/) { |
| 2439 | $is_end = 1; |
| 2440 | last; |
| 2441 | } |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2442 | $length++; |
| 2443 | } |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 2444 | if ($is_start && $is_end && $length < $min_conf_desc_length) { |
| 2445 | WARN("CONFIG_DESCRIPTION", |
| 2446 | "please write a paragraph that describes the config symbol fully\n" . $herecurr); |
| 2447 | } |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2448 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 2451 | # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. |
| 2452 | if ($realfile =~ /Kconfig/ && |
| 2453 | $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { |
| 2454 | WARN("CONFIG_EXPERIMENTAL", |
| 2455 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 2456 | } |
| 2457 | |
Christoph Jaeger | 327953e | 2015-02-13 14:38:29 -0800 | [diff] [blame] | 2458 | # discourage the use of boolean for type definition attributes of Kconfig options |
| 2459 | if ($realfile =~ /Kconfig/ && |
| 2460 | $line =~ /^\+\s*\bboolean\b/) { |
| 2461 | WARN("CONFIG_TYPE_BOOLEAN", |
| 2462 | "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); |
| 2463 | } |
| 2464 | |
Arnaud Lacombe | c68e587 | 2011-08-15 01:07:14 -0400 | [diff] [blame] | 2465 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 2466 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 2467 | my $flag = $1; |
| 2468 | my $replacement = { |
| 2469 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 2470 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 2471 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 2472 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 2473 | }; |
| 2474 | |
| 2475 | WARN("DEPRECATED_VARIABLE", |
| 2476 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
| 2477 | } |
| 2478 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2479 | # check for DT compatible documentation |
Florian Vaussard | 7dd05b3 | 2014-04-03 14:49:26 -0700 | [diff] [blame] | 2480 | if (defined $root && |
| 2481 | (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || |
| 2482 | ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { |
| 2483 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2484 | my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; |
| 2485 | |
Florian Vaussard | cc93319 | 2014-04-03 14:49:27 -0700 | [diff] [blame] | 2486 | my $dt_path = $root . "/Documentation/devicetree/bindings/"; |
| 2487 | my $vp_file = $dt_path . "vendor-prefixes.txt"; |
| 2488 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2489 | foreach my $compat (@compats) { |
| 2490 | my $compat2 = $compat; |
Rob Herring | 185d566 | 2014-06-04 16:12:03 -0700 | [diff] [blame] | 2491 | $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; |
| 2492 | my $compat3 = $compat; |
| 2493 | $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; |
| 2494 | `grep -Erq "$compat|$compat2|$compat3" $dt_path`; |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2495 | if ( $? >> 8 ) { |
| 2496 | WARN("UNDOCUMENTED_DT_STRING", |
| 2497 | "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); |
| 2498 | } |
| 2499 | |
Florian Vaussard | 4fbf32a | 2014-04-03 14:49:25 -0700 | [diff] [blame] | 2500 | next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; |
| 2501 | my $vendor = $1; |
Florian Vaussard | cc93319 | 2014-04-03 14:49:27 -0700 | [diff] [blame] | 2502 | `grep -Eq "^$vendor\\b" $vp_file`; |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2503 | if ( $? >> 8 ) { |
| 2504 | WARN("UNDOCUMENTED_DT_STRING", |
Florian Vaussard | cc93319 | 2014-04-03 14:49:27 -0700 | [diff] [blame] | 2505 | "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] | 2506 | } |
| 2507 | } |
| 2508 | } |
| 2509 | |
Andy Whitcroft | 5368df20 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2510 | # check we are in a valid source file if not then ignore this hunk |
Geert Uytterhoeven | de4c924 | 2014-10-13 15:51:46 -0700 | [diff] [blame] | 2511 | next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/); |
Andy Whitcroft | 5368df20 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2512 | |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2513 | #line length limit |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2514 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2515 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
Joe Perches | 29a3c46 | 2015-04-16 12:44:30 -0700 | [diff] [blame] | 2516 | !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?$String\s*(?:|,|\)\s*;)\s*$/ || |
| 2517 | $line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || |
| 2518 | $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) && |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2519 | $length > $max_line_length) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2520 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2521 | WARN("LONG_LINE", |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2522 | "line over $max_line_length characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2525 | # check for adding lines without a newline. |
| 2526 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2527 | WARN("MISSING_EOF_NEWLINE", |
| 2528 | "adding a line without newline at end of file\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2529 | } |
| 2530 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2531 | # Blackfin: use hi/lo macros |
| 2532 | if ($realfile =~ m@arch/blackfin/.*\.S$@) { |
| 2533 | if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { |
| 2534 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2535 | ERROR("LO_MACRO", |
| 2536 | "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2537 | } |
| 2538 | if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { |
| 2539 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2540 | ERROR("HI_MACRO", |
| 2541 | "use the HI() macro, not (... >> 16)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2542 | } |
| 2543 | } |
| 2544 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2545 | # 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] | 2546 | next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2547 | |
| 2548 | # at the beginning of a line any tabs must come first and anything |
| 2549 | # more than 8 must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2550 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 2551 | $rawline =~ /^\+\s* \s*/) { |
| 2552 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2553 | $rpt_cleaners = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2554 | if (ERROR("CODE_INDENT", |
| 2555 | "code indent should use tabs where possible\n" . $herevet) && |
| 2556 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2557 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2558 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2559 | } |
| 2560 | |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2561 | # check for space before tabs. |
| 2562 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 2563 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2564 | if (WARN("SPACE_BEFORE_TAB", |
| 2565 | "please, no space before tabs\n" . $herevet) && |
| 2566 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2567 | while ($fixed[$fixlinenr] =~ |
Joe Perches | d2207cc | 2014-10-13 15:51:53 -0700 | [diff] [blame] | 2568 | s/(^\+.*) {8,8}\t/$1\t\t/) {} |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2569 | while ($fixed[$fixlinenr] =~ |
Joe Perches | c76f4cb | 2014-01-23 15:54:46 -0800 | [diff] [blame] | 2570 | s/(^\+.*) +\t/$1\t/) {} |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2571 | } |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2572 | } |
| 2573 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2574 | # check for && or || at the start of a line |
| 2575 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
| 2576 | CHK("LOGICAL_CONTINUATIONS", |
| 2577 | "Logical continuations should be on the previous line\n" . $hereprev); |
| 2578 | } |
| 2579 | |
| 2580 | # check multi-line statement indentation matches previous line |
| 2581 | if ($^V && $^V ge 5.10.0 && |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 2582 | $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2583 | $prevline =~ /^\+(\t*)(.*)$/; |
| 2584 | my $oldindent = $1; |
| 2585 | my $rest = $2; |
| 2586 | |
| 2587 | my $pos = pos_last_openparen($rest); |
| 2588 | if ($pos >= 0) { |
Joe Perches | b34a26f | 2012-07-30 14:41:16 -0700 | [diff] [blame] | 2589 | $line =~ /^(\+| )([ \t]*)/; |
| 2590 | my $newindent = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2591 | |
| 2592 | my $goodtabindent = $oldindent . |
| 2593 | "\t" x ($pos / 8) . |
| 2594 | " " x ($pos % 8); |
| 2595 | my $goodspaceindent = $oldindent . " " x $pos; |
| 2596 | |
| 2597 | if ($newindent ne $goodtabindent && |
| 2598 | $newindent ne $goodspaceindent) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2599 | |
| 2600 | if (CHK("PARENTHESIS_ALIGNMENT", |
| 2601 | "Alignment should match open parenthesis\n" . $hereprev) && |
| 2602 | $fix && $line =~ /^\+/) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2603 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2604 | s/^\+[ \t]*/\+$goodtabindent/; |
| 2605 | } |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | |
Joe Perches | 6ab3a97 | 2015-04-16 12:44:05 -0700 | [diff] [blame] | 2610 | # check for space after cast like "(int) foo" or "(struct foo) bar" |
| 2611 | # avoid checking a few false positives: |
| 2612 | # "sizeof(<type>)" or "__alignof__(<type>)" |
| 2613 | # function pointer declarations like "(*foo)(int) = bar;" |
| 2614 | # structure definitions like "(struct foo) { 0 };" |
| 2615 | # multiline macros that define functions |
| 2616 | # known attributes or the __attribute__ keyword |
| 2617 | if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && |
| 2618 | (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2619 | if (CHK("SPACING", |
Joe Perches | f27c95d | 2014-08-06 16:10:52 -0700 | [diff] [blame] | 2620 | "No space is necessary after a cast\n" . $herecurr) && |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2621 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2622 | $fixed[$fixlinenr] =~ |
Joe Perches | f27c95d | 2014-08-06 16:10:52 -0700 | [diff] [blame] | 2623 | s/(\(\s*$Type\s*\))[ \t]+/$1/; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2624 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2627 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | fdb4bcd | 2013-07-03 15:05:23 -0700 | [diff] [blame] | 2628 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
Joe Perches | 85ad978 | 2014-04-03 14:49:20 -0700 | [diff] [blame] | 2629 | $rawline =~ /^\+[ \t]*\*/ && |
| 2630 | $realline > 2) { |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2631 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2632 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 2633 | } |
| 2634 | |
| 2635 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2636 | $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* |
| 2637 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
Joe Perches | 61135e9 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 2638 | $rawline =~ /^\+/ && #line is new |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2639 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
| 2640 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2641 | "networking block comments start with * on subsequent lines\n" . $hereprev); |
| 2642 | } |
| 2643 | |
| 2644 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | c24f9f1 | 2012-11-08 15:53:29 -0800 | [diff] [blame] | 2645 | $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
| 2646 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 2647 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 2648 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2649 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2650 | "networking block comments put the trailing */ on a separate line\n" . $herecurr); |
| 2651 | } |
| 2652 | |
Joe Perches | 7f61919 | 2014-08-06 16:10:39 -0700 | [diff] [blame] | 2653 | # check for missing blank lines after struct/union declarations |
| 2654 | # with exceptions for various attributes and macros |
| 2655 | if ($prevline =~ /^[\+ ]};?\s*$/ && |
| 2656 | $line =~ /^\+/ && |
| 2657 | !($line =~ /^\+\s*$/ || |
| 2658 | $line =~ /^\+\s*EXPORT_SYMBOL/ || |
| 2659 | $line =~ /^\+\s*MODULE_/i || |
| 2660 | $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || |
| 2661 | $line =~ /^\+[a-z_]*init/ || |
| 2662 | $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || |
| 2663 | $line =~ /^\+\s*DECLARE/ || |
| 2664 | $line =~ /^\+\s*__setup/)) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2665 | if (CHK("LINE_SPACING", |
| 2666 | "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && |
| 2667 | $fix) { |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 2668 | fix_insert_line($fixlinenr, "\+"); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2669 | } |
Joe Perches | 7f61919 | 2014-08-06 16:10:39 -0700 | [diff] [blame] | 2670 | } |
| 2671 | |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 2672 | # check for multiple consecutive blank lines |
| 2673 | if ($prevline =~ /^[\+ ]\s*$/ && |
| 2674 | $line =~ /^\+\s*$/ && |
| 2675 | $last_blank_line != ($linenr - 1)) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2676 | if (CHK("LINE_SPACING", |
| 2677 | "Please don't use multiple blank lines\n" . $hereprev) && |
| 2678 | $fix) { |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 2679 | fix_delete_line($fixlinenr, $rawline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2680 | } |
| 2681 | |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 2682 | $last_blank_line = $linenr; |
| 2683 | } |
| 2684 | |
Joe Perches | 3b617e3 | 2014-04-03 14:49:28 -0700 | [diff] [blame] | 2685 | # check for missing blank lines after declarations |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 2686 | if ($sline =~ /^\+\s+\S/ && #Not at char 1 |
| 2687 | # actual declarations |
| 2688 | ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
Joe Perches | 5a4e1fd | 2014-08-06 16:10:33 -0700 | [diff] [blame] | 2689 | # function pointer declarations |
| 2690 | $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 2691 | # foo bar; where foo is some local typedef or #define |
| 2692 | $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
| 2693 | # known declaration macros |
| 2694 | $prevline =~ /^\+\s+$declaration_macros/) && |
| 2695 | # for "else if" which can look like "$Ident $Ident" |
| 2696 | !($prevline =~ /^\+\s+$c90_Keywords\b/ || |
| 2697 | # other possible extensions of declaration lines |
| 2698 | $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || |
| 2699 | # not starting a section or a macro "\" extended line |
| 2700 | $prevline =~ /(?:\{\s*|\\)$/) && |
| 2701 | # looks like a declaration |
| 2702 | !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
Joe Perches | 5a4e1fd | 2014-08-06 16:10:33 -0700 | [diff] [blame] | 2703 | # function pointer declarations |
| 2704 | $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 2705 | # foo bar; where foo is some local typedef or #define |
| 2706 | $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
| 2707 | # known declaration macros |
| 2708 | $sline =~ /^\+\s+$declaration_macros/ || |
| 2709 | # start of struct or union or enum |
Joe Perches | 3b617e3 | 2014-04-03 14:49:28 -0700 | [diff] [blame] | 2710 | $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 2711 | # start or end of block or continuation of declaration |
| 2712 | $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || |
| 2713 | # bitfield continuation |
| 2714 | $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || |
| 2715 | # other possible extensions of declaration lines |
| 2716 | $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && |
| 2717 | # indentation of previous and current line are the same |
| 2718 | (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2719 | if (WARN("LINE_SPACING", |
| 2720 | "Missing a blank line after declarations\n" . $hereprev) && |
| 2721 | $fix) { |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 2722 | fix_insert_line($fixlinenr, "\+"); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2723 | } |
Joe Perches | 3b617e3 | 2014-04-03 14:49:28 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2726 | # check for spaces at the beginning of a line. |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 2727 | # Exceptions: |
| 2728 | # 1) within comments |
| 2729 | # 2) indented preprocessor commands |
| 2730 | # 3) hanging labels |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2731 | if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2732 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2733 | if (WARN("LEADING_SPACE", |
| 2734 | "please, no spaces at the start of a line\n" . $herevet) && |
| 2735 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2736 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2737 | } |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2740 | # check we are in a valid C source file if not then ignore this hunk |
| 2741 | next if ($realfile !~ /\.(h|c)$/); |
| 2742 | |
Joe Perches | 032a4c0 | 2014-08-06 16:10:29 -0700 | [diff] [blame] | 2743 | # check indentation of any line with a bare else |
Joe Perches | 840080a | 2014-10-13 15:51:59 -0700 | [diff] [blame] | 2744 | # (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] | 2745 | # if the previous line is a break or return and is indented 1 tab more... |
| 2746 | if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { |
| 2747 | my $tabs = length($1) + 1; |
Joe Perches | 840080a | 2014-10-13 15:51:59 -0700 | [diff] [blame] | 2748 | if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || |
| 2749 | ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && |
| 2750 | defined $lines[$linenr] && |
| 2751 | $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { |
Joe Perches | 032a4c0 | 2014-08-06 16:10:29 -0700 | [diff] [blame] | 2752 | WARN("UNNECESSARY_ELSE", |
| 2753 | "else is not generally useful after a break or return\n" . $hereprev); |
| 2754 | } |
| 2755 | } |
| 2756 | |
Joe Perches | c00df19 | 2014-08-06 16:11:01 -0700 | [diff] [blame] | 2757 | # check indentation of a line with a break; |
| 2758 | # if the previous line is a goto or return and is indented the same # of tabs |
| 2759 | if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { |
| 2760 | my $tabs = $1; |
| 2761 | if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { |
| 2762 | WARN("UNNECESSARY_BREAK", |
| 2763 | "break is not useful after a goto or return\n" . $hereprev); |
| 2764 | } |
| 2765 | } |
| 2766 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 2767 | # discourage the addition of CONFIG_EXPERIMENTAL in #if(def). |
| 2768 | if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { |
| 2769 | WARN("CONFIG_EXPERIMENTAL", |
| 2770 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 2771 | } |
| 2772 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2773 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2774 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2775 | WARN("CVS_KEYWORD", |
| 2776 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2777 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2778 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2779 | # Blackfin: don't use __builtin_bfin_[cs]sync |
| 2780 | if ($line =~ /__builtin_bfin_csync/) { |
| 2781 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2782 | ERROR("CSYNC", |
| 2783 | "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2784 | } |
| 2785 | if ($line =~ /__builtin_bfin_ssync/) { |
| 2786 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2787 | ERROR("SSYNC", |
| 2788 | "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
Joe Perches | 56e77d7 | 2013-02-21 16:44:14 -0800 | [diff] [blame] | 2791 | # check for old HOTPLUG __dev<foo> section markings |
| 2792 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 2793 | WARN("HOTPLUG_SECTION", |
| 2794 | "Using $1 is unnecessary\n" . $herecurr); |
| 2795 | } |
| 2796 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2797 | # Check for potential 'bare' types |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2798 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 2799 | $realline_next); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2800 | #print "LINE<$line>\n"; |
| 2801 | if ($linenr >= $suppress_statement && |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 2802 | $realcnt && $sline =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2803 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2804 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2805 | $stat =~ s/\n./\n /g; |
| 2806 | $cond =~ s/\n./\n /g; |
| 2807 | |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2808 | #print "linenr<$linenr> <$stat>\n"; |
| 2809 | # If this statement has no statement boundaries within |
| 2810 | # it there is no point in retrying a statement scan |
| 2811 | # until we hit end of it. |
| 2812 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 2813 | if ($frag !~ /(?:{|;)/) { |
| 2814 | #print "skip<$line_nr_next>\n"; |
| 2815 | $suppress_statement = $line_nr_next; |
| 2816 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 2817 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2818 | # Find the real next line. |
| 2819 | $realline_next = $line_nr_next; |
| 2820 | if (defined $realline_next && |
| 2821 | (!defined $lines[$realline_next - 1] || |
| 2822 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 2823 | $realline_next++; |
| 2824 | } |
| 2825 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2826 | my $s = $stat; |
| 2827 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2828 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2829 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2830 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2831 | |
| 2832 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2833 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2834 | |
Andy Whitcroft | 463f286 | 2009-09-21 17:04:34 -0700 | [diff] [blame] | 2835 | } elsif ($s =~ /^.\s*else\b/s) { |
| 2836 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2837 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 2838 | } 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] | 2839 | my $type = $1; |
| 2840 | $type =~ s/\s+/ /g; |
| 2841 | possible($type, "A:" . $s); |
| 2842 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2843 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a84062 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2844 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2845 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2846 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2847 | |
| 2848 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2849 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2850 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2851 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2852 | |
| 2853 | # Check for any sort of function declaration. |
| 2854 | # int foo(something bar, other baz); |
| 2855 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2856 | 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] | 2857 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2858 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2859 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2860 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2861 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2862 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2863 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2864 | 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] | 2865 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2866 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | } |
| 2870 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2871 | } |
| 2872 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2873 | # |
| 2874 | # Checks which may be anchored in the context. |
| 2875 | # |
| 2876 | |
| 2877 | # Check for switch () and associated case and default |
| 2878 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2879 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 2880 | my $err = ''; |
| 2881 | my $sep = ''; |
| 2882 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 2883 | shift(@ctx); |
| 2884 | for my $ctx (@ctx) { |
| 2885 | my ($clen, $cindent) = line_stats($ctx); |
| 2886 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 2887 | $indent != $cindent) { |
| 2888 | $err .= "$sep$ctx\n"; |
| 2889 | $sep = ''; |
| 2890 | } else { |
| 2891 | $sep = "[...]\n"; |
| 2892 | } |
| 2893 | } |
| 2894 | if ($err ne '') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2895 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 2896 | "switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2897 | } |
| 2898 | } |
| 2899 | |
| 2900 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 2901 | # or if that brace on the next line is for something else |
Joe Perches | 0fe3dc2 | 2014-08-06 16:11:16 -0700 | [diff] [blame] | 2902 | 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] | 2903 | my $pre_ctx = "$1$2"; |
| 2904 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2905 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Joe Perches | 8eef05d | 2012-02-03 15:20:39 -0800 | [diff] [blame] | 2906 | |
| 2907 | if ($line =~ /^\+\t{6,}/) { |
| 2908 | WARN("DEEP_INDENTATION", |
| 2909 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 2910 | } |
| 2911 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2912 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 2913 | my $ctx = join("\n", @ctx); |
| 2914 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2915 | my $ctx_ln = $linenr; |
| 2916 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2917 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2918 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 2919 | defined $lines[$ctx_ln - 1] && |
| 2920 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 2921 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 2922 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2923 | $ctx_ln++; |
| 2924 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2925 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2926 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 2927 | #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] | 2928 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2929 | 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] | 2930 | ERROR("OPEN_BRACE", |
| 2931 | "that open brace { should be on the previous line\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2932 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2933 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2934 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 2935 | $ctx =~ /\)\s*\;\s*$/ && |
| 2936 | defined $lines[$ctx_ln - 1]) |
| 2937 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2938 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 2939 | if ($nindent > $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2940 | WARN("TRAILING_SEMICOLON", |
| 2941 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2942 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2943 | } |
| 2944 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2945 | } |
| 2946 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2947 | # Check relative indent for conditionals and blocks. |
Joe Perches | 0fe3dc2 | 2014-08-06 16:11:16 -0700 | [diff] [blame] | 2948 | if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2949 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 2950 | ctx_statement_block($linenr, $realcnt, 0) |
| 2951 | if (!defined $stat); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2952 | my ($s, $c) = ($stat, $cond); |
| 2953 | |
| 2954 | substr($s, 0, length($c), ''); |
| 2955 | |
| 2956 | # Make sure we remove the line prefixes as we have |
| 2957 | # none on the first line, and are going to readd them |
| 2958 | # where necessary. |
| 2959 | $s =~ s/\n./\n/gs; |
| 2960 | |
| 2961 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2962 | my @newlines = ($c =~ /\n/gs); |
| 2963 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2964 | |
| 2965 | # We want to check the first line inside the block |
| 2966 | # starting at the end of the conditional, so remove: |
| 2967 | # 1) any blank line termination |
| 2968 | # 2) any opening brace { on end of the line |
| 2969 | # 3) any do (...) { |
| 2970 | my $continuation = 0; |
| 2971 | my $check = 0; |
| 2972 | $s =~ s/^.*\bdo\b//; |
| 2973 | $s =~ s/^\s*{//; |
| 2974 | if ($s =~ s/^\s*\\//) { |
| 2975 | $continuation = 1; |
| 2976 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2977 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2978 | $check = 1; |
| 2979 | $cond_lines++; |
| 2980 | } |
| 2981 | |
| 2982 | # Also ignore a loop construct at the end of a |
| 2983 | # preprocessor statement. |
| 2984 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 2985 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 2986 | $check = 0; |
| 2987 | } |
| 2988 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2989 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2990 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2991 | while ($cond_ptr != $cond_lines) { |
| 2992 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2993 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2994 | # If we see an #else/#elif then the code |
| 2995 | # is not linear. |
| 2996 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 2997 | $check = 0; |
| 2998 | } |
| 2999 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3000 | # Ignore: |
| 3001 | # 1) blank lines, they should be at 0, |
| 3002 | # 2) preprocessor lines, and |
| 3003 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 3004 | if ($continuation || |
| 3005 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3006 | $s =~ /^\s*#\s*?/ || |
| 3007 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 3008 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 30dad6e | 2009-09-21 17:04:36 -0700 | [diff] [blame] | 3009 | if ($s =~ s/^.*?\n//) { |
| 3010 | $cond_lines++; |
| 3011 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3012 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | my (undef, $sindent) = line_stats("+" . $s); |
| 3016 | my $stat_real = raw_line($linenr, $cond_lines); |
| 3017 | |
| 3018 | # Check if either of these lines are modified, else |
| 3019 | # this is not this patch's fault. |
| 3020 | if (!defined($stat_real) || |
| 3021 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 3022 | $check = 0; |
| 3023 | } |
| 3024 | if (defined($stat_real) && $cond_lines > 1) { |
| 3025 | $stat_real = "[...]\n$stat_real"; |
| 3026 | } |
| 3027 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3028 | #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] | 3029 | |
| 3030 | if ($check && (($sindent % 8) != 0 || |
| 3031 | ($sindent <= $indent && $s ne ''))) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3032 | WARN("SUSPECT_CODE_INDENT", |
| 3033 | "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] | 3034 | } |
| 3035 | } |
| 3036 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3037 | # Track the 'values' across context and added lines. |
| 3038 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3039 | my ($curr_values, $curr_vars) = |
| 3040 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3041 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3042 | if ($dbg_values) { |
| 3043 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3044 | print "$linenr > .$outline\n"; |
| 3045 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3046 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3047 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3048 | $prev_values = substr($curr_values, -1); |
| 3049 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3050 | #ignore lines not being added |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3051 | next if ($line =~ /^[^\+]/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3052 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3053 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 3054 | if ($dbg_type) { |
| 3055 | if ($line =~ /^.\s*$Declare\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3056 | ERROR("TEST_TYPE", |
| 3057 | "TEST: is type\n" . $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 3058 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3059 | ERROR("TEST_NOT_TYPE", |
| 3060 | "TEST: is not type ($1 is)\n". $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 3061 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3062 | next; |
| 3063 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 3064 | # TEST: allow direct testing of the attribute matcher. |
| 3065 | if ($dbg_attr) { |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 3066 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3067 | ERROR("TEST_ATTR", |
| 3068 | "TEST: is attr\n" . $herecurr); |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 3069 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3070 | ERROR("TEST_NOT_ATTR", |
| 3071 | "TEST: is not attr ($1 is)\n". $herecurr); |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 3072 | } |
| 3073 | next; |
| 3074 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3075 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3076 | # check for initialisation to aggregates open brace on the next line |
Andy Whitcroft | 99423c2 | 2009-10-26 16:50:15 -0700 | [diff] [blame] | 3077 | if ($line =~ /^.\s*{/ && |
| 3078 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3079 | if (ERROR("OPEN_BRACE", |
| 3080 | "that open brace { should be on the previous line\n" . $hereprev) && |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 3081 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 3082 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 3083 | fix_delete_line($fixlinenr, $rawline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3084 | my $fixedline = $prevrawline; |
| 3085 | $fixedline =~ s/\s*=\s*$/ = {/; |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 3086 | fix_insert_line($fixlinenr, $fixedline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3087 | $fixedline = $line; |
| 3088 | $fixedline =~ s/^(.\s*){\s*/$1/; |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 3089 | fix_insert_line($fixlinenr, $fixedline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3090 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3093 | # |
| 3094 | # Checks which are anchored on the added line. |
| 3095 | # |
| 3096 | |
| 3097 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3098 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3099 | my $path = $1; |
| 3100 | if ($path =~ m{//}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3101 | ERROR("MALFORMED_INCLUDE", |
Joe Perches | 495e9d8 | 2012-12-20 15:05:37 -0800 | [diff] [blame] | 3102 | "malformed #include filename\n" . $herecurr); |
| 3103 | } |
| 3104 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 3105 | ERROR("UAPI_INCLUDE", |
| 3106 | "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] | 3107 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3108 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3109 | |
| 3110 | # no C99 // comments |
| 3111 | if ($line =~ m{//}) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3112 | if (ERROR("C99_COMMENTS", |
| 3113 | "do not use C99 // comments\n" . $herecurr) && |
| 3114 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3115 | my $line = $fixed[$fixlinenr]; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3116 | if ($line =~ /\/\/(.*)$/) { |
| 3117 | my $comment = trim($1); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3118 | $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3119 | } |
| 3120 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3121 | } |
| 3122 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3123 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3124 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3125 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3126 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 3127 | # the whole statement. |
| 3128 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 3129 | if (defined $realline_next && |
| 3130 | exists $lines[$realline_next - 1] && |
| 3131 | !defined $suppress_export{$realline_next} && |
| 3132 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 3133 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 3134 | # Handle definitions which produce identifiers with |
| 3135 | # a prefix: |
| 3136 | # XXX(foo); |
| 3137 | # EXPORT_SYMBOL(something_foo); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3138 | my $name = $1; |
Andy Whitcroft | 87a5387 | 2012-01-10 15:10:04 -0800 | [diff] [blame] | 3139 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 3140 | $name =~ /^${Ident}_$2/) { |
| 3141 | #print "FOO C name<$name>\n"; |
| 3142 | $suppress_export{$realline_next} = 1; |
| 3143 | |
| 3144 | } elsif ($stat !~ /(?: |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3145 | \n.}\s*$| |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3146 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 3147 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 3148 | ^.LIST_HEAD\(\Q$name\E\)| |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3149 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 3150 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3151 | )/x) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3152 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 3153 | $suppress_export{$realline_next} = 2; |
| 3154 | } else { |
| 3155 | $suppress_export{$realline_next} = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3156 | } |
| 3157 | } |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3158 | if (!defined $suppress_export{$linenr} && |
| 3159 | $prevline =~ /^.\s*$/ && |
| 3160 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 3161 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 3162 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 3163 | $suppress_export{$linenr} = 2; |
| 3164 | } |
| 3165 | if (defined $suppress_export{$linenr} && |
| 3166 | $suppress_export{$linenr} == 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3167 | WARN("EXPORT_SYMBOL", |
| 3168 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3169 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3170 | |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 3171 | # check for global initialisers. |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3172 | if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) { |
| 3173 | if (ERROR("GLOBAL_INITIALISERS", |
| 3174 | "do not initialise globals to 0 or NULL\n" . |
| 3175 | $herecurr) && |
| 3176 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3177 | $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3178 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3179 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3180 | # check for static initialisers. |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3181 | if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { |
| 3182 | if (ERROR("INITIALISED_STATIC", |
| 3183 | "do not initialise statics to 0 or NULL\n" . |
| 3184 | $herecurr) && |
| 3185 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3186 | $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3187 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3188 | } |
| 3189 | |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 3190 | # check for misordered declarations of char/short/int/long with signed/unsigned |
| 3191 | while ($sline =~ m{(\b$TypeMisordered\b)}g) { |
| 3192 | my $tmp = trim($1); |
| 3193 | WARN("MISORDERED_TYPE", |
| 3194 | "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); |
| 3195 | } |
| 3196 | |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 3197 | # check for static const char * arrays. |
| 3198 | 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] | 3199 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 3200 | "static const char * array should probably be static const char * const\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 3201 | $herecurr); |
| 3202 | } |
| 3203 | |
| 3204 | # check for static char foo[] = "bar" declarations. |
| 3205 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3206 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 3207 | "static char array declaration should probably be static const char\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 3208 | $herecurr); |
| 3209 | } |
| 3210 | |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 3211 | # check for const <foo> const where <foo> is not a pointer or array type |
| 3212 | if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { |
| 3213 | my $found = $1; |
| 3214 | if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { |
| 3215 | WARN("CONST_CONST", |
| 3216 | "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); |
| 3217 | } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { |
| 3218 | WARN("CONST_CONST", |
| 3219 | "'const $found const' should probably be 'const $found'\n" . $herecurr); |
| 3220 | } |
| 3221 | } |
| 3222 | |
Joe Perches | 9b0fa60 | 2014-04-03 14:49:18 -0700 | [diff] [blame] | 3223 | # check for non-global char *foo[] = {"bar", ...} declarations. |
| 3224 | if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { |
| 3225 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 3226 | "char * array declaration might be better as static const\n" . |
| 3227 | $herecurr); |
| 3228 | } |
| 3229 | |
Joe Perches | b598b67 | 2015-04-16 12:44:36 -0700 | [diff] [blame] | 3230 | # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) |
| 3231 | if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { |
| 3232 | my $array = $1; |
| 3233 | if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) { |
| 3234 | my $array_div = $1; |
| 3235 | if (WARN("ARRAY_SIZE", |
| 3236 | "Prefer ARRAY_SIZE($array)\n" . $herecurr) && |
| 3237 | $fix) { |
| 3238 | $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; |
| 3239 | } |
| 3240 | } |
| 3241 | } |
| 3242 | |
Joe Perches | b36190c | 2014-01-27 17:07:18 -0800 | [diff] [blame] | 3243 | # check for function declarations without arguments like "int foo()" |
| 3244 | if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { |
| 3245 | if (ERROR("FUNCTION_WITHOUT_ARGS", |
| 3246 | "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && |
| 3247 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3248 | $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; |
Joe Perches | b36190c | 2014-01-27 17:07:18 -0800 | [diff] [blame] | 3249 | } |
| 3250 | } |
| 3251 | |
Joe Perches | 92e112f | 2013-12-13 11:36:22 -0700 | [diff] [blame] | 3252 | # check for uses of DEFINE_PCI_DEVICE_TABLE |
| 3253 | if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { |
| 3254 | if (WARN("DEFINE_PCI_DEVICE_TABLE", |
| 3255 | "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && |
| 3256 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3257 | $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /; |
Joe Perches | 92e112f | 2013-12-13 11:36:22 -0700 | [diff] [blame] | 3258 | } |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 3259 | } |
| 3260 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3261 | # check for new typedefs, only function parameters and sparse annotations |
| 3262 | # make sense. |
| 3263 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 8054576 | 2009-01-06 14:41:26 -0800 | [diff] [blame] | 3264 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3265 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 3266 | $line !~ /\b$typeTypedefs\b/ && |
Joe Perches | 021158b | 2015-02-13 14:38:43 -0800 | [diff] [blame] | 3267 | $line !~ /\b$typeOtherOSTypedefs\b/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3268 | $line !~ /\b__bitwise(?:__|)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3269 | WARN("NEW_TYPEDEFS", |
| 3270 | "do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3274 | # (char*[ const]) |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 3275 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 3276 | #print "AA<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3277 | my ($ident, $from, $to) = ($1, $2, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3278 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3279 | # Should start with a space. |
| 3280 | $to =~ s/^(\S)/ $1/; |
| 3281 | # Should not end with a space. |
| 3282 | $to =~ s/\s+$//; |
| 3283 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 3284 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3285 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3286 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3287 | ## print "1: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3288 | if ($from ne $to) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3289 | if (ERROR("POINTER_LOCATION", |
| 3290 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && |
| 3291 | $fix) { |
| 3292 | my $sub_from = $ident; |
| 3293 | my $sub_to = $ident; |
| 3294 | $sub_to =~ s/\Q$from\E/$to/; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3295 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3296 | s@\Q$sub_from\E@$sub_to@; |
| 3297 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3298 | } |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 3299 | } |
| 3300 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 3301 | #print "BB<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3302 | my ($match, $from, $to, $ident) = ($1, $2, $2, $3); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3303 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3304 | # Should start with a space. |
| 3305 | $to =~ s/^(\S)/ $1/; |
| 3306 | # Should not end with a space. |
| 3307 | $to =~ s/\s+$//; |
| 3308 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 3309 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3310 | } |
| 3311 | # Modifiers should have spaces. |
| 3312 | $to =~ s/(\b$Modifier$)/$1 /; |
| 3313 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3314 | ## print "2: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 667026e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 3315 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3316 | if (ERROR("POINTER_LOCATION", |
| 3317 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && |
| 3318 | $fix) { |
| 3319 | |
| 3320 | my $sub_from = $match; |
| 3321 | my $sub_to = $match; |
| 3322 | $sub_to =~ s/\Q$from\E/$to/; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3323 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3324 | s@\Q$sub_from\E@$sub_to@; |
| 3325 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3326 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | # # no BUG() or BUG_ON() |
| 3330 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 3331 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 3332 | # print "$herecurr"; |
| 3333 | # $clean = 0; |
| 3334 | # } |
| 3335 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3336 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3337 | WARN("LINUX_VERSION_CODE", |
| 3338 | "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] | 3339 | } |
| 3340 | |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 3341 | # check for uses of printk_ratelimit |
| 3342 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3343 | WARN("PRINTK_RATELIMITED", |
Joe Perches | 101ee68 | 2015-02-13 14:38:54 -0800 | [diff] [blame] | 3344 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 3345 | } |
| 3346 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3347 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 3348 | # same line do not need a level, so we use the current block context |
| 3349 | # to try and find and validate the current printk. In summary the current |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3350 | # printk includes all preceding printk's which have no newline on the end. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3351 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3352 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3353 | my $ok = 0; |
| 3354 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 3355 | #print "CHECK<$lines[$ln - 1]\n"; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3356 | # we have a preceding printk if it ends |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3357 | # with "\n" ignore it, else it is to blame |
| 3358 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 3359 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 3360 | $ok = 1; |
| 3361 | } |
| 3362 | last; |
| 3363 | } |
| 3364 | } |
| 3365 | if ($ok == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3366 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 3367 | "printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3368 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3369 | } |
| 3370 | |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 3371 | if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { |
| 3372 | my $orig = $1; |
| 3373 | my $level = lc($orig); |
| 3374 | $level = "warn" if ($level eq "warning"); |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 3375 | my $level2 = $level; |
| 3376 | $level2 = "dbg" if ($level eq "debug"); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 3377 | WARN("PREFER_PR_LEVEL", |
Yogesh Chaudhari | daa8b05 | 2014-04-03 14:49:23 -0700 | [diff] [blame] | 3378 | "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] | 3379 | } |
| 3380 | |
| 3381 | if ($line =~ /\bpr_warning\s*\(/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3382 | if (WARN("PREFER_PR_LEVEL", |
| 3383 | "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && |
| 3384 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3385 | $fixed[$fixlinenr] =~ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3386 | s/\bpr_warning\b/pr_warn/; |
| 3387 | } |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 3388 | } |
| 3389 | |
Joe Perches | dc13931 | 2013-02-21 16:44:13 -0800 | [diff] [blame] | 3390 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 3391 | my $orig = $1; |
| 3392 | my $level = lc($orig); |
| 3393 | $level = "warn" if ($level eq "warning"); |
| 3394 | $level = "dbg" if ($level eq "debug"); |
| 3395 | WARN("PREFER_DEV_LEVEL", |
| 3396 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 3397 | } |
| 3398 | |
Andy Lutomirski | 91c9afa | 2015-04-16 12:44:44 -0700 | [diff] [blame] | 3399 | # ENOSYS means "bad syscall nr" and nothing else. This will have a small |
| 3400 | # number of false positives, but assembly files are not checked, so at |
| 3401 | # least the arch entry code will not trigger this warning. |
| 3402 | if ($line =~ /\bENOSYS\b/) { |
| 3403 | WARN("ENOSYS", |
| 3404 | "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); |
| 3405 | } |
| 3406 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3407 | # function brace can't be on same line, except for #defines of do while, |
| 3408 | # or if closed on same line |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 3409 | if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3410 | !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 3411 | if (ERROR("OPEN_BRACE", |
| 3412 | "open brace '{' following function declarations go on the next line\n" . $herecurr) && |
| 3413 | $fix) { |
| 3414 | fix_delete_line($fixlinenr, $rawline); |
| 3415 | my $fixed_line = $rawline; |
| 3416 | $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; |
| 3417 | my $line1 = $1; |
| 3418 | my $line2 = $2; |
| 3419 | fix_insert_line($fixlinenr, ltrim($line1)); |
| 3420 | fix_insert_line($fixlinenr, "\+{"); |
| 3421 | if ($line2 !~ /^\s*$/) { |
| 3422 | fix_insert_line($fixlinenr, "\+\t" . trim($line2)); |
| 3423 | } |
| 3424 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3425 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3426 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3427 | # open braces for enum, union and struct go on the same line. |
| 3428 | if ($line =~ /^.\s*{/ && |
| 3429 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 3430 | if (ERROR("OPEN_BRACE", |
| 3431 | "open brace '{' following $1 go on the same line\n" . $hereprev) && |
| 3432 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 3433 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 3434 | fix_delete_line($fixlinenr, $rawline); |
| 3435 | my $fixedline = rtrim($prevrawline) . " {"; |
| 3436 | fix_insert_line($fixlinenr, $fixedline); |
| 3437 | $fixedline = $rawline; |
| 3438 | $fixedline =~ s/^(.\s*){\s*/$1\t/; |
| 3439 | if ($fixedline !~ /^\+\s*$/) { |
| 3440 | fix_insert_line($fixlinenr, $fixedline); |
| 3441 | } |
| 3442 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3443 | } |
| 3444 | |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3445 | # missing space after union, struct or enum definition |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3446 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { |
| 3447 | if (WARN("SPACING", |
| 3448 | "missing space after $1 definition\n" . $herecurr) && |
| 3449 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3450 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3451 | s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; |
| 3452 | } |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3453 | } |
| 3454 | |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3455 | # Function pointer declarations |
| 3456 | # check spacing between type, funcptr, and args |
| 3457 | # canonical declaration is "type (*funcptr)(args...)" |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3458 | if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3459 | my $declare = $1; |
| 3460 | my $pre_pointer_space = $2; |
| 3461 | my $post_pointer_space = $3; |
| 3462 | my $funcname = $4; |
| 3463 | my $post_funcname_space = $5; |
| 3464 | my $pre_args_space = $6; |
| 3465 | |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3466 | # the $Declare variable will capture all spaces after the type |
| 3467 | # so check it for a missing trailing missing space but pointer return types |
| 3468 | # don't need a space so don't warn for those. |
| 3469 | my $post_declare_space = ""; |
| 3470 | if ($declare =~ /(\s+)$/) { |
| 3471 | $post_declare_space = $1; |
| 3472 | $declare = rtrim($declare); |
| 3473 | } |
| 3474 | if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3475 | WARN("SPACING", |
| 3476 | "missing space after return type\n" . $herecurr); |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3477 | $post_declare_space = " "; |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | # unnecessary space "type (*funcptr)(args...)" |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3481 | # This test is not currently implemented because these declarations are |
| 3482 | # equivalent to |
| 3483 | # int foo(int bar, ...) |
| 3484 | # and this is form shouldn't/doesn't generate a checkpatch warning. |
| 3485 | # |
| 3486 | # elsif ($declare =~ /\s{2,}$/) { |
| 3487 | # WARN("SPACING", |
| 3488 | # "Multiple spaces after return type\n" . $herecurr); |
| 3489 | # } |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3490 | |
| 3491 | # unnecessary space "type ( *funcptr)(args...)" |
| 3492 | if (defined $pre_pointer_space && |
| 3493 | $pre_pointer_space =~ /^\s/) { |
| 3494 | WARN("SPACING", |
| 3495 | "Unnecessary space after function pointer open parenthesis\n" . $herecurr); |
| 3496 | } |
| 3497 | |
| 3498 | # unnecessary space "type (* funcptr)(args...)" |
| 3499 | if (defined $post_pointer_space && |
| 3500 | $post_pointer_space =~ /^\s/) { |
| 3501 | WARN("SPACING", |
| 3502 | "Unnecessary space before function pointer name\n" . $herecurr); |
| 3503 | } |
| 3504 | |
| 3505 | # unnecessary space "type (*funcptr )(args...)" |
| 3506 | if (defined $post_funcname_space && |
| 3507 | $post_funcname_space =~ /^\s/) { |
| 3508 | WARN("SPACING", |
| 3509 | "Unnecessary space after function pointer name\n" . $herecurr); |
| 3510 | } |
| 3511 | |
| 3512 | # unnecessary space "type (*funcptr) (args...)" |
| 3513 | if (defined $pre_args_space && |
| 3514 | $pre_args_space =~ /^\s/) { |
| 3515 | WARN("SPACING", |
| 3516 | "Unnecessary space before function pointer arguments\n" . $herecurr); |
| 3517 | } |
| 3518 | |
| 3519 | if (show_type("SPACING") && $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3520 | $fixed[$fixlinenr] =~ |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3521 | 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] | 3522 | } |
| 3523 | } |
| 3524 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 3525 | # check for spacing round square brackets; allowed: |
| 3526 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 3527 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 3528 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 3529 | while ($line =~ /(.*?\s)\[/g) { |
| 3530 | my ($where, $prefix) = ($-[1], $1); |
| 3531 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 3532 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Andy Whitcroft | daebc53 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3533 | $prefix !~ /[{,]\s+$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3534 | if (ERROR("BRACKET_SPACE", |
| 3535 | "space prohibited before open square bracket '['\n" . $herecurr) && |
| 3536 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3537 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3538 | s/^(\+.*?)\s+\[/$1\[/; |
| 3539 | } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 3540 | } |
| 3541 | } |
| 3542 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3543 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3544 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3545 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3546 | my $ctx_before = substr($line, 0, $-[1]); |
| 3547 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3548 | |
| 3549 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3550 | if ($name =~ /^(?: |
| 3551 | if|for|while|switch|return|case| |
| 3552 | volatile|__volatile__| |
| 3553 | __attribute__|format|__extension__| |
| 3554 | asm|__asm__)$/x) |
| 3555 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3556 | # cpp #define statements have non-optional spaces, ie |
| 3557 | # if there is a space between the name and the open |
| 3558 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3559 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3560 | |
| 3561 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3562 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3563 | |
| 3564 | # If this whole things ends with a type its most |
| 3565 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3566 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3567 | |
| 3568 | } else { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3569 | if (WARN("SPACING", |
| 3570 | "space prohibited between function name and open parenthesis '('\n" . $herecurr) && |
| 3571 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3572 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3573 | s/\b$name\s+\(/$name\(/; |
| 3574 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3575 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3576 | } |
Eric Nelson | 9a4cad4 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 3577 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3578 | # Check operator spacing. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3579 | if (!($line=~/\#\s*include/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3580 | my $fixed_line = ""; |
| 3581 | my $line_fixed = 0; |
| 3582 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3583 | my $ops = qr{ |
| 3584 | <<=|>>=|<=|>=|==|!=| |
| 3585 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 3586 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3587 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 3588 | \?:|\?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3589 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3590 | my @elements = split(/($ops|;)/, $opline); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3591 | |
| 3592 | ## print("element count: <" . $#elements . ">\n"); |
| 3593 | ## foreach my $el (@elements) { |
| 3594 | ## print("el: <$el>\n"); |
| 3595 | ## } |
| 3596 | |
| 3597 | my @fix_elements = (); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3598 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3599 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3600 | foreach my $el (@elements) { |
| 3601 | push(@fix_elements, substr($rawline, $off, length($el))); |
| 3602 | $off += length($el); |
| 3603 | } |
| 3604 | |
| 3605 | $off = 0; |
| 3606 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3607 | my $blank = copy_spacing($opline); |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3608 | my $last_after = -1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3609 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3610 | for (my $n = 0; $n < $#elements; $n += 2) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3611 | |
| 3612 | my $good = $fix_elements[$n] . $fix_elements[$n + 1]; |
| 3613 | |
| 3614 | ## print("n: <$n> good: <$good>\n"); |
| 3615 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3616 | $off += length($elements[$n]); |
| 3617 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3618 | # Pick up the preceding and succeeding characters. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3619 | my $ca = substr($opline, 0, $off); |
| 3620 | my $cc = ''; |
| 3621 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 3622 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 3623 | } |
| 3624 | my $cb = "$ca$;$cc"; |
| 3625 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3626 | my $a = ''; |
| 3627 | $a = 'V' if ($elements[$n] ne ''); |
| 3628 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3629 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3630 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 3631 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3632 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3633 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3634 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3635 | |
| 3636 | my $c = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3637 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3638 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 3639 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3640 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3641 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 3642 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 8b1b337 | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 3643 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3644 | } else { |
| 3645 | $c = 'E'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3646 | } |
| 3647 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3648 | my $ctx = "${a}x${c}"; |
| 3649 | |
| 3650 | my $at = "(ctx:$ctx)"; |
| 3651 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3652 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3653 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3654 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3655 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3656 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3657 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3658 | # Get the full operator variant. |
| 3659 | my $opv = $op . substr($curr_vars, $off, 1); |
| 3660 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3661 | # Ignore operators passed as parameters. |
| 3662 | if ($op_type ne 'V' && |
Sam Bobroff | d7fe806 | 2015-04-16 12:44:39 -0700 | [diff] [blame] | 3663 | $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3664 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3665 | # # Ignore comments |
| 3666 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3667 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3668 | # ; 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] | 3669 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3670 | if ($ctx !~ /.x[WEBC]/ && |
| 3671 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3672 | if (ERROR("SPACING", |
| 3673 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3674 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3675 | $line_fixed = 1; |
| 3676 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | # // is a comment |
| 3680 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3681 | |
Joe Perches | b00e481 | 2014-04-03 14:49:33 -0700 | [diff] [blame] | 3682 | # : when part of a bitfield |
| 3683 | } elsif ($opv eq ':B') { |
| 3684 | # skip the bitfield test for now |
| 3685 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3686 | # No spaces for: |
| 3687 | # -> |
Joe Perches | b00e481 | 2014-04-03 14:49:33 -0700 | [diff] [blame] | 3688 | } elsif ($op eq '->') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3689 | if ($ctx =~ /Wx.|.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3690 | if (ERROR("SPACING", |
| 3691 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3692 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3693 | if (defined $fix_elements[$n + 2]) { |
| 3694 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3695 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3696 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3697 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3698 | } |
| 3699 | |
Joe Perches | 2381097 | 2014-12-10 15:51:32 -0800 | [diff] [blame] | 3700 | # , 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] | 3701 | } elsif ($op eq ',') { |
Joe Perches | 2381097 | 2014-12-10 15:51:32 -0800 | [diff] [blame] | 3702 | my $rtrim_before = 0; |
| 3703 | my $space_after = 0; |
| 3704 | if ($ctx =~ /Wx./) { |
| 3705 | if (ERROR("SPACING", |
| 3706 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 3707 | $line_fixed = 1; |
| 3708 | $rtrim_before = 1; |
| 3709 | } |
| 3710 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3711 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3712 | if (ERROR("SPACING", |
| 3713 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3714 | $line_fixed = 1; |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3715 | $last_after = $n; |
Joe Perches | 2381097 | 2014-12-10 15:51:32 -0800 | [diff] [blame] | 3716 | $space_after = 1; |
| 3717 | } |
| 3718 | } |
| 3719 | if ($rtrim_before || $space_after) { |
| 3720 | if ($rtrim_before) { |
| 3721 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 3722 | } else { |
| 3723 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
| 3724 | } |
| 3725 | if ($space_after) { |
| 3726 | $good .= " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3727 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3728 | } |
| 3729 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3730 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3731 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3732 | #warn "'*' is part of type\n"; |
| 3733 | |
| 3734 | # unary operators should have a space before and |
| 3735 | # none after. May be left adjacent to another |
| 3736 | # unary operator, or a cast |
| 3737 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3738 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 3739 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3740 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3741 | if (ERROR("SPACING", |
| 3742 | "space required before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3743 | if ($n != $last_after + 2) { |
| 3744 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
| 3745 | $line_fixed = 1; |
| 3746 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3747 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3748 | } |
Andy Whitcroft | a3340b3 | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 3749 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3750 | # A unary '*' may be const |
| 3751 | |
| 3752 | } elsif ($ctx =~ /.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3753 | if (ERROR("SPACING", |
| 3754 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3755 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3756 | if (defined $fix_elements[$n + 2]) { |
| 3757 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3758 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3759 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3760 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | # unary ++ and unary -- are allowed no space on one side. |
| 3764 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3765 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3766 | if (ERROR("SPACING", |
| 3767 | "space required one side of that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3768 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3769 | $line_fixed = 1; |
| 3770 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3771 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3772 | if ($ctx =~ /Wx[BE]/ || |
| 3773 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3774 | if (ERROR("SPACING", |
| 3775 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3776 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3777 | $line_fixed = 1; |
| 3778 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3779 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3780 | if ($ctx =~ /ExW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3781 | if (ERROR("SPACING", |
| 3782 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3783 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3784 | if (defined $fix_elements[$n + 2]) { |
| 3785 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3786 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3787 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3788 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3789 | } |
| 3790 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3791 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3792 | } elsif ($op eq '<<' or $op eq '>>' or |
| 3793 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 3794 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3795 | $op eq '*' or $op eq '/' or |
| 3796 | $op eq '%') |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3797 | { |
Joe Perches | d2e025f | 2015-02-13 14:38:57 -0800 | [diff] [blame] | 3798 | if ($check) { |
| 3799 | if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { |
| 3800 | if (CHK("SPACING", |
| 3801 | "spaces preferred around that '$op' $at\n" . $hereptr)) { |
| 3802 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3803 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3804 | $line_fixed = 1; |
| 3805 | } |
| 3806 | } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { |
| 3807 | if (CHK("SPACING", |
| 3808 | "space preferred before that '$op' $at\n" . $hereptr)) { |
| 3809 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); |
| 3810 | $line_fixed = 1; |
| 3811 | } |
| 3812 | } |
| 3813 | } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3814 | if (ERROR("SPACING", |
| 3815 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3816 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3817 | if (defined $fix_elements[$n + 2]) { |
| 3818 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3819 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3820 | $line_fixed = 1; |
| 3821 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3824 | # A colon needs no spaces before when it is |
| 3825 | # terminating a case value or a label. |
| 3826 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 3827 | if ($ctx =~ /Wx./) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3828 | if (ERROR("SPACING", |
| 3829 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3830 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3831 | $line_fixed = 1; |
| 3832 | } |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3833 | } |
| 3834 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3835 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3836 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3837 | my $ok = 0; |
| 3838 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3839 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3840 | if (($op eq '<' && |
| 3841 | $cc =~ /^\S+\@\S+>/) || |
| 3842 | ($op eq '>' && |
| 3843 | $ca =~ /<\S+\@\S+$/)) |
| 3844 | { |
| 3845 | $ok = 1; |
| 3846 | } |
| 3847 | |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 3848 | # messages are ERROR, but ?: are CHK |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3849 | if ($ok == 0) { |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 3850 | my $msg_type = \&ERROR; |
| 3851 | $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); |
| 3852 | |
| 3853 | if (&{$msg_type}("SPACING", |
| 3854 | "spaces required around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3855 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3856 | if (defined $fix_elements[$n + 2]) { |
| 3857 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3858 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3859 | $line_fixed = 1; |
| 3860 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3861 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3862 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3863 | $off += length($elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3864 | |
| 3865 | ## print("n: <$n> GOOD: <$good>\n"); |
| 3866 | |
| 3867 | $fixed_line = $fixed_line . $good; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3868 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3869 | |
| 3870 | if (($#elements % 2) == 0) { |
| 3871 | $fixed_line = $fixed_line . $fix_elements[$#elements]; |
| 3872 | } |
| 3873 | |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3874 | if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { |
| 3875 | $fixed[$fixlinenr] = $fixed_line; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3879 | } |
| 3880 | |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 3881 | # check for whitespace before a non-naked semicolon |
Joe Perches | d2e248e | 2014-01-23 15:54:41 -0800 | [diff] [blame] | 3882 | if ($line =~ /^\+.*\S\s+;\s*$/) { |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 3883 | if (WARN("SPACING", |
| 3884 | "space prohibited before semicolon\n" . $herecurr) && |
| 3885 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3886 | 1 while $fixed[$fixlinenr] =~ |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 3887 | s/^(\+.*\S)\s+;/$1;/; |
| 3888 | } |
| 3889 | } |
| 3890 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3891 | # check for multiple assignments |
| 3892 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3893 | CHK("MULTIPLE_ASSIGNMENTS", |
| 3894 | "multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3895 | } |
| 3896 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3897 | ## # check for multiple declarations, allowing for a function declaration |
| 3898 | ## # continuation. |
| 3899 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 3900 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 3901 | ## |
| 3902 | ## # Remove any bracketed sections to ensure we do not |
| 3903 | ## # falsly report the parameters of functions. |
| 3904 | ## my $ln = $line; |
| 3905 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 3906 | ## } |
| 3907 | ## if ($ln =~ /,/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3908 | ## WARN("MULTIPLE_DECLARATION", |
| 3909 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3910 | ## } |
| 3911 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3912 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3913 | #need space before brace following if, while, etc |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3914 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 3915 | $line =~ /do{/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3916 | if (ERROR("SPACING", |
| 3917 | "space required before the open brace '{'\n" . $herecurr) && |
| 3918 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3919 | $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3920 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3921 | } |
| 3922 | |
Joe Perches | c4a62ef | 2013-07-03 15:05:28 -0700 | [diff] [blame] | 3923 | ## # check for blank lines before declarations |
| 3924 | ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && |
| 3925 | ## $prevrawline =~ /^.\s*$/) { |
| 3926 | ## WARN("SPACING", |
| 3927 | ## "No blank lines before declarations\n" . $hereprev); |
| 3928 | ## } |
| 3929 | ## |
| 3930 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3931 | # closing brace should have a space following it when it has anything |
| 3932 | # on the line |
| 3933 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3934 | if (ERROR("SPACING", |
| 3935 | "space required after that close brace '}'\n" . $herecurr) && |
| 3936 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3937 | $fixed[$fixlinenr] =~ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3938 | s/}((?!(?:,|;|\)))\S)/} $1/; |
| 3939 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3940 | } |
| 3941 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3942 | # check spacing on square brackets |
| 3943 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3944 | if (ERROR("SPACING", |
| 3945 | "space prohibited after that open square bracket '['\n" . $herecurr) && |
| 3946 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3947 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3948 | s/\[\s+/\[/; |
| 3949 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3950 | } |
| 3951 | if ($line =~ /\s\]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3952 | if (ERROR("SPACING", |
| 3953 | "space prohibited before that close square bracket ']'\n" . $herecurr) && |
| 3954 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3955 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3956 | s/\s+\]/\]/; |
| 3957 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3958 | } |
| 3959 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3960 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3961 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 3962 | $line !~ /for\s*\(\s+;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3963 | if (ERROR("SPACING", |
| 3964 | "space prohibited after that open parenthesis '('\n" . $herecurr) && |
| 3965 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3966 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3967 | s/\(\s+/\(/; |
| 3968 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3969 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3970 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3971 | $line !~ /for\s*\(.*;\s+\)/ && |
| 3972 | $line !~ /:\s+\)/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3973 | if (ERROR("SPACING", |
| 3974 | "space prohibited before that close parenthesis ')'\n" . $herecurr) && |
| 3975 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3976 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3977 | s/\s+\)/\)/; |
| 3978 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
Joe Perches | e2826fd | 2014-08-06 16:10:48 -0700 | [diff] [blame] | 3981 | # check unnecessary parentheses around addressof/dereference single $Lvals |
| 3982 | # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar |
| 3983 | |
| 3984 | while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { |
Joe Perches | ea4acbb | 2014-12-10 15:51:51 -0800 | [diff] [blame] | 3985 | my $var = $1; |
| 3986 | if (CHK("UNNECESSARY_PARENTHESES", |
| 3987 | "Unnecessary parentheses around $var\n" . $herecurr) && |
| 3988 | $fix) { |
| 3989 | $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; |
| 3990 | } |
| 3991 | } |
| 3992 | |
| 3993 | # check for unnecessary parentheses around function pointer uses |
| 3994 | # ie: (foo->bar)(); should be foo->bar(); |
| 3995 | # but not "if (foo->bar) (" to avoid some false positives |
| 3996 | if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { |
| 3997 | my $var = $2; |
| 3998 | if (CHK("UNNECESSARY_PARENTHESES", |
| 3999 | "Unnecessary parentheses around function pointer $var\n" . $herecurr) && |
| 4000 | $fix) { |
| 4001 | my $var2 = deparenthesize($var); |
| 4002 | $var2 =~ s/\s//g; |
| 4003 | $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; |
| 4004 | } |
| 4005 | } |
Joe Perches | e2826fd | 2014-08-06 16:10:48 -0700 | [diff] [blame] | 4006 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4007 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4008 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4009 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4010 | if (WARN("INDENTED_LABEL", |
| 4011 | "labels should not be indented\n" . $herecurr) && |
| 4012 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4013 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4014 | s/^(.)\s+/$1/; |
| 4015 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 4018 | # return is not a function |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 4019 | if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4020 | my $spacing = $1; |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 4021 | if ($^V && $^V ge 5.10.0 && |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 4022 | $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { |
| 4023 | my $value = $1; |
| 4024 | $value = deparenthesize($value); |
| 4025 | if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { |
| 4026 | ERROR("RETURN_PARENTHESES", |
| 4027 | "return is not a function, parentheses are not required\n" . $herecurr); |
| 4028 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4029 | } elsif ($spacing !~ /\s+/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4030 | ERROR("SPACING", |
| 4031 | "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4032 | } |
| 4033 | } |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 4034 | |
Joe Perches | b43ae21 | 2014-06-23 13:22:07 -0700 | [diff] [blame] | 4035 | # unnecessary return in a void function |
| 4036 | # at end-of-function, with the previous line a single leading tab, then return; |
| 4037 | # and the line before that not a goto label target like "out:" |
| 4038 | if ($sline =~ /^[ \+]}\s*$/ && |
| 4039 | $prevline =~ /^\+\treturn\s*;\s*$/ && |
| 4040 | $linenr >= 3 && |
| 4041 | $lines[$linenr - 3] =~ /^[ +]/ && |
| 4042 | $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { |
Joe Perches | 9819cf2 | 2014-06-04 16:12:09 -0700 | [diff] [blame] | 4043 | WARN("RETURN_VOID", |
Joe Perches | b43ae21 | 2014-06-23 13:22:07 -0700 | [diff] [blame] | 4044 | "void function return statements are not generally useful\n" . $hereprev); |
| 4045 | } |
Joe Perches | 9819cf2 | 2014-06-04 16:12:09 -0700 | [diff] [blame] | 4046 | |
Joe Perches | 189248d | 2014-01-23 15:54:47 -0800 | [diff] [blame] | 4047 | # if statements using unnecessary parentheses - ie: if ((foo == bar)) |
| 4048 | if ($^V && $^V ge 5.10.0 && |
| 4049 | $line =~ /\bif\s*((?:\(\s*){2,})/) { |
| 4050 | my $openparens = $1; |
| 4051 | my $count = $openparens =~ tr@\(@\(@; |
| 4052 | my $msg = ""; |
| 4053 | if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { |
| 4054 | my $comp = $4; #Not $1 because of $LvalOrFunc |
| 4055 | $msg = " - maybe == should be = ?" if ($comp eq "=="); |
| 4056 | WARN("UNNECESSARY_PARENTHESES", |
| 4057 | "Unnecessary parentheses$msg\n" . $herecurr); |
| 4058 | } |
| 4059 | } |
| 4060 | |
Joe Perches | f34e4a4 | 2015-04-16 12:44:19 -0700 | [diff] [blame] | 4061 | # Return of what appears to be an errno should normally be negative |
| 4062 | if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 4063 | my $name = $1; |
| 4064 | if ($name ne 'EOF' && $name ne 'ERROR') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4065 | WARN("USE_NEGATIVE_ERRNO", |
Joe Perches | f34e4a4 | 2015-04-16 12:44:19 -0700 | [diff] [blame] | 4066 | "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] | 4067 | } |
| 4068 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4069 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4070 | # Need a space before open parenthesis after if, while etc |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4071 | if ($line =~ /\b(if|while|for|switch)\(/) { |
| 4072 | if (ERROR("SPACING", |
| 4073 | "space required before the open parenthesis '('\n" . $herecurr) && |
| 4074 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4075 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4076 | s/\b(if|while|for|switch)\(/$1 \(/; |
| 4077 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4078 | } |
| 4079 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 4080 | # Check for illegal assignment in if conditional -- and check for trailing |
| 4081 | # statements after the conditional. |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 4082 | if ($line =~ /do\s*(?!{)/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 4083 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 4084 | ctx_statement_block($linenr, $realcnt, 0) |
| 4085 | if (!defined $stat); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 4086 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 4087 | $remain_next, $off_next); |
| 4088 | $stat_next =~ s/\n./\n /g; |
| 4089 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 4090 | |
| 4091 | if ($stat_next =~ /^\s*while\b/) { |
| 4092 | # If the statement carries leading newlines, |
| 4093 | # then count those as offsets. |
| 4094 | my ($whitespace) = |
| 4095 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 4096 | my $offset = |
| 4097 | statement_rawlines($whitespace) - 1; |
| 4098 | |
| 4099 | $suppress_whiletrailers{$line_nr_next + |
| 4100 | $offset} = 1; |
| 4101 | } |
| 4102 | } |
| 4103 | if (!defined $suppress_whiletrailers{$linenr} && |
Joe Perches | c11230f | 2013-11-21 14:31:57 -0800 | [diff] [blame] | 4104 | defined($stat) && defined($cond) && |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 4105 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4106 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4107 | |
Andy Whitcroft | b53c8e1 | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 4108 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4109 | ERROR("ASSIGN_IN_IF", |
| 4110 | "do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | # Find out what is on the end of the line after the |
| 4114 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4115 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4116 | $s =~ s/\n.*//g; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4117 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 4118 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 4119 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4120 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 4121 | # Find out how long the conditional actually is. |
| 4122 | my @newlines = ($c =~ /\n/gs); |
| 4123 | my $cond_lines = 1 + $#newlines; |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 4124 | my $stat_real = ''; |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 4125 | |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 4126 | $stat_real = raw_line($linenr, $cond_lines) |
| 4127 | . "\n" if ($cond_lines); |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 4128 | if (defined($stat_real) && $cond_lines > 1) { |
| 4129 | $stat_real = "[...]\n$stat_real"; |
| 4130 | } |
| 4131 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4132 | ERROR("TRAILING_STATEMENTS", |
| 4133 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4134 | } |
| 4135 | } |
| 4136 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4137 | # Check for bitwise tests written as boolean |
| 4138 | if ($line =~ / |
| 4139 | (?: |
| 4140 | (?:\[|\(|\&\&|\|\|) |
| 4141 | \s*0[xX][0-9]+\s* |
| 4142 | (?:\&\&|\|\|) |
| 4143 | | |
| 4144 | (?:\&\&|\|\|) |
| 4145 | \s*0[xX][0-9]+\s* |
| 4146 | (?:\&\&|\|\||\)|\]) |
| 4147 | )/x) |
| 4148 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4149 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 4150 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4151 | } |
| 4152 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4153 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4154 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 4155 | my $s = $1; |
| 4156 | $s =~ s/$;//g; # Remove any comments |
| 4157 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4158 | ERROR("TRAILING_STATEMENTS", |
| 4159 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4160 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4161 | } |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 4162 | # if should not continue a brace |
| 4163 | if ($line =~ /}\s*if\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4164 | ERROR("TRAILING_STATEMENTS", |
Rasmus Villemoes | 048b123 | 2014-08-06 16:10:37 -0700 | [diff] [blame] | 4165 | "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] | 4166 | $herecurr); |
| 4167 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 4168 | # case and default should not have general statements after them |
| 4169 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 4170 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 4171 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 4172 | \s*return\s+ |
| 4173 | )/xg) |
| 4174 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4175 | ERROR("TRAILING_STATEMENTS", |
| 4176 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 4177 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4178 | |
| 4179 | # Check for }<nl>else {, these must be at the same |
| 4180 | # indent level to be relevant to each other. |
Joe Perches | 8b8856f | 2014-08-06 16:11:14 -0700 | [diff] [blame] | 4181 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && |
| 4182 | $previndent == $indent) { |
| 4183 | if (ERROR("ELSE_AFTER_BRACE", |
| 4184 | "else should follow close brace '}'\n" . $hereprev) && |
| 4185 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4186 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4187 | fix_delete_line($fixlinenr, $rawline); |
| 4188 | my $fixedline = $prevrawline; |
| 4189 | $fixedline =~ s/}\s*$//; |
| 4190 | if ($fixedline !~ /^\+\s*$/) { |
| 4191 | fix_insert_line($fixlinenr, $fixedline); |
| 4192 | } |
| 4193 | $fixedline = $rawline; |
| 4194 | $fixedline =~ s/^(.\s*)else/$1} else/; |
| 4195 | fix_insert_line($fixlinenr, $fixedline); |
| 4196 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4197 | } |
| 4198 | |
Joe Perches | 8b8856f | 2014-08-06 16:11:14 -0700 | [diff] [blame] | 4199 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && |
| 4200 | $previndent == $indent) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4201 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 4202 | |
| 4203 | # Find out what is on the end of the line after the |
| 4204 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4205 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4206 | $s =~ s/\n.*//g; |
| 4207 | |
| 4208 | if ($s =~ /^\s*;/) { |
Joe Perches | 8b8856f | 2014-08-06 16:11:14 -0700 | [diff] [blame] | 4209 | if (ERROR("WHILE_AFTER_BRACE", |
| 4210 | "while should follow close brace '}'\n" . $hereprev) && |
| 4211 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4212 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4213 | fix_delete_line($fixlinenr, $rawline); |
| 4214 | my $fixedline = $prevrawline; |
| 4215 | my $trailing = $rawline; |
| 4216 | $trailing =~ s/^\+//; |
| 4217 | $trailing = trim($trailing); |
| 4218 | $fixedline =~ s/}\s*$/} $trailing/; |
| 4219 | fix_insert_line($fixlinenr, $fixedline); |
| 4220 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4221 | } |
| 4222 | } |
| 4223 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 4224 | #Specific variable tests |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 4225 | while ($line =~ m{($Constant|$Lval)}g) { |
| 4226 | my $var = $1; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 4227 | |
| 4228 | #gcc binary extension |
| 4229 | if ($var =~ /^$Binary$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4230 | if (WARN("GCC_BINARY_CONSTANT", |
| 4231 | "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && |
| 4232 | $fix) { |
| 4233 | my $hexval = sprintf("0x%x", oct($var)); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4234 | $fixed[$fixlinenr] =~ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4235 | s/\b$var\b/$hexval/; |
| 4236 | } |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | #CamelCase |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 4240 | if ($var !~ /^$Constant$/ && |
Joe Perches | be79794 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 4241 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 4242 | #Ignore Page<foo> variants |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 4243 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 4244 | #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) |
Julius Werner | f512357 | 2014-12-10 15:51:54 -0800 | [diff] [blame] | 4245 | $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && |
| 4246 | #Ignore some three character SI units explicitly, like MiB and KHz |
| 4247 | $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 4248 | while ($var =~ m{($Ident)}g) { |
| 4249 | my $word = $1; |
| 4250 | next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 4251 | if ($check) { |
| 4252 | seed_camelcase_includes(); |
| 4253 | if (!$file && !$camelcase_file_seeded) { |
| 4254 | seed_camelcase_file($realfile); |
| 4255 | $camelcase_file_seeded = 1; |
| 4256 | } |
| 4257 | } |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 4258 | if (!defined $camelcase{$word}) { |
| 4259 | $camelcase{$word} = 1; |
| 4260 | CHK("CAMELCASE", |
| 4261 | "Avoid CamelCase: <$word>\n" . $herecurr); |
| 4262 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 4263 | } |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 4264 | } |
| 4265 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4266 | |
| 4267 | #no spaces allowed after \ in define |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4268 | if ($line =~ /\#\s*define.*\\\s+$/) { |
| 4269 | if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 4270 | "Whitespace after \\ makes next lines useless\n" . $herecurr) && |
| 4271 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4272 | $fixed[$fixlinenr] =~ s/\s+$//; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4273 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4274 | } |
| 4275 | |
Fabian Frederick | 0e212e0 | 2015-04-16 12:44:25 -0700 | [diff] [blame] | 4276 | # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes |
| 4277 | # itself <asm/foo.h> (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4278 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 4279 | my $file = "$1.h"; |
| 4280 | my $checkfile = "include/linux/$file"; |
| 4281 | if (-f "$root/$checkfile" && |
| 4282 | $realfile ne $checkfile && |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 4283 | $1 !~ /$allowed_asm_includes/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4284 | { |
Fabian Frederick | 0e212e0 | 2015-04-16 12:44:25 -0700 | [diff] [blame] | 4285 | my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; |
| 4286 | if ($asminclude > 0) { |
| 4287 | if ($realfile =~ m{^arch/}) { |
| 4288 | CHK("ARCH_INCLUDE_LINUX", |
| 4289 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 4290 | } else { |
| 4291 | WARN("INCLUDE_LINUX", |
| 4292 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 4293 | } |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 4294 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4295 | } |
| 4296 | } |
| 4297 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4298 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 4299 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4300 | # in a known good container |
Andy Whitcroft | b8f96a31 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 4301 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 4302 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4303 | my $ln = $linenr; |
| 4304 | my $cnt = $realcnt; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4305 | my ($off, $dstat, $dcond, $rest); |
| 4306 | my $ctx = ''; |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 4307 | my $has_flow_statement = 0; |
| 4308 | my $has_arg_concat = 0; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4309 | ($dstat, $dcond, $ln, $cnt, $off) = |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4310 | ctx_statement_block($linenr, $realcnt, 0); |
| 4311 | $ctx = $dstat; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4312 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 4313 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4314 | |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 4315 | $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); |
| 4316 | $has_arg_concat = 1 if ($ctx =~ /\#\#/); |
| 4317 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4318 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 4319 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4320 | $dstat =~ s/\\\n.//g; |
| 4321 | $dstat =~ s/^\s*//s; |
| 4322 | $dstat =~ s/\s*$//s; |
| 4323 | |
| 4324 | # Flatten any parentheses and braces |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 4325 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 4326 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
Andy Whitcroft | c81769f | 2012-01-10 15:10:10 -0800 | [diff] [blame] | 4327 | $dstat =~ s/\[[^\[\]]*\]/1/) |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 4328 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4329 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4330 | |
Andy Whitcroft | e45bab8 | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 4331 | # Flatten any obvious string concatentation. |
| 4332 | while ($dstat =~ s/("X*")\s*$Ident/$1/ || |
| 4333 | $dstat =~ s/$Ident\s*("X*")/$1/) |
| 4334 | { |
| 4335 | } |
| 4336 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4337 | my $exceptions = qr{ |
| 4338 | $Declare| |
| 4339 | module_param_named| |
Kees Cook | a0a0a7a | 2012-10-04 17:13:38 -0700 | [diff] [blame] | 4340 | MODULE_PARM_DESC| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4341 | DECLARE_PER_CPU| |
| 4342 | DEFINE_PER_CPU| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 4343 | __typeof__\(| |
Stefani Seibold | 22fd2d3 | 2010-03-05 13:43:52 -0800 | [diff] [blame] | 4344 | union| |
| 4345 | struct| |
Andy Whitcroft | ea71a0a | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 4346 | \.$Ident\s*=\s*| |
| 4347 | ^\"|\"$ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4348 | }x; |
Andy Whitcroft | 5eaa20b | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 4349 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4350 | if ($dstat ne '' && |
| 4351 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 4352 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Joe Perches | 3cc4b1c | 2013-07-03 15:05:27 -0700 | [diff] [blame] | 4353 | $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] | 4354 | $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4355 | $dstat !~ /$exceptions/ && |
| 4356 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Joe Perches | e942e2c | 2013-04-17 15:58:26 -0700 | [diff] [blame] | 4357 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
Andy Whitcroft | 72f115f | 2012-01-10 15:10:06 -0800 | [diff] [blame] | 4358 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4359 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 4360 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 4361 | $dstat !~ /^do\s*{/ && # do {... |
Joe Perches | f95a7e6 | 2013-09-11 14:24:00 -0700 | [diff] [blame] | 4362 | $dstat !~ /^\({/ && # ({... |
| 4363 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4364 | { |
| 4365 | $ctx =~ s/\n*$//; |
| 4366 | my $herectx = $here . "\n"; |
| 4367 | my $cnt = statement_rawlines($ctx); |
| 4368 | |
| 4369 | for (my $n = 0; $n < $cnt; $n++) { |
| 4370 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4371 | } |
| 4372 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4373 | if ($dstat =~ /;/) { |
| 4374 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 4375 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 4376 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4377 | ERROR("COMPLEX_MACRO", |
Andrew Morton | 388982b | 2014-10-13 15:51:40 -0700 | [diff] [blame] | 4378 | "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4379 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4380 | } |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 4381 | |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 4382 | # check for macros with flow control, but without ## concatenation |
| 4383 | # ## concatenation is commonly a macro that defines a function so ignore those |
| 4384 | if ($has_flow_statement && !$has_arg_concat) { |
| 4385 | my $herectx = $here . "\n"; |
| 4386 | my $cnt = statement_rawlines($ctx); |
| 4387 | |
| 4388 | for (my $n = 0; $n < $cnt; $n++) { |
| 4389 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 4390 | } |
| 4391 | WARN("MACRO_WITH_FLOW_CONTROL", |
| 4392 | "Macros with flow control statements should be avoided\n" . "$herectx"); |
| 4393 | } |
| 4394 | |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 4395 | # check for line continuations outside of #defines, preprocessor #, and asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 4396 | |
| 4397 | } else { |
| 4398 | if ($prevline !~ /^..*\\$/ && |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 4399 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 4400 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 4401 | $line =~ /^\+.*\\$/) { |
| 4402 | WARN("LINE_CONTINUATIONS", |
| 4403 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 4404 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4405 | } |
| 4406 | |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 4407 | # do {} while (0) macro tests: |
| 4408 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 4409 | # macro should not end with a semicolon |
| 4410 | if ($^V && $^V ge 5.10.0 && |
| 4411 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 4412 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 4413 | my $ln = $linenr; |
| 4414 | my $cnt = $realcnt; |
| 4415 | my ($off, $dstat, $dcond, $rest); |
| 4416 | my $ctx = ''; |
| 4417 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 4418 | ctx_statement_block($linenr, $realcnt, 0); |
| 4419 | $ctx = $dstat; |
| 4420 | |
| 4421 | $dstat =~ s/\\\n.//g; |
Joe Perches | 1b36b20 | 2015-02-13 14:38:32 -0800 | [diff] [blame] | 4422 | $dstat =~ s/$;/ /g; |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 4423 | |
| 4424 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 4425 | my $stmts = $2; |
| 4426 | my $semis = $3; |
| 4427 | |
| 4428 | $ctx =~ s/\n*$//; |
| 4429 | my $cnt = statement_rawlines($ctx); |
| 4430 | my $herectx = $here . "\n"; |
| 4431 | |
| 4432 | for (my $n = 0; $n < $cnt; $n++) { |
| 4433 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 4434 | } |
| 4435 | |
Joe Perches | ac8e97f | 2012-08-21 16:15:53 -0700 | [diff] [blame] | 4436 | if (($stmts =~ tr/;/;/) == 1 && |
| 4437 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 4438 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 4439 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 4440 | } |
| 4441 | if (defined $semis && $semis ne "") { |
| 4442 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 4443 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
| 4444 | } |
Joe Perches | f5ef95b | 2014-06-04 16:12:06 -0700 | [diff] [blame] | 4445 | } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { |
| 4446 | $ctx =~ s/\n*$//; |
| 4447 | my $cnt = statement_rawlines($ctx); |
| 4448 | my $herectx = $here . "\n"; |
| 4449 | |
| 4450 | for (my $n = 0; $n < $cnt; $n++) { |
| 4451 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 4452 | } |
| 4453 | |
| 4454 | WARN("TRAILING_SEMICOLON", |
| 4455 | "macros should not use a trailing semicolon\n" . "$herectx"); |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 4456 | } |
| 4457 | } |
| 4458 | |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 4459 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 4460 | # all assignments may have only one of the following with an assignment: |
| 4461 | # . |
| 4462 | # ALIGN(...) |
| 4463 | # VMLINUX_SYMBOL(...) |
| 4464 | if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4465 | WARN("MISSING_VMLINUX_SYMBOL", |
| 4466 | "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 4467 | } |
| 4468 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4469 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4470 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 4471 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4472 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4473 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4474 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 4475 | if ($#chunks > 0 && $level == 0) { |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4476 | my @allowed = (); |
| 4477 | my $allow = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4478 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4479 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4480 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4481 | for my $chunk (@chunks) { |
| 4482 | my ($cond, $block) = @{$chunk}; |
| 4483 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4484 | # If the condition carries leading newlines, then count those as offsets. |
| 4485 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 4486 | my $offset = statement_rawlines($whitespace) - 1; |
| 4487 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4488 | $allowed[$allow] = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4489 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 4490 | |
| 4491 | # We have looked at and allowed this specific line. |
| 4492 | $suppress_ifbraces{$ln + $offset} = 1; |
| 4493 | |
| 4494 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4495 | $ln += statement_rawlines($block) - 1; |
| 4496 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4497 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4498 | |
| 4499 | $seen++ if ($block =~ /^\s*{/); |
| 4500 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4501 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4502 | if (statement_lines($cond) > 1) { |
| 4503 | #print "APW: ALLOWED: cond<$cond>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4504 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4505 | } |
| 4506 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4507 | #print "APW: ALLOWED: block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4508 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4509 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4510 | if (statement_block_size($block) > 1) { |
| 4511 | #print "APW: ALLOWED: lines block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4512 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4513 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4514 | $allow++; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4515 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4516 | if ($seen) { |
| 4517 | my $sum_allowed = 0; |
| 4518 | foreach (@allowed) { |
| 4519 | $sum_allowed += $_; |
| 4520 | } |
| 4521 | if ($sum_allowed == 0) { |
| 4522 | WARN("BRACES", |
| 4523 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 4524 | } elsif ($sum_allowed != $allow && |
| 4525 | $seen != $allow) { |
| 4526 | CHK("BRACES", |
| 4527 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 4528 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4529 | } |
| 4530 | } |
| 4531 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4532 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4533 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4534 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4535 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4536 | # Check the pre-context. |
| 4537 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 4538 | #print "APW: ALLOWED: pre<$1>\n"; |
| 4539 | $allowed = 1; |
| 4540 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4541 | |
| 4542 | my ($level, $endln, @chunks) = |
| 4543 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 4544 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4545 | # Check the condition. |
| 4546 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4547 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4548 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4549 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4550 | } |
| 4551 | if (statement_lines($cond) > 1) { |
| 4552 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 4553 | $allowed = 1; |
| 4554 | } |
| 4555 | if ($block =~/\b(?:if|for|while)\b/) { |
| 4556 | #print "APW: ALLOWED: block<$block>\n"; |
| 4557 | $allowed = 1; |
| 4558 | } |
| 4559 | if (statement_block_size($block) > 1) { |
| 4560 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 4561 | $allowed = 1; |
| 4562 | } |
| 4563 | # Check the post-context. |
| 4564 | if (defined $chunks[1]) { |
| 4565 | my ($cond, $block) = @{$chunks[1]}; |
| 4566 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4567 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4568 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4569 | if ($block =~ /^\s*\{/) { |
| 4570 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 4571 | $allowed = 1; |
| 4572 | } |
| 4573 | } |
| 4574 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 4575 | my $herectx = $here . "\n"; |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 4576 | my $cnt = statement_rawlines($block); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4577 | |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 4578 | for (my $n = 0; $n < $cnt; $n++) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 4579 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4580 | } |
| 4581 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4582 | WARN("BRACES", |
| 4583 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4584 | } |
| 4585 | } |
| 4586 | |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 4587 | # check for unnecessary blank lines around braces |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 4588 | if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { |
Joe Perches | f8e5821 | 2015-02-13 14:38:46 -0800 | [diff] [blame] | 4589 | if (CHK("BRACES", |
| 4590 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && |
| 4591 | $fix && $prevrawline =~ /^\+/) { |
| 4592 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4593 | } |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 4594 | } |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 4595 | if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
Joe Perches | f8e5821 | 2015-02-13 14:38:46 -0800 | [diff] [blame] | 4596 | if (CHK("BRACES", |
| 4597 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && |
| 4598 | $fix) { |
| 4599 | fix_delete_line($fixlinenr, $rawline); |
| 4600 | } |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 4601 | } |
| 4602 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4603 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4604 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 4605 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4606 | WARN("VOLATILE", |
| 4607 | "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4608 | } |
| 4609 | |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 4610 | # Check for user-visible strings broken across lines, which breaks the ability |
| 4611 | # to grep for the string. Make exceptions when the previous string ends in a |
| 4612 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' |
| 4613 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value |
| 4614 | if ($line =~ /^\+\s*"[X\t]*"/ && |
| 4615 | $prevline =~ /"\s*$/ && |
| 4616 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { |
| 4617 | if (WARN("SPLIT_STRING", |
| 4618 | "quoted string split across lines\n" . $hereprev) && |
| 4619 | $fix && |
| 4620 | $prevrawline =~ /^\+.*"\s*$/ && |
| 4621 | $last_coalesced_string_linenr != $linenr - 1) { |
| 4622 | my $extracted_string = get_quoted_string($line, $rawline); |
| 4623 | my $comma_close = ""; |
| 4624 | if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { |
| 4625 | $comma_close = $1; |
| 4626 | } |
| 4627 | |
| 4628 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4629 | fix_delete_line($fixlinenr, $rawline); |
| 4630 | my $fixedline = $prevrawline; |
| 4631 | $fixedline =~ s/"\s*$//; |
| 4632 | $fixedline .= substr($extracted_string, 1) . trim($comma_close); |
| 4633 | fix_insert_line($fixlinenr - 1, $fixedline); |
| 4634 | $fixedline = $rawline; |
| 4635 | $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; |
| 4636 | if ($fixedline !~ /\+\s*$/) { |
| 4637 | fix_insert_line($fixlinenr, $fixedline); |
| 4638 | } |
| 4639 | $last_coalesced_string_linenr = $linenr; |
| 4640 | } |
| 4641 | } |
| 4642 | |
| 4643 | # check for missing a space in a string concatenation |
| 4644 | if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { |
| 4645 | WARN('MISSING_SPACE', |
| 4646 | "break quoted strings at a space character\n" . $hereprev); |
| 4647 | } |
| 4648 | |
| 4649 | # check for spaces before a quoted newline |
| 4650 | if ($rawline =~ /^.*\".*\s\\n/) { |
| 4651 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 4652 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && |
| 4653 | $fix) { |
| 4654 | $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; |
| 4655 | } |
| 4656 | |
| 4657 | } |
| 4658 | |
Joe Perches | f17dba4 | 2014-10-13 15:51:51 -0700 | [diff] [blame] | 4659 | # concatenated string without spaces between elements |
| 4660 | if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) { |
| 4661 | CHK("CONCATENATED_STRING", |
| 4662 | "Concatenated strings should use spaces between elements\n" . $herecurr); |
| 4663 | } |
| 4664 | |
Joe Perches | 90ad30e | 2014-12-10 15:51:59 -0800 | [diff] [blame] | 4665 | # uncoalesced string fragments |
| 4666 | if ($line =~ /"X*"\s*"/) { |
| 4667 | WARN("STRING_FRAGMENTS", |
| 4668 | "Consecutive strings are generally better as a single string\n" . $herecurr); |
| 4669 | } |
| 4670 | |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 4671 | # check for %L{u,d,i} in strings |
| 4672 | my $string; |
| 4673 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 4674 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
| 4675 | $string =~ s/%%/__/g; |
| 4676 | if ($string =~ /(?<!%)%L[udi]/) { |
| 4677 | WARN("PRINTF_L", |
| 4678 | "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
| 4679 | last; |
| 4680 | } |
| 4681 | } |
| 4682 | |
| 4683 | # check for line continuations in quoted strings with odd counts of " |
| 4684 | if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { |
| 4685 | WARN("LINE_CONTINUATIONS", |
| 4686 | "Avoid line continuations in quoted strings\n" . $herecurr); |
| 4687 | } |
| 4688 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4689 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4690 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4691 | CHK("REDUNDANT_CODE", |
| 4692 | "if this code is redundant consider removing it\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4693 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4694 | } |
| 4695 | |
Andy Whitcroft | 03df4b5 | 2012-12-17 16:01:52 -0800 | [diff] [blame] | 4696 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 4697 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
| 4698 | my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; |
| 4699 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { |
| 4700 | WARN('NEEDLESS_IF', |
Fabio Estevam | 15160f9 | 2014-12-10 15:51:40 -0800 | [diff] [blame] | 4701 | "$1(NULL) is safe and this check is probably not required\n" . $hereprev); |
Greg Kroah-Hartman | 4c432a8 | 2008-07-23 21:29:04 -0700 | [diff] [blame] | 4702 | } |
| 4703 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4704 | |
Joe Perches | ebfdc40 | 2014-08-06 16:10:27 -0700 | [diff] [blame] | 4705 | # check for unnecessary "Out of Memory" messages |
| 4706 | if ($line =~ /^\+.*\b$logFunctions\s*\(/ && |
| 4707 | $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && |
| 4708 | (defined $1 || defined $3) && |
| 4709 | $linenr > 3) { |
| 4710 | my $testval = $2; |
| 4711 | my $testline = $lines[$linenr - 3]; |
| 4712 | |
| 4713 | my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); |
| 4714 | # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); |
| 4715 | |
| 4716 | if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) { |
| 4717 | WARN("OOM_MESSAGE", |
| 4718 | "Possible unnecessary 'out of memory' message\n" . $hereprev); |
| 4719 | } |
| 4720 | } |
| 4721 | |
Joe Perches | f78d98f | 2014-10-13 15:52:01 -0700 | [diff] [blame] | 4722 | # check for logging functions with KERN_<LEVEL> |
Paolo Bonzini | dcaf112 | 2015-02-13 14:38:26 -0800 | [diff] [blame] | 4723 | if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && |
Joe Perches | f78d98f | 2014-10-13 15:52:01 -0700 | [diff] [blame] | 4724 | $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { |
| 4725 | my $level = $1; |
| 4726 | if (WARN("UNNECESSARY_KERN_LEVEL", |
| 4727 | "Possible unnecessary $level\n" . $herecurr) && |
| 4728 | $fix) { |
| 4729 | $fixed[$fixlinenr] =~ s/\s*$level\s*//; |
| 4730 | } |
| 4731 | } |
| 4732 | |
Joe Perches | abb08a5 | 2014-12-10 15:51:46 -0800 | [diff] [blame] | 4733 | # check for mask then right shift without a parentheses |
| 4734 | if ($^V && $^V ge 5.10.0 && |
| 4735 | $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && |
| 4736 | $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so |
| 4737 | WARN("MASK_THEN_SHIFT", |
| 4738 | "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); |
| 4739 | } |
| 4740 | |
Joe Perches | b75ac61 | 2014-12-10 15:52:02 -0800 | [diff] [blame] | 4741 | # check for pointer comparisons to NULL |
| 4742 | if ($^V && $^V ge 5.10.0) { |
| 4743 | while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { |
| 4744 | my $val = $1; |
| 4745 | my $equal = "!"; |
| 4746 | $equal = "" if ($4 eq "!="); |
| 4747 | if (CHK("COMPARISON_TO_NULL", |
| 4748 | "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && |
| 4749 | $fix) { |
| 4750 | $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; |
| 4751 | } |
| 4752 | } |
| 4753 | } |
| 4754 | |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 4755 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
| 4756 | if ($line =~ /(\b$InitAttribute\b)/) { |
| 4757 | my $attr = $1; |
| 4758 | if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { |
| 4759 | my $ptr = $1; |
| 4760 | my $var = $2; |
| 4761 | if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && |
| 4762 | ERROR("MISPLACED_INIT", |
| 4763 | "$attr should be placed after $var\n" . $herecurr)) || |
| 4764 | ($ptr !~ /\b(union|struct)\s+$attr\b/ && |
| 4765 | WARN("MISPLACED_INIT", |
| 4766 | "$attr should be placed after $var\n" . $herecurr))) && |
| 4767 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4768 | $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] | 4769 | } |
| 4770 | } |
| 4771 | } |
| 4772 | |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 4773 | # check for $InitAttributeData (ie: __initdata) with const |
| 4774 | if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { |
| 4775 | my $attr = $1; |
| 4776 | $attr =~ /($InitAttributePrefix)(.*)/; |
| 4777 | my $attr_prefix = $1; |
| 4778 | my $attr_type = $2; |
| 4779 | if (ERROR("INIT_ATTRIBUTE", |
| 4780 | "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && |
| 4781 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4782 | $fixed[$fixlinenr] =~ |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 4783 | s/$InitAttributeData/${attr_prefix}initconst/; |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | # check for $InitAttributeConst (ie: __initconst) without const |
| 4788 | if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { |
| 4789 | my $attr = $1; |
| 4790 | if (ERROR("INIT_ATTRIBUTE", |
| 4791 | "Use of $attr requires a separate use of const\n" . $herecurr) && |
| 4792 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4793 | my $lead = $fixed[$fixlinenr] =~ |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 4794 | /(^\+\s*(?:static\s+))/; |
| 4795 | $lead = rtrim($1); |
| 4796 | $lead = "$lead " if ($lead !~ /^\+$/); |
| 4797 | $lead = "${lead}const "; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4798 | $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 4799 | } |
| 4800 | } |
| 4801 | |
Joe Perches | c17893c | 2015-04-16 12:44:42 -0700 | [diff] [blame] | 4802 | # check for __read_mostly with const non-pointer (should just be const) |
| 4803 | if ($line =~ /\b__read_mostly\b/ && |
| 4804 | $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { |
| 4805 | if (ERROR("CONST_READ_MOSTLY", |
| 4806 | "Invalid use of __read_mostly with const type\n" . $herecurr) && |
| 4807 | $fix) { |
| 4808 | $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; |
| 4809 | } |
| 4810 | } |
| 4811 | |
Joe Perches | fbdb813 | 2014-04-03 14:49:14 -0700 | [diff] [blame] | 4812 | # don't use __constant_<foo> functions outside of include/uapi/ |
| 4813 | if ($realfile !~ m@^include/uapi/@ && |
| 4814 | $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { |
| 4815 | my $constant_func = $1; |
| 4816 | my $func = $constant_func; |
| 4817 | $func =~ s/^__constant_//; |
| 4818 | if (WARN("CONSTANT_CONVERSION", |
| 4819 | "$constant_func should be $func\n" . $herecurr) && |
| 4820 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4821 | $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; |
Joe Perches | fbdb813 | 2014-04-03 14:49:14 -0700 | [diff] [blame] | 4822 | } |
| 4823 | } |
| 4824 | |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 4825 | # prefer usleep_range over udelay |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 4826 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 4827 | my $delay = $1; |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 4828 | # ignore udelay's < 10, however |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 4829 | if (! ($delay < 10) ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4830 | CHK("USLEEP_RANGE", |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 4831 | "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); |
| 4832 | } |
| 4833 | if ($delay > 2000) { |
| 4834 | WARN("LONG_UDELAY", |
| 4835 | "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] | 4836 | } |
| 4837 | } |
| 4838 | |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 4839 | # warn about unexpectedly long msleep's |
| 4840 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 4841 | if ($1 < 20) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4842 | WARN("MSLEEP", |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 4843 | "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 4844 | } |
| 4845 | } |
| 4846 | |
Joe Perches | 36ec193 | 2013-07-03 15:05:25 -0700 | [diff] [blame] | 4847 | # check for comparisons of jiffies |
| 4848 | if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { |
| 4849 | WARN("JIFFIES_COMPARISON", |
| 4850 | "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); |
| 4851 | } |
| 4852 | |
Joe Perches | 9d7a34a | 2013-07-03 15:05:26 -0700 | [diff] [blame] | 4853 | # check for comparisons of get_jiffies_64() |
| 4854 | if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { |
| 4855 | WARN("JIFFIES_COMPARISON", |
| 4856 | "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); |
| 4857 | } |
| 4858 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4859 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4860 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4861 | # print "#ifdef in C files should be avoided\n"; |
| 4862 | # print "$herecurr"; |
| 4863 | # $clean = 0; |
| 4864 | # } |
| 4865 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4866 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4867 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4868 | if (ERROR("SPACING", |
| 4869 | "exactly one space required after that #$1\n" . $herecurr) && |
| 4870 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4871 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4872 | s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; |
| 4873 | } |
| 4874 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4875 | } |
| 4876 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4877 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4878 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 4879 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4880 | my $which = $1; |
| 4881 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4882 | CHK("UNCOMMENTED_DEFINITION", |
| 4883 | "$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4884 | } |
| 4885 | } |
| 4886 | # check for memory barriers without a comment. |
| 4887 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 4888 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | c1fd7bb | 2013-11-12 15:10:11 -0800 | [diff] [blame] | 4889 | WARN("MEMORY_BARRIER", |
| 4890 | "memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4891 | } |
| 4892 | } |
| 4893 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4894 | 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] | 4895 | CHK("ARCH_DEFINES", |
| 4896 | "architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4897 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4898 | |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 4899 | # Check that the storage class is at the beginning of a declaration |
| 4900 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4901 | WARN("STORAGE_CLASS", |
| 4902 | "storage class should be at the beginning of the declaration\n" . $herecurr) |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 4903 | } |
| 4904 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4905 | # check the location of the inline attribute, that it is between |
| 4906 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4907 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 4908 | $line =~ /\b$Inline\s+$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4909 | ERROR("INLINE_LOCATION", |
| 4910 | "inline keyword should sit between storage class and type\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4913 | # Check for __inline__ and __inline, prefer inline |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 4914 | if ($realfile !~ m@\binclude/uapi/@ && |
| 4915 | $line =~ /\b(__inline__|__inline)\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4916 | if (WARN("INLINE", |
| 4917 | "plain inline is preferred over $1\n" . $herecurr) && |
| 4918 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4919 | $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4920 | |
| 4921 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4922 | } |
| 4923 | |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 4924 | # Check for __attribute__ packed, prefer __packed |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 4925 | if ($realfile !~ m@\binclude/uapi/@ && |
| 4926 | $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4927 | WARN("PREFER_PACKED", |
| 4928 | "__packed is preferred over __attribute__((packed))\n" . $herecurr); |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 4929 | } |
| 4930 | |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 4931 | # Check for __attribute__ aligned, prefer __aligned |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 4932 | if ($realfile !~ m@\binclude/uapi/@ && |
| 4933 | $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4934 | WARN("PREFER_ALIGNED", |
| 4935 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 4936 | } |
| 4937 | |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 4938 | # Check for __attribute__ format(printf, prefer __printf |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 4939 | if ($realfile !~ m@\binclude/uapi/@ && |
| 4940 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4941 | if (WARN("PREFER_PRINTF", |
| 4942 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && |
| 4943 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4944 | $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4945 | |
| 4946 | } |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 4947 | } |
| 4948 | |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4949 | # Check for __attribute__ format(scanf, prefer __scanf |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 4950 | if ($realfile !~ m@\binclude/uapi/@ && |
| 4951 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4952 | if (WARN("PREFER_SCANF", |
| 4953 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && |
| 4954 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4955 | $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4956 | } |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4957 | } |
| 4958 | |
Joe Perches | 619a908 | 2014-12-10 15:51:35 -0800 | [diff] [blame] | 4959 | # Check for __attribute__ weak, or __weak declarations (may have link issues) |
| 4960 | if ($^V && $^V ge 5.10.0 && |
| 4961 | $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && |
| 4962 | ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || |
| 4963 | $line =~ /\b__weak\b/)) { |
| 4964 | ERROR("WEAK_DECLARATION", |
| 4965 | "Using weak declarations can have unintended link defects\n" . $herecurr); |
| 4966 | } |
| 4967 | |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 4968 | # check for sizeof(&) |
| 4969 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4970 | WARN("SIZEOF_ADDRESS", |
| 4971 | "sizeof(& should be avoided\n" . $herecurr); |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 4972 | } |
| 4973 | |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 4974 | # check for sizeof without parenthesis |
| 4975 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4976 | if (WARN("SIZEOF_PARENTHESIS", |
| 4977 | "sizeof $1 should be sizeof($1)\n" . $herecurr) && |
| 4978 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4979 | $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] | 4980 | } |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 4981 | } |
| 4982 | |
Joe Perches | 88982fe | 2012-12-17 16:02:00 -0800 | [diff] [blame] | 4983 | # check for struct spinlock declarations |
| 4984 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 4985 | WARN("USE_SPINLOCK_T", |
| 4986 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 4987 | } |
| 4988 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 4989 | # check for seq_printf uses that could be seq_puts |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 4990 | if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 4991 | my $fmt = get_quoted_string($line, $rawline); |
Heba Aamer | caac1d5 | 2015-02-13 14:38:49 -0800 | [diff] [blame] | 4992 | $fmt =~ s/%%//g; |
| 4993 | if ($fmt !~ /%/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4994 | if (WARN("PREFER_SEQ_PUTS", |
| 4995 | "Prefer seq_puts to seq_printf\n" . $herecurr) && |
| 4996 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4997 | $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4998 | } |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 4999 | } |
| 5000 | } |
| 5001 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 5002 | # Check for misused memsets |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5003 | if ($^V && $^V ge 5.10.0 && |
| 5004 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5005 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 5006 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5007 | my $ms_addr = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5008 | my $ms_val = $7; |
| 5009 | my $ms_size = $12; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5010 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 5011 | if ($ms_size =~ /^(0x|)0$/i) { |
| 5012 | ERROR("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5013 | "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] | 5014 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 5015 | WARN("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5016 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 5017 | } |
| 5018 | } |
| 5019 | |
Joe Perches | 98a9bba | 2014-01-23 15:54:52 -0800 | [diff] [blame] | 5020 | # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) |
| 5021 | if ($^V && $^V ge 5.10.0 && |
| 5022 | $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) { |
| 5023 | if (WARN("PREFER_ETHER_ADDR_COPY", |
| 5024 | "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) && |
| 5025 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5026 | $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; |
Joe Perches | 98a9bba | 2014-01-23 15:54:52 -0800 | [diff] [blame] | 5027 | } |
| 5028 | } |
| 5029 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5030 | # typecasts on min/max could be min_t/max_t |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5031 | if ($^V && $^V ge 5.10.0 && |
| 5032 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5033 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5034 | if (defined $2 || defined $7) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5035 | my $call = $1; |
| 5036 | my $cast1 = deparenthesize($2); |
| 5037 | my $arg1 = $3; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5038 | my $cast2 = deparenthesize($7); |
| 5039 | my $arg2 = $8; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5040 | my $cast; |
| 5041 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5042 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5043 | $cast = "$cast1 or $cast2"; |
| 5044 | } elsif ($cast1 ne "") { |
| 5045 | $cast = $cast1; |
| 5046 | } else { |
| 5047 | $cast = $cast2; |
| 5048 | } |
| 5049 | WARN("MINMAX", |
| 5050 | "$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] | 5051 | } |
| 5052 | } |
| 5053 | |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 5054 | # check usleep_range arguments |
| 5055 | if ($^V && $^V ge 5.10.0 && |
| 5056 | defined $stat && |
| 5057 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 5058 | my $min = $1; |
| 5059 | my $max = $7; |
| 5060 | if ($min eq $max) { |
| 5061 | WARN("USLEEP_RANGE", |
| 5062 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 5063 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 5064 | $min > $max) { |
| 5065 | WARN("USLEEP_RANGE", |
| 5066 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 5067 | } |
| 5068 | } |
| 5069 | |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 5070 | # check for naked sscanf |
| 5071 | if ($^V && $^V ge 5.10.0 && |
| 5072 | defined $stat && |
Joe Perches | 6c8bd70 | 2014-04-03 14:49:16 -0700 | [diff] [blame] | 5073 | $line =~ /\bsscanf\b/ && |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 5074 | ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && |
| 5075 | $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && |
| 5076 | $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { |
| 5077 | my $lc = $stat =~ tr@\n@@; |
| 5078 | $lc = $lc + $linenr; |
| 5079 | my $stat_real = raw_line($linenr, 0); |
| 5080 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 5081 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 5082 | } |
| 5083 | WARN("NAKED_SSCANF", |
| 5084 | "unchecked sscanf return value\n" . "$here\n$stat_real\n"); |
| 5085 | } |
| 5086 | |
Joe Perches | afc819a | 2014-06-04 16:12:08 -0700 | [diff] [blame] | 5087 | # check for simple sscanf that should be kstrto<foo> |
| 5088 | if ($^V && $^V ge 5.10.0 && |
| 5089 | defined $stat && |
| 5090 | $line =~ /\bsscanf\b/) { |
| 5091 | my $lc = $stat =~ tr@\n@@; |
| 5092 | $lc = $lc + $linenr; |
| 5093 | my $stat_real = raw_line($linenr, 0); |
| 5094 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 5095 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 5096 | } |
| 5097 | if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { |
| 5098 | my $format = $6; |
| 5099 | my $count = $format =~ tr@%@%@; |
| 5100 | if ($count == 1 && |
| 5101 | $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { |
| 5102 | WARN("SSCANF_TO_KSTRTO", |
| 5103 | "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); |
| 5104 | } |
| 5105 | } |
| 5106 | } |
| 5107 | |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 5108 | # check for new externs in .h files. |
| 5109 | if ($realfile =~ /\.h$/ && |
| 5110 | $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { |
Joe Perches | d1d8578 | 2013-09-24 15:27:46 -0700 | [diff] [blame] | 5111 | if (CHK("AVOID_EXTERNS", |
| 5112 | "extern prototypes should be avoided in .h files\n" . $herecurr) && |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 5113 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5114 | $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 5115 | } |
| 5116 | } |
| 5117 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5118 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5119 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5120 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5121 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5122 | my $function_name = $1; |
| 5123 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5124 | |
| 5125 | my $s = $stat; |
| 5126 | if (defined $cond) { |
| 5127 | substr($s, 0, length($cond), ''); |
| 5128 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5129 | if ($s =~ /^\s*;/ && |
| 5130 | $function_name ne 'uninitialized_var') |
| 5131 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5132 | WARN("AVOID_EXTERNS", |
| 5133 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | if ($paren_space =~ /\n/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5137 | WARN("FUNCTION_ARGUMENTS", |
| 5138 | "arguments for function declarations should follow identifier\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5139 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 5140 | |
| 5141 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 5142 | $stat =~ /^.\s*extern\s+/) |
| 5143 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5144 | WARN("AVOID_EXTERNS", |
| 5145 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | # checks for new __setup's |
| 5149 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 5150 | my $name = $1; |
| 5151 | |
| 5152 | if (!grep(/$name/, @setup_docs)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5153 | CHK("UNDOCUMENTED_SETUP", |
| 5154 | "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5155 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 5156 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 5157 | |
| 5158 | # check for pointless casting of kmalloc return |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 5159 | if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5160 | WARN("UNNECESSARY_CASTS", |
| 5161 | "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] | 5162 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5163 | |
Joe Perches | a640d25 | 2013-07-03 15:05:21 -0700 | [diff] [blame] | 5164 | # alloc style |
| 5165 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
| 5166 | if ($^V && $^V ge 5.10.0 && |
| 5167 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { |
| 5168 | CHK("ALLOC_SIZEOF_STRUCT", |
| 5169 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 5170 | } |
| 5171 | |
Joe Perches | 60a5536 | 2014-06-04 16:12:07 -0700 | [diff] [blame] | 5172 | # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc |
| 5173 | if ($^V && $^V ge 5.10.0 && |
Joe Perches | e367455 | 2014-08-06 16:10:55 -0700 | [diff] [blame] | 5174 | $line =~ /\b($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] | 5175 | my $oldfunc = $3; |
| 5176 | my $a1 = $4; |
| 5177 | my $a2 = $10; |
| 5178 | my $newfunc = "kmalloc_array"; |
| 5179 | $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); |
Joe Perches | e367455 | 2014-08-06 16:10:55 -0700 | [diff] [blame] | 5180 | my $r1 = $a1; |
| 5181 | my $r2 = $a2; |
| 5182 | if ($a1 =~ /^sizeof\s*\S/) { |
| 5183 | $r1 = $a2; |
| 5184 | $r2 = $a1; |
| 5185 | } |
| 5186 | if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && |
| 5187 | !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { |
Joe Perches | 60a5536 | 2014-06-04 16:12:07 -0700 | [diff] [blame] | 5188 | if (WARN("ALLOC_WITH_MULTIPLY", |
| 5189 | "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) && |
| 5190 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5191 | $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] | 5192 | |
| 5193 | } |
| 5194 | } |
| 5195 | } |
| 5196 | |
Joe Perches | 972fdea | 2013-04-29 16:18:12 -0700 | [diff] [blame] | 5197 | # check for krealloc arg reuse |
| 5198 | if ($^V && $^V ge 5.10.0 && |
| 5199 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { |
| 5200 | WARN("KREALLOC_ARG_REUSE", |
| 5201 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 5202 | } |
| 5203 | |
Joe Perches | 5ce59ae | 2013-02-21 16:44:18 -0800 | [diff] [blame] | 5204 | # check for alloc argument mismatch |
| 5205 | if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { |
| 5206 | WARN("ALLOC_ARRAY_ARGS", |
| 5207 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
| 5208 | } |
| 5209 | |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 5210 | # check for multiple semicolons |
| 5211 | if ($line =~ /;\s*;\s*$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5212 | if (WARN("ONE_SEMICOLON", |
| 5213 | "Statements terminations use 1 semicolon\n" . $herecurr) && |
| 5214 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5215 | $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5216 | } |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 5217 | } |
| 5218 | |
Joe Perches | 0ab9019 | 2014-12-10 15:51:57 -0800 | [diff] [blame] | 5219 | # check for #defines like: 1 << <digit> that could be BIT(digit) |
| 5220 | if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { |
| 5221 | my $ull = ""; |
| 5222 | $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); |
| 5223 | if (CHK("BIT_MACRO", |
| 5224 | "Prefer using the BIT$ull macro\n" . $herecurr) && |
| 5225 | $fix) { |
| 5226 | $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; |
| 5227 | } |
| 5228 | } |
| 5229 | |
Joe Perches | e81f239 | 2014-08-06 16:11:25 -0700 | [diff] [blame] | 5230 | # check for case / default statements not preceded by break/fallthrough/switch |
Joe Perches | c34c09a | 2014-01-23 15:54:43 -0800 | [diff] [blame] | 5231 | if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { |
| 5232 | my $has_break = 0; |
| 5233 | my $has_statement = 0; |
| 5234 | my $count = 0; |
| 5235 | my $prevline = $linenr; |
Joe Perches | e81f239 | 2014-08-06 16:11:25 -0700 | [diff] [blame] | 5236 | while ($prevline > 1 && ($file || $count < 3) && !$has_break) { |
Joe Perches | c34c09a | 2014-01-23 15:54:43 -0800 | [diff] [blame] | 5237 | $prevline--; |
| 5238 | my $rline = $rawlines[$prevline - 1]; |
| 5239 | my $fline = $lines[$prevline - 1]; |
| 5240 | last if ($fline =~ /^\@\@/); |
| 5241 | next if ($fline =~ /^\-/); |
| 5242 | next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); |
| 5243 | $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); |
| 5244 | next if ($fline =~ /^.[\s$;]*$/); |
| 5245 | $has_statement = 1; |
| 5246 | $count++; |
| 5247 | $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); |
| 5248 | } |
| 5249 | if (!$has_break && $has_statement) { |
| 5250 | WARN("MISSING_BREAK", |
| 5251 | "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); |
| 5252 | } |
| 5253 | } |
| 5254 | |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 5255 | # check for switch/default statements without a break; |
| 5256 | if ($^V && $^V ge 5.10.0 && |
| 5257 | defined $stat && |
| 5258 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
| 5259 | my $ctx = ''; |
| 5260 | my $herectx = $here . "\n"; |
| 5261 | my $cnt = statement_rawlines($stat); |
| 5262 | for (my $n = 0; $n < $cnt; $n++) { |
| 5263 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 5264 | } |
| 5265 | WARN("DEFAULT_NO_BREAK", |
| 5266 | "switch default: should use break\n" . $herectx); |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 5267 | } |
| 5268 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5269 | # check for gcc specific __FUNCTION__ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5270 | if ($line =~ /\b__FUNCTION__\b/) { |
| 5271 | if (WARN("USE_FUNC", |
| 5272 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && |
| 5273 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5274 | $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5275 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5276 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5277 | |
Joe Perches | 62ec818 | 2015-02-13 14:38:18 -0800 | [diff] [blame] | 5278 | # check for uses of __DATE__, __TIME__, __TIMESTAMP__ |
| 5279 | while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { |
| 5280 | ERROR("DATE_TIME", |
| 5281 | "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); |
| 5282 | } |
| 5283 | |
Joe Perches | 2c92488 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 5284 | # check for use of yield() |
| 5285 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 5286 | WARN("YIELD", |
| 5287 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 5288 | } |
| 5289 | |
Joe Perches | 179f8f4 | 2013-07-03 15:05:30 -0700 | [diff] [blame] | 5290 | # check for comparisons against true and false |
| 5291 | if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { |
| 5292 | my $lead = $1; |
| 5293 | my $arg = $2; |
| 5294 | my $test = $3; |
| 5295 | my $otype = $4; |
| 5296 | my $trail = $5; |
| 5297 | my $op = "!"; |
| 5298 | |
| 5299 | ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); |
| 5300 | |
| 5301 | my $type = lc($otype); |
| 5302 | if ($type =~ /^(?:true|false)$/) { |
| 5303 | if (("$test" eq "==" && "$type" eq "true") || |
| 5304 | ("$test" eq "!=" && "$type" eq "false")) { |
| 5305 | $op = ""; |
| 5306 | } |
| 5307 | |
| 5308 | CHK("BOOL_COMPARISON", |
| 5309 | "Using comparison to $otype is error prone\n" . $herecurr); |
| 5310 | |
| 5311 | ## maybe suggesting a correct construct would better |
| 5312 | ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); |
| 5313 | |
| 5314 | } |
| 5315 | } |
| 5316 | |
Thomas Gleixner | 4882720b | 2010-09-07 14:34:01 +0000 | [diff] [blame] | 5317 | # check for semaphores initialized locked |
| 5318 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5319 | WARN("CONSIDER_COMPLETION", |
| 5320 | "consider using a completion\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5321 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 5322 | |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 5323 | # recommend kstrto* over simple_strto* and strict_strto* |
| 5324 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5325 | WARN("CONSIDER_KSTRTO", |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 5326 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5327 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 5328 | |
Fabian Frederick | ae3ccc4 | 2014-06-04 16:12:10 -0700 | [diff] [blame] | 5329 | # check for __initcall(), use device_initcall() explicitly or more appropriate function please |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 5330 | if ($line =~ /^.\s*__initcall\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5331 | WARN("USE_DEVICE_INITCALL", |
Fabian Frederick | ae3ccc4 | 2014-06-04 16:12:10 -0700 | [diff] [blame] | 5332 | "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] | 5333 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 5334 | |
Joe Perches | 0f3c5aa | 2015-02-13 14:39:05 -0800 | [diff] [blame] | 5335 | # check for various structs that are normally const (ops, kgdb, device_tree) |
| 5336 | my $const_structs = qr{ |
| 5337 | acpi_dock_ops| |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 5338 | address_space_operations| |
| 5339 | backlight_ops| |
| 5340 | block_device_operations| |
| 5341 | dentry_operations| |
| 5342 | dev_pm_ops| |
| 5343 | dma_map_ops| |
| 5344 | extent_io_ops| |
| 5345 | file_lock_operations| |
| 5346 | file_operations| |
| 5347 | hv_ops| |
| 5348 | ide_dma_ops| |
| 5349 | intel_dvo_dev_ops| |
| 5350 | item_operations| |
| 5351 | iwl_ops| |
| 5352 | kgdb_arch| |
| 5353 | kgdb_io| |
| 5354 | kset_uevent_ops| |
| 5355 | lock_manager_operations| |
| 5356 | microcode_ops| |
| 5357 | mtrr_ops| |
| 5358 | neigh_ops| |
| 5359 | nlmsvc_binding| |
Joe Perches | 0f3c5aa | 2015-02-13 14:39:05 -0800 | [diff] [blame] | 5360 | of_device_id| |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 5361 | pci_raw_ops| |
| 5362 | pipe_buf_operations| |
| 5363 | platform_hibernation_ops| |
| 5364 | platform_suspend_ops| |
| 5365 | proto_ops| |
| 5366 | rpc_pipe_ops| |
| 5367 | seq_operations| |
| 5368 | snd_ac97_build_ops| |
| 5369 | soc_pcmcia_socket_ops| |
| 5370 | stacktrace_ops| |
| 5371 | sysfs_ops| |
| 5372 | tty_operations| |
Joe Perches | 6d07d01b | 2015-04-16 12:44:33 -0700 | [diff] [blame] | 5373 | uart_ops| |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 5374 | usb_mon_operations| |
| 5375 | wd_ops}x; |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 5376 | if ($line !~ /\bconst\b/ && |
Joe Perches | 0f3c5aa | 2015-02-13 14:39:05 -0800 | [diff] [blame] | 5377 | $line =~ /\bstruct\s+($const_structs)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5378 | WARN("CONST_STRUCT", |
| 5379 | "struct $1 should normally be const\n" . |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 5380 | $herecurr); |
Andy Whitcroft | 2b6db5c | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 5381 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5382 | |
| 5383 | # use of NR_CPUS is usually wrong |
| 5384 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 5385 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5386 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 5387 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5388 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 5389 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 5390 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5391 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5392 | WARN("NR_CPUS", |
| 5393 | "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] | 5394 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 5395 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 5396 | # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. |
| 5397 | if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { |
| 5398 | ERROR("DEFINE_ARCH_HAS", |
| 5399 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); |
| 5400 | } |
| 5401 | |
Joe Perches | acd9362 | 2015-02-13 14:38:38 -0800 | [diff] [blame] | 5402 | # likely/unlikely comparisons similar to "(likely(foo) > 0)" |
| 5403 | if ($^V && $^V ge 5.10.0 && |
| 5404 | $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { |
| 5405 | WARN("LIKELY_MISUSE", |
| 5406 | "Using $1 should generally have parentheses around the comparison\n" . $herecurr); |
| 5407 | } |
| 5408 | |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 5409 | # whine mightly about in_atomic |
| 5410 | if ($line =~ /\bin_atomic\s*\(/) { |
| 5411 | if ($realfile =~ m@^drivers/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5412 | ERROR("IN_ATOMIC", |
| 5413 | "do not use in_atomic in drivers\n" . $herecurr); |
Andy Whitcroft | f4a8773 | 2009-02-27 14:03:05 -0800 | [diff] [blame] | 5414 | } elsif ($realfile !~ m@^kernel/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5415 | WARN("IN_ATOMIC", |
| 5416 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 5417 | } |
| 5418 | } |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 5419 | |
| 5420 | # check for lockdep_set_novalidate_class |
| 5421 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 5422 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 5423 | if ($realfile !~ m@^kernel/lockdep@ && |
| 5424 | $realfile !~ m@^include/linux/lockdep@ && |
| 5425 | $realfile !~ m@^drivers/base/core@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5426 | ERROR("LOCKDEP", |
| 5427 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 5428 | } |
| 5429 | } |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 5430 | |
Joe Perches | b392c64 | 2015-04-16 12:44:16 -0700 | [diff] [blame] | 5431 | if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || |
| 5432 | $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5433 | WARN("EXPORTED_WORLD_WRITABLE", |
| 5434 | "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] | 5435 | } |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 5436 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 5437 | # Mode permission misuses where it seems decimal should be octal |
| 5438 | # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop |
| 5439 | if ($^V && $^V ge 5.10.0 && |
| 5440 | $line =~ /$mode_perms_search/) { |
| 5441 | foreach my $entry (@mode_permission_funcs) { |
| 5442 | my $func = $entry->[0]; |
| 5443 | my $arg_pos = $entry->[1]; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 5444 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 5445 | my $skip_args = ""; |
| 5446 | if ($arg_pos > 1) { |
| 5447 | $arg_pos--; |
| 5448 | $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; |
| 5449 | } |
| 5450 | my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]"; |
| 5451 | if ($line =~ /$test/) { |
| 5452 | my $val = $1; |
| 5453 | $val = $6 if ($skip_args ne ""); |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 5454 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 5455 | if ($val !~ /^0$/ && |
| 5456 | (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || |
| 5457 | length($val) ne 4)) { |
| 5458 | ERROR("NON_OCTAL_PERMISSIONS", |
| 5459 | "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); |
Joe Perches | c0a5c89 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 5460 | } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) { |
| 5461 | ERROR("EXPORTED_WORLD_WRITABLE", |
| 5462 | "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 5463 | } |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 5464 | } |
| 5465 | } |
| 5466 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5467 | } |
| 5468 | |
| 5469 | # If we have no input at all, then there is nothing to report on |
| 5470 | # so just keep quiet. |
| 5471 | if ($#rawlines == -1) { |
| 5472 | exit(0); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5473 | } |
| 5474 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5475 | # In mailback mode only produce a report in the negative, for |
| 5476 | # things that appear to be patches. |
| 5477 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 5478 | exit(0); |
| 5479 | } |
| 5480 | |
| 5481 | # This is not a patch, and we are are in 'no-patch' mode so |
| 5482 | # just keep quiet. |
| 5483 | if (!$chk_patch && !$is_patch) { |
| 5484 | exit(0); |
| 5485 | } |
| 5486 | |
| 5487 | if (!$is_patch) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5488 | ERROR("NOT_UNIFIED_DIFF", |
| 5489 | "Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5490 | } |
| 5491 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5492 | ERROR("MISSING_SIGN_OFF", |
| 5493 | "Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5494 | } |
| 5495 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5496 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5497 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 5498 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5499 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 5500 | (($check)? "$cnt_chk checks, " : "") . |
| 5501 | "$cnt_lines lines checked\n"; |
| 5502 | print "\n" if ($quiet == 0); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 5503 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5504 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 5505 | if ($quiet == 0) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5506 | |
| 5507 | if ($^V lt 5.10.0) { |
| 5508 | print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); |
| 5509 | print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); |
| 5510 | } |
| 5511 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 5512 | # If there were whitespace errors which cleanpatch can fix |
| 5513 | # then suggest that. |
| 5514 | if ($rpt_cleaners) { |
| 5515 | print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; |
| 5516 | print " scripts/cleanfile\n\n"; |
Mike Frysinger | b078121 | 2011-03-22 16:34:43 -0700 | [diff] [blame] | 5517 | $rpt_cleaners = 0; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 5518 | } |
| 5519 | } |
| 5520 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 5521 | hash_show_words(\%use_type, "Used"); |
| 5522 | hash_show_words(\%ignore_type, "Ignored"); |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5523 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 5524 | if ($clean == 0 && $fix && |
| 5525 | ("@rawlines" ne "@fixed" || |
| 5526 | $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 5527 | my $newfile = $filename; |
| 5528 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5529 | my $linecount = 0; |
| 5530 | my $f; |
| 5531 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 5532 | @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); |
| 5533 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5534 | open($f, '>', $newfile) |
| 5535 | or die "$P: Can't open $newfile for write\n"; |
| 5536 | foreach my $fixed_line (@fixed) { |
| 5537 | $linecount++; |
| 5538 | if ($file) { |
| 5539 | if ($linecount > 3) { |
| 5540 | $fixed_line =~ s/^\+//; |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 5541 | print $f $fixed_line . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5542 | } |
| 5543 | } else { |
| 5544 | print $f $fixed_line . "\n"; |
| 5545 | } |
| 5546 | } |
| 5547 | close($f); |
| 5548 | |
| 5549 | if (!$quiet) { |
| 5550 | print << "EOM"; |
| 5551 | Wrote EXPERIMENTAL --fix correction(s) to '$newfile' |
| 5552 | |
| 5553 | Do _NOT_ trust the results written to this file. |
| 5554 | Do _NOT_ submit these changes without inspecting them for correctness. |
| 5555 | |
| 5556 | This EXPERIMENTAL file is simply a convenience to help rewrite patches. |
| 5557 | No warranties, expressed or implied... |
| 5558 | |
| 5559 | EOM |
| 5560 | } |
| 5561 | } |
| 5562 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5563 | if ($clean == 1 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 5564 | print "$vname has no obvious style problems and is ready for submission.\n" |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5565 | } |
| 5566 | if ($clean == 0 && $quiet == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5567 | print << "EOM"; |
| 5568 | $vname has style problems, please review. |
| 5569 | |
| 5570 | If any of these errors are false positives, please report |
| 5571 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 5572 | EOM |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5573 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5574 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5575 | return $clean; |
| 5576 | } |