blob: d80b55a6f89b08ca657984c69001baa12763a076 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesf4432c52008-10-20 13:31:45 -04002# (c) 2001, Dave Jones. <davej@redhat.com> (the file handling bit)
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5# (c) 2008, Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9
10my $P = $0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -070011$P =~ s@.*/@@g;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070012
Andy Whitcroft50a7dcf2009-01-06 14:41:21 -080013my $V = '0.25';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070014
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070021my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070022my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080023my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070024my $file = 0;
25my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $summary = 1;
27my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080028my $summary_file = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070029my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080030my %debug;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070031GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070033 'tree!' => \$tree,
34 'signoff!' => \$chk_signoff,
35 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080037 'terse!' => \$terse,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070038 'file!' => \$file,
39 'subjective!' => \$check,
40 'strict!' => \$check,
41 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080042 'summary!' => \$summary,
43 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080044 'summary-file!' => \$summary_file,
45
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -070047 'test-only=s' => \$tst_only,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070048) or exit;
49
50my $exit = 0;
51
52if ($#ARGV < 0) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -070053 print "usage: $P [options] patchfile\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070054 print "version: $V\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -080055 print "options: -q => quiet\n";
56 print " --no-tree => run without a kernel tree\n";
57 print " --terse => one line per report\n";
58 print " --emacs => emacs compile window format\n";
59 print " --file => check a source file\n";
60 print " --strict => enable more subjective tests\n";
61 print " --root => path to the kernel tree root\n";
62 print " --no-summary => suppress the per-file summary\n";
63 print " --summary-file => include the filename in summary\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070064 exit(1);
65}
66
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080067my $dbg_values = 0;
68my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -070069my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -070070my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080071for my $key (keys %debug) {
72 eval "\${dbg_$key} = '$debug{$key}';"
73}
74
Andy Whitcroft8905a672007-11-28 16:21:06 -080075if ($terse) {
76 $emacs = 1;
77 $quiet++;
78}
79
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070080if ($tree) {
81 if (defined $root) {
82 if (!top_of_kernel_tree($root)) {
83 die "$P: $root: --root does not point at a valid tree\n";
84 }
85 } else {
86 if (top_of_kernel_tree('.')) {
87 $root = '.';
88 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
89 top_of_kernel_tree($1)) {
90 $root = $1;
91 }
92 }
93
94 if (!defined $root) {
95 print "Must be run from the top-level dir. of a kernel tree\n";
96 exit(2);
97 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070098}
99
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700100my $emitted_corrupt = 0;
101
102our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
103our $Storage = qr{extern|static|asmlinkage};
104our $Sparse = qr{
105 __user|
106 __kernel|
107 __force|
108 __iomem|
109 __must_check|
110 __init_refok|
Andy Whitcroftcf655042008-03-04 14:28:20 -0800111 __kprobes
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700112 }x;
113our $Attribute = qr{
114 const|
115 __read_mostly|
116 __kprobes|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700117 __(?:mem|cpu|dev|)(?:initdata|init)|
118 ____cacheline_aligned|
119 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800120 ____cacheline_internodealigned_in_smp|
121 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700122 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700123our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700124our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700125our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
126our $Lval = qr{$Ident(?:$Member)*};
127
128our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
129our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800130our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700131our $Operators = qr{
132 <=|>=|==|!=|
133 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800134 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700135 }x;
136
Andy Whitcroft8905a672007-11-28 16:21:06 -0800137our $NonptrType;
138our $Type;
139our $Declare;
140
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700141our $UTF8 = qr {
142 [\x09\x0A\x0D\x20-\x7E] # ASCII
143 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
144 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
145 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
146 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
147 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
148 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
149 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
150}x;
151
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700152our $typeTypedefs = qr{(?x:
153 (?:__)?(?:u|s|be|le)(?:\d|\d\d)|
154 atomic_t
155)};
156
Andy Whitcroft8905a672007-11-28 16:21:06 -0800157our @typeList = (
158 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700159 qr{(?:unsigned\s+)?char},
160 qr{(?:unsigned\s+)?short},
161 qr{(?:unsigned\s+)?int},
162 qr{(?:unsigned\s+)?long},
163 qr{(?:unsigned\s+)?long\s+int},
164 qr{(?:unsigned\s+)?long\s+long},
165 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800166 qr{unsigned},
167 qr{float},
168 qr{double},
169 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800170 qr{struct\s+$Ident},
171 qr{union\s+$Ident},
172 qr{enum\s+$Ident},
173 qr{${Ident}_t},
174 qr{${Ident}_handler},
175 qr{${Ident}_handler_fn},
176);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700177our @modifierList = (
178 qr{fastcall},
179);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800180
181sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700182 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
183 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700184 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800185 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700186 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800187 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700188 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700189 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700190 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800191 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700192 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800193 }x;
194 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700195 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800196 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700197 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800198 }x;
199 $Declare = qr{(?:$Storage\s+)?$Type};
200}
201build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700202
203$chk_signoff = 0 if ($file);
204
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700205my @dep_includes = ();
206my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700207my $removal = "Documentation/feature-removal-schedule.txt";
208if ($tree && -f "$root/$removal") {
209 open(REMOVE, "<$root/$removal") ||
210 die "$P: $removal: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700211 while (<REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700212 if (/^Check:\s+(.*\S)/) {
213 for my $entry (split(/[, ]+/, $1)) {
214 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700215 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700216
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700217 } elsif ($entry !~ m@/@) {
218 push(@dep_functions, $entry);
219 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700220 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700221 }
222 }
223}
224
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700225my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800226my @lines = ();
227my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228for my $filename (@ARGV) {
229 if ($file) {
230 open(FILE, "diff -u /dev/null $filename|") ||
231 die "$P: $filename: diff failed - $!\n";
232 } else {
233 open(FILE, "<$filename") ||
234 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700235 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800236 if ($filename eq '-') {
237 $vname = 'Your patch';
238 } else {
239 $vname = $filename;
240 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700241 while (<FILE>) {
242 chomp;
243 push(@rawlines, $_);
244 }
245 close(FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800246 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700247 $exit = 1;
248 }
249 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800250 @lines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700251}
252
253exit($exit);
254
255sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700256 my ($root) = @_;
257
258 my @tree_check = (
259 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
260 "README", "Documentation", "arch", "include", "drivers",
261 "fs", "init", "ipc", "kernel", "lib", "scripts",
262 );
263
264 foreach my $check (@tree_check) {
265 if (! -e $root . '/' . $check) {
266 return 0;
267 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700268 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700269 return 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700270}
271
272sub expand_tabs {
273 my ($str) = @_;
274
275 my $res = '';
276 my $n = 0;
277 for my $c (split(//, $str)) {
278 if ($c eq "\t") {
279 $res .= ' ';
280 $n++;
281 for (; ($n % 8) != 0; $n++) {
282 $res .= ' ';
283 }
284 next;
285 }
286 $res .= $c;
287 $n++;
288 }
289
290 return $res;
291}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700292sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700293 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700294 return $res;
295}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700296
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700297sub line_stats {
298 my ($line) = @_;
299
300 # Drop the diff line leader and expand tabs
301 $line =~ s/^.//;
302 $line = expand_tabs($line);
303
304 # Pick the indent from the front of the line.
305 my ($white) = ($line =~ /^(\s*)/);
306
307 return (length($line), length($white));
308}
309
Andy Whitcroft773647a2008-03-28 14:15:58 -0700310my $sanitise_quote = '';
311
312sub sanitise_line_reset {
313 my ($in_comment) = @_;
314
315 if ($in_comment) {
316 $sanitise_quote = '*/';
317 } else {
318 $sanitise_quote = '';
319 }
320}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700321sub sanitise_line {
322 my ($line) = @_;
323
324 my $res = '';
325 my $l = '';
326
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800327 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700328 my $off = 0;
329 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700330
Andy Whitcroft773647a2008-03-28 14:15:58 -0700331 # Always copy over the diff marker.
332 $res = substr($line, 0, 1);
333
334 for ($off = 1; $off < length($line); $off++) {
335 $c = substr($line, $off, 1);
336
337 # Comments we are wacking completly including the begin
338 # and end, all to $;.
339 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
340 $sanitise_quote = '*/';
341
342 substr($res, $off, 2, "$;$;");
343 $off++;
344 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800345 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700346 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700347 $sanitise_quote = '';
348 substr($res, $off, 2, "$;$;");
349 $off++;
350 next;
351 }
352
353 # A \ in a string means ignore the next character.
354 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
355 $c eq "\\") {
356 substr($res, $off, 2, 'XX');
357 $off++;
358 next;
359 }
360 # Regular quotes.
361 if ($c eq "'" || $c eq '"') {
362 if ($sanitise_quote eq '') {
363 $sanitise_quote = $c;
364
365 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700366 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700367 } elsif ($sanitise_quote eq $c) {
368 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700369 }
370 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700371
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800372 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700373 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
374 substr($res, $off, 1, $;);
375 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
376 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700377 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700378 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700379 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800380 }
381
382 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700383 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800384 my $clean = 'X' x length($1);
385 $res =~ s@\<.*\>@<$clean>@;
386
387 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700388 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800389 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700390 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800391 }
392
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700393 return $res;
394}
395
Andy Whitcroft8905a672007-11-28 16:21:06 -0800396sub ctx_statement_block {
397 my ($linenr, $remain, $off) = @_;
398 my $line = $linenr - 1;
399 my $blk = '';
400 my $soff = $off;
401 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700402 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800403
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800404 my $loff = 0;
405
Andy Whitcroft8905a672007-11-28 16:21:06 -0800406 my $type = '';
407 my $level = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800408 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800409 my $c;
410 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800411
412 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800413 while (1) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700414 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800415 # If we are about to drop off the end, pull in more
416 # context.
417 if ($off >= $len) {
418 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700419 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800420 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800421 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800422 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800423 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800424 $len = length($blk);
425 $line++;
426 last;
427 }
428 # Bail if there is no further context.
429 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800430 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800431 last;
432 }
433 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800434 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800435 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800436 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800437
Andy Whitcroft773647a2008-03-28 14:15:58 -0700438 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800439 # Statement ends at the ';' or a close '}' at the
440 # outermost level.
441 if ($level == 0 && $c eq ';') {
442 last;
443 }
444
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800445 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700446 if ($level == 0 && $coff_set == 0 &&
447 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
448 $remainder =~ /^(else)(?:\s|{)/ &&
449 $remainder !~ /^else\s+if\b/) {
450 $coff = $off + length($1) - 1;
451 $coff_set = 1;
452 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
453 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800454 }
455
Andy Whitcroft8905a672007-11-28 16:21:06 -0800456 if (($type eq '' || $type eq '(') && $c eq '(') {
457 $level++;
458 $type = '(';
459 }
460 if ($type eq '(' && $c eq ')') {
461 $level--;
462 $type = ($level != 0)? '(' : '';
463
464 if ($level == 0 && $coff < $soff) {
465 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700466 $coff_set = 1;
467 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800468 }
469 }
470 if (($type eq '' || $type eq '{') && $c eq '{') {
471 $level++;
472 $type = '{';
473 }
474 if ($type eq '{' && $c eq '}') {
475 $level--;
476 $type = ($level != 0)? '{' : '';
477
478 if ($level == 0) {
479 last;
480 }
481 }
482 $off++;
483 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700484 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800485 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700486 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800487 $line++;
488 $remain--;
489 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800490
491 my $statement = substr($blk, $soff, $off - $soff + 1);
492 my $condition = substr($blk, $soff, $coff - $soff + 1);
493
494 #warn "STATEMENT<$statement>\n";
495 #warn "CONDITION<$condition>\n";
496
Andy Whitcroft773647a2008-03-28 14:15:58 -0700497 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800498
499 return ($statement, $condition,
500 $line, $remain + 1, $off - $loff + 1, $level);
501}
502
Andy Whitcroftcf655042008-03-04 14:28:20 -0800503sub statement_lines {
504 my ($stmt) = @_;
505
506 # Strip the diff line prefixes and rip blank lines at start and end.
507 $stmt =~ s/(^|\n)./$1/g;
508 $stmt =~ s/^\s*//;
509 $stmt =~ s/\s*$//;
510
511 my @stmt_lines = ($stmt =~ /\n/g);
512
513 return $#stmt_lines + 2;
514}
515
516sub statement_rawlines {
517 my ($stmt) = @_;
518
519 my @stmt_lines = ($stmt =~ /\n/g);
520
521 return $#stmt_lines + 2;
522}
523
524sub statement_block_size {
525 my ($stmt) = @_;
526
527 $stmt =~ s/(^|\n)./$1/g;
528 $stmt =~ s/^\s*{//;
529 $stmt =~ s/}\s*$//;
530 $stmt =~ s/^\s*//;
531 $stmt =~ s/\s*$//;
532
533 my @stmt_lines = ($stmt =~ /\n/g);
534 my @stmt_statements = ($stmt =~ /;/g);
535
536 my $stmt_lines = $#stmt_lines + 2;
537 my $stmt_statements = $#stmt_statements + 1;
538
539 if ($stmt_lines > $stmt_statements) {
540 return $stmt_lines;
541 } else {
542 return $stmt_statements;
543 }
544}
545
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800546sub ctx_statement_full {
547 my ($linenr, $remain, $off) = @_;
548 my ($statement, $condition, $level);
549
550 my (@chunks);
551
Andy Whitcroftcf655042008-03-04 14:28:20 -0800552 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800553 ($statement, $condition, $linenr, $remain, $off, $level) =
554 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700555 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800556 push(@chunks, [ $condition, $statement ]);
557 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
558 return ($level, $linenr, @chunks);
559 }
560
561 # Pull in the following conditional/block pairs and see if they
562 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800563 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800564 ($statement, $condition, $linenr, $remain, $off, $level) =
565 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800566 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700567 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800568 #print "C: push\n";
569 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800570 }
571
572 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800573}
574
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700575sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700576 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700577 my $line;
578 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700579 my $blk = '';
580 my @o;
581 my @c;
582 my @res = ();
583
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700584 my $level = 0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700585 for ($line = $start; $remain > 0; $line++) {
586 next if ($rawlines[$line] =~ /^-/);
587 $remain--;
588
589 $blk .= $rawlines[$line];
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700590 foreach my $c (split(//, $rawlines[$line])) {
591 ##print "C<$c>L<$level><$open$close>O<$off>\n";
592 if ($off > 0) {
593 $off--;
594 next;
595 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700596
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700597 if ($c eq $close && $level > 0) {
598 $level--;
599 last if ($level == 0);
600 } elsif ($c eq $open) {
601 $level++;
602 }
603 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700604
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700605 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700606 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700607 }
608
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700609 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700610 }
611
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700612 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700613}
614sub ctx_block_outer {
615 my ($linenr, $remain) = @_;
616
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700617 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
618 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700619}
620sub ctx_block {
621 my ($linenr, $remain) = @_;
622
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700623 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
624 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700625}
626sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700627 my ($linenr, $remain, $off) = @_;
628
629 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
630 return @r;
631}
632sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700633 my ($linenr, $remain) = @_;
634
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700635 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700636}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700637sub ctx_statement_level {
638 my ($linenr, $remain, $off) = @_;
639
640 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
641}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700642
643sub ctx_locate_comment {
644 my ($first_line, $end_line) = @_;
645
646 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700647 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700648 return $current_comment if (defined $current_comment);
649
650 # Look through the context and try and figure out if there is a
651 # comment.
652 my $in_comment = 0;
653 $current_comment = '';
654 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700655 my $line = $rawlines[$linenr - 1];
656 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700657 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
658 $in_comment = 1;
659 }
660 if ($line =~ m@/\*@) {
661 $in_comment = 1;
662 }
663 if (!$in_comment && $current_comment ne '') {
664 $current_comment = '';
665 }
666 $current_comment .= $line . "\n" if ($in_comment);
667 if ($line =~ m@\*/@) {
668 $in_comment = 0;
669 }
670 }
671
672 chomp($current_comment);
673 return($current_comment);
674}
675sub ctx_has_comment {
676 my ($first_line, $end_line) = @_;
677 my $cmt = ctx_locate_comment($first_line, $end_line);
678
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700679 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700680 ##print "CMMT: $cmt\n";
681
682 return ($cmt ne '');
683}
684
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700685sub raw_line {
686 my ($linenr, $cnt) = @_;
687
688 my $offset = $linenr - 1;
689 $cnt++;
690
691 my $line;
692 while ($cnt) {
693 $line = $rawlines[$offset++];
694 next if (defined($line) && $line =~ /^-/);
695 $cnt--;
696 }
697
698 return $line;
699}
700
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700701sub cat_vet {
702 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700703 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700704
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700705 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700706 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
707 $res .= $1;
708 if ($2 ne '') {
709 $coded = sprintf("^%c", unpack('C', $2) + 64);
710 $res .= $coded;
711 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700712 }
713 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700714
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700715 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700716}
717
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800718my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800719my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800720my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700721my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800722
723sub annotate_reset {
724 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800725 $av_pending = '_';
726 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700727 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800728}
729
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700730sub annotate_values {
731 my ($stream, $type) = @_;
732
733 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700734 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700735 my $cur = $stream;
736
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800737 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700738
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700739 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700740 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800741 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700742 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700743 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800744 print "WS($1)\n" if ($dbg_values > 1);
745 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800746 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800747 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700748 }
749
Andy Whitcroft8ea3eb92008-07-23 21:29:08 -0700750 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800751 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700752 $type = 'T';
753
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700754 } elsif ($cur =~ /^($Modifier)\s*/) {
755 print "MODIFIER($1)\n" if ($dbg_values > 1);
756 $type = 'T';
757
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700758 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700759 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800760 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700761 push(@av_paren_type, $type);
762 if ($2 ne '') {
763 $av_pending = 'N';
764 }
765 $type = 'E';
766
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700767 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700768 print "UNDEF($1)\n" if ($dbg_values > 1);
769 $av_preprocessor = 1;
770 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700771
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700772 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800773 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800774 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800775
776 push(@av_paren_type, $type);
777 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700778 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800779
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700780 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800781 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
782 $av_preprocessor = 1;
783
784 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
785
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700786 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800787
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700788 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800789 print "PRE_END($1)\n" if ($dbg_values > 1);
790
791 $av_preprocessor = 1;
792
793 # Assume all arms of the conditional end as this
794 # one does, and continue as if the #endif was not here.
795 pop(@av_paren_type);
796 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700797 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700798
799 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800800 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700801
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700802 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
803 print "ATTR($1)\n" if ($dbg_values > 1);
804 $av_pending = $type;
805 $type = 'N';
806
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700807 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800808 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700809 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800810 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700811 }
812 $type = 'N';
813
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700814 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800815 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700816 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700817 $type = 'N';
818
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700819 } elsif ($cur =~/^(case)/o) {
820 print "CASE($1)\n" if ($dbg_values > 1);
821 $av_pend_colon = 'C';
822 $type = 'N';
823
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700824 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800825 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700826 $type = 'N';
827
828 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800829 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800830 push(@av_paren_type, $av_pending);
831 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700832 $type = 'N';
833
834 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800835 my $new_type = pop(@av_paren_type);
836 if ($new_type ne '_') {
837 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800838 print "PAREN('$1') -> $type\n"
839 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700840 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800841 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700842 }
843
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700844 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800845 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700846 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800847 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700848
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800849 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
850 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700851 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800852 } elsif ($type eq 'E') {
853 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700854 }
855 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
856 $type = 'V';
857
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700858 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800859 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700860 $type = 'V';
861
862 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800863 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700864 $type = 'N';
865
Andy Whitcroftcf655042008-03-04 14:28:20 -0800866 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800867 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800868 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700869 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800870
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800871 } elsif ($cur =~/^(,)/) {
872 print "COMMA($1)\n" if ($dbg_values > 1);
873 $type = 'C';
874
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700875 } elsif ($cur =~ /^(\?)/o) {
876 print "QUESTION($1)\n" if ($dbg_values > 1);
877 $type = 'N';
878
879 } elsif ($cur =~ /^(:)/o) {
880 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
881
882 substr($var, length($res), 1, $av_pend_colon);
883 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
884 $type = 'E';
885 } else {
886 $type = 'N';
887 }
888 $av_pend_colon = 'O';
889
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800890 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800891 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700892 $type = 'N';
893
Andy Whitcroft0d413862008-10-15 22:02:16 -0700894 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -0700895 my $variant;
896
897 print "OPV($1)\n" if ($dbg_values > 1);
898 if ($type eq 'V') {
899 $variant = 'B';
900 } else {
901 $variant = 'U';
902 }
903
904 substr($var, length($res), 1, $variant);
905 $type = 'N';
906
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700907 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800908 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700909 if ($1 ne '++' && $1 ne '--') {
910 $type = 'N';
911 }
912
913 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800914 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700915 }
916 if (defined $1) {
917 $cur = substr($cur, length($1));
918 $res .= $type x length($1);
919 }
920 }
921
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700922 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700923}
924
Andy Whitcroft8905a672007-11-28 16:21:06 -0800925sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800926 my ($possible, $line) = @_;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800927
Andy Whitcroft0776e592008-10-15 22:02:29 -0700928 print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
929 if ($possible !~ /(?:
930 ^(?:
931 $Modifier|
932 $Storage|
933 $Type|
934 DEFINE_\S+|
935 goto|
936 return|
937 case|
938 else|
939 asm|__asm__|
940 do
941 )$|
942 ^(?:typedef|struct|enum)\b
943 )/x) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700944 # Check for modifiers.
945 $possible =~ s/\s*$Storage\s*//g;
946 $possible =~ s/\s*$Sparse\s*//g;
947 if ($possible =~ /^\s*$/) {
948
949 } elsif ($possible =~ /\s/) {
950 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -0700951 for my $modifier (split(' ', $possible)) {
952 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
953 push(@modifierList, $modifier);
954 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700955
956 } else {
957 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
958 push(@typeList, $possible);
959 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800960 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -0700961 } else {
962 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800963 }
964}
965
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700966my $prefix = '';
967
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700968sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700969 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
970 return 0;
971 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800972 my $line = $prefix . $_[0];
973
974 $line = (split('\n', $line))[0] . "\n" if ($terse);
975
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800976 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700977
978 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700979}
980sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800981 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700982}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700983sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700984 if (report("ERROR: $_[0]\n")) {
985 our $clean = 0;
986 our $cnt_error++;
987 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700988}
989sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700990 if (report("WARNING: $_[0]\n")) {
991 our $clean = 0;
992 our $cnt_warn++;
993 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700994}
995sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700996 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700997 our $clean = 0;
998 our $cnt_chk++;
999 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001000}
1001
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001002sub check_absolute_file {
1003 my ($absolute, $herecurr) = @_;
1004 my $file = $absolute;
1005
1006 ##print "absolute<$absolute>\n";
1007
1008 # See if any suffix of this path is a path within the tree.
1009 while ($file =~ s@^[^/]*/@@) {
1010 if (-f "$root/$file") {
1011 ##print "file<$file>\n";
1012 last;
1013 }
1014 }
1015 if (! -f _) {
1016 return 0;
1017 }
1018
1019 # It is, so see if the prefix is acceptable.
1020 my $prefix = $absolute;
1021 substr($prefix, -length($file)) = '';
1022
1023 ##print "prefix<$prefix>\n";
1024 if ($prefix ne ".../") {
1025 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1026 }
1027}
1028
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001029sub process {
1030 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001031
1032 my $linenr=0;
1033 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001034 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001035 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001036 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001037
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001038 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001039 my $indent;
1040 my $previndent=0;
1041 my $stashindent=0;
1042
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001043 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001044 my $signoff = 0;
1045 my $is_patch = 0;
1046
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001047 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001048 our $cnt_lines = 0;
1049 our $cnt_error = 0;
1050 our $cnt_warn = 0;
1051 our $cnt_chk = 0;
1052
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001053 # Trace the real file/line as we go.
1054 my $realfile = '';
1055 my $realline = 0;
1056 my $realcnt = 0;
1057 my $here = '';
1058 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001059 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001060 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001061 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001062
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001063 my $prev_values = 'E';
1064
1065 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001066 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001067 my %suppress_whiletrailers;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001068
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001069 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001070 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001071 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001072 my @setup_docs = ();
1073 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001074
1075 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001076 my $line;
1077 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001078 $linenr++;
1079 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001080
Andy Whitcroft773647a2008-03-28 14:15:58 -07001081 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001082 $setup_docs = 0;
1083 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1084 $setup_docs = 1;
1085 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001086 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001087 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001088 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1089 $realline=$1-1;
1090 if (defined $2) {
1091 $realcnt=$3+1;
1092 } else {
1093 $realcnt=1+1;
1094 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001095 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001096
1097 # Guestimate if this is a continuing comment. Run
1098 # the context looking for a comment "edge". If this
1099 # edge is a close comment then we must be in a comment
1100 # at context start.
1101 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001102 my $cnt = $realcnt;
1103 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1104 next if (defined $rawlines[$ln - 1] &&
1105 $rawlines[$ln - 1] =~ /^-/);
1106 $cnt--;
1107 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001108 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001109 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1110 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1111 ($edge) = $1;
1112 last;
1113 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001114 }
1115 if (defined $edge && $edge eq '*/') {
1116 $in_comment = 1;
1117 }
1118
1119 # Guestimate if this is a continuing comment. If this
1120 # is the start of a diff block and this line starts
1121 # ' *' then it is very likely a comment.
1122 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001123 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001124 {
1125 $in_comment = 1;
1126 }
1127
1128 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1129 sanitise_line_reset($in_comment);
1130
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001131 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001132 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001133 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001134 $line = sanitise_line($rawline);
1135 }
1136 push(@lines, $line);
1137
1138 if ($realcnt > 1) {
1139 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1140 } else {
1141 $realcnt = 0;
1142 }
1143
1144 #print "==>$rawline\n";
1145 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001146
1147 if ($setup_docs && $line =~ /^\+/) {
1148 push(@setup_docs, $line);
1149 }
1150 }
1151
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001152 $prefix = '';
1153
Andy Whitcroft773647a2008-03-28 14:15:58 -07001154 $realcnt = 0;
1155 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001156 foreach my $line (@lines) {
1157 $linenr++;
1158
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001159 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft306708542008-10-15 22:02:28 -07001160 my $hunk_line = ($realcnt != 0);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001161
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001162#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001163 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001164 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001165 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001166 $realline=$1-1;
1167 if (defined $2) {
1168 $realcnt=$3+1;
1169 } else {
1170 $realcnt=1+1;
1171 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001172 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001173 $prev_values = 'E';
1174
Andy Whitcroft773647a2008-03-28 14:15:58 -07001175 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001176 %suppress_whiletrailers = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001177 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001178
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001179# track the line number as we move through the hunk, note that
1180# new versions of GNU diff omit the leading space on completely
1181# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001182 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001183 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001184 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001185
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001186 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001187 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001188
1189 # Track the previous line.
1190 ($prevline, $stashline) = ($stashline, $line);
1191 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001192 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1193
Andy Whitcroft773647a2008-03-28 14:15:58 -07001194 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001195
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001196 } elsif ($realcnt == 1) {
1197 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001198 }
1199
1200#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001201 $prefix = "$filename:$realline: " if ($emacs && $file);
1202 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1203
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001204 $here = "#$linenr: " if (!$file);
1205 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001206
1207 # extract the filename as it passes
1208 if ($line=~/^\+\+\+\s+(\S+)/) {
1209 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001210 $realfile =~ s@^([^/]*)/@@;
1211
1212 $p1_prefix = $1;
1213 if ($tree && $p1_prefix ne '' && -e "$root/$p1_prefix") {
1214 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1215 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001216
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001217 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001218 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1219 }
1220 next;
1221 }
1222
Randy Dunlap389834b2007-06-08 13:47:03 -07001223 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001224
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001225 my $hereline = "$here\n$rawline\n";
1226 my $herecurr = "$here\n$rawline\n";
1227 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001228
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001229 $cnt_lines++ if ($realcnt != 0);
1230
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001231#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001232 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001233 # This is a signoff, if ugly, so do not double report.
1234 $signoff++;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001235 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001236 WARN("Signed-off-by: is the preferred form\n" .
1237 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001238 }
1239 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001240 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001241 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001242 }
1243 }
1244
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001245# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001246 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001247 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001248 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001249 }
1250
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001251# Check for absolute kernel paths.
1252 if ($tree) {
1253 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1254 my $file = $1;
1255
1256 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1257 check_absolute_file($1, $herecurr)) {
1258 #
1259 } else {
1260 check_absolute_file($file, $herecurr);
1261 }
1262 }
1263 }
1264
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001265# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1266 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001267 $rawline !~ m/^$UTF8*$/) {
1268 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1269
1270 my $blank = copy_spacing($rawline);
1271 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1272 my $hereptr = "$hereline$ptr\n";
1273
1274 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001275 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001276
Andy Whitcroft306708542008-10-15 22:02:28 -07001277# ignore non-hunk lines and lines being removed
1278 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001279
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001280#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001281 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001282 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001283 ERROR("DOS line endings\n" . $herevet);
1284
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001285 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1286 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001287 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001288 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001289
1290# check we are in a valid source file if not then ignore this hunk
1291 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1292
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001293#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001294 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001295 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1296 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1297 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001298 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001299 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001300 }
1301
Andy Whitcroft8905a672007-11-28 16:21:06 -08001302# check for adding lines without a newline.
1303 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1304 WARN("adding a line without newline at end of file\n" . $herecurr);
1305 }
1306
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001307# check we are in a valid source file C or perl if not then ignore this hunk
1308 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001309
1310# at the beginning of a line any tabs must come first and anything
1311# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001312 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1313 $rawline =~ /^\+\s* \s*/) {
1314 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001315 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001316 }
1317
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001318# check we are in a valid C source file if not then ignore this hunk
1319 next if ($realfile !~ /\.(h|c)$/);
1320
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001321# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001322 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001323 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1324 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001325
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001326# Check for potential 'bare' types
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001327 my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001328 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001329 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001330 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001331 $stat =~ s/\n./\n /g;
1332 $cond =~ s/\n./\n /g;
1333
1334 my $s = $stat;
1335 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001336
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001337 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001338 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001339
1340 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001341 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001342
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001343 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001344 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001345 my $type = $1;
1346 $type =~ s/\s+/ /g;
1347 possible($type, "A:" . $s);
1348
Andy Whitcroft8905a672007-11-28 16:21:06 -08001349 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07001350 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001351 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001352 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001353
1354 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001355 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001356 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001357 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001358
1359 # Check for any sort of function declaration.
1360 # int foo(something bar, other baz);
1361 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001362 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001363 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001364
Andy Whitcroftcf655042008-03-04 14:28:20 -08001365 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001366 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001367 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001368
Andy Whitcroft8905a672007-11-28 16:21:06 -08001369 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001370 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001371
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001372 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001373 }
1374 }
1375 }
1376
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001377 }
1378
Andy Whitcroft653d4872007-06-23 17:16:34 -07001379#
1380# Checks which may be anchored in the context.
1381#
1382
1383# Check for switch () and associated case and default
1384# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001385 if ($line=~/\bswitch\s*\(.*\)/) {
1386 my $err = '';
1387 my $sep = '';
1388 my @ctx = ctx_block_outer($linenr, $realcnt);
1389 shift(@ctx);
1390 for my $ctx (@ctx) {
1391 my ($clen, $cindent) = line_stats($ctx);
1392 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1393 $indent != $cindent) {
1394 $err .= "$sep$ctx\n";
1395 $sep = '';
1396 } else {
1397 $sep = "[...]\n";
1398 }
1399 }
1400 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001401 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001402 }
1403 }
1404
1405# if/while/etc brace do not go on next line, unless defining a do while loop,
1406# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001407 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001408 my $pre_ctx = "$1$2";
1409
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001410 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001411 my $ctx_cnt = $realcnt - $#ctx - 1;
1412 my $ctx = join("\n", @ctx);
1413
Andy Whitcroft548596d2008-07-23 21:29:01 -07001414 my $ctx_ln = $linenr;
1415 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001416
Andy Whitcroft548596d2008-07-23 21:29:01 -07001417 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1418 defined $lines[$ctx_ln - 1] &&
1419 $lines[$ctx_ln - 1] =~ /^-/)) {
1420 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1421 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001422 $ctx_ln++;
1423 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001424
Andy Whitcroft53210162008-07-23 21:29:03 -07001425 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1426 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001427
1428 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1429 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001430 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001431 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001432 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1433 $ctx =~ /\)\s*\;\s*$/ &&
1434 defined $lines[$ctx_ln - 1])
1435 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001436 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1437 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001438 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001439 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001440 }
1441 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001442 }
1443
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001444# Check relative indent for conditionals and blocks.
1445 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1446 my ($s, $c) = ($stat, $cond);
1447
1448 substr($s, 0, length($c), '');
1449
1450 # Make sure we remove the line prefixes as we have
1451 # none on the first line, and are going to readd them
1452 # where necessary.
1453 $s =~ s/\n./\n/gs;
1454
1455 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001456 my @newlines = ($c =~ /\n/gs);
1457 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001458
1459 # We want to check the first line inside the block
1460 # starting at the end of the conditional, so remove:
1461 # 1) any blank line termination
1462 # 2) any opening brace { on end of the line
1463 # 3) any do (...) {
1464 my $continuation = 0;
1465 my $check = 0;
1466 $s =~ s/^.*\bdo\b//;
1467 $s =~ s/^\s*{//;
1468 if ($s =~ s/^\s*\\//) {
1469 $continuation = 1;
1470 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001471 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001472 $check = 1;
1473 $cond_lines++;
1474 }
1475
1476 # Also ignore a loop construct at the end of a
1477 # preprocessor statement.
1478 if (($prevline =~ /^.\s*#\s*define\s/ ||
1479 $prevline =~ /\\\s*$/) && $continuation == 0) {
1480 $check = 0;
1481 }
1482
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001483 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001484 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001485 while ($cond_ptr != $cond_lines) {
1486 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001487
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001488 # If we see an #else/#elif then the code
1489 # is not linear.
1490 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1491 $check = 0;
1492 }
1493
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001494 # Ignore:
1495 # 1) blank lines, they should be at 0,
1496 # 2) preprocessor lines, and
1497 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001498 if ($continuation ||
1499 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001500 $s =~ /^\s*#\s*?/ ||
1501 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001502 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001503 $s =~ s/^.*?\n//;
1504 $cond_lines++;
1505 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001506 }
1507
1508 my (undef, $sindent) = line_stats("+" . $s);
1509 my $stat_real = raw_line($linenr, $cond_lines);
1510
1511 # Check if either of these lines are modified, else
1512 # this is not this patch's fault.
1513 if (!defined($stat_real) ||
1514 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1515 $check = 0;
1516 }
1517 if (defined($stat_real) && $cond_lines > 1) {
1518 $stat_real = "[...]\n$stat_real";
1519 }
1520
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001521 #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001522
1523 if ($check && (($sindent % 8) != 0 ||
1524 ($sindent <= $indent && $s ne ''))) {
1525 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1526 }
1527 }
1528
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001529 # Track the 'values' across context and added lines.
1530 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001531 my ($curr_values, $curr_vars) =
1532 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001533 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001534 if ($dbg_values) {
1535 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001536 print "$linenr > .$outline\n";
1537 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001538 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001539 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001540 $prev_values = substr($curr_values, -1);
1541
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001542#ignore lines not being added
1543 if ($line=~/^[^\+]/) {next;}
1544
Andy Whitcroft653d4872007-06-23 17:16:34 -07001545# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001546 if ($dbg_type) {
1547 if ($line =~ /^.\s*$Declare\s*$/) {
1548 ERROR("TEST: is type\n" . $herecurr);
1549 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1550 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1551 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001552 next;
1553 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001554# TEST: allow direct testing of the attribute matcher.
1555 if ($dbg_attr) {
1556 if ($line =~ /^.\s*$Attribute\s*$/) {
1557 ERROR("TEST: is attr\n" . $herecurr);
1558 } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1559 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1560 }
1561 next;
1562 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001563
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001564# check for initialisation to aggregates open brace on the next line
1565 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1566 $line =~ /^.\s*{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001567 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001568 }
1569
Andy Whitcroft653d4872007-06-23 17:16:34 -07001570#
1571# Checks which are anchored on the added line.
1572#
1573
1574# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001575 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001576 my $path = $1;
1577 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001578 ERROR("malformed #include filename\n" .
1579 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001580 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001581 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001582
1583# no C99 // comments
1584 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001585 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001586 }
1587 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001588 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001589 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001590
1591#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -07001592 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1593 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1594 my $name = $1;
Andy Whitcroft48012052008-10-15 22:02:34 -07001595 if ($prevline !~ /(?:
1596 ^.}|
1597 ^.DEFINE_$Ident\(\Q$name\E\)|
1598 ^.DECLARE_$Ident\(\Q$name\E\)|
1599 ^.LIST_HEAD\(\Q$name\E\)|
1600 ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1601 \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
1602 )/x) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001603 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001604 }
1605 }
1606
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001607# check for external initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001608 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001609 ERROR("do not initialise externals to 0 or NULL\n" .
1610 $herecurr);
1611 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001612# check for static initialisers.
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001613 if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001614 ERROR("do not initialise statics to 0 or NULL\n" .
1615 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001616 }
1617
Andy Whitcroft653d4872007-06-23 17:16:34 -07001618# check for new typedefs, only function parameters and sparse annotations
1619# make sense.
1620 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001621 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001622 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001623 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001624 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001625 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001626 }
1627
1628# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001629 # (char*[ const])
1630 if ($line =~ m{\($NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)*)\)}) {
1631 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001632
Andy Whitcroft65863862009-01-06 14:41:21 -08001633 # Should start with a space.
1634 $to =~ s/^(\S)/ $1/;
1635 # Should not end with a space.
1636 $to =~ s/\s+$//;
1637 # '*'s should not have spaces between.
1638 while ($to =~ s/(.)\s\*/$1\*/) {
1639 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001640
Andy Whitcroft65863862009-01-06 14:41:21 -08001641 #print "from<$from> to<$to>\n";
1642 if ($from ne $to) {
1643 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1644 }
1645 } elsif ($line =~ m{\b$NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)?)($Ident)}) {
1646 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001647
Andy Whitcroft65863862009-01-06 14:41:21 -08001648 # Should start with a space.
1649 $to =~ s/^(\S)/ $1/;
1650 # Should not end with a space.
1651 $to =~ s/\s+$//;
1652 # '*'s should not have spaces between.
1653 while ($to =~ s/(.)\s\*/$1\*/) {
1654 }
1655 # Modifiers should have spaces.
1656 $to =~ s/(\b$Modifier$)/$1 /;
1657
1658 #print "from<$from> to<$to>\n";
1659 if ($from ne $to) {
1660 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1661 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001662 }
1663
1664# # no BUG() or BUG_ON()
1665# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1666# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1667# print "$herecurr";
1668# $clean = 0;
1669# }
1670
Andy Whitcroft8905a672007-11-28 16:21:06 -08001671 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001672 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001673 }
1674
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001675# printk should use KERN_* levels. Note that follow on printk's on the
1676# same line do not need a level, so we use the current block context
1677# to try and find and validate the current printk. In summary the current
1678# printk includes all preceeding printk's which have no newline on the end.
1679# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001680 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001681 my $ok = 0;
1682 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1683 #print "CHECK<$lines[$ln - 1]\n";
1684 # we have a preceeding printk if it ends
1685 # with "\n" ignore it, else it is to blame
1686 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1687 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1688 $ok = 1;
1689 }
1690 last;
1691 }
1692 }
1693 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001694 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001695 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001696 }
1697
Andy Whitcroft653d4872007-06-23 17:16:34 -07001698# function brace can't be on same line, except for #defines of do while,
1699# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001700 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1701 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001702 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001703 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001704
Andy Whitcroft8905a672007-11-28 16:21:06 -08001705# open braces for enum, union and struct go on the same line.
1706 if ($line =~ /^.\s*{/ &&
1707 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1708 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1709 }
1710
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001711# check for spacing round square brackets; allowed:
1712# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001713# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1714# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001715 while ($line =~ /(.*?\s)\[/g) {
1716 my ($where, $prefix) = ($-[1], $1);
1717 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001718 ($where != 0 || $prefix !~ /^.\s+$/) &&
1719 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001720 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1721 }
1722 }
1723
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001724# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001725 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001726 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001727 my $ctx_before = substr($line, 0, $-[1]);
1728 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001729
1730 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001731 if ($name =~ /^(?:
1732 if|for|while|switch|return|case|
1733 volatile|__volatile__|
1734 __attribute__|format|__extension__|
1735 asm|__asm__)$/x)
1736 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001737
1738 # cpp #define statements have non-optional spaces, ie
1739 # if there is a space between the name and the open
1740 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001741 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001742
1743 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001744 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001745
1746 # If this whole things ends with a type its most
1747 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001748 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001749
1750 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001751 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001752 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001753 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001754# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001755 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001756 my $ops = qr{
1757 <<=|>>=|<=|>=|==|!=|
1758 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1759 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001760 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1761 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001762 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001763 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001764 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001765
1766 my $blank = copy_spacing($opline);
1767
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001768 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001769 $off += length($elements[$n]);
1770
Andy Whitcroft773647a2008-03-28 14:15:58 -07001771 # Pick up the preceeding and succeeding characters.
1772 my $ca = substr($opline, 0, $off);
1773 my $cc = '';
1774 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1775 $cc = substr($opline, $off + length($elements[$n + 1]));
1776 }
1777 my $cb = "$ca$;$cc";
1778
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001779 my $a = '';
1780 $a = 'V' if ($elements[$n] ne '');
1781 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001782 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001783 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1784 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07001785 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001786
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001787 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001788
1789 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001790 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001791 $c = 'V' if ($elements[$n + 2] ne '');
1792 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001793 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001794 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1795 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001796 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001797 } else {
1798 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001799 }
1800
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001801 my $ctx = "${a}x${c}";
1802
1803 my $at = "(ctx:$ctx)";
1804
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001805 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001806 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001807
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001808 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001809 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001810
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001811 # Get the full operator variant.
1812 my $opv = $op . substr($curr_vars, $off, 1);
1813
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001814 # Ignore operators passed as parameters.
1815 if ($op_type ne 'V' &&
1816 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1817
Andy Whitcroftcf655042008-03-04 14:28:20 -08001818# # Ignore comments
1819# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001820
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001821 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001822 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001823 if ($ctx !~ /.x[WEBC]/ &&
1824 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001825 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001826 }
1827
1828 # // is a comment
1829 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001830
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001831 # No spaces for:
1832 # ->
1833 # : when part of a bitfield
1834 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001835 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001836 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001837 }
1838
1839 # , must have a space on the right.
1840 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001841 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001842 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001843 }
1844
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001845 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001846 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001847 #warn "'*' is part of type\n";
1848
1849 # unary operators should have a space before and
1850 # none after. May be left adjacent to another
1851 # unary operator, or a cast
1852 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001853 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07001854 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001855 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001856 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001857 }
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001858 if ($op eq '*' && $cc =~/\s*const\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001859 # A unary '*' may be const
1860
1861 } elsif ($ctx =~ /.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001862 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001863 }
1864
1865 # unary ++ and unary -- are allowed no space on one side.
1866 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001867 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1868 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001869 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001870 if ($ctx =~ /Wx[BE]/ ||
1871 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1872 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001873 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001874 if ($ctx =~ /ExW/) {
1875 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1876 }
1877
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001878
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001879 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001880 } elsif ($op eq '<<' or $op eq '>>' or
1881 $op eq '&' or $op eq '^' or $op eq '|' or
1882 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001883 $op eq '*' or $op eq '/' or
1884 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001885 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001886 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001887 ERROR("need consistent spacing around '$op' $at\n" .
1888 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001889 }
1890
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001891 # A colon needs no spaces before when it is
1892 # terminating a case value or a label.
1893 } elsif ($opv eq ':C' || $opv eq ':L') {
1894 if ($ctx =~ /Wx./) {
1895 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1896 }
1897
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001898 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08001899 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001900 my $ok = 0;
1901
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001902 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001903 if (($op eq '<' &&
1904 $cc =~ /^\S+\@\S+>/) ||
1905 ($op eq '>' &&
1906 $ca =~ /<\S+\@\S+$/))
1907 {
1908 $ok = 1;
1909 }
1910
1911 # Ignore ?:
1912 if (($opv eq ':O' && $ca =~ /\?$/) ||
1913 ($op eq '?' && $cc =~ /^:/)) {
1914 $ok = 1;
1915 }
1916
1917 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001918 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001919 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001920 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001921 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001922 }
1923 }
1924
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001925# check for multiple assignments
1926 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001927 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001928 }
1929
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001930## # check for multiple declarations, allowing for a function declaration
1931## # continuation.
1932## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1933## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1934##
1935## # Remove any bracketed sections to ensure we do not
1936## # falsly report the parameters of functions.
1937## my $ln = $line;
1938## while ($ln =~ s/\([^\(\)]*\)//g) {
1939## }
1940## if ($ln =~ /,/) {
1941## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1942## }
1943## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001944
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001945#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001946 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1947 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001948 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001949 }
1950
1951# closing brace should have a space following it when it has anything
1952# on the line
1953 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001954 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001955 }
1956
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001957# check spacing on square brackets
1958 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001959 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001960 }
1961 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001962 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001963 }
1964
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001965# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001966 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1967 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001968 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001969 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001970 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001971 $line !~ /for\s*\(.*;\s+\)/ &&
1972 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001973 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001974 }
1975
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001976#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001977 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001978 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001979 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001980 }
1981
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001982# Return is not a function.
1983 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1984 my $spacing = $1;
1985 my $value = $2;
1986
Andy Whitcroft86f9d052009-01-06 14:41:24 -08001987 # Flatten any parentheses
Andy Whitcroftfee61c42008-07-23 21:28:56 -07001988 $value =~ s/\)\(/\) \(/g;
Andy Whitcroft86f9d052009-01-06 14:41:24 -08001989 while ($value !~ /(?:$Ident|-?$Constant)\s*$Compare\s*(?:$Ident|-?$Constant)/ && $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001990 }
1991
1992 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1993 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1994
1995 } elsif ($spacing !~ /\s+/) {
1996 ERROR("space required before the open parenthesis '('\n" . $herecurr);
1997 }
1998 }
1999
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002000# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002001 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002002 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002003 }
2004
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002005# Check for illegal assignment in if conditional -- and check for trailing
2006# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002007 if ($line =~ /do\s*(?!{)/) {
2008 my ($stat_next) = ctx_statement_block($line_nr_next,
2009 $remain_next, $off_next);
2010 $stat_next =~ s/\n./\n /g;
2011 ##print "stat<$stat> stat_next<$stat_next>\n";
2012
2013 if ($stat_next =~ /^\s*while\b/) {
2014 # If the statement carries leading newlines,
2015 # then count those as offsets.
2016 my ($whitespace) =
2017 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2018 my $offset =
2019 statement_rawlines($whitespace) - 1;
2020
2021 $suppress_whiletrailers{$line_nr_next +
2022 $offset} = 1;
2023 }
2024 }
2025 if (!defined $suppress_whiletrailers{$linenr} &&
2026 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002027 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002028
2029 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002030 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002031 }
2032
2033 # Find out what is on the end of the line after the
2034 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002035 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002036 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002037 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002038 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2039 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002040 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002041 # Find out how long the conditional actually is.
2042 my @newlines = ($c =~ /\n/gs);
2043 my $cond_lines = 1 + $#newlines;
2044
2045 my $stat_real = raw_line($linenr, $cond_lines);
2046 if (defined($stat_real) && $cond_lines > 1) {
2047 $stat_real = "[...]\n$stat_real";
2048 }
2049
2050 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002051 }
2052 }
2053
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002054# Check for bitwise tests written as boolean
2055 if ($line =~ /
2056 (?:
2057 (?:\[|\(|\&\&|\|\|)
2058 \s*0[xX][0-9]+\s*
2059 (?:\&\&|\|\|)
2060 |
2061 (?:\&\&|\|\|)
2062 \s*0[xX][0-9]+\s*
2063 (?:\&\&|\|\||\)|\])
2064 )/x)
2065 {
2066 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2067 }
2068
Andy Whitcroft8905a672007-11-28 16:21:06 -08002069# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002070 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2071 my $s = $1;
2072 $s =~ s/$;//g; # Remove any comments
2073 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2074 ERROR("trailing statements should be on next line\n" . $herecurr);
2075 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002076 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002077# case and default should not have general statements after them
2078 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2079 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002080 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002081 \s*return\s+
2082 )/xg)
2083 {
2084 ERROR("trailing statements should be on next line\n" . $herecurr);
2085 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002086
2087 # Check for }<nl>else {, these must be at the same
2088 # indent level to be relevant to each other.
2089 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2090 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002091 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002092 }
2093
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002094 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2095 $previndent == $indent) {
2096 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2097
2098 # Find out what is on the end of the line after the
2099 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002100 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002101 $s =~ s/\n.*//g;
2102
2103 if ($s =~ /^\s*;/) {
2104 ERROR("while should follow close brace '}'\n" . $hereprev);
2105 }
2106 }
2107
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002108#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2109# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2110# print "No studly caps, use _\n";
2111# print "$herecurr";
2112# $clean = 0;
2113# }
2114
2115#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002116 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002117 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002118 }
2119
Andy Whitcroft653d4872007-06-23 17:16:34 -07002120#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002121 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002122 my $file = "$1.h";
2123 my $checkfile = "include/linux/$file";
2124 if (-f "$root/$checkfile" &&
2125 $realfile ne $checkfile &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002126 $1 ne 'irq')
2127 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002128 if ($realfile =~ m{^arch/}) {
2129 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2130 } else {
2131 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2132 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002133 }
2134 }
2135
Andy Whitcroft653d4872007-06-23 17:16:34 -07002136# multi-statement macros should be enclosed in a do while loop, grab the
2137# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002138# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07002139 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2140 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002141 my $ln = $linenr;
2142 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002143 my ($off, $dstat, $dcond, $rest);
2144 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002145
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002146 my $args = defined($1);
2147
2148 # Find the end of the macro and limit our statement
2149 # search to that.
2150 while ($cnt > 0 && defined $lines[$ln - 1] &&
2151 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2152 {
2153 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002154 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002155 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002156 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002157 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002158
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002159 ($dstat, $dcond, $ln, $cnt, $off) =
2160 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2161 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002162 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002163
2164 # Extract the remainder of the define (if any) and
2165 # rip off surrounding spaces, and trailing \'s.
2166 $rest = '';
Andy Whitcroft636d140a2008-10-15 22:02:18 -07002167 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2168 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002169 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2170 $rest .= substr($lines[$ln - 1], $off) . "\n";
2171 $cnt--;
2172 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002173 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002174 $off = 0;
2175 }
2176 $rest =~ s/\\\n.//g;
2177 $rest =~ s/^\s*//s;
2178 $rest =~ s/\s*$//s;
2179
2180 # Clean up the original statement.
2181 if ($args) {
2182 substr($dstat, 0, length($dcond), '');
2183 } else {
2184 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2185 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002186 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002187 $dstat =~ s/\\\n.//g;
2188 $dstat =~ s/^\s*//s;
2189 $dstat =~ s/\s*$//s;
2190
2191 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002192 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2193 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2194 $dstat =~ s/\[[^\{\}]*\]/1/)
2195 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002196 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002197
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002198 my $exceptions = qr{
2199 $Declare|
2200 module_param_named|
2201 MODULE_PARAM_DESC|
2202 DECLARE_PER_CPU|
2203 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002204 __typeof__\(|
2205 \.$Ident\s*=\s*
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002206 }x;
Andy Whitcroft383099f2009-01-06 14:41:18 -08002207 #print "REST<$rest> dstat<$dstat>\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002208 if ($rest ne '') {
2209 if ($rest !~ /while\s*\(/ &&
2210 $dstat !~ /$exceptions/)
2211 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002212 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002213 }
2214
2215 } elsif ($ctx !~ /;/) {
2216 if ($dstat ne '' &&
2217 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2218 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002219 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002220 $dstat =~ /$Operators/)
2221 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002222 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002223 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002224 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002225 }
2226
Mike Frysinger080ba922009-01-06 14:41:25 -08002227# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2228# all assignments may have only one of the following with an assignment:
2229# .
2230# ALIGN(...)
2231# VMLINUX_SYMBOL(...)
2232 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2233 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2234 }
2235
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002236# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002237 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2238 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002239 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002240 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002241 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2242 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002243 my $allowed = 0;
2244 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002245 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002246 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002247 for my $chunk (@chunks) {
2248 my ($cond, $block) = @{$chunk};
2249
Andy Whitcroft773647a2008-03-28 14:15:58 -07002250 # If the condition carries leading newlines, then count those as offsets.
2251 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2252 my $offset = statement_rawlines($whitespace) - 1;
2253
2254 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2255
2256 # We have looked at and allowed this specific line.
2257 $suppress_ifbraces{$ln + $offset} = 1;
2258
2259 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002260 $ln += statement_rawlines($block) - 1;
2261
Andy Whitcroft773647a2008-03-28 14:15:58 -07002262 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002263
2264 $seen++ if ($block =~ /^\s*{/);
2265
Andy Whitcroftcf655042008-03-04 14:28:20 -08002266 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2267 if (statement_lines($cond) > 1) {
2268 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002269 $allowed = 1;
2270 }
2271 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002272 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002273 $allowed = 1;
2274 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002275 if (statement_block_size($block) > 1) {
2276 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002277 $allowed = 1;
2278 }
2279 }
2280 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002281 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002282 }
2283 }
2284 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002286 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002287 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002288
Andy Whitcroftcf655042008-03-04 14:28:20 -08002289 # Check the pre-context.
2290 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2291 #print "APW: ALLOWED: pre<$1>\n";
2292 $allowed = 1;
2293 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002294
2295 my ($level, $endln, @chunks) =
2296 ctx_statement_full($linenr, $realcnt, $-[0]);
2297
Andy Whitcroftcf655042008-03-04 14:28:20 -08002298 # Check the condition.
2299 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002300 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002301 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002302 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002303 }
2304 if (statement_lines($cond) > 1) {
2305 #print "APW: ALLOWED: cond<$cond>\n";
2306 $allowed = 1;
2307 }
2308 if ($block =~/\b(?:if|for|while)\b/) {
2309 #print "APW: ALLOWED: block<$block>\n";
2310 $allowed = 1;
2311 }
2312 if (statement_block_size($block) > 1) {
2313 #print "APW: ALLOWED: lines block<$block>\n";
2314 $allowed = 1;
2315 }
2316 # Check the post-context.
2317 if (defined $chunks[1]) {
2318 my ($cond, $block) = @{$chunks[1]};
2319 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002320 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002321 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002322 if ($block =~ /^\s*\{/) {
2323 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2324 $allowed = 1;
2325 }
2326 }
2327 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2328 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002329 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002330
Andy Whitcroftf0556632008-10-15 22:02:23 -07002331 for (my $n = 0; $n < $cnt; $n++) {
2332 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002333 }
2334
2335 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002336 }
2337 }
2338
Andy Whitcroft653d4872007-06-23 17:16:34 -07002339# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002340 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002341 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002342 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002343 }
2344 }
2345
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002346# don't use deprecated functions
2347 for my $func (@dep_functions) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002348 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002349 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002350 }
2351 }
2352
2353# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002354 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2355 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002356 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002357 }
2358
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002359# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2360 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2361 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2362 }
2363
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002364# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002365 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002366 CHK("if this code is redundant consider removing it\n" .
2367 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002368 }
2369
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002370# check for needless kfree() checks
2371 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2372 my $expr = $1;
2373 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002374 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002375 }
2376 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002377# check for needless usb_free_urb() checks
2378 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2379 my $expr = $1;
2380 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2381 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2382 }
2383 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002384
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002385# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002386# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002387# print "#ifdef in C files should be avoided\n";
2388# print "$herecurr";
2389# $clean = 0;
2390# }
2391
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002392# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002393 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002394 ERROR("exactly one space required after that #$1\n" . $herecurr);
2395 }
2396
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002397# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002398 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2399 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002400 my $which = $1;
2401 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002402 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002403 }
2404 }
2405# check for memory barriers without a comment.
2406 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2407 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002408 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002409 }
2410 }
2411# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002412 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002413 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002414 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002415
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002416# check the location of the inline attribute, that it is between
2417# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002418 if ($line =~ /\b$Type\s+$Inline\b/ ||
2419 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002420 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2421 }
2422
Andy Whitcroft8905a672007-11-28 16:21:06 -08002423# Check for __inline__ and __inline, prefer inline
2424 if ($line =~ /\b(__inline__|__inline)\b/) {
2425 WARN("plain inline is preferred over $1\n" . $herecurr);
2426 }
2427
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002428# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002429 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002430 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002431 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002432 my $function_name = $1;
2433 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002434
2435 my $s = $stat;
2436 if (defined $cond) {
2437 substr($s, 0, length($cond), '');
2438 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002439 if ($s =~ /^\s*;/ &&
2440 $function_name ne 'uninitialized_var')
2441 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002442 WARN("externs should be avoided in .c files\n" . $herecurr);
2443 }
2444
2445 if ($paren_space =~ /\n/) {
2446 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2447 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002448
2449 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2450 $stat =~ /^.\s*extern\s+/)
2451 {
2452 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002453 }
2454
2455# checks for new __setup's
2456 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2457 my $name = $1;
2458
2459 if (!grep(/$name/, @setup_docs)) {
2460 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2461 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002462 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002463
2464# check for pointless casting of kmalloc return
2465 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2466 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2467 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002468
2469# check for gcc specific __FUNCTION__
2470 if ($line =~ /__FUNCTION__/) {
2471 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2472 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002473
2474# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002475 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002476 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2477 }
2478# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002479 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002480 WARN("consider using a completion\n" . $herecurr);
2481 }
2482# recommend strict_strto* over simple_strto*
2483 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2484 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2485 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002486# check for __initcall(), use device_initcall() explicitly please
2487 if ($line =~ /^.\s*__initcall\s*\(/) {
2488 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2489 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002490
2491# use of NR_CPUS is usually wrong
2492# ignore definitions of NR_CPUS and usage to define arrays as likely right
2493 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002494 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2495 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002496 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2497 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2498 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002499 {
2500 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2501 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002502
2503# check for %L{u,d,i} in strings
2504 my $string;
2505 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2506 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07002507 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002508 if ($string =~ /(?<!%)%L[udi]/) {
2509 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2510 last;
2511 }
2512 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002513
2514# whine mightly about in_atomic
2515 if ($line =~ /\bin_atomic\s*\(/) {
2516 if ($realfile =~ m@^drivers/@) {
2517 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2518 } else {
2519 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2520 }
2521 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002522 }
2523
2524 # If we have no input at all, then there is nothing to report on
2525 # so just keep quiet.
2526 if ($#rawlines == -1) {
2527 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002528 }
2529
Andy Whitcroft8905a672007-11-28 16:21:06 -08002530 # In mailback mode only produce a report in the negative, for
2531 # things that appear to be patches.
2532 if ($mailback && ($clean == 1 || !$is_patch)) {
2533 exit(0);
2534 }
2535
2536 # This is not a patch, and we are are in 'no-patch' mode so
2537 # just keep quiet.
2538 if (!$chk_patch && !$is_patch) {
2539 exit(0);
2540 }
2541
2542 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002543 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002544 }
2545 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002546 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002547 }
2548
Andy Whitcroft8905a672007-11-28 16:21:06 -08002549 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002550 if ($summary && !($clean == 1 && $quiet == 1)) {
2551 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002552 print "total: $cnt_error errors, $cnt_warn warnings, " .
2553 (($check)? "$cnt_chk checks, " : "") .
2554 "$cnt_lines lines checked\n";
2555 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002556 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002557
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002558 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002559 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002560 }
2561 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002562 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002563 print "are false positives report them to the maintainer, see\n";
2564 print "CHECKPATCH in MAINTAINERS.\n";
2565 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002566
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002567 return $clean;
2568}