blob: b43e309c38ff22b1d39813c7a5d72802d8570ec5 [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 Whitcroft04876832009-09-21 17:04:39 -070013my $V = '0.29';
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;
Hannes Eder77f5b102009-09-21 17:04:37 -070031my $help = 0;
32
33sub help {
34 my ($exitcode) = @_;
35
36 print << "EOM";
37Usage: $P [OPTION]... [FILE]...
38Version: $V
39
40Options:
41 -q, --quiet quiet
42 --no-tree run without a kernel tree
43 --no-signoff do not check for 'Signed-off-by' line
44 --patch treat FILE as patchfile (default)
45 --emacs emacs compile window format
46 --terse one line per report
47 -f, --file treat FILE as regular source file
48 --subjective, --strict enable more subjective tests
49 --root=PATH PATH to the kernel tree root
50 --no-summary suppress the per-file summary
51 --mailback only produce a report in case of warnings/errors
52 --summary-file include the filename in summary
53 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
54 'values', 'possible', 'type', and 'attr' (default
55 is all off)
56 --test-only=WORD report only warnings/errors containing WORD
57 literally
58 -h, --help, --version display this help and exit
59
60When FILE is - read standard input.
61EOM
62
63 exit($exitcode);
64}
65
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070066GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070067 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070068 'tree!' => \$tree,
69 'signoff!' => \$chk_signoff,
70 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070071 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080072 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -070073 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070074 'subjective!' => \$check,
75 'strict!' => \$check,
76 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080077 'summary!' => \$summary,
78 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080079 'summary-file!' => \$summary_file,
80
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080081 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -070082 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -070083 'h|help' => \$help,
84 'version' => \$help
85) or help(1);
86
87help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070088
89my $exit = 0;
90
91if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -070092 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070093 exit(1);
94}
95
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080096my $dbg_values = 0;
97my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -070098my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -070099my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800100for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800101 ## no critic
102 eval "\${dbg_$key} = '$debug{$key}';";
103 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800104}
105
Andy Whitcroft8905a672007-11-28 16:21:06 -0800106if ($terse) {
107 $emacs = 1;
108 $quiet++;
109}
110
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700111if ($tree) {
112 if (defined $root) {
113 if (!top_of_kernel_tree($root)) {
114 die "$P: $root: --root does not point at a valid tree\n";
115 }
116 } else {
117 if (top_of_kernel_tree('.')) {
118 $root = '.';
119 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
120 top_of_kernel_tree($1)) {
121 $root = $1;
122 }
123 }
124
125 if (!defined $root) {
126 print "Must be run from the top-level dir. of a kernel tree\n";
127 exit(2);
128 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700129}
130
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700131my $emitted_corrupt = 0;
132
133our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
134our $Storage = qr{extern|static|asmlinkage};
135our $Sparse = qr{
136 __user|
137 __kernel|
138 __force|
139 __iomem|
140 __must_check|
141 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800142 __kprobes|
143 __ref
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700144 }x;
145our $Attribute = qr{
146 const|
147 __read_mostly|
148 __kprobes|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700149 __(?:mem|cpu|dev|)(?:initdata|init)|
150 ____cacheline_aligned|
151 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800152 ____cacheline_internodealigned_in_smp|
153 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700154 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700155our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700156our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700157our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
158our $Lval = qr{$Ident(?:$Member)*};
159
160our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
161our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800162our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700163our $Operators = qr{
164 <=|>=|==|!=|
165 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800166 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700167 }x;
168
Andy Whitcroft8905a672007-11-28 16:21:06 -0800169our $NonptrType;
170our $Type;
171our $Declare;
172
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700173our $UTF8 = qr {
174 [\x09\x0A\x0D\x20-\x7E] # ASCII
175 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
176 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
177 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
178 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
179 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
180 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
181 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
182}x;
183
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700184our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700185 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700186 atomic_t
187)};
188
Andy Whitcroft8905a672007-11-28 16:21:06 -0800189our @typeList = (
190 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700191 qr{(?:unsigned\s+)?char},
192 qr{(?:unsigned\s+)?short},
193 qr{(?:unsigned\s+)?int},
194 qr{(?:unsigned\s+)?long},
195 qr{(?:unsigned\s+)?long\s+int},
196 qr{(?:unsigned\s+)?long\s+long},
197 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800198 qr{unsigned},
199 qr{float},
200 qr{double},
201 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800202 qr{struct\s+$Ident},
203 qr{union\s+$Ident},
204 qr{enum\s+$Ident},
205 qr{${Ident}_t},
206 qr{${Ident}_handler},
207 qr{${Ident}_handler_fn},
208);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700209our @modifierList = (
210 qr{fastcall},
211);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800212
213sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700214 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
215 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700216 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800217 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700218 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800219 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700220 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700221 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700222 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800223 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700224 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800225 }x;
226 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700227 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800228 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700229 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800230 }x;
231 $Declare = qr{(?:$Storage\s+)?$Type};
232}
233build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700234
235$chk_signoff = 0 if ($file);
236
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700237my @dep_includes = ();
238my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700239my $removal = "Documentation/feature-removal-schedule.txt";
240if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800241 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700242 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800243 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700244 if (/^Check:\s+(.*\S)/) {
245 for my $entry (split(/[, ]+/, $1)) {
246 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700247 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700248
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700249 } elsif ($entry !~ m@/@) {
250 push(@dep_functions, $entry);
251 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700252 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700253 }
254 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800255 close($REMOVE);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700256}
257
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700258my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800259my @lines = ();
260my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700261for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800262 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700263 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800264 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700265 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800266 } elsif ($filename eq '-') {
267 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700268 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800269 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700270 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700271 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800272 if ($filename eq '-') {
273 $vname = 'Your patch';
274 } else {
275 $vname = $filename;
276 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800277 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700278 chomp;
279 push(@rawlines, $_);
280 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800281 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800282 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700283 $exit = 1;
284 }
285 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800286 @lines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700287}
288
289exit($exit);
290
291sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700292 my ($root) = @_;
293
294 my @tree_check = (
295 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
296 "README", "Documentation", "arch", "include", "drivers",
297 "fs", "init", "ipc", "kernel", "lib", "scripts",
298 );
299
300 foreach my $check (@tree_check) {
301 if (! -e $root . '/' . $check) {
302 return 0;
303 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700304 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700305 return 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700306}
307
308sub expand_tabs {
309 my ($str) = @_;
310
311 my $res = '';
312 my $n = 0;
313 for my $c (split(//, $str)) {
314 if ($c eq "\t") {
315 $res .= ' ';
316 $n++;
317 for (; ($n % 8) != 0; $n++) {
318 $res .= ' ';
319 }
320 next;
321 }
322 $res .= $c;
323 $n++;
324 }
325
326 return $res;
327}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700328sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700329 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700330 return $res;
331}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700332
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700333sub line_stats {
334 my ($line) = @_;
335
336 # Drop the diff line leader and expand tabs
337 $line =~ s/^.//;
338 $line = expand_tabs($line);
339
340 # Pick the indent from the front of the line.
341 my ($white) = ($line =~ /^(\s*)/);
342
343 return (length($line), length($white));
344}
345
Andy Whitcroft773647a2008-03-28 14:15:58 -0700346my $sanitise_quote = '';
347
348sub sanitise_line_reset {
349 my ($in_comment) = @_;
350
351 if ($in_comment) {
352 $sanitise_quote = '*/';
353 } else {
354 $sanitise_quote = '';
355 }
356}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700357sub sanitise_line {
358 my ($line) = @_;
359
360 my $res = '';
361 my $l = '';
362
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800363 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700364 my $off = 0;
365 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700366
Andy Whitcroft773647a2008-03-28 14:15:58 -0700367 # Always copy over the diff marker.
368 $res = substr($line, 0, 1);
369
370 for ($off = 1; $off < length($line); $off++) {
371 $c = substr($line, $off, 1);
372
373 # Comments we are wacking completly including the begin
374 # and end, all to $;.
375 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
376 $sanitise_quote = '*/';
377
378 substr($res, $off, 2, "$;$;");
379 $off++;
380 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800381 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700382 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700383 $sanitise_quote = '';
384 substr($res, $off, 2, "$;$;");
385 $off++;
386 next;
387 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700388 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
389 $sanitise_quote = '//';
390
391 substr($res, $off, 2, $sanitise_quote);
392 $off++;
393 next;
394 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700395
396 # A \ in a string means ignore the next character.
397 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
398 $c eq "\\") {
399 substr($res, $off, 2, 'XX');
400 $off++;
401 next;
402 }
403 # Regular quotes.
404 if ($c eq "'" || $c eq '"') {
405 if ($sanitise_quote eq '') {
406 $sanitise_quote = $c;
407
408 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700409 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700410 } elsif ($sanitise_quote eq $c) {
411 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700412 }
413 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700414
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800415 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700416 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
417 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700418 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
419 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700420 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
421 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700422 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700423 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700424 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800425 }
426
Daniel Walker113f04a2009-09-21 17:04:35 -0700427 if ($sanitise_quote eq '//') {
428 $sanitise_quote = '';
429 }
430
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800431 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700432 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800433 my $clean = 'X' x length($1);
434 $res =~ s@\<.*\>@<$clean>@;
435
436 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700437 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800438 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700439 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800440 }
441
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700442 return $res;
443}
444
Andy Whitcroft8905a672007-11-28 16:21:06 -0800445sub ctx_statement_block {
446 my ($linenr, $remain, $off) = @_;
447 my $line = $linenr - 1;
448 my $blk = '';
449 my $soff = $off;
450 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700451 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800452
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800453 my $loff = 0;
454
Andy Whitcroft8905a672007-11-28 16:21:06 -0800455 my $type = '';
456 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800457 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800458 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800459 my $c;
460 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800461
462 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800463 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800464 @stack = (['', 0]) if ($#stack == -1);
465
Andy Whitcroft773647a2008-03-28 14:15:58 -0700466 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800467 # If we are about to drop off the end, pull in more
468 # context.
469 if ($off >= $len) {
470 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700471 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800472 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800473 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800474 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800475 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800476 $len = length($blk);
477 $line++;
478 last;
479 }
480 # Bail if there is no further context.
481 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800482 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800483 last;
484 }
485 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800486 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800487 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800488 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800489
Andy Whitcroft773647a2008-03-28 14:15:58 -0700490 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800491
492 # Handle nested #if/#else.
493 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
494 push(@stack, [ $type, $level ]);
495 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
496 ($type, $level) = @{$stack[$#stack - 1]};
497 } elsif ($remainder =~ /^#\s*endif\b/) {
498 ($type, $level) = @{pop(@stack)};
499 }
500
Andy Whitcroft8905a672007-11-28 16:21:06 -0800501 # Statement ends at the ';' or a close '}' at the
502 # outermost level.
503 if ($level == 0 && $c eq ';') {
504 last;
505 }
506
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800507 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700508 if ($level == 0 && $coff_set == 0 &&
509 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
510 $remainder =~ /^(else)(?:\s|{)/ &&
511 $remainder !~ /^else\s+if\b/) {
512 $coff = $off + length($1) - 1;
513 $coff_set = 1;
514 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
515 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800516 }
517
Andy Whitcroft8905a672007-11-28 16:21:06 -0800518 if (($type eq '' || $type eq '(') && $c eq '(') {
519 $level++;
520 $type = '(';
521 }
522 if ($type eq '(' && $c eq ')') {
523 $level--;
524 $type = ($level != 0)? '(' : '';
525
526 if ($level == 0 && $coff < $soff) {
527 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700528 $coff_set = 1;
529 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800530 }
531 }
532 if (($type eq '' || $type eq '{') && $c eq '{') {
533 $level++;
534 $type = '{';
535 }
536 if ($type eq '{' && $c eq '}') {
537 $level--;
538 $type = ($level != 0)? '{' : '';
539
540 if ($level == 0) {
541 last;
542 }
543 }
544 $off++;
545 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700546 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800547 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700548 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800549 $line++;
550 $remain--;
551 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800552
553 my $statement = substr($blk, $soff, $off - $soff + 1);
554 my $condition = substr($blk, $soff, $coff - $soff + 1);
555
556 #warn "STATEMENT<$statement>\n";
557 #warn "CONDITION<$condition>\n";
558
Andy Whitcroft773647a2008-03-28 14:15:58 -0700559 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800560
561 return ($statement, $condition,
562 $line, $remain + 1, $off - $loff + 1, $level);
563}
564
Andy Whitcroftcf655042008-03-04 14:28:20 -0800565sub statement_lines {
566 my ($stmt) = @_;
567
568 # Strip the diff line prefixes and rip blank lines at start and end.
569 $stmt =~ s/(^|\n)./$1/g;
570 $stmt =~ s/^\s*//;
571 $stmt =~ s/\s*$//;
572
573 my @stmt_lines = ($stmt =~ /\n/g);
574
575 return $#stmt_lines + 2;
576}
577
578sub statement_rawlines {
579 my ($stmt) = @_;
580
581 my @stmt_lines = ($stmt =~ /\n/g);
582
583 return $#stmt_lines + 2;
584}
585
586sub statement_block_size {
587 my ($stmt) = @_;
588
589 $stmt =~ s/(^|\n)./$1/g;
590 $stmt =~ s/^\s*{//;
591 $stmt =~ s/}\s*$//;
592 $stmt =~ s/^\s*//;
593 $stmt =~ s/\s*$//;
594
595 my @stmt_lines = ($stmt =~ /\n/g);
596 my @stmt_statements = ($stmt =~ /;/g);
597
598 my $stmt_lines = $#stmt_lines + 2;
599 my $stmt_statements = $#stmt_statements + 1;
600
601 if ($stmt_lines > $stmt_statements) {
602 return $stmt_lines;
603 } else {
604 return $stmt_statements;
605 }
606}
607
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800608sub ctx_statement_full {
609 my ($linenr, $remain, $off) = @_;
610 my ($statement, $condition, $level);
611
612 my (@chunks);
613
Andy Whitcroftcf655042008-03-04 14:28:20 -0800614 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800615 ($statement, $condition, $linenr, $remain, $off, $level) =
616 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700617 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800618 push(@chunks, [ $condition, $statement ]);
619 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
620 return ($level, $linenr, @chunks);
621 }
622
623 # Pull in the following conditional/block pairs and see if they
624 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800625 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800626 ($statement, $condition, $linenr, $remain, $off, $level) =
627 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800628 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700629 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800630 #print "C: push\n";
631 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800632 }
633
634 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800635}
636
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700637sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700638 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700639 my $line;
640 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700641 my $blk = '';
642 my @o;
643 my @c;
644 my @res = ();
645
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700646 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800647 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700648 for ($line = $start; $remain > 0; $line++) {
649 next if ($rawlines[$line] =~ /^-/);
650 $remain--;
651
652 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800653
654 # Handle nested #if/#else.
655 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
656 push(@stack, $level);
657 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
658 $level = $stack[$#stack - 1];
659 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
660 $level = pop(@stack);
661 }
662
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700663 foreach my $c (split(//, $rawlines[$line])) {
664 ##print "C<$c>L<$level><$open$close>O<$off>\n";
665 if ($off > 0) {
666 $off--;
667 next;
668 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700669
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700670 if ($c eq $close && $level > 0) {
671 $level--;
672 last if ($level == 0);
673 } elsif ($c eq $open) {
674 $level++;
675 }
676 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700677
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700678 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700679 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700680 }
681
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700682 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700683 }
684
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700685 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700686}
687sub ctx_block_outer {
688 my ($linenr, $remain) = @_;
689
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700690 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
691 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700692}
693sub ctx_block {
694 my ($linenr, $remain) = @_;
695
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700696 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
697 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700698}
699sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700700 my ($linenr, $remain, $off) = @_;
701
702 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
703 return @r;
704}
705sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700706 my ($linenr, $remain) = @_;
707
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700708 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700709}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700710sub ctx_statement_level {
711 my ($linenr, $remain, $off) = @_;
712
713 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
714}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700715
716sub ctx_locate_comment {
717 my ($first_line, $end_line) = @_;
718
719 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700720 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700721 return $current_comment if (defined $current_comment);
722
723 # Look through the context and try and figure out if there is a
724 # comment.
725 my $in_comment = 0;
726 $current_comment = '';
727 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700728 my $line = $rawlines[$linenr - 1];
729 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700730 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
731 $in_comment = 1;
732 }
733 if ($line =~ m@/\*@) {
734 $in_comment = 1;
735 }
736 if (!$in_comment && $current_comment ne '') {
737 $current_comment = '';
738 }
739 $current_comment .= $line . "\n" if ($in_comment);
740 if ($line =~ m@\*/@) {
741 $in_comment = 0;
742 }
743 }
744
745 chomp($current_comment);
746 return($current_comment);
747}
748sub ctx_has_comment {
749 my ($first_line, $end_line) = @_;
750 my $cmt = ctx_locate_comment($first_line, $end_line);
751
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700752 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700753 ##print "CMMT: $cmt\n";
754
755 return ($cmt ne '');
756}
757
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700758sub raw_line {
759 my ($linenr, $cnt) = @_;
760
761 my $offset = $linenr - 1;
762 $cnt++;
763
764 my $line;
765 while ($cnt) {
766 $line = $rawlines[$offset++];
767 next if (defined($line) && $line =~ /^-/);
768 $cnt--;
769 }
770
771 return $line;
772}
773
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700774sub cat_vet {
775 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700776 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700777
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700778 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700779 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
780 $res .= $1;
781 if ($2 ne '') {
782 $coded = sprintf("^%c", unpack('C', $2) + 64);
783 $res .= $coded;
784 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700785 }
786 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700787
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700788 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700789}
790
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800791my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800792my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800793my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700794my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800795
796sub annotate_reset {
797 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800798 $av_pending = '_';
799 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700800 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800801}
802
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700803sub annotate_values {
804 my ($stream, $type) = @_;
805
806 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700807 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700808 my $cur = $stream;
809
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800810 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700811
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700812 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700813 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800814 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700815 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700816 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800817 print "WS($1)\n" if ($dbg_values > 1);
818 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800819 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800820 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700821 }
822
Andy Whitcroft8ea3eb92008-07-23 21:29:08 -0700823 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800824 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700825 $type = 'T';
826
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700827 } elsif ($cur =~ /^($Modifier)\s*/) {
828 print "MODIFIER($1)\n" if ($dbg_values > 1);
829 $type = 'T';
830
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700831 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700832 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800833 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700834 push(@av_paren_type, $type);
835 if ($2 ne '') {
836 $av_pending = 'N';
837 }
838 $type = 'E';
839
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700840 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700841 print "UNDEF($1)\n" if ($dbg_values > 1);
842 $av_preprocessor = 1;
843 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700844
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700845 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800846 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800847 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800848
849 push(@av_paren_type, $type);
850 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700851 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800852
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700853 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800854 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
855 $av_preprocessor = 1;
856
857 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
858
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700859 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800860
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700861 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800862 print "PRE_END($1)\n" if ($dbg_values > 1);
863
864 $av_preprocessor = 1;
865
866 # Assume all arms of the conditional end as this
867 # one does, and continue as if the #endif was not here.
868 pop(@av_paren_type);
869 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700870 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700871
872 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800873 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700874
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700875 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
876 print "ATTR($1)\n" if ($dbg_values > 1);
877 $av_pending = $type;
878 $type = 'N';
879
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700880 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800881 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700882 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800883 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700884 }
885 $type = 'N';
886
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700887 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800888 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700889 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700890 $type = 'N';
891
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700892 } elsif ($cur =~/^(case)/o) {
893 print "CASE($1)\n" if ($dbg_values > 1);
894 $av_pend_colon = 'C';
895 $type = 'N';
896
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700897 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800898 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700899 $type = 'N';
900
901 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800902 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800903 push(@av_paren_type, $av_pending);
904 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700905 $type = 'N';
906
907 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800908 my $new_type = pop(@av_paren_type);
909 if ($new_type ne '_') {
910 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800911 print "PAREN('$1') -> $type\n"
912 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700913 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800914 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700915 }
916
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700917 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800918 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700919 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800920 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700921
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800922 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
923 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700924 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800925 } elsif ($type eq 'E') {
926 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700927 }
928 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
929 $type = 'V';
930
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700931 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800932 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700933 $type = 'V';
934
935 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800936 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700937 $type = 'N';
938
Andy Whitcroftcf655042008-03-04 14:28:20 -0800939 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800940 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800941 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700942 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800943
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800944 } elsif ($cur =~/^(,)/) {
945 print "COMMA($1)\n" if ($dbg_values > 1);
946 $type = 'C';
947
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700948 } elsif ($cur =~ /^(\?)/o) {
949 print "QUESTION($1)\n" if ($dbg_values > 1);
950 $type = 'N';
951
952 } elsif ($cur =~ /^(:)/o) {
953 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
954
955 substr($var, length($res), 1, $av_pend_colon);
956 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
957 $type = 'E';
958 } else {
959 $type = 'N';
960 }
961 $av_pend_colon = 'O';
962
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800963 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800964 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700965 $type = 'N';
966
Andy Whitcroft0d413862008-10-15 22:02:16 -0700967 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -0700968 my $variant;
969
970 print "OPV($1)\n" if ($dbg_values > 1);
971 if ($type eq 'V') {
972 $variant = 'B';
973 } else {
974 $variant = 'U';
975 }
976
977 substr($var, length($res), 1, $variant);
978 $type = 'N';
979
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700980 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800981 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700982 if ($1 ne '++' && $1 ne '--') {
983 $type = 'N';
984 }
985
986 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800987 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700988 }
989 if (defined $1) {
990 $cur = substr($cur, length($1));
991 $res .= $type x length($1);
992 }
993 }
994
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700995 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700996}
997
Andy Whitcroft8905a672007-11-28 16:21:06 -0800998sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800999 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001000 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001001 ^(?:
1002 $Modifier|
1003 $Storage|
1004 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001005 DEFINE_\S+
1006 )$|
1007 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001008 goto|
1009 return|
1010 case|
1011 else|
1012 asm|__asm__|
1013 do
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001014 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001015 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001016 )}x;
1017 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1018 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001019 # Check for modifiers.
1020 $possible =~ s/\s*$Storage\s*//g;
1021 $possible =~ s/\s*$Sparse\s*//g;
1022 if ($possible =~ /^\s*$/) {
1023
1024 } elsif ($possible =~ /\s/) {
1025 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001026 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001027 if ($modifier !~ $notPermitted) {
1028 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1029 push(@modifierList, $modifier);
1030 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001031 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001032
1033 } else {
1034 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1035 push(@typeList, $possible);
1036 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001037 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001038 } else {
1039 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001040 }
1041}
1042
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001043my $prefix = '';
1044
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001045sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001046 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1047 return 0;
1048 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001049 my $line = $prefix . $_[0];
1050
1051 $line = (split('\n', $line))[0] . "\n" if ($terse);
1052
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001053 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001054
1055 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001056}
1057sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001058 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001059}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001060sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001061 if (report("ERROR: $_[0]\n")) {
1062 our $clean = 0;
1063 our $cnt_error++;
1064 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001065}
1066sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001067 if (report("WARNING: $_[0]\n")) {
1068 our $clean = 0;
1069 our $cnt_warn++;
1070 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001071}
1072sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001073 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001074 our $clean = 0;
1075 our $cnt_chk++;
1076 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001077}
1078
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001079sub check_absolute_file {
1080 my ($absolute, $herecurr) = @_;
1081 my $file = $absolute;
1082
1083 ##print "absolute<$absolute>\n";
1084
1085 # See if any suffix of this path is a path within the tree.
1086 while ($file =~ s@^[^/]*/@@) {
1087 if (-f "$root/$file") {
1088 ##print "file<$file>\n";
1089 last;
1090 }
1091 }
1092 if (! -f _) {
1093 return 0;
1094 }
1095
1096 # It is, so see if the prefix is acceptable.
1097 my $prefix = $absolute;
1098 substr($prefix, -length($file)) = '';
1099
1100 ##print "prefix<$prefix>\n";
1101 if ($prefix ne ".../") {
1102 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1103 }
1104}
1105
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001106sub process {
1107 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001108
1109 my $linenr=0;
1110 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001111 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001112 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001113 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001114
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001115 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001116 my $indent;
1117 my $previndent=0;
1118 my $stashindent=0;
1119
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001120 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001121 my $signoff = 0;
1122 my $is_patch = 0;
1123
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001124 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001125 our $cnt_lines = 0;
1126 our $cnt_error = 0;
1127 our $cnt_warn = 0;
1128 our $cnt_chk = 0;
1129
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001130 # Trace the real file/line as we go.
1131 my $realfile = '';
1132 my $realline = 0;
1133 my $realcnt = 0;
1134 my $here = '';
1135 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001136 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001137 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001138 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001139
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001140 my $prev_values = 'E';
1141
1142 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001143 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001144 my %suppress_whiletrailers;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001145
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001146 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001147 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001148 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001149 my @setup_docs = ();
1150 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001151
1152 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001153 my $line;
1154 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001155 $linenr++;
1156 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001157
Andy Whitcroft773647a2008-03-28 14:15:58 -07001158 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001159 $setup_docs = 0;
1160 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1161 $setup_docs = 1;
1162 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001163 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001164 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001165 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1166 $realline=$1-1;
1167 if (defined $2) {
1168 $realcnt=$3+1;
1169 } else {
1170 $realcnt=1+1;
1171 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001172 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001173
1174 # Guestimate if this is a continuing comment. Run
1175 # the context looking for a comment "edge". If this
1176 # edge is a close comment then we must be in a comment
1177 # at context start.
1178 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001179 my $cnt = $realcnt;
1180 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1181 next if (defined $rawlines[$ln - 1] &&
1182 $rawlines[$ln - 1] =~ /^-/);
1183 $cnt--;
1184 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001185 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001186 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1187 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1188 ($edge) = $1;
1189 last;
1190 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001191 }
1192 if (defined $edge && $edge eq '*/') {
1193 $in_comment = 1;
1194 }
1195
1196 # Guestimate if this is a continuing comment. If this
1197 # is the start of a diff block and this line starts
1198 # ' *' then it is very likely a comment.
1199 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001200 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001201 {
1202 $in_comment = 1;
1203 }
1204
1205 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1206 sanitise_line_reset($in_comment);
1207
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001208 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001209 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001210 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001211 $line = sanitise_line($rawline);
1212 }
1213 push(@lines, $line);
1214
1215 if ($realcnt > 1) {
1216 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1217 } else {
1218 $realcnt = 0;
1219 }
1220
1221 #print "==>$rawline\n";
1222 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001223
1224 if ($setup_docs && $line =~ /^\+/) {
1225 push(@setup_docs, $line);
1226 }
1227 }
1228
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001229 $prefix = '';
1230
Andy Whitcroft773647a2008-03-28 14:15:58 -07001231 $realcnt = 0;
1232 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001233 foreach my $line (@lines) {
1234 $linenr++;
1235
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001236 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft306708542008-10-15 22:02:28 -07001237 my $hunk_line = ($realcnt != 0);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001238
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001239#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001240 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001241 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001242 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001243 $realline=$1-1;
1244 if (defined $2) {
1245 $realcnt=$3+1;
1246 } else {
1247 $realcnt=1+1;
1248 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001249 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001250 $prev_values = 'E';
1251
Andy Whitcroft773647a2008-03-28 14:15:58 -07001252 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001253 %suppress_whiletrailers = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001254 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001255
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001256# track the line number as we move through the hunk, note that
1257# new versions of GNU diff omit the leading space on completely
1258# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001259 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001260 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001261 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001262
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001263 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001264 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001265
1266 # Track the previous line.
1267 ($prevline, $stashline) = ($stashline, $line);
1268 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001269 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1270
Andy Whitcroft773647a2008-03-28 14:15:58 -07001271 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001272
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001273 } elsif ($realcnt == 1) {
1274 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001275 }
1276
1277#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001278 $prefix = "$filename:$realline: " if ($emacs && $file);
1279 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1280
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001281 $here = "#$linenr: " if (!$file);
1282 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001283
1284 # extract the filename as it passes
1285 if ($line=~/^\+\+\+\s+(\S+)/) {
1286 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001287 $realfile =~ s@^([^/]*)/@@;
1288
1289 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001290 if (!$file && $tree && $p1_prefix ne '' &&
1291 -e "$root/$p1_prefix") {
Wolfram Sang1e855722009-01-06 14:41:24 -08001292 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1293 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001294
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001295 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001296 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1297 }
1298 next;
1299 }
1300
Randy Dunlap389834b2007-06-08 13:47:03 -07001301 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001302
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001303 my $hereline = "$here\n$rawline\n";
1304 my $herecurr = "$here\n$rawline\n";
1305 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001306
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001307 $cnt_lines++ if ($realcnt != 0);
1308
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001309#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001310 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001311 # This is a signoff, if ugly, so do not double report.
1312 $signoff++;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001313 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001314 WARN("Signed-off-by: is the preferred form\n" .
1315 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001316 }
1317 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001318 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001319 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001320 }
1321 }
1322
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001323# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001324 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001325 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001326 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001327 }
1328
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001329# Check for absolute kernel paths.
1330 if ($tree) {
1331 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1332 my $file = $1;
1333
1334 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1335 check_absolute_file($1, $herecurr)) {
1336 #
1337 } else {
1338 check_absolute_file($file, $herecurr);
1339 }
1340 }
1341 }
1342
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001343# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1344 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001345 $rawline !~ m/^$UTF8*$/) {
1346 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1347
1348 my $blank = copy_spacing($rawline);
1349 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1350 my $hereptr = "$hereline$ptr\n";
1351
1352 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001353 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001354
Andy Whitcroft306708542008-10-15 22:02:28 -07001355# ignore non-hunk lines and lines being removed
1356 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001357
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001358#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001359 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001360 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001361 ERROR("DOS line endings\n" . $herevet);
1362
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001363 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1364 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001365 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001366 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001367
1368# check we are in a valid source file if not then ignore this hunk
1369 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1370
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001371#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001372 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001373 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1374 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1375 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001376 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001377 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001378 }
1379
Andy Whitcroft8905a672007-11-28 16:21:06 -08001380# check for adding lines without a newline.
1381 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1382 WARN("adding a line without newline at end of file\n" . $herecurr);
1383 }
1384
Mike Frysinger42e41c52009-09-21 17:04:40 -07001385# Blackfin: use hi/lo macros
1386 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1387 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1388 my $herevet = "$here\n" . cat_vet($line) . "\n";
1389 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1390 }
1391 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1392 my $herevet = "$here\n" . cat_vet($line) . "\n";
1393 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1394 }
1395 }
1396
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001397# check we are in a valid source file C or perl if not then ignore this hunk
1398 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001399
1400# at the beginning of a line any tabs must come first and anything
1401# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001402 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1403 $rawline =~ /^\+\s* \s*/) {
1404 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001405 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001406 }
1407
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001408# check we are in a valid C source file if not then ignore this hunk
1409 next if ($realfile !~ /\.(h|c)$/);
1410
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001411# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001412 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001413 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1414 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001415
Mike Frysinger42e41c52009-09-21 17:04:40 -07001416# Blackfin: don't use __builtin_bfin_[cs]sync
1417 if ($line =~ /__builtin_bfin_csync/) {
1418 my $herevet = "$here\n" . cat_vet($line) . "\n";
1419 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1420 }
1421 if ($line =~ /__builtin_bfin_ssync/) {
1422 my $herevet = "$here\n" . cat_vet($line) . "\n";
1423 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1424 }
1425
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001426# Check for potential 'bare' types
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001427 my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001428 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001429 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001430 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001431 $stat =~ s/\n./\n /g;
1432 $cond =~ s/\n./\n /g;
1433
1434 my $s = $stat;
1435 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001437 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001438 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001439
1440 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001441 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001442
Andy Whitcroft463f2862009-09-21 17:04:34 -07001443 } elsif ($s =~ /^.\s*else\b/s) {
1444
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001445 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001446 } 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 -07001447 my $type = $1;
1448 $type =~ s/\s+/ /g;
1449 possible($type, "A:" . $s);
1450
Andy Whitcroft8905a672007-11-28 16:21:06 -08001451 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07001452 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001453 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001454 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001455
1456 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001457 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001458 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001459 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001460
1461 # Check for any sort of function declaration.
1462 # int foo(something bar, other baz);
1463 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001464 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 -08001465 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001466
Andy Whitcroftcf655042008-03-04 14:28:20 -08001467 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001468 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001469 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001470
Andy Whitcroft8905a672007-11-28 16:21:06 -08001471 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001472 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001473
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001474 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001475 }
1476 }
1477 }
1478
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001479 }
1480
Andy Whitcroft653d4872007-06-23 17:16:34 -07001481#
1482# Checks which may be anchored in the context.
1483#
1484
1485# Check for switch () and associated case and default
1486# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001487 if ($line=~/\bswitch\s*\(.*\)/) {
1488 my $err = '';
1489 my $sep = '';
1490 my @ctx = ctx_block_outer($linenr, $realcnt);
1491 shift(@ctx);
1492 for my $ctx (@ctx) {
1493 my ($clen, $cindent) = line_stats($ctx);
1494 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1495 $indent != $cindent) {
1496 $err .= "$sep$ctx\n";
1497 $sep = '';
1498 } else {
1499 $sep = "[...]\n";
1500 }
1501 }
1502 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001503 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001504 }
1505 }
1506
1507# if/while/etc brace do not go on next line, unless defining a do while loop,
1508# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001509 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001510 my $pre_ctx = "$1$2";
1511
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001512 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001513 my $ctx_cnt = $realcnt - $#ctx - 1;
1514 my $ctx = join("\n", @ctx);
1515
Andy Whitcroft548596d2008-07-23 21:29:01 -07001516 my $ctx_ln = $linenr;
1517 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001518
Andy Whitcroft548596d2008-07-23 21:29:01 -07001519 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1520 defined $lines[$ctx_ln - 1] &&
1521 $lines[$ctx_ln - 1] =~ /^-/)) {
1522 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1523 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001524 $ctx_ln++;
1525 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001526
Andy Whitcroft53210162008-07-23 21:29:03 -07001527 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1528 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001529
1530 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1531 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001532 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001533 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001534 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1535 $ctx =~ /\)\s*\;\s*$/ &&
1536 defined $lines[$ctx_ln - 1])
1537 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001538 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1539 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001540 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001541 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001542 }
1543 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001544 }
1545
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001546# Check relative indent for conditionals and blocks.
1547 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1548 my ($s, $c) = ($stat, $cond);
1549
1550 substr($s, 0, length($c), '');
1551
1552 # Make sure we remove the line prefixes as we have
1553 # none on the first line, and are going to readd them
1554 # where necessary.
1555 $s =~ s/\n./\n/gs;
1556
1557 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001558 my @newlines = ($c =~ /\n/gs);
1559 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001560
1561 # We want to check the first line inside the block
1562 # starting at the end of the conditional, so remove:
1563 # 1) any blank line termination
1564 # 2) any opening brace { on end of the line
1565 # 3) any do (...) {
1566 my $continuation = 0;
1567 my $check = 0;
1568 $s =~ s/^.*\bdo\b//;
1569 $s =~ s/^\s*{//;
1570 if ($s =~ s/^\s*\\//) {
1571 $continuation = 1;
1572 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001573 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001574 $check = 1;
1575 $cond_lines++;
1576 }
1577
1578 # Also ignore a loop construct at the end of a
1579 # preprocessor statement.
1580 if (($prevline =~ /^.\s*#\s*define\s/ ||
1581 $prevline =~ /\\\s*$/) && $continuation == 0) {
1582 $check = 0;
1583 }
1584
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001585 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001586 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001587 while ($cond_ptr != $cond_lines) {
1588 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001589
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001590 # If we see an #else/#elif then the code
1591 # is not linear.
1592 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1593 $check = 0;
1594 }
1595
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001596 # Ignore:
1597 # 1) blank lines, they should be at 0,
1598 # 2) preprocessor lines, and
1599 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001600 if ($continuation ||
1601 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001602 $s =~ /^\s*#\s*?/ ||
1603 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001604 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001605 if ($s =~ s/^.*?\n//) {
1606 $cond_lines++;
1607 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001608 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001609 }
1610
1611 my (undef, $sindent) = line_stats("+" . $s);
1612 my $stat_real = raw_line($linenr, $cond_lines);
1613
1614 # Check if either of these lines are modified, else
1615 # this is not this patch's fault.
1616 if (!defined($stat_real) ||
1617 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1618 $check = 0;
1619 }
1620 if (defined($stat_real) && $cond_lines > 1) {
1621 $stat_real = "[...]\n$stat_real";
1622 }
1623
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001624 #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 -07001625
1626 if ($check && (($sindent % 8) != 0 ||
1627 ($sindent <= $indent && $s ne ''))) {
1628 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1629 }
1630 }
1631
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001632 # Track the 'values' across context and added lines.
1633 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001634 my ($curr_values, $curr_vars) =
1635 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001636 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001637 if ($dbg_values) {
1638 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001639 print "$linenr > .$outline\n";
1640 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001641 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001642 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001643 $prev_values = substr($curr_values, -1);
1644
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001645#ignore lines not being added
1646 if ($line=~/^[^\+]/) {next;}
1647
Andy Whitcroft653d4872007-06-23 17:16:34 -07001648# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001649 if ($dbg_type) {
1650 if ($line =~ /^.\s*$Declare\s*$/) {
1651 ERROR("TEST: is type\n" . $herecurr);
1652 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1653 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1654 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001655 next;
1656 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001657# TEST: allow direct testing of the attribute matcher.
1658 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001659 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001660 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001661 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001662 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1663 }
1664 next;
1665 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001666
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001667# check for initialisation to aggregates open brace on the next line
1668 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1669 $line =~ /^.\s*{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001670 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001671 }
1672
Andy Whitcroft653d4872007-06-23 17:16:34 -07001673#
1674# Checks which are anchored on the added line.
1675#
1676
1677# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001678 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001679 my $path = $1;
1680 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001681 ERROR("malformed #include filename\n" .
1682 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001683 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001684 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001685
1686# no C99 // comments
1687 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001688 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001689 }
1690 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001691 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001692 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001693
1694#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -07001695 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1696 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1697 my $name = $1;
Andy Whitcroft48012052008-10-15 22:02:34 -07001698 if ($prevline !~ /(?:
1699 ^.}|
1700 ^.DEFINE_$Ident\(\Q$name\E\)|
1701 ^.DECLARE_$Ident\(\Q$name\E\)|
1702 ^.LIST_HEAD\(\Q$name\E\)|
1703 ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1704 \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
1705 )/x) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001706 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001707 }
1708 }
1709
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001710# check for external initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001711 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001712 ERROR("do not initialise externals to 0 or NULL\n" .
1713 $herecurr);
1714 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001715# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001716 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001717 ERROR("do not initialise statics to 0 or NULL\n" .
1718 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001719 }
1720
Andy Whitcroft653d4872007-06-23 17:16:34 -07001721# check for new typedefs, only function parameters and sparse annotations
1722# make sense.
1723 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001724 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001725 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001726 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001727 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001728 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001729 }
1730
1731# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001732 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001733 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001734 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001735
Andy Whitcroft65863862009-01-06 14:41:21 -08001736 # Should start with a space.
1737 $to =~ s/^(\S)/ $1/;
1738 # Should not end with a space.
1739 $to =~ s/\s+$//;
1740 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001741 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001742 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001743
Andy Whitcroft65863862009-01-06 14:41:21 -08001744 #print "from<$from> to<$to>\n";
1745 if ($from ne $to) {
1746 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1747 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001748 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001749 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001750
Andy Whitcroft65863862009-01-06 14:41:21 -08001751 # Should start with a space.
1752 $to =~ s/^(\S)/ $1/;
1753 # Should not end with a space.
1754 $to =~ s/\s+$//;
1755 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001756 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001757 }
1758 # Modifiers should have spaces.
1759 $to =~ s/(\b$Modifier$)/$1 /;
1760
Andy Whitcroft667026e2009-02-27 14:03:08 -08001761 #print "from<$from> to<$to> ident<$ident>\n";
1762 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001763 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1764 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001765 }
1766
1767# # no BUG() or BUG_ON()
1768# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1769# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1770# print "$herecurr";
1771# $clean = 0;
1772# }
1773
Andy Whitcroft8905a672007-11-28 16:21:06 -08001774 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001775 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 -08001776 }
1777
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001778# printk should use KERN_* levels. Note that follow on printk's on the
1779# same line do not need a level, so we use the current block context
1780# to try and find and validate the current printk. In summary the current
1781# printk includes all preceeding printk's which have no newline on the end.
1782# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001783 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001784 my $ok = 0;
1785 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1786 #print "CHECK<$lines[$ln - 1]\n";
1787 # we have a preceeding printk if it ends
1788 # with "\n" ignore it, else it is to blame
1789 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1790 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1791 $ok = 1;
1792 }
1793 last;
1794 }
1795 }
1796 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001797 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001798 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001799 }
1800
Andy Whitcroft653d4872007-06-23 17:16:34 -07001801# function brace can't be on same line, except for #defines of do while,
1802# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001803 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1804 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001805 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001806 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001807
Andy Whitcroft8905a672007-11-28 16:21:06 -08001808# open braces for enum, union and struct go on the same line.
1809 if ($line =~ /^.\s*{/ &&
1810 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1811 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1812 }
1813
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001814# check for spacing round square brackets; allowed:
1815# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001816# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1817# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001818 while ($line =~ /(.*?\s)\[/g) {
1819 my ($where, $prefix) = ($-[1], $1);
1820 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001821 ($where != 0 || $prefix !~ /^.\s+$/) &&
1822 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001823 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1824 }
1825 }
1826
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001827# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001828 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001829 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001830 my $ctx_before = substr($line, 0, $-[1]);
1831 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001832
1833 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001834 if ($name =~ /^(?:
1835 if|for|while|switch|return|case|
1836 volatile|__volatile__|
1837 __attribute__|format|__extension__|
1838 asm|__asm__)$/x)
1839 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001840
1841 # cpp #define statements have non-optional spaces, ie
1842 # if there is a space between the name and the open
1843 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001844 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001845
1846 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001847 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001848
1849 # If this whole things ends with a type its most
1850 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001851 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001852
1853 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001854 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001855 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001856 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001857# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001858 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001859 my $ops = qr{
1860 <<=|>>=|<=|>=|==|!=|
1861 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1862 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001863 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1864 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001865 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001866 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001867 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001868
1869 my $blank = copy_spacing($opline);
1870
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001871 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001872 $off += length($elements[$n]);
1873
Andy Whitcroft773647a2008-03-28 14:15:58 -07001874 # Pick up the preceeding and succeeding characters.
1875 my $ca = substr($opline, 0, $off);
1876 my $cc = '';
1877 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1878 $cc = substr($opline, $off + length($elements[$n + 1]));
1879 }
1880 my $cb = "$ca$;$cc";
1881
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001882 my $a = '';
1883 $a = 'V' if ($elements[$n] ne '');
1884 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001885 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001886 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1887 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07001888 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001889
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001890 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001891
1892 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001893 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001894 $c = 'V' if ($elements[$n + 2] ne '');
1895 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001896 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001897 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1898 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08001899 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001900 } else {
1901 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001902 }
1903
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001904 my $ctx = "${a}x${c}";
1905
1906 my $at = "(ctx:$ctx)";
1907
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001908 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001909 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001910
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001911 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001912 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001913
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001914 # Get the full operator variant.
1915 my $opv = $op . substr($curr_vars, $off, 1);
1916
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001917 # Ignore operators passed as parameters.
1918 if ($op_type ne 'V' &&
1919 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1920
Andy Whitcroftcf655042008-03-04 14:28:20 -08001921# # Ignore comments
1922# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001923
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001924 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001925 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001926 if ($ctx !~ /.x[WEBC]/ &&
1927 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001928 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001929 }
1930
1931 # // is a comment
1932 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001933
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001934 # No spaces for:
1935 # ->
1936 # : when part of a bitfield
1937 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001938 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001939 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001940 }
1941
1942 # , must have a space on the right.
1943 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001944 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001945 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001946 }
1947
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001948 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001949 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001950 #warn "'*' is part of type\n";
1951
1952 # unary operators should have a space before and
1953 # none after. May be left adjacent to another
1954 # unary operator, or a cast
1955 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001956 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07001957 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001958 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001959 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001960 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08001961 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001962 # A unary '*' may be const
1963
1964 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07001965 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001966 }
1967
1968 # unary ++ and unary -- are allowed no space on one side.
1969 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001970 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1971 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001972 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001973 if ($ctx =~ /Wx[BE]/ ||
1974 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1975 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001976 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001977 if ($ctx =~ /ExW/) {
1978 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1979 }
1980
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001981
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001982 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001983 } elsif ($op eq '<<' or $op eq '>>' or
1984 $op eq '&' or $op eq '^' or $op eq '|' or
1985 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001986 $op eq '*' or $op eq '/' or
1987 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001988 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001989 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001990 ERROR("need consistent spacing around '$op' $at\n" .
1991 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001992 }
1993
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001994 # A colon needs no spaces before when it is
1995 # terminating a case value or a label.
1996 } elsif ($opv eq ':C' || $opv eq ':L') {
1997 if ($ctx =~ /Wx./) {
1998 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1999 }
2000
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002001 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002002 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002003 my $ok = 0;
2004
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002005 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002006 if (($op eq '<' &&
2007 $cc =~ /^\S+\@\S+>/) ||
2008 ($op eq '>' &&
2009 $ca =~ /<\S+\@\S+$/))
2010 {
2011 $ok = 1;
2012 }
2013
2014 # Ignore ?:
2015 if (($opv eq ':O' && $ca =~ /\?$/) ||
2016 ($op eq '?' && $cc =~ /^:/)) {
2017 $ok = 1;
2018 }
2019
2020 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002021 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002022 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002023 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002024 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002025 }
2026 }
2027
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002028# check for multiple assignments
2029 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002030 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002031 }
2032
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002033## # check for multiple declarations, allowing for a function declaration
2034## # continuation.
2035## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2036## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2037##
2038## # Remove any bracketed sections to ensure we do not
2039## # falsly report the parameters of functions.
2040## my $ln = $line;
2041## while ($ln =~ s/\([^\(\)]*\)//g) {
2042## }
2043## if ($ln =~ /,/) {
2044## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2045## }
2046## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002047
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002048#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002049 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2050 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002051 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002052 }
2053
2054# closing brace should have a space following it when it has anything
2055# on the line
2056 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002057 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002058 }
2059
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002060# check spacing on square brackets
2061 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002062 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002063 }
2064 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002065 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002066 }
2067
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002068# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002069 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2070 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002071 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002072 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002073 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002074 $line !~ /for\s*\(.*;\s+\)/ &&
2075 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002076 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002077 }
2078
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002079#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002080 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002081 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002082 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002083 }
2084
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002085# Return is not a function.
2086 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2087 my $spacing = $1;
2088 my $value = $2;
2089
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002090 # Flatten any parentheses
Andy Whitcroftfee61c42008-07-23 21:28:56 -07002091 $value =~ s/\)\(/\) \(/g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002092 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2093 $value !~ /(?:$Ident|-?$Constant)\s*
2094 $Compare\s*
2095 (?:$Ident|-?$Constant)/x &&
2096 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002097 }
2098
2099 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2100 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2101
2102 } elsif ($spacing !~ /\s+/) {
2103 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2104 }
2105 }
2106
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002107# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002108 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002109 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002110 }
2111
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002112# Check for illegal assignment in if conditional -- and check for trailing
2113# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002114 if ($line =~ /do\s*(?!{)/) {
2115 my ($stat_next) = ctx_statement_block($line_nr_next,
2116 $remain_next, $off_next);
2117 $stat_next =~ s/\n./\n /g;
2118 ##print "stat<$stat> stat_next<$stat_next>\n";
2119
2120 if ($stat_next =~ /^\s*while\b/) {
2121 # If the statement carries leading newlines,
2122 # then count those as offsets.
2123 my ($whitespace) =
2124 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2125 my $offset =
2126 statement_rawlines($whitespace) - 1;
2127
2128 $suppress_whiletrailers{$line_nr_next +
2129 $offset} = 1;
2130 }
2131 }
2132 if (!defined $suppress_whiletrailers{$linenr} &&
2133 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002134 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002135
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002136 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002137 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002138 }
2139
2140 # Find out what is on the end of the line after the
2141 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002142 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002143 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002144 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002145 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2146 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002147 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002148 # Find out how long the conditional actually is.
2149 my @newlines = ($c =~ /\n/gs);
2150 my $cond_lines = 1 + $#newlines;
2151
2152 my $stat_real = raw_line($linenr, $cond_lines);
2153 if (defined($stat_real) && $cond_lines > 1) {
2154 $stat_real = "[...]\n$stat_real";
2155 }
2156
2157 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002158 }
2159 }
2160
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002161# Check for bitwise tests written as boolean
2162 if ($line =~ /
2163 (?:
2164 (?:\[|\(|\&\&|\|\|)
2165 \s*0[xX][0-9]+\s*
2166 (?:\&\&|\|\|)
2167 |
2168 (?:\&\&|\|\|)
2169 \s*0[xX][0-9]+\s*
2170 (?:\&\&|\|\||\)|\])
2171 )/x)
2172 {
2173 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2174 }
2175
Andy Whitcroft8905a672007-11-28 16:21:06 -08002176# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002177 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2178 my $s = $1;
2179 $s =~ s/$;//g; # Remove any comments
2180 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2181 ERROR("trailing statements should be on next line\n" . $herecurr);
2182 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002183 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002184# if should not continue a brace
2185 if ($line =~ /}\s*if\b/) {
2186 ERROR("trailing statements should be on next line\n" .
2187 $herecurr);
2188 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002189# case and default should not have general statements after them
2190 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2191 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002192 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002193 \s*return\s+
2194 )/xg)
2195 {
2196 ERROR("trailing statements should be on next line\n" . $herecurr);
2197 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002198
2199 # Check for }<nl>else {, these must be at the same
2200 # indent level to be relevant to each other.
2201 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2202 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002203 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002204 }
2205
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002206 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2207 $previndent == $indent) {
2208 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2209
2210 # Find out what is on the end of the line after the
2211 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002212 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002213 $s =~ s/\n.*//g;
2214
2215 if ($s =~ /^\s*;/) {
2216 ERROR("while should follow close brace '}'\n" . $hereprev);
2217 }
2218 }
2219
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002220#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2221# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2222# print "No studly caps, use _\n";
2223# print "$herecurr";
2224# $clean = 0;
2225# }
2226
2227#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002228 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002229 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002230 }
2231
Andy Whitcroft653d4872007-06-23 17:16:34 -07002232#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002233 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002234 my $file = "$1.h";
2235 my $checkfile = "include/linux/$file";
2236 if (-f "$root/$checkfile" &&
2237 $realfile ne $checkfile &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002238 $1 ne 'irq')
2239 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002240 if ($realfile =~ m{^arch/}) {
2241 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2242 } else {
2243 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2244 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002245 }
2246 }
2247
Andy Whitcroft653d4872007-06-23 17:16:34 -07002248# multi-statement macros should be enclosed in a do while loop, grab the
2249# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002250# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07002251 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2252 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002253 my $ln = $linenr;
2254 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002255 my ($off, $dstat, $dcond, $rest);
2256 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002257
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002258 my $args = defined($1);
2259
2260 # Find the end of the macro and limit our statement
2261 # search to that.
2262 while ($cnt > 0 && defined $lines[$ln - 1] &&
2263 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2264 {
2265 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002266 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002267 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002268 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002269 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002270
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002271 ($dstat, $dcond, $ln, $cnt, $off) =
2272 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2273 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002274 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002275
2276 # Extract the remainder of the define (if any) and
2277 # rip off surrounding spaces, and trailing \'s.
2278 $rest = '';
Andy Whitcroft636d140a2008-10-15 22:02:18 -07002279 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2280 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002281 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2282 $rest .= substr($lines[$ln - 1], $off) . "\n";
2283 $cnt--;
2284 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002285 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002286 $off = 0;
2287 }
2288 $rest =~ s/\\\n.//g;
2289 $rest =~ s/^\s*//s;
2290 $rest =~ s/\s*$//s;
2291
2292 # Clean up the original statement.
2293 if ($args) {
2294 substr($dstat, 0, length($dcond), '');
2295 } else {
2296 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2297 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002298 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002299 $dstat =~ s/\\\n.//g;
2300 $dstat =~ s/^\s*//s;
2301 $dstat =~ s/\s*$//s;
2302
2303 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002304 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2305 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2306 $dstat =~ s/\[[^\{\}]*\]/1/)
2307 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002308 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002309
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002310 my $exceptions = qr{
2311 $Declare|
2312 module_param_named|
2313 MODULE_PARAM_DESC|
2314 DECLARE_PER_CPU|
2315 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002316 __typeof__\(|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002317 \.$Ident\s*=\s*|
2318 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002319 }x;
Andy Whitcroft383099f2009-01-06 14:41:18 -08002320 #print "REST<$rest> dstat<$dstat>\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002321 if ($rest ne '') {
2322 if ($rest !~ /while\s*\(/ &&
2323 $dstat !~ /$exceptions/)
2324 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002325 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 -07002326 }
2327
2328 } elsif ($ctx !~ /;/) {
2329 if ($dstat ne '' &&
2330 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2331 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002332 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002333 $dstat =~ /$Operators/)
2334 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002335 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002336 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002337 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002338 }
2339
Mike Frysinger080ba922009-01-06 14:41:25 -08002340# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2341# all assignments may have only one of the following with an assignment:
2342# .
2343# ALIGN(...)
2344# VMLINUX_SYMBOL(...)
2345 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2346 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2347 }
2348
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002349# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002350 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2351 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002352 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002353 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002354 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2355 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002356 my $allowed = 0;
2357 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002358 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002359 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002360 for my $chunk (@chunks) {
2361 my ($cond, $block) = @{$chunk};
2362
Andy Whitcroft773647a2008-03-28 14:15:58 -07002363 # If the condition carries leading newlines, then count those as offsets.
2364 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2365 my $offset = statement_rawlines($whitespace) - 1;
2366
2367 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2368
2369 # We have looked at and allowed this specific line.
2370 $suppress_ifbraces{$ln + $offset} = 1;
2371
2372 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002373 $ln += statement_rawlines($block) - 1;
2374
Andy Whitcroft773647a2008-03-28 14:15:58 -07002375 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002376
2377 $seen++ if ($block =~ /^\s*{/);
2378
Andy Whitcroftcf655042008-03-04 14:28:20 -08002379 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2380 if (statement_lines($cond) > 1) {
2381 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002382 $allowed = 1;
2383 }
2384 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002385 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002386 $allowed = 1;
2387 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002388 if (statement_block_size($block) > 1) {
2389 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002390 $allowed = 1;
2391 }
2392 }
2393 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002394 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002395 }
2396 }
2397 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002398 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002399 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002400 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002401
Andy Whitcroftcf655042008-03-04 14:28:20 -08002402 # Check the pre-context.
2403 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2404 #print "APW: ALLOWED: pre<$1>\n";
2405 $allowed = 1;
2406 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002407
2408 my ($level, $endln, @chunks) =
2409 ctx_statement_full($linenr, $realcnt, $-[0]);
2410
Andy Whitcroftcf655042008-03-04 14:28:20 -08002411 # Check the condition.
2412 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002413 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002414 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002415 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002416 }
2417 if (statement_lines($cond) > 1) {
2418 #print "APW: ALLOWED: cond<$cond>\n";
2419 $allowed = 1;
2420 }
2421 if ($block =~/\b(?:if|for|while)\b/) {
2422 #print "APW: ALLOWED: block<$block>\n";
2423 $allowed = 1;
2424 }
2425 if (statement_block_size($block) > 1) {
2426 #print "APW: ALLOWED: lines block<$block>\n";
2427 $allowed = 1;
2428 }
2429 # Check the post-context.
2430 if (defined $chunks[1]) {
2431 my ($cond, $block) = @{$chunks[1]};
2432 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002433 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002434 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002435 if ($block =~ /^\s*\{/) {
2436 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2437 $allowed = 1;
2438 }
2439 }
2440 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2441 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002442 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002443
Andy Whitcroftf0556632008-10-15 22:02:23 -07002444 for (my $n = 0; $n < $cnt; $n++) {
2445 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002446 }
2447
2448 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002449 }
2450 }
2451
Andy Whitcroft653d4872007-06-23 17:16:34 -07002452# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002453 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002454 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002455 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002456 }
2457 }
2458
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002459# don't use deprecated functions
2460 for my $func (@dep_functions) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002461 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002462 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002463 }
2464 }
2465
2466# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002467 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2468 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002469 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002470 }
2471
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002472# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2473 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2474 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2475 }
2476
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002477# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002478 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002479 CHK("if this code is redundant consider removing it\n" .
2480 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002481 }
2482
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002483# check for needless kfree() checks
2484 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2485 my $expr = $1;
2486 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002487 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002488 }
2489 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002490# check for needless usb_free_urb() checks
2491 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2492 my $expr = $1;
2493 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2494 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2495 }
2496 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002497
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002498# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002499# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002500# print "#ifdef in C files should be avoided\n";
2501# print "$herecurr";
2502# $clean = 0;
2503# }
2504
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002505# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002506 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002507 ERROR("exactly one space required after that #$1\n" . $herecurr);
2508 }
2509
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002510# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002511 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2512 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002513 my $which = $1;
2514 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002515 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002516 }
2517 }
2518# check for memory barriers without a comment.
2519 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2520 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002521 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002522 }
2523 }
2524# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002525 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002526 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002527 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002528
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002529# check the location of the inline attribute, that it is between
2530# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002531 if ($line =~ /\b$Type\s+$Inline\b/ ||
2532 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002533 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2534 }
2535
Andy Whitcroft8905a672007-11-28 16:21:06 -08002536# Check for __inline__ and __inline, prefer inline
2537 if ($line =~ /\b(__inline__|__inline)\b/) {
2538 WARN("plain inline is preferred over $1\n" . $herecurr);
2539 }
2540
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002541# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002542 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002543 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002544 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002545 my $function_name = $1;
2546 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002547
2548 my $s = $stat;
2549 if (defined $cond) {
2550 substr($s, 0, length($cond), '');
2551 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002552 if ($s =~ /^\s*;/ &&
2553 $function_name ne 'uninitialized_var')
2554 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002555 WARN("externs should be avoided in .c files\n" . $herecurr);
2556 }
2557
2558 if ($paren_space =~ /\n/) {
2559 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2560 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002561
2562 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2563 $stat =~ /^.\s*extern\s+/)
2564 {
2565 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002566 }
2567
2568# checks for new __setup's
2569 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2570 my $name = $1;
2571
2572 if (!grep(/$name/, @setup_docs)) {
2573 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2574 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002575 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002576
2577# check for pointless casting of kmalloc return
2578 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2579 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2580 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002581
2582# check for gcc specific __FUNCTION__
2583 if ($line =~ /__FUNCTION__/) {
2584 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2585 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002586
2587# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002588 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002589 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2590 }
2591# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002592 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002593 WARN("consider using a completion\n" . $herecurr);
2594 }
2595# recommend strict_strto* over simple_strto*
2596 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2597 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2598 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002599# check for __initcall(), use device_initcall() explicitly please
2600 if ($line =~ /^.\s*__initcall\s*\(/) {
2601 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2602 }
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08002603# check for struct file_operations, ensure they are const.
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08002604 if ($line !~ /\bconst\b/ &&
2605 $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) {
2606 WARN("struct $1 should normally be const\n" .
2607 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08002608 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002609
2610# use of NR_CPUS is usually wrong
2611# ignore definitions of NR_CPUS and usage to define arrays as likely right
2612 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002613 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2614 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002615 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2616 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2617 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002618 {
2619 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2620 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002621
2622# check for %L{u,d,i} in strings
2623 my $string;
2624 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2625 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07002626 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002627 if ($string =~ /(?<!%)%L[udi]/) {
2628 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2629 last;
2630 }
2631 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002632
2633# whine mightly about in_atomic
2634 if ($line =~ /\bin_atomic\s*\(/) {
2635 if ($realfile =~ m@^drivers/@) {
2636 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08002637 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002638 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2639 }
2640 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002641 }
2642
2643 # If we have no input at all, then there is nothing to report on
2644 # so just keep quiet.
2645 if ($#rawlines == -1) {
2646 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002647 }
2648
Andy Whitcroft8905a672007-11-28 16:21:06 -08002649 # In mailback mode only produce a report in the negative, for
2650 # things that appear to be patches.
2651 if ($mailback && ($clean == 1 || !$is_patch)) {
2652 exit(0);
2653 }
2654
2655 # This is not a patch, and we are are in 'no-patch' mode so
2656 # just keep quiet.
2657 if (!$chk_patch && !$is_patch) {
2658 exit(0);
2659 }
2660
2661 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002662 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002663 }
2664 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002665 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002666 }
2667
Andy Whitcroft8905a672007-11-28 16:21:06 -08002668 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002669 if ($summary && !($clean == 1 && $quiet == 1)) {
2670 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002671 print "total: $cnt_error errors, $cnt_warn warnings, " .
2672 (($check)? "$cnt_chk checks, " : "") .
2673 "$cnt_lines lines checked\n";
2674 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002675 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002676
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002677 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002678 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002679 }
2680 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002681 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002682 print "are false positives report them to the maintainer, see\n";
2683 print "CHECKPATCH in MAINTAINERS.\n";
2684 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002685
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002686 return $clean;
2687}