blob: 5ba62d8b5e521a7e2988db6a0cf3d188ba8af613 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (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)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 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 Whitcroft267ad8f2010-10-26 14:23:19 -070013my $V = '0.31';
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 Whitcroftd2c0a232010-10-26 14:23:12 -0700106my $rpt_cleaners = 0;
107
Andy Whitcroft8905a672007-11-28 16:21:06 -0800108if ($terse) {
109 $emacs = 1;
110 $quiet++;
111}
112
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113if ($tree) {
114 if (defined $root) {
115 if (!top_of_kernel_tree($root)) {
116 die "$P: $root: --root does not point at a valid tree\n";
117 }
118 } else {
119 if (top_of_kernel_tree('.')) {
120 $root = '.';
121 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
122 top_of_kernel_tree($1)) {
123 $root = $1;
124 }
125 }
126
127 if (!defined $root) {
128 print "Must be run from the top-level dir. of a kernel tree\n";
129 exit(2);
130 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700131}
132
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700133my $emitted_corrupt = 0;
134
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700135our $Ident = qr{
136 [A-Za-z_][A-Za-z\d_]*
137 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
138 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700139our $Storage = qr{extern|static|asmlinkage};
140our $Sparse = qr{
141 __user|
142 __kernel|
143 __force|
144 __iomem|
145 __must_check|
146 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800147 __kprobes|
148 __ref
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700149 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800150
151# Notes to $Attribute:
152# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700153our $Attribute = qr{
154 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700155 __percpu|
156 __nocast|
157 __safe|
158 __bitwise__|
159 __packed__|
160 __packed2__|
161 __naked|
162 __maybe_unused|
163 __always_unused|
164 __noreturn|
165 __used|
166 __cold|
167 __noclone|
168 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700169 __read_mostly|
170 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800171 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700172 ____cacheline_aligned|
173 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800174 ____cacheline_internodealigned_in_smp|
175 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700176 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700177our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700178our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700179our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
180our $Lval = qr{$Ident(?:$Member)*};
181
182our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
183our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800184our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700185our $Operators = qr{
186 <=|>=|==|!=|
187 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800188 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700189 }x;
190
Andy Whitcroft8905a672007-11-28 16:21:06 -0800191our $NonptrType;
192our $Type;
193our $Declare;
194
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700195our $UTF8 = qr {
196 [\x09\x0A\x0D\x20-\x7E] # ASCII
197 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
198 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
199 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
200 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
201 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
202 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
203 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
204}x;
205
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700206our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700207 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700208 atomic_t
209)};
210
Joe Perches691e6692010-03-05 13:43:51 -0800211our $logFunctions = qr{(?x:
212 printk|
Joe Perchesb0531722011-05-24 17:13:40 -0700213 [a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
Joe Perches691e6692010-03-05 13:43:51 -0800214 WARN|
Joe Perchesb0531722011-05-24 17:13:40 -0700215 panic|
216 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800217)};
218
Andy Whitcroft8905a672007-11-28 16:21:06 -0800219our @typeList = (
220 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700221 qr{(?:unsigned\s+)?char},
222 qr{(?:unsigned\s+)?short},
223 qr{(?:unsigned\s+)?int},
224 qr{(?:unsigned\s+)?long},
225 qr{(?:unsigned\s+)?long\s+int},
226 qr{(?:unsigned\s+)?long\s+long},
227 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800228 qr{unsigned},
229 qr{float},
230 qr{double},
231 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800232 qr{struct\s+$Ident},
233 qr{union\s+$Ident},
234 qr{enum\s+$Ident},
235 qr{${Ident}_t},
236 qr{${Ident}_handler},
237 qr{${Ident}_handler_fn},
238);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700239our @modifierList = (
240 qr{fastcall},
241);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800242
Wolfram Sang7840a942010-08-09 17:20:57 -0700243our $allowed_asm_includes = qr{(?x:
244 irq|
245 memory
246)};
247# memory.h: ARM has a custom one
248
Andy Whitcroft8905a672007-11-28 16:21:06 -0800249sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700250 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
251 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700252 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800253 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700254 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800255 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700256 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700257 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700258 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800259 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700260 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800261 }x;
262 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700263 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800264 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700265 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800266 }x;
267 $Declare = qr{(?:$Storage\s+)?$Type};
268}
269build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700270
Joe Perches7d2367a2011-07-25 17:13:22 -0700271our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
272
273our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
274our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
275
276sub deparenthesize {
277 my ($string) = @_;
278 return "" if (!defined($string));
279 $string =~ s@^\s*\(\s*@@g;
280 $string =~ s@\s*\)\s*$@@g;
281 $string =~ s@\s+@ @g;
282 return $string;
283}
284
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700285$chk_signoff = 0 if ($file);
286
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700287my @dep_includes = ();
288my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700289my $removal = "Documentation/feature-removal-schedule.txt";
290if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800291 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700292 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800293 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700294 if (/^Check:\s+(.*\S)/) {
295 for my $entry (split(/[, ]+/, $1)) {
296 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700297 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700298
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700299 } elsif ($entry !~ m@/@) {
300 push(@dep_functions, $entry);
301 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700302 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700303 }
304 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800305 close($REMOVE);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700306}
307
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700308my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800309my @lines = ();
310my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700311for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800312 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700313 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800314 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700315 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800316 } elsif ($filename eq '-') {
317 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700318 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800319 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700320 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700321 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800322 if ($filename eq '-') {
323 $vname = 'Your patch';
324 } else {
325 $vname = $filename;
326 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800327 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700328 chomp;
329 push(@rawlines, $_);
330 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800331 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800332 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700333 $exit = 1;
334 }
335 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800336 @lines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700337}
338
339exit($exit);
340
341sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700342 my ($root) = @_;
343
344 my @tree_check = (
345 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
346 "README", "Documentation", "arch", "include", "drivers",
347 "fs", "init", "ipc", "kernel", "lib", "scripts",
348 );
349
350 foreach my $check (@tree_check) {
351 if (! -e $root . '/' . $check) {
352 return 0;
353 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700354 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700355 return 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700356}
357
358sub expand_tabs {
359 my ($str) = @_;
360
361 my $res = '';
362 my $n = 0;
363 for my $c (split(//, $str)) {
364 if ($c eq "\t") {
365 $res .= ' ';
366 $n++;
367 for (; ($n % 8) != 0; $n++) {
368 $res .= ' ';
369 }
370 next;
371 }
372 $res .= $c;
373 $n++;
374 }
375
376 return $res;
377}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700378sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700379 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700380 return $res;
381}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700382
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700383sub line_stats {
384 my ($line) = @_;
385
386 # Drop the diff line leader and expand tabs
387 $line =~ s/^.//;
388 $line = expand_tabs($line);
389
390 # Pick the indent from the front of the line.
391 my ($white) = ($line =~ /^(\s*)/);
392
393 return (length($line), length($white));
394}
395
Andy Whitcroft773647a2008-03-28 14:15:58 -0700396my $sanitise_quote = '';
397
398sub sanitise_line_reset {
399 my ($in_comment) = @_;
400
401 if ($in_comment) {
402 $sanitise_quote = '*/';
403 } else {
404 $sanitise_quote = '';
405 }
406}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700407sub sanitise_line {
408 my ($line) = @_;
409
410 my $res = '';
411 my $l = '';
412
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800413 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700414 my $off = 0;
415 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700416
Andy Whitcroft773647a2008-03-28 14:15:58 -0700417 # Always copy over the diff marker.
418 $res = substr($line, 0, 1);
419
420 for ($off = 1; $off < length($line); $off++) {
421 $c = substr($line, $off, 1);
422
423 # Comments we are wacking completly including the begin
424 # and end, all to $;.
425 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
426 $sanitise_quote = '*/';
427
428 substr($res, $off, 2, "$;$;");
429 $off++;
430 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800431 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700432 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700433 $sanitise_quote = '';
434 substr($res, $off, 2, "$;$;");
435 $off++;
436 next;
437 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700438 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
439 $sanitise_quote = '//';
440
441 substr($res, $off, 2, $sanitise_quote);
442 $off++;
443 next;
444 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700445
446 # A \ in a string means ignore the next character.
447 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
448 $c eq "\\") {
449 substr($res, $off, 2, 'XX');
450 $off++;
451 next;
452 }
453 # Regular quotes.
454 if ($c eq "'" || $c eq '"') {
455 if ($sanitise_quote eq '') {
456 $sanitise_quote = $c;
457
458 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700459 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700460 } elsif ($sanitise_quote eq $c) {
461 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700462 }
463 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700464
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800465 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700466 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
467 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700468 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
469 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700470 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
471 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700472 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700473 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700474 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800475 }
476
Daniel Walker113f04a2009-09-21 17:04:35 -0700477 if ($sanitise_quote eq '//') {
478 $sanitise_quote = '';
479 }
480
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800481 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700482 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800483 my $clean = 'X' x length($1);
484 $res =~ s@\<.*\>@<$clean>@;
485
486 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700487 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800488 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700489 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800490 }
491
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700492 return $res;
493}
494
Andy Whitcroft8905a672007-11-28 16:21:06 -0800495sub ctx_statement_block {
496 my ($linenr, $remain, $off) = @_;
497 my $line = $linenr - 1;
498 my $blk = '';
499 my $soff = $off;
500 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700501 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800502
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800503 my $loff = 0;
504
Andy Whitcroft8905a672007-11-28 16:21:06 -0800505 my $type = '';
506 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800507 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800508 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509 my $c;
510 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800511
512 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800513 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800514 @stack = (['', 0]) if ($#stack == -1);
515
Andy Whitcroft773647a2008-03-28 14:15:58 -0700516 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800517 # If we are about to drop off the end, pull in more
518 # context.
519 if ($off >= $len) {
520 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700521 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800522 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800524 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800525 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800526 $len = length($blk);
527 $line++;
528 last;
529 }
530 # Bail if there is no further context.
531 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800532 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800533 last;
534 }
535 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800536 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800537 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800538 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800539
Andy Whitcroft773647a2008-03-28 14:15:58 -0700540 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800541
542 # Handle nested #if/#else.
543 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
544 push(@stack, [ $type, $level ]);
545 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
546 ($type, $level) = @{$stack[$#stack - 1]};
547 } elsif ($remainder =~ /^#\s*endif\b/) {
548 ($type, $level) = @{pop(@stack)};
549 }
550
Andy Whitcroft8905a672007-11-28 16:21:06 -0800551 # Statement ends at the ';' or a close '}' at the
552 # outermost level.
553 if ($level == 0 && $c eq ';') {
554 last;
555 }
556
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800557 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700558 if ($level == 0 && $coff_set == 0 &&
559 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
560 $remainder =~ /^(else)(?:\s|{)/ &&
561 $remainder !~ /^else\s+if\b/) {
562 $coff = $off + length($1) - 1;
563 $coff_set = 1;
564 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
565 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800566 }
567
Andy Whitcroft8905a672007-11-28 16:21:06 -0800568 if (($type eq '' || $type eq '(') && $c eq '(') {
569 $level++;
570 $type = '(';
571 }
572 if ($type eq '(' && $c eq ')') {
573 $level--;
574 $type = ($level != 0)? '(' : '';
575
576 if ($level == 0 && $coff < $soff) {
577 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700578 $coff_set = 1;
579 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800580 }
581 }
582 if (($type eq '' || $type eq '{') && $c eq '{') {
583 $level++;
584 $type = '{';
585 }
586 if ($type eq '{' && $c eq '}') {
587 $level--;
588 $type = ($level != 0)? '{' : '';
589
590 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700591 if (substr($blk, $off + 1, 1) eq ';') {
592 $off++;
593 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800594 last;
595 }
596 }
597 $off++;
598 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700599 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800600 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700601 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800602 $line++;
603 $remain--;
604 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800605
606 my $statement = substr($blk, $soff, $off - $soff + 1);
607 my $condition = substr($blk, $soff, $coff - $soff + 1);
608
609 #warn "STATEMENT<$statement>\n";
610 #warn "CONDITION<$condition>\n";
611
Andy Whitcroft773647a2008-03-28 14:15:58 -0700612 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800613
614 return ($statement, $condition,
615 $line, $remain + 1, $off - $loff + 1, $level);
616}
617
Andy Whitcroftcf655042008-03-04 14:28:20 -0800618sub statement_lines {
619 my ($stmt) = @_;
620
621 # Strip the diff line prefixes and rip blank lines at start and end.
622 $stmt =~ s/(^|\n)./$1/g;
623 $stmt =~ s/^\s*//;
624 $stmt =~ s/\s*$//;
625
626 my @stmt_lines = ($stmt =~ /\n/g);
627
628 return $#stmt_lines + 2;
629}
630
631sub statement_rawlines {
632 my ($stmt) = @_;
633
634 my @stmt_lines = ($stmt =~ /\n/g);
635
636 return $#stmt_lines + 2;
637}
638
639sub statement_block_size {
640 my ($stmt) = @_;
641
642 $stmt =~ s/(^|\n)./$1/g;
643 $stmt =~ s/^\s*{//;
644 $stmt =~ s/}\s*$//;
645 $stmt =~ s/^\s*//;
646 $stmt =~ s/\s*$//;
647
648 my @stmt_lines = ($stmt =~ /\n/g);
649 my @stmt_statements = ($stmt =~ /;/g);
650
651 my $stmt_lines = $#stmt_lines + 2;
652 my $stmt_statements = $#stmt_statements + 1;
653
654 if ($stmt_lines > $stmt_statements) {
655 return $stmt_lines;
656 } else {
657 return $stmt_statements;
658 }
659}
660
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800661sub ctx_statement_full {
662 my ($linenr, $remain, $off) = @_;
663 my ($statement, $condition, $level);
664
665 my (@chunks);
666
Andy Whitcroftcf655042008-03-04 14:28:20 -0800667 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800668 ($statement, $condition, $linenr, $remain, $off, $level) =
669 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700670 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800671 push(@chunks, [ $condition, $statement ]);
672 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
673 return ($level, $linenr, @chunks);
674 }
675
676 # Pull in the following conditional/block pairs and see if they
677 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800678 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800679 ($statement, $condition, $linenr, $remain, $off, $level) =
680 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800681 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700682 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800683 #print "C: push\n";
684 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800685 }
686
687 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800688}
689
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700690sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700691 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700692 my $line;
693 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700694 my $blk = '';
695 my @o;
696 my @c;
697 my @res = ();
698
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700699 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800700 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700701 for ($line = $start; $remain > 0; $line++) {
702 next if ($rawlines[$line] =~ /^-/);
703 $remain--;
704
705 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800706
707 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700708 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800709 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700710 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800711 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700712 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800713 $level = pop(@stack);
714 }
715
Andy Whitcroft01464f32010-10-26 14:23:19 -0700716 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700717 ##print "C<$c>L<$level><$open$close>O<$off>\n";
718 if ($off > 0) {
719 $off--;
720 next;
721 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700722
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700723 if ($c eq $close && $level > 0) {
724 $level--;
725 last if ($level == 0);
726 } elsif ($c eq $open) {
727 $level++;
728 }
729 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700730
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700731 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700732 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700733 }
734
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700735 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700736 }
737
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700738 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700739}
740sub ctx_block_outer {
741 my ($linenr, $remain) = @_;
742
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700743 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
744 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700745}
746sub ctx_block {
747 my ($linenr, $remain) = @_;
748
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700749 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
750 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700751}
752sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700753 my ($linenr, $remain, $off) = @_;
754
755 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
756 return @r;
757}
758sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700759 my ($linenr, $remain) = @_;
760
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700761 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700762}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700763sub ctx_statement_level {
764 my ($linenr, $remain, $off) = @_;
765
766 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
767}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700768
769sub ctx_locate_comment {
770 my ($first_line, $end_line) = @_;
771
772 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700773 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700774 return $current_comment if (defined $current_comment);
775
776 # Look through the context and try and figure out if there is a
777 # comment.
778 my $in_comment = 0;
779 $current_comment = '';
780 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700781 my $line = $rawlines[$linenr - 1];
782 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700783 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
784 $in_comment = 1;
785 }
786 if ($line =~ m@/\*@) {
787 $in_comment = 1;
788 }
789 if (!$in_comment && $current_comment ne '') {
790 $current_comment = '';
791 }
792 $current_comment .= $line . "\n" if ($in_comment);
793 if ($line =~ m@\*/@) {
794 $in_comment = 0;
795 }
796 }
797
798 chomp($current_comment);
799 return($current_comment);
800}
801sub ctx_has_comment {
802 my ($first_line, $end_line) = @_;
803 my $cmt = ctx_locate_comment($first_line, $end_line);
804
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700805 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700806 ##print "CMMT: $cmt\n";
807
808 return ($cmt ne '');
809}
810
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700811sub raw_line {
812 my ($linenr, $cnt) = @_;
813
814 my $offset = $linenr - 1;
815 $cnt++;
816
817 my $line;
818 while ($cnt) {
819 $line = $rawlines[$offset++];
820 next if (defined($line) && $line =~ /^-/);
821 $cnt--;
822 }
823
824 return $line;
825}
826
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700827sub cat_vet {
828 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700829 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700830
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700831 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700832 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
833 $res .= $1;
834 if ($2 ne '') {
835 $coded = sprintf("^%c", unpack('C', $2) + 64);
836 $res .= $coded;
837 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700838 }
839 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700840
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700841 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700842}
843
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800844my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800845my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800846my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700847my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800848
849sub annotate_reset {
850 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800851 $av_pending = '_';
852 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700853 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800854}
855
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700856sub annotate_values {
857 my ($stream, $type) = @_;
858
859 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700860 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700861 my $cur = $stream;
862
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800863 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700864
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700865 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700866 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800867 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700868 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700869 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800870 print "WS($1)\n" if ($dbg_values > 1);
871 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800872 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800873 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700874 }
875
Florian Micklerc023e4732011-01-12 16:59:58 -0800876 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -0700877 print "CAST($1)\n" if ($dbg_values > 1);
878 push(@av_paren_type, $type);
879 $type = 'C';
880
Andy Whitcrofte91b6e22010-10-26 14:23:11 -0700881 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800882 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700883 $type = 'T';
884
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700885 } elsif ($cur =~ /^($Modifier)\s*/) {
886 print "MODIFIER($1)\n" if ($dbg_values > 1);
887 $type = 'T';
888
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700889 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700890 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800891 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700892 push(@av_paren_type, $type);
893 if ($2 ne '') {
894 $av_pending = 'N';
895 }
896 $type = 'E';
897
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700898 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700899 print "UNDEF($1)\n" if ($dbg_values > 1);
900 $av_preprocessor = 1;
901 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700902
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700903 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800904 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800905 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800906
907 push(@av_paren_type, $type);
908 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700909 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800910
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700911 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800912 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
913 $av_preprocessor = 1;
914
915 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
916
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700917 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800918
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700919 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800920 print "PRE_END($1)\n" if ($dbg_values > 1);
921
922 $av_preprocessor = 1;
923
924 # Assume all arms of the conditional end as this
925 # one does, and continue as if the #endif was not here.
926 pop(@av_paren_type);
927 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700928 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700929
930 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800931 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700932
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700933 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
934 print "ATTR($1)\n" if ($dbg_values > 1);
935 $av_pending = $type;
936 $type = 'N';
937
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700938 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800939 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700940 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800941 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700942 }
943 $type = 'N';
944
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700945 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800946 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700947 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700948 $type = 'N';
949
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700950 } elsif ($cur =~/^(case)/o) {
951 print "CASE($1)\n" if ($dbg_values > 1);
952 $av_pend_colon = 'C';
953 $type = 'N';
954
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700955 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800956 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700957 $type = 'N';
958
959 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800960 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800961 push(@av_paren_type, $av_pending);
962 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700963 $type = 'N';
964
965 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800966 my $new_type = pop(@av_paren_type);
967 if ($new_type ne '_') {
968 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800969 print "PAREN('$1') -> $type\n"
970 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700971 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800972 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700973 }
974
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700975 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800976 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700977 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800978 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700979
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800980 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
981 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700982 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800983 } elsif ($type eq 'E') {
984 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700985 }
986 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
987 $type = 'V';
988
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700989 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800990 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700991 $type = 'V';
992
993 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800994 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700995 $type = 'N';
996
Andy Whitcroftcf655042008-03-04 14:28:20 -0800997 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800998 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800999 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001000 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001001
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001002 } elsif ($cur =~/^(,)/) {
1003 print "COMMA($1)\n" if ($dbg_values > 1);
1004 $type = 'C';
1005
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001006 } elsif ($cur =~ /^(\?)/o) {
1007 print "QUESTION($1)\n" if ($dbg_values > 1);
1008 $type = 'N';
1009
1010 } elsif ($cur =~ /^(:)/o) {
1011 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1012
1013 substr($var, length($res), 1, $av_pend_colon);
1014 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1015 $type = 'E';
1016 } else {
1017 $type = 'N';
1018 }
1019 $av_pend_colon = 'O';
1020
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001021 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001022 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001023 $type = 'N';
1024
Andy Whitcroft0d413862008-10-15 22:02:16 -07001025 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001026 my $variant;
1027
1028 print "OPV($1)\n" if ($dbg_values > 1);
1029 if ($type eq 'V') {
1030 $variant = 'B';
1031 } else {
1032 $variant = 'U';
1033 }
1034
1035 substr($var, length($res), 1, $variant);
1036 $type = 'N';
1037
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001038 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001039 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001040 if ($1 ne '++' && $1 ne '--') {
1041 $type = 'N';
1042 }
1043
1044 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001045 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001046 }
1047 if (defined $1) {
1048 $cur = substr($cur, length($1));
1049 $res .= $type x length($1);
1050 }
1051 }
1052
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001053 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001054}
1055
Andy Whitcroft8905a672007-11-28 16:21:06 -08001056sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001057 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001058 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001059 ^(?:
1060 $Modifier|
1061 $Storage|
1062 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001063 DEFINE_\S+
1064 )$|
1065 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001066 goto|
1067 return|
1068 case|
1069 else|
1070 asm|__asm__|
1071 do
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001072 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001073 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001074 )}x;
1075 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1076 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001077 # Check for modifiers.
1078 $possible =~ s/\s*$Storage\s*//g;
1079 $possible =~ s/\s*$Sparse\s*//g;
1080 if ($possible =~ /^\s*$/) {
1081
1082 } elsif ($possible =~ /\s/) {
1083 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001084 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001085 if ($modifier !~ $notPermitted) {
1086 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1087 push(@modifierList, $modifier);
1088 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001089 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001090
1091 } else {
1092 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1093 push(@typeList, $possible);
1094 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001095 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001096 } else {
1097 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001098 }
1099}
1100
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001101my $prefix = '';
1102
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001103sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001104 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1105 return 0;
1106 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001107 my $line = $prefix . $_[0];
1108
1109 $line = (split('\n', $line))[0] . "\n" if ($terse);
1110
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001111 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001112
1113 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001114}
1115sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001116 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001117}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001118sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001119 if (report("ERROR: $_[0]\n")) {
1120 our $clean = 0;
1121 our $cnt_error++;
1122 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001123}
1124sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001125 if (report("WARNING: $_[0]\n")) {
1126 our $clean = 0;
1127 our $cnt_warn++;
1128 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001129}
1130sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001131 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001132 our $clean = 0;
1133 our $cnt_chk++;
1134 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001135}
1136
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001137sub check_absolute_file {
1138 my ($absolute, $herecurr) = @_;
1139 my $file = $absolute;
1140
1141 ##print "absolute<$absolute>\n";
1142
1143 # See if any suffix of this path is a path within the tree.
1144 while ($file =~ s@^[^/]*/@@) {
1145 if (-f "$root/$file") {
1146 ##print "file<$file>\n";
1147 last;
1148 }
1149 }
1150 if (! -f _) {
1151 return 0;
1152 }
1153
1154 # It is, so see if the prefix is acceptable.
1155 my $prefix = $absolute;
1156 substr($prefix, -length($file)) = '';
1157
1158 ##print "prefix<$prefix>\n";
1159 if ($prefix ne ".../") {
1160 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1161 }
1162}
1163
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001164sub process {
1165 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001166
1167 my $linenr=0;
1168 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001169 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001170 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001171 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001172
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001173 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001174 my $indent;
1175 my $previndent=0;
1176 my $stashindent=0;
1177
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001178 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001179 my $signoff = 0;
1180 my $is_patch = 0;
1181
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001182 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001183 our $cnt_lines = 0;
1184 our $cnt_error = 0;
1185 our $cnt_warn = 0;
1186 our $cnt_chk = 0;
1187
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001188 # Trace the real file/line as we go.
1189 my $realfile = '';
1190 my $realline = 0;
1191 my $realcnt = 0;
1192 my $here = '';
1193 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001194 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001195 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001196 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001197
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001198 my $prev_values = 'E';
1199
1200 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001201 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001202 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001203 my %suppress_export;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001204
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001205 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001206 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001207 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001208 my @setup_docs = ();
1209 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001210
1211 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001212 my $line;
1213 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001214 $linenr++;
1215 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001216
Andy Whitcroft773647a2008-03-28 14:15:58 -07001217 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001218 $setup_docs = 0;
1219 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1220 $setup_docs = 1;
1221 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001222 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001223 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001224 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1225 $realline=$1-1;
1226 if (defined $2) {
1227 $realcnt=$3+1;
1228 } else {
1229 $realcnt=1+1;
1230 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001231 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001232
1233 # Guestimate if this is a continuing comment. Run
1234 # the context looking for a comment "edge". If this
1235 # edge is a close comment then we must be in a comment
1236 # at context start.
1237 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001238 my $cnt = $realcnt;
1239 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1240 next if (defined $rawlines[$ln - 1] &&
1241 $rawlines[$ln - 1] =~ /^-/);
1242 $cnt--;
1243 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001244 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001245 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1246 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1247 ($edge) = $1;
1248 last;
1249 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001250 }
1251 if (defined $edge && $edge eq '*/') {
1252 $in_comment = 1;
1253 }
1254
1255 # Guestimate if this is a continuing comment. If this
1256 # is the start of a diff block and this line starts
1257 # ' *' then it is very likely a comment.
1258 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001259 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001260 {
1261 $in_comment = 1;
1262 }
1263
1264 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1265 sanitise_line_reset($in_comment);
1266
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001267 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001268 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001269 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001270 $line = sanitise_line($rawline);
1271 }
1272 push(@lines, $line);
1273
1274 if ($realcnt > 1) {
1275 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1276 } else {
1277 $realcnt = 0;
1278 }
1279
1280 #print "==>$rawline\n";
1281 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001282
1283 if ($setup_docs && $line =~ /^\+/) {
1284 push(@setup_docs, $line);
1285 }
1286 }
1287
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001288 $prefix = '';
1289
Andy Whitcroft773647a2008-03-28 14:15:58 -07001290 $realcnt = 0;
1291 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001292 foreach my $line (@lines) {
1293 $linenr++;
1294
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001295 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001296
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001297#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001298 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001299 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001300 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001301 $realline=$1-1;
1302 if (defined $2) {
1303 $realcnt=$3+1;
1304 } else {
1305 $realcnt=1+1;
1306 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001307 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001308 $prev_values = 'E';
1309
Andy Whitcroft773647a2008-03-28 14:15:58 -07001310 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001311 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001312 %suppress_export = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001313 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001314
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001315# track the line number as we move through the hunk, note that
1316# new versions of GNU diff omit the leading space on completely
1317# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001318 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001319 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001320 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001321
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001322 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001323 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001324
1325 # Track the previous line.
1326 ($prevline, $stashline) = ($stashline, $line);
1327 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001328 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1329
Andy Whitcroft773647a2008-03-28 14:15:58 -07001330 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001331
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001332 } elsif ($realcnt == 1) {
1333 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001334 }
1335
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001336 my $hunk_line = ($realcnt != 0);
1337
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001338#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001339 $prefix = "$filename:$realline: " if ($emacs && $file);
1340 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1341
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001342 $here = "#$linenr: " if (!$file);
1343 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001344
1345 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001346 if ($line =~ /^diff --git.*?(\S+)$/) {
1347 $realfile = $1;
1348 $realfile =~ s@^([^/]*)/@@;
1349
1350 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001351 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001352 $realfile =~ s@^([^/]*)/@@;
1353
1354 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001355 if (!$file && $tree && $p1_prefix ne '' &&
1356 -e "$root/$p1_prefix") {
Wolfram Sang1e855722009-01-06 14:41:24 -08001357 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1358 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001359
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001360 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001361 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1362 }
1363 next;
1364 }
1365
Randy Dunlap389834b2007-06-08 13:47:03 -07001366 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001367
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001368 my $hereline = "$here\n$rawline\n";
1369 my $herecurr = "$here\n$rawline\n";
1370 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001371
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001372 $cnt_lines++ if ($realcnt != 0);
1373
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001374# Check for incorrect file permissions
1375 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1376 my $permhere = $here . "FILE: $realfile\n";
1377 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1378 ERROR("do not set execute permissions for source files\n" . $permhere);
1379 }
1380 }
1381
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001382#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001383 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001384 # This is a signoff, if ugly, so do not double report.
1385 $signoff++;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001386 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001387 WARN("Signed-off-by: is the preferred form\n" .
1388 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001389 }
1390 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001391 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001392 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001393 }
1394 }
1395
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001396# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001397 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001398 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001399 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001400 }
1401
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001402# Check for absolute kernel paths.
1403 if ($tree) {
1404 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1405 my $file = $1;
1406
1407 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1408 check_absolute_file($1, $herecurr)) {
1409 #
1410 } else {
1411 check_absolute_file($file, $herecurr);
1412 }
1413 }
1414 }
1415
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001416# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1417 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001418 $rawline !~ m/^$UTF8*$/) {
1419 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1420
1421 my $blank = copy_spacing($rawline);
1422 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1423 my $hereptr = "$hereline$ptr\n";
1424
1425 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001426 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001427
Andy Whitcroft306708542008-10-15 22:02:28 -07001428# ignore non-hunk lines and lines being removed
1429 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001430
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001431#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001432 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001433 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001434 ERROR("DOS line endings\n" . $herevet);
1435
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001436 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1437 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001438 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001439 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001440 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001441
Andi Kleen33549572010-05-24 14:33:29 -07001442# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001443# Only applies when adding the entry originally, after that we do not have
1444# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001445 if ($realfile =~ /Kconfig/ &&
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001446 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
Andi Kleen33549572010-05-24 14:33:29 -07001447 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001448 my $cnt = $realcnt;
1449 my $ln = $linenr + 1;
1450 my $f;
1451 my $is_end = 0;
1452 while ($cnt > 0 && defined $lines[$ln - 1]) {
1453 $f = $lines[$ln - 1];
1454 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1455 $is_end = $lines[$ln - 1] =~ /^\+/;
1456 $ln++;
1457
1458 next if ($f =~ /^-/);
1459 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001460 $f =~ s/#.*//;
1461 $f =~ s/^\s+//;
1462 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001463 if ($f =~ /^\s*config\s/) {
1464 $is_end = 1;
1465 last;
1466 }
Andi Kleen33549572010-05-24 14:33:29 -07001467 $length++;
1468 }
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001469 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1470 #print "is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001471 }
1472
Andy Whitcroft5368df202008-10-15 22:02:27 -07001473# check we are in a valid source file if not then ignore this hunk
1474 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1475
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001476#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001477 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001478 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001479 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001480 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001481 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001482 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001483 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001484 }
1485
Joe Perches5e79d962010-03-05 13:43:55 -08001486# check for spaces before a quoted newline
1487 if ($rawline =~ /^.*\".*\s\\n/) {
1488 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1489 }
1490
Andy Whitcroft8905a672007-11-28 16:21:06 -08001491# check for adding lines without a newline.
1492 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1493 WARN("adding a line without newline at end of file\n" . $herecurr);
1494 }
1495
Mike Frysinger42e41c52009-09-21 17:04:40 -07001496# Blackfin: use hi/lo macros
1497 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1498 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1499 my $herevet = "$here\n" . cat_vet($line) . "\n";
1500 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1501 }
1502 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1503 my $herevet = "$here\n" . cat_vet($line) . "\n";
1504 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1505 }
1506 }
1507
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001508# check we are in a valid source file C or perl if not then ignore this hunk
1509 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001510
1511# at the beginning of a line any tabs must come first and anything
1512# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001513 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1514 $rawline =~ /^\+\s* \s*/) {
1515 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001516 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001517 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001518 }
1519
Alberto Panizzo08e44362010-03-05 13:43:54 -08001520# check for space before tabs.
1521 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1522 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1523 WARN("please, no space before tabs\n" . $herevet);
1524 }
1525
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001526# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001527# Exceptions:
1528# 1) within comments
1529# 2) indented preprocessor commands
1530# 3) hanging labels
1531 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001532 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001533 WARN("please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001534 }
1535
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001536# check we are in a valid C source file if not then ignore this hunk
1537 next if ($realfile !~ /\.(h|c)$/);
1538
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001539# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001540 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001541 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1542 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001543
Mike Frysinger42e41c52009-09-21 17:04:40 -07001544# Blackfin: don't use __builtin_bfin_[cs]sync
1545 if ($line =~ /__builtin_bfin_csync/) {
1546 my $herevet = "$here\n" . cat_vet($line) . "\n";
1547 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1548 }
1549 if ($line =~ /__builtin_bfin_ssync/) {
1550 my $herevet = "$here\n" . cat_vet($line) . "\n";
1551 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1552 }
1553
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001554# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001555 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1556 $realline_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001557 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001558 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001559 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001560 $stat =~ s/\n./\n /g;
1561 $cond =~ s/\n./\n /g;
1562
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001563 # Find the real next line.
1564 $realline_next = $line_nr_next;
1565 if (defined $realline_next &&
1566 (!defined $lines[$realline_next - 1] ||
1567 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1568 $realline_next++;
1569 }
1570
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001571 my $s = $stat;
1572 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001573
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001574 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001575 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001576
1577 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001578 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001579
Andy Whitcroft463f2862009-09-21 17:04:34 -07001580 } elsif ($s =~ /^.\s*else\b/s) {
1581
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001582 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001583 } 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 -07001584 my $type = $1;
1585 $type =~ s/\s+/ /g;
1586 possible($type, "A:" . $s);
1587
Andy Whitcroft8905a672007-11-28 16:21:06 -08001588 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07001589 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001590 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001591 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001592
1593 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001594 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001595 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001596 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001597
1598 # Check for any sort of function declaration.
1599 # int foo(something bar, other baz);
1600 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001601 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 -08001602 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001603
Andy Whitcroftcf655042008-03-04 14:28:20 -08001604 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001605 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001606 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001607
Andy Whitcroft8905a672007-11-28 16:21:06 -08001608 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001609 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001610
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001611 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001612 }
1613 }
1614 }
1615
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001616 }
1617
Andy Whitcroft653d4872007-06-23 17:16:34 -07001618#
1619# Checks which may be anchored in the context.
1620#
1621
1622# Check for switch () and associated case and default
1623# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001624 if ($line=~/\bswitch\s*\(.*\)/) {
1625 my $err = '';
1626 my $sep = '';
1627 my @ctx = ctx_block_outer($linenr, $realcnt);
1628 shift(@ctx);
1629 for my $ctx (@ctx) {
1630 my ($clen, $cindent) = line_stats($ctx);
1631 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1632 $indent != $cindent) {
1633 $err .= "$sep$ctx\n";
1634 $sep = '';
1635 } else {
1636 $sep = "[...]\n";
1637 }
1638 }
1639 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001640 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001641 }
1642 }
1643
1644# if/while/etc brace do not go on next line, unless defining a do while loop,
1645# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001646 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001647 my $pre_ctx = "$1$2";
1648
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001649 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001650 my $ctx_cnt = $realcnt - $#ctx - 1;
1651 my $ctx = join("\n", @ctx);
1652
Andy Whitcroft548596d2008-07-23 21:29:01 -07001653 my $ctx_ln = $linenr;
1654 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001655
Andy Whitcroft548596d2008-07-23 21:29:01 -07001656 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1657 defined $lines[$ctx_ln - 1] &&
1658 $lines[$ctx_ln - 1] =~ /^-/)) {
1659 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1660 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001661 $ctx_ln++;
1662 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001663
Andy Whitcroft53210162008-07-23 21:29:03 -07001664 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1665 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001666
1667 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1668 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001669 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001670 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001671 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1672 $ctx =~ /\)\s*\;\s*$/ &&
1673 defined $lines[$ctx_ln - 1])
1674 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001675 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1676 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001677 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001678 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001679 }
1680 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001681 }
1682
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001683# Check relative indent for conditionals and blocks.
1684 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1685 my ($s, $c) = ($stat, $cond);
1686
1687 substr($s, 0, length($c), '');
1688
1689 # Make sure we remove the line prefixes as we have
1690 # none on the first line, and are going to readd them
1691 # where necessary.
1692 $s =~ s/\n./\n/gs;
1693
1694 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001695 my @newlines = ($c =~ /\n/gs);
1696 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001697
1698 # We want to check the first line inside the block
1699 # starting at the end of the conditional, so remove:
1700 # 1) any blank line termination
1701 # 2) any opening brace { on end of the line
1702 # 3) any do (...) {
1703 my $continuation = 0;
1704 my $check = 0;
1705 $s =~ s/^.*\bdo\b//;
1706 $s =~ s/^\s*{//;
1707 if ($s =~ s/^\s*\\//) {
1708 $continuation = 1;
1709 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001710 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001711 $check = 1;
1712 $cond_lines++;
1713 }
1714
1715 # Also ignore a loop construct at the end of a
1716 # preprocessor statement.
1717 if (($prevline =~ /^.\s*#\s*define\s/ ||
1718 $prevline =~ /\\\s*$/) && $continuation == 0) {
1719 $check = 0;
1720 }
1721
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001722 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001723 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001724 while ($cond_ptr != $cond_lines) {
1725 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001726
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001727 # If we see an #else/#elif then the code
1728 # is not linear.
1729 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1730 $check = 0;
1731 }
1732
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001733 # Ignore:
1734 # 1) blank lines, they should be at 0,
1735 # 2) preprocessor lines, and
1736 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001737 if ($continuation ||
1738 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001739 $s =~ /^\s*#\s*?/ ||
1740 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001741 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001742 if ($s =~ s/^.*?\n//) {
1743 $cond_lines++;
1744 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001745 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001746 }
1747
1748 my (undef, $sindent) = line_stats("+" . $s);
1749 my $stat_real = raw_line($linenr, $cond_lines);
1750
1751 # Check if either of these lines are modified, else
1752 # this is not this patch's fault.
1753 if (!defined($stat_real) ||
1754 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1755 $check = 0;
1756 }
1757 if (defined($stat_real) && $cond_lines > 1) {
1758 $stat_real = "[...]\n$stat_real";
1759 }
1760
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001761 #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 -07001762
1763 if ($check && (($sindent % 8) != 0 ||
1764 ($sindent <= $indent && $s ne ''))) {
1765 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1766 }
1767 }
1768
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001769 # Track the 'values' across context and added lines.
1770 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001771 my ($curr_values, $curr_vars) =
1772 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001773 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001774 if ($dbg_values) {
1775 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001776 print "$linenr > .$outline\n";
1777 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001778 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001779 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001780 $prev_values = substr($curr_values, -1);
1781
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001782#ignore lines not being added
1783 if ($line=~/^[^\+]/) {next;}
1784
Andy Whitcroft653d4872007-06-23 17:16:34 -07001785# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001786 if ($dbg_type) {
1787 if ($line =~ /^.\s*$Declare\s*$/) {
1788 ERROR("TEST: is type\n" . $herecurr);
1789 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1790 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1791 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001792 next;
1793 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001794# TEST: allow direct testing of the attribute matcher.
1795 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001796 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001797 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001798 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001799 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1800 }
1801 next;
1802 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001803
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001804# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07001805 if ($line =~ /^.\s*{/ &&
1806 $prevline =~ /(?:^|[^=])=\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001807 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001808 }
1809
Andy Whitcroft653d4872007-06-23 17:16:34 -07001810#
1811# Checks which are anchored on the added line.
1812#
1813
1814# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001815 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001816 my $path = $1;
1817 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001818 ERROR("malformed #include filename\n" .
1819 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001820 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001821 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001822
1823# no C99 // comments
1824 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001825 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001826 }
1827 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001828 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001829 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001830
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001831# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1832# the whole statement.
1833#print "APW <$lines[$realline_next - 1]>\n";
1834 if (defined $realline_next &&
1835 exists $lines[$realline_next - 1] &&
1836 !defined $suppress_export{$realline_next} &&
1837 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1838 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001839 # Handle definitions which produce identifiers with
1840 # a prefix:
1841 # XXX(foo);
1842 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001843 my $name = $1;
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001844 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1845 $name =~ /^${Ident}_$2/) {
1846#print "FOO C name<$name>\n";
1847 $suppress_export{$realline_next} = 1;
1848
1849 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001850 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07001851 ^.DEFINE_$Ident\(\Q$name\E\)|
1852 ^.DECLARE_$Ident\(\Q$name\E\)|
1853 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001854 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1855 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07001856 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001857#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1858 $suppress_export{$realline_next} = 2;
1859 } else {
1860 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001861 }
1862 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001863 if (!defined $suppress_export{$linenr} &&
1864 $prevline =~ /^.\s*$/ &&
1865 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1866 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1867#print "FOO B <$lines[$linenr - 1]>\n";
1868 $suppress_export{$linenr} = 2;
1869 }
1870 if (defined $suppress_export{$linenr} &&
1871 $suppress_export{$linenr} == 2) {
1872 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1873 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001874
Joe Eloff5150bda2010-08-09 17:21:00 -07001875# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001876 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Eloff5150bda2010-08-09 17:21:00 -07001877 ERROR("do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001878 $herecurr);
1879 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001880# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001881 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001882 ERROR("do not initialise statics to 0 or NULL\n" .
1883 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001884 }
1885
Joe Perchescb710ec2010-10-26 14:23:20 -07001886# check for static const char * arrays.
1887 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1888 WARN("static const char * array should probably be static const char * const\n" .
1889 $herecurr);
1890 }
1891
1892# check for static char foo[] = "bar" declarations.
1893 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1894 WARN("static char array declaration should probably be static const char\n" .
1895 $herecurr);
1896 }
1897
Joe Perches93ed0e22010-10-26 14:23:21 -07001898# check for declarations of struct pci_device_id
1899 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1900 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1901 }
1902
Andy Whitcroft653d4872007-06-23 17:16:34 -07001903# check for new typedefs, only function parameters and sparse annotations
1904# make sense.
1905 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001906 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001907 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001908 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001909 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001910 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001911 }
1912
1913# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001914 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001915 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001916 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001917
Andy Whitcroft65863862009-01-06 14:41:21 -08001918 # Should start with a space.
1919 $to =~ s/^(\S)/ $1/;
1920 # Should not end with a space.
1921 $to =~ s/\s+$//;
1922 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001923 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001924 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001925
Andy Whitcroft65863862009-01-06 14:41:21 -08001926 #print "from<$from> to<$to>\n";
1927 if ($from ne $to) {
1928 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1929 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001930 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001931 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001932
Andy Whitcroft65863862009-01-06 14:41:21 -08001933 # Should start with a space.
1934 $to =~ s/^(\S)/ $1/;
1935 # Should not end with a space.
1936 $to =~ s/\s+$//;
1937 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001938 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001939 }
1940 # Modifiers should have spaces.
1941 $to =~ s/(\b$Modifier$)/$1 /;
1942
Andy Whitcroft667026e2009-02-27 14:03:08 -08001943 #print "from<$from> to<$to> ident<$ident>\n";
1944 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001945 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1946 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001947 }
1948
1949# # no BUG() or BUG_ON()
1950# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1951# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1952# print "$herecurr";
1953# $clean = 0;
1954# }
1955
Andy Whitcroft8905a672007-11-28 16:21:06 -08001956 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001957 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 -08001958 }
1959
Joe Perches17441222011-06-15 15:08:17 -07001960# check for uses of printk_ratelimit
1961 if ($line =~ /\bprintk_ratelimit\s*\(/) {
1962 WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
1963 }
1964
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001965# printk should use KERN_* levels. Note that follow on printk's on the
1966# same line do not need a level, so we use the current block context
1967# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001968# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001969# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001970 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001971 my $ok = 0;
1972 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1973 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001974 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001975 # with "\n" ignore it, else it is to blame
1976 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1977 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1978 $ok = 1;
1979 }
1980 last;
1981 }
1982 }
1983 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001984 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001985 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001986 }
1987
Andy Whitcroft653d4872007-06-23 17:16:34 -07001988# function brace can't be on same line, except for #defines of do while,
1989# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001990 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1991 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001992 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001993 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001994
Andy Whitcroft8905a672007-11-28 16:21:06 -08001995# open braces for enum, union and struct go on the same line.
1996 if ($line =~ /^.\s*{/ &&
1997 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1998 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1999 }
2000
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002001# missing space after union, struct or enum definition
2002 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2003 WARN("missing space after $1 definition\n" . $herecurr);
2004 }
2005
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002006# check for spacing round square brackets; allowed:
2007# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002008# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2009# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002010 while ($line =~ /(.*?\s)\[/g) {
2011 my ($where, $prefix) = ($-[1], $1);
2012 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002013 ($where != 0 || $prefix !~ /^.\s+$/) &&
2014 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002015 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2016 }
2017 }
2018
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002019# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002020 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002021 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002022 my $ctx_before = substr($line, 0, $-[1]);
2023 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002024
2025 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002026 if ($name =~ /^(?:
2027 if|for|while|switch|return|case|
2028 volatile|__volatile__|
2029 __attribute__|format|__extension__|
2030 asm|__asm__)$/x)
2031 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002032
2033 # cpp #define statements have non-optional spaces, ie
2034 # if there is a space between the name and the open
2035 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002036 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002037
2038 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002039 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002040
2041 # If this whole things ends with a type its most
2042 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002043 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002044
2045 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002046 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002047 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002048 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002049# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002050 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002051 my $ops = qr{
2052 <<=|>>=|<=|>=|==|!=|
2053 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2054 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002055 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2056 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002057 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002058 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002059 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002060
2061 my $blank = copy_spacing($opline);
2062
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002063 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002064 $off += length($elements[$n]);
2065
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002066 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002067 my $ca = substr($opline, 0, $off);
2068 my $cc = '';
2069 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2070 $cc = substr($opline, $off + length($elements[$n + 1]));
2071 }
2072 my $cb = "$ca$;$cc";
2073
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002074 my $a = '';
2075 $a = 'V' if ($elements[$n] ne '');
2076 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002077 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002078 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2079 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002080 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002081
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002082 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002083
2084 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002085 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002086 $c = 'V' if ($elements[$n + 2] ne '');
2087 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002088 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002089 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2090 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002091 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002092 } else {
2093 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002094 }
2095
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002096 my $ctx = "${a}x${c}";
2097
2098 my $at = "(ctx:$ctx)";
2099
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002100 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002101 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002102
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002103 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002104 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002105
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002106 # Get the full operator variant.
2107 my $opv = $op . substr($curr_vars, $off, 1);
2108
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002109 # Ignore operators passed as parameters.
2110 if ($op_type ne 'V' &&
2111 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2112
Andy Whitcroftcf655042008-03-04 14:28:20 -08002113# # Ignore comments
2114# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002115
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002116 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002117 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002118 if ($ctx !~ /.x[WEBC]/ &&
2119 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002120 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002121 }
2122
2123 # // is a comment
2124 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002125
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002126 # No spaces for:
2127 # ->
2128 # : when part of a bitfield
2129 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002130 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002131 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002132 }
2133
2134 # , must have a space on the right.
2135 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002136 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002137 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002138 }
2139
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002140 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002141 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002142 #warn "'*' is part of type\n";
2143
2144 # unary operators should have a space before and
2145 # none after. May be left adjacent to another
2146 # unary operator, or a cast
2147 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002148 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002149 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002150 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002151 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002152 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002153 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002154 # A unary '*' may be const
2155
2156 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07002157 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002158 }
2159
2160 # unary ++ and unary -- are allowed no space on one side.
2161 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002162 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2163 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002164 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002165 if ($ctx =~ /Wx[BE]/ ||
2166 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2167 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002168 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002169 if ($ctx =~ /ExW/) {
2170 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2171 }
2172
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002173
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002174 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002175 } elsif ($op eq '<<' or $op eq '>>' or
2176 $op eq '&' or $op eq '^' or $op eq '|' or
2177 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002178 $op eq '*' or $op eq '/' or
2179 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002180 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002181 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002182 ERROR("need consistent spacing around '$op' $at\n" .
2183 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002184 }
2185
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002186 # A colon needs no spaces before when it is
2187 # terminating a case value or a label.
2188 } elsif ($opv eq ':C' || $opv eq ':L') {
2189 if ($ctx =~ /Wx./) {
2190 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2191 }
2192
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002193 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002194 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002195 my $ok = 0;
2196
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002197 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002198 if (($op eq '<' &&
2199 $cc =~ /^\S+\@\S+>/) ||
2200 ($op eq '>' &&
2201 $ca =~ /<\S+\@\S+$/))
2202 {
2203 $ok = 1;
2204 }
2205
2206 # Ignore ?:
2207 if (($opv eq ':O' && $ca =~ /\?$/) ||
2208 ($op eq '?' && $cc =~ /^:/)) {
2209 $ok = 1;
2210 }
2211
2212 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002213 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002214 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002215 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002216 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002217 }
2218 }
2219
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002220# check for multiple assignments
2221 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002222 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002223 }
2224
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002225## # check for multiple declarations, allowing for a function declaration
2226## # continuation.
2227## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2228## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2229##
2230## # Remove any bracketed sections to ensure we do not
2231## # falsly report the parameters of functions.
2232## my $ln = $line;
2233## while ($ln =~ s/\([^\(\)]*\)//g) {
2234## }
2235## if ($ln =~ /,/) {
2236## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2237## }
2238## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002239
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002240#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002241 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2242 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002243 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002244 }
2245
2246# closing brace should have a space following it when it has anything
2247# on the line
2248 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002249 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002250 }
2251
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002252# check spacing on square brackets
2253 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002254 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002255 }
2256 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002257 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002258 }
2259
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002260# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002261 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2262 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002263 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002264 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002265 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002266 $line !~ /for\s*\(.*;\s+\)/ &&
2267 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002268 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002269 }
2270
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002271#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002272 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002273 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002274 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002275 }
2276
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002277# Return is not a function.
2278 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2279 my $spacing = $1;
2280 my $value = $2;
2281
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002282 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002283 $value =~ s/\(/ \(/g;
2284 $value =~ s/\)/\) /g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002285 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2286 $value !~ /(?:$Ident|-?$Constant)\s*
2287 $Compare\s*
2288 (?:$Ident|-?$Constant)/x &&
2289 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002290 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002291#print "value<$value>\n";
2292 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002293 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2294
2295 } elsif ($spacing !~ /\s+/) {
2296 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2297 }
2298 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002299# Return of what appears to be an errno should normally be -'ve
2300 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2301 my $name = $1;
2302 if ($name ne 'EOF' && $name ne 'ERROR') {
2303 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2304 }
2305 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002306
Joe Perches7d2367a2011-07-25 17:13:22 -07002307# typecasts on min/max could be min_t/max_t
2308 if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
2309 if (defined $2 || defined $8) {
2310 my $call = $1;
2311 my $cast1 = deparenthesize($2);
2312 my $arg1 = $3;
2313 my $cast2 = deparenthesize($8);
2314 my $arg2 = $9;
2315 my $cast;
2316
2317 if ($cast1 ne "" && $cast2 ne "") {
2318 $cast = "$cast1 or $cast2";
2319 } elsif ($cast1 ne "") {
2320 $cast = $cast1;
2321 } else {
2322 $cast = $cast2;
2323 }
2324 WARN("$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
2325 }
2326 }
2327
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002328# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002329 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002330 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002331 }
2332
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002333# Check for illegal assignment in if conditional -- and check for trailing
2334# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002335 if ($line =~ /do\s*(?!{)/) {
2336 my ($stat_next) = ctx_statement_block($line_nr_next,
2337 $remain_next, $off_next);
2338 $stat_next =~ s/\n./\n /g;
2339 ##print "stat<$stat> stat_next<$stat_next>\n";
2340
2341 if ($stat_next =~ /^\s*while\b/) {
2342 # If the statement carries leading newlines,
2343 # then count those as offsets.
2344 my ($whitespace) =
2345 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2346 my $offset =
2347 statement_rawlines($whitespace) - 1;
2348
2349 $suppress_whiletrailers{$line_nr_next +
2350 $offset} = 1;
2351 }
2352 }
2353 if (!defined $suppress_whiletrailers{$linenr} &&
2354 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002355 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002356
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002357 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002358 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002359 }
2360
2361 # Find out what is on the end of the line after the
2362 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002363 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002364 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002365 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002366 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2367 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002368 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002369 # Find out how long the conditional actually is.
2370 my @newlines = ($c =~ /\n/gs);
2371 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002372 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002373
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002374 $stat_real = raw_line($linenr, $cond_lines)
2375 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002376 if (defined($stat_real) && $cond_lines > 1) {
2377 $stat_real = "[...]\n$stat_real";
2378 }
2379
2380 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002381 }
2382 }
2383
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002384# Check for bitwise tests written as boolean
2385 if ($line =~ /
2386 (?:
2387 (?:\[|\(|\&\&|\|\|)
2388 \s*0[xX][0-9]+\s*
2389 (?:\&\&|\|\|)
2390 |
2391 (?:\&\&|\|\|)
2392 \s*0[xX][0-9]+\s*
2393 (?:\&\&|\|\||\)|\])
2394 )/x)
2395 {
2396 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2397 }
2398
Andy Whitcroft8905a672007-11-28 16:21:06 -08002399# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002400 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2401 my $s = $1;
2402 $s =~ s/$;//g; # Remove any comments
2403 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2404 ERROR("trailing statements should be on next line\n" . $herecurr);
2405 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002406 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002407# if should not continue a brace
2408 if ($line =~ /}\s*if\b/) {
2409 ERROR("trailing statements should be on next line\n" .
2410 $herecurr);
2411 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002412# case and default should not have general statements after them
2413 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2414 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002415 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002416 \s*return\s+
2417 )/xg)
2418 {
2419 ERROR("trailing statements should be on next line\n" . $herecurr);
2420 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002421
2422 # Check for }<nl>else {, these must be at the same
2423 # indent level to be relevant to each other.
2424 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2425 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002426 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002427 }
2428
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002429 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2430 $previndent == $indent) {
2431 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2432
2433 # Find out what is on the end of the line after the
2434 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002435 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002436 $s =~ s/\n.*//g;
2437
2438 if ($s =~ /^\s*;/) {
2439 ERROR("while should follow close brace '}'\n" . $hereprev);
2440 }
2441 }
2442
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002443#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2444# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2445# print "No studly caps, use _\n";
2446# print "$herecurr";
2447# $clean = 0;
2448# }
2449
2450#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002451 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002452 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002453 }
2454
Andy Whitcroft653d4872007-06-23 17:16:34 -07002455#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002456 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002457 my $file = "$1.h";
2458 my $checkfile = "include/linux/$file";
2459 if (-f "$root/$checkfile" &&
2460 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002461 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002462 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002463 if ($realfile =~ m{^arch/}) {
2464 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2465 } else {
2466 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2467 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002468 }
2469 }
2470
Andy Whitcroft653d4872007-06-23 17:16:34 -07002471# multi-statement macros should be enclosed in a do while loop, grab the
2472# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002473# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07002474 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2475 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002476 my $ln = $linenr;
2477 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002478 my ($off, $dstat, $dcond, $rest);
2479 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002480
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002481 my $args = defined($1);
2482
2483 # Find the end of the macro and limit our statement
2484 # search to that.
2485 while ($cnt > 0 && defined $lines[$ln - 1] &&
2486 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2487 {
2488 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002489 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002490 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002491 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002492 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002493
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002494 ($dstat, $dcond, $ln, $cnt, $off) =
2495 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2496 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002497 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002498
2499 # Extract the remainder of the define (if any) and
2500 # rip off surrounding spaces, and trailing \'s.
2501 $rest = '';
Andy Whitcroft636d140a2008-10-15 22:02:18 -07002502 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2503 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002504 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2505 $rest .= substr($lines[$ln - 1], $off) . "\n";
2506 $cnt--;
2507 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002508 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002509 $off = 0;
2510 }
2511 $rest =~ s/\\\n.//g;
2512 $rest =~ s/^\s*//s;
2513 $rest =~ s/\s*$//s;
2514
2515 # Clean up the original statement.
2516 if ($args) {
2517 substr($dstat, 0, length($dcond), '');
2518 } else {
2519 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2520 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002521 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002522 $dstat =~ s/\\\n.//g;
2523 $dstat =~ s/^\s*//s;
2524 $dstat =~ s/\s*$//s;
2525
2526 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002527 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2528 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2529 $dstat =~ s/\[[^\{\}]*\]/1/)
2530 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002531 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002532
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002533 my $exceptions = qr{
2534 $Declare|
2535 module_param_named|
2536 MODULE_PARAM_DESC|
2537 DECLARE_PER_CPU|
2538 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002539 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002540 union|
2541 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002542 \.$Ident\s*=\s*|
2543 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002544 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07002545 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2546 if ($rest ne '' && $rest ne ',') {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002547 if ($rest !~ /while\s*\(/ &&
2548 $dstat !~ /$exceptions/)
2549 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002550 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 -07002551 }
2552
2553 } elsif ($ctx !~ /;/) {
2554 if ($dstat ne '' &&
2555 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2556 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002557 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002558 $dstat =~ /$Operators/)
2559 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002560 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002561 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002562 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002563 }
2564
Mike Frysinger080ba922009-01-06 14:41:25 -08002565# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2566# all assignments may have only one of the following with an assignment:
2567# .
2568# ALIGN(...)
2569# VMLINUX_SYMBOL(...)
2570 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2571 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2572 }
2573
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002574# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002575 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2576 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002577 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002578 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002579 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2580 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002581 my $allowed = 0;
2582 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002583 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002584 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002585 for my $chunk (@chunks) {
2586 my ($cond, $block) = @{$chunk};
2587
Andy Whitcroft773647a2008-03-28 14:15:58 -07002588 # If the condition carries leading newlines, then count those as offsets.
2589 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2590 my $offset = statement_rawlines($whitespace) - 1;
2591
2592 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2593
2594 # We have looked at and allowed this specific line.
2595 $suppress_ifbraces{$ln + $offset} = 1;
2596
2597 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002598 $ln += statement_rawlines($block) - 1;
2599
Andy Whitcroft773647a2008-03-28 14:15:58 -07002600 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002601
2602 $seen++ if ($block =~ /^\s*{/);
2603
Andy Whitcroftcf655042008-03-04 14:28:20 -08002604 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2605 if (statement_lines($cond) > 1) {
2606 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002607 $allowed = 1;
2608 }
2609 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002610 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002611 $allowed = 1;
2612 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002613 if (statement_block_size($block) > 1) {
2614 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002615 $allowed = 1;
2616 }
2617 }
2618 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002619 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002620 }
2621 }
2622 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002623 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002624 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002625 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002626
Andy Whitcroftcf655042008-03-04 14:28:20 -08002627 # Check the pre-context.
2628 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2629 #print "APW: ALLOWED: pre<$1>\n";
2630 $allowed = 1;
2631 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002632
2633 my ($level, $endln, @chunks) =
2634 ctx_statement_full($linenr, $realcnt, $-[0]);
2635
Andy Whitcroftcf655042008-03-04 14:28:20 -08002636 # Check the condition.
2637 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002638 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002639 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002640 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002641 }
2642 if (statement_lines($cond) > 1) {
2643 #print "APW: ALLOWED: cond<$cond>\n";
2644 $allowed = 1;
2645 }
2646 if ($block =~/\b(?:if|for|while)\b/) {
2647 #print "APW: ALLOWED: block<$block>\n";
2648 $allowed = 1;
2649 }
2650 if (statement_block_size($block) > 1) {
2651 #print "APW: ALLOWED: lines block<$block>\n";
2652 $allowed = 1;
2653 }
2654 # Check the post-context.
2655 if (defined $chunks[1]) {
2656 my ($cond, $block) = @{$chunks[1]};
2657 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002658 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002659 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002660 if ($block =~ /^\s*\{/) {
2661 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2662 $allowed = 1;
2663 }
2664 }
2665 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2666 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002667 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002668
Andy Whitcroftf0556632008-10-15 22:02:23 -07002669 for (my $n = 0; $n < $cnt; $n++) {
2670 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002671 }
2672
2673 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002674 }
2675 }
2676
Andy Whitcroft653d4872007-06-23 17:16:34 -07002677# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002678 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002679 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002680 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002681 }
2682 }
2683
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002684# don't use deprecated functions
2685 for my $func (@dep_functions) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002686 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002687 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002688 }
2689 }
2690
2691# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002692 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2693 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002694 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002695 }
2696
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002697# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002698 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002699 CHK("if this code is redundant consider removing it\n" .
2700 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002701 }
2702
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002703# check for needless kfree() checks
2704 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2705 my $expr = $1;
2706 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002707 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002708 }
2709 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002710# check for needless usb_free_urb() checks
2711 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2712 my $expr = $1;
2713 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2714 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2715 }
2716 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002717
Patrick Pannuto1a15a252010-08-09 17:21:01 -07002718# prefer usleep_range over udelay
2719 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2720 # ignore udelay's < 10, however
2721 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2722 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2723 }
2724 }
2725
Patrick Pannuto09ef8722010-08-09 17:21:02 -07002726# warn about unexpectedly long msleep's
2727 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2728 if ($1 < 20) {
2729 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2730 }
2731 }
2732
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002733# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002734# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002735# print "#ifdef in C files should be avoided\n";
2736# print "$herecurr";
2737# $clean = 0;
2738# }
2739
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002740# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002741 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002742 ERROR("exactly one space required after that #$1\n" . $herecurr);
2743 }
2744
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002745# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002746 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2747 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002748 my $which = $1;
2749 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002750 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002751 }
2752 }
2753# check for memory barriers without a comment.
2754 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2755 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002756 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002757 }
2758 }
2759# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002760 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002761 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002762 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002763
Tobias Klauserd4977c72010-05-24 14:33:30 -07002764# Check that the storage class is at the beginning of a declaration
2765 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2766 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2767 }
2768
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002769# check the location of the inline attribute, that it is between
2770# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002771 if ($line =~ /\b$Type\s+$Inline\b/ ||
2772 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002773 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2774 }
2775
Andy Whitcroft8905a672007-11-28 16:21:06 -08002776# Check for __inline__ and __inline, prefer inline
2777 if ($line =~ /\b(__inline__|__inline)\b/) {
2778 WARN("plain inline is preferred over $1\n" . $herecurr);
2779 }
2780
Joe Perches3d130fd2011-01-12 17:00:00 -08002781# Check for __attribute__ packed, prefer __packed
2782 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2783 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2784 }
2785
Joe Perches8f53a9b2010-03-05 13:43:48 -08002786# check for sizeof(&)
2787 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2788 WARN("sizeof(& should be avoided\n" . $herecurr);
2789 }
2790
Joe Perches428e2fd2011-05-24 17:13:39 -07002791# check for line continuations in quoted strings with odd counts of "
2792 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2793 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2794 }
2795
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002796# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002797 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002798 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002799 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002800 my $function_name = $1;
2801 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002802
2803 my $s = $stat;
2804 if (defined $cond) {
2805 substr($s, 0, length($cond), '');
2806 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002807 if ($s =~ /^\s*;/ &&
2808 $function_name ne 'uninitialized_var')
2809 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002810 WARN("externs should be avoided in .c files\n" . $herecurr);
2811 }
2812
2813 if ($paren_space =~ /\n/) {
2814 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2815 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002816
2817 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2818 $stat =~ /^.\s*extern\s+/)
2819 {
2820 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002821 }
2822
2823# checks for new __setup's
2824 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2825 my $name = $1;
2826
2827 if (!grep(/$name/, @setup_docs)) {
2828 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2829 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002830 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002831
2832# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08002833 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002834 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2835 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002836
Joe Perchescaf2a542011-01-12 16:59:56 -08002837# check for multiple semicolons
2838 if ($line =~ /;\s*;\s*$/) {
2839 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2840 }
2841
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002842# check for gcc specific __FUNCTION__
2843 if ($line =~ /__FUNCTION__/) {
2844 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2845 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002846
Thomas Gleixner4882720b2010-09-07 14:34:01 +00002847# check for semaphores initialized locked
2848 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002849 WARN("consider using a completion\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01002850
Andy Whitcroft773647a2008-03-28 14:15:58 -07002851 }
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002852# recommend kstrto* over simple_strto*
Andy Whitcroft773647a2008-03-28 14:15:58 -07002853 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002854 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002855 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002856# check for __initcall(), use device_initcall() explicitly please
2857 if ($line =~ /^.\s*__initcall\s*\(/) {
2858 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2859 }
Emese Revfy79404842010-03-05 13:43:53 -08002860# check for various ops structs, ensure they are const.
2861 my $struct_ops = qr{acpi_dock_ops|
2862 address_space_operations|
2863 backlight_ops|
2864 block_device_operations|
2865 dentry_operations|
2866 dev_pm_ops|
2867 dma_map_ops|
2868 extent_io_ops|
2869 file_lock_operations|
2870 file_operations|
2871 hv_ops|
2872 ide_dma_ops|
2873 intel_dvo_dev_ops|
2874 item_operations|
2875 iwl_ops|
2876 kgdb_arch|
2877 kgdb_io|
2878 kset_uevent_ops|
2879 lock_manager_operations|
2880 microcode_ops|
2881 mtrr_ops|
2882 neigh_ops|
2883 nlmsvc_binding|
2884 pci_raw_ops|
2885 pipe_buf_operations|
2886 platform_hibernation_ops|
2887 platform_suspend_ops|
2888 proto_ops|
2889 rpc_pipe_ops|
2890 seq_operations|
2891 snd_ac97_build_ops|
2892 soc_pcmcia_socket_ops|
2893 stacktrace_ops|
2894 sysfs_ops|
2895 tty_operations|
2896 usb_mon_operations|
2897 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08002898 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08002899 $line =~ /\bstruct\s+($struct_ops)\b/) {
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08002900 WARN("struct $1 should normally be const\n" .
2901 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08002902 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002903
2904# use of NR_CPUS is usually wrong
2905# ignore definitions of NR_CPUS and usage to define arrays as likely right
2906 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002907 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2908 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002909 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2910 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2911 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002912 {
2913 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2914 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002915
2916# check for %L{u,d,i} in strings
2917 my $string;
2918 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2919 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07002920 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002921 if ($string =~ /(?<!%)%L[udi]/) {
2922 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2923 last;
2924 }
2925 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002926
2927# whine mightly about in_atomic
2928 if ($line =~ /\bin_atomic\s*\(/) {
2929 if ($realfile =~ m@^drivers/@) {
2930 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08002931 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002932 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2933 }
2934 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01002935
2936# check for lockdep_set_novalidate_class
2937 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2938 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2939 if ($realfile !~ m@^kernel/lockdep@ &&
2940 $realfile !~ m@^include/linux/lockdep@ &&
2941 $realfile !~ m@^drivers/base/core@) {
2942 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2943 }
2944 }
Dave Jones88f88312011-01-12 16:59:59 -08002945
2946 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
2947 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
2948 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
2949 }
Dave Jones309c00c2011-03-22 16:34:44 -07002950
2951 # Check for memset with swapped arguments
2952 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
2953 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
2954 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002955 }
2956
2957 # If we have no input at all, then there is nothing to report on
2958 # so just keep quiet.
2959 if ($#rawlines == -1) {
2960 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002961 }
2962
Andy Whitcroft8905a672007-11-28 16:21:06 -08002963 # In mailback mode only produce a report in the negative, for
2964 # things that appear to be patches.
2965 if ($mailback && ($clean == 1 || !$is_patch)) {
2966 exit(0);
2967 }
2968
2969 # This is not a patch, and we are are in 'no-patch' mode so
2970 # just keep quiet.
2971 if (!$chk_patch && !$is_patch) {
2972 exit(0);
2973 }
2974
2975 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002976 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002977 }
2978 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002979 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002980 }
2981
Andy Whitcroft8905a672007-11-28 16:21:06 -08002982 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002983 if ($summary && !($clean == 1 && $quiet == 1)) {
2984 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002985 print "total: $cnt_error errors, $cnt_warn warnings, " .
2986 (($check)? "$cnt_chk checks, " : "") .
2987 "$cnt_lines lines checked\n";
2988 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002989 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002990
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002991 if ($quiet == 0) {
2992 # If there were whitespace errors which cleanpatch can fix
2993 # then suggest that.
2994 if ($rpt_cleaners) {
2995 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2996 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07002997 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002998 }
2999 }
3000
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003001 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003002 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003003 }
3004 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003005 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003006 print "are false positives report them to the maintainer, see\n";
3007 print "CHECKPATCH in MAINTAINERS.\n";
3008 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003009
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003010 return $clean;
3011}