blob: 2d4ce154401568e515e836e8b29d2635a0f77bd2 [file] [log] [blame]
Andy Whitcroft0a920b52007-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 Whitcroft00df3442007-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 Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070010use constant BEFORE_SHORTTEXT => 0;
11use constant IN_SHORTTEXT => 1;
12use constant AFTER_SHORTTEXT => 2;
13use constant CHECK_NEXT_SHORTTEXT => 3;
14use constant SHORTTEXT_LIMIT => 75;
15
Andy Whitcroft0a920b52007-06-01 00:46:48 -070016my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070017$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070018
Andy Whitcroft267ad8f2010-10-26 14:23:19 -070019my $V = '0.31';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070020
21use Getopt::Long qw(:config no_auto_abbrev);
22
23my $quiet = 0;
24my $tree = 1;
25my $chk_signoff = 1;
26my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070027my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070028my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080029my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $file = 0;
31my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080032my $summary = 1;
33my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080034my $summary_file = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070035my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080036my %debug;
Hannes Eder77f5b102009-09-21 17:04:37 -070037my $help = 0;
38
39sub help {
40 my ($exitcode) = @_;
41
42 print << "EOM";
43Usage: $P [OPTION]... [FILE]...
44Version: $V
45
46Options:
47 -q, --quiet quiet
48 --no-tree run without a kernel tree
49 --no-signoff do not check for 'Signed-off-by' line
50 --patch treat FILE as patchfile (default)
51 --emacs emacs compile window format
52 --terse one line per report
53 -f, --file treat FILE as regular source file
54 --subjective, --strict enable more subjective tests
55 --root=PATH PATH to the kernel tree root
56 --no-summary suppress the per-file summary
57 --mailback only produce a report in case of warnings/errors
58 --summary-file include the filename in summary
59 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
60 'values', 'possible', 'type', and 'attr' (default
61 is all off)
62 --test-only=WORD report only warnings/errors containing WORD
63 literally
64 -h, --help, --version display this help and exit
65
66When FILE is - read standard input.
67EOM
68
69 exit($exitcode);
70}
71
Andy Whitcroft0a920b52007-06-01 00:46:48 -070072GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070073 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070074 'tree!' => \$tree,
75 'signoff!' => \$chk_signoff,
76 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070077 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080078 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -070079 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070080 'subjective!' => \$check,
81 'strict!' => \$check,
82 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080083 'summary!' => \$summary,
84 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080085 'summary-file!' => \$summary_file,
86
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080087 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -070088 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -070089 'h|help' => \$help,
90 'version' => \$help
91) or help(1);
92
93help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070094
95my $exit = 0;
96
97if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -070098 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070099 exit(1);
100}
101
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800102my $dbg_values = 0;
103my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700104my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700105my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800106for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800107 ## no critic
108 eval "\${dbg_$key} = '$debug{$key}';";
109 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800110}
111
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700112my $rpt_cleaners = 0;
113
Andy Whitcroft8905a672007-11-28 16:21:06 -0800114if ($terse) {
115 $emacs = 1;
116 $quiet++;
117}
118
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700119if ($tree) {
120 if (defined $root) {
121 if (!top_of_kernel_tree($root)) {
122 die "$P: $root: --root does not point at a valid tree\n";
123 }
124 } else {
125 if (top_of_kernel_tree('.')) {
126 $root = '.';
127 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
128 top_of_kernel_tree($1)) {
129 $root = $1;
130 }
131 }
132
133 if (!defined $root) {
134 print "Must be run from the top-level dir. of a kernel tree\n";
135 exit(2);
136 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700137}
138
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700139my $emitted_corrupt = 0;
140
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700141our $Ident = qr{
142 [A-Za-z_][A-Za-z\d_]*
143 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
144 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700145our $Storage = qr{extern|static|asmlinkage};
146our $Sparse = qr{
147 __user|
148 __kernel|
149 __force|
150 __iomem|
151 __must_check|
152 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800153 __kprobes|
154 __ref
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700155 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800156
157# Notes to $Attribute:
158# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700159our $Attribute = qr{
160 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700161 __percpu|
162 __nocast|
163 __safe|
164 __bitwise__|
165 __packed__|
166 __packed2__|
167 __naked|
168 __maybe_unused|
169 __always_unused|
170 __noreturn|
171 __used|
172 __cold|
173 __noclone|
174 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700175 __read_mostly|
176 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800177 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700178 ____cacheline_aligned|
179 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800180 ____cacheline_internodealigned_in_smp|
181 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700182 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700183our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700184our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700185our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
186our $Lval = qr{$Ident(?:$Member)*};
187
188our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
189our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800190our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700191our $Operators = qr{
192 <=|>=|==|!=|
193 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800194 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700195 }x;
196
Andy Whitcroft8905a672007-11-28 16:21:06 -0800197our $NonptrType;
198our $Type;
199our $Declare;
200
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700201our $UTF8 = qr {
202 [\x09\x0A\x0D\x20-\x7E] # ASCII
203 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
204 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
205 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
206 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
207 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
208 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
209 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
210}x;
211
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700212our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700213 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700214 atomic_t
215)};
216
Joe Perches691e6692010-03-05 13:43:51 -0800217our $logFunctions = qr{(?x:
218 printk|
Joe Perchesb0531722011-05-24 17:13:40 -0700219 [a-z]+_(emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)|
Joe Perches691e6692010-03-05 13:43:51 -0800220 WARN|
Joe Perchesb0531722011-05-24 17:13:40 -0700221 panic|
222 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800223)};
224
Andy Whitcroft8905a672007-11-28 16:21:06 -0800225our @typeList = (
226 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700227 qr{(?:unsigned\s+)?char},
228 qr{(?:unsigned\s+)?short},
229 qr{(?:unsigned\s+)?int},
230 qr{(?:unsigned\s+)?long},
231 qr{(?:unsigned\s+)?long\s+int},
232 qr{(?:unsigned\s+)?long\s+long},
233 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800234 qr{unsigned},
235 qr{float},
236 qr{double},
237 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800238 qr{struct\s+$Ident},
239 qr{union\s+$Ident},
240 qr{enum\s+$Ident},
241 qr{${Ident}_t},
242 qr{${Ident}_handler},
243 qr{${Ident}_handler_fn},
244);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700245our @modifierList = (
246 qr{fastcall},
247);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800248
Wolfram Sang7840a942010-08-09 17:20:57 -0700249our $allowed_asm_includes = qr{(?x:
250 irq|
251 memory
252)};
253# memory.h: ARM has a custom one
254
Andy Whitcroft8905a672007-11-28 16:21:06 -0800255sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700256 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
257 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700258 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800259 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700260 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800261 (?:
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700262 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700263 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700264 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800265 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700266 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800267 }x;
268 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700269 $NonptrType
Andy Whitcroft65863862009-01-06 14:41:21 -0800270 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700271 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800272 }x;
273 $Declare = qr{(?:$Storage\s+)?$Type};
274}
275build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700276
277$chk_signoff = 0 if ($file);
278
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700279my @dep_includes = ();
280my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700281my $removal = "Documentation/feature-removal-schedule.txt";
282if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800283 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700284 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800285 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700286 if (/^Check:\s+(.*\S)/) {
287 for my $entry (split(/[, ]+/, $1)) {
288 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700289 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700290
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700291 } elsif ($entry !~ m@/@) {
292 push(@dep_functions, $entry);
293 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700294 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700295 }
296 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800297 close($REMOVE);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700298}
299
Andy Whitcroft00df3442007-06-08 13:47:06 -0700300my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800301my @lines = ();
302my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700303for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800304 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700305 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800306 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700307 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800308 } elsif ($filename eq '-') {
309 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700310 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800311 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700312 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700313 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800314 if ($filename eq '-') {
315 $vname = 'Your patch';
316 } else {
317 $vname = $filename;
318 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800319 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700320 chomp;
321 push(@rawlines, $_);
322 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800323 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800324 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700325 $exit = 1;
326 }
327 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800328 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700329}
330
331exit($exit);
332
333sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700334 my ($root) = @_;
335
336 my @tree_check = (
337 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
338 "README", "Documentation", "arch", "include", "drivers",
339 "fs", "init", "ipc", "kernel", "lib", "scripts",
340 );
341
342 foreach my $check (@tree_check) {
343 if (! -e $root . '/' . $check) {
344 return 0;
345 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700346 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700347 return 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700348}
349
350sub expand_tabs {
351 my ($str) = @_;
352
353 my $res = '';
354 my $n = 0;
355 for my $c (split(//, $str)) {
356 if ($c eq "\t") {
357 $res .= ' ';
358 $n++;
359 for (; ($n % 8) != 0; $n++) {
360 $res .= ' ';
361 }
362 next;
363 }
364 $res .= $c;
365 $n++;
366 }
367
368 return $res;
369}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700370sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700371 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700372 return $res;
373}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700374
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700375sub line_stats {
376 my ($line) = @_;
377
378 # Drop the diff line leader and expand tabs
379 $line =~ s/^.//;
380 $line = expand_tabs($line);
381
382 # Pick the indent from the front of the line.
383 my ($white) = ($line =~ /^(\s*)/);
384
385 return (length($line), length($white));
386}
387
Andy Whitcroft773647a2008-03-28 14:15:58 -0700388my $sanitise_quote = '';
389
390sub sanitise_line_reset {
391 my ($in_comment) = @_;
392
393 if ($in_comment) {
394 $sanitise_quote = '*/';
395 } else {
396 $sanitise_quote = '';
397 }
398}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700399sub sanitise_line {
400 my ($line) = @_;
401
402 my $res = '';
403 my $l = '';
404
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800405 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700406 my $off = 0;
407 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700408
Andy Whitcroft773647a2008-03-28 14:15:58 -0700409 # Always copy over the diff marker.
410 $res = substr($line, 0, 1);
411
412 for ($off = 1; $off < length($line); $off++) {
413 $c = substr($line, $off, 1);
414
415 # Comments we are wacking completly including the begin
416 # and end, all to $;.
417 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
418 $sanitise_quote = '*/';
419
420 substr($res, $off, 2, "$;$;");
421 $off++;
422 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800423 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700424 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700425 $sanitise_quote = '';
426 substr($res, $off, 2, "$;$;");
427 $off++;
428 next;
429 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700430 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
431 $sanitise_quote = '//';
432
433 substr($res, $off, 2, $sanitise_quote);
434 $off++;
435 next;
436 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700437
438 # A \ in a string means ignore the next character.
439 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
440 $c eq "\\") {
441 substr($res, $off, 2, 'XX');
442 $off++;
443 next;
444 }
445 # Regular quotes.
446 if ($c eq "'" || $c eq '"') {
447 if ($sanitise_quote eq '') {
448 $sanitise_quote = $c;
449
450 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700451 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700452 } elsif ($sanitise_quote eq $c) {
453 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700454 }
455 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700456
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800457 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700458 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
459 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700460 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
461 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700462 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
463 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700464 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700465 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700466 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800467 }
468
Daniel Walker113f04a2009-09-21 17:04:35 -0700469 if ($sanitise_quote eq '//') {
470 $sanitise_quote = '';
471 }
472
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800473 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700474 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800475 my $clean = 'X' x length($1);
476 $res =~ s@\<.*\>@<$clean>@;
477
478 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700479 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800480 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700481 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800482 }
483
Andy Whitcroft00df3442007-06-08 13:47:06 -0700484 return $res;
485}
486
Andy Whitcroft8905a672007-11-28 16:21:06 -0800487sub ctx_statement_block {
488 my ($linenr, $remain, $off) = @_;
489 my $line = $linenr - 1;
490 my $blk = '';
491 my $soff = $off;
492 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700493 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800494
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800495 my $loff = 0;
496
Andy Whitcroft8905a672007-11-28 16:21:06 -0800497 my $type = '';
498 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800499 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800500 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800501 my $c;
502 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800503
504 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800505 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800506 @stack = (['', 0]) if ($#stack == -1);
507
Andy Whitcroft773647a2008-03-28 14:15:58 -0700508 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509 # If we are about to drop off the end, pull in more
510 # context.
511 if ($off >= $len) {
512 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700513 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800514 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800515 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800516 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800517 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800518 $len = length($blk);
519 $line++;
520 last;
521 }
522 # Bail if there is no further context.
523 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800524 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800525 last;
526 }
527 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800528 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800529 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800530 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800531
Andy Whitcroft773647a2008-03-28 14:15:58 -0700532 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800533
534 # Handle nested #if/#else.
535 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
536 push(@stack, [ $type, $level ]);
537 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
538 ($type, $level) = @{$stack[$#stack - 1]};
539 } elsif ($remainder =~ /^#\s*endif\b/) {
540 ($type, $level) = @{pop(@stack)};
541 }
542
Andy Whitcroft8905a672007-11-28 16:21:06 -0800543 # Statement ends at the ';' or a close '}' at the
544 # outermost level.
545 if ($level == 0 && $c eq ';') {
546 last;
547 }
548
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800549 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700550 if ($level == 0 && $coff_set == 0 &&
551 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
552 $remainder =~ /^(else)(?:\s|{)/ &&
553 $remainder !~ /^else\s+if\b/) {
554 $coff = $off + length($1) - 1;
555 $coff_set = 1;
556 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
557 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800558 }
559
Andy Whitcroft8905a672007-11-28 16:21:06 -0800560 if (($type eq '' || $type eq '(') && $c eq '(') {
561 $level++;
562 $type = '(';
563 }
564 if ($type eq '(' && $c eq ')') {
565 $level--;
566 $type = ($level != 0)? '(' : '';
567
568 if ($level == 0 && $coff < $soff) {
569 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700570 $coff_set = 1;
571 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800572 }
573 }
574 if (($type eq '' || $type eq '{') && $c eq '{') {
575 $level++;
576 $type = '{';
577 }
578 if ($type eq '{' && $c eq '}') {
579 $level--;
580 $type = ($level != 0)? '{' : '';
581
582 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700583 if (substr($blk, $off + 1, 1) eq ';') {
584 $off++;
585 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800586 last;
587 }
588 }
589 $off++;
590 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700591 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800592 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700593 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800594 $line++;
595 $remain--;
596 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800597
598 my $statement = substr($blk, $soff, $off - $soff + 1);
599 my $condition = substr($blk, $soff, $coff - $soff + 1);
600
601 #warn "STATEMENT<$statement>\n";
602 #warn "CONDITION<$condition>\n";
603
Andy Whitcroft773647a2008-03-28 14:15:58 -0700604 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800605
606 return ($statement, $condition,
607 $line, $remain + 1, $off - $loff + 1, $level);
608}
609
Andy Whitcroftcf655042008-03-04 14:28:20 -0800610sub statement_lines {
611 my ($stmt) = @_;
612
613 # Strip the diff line prefixes and rip blank lines at start and end.
614 $stmt =~ s/(^|\n)./$1/g;
615 $stmt =~ s/^\s*//;
616 $stmt =~ s/\s*$//;
617
618 my @stmt_lines = ($stmt =~ /\n/g);
619
620 return $#stmt_lines + 2;
621}
622
623sub statement_rawlines {
624 my ($stmt) = @_;
625
626 my @stmt_lines = ($stmt =~ /\n/g);
627
628 return $#stmt_lines + 2;
629}
630
631sub statement_block_size {
632 my ($stmt) = @_;
633
634 $stmt =~ s/(^|\n)./$1/g;
635 $stmt =~ s/^\s*{//;
636 $stmt =~ s/}\s*$//;
637 $stmt =~ s/^\s*//;
638 $stmt =~ s/\s*$//;
639
640 my @stmt_lines = ($stmt =~ /\n/g);
641 my @stmt_statements = ($stmt =~ /;/g);
642
643 my $stmt_lines = $#stmt_lines + 2;
644 my $stmt_statements = $#stmt_statements + 1;
645
646 if ($stmt_lines > $stmt_statements) {
647 return $stmt_lines;
648 } else {
649 return $stmt_statements;
650 }
651}
652
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800653sub ctx_statement_full {
654 my ($linenr, $remain, $off) = @_;
655 my ($statement, $condition, $level);
656
657 my (@chunks);
658
Andy Whitcroftcf655042008-03-04 14:28:20 -0800659 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800660 ($statement, $condition, $linenr, $remain, $off, $level) =
661 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700662 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800663 push(@chunks, [ $condition, $statement ]);
664 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
665 return ($level, $linenr, @chunks);
666 }
667
668 # Pull in the following conditional/block pairs and see if they
669 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800670 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800671 ($statement, $condition, $linenr, $remain, $off, $level) =
672 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800673 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700674 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800675 #print "C: push\n";
676 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800677 }
678
679 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800680}
681
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700682sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700683 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700684 my $line;
685 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700686 my $blk = '';
687 my @o;
688 my @c;
689 my @res = ();
690
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700691 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800692 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700693 for ($line = $start; $remain > 0; $line++) {
694 next if ($rawlines[$line] =~ /^-/);
695 $remain--;
696
697 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800698
699 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700700 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800701 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700702 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800703 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700704 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800705 $level = pop(@stack);
706 }
707
Andy Whitcroft01464f32010-10-26 14:23:19 -0700708 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700709 ##print "C<$c>L<$level><$open$close>O<$off>\n";
710 if ($off > 0) {
711 $off--;
712 next;
713 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700714
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700715 if ($c eq $close && $level > 0) {
716 $level--;
717 last if ($level == 0);
718 } elsif ($c eq $open) {
719 $level++;
720 }
721 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700722
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700723 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700724 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700725 }
726
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700727 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700728 }
729
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700730 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700731}
732sub ctx_block_outer {
733 my ($linenr, $remain) = @_;
734
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700735 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
736 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700737}
738sub ctx_block {
739 my ($linenr, $remain) = @_;
740
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700741 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
742 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700743}
744sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700745 my ($linenr, $remain, $off) = @_;
746
747 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
748 return @r;
749}
750sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700751 my ($linenr, $remain) = @_;
752
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700753 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700754}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700755sub ctx_statement_level {
756 my ($linenr, $remain, $off) = @_;
757
758 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
759}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700760
761sub ctx_locate_comment {
762 my ($first_line, $end_line) = @_;
763
764 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700765 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700766 return $current_comment if (defined $current_comment);
767
768 # Look through the context and try and figure out if there is a
769 # comment.
770 my $in_comment = 0;
771 $current_comment = '';
772 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700773 my $line = $rawlines[$linenr - 1];
774 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700775 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
776 $in_comment = 1;
777 }
778 if ($line =~ m@/\*@) {
779 $in_comment = 1;
780 }
781 if (!$in_comment && $current_comment ne '') {
782 $current_comment = '';
783 }
784 $current_comment .= $line . "\n" if ($in_comment);
785 if ($line =~ m@\*/@) {
786 $in_comment = 0;
787 }
788 }
789
790 chomp($current_comment);
791 return($current_comment);
792}
793sub ctx_has_comment {
794 my ($first_line, $end_line) = @_;
795 my $cmt = ctx_locate_comment($first_line, $end_line);
796
Andy Whitcroft00df3442007-06-08 13:47:06 -0700797 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700798 ##print "CMMT: $cmt\n";
799
800 return ($cmt ne '');
801}
802
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700803sub raw_line {
804 my ($linenr, $cnt) = @_;
805
806 my $offset = $linenr - 1;
807 $cnt++;
808
809 my $line;
810 while ($cnt) {
811 $line = $rawlines[$offset++];
812 next if (defined($line) && $line =~ /^-/);
813 $cnt--;
814 }
815
816 return $line;
817}
818
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700819sub cat_vet {
820 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700821 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700822
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700823 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700824 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
825 $res .= $1;
826 if ($2 ne '') {
827 $coded = sprintf("^%c", unpack('C', $2) + 64);
828 $res .= $coded;
829 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700830 }
831 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700832
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700833 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700834}
835
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800836my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800837my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800838my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700839my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800840
841sub annotate_reset {
842 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800843 $av_pending = '_';
844 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700845 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800846}
847
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700848sub annotate_values {
849 my ($stream, $type) = @_;
850
851 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700852 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700853 my $cur = $stream;
854
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800855 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700856
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700857 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700858 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800859 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700860 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700861 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800862 print "WS($1)\n" if ($dbg_values > 1);
863 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800864 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800865 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700866 }
867
Florian Micklerc023e4732011-01-12 16:59:58 -0800868 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -0700869 print "CAST($1)\n" if ($dbg_values > 1);
870 push(@av_paren_type, $type);
871 $type = 'C';
872
Andy Whitcrofte91b6e22010-10-26 14:23:11 -0700873 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800874 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700875 $type = 'T';
876
Andy Whitcroft389a2fe2008-07-23 21:29:05 -0700877 } elsif ($cur =~ /^($Modifier)\s*/) {
878 print "MODIFIER($1)\n" if ($dbg_values > 1);
879 $type = 'T';
880
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700881 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700882 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800883 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700884 push(@av_paren_type, $type);
885 if ($2 ne '') {
886 $av_pending = 'N';
887 }
888 $type = 'E';
889
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700890 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700891 print "UNDEF($1)\n" if ($dbg_values > 1);
892 $av_preprocessor = 1;
893 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700894
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700895 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800896 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800897 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800898
899 push(@av_paren_type, $type);
900 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700901 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800902
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700903 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800904 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
905 $av_preprocessor = 1;
906
907 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
908
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700909 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800910
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700911 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800912 print "PRE_END($1)\n" if ($dbg_values > 1);
913
914 $av_preprocessor = 1;
915
916 # Assume all arms of the conditional end as this
917 # one does, and continue as if the #endif was not here.
918 pop(@av_paren_type);
919 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700920 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700921
922 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800923 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700924
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700925 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
926 print "ATTR($1)\n" if ($dbg_values > 1);
927 $av_pending = $type;
928 $type = 'N';
929
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700930 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800931 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700932 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800933 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700934 }
935 $type = 'N';
936
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700937 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800938 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700939 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700940 $type = 'N';
941
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700942 } elsif ($cur =~/^(case)/o) {
943 print "CASE($1)\n" if ($dbg_values > 1);
944 $av_pend_colon = 'C';
945 $type = 'N';
946
Andy Whitcroft14b111c2008-10-15 22:02:16 -0700947 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800948 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700949 $type = 'N';
950
951 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800952 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800953 push(@av_paren_type, $av_pending);
954 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700955 $type = 'N';
956
957 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800958 my $new_type = pop(@av_paren_type);
959 if ($new_type ne '_') {
960 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800961 print "PAREN('$1') -> $type\n"
962 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700963 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800964 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700965 }
966
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700967 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800968 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700969 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800970 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700971
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800972 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
973 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700974 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800975 } elsif ($type eq 'E') {
976 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700977 }
978 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
979 $type = 'V';
980
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700981 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800982 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700983 $type = 'V';
984
985 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800986 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700987 $type = 'N';
988
Andy Whitcroftcf655042008-03-04 14:28:20 -0800989 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800990 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800991 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700992 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800993
Andy Whitcroft8e761b02009-01-06 14:41:19 -0800994 } elsif ($cur =~/^(,)/) {
995 print "COMMA($1)\n" if ($dbg_values > 1);
996 $type = 'C';
997
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700998 } elsif ($cur =~ /^(\?)/o) {
999 print "QUESTION($1)\n" if ($dbg_values > 1);
1000 $type = 'N';
1001
1002 } elsif ($cur =~ /^(:)/o) {
1003 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1004
1005 substr($var, length($res), 1, $av_pend_colon);
1006 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1007 $type = 'E';
1008 } else {
1009 $type = 'N';
1010 }
1011 $av_pend_colon = 'O';
1012
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001013 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001014 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001015 $type = 'N';
1016
Andy Whitcroft0d413862008-10-15 22:02:16 -07001017 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001018 my $variant;
1019
1020 print "OPV($1)\n" if ($dbg_values > 1);
1021 if ($type eq 'V') {
1022 $variant = 'B';
1023 } else {
1024 $variant = 'U';
1025 }
1026
1027 substr($var, length($res), 1, $variant);
1028 $type = 'N';
1029
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001030 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001031 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001032 if ($1 ne '++' && $1 ne '--') {
1033 $type = 'N';
1034 }
1035
1036 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001037 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001038 }
1039 if (defined $1) {
1040 $cur = substr($cur, length($1));
1041 $res .= $type x length($1);
1042 }
1043 }
1044
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001045 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001046}
1047
Andy Whitcroft8905a672007-11-28 16:21:06 -08001048sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001049 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001050 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001051 ^(?:
1052 $Modifier|
1053 $Storage|
1054 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001055 DEFINE_\S+
1056 )$|
1057 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001058 goto|
1059 return|
1060 case|
1061 else|
1062 asm|__asm__|
1063 do
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001064 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001065 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001066 )}x;
1067 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1068 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001069 # Check for modifiers.
1070 $possible =~ s/\s*$Storage\s*//g;
1071 $possible =~ s/\s*$Sparse\s*//g;
1072 if ($possible =~ /^\s*$/) {
1073
1074 } elsif ($possible =~ /\s/) {
1075 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001076 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001077 if ($modifier !~ $notPermitted) {
1078 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1079 push(@modifierList, $modifier);
1080 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001081 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001082
1083 } else {
1084 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1085 push(@typeList, $possible);
1086 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001087 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001088 } else {
1089 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001090 }
1091}
1092
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001093my $prefix = '';
1094
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001095sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001096 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1097 return 0;
1098 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001099 my $line = $prefix . $_[0];
1100
1101 $line = (split('\n', $line))[0] . "\n" if ($terse);
1102
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001103 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001104
1105 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001106}
1107sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001108 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001109}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001110sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001111 if (report("ERROR: $_[0]\n")) {
1112 our $clean = 0;
1113 our $cnt_error++;
1114 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001115}
1116sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001117 if (report("WARNING: $_[0]\n")) {
1118 our $clean = 0;
1119 our $cnt_warn++;
1120 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001121}
1122sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001123 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001124 our $clean = 0;
1125 our $cnt_chk++;
1126 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001127}
1128
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001129sub check_absolute_file {
1130 my ($absolute, $herecurr) = @_;
1131 my $file = $absolute;
1132
1133 ##print "absolute<$absolute>\n";
1134
1135 # See if any suffix of this path is a path within the tree.
1136 while ($file =~ s@^[^/]*/@@) {
1137 if (-f "$root/$file") {
1138 ##print "file<$file>\n";
1139 last;
1140 }
1141 }
1142 if (! -f _) {
1143 return 0;
1144 }
1145
1146 # It is, so see if the prefix is acceptable.
1147 my $prefix = $absolute;
1148 substr($prefix, -length($file)) = '';
1149
1150 ##print "prefix<$prefix>\n";
1151 if ($prefix ne ".../") {
1152 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1153 }
1154}
1155
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001156sub process {
1157 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001158
1159 my $linenr=0;
1160 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001161 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001162 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001163 my $stashrawline="";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 my $subjectline="";
1165 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001166
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001167 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001168 my $indent;
1169 my $previndent=0;
1170 my $stashindent=0;
1171
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001172 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001173 my $signoff = 0;
1174 my $is_patch = 0;
1175
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001176 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001177 our $cnt_lines = 0;
1178 our $cnt_error = 0;
1179 our $cnt_warn = 0;
1180 our $cnt_chk = 0;
1181
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001182 # Trace the real file/line as we go.
1183 my $realfile = '';
1184 my $realline = 0;
1185 my $realcnt = 0;
1186 my $here = '';
1187 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001188 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001189 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001190 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001191
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001192 my $prev_values = 'E';
1193
1194 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001195 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001196 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001197 my %suppress_export;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001198
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001199 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001200 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001201 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001202 my @setup_docs = ();
1203 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205 my $in_code_block = 0;
1206 my $exec_file = "";
1207
1208 my $shorttext = BEFORE_SHORTTEXT;
1209 my $shorttext_exspc = 0;
1210
Andy Whitcroft773647a2008-03-28 14:15:58 -07001211 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001212 my $line;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213 # Before sanitizing the rawlines, collapse any header-continuation
1214 # lines into a single line so they can be parsed meaningfully.
1215 my $end_of_hdrs = 0;
1216 foreach my $rawline (@rawlines) {
1217 if ($rawline=~/^\s*$/) {
1218 last;
1219 } else {
1220 $end_of_hdrs++;
1221 }
1222 }
1223 my @continuation_hdrs;
1224 foreach my $n (0 .. $end_of_hdrs) {
1225 if ($rawlines[$n]=~/^\s+/) {
1226 push @continuation_hdrs, $n;
1227 }
1228 }
1229 while (my $n = pop @continuation_hdrs) {
1230 $line = splice @rawlines, $n, 1;
1231 $line=~s/^\s+/ /;
1232 $rawlines[$n - 1] .= $line;
1233 }
1234
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001235 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001236 $linenr++;
1237 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001238
Andy Whitcroft773647a2008-03-28 14:15:58 -07001239 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001240 $setup_docs = 0;
1241 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1242 $setup_docs = 1;
1243 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001244 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001245 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001246 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1247 $realline=$1-1;
1248 if (defined $2) {
1249 $realcnt=$3+1;
1250 } else {
1251 $realcnt=1+1;
1252 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001253 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001254
1255 # Guestimate if this is a continuing comment. Run
1256 # the context looking for a comment "edge". If this
1257 # edge is a close comment then we must be in a comment
1258 # at context start.
1259 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001260 my $cnt = $realcnt;
1261 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1262 next if (defined $rawlines[$ln - 1] &&
1263 $rawlines[$ln - 1] =~ /^-/);
1264 $cnt--;
1265 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001266 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001267 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1268 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1269 ($edge) = $1;
1270 last;
1271 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001272 }
1273 if (defined $edge && $edge eq '*/') {
1274 $in_comment = 1;
1275 }
1276
1277 # Guestimate if this is a continuing comment. If this
1278 # is the start of a diff block and this line starts
1279 # ' *' then it is very likely a comment.
1280 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001281 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001282 {
1283 $in_comment = 1;
1284 }
1285
1286 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1287 sanitise_line_reset($in_comment);
1288
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001289 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001290 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001291 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001292 $line = sanitise_line($rawline);
1293 }
1294 push(@lines, $line);
1295
1296 if ($realcnt > 1) {
1297 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1298 } else {
1299 $realcnt = 0;
1300 }
1301
1302 #print "==>$rawline\n";
1303 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001304
1305 if ($setup_docs && $line =~ /^\+/) {
1306 push(@setup_docs, $line);
1307 }
1308 }
1309
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001310 $prefix = '';
1311
Andy Whitcroft773647a2008-03-28 14:15:58 -07001312 $realcnt = 0;
1313 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001314 foreach my $line (@lines) {
1315 $linenr++;
1316
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001317 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001318
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001319#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001320 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001321 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001322 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001323 $realline=$1-1;
1324 if (defined $2) {
1325 $realcnt=$3+1;
1326 } else {
1327 $realcnt=1+1;
1328 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001329 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001330 $prev_values = 'E';
1331
Andy Whitcroft773647a2008-03-28 14:15:58 -07001332 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001333 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001334 %suppress_export = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001335 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001336
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001337# track the line number as we move through the hunk, note that
1338# new versions of GNU diff omit the leading space on completely
1339# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001340 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001341 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001342 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001343
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001344 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001345 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001346
1347 # Track the previous line.
1348 ($prevline, $stashline) = ($stashline, $line);
1349 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001350 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1351
Andy Whitcroft773647a2008-03-28 14:15:58 -07001352 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001353
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001354 } elsif ($realcnt == 1) {
1355 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001356 }
1357
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001358 my $hunk_line = ($realcnt != 0);
1359
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001360#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001361 $prefix = "$filename:$realline: " if ($emacs && $file);
1362 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1363
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001364 $here = "#$linenr: " if (!$file);
1365 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001366
1367 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001368 if ($line =~ /^diff --git.*?(\S+)$/) {
1369 $realfile = $1;
1370 $realfile =~ s@^([^/]*)/@@;
1371
1372 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001373 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001374 $realfile =~ s@^([^/]*)/@@;
1375
1376 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001377 if (!$file && $tree && $p1_prefix ne '' &&
1378 -e "$root/$p1_prefix") {
Wolfram Sang1e855722009-01-06 14:41:24 -08001379 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1380 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001381
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001382 if ($realfile =~ m@^include/asm/@) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001383 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1384 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385 $in_code_block = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001386 next;
1387 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001388 elsif ($rawline =~ /^diff.+a\/(.+)\sb\/.+$/) {
1389 $exec_file = $1;
1390 $in_code_block = 0;
1391 }
1392 #Check state to make sure we aren't in code block.
1393 elsif (!$in_code_block &&
1394 ($exec_file =~ /^.+\.[chS]$/ or
1395 $exec_file =~ /^.+\.txt$/ or
1396 $exec_file =~ /^.+\.ihex$/ or
1397 $exec_file =~ /^.+\.hex$/ or
1398 $exec_file =~ /^.+\.HEX$/ or
1399 $exec_file =~ /^.+defconfig$/ or
1400 $exec_file =~ /^Makefile$/ or
1401 $exec_file =~ /^Kconfig$/) &&
1402 $rawline =~ /^new (file )?mode\s([0-9]+)$/ &&
1403 (oct($2) & 0111)) {
1404 ERROR("Source file has +x permissions: " .
1405 "$exec_file\n");
1406 }
Randy Dunlap389834b2007-06-08 13:47:03 -07001407 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001408
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001409 my $hereline = "$here\n$rawline\n";
1410 my $herecurr = "$here\n$rawline\n";
1411 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001413 if ($shorttext != AFTER_SHORTTEXT) {
1414 if ($shorttext == IN_SHORTTEXT) {
1415 if ($line=~/^---/ || $line=~/^diff.*/) {
1416 $shorttext = AFTER_SHORTTEXT;
1417 } elsif (length($line) > (SHORTTEXT_LIMIT +
1418 $shorttext_exspc)
1419 && $line !~ /^:([0-7]{6}\s){2}
1420 ([[:xdigit:]]+\.*
1421 \s){2}\w+\s\w+/xms) {
1422 WARN("commit text line over " .
1423 SHORTTEXT_LIMIT .
1424 " characters\n" . $herecurr);
1425 }
1426 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
1427# The Subject line doesn't have to be the last header in the patch.
1428# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
1429# Per RFC5322, continuation lines must be folded, so any left-justified
1430# text which looks like a header is definitely a header.
1431 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
1432 $shorttext = IN_SHORTTEXT;
1433# Check for Subject line followed by a blank line.
1434 if (length($line) != 0) {
1435 WARN("non-blank line after " .
1436 "summary line\n" .
1437 $sublinenr . $here .
1438 "\n" . $subjectline .
1439 "\n" . $line . "\n");
1440 }
1441 }
1442 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
1443 $shorttext = CHECK_NEXT_SHORTTEXT;
1444 $subjectline = $line;
1445 $sublinenr = "#$linenr & ";
1446# Check for Subject line less than line limit
1447 if (length($1) > SHORTTEXT_LIMIT) {
1448 WARN("summary line over " .
1449 SHORTTEXT_LIMIT .
1450 " characters\n" . $herecurr);
1451 }
1452 } elsif ($line=~/^ (.*)/) {
1453 $shorttext = IN_SHORTTEXT;
1454 $shorttext_exspc = 4;
1455 if (length($1) > SHORTTEXT_LIMIT) {
1456 WARN("summary line over " .
1457 SHORTTEXT_LIMIT .
1458 " characters\n" . $herecurr);
1459 }
1460 }
1461 }
1462
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001463 $cnt_lines++ if ($realcnt != 0);
1464
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001465# Check for incorrect file permissions
1466 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1467 my $permhere = $here . "FILE: $realfile\n";
1468 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1469 ERROR("do not set execute permissions for source files\n" . $permhere);
1470 }
1471 }
1472
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001473#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001474 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001475 # This is a signoff, if ugly, so do not double report.
1476 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001477 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001478 WARN("Signed-off-by: is the preferred form\n" .
1479 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001480 }
1481 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001482 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001483 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001484 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001485 if ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
1486 WARN("invalid Signed-off-by identity\n" . $line );
1487 }
1488 }
1489
1490#check the patch for invalid author credentials
1491 if ($line =~ /^From:.*(quicinc|qualcomm)\.com/) {
1492 WARN("invalid author identity\n" . $line );
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001493 }
1494
Andy Whitcroft00df3442007-06-08 13:47:06 -07001495# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001496 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001497 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001498 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001499 }
1500
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001501# Check for absolute kernel paths.
1502 if ($tree) {
1503 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1504 my $file = $1;
1505
1506 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1507 check_absolute_file($1, $herecurr)) {
1508 #
1509 } else {
1510 check_absolute_file($file, $herecurr);
1511 }
1512 }
1513 }
1514
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001515# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1516 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001517 $rawline !~ m/^$UTF8*$/) {
1518 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1519
1520 my $blank = copy_spacing($rawline);
1521 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1522 my $hereptr = "$hereline$ptr\n";
1523
1524 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001525 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001526
Andy Whitcroft306708542008-10-15 22:02:28 -07001527# ignore non-hunk lines and lines being removed
1528 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001529
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001530#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001531 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001532 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001533 ERROR("DOS line endings\n" . $herevet);
1534
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001535 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1536 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001537 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001538 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001539 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001540
1541# check we are in a valid source file if not then ignore this hunk
1542 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1543
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001544#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001545 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001546 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001547 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001548 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001549 $realfile ne "scripts/checkpatch.pl" &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001550 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001551 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001552 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001553 }
1554
Joe Perches5e79d962010-03-05 13:43:55 -08001555# check for spaces before a quoted newline
1556 if ($rawline =~ /^.*\".*\s\\n/) {
1557 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1558 }
1559
Andy Whitcroft8905a672007-11-28 16:21:06 -08001560# check for adding lines without a newline.
1561 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1562 WARN("adding a line without newline at end of file\n" . $herecurr);
1563 }
1564
Mike Frysinger42e41c52009-09-21 17:04:40 -07001565# Blackfin: use hi/lo macros
1566 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1567 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1568 my $herevet = "$here\n" . cat_vet($line) . "\n";
1569 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1570 }
1571 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1572 my $herevet = "$here\n" . cat_vet($line) . "\n";
1573 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1574 }
1575 }
1576
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001577# check we are in a valid source file C or perl if not then ignore this hunk
1578 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001579
1580# at the beginning of a line any tabs must come first and anything
1581# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001582 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1583 $rawline =~ /^\+\s* \s*/) {
1584 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001585 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001586 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001587 }
1588
Alberto Panizzo08e44362010-03-05 13:43:54 -08001589# check for space before tabs.
1590 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1591 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1592 WARN("please, no space before tabs\n" . $herevet);
1593 }
1594
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001595# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001596# Exceptions:
1597# 1) within comments
1598# 2) indented preprocessor commands
1599# 3) hanging labels
1600 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001601 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001602 WARN("please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001603 }
1604
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001605# check we are in a valid C source file if not then ignore this hunk
1606 next if ($realfile !~ /\.(h|c)$/);
1607
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001608# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001609 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001610 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1611 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001612
Mike Frysinger42e41c52009-09-21 17:04:40 -07001613# Blackfin: don't use __builtin_bfin_[cs]sync
1614 if ($line =~ /__builtin_bfin_csync/) {
1615 my $herevet = "$here\n" . cat_vet($line) . "\n";
1616 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1617 }
1618 if ($line =~ /__builtin_bfin_ssync/) {
1619 my $herevet = "$here\n" . cat_vet($line) . "\n";
1620 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1621 }
1622
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001623# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001624 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1625 $realline_next);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001626 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001627 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001628 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001629 $stat =~ s/\n./\n /g;
1630 $cond =~ s/\n./\n /g;
1631
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001632 # Find the real next line.
1633 $realline_next = $line_nr_next;
1634 if (defined $realline_next &&
1635 (!defined $lines[$realline_next - 1] ||
1636 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1637 $realline_next++;
1638 }
1639
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001640 my $s = $stat;
1641 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001642
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001643 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001644 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001645
1646 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001647 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001648
Andy Whitcroft463f2862009-09-21 17:04:34 -07001649 } elsif ($s =~ /^.\s*else\b/s) {
1650
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001651 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001652 } 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 -07001653 my $type = $1;
1654 $type =~ s/\s+/ /g;
1655 possible($type, "A:" . $s);
1656
Andy Whitcroft8905a672007-11-28 16:21:06 -08001657 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07001658 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001659 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001660 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001661
1662 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001663 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001664 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001665 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001666
1667 # Check for any sort of function declaration.
1668 # int foo(something bar, other baz);
1669 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001670 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 -08001671 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001672
Andy Whitcroftcf655042008-03-04 14:28:20 -08001673 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001674 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001675 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001676
Andy Whitcroft8905a672007-11-28 16:21:06 -08001677 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001678 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001679
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001680 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001681 }
1682 }
1683 }
1684
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001685 }
1686
Andy Whitcroft653d4872007-06-23 17:16:34 -07001687#
1688# Checks which may be anchored in the context.
1689#
1690
1691# Check for switch () and associated case and default
1692# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001693 if ($line=~/\bswitch\s*\(.*\)/) {
1694 my $err = '';
1695 my $sep = '';
1696 my @ctx = ctx_block_outer($linenr, $realcnt);
1697 shift(@ctx);
1698 for my $ctx (@ctx) {
1699 my ($clen, $cindent) = line_stats($ctx);
1700 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1701 $indent != $cindent) {
1702 $err .= "$sep$ctx\n";
1703 $sep = '';
1704 } else {
1705 $sep = "[...]\n";
1706 }
1707 }
1708 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001709 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001710 }
1711 }
1712
1713# if/while/etc brace do not go on next line, unless defining a do while loop,
1714# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001715 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001716 my $pre_ctx = "$1$2";
1717
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001718 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001719 my $ctx_cnt = $realcnt - $#ctx - 1;
1720 my $ctx = join("\n", @ctx);
1721
Andy Whitcroft548596d2008-07-23 21:29:01 -07001722 my $ctx_ln = $linenr;
1723 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001724
Andy Whitcroft548596d2008-07-23 21:29:01 -07001725 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1726 defined $lines[$ctx_ln - 1] &&
1727 $lines[$ctx_ln - 1] =~ /^-/)) {
1728 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1729 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001730 $ctx_ln++;
1731 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07001732
Andy Whitcroft53210162008-07-23 21:29:03 -07001733 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1734 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001735
1736 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1737 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001738 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001739 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001740 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1741 $ctx =~ /\)\s*\;\s*$/ &&
1742 defined $lines[$ctx_ln - 1])
1743 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001744 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1745 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001746 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07001747 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001748 }
1749 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001750 }
1751
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001752# Check relative indent for conditionals and blocks.
1753 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1754 my ($s, $c) = ($stat, $cond);
1755
1756 substr($s, 0, length($c), '');
1757
1758 # Make sure we remove the line prefixes as we have
1759 # none on the first line, and are going to readd them
1760 # where necessary.
1761 $s =~ s/\n./\n/gs;
1762
1763 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07001764 my @newlines = ($c =~ /\n/gs);
1765 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001766
1767 # We want to check the first line inside the block
1768 # starting at the end of the conditional, so remove:
1769 # 1) any blank line termination
1770 # 2) any opening brace { on end of the line
1771 # 3) any do (...) {
1772 my $continuation = 0;
1773 my $check = 0;
1774 $s =~ s/^.*\bdo\b//;
1775 $s =~ s/^\s*{//;
1776 if ($s =~ s/^\s*\\//) {
1777 $continuation = 1;
1778 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001779 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001780 $check = 1;
1781 $cond_lines++;
1782 }
1783
1784 # Also ignore a loop construct at the end of a
1785 # preprocessor statement.
1786 if (($prevline =~ /^.\s*#\s*define\s/ ||
1787 $prevline =~ /\\\s*$/) && $continuation == 0) {
1788 $check = 0;
1789 }
1790
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001791 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07001792 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001793 while ($cond_ptr != $cond_lines) {
1794 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001795
Andy Whitcroftf16fa282008-10-15 22:02:32 -07001796 # If we see an #else/#elif then the code
1797 # is not linear.
1798 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1799 $check = 0;
1800 }
1801
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001802 # Ignore:
1803 # 1) blank lines, they should be at 0,
1804 # 2) preprocessor lines, and
1805 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07001806 if ($continuation ||
1807 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001808 $s =~ /^\s*#\s*?/ ||
1809 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07001810 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07001811 if ($s =~ s/^.*?\n//) {
1812 $cond_lines++;
1813 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001814 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001815 }
1816
1817 my (undef, $sindent) = line_stats("+" . $s);
1818 my $stat_real = raw_line($linenr, $cond_lines);
1819
1820 # Check if either of these lines are modified, else
1821 # this is not this patch's fault.
1822 if (!defined($stat_real) ||
1823 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1824 $check = 0;
1825 }
1826 if (defined($stat_real) && $cond_lines > 1) {
1827 $stat_real = "[...]\n$stat_real";
1828 }
1829
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07001830 #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 -07001831
1832 if ($check && (($sindent % 8) != 0 ||
1833 ($sindent <= $indent && $s ne ''))) {
1834 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1835 }
1836 }
1837
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001838 # Track the 'values' across context and added lines.
1839 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001840 my ($curr_values, $curr_vars) =
1841 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001842 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001843 if ($dbg_values) {
1844 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001845 print "$linenr > .$outline\n";
1846 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001847 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001848 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001849 $prev_values = substr($curr_values, -1);
1850
Andy Whitcroft00df3442007-06-08 13:47:06 -07001851#ignore lines not being added
1852 if ($line=~/^[^\+]/) {next;}
1853
Andy Whitcroft653d4872007-06-23 17:16:34 -07001854# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07001855 if ($dbg_type) {
1856 if ($line =~ /^.\s*$Declare\s*$/) {
1857 ERROR("TEST: is type\n" . $herecurr);
1858 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1859 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1860 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001861 next;
1862 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001863# TEST: allow direct testing of the attribute matcher.
1864 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001865 if ($line =~ /^.\s*$Modifier\s*$/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001866 ERROR("TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08001867 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07001868 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1869 }
1870 next;
1871 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001872
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001873# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07001874 if ($line =~ /^.\s*{/ &&
1875 $prevline =~ /(?:^|[^=])=\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001876 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001877 }
1878
Andy Whitcroft653d4872007-06-23 17:16:34 -07001879#
1880# Checks which are anchored on the added line.
1881#
1882
1883# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001884 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001885 my $path = $1;
1886 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001887 ERROR("malformed #include filename\n" .
1888 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001889 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001890 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001891
1892# no C99 // comments
1893 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001894 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001895 }
1896 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001897 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001898 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001899
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001900# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1901# the whole statement.
1902#print "APW <$lines[$realline_next - 1]>\n";
1903 if (defined $realline_next &&
1904 exists $lines[$realline_next - 1] &&
1905 !defined $suppress_export{$realline_next} &&
1906 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1907 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001908 # Handle definitions which produce identifiers with
1909 # a prefix:
1910 # XXX(foo);
1911 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001912 my $name = $1;
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07001913 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1914 $name =~ /^${Ident}_$2/) {
1915#print "FOO C name<$name>\n";
1916 $suppress_export{$realline_next} = 1;
1917
1918 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001919 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07001920 ^.DEFINE_$Ident\(\Q$name\E\)|
1921 ^.DECLARE_$Ident\(\Q$name\E\)|
1922 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001923 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1924 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07001925 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001926#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1927 $suppress_export{$realline_next} = 2;
1928 } else {
1929 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001930 }
1931 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001932 if (!defined $suppress_export{$linenr} &&
1933 $prevline =~ /^.\s*$/ &&
1934 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1935 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1936#print "FOO B <$lines[$linenr - 1]>\n";
1937 $suppress_export{$linenr} = 2;
1938 }
1939 if (defined $suppress_export{$linenr} &&
1940 $suppress_export{$linenr} == 2) {
1941 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1942 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001943
Joe Eloff5150bda2010-08-09 17:21:00 -07001944# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001945 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Eloff5150bda2010-08-09 17:21:00 -07001946 ERROR("do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001947 $herecurr);
1948 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001949# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08001950 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001951 ERROR("do not initialise statics to 0 or NULL\n" .
1952 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001953 }
1954
Joe Perchescb710ec2010-10-26 14:23:20 -07001955# check for static const char * arrays.
1956 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1957 WARN("static const char * array should probably be static const char * const\n" .
1958 $herecurr);
1959 }
1960
1961# check for static char foo[] = "bar" declarations.
1962 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1963 WARN("static char array declaration should probably be static const char\n" .
1964 $herecurr);
1965 }
1966
Joe Perches93ed0e22010-10-26 14:23:21 -07001967# check for declarations of struct pci_device_id
1968 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1969 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1970 }
1971
Andy Whitcroft653d4872007-06-23 17:16:34 -07001972# check for new typedefs, only function parameters and sparse annotations
1973# make sense.
1974 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08001975 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001976 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07001977 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001978 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001979 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001980 }
1981
1982# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08001983 # (char*[ const])
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001984 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001985 my ($from, $to) = ($1, $1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001986
Andy Whitcroft65863862009-01-06 14:41:21 -08001987 # Should start with a space.
1988 $to =~ s/^(\S)/ $1/;
1989 # Should not end with a space.
1990 $to =~ s/\s+$//;
1991 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08001992 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08001993 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001994
Andy Whitcroft65863862009-01-06 14:41:21 -08001995 #print "from<$from> to<$to>\n";
1996 if ($from ne $to) {
1997 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1998 }
Andy Whitcroft00ef4ec2009-02-27 14:03:07 -08001999 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002000 my ($from, $to, $ident) = ($1, $1, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002001
Andy Whitcroft65863862009-01-06 14:41:21 -08002002 # Should start with a space.
2003 $to =~ s/^(\S)/ $1/;
2004 # Should not end with a space.
2005 $to =~ s/\s+$//;
2006 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002007 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002008 }
2009 # Modifiers should have spaces.
2010 $to =~ s/(\b$Modifier$)/$1 /;
2011
Andy Whitcroft667026e2009-02-27 14:03:08 -08002012 #print "from<$from> to<$to> ident<$ident>\n";
2013 if ($from ne $to && $ident !~ /^$Modifier$/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002014 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
2015 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002016 }
2017
2018# # no BUG() or BUG_ON()
2019# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2020# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2021# print "$herecurr";
2022# $clean = 0;
2023# }
2024
Andy Whitcroft8905a672007-11-28 16:21:06 -08002025 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002026 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 -08002027 }
2028
Joe Perches17441222011-06-15 15:08:17 -07002029# check for uses of printk_ratelimit
2030 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2031 WARN("Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2032 }
2033
Andy Whitcroft00df3442007-06-08 13:47:06 -07002034# printk should use KERN_* levels. Note that follow on printk's on the
2035# same line do not need a level, so we use the current block context
2036# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002037# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002038# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002039 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002040 my $ok = 0;
2041 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2042 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002043 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002044 # with "\n" ignore it, else it is to blame
2045 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2046 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2047 $ok = 1;
2048 }
2049 last;
2050 }
2051 }
2052 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002053 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002054 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002055 }
2056
Andy Whitcroft653d4872007-06-23 17:16:34 -07002057# function brace can't be on same line, except for #defines of do while,
2058# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002059 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2060 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002061 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002062 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002063
Andy Whitcroft8905a672007-11-28 16:21:06 -08002064# open braces for enum, union and struct go on the same line.
2065 if ($line =~ /^.\s*{/ &&
2066 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2067 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
2068 }
2069
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002070# missing space after union, struct or enum definition
2071 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
2072 WARN("missing space after $1 definition\n" . $herecurr);
2073 }
2074
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002075# check for spacing round square brackets; allowed:
2076# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002077# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2078# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002079 while ($line =~ /(.*?\s)\[/g) {
2080 my ($where, $prefix) = ($-[1], $1);
2081 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002082 ($where != 0 || $prefix !~ /^.\s+$/) &&
2083 $prefix !~ /{\s+$/) {
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002084 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
2085 }
2086 }
2087
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002088# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002089 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002090 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002091 my $ctx_before = substr($line, 0, $-[1]);
2092 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002093
2094 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002095 if ($name =~ /^(?:
2096 if|for|while|switch|return|case|
2097 volatile|__volatile__|
2098 __attribute__|format|__extension__|
2099 asm|__asm__)$/x)
2100 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002101
2102 # cpp #define statements have non-optional spaces, ie
2103 # if there is a space between the name and the open
2104 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002105 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002106
2107 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002108 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002109
2110 # If this whole things ends with a type its most
2111 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002112 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002113
2114 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002115 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002116 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002117 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002118# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002119 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002120 my $ops = qr{
2121 <<=|>>=|<=|>=|==|!=|
2122 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2123 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002124 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2125 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002126 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002127 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002128 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002129
2130 my $blank = copy_spacing($opline);
2131
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002132 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002133 $off += length($elements[$n]);
2134
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002135 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002136 my $ca = substr($opline, 0, $off);
2137 my $cc = '';
2138 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2139 $cc = substr($opline, $off + length($elements[$n + 1]));
2140 }
2141 my $cb = "$ca$;$cc";
2142
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002143 my $a = '';
2144 $a = 'V' if ($elements[$n] ne '');
2145 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002146 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002147 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2148 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002149 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002150
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002151 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002152
2153 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002154 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002155 $c = 'V' if ($elements[$n + 2] ne '');
2156 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002157 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002158 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2159 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002160 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002161 } else {
2162 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002163 }
2164
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002165 my $ctx = "${a}x${c}";
2166
2167 my $at = "(ctx:$ctx)";
2168
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002169 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002170 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002171
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002172 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002173 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002174
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002175 # Get the full operator variant.
2176 my $opv = $op . substr($curr_vars, $off, 1);
2177
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002178 # Ignore operators passed as parameters.
2179 if ($op_type ne 'V' &&
2180 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2181
Andy Whitcroftcf655042008-03-04 14:28:20 -08002182# # Ignore comments
2183# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002184
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002185 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002186 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002187 if ($ctx !~ /.x[WEBC]/ &&
2188 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002189 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002190 }
2191
2192 # // is a comment
2193 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002194
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002195 # No spaces for:
2196 # ->
2197 # : when part of a bitfield
2198 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002199 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002200 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002201 }
2202
2203 # , must have a space on the right.
2204 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002205 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002206 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002207 }
2208
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002209 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002210 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002211 #warn "'*' is part of type\n";
2212
2213 # unary operators should have a space before and
2214 # none after. May be left adjacent to another
2215 # unary operator, or a cast
2216 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002217 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002218 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002219 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002220 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002221 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002222 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002223 # A unary '*' may be const
2224
2225 } elsif ($ctx =~ /.xW/) {
Andy Whitcroftfb9e9092009-09-21 17:04:38 -07002226 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002227 }
2228
2229 # unary ++ and unary -- are allowed no space on one side.
2230 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002231 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2232 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002233 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002234 if ($ctx =~ /Wx[BE]/ ||
2235 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2236 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002237 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002238 if ($ctx =~ /ExW/) {
2239 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2240 }
2241
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002242
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002243 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002244 } elsif ($op eq '<<' or $op eq '>>' or
2245 $op eq '&' or $op eq '^' or $op eq '|' or
2246 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002247 $op eq '*' or $op eq '/' or
2248 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002249 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002250 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002251 ERROR("need consistent spacing around '$op' $at\n" .
2252 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002253 }
2254
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002255 # A colon needs no spaces before when it is
2256 # terminating a case value or a label.
2257 } elsif ($opv eq ':C' || $opv eq ':L') {
2258 if ($ctx =~ /Wx./) {
2259 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2260 }
2261
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002262 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002263 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002264 my $ok = 0;
2265
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002266 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002267 if (($op eq '<' &&
2268 $cc =~ /^\S+\@\S+>/) ||
2269 ($op eq '>' &&
2270 $ca =~ /<\S+\@\S+$/))
2271 {
2272 $ok = 1;
2273 }
2274
2275 # Ignore ?:
2276 if (($opv eq ':O' && $ca =~ /\?$/) ||
2277 ($op eq '?' && $cc =~ /^:/)) {
2278 $ok = 1;
2279 }
2280
2281 if ($ok == 0) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002282 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002283 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002284 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002285 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002286 }
2287 }
2288
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002289# check for multiple assignments
2290 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002291 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002292 }
2293
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002294## # check for multiple declarations, allowing for a function declaration
2295## # continuation.
2296## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2297## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2298##
2299## # Remove any bracketed sections to ensure we do not
2300## # falsly report the parameters of functions.
2301## my $ln = $line;
2302## while ($ln =~ s/\([^\(\)]*\)//g) {
2303## }
2304## if ($ln =~ /,/) {
2305## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2306## }
2307## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002308
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002309#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002310 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2311 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002312 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002313 }
2314
2315# closing brace should have a space following it when it has anything
2316# on the line
2317 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002318 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002319 }
2320
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002321# check spacing on square brackets
2322 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002323 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002324 }
2325 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002326 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002327 }
2328
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002329# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002330 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002331 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002332 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002333 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002334 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002335 $line !~ /for\s*\(.*;\s+\)/ &&
2336 $line !~ /:\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002337 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002338 }
2339
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002340#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002341 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002343 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002344 }
2345
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002346# Return is not a function.
2347 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2348 my $spacing = $1;
2349 my $value = $2;
2350
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002351 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002352 $value =~ s/\(/ \(/g;
2353 $value =~ s/\)/\) /g;
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002354 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2355 $value !~ /(?:$Ident|-?$Constant)\s*
2356 $Compare\s*
2357 (?:$Ident|-?$Constant)/x &&
2358 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002359 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002360#print "value<$value>\n";
2361 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002362 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2363
2364 } elsif ($spacing !~ /\s+/) {
2365 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2366 }
2367 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002368# Return of what appears to be an errno should normally be -'ve
2369 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2370 my $name = $1;
2371 if ($name ne 'EOF' && $name ne 'ERROR') {
2372 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2373 }
2374 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002375
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002376# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002377 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002378 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002379 }
2380
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002381# Check for illegal assignment in if conditional -- and check for trailing
2382# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002383 if ($line =~ /do\s*(?!{)/) {
2384 my ($stat_next) = ctx_statement_block($line_nr_next,
2385 $remain_next, $off_next);
2386 $stat_next =~ s/\n./\n /g;
2387 ##print "stat<$stat> stat_next<$stat_next>\n";
2388
2389 if ($stat_next =~ /^\s*while\b/) {
2390 # If the statement carries leading newlines,
2391 # then count those as offsets.
2392 my ($whitespace) =
2393 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2394 my $offset =
2395 statement_rawlines($whitespace) - 1;
2396
2397 $suppress_whiletrailers{$line_nr_next +
2398 $offset} = 1;
2399 }
2400 }
2401 if (!defined $suppress_whiletrailers{$linenr} &&
2402 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002403 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002404
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002405 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002406 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002407 }
2408
2409 # Find out what is on the end of the line after the
2410 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002411 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002412 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002413 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002414 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2415 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002416 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002417 # Find out how long the conditional actually is.
2418 my @newlines = ($c =~ /\n/gs);
2419 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002420 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002421
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002422 $stat_real = raw_line($linenr, $cond_lines)
2423 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002424 if (defined($stat_real) && $cond_lines > 1) {
2425 $stat_real = "[...]\n$stat_real";
2426 }
2427
2428 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002429 }
2430 }
2431
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002432# Check for bitwise tests written as boolean
2433 if ($line =~ /
2434 (?:
2435 (?:\[|\(|\&\&|\|\|)
2436 \s*0[xX][0-9]+\s*
2437 (?:\&\&|\|\|)
2438 |
2439 (?:\&\&|\|\|)
2440 \s*0[xX][0-9]+\s*
2441 (?:\&\&|\|\||\)|\])
2442 )/x)
2443 {
2444 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2445 }
2446
Andy Whitcroft8905a672007-11-28 16:21:06 -08002447# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002448 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2449 my $s = $1;
2450 $s =~ s/$;//g; # Remove any comments
2451 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2452 ERROR("trailing statements should be on next line\n" . $herecurr);
2453 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002454 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002455# if should not continue a brace
2456 if ($line =~ /}\s*if\b/) {
2457 ERROR("trailing statements should be on next line\n" .
2458 $herecurr);
2459 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002460# case and default should not have general statements after them
2461 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2462 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002463 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002464 \s*return\s+
2465 )/xg)
2466 {
2467 ERROR("trailing statements should be on next line\n" . $herecurr);
2468 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002469
2470 # Check for }<nl>else {, these must be at the same
2471 # indent level to be relevant to each other.
2472 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2473 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002474 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002475 }
2476
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002477 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2478 $previndent == $indent) {
2479 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2480
2481 # Find out what is on the end of the line after the
2482 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002483 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002484 $s =~ s/\n.*//g;
2485
2486 if ($s =~ /^\s*;/) {
2487 ERROR("while should follow close brace '}'\n" . $hereprev);
2488 }
2489 }
2490
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002491#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2492# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2493# print "No studly caps, use _\n";
2494# print "$herecurr";
2495# $clean = 0;
2496# }
2497
2498#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002499 if ($line=~/\#\s*define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002500 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002501 }
2502
Andy Whitcroft653d4872007-06-23 17:16:34 -07002503#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002504 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002505 my $file = "$1.h";
2506 my $checkfile = "include/linux/$file";
2507 if (-f "$root/$checkfile" &&
2508 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002509 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002510 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002511 if ($realfile =~ m{^arch/}) {
2512 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2513 } else {
2514 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2515 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002516 }
2517 }
2518
Andy Whitcroft653d4872007-06-23 17:16:34 -07002519# multi-statement macros should be enclosed in a do while loop, grab the
2520# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002521# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07002522 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2523 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002524 my $ln = $linenr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002526 my ($off, $dstat, $dcond, $rest);
2527 my $ctx = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07002528
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002529 my $args = defined($1);
2530
2531 # Find the end of the macro and limit our statement
2532 # search to that.
2533 while ($cnt > 0 && defined $lines[$ln - 1] &&
2534 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2535 {
2536 $ctx .= $rawlines[$ln - 1] . "\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002537 $cnt-- if ($lines[$ln - 1] !~ /^-/);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002538 $ln++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002539 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002540 $ctx .= $rawlines[$ln - 1];
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002541
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002542 ($dstat, $dcond, $ln, $cnt, $off) =
2543 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2544 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002545 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002546
2547 # Extract the remainder of the define (if any) and
2548 # rip off surrounding spaces, and trailing \'s.
2549 $rest = '';
Andy Whitcroft636d1402008-10-15 22:02:18 -07002550 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2551 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002552 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2553 $rest .= substr($lines[$ln - 1], $off) . "\n";
2554 $cnt--;
2555 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002556 $ln++;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002557 $off = 0;
2558 }
2559 $rest =~ s/\\\n.//g;
2560 $rest =~ s/^\s*//s;
2561 $rest =~ s/\s*$//s;
2562
2563 # Clean up the original statement.
2564 if ($args) {
2565 substr($dstat, 0, length($dcond), '');
2566 } else {
2567 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2568 }
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002569 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002570 $dstat =~ s/\\\n.//g;
2571 $dstat =~ s/^\s*//s;
2572 $dstat =~ s/\s*$//s;
2573
2574 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002575 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2576 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2577 $dstat =~ s/\[[^\{\}]*\]/1/)
2578 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002579 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002580
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002581 # Extremely long macros may fall off the end of the
2582 # available context without closing. Give a dangling
2583 # backslash the benefit of the doubt and allow it
2584 # to gobble any hanging open-parens.
2585 $dstat =~ s/\(.+\\$/1/;
2586
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002587 my $exceptions = qr{
2588 $Declare|
2589 module_param_named|
2590 MODULE_PARAM_DESC|
2591 DECLARE_PER_CPU|
2592 DEFINE_PER_CPU|
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002593 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002594 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002595 union|
2596 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002597 \.$Ident\s*=\s*|
2598 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002599 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07002600 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2601 if ($rest ne '' && $rest ne ',') {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002602 if ($rest !~ /while\s*\(/ &&
2603 $dstat !~ /$exceptions/)
2604 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002605 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 -07002606 }
2607
2608 } elsif ($ctx !~ /;/) {
2609 if ($dstat ne '' &&
2610 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2611 $dstat !~ /$exceptions/ &&
Andy Whitcroftb132e5d2008-10-15 22:02:31 -07002612 $dstat !~ /^\.$Ident\s*=/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002613 $dstat =~ /$Operators/)
2614 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002615 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002616 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002617 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002618 }
2619
Mike Frysinger080ba922009-01-06 14:41:25 -08002620# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2621# all assignments may have only one of the following with an assignment:
2622# .
2623# ALIGN(...)
2624# VMLINUX_SYMBOL(...)
2625 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2626 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2627 }
2628
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002629# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002630 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2631 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08002632 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002633 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002634 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2635 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002636 my $allowed = 0;
2637 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002638 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002639 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002640 for my $chunk (@chunks) {
2641 my ($cond, $block) = @{$chunk};
2642
Andy Whitcroft773647a2008-03-28 14:15:58 -07002643 # If the condition carries leading newlines, then count those as offsets.
2644 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2645 my $offset = statement_rawlines($whitespace) - 1;
2646
2647 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2648
2649 # We have looked at and allowed this specific line.
2650 $suppress_ifbraces{$ln + $offset} = 1;
2651
2652 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002653 $ln += statement_rawlines($block) - 1;
2654
Andy Whitcroft773647a2008-03-28 14:15:58 -07002655 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002656
2657 $seen++ if ($block =~ /^\s*{/);
2658
Andy Whitcroftcf655042008-03-04 14:28:20 -08002659 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2660 if (statement_lines($cond) > 1) {
2661 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002662 $allowed = 1;
2663 }
2664 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002665 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002666 $allowed = 1;
2667 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002668 if (statement_block_size($block) > 1) {
2669 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002670 $allowed = 1;
2671 }
2672 }
2673 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002674 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002675 }
2676 }
2677 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002678 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002679 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002680 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002681
Andy Whitcroftcf655042008-03-04 14:28:20 -08002682 # Check the pre-context.
2683 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2684 #print "APW: ALLOWED: pre<$1>\n";
2685 $allowed = 1;
2686 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002687
2688 my ($level, $endln, @chunks) =
2689 ctx_statement_full($linenr, $realcnt, $-[0]);
2690
Andy Whitcroftcf655042008-03-04 14:28:20 -08002691 # Check the condition.
2692 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07002693 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08002694 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002695 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08002696 }
2697 if (statement_lines($cond) > 1) {
2698 #print "APW: ALLOWED: cond<$cond>\n";
2699 $allowed = 1;
2700 }
2701 if ($block =~/\b(?:if|for|while)\b/) {
2702 #print "APW: ALLOWED: block<$block>\n";
2703 $allowed = 1;
2704 }
2705 if (statement_block_size($block) > 1) {
2706 #print "APW: ALLOWED: lines block<$block>\n";
2707 $allowed = 1;
2708 }
2709 # Check the post-context.
2710 if (defined $chunks[1]) {
2711 my ($cond, $block) = @{$chunks[1]};
2712 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002713 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002714 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08002715 if ($block =~ /^\s*\{/) {
2716 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2717 $allowed = 1;
2718 }
2719 }
2720 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2721 my $herectx = $here . "\n";;
Andy Whitcroftf0556632008-10-15 22:02:23 -07002722 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002723
Andy Whitcroftf0556632008-10-15 22:02:23 -07002724 for (my $n = 0; $n < $cnt; $n++) {
2725 $herectx .= raw_line($linenr, $n) . "\n";;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002726 }
2727
2728 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002729 }
2730 }
2731
Andy Whitcroft653d4872007-06-23 17:16:34 -07002732# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002733 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002734 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002735 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002736 }
2737 }
2738
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002739# don't use deprecated functions
2740 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002741 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002742 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002743 }
2744 }
2745
2746# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002747 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2748 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002749 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002750 }
2751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002752# sys_open/read/write/close are not allowed in the kernel
2753 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
2754 ERROR("$1 is inappropriate in kernel code.\n" .
2755 $herecurr);
2756 }
2757
2758# filp_open is a backdoor for sys_open
2759 if ($line =~ /\b(filp_open)\b/) {
2760 ERROR("$1 is inappropriate in kernel code.\n" .
2761 $herecurr);
2762 }
2763
2764# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
2765 if ($line =~ /\b((?:read|write)[bwl])\b/) {
2766 ERROR("Use of $1 is deprecated: use $1_relaxed\n\t" .
2767 "with appropriate memory barriers instead.\n" .
2768 $herecurr);
2769 }
2770
2771# likewise, in/out[bwl] should be __raw_read/write[bwl]...
2772 if ($line =~ /\b((in|out)([bwl]))\b/) {
2773 my ($all, $pref, $suf) = ($1, $2, $3);
2774 $pref =~ s/in/read/;
2775 $pref =~ s/out/write/;
2776 ERROR("Use of $all is deprecated: use " .
2777 "__raw_$pref$suf\n\t" .
2778 "with appropriate memory barriers instead.\n" .
2779 $herecurr);
2780 }
2781
2782# dsb is too ARMish, and should usually be mb.
2783 if ($line =~ /\bdsb\b/) {
2784 WARN("Use of dsb is discouranged: prefer mb.\n" .
2785 $herecurr);
2786 }
2787
2788# unbounded string functions are overflow risks
2789 my %str_fns = (
2790 "sprintf" => "snprintf",
2791 "strcpy" => "strncpy",
2792 "strcat" => "strncat",
2793 "strcmp" => "strncmp",
2794 "strcasecmp" => "strncasecmp",
2795 "strchr" => "strnchr",
2796 "strstr" => "strnstr",
2797 "strlen" => "strnlen",
2798 );
2799 foreach my $k (keys %str_fns) {
2800 if ($line =~ /\b$k\b/) {
2801 ERROR("Use of $k is deprecated: " .
2802 "use $str_fns{$k} instead.\n" .
2803 $herecurr);
2804 }
2805 }
2806
Andy Whitcroft00df3442007-06-08 13:47:06 -07002807# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002808 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002809 WARN("if this code is redundant consider removing it\n"
2810 . $herecurr);
2811 }
2812# warn about #if 1
2813 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
2814 WARN("if this code is required consider removing"
2815 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002816 }
2817
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002818# check for needless kfree() checks
2819 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2820 my $expr = $1;
2821 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Wolfram Sang3c232142008-07-23 21:29:05 -07002822 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002823 }
2824 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07002825# check for needless usb_free_urb() checks
2826 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2827 my $expr = $1;
2828 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2829 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2830 }
2831 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002832
Patrick Pannuto1a15a252010-08-09 17:21:01 -07002833# prefer usleep_range over udelay
2834 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2835 # ignore udelay's < 10, however
2836 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2837 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2838 }
2839 }
2840
Patrick Pannuto09ef8722010-08-09 17:21:02 -07002841# warn about unexpectedly long msleep's
2842 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2843 if ($1 < 20) {
2844 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2845 }
2846 }
2847
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002848# check the patch for use of mdelay
2849 if ($line =~ /\bmdelay\s*\(/) {
2850 WARN("use of mdelay() found: msleep() is the preferred API.\n" . $line );
2851 }
2852
Andy Whitcroft00df3442007-06-08 13:47:06 -07002853# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002854# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002855# print "#ifdef in C files should be avoided\n";
2856# print "$herecurr";
2857# $clean = 0;
2858# }
2859
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002860# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002861 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002862 ERROR("exactly one space required after that #$1\n" . $herecurr);
2863 }
2864
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002865# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002866 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2867 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002868 my $which = $1;
2869 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002870 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002871 }
2872 }
2873# check for memory barriers without a comment.
2874 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2875 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002876 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002877 }
2878 }
2879# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002880 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002881 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002882 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002883
Tobias Klauserd4977c72010-05-24 14:33:30 -07002884# Check that the storage class is at the beginning of a declaration
2885 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2886 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2887 }
2888
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002889# check the location of the inline attribute, that it is between
2890# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002891 if ($line =~ /\b$Type\s+$Inline\b/ ||
2892 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002893 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2894 }
2895
Andy Whitcroft8905a672007-11-28 16:21:06 -08002896# Check for __inline__ and __inline, prefer inline
2897 if ($line =~ /\b(__inline__|__inline)\b/) {
2898 WARN("plain inline is preferred over $1\n" . $herecurr);
2899 }
2900
Joe Perches3d130fd2011-01-12 17:00:00 -08002901# Check for __attribute__ packed, prefer __packed
2902 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2903 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2904 }
2905
Joe Perches8f53a9b2010-03-05 13:43:48 -08002906# check for sizeof(&)
2907 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2908 WARN("sizeof(& should be avoided\n" . $herecurr);
2909 }
2910
Joe Perches428e2fd2011-05-24 17:13:39 -07002911# check for line continuations in quoted strings with odd counts of "
2912 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
2913 WARN("Avoid line continuations in quoted strings\n" . $herecurr);
2914 }
2915
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002916# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002917 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002918 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002919 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002920 my $function_name = $1;
2921 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002922
2923 my $s = $stat;
2924 if (defined $cond) {
2925 substr($s, 0, length($cond), '');
2926 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002927 if ($s =~ /^\s*;/ &&
2928 $function_name ne 'uninitialized_var')
2929 {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002930 WARN("externs should be avoided in .c files\n" . $herecurr);
2931 }
2932
2933 if ($paren_space =~ /\n/) {
2934 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2935 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002936
2937 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2938 $stat =~ /^.\s*extern\s+/)
2939 {
2940 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002941 }
2942
2943# checks for new __setup's
2944 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2945 my $name = $1;
2946
2947 if (!grep(/$name/, @setup_docs)) {
2948 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2949 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002950 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002951
2952# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08002953 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002954 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2955 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002956
Joe Perchescaf2a542011-01-12 16:59:56 -08002957# check for multiple semicolons
2958 if ($line =~ /;\s*;\s*$/) {
2959 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2960 }
2961
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002962# check for return codes on error paths
2963 if ($line =~ /\breturn\s+-\d+/) {
2964 ERROR("illegal return value, please use an error code\n" . $herecurr);
2965 }
2966
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002967# check for gcc specific __FUNCTION__
2968 if ($line =~ /__FUNCTION__/) {
2969 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2970 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002971
Thomas Gleixner4882720b2010-09-07 14:34:01 +00002972# check for semaphores initialized locked
2973 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002974 WARN("consider using a completion\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01002975
Andy Whitcroft773647a2008-03-28 14:15:58 -07002976 }
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002977# recommend kstrto* over simple_strto*
Andy Whitcroft773647a2008-03-28 14:15:58 -07002978 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07002979 WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002980 }
Michael Ellermanf3db6632008-07-23 21:28:57 -07002981# check for __initcall(), use device_initcall() explicitly please
2982 if ($line =~ /^.\s*__initcall\s*\(/) {
2983 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2984 }
Emese Revfy79404842010-03-05 13:43:53 -08002985# check for various ops structs, ensure they are const.
2986 my $struct_ops = qr{acpi_dock_ops|
2987 address_space_operations|
2988 backlight_ops|
2989 block_device_operations|
2990 dentry_operations|
2991 dev_pm_ops|
2992 dma_map_ops|
2993 extent_io_ops|
2994 file_lock_operations|
2995 file_operations|
2996 hv_ops|
2997 ide_dma_ops|
2998 intel_dvo_dev_ops|
2999 item_operations|
3000 iwl_ops|
3001 kgdb_arch|
3002 kgdb_io|
3003 kset_uevent_ops|
3004 lock_manager_operations|
3005 microcode_ops|
3006 mtrr_ops|
3007 neigh_ops|
3008 nlmsvc_binding|
3009 pci_raw_ops|
3010 pipe_buf_operations|
3011 platform_hibernation_ops|
3012 platform_suspend_ops|
3013 proto_ops|
3014 rpc_pipe_ops|
3015 seq_operations|
3016 snd_ac97_build_ops|
3017 soc_pcmcia_socket_ops|
3018 stacktrace_ops|
3019 sysfs_ops|
3020 tty_operations|
3021 usb_mon_operations|
3022 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003023 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003024 $line =~ /\bstruct\s+($struct_ops)\b/) {
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003025 WARN("struct $1 should normally be const\n" .
3026 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003027 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003028
3029# use of NR_CPUS is usually wrong
3030# ignore definitions of NR_CPUS and usage to define arrays as likely right
3031 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003032 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3033 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003034 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3035 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3036 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003037 {
3038 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3039 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003040
3041# check for %L{u,d,i} in strings
3042 my $string;
3043 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3044 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003045 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003046 if ($string =~ /(?<!%)%L[udi]/) {
3047 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3048 last;
3049 }
3050 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003051
3052# whine mightly about in_atomic
3053 if ($line =~ /\bin_atomic\s*\(/) {
3054 if ($realfile =~ m@^drivers/@) {
3055 ERROR("do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003056 } elsif ($realfile !~ m@^kernel/@) {
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003057 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3058 }
3059 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003060
3061# check for lockdep_set_novalidate_class
3062 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3063 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3064 if ($realfile !~ m@^kernel/lockdep@ &&
3065 $realfile !~ m@^include/linux/lockdep@ &&
3066 $realfile !~ m@^drivers/base/core@) {
3067 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3068 }
3069 }
Dave Jones88f88312011-01-12 16:59:59 -08003070
3071 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3072 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3073 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3074 }
Dave Jones309c00c2011-03-22 16:34:44 -07003075
3076 # Check for memset with swapped arguments
3077 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3078 ERROR("memset size is 3rd argument, not the second.\n" . $herecurr);
3079 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003080 }
3081
3082 # If we have no input at all, then there is nothing to report on
3083 # so just keep quiet.
3084 if ($#rawlines == -1) {
3085 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003086 }
3087
Andy Whitcroft8905a672007-11-28 16:21:06 -08003088 # In mailback mode only produce a report in the negative, for
3089 # things that appear to be patches.
3090 if ($mailback && ($clean == 1 || !$is_patch)) {
3091 exit(0);
3092 }
3093
3094 # This is not a patch, and we are are in 'no-patch' mode so
3095 # just keep quiet.
3096 if (!$chk_patch && !$is_patch) {
3097 exit(0);
3098 }
3099
3100 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003101 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003102 }
3103 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003104 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003105 }
3106
Andy Whitcroft8905a672007-11-28 16:21:06 -08003107 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003108 if ($summary && !($clean == 1 && $quiet == 1)) {
3109 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003110 print "total: $cnt_error errors, $cnt_warn warnings, " .
3111 (($check)? "$cnt_chk checks, " : "") .
3112 "$cnt_lines lines checked\n";
3113 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003114 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003115
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003116 if ($quiet == 0) {
3117 # If there were whitespace errors which cleanpatch can fix
3118 # then suggest that.
3119 if ($rpt_cleaners) {
3120 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3121 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003122 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003123 }
3124 }
3125
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003126 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003127 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003128 }
3129 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003130 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003131 print "are false positives report them to the maintainer, see\n";
3132 print "CHECKPATCH in MAINTAINERS.\n";
3133 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003134
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003135 return $clean;
3136}