Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (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 | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4 | # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc) |
| 5 | # Licensed under the terms of the GNU GPL License version 2 |
| 6 | |
| 7 | use strict; |
| 8 | |
| 9 | my $P = $0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 10 | $P =~ s@.*/@@g; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 11 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 12 | my $V = '0.05'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 13 | |
| 14 | use Getopt::Long qw(:config no_auto_abbrev); |
| 15 | |
| 16 | my $quiet = 0; |
| 17 | my $tree = 1; |
| 18 | my $chk_signoff = 1; |
| 19 | my $chk_patch = 1; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 20 | my $tst_type = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 21 | GetOptions( |
| 22 | 'q|quiet' => \$quiet, |
| 23 | 'tree!' => \$tree, |
| 24 | 'signoff!' => \$chk_signoff, |
| 25 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 26 | 'test-type!' => \$tst_type, |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 27 | ) or exit; |
| 28 | |
| 29 | my $exit = 0; |
| 30 | |
| 31 | if ($#ARGV < 0) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 32 | print "usage: $P [options] patchfile\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 33 | print "version: $V\n"; |
| 34 | print "options: -q => quiet\n"; |
| 35 | print " --no-tree => run without a kernel tree\n"; |
| 36 | exit(1); |
| 37 | } |
| 38 | |
| 39 | if ($tree && !top_of_kernel_tree()) { |
| 40 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 41 | exit(2); |
| 42 | } |
| 43 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 44 | my @dep_includes = (); |
| 45 | my @dep_functions = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 46 | my $removal = 'Documentation/feature-removal-schedule.txt'; |
| 47 | if ($tree && -f $removal) { |
| 48 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; |
| 49 | while (<REMOVE>) { |
| 50 | if (/^Files:\s+(.*\S)/) { |
| 51 | for my $file (split(/[, ]+/, $1)) { |
| 52 | if ($file =~ m@include/(.*)@) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 53 | push(@dep_includes, $1); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 56 | |
| 57 | } elsif (/^Funcs:\s+(.*\S)/) { |
| 58 | for my $func (split(/[, ]+/, $1)) { |
| 59 | push(@dep_functions, $func); |
| 60 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 65 | my @rawlines = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 66 | while (<>) { |
| 67 | chomp; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 68 | push(@rawlines, $_); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 69 | if (eof(ARGV)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 70 | if (!process($ARGV, @rawlines)) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 71 | $exit = 1; |
| 72 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 73 | @rawlines = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | exit($exit); |
| 78 | |
| 79 | sub top_of_kernel_tree { |
| 80 | if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") && |
| 81 | (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") && |
| 82 | (-d "Documentation") && (-d "arch") && (-d "include") && |
| 83 | (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") && |
| 84 | (-d "kernel") && (-d "lib") && (-d "scripts")) { |
| 85 | return 1; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | sub expand_tabs { |
| 91 | my ($str) = @_; |
| 92 | |
| 93 | my $res = ''; |
| 94 | my $n = 0; |
| 95 | for my $c (split(//, $str)) { |
| 96 | if ($c eq "\t") { |
| 97 | $res .= ' '; |
| 98 | $n++; |
| 99 | for (; ($n % 8) != 0; $n++) { |
| 100 | $res .= ' '; |
| 101 | } |
| 102 | next; |
| 103 | } |
| 104 | $res .= $c; |
| 105 | $n++; |
| 106 | } |
| 107 | |
| 108 | return $res; |
| 109 | } |
| 110 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 111 | sub line_stats { |
| 112 | my ($line) = @_; |
| 113 | |
| 114 | # Drop the diff line leader and expand tabs |
| 115 | $line =~ s/^.//; |
| 116 | $line = expand_tabs($line); |
| 117 | |
| 118 | # Pick the indent from the front of the line. |
| 119 | my ($white) = ($line =~ /^(\s*)/); |
| 120 | |
| 121 | return (length($line), length($white)); |
| 122 | } |
| 123 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 124 | sub sanitise_line { |
| 125 | my ($line) = @_; |
| 126 | |
| 127 | my $res = ''; |
| 128 | my $l = ''; |
| 129 | |
| 130 | my $quote = ''; |
| 131 | |
| 132 | foreach my $c (split(//, $line)) { |
| 133 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) { |
| 134 | if ($quote eq '') { |
| 135 | $quote = $c; |
| 136 | $res .= $c; |
| 137 | $l = $c; |
| 138 | next; |
| 139 | } elsif ($quote eq $c) { |
| 140 | $quote = ''; |
| 141 | } |
| 142 | } |
| 143 | if ($quote && $c ne "\t") { |
| 144 | $res .= "X"; |
| 145 | } else { |
| 146 | $res .= $c; |
| 147 | } |
| 148 | |
| 149 | $l = $c; |
| 150 | } |
| 151 | |
| 152 | return $res; |
| 153 | } |
| 154 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 155 | sub ctx_block_get { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 156 | my ($linenr, $remain, $outer, $open, $close) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 157 | my $line; |
| 158 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 159 | my $blk = ''; |
| 160 | my @o; |
| 161 | my @c; |
| 162 | my @res = (); |
| 163 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 164 | for ($line = $start; $remain > 0; $line++) { |
| 165 | next if ($rawlines[$line] =~ /^-/); |
| 166 | $remain--; |
| 167 | |
| 168 | $blk .= $rawlines[$line]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 169 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 170 | @o = ($blk =~ /$open/g); |
| 171 | @c = ($blk =~ /$close/g); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 172 | |
| 173 | if (!$outer || (scalar(@o) - scalar(@c)) == 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 174 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | last if (scalar(@o) == scalar(@c)); |
| 178 | } |
| 179 | |
| 180 | return @res; |
| 181 | } |
| 182 | sub ctx_block_outer { |
| 183 | my ($linenr, $remain) = @_; |
| 184 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 185 | return ctx_block_get($linenr, $remain, 1, '\{', '\}'); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 186 | } |
| 187 | sub ctx_block { |
| 188 | my ($linenr, $remain) = @_; |
| 189 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 190 | return ctx_block_get($linenr, $remain, 0, '\{', '\}'); |
| 191 | } |
| 192 | sub ctx_statement { |
| 193 | my ($linenr, $remain) = @_; |
| 194 | |
| 195 | return ctx_block_get($linenr, $remain, 0, '\(', '\)'); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | sub ctx_locate_comment { |
| 199 | my ($first_line, $end_line) = @_; |
| 200 | |
| 201 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 202 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 203 | return $current_comment if (defined $current_comment); |
| 204 | |
| 205 | # Look through the context and try and figure out if there is a |
| 206 | # comment. |
| 207 | my $in_comment = 0; |
| 208 | $current_comment = ''; |
| 209 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 210 | my $line = $rawlines[$linenr - 1]; |
| 211 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 212 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 213 | $in_comment = 1; |
| 214 | } |
| 215 | if ($line =~ m@/\*@) { |
| 216 | $in_comment = 1; |
| 217 | } |
| 218 | if (!$in_comment && $current_comment ne '') { |
| 219 | $current_comment = ''; |
| 220 | } |
| 221 | $current_comment .= $line . "\n" if ($in_comment); |
| 222 | if ($line =~ m@\*/@) { |
| 223 | $in_comment = 0; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | chomp($current_comment); |
| 228 | return($current_comment); |
| 229 | } |
| 230 | sub ctx_has_comment { |
| 231 | my ($first_line, $end_line) = @_; |
| 232 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 233 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 234 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 235 | ##print "CMMT: $cmt\n"; |
| 236 | |
| 237 | return ($cmt ne ''); |
| 238 | } |
| 239 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 240 | sub cat_vet { |
| 241 | my ($vet) = @_; |
| 242 | |
| 243 | $vet =~ s/\t/^I/; |
| 244 | $vet =~ s/$/\$/; |
| 245 | |
| 246 | return $vet; |
| 247 | } |
| 248 | |
| 249 | sub process { |
| 250 | my $filename = shift; |
| 251 | my @lines = @_; |
| 252 | |
| 253 | my $linenr=0; |
| 254 | my $prevline=""; |
| 255 | my $stashline=""; |
| 256 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 257 | my $length; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 258 | my $indent; |
| 259 | my $previndent=0; |
| 260 | my $stashindent=0; |
| 261 | |
| 262 | my $clean = 1; |
| 263 | my $signoff = 0; |
| 264 | my $is_patch = 0; |
| 265 | |
| 266 | # Trace the real file/line as we go. |
| 267 | my $realfile = ''; |
| 268 | my $realline = 0; |
| 269 | my $realcnt = 0; |
| 270 | my $here = ''; |
| 271 | my $in_comment = 0; |
| 272 | my $first_line = 0; |
| 273 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 274 | my $ident = '[A-Za-z\d_]+'; |
| 275 | my $storage = '(?:extern|static)'; |
| 276 | my $sparse = '(?:__user|__kernel|__force|__iomem)'; |
| 277 | my $type = '(?:unsigned\s+)?' . |
| 278 | '(?:void|char|short|int|long|unsigned|float|double|' . |
| 279 | 'long\s+long|' . |
| 280 | "struct\\s+${ident}|" . |
| 281 | "union\\s+${ident}|" . |
| 282 | "${ident}_t)" . |
| 283 | "(?:\\s+$sparse)*" . |
| 284 | '(?:\s*\*+)?'; |
| 285 | my $attribute = '(?:__read_mostly|__init|__initdata)'; |
| 286 | |
| 287 | my $Ident = $ident; |
| 288 | my $Type = $type; |
| 289 | my $Storage = $storage; |
| 290 | my $Declare = "(?:$storage\\s+)?$type"; |
| 291 | my $Attribute = $attribute; |
| 292 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 293 | foreach my $line (@lines) { |
| 294 | $linenr++; |
| 295 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 296 | my $rawline = $line; |
| 297 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 298 | #extract the filename as it passes |
| 299 | if ($line=~/^\+\+\+\s+(\S+)/) { |
| 300 | $realfile=$1; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 301 | $realfile =~ s@^[^/]*/@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 302 | $in_comment = 0; |
| 303 | next; |
| 304 | } |
| 305 | #extract the line range in the file after the patch is applied |
| 306 | if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) { |
| 307 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 308 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 309 | $in_comment = 0; |
| 310 | $realline=$1-1; |
| 311 | if (defined $2) { |
| 312 | $realcnt=$3+1; |
| 313 | } else { |
| 314 | $realcnt=1+1; |
| 315 | } |
| 316 | next; |
| 317 | } |
| 318 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 319 | # track the line number as we move through the hunk, note that |
| 320 | # new versions of GNU diff omit the leading space on completely |
| 321 | # blank context lines so we need to count that too. |
| 322 | if ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 323 | $realline++; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 324 | |
| 325 | # track any sort of multi-line comment. Obviously if |
| 326 | # the added text or context do not include the whole |
| 327 | # comment we will not see it. Such is life. |
| 328 | # |
| 329 | # Guestimate if this is a continuing comment. If this |
| 330 | # is the start of a diff block and this line starts |
| 331 | # ' *' then it is very likely a comment. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 332 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 333 | $in_comment = 1; |
| 334 | } |
| 335 | if ($line =~ m@/\*@) { |
| 336 | $in_comment = 1; |
| 337 | } |
| 338 | if ($line =~ m@\*/@) { |
| 339 | $in_comment = 0; |
| 340 | } |
| 341 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 342 | # Measure the line length and indent. |
| 343 | ($length, $indent) = line_stats($line); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 344 | |
| 345 | # Track the previous line. |
| 346 | ($prevline, $stashline) = ($stashline, $line); |
| 347 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 348 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 349 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 350 | |
| 351 | #make up the handle for any error we report on this line |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 352 | $here = "#$linenr: "; |
| 353 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 354 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 355 | my $hereline = "$here\n$line\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 356 | my $herecurr = "$here\n$line\n\n"; |
| 357 | my $hereprev = "$here\n$prevline\n$line\n\n"; |
| 358 | |
| 359 | #check the patch for a signoff: |
| 360 | if ($line =~ /^\s*Signed-off-by:\s/) { |
| 361 | $signoff++; |
| 362 | |
| 363 | } elsif ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 364 | # This is a signoff, if ugly, so do not double report. |
| 365 | $signoff++; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 366 | if (!($line =~ /^\s*Signed-off-by:/)) { |
| 367 | print "use Signed-off-by:\n"; |
| 368 | print "$herecurr"; |
| 369 | $clean = 0; |
| 370 | } |
| 371 | if ($line =~ /^\s*signed-off-by:\S/i) { |
| 372 | print "need space after Signed-off-by:\n"; |
| 373 | print "$herecurr"; |
| 374 | $clean = 0; |
| 375 | } |
| 376 | } |
| 377 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 378 | # Check for wrappage within a valid hunk of the file |
| 379 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) { |
| 380 | print "patch seems to be corrupt (line wrapped?) [$realcnt]\n"; |
| 381 | print "$herecurr"; |
| 382 | $clean = 0; |
| 383 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 384 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 385 | #ignore lines being removed |
| 386 | if ($line=~/^-/) {next;} |
| 387 | |
| 388 | # check we are in a valid source file if not then ignore this hunk |
| 389 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 390 | |
| 391 | #trailing whitespace |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 392 | if ($line=~/^\+.*\S\s+$/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 393 | my $herevet = "$here\n" . cat_vet($line) . "\n\n"; |
| 394 | print "trailing whitespace\n"; |
| 395 | print "$herevet"; |
| 396 | $clean = 0; |
| 397 | } |
| 398 | #80 column limit |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 399 | if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 400 | print "line over 80 characters\n"; |
| 401 | print "$herecurr"; |
| 402 | $clean = 0; |
| 403 | } |
| 404 | |
| 405 | # check we are in a valid source file *.[hc] if not then ignore this hunk |
| 406 | next if ($realfile !~ /\.[hc]$/); |
| 407 | |
| 408 | # at the beginning of a line any tabs must come first and anything |
| 409 | # more than 8 must use tabs. |
| 410 | if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { |
| 411 | my $herevet = "$here\n" . cat_vet($line) . "\n\n"; |
| 412 | print "use tabs not spaces\n"; |
| 413 | print "$herevet"; |
| 414 | $clean = 0; |
| 415 | } |
| 416 | |
| 417 | # |
| 418 | # The rest of our checks refer specifically to C style |
| 419 | # only apply those _outside_ comments. |
| 420 | # |
| 421 | next if ($in_comment); |
| 422 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 423 | # Remove comments from the line before processing. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 424 | $line =~ s@/\*.*\*/@@g; |
| 425 | $line =~ s@/\*.*@@; |
| 426 | $line =~ s@.*\*/@@; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 427 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 428 | # Standardise the strings and chars within the input to simplify matching. |
| 429 | $line = sanitise_line($line); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 430 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 431 | # |
| 432 | # Checks which may be anchored in the context. |
| 433 | # |
| 434 | |
| 435 | # Check for switch () and associated case and default |
| 436 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 437 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 438 | my $err = ''; |
| 439 | my $sep = ''; |
| 440 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 441 | shift(@ctx); |
| 442 | for my $ctx (@ctx) { |
| 443 | my ($clen, $cindent) = line_stats($ctx); |
| 444 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 445 | $indent != $cindent) { |
| 446 | $err .= "$sep$ctx\n"; |
| 447 | $sep = ''; |
| 448 | } else { |
| 449 | $sep = "[...]\n"; |
| 450 | } |
| 451 | } |
| 452 | if ($err ne '') { |
| 453 | print "switch and case should be at the same indent\n"; |
| 454 | print "$here\n$line\n$err\n"; |
| 455 | $clean = 0; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | #ignore lines not being added |
| 460 | if ($line=~/^[^\+]/) {next;} |
| 461 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 462 | # TEST: allow direct testing of the type matcher. |
| 463 | if ($tst_type && $line =~ /^.$Declare$/) { |
| 464 | print "TEST: is type $Declare\n"; |
| 465 | print "$herecurr"; |
| 466 | $clean = 0; |
| 467 | next; |
| 468 | } |
| 469 | |
| 470 | # |
| 471 | # Checks which are anchored on the added line. |
| 472 | # |
| 473 | |
| 474 | # check for malformed paths in #include statements (uses RAW line) |
| 475 | if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { |
| 476 | my $path = $1; |
| 477 | if ($path =~ m{//}) { |
| 478 | print "malformed #include filename\n"; |
| 479 | print "$herecurr"; |
| 480 | $clean = 0; |
| 481 | } |
| 482 | # Sanitise this special form of string. |
| 483 | $path = 'X' x length($path); |
| 484 | $line =~ s{\<.*\>}{<$path>}; |
| 485 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 486 | |
| 487 | # no C99 // comments |
| 488 | if ($line =~ m{//}) { |
| 489 | print "do not use C99 // comments\n"; |
| 490 | print "$herecurr"; |
| 491 | $clean = 0; |
| 492 | } |
| 493 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 494 | $line =~ s@//.*@@; |
| 495 | |
| 496 | #EXPORT_SYMBOL should immediately follow its function closing }. |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 497 | if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || |
| 498 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 499 | my $name = $1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 500 | if (($prevline !~ /^}/) && |
| 501 | ($prevline !~ /^\+}/) && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 502 | ($prevline !~ /^ }/) && |
| 503 | ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) { |
| 504 | print "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 505 | print "$herecurr"; |
| 506 | $clean = 0; |
| 507 | } |
| 508 | } |
| 509 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 510 | # check for static initialisers. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 511 | if ($line=~/\s*static\s.*=\s+(0|NULL);/) { |
| 512 | print "do not initialise statics to 0 or NULL\n"; |
| 513 | print "$herecurr"; |
| 514 | $clean = 0; |
| 515 | } |
| 516 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 517 | # check for new typedefs, only function parameters and sparse annotations |
| 518 | # make sense. |
| 519 | if ($line =~ /\btypedef\s/ && |
| 520 | $line !~ /\btypedef\s+$Type\s+\(\s*$Ident\s*\)\s*\(/ && |
| 521 | $line !~ /\b__bitwise(?:__|)\b/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 522 | print "do not add new typedefs\n"; |
| 523 | print "$herecurr"; |
| 524 | $clean = 0; |
| 525 | } |
| 526 | |
| 527 | # * goes on variable not on type |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 528 | if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) { |
| 529 | print "\"foo$1 bar\" should be \"foo $1bar\"\n"; |
| 530 | print "$herecurr"; |
| 531 | $clean = 0; |
| 532 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 533 | if ($line =~ m{$Type (\*) [A-Za-z\d_]+} || |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 534 | $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) { |
| 535 | print "\"foo $1 bar\" should be \"foo $1bar\"\n"; |
| 536 | print "$herecurr"; |
| 537 | $clean = 0; |
| 538 | } |
| 539 | if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_](\*+)\)}) { |
| 540 | print "\"(foo$1)\" should be \"(foo $1)\"\n"; |
| 541 | print "$herecurr"; |
| 542 | $clean = 0; |
| 543 | } |
| 544 | if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_]\s+(\*+)\s+\)}) { |
| 545 | print "\"(foo $1 )\" should be \"(foo $1)\"\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 546 | print "$herecurr"; |
| 547 | $clean = 0; |
| 548 | } |
| 549 | |
| 550 | # # no BUG() or BUG_ON() |
| 551 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 552 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 553 | # print "$herecurr"; |
| 554 | # $clean = 0; |
| 555 | # } |
| 556 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 557 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 558 | # same line do not need a level, so we use the current block context |
| 559 | # to try and find and validate the current printk. In summary the current |
| 560 | # printk includes all preceeding printk's which have no newline on the end. |
| 561 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 562 | if ($line =~ /\bprintk\((?!KERN_)/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 563 | my $ok = 0; |
| 564 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 565 | #print "CHECK<$lines[$ln - 1]\n"; |
| 566 | # we have a preceeding printk if it ends |
| 567 | # with "\n" ignore it, else it is to blame |
| 568 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 569 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 570 | $ok = 1; |
| 571 | } |
| 572 | last; |
| 573 | } |
| 574 | } |
| 575 | if ($ok == 0) { |
| 576 | print "printk() should include KERN_ facility level\n"; |
| 577 | print "$herecurr"; |
| 578 | $clean = 0; |
| 579 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 582 | # function brace can't be on same line, except for #defines of do while, |
| 583 | # or if closed on same line |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 584 | if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and |
| 585 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { |
| 586 | print "braces following function declarations go on the next line\n"; |
| 587 | print "$herecurr"; |
| 588 | $clean = 0; |
| 589 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 590 | |
| 591 | # Check operator spacing. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 592 | # Note we expand the line with the leading + as the real |
| 593 | # line will be displayed with the leading + and the tabs |
| 594 | # will therefore also expand that way. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 595 | my $opline = $line; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 596 | $opline = expand_tabs($opline); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 597 | $opline =~ s/^./ /; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 598 | if (!($line=~/\#\s*include/)) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 599 | my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 600 | my $off = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 601 | for (my $n = 0; $n < $#elements; $n += 2) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 602 | $off += length($elements[$n]); |
| 603 | |
| 604 | my $a = ''; |
| 605 | $a = 'V' if ($elements[$n] ne ''); |
| 606 | $a = 'W' if ($elements[$n] =~ /\s$/); |
| 607 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 608 | $a = 'O' if ($elements[$n] eq ''); |
| 609 | $a = 'E' if ($elements[$n] eq '' && $n == 0); |
| 610 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 611 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 612 | |
| 613 | my $c = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 614 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 615 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 616 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
| 617 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 618 | $c = 'O' if ($elements[$n + 2] eq ''); |
| 619 | } else { |
| 620 | $c = 'E'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 623 | # Pick up the preceeding and succeeding characters. |
| 624 | my $ca = substr($opline, $off - 1, 1); |
| 625 | my $cc = ''; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 626 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 627 | $cc = substr($opline, $off + length($elements[$n + 1]), 1); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 630 | my $ctx = "${a}x${c}"; |
| 631 | |
| 632 | my $at = "(ctx:$ctx)"; |
| 633 | |
| 634 | my $ptr = (" " x $off) . "^"; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 635 | my $hereptr = "$hereline$ptr\n\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 636 | |
| 637 | ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 638 | |
| 639 | # We need ; as an operator. // is a comment. |
| 640 | if ($op eq ';' or $op eq '//') { |
| 641 | |
| 642 | # -> should have no spaces |
| 643 | } elsif ($op eq '->') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 644 | if ($ctx =~ /Wx.|.xW/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 645 | print "no spaces around that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 646 | print "$hereptr"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 647 | $clean = 0; |
| 648 | } |
| 649 | |
| 650 | # , must have a space on the right. |
| 651 | } elsif ($op eq ',') { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 652 | if ($ctx !~ /.xW|.xE/ && $cc ne '}') { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 653 | print "need space after that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 654 | print "$hereptr"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 655 | $clean = 0; |
| 656 | } |
| 657 | |
| 658 | # unary ! and unary ~ are allowed no space on the right |
| 659 | } elsif ($op eq '!' or $op eq '~') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 660 | if ($ctx !~ /[WOEB]x./) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 661 | print "need space before that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 662 | print "$hereptr"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 663 | $clean = 0; |
| 664 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 665 | if ($ctx =~ /.xW/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 666 | print "no space after that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 667 | print "$hereptr"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 668 | $clean = 0; |
| 669 | } |
| 670 | |
| 671 | # unary ++ and unary -- are allowed no space on one side. |
| 672 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 673 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOB]/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 674 | print "need space one side of that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 675 | print "$hereptr"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 676 | $clean = 0; |
| 677 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 678 | if ($ctx =~ /Wx./ && $cc eq ';') { |
| 679 | print "no space before that '$op' $at\n"; |
| 680 | print "$hereptr"; |
| 681 | $clean = 0; |
| 682 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 683 | |
| 684 | # & is both unary and binary |
| 685 | # unary: |
| 686 | # a &b |
| 687 | # binary (consistent spacing): |
| 688 | # a&b OK |
| 689 | # a & b OK |
| 690 | # |
| 691 | # boiling down to: if there is a space on the right then there |
| 692 | # should be one on the left. |
| 693 | # |
| 694 | # - is the same |
| 695 | # |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 696 | } elsif ($op eq '&' or $op eq '-') { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 697 | if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 698 | print "need space before that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 699 | print "$hereptr"; |
| 700 | $clean = 0; |
| 701 | } |
| 702 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 703 | # * is the same as & only adding: |
| 704 | # type: |
| 705 | # (foo *) |
| 706 | # (foo **) |
| 707 | # |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 708 | } elsif ($op eq '*') { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 709 | if ($ca eq '*') { |
| 710 | if ($cc =~ /\s/) { |
| 711 | print "no space after that '$op' $at\n"; |
| 712 | print "$hereptr"; |
| 713 | $clean = 0; |
| 714 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 715 | } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 716 | print "need space before that '$op' $at\n"; |
| 717 | print "$hereptr"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 718 | $clean = 0; |
| 719 | } |
| 720 | |
| 721 | # << and >> may either have or not have spaces both sides |
| 722 | } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or |
| 723 | $op eq '^' or $op eq '|') |
| 724 | { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 725 | if ($ctx !~ /VxV|WxW|VxE|WxE/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 726 | print "need consistent spacing around '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 727 | print "$hereptr"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 728 | $clean = 0; |
| 729 | } |
| 730 | |
| 731 | # All the others need spaces both sides. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 732 | } elsif ($ctx !~ /[EW]x[WE]/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 733 | print "need spaces around that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 734 | print "$hereptr"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 735 | $clean = 0; |
| 736 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 737 | $off += length($elements[$n + 1]); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
| 741 | #need space before brace following if, while, etc |
| 742 | if ($line=~/\(.*\){/) { |
| 743 | print "need a space before the brace\n"; |
| 744 | print "$herecurr"; |
| 745 | $clean = 0; |
| 746 | } |
| 747 | |
| 748 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 749 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 750 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
| 751 | print "labels should not be indented\n"; |
| 752 | print "$herecurr"; |
| 753 | $clean = 0; |
| 754 | } |
| 755 | |
| 756 | # Need a space before open parenthesis after if, while etc |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 757 | if ($line=~/\b(if|while|for|switch)\(/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 758 | print "need a space before the open parenthesis\n"; |
| 759 | print "$herecurr"; |
| 760 | $clean = 0; |
| 761 | } |
| 762 | |
| 763 | # Check for illegal assignment in if conditional. |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 764 | if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 765 | #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 766 | print "do not use assignment in if condition\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 767 | print "$herecurr"; |
| 768 | $clean = 0; |
| 769 | } |
| 770 | |
| 771 | # Check for }<nl>else {, these must be at the same |
| 772 | # indent level to be relevant to each other. |
| 773 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 774 | $previndent == $indent) { |
| 775 | print "else should follow close brace\n"; |
| 776 | print "$hereprev"; |
| 777 | $clean = 0; |
| 778 | } |
| 779 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 780 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
| 781 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { |
| 782 | # print "No studly caps, use _\n"; |
| 783 | # print "$herecurr"; |
| 784 | # $clean = 0; |
| 785 | # } |
| 786 | |
| 787 | #no spaces allowed after \ in define |
| 788 | if ($line=~/\#define.*\\\s$/) { |
| 789 | print("Whitepspace after \\ makes next lines useless\n"); |
| 790 | print "$herecurr"; |
| 791 | $clean = 0; |
| 792 | } |
| 793 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 794 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
| 795 | if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 796 | my $checkfile = "include/linux/$1.h"; |
| 797 | if (-f $checkfile) { |
| 798 | print "Use #include <linux/$1.h> instead of <asm/$1.h>\n"; |
| 799 | print $herecurr; |
| 800 | $clean = 0; |
| 801 | } |
| 802 | } |
| 803 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 804 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 805 | # or if that brace on the next line is for something else |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 806 | if ($prevline=~/\b(if|while|for|switch)\s*\(/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 807 | my @opened = $prevline=~/\(/g; |
| 808 | my @closed = $prevline=~/\)/g; |
| 809 | my $nr_line = $linenr; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 810 | my $remaining = $realcnt - 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 811 | my $next_line = $line; |
| 812 | my $extra_lines = 0; |
| 813 | my $display_segment = $prevline; |
| 814 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 815 | while ($remaining > 0 && scalar @opened > scalar @closed) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 816 | $prevline .= $next_line; |
| 817 | $display_segment .= "\n" . $next_line; |
| 818 | $next_line = $lines[$nr_line]; |
| 819 | $nr_line++; |
| 820 | $remaining--; |
| 821 | |
| 822 | @opened = $prevline=~/\(/g; |
| 823 | @closed = $prevline=~/\)/g; |
| 824 | } |
| 825 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 826 | if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 827 | !($next_line=~/\b(if|while|for|switch)/) and !($next_line=~/\#define.*do.*while/)) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 828 | print "That { should be on the previous line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 829 | print "$here\n$display_segment\n$next_line\n\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 830 | $clean = 0; |
| 831 | } |
| 832 | } |
| 833 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 834 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 835 | # first statement and ensure its the whole macro if its not enclosed |
| 836 | # in a known goot container |
| 837 | if (($prevline=~/\#define.*\\/) and |
| 838 | !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and |
| 839 | !($line=~/do.*{/) and !($line=~/\(\{/) and |
| 840 | !($line=~/^.\s*$Declare\s/)) { |
| 841 | # Grab the first statement, if that is the entire macro |
| 842 | # its ok. This may start either on the #define line |
| 843 | # or the one below. |
| 844 | my $ctx1 = join('', ctx_statement($linenr - 1, $realcnt + 1)); |
| 845 | my $ctx2 = join('', ctx_statement($linenr, $realcnt)); |
| 846 | |
| 847 | if ($ctx1 =~ /\\$/ && $ctx2 =~ /\\$/) { |
| 848 | print "Macros with multiple statements should be enclosed in a do - while loop\n"; |
| 849 | print "$hereprev"; |
| 850 | $clean = 0; |
| 851 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 854 | # don't include deprecated include files (uses RAW line) |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 855 | for my $inc (@dep_includes) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 856 | if ($rawline =~ m@\#\s*include\s*\<$inc>@) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 857 | print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n"; |
| 858 | print "$herecurr"; |
| 859 | $clean = 0; |
| 860 | } |
| 861 | } |
| 862 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 863 | # don't use deprecated functions |
| 864 | for my $func (@dep_functions) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 865 | if ($line =~ /\b$func\b/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 866 | print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n"; |
| 867 | print "$herecurr"; |
| 868 | $clean = 0; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | # no volatiles please |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 873 | if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 874 | print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n"; |
| 875 | print "$herecurr"; |
| 876 | $clean = 0; |
| 877 | } |
| 878 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 879 | # warn about #if 0 |
| 880 | if ($line =~ /^.#\s*if\s+0\b/) { |
| 881 | print "#if 0 -- if this code redundant remove it\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 882 | print "$herecurr"; |
| 883 | $clean = 0; |
| 884 | } |
| 885 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 886 | # warn about #ifdefs in C files |
| 887 | # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
| 888 | # print "#ifdef in C files should be avoided\n"; |
| 889 | # print "$herecurr"; |
| 890 | # $clean = 0; |
| 891 | # } |
| 892 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 893 | # check for spinlock_t definitions without a comment. |
| 894 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { |
| 895 | my $which = $1; |
| 896 | if (!ctx_has_comment($first_line, $linenr)) { |
| 897 | print "$1 definition without comment\n"; |
| 898 | print "$herecurr"; |
| 899 | $clean = 0; |
| 900 | } |
| 901 | } |
| 902 | # check for memory barriers without a comment. |
| 903 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 904 | if (!ctx_has_comment($first_line, $linenr)) { |
| 905 | print "memory barrier without comment\n"; |
| 906 | print "$herecurr"; |
| 907 | $clean = 0; |
| 908 | } |
| 909 | } |
| 910 | # check of hardware specific defines |
| 911 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { |
| 912 | print "architecture specific defines should be avoided\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 913 | print "$herecurr"; |
| 914 | $clean = 0; |
| 915 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame^] | 916 | |
| 917 | if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ || |
| 918 | $line =~ /\b(?:inline|always_inline)\s+$Storage/) { |
| 919 | print "inline keyword should sit between storage class and type\n"; |
| 920 | print "$herecurr"; |
| 921 | $clean = 0; |
| 922 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | if ($chk_patch && !$is_patch) { |
| 926 | $clean = 0; |
| 927 | print "Does not appear to be a unified-diff format patch\n"; |
| 928 | } |
| 929 | if ($is_patch && $chk_signoff && $signoff == 0) { |
| 930 | $clean = 0; |
| 931 | print "Missing Signed-off-by: line(s)\n"; |
| 932 | } |
| 933 | |
| 934 | if ($clean == 1 && $quiet == 0) { |
| 935 | print "Your patch has no obvious style problems and is ready for submission.\n" |
| 936 | } |
| 937 | if ($clean == 0 && $quiet == 0) { |
| 938 | print "Your patch has style problems, please review. If any of these errors\n"; |
| 939 | print "are false positives report them to the maintainer, see\n"; |
| 940 | print "CHECKPATCH in MAINTAINERS.\n"; |
| 941 | } |
| 942 | return $clean; |
| 943 | } |