blob: 25e20a27fc59b79b4c42d63b284564dd36c1becf [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004# (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
7use strict;
8
9my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070010$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070011
Andy Whitcroftde7d4f02007-07-15 23:37:22 -070012my $V = '0.07';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
14use Getopt::Long qw(:config no_auto_abbrev);
15
16my $quiet = 0;
17my $tree = 1;
18my $chk_signoff = 1;
19my $chk_patch = 1;
Andy Whitcroft653d4872007-06-23 17:16:34 -070020my $tst_type = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070021GetOptions(
22 'q|quiet' => \$quiet,
23 'tree!' => \$tree,
24 'signoff!' => \$chk_signoff,
25 'patch!' => \$chk_patch,
Andy Whitcroft653d4872007-06-23 17:16:34 -070026 'test-type!' => \$tst_type,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070027) or exit;
28
29my $exit = 0;
30
31if ($#ARGV < 0) {
Andy Whitcroft00df3442007-06-08 13:47:06 -070032 print "usage: $P [options] patchfile\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070033 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
39if ($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 Whitcroft4a0df2e2007-06-08 13:46:39 -070044my @dep_includes = ();
45my @dep_functions = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -070046my $removal = 'Documentation/feature-removal-schedule.txt';
47if ($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 Whitcroft4a0df2e2007-06-08 13:46:39 -070053 push(@dep_includes, $1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070054 }
55 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -070056
57 } elsif (/^Funcs:\s+(.*\S)/) {
58 for my $func (split(/[, ]+/, $1)) {
59 push(@dep_functions, $func);
60 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -070061 }
62 }
63}
64
Andy Whitcroft00df3442007-06-08 13:47:06 -070065my @rawlines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -070066while (<>) {
67 chomp;
Andy Whitcroft00df3442007-06-08 13:47:06 -070068 push(@rawlines, $_);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070069 if (eof(ARGV)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -070070 if (!process($ARGV, @rawlines)) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -070071 $exit = 1;
72 }
Andy Whitcroft00df3442007-06-08 13:47:06 -070073 @rawlines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -070074 }
75}
76
77exit($exit);
78
79sub 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
90sub 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 Whitcroft4a0df2e2007-06-08 13:46:39 -0700111sub 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 Whitcroft00df3442007-06-08 13:47:06 -0700124sub 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 Whitcroft4a0df2e2007-06-08 13:46:39 -0700155sub ctx_block_get {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700156 my ($linenr, $remain, $outer, $open, $close) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700157 my $line;
158 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700159 my $blk = '';
160 my @o;
161 my @c;
162 my @res = ();
163
Andy Whitcroft00df3442007-06-08 13:47:06 -0700164 for ($line = $start; $remain > 0; $line++) {
165 next if ($rawlines[$line] =~ /^-/);
166 $remain--;
167
168 $blk .= $rawlines[$line];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700169
Andy Whitcroft653d4872007-06-23 17:16:34 -0700170 @o = ($blk =~ /$open/g);
171 @c = ($blk =~ /$close/g);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700172
173 if (!$outer || (scalar(@o) - scalar(@c)) == 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700174 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700175 }
176
177 last if (scalar(@o) == scalar(@c));
178 }
179
180 return @res;
181}
182sub ctx_block_outer {
183 my ($linenr, $remain) = @_;
184
Andy Whitcroft653d4872007-06-23 17:16:34 -0700185 return ctx_block_get($linenr, $remain, 1, '\{', '\}');
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700186}
187sub ctx_block {
188 my ($linenr, $remain) = @_;
189
Andy Whitcroft653d4872007-06-23 17:16:34 -0700190 return ctx_block_get($linenr, $remain, 0, '\{', '\}');
191}
192sub ctx_statement {
193 my ($linenr, $remain) = @_;
194
195 return ctx_block_get($linenr, $remain, 0, '\(', '\)');
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700196}
197
198sub ctx_locate_comment {
199 my ($first_line, $end_line) = @_;
200
201 # Catch a comment on the end of the line itself.
Andy Whitcroft00df3442007-06-08 13:47:06 -0700202 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700203 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 Whitcroft00df3442007-06-08 13:47:06 -0700210 my $line = $rawlines[$linenr - 1];
211 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700212 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}
230sub ctx_has_comment {
231 my ($first_line, $end_line) = @_;
232 my $cmt = ctx_locate_comment($first_line, $end_line);
233
Andy Whitcroft00df3442007-06-08 13:47:06 -0700234 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700235 ##print "CMMT: $cmt\n";
236
237 return ($cmt ne '');
238}
239
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700240sub cat_vet {
241 my ($vet) = @_;
242
243 $vet =~ s/\t/^I/;
244 $vet =~ s/$/\$/;
245
246 return $vet;
247}
248
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700249sub ERROR {
250 print "ERROR: $_[0]\n";
251 our $clean = 0;
252}
253sub WARN {
254 print "WARNING: $_[0]\n";
255 our $clean = 0;
256}
257sub CHK {
258 print "CHECK: $_[0]\n";
259 our $clean = 0;
260}
261
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700262sub process {
263 my $filename = shift;
264 my @lines = @_;
265
266 my $linenr=0;
267 my $prevline="";
268 my $stashline="";
269
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700270 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700271 my $indent;
272 my $previndent=0;
273 my $stashindent=0;
274
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700275 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700276 my $signoff = 0;
277 my $is_patch = 0;
278
279 # Trace the real file/line as we go.
280 my $realfile = '';
281 my $realline = 0;
282 my $realcnt = 0;
283 my $here = '';
284 my $in_comment = 0;
285 my $first_line = 0;
286
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700287 my $Ident = qr{[A-Za-z\d_]+};
288 my $Storage = qr{extern|static};
289 my $Sparse = qr{__user|__kernel|__force|__iomem};
290 my $NonptrType = qr{
291 \b
292 (?:const\s+)?
293 (?:unsigned\s+)?
294 (?:
295 void|
296 char|
297 short|
298 int|
299 long|
300 unsigned|
301 float|
302 double|
303 long\s+int|
304 long\s+long|
305 long\s+long\s+int|
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700306 u8|u16|u32|u64|
307 s8|s16|s32|s64|
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700308 struct\s+$Ident|
309 union\s+$Ident|
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700310 enum\s+$Ident|
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700311 ${Ident}_t
312 )
313 (?:\s+$Sparse)*
314 \b
315 }x;
316 my $Type = qr{
317 \b$NonptrType\b
318 (?:\s*\*+\s*const|\s*\*+)?
319 }x;
320 my $Declare = qr{(?:$Storage\s+)?$Type};
321 my $Attribute = qr{__read_mostly|__init|__initdata};
Andy Whitcroft653d4872007-06-23 17:16:34 -0700322
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700323 # Pre-scan the patch looking for any __setup documentation.
324 my @setup_docs = ();
325 my $setup_docs = 0;
326 foreach my $line (@lines) {
327 if ($line=~/^\+\+\+\s+(\S+)/) {
328 $setup_docs = 0;
329 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
330 $setup_docs = 1;
331 }
332 next;
333 }
334
335 if ($setup_docs && $line =~ /^\+/) {
336 push(@setup_docs, $line);
337 }
338 }
339
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700340 foreach my $line (@lines) {
341 $linenr++;
342
Andy Whitcroft653d4872007-06-23 17:16:34 -0700343 my $rawline = $line;
344
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700345#extract the filename as it passes
346 if ($line=~/^\+\+\+\s+(\S+)/) {
347 $realfile=$1;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700348 $realfile =~ s@^[^/]*/@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700349 $in_comment = 0;
350 next;
351 }
352#extract the line range in the file after the patch is applied
353 if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
354 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700355 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700356 $in_comment = 0;
357 $realline=$1-1;
358 if (defined $2) {
359 $realcnt=$3+1;
360 } else {
361 $realcnt=1+1;
362 }
363 next;
364 }
365
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700366# track the line number as we move through the hunk, note that
367# new versions of GNU diff omit the leading space on completely
368# blank context lines so we need to count that too.
369 if ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700370 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700371 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700372
373 # track any sort of multi-line comment. Obviously if
374 # the added text or context do not include the whole
375 # comment we will not see it. Such is life.
376 #
377 # Guestimate if this is a continuing comment. If this
378 # is the start of a diff block and this line starts
379 # ' *' then it is very likely a comment.
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700380 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700381 $in_comment = 1;
382 }
383 if ($line =~ m@/\*@) {
384 $in_comment = 1;
385 }
386 if ($line =~ m@\*/@) {
387 $in_comment = 0;
388 }
389
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700390 # Measure the line length and indent.
391 ($length, $indent) = line_stats($line);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700392
393 # Track the previous line.
394 ($prevline, $stashline) = ($stashline, $line);
395 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700396 } elsif ($realcnt == 1) {
397 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700398 }
399
400#make up the handle for any error we report on this line
Randy Dunlap389834b2007-06-08 13:47:03 -0700401 $here = "#$linenr: ";
402 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700403
Andy Whitcroft00df3442007-06-08 13:47:06 -0700404 my $hereline = "$here\n$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700405 my $herecurr = "$here\n$line\n";
406 my $hereprev = "$here\n$prevline\n$line\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700407
408#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700409 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700410 # This is a signoff, if ugly, so do not double report.
411 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700412 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700413 WARN("Signed-off-by: is the preferred form\n" .
414 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700415 }
416 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700417 WARN("need space after Signed-off-by:\n" .
418 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700419 }
420 }
421
Andy Whitcroft00df3442007-06-08 13:47:06 -0700422# Check for wrappage within a valid hunk of the file
423 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700424 ERROR("patch seems to be corrupt (line wrapped?)\n" .
425 $herecurr);
426 }
427
428# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
429 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
430 !($line =~ m/^(
431 [\x09\x0A\x0D\x20-\x7E] # ASCII
432 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
433 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
434 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
435 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
436 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
437 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
438 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
439 )*$/x )) {
440 ERROR("Invalid UTF-8\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700441 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700442
Andy Whitcroft00df3442007-06-08 13:47:06 -0700443#ignore lines being removed
444 if ($line=~/^-/) {next;}
445
446# check we are in a valid source file if not then ignore this hunk
447 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700448
449#trailing whitespace
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700450 if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700451 my $herevet = "$here\n" . cat_vet($line) . "\n";
452 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700453 }
454#80 column limit
Andy Whitcroft00df3442007-06-08 13:47:06 -0700455 if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700456 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700457 }
458
459# check we are in a valid source file *.[hc] if not then ignore this hunk
460 next if ($realfile !~ /\.[hc]$/);
461
462# at the beginning of a line any tabs must come first and anything
463# more than 8 must use tabs.
464 if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700465 my $herevet = "$here\n" . cat_vet($line) . "\n";
466 ERROR("use tabs not spaces\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700467 }
468
469 #
470 # The rest of our checks refer specifically to C style
471 # only apply those _outside_ comments.
472 #
473 next if ($in_comment);
474
Andy Whitcroft653d4872007-06-23 17:16:34 -0700475# Remove comments from the line before processing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700476 $line =~ s@/\*.*\*/@@g;
477 $line =~ s@/\*.*@@;
478 $line =~ s@.*\*/@@;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700479
Andy Whitcroft653d4872007-06-23 17:16:34 -0700480# Standardise the strings and chars within the input to simplify matching.
481 $line = sanitise_line($line);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700482
Andy Whitcroft653d4872007-06-23 17:16:34 -0700483#
484# Checks which may be anchored in the context.
485#
486
487# Check for switch () and associated case and default
488# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -0700489 if ($line=~/\bswitch\s*\(.*\)/) {
490 my $err = '';
491 my $sep = '';
492 my @ctx = ctx_block_outer($linenr, $realcnt);
493 shift(@ctx);
494 for my $ctx (@ctx) {
495 my ($clen, $cindent) = line_stats($ctx);
496 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
497 $indent != $cindent) {
498 $err .= "$sep$ctx\n";
499 $sep = '';
500 } else {
501 $sep = "[...]\n";
502 }
503 }
504 if ($err ne '') {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700505 ERROR("switch and case should be at the same indent\n$hereline\n$err\n");
506 }
507 }
508
509# if/while/etc brace do not go on next line, unless defining a do while loop,
510# or if that brace on the next line is for something else
511 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
512 my @ctx = ctx_statement($linenr, $realcnt);
513 my $ctx_ln = $linenr + $#ctx + 1;
514 my $ctx_cnt = $realcnt - $#ctx - 1;
515 my $ctx = join("\n", @ctx);
516
517 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
518 $ctx_ln++;
519 $ctx_cnt--;
520 }
521 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
522
523 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
524 ERROR("That { should be on the previous line\n" .
525 "$here\n$ctx\n$lines[$ctx_ln - 1]");
Andy Whitcroft00df3442007-06-08 13:47:06 -0700526 }
527 }
528
529#ignore lines not being added
530 if ($line=~/^[^\+]/) {next;}
531
Andy Whitcroft653d4872007-06-23 17:16:34 -0700532# TEST: allow direct testing of the type matcher.
533 if ($tst_type && $line =~ /^.$Declare$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700534 ERROR("TEST: is type $Declare\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -0700535 next;
536 }
537
538#
539# Checks which are anchored on the added line.
540#
541
542# check for malformed paths in #include statements (uses RAW line)
543 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
544 my $path = $1;
545 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700546 ERROR("malformed #include filename\n" .
547 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -0700548 }
549 # Sanitise this special form of string.
550 $path = 'X' x length($path);
551 $line =~ s{\<.*\>}{<$path>};
552 }
Andy Whitcroft00df3442007-06-08 13:47:06 -0700553
554# no C99 // comments
555 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700556 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700557 }
558 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700559 $line =~ s@//.*@@;
560
561#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -0700562 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
563 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
564 my $name = $1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700565 if (($prevline !~ /^}/) &&
566 ($prevline !~ /^\+}/) &&
Andy Whitcroft653d4872007-06-23 17:16:34 -0700567 ($prevline !~ /^ }/) &&
568 ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700569 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700570 }
571 }
572
Andy Whitcroft653d4872007-06-23 17:16:34 -0700573# check for static initialisers.
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700574 if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700575 ERROR("do not initialise statics to 0 or NULL\n" .
576 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700577 }
578
Andy Whitcroft653d4872007-06-23 17:16:34 -0700579# check for new typedefs, only function parameters and sparse annotations
580# make sense.
581 if ($line =~ /\btypedef\s/ &&
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700582 $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -0700583 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700584 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700585 }
586
587# * goes on variable not on type
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700588 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700589 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
590 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700591
592 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700593 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
594 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700595
596 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700597 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
598 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700599
600 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700601 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
602 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700603 }
604
605# # no BUG() or BUG_ON()
606# if ($line =~ /\b(BUG|BUG_ON)\b/) {
607# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
608# print "$herecurr";
609# $clean = 0;
610# }
611
Andy Whitcroft00df3442007-06-08 13:47:06 -0700612# printk should use KERN_* levels. Note that follow on printk's on the
613# same line do not need a level, so we use the current block context
614# to try and find and validate the current printk. In summary the current
615# printk includes all preceeding printk's which have no newline on the end.
616# we assume the first bad printk is the one to report.
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700617 if ($line =~ /\bprintk\((?!KERN_)/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700618 my $ok = 0;
619 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
620 #print "CHECK<$lines[$ln - 1]\n";
621 # we have a preceeding printk if it ends
622 # with "\n" ignore it, else it is to blame
623 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
624 if ($rawlines[$ln - 1] !~ m{\\n"}) {
625 $ok = 1;
626 }
627 last;
628 }
629 }
630 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700631 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700632 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700633 }
634
Andy Whitcroft653d4872007-06-23 17:16:34 -0700635# function brace can't be on same line, except for #defines of do while,
636# or if closed on same line
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700637 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700638 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700639 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700640 }
Andy Whitcroft653d4872007-06-23 17:16:34 -0700641
642# Check operator spacing.
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700643 # Note we expand the line with the leading + as the real
644 # line will be displayed with the leading + and the tabs
645 # will therefore also expand that way.
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700646 my $opline = $line;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700647 $opline = expand_tabs($opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700648 $opline =~ s/^./ /;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700649 if (!($line=~/\#\s*include/)) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700650 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700651 my $off = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700652 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700653 $off += length($elements[$n]);
654
655 my $a = '';
656 $a = 'V' if ($elements[$n] ne '');
657 $a = 'W' if ($elements[$n] =~ /\s$/);
658 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
659 $a = 'O' if ($elements[$n] eq '');
660 $a = 'E' if ($elements[$n] eq '' && $n == 0);
661
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700662 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700663
664 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700665 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700666 $c = 'V' if ($elements[$n + 2] ne '');
667 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
668 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
669 $c = 'O' if ($elements[$n + 2] eq '');
670 } else {
671 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700672 }
673
Andy Whitcroft00df3442007-06-08 13:47:06 -0700674 # Pick up the preceeding and succeeding characters.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700675 my $ca = substr($opline, 0, $off);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700676 my $cc = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -0700677 if (length($opline) >= ($off + length($elements[$n + 1]))) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700678 $cc = substr($opline, $off + length($elements[$n + 1]));
Andy Whitcroft00df3442007-06-08 13:47:06 -0700679 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700680 my $cb = "$ca$;$cc";
Andy Whitcroft00df3442007-06-08 13:47:06 -0700681
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700682 my $ctx = "${a}x${c}";
683
684 my $at = "(ctx:$ctx)";
685
686 my $ptr = (" " x $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700687 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700688
689 ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700690
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700691 # ; should have either the end of line or a space or \ after it
692 if ($op eq ';') {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700693 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
694 $cc !~ /^;/) {
695 ERROR("need space after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700696 }
697
698 # // is a comment
699 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700700
701 # -> should have no spaces
702 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700703 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700704 ERROR("no spaces around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700705 }
706
707 # , must have a space on the right.
708 } elsif ($op eq ',') {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700709 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700710 ERROR("need space after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700711 }
712
713 # unary ! and unary ~ are allowed no space on the right
714 } elsif ($op eq '!' or $op eq '~') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700715 if ($ctx !~ /[WOEB]x./) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700716 ERROR("need space before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700717 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700718 if ($ctx =~ /.xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700719 ERROR("no space after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700720 }
721
722 # unary ++ and unary -- are allowed no space on one side.
723 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700724 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700725 ERROR("need space one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700726 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700727 if ($ctx =~ /Wx./ && $cc =~ /^;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700728 ERROR("no space before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -0700729 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700730
731 # & is both unary and binary
732 # unary:
733 # a &b
734 # binary (consistent spacing):
735 # a&b OK
736 # a & b OK
737 #
738 # boiling down to: if there is a space on the right then there
739 # should be one on the left.
740 #
741 # - is the same
742 #
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700743 } elsif ($op eq '&' or $op eq '-') {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700744 if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700745 ERROR("need space before that '$op' $at\n" . $hereptr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700746 }
747
Andy Whitcroft00df3442007-06-08 13:47:06 -0700748 # * is the same as & only adding:
749 # type:
750 # (foo *)
751 # (foo **)
752 #
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700753 } elsif ($op eq '*') {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700754 if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ &&
755 $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
756 ERROR("need space before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700757 }
758
759 # << and >> may either have or not have spaces both sides
760 } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
761 $op eq '^' or $op eq '|')
762 {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700763 if ($ctx !~ /VxV|WxW|VxE|WxE/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700764 ERROR("need consistent spacing around '$op' $at\n" .
765 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700766 }
767
768 # All the others need spaces both sides.
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700769 } elsif ($ctx !~ /[EW]x[WE]/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700770 ERROR("need spaces around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700771 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700772 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700773 }
774 }
775
776#need space before brace following if, while, etc
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700777 if ($line =~ /\(.*\){/ || $line =~ /do{/) {
778 ERROR("need a space before the open brace '{'\n" . $herecurr);
779 }
780
781# closing brace should have a space following it when it has anything
782# on the line
783 if ($line =~ /}(?!(?:,|;|\)))\S/) {
784 ERROR("need a space after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700785 }
786
787#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700788 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700789 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700790 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700791 }
792
793# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700794 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700795 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700796 }
797
798# Check for illegal assignment in if conditional.
Andy Whitcroft653d4872007-06-23 17:16:34 -0700799 if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700800 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700801 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700802 }
803
804 # Check for }<nl>else {, these must be at the same
805 # indent level to be relevant to each other.
806 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
807 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700808 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700809 }
810
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700811#studly caps, commented out until figure out how to distinguish between use of existing and adding new
812# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
813# print "No studly caps, use _\n";
814# print "$herecurr";
815# $clean = 0;
816# }
817
818#no spaces allowed after \ in define
819 if ($line=~/\#define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700820 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700821 }
822
Andy Whitcroft653d4872007-06-23 17:16:34 -0700823#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
824 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700825 my $checkfile = "include/linux/$1.h";
826 if (-f $checkfile) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700827 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
828 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700829 }
830 }
831
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700832# if and else should not have general statements after it
833 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700834 $1 !~ /^\s*(?:\sif|{|\\|$)/) {
835 ERROR("trailing statements should be on next line\n" . $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700836 }
837
Andy Whitcroft653d4872007-06-23 17:16:34 -0700838# multi-statement macros should be enclosed in a do while loop, grab the
839# first statement and ensure its the whole macro if its not enclosed
840# in a known goot container
841 if (($prevline=~/\#define.*\\/) and
842 !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
843 !($line=~/do.*{/) and !($line=~/\(\{/) and
844 !($line=~/^.\s*$Declare\s/)) {
845 # Grab the first statement, if that is the entire macro
846 # its ok. This may start either on the #define line
847 # or the one below.
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700848 my $ln = $linenr;
849 my $cnt = $realcnt;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700850
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700851 # If the macro starts on the define line start there.
852 if ($prevline !~ m{^.#\s*define\s*$Ident(?:\([^\)]*\))?\s*\\\s*$}) {
853 $ln--;
854 $cnt++;
855 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700856 my @ctx = ctx_statement($ln, $cnt);
857 my $ctx_ln = $ln + $#ctx + 1;
858 my $ctx = join("\n", @ctx);
859
860 # Pull in any empty extension lines.
861 while ($ctx =~ /\\$/ &&
862 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
863 $ctx .= $lines[$ctx_ln - 1];
864 $ctx_ln++;
865 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700866
867 if ($ctx =~ /\\$/) {
868 if ($ctx =~ /;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700869 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700870 } else {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700871 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700872 }
Andy Whitcroft653d4872007-06-23 17:16:34 -0700873 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700874 }
875
Andy Whitcroft653d4872007-06-23 17:16:34 -0700876# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700877 for my $inc (@dep_includes) {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700878 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700879 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700880 }
881 }
882
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700883# don't use deprecated functions
884 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700885 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700886 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700887 }
888 }
889
890# no volatiles please
Andy Whitcroft00df3442007-06-08 13:47:06 -0700891 if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700892 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700893 }
894
Andy Whitcroft00df3442007-06-08 13:47:06 -0700895# warn about #if 0
896 if ($line =~ /^.#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700897 CHK("if this code is redundant consider removing it\n" .
898 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700899 }
900
Andy Whitcroft00df3442007-06-08 13:47:06 -0700901# warn about #ifdefs in C files
902# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
903# print "#ifdef in C files should be avoided\n";
904# print "$herecurr";
905# $clean = 0;
906# }
907
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700908# check for spinlock_t definitions without a comment.
909 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
910 my $which = $1;
911 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700912 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700913 }
914 }
915# check for memory barriers without a comment.
916 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
917 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700918 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700919 }
920 }
921# check of hardware specific defines
922 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700923 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700924 }
Andy Whitcroft653d4872007-06-23 17:16:34 -0700925
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700926# check the location of the inline attribute, that it is between
927# storage class and type.
Andy Whitcroft653d4872007-06-23 17:16:34 -0700928 if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
929 $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700930 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
931 }
932
933# check for new externs in .c files.
934 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
935 WARN("externs should be avoided in .c files\n" . $herecurr);
936 }
937
938# checks for new __setup's
939 if ($rawline =~ /\b__setup\("([^"]*)"/) {
940 my $name = $1;
941
942 if (!grep(/$name/, @setup_docs)) {
943 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
944 }
Andy Whitcroft653d4872007-06-23 17:16:34 -0700945 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700946 }
947
948 if ($chk_patch && !$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700949 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700950 }
951 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700952 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700953 }
954
955 if ($clean == 1 && $quiet == 0) {
956 print "Your patch has no obvious style problems and is ready for submission.\n"
957 }
958 if ($clean == 0 && $quiet == 0) {
959 print "Your patch has style problems, please review. If any of these errors\n";
960 print "are false positives report them to the maintainer, see\n";
961 print "CHECKPATCH in MAINTAINERS.\n";
962 }
963 return $clean;
964}