blob: b14f830cb0fa30003f3bb8f711e7962bd93d9727 [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|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700148 __ref|
149 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700150 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800151
152# Notes to $Attribute:
153# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700154our $Attribute = qr{
155 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700156 __percpu|
157 __nocast|
158 __safe|
159 __bitwise__|
160 __packed__|
161 __packed2__|
162 __naked|
163 __maybe_unused|
164 __always_unused|
165 __noreturn|
166 __used|
167 __cold|
168 __noclone|
169 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700170 __read_mostly|
171 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800172 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700173 ____cacheline_aligned|
174 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800175 ____cacheline_internodealigned_in_smp|
176 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700177 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700178our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700179our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700180our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
181our $Lval = qr{$Ident(?:$Member)*};
182
183our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
184our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800185our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700186our $Operators = qr{
187 <=|>=|==|!=|
188 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800189 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700190 }x;
191
Andy Whitcroft8905a672007-11-28 16:21:06 -0800192our $NonptrType;
193our $Type;
194our $Declare;
195
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700196our $UTF8 = qr {
197 [\x09\x0A\x0D\x20-\x7E] # ASCII
198 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
199 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
200 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
201 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
202 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
203 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
204 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
205}x;
206
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700207our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700208 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700209 atomic_t
210)};
211
Joe Perches691e6692010-03-05 13:43:51 -0800212our $logFunctions = qr{(?x:
213 printk|
Joe Perchesb0531722011-05-24 17:13:40 -0700214 [a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
Joe Perches691e6692010-03-05 13:43:51 -0800215 WARN|
Joe Perchesb0531722011-05-24 17:13:40 -0700216 panic|
217 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800218)};
219
Joe Perches20112472011-07-25 17:13:23 -0700220our $signature_tags = qr{(?xi:
221 Signed-off-by:|
222 Acked-by:|
223 Tested-by:|
224 Reviewed-by:|
225 Reported-by:|
226 To:|
227 Cc:
228)};
229
Andy Whitcroft8905a672007-11-28 16:21:06 -0800230our @typeList = (
231 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700232 qr{(?:unsigned\s+)?char},
233 qr{(?:unsigned\s+)?short},
234 qr{(?:unsigned\s+)?int},
235 qr{(?:unsigned\s+)?long},
236 qr{(?:unsigned\s+)?long\s+int},
237 qr{(?:unsigned\s+)?long\s+long},
238 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800239 qr{unsigned},
240 qr{float},
241 qr{double},
242 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800243 qr{struct\s+$Ident},
244 qr{union\s+$Ident},
245 qr{enum\s+$Ident},
246 qr{${Ident}_t},
247 qr{${Ident}_handler},
248 qr{${Ident}_handler_fn},
249);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700250our @modifierList = (
251 qr{fastcall},
252);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800253
Wolfram Sang7840a942010-08-09 17:20:57 -0700254our $allowed_asm_includes = qr{(?x:
255 irq|
256 memory
257)};
258# memory.h: ARM has a custom one
259
Andy Whitcroft8905a672007-11-28 16:21:06 -0800260sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700261 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
262 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700263 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800264 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700265 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800266 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700267 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700268 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700269 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800270 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700271 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800272 }x;
273 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700274 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800275 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700276 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800277 }x;
278 $Declare = qr{(?:$Storage\s+)?$Type};
279}
280build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700281
Joe Perches7d2367a2011-07-25 17:13:22 -0700282our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
283
284our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
285our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
286
287sub deparenthesize {
288 my ($string) = @_;
289 return "" if (!defined($string));
290 $string =~ s@^\s*\(\s*@@g;
291 $string =~ s@\s*\)\s*$@@g;
292 $string =~ s@\s+@ @g;
293 return $string;
294}
295
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700296$chk_signoff = 0 if ($file);
297
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700298my @dep_includes = ();
299my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700300my $removal = "Documentation/feature-removal-schedule.txt";
301if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800302 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700303 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800304 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700305 if (/^Check:\s+(.*\S)/) {
306 for my $entry (split(/[, ]+/, $1)) {
307 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700308 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700309
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700310 } elsif ($entry !~ m@/@) {
311 push(@dep_functions, $entry);
312 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700313 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700314 }
315 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800316 close($REMOVE);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700317}
318
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700319my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800320my @lines = ();
321my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700322for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800323 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700324 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800325 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700326 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800327 } elsif ($filename eq '-') {
328 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700329 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800330 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700331 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700332 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800333 if ($filename eq '-') {
334 $vname = 'Your patch';
335 } else {
336 $vname = $filename;
337 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800338 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700339 chomp;
340 push(@rawlines, $_);
341 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800342 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800343 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700344 $exit = 1;
345 }
346 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800347 @lines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700348}
349
350exit($exit);
351
352sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700353 my ($root) = @_;
354
355 my @tree_check = (
356 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
357 "README", "Documentation", "arch", "include", "drivers",
358 "fs", "init", "ipc", "kernel", "lib", "scripts",
359 );
360
361 foreach my $check (@tree_check) {
362 if (! -e $root . '/' . $check) {
363 return 0;
364 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700365 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700366 return 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700367}
368
Joe Perches20112472011-07-25 17:13:23 -0700369sub parse_email {
370 my ($formatted_email) = @_;
371
372 my $name = "";
373 my $address = "";
374 my $comment = "";
375
376 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
377 $name = $1;
378 $address = $2;
379 $comment = $3 if defined $3;
380 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
381 $address = $1;
382 $comment = $2 if defined $2;
383 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
384 $address = $1;
385 $comment = $2 if defined $2;
386 $formatted_email =~ s/$address.*$//;
387 $name = $formatted_email;
388 $name =~ s/^\s+|\s+$//g;
389 $name =~ s/^\"|\"$//g;
390 # If there's a name left after stripping spaces and
391 # leading quotes, and the address doesn't have both
392 # leading and trailing angle brackets, the address
393 # is invalid. ie:
394 # "joe smith joe@smith.com" bad
395 # "joe smith <joe@smith.com" bad
396 if ($name ne "" && $address !~ /^<[^>]+>$/) {
397 $name = "";
398 $address = "";
399 $comment = "";
400 }
401 }
402
403 $name =~ s/^\s+|\s+$//g;
404 $name =~ s/^\"|\"$//g;
405 $address =~ s/^\s+|\s+$//g;
406 $address =~ s/^\<|\>$//g;
407
408 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
409 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
410 $name = "\"$name\"";
411 }
412
413 return ($name, $address, $comment);
414}
415
416sub format_email {
417 my ($name, $address) = @_;
418
419 my $formatted_email;
420
421 $name =~ s/^\s+|\s+$//g;
422 $name =~ s/^\"|\"$//g;
423 $address =~ s/^\s+|\s+$//g;
424
425 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
426 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
427 $name = "\"$name\"";
428 }
429
430 if ("$name" eq "") {
431 $formatted_email = "$address";
432 } else {
433 $formatted_email = "$name <$address>";
434 }
435
436 return $formatted_email;
437}
438
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700439sub expand_tabs {
440 my ($str) = @_;
441
442 my $res = '';
443 my $n = 0;
444 for my $c (split(//, $str)) {
445 if ($c eq "\t") {
446 $res .= ' ';
447 $n++;
448 for (; ($n % 8) != 0; $n++) {
449 $res .= ' ';
450 }
451 next;
452 }
453 $res .= $c;
454 $n++;
455 }
456
457 return $res;
458}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700459sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700460 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700461 return $res;
462}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700463
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700464sub line_stats {
465 my ($line) = @_;
466
467 # Drop the diff line leader and expand tabs
468 $line =~ s/^.//;
469 $line = expand_tabs($line);
470
471 # Pick the indent from the front of the line.
472 my ($white) = ($line =~ /^(\s*)/);
473
474 return (length($line), length($white));
475}
476
Andy Whitcroft773647a2008-03-28 14:15:58 -0700477my $sanitise_quote = '';
478
479sub sanitise_line_reset {
480 my ($in_comment) = @_;
481
482 if ($in_comment) {
483 $sanitise_quote = '*/';
484 } else {
485 $sanitise_quote = '';
486 }
487}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700488sub sanitise_line {
489 my ($line) = @_;
490
491 my $res = '';
492 my $l = '';
493
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800494 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700495 my $off = 0;
496 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700497
Andy Whitcroft773647a2008-03-28 14:15:58 -0700498 # Always copy over the diff marker.
499 $res = substr($line, 0, 1);
500
501 for ($off = 1; $off < length($line); $off++) {
502 $c = substr($line, $off, 1);
503
504 # Comments we are wacking completly including the begin
505 # and end, all to $;.
506 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
507 $sanitise_quote = '*/';
508
509 substr($res, $off, 2, "$;$;");
510 $off++;
511 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800512 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700513 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700514 $sanitise_quote = '';
515 substr($res, $off, 2, "$;$;");
516 $off++;
517 next;
518 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700519 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
520 $sanitise_quote = '//';
521
522 substr($res, $off, 2, $sanitise_quote);
523 $off++;
524 next;
525 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700526
527 # A \ in a string means ignore the next character.
528 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
529 $c eq "\\") {
530 substr($res, $off, 2, 'XX');
531 $off++;
532 next;
533 }
534 # Regular quotes.
535 if ($c eq "'" || $c eq '"') {
536 if ($sanitise_quote eq '') {
537 $sanitise_quote = $c;
538
539 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700540 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700541 } elsif ($sanitise_quote eq $c) {
542 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700543 }
544 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700545
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800546 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700547 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
548 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700549 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
550 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700551 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
552 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700553 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700554 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700555 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800556 }
557
Daniel Walker113f04a2009-09-21 17:04:35 -0700558 if ($sanitise_quote eq '//') {
559 $sanitise_quote = '';
560 }
561
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800562 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700563 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800564 my $clean = 'X' x length($1);
565 $res =~ s@\<.*\>@<$clean>@;
566
567 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700568 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800569 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700570 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800571 }
572
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700573 return $res;
574}
575
Andy Whitcroft8905a672007-11-28 16:21:06 -0800576sub ctx_statement_block {
577 my ($linenr, $remain, $off) = @_;
578 my $line = $linenr - 1;
579 my $blk = '';
580 my $soff = $off;
581 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700582 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800583
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800584 my $loff = 0;
585
Andy Whitcroft8905a672007-11-28 16:21:06 -0800586 my $type = '';
587 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800588 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800589 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800590 my $c;
591 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800592
593 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800594 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800595 @stack = (['', 0]) if ($#stack == -1);
596
Andy Whitcroft773647a2008-03-28 14:15:58 -0700597 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800598 # If we are about to drop off the end, pull in more
599 # context.
600 if ($off >= $len) {
601 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700602 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800603 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800604 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800605 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800606 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800607 $len = length($blk);
608 $line++;
609 last;
610 }
611 # Bail if there is no further context.
612 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800613 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800614 last;
615 }
616 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800617 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800618 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800619 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800620
Andy Whitcroft773647a2008-03-28 14:15:58 -0700621 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800622
623 # Handle nested #if/#else.
624 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
625 push(@stack, [ $type, $level ]);
626 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
627 ($type, $level) = @{$stack[$#stack - 1]};
628 } elsif ($remainder =~ /^#\s*endif\b/) {
629 ($type, $level) = @{pop(@stack)};
630 }
631
Andy Whitcroft8905a672007-11-28 16:21:06 -0800632 # Statement ends at the ';' or a close '}' at the
633 # outermost level.
634 if ($level == 0 && $c eq ';') {
635 last;
636 }
637
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800638 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700639 if ($level == 0 && $coff_set == 0 &&
640 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
641 $remainder =~ /^(else)(?:\s|{)/ &&
642 $remainder !~ /^else\s+if\b/) {
643 $coff = $off + length($1) - 1;
644 $coff_set = 1;
645 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
646 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800647 }
648
Andy Whitcroft8905a672007-11-28 16:21:06 -0800649 if (($type eq '' || $type eq '(') && $c eq '(') {
650 $level++;
651 $type = '(';
652 }
653 if ($type eq '(' && $c eq ')') {
654 $level--;
655 $type = ($level != 0)? '(' : '';
656
657 if ($level == 0 && $coff < $soff) {
658 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700659 $coff_set = 1;
660 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800661 }
662 }
663 if (($type eq '' || $type eq '{') && $c eq '{') {
664 $level++;
665 $type = '{';
666 }
667 if ($type eq '{' && $c eq '}') {
668 $level--;
669 $type = ($level != 0)? '{' : '';
670
671 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700672 if (substr($blk, $off + 1, 1) eq ';') {
673 $off++;
674 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800675 last;
676 }
677 }
678 $off++;
679 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700680 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800681 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700682 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800683 $line++;
684 $remain--;
685 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800686
687 my $statement = substr($blk, $soff, $off - $soff + 1);
688 my $condition = substr($blk, $soff, $coff - $soff + 1);
689
690 #warn "STATEMENT<$statement>\n";
691 #warn "CONDITION<$condition>\n";
692
Andy Whitcroft773647a2008-03-28 14:15:58 -0700693 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800694
695 return ($statement, $condition,
696 $line, $remain + 1, $off - $loff + 1, $level);
697}
698
Andy Whitcroftcf655042008-03-04 14:28:20 -0800699sub statement_lines {
700 my ($stmt) = @_;
701
702 # Strip the diff line prefixes and rip blank lines at start and end.
703 $stmt =~ s/(^|\n)./$1/g;
704 $stmt =~ s/^\s*//;
705 $stmt =~ s/\s*$//;
706
707 my @stmt_lines = ($stmt =~ /\n/g);
708
709 return $#stmt_lines + 2;
710}
711
712sub statement_rawlines {
713 my ($stmt) = @_;
714
715 my @stmt_lines = ($stmt =~ /\n/g);
716
717 return $#stmt_lines + 2;
718}
719
720sub statement_block_size {
721 my ($stmt) = @_;
722
723 $stmt =~ s/(^|\n)./$1/g;
724 $stmt =~ s/^\s*{//;
725 $stmt =~ s/}\s*$//;
726 $stmt =~ s/^\s*//;
727 $stmt =~ s/\s*$//;
728
729 my @stmt_lines = ($stmt =~ /\n/g);
730 my @stmt_statements = ($stmt =~ /;/g);
731
732 my $stmt_lines = $#stmt_lines + 2;
733 my $stmt_statements = $#stmt_statements + 1;
734
735 if ($stmt_lines > $stmt_statements) {
736 return $stmt_lines;
737 } else {
738 return $stmt_statements;
739 }
740}
741
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800742sub ctx_statement_full {
743 my ($linenr, $remain, $off) = @_;
744 my ($statement, $condition, $level);
745
746 my (@chunks);
747
Andy Whitcroftcf655042008-03-04 14:28:20 -0800748 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800749 ($statement, $condition, $linenr, $remain, $off, $level) =
750 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700751 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800752 push(@chunks, [ $condition, $statement ]);
753 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
754 return ($level, $linenr, @chunks);
755 }
756
757 # Pull in the following conditional/block pairs and see if they
758 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800759 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800760 ($statement, $condition, $linenr, $remain, $off, $level) =
761 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800762 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700763 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800764 #print "C: push\n";
765 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800766 }
767
768 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800769}
770
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700771sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700772 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700773 my $line;
774 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700775 my $blk = '';
776 my @o;
777 my @c;
778 my @res = ();
779
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700780 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800781 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700782 for ($line = $start; $remain > 0; $line++) {
783 next if ($rawlines[$line] =~ /^-/);
784 $remain--;
785
786 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800787
788 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700789 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800790 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700791 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800792 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700793 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800794 $level = pop(@stack);
795 }
796
Andy Whitcroft01464f32010-10-26 14:23:19 -0700797 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700798 ##print "C<$c>L<$level><$open$close>O<$off>\n";
799 if ($off > 0) {
800 $off--;
801 next;
802 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700803
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700804 if ($c eq $close && $level > 0) {
805 $level--;
806 last if ($level == 0);
807 } elsif ($c eq $open) {
808 $level++;
809 }
810 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700811
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700812 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700813 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700814 }
815
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700816 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700817 }
818
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700819 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700820}
821sub ctx_block_outer {
822 my ($linenr, $remain) = @_;
823
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700824 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
825 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700826}
827sub ctx_block {
828 my ($linenr, $remain) = @_;
829
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700830 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
831 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700832}
833sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700834 my ($linenr, $remain, $off) = @_;
835
836 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
837 return @r;
838}
839sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700840 my ($linenr, $remain) = @_;
841
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700842 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700843}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700844sub ctx_statement_level {
845 my ($linenr, $remain, $off) = @_;
846
847 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
848}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700849
850sub ctx_locate_comment {
851 my ($first_line, $end_line) = @_;
852
853 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700854 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700855 return $current_comment if (defined $current_comment);
856
857 # Look through the context and try and figure out if there is a
858 # comment.
859 my $in_comment = 0;
860 $current_comment = '';
861 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700862 my $line = $rawlines[$linenr - 1];
863 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700864 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
865 $in_comment = 1;
866 }
867 if ($line =~ m@/\*@) {
868 $in_comment = 1;
869 }
870 if (!$in_comment && $current_comment ne '') {
871 $current_comment = '';
872 }
873 $current_comment .= $line . "\n" if ($in_comment);
874 if ($line =~ m@\*/@) {
875 $in_comment = 0;
876 }
877 }
878
879 chomp($current_comment);
880 return($current_comment);
881}
882sub ctx_has_comment {
883 my ($first_line, $end_line) = @_;
884 my $cmt = ctx_locate_comment($first_line, $end_line);
885
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700886 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700887 ##print "CMMT: $cmt\n";
888
889 return ($cmt ne '');
890}
891
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700892sub raw_line {
893 my ($linenr, $cnt) = @_;
894
895 my $offset = $linenr - 1;
896 $cnt++;
897
898 my $line;
899 while ($cnt) {
900 $line = $rawlines[$offset++];
901 next if (defined($line) && $line =~ /^-/);
902 $cnt--;
903 }
904
905 return $line;
906}
907
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700908sub cat_vet {
909 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700910 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700911
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700912 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700913 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
914 $res .= $1;
915 if ($2 ne '') {
916 $coded = sprintf("^%c", unpack('C', $2) + 64);
917 $res .= $coded;
918 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700919 }
920 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700921
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700922 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700923}
924
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800925my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800926my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800927my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700928my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800929
930sub annotate_reset {
931 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800932 $av_pending = '_';
933 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700934 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800935}
936
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700937sub annotate_values {
938 my ($stream, $type) = @_;
939
940 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700941 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700942 my $cur = $stream;
943
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800944 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700945
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700946 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700947 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800948 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700949 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700950 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800951 print "WS($1)\n" if ($dbg_values > 1);
952 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800953 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800954 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700955 }
956
Florian Micklerc023e4732011-01-12 16:59:58 -0800957 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -0700958 print "CAST($1)\n" if ($dbg_values > 1);
959 push(@av_paren_type, $type);
960 $type = 'C';
961
Andy Whitcrofte91b6e22010-10-26 14:23:11 -0700962 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800963 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700964 $type = 'T';
965
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700966 } elsif ($cur =~ /^($Modifier)\s*/) {
967 print "MODIFIER($1)\n" if ($dbg_values > 1);
968 $type = 'T';
969
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700970 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700971 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800972 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700973 push(@av_paren_type, $type);
974 if ($2 ne '') {
975 $av_pending = 'N';
976 }
977 $type = 'E';
978
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700979 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700980 print "UNDEF($1)\n" if ($dbg_values > 1);
981 $av_preprocessor = 1;
982 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700983
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700984 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800985 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800986 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800987
988 push(@av_paren_type, $type);
989 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700990 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800991
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700992 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800993 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
994 $av_preprocessor = 1;
995
996 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
997
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700998 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800999
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001000 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001001 print "PRE_END($1)\n" if ($dbg_values > 1);
1002
1003 $av_preprocessor = 1;
1004
1005 # Assume all arms of the conditional end as this
1006 # one does, and continue as if the #endif was not here.
1007 pop(@av_paren_type);
1008 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001009 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001010
1011 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001012 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001013
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001014 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1015 print "ATTR($1)\n" if ($dbg_values > 1);
1016 $av_pending = $type;
1017 $type = 'N';
1018
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001019 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001020 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001021 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001022 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001023 }
1024 $type = 'N';
1025
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001026 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001027 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001028 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001029 $type = 'N';
1030
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001031 } elsif ($cur =~/^(case)/o) {
1032 print "CASE($1)\n" if ($dbg_values > 1);
1033 $av_pend_colon = 'C';
1034 $type = 'N';
1035
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001036 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001037 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001038 $type = 'N';
1039
1040 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001041 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001042 push(@av_paren_type, $av_pending);
1043 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001044 $type = 'N';
1045
1046 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001047 my $new_type = pop(@av_paren_type);
1048 if ($new_type ne '_') {
1049 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001050 print "PAREN('$1') -> $type\n"
1051 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001052 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001053 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001054 }
1055
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001056 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001057 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001058 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001059 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001060
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001061 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1062 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001063 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001064 } elsif ($type eq 'E') {
1065 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001066 }
1067 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1068 $type = 'V';
1069
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001070 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001071 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001072 $type = 'V';
1073
1074 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001075 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001076 $type = 'N';
1077
Andy Whitcroftcf655042008-03-04 14:28:20 -08001078 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001079 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001080 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001081 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001082
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001083 } elsif ($cur =~/^(,)/) {
1084 print "COMMA($1)\n" if ($dbg_values > 1);
1085 $type = 'C';
1086
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001087 } elsif ($cur =~ /^(\?)/o) {
1088 print "QUESTION($1)\n" if ($dbg_values > 1);
1089 $type = 'N';
1090
1091 } elsif ($cur =~ /^(:)/o) {
1092 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1093
1094 substr($var, length($res), 1, $av_pend_colon);
1095 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1096 $type = 'E';
1097 } else {
1098 $type = 'N';
1099 }
1100 $av_pend_colon = 'O';
1101
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001102 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001103 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001104 $type = 'N';
1105
Andy Whitcroft0d413862008-10-15 22:02:16 -07001106 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001107 my $variant;
1108
1109 print "OPV($1)\n" if ($dbg_values > 1);
1110 if ($type eq 'V') {
1111 $variant = 'B';
1112 } else {
1113 $variant = 'U';
1114 }
1115
1116 substr($var, length($res), 1, $variant);
1117 $type = 'N';
1118
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001119 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001120 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001121 if ($1 ne '++' && $1 ne '--') {
1122 $type = 'N';
1123 }
1124
1125 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001126 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001127 }
1128 if (defined $1) {
1129 $cur = substr($cur, length($1));
1130 $res .= $type x length($1);
1131 }
1132 }
1133
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001134 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001135}
1136
Andy Whitcroft8905a672007-11-28 16:21:06 -08001137sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001138 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001139 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001140 ^(?:
1141 $Modifier|
1142 $Storage|
1143 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001144 DEFINE_\S+
1145 )$|
1146 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001147 goto|
1148 return|
1149 case|
1150 else|
1151 asm|__asm__|
1152 do
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001153 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001154 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001155 )}x;
1156 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1157 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001158 # Check for modifiers.
1159 $possible =~ s/\s*$Storage\s*//g;
1160 $possible =~ s/\s*$Sparse\s*//g;
1161 if ($possible =~ /^\s*$/) {
1162
1163 } elsif ($possible =~ /\s/) {
1164 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001165 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001166 if ($modifier !~ $notPermitted) {
1167 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1168 push(@modifierList, $modifier);
1169 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001170 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001171
1172 } else {
1173 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1174 push(@typeList, $possible);
1175 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001176 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001177 } else {
1178 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001179 }
1180}
1181
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001182my $prefix = '';
1183
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001184sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001185 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1186 return 0;
1187 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001188 my $line = $prefix . $_[0];
1189
1190 $line = (split('\n', $line))[0] . "\n" if ($terse);
1191
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001192 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001193
1194 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001195}
1196sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001197 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001198}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001199sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001200 if (report("ERROR: $_[0]\n")) {
1201 our $clean = 0;
1202 our $cnt_error++;
1203 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001204}
1205sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001206 if (report("WARNING: $_[0]\n")) {
1207 our $clean = 0;
1208 our $cnt_warn++;
1209 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001210}
1211sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001212 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001213 our $clean = 0;
1214 our $cnt_chk++;
1215 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001216}
1217
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001218sub check_absolute_file {
1219 my ($absolute, $herecurr) = @_;
1220 my $file = $absolute;
1221
1222 ##print "absolute<$absolute>\n";
1223
1224 # See if any suffix of this path is a path within the tree.
1225 while ($file =~ s@^[^/]*/@@) {
1226 if (-f "$root/$file") {
1227 ##print "file<$file>\n";
1228 last;
1229 }
1230 }
1231 if (! -f _) {
1232 return 0;
1233 }
1234
1235 # It is, so see if the prefix is acceptable.
1236 my $prefix = $absolute;
1237 substr($prefix, -length($file)) = '';
1238
1239 ##print "prefix<$prefix>\n";
1240 if ($prefix ne ".../") {
1241 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1242 }
1243}
1244
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001245sub process {
1246 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001247
1248 my $linenr=0;
1249 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001250 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001251 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001252 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001253
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001254 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001255 my $indent;
1256 my $previndent=0;
1257 my $stashindent=0;
1258
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001259 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001260 my $signoff = 0;
1261 my $is_patch = 0;
1262
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001263 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001264 our $cnt_lines = 0;
1265 our $cnt_error = 0;
1266 our $cnt_warn = 0;
1267 our $cnt_chk = 0;
1268
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001269 # Trace the real file/line as we go.
1270 my $realfile = '';
1271 my $realline = 0;
1272 my $realcnt = 0;
1273 my $here = '';
1274 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001275 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001276 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001277 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001278
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001279 my $prev_values = 'E';
1280
1281 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001282 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001283 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001284 my %suppress_export;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001285
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001286 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001287 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001288 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001289 my @setup_docs = ();
1290 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001291
1292 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001293 my $line;
1294 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001295 $linenr++;
1296 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001297
Andy Whitcroft773647a2008-03-28 14:15:58 -07001298 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001299 $setup_docs = 0;
1300 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1301 $setup_docs = 1;
1302 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001303 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001304 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001305 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1306 $realline=$1-1;
1307 if (defined $2) {
1308 $realcnt=$3+1;
1309 } else {
1310 $realcnt=1+1;
1311 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001312 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001313
1314 # Guestimate if this is a continuing comment. Run
1315 # the context looking for a comment "edge". If this
1316 # edge is a close comment then we must be in a comment
1317 # at context start.
1318 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001319 my $cnt = $realcnt;
1320 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1321 next if (defined $rawlines[$ln - 1] &&
1322 $rawlines[$ln - 1] =~ /^-/);
1323 $cnt--;
1324 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001325 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001326 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1327 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1328 ($edge) = $1;
1329 last;
1330 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001331 }
1332 if (defined $edge && $edge eq '*/') {
1333 $in_comment = 1;
1334 }
1335
1336 # Guestimate if this is a continuing comment. If this
1337 # is the start of a diff block and this line starts
1338 # ' *' then it is very likely a comment.
1339 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001340 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001341 {
1342 $in_comment = 1;
1343 }
1344
1345 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1346 sanitise_line_reset($in_comment);
1347
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001348 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001349 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001350 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001351 $line = sanitise_line($rawline);
1352 }
1353 push(@lines, $line);
1354
1355 if ($realcnt > 1) {
1356 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1357 } else {
1358 $realcnt = 0;
1359 }
1360
1361 #print "==>$rawline\n";
1362 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001363
1364 if ($setup_docs && $line =~ /^\+/) {
1365 push(@setup_docs, $line);
1366 }
1367 }
1368
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001369 $prefix = '';
1370
Andy Whitcroft773647a2008-03-28 14:15:58 -07001371 $realcnt = 0;
1372 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001373 foreach my $line (@lines) {
1374 $linenr++;
1375
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001376 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001377
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001378#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001379 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001380 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001381 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001382 $realline=$1-1;
1383 if (defined $2) {
1384 $realcnt=$3+1;
1385 } else {
1386 $realcnt=1+1;
1387 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001388 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001389 $prev_values = 'E';
1390
Andy Whitcroft773647a2008-03-28 14:15:58 -07001391 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001392 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001393 %suppress_export = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001394 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001395
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001396# track the line number as we move through the hunk, note that
1397# new versions of GNU diff omit the leading space on completely
1398# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001399 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001400 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001401 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001402
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001403 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001404 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001405
1406 # Track the previous line.
1407 ($prevline, $stashline) = ($stashline, $line);
1408 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001409 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1410
Andy Whitcroft773647a2008-03-28 14:15:58 -07001411 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001412
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001413 } elsif ($realcnt == 1) {
1414 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001415 }
1416
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001417 my $hunk_line = ($realcnt != 0);
1418
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001419#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001420 $prefix = "$filename:$realline: " if ($emacs && $file);
1421 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1422
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001423 $here = "#$linenr: " if (!$file);
1424 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001425
1426 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001427 if ($line =~ /^diff --git.*?(\S+)$/) {
1428 $realfile = $1;
1429 $realfile =~ s@^([^/]*)/@@;
1430
1431 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001432 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001433 $realfile =~ s@^([^/]*)/@@;
1434
1435 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001436 if (!$file && $tree && $p1_prefix ne '' &&
1437 -e "$root/$p1_prefix") {
Wolfram Sang1e855722009-01-06 14:41:24 -08001438 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1439 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001440
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001441 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001442 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1443 }
1444 next;
1445 }
1446
Randy Dunlap389834b2007-06-08 13:47:03 -07001447 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001448
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001449 my $hereline = "$here\n$rawline\n";
1450 my $herecurr = "$here\n$rawline\n";
1451 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001452
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001453 $cnt_lines++ if ($realcnt != 0);
1454
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001455# Check for incorrect file permissions
1456 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1457 my $permhere = $here . "FILE: $realfile\n";
1458 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1459 ERROR("do not set execute permissions for source files\n" . $permhere);
1460 }
1461 }
1462
Joe Perches20112472011-07-25 17:13:23 -07001463# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001464 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001465 $signoff++;
Joe Perches20112472011-07-25 17:13:23 -07001466 }
1467
1468# Check signature styles
1469 if ($line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1470 my $space_before = $1;
1471 my $sign_off = $2;
1472 my $space_after = $3;
1473 my $email = $4;
1474 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1475
1476 if (defined $space_before && $space_before ne "") {
1477 WARN("Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001478 }
Joe Perches20112472011-07-25 17:13:23 -07001479 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1480 WARN("'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
1481 }
1482 if (!defined $space_after || $space_after ne " ") {
1483 WARN("Use a single space after $ucfirst_sign_off\n" . $herecurr);
1484 }
1485
1486 my ($email_name, $email_address, $comment) = parse_email($email);
1487 my $suggested_email = format_email(($email_name, $email_address));
1488 if ($suggested_email eq "") {
1489 ERROR("Unrecognized email address: '$email'\n" . $herecurr);
1490 } else {
1491 my $dequoted = $suggested_email;
1492 $dequoted =~ s/^"//;
1493 $dequoted =~ s/" </ </;
1494 # Don't force email to have quotes
1495 # Allow just an angle bracketed address
1496 if ("$dequoted$comment" ne $email &&
1497 "<$email_address>$comment" ne $email &&
1498 "$suggested_email$comment" ne $email) {
1499 WARN("email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1500 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001501 }
1502 }
1503
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001504# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001505 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001506 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001507 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001508 }
1509
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001510# Check for absolute kernel paths.
1511 if ($tree) {
1512 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1513 my $file = $1;
1514
1515 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1516 check_absolute_file($1, $herecurr)) {
1517 #
1518 } else {
1519 check_absolute_file($file, $herecurr);
1520 }
1521 }
1522 }
1523
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001524# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1525 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001526 $rawline !~ m/^$UTF8*$/) {
1527 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1528
1529 my $blank = copy_spacing($rawline);
1530 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1531 my $hereptr = "$hereline$ptr\n";
1532
1533 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001534 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001535
Andy Whitcroft306708542008-10-15 22:02:28 -07001536# ignore non-hunk lines and lines being removed
1537 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001538
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001539#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001540 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001541 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001542 ERROR("DOS line endings\n" . $herevet);
1543
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001544 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1545 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001546 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001547 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001548 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001549
Andi Kleen33549572010-05-24 14:33:29 -07001550# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001551# Only applies when adding the entry originally, after that we do not have
1552# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001553 if ($realfile =~ /Kconfig/ &&
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001554 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
Andi Kleen33549572010-05-24 14:33:29 -07001555 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001556 my $cnt = $realcnt;
1557 my $ln = $linenr + 1;
1558 my $f;
1559 my $is_end = 0;
1560 while ($cnt > 0 && defined $lines[$ln - 1]) {
1561 $f = $lines[$ln - 1];
1562 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1563 $is_end = $lines[$ln - 1] =~ /^\+/;
1564 $ln++;
1565
1566 next if ($f =~ /^-/);
1567 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001568 $f =~ s/#.*//;
1569 $f =~ s/^\s+//;
1570 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001571 if ($f =~ /^\s*config\s/) {
1572 $is_end = 1;
1573 last;
1574 }
Andi Kleen33549572010-05-24 14:33:29 -07001575 $length++;
1576 }
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001577 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1578 #print "is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001579 }
1580
Andy Whitcroft5368df202008-10-15 22:02:27 -07001581# check we are in a valid source file if not then ignore this hunk
1582 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1583
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001584#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001585 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001586 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001587 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001588 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001589 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001590 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001591 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001592 }
1593
Joe Perches5e79d962010-03-05 13:43:55 -08001594# check for spaces before a quoted newline
1595 if ($rawline =~ /^.*\".*\s\\n/) {
1596 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1597 }
1598
Andy Whitcroft8905a672007-11-28 16:21:06 -08001599# check for adding lines without a newline.
1600 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1601 WARN("adding a line without newline at end of file\n" . $herecurr);
1602 }
1603
Mike Frysinger42e41c52009-09-21 17:04:40 -07001604# Blackfin: use hi/lo macros
1605 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1606 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1607 my $herevet = "$here\n" . cat_vet($line) . "\n";
1608 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1609 }
1610 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1611 my $herevet = "$here\n" . cat_vet($line) . "\n";
1612 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1613 }
1614 }
1615
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001616# check we are in a valid source file C or perl if not then ignore this hunk
1617 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001618
1619# at the beginning of a line any tabs must come first and anything
1620# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001621 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1622 $rawline =~ /^\+\s* \s*/) {
1623 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001624 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001625 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001626 }
1627
Alberto Panizzo08e44362010-03-05 13:43:54 -08001628# check for space before tabs.
1629 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1630 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1631 WARN("please, no space before tabs\n" . $herevet);
1632 }
1633
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001634# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001635# Exceptions:
1636# 1) within comments
1637# 2) indented preprocessor commands
1638# 3) hanging labels
1639 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001640 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001641 WARN("please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001642 }
1643
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001644# check we are in a valid C source file if not then ignore this hunk
1645 next if ($realfile !~ /\.(h|c)$/);
1646
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001647# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001648 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001649 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1650 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001651
Mike Frysinger42e41c52009-09-21 17:04:40 -07001652# Blackfin: don't use __builtin_bfin_[cs]sync
1653 if ($line =~ /__builtin_bfin_csync/) {
1654 my $herevet = "$here\n" . cat_vet($line) . "\n";
1655 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1656 }
1657 if ($line =~ /__builtin_bfin_ssync/) {
1658 my $herevet = "$here\n" . cat_vet($line) . "\n";
1659 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1660 }
1661
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001662# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001663 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1664 $realline_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001665 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001666 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001667 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001668 $stat =~ s/\n./\n /g;
1669 $cond =~ s/\n./\n /g;
1670
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001671 # Find the real next line.
1672 $realline_next = $line_nr_next;
1673 if (defined $realline_next &&
1674 (!defined $lines[$realline_next - 1] ||
1675 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1676 $realline_next++;
1677 }
1678
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001679 my $s = $stat;
1680 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001681
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001682 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001683 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001684
1685 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001686 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001687
Andy Whitcroft463f2862009-09-21 17:04:34 -07001688 } elsif ($s =~ /^.\s*else\b/s) {
1689
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001690 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001691 } 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 -07001692 my $type = $1;
1693 $type =~ s/\s+/ /g;
1694 possible($type, "A:" . $s);
1695
Andy Whitcroft8905a672007-11-28 16:21:06 -08001696 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07001697 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001698 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001699 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001700
1701 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001702 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001703 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001704 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001705
1706 # Check for any sort of function declaration.
1707 # int foo(something bar, other baz);
1708 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001709 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 -08001710 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001711
Andy Whitcroftcf655042008-03-04 14:28:20 -08001712 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001713 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001714 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001715
Andy Whitcroft8905a672007-11-28 16:21:06 -08001716 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001717 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001718
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001719 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001720 }
1721 }
1722 }
1723
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001724 }
1725
Andy Whitcroft653d4872007-06-23 17:16:34 -07001726#
1727# Checks which may be anchored in the context.
1728#
1729
1730# Check for switch () and associated case and default
1731# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001732 if ($line=~/\bswitch\s*\(.*\)/) {
1733 my $err = '';
1734 my $sep = '';
1735 my @ctx = ctx_block_outer($linenr, $realcnt);
1736 shift(@ctx);
1737 for my $ctx (@ctx) {
1738 my ($clen, $cindent) = line_stats($ctx);
1739 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1740 $indent != $cindent) {
1741 $err .= "$sep$ctx\n";
1742 $sep = '';
1743 } else {
1744 $sep = "[...]\n";
1745 }
1746 }
1747 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001748 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001749 }
1750 }
1751
1752# if/while/etc brace do not go on next line, unless defining a do while loop,
1753# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001754 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001755 my $pre_ctx = "$1$2";
1756
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001757 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001758 my $ctx_cnt = $realcnt - $#ctx - 1;
1759 my $ctx = join("\n", @ctx);
1760
Andy Whitcroft548596d2008-07-23 21:29:01 -07001761 my $ctx_ln = $linenr;
1762 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001763
Andy Whitcroft548596d2008-07-23 21:29:01 -07001764 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1765 defined $lines[$ctx_ln - 1] &&
1766 $lines[$ctx_ln - 1] =~ /^-/)) {
1767 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1768 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001769 $ctx_ln++;
1770 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001771
Andy Whitcroft53210162008-07-23 21:29:03 -07001772 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1773 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001774
1775 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1776 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001777 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001778 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001779 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1780 $ctx =~ /\)\s*\;\s*$/ &&
1781 defined $lines[$ctx_ln - 1])
1782 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001783 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1784 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001785 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001786 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001787 }
1788 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001789 }
1790
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001791# Check relative indent for conditionals and blocks.
1792 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1793 my ($s, $c) = ($stat, $cond);
1794
1795 substr($s, 0, length($c), '');
1796
1797 # Make sure we remove the line prefixes as we have
1798 # none on the first line, and are going to readd them
1799 # where necessary.
1800 $s =~ s/\n./\n/gs;
1801
1802 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001803 my @newlines = ($c =~ /\n/gs);
1804 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001805
1806 # We want to check the first line inside the block
1807 # starting at the end of the conditional, so remove:
1808 # 1) any blank line termination
1809 # 2) any opening brace { on end of the line
1810 # 3) any do (...) {
1811 my $continuation = 0;
1812 my $check = 0;
1813 $s =~ s/^.*\bdo\b//;
1814 $s =~ s/^\s*{//;
1815 if ($s =~ s/^\s*\\//) {
1816 $continuation = 1;
1817 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001818 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001819 $check = 1;
1820 $cond_lines++;
1821 }
1822
1823 # Also ignore a loop construct at the end of a
1824 # preprocessor statement.
1825 if (($prevline =~ /^.\s*#\s*define\s/ ||
1826 $prevline =~ /\\\s*$/) && $continuation == 0) {
1827 $check = 0;
1828 }
1829
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001830 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001831 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001832 while ($cond_ptr != $cond_lines) {
1833 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001834
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001835 # If we see an #else/#elif then the code
1836 # is not linear.
1837 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1838 $check = 0;
1839 }
1840
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001841 # Ignore:
1842 # 1) blank lines, they should be at 0,
1843 # 2) preprocessor lines, and
1844 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001845 if ($continuation ||
1846 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001847 $s =~ /^\s*#\s*?/ ||
1848 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001849 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001850 if ($s =~ s/^.*?\n//) {
1851 $cond_lines++;
1852 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001853 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001854 }
1855
1856 my (undef, $sindent) = line_stats("+" . $s);
1857 my $stat_real = raw_line($linenr, $cond_lines);
1858
1859 # Check if either of these lines are modified, else
1860 # this is not this patch's fault.
1861 if (!defined($stat_real) ||
1862 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1863 $check = 0;
1864 }
1865 if (defined($stat_real) && $cond_lines > 1) {
1866 $stat_real = "[...]\n$stat_real";
1867 }
1868
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001869 #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 -07001870
1871 if ($check && (($sindent % 8) != 0 ||
1872 ($sindent <= $indent && $s ne ''))) {
1873 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1874 }
1875 }
1876
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001877 # Track the 'values' across context and added lines.
1878 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001879 my ($curr_values, $curr_vars) =
1880 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001881 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001882 if ($dbg_values) {
1883 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001884 print "$linenr > .$outline\n";
1885 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001886 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001887 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001888 $prev_values = substr($curr_values, -1);
1889
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001890#ignore lines not being added
1891 if ($line=~/^[^\+]/) {next;}
1892
Andy Whitcroft653d4872007-06-23 17:16:34 -07001893# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001894 if ($dbg_type) {
1895 if ($line =~ /^.\s*$Declare\s*$/) {
1896 ERROR("TEST: is type\n" . $herecurr);
1897 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1898 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1899 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001900 next;
1901 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001902# TEST: allow direct testing of the attribute matcher.
1903 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001904 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001905 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001906 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001907 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1908 }
1909 next;
1910 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001911
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001912# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07001913 if ($line =~ /^.\s*{/ &&
1914 $prevline =~ /(?:^|[^=])=\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001915 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001916 }
1917
Andy Whitcroft653d4872007-06-23 17:16:34 -07001918#
1919# Checks which are anchored on the added line.
1920#
1921
1922# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001923 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001924 my $path = $1;
1925 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001926 ERROR("malformed #include filename\n" .
1927 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001928 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001929 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001930
1931# no C99 // comments
1932 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001933 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001934 }
1935 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001936 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001937 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001938
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001939# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1940# the whole statement.
1941#print "APW <$lines[$realline_next - 1]>\n";
1942 if (defined $realline_next &&
1943 exists $lines[$realline_next - 1] &&
1944 !defined $suppress_export{$realline_next} &&
1945 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1946 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001947 # Handle definitions which produce identifiers with
1948 # a prefix:
1949 # XXX(foo);
1950 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001951 my $name = $1;
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001952 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1953 $name =~ /^${Ident}_$2/) {
1954#print "FOO C name<$name>\n";
1955 $suppress_export{$realline_next} = 1;
1956
1957 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001958 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07001959 ^.DEFINE_$Ident\(\Q$name\E\)|
1960 ^.DECLARE_$Ident\(\Q$name\E\)|
1961 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001962 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1963 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07001964 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001965#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1966 $suppress_export{$realline_next} = 2;
1967 } else {
1968 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001969 }
1970 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001971 if (!defined $suppress_export{$linenr} &&
1972 $prevline =~ /^.\s*$/ &&
1973 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1974 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1975#print "FOO B <$lines[$linenr - 1]>\n";
1976 $suppress_export{$linenr} = 2;
1977 }
1978 if (defined $suppress_export{$linenr} &&
1979 $suppress_export{$linenr} == 2) {
1980 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1981 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001982
Joe Eloff5150bda2010-08-09 17:21:00 -07001983# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001984 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Eloff5150bda2010-08-09 17:21:00 -07001985 ERROR("do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001986 $herecurr);
1987 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001988# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001989 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001990 ERROR("do not initialise statics to 0 or NULL\n" .
1991 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001992 }
1993
Joe Perchescb710ec2010-10-26 14:23:20 -07001994# check for static const char * arrays.
1995 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1996 WARN("static const char * array should probably be static const char * const\n" .
1997 $herecurr);
1998 }
1999
2000# check for static char foo[] = "bar" declarations.
2001 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2002 WARN("static char array declaration should probably be static const char\n" .
2003 $herecurr);
2004 }
2005
Joe Perches93ed0e22010-10-26 14:23:21 -07002006# check for declarations of struct pci_device_id
2007 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2008 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2009 }
2010
Andy Whitcroft653d4872007-06-23 17:16:34 -07002011# check for new typedefs, only function parameters and sparse annotations
2012# make sense.
2013 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002014 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002015 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002016 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002017 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002018 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002019 }
2020
2021# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002022 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08002023 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002024 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002025
Andy Whitcroft65863862009-01-06 14:41:21 -08002026 # Should start with a space.
2027 $to =~ s/^(\S)/ $1/;
2028 # Should not end with a space.
2029 $to =~ s/\s+$//;
2030 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002031 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002032 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002033
Andy Whitcroft65863862009-01-06 14:41:21 -08002034 #print "from<$from> to<$to>\n";
2035 if ($from ne $to) {
2036 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2037 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08002038 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002039 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002040
Andy Whitcroft65863862009-01-06 14:41:21 -08002041 # Should start with a space.
2042 $to =~ s/^(\S)/ $1/;
2043 # Should not end with a space.
2044 $to =~ s/\s+$//;
2045 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002046 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002047 }
2048 # Modifiers should have spaces.
2049 $to =~ s/(\b$Modifier$)/$1 /;
2050
Andy Whitcroft667026e2009-02-27 14:03:08 -08002051 #print "from<$from> to<$to> ident<$ident>\n";
2052 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002053 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2054 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002055 }
2056
2057# # no BUG() or BUG_ON()
2058# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2059# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2060# print "$herecurr";
2061# $clean = 0;
2062# }
2063
Andy Whitcroft8905a672007-11-28 16:21:06 -08002064 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002065 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 -08002066 }
2067
Joe Perches17441222011-06-15 15:08:17 -07002068# check for uses of printk_ratelimit
2069 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2070 WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2071 }
2072
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002073# printk should use KERN_* levels. Note that follow on printk's on the
2074# same line do not need a level, so we use the current block context
2075# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002076# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002077# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002078 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002079 my $ok = 0;
2080 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2081 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002082 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002083 # with "\n" ignore it, else it is to blame
2084 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2085 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2086 $ok = 1;
2087 }
2088 last;
2089 }
2090 }
2091 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002092 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002093 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002094 }
2095
Andy Whitcroft653d4872007-06-23 17:16:34 -07002096# function brace can't be on same line, except for #defines of do while,
2097# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002098 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2099 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002100 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002101 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002102
Andy Whitcroft8905a672007-11-28 16:21:06 -08002103# open braces for enum, union and struct go on the same line.
2104 if ($line =~ /^.\s*{/ &&
2105 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2106 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
2107 }
2108
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002109# missing space after union, struct or enum definition
2110 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2111 WARN("missing space after $1 definition\n" . $herecurr);
2112 }
2113
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002114# check for spacing round square brackets; allowed:
2115# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002116# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2117# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002118 while ($line =~ /(.*?\s)\[/g) {
2119 my ($where, $prefix) = ($-[1], $1);
2120 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002121 ($where != 0 || $prefix !~ /^.\s+$/) &&
2122 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002123 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2124 }
2125 }
2126
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002127# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002128 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002129 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002130 my $ctx_before = substr($line, 0, $-[1]);
2131 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002132
2133 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002134 if ($name =~ /^(?:
2135 if|for|while|switch|return|case|
2136 volatile|__volatile__|
2137 __attribute__|format|__extension__|
2138 asm|__asm__)$/x)
2139 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002140
2141 # cpp #define statements have non-optional spaces, ie
2142 # if there is a space between the name and the open
2143 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002144 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002145
2146 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002147 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002148
2149 # If this whole things ends with a type its most
2150 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002151 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002152
2153 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002154 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002155 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002156 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002157# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002158 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002159 my $ops = qr{
2160 <<=|>>=|<=|>=|==|!=|
2161 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2162 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002163 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2164 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002165 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002166 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002167 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002168
2169 my $blank = copy_spacing($opline);
2170
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002171 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002172 $off += length($elements[$n]);
2173
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002174 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002175 my $ca = substr($opline, 0, $off);
2176 my $cc = '';
2177 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2178 $cc = substr($opline, $off + length($elements[$n + 1]));
2179 }
2180 my $cb = "$ca$;$cc";
2181
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002182 my $a = '';
2183 $a = 'V' if ($elements[$n] ne '');
2184 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002185 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002186 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2187 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002188 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002189
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002190 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002191
2192 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002193 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002194 $c = 'V' if ($elements[$n + 2] ne '');
2195 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002196 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002197 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2198 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002199 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002200 } else {
2201 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002202 }
2203
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002204 my $ctx = "${a}x${c}";
2205
2206 my $at = "(ctx:$ctx)";
2207
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002208 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002209 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002210
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002211 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002212 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002213
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002214 # Get the full operator variant.
2215 my $opv = $op . substr($curr_vars, $off, 1);
2216
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002217 # Ignore operators passed as parameters.
2218 if ($op_type ne 'V' &&
2219 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2220
Andy Whitcroftcf655042008-03-04 14:28:20 -08002221# # Ignore comments
2222# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002223
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002224 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002225 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002226 if ($ctx !~ /.x[WEBC]/ &&
2227 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002228 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002229 }
2230
2231 # // is a comment
2232 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002233
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002234 # No spaces for:
2235 # ->
2236 # : when part of a bitfield
2237 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002238 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002239 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002240 }
2241
2242 # , must have a space on the right.
2243 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002244 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002245 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002246 }
2247
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002248 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002249 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002250 #warn "'*' is part of type\n";
2251
2252 # unary operators should have a space before and
2253 # none after. May be left adjacent to another
2254 # unary operator, or a cast
2255 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002256 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002257 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002258 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002259 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002260 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002261 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002262 # A unary '*' may be const
2263
2264 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07002265 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002266 }
2267
2268 # unary ++ and unary -- are allowed no space on one side.
2269 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002270 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2271 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002272 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002273 if ($ctx =~ /Wx[BE]/ ||
2274 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2275 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002276 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002277 if ($ctx =~ /ExW/) {
2278 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2279 }
2280
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002281
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002282 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002283 } elsif ($op eq '<<' or $op eq '>>' or
2284 $op eq '&' or $op eq '^' or $op eq '|' or
2285 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002286 $op eq '*' or $op eq '/' or
2287 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002288 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002289 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002290 ERROR("need consistent spacing around '$op' $at\n" .
2291 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002292 }
2293
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002294 # A colon needs no spaces before when it is
2295 # terminating a case value or a label.
2296 } elsif ($opv eq ':C' || $opv eq ':L') {
2297 if ($ctx =~ /Wx./) {
2298 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2299 }
2300
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002301 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002302 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002303 my $ok = 0;
2304
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002305 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002306 if (($op eq '<' &&
2307 $cc =~ /^\S+\@\S+>/) ||
2308 ($op eq '>' &&
2309 $ca =~ /<\S+\@\S+$/))
2310 {
2311 $ok = 1;
2312 }
2313
2314 # Ignore ?:
2315 if (($opv eq ':O' && $ca =~ /\?$/) ||
2316 ($op eq '?' && $cc =~ /^:/)) {
2317 $ok = 1;
2318 }
2319
2320 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002321 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002322 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002323 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002324 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002325 }
2326 }
2327
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002328# check for multiple assignments
2329 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002330 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002331 }
2332
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002333## # check for multiple declarations, allowing for a function declaration
2334## # continuation.
2335## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2336## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2337##
2338## # Remove any bracketed sections to ensure we do not
2339## # falsly report the parameters of functions.
2340## my $ln = $line;
2341## while ($ln =~ s/\([^\(\)]*\)//g) {
2342## }
2343## if ($ln =~ /,/) {
2344## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2345## }
2346## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002347
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002348#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002349 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2350 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002351 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002352 }
2353
2354# closing brace should have a space following it when it has anything
2355# on the line
2356 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002357 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002358 }
2359
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002360# check spacing on square brackets
2361 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002362 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002363 }
2364 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002365 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002366 }
2367
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002368# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002369 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2370 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002371 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002372 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002373 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002374 $line !~ /for\s*\(.*;\s+\)/ &&
2375 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002376 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002377 }
2378
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002379#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002380 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002381 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002382 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002383 }
2384
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002385# Return is not a function.
2386 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2387 my $spacing = $1;
2388 my $value = $2;
2389
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002390 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002391 $value =~ s/\(/ \(/g;
2392 $value =~ s/\)/\) /g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002393 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2394 $value !~ /(?:$Ident|-?$Constant)\s*
2395 $Compare\s*
2396 (?:$Ident|-?$Constant)/x &&
2397 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002398 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002399#print "value<$value>\n";
2400 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002401 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2402
2403 } elsif ($spacing !~ /\s+/) {
2404 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2405 }
2406 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002407# Return of what appears to be an errno should normally be -'ve
2408 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2409 my $name = $1;
2410 if ($name ne 'EOF' && $name ne 'ERROR') {
2411 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2412 }
2413 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002414
Joe Perches7d2367a2011-07-25 17:13:22 -07002415# typecasts on min/max could be min_t/max_t
2416 if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
2417 if (defined $2 || defined $8) {
2418 my $call = $1;
2419 my $cast1 = deparenthesize($2);
2420 my $arg1 = $3;
2421 my $cast2 = deparenthesize($8);
2422 my $arg2 = $9;
2423 my $cast;
2424
2425 if ($cast1 ne "" && $cast2 ne "") {
2426 $cast = "$cast1 or $cast2";
2427 } elsif ($cast1 ne "") {
2428 $cast = $cast1;
2429 } else {
2430 $cast = $cast2;
2431 }
2432 WARN("$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
2433 }
2434 }
2435
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002436# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002437 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002438 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002439 }
2440
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002441# Check for illegal assignment in if conditional -- and check for trailing
2442# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002443 if ($line =~ /do\s*(?!{)/) {
2444 my ($stat_next) = ctx_statement_block($line_nr_next,
2445 $remain_next, $off_next);
2446 $stat_next =~ s/\n./\n /g;
2447 ##print "stat<$stat> stat_next<$stat_next>\n";
2448
2449 if ($stat_next =~ /^\s*while\b/) {
2450 # If the statement carries leading newlines,
2451 # then count those as offsets.
2452 my ($whitespace) =
2453 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2454 my $offset =
2455 statement_rawlines($whitespace) - 1;
2456
2457 $suppress_whiletrailers{$line_nr_next +
2458 $offset} = 1;
2459 }
2460 }
2461 if (!defined $suppress_whiletrailers{$linenr} &&
2462 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002463 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002464
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002465 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002466 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002467 }
2468
2469 # Find out what is on the end of the line after the
2470 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002471 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002472 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002473 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002474 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2475 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002476 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002477 # Find out how long the conditional actually is.
2478 my @newlines = ($c =~ /\n/gs);
2479 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002480 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002481
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002482 $stat_real = raw_line($linenr, $cond_lines)
2483 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002484 if (defined($stat_real) && $cond_lines > 1) {
2485 $stat_real = "[...]\n$stat_real";
2486 }
2487
2488 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002489 }
2490 }
2491
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002492# Check for bitwise tests written as boolean
2493 if ($line =~ /
2494 (?:
2495 (?:\[|\(|\&\&|\|\|)
2496 \s*0[xX][0-9]+\s*
2497 (?:\&\&|\|\|)
2498 |
2499 (?:\&\&|\|\|)
2500 \s*0[xX][0-9]+\s*
2501 (?:\&\&|\|\||\)|\])
2502 )/x)
2503 {
2504 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2505 }
2506
Andy Whitcroft8905a672007-11-28 16:21:06 -08002507# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002508 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2509 my $s = $1;
2510 $s =~ s/$;//g; # Remove any comments
2511 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2512 ERROR("trailing statements should be on next line\n" . $herecurr);
2513 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002514 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002515# if should not continue a brace
2516 if ($line =~ /}\s*if\b/) {
2517 ERROR("trailing statements should be on next line\n" .
2518 $herecurr);
2519 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002520# case and default should not have general statements after them
2521 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2522 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002523 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002524 \s*return\s+
2525 )/xg)
2526 {
2527 ERROR("trailing statements should be on next line\n" . $herecurr);
2528 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002529
2530 # Check for }<nl>else {, these must be at the same
2531 # indent level to be relevant to each other.
2532 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2533 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002534 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002535 }
2536
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002537 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2538 $previndent == $indent) {
2539 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2540
2541 # Find out what is on the end of the line after the
2542 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002543 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002544 $s =~ s/\n.*//g;
2545
2546 if ($s =~ /^\s*;/) {
2547 ERROR("while should follow close brace '}'\n" . $hereprev);
2548 }
2549 }
2550
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002551#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2552# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2553# print "No studly caps, use _\n";
2554# print "$herecurr";
2555# $clean = 0;
2556# }
2557
2558#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002559 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002560 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002561 }
2562
Andy Whitcroft653d4872007-06-23 17:16:34 -07002563#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002564 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002565 my $file = "$1.h";
2566 my $checkfile = "include/linux/$file";
2567 if (-f "$root/$checkfile" &&
2568 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002569 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002570 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002571 if ($realfile =~ m{^arch/}) {
2572 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2573 } else {
2574 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2575 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002576 }
2577 }
2578
Andy Whitcroft653d4872007-06-23 17:16:34 -07002579# multi-statement macros should be enclosed in a do while loop, grab the
2580# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002581# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07002582 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2583 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002584 my $ln = $linenr;
2585 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002586 my ($off, $dstat, $dcond, $rest);
2587 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002588
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002589 my $args = defined($1);
2590
2591 # Find the end of the macro and limit our statement
2592 # search to that.
2593 while ($cnt > 0 && defined $lines[$ln - 1] &&
2594 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2595 {
2596 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002597 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002598 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002599 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002600 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002601
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002602 ($dstat, $dcond, $ln, $cnt, $off) =
2603 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2604 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002605 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002606
2607 # Extract the remainder of the define (if any) and
2608 # rip off surrounding spaces, and trailing \'s.
2609 $rest = '';
Andy Whitcroft636d140a2008-10-15 22:02:18 -07002610 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2611 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002612 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2613 $rest .= substr($lines[$ln - 1], $off) . "\n";
2614 $cnt--;
2615 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002616 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002617 $off = 0;
2618 }
2619 $rest =~ s/\\\n.//g;
2620 $rest =~ s/^\s*//s;
2621 $rest =~ s/\s*$//s;
2622
2623 # Clean up the original statement.
2624 if ($args) {
2625 substr($dstat, 0, length($dcond), '');
2626 } else {
2627 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2628 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002629 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002630 $dstat =~ s/\\\n.//g;
2631 $dstat =~ s/^\s*//s;
2632 $dstat =~ s/\s*$//s;
2633
2634 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002635 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2636 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2637 $dstat =~ s/\[[^\{\}]*\]/1/)
2638 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002639 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002640
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002641 my $exceptions = qr{
2642 $Declare|
2643 module_param_named|
2644 MODULE_PARAM_DESC|
2645 DECLARE_PER_CPU|
2646 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002647 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002648 union|
2649 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002650 \.$Ident\s*=\s*|
2651 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002652 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07002653 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2654 if ($rest ne '' && $rest ne ',') {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002655 if ($rest !~ /while\s*\(/ &&
2656 $dstat !~ /$exceptions/)
2657 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002658 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 -07002659 }
2660
2661 } elsif ($ctx !~ /;/) {
2662 if ($dstat ne '' &&
2663 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2664 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002665 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002666 $dstat =~ /$Operators/)
2667 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002668 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002669 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002670 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002671 }
2672
Mike Frysinger080ba922009-01-06 14:41:25 -08002673# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2674# all assignments may have only one of the following with an assignment:
2675# .
2676# ALIGN(...)
2677# VMLINUX_SYMBOL(...)
2678 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2679 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2680 }
2681
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002682# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002683 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2684 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002685 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002686 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002687 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2688 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002689 my $allowed = 0;
2690 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002691 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002692 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002693 for my $chunk (@chunks) {
2694 my ($cond, $block) = @{$chunk};
2695
Andy Whitcroft773647a2008-03-28 14:15:58 -07002696 # If the condition carries leading newlines, then count those as offsets.
2697 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2698 my $offset = statement_rawlines($whitespace) - 1;
2699
2700 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2701
2702 # We have looked at and allowed this specific line.
2703 $suppress_ifbraces{$ln + $offset} = 1;
2704
2705 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002706 $ln += statement_rawlines($block) - 1;
2707
Andy Whitcroft773647a2008-03-28 14:15:58 -07002708 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002709
2710 $seen++ if ($block =~ /^\s*{/);
2711
Andy Whitcroftcf655042008-03-04 14:28:20 -08002712 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2713 if (statement_lines($cond) > 1) {
2714 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002715 $allowed = 1;
2716 }
2717 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002718 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002719 $allowed = 1;
2720 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002721 if (statement_block_size($block) > 1) {
2722 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002723 $allowed = 1;
2724 }
2725 }
2726 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002727 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002728 }
2729 }
2730 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002731 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002732 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002733 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002734
Andy Whitcroftcf655042008-03-04 14:28:20 -08002735 # Check the pre-context.
2736 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2737 #print "APW: ALLOWED: pre<$1>\n";
2738 $allowed = 1;
2739 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002740
2741 my ($level, $endln, @chunks) =
2742 ctx_statement_full($linenr, $realcnt, $-[0]);
2743
Andy Whitcroftcf655042008-03-04 14:28:20 -08002744 # Check the condition.
2745 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002746 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002747 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002748 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002749 }
2750 if (statement_lines($cond) > 1) {
2751 #print "APW: ALLOWED: cond<$cond>\n";
2752 $allowed = 1;
2753 }
2754 if ($block =~/\b(?:if|for|while)\b/) {
2755 #print "APW: ALLOWED: block<$block>\n";
2756 $allowed = 1;
2757 }
2758 if (statement_block_size($block) > 1) {
2759 #print "APW: ALLOWED: lines block<$block>\n";
2760 $allowed = 1;
2761 }
2762 # Check the post-context.
2763 if (defined $chunks[1]) {
2764 my ($cond, $block) = @{$chunks[1]};
2765 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002766 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002767 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002768 if ($block =~ /^\s*\{/) {
2769 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2770 $allowed = 1;
2771 }
2772 }
2773 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2774 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002775 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002776
Andy Whitcroftf0556632008-10-15 22:02:23 -07002777 for (my $n = 0; $n < $cnt; $n++) {
2778 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002779 }
2780
2781 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002782 }
2783 }
2784
Andy Whitcroft653d4872007-06-23 17:16:34 -07002785# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002786 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002787 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002788 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002789 }
2790 }
2791
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002792# don't use deprecated functions
2793 for my $func (@dep_functions) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002794 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002795 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002796 }
2797 }
2798
2799# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002800 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2801 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002802 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002803 }
2804
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002805# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002806 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002807 CHK("if this code is redundant consider removing it\n" .
2808 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002809 }
2810
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002811# check for needless kfree() checks
2812 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2813 my $expr = $1;
2814 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002815 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002816 }
2817 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002818# check for needless usb_free_urb() checks
2819 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2820 my $expr = $1;
2821 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2822 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2823 }
2824 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002825
Patrick Pannuto1a15a252010-08-09 17:21:01 -07002826# prefer usleep_range over udelay
2827 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2828 # ignore udelay's < 10, however
2829 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2830 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2831 }
2832 }
2833
Patrick Pannuto09ef8722010-08-09 17:21:02 -07002834# warn about unexpectedly long msleep's
2835 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2836 if ($1 < 20) {
2837 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2838 }
2839 }
2840
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002841# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002842# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002843# print "#ifdef in C files should be avoided\n";
2844# print "$herecurr";
2845# $clean = 0;
2846# }
2847
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002848# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002849 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002850 ERROR("exactly one space required after that #$1\n" . $herecurr);
2851 }
2852
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002853# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002854 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2855 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002856 my $which = $1;
2857 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002858 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002859 }
2860 }
2861# check for memory barriers without a comment.
2862 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2863 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002864 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002865 }
2866 }
2867# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002868 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002869 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002870 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002871
Tobias Klauserd4977c72010-05-24 14:33:30 -07002872# Check that the storage class is at the beginning of a declaration
2873 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2874 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2875 }
2876
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002877# check the location of the inline attribute, that it is between
2878# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002879 if ($line =~ /\b$Type\s+$Inline\b/ ||
2880 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002881 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2882 }
2883
Andy Whitcroft8905a672007-11-28 16:21:06 -08002884# Check for __inline__ and __inline, prefer inline
2885 if ($line =~ /\b(__inline__|__inline)\b/) {
2886 WARN("plain inline is preferred over $1\n" . $herecurr);
2887 }
2888
Joe Perches3d130fd2011-01-12 17:00:00 -08002889# Check for __attribute__ packed, prefer __packed
2890 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2891 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2892 }
2893
Joe Perches8f53a9b2010-03-05 13:43:48 -08002894# check for sizeof(&)
2895 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2896 WARN("sizeof(& should be avoided\n" . $herecurr);
2897 }
2898
Joe Perches428e2fd2011-05-24 17:13:39 -07002899# check for line continuations in quoted strings with odd counts of "
2900 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2901 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2902 }
2903
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002904# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002905 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002906 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002907 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002908 my $function_name = $1;
2909 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002910
2911 my $s = $stat;
2912 if (defined $cond) {
2913 substr($s, 0, length($cond), '');
2914 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002915 if ($s =~ /^\s*;/ &&
2916 $function_name ne 'uninitialized_var')
2917 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002918 WARN("externs should be avoided in .c files\n" . $herecurr);
2919 }
2920
2921 if ($paren_space =~ /\n/) {
2922 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2923 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002924
2925 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2926 $stat =~ /^.\s*extern\s+/)
2927 {
2928 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002929 }
2930
2931# checks for new __setup's
2932 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2933 my $name = $1;
2934
2935 if (!grep(/$name/, @setup_docs)) {
2936 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2937 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002938 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002939
2940# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08002941 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002942 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2943 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002944
Joe Perchescaf2a542011-01-12 16:59:56 -08002945# check for multiple semicolons
2946 if ($line =~ /;\s*;\s*$/) {
2947 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2948 }
2949
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002950# check for gcc specific __FUNCTION__
2951 if ($line =~ /__FUNCTION__/) {
2952 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2953 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002954
Thomas Gleixner4882720b2010-09-07 14:34:01 +00002955# check for semaphores initialized locked
2956 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002957 WARN("consider using a completion\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01002958
Andy Whitcroft773647a2008-03-28 14:15:58 -07002959 }
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002960# recommend kstrto* over simple_strto*
Andy Whitcroft773647a2008-03-28 14:15:58 -07002961 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002962 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002963 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002964# check for __initcall(), use device_initcall() explicitly please
2965 if ($line =~ /^.\s*__initcall\s*\(/) {
2966 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2967 }
Emese Revfy79404842010-03-05 13:43:53 -08002968# check for various ops structs, ensure they are const.
2969 my $struct_ops = qr{acpi_dock_ops|
2970 address_space_operations|
2971 backlight_ops|
2972 block_device_operations|
2973 dentry_operations|
2974 dev_pm_ops|
2975 dma_map_ops|
2976 extent_io_ops|
2977 file_lock_operations|
2978 file_operations|
2979 hv_ops|
2980 ide_dma_ops|
2981 intel_dvo_dev_ops|
2982 item_operations|
2983 iwl_ops|
2984 kgdb_arch|
2985 kgdb_io|
2986 kset_uevent_ops|
2987 lock_manager_operations|
2988 microcode_ops|
2989 mtrr_ops|
2990 neigh_ops|
2991 nlmsvc_binding|
2992 pci_raw_ops|
2993 pipe_buf_operations|
2994 platform_hibernation_ops|
2995 platform_suspend_ops|
2996 proto_ops|
2997 rpc_pipe_ops|
2998 seq_operations|
2999 snd_ac97_build_ops|
3000 soc_pcmcia_socket_ops|
3001 stacktrace_ops|
3002 sysfs_ops|
3003 tty_operations|
3004 usb_mon_operations|
3005 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003006 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003007 $line =~ /\bstruct\s+($struct_ops)\b/) {
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003008 WARN("struct $1 should normally be const\n" .
3009 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003010 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003011
3012# use of NR_CPUS is usually wrong
3013# ignore definitions of NR_CPUS and usage to define arrays as likely right
3014 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003015 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3016 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003017 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3018 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3019 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003020 {
3021 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3022 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003023
3024# check for %L{u,d,i} in strings
3025 my $string;
3026 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3027 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003028 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003029 if ($string =~ /(?<!%)%L[udi]/) {
3030 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3031 last;
3032 }
3033 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003034
3035# whine mightly about in_atomic
3036 if ($line =~ /\bin_atomic\s*\(/) {
3037 if ($realfile =~ m@^drivers/@) {
3038 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003039 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003040 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3041 }
3042 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003043
3044# check for lockdep_set_novalidate_class
3045 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3046 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3047 if ($realfile !~ m@^kernel/lockdep@ &&
3048 $realfile !~ m@^include/linux/lockdep@ &&
3049 $realfile !~ m@^drivers/base/core@) {
3050 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3051 }
3052 }
Dave Jones88f88312011-01-12 16:59:59 -08003053
3054 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3055 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3056 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3057 }
Dave Jones309c00c2011-03-22 16:34:44 -07003058
3059 # Check for memset with swapped arguments
3060 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3061 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
3062 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003063 }
3064
3065 # If we have no input at all, then there is nothing to report on
3066 # so just keep quiet.
3067 if ($#rawlines == -1) {
3068 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003069 }
3070
Andy Whitcroft8905a672007-11-28 16:21:06 -08003071 # In mailback mode only produce a report in the negative, for
3072 # things that appear to be patches.
3073 if ($mailback && ($clean == 1 || !$is_patch)) {
3074 exit(0);
3075 }
3076
3077 # This is not a patch, and we are are in 'no-patch' mode so
3078 # just keep quiet.
3079 if (!$chk_patch && !$is_patch) {
3080 exit(0);
3081 }
3082
3083 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003084 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003085 }
3086 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003087 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003088 }
3089
Andy Whitcroft8905a672007-11-28 16:21:06 -08003090 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003091 if ($summary && !($clean == 1 && $quiet == 1)) {
3092 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003093 print "total: $cnt_error errors, $cnt_warn warnings, " .
3094 (($check)? "$cnt_chk checks, " : "") .
3095 "$cnt_lines lines checked\n";
3096 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003097 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003098
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003099 if ($quiet == 0) {
3100 # If there were whitespace errors which cleanpatch can fix
3101 # then suggest that.
3102 if ($rpt_cleaners) {
3103 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3104 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003105 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003106 }
3107 }
3108
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003109 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003110 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003111 }
3112 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003113 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003114 print "are false positives report them to the maintainer, see\n";
3115 print "CHECKPATCH in MAINTAINERS.\n";
3116 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003117
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003118 return $clean;
3119}