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; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 10 | |
| 11 | my $P = $0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 12 | $P =~ s@.*/@@g; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 13 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 14 | my $V = '0.32'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 15 | |
| 16 | use Getopt::Long qw(:config no_auto_abbrev); |
| 17 | |
| 18 | my $quiet = 0; |
| 19 | my $tree = 1; |
| 20 | my $chk_signoff = 1; |
| 21 | my $chk_patch = 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 22 | my $tst_only; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 23 | my $emacs = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 24 | my $terse = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 25 | my $file = 0; |
| 26 | my $check = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 27 | my $summary = 1; |
| 28 | my $mailback = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 29 | my $summary_file = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 30 | my $show_types = 0; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 31 | my $fix = 0; |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 32 | my $fix_inplace = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 33 | my $root; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 34 | my %debug; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 35 | my %camelcase = (); |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 36 | my %use_type = (); |
| 37 | my @use = (); |
| 38 | my %ignore_type = (); |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 39 | my @ignore = (); |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 40 | my $help = 0; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 41 | my $configuration_file = ".checkpatch.conf"; |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 42 | my $max_line_length = 80; |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 43 | my $ignore_perl_version = 0; |
| 44 | my $minimum_perl_version = 5.10.0; |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 45 | |
| 46 | sub help { |
| 47 | my ($exitcode) = @_; |
| 48 | |
| 49 | print << "EOM"; |
| 50 | Usage: $P [OPTION]... [FILE]... |
| 51 | Version: $V |
| 52 | |
| 53 | Options: |
| 54 | -q, --quiet quiet |
| 55 | --no-tree run without a kernel tree |
| 56 | --no-signoff do not check for 'Signed-off-by' line |
| 57 | --patch treat FILE as patchfile (default) |
| 58 | --emacs emacs compile window format |
| 59 | --terse one line per report |
| 60 | -f, --file treat FILE as regular source file |
| 61 | --subjective, --strict enable more subjective tests |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 62 | --types TYPE(,TYPE2...) show only these comma separated message types |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 63 | --ignore TYPE(,TYPE2...) ignore various comma separated message types |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 64 | --max-line-length=n set the maximum line length, if exceeded, warn |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 65 | --show-types show the message "types" in the output |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 66 | --root=PATH PATH to the kernel tree root |
| 67 | --no-summary suppress the per-file summary |
| 68 | --mailback only produce a report in case of warnings/errors |
| 69 | --summary-file include the filename in summary |
| 70 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 71 | 'values', 'possible', 'type', and 'attr' (default |
| 72 | is all off) |
| 73 | --test-only=WORD report only warnings/errors containing WORD |
| 74 | literally |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 75 | --fix EXPERIMENTAL - may create horrible results |
| 76 | If correctable single-line errors exist, create |
| 77 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" |
| 78 | with potential errors corrected to the preferred |
| 79 | checkpatch style |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 80 | --fix-inplace EXPERIMENTAL - may create horrible results |
| 81 | Is the same as --fix, but overwrites the input |
| 82 | file. It's your fault if there's no backup or git |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 83 | --ignore-perl-version override checking of perl version. expect |
| 84 | runtime errors. |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 85 | -h, --help, --version display this help and exit |
| 86 | |
| 87 | When FILE is - read standard input. |
| 88 | EOM |
| 89 | |
| 90 | exit($exitcode); |
| 91 | } |
| 92 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 93 | my $conf = which_conf($configuration_file); |
| 94 | if (-f $conf) { |
| 95 | my @conf_args; |
| 96 | open(my $conffile, '<', "$conf") |
| 97 | or warn "$P: Can't find a readable $configuration_file file $!\n"; |
| 98 | |
| 99 | while (<$conffile>) { |
| 100 | my $line = $_; |
| 101 | |
| 102 | $line =~ s/\s*\n?$//g; |
| 103 | $line =~ s/^\s*//g; |
| 104 | $line =~ s/\s+/ /g; |
| 105 | |
| 106 | next if ($line =~ m/^\s*#/); |
| 107 | next if ($line =~ m/^\s*$/); |
| 108 | |
| 109 | my @words = split(" ", $line); |
| 110 | foreach my $word (@words) { |
| 111 | last if ($word =~ m/^#/); |
| 112 | push (@conf_args, $word); |
| 113 | } |
| 114 | } |
| 115 | close($conffile); |
| 116 | unshift(@ARGV, @conf_args) if @conf_args; |
| 117 | } |
| 118 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 119 | GetOptions( |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 120 | 'q|quiet+' => \$quiet, |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 121 | 'tree!' => \$tree, |
| 122 | 'signoff!' => \$chk_signoff, |
| 123 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 124 | 'emacs!' => \$emacs, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 125 | 'terse!' => \$terse, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 126 | 'f|file!' => \$file, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 127 | 'subjective!' => \$check, |
| 128 | 'strict!' => \$check, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 129 | 'ignore=s' => \@ignore, |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 130 | 'types=s' => \@use, |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 131 | 'show-types!' => \$show_types, |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 132 | 'max-line-length=i' => \$max_line_length, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 133 | 'root=s' => \$root, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 134 | 'summary!' => \$summary, |
| 135 | 'mailback!' => \$mailback, |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 136 | 'summary-file!' => \$summary_file, |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 137 | 'fix!' => \$fix, |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 138 | 'fix-inplace!' => \$fix_inplace, |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 139 | 'ignore-perl-version!' => \$ignore_perl_version, |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 140 | 'debug=s' => \%debug, |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 141 | 'test-only=s' => \$tst_only, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 142 | 'h|help' => \$help, |
| 143 | 'version' => \$help |
| 144 | ) or help(1); |
| 145 | |
| 146 | help(0) if ($help); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 147 | |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 148 | $fix = 1 if ($fix_inplace); |
| 149 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 150 | my $exit = 0; |
| 151 | |
Dave Hansen | d62a201 | 2013-09-11 14:23:56 -0700 | [diff] [blame] | 152 | if ($^V && $^V lt $minimum_perl_version) { |
| 153 | printf "$P: requires at least perl version %vd\n", $minimum_perl_version; |
| 154 | if (!$ignore_perl_version) { |
| 155 | exit(1); |
| 156 | } |
| 157 | } |
| 158 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 159 | if ($#ARGV < 0) { |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 160 | print "$P: no input files\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 161 | exit(1); |
| 162 | } |
| 163 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 164 | sub hash_save_array_words { |
| 165 | my ($hashRef, $arrayRef) = @_; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 166 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 167 | my @array = split(/,/, join(',', @$arrayRef)); |
| 168 | foreach my $word (@array) { |
| 169 | $word =~ s/\s*\n?$//g; |
| 170 | $word =~ s/^\s*//g; |
| 171 | $word =~ s/\s+/ /g; |
| 172 | $word =~ tr/[a-z]/[A-Z]/; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 173 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 174 | next if ($word =~ m/^\s*#/); |
| 175 | next if ($word =~ m/^\s*$/); |
| 176 | |
| 177 | $hashRef->{$word}++; |
| 178 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 181 | sub hash_show_words { |
| 182 | my ($hashRef, $prefix) = @_; |
| 183 | |
Joe Perches | 58cb3cf | 2013-09-11 14:24:04 -0700 | [diff] [blame] | 184 | if ($quiet == 0 && keys %$hashRef) { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 185 | print "NOTE: $prefix message types:"; |
Joe Perches | 58cb3cf | 2013-09-11 14:24:04 -0700 | [diff] [blame] | 186 | foreach my $word (sort keys %$hashRef) { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 187 | print " $word"; |
| 188 | } |
| 189 | print "\n\n"; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | hash_save_array_words(\%ignore_type, \@ignore); |
| 194 | hash_save_array_words(\%use_type, \@use); |
| 195 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 196 | my $dbg_values = 0; |
| 197 | my $dbg_possible = 0; |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 198 | my $dbg_type = 0; |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 199 | my $dbg_attr = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 200 | for my $key (keys %debug) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 201 | ## no critic |
| 202 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 203 | die "$@" if ($@); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 206 | my $rpt_cleaners = 0; |
| 207 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 208 | if ($terse) { |
| 209 | $emacs = 1; |
| 210 | $quiet++; |
| 211 | } |
| 212 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 213 | if ($tree) { |
| 214 | if (defined $root) { |
| 215 | if (!top_of_kernel_tree($root)) { |
| 216 | die "$P: $root: --root does not point at a valid tree\n"; |
| 217 | } |
| 218 | } else { |
| 219 | if (top_of_kernel_tree('.')) { |
| 220 | $root = '.'; |
| 221 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 222 | top_of_kernel_tree($1)) { |
| 223 | $root = $1; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (!defined $root) { |
| 228 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 229 | exit(2); |
| 230 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 233 | my $emitted_corrupt = 0; |
| 234 | |
Andy Whitcroft | 2ceb532 | 2009-10-26 16:50:14 -0700 | [diff] [blame] | 235 | our $Ident = qr{ |
| 236 | [A-Za-z_][A-Za-z\d_]* |
| 237 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 238 | }x; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 239 | our $Storage = qr{extern|static|asmlinkage}; |
| 240 | our $Sparse = qr{ |
| 241 | __user| |
| 242 | __kernel| |
| 243 | __force| |
| 244 | __iomem| |
| 245 | __must_check| |
| 246 | __init_refok| |
Andy Whitcroft | 417495e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 247 | __kprobes| |
Sven Eckelmann | 165e72a | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 248 | __ref| |
| 249 | __rcu |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 250 | }x; |
Joe Perches | e970b88 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 251 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; |
| 252 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; |
| 253 | our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; |
| 254 | our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; |
| 255 | our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 256 | |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 257 | # Notes to $Attribute: |
| 258 | # 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] | 259 | our $Attribute = qr{ |
| 260 | const| |
Joe Perches | 03f1df7 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 261 | __percpu| |
| 262 | __nocast| |
| 263 | __safe| |
| 264 | __bitwise__| |
| 265 | __packed__| |
| 266 | __packed2__| |
| 267 | __naked| |
| 268 | __maybe_unused| |
| 269 | __always_unused| |
| 270 | __noreturn| |
| 271 | __used| |
| 272 | __cold| |
| 273 | __noclone| |
| 274 | __deprecated| |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 275 | __read_mostly| |
| 276 | __kprobes| |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 277 | $InitAttribute| |
Andy Whitcroft | 24e1d81 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 278 | ____cacheline_aligned| |
| 279 | ____cacheline_aligned_in_smp| |
Andy Whitcroft | 5fe3af1 | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 280 | ____cacheline_internodealigned_in_smp| |
| 281 | __weak |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 282 | }x; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 283 | our $Modifier; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 284 | our $Inline = qr{inline|__always_inline|noinline}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 285 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 286 | our $Lval = qr{$Ident(?:$Member)*}; |
| 287 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 288 | our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; |
| 289 | our $Binary = qr{(?i)0b[01]+$Int_type?}; |
| 290 | our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; |
| 291 | our $Int = qr{[0-9]+$Int_type?}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 292 | our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; |
| 293 | our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; |
| 294 | our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; |
Joe Perches | 74349bc | 2012-12-17 16:02:05 -0800 | [diff] [blame] | 295 | our $Float = qr{$Float_hex|$Float_dec|$Float_int}; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 296 | our $Constant = qr{$Float|$Binary|$Hex|$Int}; |
Joe Perches | 326b1ff | 2013-02-04 14:28:51 -0800 | [diff] [blame] | 297 | our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 298 | our $Compare = qr{<=|>=|==|!=|<|>}; |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 299 | our $Arithmetic = qr{\+|-|\*|\/|%}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 300 | our $Operators = qr{ |
| 301 | <=|>=|==|!=| |
| 302 | =>|->|<<|>>|<|>|!|~| |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 303 | &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 304 | }x; |
| 305 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 306 | our $NonptrType; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 307 | our $NonptrTypeWithAttr; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 308 | our $Type; |
| 309 | our $Declare; |
| 310 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 311 | our $NON_ASCII_UTF8 = qr{ |
| 312 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 313 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 314 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 315 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 316 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 317 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 318 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 319 | }x; |
| 320 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 321 | our $UTF8 = qr{ |
| 322 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 323 | | $NON_ASCII_UTF8 |
| 324 | }x; |
| 325 | |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 326 | our $typeTypedefs = qr{(?x: |
Andy Whitcroft | fb9e909 | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 327 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 328 | atomic_t |
| 329 | )}; |
| 330 | |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 331 | our $logFunctions = qr{(?x: |
Joe Perches | 6e60c02 | 2011-07-25 17:13:27 -0700 | [diff] [blame] | 332 | printk(?:_ratelimited|_once|)| |
Jacob Keller | 7d0b659 | 2013-07-03 15:05:35 -0700 | [diff] [blame] | 333 | (?:[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] | 334 | WARN(?:_RATELIMIT|_ONCE|)| |
Joe Perches | b053172 | 2011-05-24 17:13:40 -0700 | [diff] [blame] | 335 | panic| |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 336 | MODULE_[A-Z_]+| |
| 337 | seq_vprintf|seq_printf|seq_puts |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 338 | )}; |
| 339 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 340 | our $signature_tags = qr{(?xi: |
| 341 | Signed-off-by:| |
| 342 | Acked-by:| |
| 343 | Tested-by:| |
| 344 | Reviewed-by:| |
| 345 | Reported-by:| |
Mugunthan V N | 8543ae1 | 2013-04-29 16:18:17 -0700 | [diff] [blame] | 346 | Suggested-by:| |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 347 | To:| |
| 348 | Cc: |
| 349 | )}; |
| 350 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 351 | our @typeList = ( |
| 352 | qr{void}, |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 353 | qr{(?:unsigned\s+)?char}, |
| 354 | qr{(?:unsigned\s+)?short}, |
| 355 | qr{(?:unsigned\s+)?int}, |
| 356 | qr{(?:unsigned\s+)?long}, |
| 357 | qr{(?:unsigned\s+)?long\s+int}, |
| 358 | qr{(?:unsigned\s+)?long\s+long}, |
| 359 | qr{(?:unsigned\s+)?long\s+long\s+int}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 360 | qr{unsigned}, |
| 361 | qr{float}, |
| 362 | qr{double}, |
| 363 | qr{bool}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 364 | qr{struct\s+$Ident}, |
| 365 | qr{union\s+$Ident}, |
| 366 | qr{enum\s+$Ident}, |
| 367 | qr{${Ident}_t}, |
| 368 | qr{${Ident}_handler}, |
| 369 | qr{${Ident}_handler_fn}, |
| 370 | ); |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 371 | our @typeListWithAttr = ( |
| 372 | @typeList, |
| 373 | qr{struct\s+$InitAttribute\s+$Ident}, |
| 374 | qr{union\s+$InitAttribute\s+$Ident}, |
| 375 | ); |
| 376 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 377 | our @modifierList = ( |
| 378 | qr{fastcall}, |
| 379 | ); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 380 | |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 381 | our $allowed_asm_includes = qr{(?x: |
| 382 | irq| |
| 383 | memory |
| 384 | )}; |
| 385 | # memory.h: ARM has a custom one |
| 386 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 387 | sub build_types { |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 388 | my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; |
| 389 | my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 390 | my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 391 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 392 | $NonptrType = qr{ |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 393 | (?:$Modifier\s+|const\s+)* |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 394 | (?: |
Andy Whitcroft | 6b48db2 | 2012-01-10 15:10:13 -0800 | [diff] [blame] | 395 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 396 | (?:$typeTypedefs\b)| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 397 | (?:${all}\b) |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 398 | ) |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 399 | (?:\s+$Modifier|\s+const)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 400 | }x; |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 401 | $NonptrTypeWithAttr = qr{ |
| 402 | (?:$Modifier\s+|const\s+)* |
| 403 | (?: |
| 404 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 405 | (?:$typeTypedefs\b)| |
| 406 | (?:${allWithAttr}\b) |
| 407 | ) |
| 408 | (?:\s+$Modifier|\s+const)* |
| 409 | }x; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 410 | $Type = qr{ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 411 | $NonptrType |
Andy Whitcroft | b337d8b | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 412 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 413 | (?:\s+$Inline|\s+$Modifier)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 414 | }x; |
| 415 | $Declare = qr{(?:$Storage\s+)?$Type}; |
| 416 | } |
| 417 | build_types(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 418 | |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 419 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 420 | |
| 421 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg |
| 422 | # requires at least perl version v5.10.0 |
| 423 | # Any use must be runtime checked with $^V |
| 424 | |
| 425 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; |
| 426 | our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*}; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 427 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; |
Joe Perches | 7d2367a | 2011-07-25 17:13:22 -0700 | [diff] [blame] | 428 | |
| 429 | sub deparenthesize { |
| 430 | my ($string) = @_; |
| 431 | return "" if (!defined($string)); |
| 432 | $string =~ s@^\s*\(\s*@@g; |
| 433 | $string =~ s@\s*\)\s*$@@g; |
| 434 | $string =~ s@\s+@ @g; |
| 435 | return $string; |
| 436 | } |
| 437 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 438 | sub seed_camelcase_file { |
| 439 | my ($file) = @_; |
| 440 | |
| 441 | return if (!(-f $file)); |
| 442 | |
| 443 | local $/; |
| 444 | |
| 445 | open(my $include_file, '<', "$file") |
| 446 | or warn "$P: Can't read '$file' $!\n"; |
| 447 | my $text = <$include_file>; |
| 448 | close($include_file); |
| 449 | |
| 450 | my @lines = split('\n', $text); |
| 451 | |
| 452 | foreach my $line (@lines) { |
| 453 | next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); |
| 454 | if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { |
| 455 | $camelcase{$1} = 1; |
Joe Perches | 11ea516 | 2013-11-12 15:10:08 -0800 | [diff] [blame] | 456 | } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { |
| 457 | $camelcase{$1} = 1; |
| 458 | } 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] | 459 | $camelcase{$1} = 1; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | my $camelcase_seeded = 0; |
| 465 | sub seed_camelcase_includes { |
| 466 | return if ($camelcase_seeded); |
| 467 | |
| 468 | my $files; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 469 | my $camelcase_cache = ""; |
| 470 | my @include_files = (); |
| 471 | |
| 472 | $camelcase_seeded = 1; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 473 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 474 | if (-d ".git") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 475 | my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; |
| 476 | chomp $git_last_include_commit; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 477 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 478 | } else { |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 479 | my $last_mod_date = 0; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 480 | $files = `find $root/include -name "*.h"`; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 481 | @include_files = split('\n', $files); |
| 482 | foreach my $file (@include_files) { |
| 483 | my $date = POSIX::strftime("%Y%m%d%H%M", |
| 484 | localtime((stat $file)[9])); |
| 485 | $last_mod_date = $date if ($last_mod_date < $date); |
| 486 | } |
| 487 | $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 488 | } |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 489 | |
| 490 | if ($camelcase_cache ne "" && -f $camelcase_cache) { |
| 491 | open(my $camelcase_file, '<', "$camelcase_cache") |
| 492 | or warn "$P: Can't read '$camelcase_cache' $!\n"; |
| 493 | while (<$camelcase_file>) { |
| 494 | chomp; |
| 495 | $camelcase{$_} = 1; |
| 496 | } |
| 497 | close($camelcase_file); |
| 498 | |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | if (-d ".git") { |
| 503 | $files = `git ls-files "include/*.h"`; |
| 504 | @include_files = split('\n', $files); |
| 505 | } |
| 506 | |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 507 | foreach my $file (@include_files) { |
| 508 | seed_camelcase_file($file); |
| 509 | } |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 510 | |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 511 | if ($camelcase_cache ne "") { |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 512 | unlink glob ".checkpatch-camelcase.*"; |
Joe Perches | c707a81 | 2013-07-08 16:00:43 -0700 | [diff] [blame] | 513 | open(my $camelcase_file, '>', "$camelcase_cache") |
| 514 | or warn "$P: Can't write '$camelcase_cache' $!\n"; |
Joe Perches | 351b2a1 | 2013-07-03 15:05:36 -0700 | [diff] [blame] | 515 | foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { |
| 516 | print $camelcase_file ("$_\n"); |
| 517 | } |
| 518 | close($camelcase_file); |
| 519 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 522 | $chk_signoff = 0 if ($file); |
| 523 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 524 | my @rawlines = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 525 | my @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 526 | my @fixed = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 527 | my $vname; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 528 | for my $filename (@ARGV) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 529 | my $FILE; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 530 | if ($file) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 531 | open($FILE, '-|', "diff -u /dev/null $filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 532 | die "$P: $filename: diff failed - $!\n"; |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 533 | } elsif ($filename eq '-') { |
| 534 | open($FILE, '<&STDIN'); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 535 | } else { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 536 | open($FILE, '<', "$filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 537 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 538 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 539 | if ($filename eq '-') { |
| 540 | $vname = 'Your patch'; |
| 541 | } else { |
| 542 | $vname = $filename; |
| 543 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 544 | while (<$FILE>) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 545 | chomp; |
| 546 | push(@rawlines, $_); |
| 547 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 548 | close($FILE); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 549 | if (!process($filename)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 550 | $exit = 1; |
| 551 | } |
| 552 | @rawlines = (); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 553 | @lines = (); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 554 | @fixed = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | exit($exit); |
| 558 | |
| 559 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 560 | my ($root) = @_; |
| 561 | |
| 562 | my @tree_check = ( |
| 563 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 564 | "README", "Documentation", "arch", "include", "drivers", |
| 565 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 566 | ); |
| 567 | |
| 568 | foreach my $check (@tree_check) { |
| 569 | if (! -e $root . '/' . $check) { |
| 570 | return 0; |
| 571 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 572 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 573 | return 1; |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 574 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 575 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 576 | sub parse_email { |
| 577 | my ($formatted_email) = @_; |
| 578 | |
| 579 | my $name = ""; |
| 580 | my $address = ""; |
| 581 | my $comment = ""; |
| 582 | |
| 583 | if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { |
| 584 | $name = $1; |
| 585 | $address = $2; |
| 586 | $comment = $3 if defined $3; |
| 587 | } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { |
| 588 | $address = $1; |
| 589 | $comment = $2 if defined $2; |
| 590 | } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { |
| 591 | $address = $1; |
| 592 | $comment = $2 if defined $2; |
| 593 | $formatted_email =~ s/$address.*$//; |
| 594 | $name = $formatted_email; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 595 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 596 | $name =~ s/^\"|\"$//g; |
| 597 | # If there's a name left after stripping spaces and |
| 598 | # leading quotes, and the address doesn't have both |
| 599 | # leading and trailing angle brackets, the address |
| 600 | # is invalid. ie: |
| 601 | # "joe smith joe@smith.com" bad |
| 602 | # "joe smith <joe@smith.com" bad |
| 603 | if ($name ne "" && $address !~ /^<[^>]+>$/) { |
| 604 | $name = ""; |
| 605 | $address = ""; |
| 606 | $comment = ""; |
| 607 | } |
| 608 | } |
| 609 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 610 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 611 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 612 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 613 | $address =~ s/^\<|\>$//g; |
| 614 | |
| 615 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 616 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 617 | $name = "\"$name\""; |
| 618 | } |
| 619 | |
| 620 | return ($name, $address, $comment); |
| 621 | } |
| 622 | |
| 623 | sub format_email { |
| 624 | my ($name, $address) = @_; |
| 625 | |
| 626 | my $formatted_email; |
| 627 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 628 | $name = trim($name); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 629 | $name =~ s/^\"|\"$//g; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 630 | $address = trim($address); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 631 | |
| 632 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 633 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 634 | $name = "\"$name\""; |
| 635 | } |
| 636 | |
| 637 | if ("$name" eq "") { |
| 638 | $formatted_email = "$address"; |
| 639 | } else { |
| 640 | $formatted_email = "$name <$address>"; |
| 641 | } |
| 642 | |
| 643 | return $formatted_email; |
| 644 | } |
| 645 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 646 | sub which_conf { |
| 647 | my ($conf) = @_; |
| 648 | |
| 649 | foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { |
| 650 | if (-e "$path/$conf") { |
| 651 | return "$path/$conf"; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | return ""; |
| 656 | } |
| 657 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 658 | sub expand_tabs { |
| 659 | my ($str) = @_; |
| 660 | |
| 661 | my $res = ''; |
| 662 | my $n = 0; |
| 663 | for my $c (split(//, $str)) { |
| 664 | if ($c eq "\t") { |
| 665 | $res .= ' '; |
| 666 | $n++; |
| 667 | for (; ($n % 8) != 0; $n++) { |
| 668 | $res .= ' '; |
| 669 | } |
| 670 | next; |
| 671 | } |
| 672 | $res .= $c; |
| 673 | $n++; |
| 674 | } |
| 675 | |
| 676 | return $res; |
| 677 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 678 | sub copy_spacing { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 679 | (my $res = shift) =~ tr/\t/ /c; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 680 | return $res; |
| 681 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 682 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 683 | sub line_stats { |
| 684 | my ($line) = @_; |
| 685 | |
| 686 | # Drop the diff line leader and expand tabs |
| 687 | $line =~ s/^.//; |
| 688 | $line = expand_tabs($line); |
| 689 | |
| 690 | # Pick the indent from the front of the line. |
| 691 | my ($white) = ($line =~ /^(\s*)/); |
| 692 | |
| 693 | return (length($line), length($white)); |
| 694 | } |
| 695 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 696 | my $sanitise_quote = ''; |
| 697 | |
| 698 | sub sanitise_line_reset { |
| 699 | my ($in_comment) = @_; |
| 700 | |
| 701 | if ($in_comment) { |
| 702 | $sanitise_quote = '*/'; |
| 703 | } else { |
| 704 | $sanitise_quote = ''; |
| 705 | } |
| 706 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 707 | sub sanitise_line { |
| 708 | my ($line) = @_; |
| 709 | |
| 710 | my $res = ''; |
| 711 | my $l = ''; |
| 712 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 713 | my $qlen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 714 | my $off = 0; |
| 715 | my $c; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 716 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 717 | # Always copy over the diff marker. |
| 718 | $res = substr($line, 0, 1); |
| 719 | |
| 720 | for ($off = 1; $off < length($line); $off++) { |
| 721 | $c = substr($line, $off, 1); |
| 722 | |
| 723 | # Comments we are wacking completly including the begin |
| 724 | # and end, all to $;. |
| 725 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 726 | $sanitise_quote = '*/'; |
| 727 | |
| 728 | substr($res, $off, 2, "$;$;"); |
| 729 | $off++; |
| 730 | next; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 731 | } |
Andy Whitcroft | 81bc0e0 | 2008-10-15 22:02:26 -0700 | [diff] [blame] | 732 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 733 | $sanitise_quote = ''; |
| 734 | substr($res, $off, 2, "$;$;"); |
| 735 | $off++; |
| 736 | next; |
| 737 | } |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 738 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 739 | $sanitise_quote = '//'; |
| 740 | |
| 741 | substr($res, $off, 2, $sanitise_quote); |
| 742 | $off++; |
| 743 | next; |
| 744 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 745 | |
| 746 | # A \ in a string means ignore the next character. |
| 747 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 748 | $c eq "\\") { |
| 749 | substr($res, $off, 2, 'XX'); |
| 750 | $off++; |
| 751 | next; |
| 752 | } |
| 753 | # Regular quotes. |
| 754 | if ($c eq "'" || $c eq '"') { |
| 755 | if ($sanitise_quote eq '') { |
| 756 | $sanitise_quote = $c; |
| 757 | |
| 758 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 759 | next; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 760 | } elsif ($sanitise_quote eq $c) { |
| 761 | $sanitise_quote = ''; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 764 | |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 765 | #print "c<$c> SQ<$sanitise_quote>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 766 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 767 | substr($res, $off, 1, $;); |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 768 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 769 | substr($res, $off, 1, $;); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 770 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 771 | substr($res, $off, 1, 'X'); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 772 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 773 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 774 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 777 | if ($sanitise_quote eq '//') { |
| 778 | $sanitise_quote = ''; |
| 779 | } |
| 780 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 781 | # The pathname on a #include may be surrounded by '<' and '>'. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 782 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 783 | my $clean = 'X' x length($1); |
| 784 | $res =~ s@\<.*\>@<$clean>@; |
| 785 | |
| 786 | # The whole of a #error is a string. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 787 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 788 | my $clean = 'X' x length($1); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 789 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 792 | return $res; |
| 793 | } |
| 794 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 795 | sub get_quoted_string { |
| 796 | my ($line, $rawline) = @_; |
| 797 | |
| 798 | return "" if ($line !~ m/(\"[X]+\")/g); |
| 799 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 800 | } |
| 801 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 802 | sub ctx_statement_block { |
| 803 | my ($linenr, $remain, $off) = @_; |
| 804 | my $line = $linenr - 1; |
| 805 | my $blk = ''; |
| 806 | my $soff = $off; |
| 807 | my $coff = $off - 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 808 | my $coff_set = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 809 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 810 | my $loff = 0; |
| 811 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 812 | my $type = ''; |
| 813 | my $level = 0; |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 814 | my @stack = (); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 815 | my $p; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 816 | my $c; |
| 817 | my $len = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 818 | |
| 819 | my $remainder; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 820 | while (1) { |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 821 | @stack = (['', 0]) if ($#stack == -1); |
| 822 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 823 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 824 | # If we are about to drop off the end, pull in more |
| 825 | # context. |
| 826 | if ($off >= $len) { |
| 827 | for (; $remain > 0; $line++) { |
Andy Whitcroft | dea3349 | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 828 | last if (!defined $lines[$line]); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 829 | next if ($lines[$line] =~ /^-/); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 830 | $remain--; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 831 | $loff = $len; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 832 | $blk .= $lines[$line] . "\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 833 | $len = length($blk); |
| 834 | $line++; |
| 835 | last; |
| 836 | } |
| 837 | # Bail if there is no further context. |
| 838 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 839 | if ($off >= $len) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 840 | last; |
| 841 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 842 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { |
| 843 | $level++; |
| 844 | $type = '#'; |
| 845 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 846 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 847 | $p = $c; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 848 | $c = substr($blk, $off, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 849 | $remainder = substr($blk, $off); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 850 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 851 | #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] | 852 | |
| 853 | # Handle nested #if/#else. |
| 854 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 855 | push(@stack, [ $type, $level ]); |
| 856 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 857 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 858 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 859 | ($type, $level) = @{pop(@stack)}; |
| 860 | } |
| 861 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 862 | # Statement ends at the ';' or a close '}' at the |
| 863 | # outermost level. |
| 864 | if ($level == 0 && $c eq ';') { |
| 865 | last; |
| 866 | } |
| 867 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 868 | # 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] | 869 | if ($level == 0 && $coff_set == 0 && |
| 870 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 871 | $remainder =~ /^(else)(?:\s|{)/ && |
| 872 | $remainder !~ /^else\s+if\b/) { |
| 873 | $coff = $off + length($1) - 1; |
| 874 | $coff_set = 1; |
| 875 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 876 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 879 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 880 | $level++; |
| 881 | $type = '('; |
| 882 | } |
| 883 | if ($type eq '(' && $c eq ')') { |
| 884 | $level--; |
| 885 | $type = ($level != 0)? '(' : ''; |
| 886 | |
| 887 | if ($level == 0 && $coff < $soff) { |
| 888 | $coff = $off; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 889 | $coff_set = 1; |
| 890 | #warn "CSB: mark coff<$coff>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 894 | $level++; |
| 895 | $type = '{'; |
| 896 | } |
| 897 | if ($type eq '{' && $c eq '}') { |
| 898 | $level--; |
| 899 | $type = ($level != 0)? '{' : ''; |
| 900 | |
| 901 | if ($level == 0) { |
Patrick Pannuto | b998e00 | 2010-08-09 17:21:03 -0700 | [diff] [blame] | 902 | if (substr($blk, $off + 1, 1) eq ';') { |
| 903 | $off++; |
| 904 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 905 | last; |
| 906 | } |
| 907 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 908 | # Preprocessor commands end at the newline unless escaped. |
| 909 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { |
| 910 | $level--; |
| 911 | $type = ''; |
| 912 | $off++; |
| 913 | last; |
| 914 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 915 | $off++; |
| 916 | } |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 917 | # We are truly at the end, so shuffle to the next line. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 918 | if ($off == $len) { |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 919 | $loff = $len + 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 920 | $line++; |
| 921 | $remain--; |
| 922 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 923 | |
| 924 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 925 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 926 | |
| 927 | #warn "STATEMENT<$statement>\n"; |
| 928 | #warn "CONDITION<$condition>\n"; |
| 929 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 930 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 931 | |
| 932 | return ($statement, $condition, |
| 933 | $line, $remain + 1, $off - $loff + 1, $level); |
| 934 | } |
| 935 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 936 | sub statement_lines { |
| 937 | my ($stmt) = @_; |
| 938 | |
| 939 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 940 | $stmt =~ s/(^|\n)./$1/g; |
| 941 | $stmt =~ s/^\s*//; |
| 942 | $stmt =~ s/\s*$//; |
| 943 | |
| 944 | my @stmt_lines = ($stmt =~ /\n/g); |
| 945 | |
| 946 | return $#stmt_lines + 2; |
| 947 | } |
| 948 | |
| 949 | sub statement_rawlines { |
| 950 | my ($stmt) = @_; |
| 951 | |
| 952 | my @stmt_lines = ($stmt =~ /\n/g); |
| 953 | |
| 954 | return $#stmt_lines + 2; |
| 955 | } |
| 956 | |
| 957 | sub statement_block_size { |
| 958 | my ($stmt) = @_; |
| 959 | |
| 960 | $stmt =~ s/(^|\n)./$1/g; |
| 961 | $stmt =~ s/^\s*{//; |
| 962 | $stmt =~ s/}\s*$//; |
| 963 | $stmt =~ s/^\s*//; |
| 964 | $stmt =~ s/\s*$//; |
| 965 | |
| 966 | my @stmt_lines = ($stmt =~ /\n/g); |
| 967 | my @stmt_statements = ($stmt =~ /;/g); |
| 968 | |
| 969 | my $stmt_lines = $#stmt_lines + 2; |
| 970 | my $stmt_statements = $#stmt_statements + 1; |
| 971 | |
| 972 | if ($stmt_lines > $stmt_statements) { |
| 973 | return $stmt_lines; |
| 974 | } else { |
| 975 | return $stmt_statements; |
| 976 | } |
| 977 | } |
| 978 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 979 | sub ctx_statement_full { |
| 980 | my ($linenr, $remain, $off) = @_; |
| 981 | my ($statement, $condition, $level); |
| 982 | |
| 983 | my (@chunks); |
| 984 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 985 | # Grab the first conditional/block pair. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 986 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 987 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 988 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 989 | push(@chunks, [ $condition, $statement ]); |
| 990 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 991 | return ($level, $linenr, @chunks); |
| 992 | } |
| 993 | |
| 994 | # Pull in the following conditional/block pairs and see if they |
| 995 | # could continue the statement. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 996 | for (;;) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 997 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 998 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 999 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1000 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1001 | #print "C: push\n"; |
| 1002 | push(@chunks, [ $condition, $statement ]); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | return ($level, $linenr, @chunks); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1008 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1009 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1010 | my $line; |
| 1011 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1012 | my $blk = ''; |
| 1013 | my @o; |
| 1014 | my @c; |
| 1015 | my @res = (); |
| 1016 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1017 | my $level = 0; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1018 | my @stack = ($level); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1019 | for ($line = $start; $remain > 0; $line++) { |
| 1020 | next if ($rawlines[$line] =~ /^-/); |
| 1021 | $remain--; |
| 1022 | |
| 1023 | $blk .= $rawlines[$line]; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1024 | |
| 1025 | # Handle nested #if/#else. |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1026 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1027 | push(@stack, $level); |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1028 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1029 | $level = $stack[$#stack - 1]; |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1030 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 1031 | $level = pop(@stack); |
| 1032 | } |
| 1033 | |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 1034 | foreach my $c (split(//, $lines[$line])) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1035 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 1036 | if ($off > 0) { |
| 1037 | $off--; |
| 1038 | next; |
| 1039 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1040 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1041 | if ($c eq $close && $level > 0) { |
| 1042 | $level--; |
| 1043 | last if ($level == 0); |
| 1044 | } elsif ($c eq $open) { |
| 1045 | $level++; |
| 1046 | } |
| 1047 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1048 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1049 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1050 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1053 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1056 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1057 | } |
| 1058 | sub ctx_block_outer { |
| 1059 | my ($linenr, $remain) = @_; |
| 1060 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1061 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 1062 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1063 | } |
| 1064 | sub ctx_block { |
| 1065 | my ($linenr, $remain) = @_; |
| 1066 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1067 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 1068 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1069 | } |
| 1070 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1071 | my ($linenr, $remain, $off) = @_; |
| 1072 | |
| 1073 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1074 | return @r; |
| 1075 | } |
| 1076 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1077 | my ($linenr, $remain) = @_; |
| 1078 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1079 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1080 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1081 | sub ctx_statement_level { |
| 1082 | my ($linenr, $remain, $off) = @_; |
| 1083 | |
| 1084 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 1085 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1086 | |
| 1087 | sub ctx_locate_comment { |
| 1088 | my ($first_line, $end_line) = @_; |
| 1089 | |
| 1090 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | beae633 | 2008-07-23 21:28:59 -0700 | [diff] [blame] | 1091 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1092 | return $current_comment if (defined $current_comment); |
| 1093 | |
| 1094 | # Look through the context and try and figure out if there is a |
| 1095 | # comment. |
| 1096 | my $in_comment = 0; |
| 1097 | $current_comment = ''; |
| 1098 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1099 | my $line = $rawlines[$linenr - 1]; |
| 1100 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1101 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 1102 | $in_comment = 1; |
| 1103 | } |
| 1104 | if ($line =~ m@/\*@) { |
| 1105 | $in_comment = 1; |
| 1106 | } |
| 1107 | if (!$in_comment && $current_comment ne '') { |
| 1108 | $current_comment = ''; |
| 1109 | } |
| 1110 | $current_comment .= $line . "\n" if ($in_comment); |
| 1111 | if ($line =~ m@\*/@) { |
| 1112 | $in_comment = 0; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | chomp($current_comment); |
| 1117 | return($current_comment); |
| 1118 | } |
| 1119 | sub ctx_has_comment { |
| 1120 | my ($first_line, $end_line) = @_; |
| 1121 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 1122 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1123 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1124 | ##print "CMMT: $cmt\n"; |
| 1125 | |
| 1126 | return ($cmt ne ''); |
| 1127 | } |
| 1128 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1129 | sub raw_line { |
| 1130 | my ($linenr, $cnt) = @_; |
| 1131 | |
| 1132 | my $offset = $linenr - 1; |
| 1133 | $cnt++; |
| 1134 | |
| 1135 | my $line; |
| 1136 | while ($cnt) { |
| 1137 | $line = $rawlines[$offset++]; |
| 1138 | next if (defined($line) && $line =~ /^-/); |
| 1139 | $cnt--; |
| 1140 | } |
| 1141 | |
| 1142 | return $line; |
| 1143 | } |
| 1144 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1145 | sub cat_vet { |
| 1146 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1147 | my ($res, $coded); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1148 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1149 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1150 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 1151 | $res .= $1; |
| 1152 | if ($2 ne '') { |
| 1153 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 1154 | $res .= $coded; |
| 1155 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1156 | } |
| 1157 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1158 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1159 | return $res; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1162 | my $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1163 | my $av_pending; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1164 | my @av_paren_type; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1165 | my $av_pend_colon; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1166 | |
| 1167 | sub annotate_reset { |
| 1168 | $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1169 | $av_pending = '_'; |
| 1170 | @av_paren_type = ('E'); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1171 | $av_pend_colon = 'O'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1174 | sub annotate_values { |
| 1175 | my ($stream, $type) = @_; |
| 1176 | |
| 1177 | my $res; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1178 | my $var = '_' x length($stream); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1179 | my $cur = $stream; |
| 1180 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1181 | print "$stream\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1182 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1183 | while (length($cur)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1184 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1185 | print " <" . join('', @av_paren_type) . |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1186 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1187 | if ($cur =~ /^(\s+)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1188 | print "WS($1)\n" if ($dbg_values > 1); |
| 1189 | if ($1 =~ /\n/ && $av_preprocessor) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1190 | $type = pop(@av_paren_type); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1191 | $av_preprocessor = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Florian Mickler | c023e473 | 2011-01-12 16:59:58 -0800 | [diff] [blame] | 1194 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1195 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1196 | push(@av_paren_type, $type); |
Andy Whitcroft | addcdce | 2012-01-10 15:10:11 -0800 | [diff] [blame] | 1197 | $type = 'c'; |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 1198 | |
Andy Whitcroft | e91b6e2 | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1199 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1200 | print "DECLARE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1201 | $type = 'T'; |
| 1202 | |
Andy Whitcroft | 389a2fe | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 1203 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 1204 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 1205 | $type = 'T'; |
| 1206 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1207 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1208 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1209 | $av_preprocessor = 1; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1210 | push(@av_paren_type, $type); |
| 1211 | if ($2 ne '') { |
| 1212 | $av_pending = 'N'; |
| 1213 | } |
| 1214 | $type = 'E'; |
| 1215 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1216 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1217 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 1218 | $av_preprocessor = 1; |
| 1219 | push(@av_paren_type, $type); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1220 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1221 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1222 | print "PRE_START($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1223 | $av_preprocessor = 1; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1224 | |
| 1225 | push(@av_paren_type, $type); |
| 1226 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1227 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1228 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1229 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1230 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 1231 | $av_preprocessor = 1; |
| 1232 | |
| 1233 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 1234 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1235 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1236 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1237 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1238 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 1239 | |
| 1240 | $av_preprocessor = 1; |
| 1241 | |
| 1242 | # Assume all arms of the conditional end as this |
| 1243 | # one does, and continue as if the #endif was not here. |
| 1244 | pop(@av_paren_type); |
| 1245 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1246 | $type = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1247 | |
| 1248 | } elsif ($cur =~ /^(\\\n)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1249 | print "PRECONT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1250 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1251 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 1252 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 1253 | $av_pending = $type; |
| 1254 | $type = 'N'; |
| 1255 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1256 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1257 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1258 | if (defined $2) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1259 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1260 | } |
| 1261 | $type = 'N'; |
| 1262 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1263 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1264 | print "COND($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1265 | $av_pending = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1266 | $type = 'N'; |
| 1267 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1268 | } elsif ($cur =~/^(case)/o) { |
| 1269 | print "CASE($1)\n" if ($dbg_values > 1); |
| 1270 | $av_pend_colon = 'C'; |
| 1271 | $type = 'N'; |
| 1272 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1273 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1274 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1275 | $type = 'N'; |
| 1276 | |
| 1277 | } elsif ($cur =~ /^(\()/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1278 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1279 | push(@av_paren_type, $av_pending); |
| 1280 | $av_pending = '_'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1281 | $type = 'N'; |
| 1282 | |
| 1283 | } elsif ($cur =~ /^(\))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1284 | my $new_type = pop(@av_paren_type); |
| 1285 | if ($new_type ne '_') { |
| 1286 | $type = $new_type; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1287 | print "PAREN('$1') -> $type\n" |
| 1288 | if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1289 | } else { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1290 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1293 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1294 | print "FUNC($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 1295 | $type = 'V'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1296 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1297 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1298 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 1299 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1300 | $av_pend_colon = 'B'; |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1301 | } elsif ($type eq 'E') { |
| 1302 | $av_pend_colon = 'L'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1303 | } |
| 1304 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 1305 | $type = 'V'; |
| 1306 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1307 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1308 | print "IDENT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1309 | $type = 'V'; |
| 1310 | |
| 1311 | } elsif ($cur =~ /^($Assignment)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1312 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1313 | $type = 'N'; |
| 1314 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1315 | } elsif ($cur =~/^(;|{|})/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1316 | print "END($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1317 | $type = 'E'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1318 | $av_pend_colon = 'O'; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1319 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1320 | } elsif ($cur =~/^(,)/) { |
| 1321 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 1322 | $type = 'C'; |
| 1323 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1324 | } elsif ($cur =~ /^(\?)/o) { |
| 1325 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 1326 | $type = 'N'; |
| 1327 | |
| 1328 | } elsif ($cur =~ /^(:)/o) { |
| 1329 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 1330 | |
| 1331 | substr($var, length($res), 1, $av_pend_colon); |
| 1332 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 1333 | $type = 'E'; |
| 1334 | } else { |
| 1335 | $type = 'N'; |
| 1336 | } |
| 1337 | $av_pend_colon = 'O'; |
| 1338 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 1339 | } elsif ($cur =~ /^(\[)/o) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1340 | print "CLOSE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1341 | $type = 'N'; |
| 1342 | |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 1343 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1344 | my $variant; |
| 1345 | |
| 1346 | print "OPV($1)\n" if ($dbg_values > 1); |
| 1347 | if ($type eq 'V') { |
| 1348 | $variant = 'B'; |
| 1349 | } else { |
| 1350 | $variant = 'U'; |
| 1351 | } |
| 1352 | |
| 1353 | substr($var, length($res), 1, $variant); |
| 1354 | $type = 'N'; |
| 1355 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1356 | } elsif ($cur =~ /^($Operators)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1357 | print "OP($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1358 | if ($1 ne '++' && $1 ne '--') { |
| 1359 | $type = 'N'; |
| 1360 | } |
| 1361 | |
| 1362 | } elsif ($cur =~ /(^.)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1363 | print "C($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1364 | } |
| 1365 | if (defined $1) { |
| 1366 | $cur = substr($cur, length($1)); |
| 1367 | $res .= $type x length($1); |
| 1368 | } |
| 1369 | } |
| 1370 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1371 | return ($res, $var); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1374 | sub possible { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1375 | my ($possible, $line) = @_; |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1376 | my $notPermitted = qr{(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1377 | ^(?: |
| 1378 | $Modifier| |
| 1379 | $Storage| |
| 1380 | $Type| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1381 | DEFINE_\S+ |
| 1382 | )$| |
| 1383 | ^(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1384 | goto| |
| 1385 | return| |
| 1386 | case| |
| 1387 | else| |
| 1388 | asm|__asm__| |
Andy Whitcroft | 89a8835 | 2012-01-10 15:10:00 -0800 | [diff] [blame] | 1389 | do| |
| 1390 | \#| |
| 1391 | \#\#| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1392 | )(?:\s|$)| |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1393 | ^(?:typedef|struct|enum)\b |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1394 | )}x; |
| 1395 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 1396 | if ($possible !~ $notPermitted) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1397 | # Check for modifiers. |
| 1398 | $possible =~ s/\s*$Storage\s*//g; |
| 1399 | $possible =~ s/\s*$Sparse\s*//g; |
| 1400 | if ($possible =~ /^\s*$/) { |
| 1401 | |
| 1402 | } elsif ($possible =~ /\s/) { |
| 1403 | $possible =~ s/\s*$Type\s*//g; |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1404 | for my $modifier (split(' ', $possible)) { |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1405 | if ($modifier !~ $notPermitted) { |
| 1406 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
| 1407 | push(@modifierList, $modifier); |
| 1408 | } |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1409 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1410 | |
| 1411 | } else { |
| 1412 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
| 1413 | push(@typeList, $possible); |
| 1414 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1415 | build_types(); |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1416 | } else { |
| 1417 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1418 | } |
| 1419 | } |
| 1420 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1421 | my $prefix = ''; |
| 1422 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1423 | sub show_type { |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 1424 | return defined $use_type{$_[0]} if (scalar keys %use_type > 0); |
| 1425 | |
| 1426 | return !defined $ignore_type{$_[0]}; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1429 | sub report { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1430 | if (!show_type($_[1]) || |
| 1431 | (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1432 | return 0; |
| 1433 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1434 | my $line; |
| 1435 | if ($show_types) { |
| 1436 | $line = "$prefix$_[0]:$_[1]: $_[2]\n"; |
| 1437 | } else { |
| 1438 | $line = "$prefix$_[0]: $_[2]\n"; |
| 1439 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1440 | $line = (split('\n', $line))[0] . "\n" if ($terse); |
| 1441 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1442 | push(our @report, $line); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1443 | |
| 1444 | return 1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1445 | } |
| 1446 | sub report_dump { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1447 | our @report; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1448 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1449 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1450 | sub ERROR { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1451 | if (report("ERROR", $_[0], $_[1])) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1452 | our $clean = 0; |
| 1453 | our $cnt_error++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1454 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1455 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1456 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1457 | } |
| 1458 | sub WARN { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1459 | if (report("WARNING", $_[0], $_[1])) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1460 | our $clean = 0; |
| 1461 | our $cnt_warn++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1462 | return 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1463 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1464 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1465 | } |
| 1466 | sub CHK { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1467 | if ($check && report("CHECK", $_[0], $_[1])) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1468 | our $clean = 0; |
| 1469 | our $cnt_chk++; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1470 | return 1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1471 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1472 | return 0; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1475 | sub check_absolute_file { |
| 1476 | my ($absolute, $herecurr) = @_; |
| 1477 | my $file = $absolute; |
| 1478 | |
| 1479 | ##print "absolute<$absolute>\n"; |
| 1480 | |
| 1481 | # See if any suffix of this path is a path within the tree. |
| 1482 | while ($file =~ s@^[^/]*/@@) { |
| 1483 | if (-f "$root/$file") { |
| 1484 | ##print "file<$file>\n"; |
| 1485 | last; |
| 1486 | } |
| 1487 | } |
| 1488 | if (! -f _) { |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | # It is, so see if the prefix is acceptable. |
| 1493 | my $prefix = $absolute; |
| 1494 | substr($prefix, -length($file)) = ''; |
| 1495 | |
| 1496 | ##print "prefix<$prefix>\n"; |
| 1497 | if ($prefix ne ".../") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1498 | WARN("USE_RELATIVE_PATH", |
| 1499 | "use relative pathname instead of absolute in changelog text\n" . $herecurr); |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1500 | } |
| 1501 | } |
| 1502 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1503 | sub trim { |
| 1504 | my ($string) = @_; |
| 1505 | |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 1506 | $string =~ s/^\s+|\s+$//g; |
| 1507 | |
| 1508 | return $string; |
| 1509 | } |
| 1510 | |
| 1511 | sub ltrim { |
| 1512 | my ($string) = @_; |
| 1513 | |
| 1514 | $string =~ s/^\s+//; |
| 1515 | |
| 1516 | return $string; |
| 1517 | } |
| 1518 | |
| 1519 | sub rtrim { |
| 1520 | my ($string) = @_; |
| 1521 | |
| 1522 | $string =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1523 | |
| 1524 | return $string; |
| 1525 | } |
| 1526 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 1527 | sub string_find_replace { |
| 1528 | my ($string, $find, $replace) = @_; |
| 1529 | |
| 1530 | $string =~ s/$find/$replace/g; |
| 1531 | |
| 1532 | return $string; |
| 1533 | } |
| 1534 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1535 | sub tabify { |
| 1536 | my ($leading) = @_; |
| 1537 | |
| 1538 | my $source_indent = 8; |
| 1539 | my $max_spaces_before_tab = $source_indent - 1; |
| 1540 | my $spaces_to_tab = " " x $source_indent; |
| 1541 | |
| 1542 | #convert leading spaces to tabs |
| 1543 | 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; |
| 1544 | #Remove spaces before a tab |
| 1545 | 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; |
| 1546 | |
| 1547 | return "$leading"; |
| 1548 | } |
| 1549 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 1550 | sub pos_last_openparen { |
| 1551 | my ($line) = @_; |
| 1552 | |
| 1553 | my $pos = 0; |
| 1554 | |
| 1555 | my $opens = $line =~ tr/\(/\(/; |
| 1556 | my $closes = $line =~ tr/\)/\)/; |
| 1557 | |
| 1558 | my $last_openparen = 0; |
| 1559 | |
| 1560 | if (($opens == 0) || ($closes >= $opens)) { |
| 1561 | return -1; |
| 1562 | } |
| 1563 | |
| 1564 | my $len = length($line); |
| 1565 | |
| 1566 | for ($pos = 0; $pos < $len; $pos++) { |
| 1567 | my $string = substr($line, $pos); |
| 1568 | if ($string =~ /^($FuncArg|$balanced_parens)/) { |
| 1569 | $pos += length($1) - 1; |
| 1570 | } elsif (substr($line, $pos, 1) eq '(') { |
| 1571 | $last_openparen = $pos; |
| 1572 | } elsif (index($string, '(') == -1) { |
| 1573 | last; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | return $last_openparen + 1; |
| 1578 | } |
| 1579 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1580 | sub process { |
| 1581 | my $filename = shift; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1582 | |
| 1583 | my $linenr=0; |
| 1584 | my $prevline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1585 | my $prevrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1586 | my $stashline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1587 | my $stashrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1588 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1589 | my $length; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1590 | my $indent; |
| 1591 | my $previndent=0; |
| 1592 | my $stashindent=0; |
| 1593 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1594 | our $clean = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1595 | my $signoff = 0; |
| 1596 | my $is_patch = 0; |
| 1597 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1598 | my $in_header_lines = 1; |
| 1599 | my $in_commit_log = 0; #Scanning lines before patch |
| 1600 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1601 | my $non_utf8_charset = 0; |
| 1602 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1603 | our @report = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1604 | our $cnt_lines = 0; |
| 1605 | our $cnt_error = 0; |
| 1606 | our $cnt_warn = 0; |
| 1607 | our $cnt_chk = 0; |
| 1608 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1609 | # Trace the real file/line as we go. |
| 1610 | my $realfile = ''; |
| 1611 | my $realline = 0; |
| 1612 | my $realcnt = 0; |
| 1613 | my $here = ''; |
| 1614 | my $in_comment = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1615 | my $comment_edge = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1616 | my $first_line = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1617 | my $p1_prefix = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1618 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1619 | my $prev_values = 'E'; |
| 1620 | |
| 1621 | # suppression flags |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1622 | my %suppress_ifbraces; |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1623 | my %suppress_whiletrailers; |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1624 | my %suppress_export; |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1625 | my $suppress_statement = 0; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1626 | |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 1627 | my %signatures = (); |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 1628 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1629 | # Pre-scan the patch sanitizing the lines. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1630 | # Pre-scan the patch looking for any __setup documentation. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1631 | # |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1632 | my @setup_docs = (); |
| 1633 | my $setup_docs = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1634 | |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 1635 | my $camelcase_file_seeded = 0; |
| 1636 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1637 | sanitise_line_reset(); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1638 | my $line; |
| 1639 | foreach my $rawline (@rawlines) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1640 | $linenr++; |
| 1641 | $line = $rawline; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1642 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1643 | push(@fixed, $rawline) if ($fix); |
| 1644 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1645 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1646 | $setup_docs = 0; |
| 1647 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 1648 | $setup_docs = 1; |
| 1649 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1650 | #next; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1651 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1652 | if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
| 1653 | $realline=$1-1; |
| 1654 | if (defined $2) { |
| 1655 | $realcnt=$3+1; |
| 1656 | } else { |
| 1657 | $realcnt=1+1; |
| 1658 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1659 | $in_comment = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1660 | |
| 1661 | # Guestimate if this is a continuing comment. Run |
| 1662 | # the context looking for a comment "edge". If this |
| 1663 | # edge is a close comment then we must be in a comment |
| 1664 | # at context start. |
| 1665 | my $edge; |
Andy Whitcroft | 01fa914 | 2008-10-15 22:02:19 -0700 | [diff] [blame] | 1666 | my $cnt = $realcnt; |
| 1667 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 1668 | next if (defined $rawlines[$ln - 1] && |
| 1669 | $rawlines[$ln - 1] =~ /^-/); |
| 1670 | $cnt--; |
| 1671 | #print "RAW<$rawlines[$ln - 1]>\n"; |
Andy Whitcroft | 721c1cb | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 1672 | last if (!defined $rawlines[$ln - 1]); |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 1673 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 1674 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 1675 | ($edge) = $1; |
| 1676 | last; |
| 1677 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1678 | } |
| 1679 | if (defined $edge && $edge eq '*/') { |
| 1680 | $in_comment = 1; |
| 1681 | } |
| 1682 | |
| 1683 | # Guestimate if this is a continuing comment. If this |
| 1684 | # is the start of a diff block and this line starts |
| 1685 | # ' *' then it is very likely a comment. |
| 1686 | if (!defined $edge && |
Andy Whitcroft | 83242e0 | 2009-01-06 14:41:17 -0800 | [diff] [blame] | 1687 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1688 | { |
| 1689 | $in_comment = 1; |
| 1690 | } |
| 1691 | |
| 1692 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 1693 | sanitise_line_reset($in_comment); |
| 1694 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1695 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1696 | # Standardise the strings and chars within the input to |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1697 | # simplify matching -- only bother with positive lines. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1698 | $line = sanitise_line($rawline); |
| 1699 | } |
| 1700 | push(@lines, $line); |
| 1701 | |
| 1702 | if ($realcnt > 1) { |
| 1703 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 1704 | } else { |
| 1705 | $realcnt = 0; |
| 1706 | } |
| 1707 | |
| 1708 | #print "==>$rawline\n"; |
| 1709 | #print "-->$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1710 | |
| 1711 | if ($setup_docs && $line =~ /^\+/) { |
| 1712 | push(@setup_docs, $line); |
| 1713 | } |
| 1714 | } |
| 1715 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1716 | $prefix = ''; |
| 1717 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1718 | $realcnt = 0; |
| 1719 | $linenr = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1720 | foreach my $line (@lines) { |
| 1721 | $linenr++; |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 1722 | my $sline = $line; #copy of $line |
| 1723 | $sline =~ s/$;/ /g; #with comments as spaces |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1724 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1725 | my $rawline = $rawlines[$linenr - 1]; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1726 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1727 | #extract the line range in the file after the patch is applied |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1728 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1729 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1730 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1731 | $realline=$1-1; |
| 1732 | if (defined $2) { |
| 1733 | $realcnt=$3+1; |
| 1734 | } else { |
| 1735 | $realcnt=1+1; |
| 1736 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1737 | annotate_reset(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1738 | $prev_values = 'E'; |
| 1739 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1740 | %suppress_ifbraces = (); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1741 | %suppress_whiletrailers = (); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1742 | %suppress_export = (); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 1743 | $suppress_statement = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1744 | next; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1745 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1746 | # track the line number as we move through the hunk, note that |
| 1747 | # new versions of GNU diff omit the leading space on completely |
| 1748 | # blank context lines so we need to count that too. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1749 | } elsif ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1750 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1751 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1752 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1753 | # Measure the line length and indent. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1754 | ($length, $indent) = line_stats($rawline); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1755 | |
| 1756 | # Track the previous line. |
| 1757 | ($prevline, $stashline) = ($stashline, $line); |
| 1758 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1759 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 1760 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1761 | #warn "line<$line>\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1762 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1763 | } elsif ($realcnt == 1) { |
| 1764 | $realcnt--; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Andy Whitcroft | cc77cdc | 2009-10-26 16:50:13 -0700 | [diff] [blame] | 1767 | my $hunk_line = ($realcnt != 0); |
| 1768 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1769 | #make up the handle for any error we report on this line |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1770 | $prefix = "$filename:$realline: " if ($emacs && $file); |
| 1771 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
| 1772 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1773 | $here = "#$linenr: " if (!$file); |
| 1774 | $here = "#$realline: " if ($file); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1775 | |
| 1776 | # extract the filename as it passes |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1777 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 1778 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 1779 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1780 | $in_commit_log = 0; |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1781 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1782 | $realfile = $1; |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 1783 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1784 | $in_commit_log = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1785 | |
| 1786 | $p1_prefix = $1; |
Andy Whitcroft | e2f7aa4 | 2009-02-27 14:03:06 -0800 | [diff] [blame] | 1787 | if (!$file && $tree && $p1_prefix ne '' && |
| 1788 | -e "$root/$p1_prefix") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1789 | WARN("PATCH_PREFIX", |
| 1790 | "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1791 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1792 | |
Andy Whitcroft | c1ab332 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 1793 | if ($realfile =~ m@^include/asm/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1794 | ERROR("MODIFIED_INCLUDE_ASM", |
| 1795 | "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] | 1796 | } |
| 1797 | next; |
| 1798 | } |
| 1799 | |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 1800 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1801 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1802 | my $hereline = "$here\n$rawline\n"; |
| 1803 | my $herecurr = "$here\n$rawline\n"; |
| 1804 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1805 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1806 | $cnt_lines++ if ($realcnt != 0); |
| 1807 | |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1808 | # Check for incorrect file permissions |
| 1809 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { |
| 1810 | my $permhere = $here . "FILE: $realfile\n"; |
Joe Perches | 04db4d2 | 2013-04-29 16:18:14 -0700 | [diff] [blame] | 1811 | if ($realfile !~ m@scripts/@ && |
| 1812 | $realfile !~ /\.(py|pl|awk|sh)$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1813 | ERROR("EXECUTE_PERMISSIONS", |
| 1814 | "do not set execute permissions for source files\n" . $permhere); |
Rabin Vincent | 3bf9a00 | 2010-10-26 14:23:16 -0700 | [diff] [blame] | 1815 | } |
| 1816 | } |
| 1817 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1818 | # Check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1819 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1820 | $signoff++; |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1821 | $in_commit_log = 0; |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | # Check signature styles |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1825 | if (!$in_header_lines && |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 1826 | $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1827 | my $space_before = $1; |
| 1828 | my $sign_off = $2; |
| 1829 | my $space_after = $3; |
| 1830 | my $email = $4; |
| 1831 | my $ucfirst_sign_off = ucfirst(lc($sign_off)); |
| 1832 | |
Joe Perches | ce0338df3c | 2012-07-30 14:41:18 -0700 | [diff] [blame] | 1833 | if ($sign_off !~ /$signature_tags/) { |
| 1834 | WARN("BAD_SIGN_OFF", |
| 1835 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 1836 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1837 | if (defined $space_before && $space_before ne "") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1838 | if (WARN("BAD_SIGN_OFF", |
| 1839 | "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && |
| 1840 | $fix) { |
| 1841 | $fixed[$linenr - 1] = |
| 1842 | "$ucfirst_sign_off $email"; |
| 1843 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1844 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1845 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1846 | if (WARN("BAD_SIGN_OFF", |
| 1847 | "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && |
| 1848 | $fix) { |
| 1849 | $fixed[$linenr - 1] = |
| 1850 | "$ucfirst_sign_off $email"; |
| 1851 | } |
| 1852 | |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1853 | } |
| 1854 | if (!defined $space_after || $space_after ne " ") { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1855 | if (WARN("BAD_SIGN_OFF", |
| 1856 | "Use a single space after $ucfirst_sign_off\n" . $herecurr) && |
| 1857 | $fix) { |
| 1858 | $fixed[$linenr - 1] = |
| 1859 | "$ucfirst_sign_off $email"; |
| 1860 | } |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | my ($email_name, $email_address, $comment) = parse_email($email); |
| 1864 | my $suggested_email = format_email(($email_name, $email_address)); |
| 1865 | if ($suggested_email eq "") { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1866 | ERROR("BAD_SIGN_OFF", |
| 1867 | "Unrecognized email address: '$email'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1868 | } else { |
| 1869 | my $dequoted = $suggested_email; |
| 1870 | $dequoted =~ s/^"//; |
| 1871 | $dequoted =~ s/" </ </; |
| 1872 | # Don't force email to have quotes |
| 1873 | # Allow just an angle bracketed address |
| 1874 | if ("$dequoted$comment" ne $email && |
| 1875 | "<$email_address>$comment" ne $email && |
| 1876 | "$suggested_email$comment" ne $email) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1877 | WARN("BAD_SIGN_OFF", |
| 1878 | "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); |
Joe Perches | 2011247 | 2011-07-25 17:13:23 -0700 | [diff] [blame] | 1879 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1880 | } |
Joe Perches | 7e51f19 | 2013-09-11 14:23:57 -0700 | [diff] [blame] | 1881 | |
| 1882 | # Check for duplicate signatures |
| 1883 | my $sig_nospace = $line; |
| 1884 | $sig_nospace =~ s/\s//g; |
| 1885 | $sig_nospace = lc($sig_nospace); |
| 1886 | if (defined $signatures{$sig_nospace}) { |
| 1887 | WARN("BAD_SIGN_OFF", |
| 1888 | "Duplicate signature\n" . $herecurr); |
| 1889 | } else { |
| 1890 | $signatures{$sig_nospace} = 1; |
| 1891 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1894 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1895 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 1896 | ERROR("CORRUPTED_PATCH", |
| 1897 | "patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1898 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1901 | # Check for absolute kernel paths. |
| 1902 | if ($tree) { |
| 1903 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 1904 | my $file = $1; |
| 1905 | |
| 1906 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 1907 | check_absolute_file($1, $herecurr)) { |
| 1908 | # |
| 1909 | } else { |
| 1910 | check_absolute_file($file, $herecurr); |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1915 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 1916 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1917 | $rawline !~ m/^$UTF8*$/) { |
| 1918 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 1919 | |
| 1920 | my $blank = copy_spacing($rawline); |
| 1921 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 1922 | my $hereptr = "$hereline$ptr\n"; |
| 1923 | |
Joe Perches | 34d9921 | 2011-07-25 17:13:26 -0700 | [diff] [blame] | 1924 | CHK("INVALID_UTF8", |
| 1925 | "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] | 1926 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1927 | |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1928 | # Check if it's the start of a commit log |
| 1929 | # (not a header line and we haven't seen the patch filename) |
| 1930 | if ($in_header_lines && $realfile =~ /^$/ && |
Joe Perches | 270c49a | 2012-01-10 15:09:50 -0800 | [diff] [blame] | 1931 | $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1932 | $in_header_lines = 0; |
| 1933 | $in_commit_log = 1; |
| 1934 | } |
| 1935 | |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1936 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
| 1937 | # declined it, i.e defined some charset where it is missing. |
| 1938 | if ($in_header_lines && |
| 1939 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && |
| 1940 | $1 !~ /utf-8/i) { |
| 1941 | $non_utf8_charset = 1; |
| 1942 | } |
| 1943 | |
| 1944 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1945 | $rawline =~ /$NON_ASCII_UTF8/) { |
Pasi Savanainen | fa64205d | 2012-10-04 17:13:29 -0700 | [diff] [blame] | 1946 | WARN("UTF8_BEFORE_PATCH", |
Joe Perches | 15662b3 | 2011-10-31 17:13:12 -0700 | [diff] [blame] | 1947 | "8-bit UTF-8 used in possible commit log\n" . $herecurr); |
| 1948 | } |
| 1949 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 1950 | # ignore non-hunk lines and lines being removed |
| 1951 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1952 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1953 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1954 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1955 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 1956 | if (ERROR("DOS_LINE_ENDINGS", |
| 1957 | "DOS line endings\n" . $herevet) && |
| 1958 | $fix) { |
| 1959 | $fixed[$linenr - 1] =~ s/[\s\015]+$//; |
| 1960 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1961 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 1962 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1963 | if (ERROR("TRAILING_WHITESPACE", |
| 1964 | "trailing whitespace\n" . $herevet) && |
| 1965 | $fix) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 1966 | $fixed[$linenr - 1] =~ s/\s+$//; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 1969 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1970 | } |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1971 | |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 1972 | # Check for FSF mailing addresses. |
Alexander Duyck | 109d8cb | 2014-01-23 15:54:50 -0800 | [diff] [blame] | 1973 | if ($rawline =~ /\bwrite to the Free/i || |
Joe Perches | 3e2232f | 2014-01-23 15:54:48 -0800 | [diff] [blame] | 1974 | $rawline =~ /\b59\s+Temple\s+Pl/i || |
| 1975 | $rawline =~ /\b51\s+Franklin\s+St/i) { |
Josh Triplett | 4783f89 | 2013-11-12 15:10:12 -0800 | [diff] [blame] | 1976 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
| 1977 | my $msg_type = \&ERROR; |
| 1978 | $msg_type = \&CHK if ($file); |
| 1979 | &{$msg_type}("FSF_MAILING_ADDRESS", |
Joe Perches | 3e2232f | 2014-01-23 15:54:48 -0800 | [diff] [blame] | 1980 | "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] | 1981 | } |
| 1982 | |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1983 | # check for Kconfig help text having a real description |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1984 | # Only applies when adding the entry originally, after that we do not have |
| 1985 | # sufficient context to determine whether it is indeed long enough. |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1986 | if ($realfile =~ /Kconfig/ && |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1987 | $line =~ /.\s*config\s+/) { |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1988 | my $length = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1989 | my $cnt = $realcnt; |
| 1990 | my $ln = $linenr + 1; |
| 1991 | my $f; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1992 | my $is_start = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1993 | my $is_end = 0; |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 1994 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1995 | $f = $lines[$ln - 1]; |
| 1996 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 1997 | $is_end = $lines[$ln - 1] =~ /^\+/; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1998 | |
| 1999 | next if ($f =~ /^-/); |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2000 | |
| 2001 | if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { |
| 2002 | $is_start = 1; |
| 2003 | } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { |
| 2004 | $length = -1; |
| 2005 | } |
| 2006 | |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2007 | $f =~ s/^.//; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2008 | $f =~ s/#.*//; |
| 2009 | $f =~ s/^\s+//; |
| 2010 | next if ($f =~ /^$/); |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2011 | if ($f =~ /^\s*config\s/) { |
| 2012 | $is_end = 1; |
| 2013 | last; |
| 2014 | } |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2015 | $length++; |
| 2016 | } |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2017 | WARN("CONFIG_DESCRIPTION", |
Andy Whitcroft | a138580 | 2012-01-10 15:10:03 -0800 | [diff] [blame] | 2018 | "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); |
| 2019 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 2022 | # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. |
| 2023 | if ($realfile =~ /Kconfig/ && |
| 2024 | $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { |
| 2025 | WARN("CONFIG_EXPERIMENTAL", |
| 2026 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 2027 | } |
| 2028 | |
Arnaud Lacombe | c68e587 | 2011-08-15 01:07:14 -0400 | [diff] [blame] | 2029 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| 2030 | ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { |
| 2031 | my $flag = $1; |
| 2032 | my $replacement = { |
| 2033 | 'EXTRA_AFLAGS' => 'asflags-y', |
| 2034 | 'EXTRA_CFLAGS' => 'ccflags-y', |
| 2035 | 'EXTRA_CPPFLAGS' => 'cppflags-y', |
| 2036 | 'EXTRA_LDFLAGS' => 'ldflags-y', |
| 2037 | }; |
| 2038 | |
| 2039 | WARN("DEPRECATED_VARIABLE", |
| 2040 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
| 2041 | } |
| 2042 | |
Rob Herring | bff5da4 | 2014-01-23 15:54:51 -0800 | [diff] [blame] | 2043 | # check for DT compatible documentation |
| 2044 | if (defined $root && $realfile =~ /\.dts/ && |
| 2045 | $rawline =~ /^\+\s*compatible\s*=/) { |
| 2046 | my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; |
| 2047 | |
| 2048 | foreach my $compat (@compats) { |
| 2049 | my $compat2 = $compat; |
| 2050 | my $dt_path = $root . "/Documentation/devicetree/bindings/"; |
| 2051 | $compat2 =~ s/\,[a-z]*\-/\,<\.\*>\-/; |
| 2052 | `grep -Erq "$compat|$compat2" $dt_path`; |
| 2053 | if ( $? >> 8 ) { |
| 2054 | WARN("UNDOCUMENTED_DT_STRING", |
| 2055 | "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); |
| 2056 | } |
| 2057 | |
| 2058 | my $vendor = $compat; |
| 2059 | my $vendor_path = $dt_path . "vendor-prefixes.txt"; |
| 2060 | next if (! -f $vendor_path); |
| 2061 | $vendor =~ s/^([a-zA-Z0-9]+)\,.*/$1/; |
| 2062 | `grep -Eq "$vendor" $vendor_path`; |
| 2063 | if ( $? >> 8 ) { |
| 2064 | WARN("UNDOCUMENTED_DT_STRING", |
| 2065 | "DT compatible string vendor \"$vendor\" appears un-documented -- check $vendor_path\n" . $herecurr); |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2070 | # check we are in a valid source file if not then ignore this hunk |
| 2071 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
| 2072 | |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2073 | #line length limit |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2074 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2075 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
Joe Perches | 0fccc62 | 2011-05-24 17:13:41 -0700 | [diff] [blame] | 2076 | !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || |
Joe Perches | 8bbea96 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 2077 | $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2078 | $length > $max_line_length) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2079 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2080 | WARN("LONG_LINE", |
Joe Perches | 6cd7f38 | 2012-12-17 16:01:54 -0800 | [diff] [blame] | 2081 | "line over $max_line_length characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Josh Triplett | ca56dc0 | 2012-03-23 15:02:21 -0700 | [diff] [blame] | 2084 | # Check for user-visible strings broken across lines, which breaks the ability |
Joe Perches | 8c5fcd2 | 2014-01-23 15:54:40 -0800 | [diff] [blame] | 2085 | # to grep for the string. Make exceptions when the previous string ends in a |
| 2086 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' |
| 2087 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value |
Josh Triplett | ca56dc0 | 2012-03-23 15:02:21 -0700 | [diff] [blame] | 2088 | if ($line =~ /^\+\s*"/ && |
| 2089 | $prevline =~ /"\s*$/ && |
Joe Perches | 8c5fcd2 | 2014-01-23 15:54:40 -0800 | [diff] [blame] | 2090 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { |
Josh Triplett | ca56dc0 | 2012-03-23 15:02:21 -0700 | [diff] [blame] | 2091 | WARN("SPLIT_STRING", |
| 2092 | "quoted string split across lines\n" . $hereprev); |
| 2093 | } |
| 2094 | |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 2095 | # check for spaces before a quoted newline |
| 2096 | if ($rawline =~ /^.*\".*\s\\n/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2097 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
| 2098 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && |
| 2099 | $fix) { |
| 2100 | $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; |
| 2101 | } |
| 2102 | |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 2103 | } |
| 2104 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2105 | # check for adding lines without a newline. |
| 2106 | 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] | 2107 | WARN("MISSING_EOF_NEWLINE", |
| 2108 | "adding a line without newline at end of file\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2109 | } |
| 2110 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2111 | # Blackfin: use hi/lo macros |
| 2112 | if ($realfile =~ m@arch/blackfin/.*\.S$@) { |
| 2113 | if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { |
| 2114 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2115 | ERROR("LO_MACRO", |
| 2116 | "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2117 | } |
| 2118 | if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { |
| 2119 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2120 | ERROR("HI_MACRO", |
| 2121 | "use the HI() macro, not (... >> 16)\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2122 | } |
| 2123 | } |
| 2124 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2125 | # check we are in a valid source file C or perl if not then ignore this hunk |
| 2126 | next if ($realfile !~ /\.(h|c|pl)$/); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2127 | |
| 2128 | # at the beginning of a line any tabs must come first and anything |
| 2129 | # more than 8 must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2130 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 2131 | $rawline =~ /^\+\s* \s*/) { |
| 2132 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2133 | $rpt_cleaners = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2134 | if (ERROR("CODE_INDENT", |
| 2135 | "code indent should use tabs where possible\n" . $herevet) && |
| 2136 | $fix) { |
| 2137 | $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
| 2138 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2141 | # check for space before tabs. |
| 2142 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 2143 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2144 | if (WARN("SPACE_BEFORE_TAB", |
| 2145 | "please, no space before tabs\n" . $herevet) && |
| 2146 | $fix) { |
Joe Perches | c76f4cb | 2014-01-23 15:54:46 -0800 | [diff] [blame] | 2147 | while ($fixed[$linenr - 1] =~ |
| 2148 | s/(^\+.*) {8,8}+\t/$1\t\t/) {} |
| 2149 | while ($fixed[$linenr - 1] =~ |
| 2150 | s/(^\+.*) +\t/$1\t/) {} |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2151 | } |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 2152 | } |
| 2153 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2154 | # check for && or || at the start of a line |
| 2155 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { |
| 2156 | CHK("LOGICAL_CONTINUATIONS", |
| 2157 | "Logical continuations should be on the previous line\n" . $hereprev); |
| 2158 | } |
| 2159 | |
| 2160 | # check multi-line statement indentation matches previous line |
| 2161 | if ($^V && $^V ge 5.10.0 && |
| 2162 | $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { |
| 2163 | $prevline =~ /^\+(\t*)(.*)$/; |
| 2164 | my $oldindent = $1; |
| 2165 | my $rest = $2; |
| 2166 | |
| 2167 | my $pos = pos_last_openparen($rest); |
| 2168 | if ($pos >= 0) { |
Joe Perches | b34a26f | 2012-07-30 14:41:16 -0700 | [diff] [blame] | 2169 | $line =~ /^(\+| )([ \t]*)/; |
| 2170 | my $newindent = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2171 | |
| 2172 | my $goodtabindent = $oldindent . |
| 2173 | "\t" x ($pos / 8) . |
| 2174 | " " x ($pos % 8); |
| 2175 | my $goodspaceindent = $oldindent . " " x $pos; |
| 2176 | |
| 2177 | if ($newindent ne $goodtabindent && |
| 2178 | $newindent ne $goodspaceindent) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2179 | |
| 2180 | if (CHK("PARENTHESIS_ALIGNMENT", |
| 2181 | "Alignment should match open parenthesis\n" . $hereprev) && |
| 2182 | $fix && $line =~ /^\+/) { |
| 2183 | $fixed[$linenr - 1] =~ |
| 2184 | s/^\+[ \t]*/\+$goodtabindent/; |
| 2185 | } |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | } |
| 2189 | |
Joe Perches | 23f780c | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2190 | if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2191 | if (CHK("SPACING", |
| 2192 | "No space is necessary after a cast\n" . $hereprev) && |
| 2193 | $fix) { |
| 2194 | $fixed[$linenr - 1] =~ |
| 2195 | s/^(\+.*\*[ \t]*\))[ \t]+/$1/; |
| 2196 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2199 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | fdb4bcd | 2013-07-03 15:05:23 -0700 | [diff] [blame] | 2200 | $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && |
| 2201 | $rawline =~ /^\+[ \t]*\*/) { |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2202 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2203 | "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); |
| 2204 | } |
| 2205 | |
| 2206 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2207 | $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* |
| 2208 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ |
Joe Perches | 61135e9 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 2209 | $rawline =~ /^\+/ && #line is new |
Joe Perches | a605e32 | 2013-07-03 15:05:24 -0700 | [diff] [blame] | 2210 | $rawline !~ /^\+[ \t]*\*/) { #no leading * |
| 2211 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2212 | "networking block comments start with * on subsequent lines\n" . $hereprev); |
| 2213 | } |
| 2214 | |
| 2215 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
Joe Perches | c24f9f1 | 2012-11-08 15:53:29 -0800 | [diff] [blame] | 2216 | $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ |
| 2217 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ |
| 2218 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ |
| 2219 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ |
Joe Perches | 0588060 | 2012-10-04 17:13:35 -0700 | [diff] [blame] | 2220 | WARN("NETWORKING_BLOCK_COMMENT_STYLE", |
| 2221 | "networking block comments put the trailing */ on a separate line\n" . $herecurr); |
| 2222 | } |
| 2223 | |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2224 | # check for spaces at the beginning of a line. |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 2225 | # Exceptions: |
| 2226 | # 1) within comments |
| 2227 | # 2) indented preprocessor commands |
| 2228 | # 3) hanging labels |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2229 | if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2230 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2231 | if (WARN("LEADING_SPACE", |
| 2232 | "please, no spaces at the start of a line\n" . $herevet) && |
| 2233 | $fix) { |
| 2234 | $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; |
| 2235 | } |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 2238 | # check we are in a valid C source file if not then ignore this hunk |
| 2239 | next if ($realfile !~ /\.(h|c)$/); |
| 2240 | |
Kees Cook | 1ba8dfd | 2012-12-17 16:01:48 -0800 | [diff] [blame] | 2241 | # discourage the addition of CONFIG_EXPERIMENTAL in #if(def). |
| 2242 | if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { |
| 2243 | WARN("CONFIG_EXPERIMENTAL", |
| 2244 | "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); |
| 2245 | } |
| 2246 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2247 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2248 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2249 | WARN("CVS_KEYWORD", |
| 2250 | "CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2251 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2252 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2253 | # Blackfin: don't use __builtin_bfin_[cs]sync |
| 2254 | if ($line =~ /__builtin_bfin_csync/) { |
| 2255 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2256 | ERROR("CSYNC", |
| 2257 | "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2258 | } |
| 2259 | if ($line =~ /__builtin_bfin_ssync/) { |
| 2260 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2261 | ERROR("SSYNC", |
| 2262 | "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
Joe Perches | 56e77d7 | 2013-02-21 16:44:14 -0800 | [diff] [blame] | 2265 | # check for old HOTPLUG __dev<foo> section markings |
| 2266 | if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { |
| 2267 | WARN("HOTPLUG_SECTION", |
| 2268 | "Using $1 is unnecessary\n" . $herecurr); |
| 2269 | } |
| 2270 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2271 | # Check for potential 'bare' types |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2272 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 2273 | $realline_next); |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2274 | #print "LINE<$line>\n"; |
| 2275 | if ($linenr >= $suppress_statement && |
Joe Perches | 1b5539b | 2013-09-11 14:24:03 -0700 | [diff] [blame] | 2276 | $realcnt && $sline =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2277 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2278 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2279 | $stat =~ s/\n./\n /g; |
| 2280 | $cond =~ s/\n./\n /g; |
| 2281 | |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2282 | #print "linenr<$linenr> <$stat>\n"; |
| 2283 | # If this statement has no statement boundaries within |
| 2284 | # it there is no point in retrying a statement scan |
| 2285 | # until we hit end of it. |
| 2286 | my $frag = $stat; $frag =~ s/;+\s*$//; |
| 2287 | if ($frag !~ /(?:{|;)/) { |
| 2288 | #print "skip<$line_nr_next>\n"; |
| 2289 | $suppress_statement = $line_nr_next; |
| 2290 | } |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 2291 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2292 | # Find the real next line. |
| 2293 | $realline_next = $line_nr_next; |
| 2294 | if (defined $realline_next && |
| 2295 | (!defined $lines[$realline_next - 1] || |
| 2296 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 2297 | $realline_next++; |
| 2298 | } |
| 2299 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2300 | my $s = $stat; |
| 2301 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2302 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2303 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2304 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2305 | |
| 2306 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2307 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2308 | |
Andy Whitcroft | 463f286 | 2009-09-21 17:04:34 -0700 | [diff] [blame] | 2309 | } elsif ($s =~ /^.\s*else\b/s) { |
| 2310 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2311 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 2312 | } 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] | 2313 | my $type = $1; |
| 2314 | $type =~ s/\s+/ /g; |
| 2315 | possible($type, "A:" . $s); |
| 2316 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2317 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a84062 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2318 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2319 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2320 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2321 | |
| 2322 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2323 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2324 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2325 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2326 | |
| 2327 | # Check for any sort of function declaration. |
| 2328 | # int foo(something bar, other baz); |
| 2329 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2330 | 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] | 2331 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2332 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2333 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2334 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2335 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2336 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2337 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2338 | 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] | 2339 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2340 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2347 | # |
| 2348 | # Checks which may be anchored in the context. |
| 2349 | # |
| 2350 | |
| 2351 | # Check for switch () and associated case and default |
| 2352 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2353 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 2354 | my $err = ''; |
| 2355 | my $sep = ''; |
| 2356 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 2357 | shift(@ctx); |
| 2358 | for my $ctx (@ctx) { |
| 2359 | my ($clen, $cindent) = line_stats($ctx); |
| 2360 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 2361 | $indent != $cindent) { |
| 2362 | $err .= "$sep$ctx\n"; |
| 2363 | $sep = ''; |
| 2364 | } else { |
| 2365 | $sep = "[...]\n"; |
| 2366 | } |
| 2367 | } |
| 2368 | if ($err ne '') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2369 | ERROR("SWITCH_CASE_INDENT_LEVEL", |
| 2370 | "switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 2375 | # or if that brace on the next line is for something else |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2376 | if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2377 | my $pre_ctx = "$1$2"; |
| 2378 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2379 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Joe Perches | 8eef05d | 2012-02-03 15:20:39 -0800 | [diff] [blame] | 2380 | |
| 2381 | if ($line =~ /^\+\t{6,}/) { |
| 2382 | WARN("DEEP_INDENTATION", |
| 2383 | "Too many leading tabs - consider code refactoring\n" . $herecurr); |
| 2384 | } |
| 2385 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2386 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 2387 | my $ctx = join("\n", @ctx); |
| 2388 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2389 | my $ctx_ln = $linenr; |
| 2390 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2391 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2392 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 2393 | defined $lines[$ctx_ln - 1] && |
| 2394 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 2395 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 2396 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2397 | $ctx_ln++; |
| 2398 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 2399 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2400 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 2401 | #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] | 2402 | |
| 2403 | 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] | 2404 | ERROR("OPEN_BRACE", |
| 2405 | "that open brace { should be on the previous line\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2406 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2407 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2408 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 2409 | $ctx =~ /\)\s*\;\s*$/ && |
| 2410 | defined $lines[$ctx_ln - 1]) |
| 2411 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2412 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 2413 | if ($nindent > $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2414 | WARN("TRAILING_SEMICOLON", |
| 2415 | "trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | 01464f3 | 2010-10-26 14:23:19 -0700 | [diff] [blame] | 2416 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2417 | } |
| 2418 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2419 | } |
| 2420 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2421 | # Check relative indent for conditionals and blocks. |
| 2422 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 2423 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 2424 | ctx_statement_block($linenr, $realcnt, 0) |
| 2425 | if (!defined $stat); |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2426 | my ($s, $c) = ($stat, $cond); |
| 2427 | |
| 2428 | substr($s, 0, length($c), ''); |
| 2429 | |
| 2430 | # Make sure we remove the line prefixes as we have |
| 2431 | # none on the first line, and are going to readd them |
| 2432 | # where necessary. |
| 2433 | $s =~ s/\n./\n/gs; |
| 2434 | |
| 2435 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 2436 | my @newlines = ($c =~ /\n/gs); |
| 2437 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2438 | |
| 2439 | # We want to check the first line inside the block |
| 2440 | # starting at the end of the conditional, so remove: |
| 2441 | # 1) any blank line termination |
| 2442 | # 2) any opening brace { on end of the line |
| 2443 | # 3) any do (...) { |
| 2444 | my $continuation = 0; |
| 2445 | my $check = 0; |
| 2446 | $s =~ s/^.*\bdo\b//; |
| 2447 | $s =~ s/^\s*{//; |
| 2448 | if ($s =~ s/^\s*\\//) { |
| 2449 | $continuation = 1; |
| 2450 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2451 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2452 | $check = 1; |
| 2453 | $cond_lines++; |
| 2454 | } |
| 2455 | |
| 2456 | # Also ignore a loop construct at the end of a |
| 2457 | # preprocessor statement. |
| 2458 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 2459 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 2460 | $check = 0; |
| 2461 | } |
| 2462 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2463 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2464 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2465 | while ($cond_ptr != $cond_lines) { |
| 2466 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2467 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2468 | # If we see an #else/#elif then the code |
| 2469 | # is not linear. |
| 2470 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 2471 | $check = 0; |
| 2472 | } |
| 2473 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2474 | # Ignore: |
| 2475 | # 1) blank lines, they should be at 0, |
| 2476 | # 2) preprocessor lines, and |
| 2477 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2478 | if ($continuation || |
| 2479 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2480 | $s =~ /^\s*#\s*?/ || |
| 2481 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 2482 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 30dad6e | 2009-09-21 17:04:36 -0700 | [diff] [blame] | 2483 | if ($s =~ s/^.*?\n//) { |
| 2484 | $cond_lines++; |
| 2485 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2486 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | my (undef, $sindent) = line_stats("+" . $s); |
| 2490 | my $stat_real = raw_line($linenr, $cond_lines); |
| 2491 | |
| 2492 | # Check if either of these lines are modified, else |
| 2493 | # this is not this patch's fault. |
| 2494 | if (!defined($stat_real) || |
| 2495 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 2496 | $check = 0; |
| 2497 | } |
| 2498 | if (defined($stat_real) && $cond_lines > 1) { |
| 2499 | $stat_real = "[...]\n$stat_real"; |
| 2500 | } |
| 2501 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 2502 | #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] | 2503 | |
| 2504 | if ($check && (($sindent % 8) != 0 || |
| 2505 | ($sindent <= $indent && $s ne ''))) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2506 | WARN("SUSPECT_CODE_INDENT", |
| 2507 | "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] | 2508 | } |
| 2509 | } |
| 2510 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2511 | # Track the 'values' across context and added lines. |
| 2512 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2513 | my ($curr_values, $curr_vars) = |
| 2514 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2515 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2516 | if ($dbg_values) { |
| 2517 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2518 | print "$linenr > .$outline\n"; |
| 2519 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2520 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2521 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2522 | $prev_values = substr($curr_values, -1); |
| 2523 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2524 | #ignore lines not being added |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2525 | next if ($line =~ /^[^\+]/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2526 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2527 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2528 | if ($dbg_type) { |
| 2529 | if ($line =~ /^.\s*$Declare\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2530 | ERROR("TEST_TYPE", |
| 2531 | "TEST: is type\n" . $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2532 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2533 | ERROR("TEST_NOT_TYPE", |
| 2534 | "TEST: is not type ($1 is)\n". $herecurr); |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 2535 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2536 | next; |
| 2537 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2538 | # TEST: allow direct testing of the attribute matcher. |
| 2539 | if ($dbg_attr) { |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2540 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2541 | ERROR("TEST_ATTR", |
| 2542 | "TEST: is attr\n" . $herecurr); |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2543 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2544 | ERROR("TEST_NOT_ATTR", |
| 2545 | "TEST: is not attr ($1 is)\n". $herecurr); |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 2546 | } |
| 2547 | next; |
| 2548 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2549 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2550 | # check for initialisation to aggregates open brace on the next line |
Andy Whitcroft | 99423c2 | 2009-10-26 16:50:15 -0700 | [diff] [blame] | 2551 | if ($line =~ /^.\s*{/ && |
| 2552 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2553 | ERROR("OPEN_BRACE", |
| 2554 | "that open brace { should be on the previous line\n" . $hereprev); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2557 | # |
| 2558 | # Checks which are anchored on the added line. |
| 2559 | # |
| 2560 | |
| 2561 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2562 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2563 | my $path = $1; |
| 2564 | if ($path =~ m{//}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2565 | ERROR("MALFORMED_INCLUDE", |
Joe Perches | 495e9d8 | 2012-12-20 15:05:37 -0800 | [diff] [blame] | 2566 | "malformed #include filename\n" . $herecurr); |
| 2567 | } |
| 2568 | if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { |
| 2569 | ERROR("UAPI_INCLUDE", |
| 2570 | "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] | 2571 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2572 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2573 | |
| 2574 | # no C99 // comments |
| 2575 | if ($line =~ m{//}) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2576 | if (ERROR("C99_COMMENTS", |
| 2577 | "do not use C99 // comments\n" . $herecurr) && |
| 2578 | $fix) { |
| 2579 | my $line = $fixed[$linenr - 1]; |
| 2580 | if ($line =~ /\/\/(.*)$/) { |
| 2581 | my $comment = trim($1); |
| 2582 | $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@; |
| 2583 | } |
| 2584 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2585 | } |
| 2586 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2587 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2588 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2589 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2590 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 2591 | # the whole statement. |
| 2592 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 2593 | if (defined $realline_next && |
| 2594 | exists $lines[$realline_next - 1] && |
| 2595 | !defined $suppress_export{$realline_next} && |
| 2596 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2597 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2598 | # Handle definitions which produce identifiers with |
| 2599 | # a prefix: |
| 2600 | # XXX(foo); |
| 2601 | # EXPORT_SYMBOL(something_foo); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2602 | my $name = $1; |
Andy Whitcroft | 87a5387 | 2012-01-10 15:10:04 -0800 | [diff] [blame] | 2603 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
Andy Whitcroft | 3cbf62d | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 2604 | $name =~ /^${Ident}_$2/) { |
| 2605 | #print "FOO C name<$name>\n"; |
| 2606 | $suppress_export{$realline_next} = 1; |
| 2607 | |
| 2608 | } elsif ($stat !~ /(?: |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2609 | \n.}\s*$| |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2610 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 2611 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 2612 | ^.LIST_HEAD\(\Q$name\E\)| |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2613 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 2614 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2615 | )/x) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2616 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 2617 | $suppress_export{$realline_next} = 2; |
| 2618 | } else { |
| 2619 | $suppress_export{$realline_next} = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2620 | } |
| 2621 | } |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2622 | if (!defined $suppress_export{$linenr} && |
| 2623 | $prevline =~ /^.\s*$/ && |
| 2624 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 2625 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 2626 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 2627 | $suppress_export{$linenr} = 2; |
| 2628 | } |
| 2629 | if (defined $suppress_export{$linenr} && |
| 2630 | $suppress_export{$linenr} == 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2631 | WARN("EXPORT_SYMBOL", |
| 2632 | "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 2633 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2634 | |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 2635 | # check for global initialisers. |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2636 | if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) { |
| 2637 | if (ERROR("GLOBAL_INITIALISERS", |
| 2638 | "do not initialise globals to 0 or NULL\n" . |
| 2639 | $herecurr) && |
| 2640 | $fix) { |
| 2641 | $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/; |
| 2642 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2643 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2644 | # check for static initialisers. |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2645 | if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { |
| 2646 | if (ERROR("INITIALISED_STATIC", |
| 2647 | "do not initialise statics to 0 or NULL\n" . |
| 2648 | $herecurr) && |
| 2649 | $fix) { |
| 2650 | $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; |
| 2651 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2652 | } |
| 2653 | |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2654 | # check for static const char * arrays. |
| 2655 | 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] | 2656 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2657 | "static const char * array should probably be static const char * const\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2658 | $herecurr); |
| 2659 | } |
| 2660 | |
| 2661 | # check for static char foo[] = "bar" declarations. |
| 2662 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2663 | WARN("STATIC_CONST_CHAR_ARRAY", |
| 2664 | "static char array declaration should probably be static const char\n" . |
Joe Perches | cb710ec | 2010-10-26 14:23:20 -0700 | [diff] [blame] | 2665 | $herecurr); |
| 2666 | } |
| 2667 | |
Joe Perches | b36190c | 2014-01-27 17:07:18 -0800 | [diff] [blame] | 2668 | # check for function declarations without arguments like "int foo()" |
| 2669 | if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { |
| 2670 | if (ERROR("FUNCTION_WITHOUT_ARGS", |
| 2671 | "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && |
| 2672 | $fix) { |
| 2673 | $fixed[$linenr - 1] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; |
| 2674 | } |
| 2675 | } |
| 2676 | |
Joe Perches | 92e112f | 2013-12-13 11:36:22 -0700 | [diff] [blame] | 2677 | # check for uses of DEFINE_PCI_DEVICE_TABLE |
| 2678 | if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { |
| 2679 | if (WARN("DEFINE_PCI_DEVICE_TABLE", |
| 2680 | "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && |
| 2681 | $fix) { |
| 2682 | $fixed[$linenr - 1] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /; |
| 2683 | } |
Joe Perches | 93ed0e2 | 2010-10-26 14:23:21 -0700 | [diff] [blame] | 2684 | } |
| 2685 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2686 | # check for new typedefs, only function parameters and sparse annotations |
| 2687 | # make sense. |
| 2688 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 8054576 | 2009-01-06 14:41:26 -0800 | [diff] [blame] | 2689 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2690 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 2691 | $line !~ /\b$typeTypedefs\b/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2692 | $line !~ /\b__bitwise(?:__|)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2693 | WARN("NEW_TYPEDEFS", |
| 2694 | "do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2698 | # (char*[ const]) |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2699 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 2700 | #print "AA<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2701 | my ($ident, $from, $to) = ($1, $2, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2702 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2703 | # Should start with a space. |
| 2704 | $to =~ s/^(\S)/ $1/; |
| 2705 | # Should not end with a space. |
| 2706 | $to =~ s/\s+$//; |
| 2707 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2708 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2709 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2710 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2711 | ## print "1: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2712 | if ($from ne $to) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2713 | if (ERROR("POINTER_LOCATION", |
| 2714 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && |
| 2715 | $fix) { |
| 2716 | my $sub_from = $ident; |
| 2717 | my $sub_to = $ident; |
| 2718 | $sub_to =~ s/\Q$from\E/$to/; |
| 2719 | $fixed[$linenr - 1] =~ |
| 2720 | s@\Q$sub_from\E@$sub_to@; |
| 2721 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2722 | } |
Andy Whitcroft | bfcb2cc | 2012-01-10 15:10:15 -0800 | [diff] [blame] | 2723 | } |
| 2724 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 2725 | #print "BB<$1>\n"; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2726 | my ($match, $from, $to, $ident) = ($1, $2, $2, $3); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2727 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2728 | # Should start with a space. |
| 2729 | $to =~ s/^(\S)/ $1/; |
| 2730 | # Should not end with a space. |
| 2731 | $to =~ s/\s+$//; |
| 2732 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 2733 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2734 | } |
| 2735 | # Modifiers should have spaces. |
| 2736 | $to =~ s/(\b$Modifier$)/$1 /; |
| 2737 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2738 | ## print "2: from<$from> to<$to> ident<$ident>\n"; |
Andy Whitcroft | 667026e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 2739 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2740 | if (ERROR("POINTER_LOCATION", |
| 2741 | "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && |
| 2742 | $fix) { |
| 2743 | |
| 2744 | my $sub_from = $match; |
| 2745 | my $sub_to = $match; |
| 2746 | $sub_to =~ s/\Q$from\E/$to/; |
| 2747 | $fixed[$linenr - 1] =~ |
| 2748 | s@\Q$sub_from\E@$sub_to@; |
| 2749 | } |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 2750 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | # # no BUG() or BUG_ON() |
| 2754 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 2755 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 2756 | # print "$herecurr"; |
| 2757 | # $clean = 0; |
| 2758 | # } |
| 2759 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2760 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2761 | WARN("LINUX_VERSION_CODE", |
| 2762 | "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] | 2763 | } |
| 2764 | |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2765 | # check for uses of printk_ratelimit |
| 2766 | if ($line =~ /\bprintk_ratelimit\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2767 | WARN("PRINTK_RATELIMITED", |
| 2768 | "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); |
Joe Perches | 1744122 | 2011-06-15 15:08:17 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2771 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 2772 | # same line do not need a level, so we use the current block context |
| 2773 | # 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] | 2774 | # 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] | 2775 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2776 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2777 | my $ok = 0; |
| 2778 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 2779 | #print "CHECK<$lines[$ln - 1]\n"; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2780 | # we have a preceding printk if it ends |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2781 | # with "\n" ignore it, else it is to blame |
| 2782 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 2783 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 2784 | $ok = 1; |
| 2785 | } |
| 2786 | last; |
| 2787 | } |
| 2788 | } |
| 2789 | if ($ok == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2790 | WARN("PRINTK_WITHOUT_KERN_LEVEL", |
| 2791 | "printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2792 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2793 | } |
| 2794 | |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2795 | if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { |
| 2796 | my $orig = $1; |
| 2797 | my $level = lc($orig); |
| 2798 | $level = "warn" if ($level eq "warning"); |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 2799 | my $level2 = $level; |
| 2800 | $level2 = "dbg" if ($level eq "debug"); |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2801 | WARN("PREFER_PR_LEVEL", |
Joe Perches | 8f26b83 | 2012-10-04 17:13:32 -0700 | [diff] [blame] | 2802 | "Prefer netdev_$level2(netdev, ... 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] | 2803 | } |
| 2804 | |
| 2805 | if ($line =~ /\bpr_warning\s*\(/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 2806 | if (WARN("PREFER_PR_LEVEL", |
| 2807 | "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && |
| 2808 | $fix) { |
| 2809 | $fixed[$linenr - 1] =~ |
| 2810 | s/\bpr_warning\b/pr_warn/; |
| 2811 | } |
Joe Perches | 243f380 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
Joe Perches | dc13931 | 2013-02-21 16:44:13 -0800 | [diff] [blame] | 2814 | if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { |
| 2815 | my $orig = $1; |
| 2816 | my $level = lc($orig); |
| 2817 | $level = "warn" if ($level eq "warning"); |
| 2818 | $level = "dbg" if ($level eq "debug"); |
| 2819 | WARN("PREFER_DEV_LEVEL", |
| 2820 | "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); |
| 2821 | } |
| 2822 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2823 | # function brace can't be on same line, except for #defines of do while, |
| 2824 | # or if closed on same line |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2825 | if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and |
| 2826 | !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2827 | ERROR("OPEN_BRACE", |
| 2828 | "open brace '{' following function declarations go on the next line\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2829 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2830 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2831 | # open braces for enum, union and struct go on the same line. |
| 2832 | if ($line =~ /^.\s*{/ && |
| 2833 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 2834 | ERROR("OPEN_BRACE", |
| 2835 | "open brace '{' following $1 go on the same line\n" . $hereprev); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2836 | } |
| 2837 | |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2838 | # missing space after union, struct or enum definition |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2839 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { |
| 2840 | if (WARN("SPACING", |
| 2841 | "missing space after $1 definition\n" . $herecurr) && |
| 2842 | $fix) { |
| 2843 | $fixed[$linenr - 1] =~ |
| 2844 | s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; |
| 2845 | } |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
Joe Perches | 31070b5 | 2014-01-23 15:54:49 -0800 | [diff] [blame] | 2848 | # Function pointer declarations |
| 2849 | # check spacing between type, funcptr, and args |
| 2850 | # canonical declaration is "type (*funcptr)(args...)" |
| 2851 | # |
| 2852 | # the $Declare variable will capture all spaces after the type |
| 2853 | # so check it for trailing missing spaces or multiple spaces |
| 2854 | if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)$Ident(\s*)\)(\s*)\(/) { |
| 2855 | my $declare = $1; |
| 2856 | my $pre_pointer_space = $2; |
| 2857 | my $post_pointer_space = $3; |
| 2858 | my $funcname = $4; |
| 2859 | my $post_funcname_space = $5; |
| 2860 | my $pre_args_space = $6; |
| 2861 | |
| 2862 | if ($declare !~ /\s$/) { |
| 2863 | WARN("SPACING", |
| 2864 | "missing space after return type\n" . $herecurr); |
| 2865 | } |
| 2866 | |
| 2867 | # unnecessary space "type (*funcptr)(args...)" |
| 2868 | elsif ($declare =~ /\s{2,}$/) { |
| 2869 | WARN("SPACING", |
| 2870 | "Multiple spaces after return type\n" . $herecurr); |
| 2871 | } |
| 2872 | |
| 2873 | # unnecessary space "type ( *funcptr)(args...)" |
| 2874 | if (defined $pre_pointer_space && |
| 2875 | $pre_pointer_space =~ /^\s/) { |
| 2876 | WARN("SPACING", |
| 2877 | "Unnecessary space after function pointer open parenthesis\n" . $herecurr); |
| 2878 | } |
| 2879 | |
| 2880 | # unnecessary space "type (* funcptr)(args...)" |
| 2881 | if (defined $post_pointer_space && |
| 2882 | $post_pointer_space =~ /^\s/) { |
| 2883 | WARN("SPACING", |
| 2884 | "Unnecessary space before function pointer name\n" . $herecurr); |
| 2885 | } |
| 2886 | |
| 2887 | # unnecessary space "type (*funcptr )(args...)" |
| 2888 | if (defined $post_funcname_space && |
| 2889 | $post_funcname_space =~ /^\s/) { |
| 2890 | WARN("SPACING", |
| 2891 | "Unnecessary space after function pointer name\n" . $herecurr); |
| 2892 | } |
| 2893 | |
| 2894 | # unnecessary space "type (*funcptr) (args...)" |
| 2895 | if (defined $pre_args_space && |
| 2896 | $pre_args_space =~ /^\s/) { |
| 2897 | WARN("SPACING", |
| 2898 | "Unnecessary space before function pointer arguments\n" . $herecurr); |
| 2899 | } |
| 2900 | |
| 2901 | if (show_type("SPACING") && $fix) { |
| 2902 | $fixed[$linenr - 1] =~ |
| 2903 | s/^(.\s*$Declare)\(\s*\*\s*($Ident)\s*\)\s*\(/rtrim($1) . " " . "\(\*$2\)\("/ex; |
| 2904 | } |
| 2905 | } |
| 2906 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2907 | # check for spacing round square brackets; allowed: |
| 2908 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2909 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 2910 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2911 | while ($line =~ /(.*?\s)\[/g) { |
| 2912 | my ($where, $prefix) = ($-[1], $1); |
| 2913 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 2914 | ($where != 0 || $prefix !~ /^.\s+$/) && |
Andy Whitcroft | daebc53 | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 2915 | $prefix !~ /[{,]\s+$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2916 | if (ERROR("BRACKET_SPACE", |
| 2917 | "space prohibited before open square bracket '['\n" . $herecurr) && |
| 2918 | $fix) { |
| 2919 | $fixed[$linenr - 1] =~ |
| 2920 | s/^(\+.*?)\s+\[/$1\[/; |
| 2921 | } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 2922 | } |
| 2923 | } |
| 2924 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2925 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2926 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2927 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2928 | my $ctx_before = substr($line, 0, $-[1]); |
| 2929 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2930 | |
| 2931 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2932 | if ($name =~ /^(?: |
| 2933 | if|for|while|switch|return|case| |
| 2934 | volatile|__volatile__| |
| 2935 | __attribute__|format|__extension__| |
| 2936 | asm|__asm__)$/x) |
| 2937 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2938 | # cpp #define statements have non-optional spaces, ie |
| 2939 | # if there is a space between the name and the open |
| 2940 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2941 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2942 | |
| 2943 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2944 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2945 | |
| 2946 | # If this whole things ends with a type its most |
| 2947 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2948 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2949 | |
| 2950 | } else { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2951 | if (WARN("SPACING", |
| 2952 | "space prohibited between function name and open parenthesis '('\n" . $herecurr) && |
| 2953 | $fix) { |
| 2954 | $fixed[$linenr - 1] =~ |
| 2955 | s/\b$name\s+\(/$name\(/; |
| 2956 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2957 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2958 | } |
Eric Nelson | 9a4cad4 | 2012-05-31 16:26:09 -0700 | [diff] [blame] | 2959 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2960 | # Check operator spacing. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2961 | if (!($line=~/\#\s*include/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2962 | my $fixed_line = ""; |
| 2963 | my $line_fixed = 0; |
| 2964 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2965 | my $ops = qr{ |
| 2966 | <<=|>>=|<=|>=|==|!=| |
| 2967 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 2968 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2969 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 2970 | \?:|\?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2971 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2972 | my @elements = split(/($ops|;)/, $opline); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2973 | |
| 2974 | ## print("element count: <" . $#elements . ">\n"); |
| 2975 | ## foreach my $el (@elements) { |
| 2976 | ## print("el: <$el>\n"); |
| 2977 | ## } |
| 2978 | |
| 2979 | my @fix_elements = (); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2980 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2981 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2982 | foreach my $el (@elements) { |
| 2983 | push(@fix_elements, substr($rawline, $off, length($el))); |
| 2984 | $off += length($el); |
| 2985 | } |
| 2986 | |
| 2987 | $off = 0; |
| 2988 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2989 | my $blank = copy_spacing($opline); |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 2990 | my $last_after = -1; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2991 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2992 | for (my $n = 0; $n < $#elements; $n += 2) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 2993 | |
| 2994 | my $good = $fix_elements[$n] . $fix_elements[$n + 1]; |
| 2995 | |
| 2996 | ## print("n: <$n> good: <$good>\n"); |
| 2997 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2998 | $off += length($elements[$n]); |
| 2999 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3000 | # Pick up the preceding and succeeding characters. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3001 | my $ca = substr($opline, 0, $off); |
| 3002 | my $cc = ''; |
| 3003 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 3004 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 3005 | } |
| 3006 | my $cb = "$ca$;$cc"; |
| 3007 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3008 | my $a = ''; |
| 3009 | $a = 'V' if ($elements[$n] ne ''); |
| 3010 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3011 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3012 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 3013 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3014 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3015 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3016 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3017 | |
| 3018 | my $c = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3019 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3020 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 3021 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3022 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3023 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 3024 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 8b1b337 | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 3025 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3026 | } else { |
| 3027 | $c = 'E'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3028 | } |
| 3029 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3030 | my $ctx = "${a}x${c}"; |
| 3031 | |
| 3032 | my $at = "(ctx:$ctx)"; |
| 3033 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3034 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3035 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3036 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3037 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3038 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3039 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3040 | # Get the full operator variant. |
| 3041 | my $opv = $op . substr($curr_vars, $off, 1); |
| 3042 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3043 | # Ignore operators passed as parameters. |
| 3044 | if ($op_type ne 'V' && |
| 3045 | $ca =~ /\s$/ && $cc =~ /^\s*,/) { |
| 3046 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3047 | # # Ignore comments |
| 3048 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3049 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3050 | # ; 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] | 3051 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3052 | if ($ctx !~ /.x[WEBC]/ && |
| 3053 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3054 | if (ERROR("SPACING", |
| 3055 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3056 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3057 | $line_fixed = 1; |
| 3058 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | # // is a comment |
| 3062 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3063 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3064 | # No spaces for: |
| 3065 | # -> |
| 3066 | # : when part of a bitfield |
| 3067 | } elsif ($op eq '->' || $opv eq ':B') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3068 | if ($ctx =~ /Wx.|.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3069 | if (ERROR("SPACING", |
| 3070 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3071 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3072 | if (defined $fix_elements[$n + 2]) { |
| 3073 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3074 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3075 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3076 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | # , must have a space on the right. |
| 3080 | } elsif ($op eq ',') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3081 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3082 | if (ERROR("SPACING", |
| 3083 | "space required after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3084 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3085 | $line_fixed = 1; |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3086 | $last_after = $n; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3087 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3088 | } |
| 3089 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3090 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3091 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3092 | #warn "'*' is part of type\n"; |
| 3093 | |
| 3094 | # unary operators should have a space before and |
| 3095 | # none after. May be left adjacent to another |
| 3096 | # unary operator, or a cast |
| 3097 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3098 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 3099 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3100 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3101 | if (ERROR("SPACING", |
| 3102 | "space required before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3103 | if ($n != $last_after + 2) { |
| 3104 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
| 3105 | $line_fixed = 1; |
| 3106 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3107 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3108 | } |
Andy Whitcroft | a3340b3 | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 3109 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3110 | # A unary '*' may be const |
| 3111 | |
| 3112 | } elsif ($ctx =~ /.xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3113 | if (ERROR("SPACING", |
| 3114 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3115 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3116 | if (defined $fix_elements[$n + 2]) { |
| 3117 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3118 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3119 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3120 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | # unary ++ and unary -- are allowed no space on one side. |
| 3124 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3125 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3126 | if (ERROR("SPACING", |
| 3127 | "space required one side of that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3128 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3129 | $line_fixed = 1; |
| 3130 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3131 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3132 | if ($ctx =~ /Wx[BE]/ || |
| 3133 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3134 | if (ERROR("SPACING", |
| 3135 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3136 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3137 | $line_fixed = 1; |
| 3138 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3139 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3140 | if ($ctx =~ /ExW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3141 | if (ERROR("SPACING", |
| 3142 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3143 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3144 | if (defined $fix_elements[$n + 2]) { |
| 3145 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3146 | } |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3147 | $line_fixed = 1; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3148 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3149 | } |
| 3150 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3151 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3152 | } elsif ($op eq '<<' or $op eq '>>' or |
| 3153 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 3154 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3155 | $op eq '*' or $op eq '/' or |
| 3156 | $op eq '%') |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3157 | { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3158 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3159 | if (ERROR("SPACING", |
| 3160 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3161 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3162 | if (defined $fix_elements[$n + 2]) { |
| 3163 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3164 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3165 | $line_fixed = 1; |
| 3166 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3169 | # A colon needs no spaces before when it is |
| 3170 | # terminating a case value or a label. |
| 3171 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 3172 | if ($ctx =~ /Wx./) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3173 | if (ERROR("SPACING", |
| 3174 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3175 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3176 | $line_fixed = 1; |
| 3177 | } |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3180 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3181 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3182 | my $ok = 0; |
| 3183 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3184 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3185 | if (($op eq '<' && |
| 3186 | $cc =~ /^\S+\@\S+>/) || |
| 3187 | ($op eq '>' && |
| 3188 | $ca =~ /<\S+\@\S+$/)) |
| 3189 | { |
| 3190 | $ok = 1; |
| 3191 | } |
| 3192 | |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 3193 | # messages are ERROR, but ?: are CHK |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 3194 | if ($ok == 0) { |
Joe Perches | 8473162 | 2013-11-12 15:10:05 -0800 | [diff] [blame] | 3195 | my $msg_type = \&ERROR; |
| 3196 | $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); |
| 3197 | |
| 3198 | if (&{$msg_type}("SPACING", |
| 3199 | "spaces required around that '$op' $at\n" . $hereptr)) { |
Joe Perches | b34c648 | 2013-09-11 14:24:01 -0700 | [diff] [blame] | 3200 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
| 3201 | if (defined $fix_elements[$n + 2]) { |
| 3202 | $fix_elements[$n + 2] =~ s/^\s+//; |
| 3203 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3204 | $line_fixed = 1; |
| 3205 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3206 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3207 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3208 | $off += length($elements[$n + 1]); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3209 | |
| 3210 | ## print("n: <$n> GOOD: <$good>\n"); |
| 3211 | |
| 3212 | $fixed_line = $fixed_line . $good; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3213 | } |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3214 | |
| 3215 | if (($#elements % 2) == 0) { |
| 3216 | $fixed_line = $fixed_line . $fix_elements[$#elements]; |
| 3217 | } |
| 3218 | |
| 3219 | if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) { |
| 3220 | $fixed[$linenr - 1] = $fixed_line; |
| 3221 | } |
| 3222 | |
| 3223 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3224 | } |
| 3225 | |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 3226 | # check for whitespace before a non-naked semicolon |
Joe Perches | d2e248e | 2014-01-23 15:54:41 -0800 | [diff] [blame] | 3227 | if ($line =~ /^\+.*\S\s+;\s*$/) { |
Joe Perches | 786b632 | 2013-07-03 15:05:32 -0700 | [diff] [blame] | 3228 | if (WARN("SPACING", |
| 3229 | "space prohibited before semicolon\n" . $herecurr) && |
| 3230 | $fix) { |
| 3231 | 1 while $fixed[$linenr - 1] =~ |
| 3232 | s/^(\+.*\S)\s+;/$1;/; |
| 3233 | } |
| 3234 | } |
| 3235 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3236 | # check for multiple assignments |
| 3237 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3238 | CHK("MULTIPLE_ASSIGNMENTS", |
| 3239 | "multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3240 | } |
| 3241 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3242 | ## # check for multiple declarations, allowing for a function declaration |
| 3243 | ## # continuation. |
| 3244 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 3245 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 3246 | ## |
| 3247 | ## # Remove any bracketed sections to ensure we do not |
| 3248 | ## # falsly report the parameters of functions. |
| 3249 | ## my $ln = $line; |
| 3250 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 3251 | ## } |
| 3252 | ## if ($ln =~ /,/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3253 | ## WARN("MULTIPLE_DECLARATION", |
| 3254 | ## "declaring multiple variables together should be avoided\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3255 | ## } |
| 3256 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3257 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3258 | #need space before brace following if, while, etc |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3259 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 3260 | $line =~ /do{/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3261 | if (ERROR("SPACING", |
| 3262 | "space required before the open brace '{'\n" . $herecurr) && |
| 3263 | $fix) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3264 | $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/; |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3265 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
Joe Perches | c4a62ef | 2013-07-03 15:05:28 -0700 | [diff] [blame] | 3268 | ## # check for blank lines before declarations |
| 3269 | ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && |
| 3270 | ## $prevrawline =~ /^.\s*$/) { |
| 3271 | ## WARN("SPACING", |
| 3272 | ## "No blank lines before declarations\n" . $hereprev); |
| 3273 | ## } |
| 3274 | ## |
| 3275 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3276 | # closing brace should have a space following it when it has anything |
| 3277 | # on the line |
| 3278 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3279 | if (ERROR("SPACING", |
| 3280 | "space required after that close brace '}'\n" . $herecurr) && |
| 3281 | $fix) { |
| 3282 | $fixed[$linenr - 1] =~ |
| 3283 | s/}((?!(?:,|;|\)))\S)/} $1/; |
| 3284 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3285 | } |
| 3286 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3287 | # check spacing on square brackets |
| 3288 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3289 | if (ERROR("SPACING", |
| 3290 | "space prohibited after that open square bracket '['\n" . $herecurr) && |
| 3291 | $fix) { |
| 3292 | $fixed[$linenr - 1] =~ |
| 3293 | s/\[\s+/\[/; |
| 3294 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3295 | } |
| 3296 | if ($line =~ /\s\]/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3297 | if (ERROR("SPACING", |
| 3298 | "space prohibited before that close square bracket ']'\n" . $herecurr) && |
| 3299 | $fix) { |
| 3300 | $fixed[$linenr - 1] =~ |
| 3301 | s/\s+\]/\]/; |
| 3302 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3303 | } |
| 3304 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3305 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3306 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 3307 | $line !~ /for\s*\(\s+;/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3308 | if (ERROR("SPACING", |
| 3309 | "space prohibited after that open parenthesis '('\n" . $herecurr) && |
| 3310 | $fix) { |
| 3311 | $fixed[$linenr - 1] =~ |
| 3312 | s/\(\s+/\(/; |
| 3313 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3314 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3315 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3316 | $line !~ /for\s*\(.*;\s+\)/ && |
| 3317 | $line !~ /:\s+\)/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3318 | if (ERROR("SPACING", |
| 3319 | "space prohibited before that close parenthesis ')'\n" . $herecurr) && |
| 3320 | $fix) { |
| 3321 | $fixed[$linenr - 1] =~ |
| 3322 | s/\s+\)/\)/; |
| 3323 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3324 | } |
| 3325 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3326 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3327 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3328 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3329 | if (WARN("INDENTED_LABEL", |
| 3330 | "labels should not be indented\n" . $herecurr) && |
| 3331 | $fix) { |
| 3332 | $fixed[$linenr - 1] =~ |
| 3333 | s/^(.)\s+/$1/; |
| 3334 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3335 | } |
| 3336 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3337 | # Return is not a function. |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 3338 | if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3339 | my $spacing = $1; |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 3340 | if ($^V && $^V ge 5.10.0 && |
| 3341 | $stat =~ /^.\s*return\s*$balanced_parens\s*;\s*$/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3342 | ERROR("RETURN_PARENTHESES", |
| 3343 | "return is not a function, parentheses are not required\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3344 | |
| 3345 | } elsif ($spacing !~ /\s+/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3346 | ERROR("SPACING", |
| 3347 | "space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3348 | } |
| 3349 | } |
Joe Perches | 507e514 | 2013-11-12 15:10:13 -0800 | [diff] [blame] | 3350 | |
Joe Perches | 189248d | 2014-01-23 15:54:47 -0800 | [diff] [blame] | 3351 | # if statements using unnecessary parentheses - ie: if ((foo == bar)) |
| 3352 | if ($^V && $^V ge 5.10.0 && |
| 3353 | $line =~ /\bif\s*((?:\(\s*){2,})/) { |
| 3354 | my $openparens = $1; |
| 3355 | my $count = $openparens =~ tr@\(@\(@; |
| 3356 | my $msg = ""; |
| 3357 | if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { |
| 3358 | my $comp = $4; #Not $1 because of $LvalOrFunc |
| 3359 | $msg = " - maybe == should be = ?" if ($comp eq "=="); |
| 3360 | WARN("UNNECESSARY_PARENTHESES", |
| 3361 | "Unnecessary parentheses$msg\n" . $herecurr); |
| 3362 | } |
| 3363 | } |
| 3364 | |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 3365 | # Return of what appears to be an errno should normally be -'ve |
| 3366 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { |
| 3367 | my $name = $1; |
| 3368 | if ($name ne 'EOF' && $name ne 'ERROR') { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3369 | WARN("USE_NEGATIVE_ERRNO", |
| 3370 | "return of an errno should typically be -ve (return -$1)\n" . $herecurr); |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 3371 | } |
| 3372 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3373 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3374 | # Need a space before open parenthesis after if, while etc |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3375 | if ($line =~ /\b(if|while|for|switch)\(/) { |
| 3376 | if (ERROR("SPACING", |
| 3377 | "space required before the open parenthesis '('\n" . $herecurr) && |
| 3378 | $fix) { |
| 3379 | $fixed[$linenr - 1] =~ |
| 3380 | s/\b(if|while|for|switch)\(/$1 \(/; |
| 3381 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3382 | } |
| 3383 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 3384 | # Check for illegal assignment in if conditional -- and check for trailing |
| 3385 | # statements after the conditional. |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3386 | if ($line =~ /do\s*(?!{)/) { |
Andy Whitcroft | 3e469cd | 2012-01-10 15:10:01 -0800 | [diff] [blame] | 3387 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 3388 | ctx_statement_block($linenr, $realcnt, 0) |
| 3389 | if (!defined $stat); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3390 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 3391 | $remain_next, $off_next); |
| 3392 | $stat_next =~ s/\n./\n /g; |
| 3393 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 3394 | |
| 3395 | if ($stat_next =~ /^\s*while\b/) { |
| 3396 | # If the statement carries leading newlines, |
| 3397 | # then count those as offsets. |
| 3398 | my ($whitespace) = |
| 3399 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 3400 | my $offset = |
| 3401 | statement_rawlines($whitespace) - 1; |
| 3402 | |
| 3403 | $suppress_whiletrailers{$line_nr_next + |
| 3404 | $offset} = 1; |
| 3405 | } |
| 3406 | } |
| 3407 | if (!defined $suppress_whiletrailers{$linenr} && |
Joe Perches | c11230f | 2013-11-21 14:31:57 -0800 | [diff] [blame] | 3408 | defined($stat) && defined($cond) && |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 3409 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3410 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3411 | |
Andy Whitcroft | b53c8e1 | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 3412 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3413 | ERROR("ASSIGN_IN_IF", |
| 3414 | "do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | # Find out what is on the end of the line after the |
| 3418 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3419 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3420 | $s =~ s/\n.*//g; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3421 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 3422 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 3423 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3424 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3425 | # Find out how long the conditional actually is. |
| 3426 | my @newlines = ($c =~ /\n/gs); |
| 3427 | my $cond_lines = 1 + $#newlines; |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 3428 | my $stat_real = ''; |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3429 | |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 3430 | $stat_real = raw_line($linenr, $cond_lines) |
| 3431 | . "\n" if ($cond_lines); |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 3432 | if (defined($stat_real) && $cond_lines > 1) { |
| 3433 | $stat_real = "[...]\n$stat_real"; |
| 3434 | } |
| 3435 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3436 | ERROR("TRAILING_STATEMENTS", |
| 3437 | "trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3438 | } |
| 3439 | } |
| 3440 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3441 | # Check for bitwise tests written as boolean |
| 3442 | if ($line =~ / |
| 3443 | (?: |
| 3444 | (?:\[|\(|\&\&|\|\|) |
| 3445 | \s*0[xX][0-9]+\s* |
| 3446 | (?:\&\&|\|\|) |
| 3447 | | |
| 3448 | (?:\&\&|\|\|) |
| 3449 | \s*0[xX][0-9]+\s* |
| 3450 | (?:\&\&|\|\||\)|\]) |
| 3451 | )/x) |
| 3452 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3453 | WARN("HEXADECIMAL_BOOLEAN_TEST", |
| 3454 | "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3455 | } |
| 3456 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3457 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3458 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 3459 | my $s = $1; |
| 3460 | $s =~ s/$;//g; # Remove any comments |
| 3461 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3462 | ERROR("TRAILING_STATEMENTS", |
| 3463 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3464 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3465 | } |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 3466 | # if should not continue a brace |
| 3467 | if ($line =~ /}\s*if\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3468 | ERROR("TRAILING_STATEMENTS", |
| 3469 | "trailing statements should be on next line\n" . |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 3470 | $herecurr); |
| 3471 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3472 | # case and default should not have general statements after them |
| 3473 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 3474 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 3475 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3476 | \s*return\s+ |
| 3477 | )/xg) |
| 3478 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3479 | ERROR("TRAILING_STATEMENTS", |
| 3480 | "trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 3481 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3482 | |
| 3483 | # Check for }<nl>else {, these must be at the same |
| 3484 | # indent level to be relevant to each other. |
| 3485 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 3486 | $previndent == $indent) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3487 | ERROR("ELSE_AFTER_BRACE", |
| 3488 | "else should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3489 | } |
| 3490 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3491 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and |
| 3492 | $previndent == $indent) { |
| 3493 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 3494 | |
| 3495 | # Find out what is on the end of the line after the |
| 3496 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3497 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3498 | $s =~ s/\n.*//g; |
| 3499 | |
| 3500 | if ($s =~ /^\s*;/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3501 | ERROR("WHILE_AFTER_BRACE", |
| 3502 | "while should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 3503 | } |
| 3504 | } |
| 3505 | |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3506 | #Specific variable tests |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 3507 | while ($line =~ m{($Constant|$Lval)}g) { |
| 3508 | my $var = $1; |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3509 | |
| 3510 | #gcc binary extension |
| 3511 | if ($var =~ /^$Binary$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3512 | if (WARN("GCC_BINARY_CONSTANT", |
| 3513 | "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && |
| 3514 | $fix) { |
| 3515 | my $hexval = sprintf("0x%x", oct($var)); |
| 3516 | $fixed[$linenr - 1] =~ |
| 3517 | s/\b$var\b/$hexval/; |
| 3518 | } |
Joe Perches | 95e2c60 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | #CamelCase |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 3522 | if ($var !~ /^$Constant$/ && |
Joe Perches | be79794 | 2013-07-03 15:05:20 -0700 | [diff] [blame] | 3523 | $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 3524 | #Ignore Page<foo> variants |
Joe Perches | 807bd26 | 2013-07-03 15:05:22 -0700 | [diff] [blame] | 3525 | $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && |
Joe Perches | 22735ce | 2013-07-03 15:05:33 -0700 | [diff] [blame] | 3526 | #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 3527 | $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) { |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 3528 | while ($var =~ m{($Ident)}g) { |
| 3529 | my $word = $1; |
| 3530 | next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); |
Joe Perches | d8b0771 | 2013-11-12 15:10:06 -0800 | [diff] [blame] | 3531 | if ($check) { |
| 3532 | seed_camelcase_includes(); |
| 3533 | if (!$file && !$camelcase_file_seeded) { |
| 3534 | seed_camelcase_file($realfile); |
| 3535 | $camelcase_file_seeded = 1; |
| 3536 | } |
| 3537 | } |
Joe Perches | 7e781f6 | 2013-09-11 14:23:55 -0700 | [diff] [blame] | 3538 | if (!defined $camelcase{$word}) { |
| 3539 | $camelcase{$word} = 1; |
| 3540 | CHK("CAMELCASE", |
| 3541 | "Avoid CamelCase: <$word>\n" . $herecurr); |
| 3542 | } |
Joe Perches | 3445686 | 2013-07-03 15:05:34 -0700 | [diff] [blame] | 3543 | } |
Joe Perches | 323c126 | 2012-12-17 16:02:07 -0800 | [diff] [blame] | 3544 | } |
| 3545 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3546 | |
| 3547 | #no spaces allowed after \ in define |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3548 | if ($line =~ /\#\s*define.*\\\s+$/) { |
| 3549 | if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", |
| 3550 | "Whitespace after \\ makes next lines useless\n" . $herecurr) && |
| 3551 | $fix) { |
| 3552 | $fixed[$linenr - 1] =~ s/\s+$//; |
| 3553 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3554 | } |
| 3555 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3556 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3557 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3558 | my $file = "$1.h"; |
| 3559 | my $checkfile = "include/linux/$file"; |
| 3560 | if (-f "$root/$checkfile" && |
| 3561 | $realfile ne $checkfile && |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 3562 | $1 !~ /$allowed_asm_includes/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3563 | { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3564 | if ($realfile =~ m{^arch/}) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3565 | CHK("ARCH_INCLUDE_LINUX", |
| 3566 | "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3567 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3568 | WARN("INCLUDE_LINUX", |
| 3569 | "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 3570 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3571 | } |
| 3572 | } |
| 3573 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3574 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 3575 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3576 | # in a known good container |
Andy Whitcroft | b8f96a3 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 3577 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 3578 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3579 | my $ln = $linenr; |
| 3580 | my $cnt = $realcnt; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3581 | my ($off, $dstat, $dcond, $rest); |
| 3582 | my $ctx = ''; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3583 | ($dstat, $dcond, $ln, $cnt, $off) = |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3584 | ctx_statement_block($linenr, $realcnt, 0); |
| 3585 | $ctx = $dstat; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3586 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 3587 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3588 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3589 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 3590 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3591 | $dstat =~ s/\\\n.//g; |
| 3592 | $dstat =~ s/^\s*//s; |
| 3593 | $dstat =~ s/\s*$//s; |
| 3594 | |
| 3595 | # Flatten any parentheses and braces |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3596 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 3597 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
Andy Whitcroft | c81769f | 2012-01-10 15:10:10 -0800 | [diff] [blame] | 3598 | $dstat =~ s/\[[^\[\]]*\]/1/) |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 3599 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3600 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3601 | |
Andy Whitcroft | e45bab8 | 2012-03-23 15:02:18 -0700 | [diff] [blame] | 3602 | # Flatten any obvious string concatentation. |
| 3603 | while ($dstat =~ s/("X*")\s*$Ident/$1/ || |
| 3604 | $dstat =~ s/$Ident\s*("X*")/$1/) |
| 3605 | { |
| 3606 | } |
| 3607 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3608 | my $exceptions = qr{ |
| 3609 | $Declare| |
| 3610 | module_param_named| |
Kees Cook | a0a0a7a | 2012-10-04 17:13:38 -0700 | [diff] [blame] | 3611 | MODULE_PARM_DESC| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3612 | DECLARE_PER_CPU| |
| 3613 | DEFINE_PER_CPU| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 3614 | __typeof__\(| |
Stefani Seibold | 22fd2d3 | 2010-03-05 13:43:52 -0800 | [diff] [blame] | 3615 | union| |
| 3616 | struct| |
Andy Whitcroft | ea71a0a | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 3617 | \.$Ident\s*=\s*| |
| 3618 | ^\"|\"$ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3619 | }x; |
Andy Whitcroft | 5eaa20b | 2010-10-26 14:23:18 -0700 | [diff] [blame] | 3620 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3621 | if ($dstat ne '' && |
| 3622 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 3623 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
Joe Perches | 3cc4b1c | 2013-07-03 15:05:27 -0700 | [diff] [blame] | 3624 | $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz |
Andy Whitcroft | b9df76a | 2012-03-23 15:02:17 -0700 | [diff] [blame] | 3625 | $dstat !~ /^'X'$/ && # character constants |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3626 | $dstat !~ /$exceptions/ && |
| 3627 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
Joe Perches | e942e2c | 2013-04-17 15:58:26 -0700 | [diff] [blame] | 3628 | $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo |
Andy Whitcroft | 72f115f | 2012-01-10 15:10:06 -0800 | [diff] [blame] | 3629 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3630 | $dstat !~ /^for\s*$Constant$/ && # for (...) |
| 3631 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() |
| 3632 | $dstat !~ /^do\s*{/ && # do {... |
Joe Perches | f95a7e6 | 2013-09-11 14:24:00 -0700 | [diff] [blame] | 3633 | $dstat !~ /^\({/ && # ({... |
| 3634 | $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3635 | { |
| 3636 | $ctx =~ s/\n*$//; |
| 3637 | my $herectx = $here . "\n"; |
| 3638 | my $cnt = statement_rawlines($ctx); |
| 3639 | |
| 3640 | for (my $n = 0; $n < $cnt; $n++) { |
| 3641 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3644 | if ($dstat =~ /;/) { |
| 3645 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 3646 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 3647 | } else { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3648 | ERROR("COMPLEX_MACRO", |
Andy Whitcroft | f74bd19 | 2012-01-10 15:09:54 -0800 | [diff] [blame] | 3649 | "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 3650 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3651 | } |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3652 | |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 3653 | # check for line continuations outside of #defines, preprocessor #, and asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3654 | |
| 3655 | } else { |
| 3656 | if ($prevline !~ /^..*\\$/ && |
Joe Perches | 481eb48 | 2012-12-17 16:01:56 -0800 | [diff] [blame] | 3657 | $line !~ /^\+\s*\#.*\\$/ && # preprocessor |
| 3658 | $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm |
Joe Perches | 5023d34 | 2012-12-17 16:01:47 -0800 | [diff] [blame] | 3659 | $line =~ /^\+.*\\$/) { |
| 3660 | WARN("LINE_CONTINUATIONS", |
| 3661 | "Avoid unnecessary line continuations\n" . $herecurr); |
| 3662 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3663 | } |
| 3664 | |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 3665 | # do {} while (0) macro tests: |
| 3666 | # single-statement macros do not need to be enclosed in do while (0) loop, |
| 3667 | # macro should not end with a semicolon |
| 3668 | if ($^V && $^V ge 5.10.0 && |
| 3669 | $realfile !~ m@/vmlinux.lds.h$@ && |
| 3670 | $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { |
| 3671 | my $ln = $linenr; |
| 3672 | my $cnt = $realcnt; |
| 3673 | my ($off, $dstat, $dcond, $rest); |
| 3674 | my $ctx = ''; |
| 3675 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 3676 | ctx_statement_block($linenr, $realcnt, 0); |
| 3677 | $ctx = $dstat; |
| 3678 | |
| 3679 | $dstat =~ s/\\\n.//g; |
| 3680 | |
| 3681 | if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { |
| 3682 | my $stmts = $2; |
| 3683 | my $semis = $3; |
| 3684 | |
| 3685 | $ctx =~ s/\n*$//; |
| 3686 | my $cnt = statement_rawlines($ctx); |
| 3687 | my $herectx = $here . "\n"; |
| 3688 | |
| 3689 | for (my $n = 0; $n < $cnt; $n++) { |
| 3690 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 3691 | } |
| 3692 | |
Joe Perches | ac8e97f | 2012-08-21 16:15:53 -0700 | [diff] [blame] | 3693 | if (($stmts =~ tr/;/;/) == 1 && |
| 3694 | $stmts !~ /^\s*(if|while|for|switch)\b/) { |
Joe Perches | b13edf7 | 2012-07-30 14:41:24 -0700 | [diff] [blame] | 3695 | WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", |
| 3696 | "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); |
| 3697 | } |
| 3698 | if (defined $semis && $semis ne "") { |
| 3699 | WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", |
| 3700 | "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); |
| 3701 | } |
| 3702 | } |
| 3703 | } |
| 3704 | |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3705 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 3706 | # all assignments may have only one of the following with an assignment: |
| 3707 | # . |
| 3708 | # ALIGN(...) |
| 3709 | # VMLINUX_SYMBOL(...) |
| 3710 | 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] | 3711 | WARN("MISSING_VMLINUX_SYMBOL", |
| 3712 | "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 3713 | } |
| 3714 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3715 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3716 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 3717 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3718 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3719 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3720 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 3721 | if ($#chunks > 0 && $level == 0) { |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3722 | my @allowed = (); |
| 3723 | my $allow = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3724 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3725 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3726 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3727 | for my $chunk (@chunks) { |
| 3728 | my ($cond, $block) = @{$chunk}; |
| 3729 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3730 | # If the condition carries leading newlines, then count those as offsets. |
| 3731 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 3732 | my $offset = statement_rawlines($whitespace) - 1; |
| 3733 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3734 | $allowed[$allow] = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3735 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 3736 | |
| 3737 | # We have looked at and allowed this specific line. |
| 3738 | $suppress_ifbraces{$ln + $offset} = 1; |
| 3739 | |
| 3740 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3741 | $ln += statement_rawlines($block) - 1; |
| 3742 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3743 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3744 | |
| 3745 | $seen++ if ($block =~ /^\s*{/); |
| 3746 | |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3747 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3748 | if (statement_lines($cond) > 1) { |
| 3749 | #print "APW: ALLOWED: cond<$cond>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3750 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3751 | } |
| 3752 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3753 | #print "APW: ALLOWED: block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3754 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3755 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3756 | if (statement_block_size($block) > 1) { |
| 3757 | #print "APW: ALLOWED: lines block<$block>\n"; |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3758 | $allowed[$allow] = 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3759 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3760 | $allow++; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3761 | } |
Joe Perches | aad4f61 | 2012-03-23 15:02:19 -0700 | [diff] [blame] | 3762 | if ($seen) { |
| 3763 | my $sum_allowed = 0; |
| 3764 | foreach (@allowed) { |
| 3765 | $sum_allowed += $_; |
| 3766 | } |
| 3767 | if ($sum_allowed == 0) { |
| 3768 | WARN("BRACES", |
| 3769 | "braces {} are not necessary for any arm of this statement\n" . $herectx); |
| 3770 | } elsif ($sum_allowed != $allow && |
| 3771 | $seen != $allow) { |
| 3772 | CHK("BRACES", |
| 3773 | "braces {} should be used on all arms of this statement\n" . $herectx); |
| 3774 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3775 | } |
| 3776 | } |
| 3777 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3778 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 3779 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3780 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3781 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3782 | # Check the pre-context. |
| 3783 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 3784 | #print "APW: ALLOWED: pre<$1>\n"; |
| 3785 | $allowed = 1; |
| 3786 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3787 | |
| 3788 | my ($level, $endln, @chunks) = |
| 3789 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 3790 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3791 | # Check the condition. |
| 3792 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3793 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3794 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3795 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3796 | } |
| 3797 | if (statement_lines($cond) > 1) { |
| 3798 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 3799 | $allowed = 1; |
| 3800 | } |
| 3801 | if ($block =~/\b(?:if|for|while)\b/) { |
| 3802 | #print "APW: ALLOWED: block<$block>\n"; |
| 3803 | $allowed = 1; |
| 3804 | } |
| 3805 | if (statement_block_size($block) > 1) { |
| 3806 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 3807 | $allowed = 1; |
| 3808 | } |
| 3809 | # Check the post-context. |
| 3810 | if (defined $chunks[1]) { |
| 3811 | my ($cond, $block) = @{$chunks[1]}; |
| 3812 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 3813 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3814 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3815 | if ($block =~ /^\s*\{/) { |
| 3816 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 3817 | $allowed = 1; |
| 3818 | } |
| 3819 | } |
| 3820 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3821 | my $herectx = $here . "\n"; |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3822 | my $cnt = statement_rawlines($block); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3823 | |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 3824 | for (my $n = 0; $n < $cnt; $n++) { |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 3825 | $herectx .= raw_line($linenr, $n) . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 3826 | } |
| 3827 | |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3828 | WARN("BRACES", |
| 3829 | "braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3830 | } |
| 3831 | } |
| 3832 | |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3833 | # check for unnecessary blank lines around braces |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 3834 | if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3835 | CHK("BRACES", |
| 3836 | "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); |
| 3837 | } |
Joe Perches | 77b9a53 | 2013-07-03 15:05:29 -0700 | [diff] [blame] | 3838 | if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { |
Joe Perches | 0979ae6 | 2012-12-17 16:01:59 -0800 | [diff] [blame] | 3839 | CHK("BRACES", |
| 3840 | "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); |
| 3841 | } |
| 3842 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3843 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 3844 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 3845 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3846 | WARN("VOLATILE", |
| 3847 | "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] | 3848 | } |
| 3849 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3850 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3851 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3852 | CHK("REDUNDANT_CODE", |
| 3853 | "if this code is redundant consider removing it\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3854 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3855 | } |
| 3856 | |
Andy Whitcroft | 03df4b5 | 2012-12-17 16:01:52 -0800 | [diff] [blame] | 3857 | # check for needless "if (<foo>) fn(<foo>)" uses |
| 3858 | if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { |
| 3859 | my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; |
| 3860 | if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { |
| 3861 | WARN('NEEDLESS_IF', |
| 3862 | "$1(NULL) is safe this check is probably not required\n" . $hereprev); |
Greg Kroah-Hartman | 4c432a8 | 2008-07-23 21:29:04 -0700 | [diff] [blame] | 3863 | } |
| 3864 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 3865 | |
Joe Perches | 8716de3 | 2013-09-11 14:24:05 -0700 | [diff] [blame] | 3866 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
| 3867 | if ($line =~ /(\b$InitAttribute\b)/) { |
| 3868 | my $attr = $1; |
| 3869 | if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { |
| 3870 | my $ptr = $1; |
| 3871 | my $var = $2; |
| 3872 | if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && |
| 3873 | ERROR("MISPLACED_INIT", |
| 3874 | "$attr should be placed after $var\n" . $herecurr)) || |
| 3875 | ($ptr !~ /\b(union|struct)\s+$attr\b/ && |
| 3876 | WARN("MISPLACED_INIT", |
| 3877 | "$attr should be placed after $var\n" . $herecurr))) && |
| 3878 | $fix) { |
| 3879 | $fixed[$linenr - 1] =~ 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; |
| 3880 | } |
| 3881 | } |
| 3882 | } |
| 3883 | |
Joe Perches | e970b88 | 2013-11-12 15:10:10 -0800 | [diff] [blame] | 3884 | # check for $InitAttributeData (ie: __initdata) with const |
| 3885 | if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { |
| 3886 | my $attr = $1; |
| 3887 | $attr =~ /($InitAttributePrefix)(.*)/; |
| 3888 | my $attr_prefix = $1; |
| 3889 | my $attr_type = $2; |
| 3890 | if (ERROR("INIT_ATTRIBUTE", |
| 3891 | "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && |
| 3892 | $fix) { |
| 3893 | $fixed[$linenr - 1] =~ |
| 3894 | s/$InitAttributeData/${attr_prefix}initconst/; |
| 3895 | } |
| 3896 | } |
| 3897 | |
| 3898 | # check for $InitAttributeConst (ie: __initconst) without const |
| 3899 | if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { |
| 3900 | my $attr = $1; |
| 3901 | if (ERROR("INIT_ATTRIBUTE", |
| 3902 | "Use of $attr requires a separate use of const\n" . $herecurr) && |
| 3903 | $fix) { |
| 3904 | my $lead = $fixed[$linenr - 1] =~ |
| 3905 | /(^\+\s*(?:static\s+))/; |
| 3906 | $lead = rtrim($1); |
| 3907 | $lead = "$lead " if ($lead !~ /^\+$/); |
| 3908 | $lead = "${lead}const "; |
| 3909 | $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/; |
| 3910 | } |
| 3911 | } |
| 3912 | |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3913 | # prefer usleep_range over udelay |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 3914 | if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3915 | # ignore udelay's < 10, however |
Bruce Allan | 37581c2 | 2013-02-21 16:44:19 -0800 | [diff] [blame] | 3916 | if (! ($1 < 10) ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3917 | CHK("USLEEP_RANGE", |
| 3918 | "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 3922 | # warn about unexpectedly long msleep's |
| 3923 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 3924 | if ($1 < 20) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3925 | WARN("MSLEEP", |
| 3926 | "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 3927 | } |
| 3928 | } |
| 3929 | |
Joe Perches | 36ec193 | 2013-07-03 15:05:25 -0700 | [diff] [blame] | 3930 | # check for comparisons of jiffies |
| 3931 | if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { |
| 3932 | WARN("JIFFIES_COMPARISON", |
| 3933 | "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); |
| 3934 | } |
| 3935 | |
Joe Perches | 9d7a34a | 2013-07-03 15:05:26 -0700 | [diff] [blame] | 3936 | # check for comparisons of get_jiffies_64() |
| 3937 | if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { |
| 3938 | WARN("JIFFIES_COMPARISON", |
| 3939 | "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); |
| 3940 | } |
| 3941 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3942 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3943 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3944 | # print "#ifdef in C files should be avoided\n"; |
| 3945 | # print "$herecurr"; |
| 3946 | # $clean = 0; |
| 3947 | # } |
| 3948 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3949 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3950 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 3951 | if (ERROR("SPACING", |
| 3952 | "exactly one space required after that #$1\n" . $herecurr) && |
| 3953 | $fix) { |
| 3954 | $fixed[$linenr - 1] =~ |
| 3955 | s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; |
| 3956 | } |
| 3957 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 3958 | } |
| 3959 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3960 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 3961 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 3962 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3963 | my $which = $1; |
| 3964 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3965 | CHK("UNCOMMENTED_DEFINITION", |
| 3966 | "$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3967 | } |
| 3968 | } |
| 3969 | # check for memory barriers without a comment. |
| 3970 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 3971 | if (!ctx_has_comment($first_line, $linenr)) { |
Joe Perches | c1fd7bb | 2013-11-12 15:10:11 -0800 | [diff] [blame] | 3972 | WARN("MEMORY_BARRIER", |
| 3973 | "memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 3974 | } |
| 3975 | } |
| 3976 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 3977 | 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] | 3978 | CHK("ARCH_DEFINES", |
| 3979 | "architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 3980 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 3981 | |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3982 | # Check that the storage class is at the beginning of a declaration |
| 3983 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3984 | WARN("STORAGE_CLASS", |
| 3985 | "storage class should be at the beginning of the declaration\n" . $herecurr) |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 3986 | } |
| 3987 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3988 | # check the location of the inline attribute, that it is between |
| 3989 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 3990 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 3991 | $line =~ /\b$Inline\s+$Storage\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 3992 | ERROR("INLINE_LOCATION", |
| 3993 | "inline keyword should sit between storage class and type\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 3994 | } |
| 3995 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 3996 | # Check for __inline__ and __inline, prefer inline |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 3997 | if ($realfile !~ m@\binclude/uapi/@ && |
| 3998 | $line =~ /\b(__inline__|__inline)\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 3999 | if (WARN("INLINE", |
| 4000 | "plain inline is preferred over $1\n" . $herecurr) && |
| 4001 | $fix) { |
| 4002 | $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/; |
| 4003 | |
| 4004 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4005 | } |
| 4006 | |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 4007 | # Check for __attribute__ packed, prefer __packed |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 4008 | if ($realfile !~ m@\binclude/uapi/@ && |
| 4009 | $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4010 | WARN("PREFER_PACKED", |
| 4011 | "__packed is preferred over __attribute__((packed))\n" . $herecurr); |
Joe Perches | 3d130fd | 2011-01-12 17:00:00 -0800 | [diff] [blame] | 4012 | } |
| 4013 | |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 4014 | # Check for __attribute__ aligned, prefer __aligned |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 4015 | if ($realfile !~ m@\binclude/uapi/@ && |
| 4016 | $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4017 | WARN("PREFER_ALIGNED", |
| 4018 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
Joe Perches | 39b7e28 | 2011-07-25 17:13:24 -0700 | [diff] [blame] | 4019 | } |
| 4020 | |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 4021 | # Check for __attribute__ format(printf, prefer __printf |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 4022 | if ($realfile !~ m@\binclude/uapi/@ && |
| 4023 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4024 | if (WARN("PREFER_PRINTF", |
| 4025 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && |
| 4026 | $fix) { |
| 4027 | $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; |
| 4028 | |
| 4029 | } |
Joe Perches | 5f14d3b | 2012-01-10 15:09:52 -0800 | [diff] [blame] | 4030 | } |
| 4031 | |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4032 | # Check for __attribute__ format(scanf, prefer __scanf |
Joe Perches | 2b7ab45 | 2013-11-12 15:10:14 -0800 | [diff] [blame] | 4033 | if ($realfile !~ m@\binclude/uapi/@ && |
| 4034 | $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4035 | if (WARN("PREFER_SCANF", |
| 4036 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && |
| 4037 | $fix) { |
| 4038 | $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; |
| 4039 | } |
Joe Perches | 6061d94 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4040 | } |
| 4041 | |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 4042 | # check for sizeof(&) |
| 4043 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4044 | WARN("SIZEOF_ADDRESS", |
| 4045 | "sizeof(& should be avoided\n" . $herecurr); |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 4046 | } |
| 4047 | |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 4048 | # check for sizeof without parenthesis |
| 4049 | if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4050 | if (WARN("SIZEOF_PARENTHESIS", |
| 4051 | "sizeof $1 should be sizeof($1)\n" . $herecurr) && |
| 4052 | $fix) { |
| 4053 | $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; |
| 4054 | } |
Joe Perches | 66c80b6 | 2012-07-30 14:41:22 -0700 | [diff] [blame] | 4055 | } |
| 4056 | |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 4057 | # check for line continuations in quoted strings with odd counts of " |
| 4058 | if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4059 | WARN("LINE_CONTINUATIONS", |
| 4060 | "Avoid line continuations in quoted strings\n" . $herecurr); |
Joe Perches | 428e2fd | 2011-05-24 17:13:39 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
Joe Perches | 88982fe | 2012-12-17 16:02:00 -0800 | [diff] [blame] | 4063 | # check for struct spinlock declarations |
| 4064 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 4065 | WARN("USE_SPINLOCK_T", |
| 4066 | "struct spinlock should be spinlock_t\n" . $herecurr); |
| 4067 | } |
| 4068 | |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 4069 | # check for seq_printf uses that could be seq_puts |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 4070 | if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 4071 | my $fmt = get_quoted_string($line, $rawline); |
Joe Perches | 0666872 | 2013-11-12 15:10:07 -0800 | [diff] [blame] | 4072 | if ($fmt ne "" && $fmt !~ /[^\\]\%/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4073 | if (WARN("PREFER_SEQ_PUTS", |
| 4074 | "Prefer seq_puts to seq_printf\n" . $herecurr) && |
| 4075 | $fix) { |
| 4076 | $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/; |
| 4077 | } |
Joe Perches | a6962d7 | 2013-04-29 16:18:13 -0700 | [diff] [blame] | 4078 | } |
| 4079 | } |
| 4080 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 4081 | # Check for misused memsets |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4082 | if ($^V && $^V ge 5.10.0 && |
| 4083 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4084 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 4085 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4086 | my $ms_addr = $2; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4087 | my $ms_val = $7; |
| 4088 | my $ms_size = $12; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4089 | |
Andy Whitcroft | 554e165 | 2012-01-10 15:09:57 -0800 | [diff] [blame] | 4090 | if ($ms_size =~ /^(0x|)0$/i) { |
| 4091 | ERROR("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4092 | "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] | 4093 | } elsif ($ms_size =~ /^(0x|)1$/i) { |
| 4094 | WARN("MEMSET", |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4095 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); |
| 4096 | } |
| 4097 | } |
| 4098 | |
Joe Perches | 98a9bba | 2014-01-23 15:54:52 -0800 | [diff] [blame] | 4099 | # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) |
| 4100 | if ($^V && $^V ge 5.10.0 && |
| 4101 | $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) { |
| 4102 | if (WARN("PREFER_ETHER_ADDR_COPY", |
| 4103 | "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) && |
| 4104 | $fix) { |
| 4105 | $fixed[$linenr - 1] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; |
| 4106 | } |
| 4107 | } |
| 4108 | |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4109 | # typecasts on min/max could be min_t/max_t |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4110 | if ($^V && $^V ge 5.10.0 && |
| 4111 | defined $stat && |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4112 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4113 | if (defined $2 || defined $7) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4114 | my $call = $1; |
| 4115 | my $cast1 = deparenthesize($2); |
| 4116 | my $arg1 = $3; |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4117 | my $cast2 = deparenthesize($7); |
| 4118 | my $arg2 = $8; |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4119 | my $cast; |
| 4120 | |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4121 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
Joe Perches | d7c76ba | 2012-01-10 15:09:58 -0800 | [diff] [blame] | 4122 | $cast = "$cast1 or $cast2"; |
| 4123 | } elsif ($cast1 ne "") { |
| 4124 | $cast = $cast1; |
| 4125 | } else { |
| 4126 | $cast = $cast2; |
| 4127 | } |
| 4128 | WARN("MINMAX", |
| 4129 | "$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] | 4130 | } |
| 4131 | } |
| 4132 | |
Joe Perches | 4a27319 | 2012-07-30 14:41:20 -0700 | [diff] [blame] | 4133 | # check usleep_range arguments |
| 4134 | if ($^V && $^V ge 5.10.0 && |
| 4135 | defined $stat && |
| 4136 | $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { |
| 4137 | my $min = $1; |
| 4138 | my $max = $7; |
| 4139 | if ($min eq $max) { |
| 4140 | WARN("USLEEP_RANGE", |
| 4141 | "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 4142 | } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && |
| 4143 | $min > $max) { |
| 4144 | WARN("USLEEP_RANGE", |
| 4145 | "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); |
| 4146 | } |
| 4147 | } |
| 4148 | |
Joe Perches | 823b794 | 2013-11-12 15:10:15 -0800 | [diff] [blame] | 4149 | # check for naked sscanf |
| 4150 | if ($^V && $^V ge 5.10.0 && |
| 4151 | defined $stat && |
| 4152 | $stat =~ /\bsscanf\b/ && |
| 4153 | ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && |
| 4154 | $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && |
| 4155 | $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { |
| 4156 | my $lc = $stat =~ tr@\n@@; |
| 4157 | $lc = $lc + $linenr; |
| 4158 | my $stat_real = raw_line($linenr, 0); |
| 4159 | for (my $count = $linenr + 1; $count <= $lc; $count++) { |
| 4160 | $stat_real = $stat_real . "\n" . raw_line($count, 0); |
| 4161 | } |
| 4162 | WARN("NAKED_SSCANF", |
| 4163 | "unchecked sscanf return value\n" . "$here\n$stat_real\n"); |
| 4164 | } |
| 4165 | |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 4166 | # check for new externs in .h files. |
| 4167 | if ($realfile =~ /\.h$/ && |
| 4168 | $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { |
Joe Perches | d1d8578 | 2013-09-24 15:27:46 -0700 | [diff] [blame] | 4169 | if (CHK("AVOID_EXTERNS", |
| 4170 | "extern prototypes should be avoided in .h files\n" . $herecurr) && |
Joe Perches | 70dc8a4 | 2013-09-11 14:23:58 -0700 | [diff] [blame] | 4171 | $fix) { |
| 4172 | $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; |
| 4173 | } |
| 4174 | } |
| 4175 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4176 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4177 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4178 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4179 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4180 | my $function_name = $1; |
| 4181 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4182 | |
| 4183 | my $s = $stat; |
| 4184 | if (defined $cond) { |
| 4185 | substr($s, 0, length($cond), ''); |
| 4186 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4187 | if ($s =~ /^\s*;/ && |
| 4188 | $function_name ne 'uninitialized_var') |
| 4189 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4190 | WARN("AVOID_EXTERNS", |
| 4191 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | if ($paren_space =~ /\n/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4195 | WARN("FUNCTION_ARGUMENTS", |
| 4196 | "arguments for function declarations should follow identifier\n" . $herecurr); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4197 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4198 | |
| 4199 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 4200 | $stat =~ /^.\s*extern\s+/) |
| 4201 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4202 | WARN("AVOID_EXTERNS", |
| 4203 | "externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | # checks for new __setup's |
| 4207 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 4208 | my $name = $1; |
| 4209 | |
| 4210 | if (!grep(/$name/, @setup_docs)) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4211 | CHK("UNDOCUMENTED_SETUP", |
| 4212 | "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 4213 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 4214 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 4215 | |
| 4216 | # check for pointless casting of kmalloc return |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 4217 | if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4218 | WARN("UNNECESSARY_CASTS", |
| 4219 | "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] | 4220 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4221 | |
Joe Perches | a640d25 | 2013-07-03 15:05:21 -0700 | [diff] [blame] | 4222 | # alloc style |
| 4223 | # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) |
| 4224 | if ($^V && $^V ge 5.10.0 && |
| 4225 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { |
| 4226 | CHK("ALLOC_SIZEOF_STRUCT", |
| 4227 | "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); |
| 4228 | } |
| 4229 | |
Joe Perches | 972fdea | 2013-04-29 16:18:12 -0700 | [diff] [blame] | 4230 | # check for krealloc arg reuse |
| 4231 | if ($^V && $^V ge 5.10.0 && |
| 4232 | $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { |
| 4233 | WARN("KREALLOC_ARG_REUSE", |
| 4234 | "Reusing the krealloc arg is almost always a bug\n" . $herecurr); |
| 4235 | } |
| 4236 | |
Joe Perches | 5ce59ae | 2013-02-21 16:44:18 -0800 | [diff] [blame] | 4237 | # check for alloc argument mismatch |
| 4238 | if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { |
| 4239 | WARN("ALLOC_ARRAY_ARGS", |
| 4240 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
| 4241 | } |
| 4242 | |
David Rientjes | 7e4915e | 2014-01-23 15:54:42 -0800 | [diff] [blame] | 4243 | # check for GFP_NOWAIT use |
| 4244 | if ($line =~ /\b__GFP_NOFAIL\b/) { |
| 4245 | WARN("__GFP_NOFAIL", |
| 4246 | "Use of __GFP_NOFAIL is deprecated, no new users should be added\n" . $herecurr); |
| 4247 | } |
| 4248 | |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 4249 | # check for multiple semicolons |
| 4250 | if ($line =~ /;\s*;\s*$/) { |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4251 | if (WARN("ONE_SEMICOLON", |
| 4252 | "Statements terminations use 1 semicolon\n" . $herecurr) && |
| 4253 | $fix) { |
| 4254 | $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g; |
| 4255 | } |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 4256 | } |
| 4257 | |
Joe Perches | c34c09a | 2014-01-23 15:54:43 -0800 | [diff] [blame] | 4258 | # check for case / default statements not preceeded by break/fallthrough/switch |
| 4259 | if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { |
| 4260 | my $has_break = 0; |
| 4261 | my $has_statement = 0; |
| 4262 | my $count = 0; |
| 4263 | my $prevline = $linenr; |
| 4264 | while ($prevline > 1 && $count < 3 && !$has_break) { |
| 4265 | $prevline--; |
| 4266 | my $rline = $rawlines[$prevline - 1]; |
| 4267 | my $fline = $lines[$prevline - 1]; |
| 4268 | last if ($fline =~ /^\@\@/); |
| 4269 | next if ($fline =~ /^\-/); |
| 4270 | next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); |
| 4271 | $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); |
| 4272 | next if ($fline =~ /^.[\s$;]*$/); |
| 4273 | $has_statement = 1; |
| 4274 | $count++; |
| 4275 | $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); |
| 4276 | } |
| 4277 | if (!$has_break && $has_statement) { |
| 4278 | WARN("MISSING_BREAK", |
| 4279 | "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); |
| 4280 | } |
| 4281 | } |
| 4282 | |
Joe Perches | d1e2ad0 | 2012-12-17 16:02:01 -0800 | [diff] [blame] | 4283 | # check for switch/default statements without a break; |
| 4284 | if ($^V && $^V ge 5.10.0 && |
| 4285 | defined $stat && |
| 4286 | $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { |
| 4287 | my $ctx = ''; |
| 4288 | my $herectx = $here . "\n"; |
| 4289 | my $cnt = statement_rawlines($stat); |
| 4290 | for (my $n = 0; $n < $cnt; $n++) { |
| 4291 | $herectx .= raw_line($linenr, $n) . "\n"; |
| 4292 | } |
| 4293 | WARN("DEFAULT_NO_BREAK", |
| 4294 | "switch default: should use break\n" . $herectx); |
Joe Perches | caf2a54 | 2011-01-12 16:59:56 -0800 | [diff] [blame] | 4295 | } |
| 4296 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4297 | # check for gcc specific __FUNCTION__ |
Joe Perches | d5e616f | 2013-09-11 14:23:54 -0700 | [diff] [blame] | 4298 | if ($line =~ /\b__FUNCTION__\b/) { |
| 4299 | if (WARN("USE_FUNC", |
| 4300 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && |
| 4301 | $fix) { |
| 4302 | $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g; |
| 4303 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4304 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4305 | |
Joe Perches | 2c92488 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4306 | # check for use of yield() |
| 4307 | if ($line =~ /\byield\s*\(\s*\)/) { |
| 4308 | WARN("YIELD", |
| 4309 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); |
| 4310 | } |
| 4311 | |
Joe Perches | 179f8f4 | 2013-07-03 15:05:30 -0700 | [diff] [blame] | 4312 | # check for comparisons against true and false |
| 4313 | if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { |
| 4314 | my $lead = $1; |
| 4315 | my $arg = $2; |
| 4316 | my $test = $3; |
| 4317 | my $otype = $4; |
| 4318 | my $trail = $5; |
| 4319 | my $op = "!"; |
| 4320 | |
| 4321 | ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); |
| 4322 | |
| 4323 | my $type = lc($otype); |
| 4324 | if ($type =~ /^(?:true|false)$/) { |
| 4325 | if (("$test" eq "==" && "$type" eq "true") || |
| 4326 | ("$test" eq "!=" && "$type" eq "false")) { |
| 4327 | $op = ""; |
| 4328 | } |
| 4329 | |
| 4330 | CHK("BOOL_COMPARISON", |
| 4331 | "Using comparison to $otype is error prone\n" . $herecurr); |
| 4332 | |
| 4333 | ## maybe suggesting a correct construct would better |
| 4334 | ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); |
| 4335 | |
| 4336 | } |
| 4337 | } |
| 4338 | |
Thomas Gleixner | 4882720b | 2010-09-07 14:34:01 +0000 | [diff] [blame] | 4339 | # check for semaphores initialized locked |
| 4340 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4341 | WARN("CONSIDER_COMPLETION", |
| 4342 | "consider using a completion\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4343 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4344 | |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 4345 | # recommend kstrto* over simple_strto* and strict_strto* |
| 4346 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4347 | WARN("CONSIDER_KSTRTO", |
Joe Perches | 67d0a07 | 2011-10-31 17:13:10 -0700 | [diff] [blame] | 4348 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4349 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4350 | |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 4351 | # check for __initcall(), use device_initcall() explicitly please |
| 4352 | if ($line =~ /^.\s*__initcall\s*\(/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4353 | WARN("USE_DEVICE_INITCALL", |
| 4354 | "please use device_initcall() instead of __initcall()\n" . $herecurr); |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 4355 | } |
Joe Perches | 6712d85 | 2012-03-23 15:02:20 -0700 | [diff] [blame] | 4356 | |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 4357 | # check for various ops structs, ensure they are const. |
| 4358 | my $struct_ops = qr{acpi_dock_ops| |
| 4359 | address_space_operations| |
| 4360 | backlight_ops| |
| 4361 | block_device_operations| |
| 4362 | dentry_operations| |
| 4363 | dev_pm_ops| |
| 4364 | dma_map_ops| |
| 4365 | extent_io_ops| |
| 4366 | file_lock_operations| |
| 4367 | file_operations| |
| 4368 | hv_ops| |
| 4369 | ide_dma_ops| |
| 4370 | intel_dvo_dev_ops| |
| 4371 | item_operations| |
| 4372 | iwl_ops| |
| 4373 | kgdb_arch| |
| 4374 | kgdb_io| |
| 4375 | kset_uevent_ops| |
| 4376 | lock_manager_operations| |
| 4377 | microcode_ops| |
| 4378 | mtrr_ops| |
| 4379 | neigh_ops| |
| 4380 | nlmsvc_binding| |
| 4381 | pci_raw_ops| |
| 4382 | pipe_buf_operations| |
| 4383 | platform_hibernation_ops| |
| 4384 | platform_suspend_ops| |
| 4385 | proto_ops| |
| 4386 | rpc_pipe_ops| |
| 4387 | seq_operations| |
| 4388 | snd_ac97_build_ops| |
| 4389 | soc_pcmcia_socket_ops| |
| 4390 | stacktrace_ops| |
| 4391 | sysfs_ops| |
| 4392 | tty_operations| |
| 4393 | usb_mon_operations| |
| 4394 | wd_ops}x; |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 4395 | if ($line !~ /\bconst\b/ && |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 4396 | $line =~ /\bstruct\s+($struct_ops)\b/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4397 | WARN("CONST_STRUCT", |
| 4398 | "struct $1 should normally be const\n" . |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 4399 | $herecurr); |
Andy Whitcroft | 2b6db5c | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 4400 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4401 | |
| 4402 | # use of NR_CPUS is usually wrong |
| 4403 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 4404 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 4405 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 4406 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 4407 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 4408 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 4409 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 4410 | { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4411 | WARN("NR_CPUS", |
| 4412 | "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] | 4413 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4414 | |
Joe Perches | 52ea850 | 2013-11-12 15:10:09 -0800 | [diff] [blame] | 4415 | # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. |
| 4416 | if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { |
| 4417 | ERROR("DEFINE_ARCH_HAS", |
| 4418 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); |
| 4419 | } |
| 4420 | |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4421 | # check for %L{u,d,i} in strings |
| 4422 | my $string; |
| 4423 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 4424 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
Andy Whitcroft | 2a1bc5d | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 4425 | $string =~ s/%%/__/g; |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4426 | if ($string =~ /(?<!%)%L[udi]/) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4427 | WARN("PRINTF_L", |
| 4428 | "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 4429 | last; |
| 4430 | } |
| 4431 | } |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 4432 | |
| 4433 | # whine mightly about in_atomic |
| 4434 | if ($line =~ /\bin_atomic\s*\(/) { |
| 4435 | if ($realfile =~ m@^drivers/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4436 | ERROR("IN_ATOMIC", |
| 4437 | "do not use in_atomic in drivers\n" . $herecurr); |
Andy Whitcroft | f4a8773 | 2009-02-27 14:03:05 -0800 | [diff] [blame] | 4438 | } elsif ($realfile !~ m@^kernel/@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4439 | WARN("IN_ATOMIC", |
| 4440 | "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 4441 | } |
| 4442 | } |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 4443 | |
| 4444 | # check for lockdep_set_novalidate_class |
| 4445 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 4446 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 4447 | if ($realfile !~ m@^kernel/lockdep@ && |
| 4448 | $realfile !~ m@^include/linux/lockdep@ && |
| 4449 | $realfile !~ m@^drivers/base/core@) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4450 | ERROR("LOCKDEP", |
| 4451 | "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 4452 | } |
| 4453 | } |
Dave Jones | 88f8831 | 2011-01-12 16:59:59 -0800 | [diff] [blame] | 4454 | |
| 4455 | if ($line =~ /debugfs_create_file.*S_IWUGO/ || |
| 4456 | $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4457 | WARN("EXPORTED_WORLD_WRITABLE", |
| 4458 | "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] | 4459 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4460 | } |
| 4461 | |
| 4462 | # If we have no input at all, then there is nothing to report on |
| 4463 | # so just keep quiet. |
| 4464 | if ($#rawlines == -1) { |
| 4465 | exit(0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4466 | } |
| 4467 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4468 | # In mailback mode only produce a report in the negative, for |
| 4469 | # things that appear to be patches. |
| 4470 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 4471 | exit(0); |
| 4472 | } |
| 4473 | |
| 4474 | # This is not a patch, and we are are in 'no-patch' mode so |
| 4475 | # just keep quiet. |
| 4476 | if (!$chk_patch && !$is_patch) { |
| 4477 | exit(0); |
| 4478 | } |
| 4479 | |
| 4480 | if (!$is_patch) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4481 | ERROR("NOT_UNIFIED_DIFF", |
| 4482 | "Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4483 | } |
| 4484 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4485 | ERROR("MISSING_SIGN_OFF", |
| 4486 | "Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4487 | } |
| 4488 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4489 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4490 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 4491 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4492 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 4493 | (($check)? "$cnt_chk checks, " : "") . |
| 4494 | "$cnt_lines lines checked\n"; |
| 4495 | print "\n" if ($quiet == 0); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 4496 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 4497 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 4498 | if ($quiet == 0) { |
Joe Perches | d1fe9c0 | 2012-03-23 15:02:16 -0700 | [diff] [blame] | 4499 | |
| 4500 | if ($^V lt 5.10.0) { |
| 4501 | print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); |
| 4502 | print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); |
| 4503 | } |
| 4504 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 4505 | # If there were whitespace errors which cleanpatch can fix |
| 4506 | # then suggest that. |
| 4507 | if ($rpt_cleaners) { |
| 4508 | print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; |
| 4509 | print " scripts/cleanfile\n\n"; |
Mike Frysinger | b078121 | 2011-03-22 16:34:43 -0700 | [diff] [blame] | 4510 | $rpt_cleaners = 0; |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 4511 | } |
| 4512 | } |
| 4513 | |
Joe Perches | 91bfe48 | 2013-09-11 14:23:59 -0700 | [diff] [blame] | 4514 | hash_show_words(\%use_type, "Used"); |
| 4515 | hash_show_words(\%ignore_type, "Ignored"); |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4516 | |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4517 | if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { |
Joe Perches | 9624b8d | 2014-01-23 15:54:44 -0800 | [diff] [blame] | 4518 | my $newfile = $filename; |
| 4519 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); |
Joe Perches | 3705ce5 | 2013-07-03 15:05:31 -0700 | [diff] [blame] | 4520 | my $linecount = 0; |
| 4521 | my $f; |
| 4522 | |
| 4523 | open($f, '>', $newfile) |
| 4524 | or die "$P: Can't open $newfile for write\n"; |
| 4525 | foreach my $fixed_line (@fixed) { |
| 4526 | $linecount++; |
| 4527 | if ($file) { |
| 4528 | if ($linecount > 3) { |
| 4529 | $fixed_line =~ s/^\+//; |
| 4530 | print $f $fixed_line. "\n"; |
| 4531 | } |
| 4532 | } else { |
| 4533 | print $f $fixed_line . "\n"; |
| 4534 | } |
| 4535 | } |
| 4536 | close($f); |
| 4537 | |
| 4538 | if (!$quiet) { |
| 4539 | print << "EOM"; |
| 4540 | Wrote EXPERIMENTAL --fix correction(s) to '$newfile' |
| 4541 | |
| 4542 | Do _NOT_ trust the results written to this file. |
| 4543 | Do _NOT_ submit these changes without inspecting them for correctness. |
| 4544 | |
| 4545 | This EXPERIMENTAL file is simply a convenience to help rewrite patches. |
| 4546 | No warranties, expressed or implied... |
| 4547 | |
| 4548 | EOM |
| 4549 | } |
| 4550 | } |
| 4551 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4552 | if ($clean == 1 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 4553 | print "$vname has no obvious style problems and is ready for submission.\n" |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4554 | } |
| 4555 | if ($clean == 0 && $quiet == 0) { |
Joe Perches | 000d1cc1 | 2011-07-25 17:13:25 -0700 | [diff] [blame] | 4556 | print << "EOM"; |
| 4557 | $vname has style problems, please review. |
| 4558 | |
| 4559 | If any of these errors are false positives, please report |
| 4560 | them to the maintainer, see CHECKPATCH in MAINTAINERS. |
| 4561 | EOM |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4562 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 4563 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4564 | return $clean; |
| 4565 | } |