blob: d2f60a0d5edc0671410b8e7fc528f8fd6b4136ed [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070010use constant BEFORE_SHORTTEXT => 0;
Steve Muckled3b3b642012-03-05 10:12:04 -080011use constant IN_SHORTTEXT_BLANKLINE => 1;
12use constant IN_SHORTTEXT => 2;
13use constant AFTER_SHORTTEXT => 3;
14use constant CHECK_NEXT_SHORTTEXT => 4;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015use constant SHORTTEXT_LIMIT => 75;
16
Andy Whitcroft0a920b52007-06-01 00:46:48 -070017my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070018$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070019
Joe Perches000d1cc2011-07-25 17:13:25 -070020my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070021
22use Getopt::Long qw(:config no_auto_abbrev);
23
24my $quiet = 0;
25my $tree = 1;
26my $chk_signoff = 1;
27my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070028my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070029my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080030my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070031my $file = 0;
32my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080033my $summary = 1;
34my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080035my $summary_file = 0;
Joe Perches000d1cc2011-07-25 17:13:25 -070036my $show_types = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070037my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080038my %debug;
Joe Perches000d1cc2011-07-25 17:13:25 -070039my %ignore_type = ();
40my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070041my $help = 0;
Joe Perches000d1cc2011-07-25 17:13:25 -070042my $configuration_file = ".checkpatch.conf";
Hannes Eder77f5b102009-09-21 17:04:37 -070043
44sub help {
45 my ($exitcode) = @_;
46
47 print << "EOM";
48Usage: $P [OPTION]... [FILE]...
49Version: $V
50
51Options:
52 -q, --quiet quiet
53 --no-tree run without a kernel tree
54 --no-signoff do not check for 'Signed-off-by' line
55 --patch treat FILE as patchfile (default)
56 --emacs emacs compile window format
57 --terse one line per report
58 -f, --file treat FILE as regular source file
59 --subjective, --strict enable more subjective tests
Joe Perches000d1cc2011-07-25 17:13:25 -070060 --ignore TYPE(,TYPE2...) ignore various comma separated message types
61 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070062 --root=PATH PATH to the kernel tree root
63 --no-summary suppress the per-file summary
64 --mailback only produce a report in case of warnings/errors
65 --summary-file include the filename in summary
66 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
67 'values', 'possible', 'type', and 'attr' (default
68 is all off)
69 --test-only=WORD report only warnings/errors containing WORD
70 literally
71 -h, --help, --version display this help and exit
72
73When FILE is - read standard input.
74EOM
75
76 exit($exitcode);
77}
78
Joe Perches000d1cc2011-07-25 17:13:25 -070079my $conf = which_conf($configuration_file);
80if (-f $conf) {
81 my @conf_args;
82 open(my $conffile, '<', "$conf")
83 or warn "$P: Can't find a readable $configuration_file file $!\n";
84
85 while (<$conffile>) {
86 my $line = $_;
87
88 $line =~ s/\s*\n?$//g;
89 $line =~ s/^\s*//g;
90 $line =~ s/\s+/ /g;
91
92 next if ($line =~ m/^\s*#/);
93 next if ($line =~ m/^\s*$/);
94
95 my @words = split(" ", $line);
96 foreach my $word (@words) {
97 last if ($word =~ m/^#/);
98 push (@conf_args, $word);
99 }
100 }
101 close($conffile);
102 unshift(@ARGV, @conf_args) if @conf_args;
103}
104
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700105GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700106 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700107 'tree!' => \$tree,
108 'signoff!' => \$chk_signoff,
109 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700110 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800111 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700112 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113 'subjective!' => \$check,
114 'strict!' => \$check,
Joe Perches000d1cc2011-07-25 17:13:25 -0700115 'ignore=s' => \@ignore,
116 'show-types!' => \$show_types,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700117 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800118 'summary!' => \$summary,
119 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800120 'summary-file!' => \$summary_file,
121
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800122 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700123 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700124 'h|help' => \$help,
125 'version' => \$help
126) or help(1);
127
128help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700129
130my $exit = 0;
131
132if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700133 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700134 exit(1);
135}
136
Joe Perches000d1cc2011-07-25 17:13:25 -0700137@ignore = split(/,/, join(',',@ignore));
138foreach my $word (@ignore) {
139 $word =~ s/\s*\n?$//g;
140 $word =~ s/^\s*//g;
141 $word =~ s/\s+/ /g;
142 $word =~ tr/[a-z]/[A-Z]/;
143
144 next if ($word =~ m/^\s*#/);
145 next if ($word =~ m/^\s*$/);
146
147 $ignore_type{$word}++;
148}
149
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800150my $dbg_values = 0;
151my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700152my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700153my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800154for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800155 ## no critic
156 eval "\${dbg_$key} = '$debug{$key}';";
157 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800158}
159
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700160my $rpt_cleaners = 0;
161
Andy Whitcroft8905a672007-11-28 16:21:06 -0800162if ($terse) {
163 $emacs = 1;
164 $quiet++;
165}
166
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700167if ($tree) {
168 if (defined $root) {
169 if (!top_of_kernel_tree($root)) {
170 die "$P: $root: --root does not point at a valid tree\n";
171 }
172 } else {
173 if (top_of_kernel_tree('.')) {
174 $root = '.';
175 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
176 top_of_kernel_tree($1)) {
177 $root = $1;
178 }
179 }
180
181 if (!defined $root) {
182 print "Must be run from the top-level dir. of a kernel tree\n";
183 exit(2);
184 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700185}
186
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700187my $emitted_corrupt = 0;
188
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700189our $Ident = qr{
190 [A-Za-z_][A-Za-z\d_]*
191 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
192 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700193our $Storage = qr{extern|static|asmlinkage};
194our $Sparse = qr{
195 __user|
196 __kernel|
197 __force|
198 __iomem|
199 __must_check|
200 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800201 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700202 __ref|
203 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700204 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800205
206# Notes to $Attribute:
207# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208our $Attribute = qr{
209 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700210 __percpu|
211 __nocast|
212 __safe|
213 __bitwise__|
214 __packed__|
215 __packed2__|
216 __naked|
217 __maybe_unused|
218 __always_unused|
219 __noreturn|
220 __used|
221 __cold|
222 __noclone|
223 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700224 __read_mostly|
225 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800226 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700227 ____cacheline_aligned|
228 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800229 ____cacheline_internodealigned_in_smp|
230 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700231 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700232our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700233our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700234our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
235our $Lval = qr{$Ident(?:$Member)*};
236
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800237our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700238our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800239our $Compare = qr{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700240our $Operators = qr{
241 <=|>=|==|!=|
242 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800243 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700244 }x;
245
Andy Whitcroft8905a672007-11-28 16:21:06 -0800246our $NonptrType;
247our $Type;
248our $Declare;
249
Joe Perches15662b32011-10-31 17:13:12 -0700250our $NON_ASCII_UTF8 = qr{
251 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700252 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
253 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
254 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
255 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
256 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
257 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
258}x;
259
Joe Perches15662b32011-10-31 17:13:12 -0700260our $UTF8 = qr{
261 [\x09\x0A\x0D\x20-\x7E] # ASCII
262 | $NON_ASCII_UTF8
263}x;
264
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700265our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700266 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700267 atomic_t
268)};
269
Joe Perches691e6692010-03-05 13:43:51 -0800270our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700271 printk(?:_ratelimited|_once|)|
272 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
273 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700274 panic|
275 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800276)};
277
Joe Perches20112472011-07-25 17:13:23 -0700278our $signature_tags = qr{(?xi:
279 Signed-off-by:|
280 Acked-by:|
281 Tested-by:|
282 Reviewed-by:|
283 Reported-by:|
284 To:|
285 Cc:
286)};
287
Andy Whitcroft8905a672007-11-28 16:21:06 -0800288our @typeList = (
289 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700290 qr{(?:unsigned\s+)?char},
291 qr{(?:unsigned\s+)?short},
292 qr{(?:unsigned\s+)?int},
293 qr{(?:unsigned\s+)?long},
294 qr{(?:unsigned\s+)?long\s+int},
295 qr{(?:unsigned\s+)?long\s+long},
296 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800297 qr{unsigned},
298 qr{float},
299 qr{double},
300 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800301 qr{struct\s+$Ident},
302 qr{union\s+$Ident},
303 qr{enum\s+$Ident},
304 qr{${Ident}_t},
305 qr{${Ident}_handler},
306 qr{${Ident}_handler_fn},
307);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700308our @modifierList = (
309 qr{fastcall},
310);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800311
Wolfram Sang7840a942010-08-09 17:20:57 -0700312our $allowed_asm_includes = qr{(?x:
313 irq|
314 memory
315)};
316# memory.h: ARM has a custom one
317
Andy Whitcroft8905a672007-11-28 16:21:06 -0800318sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700319 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
320 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700321 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800322 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700323 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800324 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800325 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700326 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700327 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800328 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700329 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330 }x;
331 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700332 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700333 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700334 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800335 }x;
336 $Declare = qr{(?:$Storage\s+)?$Type};
337}
338build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700339
Joe Perches7d2367a2011-07-25 17:13:22 -0700340
341our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700342
343# Using $balanced_parens, $LvalOrFunc, or $FuncArg
344# requires at least perl version v5.10.0
345# Any use must be runtime checked with $^V
346
347our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
348our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800349our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700350
351sub deparenthesize {
352 my ($string) = @_;
353 return "" if (!defined($string));
354 $string =~ s@^\s*\(\s*@@g;
355 $string =~ s@\s*\)\s*$@@g;
356 $string =~ s@\s+@ @g;
357 return $string;
358}
359
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700360$chk_signoff = 0 if ($file);
361
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700362my @dep_includes = ();
363my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700364my $removal = "Documentation/feature-removal-schedule.txt";
365if ($tree && -f "$root/$removal") {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800366 open(my $REMOVE, '<', "$root/$removal") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700367 die "$P: $removal: open failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800368 while (<$REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700369 if (/^Check:\s+(.*\S)/) {
370 for my $entry (split(/[, ]+/, $1)) {
371 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700372 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700373
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700374 } elsif ($entry !~ m@/@) {
375 push(@dep_functions, $entry);
376 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700377 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700378 }
379 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800380 close($REMOVE);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700381}
382
Andy Whitcroft00df3442007-06-08 13:47:06 -0700383my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800384my @lines = ();
385my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700386for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800387 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700388 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800389 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700390 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800391 } elsif ($filename eq '-') {
392 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700393 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800394 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700395 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700396 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800397 if ($filename eq '-') {
398 $vname = 'Your patch';
399 } else {
400 $vname = $filename;
401 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800402 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700403 chomp;
404 push(@rawlines, $_);
405 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800406 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800407 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700408 $exit = 1;
409 }
410 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800411 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700412}
413
414exit($exit);
415
416sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700417 my ($root) = @_;
418
419 my @tree_check = (
420 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
421 "README", "Documentation", "arch", "include", "drivers",
422 "fs", "init", "ipc", "kernel", "lib", "scripts",
423 );
424
425 foreach my $check (@tree_check) {
426 if (! -e $root . '/' . $check) {
427 return 0;
428 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700429 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700430 return 1;
Joe Perches000d1cc2011-07-25 17:13:25 -0700431 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700432
Joe Perches20112472011-07-25 17:13:23 -0700433sub parse_email {
434 my ($formatted_email) = @_;
435
436 my $name = "";
437 my $address = "";
438 my $comment = "";
439
440 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
441 $name = $1;
442 $address = $2;
443 $comment = $3 if defined $3;
444 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
445 $address = $1;
446 $comment = $2 if defined $2;
447 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
448 $address = $1;
449 $comment = $2 if defined $2;
450 $formatted_email =~ s/$address.*$//;
451 $name = $formatted_email;
452 $name =~ s/^\s+|\s+$//g;
453 $name =~ s/^\"|\"$//g;
454 # If there's a name left after stripping spaces and
455 # leading quotes, and the address doesn't have both
456 # leading and trailing angle brackets, the address
457 # is invalid. ie:
458 # "joe smith joe@smith.com" bad
459 # "joe smith <joe@smith.com" bad
460 if ($name ne "" && $address !~ /^<[^>]+>$/) {
461 $name = "";
462 $address = "";
463 $comment = "";
464 }
465 }
466
467 $name =~ s/^\s+|\s+$//g;
468 $name =~ s/^\"|\"$//g;
469 $address =~ s/^\s+|\s+$//g;
470 $address =~ s/^\<|\>$//g;
471
472 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
473 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
474 $name = "\"$name\"";
475 }
476
477 return ($name, $address, $comment);
478}
479
480sub format_email {
481 my ($name, $address) = @_;
482
483 my $formatted_email;
484
485 $name =~ s/^\s+|\s+$//g;
486 $name =~ s/^\"|\"$//g;
487 $address =~ s/^\s+|\s+$//g;
488
489 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
490 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
491 $name = "\"$name\"";
492 }
493
494 if ("$name" eq "") {
495 $formatted_email = "$address";
496 } else {
497 $formatted_email = "$name <$address>";
498 }
499
500 return $formatted_email;
501}
502
Joe Perches000d1cc2011-07-25 17:13:25 -0700503sub which_conf {
504 my ($conf) = @_;
505
506 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
507 if (-e "$path/$conf") {
508 return "$path/$conf";
509 }
510 }
511
512 return "";
513}
514
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700515sub expand_tabs {
516 my ($str) = @_;
517
518 my $res = '';
519 my $n = 0;
520 for my $c (split(//, $str)) {
521 if ($c eq "\t") {
522 $res .= ' ';
523 $n++;
524 for (; ($n % 8) != 0; $n++) {
525 $res .= ' ';
526 }
527 next;
528 }
529 $res .= $c;
530 $n++;
531 }
532
533 return $res;
534}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700535sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700536 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700537 return $res;
538}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700539
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700540sub line_stats {
541 my ($line) = @_;
542
543 # Drop the diff line leader and expand tabs
544 $line =~ s/^.//;
545 $line = expand_tabs($line);
546
547 # Pick the indent from the front of the line.
548 my ($white) = ($line =~ /^(\s*)/);
549
550 return (length($line), length($white));
551}
552
Andy Whitcroft773647a2008-03-28 14:15:58 -0700553my $sanitise_quote = '';
554
555sub sanitise_line_reset {
556 my ($in_comment) = @_;
557
558 if ($in_comment) {
559 $sanitise_quote = '*/';
560 } else {
561 $sanitise_quote = '';
562 }
563}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700564sub sanitise_line {
565 my ($line) = @_;
566
567 my $res = '';
568 my $l = '';
569
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800570 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700571 my $off = 0;
572 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700573
Andy Whitcroft773647a2008-03-28 14:15:58 -0700574 # Always copy over the diff marker.
575 $res = substr($line, 0, 1);
576
577 for ($off = 1; $off < length($line); $off++) {
578 $c = substr($line, $off, 1);
579
580 # Comments we are wacking completly including the begin
581 # and end, all to $;.
582 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
583 $sanitise_quote = '*/';
584
585 substr($res, $off, 2, "$;$;");
586 $off++;
587 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800588 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700589 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700590 $sanitise_quote = '';
591 substr($res, $off, 2, "$;$;");
592 $off++;
593 next;
594 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700595 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
596 $sanitise_quote = '//';
597
598 substr($res, $off, 2, $sanitise_quote);
599 $off++;
600 next;
601 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700602
603 # A \ in a string means ignore the next character.
604 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
605 $c eq "\\") {
606 substr($res, $off, 2, 'XX');
607 $off++;
608 next;
609 }
610 # Regular quotes.
611 if ($c eq "'" || $c eq '"') {
612 if ($sanitise_quote eq '') {
613 $sanitise_quote = $c;
614
615 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700616 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700617 } elsif ($sanitise_quote eq $c) {
618 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700619 }
620 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700621
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800622 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700623 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
624 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700625 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
626 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700627 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
628 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700629 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700630 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700631 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800632 }
633
Daniel Walker113f04a2009-09-21 17:04:35 -0700634 if ($sanitise_quote eq '//') {
635 $sanitise_quote = '';
636 }
637
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800638 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700639 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800640 my $clean = 'X' x length($1);
641 $res =~ s@\<.*\>@<$clean>@;
642
643 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700644 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800645 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700646 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800647 }
648
Andy Whitcroft00df3442007-06-08 13:47:06 -0700649 return $res;
650}
651
Andy Whitcroft8905a672007-11-28 16:21:06 -0800652sub ctx_statement_block {
653 my ($linenr, $remain, $off) = @_;
654 my $line = $linenr - 1;
655 my $blk = '';
656 my $soff = $off;
657 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700658 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800659
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800660 my $loff = 0;
661
Andy Whitcroft8905a672007-11-28 16:21:06 -0800662 my $type = '';
663 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800664 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800665 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800666 my $c;
667 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800668
669 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800670 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800671 @stack = (['', 0]) if ($#stack == -1);
672
Andy Whitcroft773647a2008-03-28 14:15:58 -0700673 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800674 # If we are about to drop off the end, pull in more
675 # context.
676 if ($off >= $len) {
677 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700678 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800679 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800680 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800681 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800682 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800683 $len = length($blk);
684 $line++;
685 last;
686 }
687 # Bail if there is no further context.
688 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800689 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800690 last;
691 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800692 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
693 $level++;
694 $type = '#';
695 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800696 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800697 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800698 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800699 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800700
Andy Whitcroft773647a2008-03-28 14:15:58 -0700701 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800702
703 # Handle nested #if/#else.
704 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
705 push(@stack, [ $type, $level ]);
706 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
707 ($type, $level) = @{$stack[$#stack - 1]};
708 } elsif ($remainder =~ /^#\s*endif\b/) {
709 ($type, $level) = @{pop(@stack)};
710 }
711
Andy Whitcroft8905a672007-11-28 16:21:06 -0800712 # Statement ends at the ';' or a close '}' at the
713 # outermost level.
714 if ($level == 0 && $c eq ';') {
715 last;
716 }
717
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800718 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700719 if ($level == 0 && $coff_set == 0 &&
720 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
721 $remainder =~ /^(else)(?:\s|{)/ &&
722 $remainder !~ /^else\s+if\b/) {
723 $coff = $off + length($1) - 1;
724 $coff_set = 1;
725 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
726 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800727 }
728
Andy Whitcroft8905a672007-11-28 16:21:06 -0800729 if (($type eq '' || $type eq '(') && $c eq '(') {
730 $level++;
731 $type = '(';
732 }
733 if ($type eq '(' && $c eq ')') {
734 $level--;
735 $type = ($level != 0)? '(' : '';
736
737 if ($level == 0 && $coff < $soff) {
738 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700739 $coff_set = 1;
740 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800741 }
742 }
743 if (($type eq '' || $type eq '{') && $c eq '{') {
744 $level++;
745 $type = '{';
746 }
747 if ($type eq '{' && $c eq '}') {
748 $level--;
749 $type = ($level != 0)? '{' : '';
750
751 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700752 if (substr($blk, $off + 1, 1) eq ';') {
753 $off++;
754 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800755 last;
756 }
757 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800758 # Preprocessor commands end at the newline unless escaped.
759 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
760 $level--;
761 $type = '';
762 $off++;
763 last;
764 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800765 $off++;
766 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700767 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800768 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700769 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800770 $line++;
771 $remain--;
772 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800773
774 my $statement = substr($blk, $soff, $off - $soff + 1);
775 my $condition = substr($blk, $soff, $coff - $soff + 1);
776
777 #warn "STATEMENT<$statement>\n";
778 #warn "CONDITION<$condition>\n";
779
Andy Whitcroft773647a2008-03-28 14:15:58 -0700780 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800781
782 return ($statement, $condition,
783 $line, $remain + 1, $off - $loff + 1, $level);
784}
785
Andy Whitcroftcf655042008-03-04 14:28:20 -0800786sub statement_lines {
787 my ($stmt) = @_;
788
789 # Strip the diff line prefixes and rip blank lines at start and end.
790 $stmt =~ s/(^|\n)./$1/g;
791 $stmt =~ s/^\s*//;
792 $stmt =~ s/\s*$//;
793
794 my @stmt_lines = ($stmt =~ /\n/g);
795
796 return $#stmt_lines + 2;
797}
798
799sub statement_rawlines {
800 my ($stmt) = @_;
801
802 my @stmt_lines = ($stmt =~ /\n/g);
803
804 return $#stmt_lines + 2;
805}
806
807sub statement_block_size {
808 my ($stmt) = @_;
809
810 $stmt =~ s/(^|\n)./$1/g;
811 $stmt =~ s/^\s*{//;
812 $stmt =~ s/}\s*$//;
813 $stmt =~ s/^\s*//;
814 $stmt =~ s/\s*$//;
815
816 my @stmt_lines = ($stmt =~ /\n/g);
817 my @stmt_statements = ($stmt =~ /;/g);
818
819 my $stmt_lines = $#stmt_lines + 2;
820 my $stmt_statements = $#stmt_statements + 1;
821
822 if ($stmt_lines > $stmt_statements) {
823 return $stmt_lines;
824 } else {
825 return $stmt_statements;
826 }
827}
828
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800829sub ctx_statement_full {
830 my ($linenr, $remain, $off) = @_;
831 my ($statement, $condition, $level);
832
833 my (@chunks);
834
Andy Whitcroftcf655042008-03-04 14:28:20 -0800835 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800836 ($statement, $condition, $linenr, $remain, $off, $level) =
837 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700838 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800839 push(@chunks, [ $condition, $statement ]);
840 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
841 return ($level, $linenr, @chunks);
842 }
843
844 # Pull in the following conditional/block pairs and see if they
845 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800846 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800847 ($statement, $condition, $linenr, $remain, $off, $level) =
848 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800849 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700850 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800851 #print "C: push\n";
852 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800853 }
854
855 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800856}
857
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700858sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700859 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700860 my $line;
861 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700862 my $blk = '';
863 my @o;
864 my @c;
865 my @res = ();
866
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700867 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800868 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700869 for ($line = $start; $remain > 0; $line++) {
870 next if ($rawlines[$line] =~ /^-/);
871 $remain--;
872
873 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800874
875 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700876 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800877 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700878 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800879 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700880 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800881 $level = pop(@stack);
882 }
883
Andy Whitcroft01464f32010-10-26 14:23:19 -0700884 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700885 ##print "C<$c>L<$level><$open$close>O<$off>\n";
886 if ($off > 0) {
887 $off--;
888 next;
889 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700890
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700891 if ($c eq $close && $level > 0) {
892 $level--;
893 last if ($level == 0);
894 } elsif ($c eq $open) {
895 $level++;
896 }
897 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700898
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700899 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700900 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700901 }
902
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700903 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700904 }
905
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700906 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700907}
908sub ctx_block_outer {
909 my ($linenr, $remain) = @_;
910
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700911 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
912 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700913}
914sub ctx_block {
915 my ($linenr, $remain) = @_;
916
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700917 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
918 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700919}
920sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700921 my ($linenr, $remain, $off) = @_;
922
923 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
924 return @r;
925}
926sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700927 my ($linenr, $remain) = @_;
928
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700929 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700930}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700931sub ctx_statement_level {
932 my ($linenr, $remain, $off) = @_;
933
934 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
935}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700936
937sub ctx_locate_comment {
938 my ($first_line, $end_line) = @_;
939
940 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700941 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700942 return $current_comment if (defined $current_comment);
943
944 # Look through the context and try and figure out if there is a
945 # comment.
946 my $in_comment = 0;
947 $current_comment = '';
948 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700949 my $line = $rawlines[$linenr - 1];
950 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700951 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
952 $in_comment = 1;
953 }
954 if ($line =~ m@/\*@) {
955 $in_comment = 1;
956 }
957 if (!$in_comment && $current_comment ne '') {
958 $current_comment = '';
959 }
960 $current_comment .= $line . "\n" if ($in_comment);
961 if ($line =~ m@\*/@) {
962 $in_comment = 0;
963 }
964 }
965
966 chomp($current_comment);
967 return($current_comment);
968}
969sub ctx_has_comment {
970 my ($first_line, $end_line) = @_;
971 my $cmt = ctx_locate_comment($first_line, $end_line);
972
Andy Whitcroft00df3442007-06-08 13:47:06 -0700973 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700974 ##print "CMMT: $cmt\n";
975
976 return ($cmt ne '');
977}
978
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700979sub raw_line {
980 my ($linenr, $cnt) = @_;
981
982 my $offset = $linenr - 1;
983 $cnt++;
984
985 my $line;
986 while ($cnt) {
987 $line = $rawlines[$offset++];
988 next if (defined($line) && $line =~ /^-/);
989 $cnt--;
990 }
991
992 return $line;
993}
994
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700995sub cat_vet {
996 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700997 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700998
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700999 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001000 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1001 $res .= $1;
1002 if ($2 ne '') {
1003 $coded = sprintf("^%c", unpack('C', $2) + 64);
1004 $res .= $coded;
1005 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001006 }
1007 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001008
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001009 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001010}
1011
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001012my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001013my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001014my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001015my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001016
1017sub annotate_reset {
1018 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001019 $av_pending = '_';
1020 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001021 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001022}
1023
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024sub annotate_values {
1025 my ($stream, $type) = @_;
1026
1027 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001028 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001029 my $cur = $stream;
1030
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001031 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001032
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001033 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001034 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001035 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001036 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001037 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001038 print "WS($1)\n" if ($dbg_values > 1);
1039 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001040 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001041 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001042 }
1043
Florian Micklerc023e4732011-01-12 16:59:58 -08001044 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001045 print "CAST($1)\n" if ($dbg_values > 1);
1046 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001047 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001048
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001049 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001050 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001051 $type = 'T';
1052
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001053 } elsif ($cur =~ /^($Modifier)\s*/) {
1054 print "MODIFIER($1)\n" if ($dbg_values > 1);
1055 $type = 'T';
1056
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001057 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001058 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001059 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001060 push(@av_paren_type, $type);
1061 if ($2 ne '') {
1062 $av_pending = 'N';
1063 }
1064 $type = 'E';
1065
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001066 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001067 print "UNDEF($1)\n" if ($dbg_values > 1);
1068 $av_preprocessor = 1;
1069 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001070
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001071 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001072 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001073 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001074
1075 push(@av_paren_type, $type);
1076 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001077 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001078
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001079 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001080 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1081 $av_preprocessor = 1;
1082
1083 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1084
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001085 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001086
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001087 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001088 print "PRE_END($1)\n" if ($dbg_values > 1);
1089
1090 $av_preprocessor = 1;
1091
1092 # Assume all arms of the conditional end as this
1093 # one does, and continue as if the #endif was not here.
1094 pop(@av_paren_type);
1095 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001096 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001097
1098 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001099 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001100
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001101 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1102 print "ATTR($1)\n" if ($dbg_values > 1);
1103 $av_pending = $type;
1104 $type = 'N';
1105
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001106 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001107 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001108 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001109 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001110 }
1111 $type = 'N';
1112
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001113 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001114 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001115 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001116 $type = 'N';
1117
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001118 } elsif ($cur =~/^(case)/o) {
1119 print "CASE($1)\n" if ($dbg_values > 1);
1120 $av_pend_colon = 'C';
1121 $type = 'N';
1122
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001123 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001124 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001125 $type = 'N';
1126
1127 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001128 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001129 push(@av_paren_type, $av_pending);
1130 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001131 $type = 'N';
1132
1133 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001134 my $new_type = pop(@av_paren_type);
1135 if ($new_type ne '_') {
1136 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001137 print "PAREN('$1') -> $type\n"
1138 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001139 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001140 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001141 }
1142
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001143 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001144 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001145 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001146 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001147
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001148 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1149 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001150 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001151 } elsif ($type eq 'E') {
1152 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001153 }
1154 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1155 $type = 'V';
1156
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001157 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001158 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001159 $type = 'V';
1160
1161 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001162 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001163 $type = 'N';
1164
Andy Whitcroftcf655042008-03-04 14:28:20 -08001165 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001166 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001167 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001168 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001169
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001170 } elsif ($cur =~/^(,)/) {
1171 print "COMMA($1)\n" if ($dbg_values > 1);
1172 $type = 'C';
1173
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001174 } elsif ($cur =~ /^(\?)/o) {
1175 print "QUESTION($1)\n" if ($dbg_values > 1);
1176 $type = 'N';
1177
1178 } elsif ($cur =~ /^(:)/o) {
1179 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1180
1181 substr($var, length($res), 1, $av_pend_colon);
1182 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1183 $type = 'E';
1184 } else {
1185 $type = 'N';
1186 }
1187 $av_pend_colon = 'O';
1188
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001189 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001190 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001191 $type = 'N';
1192
Andy Whitcroft0d413862008-10-15 22:02:16 -07001193 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001194 my $variant;
1195
1196 print "OPV($1)\n" if ($dbg_values > 1);
1197 if ($type eq 'V') {
1198 $variant = 'B';
1199 } else {
1200 $variant = 'U';
1201 }
1202
1203 substr($var, length($res), 1, $variant);
1204 $type = 'N';
1205
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001206 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001207 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001208 if ($1 ne '++' && $1 ne '--') {
1209 $type = 'N';
1210 }
1211
1212 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001214 }
1215 if (defined $1) {
1216 $cur = substr($cur, length($1));
1217 $res .= $type x length($1);
1218 }
1219 }
1220
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001221 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001222}
1223
Andy Whitcroft8905a672007-11-28 16:21:06 -08001224sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001225 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001226 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001227 ^(?:
1228 $Modifier|
1229 $Storage|
1230 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001231 DEFINE_\S+
1232 )$|
1233 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001234 goto|
1235 return|
1236 case|
1237 else|
1238 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001239 do|
1240 \#|
1241 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001242 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001243 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001244 )}x;
1245 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1246 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001247 # Check for modifiers.
1248 $possible =~ s/\s*$Storage\s*//g;
1249 $possible =~ s/\s*$Sparse\s*//g;
1250 if ($possible =~ /^\s*$/) {
1251
1252 } elsif ($possible =~ /\s/) {
1253 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001254 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001255 if ($modifier !~ $notPermitted) {
1256 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1257 push(@modifierList, $modifier);
1258 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001259 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001260
1261 } else {
1262 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1263 push(@typeList, $possible);
1264 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001265 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001266 } else {
1267 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001268 }
1269}
1270
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001271my $prefix = '';
1272
Joe Perches000d1cc2011-07-25 17:13:25 -07001273sub show_type {
1274 return !defined $ignore_type{$_[0]};
1275}
1276
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001277sub report {
Joe Perches000d1cc2011-07-25 17:13:25 -07001278 if (!show_type($_[1]) ||
1279 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001280 return 0;
1281 }
Joe Perches000d1cc2011-07-25 17:13:25 -07001282 my $line;
1283 if ($show_types) {
1284 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1285 } else {
1286 $line = "$prefix$_[0]: $_[2]\n";
1287 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001288 $line = (split('\n', $line))[0] . "\n" if ($terse);
1289
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001290 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001291
1292 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001293}
1294sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001295 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001296}
Joe Perches000d1cc2011-07-25 17:13:25 -07001297
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001298sub ERROR {
Joe Perches000d1cc2011-07-25 17:13:25 -07001299 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300 our $clean = 0;
1301 our $cnt_error++;
1302 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001303}
1304sub WARN {
Joe Perches000d1cc2011-07-25 17:13:25 -07001305 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306 our $clean = 0;
1307 our $cnt_warn++;
1308 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001309}
1310sub CHK {
Joe Perches000d1cc2011-07-25 17:13:25 -07001311 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001312 our $clean = 0;
1313 our $cnt_chk++;
1314 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001315}
1316
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001317sub check_absolute_file {
1318 my ($absolute, $herecurr) = @_;
1319 my $file = $absolute;
1320
1321 ##print "absolute<$absolute>\n";
1322
1323 # See if any suffix of this path is a path within the tree.
1324 while ($file =~ s@^[^/]*/@@) {
1325 if (-f "$root/$file") {
1326 ##print "file<$file>\n";
1327 last;
1328 }
1329 }
1330 if (! -f _) {
1331 return 0;
1332 }
1333
1334 # It is, so see if the prefix is acceptable.
1335 my $prefix = $absolute;
1336 substr($prefix, -length($file)) = '';
1337
1338 ##print "prefix<$prefix>\n";
1339 if ($prefix ne ".../") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001340 WARN("USE_RELATIVE_PATH",
1341 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001342 }
1343}
1344
Gregory Bean3f66eea2011-07-19 16:37:49 -07001345sub cleanup_continuation_headers {
1346 # Collapse any header-continuation lines into a single line so they
1347 # can be parsed meaningfully, as the parser only has one line
1348 # of context to work with.
1349 my $again;
1350 do {
1351 $again = 0;
1352 foreach my $n (0 .. scalar(@rawlines) - 2) {
1353 if ($rawlines[$n]=~/^\s*$/) {
1354 # A blank line means there's no more chance
1355 # of finding headers. Shortcut to done.
1356 return;
1357 }
1358 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
1359 $rawlines[$n+1]=~/^\s+/) {
1360 # Continuation header. Collapse it.
1361 my $line = splice @rawlines, $n+1, 1;
1362 $line=~s/^\s+/ /;
1363 $rawlines[$n] .= $line;
1364 # We've 'destabilized' the list, so restart.
1365 $again = 1;
1366 last;
1367 }
1368 }
1369 } while ($again);
1370}
1371
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001372sub pos_last_openparen {
1373 my ($line) = @_;
1374
1375 my $pos = 0;
1376
1377 my $opens = $line =~ tr/\(/\(/;
1378 my $closes = $line =~ tr/\)/\)/;
1379
1380 my $last_openparen = 0;
1381
1382 if (($opens == 0) || ($closes >= $opens)) {
1383 return -1;
1384 }
1385
1386 my $len = length($line);
1387
1388 for ($pos = 0; $pos < $len; $pos++) {
1389 my $string = substr($line, $pos);
1390 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1391 $pos += length($1) - 1;
1392 } elsif (substr($line, $pos, 1) eq '(') {
1393 $last_openparen = $pos;
1394 } elsif (index($string, '(') == -1) {
1395 last;
1396 }
1397 }
1398
1399 return $last_openparen + 1;
1400}
1401
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001402sub process {
1403 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001404
1405 my $linenr=0;
1406 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001407 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001408 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001409 my $stashrawline="";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410 my $subjectline="";
1411 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001412
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001413 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001414 my $indent;
1415 my $previndent=0;
1416 my $stashindent=0;
1417
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001418 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001419 my $signoff = 0;
1420 my $is_patch = 0;
1421
Joe Perches15662b32011-10-31 17:13:12 -07001422 my $in_header_lines = 1;
1423 my $in_commit_log = 0; #Scanning lines before patch
1424
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001425 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001426 our $cnt_lines = 0;
1427 our $cnt_error = 0;
1428 our $cnt_warn = 0;
1429 our $cnt_chk = 0;
1430
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001431 # Trace the real file/line as we go.
1432 my $realfile = '';
1433 my $realline = 0;
1434 my $realcnt = 0;
1435 my $here = '';
1436 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001437 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001438 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001439 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001440
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001441 my $prev_values = 'E';
1442
1443 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001444 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001445 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001446 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001447 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001448
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001449 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001450 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001451 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001452 my @setup_docs = ();
1453 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001454
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455 my $exec_file = "";
1456
1457 my $shorttext = BEFORE_SHORTTEXT;
1458 my $shorttext_exspc = 0;
Steve Muckled3b3b642012-03-05 10:12:04 -08001459 my $commit_text_present = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460
Andy Whitcroft773647a2008-03-28 14:15:58 -07001461 sanitise_line_reset();
Gregory Bean3f66eea2011-07-19 16:37:49 -07001462 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001463 my $line;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001464
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001465 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001466 $linenr++;
1467 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001468
Andy Whitcroft773647a2008-03-28 14:15:58 -07001469 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001470 $setup_docs = 0;
1471 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1472 $setup_docs = 1;
1473 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001474 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001475 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001476 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1477 $realline=$1-1;
1478 if (defined $2) {
1479 $realcnt=$3+1;
1480 } else {
1481 $realcnt=1+1;
1482 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001483 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001484
1485 # Guestimate if this is a continuing comment. Run
1486 # the context looking for a comment "edge". If this
1487 # edge is a close comment then we must be in a comment
1488 # at context start.
1489 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001490 my $cnt = $realcnt;
1491 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1492 next if (defined $rawlines[$ln - 1] &&
1493 $rawlines[$ln - 1] =~ /^-/);
1494 $cnt--;
1495 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001496 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001497 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1498 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1499 ($edge) = $1;
1500 last;
1501 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001502 }
1503 if (defined $edge && $edge eq '*/') {
1504 $in_comment = 1;
1505 }
1506
1507 # Guestimate if this is a continuing comment. If this
1508 # is the start of a diff block and this line starts
1509 # ' *' then it is very likely a comment.
1510 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001511 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001512 {
1513 $in_comment = 1;
1514 }
1515
1516 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1517 sanitise_line_reset($in_comment);
1518
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001519 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001520 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001521 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001522 $line = sanitise_line($rawline);
1523 }
1524 push(@lines, $line);
1525
1526 if ($realcnt > 1) {
1527 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1528 } else {
1529 $realcnt = 0;
1530 }
1531
1532 #print "==>$rawline\n";
1533 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001534
1535 if ($setup_docs && $line =~ /^\+/) {
1536 push(@setup_docs, $line);
1537 }
1538 }
1539
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001540 $prefix = '';
1541
Andy Whitcroft773647a2008-03-28 14:15:58 -07001542 $realcnt = 0;
1543 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001544 foreach my $line (@lines) {
1545 $linenr++;
1546
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001547 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001548
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001549#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001550 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001551 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001552 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001553 $realline=$1-1;
1554 if (defined $2) {
1555 $realcnt=$3+1;
1556 } else {
1557 $realcnt=1+1;
1558 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001559 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001560 $prev_values = 'E';
1561
Andy Whitcroft773647a2008-03-28 14:15:58 -07001562 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001563 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001564 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001565 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001566 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001567
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001568# track the line number as we move through the hunk, note that
1569# new versions of GNU diff omit the leading space on completely
1570# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001571 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001572 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001573 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001574
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001575 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001576 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001577
1578 # Track the previous line.
1579 ($prevline, $stashline) = ($stashline, $line);
1580 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001581 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1582
Andy Whitcroft773647a2008-03-28 14:15:58 -07001583 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001584
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001585 } elsif ($realcnt == 1) {
1586 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001587 }
1588
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001589 my $hunk_line = ($realcnt != 0);
1590
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001591#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001592 $prefix = "$filename:$realline: " if ($emacs && $file);
1593 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1594
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001595 $here = "#$linenr: " if (!$file);
1596 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001597
1598 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001599 if ($line =~ /^diff --git.*?(\S+)$/) {
1600 $realfile = $1;
1601 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001602 $in_commit_log = 0;
Gregory Bean246e1f12011-10-17 12:27:01 -07001603 $exec_file = $realfile;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001604 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001605 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001606 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001607 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001608
1609 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001610 if (!$file && $tree && $p1_prefix ne '' &&
1611 -e "$root/$p1_prefix") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001612 WARN("PATCH_PREFIX",
1613 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001614 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001615
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001616 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001617 ERROR("MODIFIED_INCLUDE_ASM",
1618 "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 -07001619 }
Gregory Bean246e1f12011-10-17 12:27:01 -07001620 $exec_file = "";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001621 next;
1622 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623 elsif ($rawline =~ /^diff.+a\/(.+)\sb\/.+$/) {
1624 $exec_file = $1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001625 }
1626 #Check state to make sure we aren't in code block.
Gregory Bean246e1f12011-10-17 12:27:01 -07001627 elsif (($exec_file =~ /^.+\.[chS]$/ or
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001628 $exec_file =~ /^.+\.txt$/ or
1629 $exec_file =~ /^.+\.ihex$/ or
1630 $exec_file =~ /^.+\.hex$/ or
1631 $exec_file =~ /^.+\.HEX$/ or
Michael Bohan01020ea2012-10-03 17:17:04 -07001632 $exec_file =~ /^.+\.dts$/ or
1633 $exec_file =~ /^.+\.dtsi$/ or
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001634 $exec_file =~ /^.+defconfig$/ or
1635 $exec_file =~ /^Makefile$/ or
1636 $exec_file =~ /^Kconfig$/) &&
1637 $rawline =~ /^new (file )?mode\s([0-9]+)$/ &&
1638 (oct($2) & 0111)) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001639 ERROR("EXECUTE_PERMISSIONS",
1640 "Source file has +x permissions: " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 "$exec_file\n");
1642 }
Randy Dunlap389834b2007-06-08 13:47:03 -07001643 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001644
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001645 my $hereline = "$here\n$rawline\n";
1646 my $herecurr = "$here\n$rawline\n";
1647 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001648
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001649 if ($shorttext != AFTER_SHORTTEXT) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001650 if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) {
1651 # the subject line was just processed,
1652 # a blank line must be next
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001653 WARN("NONBLANK_AFTER_SUMMARY",
1654 "non-blank line after summary line\n" . $herecurr);
Steve Muckled3b3b642012-03-05 10:12:04 -08001655 $shorttext = IN_SHORTTEXT;
1656 # this non-blank line may or may not be commit text -
1657 # a warning has been generated so assume it is commit
1658 # text and move on
1659 $commit_text_present = 1;
1660 # fall through and treat this line as IN_SHORTTEXT
1661 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001662 if ($shorttext == IN_SHORTTEXT) {
1663 if ($line=~/^---/ || $line=~/^diff.*/) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001664 if ($commit_text_present == 0) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001665 WARN("NO_COMMIT_TEXT",
1666 "please add commit text explaining " .
Steve Muckled3b3b642012-03-05 10:12:04 -08001667 "*why* the change is needed\n" .
1668 $herecurr);
1669 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 $shorttext = AFTER_SHORTTEXT;
1671 } elsif (length($line) > (SHORTTEXT_LIMIT +
1672 $shorttext_exspc)
1673 && $line !~ /^:([0-7]{6}\s){2}
1674 ([[:xdigit:]]+\.*
1675 \s){2}\w+\s\w+/xms) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001676 WARN("LONG_COMMIT_TEXT",
1677 "commit text line over " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001678 SHORTTEXT_LIMIT .
1679 " characters\n" . $herecurr);
Steve Muckle336a3ae2012-03-09 11:15:24 -08001680 } elsif ($line=~/^\s*change-id:/i ||
1681 $line=~/^\s*signed-off-by:/i ||
1682 $line=~/^\s*crs-fixed:/i ||
1683 $line=~/^\s*acked-by:/i) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001684 # this is a tag, there must be commit
1685 # text by now
1686 if ($commit_text_present == 0) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001687 WARN("NO_COMMIT_TEXT",
1688 "please add commit text explaining " .
Steve Muckled3b3b642012-03-05 10:12:04 -08001689 "*why* the change is needed\n" .
1690 $herecurr);
1691 # prevent duplicate warnings
1692 $commit_text_present = 1;
1693 }
1694 } elsif ($line=~/\S/) {
1695 $commit_text_present = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696 }
Steve Muckled3b3b642012-03-05 10:12:04 -08001697 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
1698 # case of non-blank line in this state handled above
1699 $shorttext = IN_SHORTTEXT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001700 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
1701# The Subject line doesn't have to be the last header in the patch.
1702# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
1703# Per RFC5322, continuation lines must be folded, so any left-justified
1704# text which looks like a header is definitely a header.
1705 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
1706 $shorttext = IN_SHORTTEXT;
Steve Muckled3b3b642012-03-05 10:12:04 -08001707 # Check for Subject line followed by a blank line.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001708 if (length($line) != 0) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001709 WARN("NONBLANK_AFTER_SUMMARY",
1710 "non-blank line after " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001711 "summary line\n" .
1712 $sublinenr . $here .
1713 "\n" . $subjectline .
1714 "\n" . $line . "\n");
Steve Muckled3b3b642012-03-05 10:12:04 -08001715 # this non-blank line may or may not
1716 # be commit text - a warning has been
1717 # generated so assume it is commit
1718 # text and move on
1719 $commit_text_present = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001720 }
1721 }
Steve Muckled3b3b642012-03-05 10:12:04 -08001722 # The next two cases are BEFORE_SHORTTEXT.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001723 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001724 # This is the subject line. Go to
1725 # CHECK_NEXT_SHORTTEXT to wait for the commit
1726 # text to show up.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001727 $shorttext = CHECK_NEXT_SHORTTEXT;
1728 $subjectline = $line;
1729 $sublinenr = "#$linenr & ";
1730# Check for Subject line less than line limit
Matt Wagantall3c09aeb2012-08-18 11:21:00 -07001731 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001732 WARN("LONG_SUMMARY_LINE",
1733 "summary line over " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734 SHORTTEXT_LIMIT .
1735 " characters\n" . $herecurr);
1736 }
1737 } elsif ($line=~/^ (.*)/) {
Steve Muckled3b3b642012-03-05 10:12:04 -08001738 # Indented format, this must be the summary
1739 # line (i.e. git show). There will be no more
1740 # headers so we are now in the shorttext.
1741 $shorttext = IN_SHORTTEXT_BLANKLINE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742 $shorttext_exspc = 4;
Matt Wagantall3c09aeb2012-08-18 11:21:00 -07001743 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001744 WARN("LONG_SUMMARY_LINE",
1745 "summary line over " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001746 SHORTTEXT_LIMIT .
1747 " characters\n" . $herecurr);
1748 }
1749 }
1750 }
1751
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001752 $cnt_lines++ if ($realcnt != 0);
1753
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001754# Check for incorrect file permissions
1755 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1756 my $permhere = $here . "FILE: $realfile\n";
1757 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001758 ERROR("EXECUTE_PERMISSIONS",
1759 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001760 }
1761 }
1762
Joe Perches20112472011-07-25 17:13:23 -07001763# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001764 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001765 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001766 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001767 }
1768
1769# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001770 if (!$in_header_lines &&
1771 $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
Joe Perches20112472011-07-25 17:13:23 -07001772 my $space_before = $1;
1773 my $sign_off = $2;
1774 my $space_after = $3;
1775 my $email = $4;
1776 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1777
1778 if (defined $space_before && $space_before ne "") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001779 WARN("BAD_SIGN_OFF",
1780 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001781 }
Joe Perches20112472011-07-25 17:13:23 -07001782 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001783 WARN("BAD_SIGN_OFF",
1784 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001785 }
1786 if (!defined $space_after || $space_after ne " ") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001787 WARN("BAD_SIGN_OFF",
1788 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001789 }
1790
1791 my ($email_name, $email_address, $comment) = parse_email($email);
1792 my $suggested_email = format_email(($email_name, $email_address));
1793 if ($suggested_email eq "") {
Joe Perches000d1cc2011-07-25 17:13:25 -07001794 ERROR("BAD_SIGN_OFF",
1795 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001796 } else {
1797 my $dequoted = $suggested_email;
1798 $dequoted =~ s/^"//;
1799 $dequoted =~ s/" </ </;
1800 # Don't force email to have quotes
1801 # Allow just an angle bracketed address
1802 if ("$dequoted$comment" ne $email &&
1803 "<$email_address>$comment" ne $email &&
1804 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001805 WARN("BAD_SIGN_OFF",
1806 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001807 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001808 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001809 if ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001810 WARN("BAD_SIGN_OFF",
1811 "invalid Signed-off-by identity\n" . $line );
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001812 }
1813 }
1814
1815#check the patch for invalid author credentials
1816 if ($line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07001817 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001818 }
1819
Andy Whitcroft00df3442007-06-08 13:47:06 -07001820# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001821 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001822 ERROR("CORRUPTED_PATCH",
1823 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001824 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001825 }
1826
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001827# Check for absolute kernel paths.
1828 if ($tree) {
1829 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1830 my $file = $1;
1831
1832 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1833 check_absolute_file($1, $herecurr)) {
1834 #
1835 } else {
1836 check_absolute_file($file, $herecurr);
1837 }
1838 }
1839 }
1840
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001841# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1842 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001843 $rawline !~ m/^$UTF8*$/) {
1844 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1845
1846 my $blank = copy_spacing($rawline);
1847 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1848 my $hereptr = "$hereline$ptr\n";
1849
Joe Perches34d99212011-07-25 17:13:26 -07001850 CHK("INVALID_UTF8",
1851 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001852 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001853
Joe Perches15662b32011-10-31 17:13:12 -07001854# Check if it's the start of a commit log
1855# (not a header line and we haven't seen the patch filename)
1856 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001857 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001858 $in_header_lines = 0;
1859 $in_commit_log = 1;
1860 }
1861
1862# Still not yet in a patch, check for any UTF-8
1863 if ($in_commit_log && $realfile =~ /^$/ &&
1864 $rawline =~ /$NON_ASCII_UTF8/) {
1865 CHK("UTF8_BEFORE_PATCH",
1866 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1867 }
1868
Andy Whitcroft306708542008-10-15 22:02:28 -07001869# ignore non-hunk lines and lines being removed
1870 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001871
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001872#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001873 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001874 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001875 ERROR("DOS_LINE_ENDINGS",
1876 "DOS line endings\n" . $herevet);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001877
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001878 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1879 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001880 ERROR("TRAILING_WHITESPACE",
1881 "trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001882 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001883 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07001884
Andi Kleen33549572010-05-24 14:33:29 -07001885# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001886# Only applies when adding the entry originally, after that we do not have
1887# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001888 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001889 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001890 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001891 my $cnt = $realcnt;
1892 my $ln = $linenr + 1;
1893 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001894 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001895 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001896 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001897 $f = $lines[$ln - 1];
1898 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1899 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001900
1901 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001902
1903 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1904 $is_start = 1;
1905 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1906 $length = -1;
1907 }
1908
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001909 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001910 $f =~ s/#.*//;
1911 $f =~ s/^\s+//;
1912 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001913 if ($f =~ /^\s*config\s/) {
1914 $is_end = 1;
1915 last;
1916 }
Andi Kleen33549572010-05-24 14:33:29 -07001917 $length++;
1918 }
Joe Perches000d1cc2011-07-25 17:13:25 -07001919 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001920 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1921 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001922 }
1923
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001924 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1925 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1926 my $flag = $1;
1927 my $replacement = {
1928 'EXTRA_AFLAGS' => 'asflags-y',
1929 'EXTRA_CFLAGS' => 'ccflags-y',
1930 'EXTRA_CPPFLAGS' => 'cppflags-y',
1931 'EXTRA_LDFLAGS' => 'ldflags-y',
1932 };
1933
1934 WARN("DEPRECATED_VARIABLE",
1935 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1936 }
1937
Andy Whitcroft5368df22008-10-15 22:02:27 -07001938# check we are in a valid source file if not then ignore this hunk
1939 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1940
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001941#80 column limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001942 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001943 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001944 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001945 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946 $realfile ne "scripts/checkpatch.pl" &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001947 $length > 80)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001948 {
Joe Perches000d1cc2011-07-25 17:13:25 -07001949 WARN("LONG_LINE",
1950 "line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001951 }
1952
Josh Triplettca56dc02012-03-23 15:02:21 -07001953# Check for user-visible strings broken across lines, which breaks the ability
1954# to grep for the string. Limited to strings used as parameters (those
1955# following an open parenthesis), which almost completely eliminates false
1956# positives, as well as warning only once per parameter rather than once per
1957# line of the string. Make an exception when the previous string ends in a
1958# newline (multiple lines in one string constant) or \n\t (common in inline
1959# assembly to indent the instruction on the following line).
1960 if ($line =~ /^\+\s*"/ &&
1961 $prevline =~ /"\s*$/ &&
1962 $prevline =~ /\(/ &&
1963 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1964 WARN("SPLIT_STRING",
1965 "quoted string split across lines\n" . $hereprev);
1966 }
1967
Joe Perches5e79d962010-03-05 13:43:55 -08001968# check for spaces before a quoted newline
1969 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001970 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1971 "unnecessary whitespace before a quoted newline\n" . $herecurr);
Joe Perches5e79d962010-03-05 13:43:55 -08001972 }
1973
Andy Whitcroft8905a672007-11-28 16:21:06 -08001974# check for adding lines without a newline.
1975 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07001976 WARN("MISSING_EOF_NEWLINE",
1977 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001978 }
1979
Mike Frysinger42e41c52009-09-21 17:04:40 -07001980# Blackfin: use hi/lo macros
1981 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1982 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1983 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001984 ERROR("LO_MACRO",
1985 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001986 }
1987 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1988 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07001989 ERROR("HI_MACRO",
1990 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001991 }
1992 }
1993
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001994# check we are in a valid source file C or perl if not then ignore this hunk
1995 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001996
1997# at the beginning of a line any tabs must come first and anything
1998# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001999 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2000 $rawline =~ /^\+\s* \s*/) {
2001 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002002 ERROR("CODE_INDENT",
2003 "code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002004 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002005 }
2006
Alberto Panizzo08e44362010-03-05 13:43:54 -08002007# check for space before tabs.
2008 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2009 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002010 WARN("SPACE_BEFORE_TAB",
2011 "please, no space before tabs\n" . $herevet);
Alberto Panizzo08e44362010-03-05 13:43:54 -08002012 }
2013
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002014# check for && or || at the start of a line
2015 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2016 CHK("LOGICAL_CONTINUATIONS",
2017 "Logical continuations should be on the previous line\n" . $hereprev);
2018 }
2019
2020# check multi-line statement indentation matches previous line
2021 if ($^V && $^V ge 5.10.0 &&
2022 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
2023 $prevline =~ /^\+(\t*)(.*)$/;
2024 my $oldindent = $1;
2025 my $rest = $2;
2026
2027 my $pos = pos_last_openparen($rest);
2028 if ($pos >= 0) {
2029 $line =~ /^\+([ \t]*)/;
2030 my $newindent = $1;
2031
2032 my $goodtabindent = $oldindent .
2033 "\t" x ($pos / 8) .
2034 " " x ($pos % 8);
2035 my $goodspaceindent = $oldindent . " " x $pos;
2036
2037 if ($newindent ne $goodtabindent &&
2038 $newindent ne $goodspaceindent) {
2039 CHK("PARENTHESIS_ALIGNMENT",
2040 "Alignment should match open parenthesis\n" . $hereprev);
2041 }
2042 }
2043 }
2044
Joe Perchesaad4f612012-03-23 15:02:19 -07002045 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
2046 CHK("SPACING",
2047 "No space is necessary after a cast\n" . $hereprev);
2048 }
2049
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002050# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002051# Exceptions:
2052# 1) within comments
2053# 2) indented preprocessor commands
2054# 3) hanging labels
2055 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002056 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002057 WARN("LEADING_SPACE",
2058 "please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002059 }
2060
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002061# check we are in a valid C source file if not then ignore this hunk
2062 next if ($realfile !~ /\.(h|c)$/);
2063
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002064# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002065 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002066 WARN("CVS_KEYWORD",
2067 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002068 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002069
Mike Frysinger42e41c52009-09-21 17:04:40 -07002070# Blackfin: don't use __builtin_bfin_[cs]sync
2071 if ($line =~ /__builtin_bfin_csync/) {
2072 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002073 ERROR("CSYNC",
2074 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002075 }
2076 if ($line =~ /__builtin_bfin_ssync/) {
2077 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07002078 ERROR("SSYNC",
2079 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002080 }
2081
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002082# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002083 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2084 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002085#print "LINE<$line>\n";
2086 if ($linenr >= $suppress_statement &&
2087 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002088 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002089 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002090 $stat =~ s/\n./\n /g;
2091 $cond =~ s/\n./\n /g;
2092
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002093#print "linenr<$linenr> <$stat>\n";
2094 # If this statement has no statement boundaries within
2095 # it there is no point in retrying a statement scan
2096 # until we hit end of it.
2097 my $frag = $stat; $frag =~ s/;+\s*$//;
2098 if ($frag !~ /(?:{|;)/) {
2099#print "skip<$line_nr_next>\n";
2100 $suppress_statement = $line_nr_next;
2101 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002102
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002103 # Find the real next line.
2104 $realline_next = $line_nr_next;
2105 if (defined $realline_next &&
2106 (!defined $lines[$realline_next - 1] ||
2107 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2108 $realline_next++;
2109 }
2110
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002111 my $s = $stat;
2112 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002113
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002114 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002115 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002116
2117 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002118 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002119
Andy Whitcroft463f2862009-09-21 17:04:34 -07002120 } elsif ($s =~ /^.\s*else\b/s) {
2121
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002122 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002123 } 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 -07002124 my $type = $1;
2125 $type =~ s/\s+/ /g;
2126 possible($type, "A:" . $s);
2127
Andy Whitcroft8905a672007-11-28 16:21:06 -08002128 # definitions in global scope can only start with types
Andy Whitcrofta6a84062008-10-15 22:02:30 -07002129 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002130 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002131 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002132
2133 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002134 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002135 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002136 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002137
2138 # Check for any sort of function declaration.
2139 # int foo(something bar, other baz);
2140 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002141 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 -08002142 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002143
Andy Whitcroftcf655042008-03-04 14:28:20 -08002144 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002145 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002146 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002147
Andy Whitcroft8905a672007-11-28 16:21:06 -08002148 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002149 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002150
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002151 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002152 }
2153 }
2154 }
2155
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002156 }
2157
Andy Whitcroft653d4872007-06-23 17:16:34 -07002158#
2159# Checks which may be anchored in the context.
2160#
2161
2162# Check for switch () and associated case and default
2163# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002164 if ($line=~/\bswitch\s*\(.*\)/) {
2165 my $err = '';
2166 my $sep = '';
2167 my @ctx = ctx_block_outer($linenr, $realcnt);
2168 shift(@ctx);
2169 for my $ctx (@ctx) {
2170 my ($clen, $cindent) = line_stats($ctx);
2171 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2172 $indent != $cindent) {
2173 $err .= "$sep$ctx\n";
2174 $sep = '';
2175 } else {
2176 $sep = "[...]\n";
2177 }
2178 }
2179 if ($err ne '') {
Joe Perches000d1cc2011-07-25 17:13:25 -07002180 ERROR("SWITCH_CASE_INDENT_LEVEL",
2181 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002182 }
2183 }
2184
2185# if/while/etc brace do not go on next line, unless defining a do while loop,
2186# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002187 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002188 my $pre_ctx = "$1$2";
2189
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002190 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002191
2192 if ($line =~ /^\+\t{6,}/) {
2193 WARN("DEEP_INDENTATION",
2194 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2195 }
2196
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002197 my $ctx_cnt = $realcnt - $#ctx - 1;
2198 my $ctx = join("\n", @ctx);
2199
Andy Whitcroft548596d2008-07-23 21:29:01 -07002200 my $ctx_ln = $linenr;
2201 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002202
Andy Whitcroft548596d2008-07-23 21:29:01 -07002203 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2204 defined $lines[$ctx_ln - 1] &&
2205 $lines[$ctx_ln - 1] =~ /^-/)) {
2206 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2207 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002208 $ctx_ln++;
2209 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002210
Andy Whitcroft53210162008-07-23 21:29:03 -07002211 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2212 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002213
2214 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002215 ERROR("OPEN_BRACE",
2216 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002217 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002218 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002219 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2220 $ctx =~ /\)\s*\;\s*$/ &&
2221 defined $lines[$ctx_ln - 1])
2222 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002223 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2224 if ($nindent > $indent) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002225 WARN("TRAILING_SEMICOLON",
2226 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002227 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002228 }
2229 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002230 }
2231
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002232# Check relative indent for conditionals and blocks.
2233 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002234 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2235 ctx_statement_block($linenr, $realcnt, 0)
2236 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002237 my ($s, $c) = ($stat, $cond);
2238
2239 substr($s, 0, length($c), '');
2240
2241 # Make sure we remove the line prefixes as we have
2242 # none on the first line, and are going to readd them
2243 # where necessary.
2244 $s =~ s/\n./\n/gs;
2245
2246 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002247 my @newlines = ($c =~ /\n/gs);
2248 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002249
2250 # We want to check the first line inside the block
2251 # starting at the end of the conditional, so remove:
2252 # 1) any blank line termination
2253 # 2) any opening brace { on end of the line
2254 # 3) any do (...) {
2255 my $continuation = 0;
2256 my $check = 0;
2257 $s =~ s/^.*\bdo\b//;
2258 $s =~ s/^\s*{//;
2259 if ($s =~ s/^\s*\\//) {
2260 $continuation = 1;
2261 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002262 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002263 $check = 1;
2264 $cond_lines++;
2265 }
2266
2267 # Also ignore a loop construct at the end of a
2268 # preprocessor statement.
2269 if (($prevline =~ /^.\s*#\s*define\s/ ||
2270 $prevline =~ /\\\s*$/) && $continuation == 0) {
2271 $check = 0;
2272 }
2273
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002274 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002275 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002276 while ($cond_ptr != $cond_lines) {
2277 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002278
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002279 # If we see an #else/#elif then the code
2280 # is not linear.
2281 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2282 $check = 0;
2283 }
2284
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002285 # Ignore:
2286 # 1) blank lines, they should be at 0,
2287 # 2) preprocessor lines, and
2288 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002289 if ($continuation ||
2290 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002291 $s =~ /^\s*#\s*?/ ||
2292 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002293 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002294 if ($s =~ s/^.*?\n//) {
2295 $cond_lines++;
2296 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002297 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002298 }
2299
2300 my (undef, $sindent) = line_stats("+" . $s);
2301 my $stat_real = raw_line($linenr, $cond_lines);
2302
2303 # Check if either of these lines are modified, else
2304 # this is not this patch's fault.
2305 if (!defined($stat_real) ||
2306 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2307 $check = 0;
2308 }
2309 if (defined($stat_real) && $cond_lines > 1) {
2310 $stat_real = "[...]\n$stat_real";
2311 }
2312
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002313 #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 -07002314
2315 if ($check && (($sindent % 8) != 0 ||
2316 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002317 WARN("SUSPECT_CODE_INDENT",
2318 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002319 }
2320 }
2321
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002322 # Track the 'values' across context and added lines.
2323 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002324 my ($curr_values, $curr_vars) =
2325 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002326 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002327 if ($dbg_values) {
2328 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002329 print "$linenr > .$outline\n";
2330 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002331 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002332 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002333 $prev_values = substr($curr_values, -1);
2334
Andy Whitcroft00df3442007-06-08 13:47:06 -07002335#ignore lines not being added
2336 if ($line=~/^[^\+]/) {next;}
2337
Andy Whitcroft653d4872007-06-23 17:16:34 -07002338# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002339 if ($dbg_type) {
2340 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002341 ERROR("TEST_TYPE",
2342 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002343 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002344 ERROR("TEST_NOT_TYPE",
2345 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002346 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002347 next;
2348 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002349# TEST: allow direct testing of the attribute matcher.
2350 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002351 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002352 ERROR("TEST_ATTR",
2353 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002354 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002355 ERROR("TEST_NOT_ATTR",
2356 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002357 }
2358 next;
2359 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002360
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002361# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002362 if ($line =~ /^.\s*{/ &&
2363 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002364 ERROR("OPEN_BRACE",
2365 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002366 }
2367
Andy Whitcroft653d4872007-06-23 17:16:34 -07002368#
2369# Checks which are anchored on the added line.
2370#
2371
2372# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002373 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002374 my $path = $1;
2375 if ($path =~ m{//}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002376 ERROR("MALFORMED_INCLUDE",
2377 "malformed #include filename\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002378 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002379 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002380 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002381
2382# no C99 // comments
2383 if ($line =~ m{//}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002384 ERROR("C99_COMMENTS",
2385 "do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002386 }
2387 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002388 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002389 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002390
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002391# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2392# the whole statement.
2393#print "APW <$lines[$realline_next - 1]>\n";
2394 if (defined $realline_next &&
2395 exists $lines[$realline_next - 1] &&
2396 !defined $suppress_export{$realline_next} &&
2397 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2398 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002399 # Handle definitions which produce identifiers with
2400 # a prefix:
2401 # XXX(foo);
2402 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002403 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002404 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002405 $name =~ /^${Ident}_$2/) {
2406#print "FOO C name<$name>\n";
2407 $suppress_export{$realline_next} = 1;
2408
2409 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002410 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002411 ^.DEFINE_$Ident\(\Q$name\E\)|
2412 ^.DECLARE_$Ident\(\Q$name\E\)|
2413 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002414 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2415 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002416 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002417#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2418 $suppress_export{$realline_next} = 2;
2419 } else {
2420 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002421 }
2422 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002423 if (!defined $suppress_export{$linenr} &&
2424 $prevline =~ /^.\s*$/ &&
2425 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2426 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2427#print "FOO B <$lines[$linenr - 1]>\n";
2428 $suppress_export{$linenr} = 2;
2429 }
2430 if (defined $suppress_export{$linenr} &&
2431 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002432 WARN("EXPORT_SYMBOL",
2433 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002434 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002435
Joe Eloff5150bda2010-08-09 17:21:00 -07002436# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002437 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002438 ERROR("GLOBAL_INITIALISERS",
2439 "do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002440 $herecurr);
2441 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002442# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08002443 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002444 ERROR("INITIALISED_STATIC",
2445 "do not initialise statics to 0 or NULL\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002446 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002447 }
2448
Joe Perchescb710ec2010-10-26 14:23:20 -07002449# check for static const char * arrays.
2450 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002451 WARN("STATIC_CONST_CHAR_ARRAY",
2452 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002453 $herecurr);
2454 }
2455
2456# check for static char foo[] = "bar" declarations.
2457 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002458 WARN("STATIC_CONST_CHAR_ARRAY",
2459 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002460 $herecurr);
2461 }
2462
Joe Perches93ed0e22010-10-26 14:23:21 -07002463# check for declarations of struct pci_device_id
2464 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002465 WARN("DEFINE_PCI_DEVICE_TABLE",
2466 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002467 }
2468
Andy Whitcroft653d4872007-06-23 17:16:34 -07002469# check for new typedefs, only function parameters and sparse annotations
2470# make sense.
2471 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002472 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002473 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002474 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002475 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002476 WARN("NEW_TYPEDEFS",
2477 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002478 }
2479
2480# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002481 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002482 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2483 #print "AA<$1>\n";
2484 my ($from, $to) = ($2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002485
Andy Whitcroft65863862009-01-06 14:41:21 -08002486 # Should start with a space.
2487 $to =~ s/^(\S)/ $1/;
2488 # Should not end with a space.
2489 $to =~ s/\s+$//;
2490 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002491 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002492 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002493
Andy Whitcroft65863862009-01-06 14:41:21 -08002494 #print "from<$from> to<$to>\n";
2495 if ($from ne $to) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002496 ERROR("POINTER_LOCATION",
2497 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002498 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002499 }
2500 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2501 #print "BB<$1>\n";
2502 my ($from, $to, $ident) = ($2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002503
Andy Whitcroft65863862009-01-06 14:41:21 -08002504 # Should start with a space.
2505 $to =~ s/^(\S)/ $1/;
2506 # Should not end with a space.
2507 $to =~ s/\s+$//;
2508 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002509 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002510 }
2511 # Modifiers should have spaces.
2512 $to =~ s/(\b$Modifier$)/$1 /;
2513
Andy Whitcroft667026e2009-02-27 14:03:08 -08002514 #print "from<$from> to<$to> ident<$ident>\n";
2515 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002516 ERROR("POINTER_LOCATION",
2517 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002518 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002519 }
2520
2521# # no BUG() or BUG_ON()
2522# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2523# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2524# print "$herecurr";
2525# $clean = 0;
2526# }
2527
Andy Whitcroft8905a672007-11-28 16:21:06 -08002528 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002529 WARN("LINUX_VERSION_CODE",
2530 "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 -08002531 }
2532
Joe Perches17441222011-06-15 15:08:17 -07002533# check for uses of printk_ratelimit
2534 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002535 WARN("PRINTK_RATELIMITED",
2536"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002537 }
2538
Andy Whitcroft00df3442007-06-08 13:47:06 -07002539# printk should use KERN_* levels. Note that follow on printk's on the
2540# same line do not need a level, so we use the current block context
2541# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002542# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002543# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002544 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07002545 my $ok = 0;
2546 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2547 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002548 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07002549 # with "\n" ignore it, else it is to blame
2550 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2551 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2552 $ok = 1;
2553 }
2554 last;
2555 }
2556 }
2557 if ($ok == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002558 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2559 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002560 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002561 }
2562
Andy Whitcroft653d4872007-06-23 17:16:34 -07002563# function brace can't be on same line, except for #defines of do while,
2564# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002565 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2566 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002567 ERROR("OPEN_BRACE",
2568 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002569 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002570
Andy Whitcroft8905a672007-11-28 16:21:06 -08002571# open braces for enum, union and struct go on the same line.
2572 if ($line =~ /^.\s*{/ &&
2573 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002574 ERROR("OPEN_BRACE",
2575 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002576 }
2577
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002578# missing space after union, struct or enum definition
2579 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002580 WARN("SPACING",
2581 "missing space after $1 definition\n" . $herecurr);
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002582 }
2583
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002584# check for spacing round square brackets; allowed:
2585# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002586# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2587# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002588 while ($line =~ /(.*?\s)\[/g) {
2589 my ($where, $prefix) = ($-[1], $1);
2590 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002591 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002592 $prefix !~ /[{,]\s+$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002593 ERROR("BRACKET_SPACE",
2594 "space prohibited before open square bracket '['\n" . $herecurr);
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002595 }
2596 }
2597
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002598# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002599 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002600 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002601 my $ctx_before = substr($line, 0, $-[1]);
2602 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002603
2604 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002605 if ($name =~ /^(?:
2606 if|for|while|switch|return|case|
2607 volatile|__volatile__|
2608 __attribute__|format|__extension__|
2609 asm|__asm__)$/x)
2610 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002611
2612 # cpp #define statements have non-optional spaces, ie
2613 # if there is a space between the name and the open
2614 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002615 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002616
2617 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002618 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002619
2620 # If this whole things ends with a type its most
2621 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002622 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002623
2624 } else {
Joe Perches000d1cc2011-07-25 17:13:25 -07002625 WARN("SPACING",
2626 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002627 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002628 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002629# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002630 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002631 my $ops = qr{
2632 <<=|>>=|<=|>=|==|!=|
2633 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2634 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002635 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2636 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002637 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002638 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002639 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002640
2641 my $blank = copy_spacing($opline);
2642
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002643 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002644 $off += length($elements[$n]);
2645
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002646 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002647 my $ca = substr($opline, 0, $off);
2648 my $cc = '';
2649 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2650 $cc = substr($opline, $off + length($elements[$n + 1]));
2651 }
2652 my $cb = "$ca$;$cc";
2653
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002654 my $a = '';
2655 $a = 'V' if ($elements[$n] ne '');
2656 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002657 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002658 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2659 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002660 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002661
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002662 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002663
2664 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002665 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002666 $c = 'V' if ($elements[$n + 2] ne '');
2667 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002668 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002669 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2670 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002671 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002672 } else {
2673 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002674 }
2675
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002676 my $ctx = "${a}x${c}";
2677
2678 my $at = "(ctx:$ctx)";
2679
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002680 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002681 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002682
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002683 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002684 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002685
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002686 # Get the full operator variant.
2687 my $opv = $op . substr($curr_vars, $off, 1);
2688
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002689 # Ignore operators passed as parameters.
2690 if ($op_type ne 'V' &&
2691 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2692
Andy Whitcroftcf655042008-03-04 14:28:20 -08002693# # Ignore comments
2694# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002695
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002696 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002697 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002698 if ($ctx !~ /.x[WEBC]/ &&
2699 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002700 ERROR("SPACING",
2701 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002702 }
2703
2704 # // is a comment
2705 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002706
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002707 # No spaces for:
2708 # ->
2709 # : when part of a bitfield
2710 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002711 if ($ctx =~ /Wx.|.xW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002712 ERROR("SPACING",
2713 "spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002714 }
2715
2716 # , must have a space on the right.
2717 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002718 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002719 ERROR("SPACING",
2720 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002721 }
2722
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002723 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002724 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002725 #warn "'*' is part of type\n";
2726
2727 # unary operators should have a space before and
2728 # none after. May be left adjacent to another
2729 # unary operator, or a cast
2730 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002731 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002732 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002733 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002734 ERROR("SPACING",
2735 "space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002736 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002737 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002738 # A unary '*' may be const
2739
2740 } elsif ($ctx =~ /.xW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002741 ERROR("SPACING",
2742 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002743 }
2744
2745 # unary ++ and unary -- are allowed no space on one side.
2746 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002747 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002748 ERROR("SPACING",
2749 "space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002750 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002751 if ($ctx =~ /Wx[BE]/ ||
2752 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002753 ERROR("SPACING",
2754 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002755 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002756 if ($ctx =~ /ExW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002757 ERROR("SPACING",
2758 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002759 }
2760
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002761
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002762 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002763 } elsif ($op eq '<<' or $op eq '>>' or
2764 $op eq '&' or $op eq '^' or $op eq '|' or
2765 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002766 $op eq '*' or $op eq '/' or
2767 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002768 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002769 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002770 ERROR("SPACING",
2771 "need consistent spacing around '$op' $at\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002772 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002773 }
2774
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002775 # A colon needs no spaces before when it is
2776 # terminating a case value or a label.
2777 } elsif ($opv eq ':C' || $opv eq ':L') {
2778 if ($ctx =~ /Wx./) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002779 ERROR("SPACING",
2780 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002781 }
2782
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002783 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002784 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002785 my $ok = 0;
2786
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002787 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002788 if (($op eq '<' &&
2789 $cc =~ /^\S+\@\S+>/) ||
2790 ($op eq '>' &&
2791 $ca =~ /<\S+\@\S+$/))
2792 {
2793 $ok = 1;
2794 }
2795
2796 # Ignore ?:
2797 if (($opv eq ':O' && $ca =~ /\?$/) ||
2798 ($op eq '?' && $cc =~ /^:/)) {
2799 $ok = 1;
2800 }
2801
2802 if ($ok == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002803 ERROR("SPACING",
2804 "spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002805 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002806 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002807 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002808 }
2809 }
2810
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002811# check for multiple assignments
2812 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002813 CHK("MULTIPLE_ASSIGNMENTS",
2814 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002815 }
2816
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002817## # check for multiple declarations, allowing for a function declaration
2818## # continuation.
2819## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2820## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2821##
2822## # Remove any bracketed sections to ensure we do not
2823## # falsly report the parameters of functions.
2824## my $ln = $line;
2825## while ($ln =~ s/\([^\(\)]*\)//g) {
2826## }
2827## if ($ln =~ /,/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002828## WARN("MULTIPLE_DECLARATION",
2829## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002830## }
2831## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002832
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002833#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002834 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2835 $line =~ /do{/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002836 ERROR("SPACING",
2837 "space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002838 }
2839
2840# closing brace should have a space following it when it has anything
2841# on the line
2842 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002843 ERROR("SPACING",
2844 "space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002845 }
2846
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002847# check spacing on square brackets
2848 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002849 ERROR("SPACING",
2850 "space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002851 }
2852 if ($line =~ /\s\]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002853 ERROR("SPACING",
2854 "space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002855 }
2856
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002857# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002858 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002859 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002860 ERROR("SPACING",
Steve Mucklef132c6c2012-06-06 18:30:57 -07002861 "space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002862 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002863 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002864 $line !~ /for\s*\(.*;\s+\)/ &&
2865 $line !~ /:\s+\)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002866 ERROR("SPACING",
2867 "space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002868 }
2869
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002870#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002871 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002872 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002873 WARN("INDENTED_LABEL",
2874 "labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002875 }
2876
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002877# Return is not a function.
2878 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2879 my $spacing = $1;
2880 my $value = $2;
2881
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002882 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002883 $value =~ s/\(/ \(/g;
2884 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08002885 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002886 $value !~ /(?:$Ident|-?$Constant)\s*
2887 $Compare\s*
2888 (?:$Ident|-?$Constant)/x &&
2889 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002890 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002891#print "value<$value>\n";
2892 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002893 ERROR("RETURN_PARENTHESES",
2894 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002895
2896 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002897 ERROR("SPACING",
2898 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002899 }
2900 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002901# Return of what appears to be an errno should normally be -'ve
2902 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2903 my $name = $1;
2904 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc2011-07-25 17:13:25 -07002905 WARN("USE_NEGATIVE_ERRNO",
2906 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002907 }
2908 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002909
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002910# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002911 if ($line=~/\b(if|while|for|switch)\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002912 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002913 }
2914
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002915# Check for illegal assignment in if conditional -- and check for trailing
2916# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002917 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002918 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2919 ctx_statement_block($linenr, $realcnt, 0)
2920 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002921 my ($stat_next) = ctx_statement_block($line_nr_next,
2922 $remain_next, $off_next);
2923 $stat_next =~ s/\n./\n /g;
2924 ##print "stat<$stat> stat_next<$stat_next>\n";
2925
2926 if ($stat_next =~ /^\s*while\b/) {
2927 # If the statement carries leading newlines,
2928 # then count those as offsets.
2929 my ($whitespace) =
2930 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2931 my $offset =
2932 statement_rawlines($whitespace) - 1;
2933
2934 $suppress_whiletrailers{$line_nr_next +
2935 $offset} = 1;
2936 }
2937 }
2938 if (!defined $suppress_whiletrailers{$linenr} &&
2939 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002940 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002941
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002942 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002943 ERROR("ASSIGN_IN_IF",
2944 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002945 }
2946
2947 # Find out what is on the end of the line after the
2948 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002949 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002950 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002951 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002952 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2953 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002954 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002955 # Find out how long the conditional actually is.
2956 my @newlines = ($c =~ /\n/gs);
2957 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002958 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002959
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002960 $stat_real = raw_line($linenr, $cond_lines)
2961 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002962 if (defined($stat_real) && $cond_lines > 1) {
2963 $stat_real = "[...]\n$stat_real";
2964 }
2965
Joe Perches000d1cc2011-07-25 17:13:25 -07002966 ERROR("TRAILING_STATEMENTS",
2967 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002968 }
2969 }
2970
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002971# Check for bitwise tests written as boolean
2972 if ($line =~ /
2973 (?:
2974 (?:\[|\(|\&\&|\|\|)
2975 \s*0[xX][0-9]+\s*
2976 (?:\&\&|\|\|)
2977 |
2978 (?:\&\&|\|\|)
2979 \s*0[xX][0-9]+\s*
2980 (?:\&\&|\|\||\)|\])
2981 )/x)
2982 {
Joe Perches000d1cc2011-07-25 17:13:25 -07002983 WARN("HEXADECIMAL_BOOLEAN_TEST",
2984 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002985 }
2986
Andy Whitcroft8905a672007-11-28 16:21:06 -08002987# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002988 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2989 my $s = $1;
2990 $s =~ s/$;//g; # Remove any comments
2991 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002992 ERROR("TRAILING_STATEMENTS",
2993 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002994 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002995 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002996# if should not continue a brace
2997 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07002998 ERROR("TRAILING_STATEMENTS",
2999 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003000 $herecurr);
3001 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003002# case and default should not have general statements after them
3003 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3004 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003005 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003006 \s*return\s+
3007 )/xg)
3008 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003009 ERROR("TRAILING_STATEMENTS",
3010 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003011 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003012
3013 # Check for }<nl>else {, these must be at the same
3014 # indent level to be relevant to each other.
3015 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3016 $previndent == $indent) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003017 ERROR("ELSE_AFTER_BRACE",
3018 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003019 }
3020
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003021 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3022 $previndent == $indent) {
3023 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3024
3025 # Find out what is on the end of the line after the
3026 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003027 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003028 $s =~ s/\n.*//g;
3029
3030 if ($s =~ /^\s*;/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003031 ERROR("WHILE_AFTER_BRACE",
3032 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003033 }
3034 }
3035
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003036#studly caps, commented out until figure out how to distinguish between use of existing and adding new
3037# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
3038# print "No studly caps, use _\n";
3039# print "$herecurr";
3040# $clean = 0;
3041# }
3042
3043#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003044 if ($line=~/\#\s*define.*\\\s$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003045 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3046 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003047 }
3048
Andy Whitcroft653d4872007-06-23 17:16:34 -07003049#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003050 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003051 my $file = "$1.h";
3052 my $checkfile = "include/linux/$file";
3053 if (-f "$root/$checkfile" &&
3054 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07003055 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003056 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003057 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003058 CHK("ARCH_INCLUDE_LINUX",
3059 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003060 } else {
Joe Perches000d1cc2011-07-25 17:13:25 -07003061 WARN("INCLUDE_LINUX",
3062 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07003063 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003064 }
3065 }
3066
Andy Whitcroft653d4872007-06-23 17:16:34 -07003067# multi-statement macros should be enclosed in a do while loop, grab the
3068# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003069# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07003070 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3071 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003072 my $ln = $linenr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003073 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003074 my ($off, $dstat, $dcond, $rest);
3075 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003076 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003077 ctx_statement_block($linenr, $realcnt, 0);
3078 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003079 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003080 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003081
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003082 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003083 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003084 $dstat =~ s/\\\n.//g;
3085 $dstat =~ s/^\s*//s;
3086 $dstat =~ s/\s*$//s;
3087
3088 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003089 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3090 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003091 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003092 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003093 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003094
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003095 # Extremely long macros may fall off the end of the
3096 # available context without closing. Give a dangling
3097 # backslash the benefit of the doubt and allow it
3098 # to gobble any hanging open-parens.
3099 $dstat =~ s/\(.+\\$/1/;
3100
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003101 # Flatten any obvious string concatentation.
3102 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3103 $dstat =~ s/$Ident\s*("X*")/$1/)
3104 {
3105 }
3106
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003107 my $exceptions = qr{
3108 $Declare|
3109 module_param_named|
3110 MODULE_PARAM_DESC|
3111 DECLARE_PER_CPU|
3112 DEFINE_PER_CPU|
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003113 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003114 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003115 union|
3116 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003117 \.$Ident\s*=\s*|
3118 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003119 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003120 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003121 if ($dstat ne '' &&
3122 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3123 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Andy Whitcroftfd1b57a2012-03-23 15:02:18 -07003124 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003125 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003126 $dstat !~ /$exceptions/ &&
3127 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003128 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003129 $dstat !~ /^for\s*$Constant$/ && # for (...)
3130 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3131 $dstat !~ /^do\s*{/ && # do {...
3132 $dstat !~ /^\({/) # ({...
3133 {
3134 $ctx =~ s/\n*$//;
3135 my $herectx = $here . "\n";
3136 my $cnt = statement_rawlines($ctx);
3137
3138 for (my $n = 0; $n < $cnt; $n++) {
3139 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003140 }
3141
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003142 if ($dstat =~ /;/) {
3143 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3144 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3145 } else {
Joe Perches000d1cc2011-07-25 17:13:25 -07003146 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003147 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003148 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003149 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003150 }
3151
Mike Frysinger080ba922009-01-06 14:41:25 -08003152# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3153# all assignments may have only one of the following with an assignment:
3154# .
3155# ALIGN(...)
3156# VMLINUX_SYMBOL(...)
3157 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003158 WARN("MISSING_VMLINUX_SYMBOL",
3159 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003160 }
3161
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003162# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003163 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3164 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003165 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003166 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003167 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3168 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003169 my @allowed = ();
3170 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003171 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003172 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003173 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003174 for my $chunk (@chunks) {
3175 my ($cond, $block) = @{$chunk};
3176
Andy Whitcroft773647a2008-03-28 14:15:58 -07003177 # If the condition carries leading newlines, then count those as offsets.
3178 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3179 my $offset = statement_rawlines($whitespace) - 1;
3180
Joe Perchesaad4f612012-03-23 15:02:19 -07003181 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003182 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3183
3184 # We have looked at and allowed this specific line.
3185 $suppress_ifbraces{$ln + $offset} = 1;
3186
3187 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003188 $ln += statement_rawlines($block) - 1;
3189
Andy Whitcroft773647a2008-03-28 14:15:58 -07003190 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003191
3192 $seen++ if ($block =~ /^\s*{/);
3193
Joe Perchesaad4f612012-03-23 15:02:19 -07003194 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003195 if (statement_lines($cond) > 1) {
3196 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003197 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003198 }
3199 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003200 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003201 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003202 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003203 if (statement_block_size($block) > 1) {
3204 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003205 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003206 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003207 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003208 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003209 if ($seen) {
3210 my $sum_allowed = 0;
3211 foreach (@allowed) {
3212 $sum_allowed += $_;
3213 }
3214 if ($sum_allowed == 0) {
3215 WARN("BRACES",
3216 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3217 } elsif ($sum_allowed != $allow &&
3218 $seen != $allow) {
3219 CHK("BRACES",
3220 "braces {} should be used on all arms of this statement\n" . $herectx);
3221 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003222 }
3223 }
3224 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003225 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003226 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003227 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003228
Andy Whitcroftcf655042008-03-04 14:28:20 -08003229 # Check the pre-context.
3230 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3231 #print "APW: ALLOWED: pre<$1>\n";
3232 $allowed = 1;
3233 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003234
3235 my ($level, $endln, @chunks) =
3236 ctx_statement_full($linenr, $realcnt, $-[0]);
3237
Andy Whitcroftcf655042008-03-04 14:28:20 -08003238 # Check the condition.
3239 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003240 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003241 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003242 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003243 }
3244 if (statement_lines($cond) > 1) {
3245 #print "APW: ALLOWED: cond<$cond>\n";
3246 $allowed = 1;
3247 }
3248 if ($block =~/\b(?:if|for|while)\b/) {
3249 #print "APW: ALLOWED: block<$block>\n";
3250 $allowed = 1;
3251 }
3252 if (statement_block_size($block) > 1) {
3253 #print "APW: ALLOWED: lines block<$block>\n";
3254 $allowed = 1;
3255 }
3256 # Check the post-context.
3257 if (defined $chunks[1]) {
3258 my ($cond, $block) = @{$chunks[1]};
3259 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003260 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003261 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003262 if ($block =~ /^\s*\{/) {
3263 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3264 $allowed = 1;
3265 }
3266 }
3267 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003268 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003269 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003270
Andy Whitcroftf0556632008-10-15 22:02:23 -07003271 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003272 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003273 }
3274
Joe Perches000d1cc2011-07-25 17:13:25 -07003275 WARN("BRACES",
3276 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003277 }
3278 }
3279
Andy Whitcroft653d4872007-06-23 17:16:34 -07003280# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003281 for my $inc (@dep_includes) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003282 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003283 ERROR("DEPRECATED_INCLUDE",
3284 "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003285 }
3286 }
3287
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003288# don't use deprecated functions
3289 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003290 if ($line =~ /\b$func\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003291 ERROR("DEPRECATED_FUNCTION",
3292 "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003293 }
3294 }
3295
3296# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003297 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3298 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003299 WARN("VOLATILE",
3300 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003301 }
3302
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003303# sys_open/read/write/close are not allowed in the kernel
3304 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003305 ERROR("FILE_OPS", "$1 is inappropriate in kernel code.\n" .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003306 $herecurr);
3307 }
3308
3309# filp_open is a backdoor for sys_open
3310 if ($line =~ /\b(filp_open)\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003311 ERROR("FILE_OPS", "$1 is inappropriate in kernel code.\n" .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003312 $herecurr);
3313 }
3314
3315# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
3316 if ($line =~ /\b((?:read|write)[bwl])\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003317 ERROR("NON_RELAXED_IO",
3318 "Use of $1 is deprecated: use $1_relaxed\n\t" .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003319 "with appropriate memory barriers instead.\n" .
3320 $herecurr);
3321 }
3322
3323# likewise, in/out[bwl] should be __raw_read/write[bwl]...
3324 if ($line =~ /\b((in|out)([bwl]))\b/) {
3325 my ($all, $pref, $suf) = ($1, $2, $3);
3326 $pref =~ s/in/read/;
3327 $pref =~ s/out/write/;
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003328 ERROR("NON_RELAXED_IO",
3329 "Use of $all is deprecated: use " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003330 "__raw_$pref$suf\n\t" .
3331 "with appropriate memory barriers instead.\n" .
3332 $herecurr);
3333 }
3334
3335# dsb is too ARMish, and should usually be mb.
3336 if ($line =~ /\bdsb\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003337 WARN("ARM_BARRIER",
3338 "Use of dsb is discouranged: prefer mb.\n" .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003339 $herecurr);
3340 }
3341
Rohit Vaswani60d78bb2011-12-06 20:03:07 -08003342# MSM - check if a non board-gpiomux file has any gpiomux declarations
3343 if ($realfile =~ /\/mach-msm\/board-[0-9]+/ &&
3344 $realfile !~ /camera/ && $realfile !~ /gpiomux/ &&
3345 $line =~ /\s*struct msm_gpiomux_config\s*/ ) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003346 WARN("GPIOMUX_IN_BOARD",
3347 "Non gpiomux board file cannot have a gpiomux config declarations. Please declare gpiomux configs in board-*-gpiomux.c file.\n" . $herecurr);
Rohit Vaswani60d78bb2011-12-06 20:03:07 -08003348 }
3349
Pankaj Kumaradfb9332012-01-06 15:02:19 +05303350# MSM - check if vreg_xxx function are used
3351 if ($line =~ /\b(vreg_(get|put|set_level|enable|disable))\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003352 WARN("DEPRECATED_VREG_APIS", "Use of $1 API is deprecated: " .
Pankaj Kumaradfb9332012-01-06 15:02:19 +05303353 "use regulator APIs\n" . $herecurr);
3354 }
3355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003356# unbounded string functions are overflow risks
3357 my %str_fns = (
3358 "sprintf" => "snprintf",
Olav Haugan665be0d2012-02-06 09:42:18 -08003359 "strcpy" => "strlcpy",
3360 "strncpy" => "strlcpy",
3361 "strcat" => "strlcat",
3362 "strncat" => "strlcat",
3363 "vsprintf" => "vsnprintf",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003364 "strchr" => "strnchr",
3365 "strstr" => "strnstr",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003366 );
3367 foreach my $k (keys %str_fns) {
3368 if ($line =~ /\b$k\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003369 ERROR("UNBOUNDED_STRING_FNS",
3370 "Use of $k is deprecated: " .
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003371 "use $str_fns{$k} instead.\n" .
3372 $herecurr);
3373 }
3374 }
3375
Andy Whitcroft00df3442007-06-08 13:47:06 -07003376# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003377 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003378 WARN("IF_0",
3379 "if this code is redundant consider removing it\n"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003380 . $herecurr);
3381 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07003382
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003383# warn about #if 1
3384 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003385 WARN("IF_1",
3386 "if this code is required consider removing"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003387 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003388 }
3389
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003390# check for needless kfree() checks
3391 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3392 my $expr = $1;
3393 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003394 WARN("NEEDLESS_KFREE",
3395 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003396 }
3397 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003398# check for needless usb_free_urb() checks
3399 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3400 my $expr = $1;
3401 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003402 WARN("NEEDLESS_USB_FREE_URB",
3403 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003404 }
3405 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003406
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003407# prefer usleep_range over udelay
3408 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
3409 # ignore udelay's < 10, however
3410 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003411 CHK("USLEEP_RANGE",
3412 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003413 }
3414 }
3415
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003416# warn about unexpectedly long msleep's
3417 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3418 if ($1 < 20) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003419 WARN("MSLEEP",
3420 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003421 }
3422 }
3423
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003424# check the patch for use of mdelay
3425 if ($line =~ /\bmdelay\s*\(/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003426 WARN("MDELAY",
3427 "use of mdelay() found: msleep() is the preferred API.\n" . $line );
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003428 }
3429
Andy Whitcroft00df3442007-06-08 13:47:06 -07003430# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003431# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003432# print "#ifdef in C files should be avoided\n";
3433# print "$herecurr";
3434# $clean = 0;
3435# }
3436
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003437# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003438 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003439 ERROR("SPACING",
3440 "exactly one space required after that #$1\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003441 }
3442
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003443# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003444 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3445 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003446 my $which = $1;
3447 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003448 CHK("UNCOMMENTED_DEFINITION",
3449 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003450 }
3451 }
3452# check for memory barriers without a comment.
3453 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3454 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003455 CHK("MEMORY_BARRIER",
3456 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003457 }
3458 }
3459# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003460 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003461 CHK("ARCH_DEFINES",
3462 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003463 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003464
Tobias Klauserd4977c72010-05-24 14:33:30 -07003465# Check that the storage class is at the beginning of a declaration
3466 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003467 WARN("STORAGE_CLASS",
3468 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003469 }
3470
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003471# check the location of the inline attribute, that it is between
3472# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003473 if ($line =~ /\b$Type\s+$Inline\b/ ||
3474 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003475 ERROR("INLINE_LOCATION",
3476 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003477 }
3478
Andy Whitcroft8905a672007-11-28 16:21:06 -08003479# Check for __inline__ and __inline, prefer inline
3480 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003481 WARN("INLINE",
3482 "plain inline is preferred over $1\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003483 }
3484
Joe Perches3d130fd2011-01-12 17:00:00 -08003485# Check for __attribute__ packed, prefer __packed
3486 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003487 WARN("PREFER_PACKED",
3488 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003489 }
3490
Joe Perches39b7e282011-07-25 17:13:24 -07003491# Check for __attribute__ aligned, prefer __aligned
3492 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003493 WARN("PREFER_ALIGNED",
3494 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003495 }
3496
Joe Perches5f14d3b2012-01-10 15:09:52 -08003497# Check for __attribute__ format(printf, prefer __printf
3498 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3499 WARN("PREFER_PRINTF",
3500 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3501 }
3502
Joe Perches6061d942012-03-23 15:02:16 -07003503# Check for __attribute__ format(scanf, prefer __scanf
3504 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3505 WARN("PREFER_SCANF",
3506 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3507 }
3508
Joe Perches8f53a9b2010-03-05 13:43:48 -08003509# check for sizeof(&)
3510 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003511 WARN("SIZEOF_ADDRESS",
3512 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003513 }
3514
Joe Perches428e2fd2011-05-24 17:13:39 -07003515# check for line continuations in quoted strings with odd counts of "
3516 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003517 WARN("LINE_CONTINUATIONS",
3518 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003519 }
3520
Andy Whitcroft554e1652012-01-10 15:09:57 -08003521# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003522 if ($^V && $^V ge 5.10.0 &&
3523 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003524 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003525
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003526 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003527 my $ms_val = $7;
3528 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003529
Andy Whitcroft554e1652012-01-10 15:09:57 -08003530 if ($ms_size =~ /^(0x|)0$/i) {
3531 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003532 "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 -08003533 } elsif ($ms_size =~ /^(0x|)1$/i) {
3534 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003535 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3536 }
3537 }
3538
3539# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003540 if ($^V && $^V ge 5.10.0 &&
3541 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003542 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003543 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003544 my $call = $1;
3545 my $cast1 = deparenthesize($2);
3546 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003547 my $cast2 = deparenthesize($7);
3548 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003549 my $cast;
3550
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003551 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003552 $cast = "$cast1 or $cast2";
3553 } elsif ($cast1 ne "") {
3554 $cast = $cast1;
3555 } else {
3556 $cast = $cast2;
3557 }
3558 WARN("MINMAX",
3559 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003560 }
3561 }
3562
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003563# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003564 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003565 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003566 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003567 my $function_name = $1;
3568 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003569
3570 my $s = $stat;
3571 if (defined $cond) {
3572 substr($s, 0, length($cond), '');
3573 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003574 if ($s =~ /^\s*;/ &&
3575 $function_name ne 'uninitialized_var')
3576 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003577 WARN("AVOID_EXTERNS",
3578 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003579 }
3580
3581 if ($paren_space =~ /\n/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003582 WARN("FUNCTION_ARGUMENTS",
3583 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003584 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003585
3586 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3587 $stat =~ /^.\s*extern\s+/)
3588 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003589 WARN("AVOID_EXTERNS",
3590 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003591 }
3592
3593# checks for new __setup's
3594 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3595 my $name = $1;
3596
3597 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003598 CHK("UNDOCUMENTED_SETUP",
3599 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003600 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003601 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003602
3603# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003604 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003605 WARN("UNNECESSARY_CASTS",
3606 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003607 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003608
Joe Perchescaf2a542011-01-12 16:59:56 -08003609# check for multiple semicolons
3610 if ($line =~ /;\s*;\s*$/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003611 WARN("ONE_SEMICOLON",
3612 "Statements terminations use 1 semicolon\n" . $herecurr);
Joe Perchescaf2a542011-01-12 16:59:56 -08003613 }
3614
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003615# check for return codes on error paths
3616 if ($line =~ /\breturn\s+-\d+/) {
Steve Muckle40f0c3c2012-06-13 15:31:18 -07003617 ERROR("NO_ERROR_CODE",
3618 "illegal return value, please use an error code\n" . $herecurr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003619 }
3620
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003621# check for gcc specific __FUNCTION__
3622 if ($line =~ /__FUNCTION__/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003623 WARN("USE_FUNC",
3624 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003625 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003626
Joe Perches2c924882012-03-23 15:02:20 -07003627# check for use of yield()
3628 if ($line =~ /\byield\s*\(\s*\)/) {
3629 WARN("YIELD",
3630 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3631 }
3632
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003633# check for semaphores initialized locked
3634 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003635 WARN("CONSIDER_COMPLETION",
3636 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003637 }
Joe Perches6712d852012-03-23 15:02:20 -07003638
Joe Perches67d0a072011-10-31 17:13:10 -07003639# recommend kstrto* over simple_strto* and strict_strto*
3640 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003641 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07003642 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003643 }
Joe Perches6712d852012-03-23 15:02:20 -07003644
Michael Ellermanf3db6632008-07-23 21:28:57 -07003645# check for __initcall(), use device_initcall() explicitly please
3646 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003647 WARN("USE_DEVICE_INITCALL",
3648 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07003649 }
Joe Perches6712d852012-03-23 15:02:20 -07003650
Emese Revfy79404842010-03-05 13:43:53 -08003651# check for various ops structs, ensure they are const.
3652 my $struct_ops = qr{acpi_dock_ops|
3653 address_space_operations|
3654 backlight_ops|
3655 block_device_operations|
3656 dentry_operations|
3657 dev_pm_ops|
3658 dma_map_ops|
3659 extent_io_ops|
3660 file_lock_operations|
3661 file_operations|
3662 hv_ops|
3663 ide_dma_ops|
3664 intel_dvo_dev_ops|
3665 item_operations|
3666 iwl_ops|
3667 kgdb_arch|
3668 kgdb_io|
3669 kset_uevent_ops|
3670 lock_manager_operations|
3671 microcode_ops|
3672 mtrr_ops|
3673 neigh_ops|
3674 nlmsvc_binding|
3675 pci_raw_ops|
3676 pipe_buf_operations|
3677 platform_hibernation_ops|
3678 platform_suspend_ops|
3679 proto_ops|
3680 rpc_pipe_ops|
3681 seq_operations|
3682 snd_ac97_build_ops|
3683 soc_pcmcia_socket_ops|
3684 stacktrace_ops|
3685 sysfs_ops|
3686 tty_operations|
3687 usb_mon_operations|
3688 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003689 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003690 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003691 WARN("CONST_STRUCT",
3692 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003693 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003694 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003695
3696# use of NR_CPUS is usually wrong
3697# ignore definitions of NR_CPUS and usage to define arrays as likely right
3698 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003699 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3700 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003701 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3702 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3703 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003704 {
Joe Perches000d1cc2011-07-25 17:13:25 -07003705 WARN("NR_CPUS",
3706 "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 -07003707 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003708
3709# check for %L{u,d,i} in strings
3710 my $string;
3711 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3712 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003713 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003714 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003715 WARN("PRINTF_L",
3716 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003717 last;
3718 }
3719 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003720
3721# whine mightly about in_atomic
3722 if ($line =~ /\bin_atomic\s*\(/) {
3723 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003724 ERROR("IN_ATOMIC",
3725 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003726 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003727 WARN("IN_ATOMIC",
3728 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003729 }
3730 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003731
3732# check for lockdep_set_novalidate_class
3733 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3734 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3735 if ($realfile !~ m@^kernel/lockdep@ &&
3736 $realfile !~ m@^include/linux/lockdep@ &&
3737 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003738 ERROR("LOCKDEP",
3739 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003740 }
3741 }
Dave Jones88f88312011-01-12 16:59:59 -08003742
3743 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3744 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003745 WARN("EXPORTED_WORLD_WRITABLE",
3746 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08003747 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003748 }
3749
3750 # If we have no input at all, then there is nothing to report on
3751 # so just keep quiet.
3752 if ($#rawlines == -1) {
3753 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003754 }
3755
Andy Whitcroft8905a672007-11-28 16:21:06 -08003756 # In mailback mode only produce a report in the negative, for
3757 # things that appear to be patches.
3758 if ($mailback && ($clean == 1 || !$is_patch)) {
3759 exit(0);
3760 }
3761
3762 # This is not a patch, and we are are in 'no-patch' mode so
3763 # just keep quiet.
3764 if (!$chk_patch && !$is_patch) {
3765 exit(0);
3766 }
3767
3768 if (!$is_patch) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003769 ERROR("NOT_UNIFIED_DIFF",
3770 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003771 }
3772 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003773 ERROR("MISSING_SIGN_OFF",
3774 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003775 }
3776
Andy Whitcroft8905a672007-11-28 16:21:06 -08003777 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003778 if ($summary && !($clean == 1 && $quiet == 1)) {
3779 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003780 print "total: $cnt_error errors, $cnt_warn warnings, " .
3781 (($check)? "$cnt_chk checks, " : "") .
3782 "$cnt_lines lines checked\n";
3783 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003784 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003785
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003786 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003787
3788 if ($^V lt 5.10.0) {
3789 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3790 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3791 }
3792
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003793 # If there were whitespace errors which cleanpatch can fix
3794 # then suggest that.
3795 if ($rpt_cleaners) {
3796 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3797 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003798 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003799 }
3800 }
3801
Artem Bityutskiy11232682012-03-23 15:02:17 -07003802 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003803 print "NOTE: Ignored message types:";
3804 foreach my $ignore (sort keys %ignore_type) {
3805 print " $ignore";
3806 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07003807 print "\n\n";
Joe Perches000d1cc2011-07-25 17:13:25 -07003808 }
3809
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003810 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003811 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003812 }
3813 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc2011-07-25 17:13:25 -07003814 print << "EOM";
3815$vname has style problems, please review.
3816
3817If any of these errors are false positives, please report
3818them to the maintainer, see CHECKPATCH in MAINTAINERS.
3819EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003820 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003821
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003822 return $clean;
3823}