blob: 32d6a236570db0d48907880649b369820c50c17f [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 Whitcroft131edb32009-10-26 16:50:14 -07005# (c) 2008,2009, 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 Whitcroft5e8d8f62009-10-26 16:50:17 -070013my $V = '0.30';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070014
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070021my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070022my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080023my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070024my $file = 0;
25my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $summary = 1;
27my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080028my $summary_file = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070029my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080030my %debug;
Hannes Eder77f5b102009-09-21 17:04:37 -070031my $help = 0;
32
33sub help {
34 my ($exitcode) = @_;
35
36 print << "EOM";
37Usage: $P [OPTION]... [FILE]...
38Version: $V
39
40Options:
41 -q, --quiet quiet
42 --no-tree run without a kernel tree
43 --no-signoff do not check for 'Signed-off-by' line
44 --patch treat FILE as patchfile (default)
45 --emacs emacs compile window format
46 --terse one line per report
47 -f, --file treat FILE as regular source file
48 --subjective, --strict enable more subjective tests
49 --root=PATH PATH to the kernel tree root
50 --no-summary suppress the per-file summary
51 --mailback only produce a report in case of warnings/errors
52 --summary-file include the filename in summary
53 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
54 'values', 'possible', 'type', and 'attr' (default
55 is all off)
56 --test-only=WORD report only warnings/errors containing WORD
57 literally
58 -h, --help, --version display this help and exit
59
60When FILE is - read standard input.
61EOM
62
63 exit($exitcode);
64}
65
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070066GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070067 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070068 'tree!' => \$tree,
69 'signoff!' => \$chk_signoff,
70 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070071 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080072 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -070073 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070074 'subjective!' => \$check,
75 'strict!' => \$check,
76 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080077 'summary!' => \$summary,
78 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080079 'summary-file!' => \$summary_file,
80
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080081 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -070082 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -070083 'h|help' => \$help,
84 'version' => \$help
85) or help(1);
86
87help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070088
89my $exit = 0;
90
91if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -070092 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070093 exit(1);
94}
95
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080096my $dbg_values = 0;
97my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -070098my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -070099my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800100for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800101 ## no critic
102 eval "\${dbg_$key} = '$debug{$key}';";
103 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800104}
105
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700106my $rpt_cleaners = 0;
107
Andy Whitcroft8905a672007-11-28 16:21:06 -0800108if ($terse) {
109 $emacs = 1;
110 $quiet++;
111}
112
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113if ($tree) {
114 if (defined $root) {
115 if (!top_of_kernel_tree($root)) {
116 die "$P: $root: --root does not point at a valid tree\n";
117 }
118 } else {
119 if (top_of_kernel_tree('.')) {
120 $root = '.';
121 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
122 top_of_kernel_tree($1)) {
123 $root = $1;
124 }
125 }
126
127 if (!defined $root) {
128 print "Must be run from the top-level dir. of a kernel tree\n";
129 exit(2);
130 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700131}
132
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700133my $emitted_corrupt = 0;
134
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700135our $Ident = qr{
136 [A-Za-z_][A-Za-z\d_]*
137 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
138 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700139our $Storage = qr{extern|static|asmlinkage};
140our $Sparse = qr{
141 __user|
142 __kernel|
143 __force|
144 __iomem|
145 __must_check|
146 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800147 __kprobes|
148 __ref
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700149 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800150
151# Notes to $Attribute:
152# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700153our $Attribute = qr{
154 const|
155 __read_mostly|
156 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800157 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700158 ____cacheline_aligned|
159 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800160 ____cacheline_internodealigned_in_smp|
161 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700162 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700163our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700164our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700165our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
166our $Lval = qr{$Ident(?:$Member)*};
167
168our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
169our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800170our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700171our $Operators = qr{
172 <=|>=|==|!=|
173 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800174 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700175 }x;
176
Andy Whitcroft8905a672007-11-28 16:21:06 -0800177our $NonptrType;
178our $Type;
179our $Declare;
180
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700181our $UTF8 = qr {
182 [\x09\x0A\x0D\x20-\x7E] # ASCII
183 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
184 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
185 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
186 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
187 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
188 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
189 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
190}x;
191
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700192our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700193 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700194 atomic_t
195)};
196
Joe Perches691e6692010-03-05 13:43:51 -0800197our $logFunctions = qr{(?x:
198 printk|
199 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
Joe Perches8bbea962010-08-09 17:21:01 -0700200 (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
Joe Perches691e6692010-03-05 13:43:51 -0800201 WARN|
202 panic
203)};
204
Andy Whitcroft8905a672007-11-28 16:21:06 -0800205our @typeList = (
206 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700207 qr{(?:unsigned\s+)?char},
208 qr{(?:unsigned\s+)?short},
209 qr{(?:unsigned\s+)?int},
210 qr{(?:unsigned\s+)?long},
211 qr{(?:unsigned\s+)?long\s+int},
212 qr{(?:unsigned\s+)?long\s+long},
213 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800214 qr{unsigned},
215 qr{float},
216 qr{double},
217 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800218 qr{struct\s+$Ident},
219 qr{union\s+$Ident},
220 qr{enum\s+$Ident},
221 qr{${Ident}_t},
222 qr{${Ident}_handler},
223 qr{${Ident}_handler_fn},
224);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700225our @modifierList = (
226 qr{fastcall},
227);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800228
Wolfram Sang7840a942010-08-09 17:20:57 -0700229our $allowed_asm_includes = qr{(?x:
230 irq|
231 memory
232)};
233# memory.h: ARM has a custom one
234
Andy Whitcroft8905a672007-11-28 16:21:06 -0800235sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700236 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
237 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700238 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800239 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700240 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800241 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700242 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700243 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700244 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800245 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700246 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800247 }x;
248 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700249 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800250 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700251 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800252 }x;
253 $Declare = qr{(?:$Storage\s+)?$Type};
254}
255build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700256
257$chk_signoff = 0 if ($file);
258
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700259my @dep_includes = ();
260my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700261my $removal = "Documentation/feature-removal-schedule.txt";
262if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800263 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700264 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800265 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700266 if (/^Check:\s+(.*\S)/) {
267 for my $entry (split(/[, ]+/, $1)) {
268 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700269 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700270
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700271 } elsif ($entry !~ m@/@) {
272 push(@dep_functions, $entry);
273 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700274 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700275 }
276 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800277 close($REMOVE);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700278}
279
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700280my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800281my @lines = ();
282my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700283for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800284 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700285 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800286 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700287 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800288 } elsif ($filename eq '-') {
289 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700290 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800291 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700292 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700293 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800294 if ($filename eq '-') {
295 $vname = 'Your patch';
296 } else {
297 $vname = $filename;
298 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800299 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700300 chomp;
301 push(@rawlines, $_);
302 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800303 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800304 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700305 $exit = 1;
306 }
307 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800308 @lines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700309}
310
311exit($exit);
312
313sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700314 my ($root) = @_;
315
316 my @tree_check = (
317 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
318 "README", "Documentation", "arch", "include", "drivers",
319 "fs", "init", "ipc", "kernel", "lib", "scripts",
320 );
321
322 foreach my $check (@tree_check) {
323 if (! -e $root . '/' . $check) {
324 return 0;
325 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700326 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700327 return 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700328}
329
330sub expand_tabs {
331 my ($str) = @_;
332
333 my $res = '';
334 my $n = 0;
335 for my $c (split(//, $str)) {
336 if ($c eq "\t") {
337 $res .= ' ';
338 $n++;
339 for (; ($n % 8) != 0; $n++) {
340 $res .= ' ';
341 }
342 next;
343 }
344 $res .= $c;
345 $n++;
346 }
347
348 return $res;
349}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700350sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700351 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700352 return $res;
353}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700354
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700355sub line_stats {
356 my ($line) = @_;
357
358 # Drop the diff line leader and expand tabs
359 $line =~ s/^.//;
360 $line = expand_tabs($line);
361
362 # Pick the indent from the front of the line.
363 my ($white) = ($line =~ /^(\s*)/);
364
365 return (length($line), length($white));
366}
367
Andy Whitcroft773647a2008-03-28 14:15:58 -0700368my $sanitise_quote = '';
369
370sub sanitise_line_reset {
371 my ($in_comment) = @_;
372
373 if ($in_comment) {
374 $sanitise_quote = '*/';
375 } else {
376 $sanitise_quote = '';
377 }
378}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700379sub sanitise_line {
380 my ($line) = @_;
381
382 my $res = '';
383 my $l = '';
384
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800385 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700386 my $off = 0;
387 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700388
Andy Whitcroft773647a2008-03-28 14:15:58 -0700389 # Always copy over the diff marker.
390 $res = substr($line, 0, 1);
391
392 for ($off = 1; $off < length($line); $off++) {
393 $c = substr($line, $off, 1);
394
395 # Comments we are wacking completly including the begin
396 # and end, all to $;.
397 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
398 $sanitise_quote = '*/';
399
400 substr($res, $off, 2, "$;$;");
401 $off++;
402 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800403 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700404 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700405 $sanitise_quote = '';
406 substr($res, $off, 2, "$;$;");
407 $off++;
408 next;
409 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700410 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
411 $sanitise_quote = '//';
412
413 substr($res, $off, 2, $sanitise_quote);
414 $off++;
415 next;
416 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700417
418 # A \ in a string means ignore the next character.
419 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
420 $c eq "\\") {
421 substr($res, $off, 2, 'XX');
422 $off++;
423 next;
424 }
425 # Regular quotes.
426 if ($c eq "'" || $c eq '"') {
427 if ($sanitise_quote eq '') {
428 $sanitise_quote = $c;
429
430 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700431 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700432 } elsif ($sanitise_quote eq $c) {
433 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700434 }
435 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700436
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800437 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700438 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
439 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700440 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
441 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700442 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
443 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700444 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700445 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700446 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800447 }
448
Daniel Walker113f04a2009-09-21 17:04:35 -0700449 if ($sanitise_quote eq '//') {
450 $sanitise_quote = '';
451 }
452
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800453 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700454 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800455 my $clean = 'X' x length($1);
456 $res =~ s@\<.*\>@<$clean>@;
457
458 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700459 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800460 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700461 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800462 }
463
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700464 return $res;
465}
466
Andy Whitcroft8905a672007-11-28 16:21:06 -0800467sub ctx_statement_block {
468 my ($linenr, $remain, $off) = @_;
469 my $line = $linenr - 1;
470 my $blk = '';
471 my $soff = $off;
472 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700473 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800474
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800475 my $loff = 0;
476
Andy Whitcroft8905a672007-11-28 16:21:06 -0800477 my $type = '';
478 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800479 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800480 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800481 my $c;
482 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800483
484 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800485 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800486 @stack = (['', 0]) if ($#stack == -1);
487
Andy Whitcroft773647a2008-03-28 14:15:58 -0700488 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800489 # If we are about to drop off the end, pull in more
490 # context.
491 if ($off >= $len) {
492 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700493 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800494 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800495 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800496 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800497 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800498 $len = length($blk);
499 $line++;
500 last;
501 }
502 # Bail if there is no further context.
503 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800504 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800505 last;
506 }
507 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800508 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800510 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800511
Andy Whitcroft773647a2008-03-28 14:15:58 -0700512 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800513
514 # Handle nested #if/#else.
515 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
516 push(@stack, [ $type, $level ]);
517 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
518 ($type, $level) = @{$stack[$#stack - 1]};
519 } elsif ($remainder =~ /^#\s*endif\b/) {
520 ($type, $level) = @{pop(@stack)};
521 }
522
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523 # Statement ends at the ';' or a close '}' at the
524 # outermost level.
525 if ($level == 0 && $c eq ';') {
526 last;
527 }
528
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800529 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700530 if ($level == 0 && $coff_set == 0 &&
531 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
532 $remainder =~ /^(else)(?:\s|{)/ &&
533 $remainder !~ /^else\s+if\b/) {
534 $coff = $off + length($1) - 1;
535 $coff_set = 1;
536 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
537 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800538 }
539
Andy Whitcroft8905a672007-11-28 16:21:06 -0800540 if (($type eq '' || $type eq '(') && $c eq '(') {
541 $level++;
542 $type = '(';
543 }
544 if ($type eq '(' && $c eq ')') {
545 $level--;
546 $type = ($level != 0)? '(' : '';
547
548 if ($level == 0 && $coff < $soff) {
549 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700550 $coff_set = 1;
551 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800552 }
553 }
554 if (($type eq '' || $type eq '{') && $c eq '{') {
555 $level++;
556 $type = '{';
557 }
558 if ($type eq '{' && $c eq '}') {
559 $level--;
560 $type = ($level != 0)? '{' : '';
561
562 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700563 if (substr($blk, $off + 1, 1) eq ';') {
564 $off++;
565 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800566 last;
567 }
568 }
569 $off++;
570 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700571 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800572 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700573 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800574 $line++;
575 $remain--;
576 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800577
578 my $statement = substr($blk, $soff, $off - $soff + 1);
579 my $condition = substr($blk, $soff, $coff - $soff + 1);
580
581 #warn "STATEMENT<$statement>\n";
582 #warn "CONDITION<$condition>\n";
583
Andy Whitcroft773647a2008-03-28 14:15:58 -0700584 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800585
586 return ($statement, $condition,
587 $line, $remain + 1, $off - $loff + 1, $level);
588}
589
Andy Whitcroftcf655042008-03-04 14:28:20 -0800590sub statement_lines {
591 my ($stmt) = @_;
592
593 # Strip the diff line prefixes and rip blank lines at start and end.
594 $stmt =~ s/(^|\n)./$1/g;
595 $stmt =~ s/^\s*//;
596 $stmt =~ s/\s*$//;
597
598 my @stmt_lines = ($stmt =~ /\n/g);
599
600 return $#stmt_lines + 2;
601}
602
603sub statement_rawlines {
604 my ($stmt) = @_;
605
606 my @stmt_lines = ($stmt =~ /\n/g);
607
608 return $#stmt_lines + 2;
609}
610
611sub statement_block_size {
612 my ($stmt) = @_;
613
614 $stmt =~ s/(^|\n)./$1/g;
615 $stmt =~ s/^\s*{//;
616 $stmt =~ s/}\s*$//;
617 $stmt =~ s/^\s*//;
618 $stmt =~ s/\s*$//;
619
620 my @stmt_lines = ($stmt =~ /\n/g);
621 my @stmt_statements = ($stmt =~ /;/g);
622
623 my $stmt_lines = $#stmt_lines + 2;
624 my $stmt_statements = $#stmt_statements + 1;
625
626 if ($stmt_lines > $stmt_statements) {
627 return $stmt_lines;
628 } else {
629 return $stmt_statements;
630 }
631}
632
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800633sub ctx_statement_full {
634 my ($linenr, $remain, $off) = @_;
635 my ($statement, $condition, $level);
636
637 my (@chunks);
638
Andy Whitcroftcf655042008-03-04 14:28:20 -0800639 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800640 ($statement, $condition, $linenr, $remain, $off, $level) =
641 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700642 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800643 push(@chunks, [ $condition, $statement ]);
644 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
645 return ($level, $linenr, @chunks);
646 }
647
648 # Pull in the following conditional/block pairs and see if they
649 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800650 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800651 ($statement, $condition, $linenr, $remain, $off, $level) =
652 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800653 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700654 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800655 #print "C: push\n";
656 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800657 }
658
659 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800660}
661
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700662sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700663 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700664 my $line;
665 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700666 my $blk = '';
667 my @o;
668 my @c;
669 my @res = ();
670
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700671 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800672 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700673 for ($line = $start; $remain > 0; $line++) {
674 next if ($rawlines[$line] =~ /^-/);
675 $remain--;
676
677 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800678
679 # Handle nested #if/#else.
680 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
681 push(@stack, $level);
682 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
683 $level = $stack[$#stack - 1];
684 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
685 $level = pop(@stack);
686 }
687
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700688 foreach my $c (split(//, $rawlines[$line])) {
689 ##print "C<$c>L<$level><$open$close>O<$off>\n";
690 if ($off > 0) {
691 $off--;
692 next;
693 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700694
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700695 if ($c eq $close && $level > 0) {
696 $level--;
697 last if ($level == 0);
698 } elsif ($c eq $open) {
699 $level++;
700 }
701 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700702
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700703 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700704 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700705 }
706
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700707 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700708 }
709
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700710 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700711}
712sub ctx_block_outer {
713 my ($linenr, $remain) = @_;
714
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700715 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
716 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700717}
718sub ctx_block {
719 my ($linenr, $remain) = @_;
720
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700721 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
722 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700723}
724sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700725 my ($linenr, $remain, $off) = @_;
726
727 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
728 return @r;
729}
730sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700731 my ($linenr, $remain) = @_;
732
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700733 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700734}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700735sub ctx_statement_level {
736 my ($linenr, $remain, $off) = @_;
737
738 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
739}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700740
741sub ctx_locate_comment {
742 my ($first_line, $end_line) = @_;
743
744 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700745 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700746 return $current_comment if (defined $current_comment);
747
748 # Look through the context and try and figure out if there is a
749 # comment.
750 my $in_comment = 0;
751 $current_comment = '';
752 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700753 my $line = $rawlines[$linenr - 1];
754 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700755 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
756 $in_comment = 1;
757 }
758 if ($line =~ m@/\*@) {
759 $in_comment = 1;
760 }
761 if (!$in_comment && $current_comment ne '') {
762 $current_comment = '';
763 }
764 $current_comment .= $line . "\n" if ($in_comment);
765 if ($line =~ m@\*/@) {
766 $in_comment = 0;
767 }
768 }
769
770 chomp($current_comment);
771 return($current_comment);
772}
773sub ctx_has_comment {
774 my ($first_line, $end_line) = @_;
775 my $cmt = ctx_locate_comment($first_line, $end_line);
776
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700777 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700778 ##print "CMMT: $cmt\n";
779
780 return ($cmt ne '');
781}
782
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700783sub raw_line {
784 my ($linenr, $cnt) = @_;
785
786 my $offset = $linenr - 1;
787 $cnt++;
788
789 my $line;
790 while ($cnt) {
791 $line = $rawlines[$offset++];
792 next if (defined($line) && $line =~ /^-/);
793 $cnt--;
794 }
795
796 return $line;
797}
798
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700799sub cat_vet {
800 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700801 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700802
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700803 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700804 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
805 $res .= $1;
806 if ($2 ne '') {
807 $coded = sprintf("^%c", unpack('C', $2) + 64);
808 $res .= $coded;
809 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700810 }
811 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700812
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700813 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700814}
815
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800816my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800817my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800818my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700819my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800820
821sub annotate_reset {
822 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800823 $av_pending = '_';
824 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700825 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800826}
827
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700828sub annotate_values {
829 my ($stream, $type) = @_;
830
831 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700832 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700833 my $cur = $stream;
834
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800835 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700836
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700837 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700838 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800839 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700840 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700841 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800842 print "WS($1)\n" if ($dbg_values > 1);
843 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800844 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800845 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700846 }
847
Andy Whitcrofte91b6e22010-10-26 14:23:11 -0700848 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800849 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700850 $type = 'T';
851
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700852 } elsif ($cur =~ /^($Modifier)\s*/) {
853 print "MODIFIER($1)\n" if ($dbg_values > 1);
854 $type = 'T';
855
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700856 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700857 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800858 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700859 push(@av_paren_type, $type);
860 if ($2 ne '') {
861 $av_pending = 'N';
862 }
863 $type = 'E';
864
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700865 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700866 print "UNDEF($1)\n" if ($dbg_values > 1);
867 $av_preprocessor = 1;
868 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700869
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700870 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800871 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800872 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800873
874 push(@av_paren_type, $type);
875 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700876 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800877
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700878 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800879 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
880 $av_preprocessor = 1;
881
882 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
883
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700884 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800885
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700886 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800887 print "PRE_END($1)\n" if ($dbg_values > 1);
888
889 $av_preprocessor = 1;
890
891 # Assume all arms of the conditional end as this
892 # one does, and continue as if the #endif was not here.
893 pop(@av_paren_type);
894 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700895 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700896
897 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800898 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700899
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700900 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
901 print "ATTR($1)\n" if ($dbg_values > 1);
902 $av_pending = $type;
903 $type = 'N';
904
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700905 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800906 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700907 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800908 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700909 }
910 $type = 'N';
911
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700912 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800913 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700914 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700915 $type = 'N';
916
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700917 } elsif ($cur =~/^(case)/o) {
918 print "CASE($1)\n" if ($dbg_values > 1);
919 $av_pend_colon = 'C';
920 $type = 'N';
921
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700922 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800923 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700924 $type = 'N';
925
926 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800927 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800928 push(@av_paren_type, $av_pending);
929 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700930 $type = 'N';
931
932 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800933 my $new_type = pop(@av_paren_type);
934 if ($new_type ne '_') {
935 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800936 print "PAREN('$1') -> $type\n"
937 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700938 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800939 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700940 }
941
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700942 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800943 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700944 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800945 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700946
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800947 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
948 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700949 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800950 } elsif ($type eq 'E') {
951 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700952 }
953 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
954 $type = 'V';
955
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700956 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800957 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700958 $type = 'V';
959
960 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800961 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700962 $type = 'N';
963
Andy Whitcroftcf655042008-03-04 14:28:20 -0800964 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800965 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800966 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700967 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800968
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800969 } elsif ($cur =~/^(,)/) {
970 print "COMMA($1)\n" if ($dbg_values > 1);
971 $type = 'C';
972
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700973 } elsif ($cur =~ /^(\?)/o) {
974 print "QUESTION($1)\n" if ($dbg_values > 1);
975 $type = 'N';
976
977 } elsif ($cur =~ /^(:)/o) {
978 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
979
980 substr($var, length($res), 1, $av_pend_colon);
981 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
982 $type = 'E';
983 } else {
984 $type = 'N';
985 }
986 $av_pend_colon = 'O';
987
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800988 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800989 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700990 $type = 'N';
991
Andy Whitcroft0d413862008-10-15 22:02:16 -0700992 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -0700993 my $variant;
994
995 print "OPV($1)\n" if ($dbg_values > 1);
996 if ($type eq 'V') {
997 $variant = 'B';
998 } else {
999 $variant = 'U';
1000 }
1001
1002 substr($var, length($res), 1, $variant);
1003 $type = 'N';
1004
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001005 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001006 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001007 if ($1 ne '++' && $1 ne '--') {
1008 $type = 'N';
1009 }
1010
1011 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001012 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001013 }
1014 if (defined $1) {
1015 $cur = substr($cur, length($1));
1016 $res .= $type x length($1);
1017 }
1018 }
1019
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001020 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001021}
1022
Andy Whitcroft8905a672007-11-28 16:21:06 -08001023sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001024 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001025 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001026 ^(?:
1027 $Modifier|
1028 $Storage|
1029 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001030 DEFINE_\S+
1031 )$|
1032 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001033 goto|
1034 return|
1035 case|
1036 else|
1037 asm|__asm__|
1038 do
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001039 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001040 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001041 )}x;
1042 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1043 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001044 # Check for modifiers.
1045 $possible =~ s/\s*$Storage\s*//g;
1046 $possible =~ s/\s*$Sparse\s*//g;
1047 if ($possible =~ /^\s*$/) {
1048
1049 } elsif ($possible =~ /\s/) {
1050 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001051 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001052 if ($modifier !~ $notPermitted) {
1053 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1054 push(@modifierList, $modifier);
1055 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001056 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001057
1058 } else {
1059 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1060 push(@typeList, $possible);
1061 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001062 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001063 } else {
1064 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001065 }
1066}
1067
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001068my $prefix = '';
1069
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001070sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001071 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1072 return 0;
1073 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001074 my $line = $prefix . $_[0];
1075
1076 $line = (split('\n', $line))[0] . "\n" if ($terse);
1077
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001078 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001079
1080 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001081}
1082sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001083 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001084}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001085sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001086 if (report("ERROR: $_[0]\n")) {
1087 our $clean = 0;
1088 our $cnt_error++;
1089 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001090}
1091sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001092 if (report("WARNING: $_[0]\n")) {
1093 our $clean = 0;
1094 our $cnt_warn++;
1095 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001096}
1097sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001098 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001099 our $clean = 0;
1100 our $cnt_chk++;
1101 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001102}
1103
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001104sub check_absolute_file {
1105 my ($absolute, $herecurr) = @_;
1106 my $file = $absolute;
1107
1108 ##print "absolute<$absolute>\n";
1109
1110 # See if any suffix of this path is a path within the tree.
1111 while ($file =~ s@^[^/]*/@@) {
1112 if (-f "$root/$file") {
1113 ##print "file<$file>\n";
1114 last;
1115 }
1116 }
1117 if (! -f _) {
1118 return 0;
1119 }
1120
1121 # It is, so see if the prefix is acceptable.
1122 my $prefix = $absolute;
1123 substr($prefix, -length($file)) = '';
1124
1125 ##print "prefix<$prefix>\n";
1126 if ($prefix ne ".../") {
1127 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1128 }
1129}
1130
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001131sub process {
1132 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001133
1134 my $linenr=0;
1135 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001136 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001137 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001138 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001139
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001140 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001141 my $indent;
1142 my $previndent=0;
1143 my $stashindent=0;
1144
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001145 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001146 my $signoff = 0;
1147 my $is_patch = 0;
1148
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001149 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001150 our $cnt_lines = 0;
1151 our $cnt_error = 0;
1152 our $cnt_warn = 0;
1153 our $cnt_chk = 0;
1154
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001155 # Trace the real file/line as we go.
1156 my $realfile = '';
1157 my $realline = 0;
1158 my $realcnt = 0;
1159 my $here = '';
1160 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001161 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001162 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001163 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001164
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001165 my $prev_values = 'E';
1166
1167 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001168 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001169 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001170 my %suppress_export;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001171
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001172 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001173 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001174 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001175 my @setup_docs = ();
1176 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001177
1178 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001179 my $line;
1180 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001181 $linenr++;
1182 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001183
Andy Whitcroft773647a2008-03-28 14:15:58 -07001184 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001185 $setup_docs = 0;
1186 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1187 $setup_docs = 1;
1188 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001189 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001190 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001191 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1192 $realline=$1-1;
1193 if (defined $2) {
1194 $realcnt=$3+1;
1195 } else {
1196 $realcnt=1+1;
1197 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001198 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001199
1200 # Guestimate if this is a continuing comment. Run
1201 # the context looking for a comment "edge". If this
1202 # edge is a close comment then we must be in a comment
1203 # at context start.
1204 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001205 my $cnt = $realcnt;
1206 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1207 next if (defined $rawlines[$ln - 1] &&
1208 $rawlines[$ln - 1] =~ /^-/);
1209 $cnt--;
1210 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001211 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001212 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1213 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1214 ($edge) = $1;
1215 last;
1216 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001217 }
1218 if (defined $edge && $edge eq '*/') {
1219 $in_comment = 1;
1220 }
1221
1222 # Guestimate if this is a continuing comment. If this
1223 # is the start of a diff block and this line starts
1224 # ' *' then it is very likely a comment.
1225 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001226 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001227 {
1228 $in_comment = 1;
1229 }
1230
1231 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1232 sanitise_line_reset($in_comment);
1233
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001234 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001235 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001236 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001237 $line = sanitise_line($rawline);
1238 }
1239 push(@lines, $line);
1240
1241 if ($realcnt > 1) {
1242 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1243 } else {
1244 $realcnt = 0;
1245 }
1246
1247 #print "==>$rawline\n";
1248 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001249
1250 if ($setup_docs && $line =~ /^\+/) {
1251 push(@setup_docs, $line);
1252 }
1253 }
1254
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001255 $prefix = '';
1256
Andy Whitcroft773647a2008-03-28 14:15:58 -07001257 $realcnt = 0;
1258 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001259 foreach my $line (@lines) {
1260 $linenr++;
1261
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001262 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001263
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001264#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001265 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001266 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001267 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001268 $realline=$1-1;
1269 if (defined $2) {
1270 $realcnt=$3+1;
1271 } else {
1272 $realcnt=1+1;
1273 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001274 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001275 $prev_values = 'E';
1276
Andy Whitcroft773647a2008-03-28 14:15:58 -07001277 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001278 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001279 %suppress_export = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001280 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001281
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001282# track the line number as we move through the hunk, note that
1283# new versions of GNU diff omit the leading space on completely
1284# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001285 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001286 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001287 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001288
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001289 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001290 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001291
1292 # Track the previous line.
1293 ($prevline, $stashline) = ($stashline, $line);
1294 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001295 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1296
Andy Whitcroft773647a2008-03-28 14:15:58 -07001297 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001298
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001299 } elsif ($realcnt == 1) {
1300 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001301 }
1302
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001303 my $hunk_line = ($realcnt != 0);
1304
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001305#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306 $prefix = "$filename:$realline: " if ($emacs && $file);
1307 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1308
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001309 $here = "#$linenr: " if (!$file);
1310 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001311
1312 # extract the filename as it passes
1313 if ($line=~/^\+\+\+\s+(\S+)/) {
1314 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001315 $realfile =~ s@^([^/]*)/@@;
1316
1317 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001318 if (!$file && $tree && $p1_prefix ne '' &&
1319 -e "$root/$p1_prefix") {
Wolfram Sang1e855722009-01-06 14:41:24 -08001320 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1321 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001322
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001323 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001324 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1325 }
1326 next;
1327 }
1328
Randy Dunlap389834b2007-06-08 13:47:03 -07001329 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001330
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001331 my $hereline = "$here\n$rawline\n";
1332 my $herecurr = "$here\n$rawline\n";
1333 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001334
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001335 $cnt_lines++ if ($realcnt != 0);
1336
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001337#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001338 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001339 # This is a signoff, if ugly, so do not double report.
1340 $signoff++;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001341 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001342 WARN("Signed-off-by: is the preferred form\n" .
1343 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001344 }
1345 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001346 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001347 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001348 }
1349 }
1350
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001351# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001352 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001353 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001354 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001355 }
1356
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001357# Check for absolute kernel paths.
1358 if ($tree) {
1359 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1360 my $file = $1;
1361
1362 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1363 check_absolute_file($1, $herecurr)) {
1364 #
1365 } else {
1366 check_absolute_file($file, $herecurr);
1367 }
1368 }
1369 }
1370
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001371# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1372 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001373 $rawline !~ m/^$UTF8*$/) {
1374 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1375
1376 my $blank = copy_spacing($rawline);
1377 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1378 my $hereptr = "$hereline$ptr\n";
1379
1380 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001381 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001382
Andy Whitcroft306708542008-10-15 22:02:28 -07001383# ignore non-hunk lines and lines being removed
1384 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001385
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001386#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001387 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001388 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001389 ERROR("DOS line endings\n" . $herevet);
1390
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001391 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1392 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001393 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001394 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001395 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001396
Andi Kleen33549572010-05-24 14:33:29 -07001397# check for Kconfig help text having a real description
1398 if ($realfile =~ /Kconfig/ &&
1399 $line =~ /\+?\s*(---)?help(---)?$/) {
1400 my $length = 0;
1401 for (my $l = $linenr; defined($lines[$l]); $l++) {
1402 my $f = $lines[$l];
1403 $f =~ s/#.*//;
1404 $f =~ s/^\s+//;
1405 next if ($f =~ /^$/);
1406 last if ($f =~ /^\s*config\s/);
1407 $length++;
1408 }
1409 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4);
1410 }
1411
Andy Whitcroft5368df202008-10-15 22:02:27 -07001412# check we are in a valid source file if not then ignore this hunk
1413 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1414
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001415#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001416 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001417 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches8bbea962010-08-09 17:21:01 -07001418 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
1419 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001420 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001421 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001422 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001423 }
1424
Joe Perches5e79d962010-03-05 13:43:55 -08001425# check for spaces before a quoted newline
1426 if ($rawline =~ /^.*\".*\s\\n/) {
1427 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1428 }
1429
Andy Whitcroft8905a672007-11-28 16:21:06 -08001430# check for adding lines without a newline.
1431 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1432 WARN("adding a line without newline at end of file\n" . $herecurr);
1433 }
1434
Mike Frysinger42e41c52009-09-21 17:04:40 -07001435# Blackfin: use hi/lo macros
1436 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1437 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1438 my $herevet = "$here\n" . cat_vet($line) . "\n";
1439 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1440 }
1441 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1442 my $herevet = "$here\n" . cat_vet($line) . "\n";
1443 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1444 }
1445 }
1446
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001447# check we are in a valid source file C or perl if not then ignore this hunk
1448 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001449
1450# at the beginning of a line any tabs must come first and anything
1451# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001452 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1453 $rawline =~ /^\+\s* \s*/) {
1454 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001455 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001456 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001457 }
1458
Alberto Panizzo08e44362010-03-05 13:43:54 -08001459# check for space before tabs.
1460 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1461 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1462 WARN("please, no space before tabs\n" . $herevet);
1463 }
1464
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001465# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001466# Exceptions:
1467# 1) within comments
1468# 2) indented preprocessor commands
1469# 3) hanging labels
1470 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001471 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001472 WARN("please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001473 }
1474
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001475# check we are in a valid C source file if not then ignore this hunk
1476 next if ($realfile !~ /\.(h|c)$/);
1477
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001478# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001479 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001480 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1481 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001482
Mike Frysinger42e41c52009-09-21 17:04:40 -07001483# Blackfin: don't use __builtin_bfin_[cs]sync
1484 if ($line =~ /__builtin_bfin_csync/) {
1485 my $herevet = "$here\n" . cat_vet($line) . "\n";
1486 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1487 }
1488 if ($line =~ /__builtin_bfin_ssync/) {
1489 my $herevet = "$here\n" . cat_vet($line) . "\n";
1490 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1491 }
1492
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001493# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001494 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1495 $realline_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001496 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001497 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001498 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001499 $stat =~ s/\n./\n /g;
1500 $cond =~ s/\n./\n /g;
1501
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001502 # Find the real next line.
1503 $realline_next = $line_nr_next;
1504 if (defined $realline_next &&
1505 (!defined $lines[$realline_next - 1] ||
1506 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1507 $realline_next++;
1508 }
1509
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001510 my $s = $stat;
1511 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001512
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001513 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001514 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001515
1516 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001517 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001518
Andy Whitcroft463f2862009-09-21 17:04:34 -07001519 } elsif ($s =~ /^.\s*else\b/s) {
1520
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001521 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001522 } 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 -07001523 my $type = $1;
1524 $type =~ s/\s+/ /g;
1525 possible($type, "A:" . $s);
1526
Andy Whitcroft8905a672007-11-28 16:21:06 -08001527 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07001528 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001529 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001530 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001531
1532 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001533 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001534 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001535 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001536
1537 # Check for any sort of function declaration.
1538 # int foo(something bar, other baz);
1539 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001540 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 -08001541 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001542
Andy Whitcroftcf655042008-03-04 14:28:20 -08001543 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001544 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001545 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001546
Andy Whitcroft8905a672007-11-28 16:21:06 -08001547 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001548 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001549
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001550 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001551 }
1552 }
1553 }
1554
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001555 }
1556
Andy Whitcroft653d4872007-06-23 17:16:34 -07001557#
1558# Checks which may be anchored in the context.
1559#
1560
1561# Check for switch () and associated case and default
1562# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001563 if ($line=~/\bswitch\s*\(.*\)/) {
1564 my $err = '';
1565 my $sep = '';
1566 my @ctx = ctx_block_outer($linenr, $realcnt);
1567 shift(@ctx);
1568 for my $ctx (@ctx) {
1569 my ($clen, $cindent) = line_stats($ctx);
1570 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1571 $indent != $cindent) {
1572 $err .= "$sep$ctx\n";
1573 $sep = '';
1574 } else {
1575 $sep = "[...]\n";
1576 }
1577 }
1578 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001579 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001580 }
1581 }
1582
1583# if/while/etc brace do not go on next line, unless defining a do while loop,
1584# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001585 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001586 my $pre_ctx = "$1$2";
1587
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001588 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001589 my $ctx_cnt = $realcnt - $#ctx - 1;
1590 my $ctx = join("\n", @ctx);
1591
Andy Whitcroft548596d2008-07-23 21:29:01 -07001592 my $ctx_ln = $linenr;
1593 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001594
Andy Whitcroft548596d2008-07-23 21:29:01 -07001595 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1596 defined $lines[$ctx_ln - 1] &&
1597 $lines[$ctx_ln - 1] =~ /^-/)) {
1598 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1599 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001600 $ctx_ln++;
1601 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001602
Andy Whitcroft53210162008-07-23 21:29:03 -07001603 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1604 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001605
1606 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1607 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001608 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001609 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001610 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1611 $ctx =~ /\)\s*\;\s*$/ &&
1612 defined $lines[$ctx_ln - 1])
1613 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001614 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1615 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001616 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001617 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001618 }
1619 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001620 }
1621
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001622# Check relative indent for conditionals and blocks.
1623 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1624 my ($s, $c) = ($stat, $cond);
1625
1626 substr($s, 0, length($c), '');
1627
1628 # Make sure we remove the line prefixes as we have
1629 # none on the first line, and are going to readd them
1630 # where necessary.
1631 $s =~ s/\n./\n/gs;
1632
1633 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001634 my @newlines = ($c =~ /\n/gs);
1635 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001636
1637 # We want to check the first line inside the block
1638 # starting at the end of the conditional, so remove:
1639 # 1) any blank line termination
1640 # 2) any opening brace { on end of the line
1641 # 3) any do (...) {
1642 my $continuation = 0;
1643 my $check = 0;
1644 $s =~ s/^.*\bdo\b//;
1645 $s =~ s/^\s*{//;
1646 if ($s =~ s/^\s*\\//) {
1647 $continuation = 1;
1648 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001649 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001650 $check = 1;
1651 $cond_lines++;
1652 }
1653
1654 # Also ignore a loop construct at the end of a
1655 # preprocessor statement.
1656 if (($prevline =~ /^.\s*#\s*define\s/ ||
1657 $prevline =~ /\\\s*$/) && $continuation == 0) {
1658 $check = 0;
1659 }
1660
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001661 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001662 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001663 while ($cond_ptr != $cond_lines) {
1664 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001665
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001666 # If we see an #else/#elif then the code
1667 # is not linear.
1668 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1669 $check = 0;
1670 }
1671
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001672 # Ignore:
1673 # 1) blank lines, they should be at 0,
1674 # 2) preprocessor lines, and
1675 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001676 if ($continuation ||
1677 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001678 $s =~ /^\s*#\s*?/ ||
1679 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001680 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001681 if ($s =~ s/^.*?\n//) {
1682 $cond_lines++;
1683 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001684 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001685 }
1686
1687 my (undef, $sindent) = line_stats("+" . $s);
1688 my $stat_real = raw_line($linenr, $cond_lines);
1689
1690 # Check if either of these lines are modified, else
1691 # this is not this patch's fault.
1692 if (!defined($stat_real) ||
1693 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1694 $check = 0;
1695 }
1696 if (defined($stat_real) && $cond_lines > 1) {
1697 $stat_real = "[...]\n$stat_real";
1698 }
1699
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001700 #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 -07001701
1702 if ($check && (($sindent % 8) != 0 ||
1703 ($sindent <= $indent && $s ne ''))) {
1704 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1705 }
1706 }
1707
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001708 # Track the 'values' across context and added lines.
1709 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001710 my ($curr_values, $curr_vars) =
1711 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001712 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001713 if ($dbg_values) {
1714 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001715 print "$linenr > .$outline\n";
1716 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001717 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001718 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001719 $prev_values = substr($curr_values, -1);
1720
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001721#ignore lines not being added
1722 if ($line=~/^[^\+]/) {next;}
1723
Andy Whitcroft653d4872007-06-23 17:16:34 -07001724# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001725 if ($dbg_type) {
1726 if ($line =~ /^.\s*$Declare\s*$/) {
1727 ERROR("TEST: is type\n" . $herecurr);
1728 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1729 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1730 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001731 next;
1732 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001733# TEST: allow direct testing of the attribute matcher.
1734 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001735 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001736 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001737 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001738 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1739 }
1740 next;
1741 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001742
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001743# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07001744 if ($line =~ /^.\s*{/ &&
1745 $prevline =~ /(?:^|[^=])=\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001746 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001747 }
1748
Andy Whitcroft653d4872007-06-23 17:16:34 -07001749#
1750# Checks which are anchored on the added line.
1751#
1752
1753# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001754 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001755 my $path = $1;
1756 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001757 ERROR("malformed #include filename\n" .
1758 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001759 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001760 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001761
1762# no C99 // comments
1763 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001764 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001765 }
1766 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001767 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001768 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001769
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001770# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1771# the whole statement.
1772#print "APW <$lines[$realline_next - 1]>\n";
1773 if (defined $realline_next &&
1774 exists $lines[$realline_next - 1] &&
1775 !defined $suppress_export{$realline_next} &&
1776 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1777 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001778 my $name = $1;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001779 if ($stat !~ /(?:
1780 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07001781 ^.DEFINE_$Ident\(\Q$name\E\)|
1782 ^.DECLARE_$Ident\(\Q$name\E\)|
1783 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001784 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1785 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07001786 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001787#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1788 $suppress_export{$realline_next} = 2;
1789 } else {
1790 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001791 }
1792 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001793 if (!defined $suppress_export{$linenr} &&
1794 $prevline =~ /^.\s*$/ &&
1795 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1796 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1797#print "FOO B <$lines[$linenr - 1]>\n";
1798 $suppress_export{$linenr} = 2;
1799 }
1800 if (defined $suppress_export{$linenr} &&
1801 $suppress_export{$linenr} == 2) {
1802 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1803 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001804
Joe Eloff5150bda2010-08-09 17:21:00 -07001805# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001806 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Eloff5150bda2010-08-09 17:21:00 -07001807 ERROR("do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001808 $herecurr);
1809 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001810# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001811 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001812 ERROR("do not initialise statics to 0 or NULL\n" .
1813 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001814 }
1815
Andy Whitcroft653d4872007-06-23 17:16:34 -07001816# check for new typedefs, only function parameters and sparse annotations
1817# make sense.
1818 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001819 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001820 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001821 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001822 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001823 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001824 }
1825
1826# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001827 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001828 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001829 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001830
Andy Whitcroft65863862009-01-06 14:41:21 -08001831 # Should start with a space.
1832 $to =~ s/^(\S)/ $1/;
1833 # Should not end with a space.
1834 $to =~ s/\s+$//;
1835 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001836 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001837 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001838
Andy Whitcroft65863862009-01-06 14:41:21 -08001839 #print "from<$from> to<$to>\n";
1840 if ($from ne $to) {
1841 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1842 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001843 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001844 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001845
Andy Whitcroft65863862009-01-06 14:41:21 -08001846 # Should start with a space.
1847 $to =~ s/^(\S)/ $1/;
1848 # Should not end with a space.
1849 $to =~ s/\s+$//;
1850 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001851 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001852 }
1853 # Modifiers should have spaces.
1854 $to =~ s/(\b$Modifier$)/$1 /;
1855
Andy Whitcroft667026e2009-02-27 14:03:08 -08001856 #print "from<$from> to<$to> ident<$ident>\n";
1857 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001858 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1859 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001860 }
1861
1862# # no BUG() or BUG_ON()
1863# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1864# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1865# print "$herecurr";
1866# $clean = 0;
1867# }
1868
Andy Whitcroft8905a672007-11-28 16:21:06 -08001869 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001870 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 -08001871 }
1872
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001873# printk should use KERN_* levels. Note that follow on printk's on the
1874# same line do not need a level, so we use the current block context
1875# to try and find and validate the current printk. In summary the current
1876# printk includes all preceeding printk's which have no newline on the end.
1877# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001878 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001879 my $ok = 0;
1880 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1881 #print "CHECK<$lines[$ln - 1]\n";
1882 # we have a preceeding printk if it ends
1883 # with "\n" ignore it, else it is to blame
1884 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1885 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1886 $ok = 1;
1887 }
1888 last;
1889 }
1890 }
1891 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001892 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001893 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001894 }
1895
Andy Whitcroft653d4872007-06-23 17:16:34 -07001896# function brace can't be on same line, except for #defines of do while,
1897# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001898 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1899 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001900 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001901 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001902
Andy Whitcroft8905a672007-11-28 16:21:06 -08001903# open braces for enum, union and struct go on the same line.
1904 if ($line =~ /^.\s*{/ &&
1905 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1906 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1907 }
1908
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001909# check for spacing round square brackets; allowed:
1910# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001911# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1912# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001913 while ($line =~ /(.*?\s)\[/g) {
1914 my ($where, $prefix) = ($-[1], $1);
1915 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07001916 ($where != 0 || $prefix !~ /^.\s+$/) &&
1917 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07001918 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1919 }
1920 }
1921
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001922# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001923 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001924 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001925 my $ctx_before = substr($line, 0, $-[1]);
1926 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001927
1928 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001929 if ($name =~ /^(?:
1930 if|for|while|switch|return|case|
1931 volatile|__volatile__|
1932 __attribute__|format|__extension__|
1933 asm|__asm__)$/x)
1934 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001935
1936 # cpp #define statements have non-optional spaces, ie
1937 # if there is a space between the name and the open
1938 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001939 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001940
1941 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001942 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001943
1944 # If this whole things ends with a type its most
1945 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001946 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001947
1948 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001949 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001950 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001951 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001952# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001953 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001954 my $ops = qr{
1955 <<=|>>=|<=|>=|==|!=|
1956 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1957 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001958 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1959 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001960 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001961 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001962 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001963
1964 my $blank = copy_spacing($opline);
1965
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001966 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001967 $off += length($elements[$n]);
1968
Andy Whitcroft773647a2008-03-28 14:15:58 -07001969 # Pick up the preceeding and succeeding characters.
1970 my $ca = substr($opline, 0, $off);
1971 my $cc = '';
1972 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1973 $cc = substr($opline, $off + length($elements[$n + 1]));
1974 }
1975 my $cb = "$ca$;$cc";
1976
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001977 my $a = '';
1978 $a = 'V' if ($elements[$n] ne '');
1979 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001980 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001981 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1982 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07001983 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001984
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001985 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001986
1987 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001988 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001989 $c = 'V' if ($elements[$n + 2] ne '');
1990 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001991 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001992 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1993 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08001994 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001995 } else {
1996 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001997 }
1998
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001999 my $ctx = "${a}x${c}";
2000
2001 my $at = "(ctx:$ctx)";
2002
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002003 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002004 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002005
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002006 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002007 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002008
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002009 # Get the full operator variant.
2010 my $opv = $op . substr($curr_vars, $off, 1);
2011
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002012 # Ignore operators passed as parameters.
2013 if ($op_type ne 'V' &&
2014 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2015
Andy Whitcroftcf655042008-03-04 14:28:20 -08002016# # Ignore comments
2017# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002018
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002019 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002020 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002021 if ($ctx !~ /.x[WEBC]/ &&
2022 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002023 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002024 }
2025
2026 # // is a comment
2027 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002028
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002029 # No spaces for:
2030 # ->
2031 # : when part of a bitfield
2032 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002033 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002034 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002035 }
2036
2037 # , must have a space on the right.
2038 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002039 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002040 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002041 }
2042
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002043 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002044 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002045 #warn "'*' is part of type\n";
2046
2047 # unary operators should have a space before and
2048 # none after. May be left adjacent to another
2049 # unary operator, or a cast
2050 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002051 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002052 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002053 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002054 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002055 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002056 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002057 # A unary '*' may be const
2058
2059 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07002060 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002061 }
2062
2063 # unary ++ and unary -- are allowed no space on one side.
2064 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002065 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2066 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002067 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002068 if ($ctx =~ /Wx[BE]/ ||
2069 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2070 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002071 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002072 if ($ctx =~ /ExW/) {
2073 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2074 }
2075
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002076
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002077 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002078 } elsif ($op eq '<<' or $op eq '>>' or
2079 $op eq '&' or $op eq '^' or $op eq '|' or
2080 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002081 $op eq '*' or $op eq '/' or
2082 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002083 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002084 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002085 ERROR("need consistent spacing around '$op' $at\n" .
2086 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002087 }
2088
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002089 # A colon needs no spaces before when it is
2090 # terminating a case value or a label.
2091 } elsif ($opv eq ':C' || $opv eq ':L') {
2092 if ($ctx =~ /Wx./) {
2093 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2094 }
2095
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002096 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002097 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002098 my $ok = 0;
2099
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002100 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002101 if (($op eq '<' &&
2102 $cc =~ /^\S+\@\S+>/) ||
2103 ($op eq '>' &&
2104 $ca =~ /<\S+\@\S+$/))
2105 {
2106 $ok = 1;
2107 }
2108
2109 # Ignore ?:
2110 if (($opv eq ':O' && $ca =~ /\?$/) ||
2111 ($op eq '?' && $cc =~ /^:/)) {
2112 $ok = 1;
2113 }
2114
2115 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002116 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002117 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002118 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002119 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002120 }
2121 }
2122
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002123# check for multiple assignments
2124 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002125 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002126 }
2127
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002128## # check for multiple declarations, allowing for a function declaration
2129## # continuation.
2130## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2131## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2132##
2133## # Remove any bracketed sections to ensure we do not
2134## # falsly report the parameters of functions.
2135## my $ln = $line;
2136## while ($ln =~ s/\([^\(\)]*\)//g) {
2137## }
2138## if ($ln =~ /,/) {
2139## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2140## }
2141## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002142
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002143#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002144 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2145 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002146 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002147 }
2148
2149# closing brace should have a space following it when it has anything
2150# on the line
2151 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002152 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002153 }
2154
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002155# check spacing on square brackets
2156 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002157 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002158 }
2159 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002160 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002161 }
2162
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002163# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002164 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2165 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002166 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002167 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002168 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002169 $line !~ /for\s*\(.*;\s+\)/ &&
2170 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002171 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002172 }
2173
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002174#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002175 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002176 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002177 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002178 }
2179
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002180# Return is not a function.
2181 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2182 my $spacing = $1;
2183 my $value = $2;
2184
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002185 # Flatten any parentheses
Andy Whitcroftfee61c42008-07-23 21:28:56 -07002186 $value =~ s/\)\(/\) \(/g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002187 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2188 $value !~ /(?:$Ident|-?$Constant)\s*
2189 $Compare\s*
2190 (?:$Ident|-?$Constant)/x &&
2191 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002192 }
2193
2194 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2195 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2196
2197 } elsif ($spacing !~ /\s+/) {
2198 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2199 }
2200 }
2201
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002202# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002203 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002204 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002205 }
2206
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002207# Check for illegal assignment in if conditional -- and check for trailing
2208# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002209 if ($line =~ /do\s*(?!{)/) {
2210 my ($stat_next) = ctx_statement_block($line_nr_next,
2211 $remain_next, $off_next);
2212 $stat_next =~ s/\n./\n /g;
2213 ##print "stat<$stat> stat_next<$stat_next>\n";
2214
2215 if ($stat_next =~ /^\s*while\b/) {
2216 # If the statement carries leading newlines,
2217 # then count those as offsets.
2218 my ($whitespace) =
2219 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2220 my $offset =
2221 statement_rawlines($whitespace) - 1;
2222
2223 $suppress_whiletrailers{$line_nr_next +
2224 $offset} = 1;
2225 }
2226 }
2227 if (!defined $suppress_whiletrailers{$linenr} &&
2228 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002229 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002230
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002231 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002232 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002233 }
2234
2235 # Find out what is on the end of the line after the
2236 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002237 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002238 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002239 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002240 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2241 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002242 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002243 # Find out how long the conditional actually is.
2244 my @newlines = ($c =~ /\n/gs);
2245 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002246 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002247
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002248 $stat_real = raw_line($linenr, $cond_lines)
2249 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002250 if (defined($stat_real) && $cond_lines > 1) {
2251 $stat_real = "[...]\n$stat_real";
2252 }
2253
2254 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002255 }
2256 }
2257
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002258# Check for bitwise tests written as boolean
2259 if ($line =~ /
2260 (?:
2261 (?:\[|\(|\&\&|\|\|)
2262 \s*0[xX][0-9]+\s*
2263 (?:\&\&|\|\|)
2264 |
2265 (?:\&\&|\|\|)
2266 \s*0[xX][0-9]+\s*
2267 (?:\&\&|\|\||\)|\])
2268 )/x)
2269 {
2270 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2271 }
2272
Andy Whitcroft8905a672007-11-28 16:21:06 -08002273# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002274 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2275 my $s = $1;
2276 $s =~ s/$;//g; # Remove any comments
2277 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2278 ERROR("trailing statements should be on next line\n" . $herecurr);
2279 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002280 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002281# if should not continue a brace
2282 if ($line =~ /}\s*if\b/) {
2283 ERROR("trailing statements should be on next line\n" .
2284 $herecurr);
2285 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002286# case and default should not have general statements after them
2287 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2288 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002289 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002290 \s*return\s+
2291 )/xg)
2292 {
2293 ERROR("trailing statements should be on next line\n" . $herecurr);
2294 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002295
2296 # Check for }<nl>else {, these must be at the same
2297 # indent level to be relevant to each other.
2298 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2299 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002300 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002301 }
2302
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002303 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2304 $previndent == $indent) {
2305 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2306
2307 # Find out what is on the end of the line after the
2308 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002309 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002310 $s =~ s/\n.*//g;
2311
2312 if ($s =~ /^\s*;/) {
2313 ERROR("while should follow close brace '}'\n" . $hereprev);
2314 }
2315 }
2316
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002317#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2318# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2319# print "No studly caps, use _\n";
2320# print "$herecurr";
2321# $clean = 0;
2322# }
2323
2324#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002325 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002326 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002327 }
2328
Andy Whitcroft653d4872007-06-23 17:16:34 -07002329#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002330 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002331 my $file = "$1.h";
2332 my $checkfile = "include/linux/$file";
2333 if (-f "$root/$checkfile" &&
2334 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002335 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002336 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002337 if ($realfile =~ m{^arch/}) {
2338 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2339 } else {
2340 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2341 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002342 }
2343 }
2344
Andy Whitcroft653d4872007-06-23 17:16:34 -07002345# multi-statement macros should be enclosed in a do while loop, grab the
2346# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002347# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07002348 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2349 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002350 my $ln = $linenr;
2351 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002352 my ($off, $dstat, $dcond, $rest);
2353 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002354
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002355 my $args = defined($1);
2356
2357 # Find the end of the macro and limit our statement
2358 # search to that.
2359 while ($cnt > 0 && defined $lines[$ln - 1] &&
2360 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2361 {
2362 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002363 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002364 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002365 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002366 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002367
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002368 ($dstat, $dcond, $ln, $cnt, $off) =
2369 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2370 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002371 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002372
2373 # Extract the remainder of the define (if any) and
2374 # rip off surrounding spaces, and trailing \'s.
2375 $rest = '';
Andy Whitcroft636d140a2008-10-15 22:02:18 -07002376 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2377 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002378 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2379 $rest .= substr($lines[$ln - 1], $off) . "\n";
2380 $cnt--;
2381 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002382 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002383 $off = 0;
2384 }
2385 $rest =~ s/\\\n.//g;
2386 $rest =~ s/^\s*//s;
2387 $rest =~ s/\s*$//s;
2388
2389 # Clean up the original statement.
2390 if ($args) {
2391 substr($dstat, 0, length($dcond), '');
2392 } else {
2393 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2394 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002395 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002396 $dstat =~ s/\\\n.//g;
2397 $dstat =~ s/^\s*//s;
2398 $dstat =~ s/\s*$//s;
2399
2400 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002401 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2402 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2403 $dstat =~ s/\[[^\{\}]*\]/1/)
2404 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002405 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002406
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002407 my $exceptions = qr{
2408 $Declare|
2409 module_param_named|
2410 MODULE_PARAM_DESC|
2411 DECLARE_PER_CPU|
2412 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002413 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002414 union|
2415 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002416 \.$Ident\s*=\s*|
2417 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002418 }x;
Andy Whitcroft383099f2009-01-06 14:41:18 -08002419 #print "REST<$rest> dstat<$dstat>\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002420 if ($rest ne '') {
2421 if ($rest !~ /while\s*\(/ &&
2422 $dstat !~ /$exceptions/)
2423 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002424 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 -07002425 }
2426
2427 } elsif ($ctx !~ /;/) {
2428 if ($dstat ne '' &&
2429 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2430 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002431 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002432 $dstat =~ /$Operators/)
2433 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002434 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002435 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002436 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002437 }
2438
Mike Frysinger080ba922009-01-06 14:41:25 -08002439# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2440# all assignments may have only one of the following with an assignment:
2441# .
2442# ALIGN(...)
2443# VMLINUX_SYMBOL(...)
2444 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2445 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2446 }
2447
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002448# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002449 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2450 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002451 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002452 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002453 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2454 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002455 my $allowed = 0;
2456 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002457 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002458 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002459 for my $chunk (@chunks) {
2460 my ($cond, $block) = @{$chunk};
2461
Andy Whitcroft773647a2008-03-28 14:15:58 -07002462 # If the condition carries leading newlines, then count those as offsets.
2463 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2464 my $offset = statement_rawlines($whitespace) - 1;
2465
2466 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2467
2468 # We have looked at and allowed this specific line.
2469 $suppress_ifbraces{$ln + $offset} = 1;
2470
2471 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002472 $ln += statement_rawlines($block) - 1;
2473
Andy Whitcroft773647a2008-03-28 14:15:58 -07002474 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002475
2476 $seen++ if ($block =~ /^\s*{/);
2477
Andy Whitcroftcf655042008-03-04 14:28:20 -08002478 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2479 if (statement_lines($cond) > 1) {
2480 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002481 $allowed = 1;
2482 }
2483 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002484 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002485 $allowed = 1;
2486 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002487 if (statement_block_size($block) > 1) {
2488 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002489 $allowed = 1;
2490 }
2491 }
2492 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002493 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002494 }
2495 }
2496 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002497 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002498 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002499 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002500
Andy Whitcroftcf655042008-03-04 14:28:20 -08002501 # Check the pre-context.
2502 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2503 #print "APW: ALLOWED: pre<$1>\n";
2504 $allowed = 1;
2505 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002506
2507 my ($level, $endln, @chunks) =
2508 ctx_statement_full($linenr, $realcnt, $-[0]);
2509
Andy Whitcroftcf655042008-03-04 14:28:20 -08002510 # Check the condition.
2511 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002512 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002513 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002514 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002515 }
2516 if (statement_lines($cond) > 1) {
2517 #print "APW: ALLOWED: cond<$cond>\n";
2518 $allowed = 1;
2519 }
2520 if ($block =~/\b(?:if|for|while)\b/) {
2521 #print "APW: ALLOWED: block<$block>\n";
2522 $allowed = 1;
2523 }
2524 if (statement_block_size($block) > 1) {
2525 #print "APW: ALLOWED: lines block<$block>\n";
2526 $allowed = 1;
2527 }
2528 # Check the post-context.
2529 if (defined $chunks[1]) {
2530 my ($cond, $block) = @{$chunks[1]};
2531 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002532 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002533 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002534 if ($block =~ /^\s*\{/) {
2535 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2536 $allowed = 1;
2537 }
2538 }
2539 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2540 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002541 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002542
Andy Whitcroftf0556632008-10-15 22:02:23 -07002543 for (my $n = 0; $n < $cnt; $n++) {
2544 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002545 }
2546
2547 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002548 }
2549 }
2550
Andy Whitcroft653d4872007-06-23 17:16:34 -07002551# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002552 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002553 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002554 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002555 }
2556 }
2557
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002558# don't use deprecated functions
2559 for my $func (@dep_functions) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002560 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002561 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002562 }
2563 }
2564
2565# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002566 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2567 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002568 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002569 }
2570
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002571# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2572 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2573 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2574 }
2575
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002576# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002577 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002578 CHK("if this code is redundant consider removing it\n" .
2579 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002580 }
2581
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002582# check for needless kfree() checks
2583 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2584 my $expr = $1;
2585 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002586 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002587 }
2588 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002589# check for needless usb_free_urb() checks
2590 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2591 my $expr = $1;
2592 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2593 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2594 }
2595 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002596
Patrick Pannuto1a15a252010-08-09 17:21:01 -07002597# prefer usleep_range over udelay
2598 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2599 # ignore udelay's < 10, however
2600 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2601 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2602 }
2603 }
2604
Patrick Pannuto09ef8722010-08-09 17:21:02 -07002605# warn about unexpectedly long msleep's
2606 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2607 if ($1 < 20) {
2608 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2609 }
2610 }
2611
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002612# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002613# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002614# print "#ifdef in C files should be avoided\n";
2615# print "$herecurr";
2616# $clean = 0;
2617# }
2618
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002619# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002620 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002621 ERROR("exactly one space required after that #$1\n" . $herecurr);
2622 }
2623
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002624# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002625 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2626 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002627 my $which = $1;
2628 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002629 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002630 }
2631 }
2632# check for memory barriers without a comment.
2633 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2634 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002635 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002636 }
2637 }
2638# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002639 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002640 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002641 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002642
Tobias Klauserd4977c72010-05-24 14:33:30 -07002643# Check that the storage class is at the beginning of a declaration
2644 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2645 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2646 }
2647
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002648# check the location of the inline attribute, that it is between
2649# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002650 if ($line =~ /\b$Type\s+$Inline\b/ ||
2651 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002652 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2653 }
2654
Andy Whitcroft8905a672007-11-28 16:21:06 -08002655# Check for __inline__ and __inline, prefer inline
2656 if ($line =~ /\b(__inline__|__inline)\b/) {
2657 WARN("plain inline is preferred over $1\n" . $herecurr);
2658 }
2659
Joe Perches8f53a9b2010-03-05 13:43:48 -08002660# check for sizeof(&)
2661 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2662 WARN("sizeof(& should be avoided\n" . $herecurr);
2663 }
2664
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002665# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002666 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002667 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002668 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002669 my $function_name = $1;
2670 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002671
2672 my $s = $stat;
2673 if (defined $cond) {
2674 substr($s, 0, length($cond), '');
2675 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002676 if ($s =~ /^\s*;/ &&
2677 $function_name ne 'uninitialized_var')
2678 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002679 WARN("externs should be avoided in .c files\n" . $herecurr);
2680 }
2681
2682 if ($paren_space =~ /\n/) {
2683 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2684 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002685
2686 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2687 $stat =~ /^.\s*extern\s+/)
2688 {
2689 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002690 }
2691
2692# checks for new __setup's
2693 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2694 my $name = $1;
2695
2696 if (!grep(/$name/, @setup_docs)) {
2697 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2698 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002699 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002700
2701# check for pointless casting of kmalloc return
2702 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2703 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2704 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002705
2706# check for gcc specific __FUNCTION__
2707 if ($line =~ /__FUNCTION__/) {
2708 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2709 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002710
2711# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002712 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002713 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2714 }
2715# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002716 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002717 WARN("consider using a completion\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01002718
Andy Whitcroft773647a2008-03-28 14:15:58 -07002719 }
2720# recommend strict_strto* over simple_strto*
2721 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2722 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2723 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002724# check for __initcall(), use device_initcall() explicitly please
2725 if ($line =~ /^.\s*__initcall\s*\(/) {
2726 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2727 }
Emese Revfy79404842010-03-05 13:43:53 -08002728# check for various ops structs, ensure they are const.
2729 my $struct_ops = qr{acpi_dock_ops|
2730 address_space_operations|
2731 backlight_ops|
2732 block_device_operations|
2733 dentry_operations|
2734 dev_pm_ops|
2735 dma_map_ops|
2736 extent_io_ops|
2737 file_lock_operations|
2738 file_operations|
2739 hv_ops|
2740 ide_dma_ops|
2741 intel_dvo_dev_ops|
2742 item_operations|
2743 iwl_ops|
2744 kgdb_arch|
2745 kgdb_io|
2746 kset_uevent_ops|
2747 lock_manager_operations|
2748 microcode_ops|
2749 mtrr_ops|
2750 neigh_ops|
2751 nlmsvc_binding|
2752 pci_raw_ops|
2753 pipe_buf_operations|
2754 platform_hibernation_ops|
2755 platform_suspend_ops|
2756 proto_ops|
2757 rpc_pipe_ops|
2758 seq_operations|
2759 snd_ac97_build_ops|
2760 soc_pcmcia_socket_ops|
2761 stacktrace_ops|
2762 sysfs_ops|
2763 tty_operations|
2764 usb_mon_operations|
2765 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08002766 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08002767 $line =~ /\bstruct\s+($struct_ops)\b/) {
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08002768 WARN("struct $1 should normally be const\n" .
2769 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08002770 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002771
2772# use of NR_CPUS is usually wrong
2773# ignore definitions of NR_CPUS and usage to define arrays as likely right
2774 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002775 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2776 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002777 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2778 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2779 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002780 {
2781 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2782 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002783
2784# check for %L{u,d,i} in strings
2785 my $string;
2786 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2787 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07002788 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002789 if ($string =~ /(?<!%)%L[udi]/) {
2790 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2791 last;
2792 }
2793 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002794
2795# whine mightly about in_atomic
2796 if ($line =~ /\bin_atomic\s*\(/) {
2797 if ($realfile =~ m@^drivers/@) {
2798 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08002799 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08002800 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2801 }
2802 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01002803
2804# check for lockdep_set_novalidate_class
2805 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2806 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2807 if ($realfile !~ m@^kernel/lockdep@ &&
2808 $realfile !~ m@^include/linux/lockdep@ &&
2809 $realfile !~ m@^drivers/base/core@) {
2810 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2811 }
2812 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002813 }
2814
2815 # If we have no input at all, then there is nothing to report on
2816 # so just keep quiet.
2817 if ($#rawlines == -1) {
2818 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002819 }
2820
Andy Whitcroft8905a672007-11-28 16:21:06 -08002821 # In mailback mode only produce a report in the negative, for
2822 # things that appear to be patches.
2823 if ($mailback && ($clean == 1 || !$is_patch)) {
2824 exit(0);
2825 }
2826
2827 # This is not a patch, and we are are in 'no-patch' mode so
2828 # just keep quiet.
2829 if (!$chk_patch && !$is_patch) {
2830 exit(0);
2831 }
2832
2833 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002834 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002835 }
2836 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002837 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002838 }
2839
Andy Whitcroft8905a672007-11-28 16:21:06 -08002840 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002841 if ($summary && !($clean == 1 && $quiet == 1)) {
2842 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002843 print "total: $cnt_error errors, $cnt_warn warnings, " .
2844 (($check)? "$cnt_chk checks, " : "") .
2845 "$cnt_lines lines checked\n";
2846 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002847 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002848
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002849 if ($quiet == 0) {
2850 # If there were whitespace errors which cleanpatch can fix
2851 # then suggest that.
2852 if ($rpt_cleaners) {
2853 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2854 print " scripts/cleanfile\n\n";
2855 }
2856 }
2857
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002858 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002859 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002860 }
2861 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002862 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002863 print "are false positives report them to the maintainer, see\n";
2864 print "CHECKPATCH in MAINTAINERS.\n";
2865 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002866
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002867 return $clean;
2868}