blob: 3e9fee60642cd0ddf648602ffb243a0a70fc7bad [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy 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 Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9
10my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070011$P =~ s@.*/@@g;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070012
Joe Perches000d1cc12011-07-25 17:13:25 -070013my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070014
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070021my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070022my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080023my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070024my $file = 0;
25my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $summary = 1;
27my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080028my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070029my $show_types = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080031my %debug;
Joe Perches000d1cc12011-07-25 17:13:25 -070032my %ignore_type = ();
33my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070034my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070035my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080036my $max_line_length = 80;
Hannes Eder77f5b102009-09-21 17:04:37 -070037
38sub help {
39 my ($exitcode) = @_;
40
41 print << "EOM";
42Usage: $P [OPTION]... [FILE]...
43Version: $V
44
45Options:
46 -q, --quiet quiet
47 --no-tree run without a kernel tree
48 --no-signoff do not check for 'Signed-off-by' line
49 --patch treat FILE as patchfile (default)
50 --emacs emacs compile window format
51 --terse one line per report
52 -f, --file treat FILE as regular source file
53 --subjective, --strict enable more subjective tests
Joe Perches000d1cc12011-07-25 17:13:25 -070054 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080055 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070056 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070057 --root=PATH PATH to the kernel tree root
58 --no-summary suppress the per-file summary
59 --mailback only produce a report in case of warnings/errors
60 --summary-file include the filename in summary
61 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
62 'values', 'possible', 'type', and 'attr' (default
63 is all off)
64 --test-only=WORD report only warnings/errors containing WORD
65 literally
66 -h, --help, --version display this help and exit
67
68When FILE is - read standard input.
69EOM
70
71 exit($exitcode);
72}
73
Joe Perches000d1cc12011-07-25 17:13:25 -070074my $conf = which_conf($configuration_file);
75if (-f $conf) {
76 my @conf_args;
77 open(my $conffile, '<', "$conf")
78 or warn "$P: Can't find a readable $configuration_file file $!\n";
79
80 while (<$conffile>) {
81 my $line = $_;
82
83 $line =~ s/\s*\n?$//g;
84 $line =~ s/^\s*//g;
85 $line =~ s/\s+/ /g;
86
87 next if ($line =~ m/^\s*#/);
88 next if ($line =~ m/^\s*$/);
89
90 my @words = split(" ", $line);
91 foreach my $word (@words) {
92 last if ($word =~ m/^#/);
93 push (@conf_args, $word);
94 }
95 }
96 close($conffile);
97 unshift(@ARGV, @conf_args) if @conf_args;
98}
99
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700100GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700101 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700102 'tree!' => \$tree,
103 'signoff!' => \$chk_signoff,
104 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700105 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800106 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700107 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700108 'subjective!' => \$check,
109 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700110 'ignore=s' => \@ignore,
111 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800112 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800114 'summary!' => \$summary,
115 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800116 'summary-file!' => \$summary_file,
117
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800118 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700119 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700120 'h|help' => \$help,
121 'version' => \$help
122) or help(1);
123
124help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700125
126my $exit = 0;
127
128if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700129 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700130 exit(1);
131}
132
Joe Perches000d1cc12011-07-25 17:13:25 -0700133@ignore = split(/,/, join(',',@ignore));
134foreach my $word (@ignore) {
135 $word =~ s/\s*\n?$//g;
136 $word =~ s/^\s*//g;
137 $word =~ s/\s+/ /g;
138 $word =~ tr/[a-z]/[A-Z]/;
139
140 next if ($word =~ m/^\s*#/);
141 next if ($word =~ m/^\s*$/);
142
143 $ignore_type{$word}++;
144}
145
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800146my $dbg_values = 0;
147my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700148my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700149my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800150for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800151 ## no critic
152 eval "\${dbg_$key} = '$debug{$key}';";
153 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800154}
155
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700156my $rpt_cleaners = 0;
157
Andy Whitcroft8905a672007-11-28 16:21:06 -0800158if ($terse) {
159 $emacs = 1;
160 $quiet++;
161}
162
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700163if ($tree) {
164 if (defined $root) {
165 if (!top_of_kernel_tree($root)) {
166 die "$P: $root: --root does not point at a valid tree\n";
167 }
168 } else {
169 if (top_of_kernel_tree('.')) {
170 $root = '.';
171 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
172 top_of_kernel_tree($1)) {
173 $root = $1;
174 }
175 }
176
177 if (!defined $root) {
178 print "Must be run from the top-level dir. of a kernel tree\n";
179 exit(2);
180 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700181}
182
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700183my $emitted_corrupt = 0;
184
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700185our $Ident = qr{
186 [A-Za-z_][A-Za-z\d_]*
187 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
188 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700189our $Storage = qr{extern|static|asmlinkage};
190our $Sparse = qr{
191 __user|
192 __kernel|
193 __force|
194 __iomem|
195 __must_check|
196 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800197 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700198 __ref|
199 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700200 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800201
202# Notes to $Attribute:
203# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700204our $Attribute = qr{
205 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700206 __percpu|
207 __nocast|
208 __safe|
209 __bitwise__|
210 __packed__|
211 __packed2__|
212 __naked|
213 __maybe_unused|
214 __always_unused|
215 __noreturn|
216 __used|
217 __cold|
218 __noclone|
219 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700220 __read_mostly|
221 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800222 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700223 ____cacheline_aligned|
224 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800225 ____cacheline_internodealigned_in_smp|
226 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700227 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700228our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700229our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700230our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
231our $Lval = qr{$Ident(?:$Member)*};
232
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800233our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700234our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800235our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700236our $Operators = qr{
237 <=|>=|==|!=|
238 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800239 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700240 }x;
241
Andy Whitcroft8905a672007-11-28 16:21:06 -0800242our $NonptrType;
243our $Type;
244our $Declare;
245
Joe Perches15662b32011-10-31 17:13:12 -0700246our $NON_ASCII_UTF8 = qr{
247 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700248 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
249 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
250 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
251 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
252 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
253 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
254}x;
255
Joe Perches15662b32011-10-31 17:13:12 -0700256our $UTF8 = qr{
257 [\x09\x0A\x0D\x20-\x7E] # ASCII
258 | $NON_ASCII_UTF8
259}x;
260
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700261our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700262 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700263 atomic_t
264)};
265
Joe Perches691e6692010-03-05 13:43:51 -0800266our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700267 printk(?:_ratelimited|_once|)|
268 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
269 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700270 panic|
271 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800272)};
273
Joe Perches20112472011-07-25 17:13:23 -0700274our $signature_tags = qr{(?xi:
275 Signed-off-by:|
276 Acked-by:|
277 Tested-by:|
278 Reviewed-by:|
279 Reported-by:|
280 To:|
281 Cc:
282)};
283
Andy Whitcroft8905a672007-11-28 16:21:06 -0800284our @typeList = (
285 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700286 qr{(?:unsigned\s+)?char},
287 qr{(?:unsigned\s+)?short},
288 qr{(?:unsigned\s+)?int},
289 qr{(?:unsigned\s+)?long},
290 qr{(?:unsigned\s+)?long\s+int},
291 qr{(?:unsigned\s+)?long\s+long},
292 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800293 qr{unsigned},
294 qr{float},
295 qr{double},
296 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800297 qr{struct\s+$Ident},
298 qr{union\s+$Ident},
299 qr{enum\s+$Ident},
300 qr{${Ident}_t},
301 qr{${Ident}_handler},
302 qr{${Ident}_handler_fn},
303);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700304our @modifierList = (
305 qr{fastcall},
306);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800307
Wolfram Sang7840a942010-08-09 17:20:57 -0700308our $allowed_asm_includes = qr{(?x:
309 irq|
310 memory
311)};
312# memory.h: ARM has a custom one
313
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700315 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
316 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700317 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800318 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700319 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800320 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800321 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700322 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700323 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800324 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700325 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800326 }x;
327 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700328 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700329 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700330 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800331 }x;
332 $Declare = qr{(?:$Storage\s+)?$Type};
333}
334build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700335
Joe Perches7d2367a2011-07-25 17:13:22 -0700336
337our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700338
339# Using $balanced_parens, $LvalOrFunc, or $FuncArg
340# requires at least perl version v5.10.0
341# Any use must be runtime checked with $^V
342
343our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
344our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800345our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700346
347sub deparenthesize {
348 my ($string) = @_;
349 return "" if (!defined($string));
350 $string =~ s@^\s*\(\s*@@g;
351 $string =~ s@\s*\)\s*$@@g;
352 $string =~ s@\s+@ @g;
353 return $string;
354}
355
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700356$chk_signoff = 0 if ($file);
357
Andy Whitcroft00df3442007-06-08 13:47:06 -0700358my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800359my @lines = ();
360my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700361for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800362 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700363 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800364 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700365 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800366 } elsif ($filename eq '-') {
367 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700368 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800369 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700370 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700371 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800372 if ($filename eq '-') {
373 $vname = 'Your patch';
374 } else {
375 $vname = $filename;
376 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800377 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700378 chomp;
379 push(@rawlines, $_);
380 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800381 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800382 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700383 $exit = 1;
384 }
385 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800386 @lines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700387}
388
389exit($exit);
390
391sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392 my ($root) = @_;
393
394 my @tree_check = (
395 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
396 "README", "Documentation", "arch", "include", "drivers",
397 "fs", "init", "ipc", "kernel", "lib", "scripts",
398 );
399
400 foreach my $check (@tree_check) {
401 if (! -e $root . '/' . $check) {
402 return 0;
403 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700404 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700405 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700406}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700407
Joe Perches20112472011-07-25 17:13:23 -0700408sub parse_email {
409 my ($formatted_email) = @_;
410
411 my $name = "";
412 my $address = "";
413 my $comment = "";
414
415 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
416 $name = $1;
417 $address = $2;
418 $comment = $3 if defined $3;
419 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
420 $address = $1;
421 $comment = $2 if defined $2;
422 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
423 $address = $1;
424 $comment = $2 if defined $2;
425 $formatted_email =~ s/$address.*$//;
426 $name = $formatted_email;
427 $name =~ s/^\s+|\s+$//g;
428 $name =~ s/^\"|\"$//g;
429 # If there's a name left after stripping spaces and
430 # leading quotes, and the address doesn't have both
431 # leading and trailing angle brackets, the address
432 # is invalid. ie:
433 # "joe smith joe@smith.com" bad
434 # "joe smith <joe@smith.com" bad
435 if ($name ne "" && $address !~ /^<[^>]+>$/) {
436 $name = "";
437 $address = "";
438 $comment = "";
439 }
440 }
441
442 $name =~ s/^\s+|\s+$//g;
443 $name =~ s/^\"|\"$//g;
444 $address =~ s/^\s+|\s+$//g;
445 $address =~ s/^\<|\>$//g;
446
447 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
448 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
449 $name = "\"$name\"";
450 }
451
452 return ($name, $address, $comment);
453}
454
455sub format_email {
456 my ($name, $address) = @_;
457
458 my $formatted_email;
459
460 $name =~ s/^\s+|\s+$//g;
461 $name =~ s/^\"|\"$//g;
462 $address =~ s/^\s+|\s+$//g;
463
464 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
465 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
466 $name = "\"$name\"";
467 }
468
469 if ("$name" eq "") {
470 $formatted_email = "$address";
471 } else {
472 $formatted_email = "$name <$address>";
473 }
474
475 return $formatted_email;
476}
477
Joe Perches000d1cc12011-07-25 17:13:25 -0700478sub which_conf {
479 my ($conf) = @_;
480
481 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
482 if (-e "$path/$conf") {
483 return "$path/$conf";
484 }
485 }
486
487 return "";
488}
489
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700490sub expand_tabs {
491 my ($str) = @_;
492
493 my $res = '';
494 my $n = 0;
495 for my $c (split(//, $str)) {
496 if ($c eq "\t") {
497 $res .= ' ';
498 $n++;
499 for (; ($n % 8) != 0; $n++) {
500 $res .= ' ';
501 }
502 next;
503 }
504 $res .= $c;
505 $n++;
506 }
507
508 return $res;
509}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700510sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700511 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700512 return $res;
513}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700514
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700515sub line_stats {
516 my ($line) = @_;
517
518 # Drop the diff line leader and expand tabs
519 $line =~ s/^.//;
520 $line = expand_tabs($line);
521
522 # Pick the indent from the front of the line.
523 my ($white) = ($line =~ /^(\s*)/);
524
525 return (length($line), length($white));
526}
527
Andy Whitcroft773647a2008-03-28 14:15:58 -0700528my $sanitise_quote = '';
529
530sub sanitise_line_reset {
531 my ($in_comment) = @_;
532
533 if ($in_comment) {
534 $sanitise_quote = '*/';
535 } else {
536 $sanitise_quote = '';
537 }
538}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700539sub sanitise_line {
540 my ($line) = @_;
541
542 my $res = '';
543 my $l = '';
544
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800545 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700546 my $off = 0;
547 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700548
Andy Whitcroft773647a2008-03-28 14:15:58 -0700549 # Always copy over the diff marker.
550 $res = substr($line, 0, 1);
551
552 for ($off = 1; $off < length($line); $off++) {
553 $c = substr($line, $off, 1);
554
555 # Comments we are wacking completly including the begin
556 # and end, all to $;.
557 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
558 $sanitise_quote = '*/';
559
560 substr($res, $off, 2, "$;$;");
561 $off++;
562 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800563 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700564 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700565 $sanitise_quote = '';
566 substr($res, $off, 2, "$;$;");
567 $off++;
568 next;
569 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700570 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
571 $sanitise_quote = '//';
572
573 substr($res, $off, 2, $sanitise_quote);
574 $off++;
575 next;
576 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700577
578 # A \ in a string means ignore the next character.
579 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
580 $c eq "\\") {
581 substr($res, $off, 2, 'XX');
582 $off++;
583 next;
584 }
585 # Regular quotes.
586 if ($c eq "'" || $c eq '"') {
587 if ($sanitise_quote eq '') {
588 $sanitise_quote = $c;
589
590 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700591 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700592 } elsif ($sanitise_quote eq $c) {
593 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700594 }
595 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700596
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800597 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700598 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
599 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700600 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
601 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700602 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
603 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700604 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700605 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700606 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800607 }
608
Daniel Walker113f04a2009-09-21 17:04:35 -0700609 if ($sanitise_quote eq '//') {
610 $sanitise_quote = '';
611 }
612
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800613 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700614 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800615 my $clean = 'X' x length($1);
616 $res =~ s@\<.*\>@<$clean>@;
617
618 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700619 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800620 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700621 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800622 }
623
Andy Whitcroft00df3442007-06-08 13:47:06 -0700624 return $res;
625}
626
Andy Whitcroft8905a672007-11-28 16:21:06 -0800627sub ctx_statement_block {
628 my ($linenr, $remain, $off) = @_;
629 my $line = $linenr - 1;
630 my $blk = '';
631 my $soff = $off;
632 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700633 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800634
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800635 my $loff = 0;
636
Andy Whitcroft8905a672007-11-28 16:21:06 -0800637 my $type = '';
638 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800639 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800640 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800641 my $c;
642 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800643
644 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800645 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800646 @stack = (['', 0]) if ($#stack == -1);
647
Andy Whitcroft773647a2008-03-28 14:15:58 -0700648 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800649 # If we are about to drop off the end, pull in more
650 # context.
651 if ($off >= $len) {
652 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700653 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800654 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800655 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800656 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800657 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800658 $len = length($blk);
659 $line++;
660 last;
661 }
662 # Bail if there is no further context.
663 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800664 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800665 last;
666 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800667 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
668 $level++;
669 $type = '#';
670 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800671 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800672 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800673 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800674 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800675
Andy Whitcroft773647a2008-03-28 14:15:58 -0700676 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800677
678 # Handle nested #if/#else.
679 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
680 push(@stack, [ $type, $level ]);
681 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
682 ($type, $level) = @{$stack[$#stack - 1]};
683 } elsif ($remainder =~ /^#\s*endif\b/) {
684 ($type, $level) = @{pop(@stack)};
685 }
686
Andy Whitcroft8905a672007-11-28 16:21:06 -0800687 # Statement ends at the ';' or a close '}' at the
688 # outermost level.
689 if ($level == 0 && $c eq ';') {
690 last;
691 }
692
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800693 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700694 if ($level == 0 && $coff_set == 0 &&
695 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
696 $remainder =~ /^(else)(?:\s|{)/ &&
697 $remainder !~ /^else\s+if\b/) {
698 $coff = $off + length($1) - 1;
699 $coff_set = 1;
700 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
701 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800702 }
703
Andy Whitcroft8905a672007-11-28 16:21:06 -0800704 if (($type eq '' || $type eq '(') && $c eq '(') {
705 $level++;
706 $type = '(';
707 }
708 if ($type eq '(' && $c eq ')') {
709 $level--;
710 $type = ($level != 0)? '(' : '';
711
712 if ($level == 0 && $coff < $soff) {
713 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700714 $coff_set = 1;
715 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800716 }
717 }
718 if (($type eq '' || $type eq '{') && $c eq '{') {
719 $level++;
720 $type = '{';
721 }
722 if ($type eq '{' && $c eq '}') {
723 $level--;
724 $type = ($level != 0)? '{' : '';
725
726 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700727 if (substr($blk, $off + 1, 1) eq ';') {
728 $off++;
729 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800730 last;
731 }
732 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800733 # Preprocessor commands end at the newline unless escaped.
734 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
735 $level--;
736 $type = '';
737 $off++;
738 last;
739 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800740 $off++;
741 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700742 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800743 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700744 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800745 $line++;
746 $remain--;
747 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800748
749 my $statement = substr($blk, $soff, $off - $soff + 1);
750 my $condition = substr($blk, $soff, $coff - $soff + 1);
751
752 #warn "STATEMENT<$statement>\n";
753 #warn "CONDITION<$condition>\n";
754
Andy Whitcroft773647a2008-03-28 14:15:58 -0700755 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800756
757 return ($statement, $condition,
758 $line, $remain + 1, $off - $loff + 1, $level);
759}
760
Andy Whitcroftcf655042008-03-04 14:28:20 -0800761sub statement_lines {
762 my ($stmt) = @_;
763
764 # Strip the diff line prefixes and rip blank lines at start and end.
765 $stmt =~ s/(^|\n)./$1/g;
766 $stmt =~ s/^\s*//;
767 $stmt =~ s/\s*$//;
768
769 my @stmt_lines = ($stmt =~ /\n/g);
770
771 return $#stmt_lines + 2;
772}
773
774sub statement_rawlines {
775 my ($stmt) = @_;
776
777 my @stmt_lines = ($stmt =~ /\n/g);
778
779 return $#stmt_lines + 2;
780}
781
782sub statement_block_size {
783 my ($stmt) = @_;
784
785 $stmt =~ s/(^|\n)./$1/g;
786 $stmt =~ s/^\s*{//;
787 $stmt =~ s/}\s*$//;
788 $stmt =~ s/^\s*//;
789 $stmt =~ s/\s*$//;
790
791 my @stmt_lines = ($stmt =~ /\n/g);
792 my @stmt_statements = ($stmt =~ /;/g);
793
794 my $stmt_lines = $#stmt_lines + 2;
795 my $stmt_statements = $#stmt_statements + 1;
796
797 if ($stmt_lines > $stmt_statements) {
798 return $stmt_lines;
799 } else {
800 return $stmt_statements;
801 }
802}
803
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800804sub ctx_statement_full {
805 my ($linenr, $remain, $off) = @_;
806 my ($statement, $condition, $level);
807
808 my (@chunks);
809
Andy Whitcroftcf655042008-03-04 14:28:20 -0800810 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800811 ($statement, $condition, $linenr, $remain, $off, $level) =
812 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700813 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800814 push(@chunks, [ $condition, $statement ]);
815 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
816 return ($level, $linenr, @chunks);
817 }
818
819 # Pull in the following conditional/block pairs and see if they
820 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800821 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800822 ($statement, $condition, $linenr, $remain, $off, $level) =
823 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800824 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700825 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800826 #print "C: push\n";
827 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800828 }
829
830 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800831}
832
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700833sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700834 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700835 my $line;
836 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700837 my $blk = '';
838 my @o;
839 my @c;
840 my @res = ();
841
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700842 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800843 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700844 for ($line = $start; $remain > 0; $line++) {
845 next if ($rawlines[$line] =~ /^-/);
846 $remain--;
847
848 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800849
850 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700851 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800852 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700853 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800854 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700855 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800856 $level = pop(@stack);
857 }
858
Andy Whitcroft01464f32010-10-26 14:23:19 -0700859 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700860 ##print "C<$c>L<$level><$open$close>O<$off>\n";
861 if ($off > 0) {
862 $off--;
863 next;
864 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700865
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700866 if ($c eq $close && $level > 0) {
867 $level--;
868 last if ($level == 0);
869 } elsif ($c eq $open) {
870 $level++;
871 }
872 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700873
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700874 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700875 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700876 }
877
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700878 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700879 }
880
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700881 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700882}
883sub ctx_block_outer {
884 my ($linenr, $remain) = @_;
885
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700886 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
887 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700888}
889sub ctx_block {
890 my ($linenr, $remain) = @_;
891
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700892 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
893 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700894}
895sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700896 my ($linenr, $remain, $off) = @_;
897
898 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
899 return @r;
900}
901sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700902 my ($linenr, $remain) = @_;
903
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700904 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700905}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700906sub ctx_statement_level {
907 my ($linenr, $remain, $off) = @_;
908
909 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
910}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700911
912sub ctx_locate_comment {
913 my ($first_line, $end_line) = @_;
914
915 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700916 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700917 return $current_comment if (defined $current_comment);
918
919 # Look through the context and try and figure out if there is a
920 # comment.
921 my $in_comment = 0;
922 $current_comment = '';
923 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700924 my $line = $rawlines[$linenr - 1];
925 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700926 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
927 $in_comment = 1;
928 }
929 if ($line =~ m@/\*@) {
930 $in_comment = 1;
931 }
932 if (!$in_comment && $current_comment ne '') {
933 $current_comment = '';
934 }
935 $current_comment .= $line . "\n" if ($in_comment);
936 if ($line =~ m@\*/@) {
937 $in_comment = 0;
938 }
939 }
940
941 chomp($current_comment);
942 return($current_comment);
943}
944sub ctx_has_comment {
945 my ($first_line, $end_line) = @_;
946 my $cmt = ctx_locate_comment($first_line, $end_line);
947
Andy Whitcroft00df3442007-06-08 13:47:06 -0700948 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700949 ##print "CMMT: $cmt\n";
950
951 return ($cmt ne '');
952}
953
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700954sub raw_line {
955 my ($linenr, $cnt) = @_;
956
957 my $offset = $linenr - 1;
958 $cnt++;
959
960 my $line;
961 while ($cnt) {
962 $line = $rawlines[$offset++];
963 next if (defined($line) && $line =~ /^-/);
964 $cnt--;
965 }
966
967 return $line;
968}
969
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700970sub cat_vet {
971 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700972 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700973
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700974 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700975 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
976 $res .= $1;
977 if ($2 ne '') {
978 $coded = sprintf("^%c", unpack('C', $2) + 64);
979 $res .= $coded;
980 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700981 }
982 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700983
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700984 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700985}
986
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800987my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800988my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800989my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700990my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800991
992sub annotate_reset {
993 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800994 $av_pending = '_';
995 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -0700996 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800997}
998
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700999sub annotate_values {
1000 my ($stream, $type) = @_;
1001
1002 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001003 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001004 my $cur = $stream;
1005
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001006 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001007
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001008 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001009 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001010 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001011 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001012 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001013 print "WS($1)\n" if ($dbg_values > 1);
1014 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001015 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001016 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001017 }
1018
Florian Micklerc023e4732011-01-12 16:59:58 -08001019 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001020 print "CAST($1)\n" if ($dbg_values > 1);
1021 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001022 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001023
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001024 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001025 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001026 $type = 'T';
1027
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001028 } elsif ($cur =~ /^($Modifier)\s*/) {
1029 print "MODIFIER($1)\n" if ($dbg_values > 1);
1030 $type = 'T';
1031
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001032 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001033 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001034 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001035 push(@av_paren_type, $type);
1036 if ($2 ne '') {
1037 $av_pending = 'N';
1038 }
1039 $type = 'E';
1040
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001041 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001042 print "UNDEF($1)\n" if ($dbg_values > 1);
1043 $av_preprocessor = 1;
1044 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001045
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001046 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001047 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001048 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001049
1050 push(@av_paren_type, $type);
1051 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001052 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001053
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001054 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001055 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1056 $av_preprocessor = 1;
1057
1058 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1059
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001060 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001061
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001062 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001063 print "PRE_END($1)\n" if ($dbg_values > 1);
1064
1065 $av_preprocessor = 1;
1066
1067 # Assume all arms of the conditional end as this
1068 # one does, and continue as if the #endif was not here.
1069 pop(@av_paren_type);
1070 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001071 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001072
1073 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001074 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001075
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001076 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1077 print "ATTR($1)\n" if ($dbg_values > 1);
1078 $av_pending = $type;
1079 $type = 'N';
1080
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001081 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001082 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001083 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001084 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001085 }
1086 $type = 'N';
1087
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001088 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001089 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001090 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001091 $type = 'N';
1092
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001093 } elsif ($cur =~/^(case)/o) {
1094 print "CASE($1)\n" if ($dbg_values > 1);
1095 $av_pend_colon = 'C';
1096 $type = 'N';
1097
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001098 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001099 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001100 $type = 'N';
1101
1102 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001103 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001104 push(@av_paren_type, $av_pending);
1105 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001106 $type = 'N';
1107
1108 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001109 my $new_type = pop(@av_paren_type);
1110 if ($new_type ne '_') {
1111 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001112 print "PAREN('$1') -> $type\n"
1113 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001114 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001115 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001116 }
1117
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001118 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001119 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001120 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001121 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001122
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001123 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1124 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001125 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001126 } elsif ($type eq 'E') {
1127 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001128 }
1129 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1130 $type = 'V';
1131
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001132 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001133 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001134 $type = 'V';
1135
1136 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001137 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001138 $type = 'N';
1139
Andy Whitcroftcf655042008-03-04 14:28:20 -08001140 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001141 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001142 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001143 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001144
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001145 } elsif ($cur =~/^(,)/) {
1146 print "COMMA($1)\n" if ($dbg_values > 1);
1147 $type = 'C';
1148
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001149 } elsif ($cur =~ /^(\?)/o) {
1150 print "QUESTION($1)\n" if ($dbg_values > 1);
1151 $type = 'N';
1152
1153 } elsif ($cur =~ /^(:)/o) {
1154 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1155
1156 substr($var, length($res), 1, $av_pend_colon);
1157 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1158 $type = 'E';
1159 } else {
1160 $type = 'N';
1161 }
1162 $av_pend_colon = 'O';
1163
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001164 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001165 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001166 $type = 'N';
1167
Andy Whitcroft0d413862008-10-15 22:02:16 -07001168 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001169 my $variant;
1170
1171 print "OPV($1)\n" if ($dbg_values > 1);
1172 if ($type eq 'V') {
1173 $variant = 'B';
1174 } else {
1175 $variant = 'U';
1176 }
1177
1178 substr($var, length($res), 1, $variant);
1179 $type = 'N';
1180
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001181 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001182 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001183 if ($1 ne '++' && $1 ne '--') {
1184 $type = 'N';
1185 }
1186
1187 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001188 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001189 }
1190 if (defined $1) {
1191 $cur = substr($cur, length($1));
1192 $res .= $type x length($1);
1193 }
1194 }
1195
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001196 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001197}
1198
Andy Whitcroft8905a672007-11-28 16:21:06 -08001199sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001200 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001201 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001202 ^(?:
1203 $Modifier|
1204 $Storage|
1205 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001206 DEFINE_\S+
1207 )$|
1208 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001209 goto|
1210 return|
1211 case|
1212 else|
1213 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001214 do|
1215 \#|
1216 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001217 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001218 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001219 )}x;
1220 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1221 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001222 # Check for modifiers.
1223 $possible =~ s/\s*$Storage\s*//g;
1224 $possible =~ s/\s*$Sparse\s*//g;
1225 if ($possible =~ /^\s*$/) {
1226
1227 } elsif ($possible =~ /\s/) {
1228 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001229 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001230 if ($modifier !~ $notPermitted) {
1231 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1232 push(@modifierList, $modifier);
1233 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001234 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001235
1236 } else {
1237 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1238 push(@typeList, $possible);
1239 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001240 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001241 } else {
1242 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001243 }
1244}
1245
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001246my $prefix = '';
1247
Joe Perches000d1cc12011-07-25 17:13:25 -07001248sub show_type {
1249 return !defined $ignore_type{$_[0]};
1250}
1251
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001252sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001253 if (!show_type($_[1]) ||
1254 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001255 return 0;
1256 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001257 my $line;
1258 if ($show_types) {
1259 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1260 } else {
1261 $line = "$prefix$_[0]: $_[2]\n";
1262 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001263 $line = (split('\n', $line))[0] . "\n" if ($terse);
1264
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001265 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001266
1267 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001268}
1269sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001270 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001271}
Joe Perches000d1cc12011-07-25 17:13:25 -07001272
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001273sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001274 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001275 our $clean = 0;
1276 our $cnt_error++;
1277 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001278}
1279sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001280 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001281 our $clean = 0;
1282 our $cnt_warn++;
1283 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001284}
1285sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001286 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001287 our $clean = 0;
1288 our $cnt_chk++;
1289 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001290}
1291
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001292sub check_absolute_file {
1293 my ($absolute, $herecurr) = @_;
1294 my $file = $absolute;
1295
1296 ##print "absolute<$absolute>\n";
1297
1298 # See if any suffix of this path is a path within the tree.
1299 while ($file =~ s@^[^/]*/@@) {
1300 if (-f "$root/$file") {
1301 ##print "file<$file>\n";
1302 last;
1303 }
1304 }
1305 if (! -f _) {
1306 return 0;
1307 }
1308
1309 # It is, so see if the prefix is acceptable.
1310 my $prefix = $absolute;
1311 substr($prefix, -length($file)) = '';
1312
1313 ##print "prefix<$prefix>\n";
1314 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001315 WARN("USE_RELATIVE_PATH",
1316 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001317 }
1318}
1319
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001320sub pos_last_openparen {
1321 my ($line) = @_;
1322
1323 my $pos = 0;
1324
1325 my $opens = $line =~ tr/\(/\(/;
1326 my $closes = $line =~ tr/\)/\)/;
1327
1328 my $last_openparen = 0;
1329
1330 if (($opens == 0) || ($closes >= $opens)) {
1331 return -1;
1332 }
1333
1334 my $len = length($line);
1335
1336 for ($pos = 0; $pos < $len; $pos++) {
1337 my $string = substr($line, $pos);
1338 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1339 $pos += length($1) - 1;
1340 } elsif (substr($line, $pos, 1) eq '(') {
1341 $last_openparen = $pos;
1342 } elsif (index($string, '(') == -1) {
1343 last;
1344 }
1345 }
1346
1347 return $last_openparen + 1;
1348}
1349
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001350sub process {
1351 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001352
1353 my $linenr=0;
1354 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001355 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001356 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001357 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001358
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001359 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001360 my $indent;
1361 my $previndent=0;
1362 my $stashindent=0;
1363
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001364 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001365 my $signoff = 0;
1366 my $is_patch = 0;
1367
Joe Perches15662b32011-10-31 17:13:12 -07001368 my $in_header_lines = 1;
1369 my $in_commit_log = 0; #Scanning lines before patch
1370
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001371 my $non_utf8_charset = 0;
1372
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001373 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001374 our $cnt_lines = 0;
1375 our $cnt_error = 0;
1376 our $cnt_warn = 0;
1377 our $cnt_chk = 0;
1378
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001379 # Trace the real file/line as we go.
1380 my $realfile = '';
1381 my $realline = 0;
1382 my $realcnt = 0;
1383 my $here = '';
1384 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001385 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001386 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001387 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001388
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001389 my $prev_values = 'E';
1390
1391 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001392 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001393 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001394 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001395 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001396
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001397 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001398 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001399 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001400 my @setup_docs = ();
1401 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001402
1403 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001404 my $line;
1405 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001406 $linenr++;
1407 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001408
Andy Whitcroft773647a2008-03-28 14:15:58 -07001409 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001410 $setup_docs = 0;
1411 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1412 $setup_docs = 1;
1413 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001414 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001415 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001416 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1417 $realline=$1-1;
1418 if (defined $2) {
1419 $realcnt=$3+1;
1420 } else {
1421 $realcnt=1+1;
1422 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001423 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001424
1425 # Guestimate if this is a continuing comment. Run
1426 # the context looking for a comment "edge". If this
1427 # edge is a close comment then we must be in a comment
1428 # at context start.
1429 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001430 my $cnt = $realcnt;
1431 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1432 next if (defined $rawlines[$ln - 1] &&
1433 $rawlines[$ln - 1] =~ /^-/);
1434 $cnt--;
1435 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001436 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001437 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1438 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1439 ($edge) = $1;
1440 last;
1441 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001442 }
1443 if (defined $edge && $edge eq '*/') {
1444 $in_comment = 1;
1445 }
1446
1447 # Guestimate if this is a continuing comment. If this
1448 # is the start of a diff block and this line starts
1449 # ' *' then it is very likely a comment.
1450 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001451 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001452 {
1453 $in_comment = 1;
1454 }
1455
1456 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1457 sanitise_line_reset($in_comment);
1458
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001459 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001460 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001461 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001462 $line = sanitise_line($rawline);
1463 }
1464 push(@lines, $line);
1465
1466 if ($realcnt > 1) {
1467 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1468 } else {
1469 $realcnt = 0;
1470 }
1471
1472 #print "==>$rawline\n";
1473 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001474
1475 if ($setup_docs && $line =~ /^\+/) {
1476 push(@setup_docs, $line);
1477 }
1478 }
1479
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001480 $prefix = '';
1481
Andy Whitcroft773647a2008-03-28 14:15:58 -07001482 $realcnt = 0;
1483 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001484 foreach my $line (@lines) {
1485 $linenr++;
1486
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001487 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001488
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001489#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001490 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001491 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001492 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001493 $realline=$1-1;
1494 if (defined $2) {
1495 $realcnt=$3+1;
1496 } else {
1497 $realcnt=1+1;
1498 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001499 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001500 $prev_values = 'E';
1501
Andy Whitcroft773647a2008-03-28 14:15:58 -07001502 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001503 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001504 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001505 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001506 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001507
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001508# track the line number as we move through the hunk, note that
1509# new versions of GNU diff omit the leading space on completely
1510# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001511 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001512 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001513 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001514
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001515 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001516 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001517
1518 # Track the previous line.
1519 ($prevline, $stashline) = ($stashline, $line);
1520 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001521 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1522
Andy Whitcroft773647a2008-03-28 14:15:58 -07001523 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001524
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001525 } elsif ($realcnt == 1) {
1526 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001527 }
1528
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001529 my $hunk_line = ($realcnt != 0);
1530
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001531#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001532 $prefix = "$filename:$realline: " if ($emacs && $file);
1533 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1534
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001535 $here = "#$linenr: " if (!$file);
1536 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001537
1538 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001539 if ($line =~ /^diff --git.*?(\S+)$/) {
1540 $realfile = $1;
1541 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001542 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001543 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001544 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001545 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001546 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001547
1548 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001549 if (!$file && $tree && $p1_prefix ne '' &&
1550 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001551 WARN("PATCH_PREFIX",
1552 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001553 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001554
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001555 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001556 ERROR("MODIFIED_INCLUDE_ASM",
1557 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
Andy Whitcroft773647a2008-03-28 14:15:58 -07001558 }
1559 next;
1560 }
1561
Randy Dunlap389834b2007-06-08 13:47:03 -07001562 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001563
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001564 my $hereline = "$here\n$rawline\n";
1565 my $herecurr = "$here\n$rawline\n";
1566 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001567
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001568 $cnt_lines++ if ($realcnt != 0);
1569
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001570# Check for incorrect file permissions
1571 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1572 my $permhere = $here . "FILE: $realfile\n";
1573 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001574 ERROR("EXECUTE_PERMISSIONS",
1575 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001576 }
1577 }
1578
Joe Perches20112472011-07-25 17:13:23 -07001579# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001580 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001581 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001582 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001583 }
1584
1585# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001586 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001587 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001588 my $space_before = $1;
1589 my $sign_off = $2;
1590 my $space_after = $3;
1591 my $email = $4;
1592 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1593
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001594 if ($sign_off !~ /$signature_tags/) {
1595 WARN("BAD_SIGN_OFF",
1596 "Non-standard signature: $sign_off\n" . $herecurr);
1597 }
Joe Perches20112472011-07-25 17:13:23 -07001598 if (defined $space_before && $space_before ne "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001599 WARN("BAD_SIGN_OFF",
1600 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001601 }
Joe Perches20112472011-07-25 17:13:23 -07001602 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001603 WARN("BAD_SIGN_OFF",
1604 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001605 }
1606 if (!defined $space_after || $space_after ne " ") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001607 WARN("BAD_SIGN_OFF",
1608 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001609 }
1610
1611 my ($email_name, $email_address, $comment) = parse_email($email);
1612 my $suggested_email = format_email(($email_name, $email_address));
1613 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001614 ERROR("BAD_SIGN_OFF",
1615 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001616 } else {
1617 my $dequoted = $suggested_email;
1618 $dequoted =~ s/^"//;
1619 $dequoted =~ s/" </ </;
1620 # Don't force email to have quotes
1621 # Allow just an angle bracketed address
1622 if ("$dequoted$comment" ne $email &&
1623 "<$email_address>$comment" ne $email &&
1624 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001625 WARN("BAD_SIGN_OFF",
1626 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001627 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001628 }
1629 }
1630
Andy Whitcroft00df3442007-06-08 13:47:06 -07001631# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001632 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001633 ERROR("CORRUPTED_PATCH",
1634 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001635 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001636 }
1637
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001638# Check for absolute kernel paths.
1639 if ($tree) {
1640 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1641 my $file = $1;
1642
1643 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1644 check_absolute_file($1, $herecurr)) {
1645 #
1646 } else {
1647 check_absolute_file($file, $herecurr);
1648 }
1649 }
1650 }
1651
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001652# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1653 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001654 $rawline !~ m/^$UTF8*$/) {
1655 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1656
1657 my $blank = copy_spacing($rawline);
1658 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1659 my $hereptr = "$hereline$ptr\n";
1660
Joe Perches34d99212011-07-25 17:13:26 -07001661 CHK("INVALID_UTF8",
1662 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001663 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001664
Joe Perches15662b32011-10-31 17:13:12 -07001665# Check if it's the start of a commit log
1666# (not a header line and we haven't seen the patch filename)
1667 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001668 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001669 $in_header_lines = 0;
1670 $in_commit_log = 1;
1671 }
1672
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001673# Check if there is UTF-8 in a commit log when a mail header has explicitly
1674# declined it, i.e defined some charset where it is missing.
1675 if ($in_header_lines &&
1676 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1677 $1 !~ /utf-8/i) {
1678 $non_utf8_charset = 1;
1679 }
1680
1681 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001682 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001683 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001684 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1685 }
1686
Andy Whitcroft306708542008-10-15 22:02:28 -07001687# ignore non-hunk lines and lines being removed
1688 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001689
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001690#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001691 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001692 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001693 ERROR("DOS_LINE_ENDINGS",
1694 "DOS line endings\n" . $herevet);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001695
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001696 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1697 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001698 ERROR("TRAILING_WHITESPACE",
1699 "trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001700 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001701 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001702
Andi Kleen33549572010-05-24 14:33:29 -07001703# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001704# Only applies when adding the entry originally, after that we do not have
1705# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001706 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001707 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001708 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001709 my $cnt = $realcnt;
1710 my $ln = $linenr + 1;
1711 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001712 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001713 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001714 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001715 $f = $lines[$ln - 1];
1716 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1717 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001718
1719 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001720
1721 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1722 $is_start = 1;
1723 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1724 $length = -1;
1725 }
1726
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001727 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001728 $f =~ s/#.*//;
1729 $f =~ s/^\s+//;
1730 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001731 if ($f =~ /^\s*config\s/) {
1732 $is_end = 1;
1733 last;
1734 }
Andi Kleen33549572010-05-24 14:33:29 -07001735 $length++;
1736 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001737 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001738 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1739 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001740 }
1741
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001742# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1743 if ($realfile =~ /Kconfig/ &&
1744 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1745 WARN("CONFIG_EXPERIMENTAL",
1746 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1747 }
1748
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001749 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1750 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1751 my $flag = $1;
1752 my $replacement = {
1753 'EXTRA_AFLAGS' => 'asflags-y',
1754 'EXTRA_CFLAGS' => 'ccflags-y',
1755 'EXTRA_CPPFLAGS' => 'cppflags-y',
1756 'EXTRA_LDFLAGS' => 'ldflags-y',
1757 };
1758
1759 WARN("DEPRECATED_VARIABLE",
1760 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1761 }
1762
Andy Whitcroft5368df202008-10-15 22:02:27 -07001763# check we are in a valid source file if not then ignore this hunk
1764 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1765
Joe Perches6cd7f382012-12-17 16:01:54 -08001766#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001767 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001768 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001769 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001770 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08001771 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001772 {
Joe Perches000d1cc12011-07-25 17:13:25 -07001773 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08001774 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001775 }
1776
Josh Triplettca56dc02012-03-23 15:02:21 -07001777# Check for user-visible strings broken across lines, which breaks the ability
1778# to grep for the string. Limited to strings used as parameters (those
1779# following an open parenthesis), which almost completely eliminates false
1780# positives, as well as warning only once per parameter rather than once per
1781# line of the string. Make an exception when the previous string ends in a
1782# newline (multiple lines in one string constant) or \n\t (common in inline
1783# assembly to indent the instruction on the following line).
1784 if ($line =~ /^\+\s*"/ &&
1785 $prevline =~ /"\s*$/ &&
1786 $prevline =~ /\(/ &&
1787 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1788 WARN("SPLIT_STRING",
1789 "quoted string split across lines\n" . $hereprev);
1790 }
1791
Joe Perches5e79d962010-03-05 13:43:55 -08001792# check for spaces before a quoted newline
1793 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001794 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1795 "unnecessary whitespace before a quoted newline\n" . $herecurr);
Joe Perches5e79d962010-03-05 13:43:55 -08001796 }
1797
Andy Whitcroft8905a672007-11-28 16:21:06 -08001798# check for adding lines without a newline.
1799 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001800 WARN("MISSING_EOF_NEWLINE",
1801 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001802 }
1803
Mike Frysinger42e41c52009-09-21 17:04:40 -07001804# Blackfin: use hi/lo macros
1805 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1806 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1807 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001808 ERROR("LO_MACRO",
1809 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001810 }
1811 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1812 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001813 ERROR("HI_MACRO",
1814 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001815 }
1816 }
1817
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001818# check we are in a valid source file C or perl if not then ignore this hunk
1819 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001820
1821# at the beginning of a line any tabs must come first and anything
1822# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001823 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1824 $rawline =~ /^\+\s* \s*/) {
1825 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001826 ERROR("CODE_INDENT",
1827 "code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001828 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001829 }
1830
Alberto Panizzo08e44362010-03-05 13:43:54 -08001831# check for space before tabs.
1832 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1833 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001834 WARN("SPACE_BEFORE_TAB",
1835 "please, no space before tabs\n" . $herevet);
Alberto Panizzo08e44362010-03-05 13:43:54 -08001836 }
1837
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001838# check for && or || at the start of a line
1839 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1840 CHK("LOGICAL_CONTINUATIONS",
1841 "Logical continuations should be on the previous line\n" . $hereprev);
1842 }
1843
1844# check multi-line statement indentation matches previous line
1845 if ($^V && $^V ge 5.10.0 &&
1846 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1847 $prevline =~ /^\+(\t*)(.*)$/;
1848 my $oldindent = $1;
1849 my $rest = $2;
1850
1851 my $pos = pos_last_openparen($rest);
1852 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07001853 $line =~ /^(\+| )([ \t]*)/;
1854 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001855
1856 my $goodtabindent = $oldindent .
1857 "\t" x ($pos / 8) .
1858 " " x ($pos % 8);
1859 my $goodspaceindent = $oldindent . " " x $pos;
1860
1861 if ($newindent ne $goodtabindent &&
1862 $newindent ne $goodspaceindent) {
1863 CHK("PARENTHESIS_ALIGNMENT",
1864 "Alignment should match open parenthesis\n" . $hereprev);
1865 }
1866 }
1867 }
1868
Joe Perchesaad4f612012-03-23 15:02:19 -07001869 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1870 CHK("SPACING",
1871 "No space is necessary after a cast\n" . $hereprev);
1872 }
1873
Joe Perches05880602012-10-04 17:13:35 -07001874 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1875 $rawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1876 $prevrawline =~ /^\+[ \t]*$/) {
1877 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1878 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1879 }
1880
1881 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08001882 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1883 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1884 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1885 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07001886 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1887 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
1888 }
1889
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001890# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001891# Exceptions:
1892# 1) within comments
1893# 2) indented preprocessor commands
1894# 3) hanging labels
1895 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001896 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001897 WARN("LEADING_SPACE",
1898 "please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001899 }
1900
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001901# check we are in a valid C source file if not then ignore this hunk
1902 next if ($realfile !~ /\.(h|c)$/);
1903
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001904# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
1905 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
1906 WARN("CONFIG_EXPERIMENTAL",
1907 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1908 }
1909
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001910# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001911 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001912 WARN("CVS_KEYWORD",
1913 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001914 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001915
Mike Frysinger42e41c52009-09-21 17:04:40 -07001916# Blackfin: don't use __builtin_bfin_[cs]sync
1917 if ($line =~ /__builtin_bfin_csync/) {
1918 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001919 ERROR("CSYNC",
1920 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001921 }
1922 if ($line =~ /__builtin_bfin_ssync/) {
1923 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001924 ERROR("SSYNC",
1925 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001926 }
1927
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001928# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001929 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1930 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001931#print "LINE<$line>\n";
1932 if ($linenr >= $suppress_statement &&
1933 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001934 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001935 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001936 $stat =~ s/\n./\n /g;
1937 $cond =~ s/\n./\n /g;
1938
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001939#print "linenr<$linenr> <$stat>\n";
1940 # If this statement has no statement boundaries within
1941 # it there is no point in retrying a statement scan
1942 # until we hit end of it.
1943 my $frag = $stat; $frag =~ s/;+\s*$//;
1944 if ($frag !~ /(?:{|;)/) {
1945#print "skip<$line_nr_next>\n";
1946 $suppress_statement = $line_nr_next;
1947 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001948
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001949 # Find the real next line.
1950 $realline_next = $line_nr_next;
1951 if (defined $realline_next &&
1952 (!defined $lines[$realline_next - 1] ||
1953 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1954 $realline_next++;
1955 }
1956
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001957 my $s = $stat;
1958 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001959
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001960 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001961 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001962
1963 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001964 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001965
Andy Whitcroft463f2862009-09-21 17:04:34 -07001966 } elsif ($s =~ /^.\s*else\b/s) {
1967
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001968 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001969 } 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 -07001970 my $type = $1;
1971 $type =~ s/\s+/ /g;
1972 possible($type, "A:" . $s);
1973
Andy Whitcroft8905a672007-11-28 16:21:06 -08001974 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07001975 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001976 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001977 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001978
1979 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08001980 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001981 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001982 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001983
1984 # Check for any sort of function declaration.
1985 # int foo(something bar, other baz);
1986 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001987 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 -08001988 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001989
Andy Whitcroftcf655042008-03-04 14:28:20 -08001990 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001991 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001992 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001993
Andy Whitcroft8905a672007-11-28 16:21:06 -08001994 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001995 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001996
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001997 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001998 }
1999 }
2000 }
2001
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002002 }
2003
Andy Whitcroft653d4872007-06-23 17:16:34 -07002004#
2005# Checks which may be anchored in the context.
2006#
2007
2008# Check for switch () and associated case and default
2009# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002010 if ($line=~/\bswitch\s*\(.*\)/) {
2011 my $err = '';
2012 my $sep = '';
2013 my @ctx = ctx_block_outer($linenr, $realcnt);
2014 shift(@ctx);
2015 for my $ctx (@ctx) {
2016 my ($clen, $cindent) = line_stats($ctx);
2017 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2018 $indent != $cindent) {
2019 $err .= "$sep$ctx\n";
2020 $sep = '';
2021 } else {
2022 $sep = "[...]\n";
2023 }
2024 }
2025 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002026 ERROR("SWITCH_CASE_INDENT_LEVEL",
2027 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002028 }
2029 }
2030
2031# if/while/etc brace do not go on next line, unless defining a do while loop,
2032# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002033 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002034 my $pre_ctx = "$1$2";
2035
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002036 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002037
2038 if ($line =~ /^\+\t{6,}/) {
2039 WARN("DEEP_INDENTATION",
2040 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2041 }
2042
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002043 my $ctx_cnt = $realcnt - $#ctx - 1;
2044 my $ctx = join("\n", @ctx);
2045
Andy Whitcroft548596d2008-07-23 21:29:01 -07002046 my $ctx_ln = $linenr;
2047 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002048
Andy Whitcroft548596d2008-07-23 21:29:01 -07002049 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2050 defined $lines[$ctx_ln - 1] &&
2051 $lines[$ctx_ln - 1] =~ /^-/)) {
2052 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2053 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002054 $ctx_ln++;
2055 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002056
Andy Whitcroft53210162008-07-23 21:29:03 -07002057 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2058 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002059
2060 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002061 ERROR("OPEN_BRACE",
2062 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002063 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002064 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002065 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2066 $ctx =~ /\)\s*\;\s*$/ &&
2067 defined $lines[$ctx_ln - 1])
2068 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002069 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2070 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002071 WARN("TRAILING_SEMICOLON",
2072 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002073 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002074 }
2075 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002076 }
2077
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002078# Check relative indent for conditionals and blocks.
2079 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002080 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2081 ctx_statement_block($linenr, $realcnt, 0)
2082 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002083 my ($s, $c) = ($stat, $cond);
2084
2085 substr($s, 0, length($c), '');
2086
2087 # Make sure we remove the line prefixes as we have
2088 # none on the first line, and are going to readd them
2089 # where necessary.
2090 $s =~ s/\n./\n/gs;
2091
2092 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002093 my @newlines = ($c =~ /\n/gs);
2094 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002095
2096 # We want to check the first line inside the block
2097 # starting at the end of the conditional, so remove:
2098 # 1) any blank line termination
2099 # 2) any opening brace { on end of the line
2100 # 3) any do (...) {
2101 my $continuation = 0;
2102 my $check = 0;
2103 $s =~ s/^.*\bdo\b//;
2104 $s =~ s/^\s*{//;
2105 if ($s =~ s/^\s*\\//) {
2106 $continuation = 1;
2107 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002108 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002109 $check = 1;
2110 $cond_lines++;
2111 }
2112
2113 # Also ignore a loop construct at the end of a
2114 # preprocessor statement.
2115 if (($prevline =~ /^.\s*#\s*define\s/ ||
2116 $prevline =~ /\\\s*$/) && $continuation == 0) {
2117 $check = 0;
2118 }
2119
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002120 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002121 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002122 while ($cond_ptr != $cond_lines) {
2123 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002124
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002125 # If we see an #else/#elif then the code
2126 # is not linear.
2127 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2128 $check = 0;
2129 }
2130
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002131 # Ignore:
2132 # 1) blank lines, they should be at 0,
2133 # 2) preprocessor lines, and
2134 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002135 if ($continuation ||
2136 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002137 $s =~ /^\s*#\s*?/ ||
2138 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002139 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002140 if ($s =~ s/^.*?\n//) {
2141 $cond_lines++;
2142 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002143 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002144 }
2145
2146 my (undef, $sindent) = line_stats("+" . $s);
2147 my $stat_real = raw_line($linenr, $cond_lines);
2148
2149 # Check if either of these lines are modified, else
2150 # this is not this patch's fault.
2151 if (!defined($stat_real) ||
2152 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2153 $check = 0;
2154 }
2155 if (defined($stat_real) && $cond_lines > 1) {
2156 $stat_real = "[...]\n$stat_real";
2157 }
2158
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002159 #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 -07002160
2161 if ($check && (($sindent % 8) != 0 ||
2162 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002163 WARN("SUSPECT_CODE_INDENT",
2164 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002165 }
2166 }
2167
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002168 # Track the 'values' across context and added lines.
2169 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002170 my ($curr_values, $curr_vars) =
2171 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002172 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002173 if ($dbg_values) {
2174 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002175 print "$linenr > .$outline\n";
2176 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002177 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002178 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002179 $prev_values = substr($curr_values, -1);
2180
Andy Whitcroft00df3442007-06-08 13:47:06 -07002181#ignore lines not being added
2182 if ($line=~/^[^\+]/) {next;}
2183
Andy Whitcroft653d4872007-06-23 17:16:34 -07002184# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002185 if ($dbg_type) {
2186 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002187 ERROR("TEST_TYPE",
2188 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002189 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002190 ERROR("TEST_NOT_TYPE",
2191 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002192 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002193 next;
2194 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002195# TEST: allow direct testing of the attribute matcher.
2196 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002197 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002198 ERROR("TEST_ATTR",
2199 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002200 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002201 ERROR("TEST_NOT_ATTR",
2202 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002203 }
2204 next;
2205 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002206
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002207# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002208 if ($line =~ /^.\s*{/ &&
2209 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002210 ERROR("OPEN_BRACE",
2211 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002212 }
2213
Andy Whitcroft653d4872007-06-23 17:16:34 -07002214#
2215# Checks which are anchored on the added line.
2216#
2217
2218# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002219 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002220 my $path = $1;
2221 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002222 ERROR("MALFORMED_INCLUDE",
2223 "malformed #include filename\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002224 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002225 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002226 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002227
2228# no C99 // comments
2229 if ($line =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002230 ERROR("C99_COMMENTS",
2231 "do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002232 }
2233 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002234 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002235 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002236
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002237# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2238# the whole statement.
2239#print "APW <$lines[$realline_next - 1]>\n";
2240 if (defined $realline_next &&
2241 exists $lines[$realline_next - 1] &&
2242 !defined $suppress_export{$realline_next} &&
2243 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2244 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002245 # Handle definitions which produce identifiers with
2246 # a prefix:
2247 # XXX(foo);
2248 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002249 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002250 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002251 $name =~ /^${Ident}_$2/) {
2252#print "FOO C name<$name>\n";
2253 $suppress_export{$realline_next} = 1;
2254
2255 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002256 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002257 ^.DEFINE_$Ident\(\Q$name\E\)|
2258 ^.DECLARE_$Ident\(\Q$name\E\)|
2259 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002260 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2261 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002262 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002263#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2264 $suppress_export{$realline_next} = 2;
2265 } else {
2266 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002267 }
2268 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002269 if (!defined $suppress_export{$linenr} &&
2270 $prevline =~ /^.\s*$/ &&
2271 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2272 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2273#print "FOO B <$lines[$linenr - 1]>\n";
2274 $suppress_export{$linenr} = 2;
2275 }
2276 if (defined $suppress_export{$linenr} &&
2277 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002278 WARN("EXPORT_SYMBOL",
2279 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002280 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002281
Joe Eloff5150bda2010-08-09 17:21:00 -07002282# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002283 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002284 ERROR("GLOBAL_INITIALISERS",
2285 "do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002286 $herecurr);
2287 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002288# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08002289 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002290 ERROR("INITIALISED_STATIC",
2291 "do not initialise statics to 0 or NULL\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002292 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002293 }
2294
Joe Perchescb710ec2010-10-26 14:23:20 -07002295# check for static const char * arrays.
2296 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002297 WARN("STATIC_CONST_CHAR_ARRAY",
2298 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002299 $herecurr);
2300 }
2301
2302# check for static char foo[] = "bar" declarations.
2303 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002304 WARN("STATIC_CONST_CHAR_ARRAY",
2305 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002306 $herecurr);
2307 }
2308
Joe Perches93ed0e22010-10-26 14:23:21 -07002309# check for declarations of struct pci_device_id
2310 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002311 WARN("DEFINE_PCI_DEVICE_TABLE",
2312 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002313 }
2314
Andy Whitcroft653d4872007-06-23 17:16:34 -07002315# check for new typedefs, only function parameters and sparse annotations
2316# make sense.
2317 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002318 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002319 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002320 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002321 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002322 WARN("NEW_TYPEDEFS",
2323 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002324 }
2325
2326# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002327 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002328 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2329 #print "AA<$1>\n";
2330 my ($from, $to) = ($2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002331
Andy Whitcroft65863862009-01-06 14:41:21 -08002332 # Should start with a space.
2333 $to =~ s/^(\S)/ $1/;
2334 # Should not end with a space.
2335 $to =~ s/\s+$//;
2336 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002337 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002338 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002339
Andy Whitcroft65863862009-01-06 14:41:21 -08002340 #print "from<$from> to<$to>\n";
2341 if ($from ne $to) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002342 ERROR("POINTER_LOCATION",
2343 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002344 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002345 }
2346 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2347 #print "BB<$1>\n";
2348 my ($from, $to, $ident) = ($2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002349
Andy Whitcroft65863862009-01-06 14:41:21 -08002350 # Should start with a space.
2351 $to =~ s/^(\S)/ $1/;
2352 # Should not end with a space.
2353 $to =~ s/\s+$//;
2354 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002355 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002356 }
2357 # Modifiers should have spaces.
2358 $to =~ s/(\b$Modifier$)/$1 /;
2359
Andy Whitcroft667026e2009-02-27 14:03:08 -08002360 #print "from<$from> to<$to> ident<$ident>\n";
2361 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002362 ERROR("POINTER_LOCATION",
2363 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002364 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002365 }
2366
2367# # no BUG() or BUG_ON()
2368# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2369# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2370# print "$herecurr";
2371# $clean = 0;
2372# }
2373
Andy Whitcroft8905a672007-11-28 16:21:06 -08002374 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002375 WARN("LINUX_VERSION_CODE",
2376 "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 -08002377 }
2378
Joe Perches17441222011-06-15 15:08:17 -07002379# check for uses of printk_ratelimit
2380 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002381 WARN("PRINTK_RATELIMITED",
2382"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002383 }
2384
Andy Whitcroft00df3442007-06-08 13:47:06 -07002385# printk should use KERN_* levels. Note that follow on printk's on the
2386# same line do not need a level, so we use the current block context
2387# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002388# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002389# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002390 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002391 my $ok = 0;
2392 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2393 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002394 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002395 # with "\n" ignore it, else it is to blame
2396 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2397 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2398 $ok = 1;
2399 }
2400 last;
2401 }
2402 }
2403 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002404 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2405 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002406 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002407 }
2408
Joe Perches243f3802012-05-31 16:26:09 -07002409 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2410 my $orig = $1;
2411 my $level = lc($orig);
2412 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002413 my $level2 = $level;
2414 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002415 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002416 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002417 }
2418
2419 if ($line =~ /\bpr_warning\s*\(/) {
2420 WARN("PREFER_PR_LEVEL",
2421 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2422 }
2423
Andy Whitcroft653d4872007-06-23 17:16:34 -07002424# function brace can't be on same line, except for #defines of do while,
2425# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002426 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2427 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002428 ERROR("OPEN_BRACE",
2429 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002430 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002431
Andy Whitcroft8905a672007-11-28 16:21:06 -08002432# open braces for enum, union and struct go on the same line.
2433 if ($line =~ /^.\s*{/ &&
2434 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002435 ERROR("OPEN_BRACE",
2436 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002437 }
2438
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002439# missing space after union, struct or enum definition
2440 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002441 WARN("SPACING",
2442 "missing space after $1 definition\n" . $herecurr);
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002443 }
2444
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002445# check for spacing round square brackets; allowed:
2446# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002447# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2448# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002449 while ($line =~ /(.*?\s)\[/g) {
2450 my ($where, $prefix) = ($-[1], $1);
2451 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002452 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002453 $prefix !~ /[{,]\s+$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002454 ERROR("BRACKET_SPACE",
2455 "space prohibited before open square bracket '['\n" . $herecurr);
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002456 }
2457 }
2458
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002459# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002460 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002461 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002462 my $ctx_before = substr($line, 0, $-[1]);
2463 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002464
2465 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002466 if ($name =~ /^(?:
2467 if|for|while|switch|return|case|
2468 volatile|__volatile__|
2469 __attribute__|format|__extension__|
2470 asm|__asm__)$/x)
2471 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002472
2473 # cpp #define statements have non-optional spaces, ie
2474 # if there is a space between the name and the open
2475 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002476 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002477
2478 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002479 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002480
2481 # If this whole things ends with a type its most
2482 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002483 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002484
2485 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07002486 WARN("SPACING",
2487 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002488 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002489 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002490
2491# check for whitespace before a non-naked semicolon
2492 if ($line =~ /^\+.*\S\s+;/) {
2493 CHK("SPACING",
2494 "space prohibited before semicolon\n" . $herecurr);
2495 }
2496
Andy Whitcroft653d4872007-06-23 17:16:34 -07002497# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002498 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002499 my $ops = qr{
2500 <<=|>>=|<=|>=|==|!=|
2501 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2502 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002503 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2504 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002505 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002506 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002507 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002508
2509 my $blank = copy_spacing($opline);
2510
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002511 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002512 $off += length($elements[$n]);
2513
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002514 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002515 my $ca = substr($opline, 0, $off);
2516 my $cc = '';
2517 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2518 $cc = substr($opline, $off + length($elements[$n + 1]));
2519 }
2520 my $cb = "$ca$;$cc";
2521
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002522 my $a = '';
2523 $a = 'V' if ($elements[$n] ne '');
2524 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002525 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002526 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2527 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002528 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002529
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002530 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002531
2532 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002533 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002534 $c = 'V' if ($elements[$n + 2] ne '');
2535 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002536 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002537 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2538 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002539 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002540 } else {
2541 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002542 }
2543
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002544 my $ctx = "${a}x${c}";
2545
2546 my $at = "(ctx:$ctx)";
2547
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002548 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002549 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002550
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002551 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002552 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002553
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002554 # Get the full operator variant.
2555 my $opv = $op . substr($curr_vars, $off, 1);
2556
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002557 # Ignore operators passed as parameters.
2558 if ($op_type ne 'V' &&
2559 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2560
Andy Whitcroftcf655042008-03-04 14:28:20 -08002561# # Ignore comments
2562# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002563
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002564 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002565 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002566 if ($ctx !~ /.x[WEBC]/ &&
2567 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002568 ERROR("SPACING",
2569 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002570 }
2571
2572 # // is a comment
2573 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002574
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002575 # No spaces for:
2576 # ->
2577 # : when part of a bitfield
2578 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002579 if ($ctx =~ /Wx.|.xW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002580 ERROR("SPACING",
2581 "spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002582 }
2583
2584 # , must have a space on the right.
2585 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002586 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002587 ERROR("SPACING",
2588 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002589 }
2590
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002591 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002592 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002593 #warn "'*' is part of type\n";
2594
2595 # unary operators should have a space before and
2596 # none after. May be left adjacent to another
2597 # unary operator, or a cast
2598 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002599 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002600 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002601 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002602 ERROR("SPACING",
2603 "space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002604 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002605 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002606 # A unary '*' may be const
2607
2608 } elsif ($ctx =~ /.xW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002609 ERROR("SPACING",
2610 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002611 }
2612
2613 # unary ++ and unary -- are allowed no space on one side.
2614 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002615 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002616 ERROR("SPACING",
2617 "space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002618 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002619 if ($ctx =~ /Wx[BE]/ ||
2620 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002621 ERROR("SPACING",
2622 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002623 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002624 if ($ctx =~ /ExW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002625 ERROR("SPACING",
2626 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002627 }
2628
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002629
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002630 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002631 } elsif ($op eq '<<' or $op eq '>>' or
2632 $op eq '&' or $op eq '^' or $op eq '|' or
2633 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002634 $op eq '*' or $op eq '/' or
2635 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002636 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002637 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002638 ERROR("SPACING",
2639 "need consistent spacing around '$op' $at\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002640 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002641 }
2642
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002643 # A colon needs no spaces before when it is
2644 # terminating a case value or a label.
2645 } elsif ($opv eq ':C' || $opv eq ':L') {
2646 if ($ctx =~ /Wx./) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002647 ERROR("SPACING",
2648 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002649 }
2650
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002651 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002652 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002653 my $ok = 0;
2654
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002655 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002656 if (($op eq '<' &&
2657 $cc =~ /^\S+\@\S+>/) ||
2658 ($op eq '>' &&
2659 $ca =~ /<\S+\@\S+$/))
2660 {
2661 $ok = 1;
2662 }
2663
2664 # Ignore ?:
2665 if (($opv eq ':O' && $ca =~ /\?$/) ||
2666 ($op eq '?' && $cc =~ /^:/)) {
2667 $ok = 1;
2668 }
2669
2670 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002671 ERROR("SPACING",
2672 "spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002673 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002674 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002675 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002676 }
2677 }
2678
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002679# check for multiple assignments
2680 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002681 CHK("MULTIPLE_ASSIGNMENTS",
2682 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002683 }
2684
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002685## # check for multiple declarations, allowing for a function declaration
2686## # continuation.
2687## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2688## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2689##
2690## # Remove any bracketed sections to ensure we do not
2691## # falsly report the parameters of functions.
2692## my $ln = $line;
2693## while ($ln =~ s/\([^\(\)]*\)//g) {
2694## }
2695## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002696## WARN("MULTIPLE_DECLARATION",
2697## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002698## }
2699## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002700
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002701#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002702 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2703 $line =~ /do{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002704 ERROR("SPACING",
2705 "space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002706 }
2707
2708# closing brace should have a space following it when it has anything
2709# on the line
2710 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002711 ERROR("SPACING",
2712 "space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002713 }
2714
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002715# check spacing on square brackets
2716 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002717 ERROR("SPACING",
2718 "space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002719 }
2720 if ($line =~ /\s\]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002721 ERROR("SPACING",
2722 "space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002723 }
2724
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002725# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002726 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2727 $line !~ /for\s*\(\s+;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002728 ERROR("SPACING",
2729 "space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002730 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002731 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002732 $line !~ /for\s*\(.*;\s+\)/ &&
2733 $line !~ /:\s+\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002734 ERROR("SPACING",
2735 "space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002736 }
2737
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002738#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002739 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002740 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002741 WARN("INDENTED_LABEL",
2742 "labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002743 }
2744
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002745# Return is not a function.
2746 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2747 my $spacing = $1;
2748 my $value = $2;
2749
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002750 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002751 $value =~ s/\(/ \(/g;
2752 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08002753 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002754 $value !~ /(?:$Ident|-?$Constant)\s*
2755 $Compare\s*
2756 (?:$Ident|-?$Constant)/x &&
2757 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002758 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002759#print "value<$value>\n";
2760 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002761 ERROR("RETURN_PARENTHESES",
2762 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002763
2764 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002765 ERROR("SPACING",
2766 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002767 }
2768 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002769# Return of what appears to be an errno should normally be -'ve
2770 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2771 my $name = $1;
2772 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002773 WARN("USE_NEGATIVE_ERRNO",
2774 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002775 }
2776 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002777
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002778# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002779 if ($line=~/\b(if|while|for|switch)\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002780 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002781 }
2782
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002783# Check for illegal assignment in if conditional -- and check for trailing
2784# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002785 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002786 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2787 ctx_statement_block($linenr, $realcnt, 0)
2788 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002789 my ($stat_next) = ctx_statement_block($line_nr_next,
2790 $remain_next, $off_next);
2791 $stat_next =~ s/\n./\n /g;
2792 ##print "stat<$stat> stat_next<$stat_next>\n";
2793
2794 if ($stat_next =~ /^\s*while\b/) {
2795 # If the statement carries leading newlines,
2796 # then count those as offsets.
2797 my ($whitespace) =
2798 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2799 my $offset =
2800 statement_rawlines($whitespace) - 1;
2801
2802 $suppress_whiletrailers{$line_nr_next +
2803 $offset} = 1;
2804 }
2805 }
2806 if (!defined $suppress_whiletrailers{$linenr} &&
2807 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002808 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002809
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002810 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002811 ERROR("ASSIGN_IN_IF",
2812 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002813 }
2814
2815 # Find out what is on the end of the line after the
2816 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002817 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002818 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002819 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002820 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2821 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002822 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002823 # Find out how long the conditional actually is.
2824 my @newlines = ($c =~ /\n/gs);
2825 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002826 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002827
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002828 $stat_real = raw_line($linenr, $cond_lines)
2829 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002830 if (defined($stat_real) && $cond_lines > 1) {
2831 $stat_real = "[...]\n$stat_real";
2832 }
2833
Joe Perches000d1cc12011-07-25 17:13:25 -07002834 ERROR("TRAILING_STATEMENTS",
2835 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002836 }
2837 }
2838
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002839# Check for bitwise tests written as boolean
2840 if ($line =~ /
2841 (?:
2842 (?:\[|\(|\&\&|\|\|)
2843 \s*0[xX][0-9]+\s*
2844 (?:\&\&|\|\|)
2845 |
2846 (?:\&\&|\|\|)
2847 \s*0[xX][0-9]+\s*
2848 (?:\&\&|\|\||\)|\])
2849 )/x)
2850 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002851 WARN("HEXADECIMAL_BOOLEAN_TEST",
2852 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002853 }
2854
Andy Whitcroft8905a672007-11-28 16:21:06 -08002855# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002856 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2857 my $s = $1;
2858 $s =~ s/$;//g; # Remove any comments
2859 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002860 ERROR("TRAILING_STATEMENTS",
2861 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002862 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002863 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002864# if should not continue a brace
2865 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002866 ERROR("TRAILING_STATEMENTS",
2867 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08002868 $herecurr);
2869 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002870# case and default should not have general statements after them
2871 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2872 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002873 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002874 \s*return\s+
2875 )/xg)
2876 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002877 ERROR("TRAILING_STATEMENTS",
2878 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002879 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002880
2881 # Check for }<nl>else {, these must be at the same
2882 # indent level to be relevant to each other.
2883 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2884 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002885 ERROR("ELSE_AFTER_BRACE",
2886 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002887 }
2888
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002889 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2890 $previndent == $indent) {
2891 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2892
2893 # Find out what is on the end of the line after the
2894 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002895 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002896 $s =~ s/\n.*//g;
2897
2898 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002899 ERROR("WHILE_AFTER_BRACE",
2900 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002901 }
2902 }
2903
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002904#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2905# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2906# print "No studly caps, use _\n";
2907# print "$herecurr";
2908# $clean = 0;
2909# }
2910
2911#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002912 if ($line=~/\#\s*define.*\\\s$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002913 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2914 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002915 }
2916
Andy Whitcroft653d4872007-06-23 17:16:34 -07002917#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002918 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002919 my $file = "$1.h";
2920 my $checkfile = "include/linux/$file";
2921 if (-f "$root/$checkfile" &&
2922 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002923 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002924 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002925 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002926 CHK("ARCH_INCLUDE_LINUX",
2927 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002928 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07002929 WARN("INCLUDE_LINUX",
2930 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002931 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002932 }
2933 }
2934
Andy Whitcroft653d4872007-06-23 17:16:34 -07002935# multi-statement macros should be enclosed in a do while loop, grab the
2936# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002937# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07002938 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2939 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002940 my $ln = $linenr;
2941 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002942 my ($off, $dstat, $dcond, $rest);
2943 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002944 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002945 ctx_statement_block($linenr, $realcnt, 0);
2946 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002947 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002948 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002949
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002950 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07002951 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002952 $dstat =~ s/\\\n.//g;
2953 $dstat =~ s/^\s*//s;
2954 $dstat =~ s/\s*$//s;
2955
2956 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002957 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2958 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08002959 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07002960 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002961 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002962
Andy Whitcrofte45bab82012-03-23 15:02:18 -07002963 # Flatten any obvious string concatentation.
2964 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
2965 $dstat =~ s/$Ident\s*("X*")/$1/)
2966 {
2967 }
2968
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002969 my $exceptions = qr{
2970 $Declare|
2971 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07002972 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002973 DECLARE_PER_CPU|
2974 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08002975 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08002976 union|
2977 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07002978 \.$Ident\s*=\s*|
2979 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002980 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07002981 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002982 if ($dstat ne '' &&
2983 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
2984 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Andy Whitcroftfd1b57a2012-03-23 15:02:18 -07002985 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07002986 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002987 $dstat !~ /$exceptions/ &&
2988 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Andy Whitcroft72f115f2012-01-10 15:10:06 -08002989 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002990 $dstat !~ /^for\s*$Constant$/ && # for (...)
2991 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
2992 $dstat !~ /^do\s*{/ && # do {...
2993 $dstat !~ /^\({/) # ({...
2994 {
2995 $ctx =~ s/\n*$//;
2996 my $herectx = $here . "\n";
2997 my $cnt = statement_rawlines($ctx);
2998
2999 for (my $n = 0; $n < $cnt; $n++) {
3000 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003001 }
3002
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003003 if ($dstat =~ /;/) {
3004 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3005 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3006 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003007 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003008 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003009 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003010 }
Joe Perches5023d342012-12-17 16:01:47 -08003011
Joe Perches481eb482012-12-17 16:01:56 -08003012# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003013
3014 } else {
3015 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003016 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3017 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003018 $line =~ /^\+.*\\$/) {
3019 WARN("LINE_CONTINUATIONS",
3020 "Avoid unnecessary line continuations\n" . $herecurr);
3021 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003022 }
3023
Joe Perchesb13edf72012-07-30 14:41:24 -07003024# do {} while (0) macro tests:
3025# single-statement macros do not need to be enclosed in do while (0) loop,
3026# macro should not end with a semicolon
3027 if ($^V && $^V ge 5.10.0 &&
3028 $realfile !~ m@/vmlinux.lds.h$@ &&
3029 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3030 my $ln = $linenr;
3031 my $cnt = $realcnt;
3032 my ($off, $dstat, $dcond, $rest);
3033 my $ctx = '';
3034 ($dstat, $dcond, $ln, $cnt, $off) =
3035 ctx_statement_block($linenr, $realcnt, 0);
3036 $ctx = $dstat;
3037
3038 $dstat =~ s/\\\n.//g;
3039
3040 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3041 my $stmts = $2;
3042 my $semis = $3;
3043
3044 $ctx =~ s/\n*$//;
3045 my $cnt = statement_rawlines($ctx);
3046 my $herectx = $here . "\n";
3047
3048 for (my $n = 0; $n < $cnt; $n++) {
3049 $herectx .= raw_line($linenr, $n) . "\n";
3050 }
3051
Joe Perchesac8e97f2012-08-21 16:15:53 -07003052 if (($stmts =~ tr/;/;/) == 1 &&
3053 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003054 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3055 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3056 }
3057 if (defined $semis && $semis ne "") {
3058 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3059 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3060 }
3061 }
3062 }
3063
Mike Frysinger080ba922009-01-06 14:41:25 -08003064# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3065# all assignments may have only one of the following with an assignment:
3066# .
3067# ALIGN(...)
3068# VMLINUX_SYMBOL(...)
3069 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003070 WARN("MISSING_VMLINUX_SYMBOL",
3071 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003072 }
3073
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003074# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003075 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3076 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003077 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003078 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003079 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3080 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003081 my @allowed = ();
3082 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003083 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003084 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003085 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003086 for my $chunk (@chunks) {
3087 my ($cond, $block) = @{$chunk};
3088
Andy Whitcroft773647a2008-03-28 14:15:58 -07003089 # If the condition carries leading newlines, then count those as offsets.
3090 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3091 my $offset = statement_rawlines($whitespace) - 1;
3092
Joe Perchesaad4f612012-03-23 15:02:19 -07003093 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003094 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3095
3096 # We have looked at and allowed this specific line.
3097 $suppress_ifbraces{$ln + $offset} = 1;
3098
3099 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003100 $ln += statement_rawlines($block) - 1;
3101
Andy Whitcroft773647a2008-03-28 14:15:58 -07003102 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003103
3104 $seen++ if ($block =~ /^\s*{/);
3105
Joe Perchesaad4f612012-03-23 15:02:19 -07003106 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003107 if (statement_lines($cond) > 1) {
3108 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003109 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003110 }
3111 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003112 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003113 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003114 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003115 if (statement_block_size($block) > 1) {
3116 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003117 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003118 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003119 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003120 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003121 if ($seen) {
3122 my $sum_allowed = 0;
3123 foreach (@allowed) {
3124 $sum_allowed += $_;
3125 }
3126 if ($sum_allowed == 0) {
3127 WARN("BRACES",
3128 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3129 } elsif ($sum_allowed != $allow &&
3130 $seen != $allow) {
3131 CHK("BRACES",
3132 "braces {} should be used on all arms of this statement\n" . $herectx);
3133 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003134 }
3135 }
3136 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003137 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003138 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003139 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003140
Andy Whitcroftcf655042008-03-04 14:28:20 -08003141 # Check the pre-context.
3142 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3143 #print "APW: ALLOWED: pre<$1>\n";
3144 $allowed = 1;
3145 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003146
3147 my ($level, $endln, @chunks) =
3148 ctx_statement_full($linenr, $realcnt, $-[0]);
3149
Andy Whitcroftcf655042008-03-04 14:28:20 -08003150 # Check the condition.
3151 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003152 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003153 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003154 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003155 }
3156 if (statement_lines($cond) > 1) {
3157 #print "APW: ALLOWED: cond<$cond>\n";
3158 $allowed = 1;
3159 }
3160 if ($block =~/\b(?:if|for|while)\b/) {
3161 #print "APW: ALLOWED: block<$block>\n";
3162 $allowed = 1;
3163 }
3164 if (statement_block_size($block) > 1) {
3165 #print "APW: ALLOWED: lines block<$block>\n";
3166 $allowed = 1;
3167 }
3168 # Check the post-context.
3169 if (defined $chunks[1]) {
3170 my ($cond, $block) = @{$chunks[1]};
3171 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003172 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003173 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003174 if ($block =~ /^\s*\{/) {
3175 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3176 $allowed = 1;
3177 }
3178 }
3179 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003180 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003181 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003182
Andy Whitcroftf0556632008-10-15 22:02:23 -07003183 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003184 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003185 }
3186
Joe Perches000d1cc12011-07-25 17:13:25 -07003187 WARN("BRACES",
3188 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003189 }
3190 }
3191
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003192# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003193 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3194 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003195 WARN("VOLATILE",
3196 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003197 }
3198
Andy Whitcroft00df3442007-06-08 13:47:06 -07003199# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003200 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003201 CHK("REDUNDANT_CODE",
3202 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003203 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003204 }
3205
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003206# check for needless "if (<foo>) fn(<foo>)" uses
3207 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3208 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3209 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3210 WARN('NEEDLESS_IF',
3211 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003212 }
3213 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003214
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003215# prefer usleep_range over udelay
3216 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3217 # ignore udelay's < 10, however
3218 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003219 CHK("USLEEP_RANGE",
3220 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003221 }
3222 }
3223
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003224# warn about unexpectedly long msleep's
3225 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3226 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003227 WARN("MSLEEP",
3228 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003229 }
3230 }
3231
Andy Whitcroft00df3442007-06-08 13:47:06 -07003232# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003233# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003234# print "#ifdef in C files should be avoided\n";
3235# print "$herecurr";
3236# $clean = 0;
3237# }
3238
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003239# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003240 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003241 ERROR("SPACING",
3242 "exactly one space required after that #$1\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003243 }
3244
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003245# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003246 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3247 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003248 my $which = $1;
3249 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003250 CHK("UNCOMMENTED_DEFINITION",
3251 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003252 }
3253 }
3254# check for memory barriers without a comment.
3255 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3256 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003257 CHK("MEMORY_BARRIER",
3258 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003259 }
3260 }
3261# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003262 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003263 CHK("ARCH_DEFINES",
3264 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003265 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003266
Tobias Klauserd4977c72010-05-24 14:33:30 -07003267# Check that the storage class is at the beginning of a declaration
3268 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003269 WARN("STORAGE_CLASS",
3270 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003271 }
3272
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003273# check the location of the inline attribute, that it is between
3274# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003275 if ($line =~ /\b$Type\s+$Inline\b/ ||
3276 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003277 ERROR("INLINE_LOCATION",
3278 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003279 }
3280
Andy Whitcroft8905a672007-11-28 16:21:06 -08003281# Check for __inline__ and __inline, prefer inline
3282 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003283 WARN("INLINE",
3284 "plain inline is preferred over $1\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003285 }
3286
Joe Perches3d130fd2011-01-12 17:00:00 -08003287# Check for __attribute__ packed, prefer __packed
3288 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003289 WARN("PREFER_PACKED",
3290 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003291 }
3292
Joe Perches39b7e282011-07-25 17:13:24 -07003293# Check for __attribute__ aligned, prefer __aligned
3294 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003295 WARN("PREFER_ALIGNED",
3296 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003297 }
3298
Joe Perches5f14d3b2012-01-10 15:09:52 -08003299# Check for __attribute__ format(printf, prefer __printf
3300 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3301 WARN("PREFER_PRINTF",
3302 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3303 }
3304
Joe Perches6061d942012-03-23 15:02:16 -07003305# Check for __attribute__ format(scanf, prefer __scanf
3306 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3307 WARN("PREFER_SCANF",
3308 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3309 }
3310
Joe Perches8f53a9b2010-03-05 13:43:48 -08003311# check for sizeof(&)
3312 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003313 WARN("SIZEOF_ADDRESS",
3314 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003315 }
3316
Joe Perches66c80b62012-07-30 14:41:22 -07003317# check for sizeof without parenthesis
3318 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3319 WARN("SIZEOF_PARENTHESIS",
3320 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3321 }
3322
Joe Perches428e2fd2011-05-24 17:13:39 -07003323# check for line continuations in quoted strings with odd counts of "
3324 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003325 WARN("LINE_CONTINUATIONS",
3326 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003327 }
3328
Andy Whitcroft554e1652012-01-10 15:09:57 -08003329# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003330 if ($^V && $^V ge 5.10.0 &&
3331 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003332 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003333
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003334 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003335 my $ms_val = $7;
3336 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003337
Andy Whitcroft554e1652012-01-10 15:09:57 -08003338 if ($ms_size =~ /^(0x|)0$/i) {
3339 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003340 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003341 } elsif ($ms_size =~ /^(0x|)1$/i) {
3342 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003343 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3344 }
3345 }
3346
3347# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003348 if ($^V && $^V ge 5.10.0 &&
3349 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003350 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003351 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003352 my $call = $1;
3353 my $cast1 = deparenthesize($2);
3354 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003355 my $cast2 = deparenthesize($7);
3356 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003357 my $cast;
3358
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003359 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003360 $cast = "$cast1 or $cast2";
3361 } elsif ($cast1 ne "") {
3362 $cast = $cast1;
3363 } else {
3364 $cast = $cast2;
3365 }
3366 WARN("MINMAX",
3367 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003368 }
3369 }
3370
Joe Perches4a273192012-07-30 14:41:20 -07003371# check usleep_range arguments
3372 if ($^V && $^V ge 5.10.0 &&
3373 defined $stat &&
3374 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3375 my $min = $1;
3376 my $max = $7;
3377 if ($min eq $max) {
3378 WARN("USLEEP_RANGE",
3379 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3380 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3381 $min > $max) {
3382 WARN("USLEEP_RANGE",
3383 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3384 }
3385 }
3386
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003387# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003388 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003389 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003390 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003391 my $function_name = $1;
3392 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003393
3394 my $s = $stat;
3395 if (defined $cond) {
3396 substr($s, 0, length($cond), '');
3397 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003398 if ($s =~ /^\s*;/ &&
3399 $function_name ne 'uninitialized_var')
3400 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003401 WARN("AVOID_EXTERNS",
3402 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003403 }
3404
3405 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003406 WARN("FUNCTION_ARGUMENTS",
3407 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003408 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003409
3410 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3411 $stat =~ /^.\s*extern\s+/)
3412 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003413 WARN("AVOID_EXTERNS",
3414 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003415 }
3416
3417# checks for new __setup's
3418 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3419 my $name = $1;
3420
3421 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003422 CHK("UNDOCUMENTED_SETUP",
3423 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003424 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003425 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003426
3427# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003428 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003429 WARN("UNNECESSARY_CASTS",
3430 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003431 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003432
Joe Perchescaf2a542011-01-12 16:59:56 -08003433# check for multiple semicolons
3434 if ($line =~ /;\s*;\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003435 WARN("ONE_SEMICOLON",
3436 "Statements terminations use 1 semicolon\n" . $herecurr);
Joe Perchescaf2a542011-01-12 16:59:56 -08003437 }
3438
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003439# check for gcc specific __FUNCTION__
3440 if ($line =~ /__FUNCTION__/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003441 WARN("USE_FUNC",
3442 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003443 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003444
Joe Perches2c924882012-03-23 15:02:20 -07003445# check for use of yield()
3446 if ($line =~ /\byield\s*\(\s*\)/) {
3447 WARN("YIELD",
3448 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3449 }
3450
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003451# check for semaphores initialized locked
3452 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003453 WARN("CONSIDER_COMPLETION",
3454 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003455 }
Joe Perches6712d852012-03-23 15:02:20 -07003456
Joe Perches67d0a072011-10-31 17:13:10 -07003457# recommend kstrto* over simple_strto* and strict_strto*
3458 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003459 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07003460 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003461 }
Joe Perches6712d852012-03-23 15:02:20 -07003462
Michael Ellermanf3db6632008-07-23 21:28:57 -07003463# check for __initcall(), use device_initcall() explicitly please
3464 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003465 WARN("USE_DEVICE_INITCALL",
3466 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07003467 }
Joe Perches6712d852012-03-23 15:02:20 -07003468
Emese Revfy79404842010-03-05 13:43:53 -08003469# check for various ops structs, ensure they are const.
3470 my $struct_ops = qr{acpi_dock_ops|
3471 address_space_operations|
3472 backlight_ops|
3473 block_device_operations|
3474 dentry_operations|
3475 dev_pm_ops|
3476 dma_map_ops|
3477 extent_io_ops|
3478 file_lock_operations|
3479 file_operations|
3480 hv_ops|
3481 ide_dma_ops|
3482 intel_dvo_dev_ops|
3483 item_operations|
3484 iwl_ops|
3485 kgdb_arch|
3486 kgdb_io|
3487 kset_uevent_ops|
3488 lock_manager_operations|
3489 microcode_ops|
3490 mtrr_ops|
3491 neigh_ops|
3492 nlmsvc_binding|
3493 pci_raw_ops|
3494 pipe_buf_operations|
3495 platform_hibernation_ops|
3496 platform_suspend_ops|
3497 proto_ops|
3498 rpc_pipe_ops|
3499 seq_operations|
3500 snd_ac97_build_ops|
3501 soc_pcmcia_socket_ops|
3502 stacktrace_ops|
3503 sysfs_ops|
3504 tty_operations|
3505 usb_mon_operations|
3506 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003507 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003508 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003509 WARN("CONST_STRUCT",
3510 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003511 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003512 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003513
3514# use of NR_CPUS is usually wrong
3515# ignore definitions of NR_CPUS and usage to define arrays as likely right
3516 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003517 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3518 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003519 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3520 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3521 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003522 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003523 WARN("NR_CPUS",
3524 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003525 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003526
3527# check for %L{u,d,i} in strings
3528 my $string;
3529 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3530 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003531 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003532 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003533 WARN("PRINTF_L",
3534 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003535 last;
3536 }
3537 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003538
3539# whine mightly about in_atomic
3540 if ($line =~ /\bin_atomic\s*\(/) {
3541 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003542 ERROR("IN_ATOMIC",
3543 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003544 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003545 WARN("IN_ATOMIC",
3546 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003547 }
3548 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003549
3550# check for lockdep_set_novalidate_class
3551 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3552 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3553 if ($realfile !~ m@^kernel/lockdep@ &&
3554 $realfile !~ m@^include/linux/lockdep@ &&
3555 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003556 ERROR("LOCKDEP",
3557 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003558 }
3559 }
Dave Jones88f88312011-01-12 16:59:59 -08003560
3561 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3562 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003563 WARN("EXPORTED_WORLD_WRITABLE",
3564 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08003565 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003566 }
3567
3568 # If we have no input at all, then there is nothing to report on
3569 # so just keep quiet.
3570 if ($#rawlines == -1) {
3571 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003572 }
3573
Andy Whitcroft8905a672007-11-28 16:21:06 -08003574 # In mailback mode only produce a report in the negative, for
3575 # things that appear to be patches.
3576 if ($mailback && ($clean == 1 || !$is_patch)) {
3577 exit(0);
3578 }
3579
3580 # This is not a patch, and we are are in 'no-patch' mode so
3581 # just keep quiet.
3582 if (!$chk_patch && !$is_patch) {
3583 exit(0);
3584 }
3585
3586 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003587 ERROR("NOT_UNIFIED_DIFF",
3588 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003589 }
3590 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003591 ERROR("MISSING_SIGN_OFF",
3592 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003593 }
3594
Andy Whitcroft8905a672007-11-28 16:21:06 -08003595 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003596 if ($summary && !($clean == 1 && $quiet == 1)) {
3597 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003598 print "total: $cnt_error errors, $cnt_warn warnings, " .
3599 (($check)? "$cnt_chk checks, " : "") .
3600 "$cnt_lines lines checked\n";
3601 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003602 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003603
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003604 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003605
3606 if ($^V lt 5.10.0) {
3607 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3608 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3609 }
3610
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003611 # If there were whitespace errors which cleanpatch can fix
3612 # then suggest that.
3613 if ($rpt_cleaners) {
3614 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3615 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003616 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003617 }
3618 }
3619
Artem Bityutskiy11232682012-03-23 15:02:17 -07003620 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003621 print "NOTE: Ignored message types:";
3622 foreach my $ignore (sort keys %ignore_type) {
3623 print " $ignore";
3624 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07003625 print "\n\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003626 }
3627
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003628 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003629 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003630 }
3631 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003632 print << "EOM";
3633$vname has style problems, please review.
3634
3635If any of these errors are false positives, please report
3636them to the maintainer, see CHECKPATCH in MAINTAINERS.
3637EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003638 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003639
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003640 return $clean;
3641}