Commit 22f2a2ef9b468569cb34a7a056e54d56fdac0b9d

Authored by Andy Whitcroft
Committed by Linus Torvalds
1 parent a44648b057

update checkpatch.pl to version 0.09

This version brings a number of new checks, and a number of bug
fixes.  Of note:

  - checks for spacing on round and square bracket combinations
  - loosening of the single statement brace checks, to allow
    them when they contain comments or where other blocks in a
    compound statement have them.
  - parks the multple declaration support
  - allows architecture defines in architecture specific headers

Andy Whitcroft (21):
      Version: 0.09
      loosen single statement brace checks
      fix up multiple declaration to avoid function arguments
      add some function space parenthesis check exceptions
      handle EXPORT_'s with parentheses in their names
      clean up some warnings in multi-line macro bracketing support
      park the multiple declaration checks
      make block brace checks count comments as a statement
      __volatile__ and __extension__ are not functions
      allow architecture specific defined within architecture includes
      check spacing on square brackets
      check spacing on parentheses
      ensure we apply checks to the part before start comment
      check #ifdef conditional spacing
      handle __init_refok and __must_check
      add noinline to inline checks
      prevent email addresses from tripping spacing checks
      handle typed initialiser spacing
      handle line contination as end of line
      add bool to the type matcher
      refine EXPORT_SYMBOL checks to handle pointers

Signed-off-by: Andy Whitcroft <apw@shadowen.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 1 changed file with 71 additions and 31 deletions Inline Diff

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