Andy Whitcroft | 0a920b5 | 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 | 015830b | 2010-10-26 14:23:17 -0700 | [diff] [blame] | 5 | # (c) 2008-2010 Andy Whitcroft <apw@canonical.com> |
Andy Whitcroft | 0a920b5 | 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'; |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 12 | use Term::ANSIColor qw(:constants); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 13 | |
| 14 | my $P = $0; |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 15 | my $D = dirname(abs_path($P)); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 16 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 17 | my $V = '0.32'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 18 | |
| 19 | use Getopt::Long qw(:config no_auto_abbrev); |
| 20 | |
| 21 | my $quiet = 0; |
| 22 | my $tree = 1; |
| 23 | my $chk_signoff = 1; |
| 24 | my $chk_patch = 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 25 | my $tst_only; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 26 | my $emacs = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 27 | my $terse = 0; |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 28 | my $showfile = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 29 | my $file = 0; |
| 30 | my $check = 0; |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 31 | my $check_orig = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 32 | my $summary = 1; |
| 33 | my $mailback = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 34 | my $summary_file = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 35 | my $show_types = 0; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 36 | my $fix = 0; |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 37 | my $fix_inplace = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 38 | my $root; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 39 | my %debug; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 40 | my %camelcase = (); |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 41 | my %use_type = (); |
| 42 | my @use = (); |
| 43 | my %ignore_type = (); |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 44 | my @ignore = (); |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 45 | my $help = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 46 | my $configuration_file = ".checkpatch.conf"; |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 47 | my $max_line_length = 80; |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 48 | my $ignore_perl_version = 0; |
| 49 | my $minimum_perl_version = 5.10.0; |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 50 | my $min_conf_desc_length = 4; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 51 | my $spelling_file = "$D/spelling.txt"; |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 52 | my $codespell = 0; |
Maxim Uvarov | f1a6367 | 2015-06-25 15:03:08 -0700 | [diff] [blame] | 53 | my $codespellfile = "/usr/share/codespell/dictionary.txt"; |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 54 | my $color = 1; |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 55 | |
| 56 | sub help { |
| 57 | my ($exitcode) = @_; |
| 58 | |
| 59 | print << "EOM"; |
| 60 | Usage: $P [OPTION]... [FILE]... |
| 61 | Version: $V |
| 62 | |
| 63 | Options: |
| 64 | -q, --quiet quiet |
| 65 | --no-tree run without a kernel tree |
| 66 | --no-signoff do not check for 'Signed-off-by' line |
| 67 | --patch treat FILE as patchfile (default) |
| 68 | --emacs emacs compile window format |
| 69 | --terse one line per report |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 70 | --showfile emit diffed file position, not input file position |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 71 | -f, --file treat FILE as regular source file |
| 72 | --subjective, --strict enable more subjective tests |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 73 | --types TYPE(,TYPE2...) show only these comma separated message types |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 74 | --ignore TYPE(,TYPE2...) ignore various comma separated message types |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 75 | --max-line-length=n set the maximum line length, if exceeded, warn |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 76 | --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] | 77 | --show-types show the message "types" in the output |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 78 | --root=PATH PATH to the kernel tree root |
| 79 | --no-summary suppress the per-file summary |
| 80 | --mailback only produce a report in case of warnings/errors |
| 81 | --summary-file include the filename in summary |
| 82 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 83 | 'values', 'possible', 'type', and 'attr' (default |
| 84 | is all off) |
| 85 | --test-only=WORD report only warnings/errors containing WORD |
| 86 | literally |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 87 | --fix EXPERIMENTAL - may create horrible results |
| 88 | If correctable single-line errors exist, create |
| 89 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" |
| 90 | with potential errors corrected to the preferred |
| 91 | checkpatch style |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 92 | --fix-inplace EXPERIMENTAL - may create horrible results |
| 93 | Is the same as --fix, but overwrites the input |
| 94 | file. It's your fault if there's no backup or git |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 95 | --ignore-perl-version override checking of perl version. expect |
| 96 | runtime errors. |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 97 | --codespell Use the codespell dictionary for spelling/typos |
Maxim Uvarov | f1a6367 | 2015-06-25 15:03:08 -0700 | [diff] [blame] | 98 | (default:/usr/share/codespell/dictionary.txt) |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 99 | --codespellfile Use this codespell dictionary |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 100 | --color Use colors when output is STDOUT (default: on) |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 101 | -h, --help, --version display this help and exit |
| 102 | |
| 103 | When FILE is - read standard input. |
| 104 | EOM |
| 105 | |
| 106 | exit($exitcode); |
| 107 | } |
| 108 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 109 | my $conf = which_conf($configuration_file); |
| 110 | if (-f $conf) { |
| 111 | my @conf_args; |
| 112 | open(my $conffile, '<', "$conf") |
| 113 | or warn "$P: Can't find a readable $configuration_file file $!\n"; |
| 114 | |
| 115 | while (<$conffile>) { |
| 116 | my $line = $_; |
| 117 | |
| 118 | $line =~ s/\s*\n?$//g; |
| 119 | $line =~ s/^\s*//g; |
| 120 | $line =~ s/\s+/ /g; |
| 121 | |
| 122 | next if ($line =~ m/^\s*#/); |
| 123 | next if ($line =~ m/^\s*$/); |
| 124 | |
| 125 | my @words = split(" ", $line); |
| 126 | foreach my $word (@words) { |
| 127 | last if ($word =~ m/^#/); |
| 128 | push (@conf_args, $word); |
| 129 | } |
| 130 | } |
| 131 | close($conffile); |
| 132 | unshift(@ARGV, @conf_args) if @conf_args; |
| 133 | } |
| 134 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 135 | GetOptions( |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 136 | 'q|quiet+' => \$quiet, |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 137 | 'tree!' => \$tree, |
| 138 | 'signoff!' => \$chk_signoff, |
| 139 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 140 | 'emacs!' => \$emacs, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 141 | 'terse!' => \$terse, |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 142 | 'showfile!' => \$showfile, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 143 | 'f|file!' => \$file, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 144 | 'subjective!' => \$check, |
| 145 | 'strict!' => \$check, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 146 | 'ignore=s' => \@ignore, |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 147 | 'types=s' => \@use, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 148 | 'show-types!' => \$show_types, |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 149 | 'max-line-length=i' => \$max_line_length, |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 150 | 'min-conf-desc-length=i' => \$min_conf_desc_length, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 151 | 'root=s' => \$root, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 152 | 'summary!' => \$summary, |
| 153 | 'mailback!' => \$mailback, |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 154 | 'summary-file!' => \$summary_file, |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 155 | 'fix!' => \$fix, |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 156 | 'fix-inplace!' => \$fix_inplace, |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 157 | 'ignore-perl-version!' => \$ignore_perl_version, |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 158 | 'debug=s' => \%debug, |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 159 | 'test-only=s' => \$tst_only, |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 160 | 'codespell!' => \$codespell, |
| 161 | 'codespellfile=s' => \$codespellfile, |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 162 | 'color!' => \$color, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 163 | 'h|help' => \$help, |
| 164 | 'version' => \$help |
| 165 | ) or help(1); |
| 166 | |
| 167 | help(0) if ($help); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 168 | |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 169 | $fix = 1 if ($fix_inplace); |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 170 | $check_orig = $check; |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 171 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 172 | my $exit = 0; |
| 173 | |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 174 | if ($^V && $^V lt $minimum_perl_version) { |
| 175 | printf "$P: requires at least perl version %vd\n", $minimum_perl_version; |
| 176 | if (!$ignore_perl_version) { |
| 177 | exit(1); |
| 178 | } |
| 179 | } |
| 180 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 181 | if ($#ARGV < 0) { |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 182 | print "$P: no input files\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 183 | exit(1); |
| 184 | } |
| 185 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 186 | sub hash_save_array_words { |
| 187 | my ($hashRef, $arrayRef) = @_; |
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 | my @array = split(/,/, join(',', @$arrayRef)); |
| 190 | foreach my $word (@array) { |
| 191 | $word =~ s/\s*\n?$//g; |
| 192 | $word =~ s/^\s*//g; |
| 193 | $word =~ s/\s+/ /g; |
| 194 | $word =~ tr/[a-z]/[A-Z]/; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 195 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 196 | next if ($word =~ m/^\s*#/); |
| 197 | next if ($word =~ m/^\s*$/); |
| 198 | |
| 199 | $hashRef->{$word}++; |
| 200 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 203 | sub hash_show_words { |
| 204 | my ($hashRef, $prefix) = @_; |
| 205 | |
Joe Perches | 3c816e4 | 2015-06-25 15:03:29 -0700 | [diff] [blame] | 206 | if (keys %$hashRef) { |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 207 | print "\nNOTE: $prefix message types:"; |
Joe Perches | 58cb3cf | 2013-09-11 14:24:04 -0700 | [diff] [blame] | 208 | foreach my $word (sort keys %$hashRef) { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 209 | print " $word"; |
| 210 | } |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 211 | print "\n"; |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | hash_save_array_words(\%ignore_type, \@ignore); |
| 216 | hash_save_array_words(\%use_type, \@use); |
| 217 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 218 | my $dbg_values = 0; |
| 219 | my $dbg_possible = 0; |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 220 | my $dbg_type = 0; |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 221 | my $dbg_attr = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 222 | for my $key (keys %debug) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 223 | ## no critic |
| 224 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 225 | die "$@" if ($@); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 228 | my $rpt_cleaners = 0; |
| 229 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 230 | if ($terse) { |
| 231 | $emacs = 1; |
| 232 | $quiet++; |
| 233 | } |
| 234 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 235 | if ($tree) { |
| 236 | if (defined $root) { |
| 237 | if (!top_of_kernel_tree($root)) { |
| 238 | die "$P: $root: --root does not point at a valid tree\n"; |
| 239 | } |
| 240 | } else { |
| 241 | if (top_of_kernel_tree('.')) { |
| 242 | $root = '.'; |
| 243 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 244 | top_of_kernel_tree($1)) { |
| 245 | $root = $1; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (!defined $root) { |
| 250 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 251 | exit(2); |
| 252 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 255 | my $emitted_corrupt = 0; |
| 256 | |
Andy Whitcroft | 2ceb532 | 2009-10-26 16:50:14 -0700 | [diff] [blame] | 257 | our $Ident = qr{ |
| 258 | [A-Za-z_][A-Za-z\d_]* |
| 259 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 260 | }x; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 261 | our $Storage = qr{extern|static|asmlinkage}; |
| 262 | our $Sparse = qr{ |
| 263 | __user| |
| 264 | __kernel| |
| 265 | __force| |
| 266 | __iomem| |
Joe Perches | 54507b5 | 2015-09-09 15:37:55 -0700 | [diff] [blame] | 267 | __pmem| |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 268 | __must_check| |
| 269 | __init_refok| |
Andy Whitcroft | 417495e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 270 | __kprobes| |
Sven Eckelmann | 165e72a | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 271 | __ref| |
| 272 | __rcu |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 273 | }x; |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 274 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; |
| 275 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; |
| 276 | our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; |
| 277 | our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; |
| 278 | our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 279 | |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 280 | # Notes to $Attribute: |
| 281 | # 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] | 282 | our $Attribute = qr{ |
| 283 | const| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 284 | __percpu| |
| 285 | __nocast| |
| 286 | __safe| |
| 287 | __bitwise__| |
| 288 | __packed__| |
| 289 | __packed2__| |
| 290 | __naked| |
| 291 | __maybe_unused| |
| 292 | __always_unused| |
| 293 | __noreturn| |
| 294 | __used| |
| 295 | __cold| |
Joe Perches | e23ef1f | 2015-02-13 14:38:24 -0800 | [diff] [blame] | 296 | __pure| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 297 | __noclone| |
| 298 | __deprecated| |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 299 | __read_mostly| |
| 300 | __kprobes| |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 301 | $InitAttribute| |
Andy Whitcroft | 24e1d81 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 302 | ____cacheline_aligned| |
| 303 | ____cacheline_aligned_in_smp| |
Andy Whitcroft | 5fe3af1 | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 304 | ____cacheline_internodealigned_in_smp| |
| 305 | __weak |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 306 | }x; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 307 | our $Modifier; |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 308 | our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 309 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 310 | our $Lval = qr{$Ident(?:$Member)*}; |
| 311 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 312 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 313 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 314 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 315 | our $Int = qr{[0-9]+$Int_type?}; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 316 | our $Octal = qr{0[0-7]+$Int_type?}; |
Joe Perches | c0a5c89 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 317 | our $String = qr{"[X\t]*"}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 318 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 319 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 320 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
Joe Perches | 74349bc | 2012-12-17 16:02:05 -0800 | [diff] [blame] | 321 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 322 | our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 323 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Joe Perches | 447432f | 2014-04-03 14:49:17 -0700 | [diff] [blame] | 324 | our $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 325 | our $Arithmetic = qr{\+|-|\*|\/|%}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 326 | our $Operators = qr{ |
| 327 | <=|>=|==|!=| |
| 328 | =>|->|<<|>>|<|>|!|~| |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 329 | &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 330 | }x; |
| 331 | |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 332 | our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; |
| 333 | |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 334 | our $BasicType; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 335 | our $NonptrType; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 336 | our $NonptrTypeMisordered; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 337 | our $NonptrTypeWithAttr; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 338 | our $Type; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 339 | our $TypeMisordered; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 340 | our $Declare; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 341 | our $DeclareMisordered; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 342 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 343 | our $NON_ASCII_UTF8 = qr{ |
| 344 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 345 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 346 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 347 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 348 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 349 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 350 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 351 | }x; |
| 352 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 353 | our $UTF8 = qr{ |
| 354 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 355 | | $NON_ASCII_UTF8 |
| 356 | }x; |
| 357 | |
Joe Perches | e6176fa | 2015-06-25 15:02:49 -0700 | [diff] [blame] | 358 | our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; |
Joe Perches | 021158b | 2015-02-13 14:38:43 -0800 | [diff] [blame] | 359 | our $typeOtherOSTypedefs = qr{(?x: |
| 360 | u_(?:char|short|int|long) | # bsd |
| 361 | u(?:nchar|short|int|long) # sysv |
| 362 | )}; |
Joe Perches | e6176fa | 2015-06-25 15:02:49 -0700 | [diff] [blame] | 363 | our $typeKernelTypedefs = qr{(?x: |
Andy Whitcroft | fb9e909 | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 364 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 365 | atomic_t |
| 366 | )}; |
Joe Perches | e6176fa | 2015-06-25 15:02:49 -0700 | [diff] [blame] | 367 | our $typeTypedefs = qr{(?x: |
| 368 | $typeC99Typedefs\b| |
| 369 | $typeOtherOSTypedefs\b| |
| 370 | $typeKernelTypedefs\b |
| 371 | )}; |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 372 | |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 373 | our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; |
| 374 | |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 375 | our $logFunctions = qr{(?x: |
Joe Perches | 6e60c02 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 376 | printk(?:_ratelimited|_once|)| |
Jacob Keller | 7d0b659 | 2013-07-03 15:05:35 -0700 | [diff] [blame] | 377 | (?:[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] | 378 | WARN(?:_RATELIMIT|_ONCE|)| |
Joe Perches | b053172 | 2011-05-24 17:13:40 -0700 | [diff] [blame] | 379 | panic| |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 380 | MODULE_[A-Z_]+| |
| 381 | seq_vprintf|seq_printf|seq_puts |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 382 | )}; |
| 383 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 384 | our $signature_tags = qr{(?xi: |
| 385 | Signed-off-by:| |
| 386 | Acked-by:| |
| 387 | Tested-by:| |
| 388 | Reviewed-by:| |
| 389 | Reported-by:| |
Mugunthan V N | 8543ae1 | 2013-04-29 16:18:17 -0700 | [diff] [blame] | 390 | Suggested-by:| |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 391 | To:| |
| 392 | Cc: |
| 393 | )}; |
| 394 | |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 395 | our @typeListMisordered = ( |
| 396 | qr{char\s+(?:un)?signed}, |
| 397 | qr{int\s+(?:(?:un)?signed\s+)?short\s}, |
| 398 | qr{int\s+short(?:\s+(?:un)?signed)}, |
| 399 | qr{short\s+int(?:\s+(?:un)?signed)}, |
| 400 | qr{(?:un)?signed\s+int\s+short}, |
| 401 | qr{short\s+(?:un)?signed}, |
| 402 | qr{long\s+int\s+(?:un)?signed}, |
| 403 | qr{int\s+long\s+(?:un)?signed}, |
| 404 | qr{long\s+(?:un)?signed\s+int}, |
| 405 | qr{int\s+(?:un)?signed\s+long}, |
| 406 | qr{int\s+(?:un)?signed}, |
| 407 | qr{int\s+long\s+long\s+(?:un)?signed}, |
| 408 | qr{long\s+long\s+int\s+(?:un)?signed}, |
| 409 | qr{long\s+long\s+(?:un)?signed\s+int}, |
| 410 | qr{long\s+long\s+(?:un)?signed}, |
| 411 | qr{long\s+(?:un)?signed}, |
| 412 | ); |
| 413 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 414 | our @typeList = ( |
| 415 | qr{void}, |
Joe Perches | 0c773d9 | 2014-08-06 16:11:20 -0700 | [diff] [blame] | 416 | qr{(?:(?:un)?signed\s+)?char}, |
| 417 | qr{(?:(?:un)?signed\s+)?short\s+int}, |
| 418 | qr{(?:(?:un)?signed\s+)?short}, |
| 419 | qr{(?:(?:un)?signed\s+)?int}, |
| 420 | qr{(?:(?:un)?signed\s+)?long\s+int}, |
| 421 | qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, |
| 422 | qr{(?:(?:un)?signed\s+)?long\s+long}, |
| 423 | qr{(?:(?:un)?signed\s+)?long}, |
| 424 | qr{(?:un)?signed}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 425 | qr{float}, |
| 426 | qr{double}, |
| 427 | qr{bool}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 428 | qr{struct\s+$Ident}, |
| 429 | qr{union\s+$Ident}, |
| 430 | qr{enum\s+$Ident}, |
| 431 | qr{${Ident}_t}, |
| 432 | qr{${Ident}_handler}, |
| 433 | qr{${Ident}_handler_fn}, |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 434 | @typeListMisordered, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 435 | ); |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 436 | our @typeListFile = (); |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 437 | our @typeListWithAttr = ( |
| 438 | @typeList, |
| 439 | qr{struct\s+$InitAttribute\s+$Ident}, |
| 440 | qr{union\s+$InitAttribute\s+$Ident}, |
| 441 | ); |
| 442 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 443 | our @modifierList = ( |
| 444 | qr{fastcall}, |
| 445 | ); |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 446 | our @modifierListFile = (); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 447 | |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 448 | our @mode_permission_funcs = ( |
| 449 | ["module_param", 3], |
| 450 | ["module_param_(?:array|named|string)", 4], |
| 451 | ["module_param_array_named", 5], |
| 452 | ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], |
| 453 | ["proc_create(?:_data|)", 2], |
| 454 | ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2], |
| 455 | ); |
| 456 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 457 | #Create a search pattern for all these functions to speed up a loop below |
| 458 | our $mode_perms_search = ""; |
| 459 | foreach my $entry (@mode_permission_funcs) { |
| 460 | $mode_perms_search .= '|' if ($mode_perms_search ne ""); |
| 461 | $mode_perms_search .= $entry->[0]; |
| 462 | } |
| 463 | |
Joe Perches | b392c64 | 2015-04-16 12:44:16 -0700 | [diff] [blame] | 464 | our $mode_perms_world_writable = qr{ |
| 465 | S_IWUGO | |
| 466 | S_IWOTH | |
| 467 | S_IRWXUGO | |
| 468 | S_IALLUGO | |
| 469 | 0[0-7][0-7][2367] |
| 470 | }x; |
| 471 | |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 472 | our $allowed_asm_includes = qr{(?x: |
| 473 | irq| |
Sergey Ryazanov | cdcee68 | 2014-10-13 15:51:44 -0700 | [diff] [blame] | 474 | memory| |
| 475 | time| |
| 476 | reboot |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 477 | )}; |
| 478 | # memory.h: ARM has a custom one |
| 479 | |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 480 | # Load common spelling mistakes and build regular expression list. |
| 481 | my $misspellings; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 482 | my %spelling_fix; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 483 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 484 | if (open(my $spelling, '<', $spelling_file)) { |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 485 | while (<$spelling>) { |
| 486 | my $line = $_; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 487 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 488 | $line =~ s/\s*\n?$//g; |
| 489 | $line =~ s/^\s*//g; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 490 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 491 | next if ($line =~ m/^\s*#/); |
| 492 | next if ($line =~ m/^\s*$/); |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 493 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 494 | my ($suspect, $fix) = split(/\|\|/, $line); |
| 495 | |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 496 | $spelling_fix{$suspect} = $fix; |
| 497 | } |
| 498 | close($spelling); |
Joe Perches | 36061e3 | 2014-12-10 15:51:43 -0800 | [diff] [blame] | 499 | } else { |
| 500 | warn "No typos will be found - file '$spelling_file': $!\n"; |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 501 | } |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 502 | |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 503 | if ($codespell) { |
| 504 | if (open(my $spelling, '<', $codespellfile)) { |
| 505 | while (<$spelling>) { |
| 506 | my $line = $_; |
| 507 | |
| 508 | $line =~ s/\s*\n?$//g; |
| 509 | $line =~ s/^\s*//g; |
| 510 | |
| 511 | next if ($line =~ m/^\s*#/); |
| 512 | next if ($line =~ m/^\s*$/); |
| 513 | next if ($line =~ m/, disabled/i); |
| 514 | |
| 515 | $line =~ s/,.*$//; |
| 516 | |
| 517 | my ($suspect, $fix) = split(/->/, $line); |
| 518 | |
| 519 | $spelling_fix{$suspect} = $fix; |
| 520 | } |
| 521 | close($spelling); |
| 522 | } else { |
| 523 | warn "No codespell typos will be found - file '$codespellfile': $!\n"; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; |
| 528 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 529 | sub build_types { |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 530 | my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; |
| 531 | my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 532 | my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 533 | my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 534 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 535 | $BasicType = qr{ |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 536 | (?:$typeTypedefs\b)| |
| 537 | (?:${all}\b) |
| 538 | }x; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 539 | $NonptrType = qr{ |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 540 | (?:$Modifier\s+|const\s+)* |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 541 | (?: |
Andy Whitcroft | 6b48db2 | 2012-01-10 15:10:13 -0800 | [diff] [blame] | 542 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 543 | (?:$typeTypedefs\b)| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 544 | (?:${all}\b) |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 545 | ) |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 546 | (?:\s+$Modifier|\s+const)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 547 | }x; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 548 | $NonptrTypeMisordered = qr{ |
| 549 | (?:$Modifier\s+|const\s+)* |
| 550 | (?: |
| 551 | (?:${Misordered}\b) |
| 552 | ) |
| 553 | (?:\s+$Modifier|\s+const)* |
| 554 | }x; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 555 | $NonptrTypeWithAttr = qr{ |
| 556 | (?:$Modifier\s+|const\s+)* |
| 557 | (?: |
| 558 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 559 | (?:$typeTypedefs\b)| |
| 560 | (?:${allWithAttr}\b) |
| 561 | ) |
| 562 | (?:\s+$Modifier|\s+const)* |
| 563 | }x; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 564 | $Type = qr{ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 565 | $NonptrType |
Joe Perches | 1574a29 | 2014-08-06 16:10:50 -0700 | [diff] [blame] | 566 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 567 | (?:\s+$Inline|\s+$Modifier)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 568 | }x; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 569 | $TypeMisordered = qr{ |
| 570 | $NonptrTypeMisordered |
| 571 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? |
| 572 | (?:\s+$Inline|\s+$Modifier)* |
| 573 | }x; |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 574 | $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 575 | $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 576 | } |
| 577 | build_types(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 578 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 579 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 580 | |
| 581 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 582 | # requires at least perl version v5.10.0 |
| 583 | # Any use must be runtime checked with $^V |
| 584 | |
| 585 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 586 | our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; |
Joe Perches | c0a5c89 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 587 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 588 | |
Joe Perches | f842230 | 2014-08-06 16:11:31 -0700 | [diff] [blame] | 589 | our $declaration_macros = qr{(?x: |
Joe Perches | 3e838b6 | 2015-09-09 15:37:33 -0700 | [diff] [blame] | 590 | (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| |
Joe Perches | f842230 | 2014-08-06 16:11:31 -0700 | [diff] [blame] | 591 | (?:$Storage\s+)?LIST_HEAD\s*\(| |
| 592 | (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\( |
| 593 | )}; |
| 594 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 595 | sub deparenthesize { |
| 596 | my ($string) = @_; |
| 597 | return "" if (!defined($string)); |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 598 | |
| 599 | while ($string =~ /^\s*\(.*\)\s*$/) { |
| 600 | $string =~ s@^\s*\(\s*@@; |
| 601 | $string =~ s@\s*\)\s*$@@; |
| 602 | } |
| 603 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 604 | $string =~ s@\s+@ @g; |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 605 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 606 | return $string; |
| 607 | } |
| 608 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 609 | sub seed_camelcase_file { |
| 610 | my ($file) = @_; |
| 611 | |
| 612 | return if (!(-f $file)); |
| 613 | |
| 614 | local $/; |
| 615 | |
| 616 | open(my $include_file, '<', "$file") |
| 617 | or warn "$P: Can't read '$file' $!\n"; |
| 618 | my $text = <$include_file>; |
| 619 | close($include_file); |
| 620 | |
| 621 | my @lines = split('\n', $text); |
| 622 | |
| 623 | foreach my $line (@lines) { |
| 624 | next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); |
| 625 | if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { |
| 626 | $camelcase{$1} = 1; |
Joe Perches | 11ea516 | 2013-11-12 15:10:08 -0800 | [diff] [blame] | 627 | } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { |
| 628 | $camelcase{$1} = 1; |
| 629 | } 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] | 630 | $camelcase{$1} = 1; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | my $camelcase_seeded = 0; |
| 636 | sub seed_camelcase_includes { |
| 637 | return if ($camelcase_seeded); |
| 638 | |
| 639 | my $files; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 640 | my $camelcase_cache = ""; |
| 641 | my @include_files = (); |
| 642 | |
| 643 | $camelcase_seeded = 1; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 644 | |
Richard Genoud | 3645e32 | 2014-02-10 14:25:32 -0800 | [diff] [blame] | 645 | if (-e ".git") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 646 | my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; |
| 647 | chomp $git_last_include_commit; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 648 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 649 | } else { |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 650 | my $last_mod_date = 0; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 651 | $files = `find $root/include -name "*.h"`; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 652 | @include_files = split('\n', $files); |
| 653 | foreach my $file (@include_files) { |
| 654 | my $date = POSIX::strftime("%Y%m%d%H%M", |
| 655 | localtime((stat $file)[9])); |
| 656 | $last_mod_date = $date if ($last_mod_date < $date); |
| 657 | } |
| 658 | $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 659 | } |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 660 | |
| 661 | if ($camelcase_cache ne "" && -f $camelcase_cache) { |
| 662 | open(my $camelcase_file, '<', "$camelcase_cache") |
| 663 | or warn "$P: Can't read '$camelcase_cache' $!\n"; |
| 664 | while (<$camelcase_file>) { |
| 665 | chomp; |
| 666 | $camelcase{$_} = 1; |
| 667 | } |
| 668 | close($camelcase_file); |
| 669 | |
| 670 | return; |
| 671 | } |
| 672 | |
Richard Genoud | 3645e32 | 2014-02-10 14:25:32 -0800 | [diff] [blame] | 673 | if (-e ".git") { |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 674 | $files = `git ls-files "include/*.h"`; |
| 675 | @include_files = split('\n', $files); |
| 676 | } |
| 677 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 678 | foreach my $file (@include_files) { |
| 679 | seed_camelcase_file($file); |
| 680 | } |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 681 | |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 682 | if ($camelcase_cache ne "") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 683 | unlink glob ".checkpatch-camelcase.*"; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 684 | open(my $camelcase_file, '>', "$camelcase_cache") |
| 685 | or warn "$P: Can't write '$camelcase_cache' $!\n"; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 686 | foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { |
| 687 | print $camelcase_file ("$_\n"); |
| 688 | } |
| 689 | close($camelcase_file); |
| 690 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 693 | sub git_commit_info { |
| 694 | my ($commit, $id, $desc) = @_; |
| 695 | |
| 696 | return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); |
| 697 | |
| 698 | my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; |
| 699 | $output =~ s/^\s*//gm; |
| 700 | my @lines = split("\n", $output); |
| 701 | |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 702 | return ($id, $desc) if ($#lines < 0); |
| 703 | |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 704 | if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { |
| 705 | # Maybe one day convert this block of bash into something that returns |
| 706 | # all matching commit ids, but it's very slow... |
| 707 | # |
| 708 | # echo "checking commits $1..." |
| 709 | # git rev-list --remotes | grep -i "^$1" | |
| 710 | # while read line ; do |
| 711 | # git log --format='%H %s' -1 $line | |
| 712 | # echo "commit $(cut -c 1-12,41-)" |
| 713 | # done |
| 714 | } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { |
| 715 | } else { |
| 716 | $id = substr($lines[0], 0, 12); |
| 717 | $desc = substr($lines[0], 41); |
| 718 | } |
| 719 | |
| 720 | return ($id, $desc); |
| 721 | } |
| 722 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 723 | $chk_signoff = 0 if ($file); |
| 724 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 725 | my @rawlines = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 726 | my @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 727 | my @fixed = (); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 728 | my @fixed_inserted = (); |
| 729 | my @fixed_deleted = (); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 730 | my $fixlinenr = -1; |
| 731 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 732 | my $vname; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 733 | for my $filename (@ARGV) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 734 | my $FILE; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 735 | if ($file) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 736 | open($FILE, '-|', "diff -u /dev/null $filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 737 | die "$P: $filename: diff failed - $!\n"; |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 738 | } elsif ($filename eq '-') { |
| 739 | open($FILE, '<&STDIN'); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 740 | } else { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 741 | open($FILE, '<', "$filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 742 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 743 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 744 | if ($filename eq '-') { |
| 745 | $vname = 'Your patch'; |
| 746 | } else { |
| 747 | $vname = $filename; |
| 748 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 749 | while (<$FILE>) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 750 | chomp; |
| 751 | push(@rawlines, $_); |
| 752 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 753 | close($FILE); |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 754 | |
| 755 | if ($#ARGV > 0 && $quiet == 0) { |
| 756 | print '-' x length($vname) . "\n"; |
| 757 | print "$vname\n"; |
| 758 | print '-' x length($vname) . "\n"; |
| 759 | } |
| 760 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 761 | if (!process($filename)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 762 | $exit = 1; |
| 763 | } |
| 764 | @rawlines = (); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 765 | @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 766 | @fixed = (); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 767 | @fixed_inserted = (); |
| 768 | @fixed_deleted = (); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 769 | $fixlinenr = -1; |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 770 | @modifierListFile = (); |
| 771 | @typeListFile = (); |
| 772 | build_types(); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 775 | if (!$quiet) { |
Joe Perches | 3c816e4 | 2015-06-25 15:03:29 -0700 | [diff] [blame] | 776 | hash_show_words(\%use_type, "Used"); |
| 777 | hash_show_words(\%ignore_type, "Ignored"); |
| 778 | |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 779 | if ($^V lt 5.10.0) { |
| 780 | print << "EOM" |
| 781 | |
| 782 | NOTE: perl $^V is not modern enough to detect all possible issues. |
| 783 | An upgrade to at least perl v5.10.0 is suggested. |
| 784 | EOM |
| 785 | } |
| 786 | if ($exit) { |
| 787 | print << "EOM" |
| 788 | |
| 789 | NOTE: If any of the errors are false positives, please report |
| 790 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 791 | EOM |
| 792 | } |
| 793 | } |
| 794 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 795 | exit($exit); |
| 796 | |
| 797 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 798 | my ($root) = @_; |
| 799 | |
| 800 | my @tree_check = ( |
| 801 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 802 | "README", "Documentation", "arch", "include", "drivers", |
| 803 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 804 | ); |
| 805 | |
| 806 | foreach my $check (@tree_check) { |
| 807 | if (! -e $root . '/' . $check) { |
| 808 | return 0; |
| 809 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 810 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 811 | return 1; |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 812 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 813 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 814 | sub parse_email { |
| 815 | my ($formatted_email) = @_; |
| 816 | |
| 817 | my $name = ""; |
| 818 | my $address = ""; |
| 819 | my $comment = ""; |
| 820 | |
| 821 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 822 | $name = $1; |
| 823 | $address = $2; |
| 824 | $comment = $3 if defined $3; |
| 825 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 826 | $address = $1; |
| 827 | $comment = $2 if defined $2; |
| 828 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 829 | $address = $1; |
| 830 | $comment = $2 if defined $2; |
| 831 | $formatted_email =~ s/$address.*$//; |
| 832 | $name = $formatted_email; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 833 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 834 | $name =~ s/^\"|\"$//g; |
| 835 | # If there's a name left after stripping spaces and |
| 836 | # leading quotes, and the address doesn't have both |
| 837 | # leading and trailing angle brackets, the address |
| 838 | # is invalid. ie: |
| 839 | # "joe smith joe@smith.com" bad |
| 840 | # "joe smith <joe@smith.com" bad |
| 841 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 842 | $name = ""; |
| 843 | $address = ""; |
| 844 | $comment = ""; |
| 845 | } |
| 846 | } |
| 847 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 848 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 849 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 850 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 851 | $address =~ s/^\<|\>$//g; |
| 852 | |
| 853 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 854 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 855 | $name = "\"$name\""; |
| 856 | } |
| 857 | |
| 858 | return ($name, $address, $comment); |
| 859 | } |
| 860 | |
| 861 | sub format_email { |
| 862 | my ($name, $address) = @_; |
| 863 | |
| 864 | my $formatted_email; |
| 865 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 866 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 867 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 868 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 869 | |
| 870 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 871 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 872 | $name = "\"$name\""; |
| 873 | } |
| 874 | |
| 875 | if ("$name" eq "") { |
| 876 | $formatted_email = "$address"; |
| 877 | } else { |
| 878 | $formatted_email = "$name <$address>"; |
| 879 | } |
| 880 | |
| 881 | return $formatted_email; |
| 882 | } |
| 883 | |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 884 | sub which { |
Joe Perches | bd474ca | 2014-08-06 16:11:10 -0700 | [diff] [blame] | 885 | my ($bin) = @_; |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 886 | |
Joe Perches | bd474ca | 2014-08-06 16:11:10 -0700 | [diff] [blame] | 887 | foreach my $path (split(/:/, $ENV{PATH})) { |
| 888 | if (-e "$path/$bin") { |
| 889 | return "$path/$bin"; |
| 890 | } |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 891 | } |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 892 | |
Joe Perches | bd474ca | 2014-08-06 16:11:10 -0700 | [diff] [blame] | 893 | return ""; |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 896 | sub which_conf { |
| 897 | my ($conf) = @_; |
| 898 | |
| 899 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 900 | if (-e "$path/$conf") { |
| 901 | return "$path/$conf"; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | return ""; |
| 906 | } |
| 907 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 908 | sub expand_tabs { |
| 909 | my ($str) = @_; |
| 910 | |
| 911 | my $res = ''; |
| 912 | my $n = 0; |
| 913 | for my $c (split(//, $str)) { |
| 914 | if ($c eq "\t") { |
| 915 | $res .= ' '; |
| 916 | $n++; |
| 917 | for (; ($n % 8) != 0; $n++) { |
| 918 | $res .= ' '; |
| 919 | } |
| 920 | next; |
| 921 | } |
| 922 | $res .= $c; |
| 923 | $n++; |
| 924 | } |
| 925 | |
| 926 | return $res; |
| 927 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 928 | sub copy_spacing { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 929 | (my $res = shift) =~ tr/\t/ /c; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 930 | return $res; |
| 931 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 932 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 933 | sub line_stats { |
| 934 | my ($line) = @_; |
| 935 | |
| 936 | # Drop the diff line leader and expand tabs |
| 937 | $line =~ s/^.//; |
| 938 | $line = expand_tabs($line); |
| 939 | |
| 940 | # Pick the indent from the front of the line. |
| 941 | my ($white) = ($line =~ /^(\s*)/); |
| 942 | |
| 943 | return (length($line), length($white)); |
| 944 | } |
| 945 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 946 | my $sanitise_quote = ''; |
| 947 | |
| 948 | sub sanitise_line_reset { |
| 949 | my ($in_comment) = @_; |
| 950 | |
| 951 | if ($in_comment) { |
| 952 | $sanitise_quote = '*/'; |
| 953 | } else { |
| 954 | $sanitise_quote = ''; |
| 955 | } |
| 956 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 957 | sub sanitise_line { |
| 958 | my ($line) = @_; |
| 959 | |
| 960 | my $res = ''; |
| 961 | my $l = ''; |
| 962 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 963 | my $qlen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 964 | my $off = 0; |
| 965 | my $c; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 966 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 967 | # Always copy over the diff marker. |
| 968 | $res = substr($line, 0, 1); |
| 969 | |
| 970 | for ($off = 1; $off < length($line); $off++) { |
| 971 | $c = substr($line, $off, 1); |
| 972 | |
| 973 | # Comments we are wacking completly including the begin |
| 974 | # and end, all to $;. |
| 975 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 976 | $sanitise_quote = '*/'; |
| 977 | |
| 978 | substr($res, $off, 2, "$;$;"); |
| 979 | $off++; |
| 980 | next; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 981 | } |
Andy Whitcroft | 81bc0e0 | 2008-10-15 22:02:26 -0700 | [diff] [blame] | 982 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 983 | $sanitise_quote = ''; |
| 984 | substr($res, $off, 2, "$;$;"); |
| 985 | $off++; |
| 986 | next; |
| 987 | } |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 988 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 989 | $sanitise_quote = '//'; |
| 990 | |
| 991 | substr($res, $off, 2, $sanitise_quote); |
| 992 | $off++; |
| 993 | next; |
| 994 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 995 | |
| 996 | # A \ in a string means ignore the next character. |
| 997 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 998 | $c eq "\\") { |
| 999 | substr($res, $off, 2, 'XX'); |
| 1000 | $off++; |
| 1001 | next; |
| 1002 | } |
| 1003 | # Regular quotes. |
| 1004 | if ($c eq "'" || $c eq '"') { |
| 1005 | if ($sanitise_quote eq '') { |
| 1006 | $sanitise_quote = $c; |
| 1007 | |
| 1008 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1009 | next; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1010 | } elsif ($sanitise_quote eq $c) { |
| 1011 | $sanitise_quote = ''; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1012 | } |
| 1013 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1014 | |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 1015 | #print "c<$c> SQ<$sanitise_quote>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1016 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 1017 | substr($res, $off, 1, $;); |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 1018 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 1019 | substr($res, $off, 1, $;); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1020 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 1021 | substr($res, $off, 1, 'X'); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1022 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1023 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1024 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 1027 | if ($sanitise_quote eq '//') { |
| 1028 | $sanitise_quote = ''; |
| 1029 | } |
| 1030 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1031 | # The pathname on a #include may be surrounded by '<' and '>'. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1032 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1033 | my $clean = 'X' x length($1); |
| 1034 | $res =~ s@\<.*\>@<$clean>@; |
| 1035 | |
| 1036 | # The whole of a #error is a string. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1037 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1038 | my $clean = 'X' x length($1); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1039 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1042 | return $res; |
| 1043 | } |
| 1044 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 1045 | sub get_quoted_string { |
| 1046 | my ($line, $rawline) = @_; |
| 1047 | |
Joe Perches | 33acb54 | 2015-06-25 15:02:54 -0700 | [diff] [blame] | 1048 | return "" if ($line !~ m/($String)/g); |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 1049 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 1050 | } |
| 1051 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1052 | sub ctx_statement_block { |
| 1053 | my ($linenr, $remain, $off) = @_; |
| 1054 | my $line = $linenr - 1; |
| 1055 | my $blk = ''; |
| 1056 | my $soff = $off; |
| 1057 | my $coff = $off - 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1058 | my $coff_set = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1059 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1060 | my $loff = 0; |
| 1061 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1062 | my $type = ''; |
| 1063 | my $level = 0; |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 1064 | my @stack = (); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1065 | my $p; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1066 | my $c; |
| 1067 | my $len = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1068 | |
| 1069 | my $remainder; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1070 | while (1) { |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 1071 | @stack = (['', 0]) if ($#stack == -1); |
| 1072 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1073 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1074 | # If we are about to drop off the end, pull in more |
| 1075 | # context. |
| 1076 | if ($off >= $len) { |
| 1077 | for (; $remain > 0; $line++) { |
Andy Whitcroft | dea3349 | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 1078 | last if (!defined $lines[$line]); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1079 | next if ($lines[$line] =~ /^-/); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1080 | $remain--; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1081 | $loff = $len; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1082 | $blk .= $lines[$line] . "\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1083 | $len = length($blk); |
| 1084 | $line++; |
| 1085 | last; |
| 1086 | } |
| 1087 | # Bail if there is no further context. |
| 1088 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1089 | if ($off >= $len) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1090 | last; |
| 1091 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 1092 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 1093 | $level++; |
| 1094 | $type = '#'; |
| 1095 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1096 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1097 | $p = $c; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1098 | $c = substr($blk, $off, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1099 | $remainder = substr($blk, $off); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1100 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1101 | #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] | 1102 | |
| 1103 | # Handle nested #if/#else. |
| 1104 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 1105 | push(@stack, [ $type, $level ]); |
| 1106 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 1107 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 1108 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 1109 | ($type, $level) = @{pop(@stack)}; |
| 1110 | } |
| 1111 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1112 | # Statement ends at the ';' or a close '}' at the |
| 1113 | # outermost level. |
| 1114 | if ($level == 0 && $c eq ';') { |
| 1115 | last; |
| 1116 | } |
| 1117 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1118 | # 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] | 1119 | if ($level == 0 && $coff_set == 0 && |
| 1120 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 1121 | $remainder =~ /^(else)(?:\s|{)/ && |
| 1122 | $remainder !~ /^else\s+if\b/) { |
| 1123 | $coff = $off + length($1) - 1; |
| 1124 | $coff_set = 1; |
| 1125 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 1126 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1127 | } |
| 1128 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1129 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 1130 | $level++; |
| 1131 | $type = '('; |
| 1132 | } |
| 1133 | if ($type eq '(' && $c eq ')') { |
| 1134 | $level--; |
| 1135 | $type = ($level != 0)? '(' : ''; |
| 1136 | |
| 1137 | if ($level == 0 && $coff < $soff) { |
| 1138 | $coff = $off; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1139 | $coff_set = 1; |
| 1140 | #warn "CSB: mark coff<$coff>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 1144 | $level++; |
| 1145 | $type = '{'; |
| 1146 | } |
| 1147 | if ($type eq '{' && $c eq '}') { |
| 1148 | $level--; |
| 1149 | $type = ($level != 0)? '{' : ''; |
| 1150 | |
| 1151 | if ($level == 0) { |
Patrick Pannuto | b998e00 | 2010-08-09 17:21:03 -0700 | [diff] [blame] | 1152 | if (substr($blk, $off + 1, 1) eq ';') { |
| 1153 | $off++; |
| 1154 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1155 | last; |
| 1156 | } |
| 1157 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 1158 | # Preprocessor commands end at the newline unless escaped. |
| 1159 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 1160 | $level--; |
| 1161 | $type = ''; |
| 1162 | $off++; |
| 1163 | last; |
| 1164 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1165 | $off++; |
| 1166 | } |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 1167 | # We are truly at the end, so shuffle to the next line. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1168 | if ($off == $len) { |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 1169 | $loff = $len + 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1170 | $line++; |
| 1171 | $remain--; |
| 1172 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1173 | |
| 1174 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 1175 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 1176 | |
| 1177 | #warn "STATEMENT<$statement>\n"; |
| 1178 | #warn "CONDITION<$condition>\n"; |
| 1179 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1180 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1181 | |
| 1182 | return ($statement, $condition, |
| 1183 | $line, $remain + 1, $off - $loff + 1, $level); |
| 1184 | } |
| 1185 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1186 | sub statement_lines { |
| 1187 | my ($stmt) = @_; |
| 1188 | |
| 1189 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 1190 | $stmt =~ s/(^|\n)./$1/g; |
| 1191 | $stmt =~ s/^\s*//; |
| 1192 | $stmt =~ s/\s*$//; |
| 1193 | |
| 1194 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1195 | |
| 1196 | return $#stmt_lines + 2; |
| 1197 | } |
| 1198 | |
| 1199 | sub statement_rawlines { |
| 1200 | my ($stmt) = @_; |
| 1201 | |
| 1202 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1203 | |
| 1204 | return $#stmt_lines + 2; |
| 1205 | } |
| 1206 | |
| 1207 | sub statement_block_size { |
| 1208 | my ($stmt) = @_; |
| 1209 | |
| 1210 | $stmt =~ s/(^|\n)./$1/g; |
| 1211 | $stmt =~ s/^\s*{//; |
| 1212 | $stmt =~ s/}\s*$//; |
| 1213 | $stmt =~ s/^\s*//; |
| 1214 | $stmt =~ s/\s*$//; |
| 1215 | |
| 1216 | my @stmt_lines = ($stmt =~ /\n/g); |
| 1217 | my @stmt_statements = ($stmt =~ /;/g); |
| 1218 | |
| 1219 | my $stmt_lines = $#stmt_lines + 2; |
| 1220 | my $stmt_statements = $#stmt_statements + 1; |
| 1221 | |
| 1222 | if ($stmt_lines > $stmt_statements) { |
| 1223 | return $stmt_lines; |
| 1224 | } else { |
| 1225 | return $stmt_statements; |
| 1226 | } |
| 1227 | } |
| 1228 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1229 | sub ctx_statement_full { |
| 1230 | my ($linenr, $remain, $off) = @_; |
| 1231 | my ($statement, $condition, $level); |
| 1232 | |
| 1233 | my (@chunks); |
| 1234 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1235 | # Grab the first conditional/block pair. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1236 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1237 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1238 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1239 | push(@chunks, [ $condition, $statement ]); |
| 1240 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 1241 | return ($level, $linenr, @chunks); |
| 1242 | } |
| 1243 | |
| 1244 | # Pull in the following conditional/block pairs and see if they |
| 1245 | # could continue the statement. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1246 | for (;;) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1247 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 1248 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1249 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1250 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1251 | #print "C: push\n"; |
| 1252 | push(@chunks, [ $condition, $statement ]); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | return ($level, $linenr, @chunks); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1258 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1259 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1260 | my $line; |
| 1261 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1262 | my $blk = ''; |
| 1263 | my @o; |
| 1264 | my @c; |
| 1265 | my @res = (); |
| 1266 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1267 | my $level = 0; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1268 | my @stack = ($level); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1269 | for ($line = $start; $remain > 0; $line++) { |
| 1270 | next if ($rawlines[$line] =~ /^-/); |
| 1271 | $remain--; |
| 1272 | |
| 1273 | $blk .= $rawlines[$line]; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1274 | |
| 1275 | # Handle nested #if/#else. |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1276 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1277 | push(@stack, $level); |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1278 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1279 | $level = $stack[$#stack - 1]; |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1280 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1281 | $level = pop(@stack); |
| 1282 | } |
| 1283 | |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1284 | foreach my $c (split(//, $lines[$line])) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1285 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 1286 | if ($off > 0) { |
| 1287 | $off--; |
| 1288 | next; |
| 1289 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1290 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1291 | if ($c eq $close && $level > 0) { |
| 1292 | $level--; |
| 1293 | last if ($level == 0); |
| 1294 | } elsif ($c eq $open) { |
| 1295 | $level++; |
| 1296 | } |
| 1297 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1298 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1299 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1300 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1303 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1306 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1307 | } |
| 1308 | sub ctx_block_outer { |
| 1309 | my ($linenr, $remain) = @_; |
| 1310 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1311 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 1312 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1313 | } |
| 1314 | sub ctx_block { |
| 1315 | my ($linenr, $remain) = @_; |
| 1316 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1317 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1318 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1319 | } |
| 1320 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1321 | my ($linenr, $remain, $off) = @_; |
| 1322 | |
| 1323 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1324 | return @r; |
| 1325 | } |
| 1326 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1327 | my ($linenr, $remain) = @_; |
| 1328 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1329 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1330 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1331 | sub ctx_statement_level { |
| 1332 | my ($linenr, $remain, $off) = @_; |
| 1333 | |
| 1334 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1335 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1336 | |
| 1337 | sub ctx_locate_comment { |
| 1338 | my ($first_line, $end_line) = @_; |
| 1339 | |
| 1340 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | beae633 | 2008-07-23 21:28:59 -0700 | [diff] [blame] | 1341 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1342 | return $current_comment if (defined $current_comment); |
| 1343 | |
| 1344 | # Look through the context and try and figure out if there is a |
| 1345 | # comment. |
| 1346 | my $in_comment = 0; |
| 1347 | $current_comment = ''; |
| 1348 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1349 | my $line = $rawlines[$linenr - 1]; |
| 1350 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1351 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 1352 | $in_comment = 1; |
| 1353 | } |
| 1354 | if ($line =~ m@/\*@) { |
| 1355 | $in_comment = 1; |
| 1356 | } |
| 1357 | if (!$in_comment && $current_comment ne '') { |
| 1358 | $current_comment = ''; |
| 1359 | } |
| 1360 | $current_comment .= $line . "\n" if ($in_comment); |
| 1361 | if ($line =~ m@\*/@) { |
| 1362 | $in_comment = 0; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | chomp($current_comment); |
| 1367 | return($current_comment); |
| 1368 | } |
| 1369 | sub ctx_has_comment { |
| 1370 | my ($first_line, $end_line) = @_; |
| 1371 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 1372 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1373 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1374 | ##print "CMMT: $cmt\n"; |
| 1375 | |
| 1376 | return ($cmt ne ''); |
| 1377 | } |
| 1378 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1379 | sub raw_line { |
| 1380 | my ($linenr, $cnt) = @_; |
| 1381 | |
| 1382 | my $offset = $linenr - 1; |
| 1383 | $cnt++; |
| 1384 | |
| 1385 | my $line; |
| 1386 | while ($cnt) { |
| 1387 | $line = $rawlines[$offset++]; |
| 1388 | next if (defined($line) && $line =~ /^-/); |
| 1389 | $cnt--; |
| 1390 | } |
| 1391 | |
| 1392 | return $line; |
| 1393 | } |
| 1394 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1395 | sub cat_vet { |
| 1396 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1397 | my ($res, $coded); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1398 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1399 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1400 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 1401 | $res .= $1; |
| 1402 | if ($2 ne '') { |
| 1403 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 1404 | $res .= $coded; |
| 1405 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1406 | } |
| 1407 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1408 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1409 | return $res; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1412 | my $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1413 | my $av_pending; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1414 | my @av_paren_type; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1415 | my $av_pend_colon; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1416 | |
| 1417 | sub annotate_reset { |
| 1418 | $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1419 | $av_pending = '_'; |
| 1420 | @av_paren_type = ('E'); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1421 | $av_pend_colon = 'O'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1422 | } |
| 1423 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1424 | sub annotate_values { |
| 1425 | my ($stream, $type) = @_; |
| 1426 | |
| 1427 | my $res; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1428 | my $var = '_' x length($stream); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1429 | my $cur = $stream; |
| 1430 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1431 | print "$stream\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1432 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1433 | while (length($cur)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1434 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1435 | print " <" . join('', @av_paren_type) . |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1436 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1437 | if ($cur =~ /^(\s+)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1438 | print "WS($1)\n" if ($dbg_values > 1); |
| 1439 | if ($1 =~ /\n/ && $av_preprocessor) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1440 | $type = pop(@av_paren_type); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1441 | $av_preprocessor = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Florian Mickler | c023e473 | 2011-01-12 16:59:58 -0800 | [diff] [blame] | 1444 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1445 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1446 | push(@av_paren_type, $type); |
Andy Whitcroft | addcdce | 2012-01-10 15:10:11 -0800 | [diff] [blame] | 1447 | $type = 'c'; |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1448 | |
Andy Whitcroft | e91b6e2 | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1449 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1450 | print "DECLARE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1451 | $type = 'T'; |
| 1452 | |
Andy Whitcroft | 389a2fe | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 1453 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 1454 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 1455 | $type = 'T'; |
| 1456 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1457 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1458 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1459 | $av_preprocessor = 1; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1460 | push(@av_paren_type, $type); |
| 1461 | if ($2 ne '') { |
| 1462 | $av_pending = 'N'; |
| 1463 | } |
| 1464 | $type = 'E'; |
| 1465 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1466 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1467 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 1468 | $av_preprocessor = 1; |
| 1469 | push(@av_paren_type, $type); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1470 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1471 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1472 | print "PRE_START($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1473 | $av_preprocessor = 1; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1474 | |
| 1475 | push(@av_paren_type, $type); |
| 1476 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1477 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1478 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1479 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1480 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 1481 | $av_preprocessor = 1; |
| 1482 | |
| 1483 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 1484 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1485 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1486 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1487 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1488 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 1489 | |
| 1490 | $av_preprocessor = 1; |
| 1491 | |
| 1492 | # Assume all arms of the conditional end as this |
| 1493 | # one does, and continue as if the #endif was not here. |
| 1494 | pop(@av_paren_type); |
| 1495 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1496 | $type = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1497 | |
| 1498 | } elsif ($cur =~ /^(\\\n)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1499 | print "PRECONT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1500 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1501 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 1502 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 1503 | $av_pending = $type; |
| 1504 | $type = 'N'; |
| 1505 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1506 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1507 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1508 | if (defined $2) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1509 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1510 | } |
| 1511 | $type = 'N'; |
| 1512 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1513 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1514 | print "COND($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1515 | $av_pending = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1516 | $type = 'N'; |
| 1517 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1518 | } elsif ($cur =~/^(case)/o) { |
| 1519 | print "CASE($1)\n" if ($dbg_values > 1); |
| 1520 | $av_pend_colon = 'C'; |
| 1521 | $type = 'N'; |
| 1522 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1523 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1524 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1525 | $type = 'N'; |
| 1526 | |
| 1527 | } elsif ($cur =~ /^(\()/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1528 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1529 | push(@av_paren_type, $av_pending); |
| 1530 | $av_pending = '_'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1531 | $type = 'N'; |
| 1532 | |
| 1533 | } elsif ($cur =~ /^(\))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1534 | my $new_type = pop(@av_paren_type); |
| 1535 | if ($new_type ne '_') { |
| 1536 | $type = $new_type; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1537 | print "PAREN('$1') -> $type\n" |
| 1538 | if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1539 | } else { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1540 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1543 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1544 | print "FUNC($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1545 | $type = 'V'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1546 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1547 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1548 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 1549 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1550 | $av_pend_colon = 'B'; |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1551 | } elsif ($type eq 'E') { |
| 1552 | $av_pend_colon = 'L'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1553 | } |
| 1554 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 1555 | $type = 'V'; |
| 1556 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1557 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1558 | print "IDENT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1559 | $type = 'V'; |
| 1560 | |
| 1561 | } elsif ($cur =~ /^($Assignment)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1562 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1563 | $type = 'N'; |
| 1564 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1565 | } elsif ($cur =~/^(;|{|})/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1566 | print "END($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1567 | $type = 'E'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1568 | $av_pend_colon = 'O'; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1569 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1570 | } elsif ($cur =~/^(,)/) { |
| 1571 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 1572 | $type = 'C'; |
| 1573 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1574 | } elsif ($cur =~ /^(\?)/o) { |
| 1575 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 1576 | $type = 'N'; |
| 1577 | |
| 1578 | } elsif ($cur =~ /^(:)/o) { |
| 1579 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 1580 | |
| 1581 | substr($var, length($res), 1, $av_pend_colon); |
| 1582 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 1583 | $type = 'E'; |
| 1584 | } else { |
| 1585 | $type = 'N'; |
| 1586 | } |
| 1587 | $av_pend_colon = 'O'; |
| 1588 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1589 | } elsif ($cur =~ /^(\[)/o) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1590 | print "CLOSE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1591 | $type = 'N'; |
| 1592 | |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1593 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1594 | my $variant; |
| 1595 | |
| 1596 | print "OPV($1)\n" if ($dbg_values > 1); |
| 1597 | if ($type eq 'V') { |
| 1598 | $variant = 'B'; |
| 1599 | } else { |
| 1600 | $variant = 'U'; |
| 1601 | } |
| 1602 | |
| 1603 | substr($var, length($res), 1, $variant); |
| 1604 | $type = 'N'; |
| 1605 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1606 | } elsif ($cur =~ /^($Operators)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1607 | print "OP($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1608 | if ($1 ne '++' && $1 ne '--') { |
| 1609 | $type = 'N'; |
| 1610 | } |
| 1611 | |
| 1612 | } elsif ($cur =~ /(^.)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1613 | print "C($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1614 | } |
| 1615 | if (defined $1) { |
| 1616 | $cur = substr($cur, length($1)); |
| 1617 | $res .= $type x length($1); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1621 | return ($res, $var); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1624 | sub possible { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1625 | my ($possible, $line) = @_; |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1626 | my $notPermitted = qr{(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1627 | ^(?: |
| 1628 | $Modifier| |
| 1629 | $Storage| |
| 1630 | $Type| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1631 | DEFINE_\S+ |
| 1632 | )$| |
| 1633 | ^(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1634 | goto| |
| 1635 | return| |
| 1636 | case| |
| 1637 | else| |
| 1638 | asm|__asm__| |
Andy Whitcroft | 89a8835 | 2012-01-10 15:10:00 -0800 | [diff] [blame] | 1639 | do| |
| 1640 | \#| |
| 1641 | \#\#| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1642 | )(?:\s|$)| |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1643 | ^(?:typedef|struct|enum)\b |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1644 | )}x; |
| 1645 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 1646 | if ($possible !~ $notPermitted) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1647 | # Check for modifiers. |
| 1648 | $possible =~ s/\s*$Storage\s*//g; |
| 1649 | $possible =~ s/\s*$Sparse\s*//g; |
| 1650 | if ($possible =~ /^\s*$/) { |
| 1651 | |
| 1652 | } elsif ($possible =~ /\s/) { |
| 1653 | $possible =~ s/\s*$Type\s*//g; |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1654 | for my $modifier (split(' ', $possible)) { |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1655 | if ($modifier !~ $notPermitted) { |
| 1656 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 1657 | push(@modifierListFile, $modifier); |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1658 | } |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1659 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1660 | |
| 1661 | } else { |
| 1662 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
Alex Dowad | 485ff23 | 2015-06-25 15:02:52 -0700 | [diff] [blame] | 1663 | push(@typeListFile, $possible); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1664 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1665 | build_types(); |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1666 | } else { |
| 1667 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1668 | } |
| 1669 | } |
| 1670 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1671 | my $prefix = ''; |
| 1672 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1673 | sub show_type { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1674 | my ($type) = @_; |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 1675 | |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1676 | return defined $use_type{$type} if (scalar keys %use_type > 0); |
| 1677 | |
| 1678 | return !defined $ignore_type{$type}; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1679 | } |
| 1680 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1681 | sub report { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1682 | my ($level, $type, $msg) = @_; |
| 1683 | |
| 1684 | if (!show_type($type) || |
| 1685 | (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1686 | return 0; |
| 1687 | } |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 1688 | my $output = ''; |
| 1689 | if (-t STDOUT && $color) { |
| 1690 | if ($level eq 'ERROR') { |
| 1691 | $output .= RED; |
| 1692 | } elsif ($level eq 'WARNING') { |
| 1693 | $output .= YELLOW; |
| 1694 | } else { |
| 1695 | $output .= GREEN; |
| 1696 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1697 | } |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 1698 | $output .= $prefix . $level . ':'; |
| 1699 | if ($show_types) { |
| 1700 | $output .= BLUE if (-t STDOUT && $color); |
| 1701 | $output .= "$type:"; |
| 1702 | } |
| 1703 | $output .= RESET if (-t STDOUT && $color); |
| 1704 | $output .= ' ' . $msg . "\n"; |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 1705 | |
| 1706 | if ($showfile) { |
| 1707 | my @lines = split("\n", $output, -1); |
| 1708 | splice(@lines, 1, 1); |
| 1709 | $output = join("\n", @lines); |
| 1710 | } |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 1711 | $output = (split('\n', $output))[0] . "\n" if ($terse); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1712 | |
Joe Perches | 5723029 | 2015-06-25 15:03:03 -0700 | [diff] [blame] | 1713 | push(our @report, $output); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1714 | |
| 1715 | return 1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1716 | } |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1717 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1718 | sub report_dump { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1719 | our @report; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1720 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1721 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 1722 | sub fixup_current_range { |
| 1723 | my ($lineRef, $offset, $length) = @_; |
| 1724 | |
| 1725 | if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { |
| 1726 | my $o = $1; |
| 1727 | my $l = $2; |
| 1728 | my $no = $o + $offset; |
| 1729 | my $nl = $l + $length; |
| 1730 | $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | sub fix_inserted_deleted_lines { |
| 1735 | my ($linesRef, $insertedRef, $deletedRef) = @_; |
| 1736 | |
| 1737 | my $range_last_linenr = 0; |
| 1738 | my $delta_offset = 0; |
| 1739 | |
| 1740 | my $old_linenr = 0; |
| 1741 | my $new_linenr = 0; |
| 1742 | |
| 1743 | my $next_insert = 0; |
| 1744 | my $next_delete = 0; |
| 1745 | |
| 1746 | my @lines = (); |
| 1747 | |
| 1748 | my $inserted = @{$insertedRef}[$next_insert++]; |
| 1749 | my $deleted = @{$deletedRef}[$next_delete++]; |
| 1750 | |
| 1751 | foreach my $old_line (@{$linesRef}) { |
| 1752 | my $save_line = 1; |
| 1753 | my $line = $old_line; #don't modify the array |
Joe Perches | 323b267 | 2015-04-16 12:44:50 -0700 | [diff] [blame] | 1754 | if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 1755 | $delta_offset = 0; |
| 1756 | } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk |
| 1757 | $range_last_linenr = $new_linenr; |
| 1758 | fixup_current_range(\$line, $delta_offset, 0); |
| 1759 | } |
| 1760 | |
| 1761 | while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { |
| 1762 | $deleted = @{$deletedRef}[$next_delete++]; |
| 1763 | $save_line = 0; |
| 1764 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); |
| 1765 | } |
| 1766 | |
| 1767 | while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { |
| 1768 | push(@lines, ${$inserted}{'LINE'}); |
| 1769 | $inserted = @{$insertedRef}[$next_insert++]; |
| 1770 | $new_linenr++; |
| 1771 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); |
| 1772 | } |
| 1773 | |
| 1774 | if ($save_line) { |
| 1775 | push(@lines, $line); |
| 1776 | $new_linenr++; |
| 1777 | } |
| 1778 | |
| 1779 | $old_linenr++; |
| 1780 | } |
| 1781 | |
| 1782 | return @lines; |
| 1783 | } |
| 1784 | |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 1785 | sub fix_insert_line { |
| 1786 | my ($linenr, $line) = @_; |
| 1787 | |
| 1788 | my $inserted = { |
| 1789 | LINENR => $linenr, |
| 1790 | LINE => $line, |
| 1791 | }; |
| 1792 | push(@fixed_inserted, $inserted); |
| 1793 | } |
| 1794 | |
| 1795 | sub fix_delete_line { |
| 1796 | my ($linenr, $line) = @_; |
| 1797 | |
| 1798 | my $deleted = { |
| 1799 | LINENR => $linenr, |
| 1800 | LINE => $line, |
| 1801 | }; |
| 1802 | |
| 1803 | push(@fixed_deleted, $deleted); |
| 1804 | } |
| 1805 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1806 | sub ERROR { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1807 | my ($type, $msg) = @_; |
| 1808 | |
| 1809 | if (report("ERROR", $type, $msg)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1810 | our $clean = 0; |
| 1811 | our $cnt_error++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1812 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1813 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1814 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1815 | } |
| 1816 | sub WARN { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1817 | my ($type, $msg) = @_; |
| 1818 | |
| 1819 | if (report("WARNING", $type, $msg)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1820 | our $clean = 0; |
| 1821 | our $cnt_warn++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1822 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1823 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1824 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1825 | } |
| 1826 | sub CHK { |
Joe Perches | cbec18a | 2014-04-03 14:49:19 -0700 | [diff] [blame] | 1827 | my ($type, $msg) = @_; |
| 1828 | |
| 1829 | if ($check && report("CHECK", $type, $msg)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1830 | our $clean = 0; |
| 1831 | our $cnt_chk++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1832 | return 1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1833 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1834 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1837 | sub check_absolute_file { |
| 1838 | my ($absolute, $herecurr) = @_; |
| 1839 | my $file = $absolute; |
| 1840 | |
| 1841 | ##print "absolute<$absolute>\n"; |
| 1842 | |
| 1843 | # See if any suffix of this path is a path within the tree. |
| 1844 | while ($file =~ s@^[^/]*/@@) { |
| 1845 | if (-f "$root/$file") { |
| 1846 | ##print "file<$file>\n"; |
| 1847 | last; |
| 1848 | } |
| 1849 | } |
| 1850 | if (! -f _) { |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
| 1854 | # It is, so see if the prefix is acceptable. |
| 1855 | my $prefix = $absolute; |
| 1856 | substr($prefix, -length($file)) = ''; |
| 1857 | |
| 1858 | ##print "prefix<$prefix>\n"; |
| 1859 | if ($prefix ne ".../") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1860 | WARN("USE_RELATIVE_PATH", |
| 1861 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1865 | sub trim { |
| 1866 | my ($string) = @_; |
| 1867 | |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 1868 | $string =~ s/^\s+|\s+$//g; |
| 1869 | |
| 1870 | return $string; |
| 1871 | } |
| 1872 | |
| 1873 | sub ltrim { |
| 1874 | my ($string) = @_; |
| 1875 | |
| 1876 | $string =~ s/^\s+//; |
| 1877 | |
| 1878 | return $string; |
| 1879 | } |
| 1880 | |
| 1881 | sub rtrim { |
| 1882 | my ($string) = @_; |
| 1883 | |
| 1884 | $string =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1885 | |
| 1886 | return $string; |
| 1887 | } |
| 1888 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 1889 | sub string_find_replace { |
| 1890 | my ($string, $find, $replace) = @_; |
| 1891 | |
| 1892 | $string =~ s/$find/$replace/g; |
| 1893 | |
| 1894 | return $string; |
| 1895 | } |
| 1896 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1897 | sub tabify { |
| 1898 | my ($leading) = @_; |
| 1899 | |
| 1900 | my $source_indent = 8; |
| 1901 | my $max_spaces_before_tab = $source_indent - 1; |
| 1902 | my $spaces_to_tab = " " x $source_indent; |
| 1903 | |
| 1904 | #convert leading spaces to tabs |
| 1905 | 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; |
| 1906 | #Remove spaces before a tab |
| 1907 | 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; |
| 1908 | |
| 1909 | return "$leading"; |
| 1910 | } |
| 1911 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1912 | sub pos_last_openparen { |
| 1913 | my ($line) = @_; |
| 1914 | |
| 1915 | my $pos = 0; |
| 1916 | |
| 1917 | my $opens = $line =~ tr/\(/\(/; |
| 1918 | my $closes = $line =~ tr/\)/\)/; |
| 1919 | |
| 1920 | my $last_openparen = 0; |
| 1921 | |
| 1922 | if (($opens == 0) || ($closes >= $opens)) { |
| 1923 | return -1; |
| 1924 | } |
| 1925 | |
| 1926 | my $len = length($line); |
| 1927 | |
| 1928 | for ($pos = 0; $pos < $len; $pos++) { |
| 1929 | my $string = substr($line, $pos); |
| 1930 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 1931 | $pos += length($1) - 1; |
| 1932 | } elsif (substr($line, $pos, 1) eq '(') { |
| 1933 | $last_openparen = $pos; |
| 1934 | } elsif (index($string, '(') == -1) { |
| 1935 | last; |
| 1936 | } |
| 1937 | } |
| 1938 | |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 1939 | return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1942 | sub process { |
| 1943 | my $filename = shift; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1944 | |
| 1945 | my $linenr=0; |
| 1946 | my $prevline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1947 | my $prevrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1948 | my $stashline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1949 | my $stashrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1950 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1951 | my $length; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1952 | my $indent; |
| 1953 | my $previndent=0; |
| 1954 | my $stashindent=0; |
| 1955 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1956 | our $clean = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1957 | my $signoff = 0; |
| 1958 | my $is_patch = 0; |
Joe Perches | 29ee1b0 | 2014-08-06 16:10:35 -0700 | [diff] [blame] | 1959 | my $in_header_lines = $file ? 0 : 1; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1960 | my $in_commit_log = 0; #Scanning lines before patch |
Joe Perches | bf4daf1 | 2015-09-09 15:37:50 -0700 | [diff] [blame] | 1961 | my $commit_log_possible_stack_dump = 0; |
Joe Perches | 2a076f4 | 2015-04-16 12:44:28 -0700 | [diff] [blame] | 1962 | my $commit_log_long_line = 0; |
Joe Perches | e518e9a | 2015-06-25 15:03:27 -0700 | [diff] [blame] | 1963 | my $commit_log_has_diff = 0; |
Joe Perches | 13f1937 | 2014-08-06 16:10:59 -0700 | [diff] [blame] | 1964 | my $reported_maintainer_file = 0; |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1965 | my $non_utf8_charset = 0; |
| 1966 | |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 1967 | my $last_blank_line = 0; |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 1968 | my $last_coalesced_string_linenr = -1; |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 1969 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1970 | our @report = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1971 | our $cnt_lines = 0; |
| 1972 | our $cnt_error = 0; |
| 1973 | our $cnt_warn = 0; |
| 1974 | our $cnt_chk = 0; |
| 1975 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1976 | # Trace the real file/line as we go. |
| 1977 | my $realfile = ''; |
| 1978 | my $realline = 0; |
| 1979 | my $realcnt = 0; |
| 1980 | my $here = ''; |
| 1981 | my $in_comment = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1982 | my $comment_edge = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1983 | my $first_line = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1984 | my $p1_prefix = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1985 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1986 | my $prev_values = 'E'; |
| 1987 | |
| 1988 | # suppression flags |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1989 | my %suppress_ifbraces; |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1990 | my %suppress_whiletrailers; |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1991 | my %suppress_export; |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1992 | my $suppress_statement = 0; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1993 | |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 1994 | my %signatures = (); |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 1995 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1996 | # Pre-scan the patch sanitizing the lines. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1997 | # Pre-scan the patch looking for any __setup documentation. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1998 | # |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1999 | my @setup_docs = (); |
| 2000 | my $setup_docs = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2001 | |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 2002 | my $camelcase_file_seeded = 0; |
| 2003 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2004 | sanitise_line_reset(); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2005 | my $line; |
| 2006 | foreach my $rawline (@rawlines) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2007 | $linenr++; |
| 2008 | $line = $rawline; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2009 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2010 | push(@fixed, $rawline) if ($fix); |
| 2011 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2012 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2013 | $setup_docs = 0; |
| 2014 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 2015 | $setup_docs = 1; |
| 2016 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2017 | #next; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2018 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2019 | if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
| 2020 | $realline=$1-1; |
| 2021 | if (defined $2) { |
| 2022 | $realcnt=$3+1; |
| 2023 | } else { |
| 2024 | $realcnt=1+1; |
| 2025 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2026 | $in_comment = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2027 | |
| 2028 | # Guestimate if this is a continuing comment. Run |
| 2029 | # the context looking for a comment "edge". If this |
| 2030 | # edge is a close comment then we must be in a comment |
| 2031 | # at context start. |
| 2032 | my $edge; |
Andy Whitcroft | 01fa914 | 2008-10-15 22:02:19 -0700 | [diff] [blame] | 2033 | my $cnt = $realcnt; |
| 2034 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 2035 | next if (defined $rawlines[$ln - 1] && |
| 2036 | $rawlines[$ln - 1] =~ /^-/); |
| 2037 | $cnt--; |
| 2038 | #print "RAW<$rawlines[$ln - 1]>\n"; |
Andy Whitcroft | 721c1cb | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 2039 | last if (!defined $rawlines[$ln - 1]); |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 2040 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 2041 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 2042 | ($edge) = $1; |
| 2043 | last; |
| 2044 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2045 | } |
| 2046 | if (defined $edge && $edge eq '*/') { |
| 2047 | $in_comment = 1; |
| 2048 | } |
| 2049 | |
| 2050 | # Guestimate if this is a continuing comment. If this |
| 2051 | # is the start of a diff block and this line starts |
| 2052 | # ' *' then it is very likely a comment. |
| 2053 | if (!defined $edge && |
Andy Whitcroft | 83242e0 | 2009-01-06 14:41:17 -0800 | [diff] [blame] | 2054 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2055 | { |
| 2056 | $in_comment = 1; |
| 2057 | } |
| 2058 | |
| 2059 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 2060 | sanitise_line_reset($in_comment); |
| 2061 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2062 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2063 | # Standardise the strings and chars within the input to |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2064 | # simplify matching -- only bother with positive lines. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2065 | $line = sanitise_line($rawline); |
| 2066 | } |
| 2067 | push(@lines, $line); |
| 2068 | |
| 2069 | if ($realcnt > 1) { |
| 2070 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 2071 | } else { |
| 2072 | $realcnt = 0; |
| 2073 | } |
| 2074 | |
| 2075 | #print "==>$rawline\n"; |
| 2076 | #print "-->$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2077 | |
| 2078 | if ($setup_docs && $line =~ /^\+/) { |
| 2079 | push(@setup_docs, $line); |
| 2080 | } |
| 2081 | } |
| 2082 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2083 | $prefix = ''; |
| 2084 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2085 | $realcnt = 0; |
| 2086 | $linenr = 0; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2087 | $fixlinenr = -1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2088 | foreach my $line (@lines) { |
| 2089 | $linenr++; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2090 | $fixlinenr++; |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 2091 | my $sline = $line; #copy of $line |
| 2092 | $sline =~ s/$;/ /g; #with comments as spaces |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2093 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2094 | my $rawline = $rawlines[$linenr - 1]; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2095 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2096 | #extract the line range in the file after the patch is applied |
Joe Perches | e518e9a | 2015-06-25 15:03:27 -0700 | [diff] [blame] | 2097 | if (!$in_commit_log && |
| 2098 | $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2099 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2100 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2101 | $realline=$1-1; |
| 2102 | if (defined $2) { |
| 2103 | $realcnt=$3+1; |
| 2104 | } else { |
| 2105 | $realcnt=1+1; |
| 2106 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2107 | annotate_reset(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2108 | $prev_values = 'E'; |
| 2109 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2110 | %suppress_ifbraces = (); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2111 | %suppress_whiletrailers = (); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2112 | %suppress_export = (); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2113 | $suppress_statement = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2114 | next; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2115 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2116 | # track the line number as we move through the hunk, note that |
| 2117 | # new versions of GNU diff omit the leading space on completely |
| 2118 | # blank context lines so we need to count that too. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2119 | } elsif ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2120 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2121 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2122 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2123 | # Measure the line length and indent. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2124 | ($length, $indent) = line_stats($rawline); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2125 | |
| 2126 | # Track the previous line. |
| 2127 | ($prevline, $stashline) = ($stashline, $line); |
| 2128 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2129 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 2130 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2131 | #warn "line<$line>\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2132 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2133 | } elsif ($realcnt == 1) { |
| 2134 | $realcnt--; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
Andy Whitcroft | cc77cdc | 2009-10-26 16:50:13 -0700 | [diff] [blame] | 2137 | my $hunk_line = ($realcnt != 0); |
| 2138 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2139 | $here = "#$linenr: " if (!$file); |
| 2140 | $here = "#$realline: " if ($file); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2141 | |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2142 | my $found_file = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2143 | # extract the filename as it passes |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2144 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 2145 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 2146 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 2147 | $in_commit_log = 0; |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2148 | $found_file = 1; |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2149 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2150 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 2151 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 2152 | $in_commit_log = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2153 | |
| 2154 | $p1_prefix = $1; |
Andy Whitcroft | e2f7aa4 | 2009-02-27 14:03:06 -0800 | [diff] [blame] | 2155 | if (!$file && $tree && $p1_prefix ne '' && |
| 2156 | -e "$root/$p1_prefix") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2157 | WARN("PATCH_PREFIX", |
| 2158 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2159 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2160 | |
Andy Whitcroft | c1ab332 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2161 | if ($realfile =~ m@^include/asm/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2162 | ERROR("MODIFIED_INCLUDE_ASM", |
| 2163 | "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] | 2164 | } |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2165 | $found_file = 1; |
| 2166 | } |
| 2167 | |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 2168 | #make up the handle for any error we report on this line |
| 2169 | if ($showfile) { |
| 2170 | $prefix = "$realfile:$realline: " |
| 2171 | } elsif ($emacs) { |
Joe Perches | 7d3a9f6 | 2015-09-09 15:37:39 -0700 | [diff] [blame] | 2172 | if ($file) { |
| 2173 | $prefix = "$filename:$realline: "; |
| 2174 | } else { |
| 2175 | $prefix = "$filename:$linenr: "; |
| 2176 | } |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2179 | if ($found_file) { |
Joe Perches | 7bd7e48 | 2015-09-09 15:37:44 -0700 | [diff] [blame] | 2180 | if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { |
Joe Perches | 2ac73b4 | 2014-06-04 16:12:05 -0700 | [diff] [blame] | 2181 | $check = 1; |
| 2182 | } else { |
| 2183 | $check = $check_orig; |
| 2184 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2185 | next; |
| 2186 | } |
| 2187 | |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 2188 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2189 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2190 | my $hereline = "$here\n$rawline\n"; |
| 2191 | my $herecurr = "$here\n$rawline\n"; |
| 2192 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2193 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2194 | $cnt_lines++ if ($realcnt != 0); |
| 2195 | |
Joe Perches | e518e9a | 2015-06-25 15:03:27 -0700 | [diff] [blame] | 2196 | # Check if the commit log has what seems like a diff which can confuse patch |
| 2197 | if ($in_commit_log && !$commit_log_has_diff && |
| 2198 | (($line =~ m@^\s+diff\b.*a/[\w/]+@ && |
| 2199 | $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || |
| 2200 | $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || |
| 2201 | $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { |
| 2202 | ERROR("DIFF_IN_COMMIT_MSG", |
| 2203 | "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); |
| 2204 | $commit_log_has_diff = 1; |
| 2205 | } |
| 2206 | |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2207 | # Check for incorrect file permissions |
| 2208 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 2209 | my $permhere = $here . "FILE: $realfile\n"; |
Joe Perches | 04db4d2 | 2013-04-29 16:18:14 -0700 | [diff] [blame] | 2210 | if ($realfile !~ m@scripts/@ && |
| 2211 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2212 | ERROR("EXECUTE_PERMISSIONS", |
| 2213 | "do not set execute permissions for source files\n" . $permhere); |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2217 | # Check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2218 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2219 | $signoff++; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2220 | $in_commit_log = 0; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2221 | } |
| 2222 | |
Joe Perches | e0d975b | 2014-12-10 15:51:49 -0800 | [diff] [blame] | 2223 | # Check if MAINTAINERS is being updated. If so, there's probably no need to |
| 2224 | # emit the "does MAINTAINERS need updating?" message on file add/move/delete |
| 2225 | if ($line =~ /^\s*MAINTAINERS\s*\|/) { |
| 2226 | $reported_maintainer_file = 1; |
| 2227 | } |
| 2228 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2229 | # Check signature styles |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 2230 | if (!$in_header_lines && |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 2231 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2232 | my $space_before = $1; |
| 2233 | my $sign_off = $2; |
| 2234 | my $space_after = $3; |
| 2235 | my $email = $4; |
| 2236 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 2237 | |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 2238 | if ($sign_off !~ /$signature_tags/) { |
| 2239 | WARN("BAD_SIGN_OFF", |
| 2240 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 2241 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2242 | if (defined $space_before && $space_before ne "") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2243 | if (WARN("BAD_SIGN_OFF", |
| 2244 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && |
| 2245 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2246 | $fixed[$fixlinenr] = |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2247 | "$ucfirst_sign_off $email"; |
| 2248 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2249 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2250 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2251 | if (WARN("BAD_SIGN_OFF", |
| 2252 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && |
| 2253 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2254 | $fixed[$fixlinenr] = |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2255 | "$ucfirst_sign_off $email"; |
| 2256 | } |
| 2257 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2258 | } |
| 2259 | if (!defined $space_after || $space_after ne " ") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2260 | if (WARN("BAD_SIGN_OFF", |
| 2261 | "Use a single space after $ucfirst_sign_off\n" . $herecurr) && |
| 2262 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2263 | $fixed[$fixlinenr] = |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2264 | "$ucfirst_sign_off $email"; |
| 2265 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | my ($email_name, $email_address, $comment) = parse_email($email); |
| 2269 | my $suggested_email = format_email(($email_name, $email_address)); |
| 2270 | if ($suggested_email eq "") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2271 | ERROR("BAD_SIGN_OFF", |
| 2272 | "Unrecognized email address: '$email'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2273 | } else { |
| 2274 | my $dequoted = $suggested_email; |
| 2275 | $dequoted =~ s/^"//; |
| 2276 | $dequoted =~ s/" </ </; |
| 2277 | # Don't force email to have quotes |
| 2278 | # Allow just an angle bracketed address |
| 2279 | if ("$dequoted$comment" ne $email && |
| 2280 | "<$email_address>$comment" ne $email && |
| 2281 | "$suggested_email$comment" ne $email) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2282 | WARN("BAD_SIGN_OFF", |
| 2283 | "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 2284 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2285 | } |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 2286 | |
| 2287 | # Check for duplicate signatures |
| 2288 | my $sig_nospace = $line; |
| 2289 | $sig_nospace =~ s/\s//g; |
| 2290 | $sig_nospace = lc($sig_nospace); |
| 2291 | if (defined $signatures{$sig_nospace}) { |
| 2292 | WARN("BAD_SIGN_OFF", |
| 2293 | "Duplicate signature\n" . $herecurr); |
| 2294 | } else { |
| 2295 | $signatures{$sig_nospace} = 1; |
| 2296 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
Joe Perches | a2fe16b | 2015-02-13 14:39:02 -0800 | [diff] [blame] | 2299 | # Check email subject for common tools that don't need to be mentioned |
| 2300 | if ($in_header_lines && |
| 2301 | $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { |
| 2302 | WARN("EMAIL_SUBJECT", |
| 2303 | "A patch subject line should describe the change not the tool that found it\n" . $herecurr); |
| 2304 | } |
| 2305 | |
Joe Perches | 9b3189e | 2014-06-04 16:12:10 -0700 | [diff] [blame] | 2306 | # Check for old stable address |
| 2307 | if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) { |
| 2308 | ERROR("STABLE_ADDRESS", |
| 2309 | "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr); |
| 2310 | } |
| 2311 | |
Christopher Covington | 7ebd05e | 2014-04-03 14:49:31 -0700 | [diff] [blame] | 2312 | # Check for unwanted Gerrit info |
| 2313 | if ($in_commit_log && $line =~ /^\s*change-id:/i) { |
| 2314 | ERROR("GERRIT_CHANGE_ID", |
| 2315 | "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); |
| 2316 | } |
| 2317 | |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 2318 | # Check if the commit log is in a possible stack dump |
| 2319 | if ($in_commit_log && !$commit_log_possible_stack_dump && |
| 2320 | ($line =~ /^\s*(?:WARNING:|BUG:)/ || |
| 2321 | $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || |
| 2322 | # timestamp |
| 2323 | $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) { |
| 2324 | # stack dump address |
| 2325 | $commit_log_possible_stack_dump = 1; |
| 2326 | } |
| 2327 | |
Joe Perches | 2a076f4 | 2015-04-16 12:44:28 -0700 | [diff] [blame] | 2328 | # Check for line lengths > 75 in commit log, warn once |
| 2329 | if ($in_commit_log && !$commit_log_long_line && |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 2330 | length($line) > 75 && |
| 2331 | !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || |
| 2332 | # file delta changes |
| 2333 | $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || |
| 2334 | # filename then : |
| 2335 | $line =~ /^\s*(?:Fixes:|Link:)/i || |
| 2336 | # A Fixes: or Link: line |
| 2337 | $commit_log_possible_stack_dump)) { |
Joe Perches | 2a076f4 | 2015-04-16 12:44:28 -0700 | [diff] [blame] | 2338 | WARN("COMMIT_LOG_LONG_LINE", |
| 2339 | "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); |
| 2340 | $commit_log_long_line = 1; |
| 2341 | } |
| 2342 | |
Joe Perches | bf4daf1 | 2015-09-09 15:37:50 -0700 | [diff] [blame] | 2343 | # Reset possible stack dump if a blank line is found |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 2344 | if ($in_commit_log && $commit_log_possible_stack_dump && |
| 2345 | $line =~ /^\s*$/) { |
| 2346 | $commit_log_possible_stack_dump = 0; |
| 2347 | } |
Joe Perches | bf4daf1 | 2015-09-09 15:37:50 -0700 | [diff] [blame] | 2348 | |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2349 | # Check for git id commit length and improperly formed commit descriptions |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 2350 | if ($in_commit_log && !$commit_log_possible_stack_dump && |
Joe Perches | fe043ea | 2015-09-09 15:37:25 -0700 | [diff] [blame] | 2351 | ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || |
Joe Perches | 369c8dd | 2015-11-06 16:31:34 -0800 | [diff] [blame] | 2352 | ($line =~ /\b[0-9a-f]{12,40}\b/i && |
| 2353 | $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && |
| 2354 | $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { |
Joe Perches | fe043ea | 2015-09-09 15:37:25 -0700 | [diff] [blame] | 2355 | my $init_char = "c"; |
| 2356 | my $orig_commit = ""; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2357 | my $short = 1; |
| 2358 | my $long = 0; |
| 2359 | my $case = 1; |
| 2360 | my $space = 1; |
| 2361 | my $hasdesc = 0; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2362 | my $hasparens = 0; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2363 | my $id = '0123456789ab'; |
| 2364 | my $orig_desc = "commit description"; |
| 2365 | my $description = ""; |
| 2366 | |
Joe Perches | fe043ea | 2015-09-09 15:37:25 -0700 | [diff] [blame] | 2367 | if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { |
| 2368 | $init_char = $1; |
| 2369 | $orig_commit = lc($2); |
| 2370 | } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { |
| 2371 | $orig_commit = lc($1); |
| 2372 | } |
| 2373 | |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2374 | $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); |
| 2375 | $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); |
| 2376 | $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); |
| 2377 | $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); |
| 2378 | if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { |
| 2379 | $orig_desc = $1; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2380 | $hasparens = 1; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2381 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && |
| 2382 | defined $rawlines[$linenr] && |
| 2383 | $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { |
| 2384 | $orig_desc = $1; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2385 | $hasparens = 1; |
Joe Perches | b671fde | 2015-02-13 14:38:41 -0800 | [diff] [blame] | 2386 | } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && |
| 2387 | defined $rawlines[$linenr] && |
| 2388 | $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { |
| 2389 | $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; |
| 2390 | $orig_desc = $1; |
| 2391 | $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; |
| 2392 | $orig_desc .= " " . $1; |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2393 | $hasparens = 1; |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | ($id, $description) = git_commit_info($orig_commit, |
| 2397 | $id, $orig_desc); |
| 2398 | |
Joe Perches | 19c146a | 2015-02-13 14:39:00 -0800 | [diff] [blame] | 2399 | if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) { |
Joe Perches | 0d7835f | 2015-02-13 14:38:35 -0800 | [diff] [blame] | 2400 | ERROR("GIT_COMMIT_ID", |
| 2401 | "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); |
| 2402 | } |
Joe Perches | d311cd4 | 2014-08-06 16:10:57 -0700 | [diff] [blame] | 2403 | } |
| 2404 | |
Joe Perches | 13f1937 | 2014-08-06 16:10:59 -0700 | [diff] [blame] | 2405 | # Check for added, moved or deleted files |
| 2406 | if (!$reported_maintainer_file && !$in_commit_log && |
| 2407 | ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || |
| 2408 | $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || |
| 2409 | ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && |
| 2410 | (defined($1) || defined($2))))) { |
| 2411 | $reported_maintainer_file = 1; |
| 2412 | WARN("FILE_PATH_CHANGES", |
| 2413 | "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); |
| 2414 | } |
| 2415 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2416 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2417 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2418 | ERROR("CORRUPTED_PATCH", |
| 2419 | "patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2420 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2421 | } |
| 2422 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2423 | # Check for absolute kernel paths. |
| 2424 | if ($tree) { |
| 2425 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 2426 | my $file = $1; |
| 2427 | |
| 2428 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 2429 | check_absolute_file($1, $herecurr)) { |
| 2430 | # |
| 2431 | } else { |
| 2432 | check_absolute_file($file, $herecurr); |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2437 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 2438 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2439 | $rawline !~ m/^$UTF8*$/) { |
| 2440 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 2441 | |
| 2442 | my $blank = copy_spacing($rawline); |
| 2443 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 2444 | my $hereptr = "$hereline$ptr\n"; |
| 2445 | |
Joe Perches | 34d9921 | 2011-07-25 17:13:26 -0700 | [diff] [blame] | 2446 | CHK("INVALID_UTF8", |
| 2447 | "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] | 2448 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2449 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2450 | # Check if it's the start of a commit log |
| 2451 | # (not a header line and we haven't seen the patch filename) |
| 2452 | if ($in_header_lines && $realfile =~ /^$/ && |
Joe Perches | 29ee1b0 | 2014-08-06 16:10:35 -0700 | [diff] [blame] | 2453 | !($rawline =~ /^\s+\S/ || |
| 2454 | $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) { |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2455 | $in_header_lines = 0; |
| 2456 | $in_commit_log = 1; |
| 2457 | } |
| 2458 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 2459 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 2460 | # declined it, i.e defined some charset where it is missing. |
| 2461 | if ($in_header_lines && |
| 2462 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 2463 | $1 !~ /utf-8/i) { |
| 2464 | $non_utf8_charset = 1; |
| 2465 | } |
| 2466 | |
| 2467 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2468 | $rawline =~ /$NON_ASCII_UTF8/) { |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 2469 | WARN("UTF8_BEFORE_PATCH", |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 2470 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 2471 | } |
| 2472 | |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 2473 | # Check for various typo / spelling mistakes |
Joe Perches | 66d7a38 | 2015-04-16 12:44:08 -0700 | [diff] [blame] | 2474 | if (defined($misspellings) && |
| 2475 | ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { |
Joe Perches | ebfd7d6 | 2015-04-16 12:44:14 -0700 | [diff] [blame] | 2476 | while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { |
Kees Cook | 66b47b4 | 2014-10-13 15:51:57 -0700 | [diff] [blame] | 2477 | my $typo = $1; |
| 2478 | my $typo_fix = $spelling_fix{lc($typo)}; |
| 2479 | $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); |
| 2480 | $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); |
| 2481 | my $msg_type = \&WARN; |
| 2482 | $msg_type = \&CHK if ($file); |
| 2483 | if (&{$msg_type}("TYPO_SPELLING", |
| 2484 | "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && |
| 2485 | $fix) { |
| 2486 | $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; |
| 2487 | } |
| 2488 | } |
| 2489 | } |
| 2490 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 2491 | # ignore non-hunk lines and lines being removed |
| 2492 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2493 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2494 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2495 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2496 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2497 | if (ERROR("DOS_LINE_ENDINGS", |
| 2498 | "DOS line endings\n" . $herevet) && |
| 2499 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2500 | $fixed[$fixlinenr] =~ s/[\s\015]+$//; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2501 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2502 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 2503 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2504 | if (ERROR("TRAILING_WHITESPACE", |
| 2505 | "trailing whitespace\n" . $herevet) && |
| 2506 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2507 | $fixed[$fixlinenr] =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2510 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2511 | } |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2512 | |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 2513 | # Check for FSF mailing addresses. |
Alexander Duyck | 109d8cb | 2014-01-23 15:54:50 -0800 | [diff] [blame] | 2514 | if ($rawline =~ /\bwrite to the Free/i || |
Joe Perches | 3e2232f | 2014-01-23 15:54:48 -0800 | [diff] [blame] | 2515 | $rawline =~ /\b59\s+Temple\s+Pl/i || |
| 2516 | $rawline =~ /\b51\s+Franklin\s+St/i) { |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 2517 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
| 2518 | my $msg_type = \&ERROR; |
| 2519 | $msg_type = \&CHK if ($file); |
| 2520 | &{$msg_type}("FSF_MAILING_ADDRESS", |
Joe Perches | 3e2232f | 2014-01-23 15:54:48 -0800 | [diff] [blame] | 2521 | "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] | 2522 | } |
| 2523 | |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2524 | # check for Kconfig help text having a real description |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2525 | # Only applies when adding the entry originally, after that we do not have |
| 2526 | # sufficient context to determine whether it is indeed long enough. |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2527 | if ($realfile =~ /Kconfig/ && |
Joe Perches | 8d73e0e | 2014-08-06 16:10:46 -0700 | [diff] [blame] | 2528 | $line =~ /^\+\s*config\s+/) { |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2529 | my $length = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2530 | my $cnt = $realcnt; |
| 2531 | my $ln = $linenr + 1; |
| 2532 | my $f; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2533 | my $is_start = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2534 | my $is_end = 0; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2535 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2536 | $f = $lines[$ln - 1]; |
| 2537 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 2538 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2539 | |
| 2540 | next if ($f =~ /^-/); |
Joe Perches | 8d73e0e | 2014-08-06 16:10:46 -0700 | [diff] [blame] | 2541 | last if (!$file && $f =~ /^\@\@/); |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2542 | |
Joe Perches | 8d73e0e | 2014-08-06 16:10:46 -0700 | [diff] [blame] | 2543 | if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) { |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2544 | $is_start = 1; |
Joe Perches | 8d73e0e | 2014-08-06 16:10:46 -0700 | [diff] [blame] | 2545 | } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2546 | $length = -1; |
| 2547 | } |
| 2548 | |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2549 | $f =~ s/^.//; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2550 | $f =~ s/#.*//; |
| 2551 | $f =~ s/^\s+//; |
| 2552 | next if ($f =~ /^$/); |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2553 | if ($f =~ /^\s*config\s/) { |
| 2554 | $is_end = 1; |
| 2555 | last; |
| 2556 | } |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2557 | $length++; |
| 2558 | } |
Vadim Bendebury | 5619327 | 2014-10-13 15:51:48 -0700 | [diff] [blame] | 2559 | if ($is_start && $is_end && $length < $min_conf_desc_length) { |
| 2560 | WARN("CONFIG_DESCRIPTION", |
| 2561 | "please write a paragraph that describes the config symbol fully\n" . $herecurr); |
| 2562 | } |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2563 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2564 | } |
| 2565 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 2566 | # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. |
| 2567 | if ($realfile =~ /Kconfig/ && |
| 2568 | $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { |
| 2569 | WARN("CONFIG_EXPERIMENTAL", |
| 2570 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 2571 | } |
| 2572 | |
Christoph Jaeger | 327953e | 2015-02-13 14:38:29 -0800 | [diff] [blame] | 2573 | # discourage the use of boolean for type definition attributes of Kconfig options |
| 2574 | if ($realfile =~ /Kconfig/ && |
| 2575 | $line =~ /^\+\s*\bboolean\b/) { |
| 2576 | WARN("CONFIG_TYPE_BOOLEAN", |
| 2577 | "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); |
| 2578 | } |
| 2579 | |
Arnaud Lacombe | c68e587 | 2011-08-15 01:07:14 -0400 | [diff] [blame] | 2580 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 2581 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 2582 | my $flag = $1; |
| 2583 | my $replacement = { |
| 2584 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 2585 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 2586 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 2587 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 2588 | }; |
| 2589 | |
| 2590 | WARN("DEPRECATED_VARIABLE", |
| 2591 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
| 2592 | } |
| 2593 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2594 | # check for DT compatible documentation |
Florian Vaussard | 7dd05b3 | 2014-04-03 14:49:26 -0700 | [diff] [blame] | 2595 | if (defined $root && |
| 2596 | (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || |
| 2597 | ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { |
| 2598 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2599 | my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; |
| 2600 | |
Florian Vaussard | cc93319 | 2014-04-03 14:49:27 -0700 | [diff] [blame] | 2601 | my $dt_path = $root . "/Documentation/devicetree/bindings/"; |
| 2602 | my $vp_file = $dt_path . "vendor-prefixes.txt"; |
| 2603 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2604 | foreach my $compat (@compats) { |
| 2605 | my $compat2 = $compat; |
Rob Herring | 185d566 | 2014-06-04 16:12:03 -0700 | [diff] [blame] | 2606 | $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; |
| 2607 | my $compat3 = $compat; |
| 2608 | $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; |
| 2609 | `grep -Erq "$compat|$compat2|$compat3" $dt_path`; |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2610 | if ( $? >> 8 ) { |
| 2611 | WARN("UNDOCUMENTED_DT_STRING", |
| 2612 | "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); |
| 2613 | } |
| 2614 | |
Florian Vaussard | 4fbf32a | 2014-04-03 14:49:25 -0700 | [diff] [blame] | 2615 | next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; |
| 2616 | my $vendor = $1; |
Florian Vaussard | cc93319 | 2014-04-03 14:49:27 -0700 | [diff] [blame] | 2617 | `grep -Eq "^$vendor\\b" $vp_file`; |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2618 | if ( $? >> 8 ) { |
| 2619 | WARN("UNDOCUMENTED_DT_STRING", |
Florian Vaussard | cc93319 | 2014-04-03 14:49:27 -0700 | [diff] [blame] | 2620 | "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] | 2621 | } |
| 2622 | } |
| 2623 | } |
| 2624 | |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2625 | # 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] | 2626 | next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/); |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2627 | |
Joe Perches | 47e0c88 | 2015-06-25 15:02:57 -0700 | [diff] [blame] | 2628 | # line length limit (with some exclusions) |
| 2629 | # |
| 2630 | # There are a few types of lines that may extend beyond $max_line_length: |
| 2631 | # logging functions like pr_info that end in a string |
| 2632 | # lines with a single string |
| 2633 | # #defines that are a single string |
| 2634 | # |
| 2635 | # There are 3 different line length message types: |
| 2636 | # LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength |
| 2637 | # LONG_LINE_STRING a string starts before but extends beyond $max_line_length |
| 2638 | # LONG_LINE all other lines longer than $max_line_length |
| 2639 | # |
| 2640 | # if LONG_LINE is ignored, the other 2 types are also ignored |
| 2641 | # |
| 2642 | |
Joe Perches | b4749e9 | 2015-07-17 16:24:01 -0700 | [diff] [blame] | 2643 | if ($line =~ /^\+/ && $length > $max_line_length) { |
Joe Perches | 47e0c88 | 2015-06-25 15:02:57 -0700 | [diff] [blame] | 2644 | my $msg_type = "LONG_LINE"; |
| 2645 | |
| 2646 | # Check the allowed long line types first |
| 2647 | |
| 2648 | # logging functions that end in a string that starts |
| 2649 | # before $max_line_length |
| 2650 | if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && |
| 2651 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 2652 | $msg_type = ""; |
| 2653 | |
| 2654 | # lines with only strings (w/ possible termination) |
| 2655 | # #defines with only strings |
| 2656 | } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || |
| 2657 | $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { |
| 2658 | $msg_type = ""; |
| 2659 | |
| 2660 | # Otherwise set the alternate message types |
| 2661 | |
| 2662 | # a comment starts before $max_line_length |
| 2663 | } elsif ($line =~ /($;[\s$;]*)$/ && |
| 2664 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 2665 | $msg_type = "LONG_LINE_COMMENT" |
| 2666 | |
| 2667 | # a quoted string starts before $max_line_length |
| 2668 | } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && |
| 2669 | length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { |
| 2670 | $msg_type = "LONG_LINE_STRING" |
| 2671 | } |
| 2672 | |
| 2673 | if ($msg_type ne "" && |
| 2674 | (show_type("LONG_LINE") || show_type($msg_type))) { |
| 2675 | WARN($msg_type, |
| 2676 | "line over $max_line_length characters\n" . $herecurr); |
| 2677 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2678 | } |
| 2679 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2680 | # check for adding lines without a newline. |
| 2681 | 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] | 2682 | WARN("MISSING_EOF_NEWLINE", |
| 2683 | "adding a line without newline at end of file\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2684 | } |
| 2685 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2686 | # Blackfin: use hi/lo macros |
| 2687 | if ($realfile =~ m@arch/blackfin/.*\.S$@) { |
| 2688 | if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { |
| 2689 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2690 | ERROR("LO_MACRO", |
| 2691 | "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2692 | } |
| 2693 | if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { |
| 2694 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2695 | ERROR("HI_MACRO", |
| 2696 | "use the HI() macro, not (... >> 16)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2697 | } |
| 2698 | } |
| 2699 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2700 | # 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] | 2701 | next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2702 | |
| 2703 | # at the beginning of a line any tabs must come first and anything |
| 2704 | # more than 8 must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2705 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 2706 | $rawline =~ /^\+\s* \s*/) { |
| 2707 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2708 | $rpt_cleaners = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2709 | if (ERROR("CODE_INDENT", |
| 2710 | "code indent should use tabs where possible\n" . $herevet) && |
| 2711 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2712 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2713 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2716 | # check for space before tabs. |
| 2717 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 2718 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2719 | if (WARN("SPACE_BEFORE_TAB", |
| 2720 | "please, no space before tabs\n" . $herevet) && |
| 2721 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2722 | while ($fixed[$fixlinenr] =~ |
Joe Perches | d2207cc | 2014-10-13 15:51:53 -0700 | [diff] [blame] | 2723 | s/(^\+.*) {8,8}\t/$1\t\t/) {} |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2724 | while ($fixed[$fixlinenr] =~ |
Joe Perches | c76f4cb | 2014-01-23 15:54:46 -0800 | [diff] [blame] | 2725 | s/(^\+.*) +\t/$1\t/) {} |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2726 | } |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2727 | } |
| 2728 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2729 | # check for && or || at the start of a line |
| 2730 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
| 2731 | CHK("LOGICAL_CONTINUATIONS", |
| 2732 | "Logical continuations should be on the previous line\n" . $hereprev); |
| 2733 | } |
| 2734 | |
| 2735 | # check multi-line statement indentation matches previous line |
| 2736 | if ($^V && $^V ge 5.10.0 && |
Joe Perches | 91cb519 | 2014-04-03 14:49:32 -0700 | [diff] [blame] | 2737 | $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] | 2738 | $prevline =~ /^\+(\t*)(.*)$/; |
| 2739 | my $oldindent = $1; |
| 2740 | my $rest = $2; |
| 2741 | |
| 2742 | my $pos = pos_last_openparen($rest); |
| 2743 | if ($pos >= 0) { |
Joe Perches | b34a26f | 2012-07-30 14:41:16 -0700 | [diff] [blame] | 2744 | $line =~ /^(\+| )([ \t]*)/; |
| 2745 | my $newindent = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2746 | |
| 2747 | my $goodtabindent = $oldindent . |
| 2748 | "\t" x ($pos / 8) . |
| 2749 | " " x ($pos % 8); |
| 2750 | my $goodspaceindent = $oldindent . " " x $pos; |
| 2751 | |
| 2752 | if ($newindent ne $goodtabindent && |
| 2753 | $newindent ne $goodspaceindent) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2754 | |
| 2755 | if (CHK("PARENTHESIS_ALIGNMENT", |
| 2756 | "Alignment should match open parenthesis\n" . $hereprev) && |
| 2757 | $fix && $line =~ /^\+/) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2758 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2759 | s/^\+[ \t]*/\+$goodtabindent/; |
| 2760 | } |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2761 | } |
| 2762 | } |
| 2763 | } |
| 2764 | |
Joe Perches | 6ab3a97 | 2015-04-16 12:44:05 -0700 | [diff] [blame] | 2765 | # check for space after cast like "(int) foo" or "(struct foo) bar" |
| 2766 | # avoid checking a few false positives: |
| 2767 | # "sizeof(<type>)" or "__alignof__(<type>)" |
| 2768 | # function pointer declarations like "(*foo)(int) = bar;" |
| 2769 | # structure definitions like "(struct foo) { 0 };" |
| 2770 | # multiline macros that define functions |
| 2771 | # known attributes or the __attribute__ keyword |
| 2772 | if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && |
| 2773 | (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2774 | if (CHK("SPACING", |
Joe Perches | f27c95d | 2014-08-06 16:10:52 -0700 | [diff] [blame] | 2775 | "No space is necessary after a cast\n" . $herecurr) && |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2776 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2777 | $fixed[$fixlinenr] =~ |
Joe Perches | f27c95d | 2014-08-06 16:10:52 -0700 | [diff] [blame] | 2778 | s/(\(\s*$Type\s*\))[ \t]+/$1/; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2779 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 2780 | } |
| 2781 | |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 2782 | # Block comment styles |
| 2783 | # Networking with an initial /* |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2784 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | fdb4bcd | 2013-07-03 15:05:23 -0700 | [diff] [blame] | 2785 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
Joe Perches | 85ad978 | 2014-04-03 14:49:20 -0700 | [diff] [blame] | 2786 | $rawline =~ /^\+[ \t]*\*/ && |
| 2787 | $realline > 2) { |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2788 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2789 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 2790 | } |
| 2791 | |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 2792 | # Block comments use * on subsequent lines |
| 2793 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment |
| 2794 | $prevrawline =~ /^\+.*?\/\*/ && #starting /* |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2795 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
Joe Perches | 61135e9 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 2796 | $rawline =~ /^\+/ && #line is new |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2797 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 2798 | WARN("BLOCK_COMMENT_STYLE", |
| 2799 | "Block comments use * on subsequent lines\n" . $hereprev); |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2800 | } |
| 2801 | |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 2802 | # Block comments use */ on trailing lines |
| 2803 | if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
Joe Perches | c24f9f1 | 2012-11-08 15:53:29 -0800 | [diff] [blame] | 2804 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 2805 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 2806 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Joe Perches | 86406b1 | 2015-09-09 15:37:41 -0700 | [diff] [blame] | 2807 | WARN("BLOCK_COMMENT_STYLE", |
| 2808 | "Block comments use a trailing */ on a separate line\n" . $herecurr); |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
Joe Perches | 7f61919 | 2014-08-06 16:10:39 -0700 | [diff] [blame] | 2811 | # check for missing blank lines after struct/union declarations |
| 2812 | # with exceptions for various attributes and macros |
| 2813 | if ($prevline =~ /^[\+ ]};?\s*$/ && |
| 2814 | $line =~ /^\+/ && |
| 2815 | !($line =~ /^\+\s*$/ || |
| 2816 | $line =~ /^\+\s*EXPORT_SYMBOL/ || |
| 2817 | $line =~ /^\+\s*MODULE_/i || |
| 2818 | $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || |
| 2819 | $line =~ /^\+[a-z_]*init/ || |
| 2820 | $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || |
| 2821 | $line =~ /^\+\s*DECLARE/ || |
| 2822 | $line =~ /^\+\s*__setup/)) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2823 | if (CHK("LINE_SPACING", |
| 2824 | "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && |
| 2825 | $fix) { |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 2826 | fix_insert_line($fixlinenr, "\+"); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2827 | } |
Joe Perches | 7f61919 | 2014-08-06 16:10:39 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 2830 | # check for multiple consecutive blank lines |
| 2831 | if ($prevline =~ /^[\+ ]\s*$/ && |
| 2832 | $line =~ /^\+\s*$/ && |
| 2833 | $last_blank_line != ($linenr - 1)) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2834 | if (CHK("LINE_SPACING", |
| 2835 | "Please don't use multiple blank lines\n" . $hereprev) && |
| 2836 | $fix) { |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 2837 | fix_delete_line($fixlinenr, $rawline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2838 | } |
| 2839 | |
Joe Perches | 365dd4e | 2014-08-06 16:10:42 -0700 | [diff] [blame] | 2840 | $last_blank_line = $linenr; |
| 2841 | } |
| 2842 | |
Joe Perches | 3b617e3 | 2014-04-03 14:49:28 -0700 | [diff] [blame] | 2843 | # check for missing blank lines after declarations |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 2844 | if ($sline =~ /^\+\s+\S/ && #Not at char 1 |
| 2845 | # actual declarations |
| 2846 | ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
Joe Perches | 5a4e1fd | 2014-08-06 16:10:33 -0700 | [diff] [blame] | 2847 | # function pointer declarations |
| 2848 | $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 2849 | # foo bar; where foo is some local typedef or #define |
| 2850 | $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
| 2851 | # known declaration macros |
| 2852 | $prevline =~ /^\+\s+$declaration_macros/) && |
| 2853 | # for "else if" which can look like "$Ident $Ident" |
| 2854 | !($prevline =~ /^\+\s+$c90_Keywords\b/ || |
| 2855 | # other possible extensions of declaration lines |
| 2856 | $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || |
| 2857 | # not starting a section or a macro "\" extended line |
| 2858 | $prevline =~ /(?:\{\s*|\\)$/) && |
| 2859 | # looks like a declaration |
| 2860 | !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || |
Joe Perches | 5a4e1fd | 2014-08-06 16:10:33 -0700 | [diff] [blame] | 2861 | # function pointer declarations |
| 2862 | $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 2863 | # foo bar; where foo is some local typedef or #define |
| 2864 | $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || |
| 2865 | # known declaration macros |
| 2866 | $sline =~ /^\+\s+$declaration_macros/ || |
| 2867 | # start of struct or union or enum |
Joe Perches | 3b617e3 | 2014-04-03 14:49:28 -0700 | [diff] [blame] | 2868 | $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || |
Joe Perches | 3f7bac0 | 2014-06-04 16:12:04 -0700 | [diff] [blame] | 2869 | # start or end of block or continuation of declaration |
| 2870 | $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || |
| 2871 | # bitfield continuation |
| 2872 | $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || |
| 2873 | # other possible extensions of declaration lines |
| 2874 | $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && |
| 2875 | # indentation of previous and current line are the same |
| 2876 | (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2877 | if (WARN("LINE_SPACING", |
| 2878 | "Missing a blank line after declarations\n" . $hereprev) && |
| 2879 | $fix) { |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 2880 | fix_insert_line($fixlinenr, "\+"); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 2881 | } |
Joe Perches | 3b617e3 | 2014-04-03 14:49:28 -0700 | [diff] [blame] | 2882 | } |
| 2883 | |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2884 | # check for spaces at the beginning of a line. |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 2885 | # Exceptions: |
| 2886 | # 1) within comments |
| 2887 | # 2) indented preprocessor commands |
| 2888 | # 3) hanging labels |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2889 | if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2890 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2891 | if (WARN("LEADING_SPACE", |
| 2892 | "please, no spaces at the start of a line\n" . $herevet) && |
| 2893 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 2894 | $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2895 | } |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2896 | } |
| 2897 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2898 | # check we are in a valid C source file if not then ignore this hunk |
| 2899 | next if ($realfile !~ /\.(h|c)$/); |
| 2900 | |
Joe Perches | 032a4c0 | 2014-08-06 16:10:29 -0700 | [diff] [blame] | 2901 | # check indentation of any line with a bare else |
Joe Perches | 840080a | 2014-10-13 15:51:59 -0700 | [diff] [blame] | 2902 | # (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] | 2903 | # if the previous line is a break or return and is indented 1 tab more... |
| 2904 | if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { |
| 2905 | my $tabs = length($1) + 1; |
Joe Perches | 840080a | 2014-10-13 15:51:59 -0700 | [diff] [blame] | 2906 | if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || |
| 2907 | ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && |
| 2908 | defined $lines[$linenr] && |
| 2909 | $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { |
Joe Perches | 032a4c0 | 2014-08-06 16:10:29 -0700 | [diff] [blame] | 2910 | WARN("UNNECESSARY_ELSE", |
| 2911 | "else is not generally useful after a break or return\n" . $hereprev); |
| 2912 | } |
| 2913 | } |
| 2914 | |
Joe Perches | c00df19 | 2014-08-06 16:11:01 -0700 | [diff] [blame] | 2915 | # check indentation of a line with a break; |
| 2916 | # if the previous line is a goto or return and is indented the same # of tabs |
| 2917 | if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { |
| 2918 | my $tabs = $1; |
| 2919 | if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { |
| 2920 | WARN("UNNECESSARY_BREAK", |
| 2921 | "break is not useful after a goto or return\n" . $hereprev); |
| 2922 | } |
| 2923 | } |
| 2924 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 2925 | # discourage the addition of CONFIG_EXPERIMENTAL in #if(def). |
| 2926 | if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { |
| 2927 | WARN("CONFIG_EXPERIMENTAL", |
| 2928 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 2929 | } |
| 2930 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2931 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2932 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2933 | WARN("CVS_KEYWORD", |
| 2934 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2935 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2936 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2937 | # Blackfin: don't use __builtin_bfin_[cs]sync |
| 2938 | if ($line =~ /__builtin_bfin_csync/) { |
| 2939 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2940 | ERROR("CSYNC", |
| 2941 | "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2942 | } |
| 2943 | if ($line =~ /__builtin_bfin_ssync/) { |
| 2944 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2945 | ERROR("SSYNC", |
| 2946 | "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2947 | } |
| 2948 | |
Joe Perches | 56e77d7 | 2013-02-21 16:44:14 -0800 | [diff] [blame] | 2949 | # check for old HOTPLUG __dev<foo> section markings |
| 2950 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 2951 | WARN("HOTPLUG_SECTION", |
| 2952 | "Using $1 is unnecessary\n" . $herecurr); |
| 2953 | } |
| 2954 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2955 | # Check for potential 'bare' types |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2956 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 2957 | $realline_next); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2958 | #print "LINE<$line>\n"; |
| 2959 | if ($linenr >= $suppress_statement && |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 2960 | $realcnt && $sline =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2961 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2962 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2963 | $stat =~ s/\n./\n /g; |
| 2964 | $cond =~ s/\n./\n /g; |
| 2965 | |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2966 | #print "linenr<$linenr> <$stat>\n"; |
| 2967 | # If this statement has no statement boundaries within |
| 2968 | # it there is no point in retrying a statement scan |
| 2969 | # until we hit end of it. |
| 2970 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 2971 | if ($frag !~ /(?:{|;)/) { |
| 2972 | #print "skip<$line_nr_next>\n"; |
| 2973 | $suppress_statement = $line_nr_next; |
| 2974 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 2975 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2976 | # Find the real next line. |
| 2977 | $realline_next = $line_nr_next; |
| 2978 | if (defined $realline_next && |
| 2979 | (!defined $lines[$realline_next - 1] || |
| 2980 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 2981 | $realline_next++; |
| 2982 | } |
| 2983 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2984 | my $s = $stat; |
| 2985 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2986 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2987 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2988 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2989 | |
| 2990 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2991 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2992 | |
Andy Whitcroft | 463f286 | 2009-09-21 17:04:34 -0700 | [diff] [blame] | 2993 | } elsif ($s =~ /^.\s*else\b/s) { |
| 2994 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2995 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 2996 | } 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] | 2997 | my $type = $1; |
| 2998 | $type =~ s/\s+/ /g; |
| 2999 | possible($type, "A:" . $s); |
| 3000 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3001 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a84062 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3002 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3003 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3004 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3005 | |
| 3006 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3007 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3008 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3009 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3010 | |
| 3011 | # Check for any sort of function declaration. |
| 3012 | # int foo(something bar, other baz); |
| 3013 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3014 | 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] | 3015 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3016 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3017 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3018 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3019 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3020 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3021 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3022 | 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] | 3023 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3024 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3025 | } |
| 3026 | } |
| 3027 | } |
| 3028 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3031 | # |
| 3032 | # Checks which may be anchored in the context. |
| 3033 | # |
| 3034 | |
| 3035 | # Check for switch () and associated case and default |
| 3036 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3037 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 3038 | my $err = ''; |
| 3039 | my $sep = ''; |
| 3040 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 3041 | shift(@ctx); |
| 3042 | for my $ctx (@ctx) { |
| 3043 | my ($clen, $cindent) = line_stats($ctx); |
| 3044 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 3045 | $indent != $cindent) { |
| 3046 | $err .= "$sep$ctx\n"; |
| 3047 | $sep = ''; |
| 3048 | } else { |
| 3049 | $sep = "[...]\n"; |
| 3050 | } |
| 3051 | } |
| 3052 | if ($err ne '') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3053 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 3054 | "switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3055 | } |
| 3056 | } |
| 3057 | |
| 3058 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 3059 | # or if that brace on the next line is for something else |
Joe Perches | 0fe3dc2 | 2014-08-06 16:11:16 -0700 | [diff] [blame] | 3060 | 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] | 3061 | my $pre_ctx = "$1$2"; |
| 3062 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3063 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Joe Perches | 8eef05d | 2012-02-03 15:20:39 -0800 | [diff] [blame] | 3064 | |
| 3065 | if ($line =~ /^\+\t{6,}/) { |
| 3066 | WARN("DEEP_INDENTATION", |
| 3067 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 3068 | } |
| 3069 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3070 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 3071 | my $ctx = join("\n", @ctx); |
| 3072 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 3073 | my $ctx_ln = $linenr; |
| 3074 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3075 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 3076 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 3077 | defined $lines[$ctx_ln - 1] && |
| 3078 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 3079 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 3080 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3081 | $ctx_ln++; |
| 3082 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 3083 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 3084 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 3085 | #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] | 3086 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3087 | 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] | 3088 | ERROR("OPEN_BRACE", |
| 3089 | "that open brace { should be on the previous line\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 3090 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3091 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3092 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 3093 | $ctx =~ /\)\s*\;\s*$/ && |
| 3094 | defined $lines[$ctx_ln - 1]) |
| 3095 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3096 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 3097 | if ($nindent > $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3098 | WARN("TRAILING_SEMICOLON", |
| 3099 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 3100 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3101 | } |
| 3102 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3103 | } |
| 3104 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 3105 | # Check relative indent for conditionals and blocks. |
Joe Perches | 0fe3dc2 | 2014-08-06 16:11:16 -0700 | [diff] [blame] | 3106 | 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] | 3107 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 3108 | ctx_statement_block($linenr, $realcnt, 0) |
| 3109 | if (!defined $stat); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 3110 | my ($s, $c) = ($stat, $cond); |
| 3111 | |
| 3112 | substr($s, 0, length($c), ''); |
| 3113 | |
Joe Perches | 9f5af48 | 2015-09-09 15:37:30 -0700 | [diff] [blame] | 3114 | # remove inline comments |
| 3115 | $s =~ s/$;/ /g; |
| 3116 | $c =~ s/$;/ /g; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 3117 | |
| 3118 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 3119 | my @newlines = ($c =~ /\n/gs); |
| 3120 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 3121 | |
Joe Perches | 9f5af48 | 2015-09-09 15:37:30 -0700 | [diff] [blame] | 3122 | # Make sure we remove the line prefixes as we have |
| 3123 | # none on the first line, and are going to readd them |
| 3124 | # where necessary. |
| 3125 | $s =~ s/\n./\n/gs; |
| 3126 | while ($s =~ /\n\s+\\\n/) { |
| 3127 | $cond_lines += $s =~ s/\n\s+\\\n/\n/g; |
| 3128 | } |
| 3129 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 3130 | # We want to check the first line inside the block |
| 3131 | # starting at the end of the conditional, so remove: |
| 3132 | # 1) any blank line termination |
| 3133 | # 2) any opening brace { on end of the line |
| 3134 | # 3) any do (...) { |
| 3135 | my $continuation = 0; |
| 3136 | my $check = 0; |
| 3137 | $s =~ s/^.*\bdo\b//; |
| 3138 | $s =~ s/^\s*{//; |
| 3139 | if ($s =~ s/^\s*\\//) { |
| 3140 | $continuation = 1; |
| 3141 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3142 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 3143 | $check = 1; |
| 3144 | $cond_lines++; |
| 3145 | } |
| 3146 | |
| 3147 | # Also ignore a loop construct at the end of a |
| 3148 | # preprocessor statement. |
| 3149 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 3150 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 3151 | $check = 0; |
| 3152 | } |
| 3153 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3154 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 3155 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3156 | while ($cond_ptr != $cond_lines) { |
| 3157 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 3158 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 3159 | # If we see an #else/#elif then the code |
| 3160 | # is not linear. |
| 3161 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 3162 | $check = 0; |
| 3163 | } |
| 3164 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3165 | # Ignore: |
| 3166 | # 1) blank lines, they should be at 0, |
| 3167 | # 2) preprocessor lines, and |
| 3168 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 3169 | if ($continuation || |
| 3170 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3171 | $s =~ /^\s*#\s*?/ || |
| 3172 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 3173 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 30dad6e | 2009-09-21 17:04:36 -0700 | [diff] [blame] | 3174 | if ($s =~ s/^.*?\n//) { |
| 3175 | $cond_lines++; |
| 3176 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3177 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | my (undef, $sindent) = line_stats("+" . $s); |
| 3181 | my $stat_real = raw_line($linenr, $cond_lines); |
| 3182 | |
| 3183 | # Check if either of these lines are modified, else |
| 3184 | # this is not this patch's fault. |
| 3185 | if (!defined($stat_real) || |
| 3186 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 3187 | $check = 0; |
| 3188 | } |
| 3189 | if (defined($stat_real) && $cond_lines > 1) { |
| 3190 | $stat_real = "[...]\n$stat_real"; |
| 3191 | } |
| 3192 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 3193 | #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] | 3194 | |
Joe Perches | 9f5af48 | 2015-09-09 15:37:30 -0700 | [diff] [blame] | 3195 | if ($check && $s ne '' && |
| 3196 | (($sindent % 8) != 0 || |
| 3197 | ($sindent < $indent) || |
| 3198 | ($sindent > $indent + 8))) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3199 | WARN("SUSPECT_CODE_INDENT", |
| 3200 | "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] | 3201 | } |
| 3202 | } |
| 3203 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3204 | # Track the 'values' across context and added lines. |
| 3205 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3206 | my ($curr_values, $curr_vars) = |
| 3207 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3208 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3209 | if ($dbg_values) { |
| 3210 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3211 | print "$linenr > .$outline\n"; |
| 3212 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3213 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3214 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3215 | $prev_values = substr($curr_values, -1); |
| 3216 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3217 | #ignore lines not being added |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3218 | next if ($line =~ /^[^\+]/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3219 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3220 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 3221 | if ($dbg_type) { |
| 3222 | if ($line =~ /^.\s*$Declare\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3223 | ERROR("TEST_TYPE", |
| 3224 | "TEST: is type\n" . $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 3225 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3226 | ERROR("TEST_NOT_TYPE", |
| 3227 | "TEST: is not type ($1 is)\n". $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 3228 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3229 | next; |
| 3230 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 3231 | # TEST: allow direct testing of the attribute matcher. |
| 3232 | if ($dbg_attr) { |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 3233 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3234 | ERROR("TEST_ATTR", |
| 3235 | "TEST: is attr\n" . $herecurr); |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 3236 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3237 | ERROR("TEST_NOT_ATTR", |
| 3238 | "TEST: is not attr ($1 is)\n". $herecurr); |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 3239 | } |
| 3240 | next; |
| 3241 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3242 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3243 | # check for initialisation to aggregates open brace on the next line |
Andy Whitcroft | 99423c2 | 2009-10-26 16:50:15 -0700 | [diff] [blame] | 3244 | if ($line =~ /^.\s*{/ && |
| 3245 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3246 | if (ERROR("OPEN_BRACE", |
| 3247 | "that open brace { should be on the previous line\n" . $hereprev) && |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 3248 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 3249 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 3250 | fix_delete_line($fixlinenr, $rawline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3251 | my $fixedline = $prevrawline; |
| 3252 | $fixedline =~ s/\s*=\s*$/ = {/; |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 3253 | fix_insert_line($fixlinenr, $fixedline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3254 | $fixedline = $line; |
| 3255 | $fixedline =~ s/^(.\s*){\s*/$1/; |
Joe Perches | f2d7e4d | 2014-08-06 16:11:07 -0700 | [diff] [blame] | 3256 | fix_insert_line($fixlinenr, $fixedline); |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 3257 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3260 | # |
| 3261 | # Checks which are anchored on the added line. |
| 3262 | # |
| 3263 | |
| 3264 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3265 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3266 | my $path = $1; |
| 3267 | if ($path =~ m{//}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3268 | ERROR("MALFORMED_INCLUDE", |
Joe Perches | 495e9d8 | 2012-12-20 15:05:37 -0800 | [diff] [blame] | 3269 | "malformed #include filename\n" . $herecurr); |
| 3270 | } |
| 3271 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 3272 | ERROR("UAPI_INCLUDE", |
| 3273 | "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] | 3274 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3275 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3276 | |
| 3277 | # no C99 // comments |
| 3278 | if ($line =~ m{//}) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3279 | if (ERROR("C99_COMMENTS", |
| 3280 | "do not use C99 // comments\n" . $herecurr) && |
| 3281 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3282 | my $line = $fixed[$fixlinenr]; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3283 | if ($line =~ /\/\/(.*)$/) { |
| 3284 | my $comment = trim($1); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3285 | $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3286 | } |
| 3287 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3288 | } |
| 3289 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3290 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3291 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3292 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3293 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 3294 | # the whole statement. |
| 3295 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 3296 | if (defined $realline_next && |
| 3297 | exists $lines[$realline_next - 1] && |
| 3298 | !defined $suppress_export{$realline_next} && |
| 3299 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 3300 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 3301 | # Handle definitions which produce identifiers with |
| 3302 | # a prefix: |
| 3303 | # XXX(foo); |
| 3304 | # EXPORT_SYMBOL(something_foo); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3305 | my $name = $1; |
Andy Whitcroft | 87a5387 | 2012-01-10 15:10:04 -0800 | [diff] [blame] | 3306 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 3307 | $name =~ /^${Ident}_$2/) { |
| 3308 | #print "FOO C name<$name>\n"; |
| 3309 | $suppress_export{$realline_next} = 1; |
| 3310 | |
| 3311 | } elsif ($stat !~ /(?: |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3312 | \n.}\s*$| |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3313 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 3314 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 3315 | ^.LIST_HEAD\(\Q$name\E\)| |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3316 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 3317 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3318 | )/x) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3319 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 3320 | $suppress_export{$realline_next} = 2; |
| 3321 | } else { |
| 3322 | $suppress_export{$realline_next} = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3323 | } |
| 3324 | } |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3325 | if (!defined $suppress_export{$linenr} && |
| 3326 | $prevline =~ /^.\s*$/ && |
| 3327 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 3328 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 3329 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 3330 | $suppress_export{$linenr} = 2; |
| 3331 | } |
| 3332 | if (defined $suppress_export{$linenr} && |
| 3333 | $suppress_export{$linenr} == 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3334 | WARN("EXPORT_SYMBOL", |
| 3335 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 3336 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3337 | |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 3338 | # check for global initialisers. |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 3339 | if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3340 | if (ERROR("GLOBAL_INITIALISERS", |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 3341 | "do not initialise globals to $1\n" . $herecurr) && |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3342 | $fix) { |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 3343 | $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3344 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3345 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3346 | # check for static initialisers. |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 3347 | if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3348 | if (ERROR("INITIALISED_STATIC", |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 3349 | "do not initialise statics to $1\n" . |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3350 | $herecurr) && |
| 3351 | $fix) { |
Joe Perches | 6d32f7a | 2015-11-06 16:31:37 -0800 | [diff] [blame] | 3352 | $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3353 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3354 | } |
| 3355 | |
Joe Perches | 1813087 | 2014-08-06 16:11:22 -0700 | [diff] [blame] | 3356 | # check for misordered declarations of char/short/int/long with signed/unsigned |
| 3357 | while ($sline =~ m{(\b$TypeMisordered\b)}g) { |
| 3358 | my $tmp = trim($1); |
| 3359 | WARN("MISORDERED_TYPE", |
| 3360 | "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); |
| 3361 | } |
| 3362 | |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 3363 | # check for static const char * arrays. |
| 3364 | 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] | 3365 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 3366 | "static const char * array should probably be static const char * const\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 3367 | $herecurr); |
| 3368 | } |
| 3369 | |
| 3370 | # check for static char foo[] = "bar" declarations. |
| 3371 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3372 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 3373 | "static char array declaration should probably be static const char\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 3374 | $herecurr); |
| 3375 | } |
| 3376 | |
Joe Perches | ab7e23f | 2015-04-16 12:44:22 -0700 | [diff] [blame] | 3377 | # check for const <foo> const where <foo> is not a pointer or array type |
| 3378 | if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { |
| 3379 | my $found = $1; |
| 3380 | if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { |
| 3381 | WARN("CONST_CONST", |
| 3382 | "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); |
| 3383 | } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { |
| 3384 | WARN("CONST_CONST", |
| 3385 | "'const $found const' should probably be 'const $found'\n" . $herecurr); |
| 3386 | } |
| 3387 | } |
| 3388 | |
Joe Perches | 9b0fa60 | 2014-04-03 14:49:18 -0700 | [diff] [blame] | 3389 | # check for non-global char *foo[] = {"bar", ...} declarations. |
| 3390 | if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { |
| 3391 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 3392 | "char * array declaration might be better as static const\n" . |
| 3393 | $herecurr); |
| 3394 | } |
| 3395 | |
Joe Perches | b598b67 | 2015-04-16 12:44:36 -0700 | [diff] [blame] | 3396 | # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) |
| 3397 | if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { |
| 3398 | my $array = $1; |
| 3399 | if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) { |
| 3400 | my $array_div = $1; |
| 3401 | if (WARN("ARRAY_SIZE", |
| 3402 | "Prefer ARRAY_SIZE($array)\n" . $herecurr) && |
| 3403 | $fix) { |
| 3404 | $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; |
| 3405 | } |
| 3406 | } |
| 3407 | } |
| 3408 | |
Joe Perches | b36190c | 2014-01-27 17:07:18 -0800 | [diff] [blame] | 3409 | # check for function declarations without arguments like "int foo()" |
| 3410 | if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { |
| 3411 | if (ERROR("FUNCTION_WITHOUT_ARGS", |
| 3412 | "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && |
| 3413 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3414 | $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; |
Joe Perches | b36190c | 2014-01-27 17:07:18 -0800 | [diff] [blame] | 3415 | } |
| 3416 | } |
| 3417 | |
Joe Perches | 92e112f | 2013-12-13 11:36:22 -0700 | [diff] [blame] | 3418 | # check for uses of DEFINE_PCI_DEVICE_TABLE |
| 3419 | if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { |
| 3420 | if (WARN("DEFINE_PCI_DEVICE_TABLE", |
| 3421 | "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && |
| 3422 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3423 | $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] | 3424 | } |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 3425 | } |
| 3426 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3427 | # check for new typedefs, only function parameters and sparse annotations |
| 3428 | # make sense. |
| 3429 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 8054576 | 2009-01-06 14:41:26 -0800 | [diff] [blame] | 3430 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3431 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 3432 | $line !~ /\b$typeTypedefs\b/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3433 | $line !~ /\b__bitwise(?:__|)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3434 | WARN("NEW_TYPEDEFS", |
| 3435 | "do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3439 | # (char*[ const]) |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 3440 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 3441 | #print "AA<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3442 | my ($ident, $from, $to) = ($1, $2, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3443 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3444 | # Should start with a space. |
| 3445 | $to =~ s/^(\S)/ $1/; |
| 3446 | # Should not end with a space. |
| 3447 | $to =~ s/\s+$//; |
| 3448 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 3449 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3450 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3451 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3452 | ## print "1: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3453 | if ($from ne $to) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3454 | if (ERROR("POINTER_LOCATION", |
| 3455 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && |
| 3456 | $fix) { |
| 3457 | my $sub_from = $ident; |
| 3458 | my $sub_to = $ident; |
| 3459 | $sub_to =~ s/\Q$from\E/$to/; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3460 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3461 | s@\Q$sub_from\E@$sub_to@; |
| 3462 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3463 | } |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 3464 | } |
| 3465 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 3466 | #print "BB<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3467 | my ($match, $from, $to, $ident) = ($1, $2, $2, $3); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3468 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3469 | # Should start with a space. |
| 3470 | $to =~ s/^(\S)/ $1/; |
| 3471 | # Should not end with a space. |
| 3472 | $to =~ s/\s+$//; |
| 3473 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 3474 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3475 | } |
| 3476 | # Modifiers should have spaces. |
| 3477 | $to =~ s/(\b$Modifier$)/$1 /; |
| 3478 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3479 | ## print "2: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 667026e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 3480 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3481 | if (ERROR("POINTER_LOCATION", |
| 3482 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && |
| 3483 | $fix) { |
| 3484 | |
| 3485 | my $sub_from = $match; |
| 3486 | my $sub_to = $match; |
| 3487 | $sub_to =~ s/\Q$from\E/$to/; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3488 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3489 | s@\Q$sub_from\E@$sub_to@; |
| 3490 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 3491 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3492 | } |
| 3493 | |
Joe Perches | 9d3e3c7 | 2015-09-09 15:37:27 -0700 | [diff] [blame] | 3494 | # avoid BUG() or BUG_ON() |
| 3495 | if ($line =~ /\b(?:BUG|BUG_ON)\b/) { |
| 3496 | my $msg_type = \&WARN; |
| 3497 | $msg_type = \&CHK if ($file); |
| 3498 | &{$msg_type}("AVOID_BUG", |
| 3499 | "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); |
| 3500 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3501 | |
Joe Perches | 9d3e3c7 | 2015-09-09 15:37:27 -0700 | [diff] [blame] | 3502 | # avoid LINUX_VERSION_CODE |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3503 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3504 | WARN("LINUX_VERSION_CODE", |
| 3505 | "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] | 3506 | } |
| 3507 | |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 3508 | # check for uses of printk_ratelimit |
| 3509 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3510 | WARN("PRINTK_RATELIMITED", |
Joe Perches | 101ee68 | 2015-02-13 14:38:54 -0800 | [diff] [blame] | 3511 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 3512 | } |
| 3513 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3514 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 3515 | # same line do not need a level, so we use the current block context |
| 3516 | # 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] | 3517 | # 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] | 3518 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3519 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3520 | my $ok = 0; |
| 3521 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 3522 | #print "CHECK<$lines[$ln - 1]\n"; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3523 | # we have a preceding printk if it ends |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3524 | # with "\n" ignore it, else it is to blame |
| 3525 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 3526 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 3527 | $ok = 1; |
| 3528 | } |
| 3529 | last; |
| 3530 | } |
| 3531 | } |
| 3532 | if ($ok == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3533 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 3534 | "printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3535 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3536 | } |
| 3537 | |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 3538 | if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { |
| 3539 | my $orig = $1; |
| 3540 | my $level = lc($orig); |
| 3541 | $level = "warn" if ($level eq "warning"); |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 3542 | my $level2 = $level; |
| 3543 | $level2 = "dbg" if ($level eq "debug"); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 3544 | WARN("PREFER_PR_LEVEL", |
Yogesh Chaudhari | daa8b05 | 2014-04-03 14:49:23 -0700 | [diff] [blame] | 3545 | "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] | 3546 | } |
| 3547 | |
| 3548 | if ($line =~ /\bpr_warning\s*\(/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3549 | if (WARN("PREFER_PR_LEVEL", |
| 3550 | "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && |
| 3551 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3552 | $fixed[$fixlinenr] =~ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3553 | s/\bpr_warning\b/pr_warn/; |
| 3554 | } |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 3555 | } |
| 3556 | |
Joe Perches | dc13931 | 2013-02-21 16:44:13 -0800 | [diff] [blame] | 3557 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 3558 | my $orig = $1; |
| 3559 | my $level = lc($orig); |
| 3560 | $level = "warn" if ($level eq "warning"); |
| 3561 | $level = "dbg" if ($level eq "debug"); |
| 3562 | WARN("PREFER_DEV_LEVEL", |
| 3563 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 3564 | } |
| 3565 | |
Andy Lutomirski | 91c9afa | 2015-04-16 12:44:44 -0700 | [diff] [blame] | 3566 | # ENOSYS means "bad syscall nr" and nothing else. This will have a small |
| 3567 | # number of false positives, but assembly files are not checked, so at |
| 3568 | # least the arch entry code will not trigger this warning. |
| 3569 | if ($line =~ /\bENOSYS\b/) { |
| 3570 | WARN("ENOSYS", |
| 3571 | "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); |
| 3572 | } |
| 3573 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3574 | # function brace can't be on same line, except for #defines of do while, |
| 3575 | # or if closed on same line |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 3576 | if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and |
Eddie Kovsky | 4e5d56b | 2015-09-09 15:37:52 -0700 | [diff] [blame] | 3577 | !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) { |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 3578 | if (ERROR("OPEN_BRACE", |
| 3579 | "open brace '{' following function declarations go on the next line\n" . $herecurr) && |
| 3580 | $fix) { |
| 3581 | fix_delete_line($fixlinenr, $rawline); |
| 3582 | my $fixed_line = $rawline; |
| 3583 | $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; |
| 3584 | my $line1 = $1; |
| 3585 | my $line2 = $2; |
| 3586 | fix_insert_line($fixlinenr, ltrim($line1)); |
| 3587 | fix_insert_line($fixlinenr, "\+{"); |
| 3588 | if ($line2 !~ /^\s*$/) { |
| 3589 | fix_insert_line($fixlinenr, "\+\t" . trim($line2)); |
| 3590 | } |
| 3591 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3592 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3593 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3594 | # open braces for enum, union and struct go on the same line. |
| 3595 | if ($line =~ /^.\s*{/ && |
| 3596 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Joe Perches | 8d18247 | 2014-08-06 16:11:12 -0700 | [diff] [blame] | 3597 | if (ERROR("OPEN_BRACE", |
| 3598 | "open brace '{' following $1 go on the same line\n" . $hereprev) && |
| 3599 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 3600 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 3601 | fix_delete_line($fixlinenr, $rawline); |
| 3602 | my $fixedline = rtrim($prevrawline) . " {"; |
| 3603 | fix_insert_line($fixlinenr, $fixedline); |
| 3604 | $fixedline = $rawline; |
| 3605 | $fixedline =~ s/^(.\s*){\s*/$1\t/; |
| 3606 | if ($fixedline !~ /^\+\s*$/) { |
| 3607 | fix_insert_line($fixlinenr, $fixedline); |
| 3608 | } |
| 3609 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3610 | } |
| 3611 | |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3612 | # missing space after union, struct or enum definition |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3613 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { |
| 3614 | if (WARN("SPACING", |
| 3615 | "missing space after $1 definition\n" . $herecurr) && |
| 3616 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3617 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3618 | s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; |
| 3619 | } |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 3620 | } |
| 3621 | |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3622 | # Function pointer declarations |
| 3623 | # check spacing between type, funcptr, and args |
| 3624 | # canonical declaration is "type (*funcptr)(args...)" |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3625 | if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3626 | my $declare = $1; |
| 3627 | my $pre_pointer_space = $2; |
| 3628 | my $post_pointer_space = $3; |
| 3629 | my $funcname = $4; |
| 3630 | my $post_funcname_space = $5; |
| 3631 | my $pre_args_space = $6; |
| 3632 | |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3633 | # the $Declare variable will capture all spaces after the type |
| 3634 | # so check it for a missing trailing missing space but pointer return types |
| 3635 | # don't need a space so don't warn for those. |
| 3636 | my $post_declare_space = ""; |
| 3637 | if ($declare =~ /(\s+)$/) { |
| 3638 | $post_declare_space = $1; |
| 3639 | $declare = rtrim($declare); |
| 3640 | } |
| 3641 | if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3642 | WARN("SPACING", |
| 3643 | "missing space after return type\n" . $herecurr); |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3644 | $post_declare_space = " "; |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | # unnecessary space "type (*funcptr)(args...)" |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3648 | # This test is not currently implemented because these declarations are |
| 3649 | # equivalent to |
| 3650 | # int foo(int bar, ...) |
| 3651 | # and this is form shouldn't/doesn't generate a checkpatch warning. |
| 3652 | # |
| 3653 | # elsif ($declare =~ /\s{2,}$/) { |
| 3654 | # WARN("SPACING", |
| 3655 | # "Multiple spaces after return type\n" . $herecurr); |
| 3656 | # } |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 3657 | |
| 3658 | # unnecessary space "type ( *funcptr)(args...)" |
| 3659 | if (defined $pre_pointer_space && |
| 3660 | $pre_pointer_space =~ /^\s/) { |
| 3661 | WARN("SPACING", |
| 3662 | "Unnecessary space after function pointer open parenthesis\n" . $herecurr); |
| 3663 | } |
| 3664 | |
| 3665 | # unnecessary space "type (* funcptr)(args...)" |
| 3666 | if (defined $post_pointer_space && |
| 3667 | $post_pointer_space =~ /^\s/) { |
| 3668 | WARN("SPACING", |
| 3669 | "Unnecessary space before function pointer name\n" . $herecurr); |
| 3670 | } |
| 3671 | |
| 3672 | # unnecessary space "type (*funcptr )(args...)" |
| 3673 | if (defined $post_funcname_space && |
| 3674 | $post_funcname_space =~ /^\s/) { |
| 3675 | WARN("SPACING", |
| 3676 | "Unnecessary space after function pointer name\n" . $herecurr); |
| 3677 | } |
| 3678 | |
| 3679 | # unnecessary space "type (*funcptr) (args...)" |
| 3680 | if (defined $pre_args_space && |
| 3681 | $pre_args_space =~ /^\s/) { |
| 3682 | WARN("SPACING", |
| 3683 | "Unnecessary space before function pointer arguments\n" . $herecurr); |
| 3684 | } |
| 3685 | |
| 3686 | if (show_type("SPACING") && $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3687 | $fixed[$fixlinenr] =~ |
Joe Perches | 91f72e9 | 2014-04-03 14:49:12 -0700 | [diff] [blame] | 3688 | 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] | 3689 | } |
| 3690 | } |
| 3691 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 3692 | # check for spacing round square brackets; allowed: |
| 3693 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 3694 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 3695 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 3696 | while ($line =~ /(.*?\s)\[/g) { |
| 3697 | my ($where, $prefix) = ($-[1], $1); |
| 3698 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 3699 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Andy Whitcroft | daebc53 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3700 | $prefix !~ /[{,]\s+$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3701 | if (ERROR("BRACKET_SPACE", |
| 3702 | "space prohibited before open square bracket '['\n" . $herecurr) && |
| 3703 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3704 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3705 | s/^(\+.*?)\s+\[/$1\[/; |
| 3706 | } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 3707 | } |
| 3708 | } |
| 3709 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3710 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3711 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3712 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3713 | my $ctx_before = substr($line, 0, $-[1]); |
| 3714 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3715 | |
| 3716 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3717 | if ($name =~ /^(?: |
| 3718 | if|for|while|switch|return|case| |
| 3719 | volatile|__volatile__| |
| 3720 | __attribute__|format|__extension__| |
| 3721 | asm|__asm__)$/x) |
| 3722 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3723 | # cpp #define statements have non-optional spaces, ie |
| 3724 | # if there is a space between the name and the open |
| 3725 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3726 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3727 | |
| 3728 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3729 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3730 | |
| 3731 | # If this whole things ends with a type its most |
| 3732 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3733 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3734 | |
| 3735 | } else { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3736 | if (WARN("SPACING", |
| 3737 | "space prohibited between function name and open parenthesis '('\n" . $herecurr) && |
| 3738 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 3739 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3740 | s/\b$name\s+\(/$name\(/; |
| 3741 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3742 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3743 | } |
Eric Nelson | 9a4cad4 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 3744 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3745 | # Check operator spacing. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3746 | if (!($line=~/\#\s*include/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3747 | my $fixed_line = ""; |
| 3748 | my $line_fixed = 0; |
| 3749 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3750 | my $ops = qr{ |
| 3751 | <<=|>>=|<=|>=|==|!=| |
| 3752 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 3753 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3754 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 3755 | \?:|\?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3756 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3757 | my @elements = split(/($ops|;)/, $opline); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3758 | |
| 3759 | ## print("element count: <" . $#elements . ">\n"); |
| 3760 | ## foreach my $el (@elements) { |
| 3761 | ## print("el: <$el>\n"); |
| 3762 | ## } |
| 3763 | |
| 3764 | my @fix_elements = (); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3765 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3766 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3767 | foreach my $el (@elements) { |
| 3768 | push(@fix_elements, substr($rawline, $off, length($el))); |
| 3769 | $off += length($el); |
| 3770 | } |
| 3771 | |
| 3772 | $off = 0; |
| 3773 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3774 | my $blank = copy_spacing($opline); |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3775 | my $last_after = -1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3776 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3777 | for (my $n = 0; $n < $#elements; $n += 2) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3778 | |
| 3779 | my $good = $fix_elements[$n] . $fix_elements[$n + 1]; |
| 3780 | |
| 3781 | ## print("n: <$n> good: <$good>\n"); |
| 3782 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3783 | $off += length($elements[$n]); |
| 3784 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3785 | # Pick up the preceding and succeeding characters. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3786 | my $ca = substr($opline, 0, $off); |
| 3787 | my $cc = ''; |
| 3788 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 3789 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 3790 | } |
| 3791 | my $cb = "$ca$;$cc"; |
| 3792 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3793 | my $a = ''; |
| 3794 | $a = 'V' if ($elements[$n] ne ''); |
| 3795 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3796 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3797 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 3798 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3799 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3800 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3801 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3802 | |
| 3803 | my $c = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3804 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3805 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 3806 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3807 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3808 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 3809 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 8b1b337 | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 3810 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3811 | } else { |
| 3812 | $c = 'E'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3813 | } |
| 3814 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3815 | my $ctx = "${a}x${c}"; |
| 3816 | |
| 3817 | my $at = "(ctx:$ctx)"; |
| 3818 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3819 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3820 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3821 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3822 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3823 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3824 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3825 | # Get the full operator variant. |
| 3826 | my $opv = $op . substr($curr_vars, $off, 1); |
| 3827 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3828 | # Ignore operators passed as parameters. |
| 3829 | if ($op_type ne 'V' && |
Sam Bobroff | d7fe806 | 2015-04-16 12:44:39 -0700 | [diff] [blame] | 3830 | $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3831 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3832 | # # Ignore comments |
| 3833 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3834 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3835 | # ; 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] | 3836 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3837 | if ($ctx !~ /.x[WEBC]/ && |
| 3838 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3839 | if (ERROR("SPACING", |
| 3840 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3841 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3842 | $line_fixed = 1; |
| 3843 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | # // is a comment |
| 3847 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3848 | |
Joe Perches | b00e481 | 2014-04-03 14:49:33 -0700 | [diff] [blame] | 3849 | # : when part of a bitfield |
| 3850 | } elsif ($opv eq ':B') { |
| 3851 | # skip the bitfield test for now |
| 3852 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3853 | # No spaces for: |
| 3854 | # -> |
Joe Perches | b00e481 | 2014-04-03 14:49:33 -0700 | [diff] [blame] | 3855 | } elsif ($op eq '->') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3856 | if ($ctx =~ /Wx.|.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3857 | if (ERROR("SPACING", |
| 3858 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3859 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3860 | if (defined $fix_elements[$n + 2]) { |
| 3861 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3862 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3863 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3864 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
Joe Perches | 2381097 | 2014-12-10 15:51:32 -0800 | [diff] [blame] | 3867 | # , must not have a space before and must have a space on the right. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3868 | } elsif ($op eq ',') { |
Joe Perches | 2381097 | 2014-12-10 15:51:32 -0800 | [diff] [blame] | 3869 | my $rtrim_before = 0; |
| 3870 | my $space_after = 0; |
| 3871 | if ($ctx =~ /Wx./) { |
| 3872 | if (ERROR("SPACING", |
| 3873 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
| 3874 | $line_fixed = 1; |
| 3875 | $rtrim_before = 1; |
| 3876 | } |
| 3877 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3878 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3879 | if (ERROR("SPACING", |
| 3880 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3881 | $line_fixed = 1; |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3882 | $last_after = $n; |
Joe Perches | 2381097 | 2014-12-10 15:51:32 -0800 | [diff] [blame] | 3883 | $space_after = 1; |
| 3884 | } |
| 3885 | } |
| 3886 | if ($rtrim_before || $space_after) { |
| 3887 | if ($rtrim_before) { |
| 3888 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
| 3889 | } else { |
| 3890 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
| 3891 | } |
| 3892 | if ($space_after) { |
| 3893 | $good .= " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3894 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3895 | } |
| 3896 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3897 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3898 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3899 | #warn "'*' is part of type\n"; |
| 3900 | |
| 3901 | # unary operators should have a space before and |
| 3902 | # none after. May be left adjacent to another |
| 3903 | # unary operator, or a cast |
| 3904 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3905 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 3906 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3907 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3908 | if (ERROR("SPACING", |
| 3909 | "space required before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3910 | if ($n != $last_after + 2) { |
| 3911 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
| 3912 | $line_fixed = 1; |
| 3913 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3914 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3915 | } |
Andy Whitcroft | a3340b3 | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 3916 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3917 | # A unary '*' may be const |
| 3918 | |
| 3919 | } elsif ($ctx =~ /.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3920 | if (ERROR("SPACING", |
| 3921 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3922 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3923 | if (defined $fix_elements[$n + 2]) { |
| 3924 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3925 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3926 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3927 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3928 | } |
| 3929 | |
| 3930 | # unary ++ and unary -- are allowed no space on one side. |
| 3931 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3932 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3933 | if (ERROR("SPACING", |
| 3934 | "space required one side of that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3935 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3936 | $line_fixed = 1; |
| 3937 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3938 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3939 | if ($ctx =~ /Wx[BE]/ || |
| 3940 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3941 | if (ERROR("SPACING", |
| 3942 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3943 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3944 | $line_fixed = 1; |
| 3945 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3946 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3947 | if ($ctx =~ /ExW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3948 | if (ERROR("SPACING", |
| 3949 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3950 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3951 | if (defined $fix_elements[$n + 2]) { |
| 3952 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3953 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3954 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3955 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3956 | } |
| 3957 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3958 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3959 | } elsif ($op eq '<<' or $op eq '>>' or |
| 3960 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 3961 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3962 | $op eq '*' or $op eq '/' or |
| 3963 | $op eq '%') |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3964 | { |
Joe Perches | d2e025f | 2015-02-13 14:38:57 -0800 | [diff] [blame] | 3965 | if ($check) { |
| 3966 | if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { |
| 3967 | if (CHK("SPACING", |
| 3968 | "spaces preferred around that '$op' $at\n" . $hereptr)) { |
| 3969 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3970 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3971 | $line_fixed = 1; |
| 3972 | } |
| 3973 | } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { |
| 3974 | if (CHK("SPACING", |
| 3975 | "space preferred before that '$op' $at\n" . $hereptr)) { |
| 3976 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); |
| 3977 | $line_fixed = 1; |
| 3978 | } |
| 3979 | } |
| 3980 | } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3981 | if (ERROR("SPACING", |
| 3982 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3983 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3984 | if (defined $fix_elements[$n + 2]) { |
| 3985 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3986 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3987 | $line_fixed = 1; |
| 3988 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3989 | } |
| 3990 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3991 | # A colon needs no spaces before when it is |
| 3992 | # terminating a case value or a label. |
| 3993 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 3994 | if ($ctx =~ /Wx./) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3995 | if (ERROR("SPACING", |
| 3996 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3997 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3998 | $line_fixed = 1; |
| 3999 | } |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4000 | } |
| 4001 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4002 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4003 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4004 | my $ok = 0; |
| 4005 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4006 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4007 | if (($op eq '<' && |
| 4008 | $cc =~ /^\S+\@\S+>/) || |
| 4009 | ($op eq '>' && |
| 4010 | $ca =~ /<\S+\@\S+$/)) |
| 4011 | { |
| 4012 | $ok = 1; |
| 4013 | } |
| 4014 | |
Joe Perches | e0df7e1 | 2015-04-16 12:44:53 -0700 | [diff] [blame] | 4015 | # for asm volatile statements |
| 4016 | # ignore a colon with another |
| 4017 | # colon immediately before or after |
| 4018 | if (($op eq ':') && |
| 4019 | ($ca =~ /:$/ || $cc =~ /^:/)) { |
| 4020 | $ok = 1; |
| 4021 | } |
| 4022 | |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 4023 | # messages are ERROR, but ?: are CHK |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 4024 | if ($ok == 0) { |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 4025 | my $msg_type = \&ERROR; |
| 4026 | $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); |
| 4027 | |
| 4028 | if (&{$msg_type}("SPACING", |
| 4029 | "spaces required around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 4030 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 4031 | if (defined $fix_elements[$n + 2]) { |
| 4032 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 4033 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4034 | $line_fixed = 1; |
| 4035 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4036 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4037 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4038 | $off += length($elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4039 | |
| 4040 | ## print("n: <$n> GOOD: <$good>\n"); |
| 4041 | |
| 4042 | $fixed_line = $fixed_line . $good; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4043 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4044 | |
| 4045 | if (($#elements % 2) == 0) { |
| 4046 | $fixed_line = $fixed_line . $fix_elements[$#elements]; |
| 4047 | } |
| 4048 | |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4049 | if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { |
| 4050 | $fixed[$fixlinenr] = $fixed_line; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4054 | } |
| 4055 | |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 4056 | # check for whitespace before a non-naked semicolon |
Joe Perches | d2e248e | 2014-01-23 15:54:41 -0800 | [diff] [blame] | 4057 | if ($line =~ /^\+.*\S\s+;\s*$/) { |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 4058 | if (WARN("SPACING", |
| 4059 | "space prohibited before semicolon\n" . $herecurr) && |
| 4060 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4061 | 1 while $fixed[$fixlinenr] =~ |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 4062 | s/^(\+.*\S)\s+;/$1;/; |
| 4063 | } |
| 4064 | } |
| 4065 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4066 | # check for multiple assignments |
| 4067 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4068 | CHK("MULTIPLE_ASSIGNMENTS", |
| 4069 | "multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4070 | } |
| 4071 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4072 | ## # check for multiple declarations, allowing for a function declaration |
| 4073 | ## # continuation. |
| 4074 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 4075 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 4076 | ## |
| 4077 | ## # Remove any bracketed sections to ensure we do not |
| 4078 | ## # falsly report the parameters of functions. |
| 4079 | ## my $ln = $line; |
| 4080 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 4081 | ## } |
| 4082 | ## if ($ln =~ /,/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4083 | ## WARN("MULTIPLE_DECLARATION", |
| 4084 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4085 | ## } |
| 4086 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4087 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4088 | #need space before brace following if, while, etc |
Eddie Kovsky | 4e5d56b | 2015-09-09 15:37:52 -0700 | [diff] [blame] | 4089 | if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\){/) || |
| 4090 | $line =~ /do\{/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4091 | if (ERROR("SPACING", |
| 4092 | "space required before the open brace '{'\n" . $herecurr) && |
| 4093 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4094 | $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4095 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4096 | } |
| 4097 | |
Joe Perches | c4a62ef | 2013-07-03 15:05:28 -0700 | [diff] [blame] | 4098 | ## # check for blank lines before declarations |
| 4099 | ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && |
| 4100 | ## $prevrawline =~ /^.\s*$/) { |
| 4101 | ## WARN("SPACING", |
| 4102 | ## "No blank lines before declarations\n" . $hereprev); |
| 4103 | ## } |
| 4104 | ## |
| 4105 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4106 | # closing brace should have a space following it when it has anything |
| 4107 | # on the line |
| 4108 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4109 | if (ERROR("SPACING", |
| 4110 | "space required after that close brace '}'\n" . $herecurr) && |
| 4111 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4112 | $fixed[$fixlinenr] =~ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4113 | s/}((?!(?:,|;|\)))\S)/} $1/; |
| 4114 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4115 | } |
| 4116 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4117 | # check spacing on square brackets |
| 4118 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4119 | if (ERROR("SPACING", |
| 4120 | "space prohibited after that open square bracket '['\n" . $herecurr) && |
| 4121 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4122 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4123 | s/\[\s+/\[/; |
| 4124 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4125 | } |
| 4126 | if ($line =~ /\s\]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4127 | if (ERROR("SPACING", |
| 4128 | "space prohibited before that close square bracket ']'\n" . $herecurr) && |
| 4129 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4130 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4131 | s/\s+\]/\]/; |
| 4132 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4133 | } |
| 4134 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4135 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4136 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 4137 | $line !~ /for\s*\(\s+;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4138 | if (ERROR("SPACING", |
| 4139 | "space prohibited after that open parenthesis '('\n" . $herecurr) && |
| 4140 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4141 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4142 | s/\(\s+/\(/; |
| 4143 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4144 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4145 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4146 | $line !~ /for\s*\(.*;\s+\)/ && |
| 4147 | $line !~ /:\s+\)/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4148 | if (ERROR("SPACING", |
| 4149 | "space prohibited before that close parenthesis ')'\n" . $herecurr) && |
| 4150 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4151 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4152 | s/\s+\)/\)/; |
| 4153 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 4154 | } |
| 4155 | |
Joe Perches | e2826fd | 2014-08-06 16:10:48 -0700 | [diff] [blame] | 4156 | # check unnecessary parentheses around addressof/dereference single $Lvals |
| 4157 | # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar |
| 4158 | |
| 4159 | while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { |
Joe Perches | ea4acbb | 2014-12-10 15:51:51 -0800 | [diff] [blame] | 4160 | my $var = $1; |
| 4161 | if (CHK("UNNECESSARY_PARENTHESES", |
| 4162 | "Unnecessary parentheses around $var\n" . $herecurr) && |
| 4163 | $fix) { |
| 4164 | $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; |
| 4165 | } |
| 4166 | } |
| 4167 | |
| 4168 | # check for unnecessary parentheses around function pointer uses |
| 4169 | # ie: (foo->bar)(); should be foo->bar(); |
| 4170 | # but not "if (foo->bar) (" to avoid some false positives |
| 4171 | if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { |
| 4172 | my $var = $2; |
| 4173 | if (CHK("UNNECESSARY_PARENTHESES", |
| 4174 | "Unnecessary parentheses around function pointer $var\n" . $herecurr) && |
| 4175 | $fix) { |
| 4176 | my $var2 = deparenthesize($var); |
| 4177 | $var2 =~ s/\s//g; |
| 4178 | $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; |
| 4179 | } |
| 4180 | } |
Joe Perches | e2826fd | 2014-08-06 16:10:48 -0700 | [diff] [blame] | 4181 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4182 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4183 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4184 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4185 | if (WARN("INDENTED_LABEL", |
| 4186 | "labels should not be indented\n" . $herecurr) && |
| 4187 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4188 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4189 | s/^(.)\s+/$1/; |
| 4190 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4191 | } |
| 4192 | |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 4193 | # return is not a function |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 4194 | if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4195 | my $spacing = $1; |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 4196 | if ($^V && $^V ge 5.10.0 && |
Joe Perches | 5b9553a | 2014-04-03 14:49:21 -0700 | [diff] [blame] | 4197 | $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { |
| 4198 | my $value = $1; |
| 4199 | $value = deparenthesize($value); |
| 4200 | if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { |
| 4201 | ERROR("RETURN_PARENTHESES", |
| 4202 | "return is not a function, parentheses are not required\n" . $herecurr); |
| 4203 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4204 | } elsif ($spacing !~ /\s+/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4205 | ERROR("SPACING", |
| 4206 | "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4207 | } |
| 4208 | } |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 4209 | |
Joe Perches | b43ae21 | 2014-06-23 13:22:07 -0700 | [diff] [blame] | 4210 | # unnecessary return in a void function |
| 4211 | # at end-of-function, with the previous line a single leading tab, then return; |
| 4212 | # and the line before that not a goto label target like "out:" |
| 4213 | if ($sline =~ /^[ \+]}\s*$/ && |
| 4214 | $prevline =~ /^\+\treturn\s*;\s*$/ && |
| 4215 | $linenr >= 3 && |
| 4216 | $lines[$linenr - 3] =~ /^[ +]/ && |
| 4217 | $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { |
Joe Perches | 9819cf2 | 2014-06-04 16:12:09 -0700 | [diff] [blame] | 4218 | WARN("RETURN_VOID", |
Joe Perches | b43ae21 | 2014-06-23 13:22:07 -0700 | [diff] [blame] | 4219 | "void function return statements are not generally useful\n" . $hereprev); |
| 4220 | } |
Joe Perches | 9819cf2 | 2014-06-04 16:12:09 -0700 | [diff] [blame] | 4221 | |
Joe Perches | 189248d | 2014-01-23 15:54:47 -0800 | [diff] [blame] | 4222 | # if statements using unnecessary parentheses - ie: if ((foo == bar)) |
| 4223 | if ($^V && $^V ge 5.10.0 && |
| 4224 | $line =~ /\bif\s*((?:\(\s*){2,})/) { |
| 4225 | my $openparens = $1; |
| 4226 | my $count = $openparens =~ tr@\(@\(@; |
| 4227 | my $msg = ""; |
| 4228 | if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { |
| 4229 | my $comp = $4; #Not $1 because of $LvalOrFunc |
| 4230 | $msg = " - maybe == should be = ?" if ($comp eq "=="); |
| 4231 | WARN("UNNECESSARY_PARENTHESES", |
| 4232 | "Unnecessary parentheses$msg\n" . $herecurr); |
| 4233 | } |
| 4234 | } |
| 4235 | |
Joe Perches | c5595fa | 2015-09-09 15:37:58 -0700 | [diff] [blame] | 4236 | # comparisons with a constant or upper case identifier on the left |
| 4237 | # avoid cases like "foo + BAR < baz" |
| 4238 | # only fix matches surrounded by parentheses to avoid incorrect |
| 4239 | # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" |
| 4240 | if ($^V && $^V ge 5.10.0 && |
| 4241 | $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { |
| 4242 | my $lead = $1; |
| 4243 | my $const = $2; |
| 4244 | my $comp = $3; |
| 4245 | my $to = $4; |
| 4246 | my $newcomp = $comp; |
| 4247 | if ($lead !~ /$Operators\s*$/ && |
| 4248 | $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && |
| 4249 | WARN("CONSTANT_COMPARISON", |
| 4250 | "Comparisons should place the constant on the right side of the test\n" . $herecurr) && |
| 4251 | $fix) { |
| 4252 | if ($comp eq "<") { |
| 4253 | $newcomp = ">"; |
| 4254 | } elsif ($comp eq "<=") { |
| 4255 | $newcomp = ">="; |
| 4256 | } elsif ($comp eq ">") { |
| 4257 | $newcomp = "<"; |
| 4258 | } elsif ($comp eq ">=") { |
| 4259 | $newcomp = "<="; |
| 4260 | } |
| 4261 | $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; |
| 4262 | } |
| 4263 | } |
| 4264 | |
Joe Perches | f34e4a4 | 2015-04-16 12:44:19 -0700 | [diff] [blame] | 4265 | # Return of what appears to be an errno should normally be negative |
| 4266 | if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 4267 | my $name = $1; |
| 4268 | if ($name ne 'EOF' && $name ne 'ERROR') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4269 | WARN("USE_NEGATIVE_ERRNO", |
Joe Perches | f34e4a4 | 2015-04-16 12:44:19 -0700 | [diff] [blame] | 4270 | "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] | 4271 | } |
| 4272 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4273 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4274 | # Need a space before open parenthesis after if, while etc |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4275 | if ($line =~ /\b(if|while|for|switch)\(/) { |
| 4276 | if (ERROR("SPACING", |
| 4277 | "space required before the open parenthesis '('\n" . $herecurr) && |
| 4278 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4279 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4280 | s/\b(if|while|for|switch)\(/$1 \(/; |
| 4281 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4282 | } |
| 4283 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 4284 | # Check for illegal assignment in if conditional -- and check for trailing |
| 4285 | # statements after the conditional. |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 4286 | if ($line =~ /do\s*(?!{)/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 4287 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 4288 | ctx_statement_block($linenr, $realcnt, 0) |
| 4289 | if (!defined $stat); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 4290 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 4291 | $remain_next, $off_next); |
| 4292 | $stat_next =~ s/\n./\n /g; |
| 4293 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 4294 | |
| 4295 | if ($stat_next =~ /^\s*while\b/) { |
| 4296 | # If the statement carries leading newlines, |
| 4297 | # then count those as offsets. |
| 4298 | my ($whitespace) = |
| 4299 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 4300 | my $offset = |
| 4301 | statement_rawlines($whitespace) - 1; |
| 4302 | |
| 4303 | $suppress_whiletrailers{$line_nr_next + |
| 4304 | $offset} = 1; |
| 4305 | } |
| 4306 | } |
| 4307 | if (!defined $suppress_whiletrailers{$linenr} && |
Joe Perches | c11230f | 2013-11-21 14:31:57 -0800 | [diff] [blame] | 4308 | defined($stat) && defined($cond) && |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 4309 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4310 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4311 | |
Andy Whitcroft | b53c8e1 | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 4312 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4313 | ERROR("ASSIGN_IN_IF", |
| 4314 | "do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | # Find out what is on the end of the line after the |
| 4318 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4319 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4320 | $s =~ s/\n.*//g; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4321 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 4322 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 4323 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4324 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 4325 | # Find out how long the conditional actually is. |
| 4326 | my @newlines = ($c =~ /\n/gs); |
| 4327 | my $cond_lines = 1 + $#newlines; |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 4328 | my $stat_real = ''; |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 4329 | |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 4330 | $stat_real = raw_line($linenr, $cond_lines) |
| 4331 | . "\n" if ($cond_lines); |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 4332 | if (defined($stat_real) && $cond_lines > 1) { |
| 4333 | $stat_real = "[...]\n$stat_real"; |
| 4334 | } |
| 4335 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4336 | ERROR("TRAILING_STATEMENTS", |
| 4337 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4338 | } |
| 4339 | } |
| 4340 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4341 | # Check for bitwise tests written as boolean |
| 4342 | if ($line =~ / |
| 4343 | (?: |
| 4344 | (?:\[|\(|\&\&|\|\|) |
| 4345 | \s*0[xX][0-9]+\s* |
| 4346 | (?:\&\&|\|\|) |
| 4347 | | |
| 4348 | (?:\&\&|\|\|) |
| 4349 | \s*0[xX][0-9]+\s* |
| 4350 | (?:\&\&|\|\||\)|\]) |
| 4351 | )/x) |
| 4352 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4353 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 4354 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4355 | } |
| 4356 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4357 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4358 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 4359 | my $s = $1; |
| 4360 | $s =~ s/$;//g; # Remove any comments |
| 4361 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4362 | ERROR("TRAILING_STATEMENTS", |
| 4363 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4364 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4365 | } |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 4366 | # if should not continue a brace |
| 4367 | if ($line =~ /}\s*if\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4368 | ERROR("TRAILING_STATEMENTS", |
Rasmus Villemoes | 048b123 | 2014-08-06 16:10:37 -0700 | [diff] [blame] | 4369 | "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] | 4370 | $herecurr); |
| 4371 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 4372 | # case and default should not have general statements after them |
| 4373 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 4374 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 4375 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 4376 | \s*return\s+ |
| 4377 | )/xg) |
| 4378 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4379 | ERROR("TRAILING_STATEMENTS", |
| 4380 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 4381 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4382 | |
| 4383 | # Check for }<nl>else {, these must be at the same |
| 4384 | # indent level to be relevant to each other. |
Joe Perches | 8b8856f | 2014-08-06 16:11:14 -0700 | [diff] [blame] | 4385 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && |
| 4386 | $previndent == $indent) { |
| 4387 | if (ERROR("ELSE_AFTER_BRACE", |
| 4388 | "else should follow close brace '}'\n" . $hereprev) && |
| 4389 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4390 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4391 | fix_delete_line($fixlinenr, $rawline); |
| 4392 | my $fixedline = $prevrawline; |
| 4393 | $fixedline =~ s/}\s*$//; |
| 4394 | if ($fixedline !~ /^\+\s*$/) { |
| 4395 | fix_insert_line($fixlinenr, $fixedline); |
| 4396 | } |
| 4397 | $fixedline = $rawline; |
| 4398 | $fixedline =~ s/^(.\s*)else/$1} else/; |
| 4399 | fix_insert_line($fixlinenr, $fixedline); |
| 4400 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4401 | } |
| 4402 | |
Joe Perches | 8b8856f | 2014-08-06 16:11:14 -0700 | [diff] [blame] | 4403 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && |
| 4404 | $previndent == $indent) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4405 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 4406 | |
| 4407 | # Find out what is on the end of the line after the |
| 4408 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4409 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4410 | $s =~ s/\n.*//g; |
| 4411 | |
| 4412 | if ($s =~ /^\s*;/) { |
Joe Perches | 8b8856f | 2014-08-06 16:11:14 -0700 | [diff] [blame] | 4413 | if (ERROR("WHILE_AFTER_BRACE", |
| 4414 | "while should follow close brace '}'\n" . $hereprev) && |
| 4415 | $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { |
| 4416 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4417 | fix_delete_line($fixlinenr, $rawline); |
| 4418 | my $fixedline = $prevrawline; |
| 4419 | my $trailing = $rawline; |
| 4420 | $trailing =~ s/^\+//; |
| 4421 | $trailing = trim($trailing); |
| 4422 | $fixedline =~ s/}\s*$/} $trailing/; |
| 4423 | fix_insert_line($fixlinenr, $fixedline); |
| 4424 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4425 | } |
| 4426 | } |
| 4427 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 4428 | #Specific variable tests |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 4429 | while ($line =~ m{($Constant|$Lval)}g) { |
| 4430 | my $var = $1; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 4431 | |
| 4432 | #gcc binary extension |
| 4433 | if ($var =~ /^$Binary$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4434 | if (WARN("GCC_BINARY_CONSTANT", |
| 4435 | "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && |
| 4436 | $fix) { |
| 4437 | my $hexval = sprintf("0x%x", oct($var)); |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4438 | $fixed[$fixlinenr] =~ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4439 | s/\b$var\b/$hexval/; |
| 4440 | } |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 4441 | } |
| 4442 | |
| 4443 | #CamelCase |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 4444 | if ($var !~ /^$Constant$/ && |
Joe Perches | be79794 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 4445 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 4446 | #Ignore Page<foo> variants |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 4447 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 4448 | #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] | 4449 | $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && |
| 4450 | #Ignore some three character SI units explicitly, like MiB and KHz |
| 4451 | $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 4452 | while ($var =~ m{($Ident)}g) { |
| 4453 | my $word = $1; |
| 4454 | next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 4455 | if ($check) { |
| 4456 | seed_camelcase_includes(); |
| 4457 | if (!$file && !$camelcase_file_seeded) { |
| 4458 | seed_camelcase_file($realfile); |
| 4459 | $camelcase_file_seeded = 1; |
| 4460 | } |
| 4461 | } |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 4462 | if (!defined $camelcase{$word}) { |
| 4463 | $camelcase{$word} = 1; |
| 4464 | CHK("CAMELCASE", |
| 4465 | "Avoid CamelCase: <$word>\n" . $herecurr); |
| 4466 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 4467 | } |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 4468 | } |
| 4469 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4470 | |
| 4471 | #no spaces allowed after \ in define |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4472 | if ($line =~ /\#\s*define.*\\\s+$/) { |
| 4473 | if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 4474 | "Whitespace after \\ makes next lines useless\n" . $herecurr) && |
| 4475 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 4476 | $fixed[$fixlinenr] =~ s/\s+$//; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4477 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4478 | } |
| 4479 | |
Fabian Frederick | 0e212e0 | 2015-04-16 12:44:25 -0700 | [diff] [blame] | 4480 | # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes |
| 4481 | # itself <asm/foo.h> (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4482 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 4483 | my $file = "$1.h"; |
| 4484 | my $checkfile = "include/linux/$file"; |
| 4485 | if (-f "$root/$checkfile" && |
| 4486 | $realfile ne $checkfile && |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 4487 | $1 !~ /$allowed_asm_includes/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4488 | { |
Fabian Frederick | 0e212e0 | 2015-04-16 12:44:25 -0700 | [diff] [blame] | 4489 | my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; |
| 4490 | if ($asminclude > 0) { |
| 4491 | if ($realfile =~ m{^arch/}) { |
| 4492 | CHK("ARCH_INCLUDE_LINUX", |
| 4493 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 4494 | } else { |
| 4495 | WARN("INCLUDE_LINUX", |
| 4496 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 4497 | } |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 4498 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4499 | } |
| 4500 | } |
| 4501 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4502 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 4503 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4504 | # in a known good container |
Andy Whitcroft | b8f96a3 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 4505 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 4506 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4507 | my $ln = $linenr; |
| 4508 | my $cnt = $realcnt; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4509 | my ($off, $dstat, $dcond, $rest); |
| 4510 | my $ctx = ''; |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 4511 | my $has_flow_statement = 0; |
| 4512 | my $has_arg_concat = 0; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4513 | ($dstat, $dcond, $ln, $cnt, $off) = |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4514 | ctx_statement_block($linenr, $realcnt, 0); |
| 4515 | $ctx = $dstat; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4516 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 4517 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4518 | |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 4519 | $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); |
| 4520 | $has_arg_concat = 1 if ($ctx =~ /\#\#/); |
| 4521 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4522 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 4523 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4524 | $dstat =~ s/\\\n.//g; |
| 4525 | $dstat =~ s/^\s*//s; |
| 4526 | $dstat =~ s/\s*$//s; |
| 4527 | |
| 4528 | # Flatten any parentheses and braces |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 4529 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 4530 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
Andy Whitcroft | c81769f | 2012-01-10 15:10:10 -0800 | [diff] [blame] | 4531 | $dstat =~ s/\[[^\[\]]*\]/1/) |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 4532 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4533 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4534 | |
Andy Whitcroft | e45bab8 | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 4535 | # Flatten any obvious string concatentation. |
Joe Perches | 33acb54 | 2015-06-25 15:02:54 -0700 | [diff] [blame] | 4536 | while ($dstat =~ s/($String)\s*$Ident/$1/ || |
| 4537 | $dstat =~ s/$Ident\s*($String)/$1/) |
Andy Whitcroft | e45bab8 | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 4538 | { |
| 4539 | } |
| 4540 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4541 | my $exceptions = qr{ |
| 4542 | $Declare| |
| 4543 | module_param_named| |
Kees Cook | a0a0a7a | 2012-10-04 17:13:38 -0700 | [diff] [blame] | 4544 | MODULE_PARM_DESC| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4545 | DECLARE_PER_CPU| |
| 4546 | DEFINE_PER_CPU| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 4547 | __typeof__\(| |
Stefani Seibold | 22fd2d3 | 2010-03-05 13:43:52 -0800 | [diff] [blame] | 4548 | union| |
| 4549 | struct| |
Andy Whitcroft | ea71a0a | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 4550 | \.$Ident\s*=\s*| |
| 4551 | ^\"|\"$ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4552 | }x; |
Andy Whitcroft | 5eaa20b | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 4553 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4554 | if ($dstat ne '' && |
| 4555 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 4556 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Joe Perches | 3cc4b1c | 2013-07-03 15:05:27 -0700 | [diff] [blame] | 4557 | $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] | 4558 | $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4559 | $dstat !~ /$exceptions/ && |
| 4560 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Joe Perches | e942e2c | 2013-04-17 15:58:26 -0700 | [diff] [blame] | 4561 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
Andy Whitcroft | 72f115f | 2012-01-10 15:10:06 -0800 | [diff] [blame] | 4562 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4563 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 4564 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 4565 | $dstat !~ /^do\s*{/ && # do {... |
Eddie Kovsky | 4e5d56b | 2015-09-09 15:37:52 -0700 | [diff] [blame] | 4566 | $dstat !~ /^\(\{/ && # ({... |
Joe Perches | f95a7e6 | 2013-09-11 14:24:00 -0700 | [diff] [blame] | 4567 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4568 | { |
| 4569 | $ctx =~ s/\n*$//; |
| 4570 | my $herectx = $here . "\n"; |
| 4571 | my $cnt = statement_rawlines($ctx); |
| 4572 | |
| 4573 | for (my $n = 0; $n < $cnt; $n++) { |
| 4574 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4575 | } |
| 4576 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 4577 | if ($dstat =~ /;/) { |
| 4578 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 4579 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 4580 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4581 | ERROR("COMPLEX_MACRO", |
Andrew Morton | 388982b | 2014-10-13 15:51:40 -0700 | [diff] [blame] | 4582 | "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 4583 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4584 | } |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 4585 | |
Joe Perches | 08a2843 | 2014-10-13 15:51:55 -0700 | [diff] [blame] | 4586 | # check for macros with flow control, but without ## concatenation |
| 4587 | # ## concatenation is commonly a macro that defines a function so ignore those |
| 4588 | if ($has_flow_statement && !$has_arg_concat) { |
| 4589 | my $herectx = $here . "\n"; |
| 4590 | my $cnt = statement_rawlines($ctx); |
| 4591 | |
| 4592 | for (my $n = 0; $n < $cnt; $n++) { |
| 4593 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 4594 | } |
| 4595 | WARN("MACRO_WITH_FLOW_CONTROL", |
| 4596 | "Macros with flow control statements should be avoided\n" . "$herectx"); |
| 4597 | } |
| 4598 | |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 4599 | # check for line continuations outside of #defines, preprocessor #, and asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 4600 | |
| 4601 | } else { |
| 4602 | if ($prevline !~ /^..*\\$/ && |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 4603 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 4604 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 4605 | $line =~ /^\+.*\\$/) { |
| 4606 | WARN("LINE_CONTINUATIONS", |
| 4607 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 4608 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4609 | } |
| 4610 | |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 4611 | # do {} while (0) macro tests: |
| 4612 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 4613 | # macro should not end with a semicolon |
| 4614 | if ($^V && $^V ge 5.10.0 && |
| 4615 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 4616 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 4617 | my $ln = $linenr; |
| 4618 | my $cnt = $realcnt; |
| 4619 | my ($off, $dstat, $dcond, $rest); |
| 4620 | my $ctx = ''; |
| 4621 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 4622 | ctx_statement_block($linenr, $realcnt, 0); |
| 4623 | $ctx = $dstat; |
| 4624 | |
| 4625 | $dstat =~ s/\\\n.//g; |
Joe Perches | 1b36b20 | 2015-02-13 14:38:32 -0800 | [diff] [blame] | 4626 | $dstat =~ s/$;/ /g; |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 4627 | |
| 4628 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 4629 | my $stmts = $2; |
| 4630 | my $semis = $3; |
| 4631 | |
| 4632 | $ctx =~ s/\n*$//; |
| 4633 | my $cnt = statement_rawlines($ctx); |
| 4634 | my $herectx = $here . "\n"; |
| 4635 | |
| 4636 | for (my $n = 0; $n < $cnt; $n++) { |
| 4637 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 4638 | } |
| 4639 | |
Joe Perches | ac8e97f | 2012-08-21 16:15:53 -0700 | [diff] [blame] | 4640 | if (($stmts =~ tr/;/;/) == 1 && |
| 4641 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 4642 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 4643 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 4644 | } |
| 4645 | if (defined $semis && $semis ne "") { |
| 4646 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 4647 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
| 4648 | } |
Joe Perches | f5ef95b | 2014-06-04 16:12:06 -0700 | [diff] [blame] | 4649 | } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { |
| 4650 | $ctx =~ s/\n*$//; |
| 4651 | my $cnt = statement_rawlines($ctx); |
| 4652 | my $herectx = $here . "\n"; |
| 4653 | |
| 4654 | for (my $n = 0; $n < $cnt; $n++) { |
| 4655 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 4656 | } |
| 4657 | |
| 4658 | WARN("TRAILING_SEMICOLON", |
| 4659 | "macros should not use a trailing semicolon\n" . "$herectx"); |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 4660 | } |
| 4661 | } |
| 4662 | |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 4663 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 4664 | # all assignments may have only one of the following with an assignment: |
| 4665 | # . |
| 4666 | # ALIGN(...) |
| 4667 | # VMLINUX_SYMBOL(...) |
| 4668 | 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] | 4669 | WARN("MISSING_VMLINUX_SYMBOL", |
| 4670 | "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 4671 | } |
| 4672 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4673 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4674 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 4675 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4676 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4677 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4678 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 4679 | if ($#chunks > 0 && $level == 0) { |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4680 | my @allowed = (); |
| 4681 | my $allow = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4682 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4683 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4684 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4685 | for my $chunk (@chunks) { |
| 4686 | my ($cond, $block) = @{$chunk}; |
| 4687 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4688 | # If the condition carries leading newlines, then count those as offsets. |
| 4689 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 4690 | my $offset = statement_rawlines($whitespace) - 1; |
| 4691 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4692 | $allowed[$allow] = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4693 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 4694 | |
| 4695 | # We have looked at and allowed this specific line. |
| 4696 | $suppress_ifbraces{$ln + $offset} = 1; |
| 4697 | |
| 4698 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4699 | $ln += statement_rawlines($block) - 1; |
| 4700 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4701 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4702 | |
| 4703 | $seen++ if ($block =~ /^\s*{/); |
| 4704 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4705 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4706 | if (statement_lines($cond) > 1) { |
| 4707 | #print "APW: ALLOWED: cond<$cond>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4708 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4709 | } |
| 4710 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4711 | #print "APW: ALLOWED: block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4712 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4713 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4714 | if (statement_block_size($block) > 1) { |
| 4715 | #print "APW: ALLOWED: lines block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4716 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4717 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4718 | $allow++; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4719 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 4720 | if ($seen) { |
| 4721 | my $sum_allowed = 0; |
| 4722 | foreach (@allowed) { |
| 4723 | $sum_allowed += $_; |
| 4724 | } |
| 4725 | if ($sum_allowed == 0) { |
| 4726 | WARN("BRACES", |
| 4727 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 4728 | } elsif ($sum_allowed != $allow && |
| 4729 | $seen != $allow) { |
| 4730 | CHK("BRACES", |
| 4731 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 4732 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4733 | } |
| 4734 | } |
| 4735 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4736 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4737 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4738 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4739 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4740 | # Check the pre-context. |
| 4741 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 4742 | #print "APW: ALLOWED: pre<$1>\n"; |
| 4743 | $allowed = 1; |
| 4744 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4745 | |
| 4746 | my ($level, $endln, @chunks) = |
| 4747 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 4748 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4749 | # Check the condition. |
| 4750 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4751 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4752 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4753 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4754 | } |
| 4755 | if (statement_lines($cond) > 1) { |
| 4756 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 4757 | $allowed = 1; |
| 4758 | } |
| 4759 | if ($block =~/\b(?:if|for|while)\b/) { |
| 4760 | #print "APW: ALLOWED: block<$block>\n"; |
| 4761 | $allowed = 1; |
| 4762 | } |
| 4763 | if (statement_block_size($block) > 1) { |
| 4764 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 4765 | $allowed = 1; |
| 4766 | } |
| 4767 | # Check the post-context. |
| 4768 | if (defined $chunks[1]) { |
| 4769 | my ($cond, $block) = @{$chunks[1]}; |
| 4770 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4771 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4772 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4773 | if ($block =~ /^\s*\{/) { |
| 4774 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 4775 | $allowed = 1; |
| 4776 | } |
| 4777 | } |
| 4778 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 4779 | my $herectx = $here . "\n"; |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 4780 | my $cnt = statement_rawlines($block); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4781 | |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 4782 | for (my $n = 0; $n < $cnt; $n++) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 4783 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 4784 | } |
| 4785 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4786 | WARN("BRACES", |
| 4787 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4788 | } |
| 4789 | } |
| 4790 | |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 4791 | # check for unnecessary blank lines around braces |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 4792 | if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { |
Joe Perches | f8e5821 | 2015-02-13 14:38:46 -0800 | [diff] [blame] | 4793 | if (CHK("BRACES", |
| 4794 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && |
| 4795 | $fix && $prevrawline =~ /^\+/) { |
| 4796 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4797 | } |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 4798 | } |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 4799 | if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
Joe Perches | f8e5821 | 2015-02-13 14:38:46 -0800 | [diff] [blame] | 4800 | if (CHK("BRACES", |
| 4801 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && |
| 4802 | $fix) { |
| 4803 | fix_delete_line($fixlinenr, $rawline); |
| 4804 | } |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 4805 | } |
| 4806 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4807 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 4808 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 4809 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4810 | WARN("VOLATILE", |
| 4811 | "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] | 4812 | } |
| 4813 | |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 4814 | # Check for user-visible strings broken across lines, which breaks the ability |
| 4815 | # to grep for the string. Make exceptions when the previous string ends in a |
| 4816 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' |
| 4817 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value |
Joe Perches | 33acb54 | 2015-06-25 15:02:54 -0700 | [diff] [blame] | 4818 | if ($line =~ /^\+\s*$String/ && |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 4819 | $prevline =~ /"\s*$/ && |
| 4820 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { |
| 4821 | if (WARN("SPLIT_STRING", |
| 4822 | "quoted string split across lines\n" . $hereprev) && |
| 4823 | $fix && |
| 4824 | $prevrawline =~ /^\+.*"\s*$/ && |
| 4825 | $last_coalesced_string_linenr != $linenr - 1) { |
| 4826 | my $extracted_string = get_quoted_string($line, $rawline); |
| 4827 | my $comma_close = ""; |
| 4828 | if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { |
| 4829 | $comma_close = $1; |
| 4830 | } |
| 4831 | |
| 4832 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4833 | fix_delete_line($fixlinenr, $rawline); |
| 4834 | my $fixedline = $prevrawline; |
| 4835 | $fixedline =~ s/"\s*$//; |
| 4836 | $fixedline .= substr($extracted_string, 1) . trim($comma_close); |
| 4837 | fix_insert_line($fixlinenr - 1, $fixedline); |
| 4838 | $fixedline = $rawline; |
| 4839 | $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; |
| 4840 | if ($fixedline !~ /\+\s*$/) { |
| 4841 | fix_insert_line($fixlinenr, $fixedline); |
| 4842 | } |
| 4843 | $last_coalesced_string_linenr = $linenr; |
| 4844 | } |
| 4845 | } |
| 4846 | |
| 4847 | # check for missing a space in a string concatenation |
| 4848 | if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { |
| 4849 | WARN('MISSING_SPACE', |
| 4850 | "break quoted strings at a space character\n" . $hereprev); |
| 4851 | } |
| 4852 | |
| 4853 | # check for spaces before a quoted newline |
| 4854 | if ($rawline =~ /^.*\".*\s\\n/) { |
| 4855 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 4856 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && |
| 4857 | $fix) { |
| 4858 | $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; |
| 4859 | } |
| 4860 | |
| 4861 | } |
| 4862 | |
Joe Perches | f17dba4 | 2014-10-13 15:51:51 -0700 | [diff] [blame] | 4863 | # concatenated string without spaces between elements |
Joe Perches | 33acb54 | 2015-06-25 15:02:54 -0700 | [diff] [blame] | 4864 | if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) { |
Joe Perches | f17dba4 | 2014-10-13 15:51:51 -0700 | [diff] [blame] | 4865 | CHK("CONCATENATED_STRING", |
| 4866 | "Concatenated strings should use spaces between elements\n" . $herecurr); |
| 4867 | } |
| 4868 | |
Joe Perches | 90ad30e | 2014-12-10 15:51:59 -0800 | [diff] [blame] | 4869 | # uncoalesced string fragments |
Joe Perches | 33acb54 | 2015-06-25 15:02:54 -0700 | [diff] [blame] | 4870 | if ($line =~ /$String\s*"/) { |
Joe Perches | 90ad30e | 2014-12-10 15:51:59 -0800 | [diff] [blame] | 4871 | WARN("STRING_FRAGMENTS", |
| 4872 | "Consecutive strings are generally better as a single string\n" . $herecurr); |
| 4873 | } |
| 4874 | |
Joe Perches | 6e30075 | 2015-09-09 15:37:47 -0700 | [diff] [blame] | 4875 | # check for %L{u,d,i} and 0x%[udi] in strings |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 4876 | my $string; |
| 4877 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 4878 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
| 4879 | $string =~ s/%%/__/g; |
Joe Perches | 6e30075 | 2015-09-09 15:37:47 -0700 | [diff] [blame] | 4880 | if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) { |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 4881 | WARN("PRINTF_L", |
| 4882 | "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
| 4883 | last; |
| 4884 | } |
Joe Perches | 6e30075 | 2015-09-09 15:37:47 -0700 | [diff] [blame] | 4885 | if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) { |
| 4886 | ERROR("PRINTF_0xDECIMAL", |
| 4887 | "Prefixing 0x with decimal output is defective\n" . $herecurr); |
| 4888 | } |
Joe Perches | 5e4f6ba | 2014-12-10 15:52:05 -0800 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | # check for line continuations in quoted strings with odd counts of " |
| 4892 | if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { |
| 4893 | WARN("LINE_CONTINUATIONS", |
| 4894 | "Avoid line continuations in quoted strings\n" . $herecurr); |
| 4895 | } |
| 4896 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 4897 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4898 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4899 | CHK("REDUNDANT_CODE", |
| 4900 | "if this code is redundant consider removing it\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4901 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 4902 | } |
| 4903 | |
Andy Whitcroft | 03df4b5 | 2012-12-17 16:01:52 -0800 | [diff] [blame] | 4904 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 4905 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
Joe Perches | 100425d | 2015-09-09 15:37:36 -0700 | [diff] [blame] | 4906 | my $tested = quotemeta($1); |
| 4907 | my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; |
| 4908 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { |
| 4909 | my $func = $1; |
| 4910 | if (WARN('NEEDLESS_IF', |
| 4911 | "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && |
| 4912 | $fix) { |
| 4913 | my $do_fix = 1; |
| 4914 | my $leading_tabs = ""; |
| 4915 | my $new_leading_tabs = ""; |
| 4916 | if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { |
| 4917 | $leading_tabs = $1; |
| 4918 | } else { |
| 4919 | $do_fix = 0; |
| 4920 | } |
| 4921 | if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { |
| 4922 | $new_leading_tabs = $1; |
| 4923 | if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { |
| 4924 | $do_fix = 0; |
| 4925 | } |
| 4926 | } else { |
| 4927 | $do_fix = 0; |
| 4928 | } |
| 4929 | if ($do_fix) { |
| 4930 | fix_delete_line($fixlinenr - 1, $prevrawline); |
| 4931 | $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; |
| 4932 | } |
| 4933 | } |
Greg Kroah-Hartman | 4c432a8 | 2008-07-23 21:29:04 -0700 | [diff] [blame] | 4934 | } |
| 4935 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4936 | |
Joe Perches | ebfdc40 | 2014-08-06 16:10:27 -0700 | [diff] [blame] | 4937 | # check for unnecessary "Out of Memory" messages |
| 4938 | if ($line =~ /^\+.*\b$logFunctions\s*\(/ && |
| 4939 | $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && |
| 4940 | (defined $1 || defined $3) && |
| 4941 | $linenr > 3) { |
| 4942 | my $testval = $2; |
| 4943 | my $testline = $lines[$linenr - 3]; |
| 4944 | |
| 4945 | my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); |
| 4946 | # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); |
| 4947 | |
| 4948 | if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) { |
| 4949 | WARN("OOM_MESSAGE", |
| 4950 | "Possible unnecessary 'out of memory' message\n" . $hereprev); |
| 4951 | } |
| 4952 | } |
| 4953 | |
Joe Perches | f78d98f | 2014-10-13 15:52:01 -0700 | [diff] [blame] | 4954 | # check for logging functions with KERN_<LEVEL> |
Paolo Bonzini | dcaf112 | 2015-02-13 14:38:26 -0800 | [diff] [blame] | 4955 | if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && |
Joe Perches | f78d98f | 2014-10-13 15:52:01 -0700 | [diff] [blame] | 4956 | $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { |
| 4957 | my $level = $1; |
| 4958 | if (WARN("UNNECESSARY_KERN_LEVEL", |
| 4959 | "Possible unnecessary $level\n" . $herecurr) && |
| 4960 | $fix) { |
| 4961 | $fixed[$fixlinenr] =~ s/\s*$level\s*//; |
| 4962 | } |
| 4963 | } |
| 4964 | |
Joe Perches | abb08a5 | 2014-12-10 15:51:46 -0800 | [diff] [blame] | 4965 | # check for mask then right shift without a parentheses |
| 4966 | if ($^V && $^V ge 5.10.0 && |
| 4967 | $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && |
| 4968 | $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so |
| 4969 | WARN("MASK_THEN_SHIFT", |
| 4970 | "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); |
| 4971 | } |
| 4972 | |
Joe Perches | b75ac61 | 2014-12-10 15:52:02 -0800 | [diff] [blame] | 4973 | # check for pointer comparisons to NULL |
| 4974 | if ($^V && $^V ge 5.10.0) { |
| 4975 | while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { |
| 4976 | my $val = $1; |
| 4977 | my $equal = "!"; |
| 4978 | $equal = "" if ($4 eq "!="); |
| 4979 | if (CHK("COMPARISON_TO_NULL", |
| 4980 | "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && |
| 4981 | $fix) { |
| 4982 | $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; |
| 4983 | } |
| 4984 | } |
| 4985 | } |
| 4986 | |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 4987 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
| 4988 | if ($line =~ /(\b$InitAttribute\b)/) { |
| 4989 | my $attr = $1; |
| 4990 | if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { |
| 4991 | my $ptr = $1; |
| 4992 | my $var = $2; |
| 4993 | if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && |
| 4994 | ERROR("MISPLACED_INIT", |
| 4995 | "$attr should be placed after $var\n" . $herecurr)) || |
| 4996 | ($ptr !~ /\b(union|struct)\s+$attr\b/ && |
| 4997 | WARN("MISPLACED_INIT", |
| 4998 | "$attr should be placed after $var\n" . $herecurr))) && |
| 4999 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5000 | $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] | 5001 | } |
| 5002 | } |
| 5003 | } |
| 5004 | |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 5005 | # check for $InitAttributeData (ie: __initdata) with const |
| 5006 | if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { |
| 5007 | my $attr = $1; |
| 5008 | $attr =~ /($InitAttributePrefix)(.*)/; |
| 5009 | my $attr_prefix = $1; |
| 5010 | my $attr_type = $2; |
| 5011 | if (ERROR("INIT_ATTRIBUTE", |
| 5012 | "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && |
| 5013 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5014 | $fixed[$fixlinenr] =~ |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 5015 | s/$InitAttributeData/${attr_prefix}initconst/; |
| 5016 | } |
| 5017 | } |
| 5018 | |
| 5019 | # check for $InitAttributeConst (ie: __initconst) without const |
| 5020 | if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { |
| 5021 | my $attr = $1; |
| 5022 | if (ERROR("INIT_ATTRIBUTE", |
| 5023 | "Use of $attr requires a separate use of const\n" . $herecurr) && |
| 5024 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5025 | my $lead = $fixed[$fixlinenr] =~ |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 5026 | /(^\+\s*(?:static\s+))/; |
| 5027 | $lead = rtrim($1); |
| 5028 | $lead = "$lead " if ($lead !~ /^\+$/); |
| 5029 | $lead = "${lead}const "; |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5030 | $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; |
Joe Perches | e970b884 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 5031 | } |
| 5032 | } |
| 5033 | |
Joe Perches | c17893c | 2015-04-16 12:44:42 -0700 | [diff] [blame] | 5034 | # check for __read_mostly with const non-pointer (should just be const) |
| 5035 | if ($line =~ /\b__read_mostly\b/ && |
| 5036 | $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { |
| 5037 | if (ERROR("CONST_READ_MOSTLY", |
| 5038 | "Invalid use of __read_mostly with const type\n" . $herecurr) && |
| 5039 | $fix) { |
| 5040 | $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; |
| 5041 | } |
| 5042 | } |
| 5043 | |
Joe Perches | fbdb813 | 2014-04-03 14:49:14 -0700 | [diff] [blame] | 5044 | # don't use __constant_<foo> functions outside of include/uapi/ |
| 5045 | if ($realfile !~ m@^include/uapi/@ && |
| 5046 | $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { |
| 5047 | my $constant_func = $1; |
| 5048 | my $func = $constant_func; |
| 5049 | $func =~ s/^__constant_//; |
| 5050 | if (WARN("CONSTANT_CONVERSION", |
| 5051 | "$constant_func should be $func\n" . $herecurr) && |
| 5052 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5053 | $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; |
Joe Perches | fbdb813 | 2014-04-03 14:49:14 -0700 | [diff] [blame] | 5054 | } |
| 5055 | } |
| 5056 | |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 5057 | # prefer usleep_range over udelay |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 5058 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 5059 | my $delay = $1; |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 5060 | # ignore udelay's < 10, however |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 5061 | if (! ($delay < 10) ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5062 | CHK("USLEEP_RANGE", |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 5063 | "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); |
| 5064 | } |
| 5065 | if ($delay > 2000) { |
| 5066 | WARN("LONG_UDELAY", |
| 5067 | "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] | 5068 | } |
| 5069 | } |
| 5070 | |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 5071 | # warn about unexpectedly long msleep's |
| 5072 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 5073 | if ($1 < 20) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5074 | WARN("MSLEEP", |
Joe Perches | 43c1d77 | 2014-04-03 14:49:11 -0700 | [diff] [blame] | 5075 | "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] | 5076 | } |
| 5077 | } |
| 5078 | |
Joe Perches | 36ec193 | 2013-07-03 15:05:25 -0700 | [diff] [blame] | 5079 | # check for comparisons of jiffies |
| 5080 | if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { |
| 5081 | WARN("JIFFIES_COMPARISON", |
| 5082 | "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); |
| 5083 | } |
| 5084 | |
Joe Perches | 9d7a34a | 2013-07-03 15:05:26 -0700 | [diff] [blame] | 5085 | # check for comparisons of get_jiffies_64() |
| 5086 | if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { |
| 5087 | WARN("JIFFIES_COMPARISON", |
| 5088 | "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); |
| 5089 | } |
| 5090 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 5091 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5092 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 5093 | # print "#ifdef in C files should be avoided\n"; |
| 5094 | # print "$herecurr"; |
| 5095 | # $clean = 0; |
| 5096 | # } |
| 5097 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5098 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5099 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5100 | if (ERROR("SPACING", |
| 5101 | "exactly one space required after that #$1\n" . $herecurr) && |
| 5102 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5103 | $fixed[$fixlinenr] =~ |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5104 | s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; |
| 5105 | } |
| 5106 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 5107 | } |
| 5108 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 5109 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5110 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 5111 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 5112 | my $which = $1; |
| 5113 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5114 | CHK("UNCOMMENTED_DEFINITION", |
| 5115 | "$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 5116 | } |
| 5117 | } |
| 5118 | # check for memory barriers without a comment. |
| 5119 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 5120 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | c1fd7bb | 2013-11-12 15:10:11 -0800 | [diff] [blame] | 5121 | WARN("MEMORY_BARRIER", |
| 5122 | "memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 5123 | } |
| 5124 | } |
Paul E. McKenney | 3ad8177 | 2015-07-02 11:55:40 -0700 | [diff] [blame] | 5125 | |
Joe Perches | cb426e9 | 2015-06-25 15:02:46 -0700 | [diff] [blame] | 5126 | # check for waitqueue_active without a comment. |
| 5127 | if ($line =~ /\bwaitqueue_active\s*\(/) { |
| 5128 | if (!ctx_has_comment($first_line, $linenr)) { |
| 5129 | WARN("WAITQUEUE_ACTIVE", |
| 5130 | "waitqueue_active without comment\n" . $herecurr); |
| 5131 | } |
| 5132 | } |
Paul E. McKenney | 3ad8177 | 2015-07-02 11:55:40 -0700 | [diff] [blame] | 5133 | |
| 5134 | # Check for expedited grace periods that interrupt non-idle non-nohz |
| 5135 | # online CPUs. These expedited can therefore degrade real-time response |
| 5136 | # if used carelessly, and should be avoided where not absolutely |
| 5137 | # needed. It is always OK to use synchronize_rcu_expedited() and |
| 5138 | # synchronize_sched_expedited() at boot time (before real-time applications |
| 5139 | # start) and in error situations where real-time response is compromised in |
| 5140 | # any case. Note that synchronize_srcu_expedited() does -not- interrupt |
| 5141 | # other CPUs, so don't warn on uses of synchronize_srcu_expedited(). |
| 5142 | # Of course, nothing comes for free, and srcu_read_lock() and |
| 5143 | # srcu_read_unlock() do contain full memory barriers in payment for |
| 5144 | # synchronize_srcu_expedited() non-interruption properties. |
| 5145 | if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) { |
| 5146 | WARN("EXPEDITED_RCU_GRACE_PERIOD", |
| 5147 | "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr); |
| 5148 | |
| 5149 | } |
| 5150 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 5151 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5152 | 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] | 5153 | CHK("ARCH_DEFINES", |
| 5154 | "architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5155 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 5156 | |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 5157 | # Check that the storage class is at the beginning of a declaration |
| 5158 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5159 | WARN("STORAGE_CLASS", |
| 5160 | "storage class should be at the beginning of the declaration\n" . $herecurr) |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 5161 | } |
| 5162 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5163 | # check the location of the inline attribute, that it is between |
| 5164 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 5165 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 5166 | $line =~ /\b$Inline\s+$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5167 | ERROR("INLINE_LOCATION", |
| 5168 | "inline keyword should sit between storage class and type\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5169 | } |
| 5170 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5171 | # Check for __inline__ and __inline, prefer inline |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 5172 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5173 | $line =~ /\b(__inline__|__inline)\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5174 | if (WARN("INLINE", |
| 5175 | "plain inline is preferred over $1\n" . $herecurr) && |
| 5176 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5177 | $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5178 | |
| 5179 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5180 | } |
| 5181 | |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 5182 | # Check for __attribute__ packed, prefer __packed |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 5183 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5184 | $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5185 | WARN("PREFER_PACKED", |
| 5186 | "__packed is preferred over __attribute__((packed))\n" . $herecurr); |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 5187 | } |
| 5188 | |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 5189 | # Check for __attribute__ aligned, prefer __aligned |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 5190 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5191 | $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5192 | WARN("PREFER_ALIGNED", |
| 5193 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 5194 | } |
| 5195 | |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 5196 | # Check for __attribute__ format(printf, prefer __printf |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 5197 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5198 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5199 | if (WARN("PREFER_PRINTF", |
| 5200 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && |
| 5201 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5202 | $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] | 5203 | |
| 5204 | } |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 5205 | } |
| 5206 | |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5207 | # Check for __attribute__ format(scanf, prefer __scanf |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 5208 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5209 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5210 | if (WARN("PREFER_SCANF", |
| 5211 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && |
| 5212 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5213 | $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] | 5214 | } |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5215 | } |
| 5216 | |
Joe Perches | 619a908 | 2014-12-10 15:51:35 -0800 | [diff] [blame] | 5217 | # Check for __attribute__ weak, or __weak declarations (may have link issues) |
| 5218 | if ($^V && $^V ge 5.10.0 && |
| 5219 | $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && |
| 5220 | ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || |
| 5221 | $line =~ /\b__weak\b/)) { |
| 5222 | ERROR("WEAK_DECLARATION", |
| 5223 | "Using weak declarations can have unintended link defects\n" . $herecurr); |
| 5224 | } |
| 5225 | |
Joe Perches | e6176fa | 2015-06-25 15:02:49 -0700 | [diff] [blame] | 5226 | # check for c99 types like uint8_t used outside of uapi/ |
| 5227 | if ($realfile !~ m@\binclude/uapi/@ && |
| 5228 | $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { |
| 5229 | my $type = $1; |
| 5230 | if ($type =~ /\b($typeC99Typedefs)\b/) { |
| 5231 | $type = $1; |
| 5232 | my $kernel_type = 'u'; |
| 5233 | $kernel_type = 's' if ($type =~ /^_*[si]/); |
| 5234 | $type =~ /(\d+)/; |
| 5235 | $kernel_type .= $1; |
| 5236 | if (CHK("PREFER_KERNEL_TYPES", |
| 5237 | "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && |
| 5238 | $fix) { |
| 5239 | $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; |
| 5240 | } |
| 5241 | } |
| 5242 | } |
| 5243 | |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 5244 | # check for sizeof(&) |
| 5245 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5246 | WARN("SIZEOF_ADDRESS", |
| 5247 | "sizeof(& should be avoided\n" . $herecurr); |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 5248 | } |
| 5249 | |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 5250 | # check for sizeof without parenthesis |
| 5251 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5252 | if (WARN("SIZEOF_PARENTHESIS", |
| 5253 | "sizeof $1 should be sizeof($1)\n" . $herecurr) && |
| 5254 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5255 | $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] | 5256 | } |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 5257 | } |
| 5258 | |
Joe Perches | 88982fe | 2012-12-17 16:02:00 -0800 | [diff] [blame] | 5259 | # check for struct spinlock declarations |
| 5260 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 5261 | WARN("USE_SPINLOCK_T", |
| 5262 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 5263 | } |
| 5264 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 5265 | # check for seq_printf uses that could be seq_puts |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 5266 | if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 5267 | my $fmt = get_quoted_string($line, $rawline); |
Heba Aamer | caac1d5 | 2015-02-13 14:38:49 -0800 | [diff] [blame] | 5268 | $fmt =~ s/%%//g; |
| 5269 | if ($fmt !~ /%/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5270 | if (WARN("PREFER_SEQ_PUTS", |
| 5271 | "Prefer seq_puts to seq_printf\n" . $herecurr) && |
| 5272 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5273 | $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5274 | } |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 5275 | } |
| 5276 | } |
| 5277 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 5278 | # Check for misused memsets |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5279 | if ($^V && $^V ge 5.10.0 && |
| 5280 | defined $stat && |
Mateusz Kulikowski | 9e20a85 | 2015-06-25 15:03:16 -0700 | [diff] [blame] | 5281 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 5282 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5283 | my $ms_addr = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5284 | my $ms_val = $7; |
| 5285 | my $ms_size = $12; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5286 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 5287 | if ($ms_size =~ /^(0x|)0$/i) { |
| 5288 | ERROR("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5289 | "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] | 5290 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 5291 | WARN("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5292 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 5293 | } |
| 5294 | } |
| 5295 | |
Joe Perches | 98a9bba | 2014-01-23 15:54:52 -0800 | [diff] [blame] | 5296 | # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) |
| 5297 | if ($^V && $^V ge 5.10.0 && |
Mateusz Kulikowski | 10895d2 | 2015-06-25 15:03:21 -0700 | [diff] [blame] | 5298 | defined $stat && |
| 5299 | $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
Joe Perches | 98a9bba | 2014-01-23 15:54:52 -0800 | [diff] [blame] | 5300 | if (WARN("PREFER_ETHER_ADDR_COPY", |
Mateusz Kulikowski | 10895d2 | 2015-06-25 15:03:21 -0700 | [diff] [blame] | 5301 | "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && |
Joe Perches | 98a9bba | 2014-01-23 15:54:52 -0800 | [diff] [blame] | 5302 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5303 | $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] | 5304 | } |
| 5305 | } |
| 5306 | |
Mateusz Kulikowski | b6117d1 | 2015-06-25 15:03:13 -0700 | [diff] [blame] | 5307 | # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) |
| 5308 | if ($^V && $^V ge 5.10.0 && |
| 5309 | defined $stat && |
| 5310 | $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 5311 | WARN("PREFER_ETHER_ADDR_EQUAL", |
| 5312 | "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") |
| 5313 | } |
| 5314 | |
Mateusz Kulikowski | 8617cd0 | 2015-06-25 15:03:19 -0700 | [diff] [blame] | 5315 | # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr |
| 5316 | # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr |
| 5317 | if ($^V && $^V ge 5.10.0 && |
| 5318 | defined $stat && |
| 5319 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { |
| 5320 | |
| 5321 | my $ms_val = $7; |
| 5322 | |
| 5323 | if ($ms_val =~ /^(?:0x|)0+$/i) { |
| 5324 | if (WARN("PREFER_ETH_ZERO_ADDR", |
| 5325 | "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && |
| 5326 | $fix) { |
| 5327 | $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; |
| 5328 | } |
| 5329 | } elsif ($ms_val =~ /^(?:0xff|255)$/i) { |
| 5330 | if (WARN("PREFER_ETH_BROADCAST_ADDR", |
| 5331 | "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && |
| 5332 | $fix) { |
| 5333 | $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; |
| 5334 | } |
| 5335 | } |
| 5336 | } |
| 5337 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5338 | # typecasts on min/max could be min_t/max_t |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5339 | if ($^V && $^V ge 5.10.0 && |
| 5340 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5341 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5342 | if (defined $2 || defined $7) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5343 | my $call = $1; |
| 5344 | my $cast1 = deparenthesize($2); |
| 5345 | my $arg1 = $3; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5346 | my $cast2 = deparenthesize($7); |
| 5347 | my $arg2 = $8; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5348 | my $cast; |
| 5349 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 5350 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 5351 | $cast = "$cast1 or $cast2"; |
| 5352 | } elsif ($cast1 ne "") { |
| 5353 | $cast = $cast1; |
| 5354 | } else { |
| 5355 | $cast = $cast2; |
| 5356 | } |
| 5357 | WARN("MINMAX", |
| 5358 | "$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] | 5359 | } |
| 5360 | } |
| 5361 | |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 5362 | # check usleep_range arguments |
| 5363 | if ($^V && $^V ge 5.10.0 && |
| 5364 | defined $stat && |
| 5365 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 5366 | my $min = $1; |
| 5367 | my $max = $7; |
| 5368 | if ($min eq $max) { |
| 5369 | WARN("USLEEP_RANGE", |
| 5370 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 5371 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 5372 | $min > $max) { |
| 5373 | WARN("USLEEP_RANGE", |
| 5374 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 5375 | } |
| 5376 | } |
| 5377 | |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 5378 | # check for naked sscanf |
| 5379 | if ($^V && $^V ge 5.10.0 && |
| 5380 | defined $stat && |
Joe Perches | 6c8bd70 | 2014-04-03 14:49:16 -0700 | [diff] [blame] | 5381 | $line =~ /\bsscanf\b/ && |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 5382 | ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && |
| 5383 | $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && |
| 5384 | $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { |
| 5385 | my $lc = $stat =~ tr@\n@@; |
| 5386 | $lc = $lc + $linenr; |
| 5387 | my $stat_real = raw_line($linenr, 0); |
| 5388 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 5389 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 5390 | } |
| 5391 | WARN("NAKED_SSCANF", |
| 5392 | "unchecked sscanf return value\n" . "$here\n$stat_real\n"); |
| 5393 | } |
| 5394 | |
Joe Perches | afc819a | 2014-06-04 16:12:08 -0700 | [diff] [blame] | 5395 | # check for simple sscanf that should be kstrto<foo> |
| 5396 | if ($^V && $^V ge 5.10.0 && |
| 5397 | defined $stat && |
| 5398 | $line =~ /\bsscanf\b/) { |
| 5399 | my $lc = $stat =~ tr@\n@@; |
| 5400 | $lc = $lc + $linenr; |
| 5401 | my $stat_real = raw_line($linenr, 0); |
| 5402 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 5403 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 5404 | } |
| 5405 | if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { |
| 5406 | my $format = $6; |
| 5407 | my $count = $format =~ tr@%@%@; |
| 5408 | if ($count == 1 && |
| 5409 | $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { |
| 5410 | WARN("SSCANF_TO_KSTRTO", |
| 5411 | "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); |
| 5412 | } |
| 5413 | } |
| 5414 | } |
| 5415 | |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 5416 | # check for new externs in .h files. |
| 5417 | if ($realfile =~ /\.h$/ && |
| 5418 | $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { |
Joe Perches | d1d8578 | 2013-09-24 15:27:46 -0700 | [diff] [blame] | 5419 | if (CHK("AVOID_EXTERNS", |
| 5420 | "extern prototypes should be avoided in .h files\n" . $herecurr) && |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 5421 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5422 | $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 5423 | } |
| 5424 | } |
| 5425 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5426 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5427 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5428 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5429 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5430 | my $function_name = $1; |
| 5431 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5432 | |
| 5433 | my $s = $stat; |
| 5434 | if (defined $cond) { |
| 5435 | substr($s, 0, length($cond), ''); |
| 5436 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5437 | if ($s =~ /^\s*;/ && |
| 5438 | $function_name ne 'uninitialized_var') |
| 5439 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5440 | WARN("AVOID_EXTERNS", |
| 5441 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | if ($paren_space =~ /\n/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5445 | WARN("FUNCTION_ARGUMENTS", |
| 5446 | "arguments for function declarations should follow identifier\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5447 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 5448 | |
| 5449 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 5450 | $stat =~ /^.\s*extern\s+/) |
| 5451 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5452 | WARN("AVOID_EXTERNS", |
| 5453 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | # checks for new __setup's |
| 5457 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 5458 | my $name = $1; |
| 5459 | |
| 5460 | if (!grep(/$name/, @setup_docs)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5461 | CHK("UNDOCUMENTED_SETUP", |
| 5462 | "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 5463 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 5464 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 5465 | |
| 5466 | # check for pointless casting of kmalloc return |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 5467 | if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5468 | WARN("UNNECESSARY_CASTS", |
| 5469 | "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] | 5470 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5471 | |
Joe Perches | a640d25 | 2013-07-03 15:05:21 -0700 | [diff] [blame] | 5472 | # alloc style |
| 5473 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
| 5474 | if ($^V && $^V ge 5.10.0 && |
| 5475 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { |
| 5476 | CHK("ALLOC_SIZEOF_STRUCT", |
| 5477 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 5478 | } |
| 5479 | |
Joe Perches | 60a5536 | 2014-06-04 16:12:07 -0700 | [diff] [blame] | 5480 | # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc |
| 5481 | if ($^V && $^V ge 5.10.0 && |
Joe Perches | e367455 | 2014-08-06 16:10:55 -0700 | [diff] [blame] | 5482 | $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] | 5483 | my $oldfunc = $3; |
| 5484 | my $a1 = $4; |
| 5485 | my $a2 = $10; |
| 5486 | my $newfunc = "kmalloc_array"; |
| 5487 | $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); |
Joe Perches | e367455 | 2014-08-06 16:10:55 -0700 | [diff] [blame] | 5488 | my $r1 = $a1; |
| 5489 | my $r2 = $a2; |
| 5490 | if ($a1 =~ /^sizeof\s*\S/) { |
| 5491 | $r1 = $a2; |
| 5492 | $r2 = $a1; |
| 5493 | } |
| 5494 | if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && |
| 5495 | !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { |
Joe Perches | 60a5536 | 2014-06-04 16:12:07 -0700 | [diff] [blame] | 5496 | if (WARN("ALLOC_WITH_MULTIPLY", |
| 5497 | "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) && |
| 5498 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5499 | $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] | 5500 | |
| 5501 | } |
| 5502 | } |
| 5503 | } |
| 5504 | |
Joe Perches | 972fdea | 2013-04-29 16:18:12 -0700 | [diff] [blame] | 5505 | # check for krealloc arg reuse |
| 5506 | if ($^V && $^V ge 5.10.0 && |
| 5507 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { |
| 5508 | WARN("KREALLOC_ARG_REUSE", |
| 5509 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 5510 | } |
| 5511 | |
Joe Perches | 5ce59ae | 2013-02-21 16:44:18 -0800 | [diff] [blame] | 5512 | # check for alloc argument mismatch |
| 5513 | if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { |
| 5514 | WARN("ALLOC_ARRAY_ARGS", |
| 5515 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
| 5516 | } |
| 5517 | |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 5518 | # check for multiple semicolons |
| 5519 | if ($line =~ /;\s*;\s*$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5520 | if (WARN("ONE_SEMICOLON", |
| 5521 | "Statements terminations use 1 semicolon\n" . $herecurr) && |
| 5522 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5523 | $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5524 | } |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 5525 | } |
| 5526 | |
Joe Perches | 0ab9019 | 2014-12-10 15:51:57 -0800 | [diff] [blame] | 5527 | # check for #defines like: 1 << <digit> that could be BIT(digit) |
| 5528 | if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { |
| 5529 | my $ull = ""; |
| 5530 | $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); |
| 5531 | if (CHK("BIT_MACRO", |
| 5532 | "Prefer using the BIT$ull macro\n" . $herecurr) && |
| 5533 | $fix) { |
| 5534 | $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; |
| 5535 | } |
| 5536 | } |
| 5537 | |
Joe Perches | e81f239 | 2014-08-06 16:11:25 -0700 | [diff] [blame] | 5538 | # check for case / default statements not preceded by break/fallthrough/switch |
Joe Perches | c34c09a | 2014-01-23 15:54:43 -0800 | [diff] [blame] | 5539 | if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { |
| 5540 | my $has_break = 0; |
| 5541 | my $has_statement = 0; |
| 5542 | my $count = 0; |
| 5543 | my $prevline = $linenr; |
Joe Perches | e81f239 | 2014-08-06 16:11:25 -0700 | [diff] [blame] | 5544 | while ($prevline > 1 && ($file || $count < 3) && !$has_break) { |
Joe Perches | c34c09a | 2014-01-23 15:54:43 -0800 | [diff] [blame] | 5545 | $prevline--; |
| 5546 | my $rline = $rawlines[$prevline - 1]; |
| 5547 | my $fline = $lines[$prevline - 1]; |
| 5548 | last if ($fline =~ /^\@\@/); |
| 5549 | next if ($fline =~ /^\-/); |
| 5550 | next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); |
| 5551 | $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); |
| 5552 | next if ($fline =~ /^.[\s$;]*$/); |
| 5553 | $has_statement = 1; |
| 5554 | $count++; |
| 5555 | $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); |
| 5556 | } |
| 5557 | if (!$has_break && $has_statement) { |
| 5558 | WARN("MISSING_BREAK", |
| 5559 | "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); |
| 5560 | } |
| 5561 | } |
| 5562 | |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 5563 | # check for switch/default statements without a break; |
| 5564 | if ($^V && $^V ge 5.10.0 && |
| 5565 | defined $stat && |
| 5566 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
| 5567 | my $ctx = ''; |
| 5568 | my $herectx = $here . "\n"; |
| 5569 | my $cnt = statement_rawlines($stat); |
| 5570 | for (my $n = 0; $n < $cnt; $n++) { |
| 5571 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 5572 | } |
| 5573 | WARN("DEFAULT_NO_BREAK", |
| 5574 | "switch default: should use break\n" . $herectx); |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 5575 | } |
| 5576 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5577 | # check for gcc specific __FUNCTION__ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5578 | if ($line =~ /\b__FUNCTION__\b/) { |
| 5579 | if (WARN("USE_FUNC", |
| 5580 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && |
| 5581 | $fix) { |
Joe Perches | 194f66f | 2014-08-06 16:11:03 -0700 | [diff] [blame] | 5582 | $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 5583 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5584 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5585 | |
Joe Perches | 62ec818 | 2015-02-13 14:38:18 -0800 | [diff] [blame] | 5586 | # check for uses of __DATE__, __TIME__, __TIMESTAMP__ |
| 5587 | while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { |
| 5588 | ERROR("DATE_TIME", |
| 5589 | "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); |
| 5590 | } |
| 5591 | |
Joe Perches | 2c92488 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 5592 | # check for use of yield() |
| 5593 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 5594 | WARN("YIELD", |
| 5595 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 5596 | } |
| 5597 | |
Joe Perches | 179f8f4 | 2013-07-03 15:05:30 -0700 | [diff] [blame] | 5598 | # check for comparisons against true and false |
| 5599 | if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { |
| 5600 | my $lead = $1; |
| 5601 | my $arg = $2; |
| 5602 | my $test = $3; |
| 5603 | my $otype = $4; |
| 5604 | my $trail = $5; |
| 5605 | my $op = "!"; |
| 5606 | |
| 5607 | ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); |
| 5608 | |
| 5609 | my $type = lc($otype); |
| 5610 | if ($type =~ /^(?:true|false)$/) { |
| 5611 | if (("$test" eq "==" && "$type" eq "true") || |
| 5612 | ("$test" eq "!=" && "$type" eq "false")) { |
| 5613 | $op = ""; |
| 5614 | } |
| 5615 | |
| 5616 | CHK("BOOL_COMPARISON", |
| 5617 | "Using comparison to $otype is error prone\n" . $herecurr); |
| 5618 | |
| 5619 | ## maybe suggesting a correct construct would better |
| 5620 | ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); |
| 5621 | |
| 5622 | } |
| 5623 | } |
| 5624 | |
Thomas Gleixner | 4882720b | 2010-09-07 14:34:01 +0000 | [diff] [blame] | 5625 | # check for semaphores initialized locked |
| 5626 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5627 | WARN("CONSIDER_COMPLETION", |
| 5628 | "consider using a completion\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5629 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 5630 | |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 5631 | # recommend kstrto* over simple_strto* and strict_strto* |
| 5632 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5633 | WARN("CONSIDER_KSTRTO", |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 5634 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5635 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 5636 | |
Fabian Frederick | ae3ccc4 | 2014-06-04 16:12:10 -0700 | [diff] [blame] | 5637 | # check for __initcall(), use device_initcall() explicitly or more appropriate function please |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 5638 | if ($line =~ /^.\s*__initcall\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5639 | WARN("USE_DEVICE_INITCALL", |
Fabian Frederick | ae3ccc4 | 2014-06-04 16:12:10 -0700 | [diff] [blame] | 5640 | "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] | 5641 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 5642 | |
Joe Perches | 0f3c5aa | 2015-02-13 14:39:05 -0800 | [diff] [blame] | 5643 | # check for various structs that are normally const (ops, kgdb, device_tree) |
| 5644 | my $const_structs = qr{ |
| 5645 | acpi_dock_ops| |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 5646 | address_space_operations| |
| 5647 | backlight_ops| |
| 5648 | block_device_operations| |
| 5649 | dentry_operations| |
| 5650 | dev_pm_ops| |
| 5651 | dma_map_ops| |
| 5652 | extent_io_ops| |
| 5653 | file_lock_operations| |
| 5654 | file_operations| |
| 5655 | hv_ops| |
| 5656 | ide_dma_ops| |
| 5657 | intel_dvo_dev_ops| |
| 5658 | item_operations| |
| 5659 | iwl_ops| |
| 5660 | kgdb_arch| |
| 5661 | kgdb_io| |
| 5662 | kset_uevent_ops| |
| 5663 | lock_manager_operations| |
| 5664 | microcode_ops| |
| 5665 | mtrr_ops| |
| 5666 | neigh_ops| |
| 5667 | nlmsvc_binding| |
Joe Perches | 0f3c5aa | 2015-02-13 14:39:05 -0800 | [diff] [blame] | 5668 | of_device_id| |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 5669 | pci_raw_ops| |
| 5670 | pipe_buf_operations| |
| 5671 | platform_hibernation_ops| |
| 5672 | platform_suspend_ops| |
| 5673 | proto_ops| |
| 5674 | rpc_pipe_ops| |
| 5675 | seq_operations| |
| 5676 | snd_ac97_build_ops| |
| 5677 | soc_pcmcia_socket_ops| |
| 5678 | stacktrace_ops| |
| 5679 | sysfs_ops| |
| 5680 | tty_operations| |
Joe Perches | 6d07d01b | 2015-04-16 12:44:33 -0700 | [diff] [blame] | 5681 | uart_ops| |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 5682 | usb_mon_operations| |
| 5683 | wd_ops}x; |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 5684 | if ($line !~ /\bconst\b/ && |
Joe Perches | 0f3c5aa | 2015-02-13 14:39:05 -0800 | [diff] [blame] | 5685 | $line =~ /\bstruct\s+($const_structs)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5686 | WARN("CONST_STRUCT", |
| 5687 | "struct $1 should normally be const\n" . |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 5688 | $herecurr); |
Andy Whitcroft | 2b6db5c | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 5689 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5690 | |
| 5691 | # use of NR_CPUS is usually wrong |
| 5692 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 5693 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 5694 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 5695 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 5696 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 5697 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 5698 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 5699 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5700 | WARN("NR_CPUS", |
| 5701 | "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] | 5702 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 5703 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 5704 | # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. |
| 5705 | if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { |
| 5706 | ERROR("DEFINE_ARCH_HAS", |
| 5707 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); |
| 5708 | } |
| 5709 | |
Joe Perches | acd9362 | 2015-02-13 14:38:38 -0800 | [diff] [blame] | 5710 | # likely/unlikely comparisons similar to "(likely(foo) > 0)" |
| 5711 | if ($^V && $^V ge 5.10.0 && |
| 5712 | $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { |
| 5713 | WARN("LIKELY_MISUSE", |
| 5714 | "Using $1 should generally have parentheses around the comparison\n" . $herecurr); |
| 5715 | } |
| 5716 | |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 5717 | # whine mightly about in_atomic |
| 5718 | if ($line =~ /\bin_atomic\s*\(/) { |
| 5719 | if ($realfile =~ m@^drivers/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5720 | ERROR("IN_ATOMIC", |
| 5721 | "do not use in_atomic in drivers\n" . $herecurr); |
Andy Whitcroft | f4a8773 | 2009-02-27 14:03:05 -0800 | [diff] [blame] | 5722 | } elsif ($realfile !~ m@^kernel/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5723 | WARN("IN_ATOMIC", |
| 5724 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 5725 | } |
| 5726 | } |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 5727 | |
| 5728 | # check for lockdep_set_novalidate_class |
| 5729 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 5730 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 5731 | if ($realfile !~ m@^kernel/lockdep@ && |
| 5732 | $realfile !~ m@^include/linux/lockdep@ && |
| 5733 | $realfile !~ m@^drivers/base/core@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5734 | ERROR("LOCKDEP", |
| 5735 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 5736 | } |
| 5737 | } |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 5738 | |
Joe Perches | b392c64 | 2015-04-16 12:44:16 -0700 | [diff] [blame] | 5739 | if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || |
| 5740 | $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5741 | WARN("EXPORTED_WORLD_WRITABLE", |
| 5742 | "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] | 5743 | } |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 5744 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 5745 | # Mode permission misuses where it seems decimal should be octal |
| 5746 | # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop |
| 5747 | if ($^V && $^V ge 5.10.0 && |
| 5748 | $line =~ /$mode_perms_search/) { |
| 5749 | foreach my $entry (@mode_permission_funcs) { |
| 5750 | my $func = $entry->[0]; |
| 5751 | my $arg_pos = $entry->[1]; |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 5752 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 5753 | my $skip_args = ""; |
| 5754 | if ($arg_pos > 1) { |
| 5755 | $arg_pos--; |
| 5756 | $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; |
| 5757 | } |
| 5758 | my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]"; |
| 5759 | if ($line =~ /$test/) { |
| 5760 | my $val = $1; |
| 5761 | $val = $6 if ($skip_args ne ""); |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 5762 | |
Joe Perches | 515a235 | 2014-04-03 14:49:24 -0700 | [diff] [blame] | 5763 | if ($val !~ /^0$/ && |
| 5764 | (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || |
| 5765 | length($val) ne 4)) { |
| 5766 | ERROR("NON_OCTAL_PERMISSIONS", |
| 5767 | "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); |
Joe Perches | c0a5c89 | 2015-02-13 14:38:21 -0800 | [diff] [blame] | 5768 | } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) { |
| 5769 | ERROR("EXPORTED_WORLD_WRITABLE", |
| 5770 | "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] | 5771 | } |
Joe Perches | 2435880 | 2014-04-03 14:49:13 -0700 | [diff] [blame] | 5772 | } |
| 5773 | } |
| 5774 | } |
Bjorn Andersson | 5a6d20c | 2015-06-25 15:03:24 -0700 | [diff] [blame] | 5775 | |
| 5776 | # validate content of MODULE_LICENSE against list from include/linux/module.h |
| 5777 | if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { |
| 5778 | my $extracted_string = get_quoted_string($line, $rawline); |
| 5779 | my $valid_licenses = qr{ |
| 5780 | GPL| |
| 5781 | GPL\ v2| |
| 5782 | GPL\ and\ additional\ rights| |
| 5783 | Dual\ BSD/GPL| |
| 5784 | Dual\ MIT/GPL| |
| 5785 | Dual\ MPL/GPL| |
| 5786 | Proprietary |
| 5787 | }x; |
| 5788 | if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { |
| 5789 | WARN("MODULE_LICENSE", |
| 5790 | "unknown module license " . $extracted_string . "\n" . $herecurr); |
| 5791 | } |
| 5792 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | # If we have no input at all, then there is nothing to report on |
| 5796 | # so just keep quiet. |
| 5797 | if ($#rawlines == -1) { |
| 5798 | exit(0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5799 | } |
| 5800 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5801 | # In mailback mode only produce a report in the negative, for |
| 5802 | # things that appear to be patches. |
| 5803 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 5804 | exit(0); |
| 5805 | } |
| 5806 | |
| 5807 | # This is not a patch, and we are are in 'no-patch' mode so |
| 5808 | # just keep quiet. |
| 5809 | if (!$chk_patch && !$is_patch) { |
| 5810 | exit(0); |
| 5811 | } |
| 5812 | |
Joe Perches | 06330fc | 2015-06-25 15:03:11 -0700 | [diff] [blame] | 5813 | if (!$is_patch && $file !~ /cover-letter\.patch$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5814 | ERROR("NOT_UNIFIED_DIFF", |
| 5815 | "Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5816 | } |
Joe Perches | 34d8815 | 2015-06-25 15:03:05 -0700 | [diff] [blame] | 5817 | if ($is_patch && $filename ne '-' && $chk_signoff && $signoff == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 5818 | ERROR("MISSING_SIGN_OFF", |
| 5819 | "Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5820 | } |
| 5821 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5822 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 5823 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 5824 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5825 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 5826 | (($check)? "$cnt_chk checks, " : "") . |
| 5827 | "$cnt_lines lines checked\n"; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 5828 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 5829 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 5830 | if ($quiet == 0) { |
| 5831 | # If there were whitespace errors which cleanpatch can fix |
| 5832 | # then suggest that. |
| 5833 | if ($rpt_cleaners) { |
Mike Frysinger | b078121 | 2011-03-22 16:34:43 -0700 | [diff] [blame] | 5834 | $rpt_cleaners = 0; |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 5835 | print << "EOM" |
| 5836 | |
| 5837 | NOTE: Whitespace errors detected. |
| 5838 | You may wish to use scripts/cleanpatch or scripts/cleanfile |
| 5839 | EOM |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 5840 | } |
| 5841 | } |
| 5842 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 5843 | if ($clean == 0 && $fix && |
| 5844 | ("@rawlines" ne "@fixed" || |
| 5845 | $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 5846 | my $newfile = $filename; |
| 5847 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5848 | my $linecount = 0; |
| 5849 | my $f; |
| 5850 | |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 5851 | @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); |
| 5852 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5853 | open($f, '>', $newfile) |
| 5854 | or die "$P: Can't open $newfile for write\n"; |
| 5855 | foreach my $fixed_line (@fixed) { |
| 5856 | $linecount++; |
| 5857 | if ($file) { |
| 5858 | if ($linecount > 3) { |
| 5859 | $fixed_line =~ s/^\+//; |
Joe Perches | d752fcc | 2014-08-06 16:11:05 -0700 | [diff] [blame] | 5860 | print $f $fixed_line . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5861 | } |
| 5862 | } else { |
| 5863 | print $f $fixed_line . "\n"; |
| 5864 | } |
| 5865 | } |
| 5866 | close($f); |
| 5867 | |
| 5868 | if (!$quiet) { |
| 5869 | print << "EOM"; |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 5870 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5871 | Wrote EXPERIMENTAL --fix correction(s) to '$newfile' |
| 5872 | |
| 5873 | Do _NOT_ trust the results written to this file. |
| 5874 | Do _NOT_ submit these changes without inspecting them for correctness. |
| 5875 | |
| 5876 | This EXPERIMENTAL file is simply a convenience to help rewrite patches. |
| 5877 | No warranties, expressed or implied... |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 5878 | EOM |
| 5879 | } |
| 5880 | } |
| 5881 | |
Joe Perches | d8469f1 | 2015-06-25 15:03:00 -0700 | [diff] [blame] | 5882 | if ($quiet == 0) { |
| 5883 | print "\n"; |
| 5884 | if ($clean == 1) { |
| 5885 | print "$vname has no obvious style problems and is ready for submission.\n"; |
| 5886 | } else { |
| 5887 | print "$vname has style problems, please review.\n"; |
| 5888 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5889 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 5890 | return $clean; |
| 5891 | } |