blob: ab39ceb3828688d993f61c2fc74f7617446b41b2 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9
10my $P = $0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -070011$P =~ s@.*/@@g;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070012
Joe Perches000d1cc12011-07-25 17:13:25 -070013my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070014
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070021my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070022my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080023my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070024my $file = 0;
25my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $summary = 1;
27my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080028my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070029my $show_types = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080031my %debug;
Joe Perches000d1cc12011-07-25 17:13:25 -070032my %ignore_type = ();
33my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070034my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070035my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080036my $max_line_length = 80;
Hannes Eder77f5b102009-09-21 17:04:37 -070037
38sub help {
39 my ($exitcode) = @_;
40
41 print << "EOM";
42Usage: $P [OPTION]... [FILE]...
43Version: $V
44
45Options:
46 -q, --quiet quiet
47 --no-tree run without a kernel tree
48 --no-signoff do not check for 'Signed-off-by' line
49 --patch treat FILE as patchfile (default)
50 --emacs emacs compile window format
51 --terse one line per report
52 -f, --file treat FILE as regular source file
53 --subjective, --strict enable more subjective tests
Joe Perches000d1cc12011-07-25 17:13:25 -070054 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080055 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070056 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070057 --root=PATH PATH to the kernel tree root
58 --no-summary suppress the per-file summary
59 --mailback only produce a report in case of warnings/errors
60 --summary-file include the filename in summary
61 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
62 'values', 'possible', 'type', and 'attr' (default
63 is all off)
64 --test-only=WORD report only warnings/errors containing WORD
65 literally
66 -h, --help, --version display this help and exit
67
68When FILE is - read standard input.
69EOM
70
71 exit($exitcode);
72}
73
Joe Perches000d1cc12011-07-25 17:13:25 -070074my $conf = which_conf($configuration_file);
75if (-f $conf) {
76 my @conf_args;
77 open(my $conffile, '<', "$conf")
78 or warn "$P: Can't find a readable $configuration_file file $!\n";
79
80 while (<$conffile>) {
81 my $line = $_;
82
83 $line =~ s/\s*\n?$//g;
84 $line =~ s/^\s*//g;
85 $line =~ s/\s+/ /g;
86
87 next if ($line =~ m/^\s*#/);
88 next if ($line =~ m/^\s*$/);
89
90 my @words = split(" ", $line);
91 foreach my $word (@words) {
92 last if ($word =~ m/^#/);
93 push (@conf_args, $word);
94 }
95 }
96 close($conffile);
97 unshift(@ARGV, @conf_args) if @conf_args;
98}
99
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700100GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700101 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700102 'tree!' => \$tree,
103 'signoff!' => \$chk_signoff,
104 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700105 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800106 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700107 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700108 'subjective!' => \$check,
109 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700110 'ignore=s' => \@ignore,
111 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800112 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800114 'summary!' => \$summary,
115 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800116 'summary-file!' => \$summary_file,
117
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800118 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700119 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700120 'h|help' => \$help,
121 'version' => \$help
122) or help(1);
123
124help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700125
126my $exit = 0;
127
128if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700129 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700130 exit(1);
131}
132
Joe Perches000d1cc12011-07-25 17:13:25 -0700133@ignore = split(/,/, join(',',@ignore));
134foreach my $word (@ignore) {
135 $word =~ s/\s*\n?$//g;
136 $word =~ s/^\s*//g;
137 $word =~ s/\s+/ /g;
138 $word =~ tr/[a-z]/[A-Z]/;
139
140 next if ($word =~ m/^\s*#/);
141 next if ($word =~ m/^\s*$/);
142
143 $ignore_type{$word}++;
144}
145
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800146my $dbg_values = 0;
147my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700148my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700149my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800150for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800151 ## no critic
152 eval "\${dbg_$key} = '$debug{$key}';";
153 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800154}
155
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700156my $rpt_cleaners = 0;
157
Andy Whitcroft8905a672007-11-28 16:21:06 -0800158if ($terse) {
159 $emacs = 1;
160 $quiet++;
161}
162
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700163if ($tree) {
164 if (defined $root) {
165 if (!top_of_kernel_tree($root)) {
166 die "$P: $root: --root does not point at a valid tree\n";
167 }
168 } else {
169 if (top_of_kernel_tree('.')) {
170 $root = '.';
171 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
172 top_of_kernel_tree($1)) {
173 $root = $1;
174 }
175 }
176
177 if (!defined $root) {
178 print "Must be run from the top-level dir. of a kernel tree\n";
179 exit(2);
180 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700181}
182
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700183my $emitted_corrupt = 0;
184
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700185our $Ident = qr{
186 [A-Za-z_][A-Za-z\d_]*
187 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
188 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700189our $Storage = qr{extern|static|asmlinkage};
190our $Sparse = qr{
191 __user|
192 __kernel|
193 __force|
194 __iomem|
195 __must_check|
196 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800197 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700198 __ref|
199 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700200 }x;
Wolfram Sang52131292010-03-05 13:43:51 -0800201
202# Notes to $Attribute:
203# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700204our $Attribute = qr{
205 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700206 __percpu|
207 __nocast|
208 __safe|
209 __bitwise__|
210 __packed__|
211 __packed2__|
212 __naked|
213 __maybe_unused|
214 __always_unused|
215 __noreturn|
216 __used|
217 __cold|
218 __noclone|
219 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700220 __read_mostly|
221 __kprobes|
Wolfram Sang52131292010-03-05 13:43:51 -0800222 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700223 ____cacheline_aligned|
224 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800225 ____cacheline_internodealigned_in_smp|
226 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700227 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700228our $Modifier;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700229our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700230our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
231our $Lval = qr{$Ident(?:$Member)*};
232
Joe Perches95e2c602013-07-03 15:05:20 -0700233our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
234our $Binary = qr{(?i)0b[01]+$Int_type?};
235our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
236our $Int = qr{[0-9]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800237our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
238our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
239our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800240our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches95e2c602013-07-03 15:05:20 -0700241our $Constant = qr{$Float|$Binary|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800242our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Andy Whitcroft86f9d052009-01-06 14:41:24 -0800243our $Compare = qr{<=|>=|==|!=|<|>};
Joe Perches23f780c2013-07-03 15:05:31 -0700244our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700245our $Operators = qr{
246 <=|>=|==|!=|
247 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700248 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700249 }x;
250
Andy Whitcroft8905a672007-11-28 16:21:06 -0800251our $NonptrType;
252our $Type;
253our $Declare;
254
Joe Perches15662b32011-10-31 17:13:12 -0700255our $NON_ASCII_UTF8 = qr{
256 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700257 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
258 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
259 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
260 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
261 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
262 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
263}x;
264
Joe Perches15662b32011-10-31 17:13:12 -0700265our $UTF8 = qr{
266 [\x09\x0A\x0D\x20-\x7E] # ASCII
267 | $NON_ASCII_UTF8
268}x;
269
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700270our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700271 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700272 atomic_t
273)};
274
Joe Perches691e6692010-03-05 13:43:51 -0800275our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700276 printk(?:_ratelimited|_once|)|
277 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
278 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700279 panic|
280 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800281)};
282
Joe Perches20112472011-07-25 17:13:23 -0700283our $signature_tags = qr{(?xi:
284 Signed-off-by:|
285 Acked-by:|
286 Tested-by:|
287 Reviewed-by:|
288 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700289 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700290 To:|
291 Cc:
292)};
293
Andy Whitcroft8905a672007-11-28 16:21:06 -0800294our @typeList = (
295 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700296 qr{(?:unsigned\s+)?char},
297 qr{(?:unsigned\s+)?short},
298 qr{(?:unsigned\s+)?int},
299 qr{(?:unsigned\s+)?long},
300 qr{(?:unsigned\s+)?long\s+int},
301 qr{(?:unsigned\s+)?long\s+long},
302 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800303 qr{unsigned},
304 qr{float},
305 qr{double},
306 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800307 qr{struct\s+$Ident},
308 qr{union\s+$Ident},
309 qr{enum\s+$Ident},
310 qr{${Ident}_t},
311 qr{${Ident}_handler},
312 qr{${Ident}_handler_fn},
313);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700314our @modifierList = (
315 qr{fastcall},
316);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800317
Wolfram Sang7840a942010-08-09 17:20:57 -0700318our $allowed_asm_includes = qr{(?x:
319 irq|
320 memory
321)};
322# memory.h: ARM has a custom one
323
Andy Whitcroft8905a672007-11-28 16:21:06 -0800324sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700325 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
326 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700327 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800328 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700329 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800330 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800331 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700332 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700333 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800334 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700335 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800336 }x;
337 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700338 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700339 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700340 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800341 }x;
342 $Declare = qr{(?:$Storage\s+)?$Type};
343}
344build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700345
Joe Perches7d2367a2011-07-25 17:13:22 -0700346
347our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700348
349# Using $balanced_parens, $LvalOrFunc, or $FuncArg
350# requires at least perl version v5.10.0
351# Any use must be runtime checked with $^V
352
353our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
354our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800355our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700356
357sub deparenthesize {
358 my ($string) = @_;
359 return "" if (!defined($string));
360 $string =~ s@^\s*\(\s*@@g;
361 $string =~ s@\s*\)\s*$@@g;
362 $string =~ s@\s+@ @g;
363 return $string;
364}
365
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700366$chk_signoff = 0 if ($file);
367
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700368my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800369my @lines = ();
370my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700371for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800372 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700373 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800374 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700375 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800376 } elsif ($filename eq '-') {
377 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700378 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800379 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700380 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700381 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800382 if ($filename eq '-') {
383 $vname = 'Your patch';
384 } else {
385 $vname = $filename;
386 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800387 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700388 chomp;
389 push(@rawlines, $_);
390 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800391 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800392 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700393 $exit = 1;
394 }
395 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800396 @lines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700397}
398
399exit($exit);
400
401sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700402 my ($root) = @_;
403
404 my @tree_check = (
405 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
406 "README", "Documentation", "arch", "include", "drivers",
407 "fs", "init", "ipc", "kernel", "lib", "scripts",
408 );
409
410 foreach my $check (@tree_check) {
411 if (! -e $root . '/' . $check) {
412 return 0;
413 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700414 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700415 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700416}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700417
Joe Perches20112472011-07-25 17:13:23 -0700418sub parse_email {
419 my ($formatted_email) = @_;
420
421 my $name = "";
422 my $address = "";
423 my $comment = "";
424
425 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
426 $name = $1;
427 $address = $2;
428 $comment = $3 if defined $3;
429 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
430 $address = $1;
431 $comment = $2 if defined $2;
432 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
433 $address = $1;
434 $comment = $2 if defined $2;
435 $formatted_email =~ s/$address.*$//;
436 $name = $formatted_email;
437 $name =~ s/^\s+|\s+$//g;
438 $name =~ s/^\"|\"$//g;
439 # If there's a name left after stripping spaces and
440 # leading quotes, and the address doesn't have both
441 # leading and trailing angle brackets, the address
442 # is invalid. ie:
443 # "joe smith joe@smith.com" bad
444 # "joe smith <joe@smith.com" bad
445 if ($name ne "" && $address !~ /^<[^>]+>$/) {
446 $name = "";
447 $address = "";
448 $comment = "";
449 }
450 }
451
452 $name =~ s/^\s+|\s+$//g;
453 $name =~ s/^\"|\"$//g;
454 $address =~ s/^\s+|\s+$//g;
455 $address =~ s/^\<|\>$//g;
456
457 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
458 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
459 $name = "\"$name\"";
460 }
461
462 return ($name, $address, $comment);
463}
464
465sub format_email {
466 my ($name, $address) = @_;
467
468 my $formatted_email;
469
470 $name =~ s/^\s+|\s+$//g;
471 $name =~ s/^\"|\"$//g;
472 $address =~ s/^\s+|\s+$//g;
473
474 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
475 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
476 $name = "\"$name\"";
477 }
478
479 if ("$name" eq "") {
480 $formatted_email = "$address";
481 } else {
482 $formatted_email = "$name <$address>";
483 }
484
485 return $formatted_email;
486}
487
Joe Perches000d1cc12011-07-25 17:13:25 -0700488sub which_conf {
489 my ($conf) = @_;
490
491 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
492 if (-e "$path/$conf") {
493 return "$path/$conf";
494 }
495 }
496
497 return "";
498}
499
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700500sub expand_tabs {
501 my ($str) = @_;
502
503 my $res = '';
504 my $n = 0;
505 for my $c (split(//, $str)) {
506 if ($c eq "\t") {
507 $res .= ' ';
508 $n++;
509 for (; ($n % 8) != 0; $n++) {
510 $res .= ' ';
511 }
512 next;
513 }
514 $res .= $c;
515 $n++;
516 }
517
518 return $res;
519}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700520sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700521 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700522 return $res;
523}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700524
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700525sub line_stats {
526 my ($line) = @_;
527
528 # Drop the diff line leader and expand tabs
529 $line =~ s/^.//;
530 $line = expand_tabs($line);
531
532 # Pick the indent from the front of the line.
533 my ($white) = ($line =~ /^(\s*)/);
534
535 return (length($line), length($white));
536}
537
Andy Whitcroft773647a2008-03-28 14:15:58 -0700538my $sanitise_quote = '';
539
540sub sanitise_line_reset {
541 my ($in_comment) = @_;
542
543 if ($in_comment) {
544 $sanitise_quote = '*/';
545 } else {
546 $sanitise_quote = '';
547 }
548}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700549sub sanitise_line {
550 my ($line) = @_;
551
552 my $res = '';
553 my $l = '';
554
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800555 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700556 my $off = 0;
557 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700558
Andy Whitcroft773647a2008-03-28 14:15:58 -0700559 # Always copy over the diff marker.
560 $res = substr($line, 0, 1);
561
562 for ($off = 1; $off < length($line); $off++) {
563 $c = substr($line, $off, 1);
564
565 # Comments we are wacking completly including the begin
566 # and end, all to $;.
567 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
568 $sanitise_quote = '*/';
569
570 substr($res, $off, 2, "$;$;");
571 $off++;
572 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800573 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700574 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700575 $sanitise_quote = '';
576 substr($res, $off, 2, "$;$;");
577 $off++;
578 next;
579 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700580 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
581 $sanitise_quote = '//';
582
583 substr($res, $off, 2, $sanitise_quote);
584 $off++;
585 next;
586 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700587
588 # A \ in a string means ignore the next character.
589 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
590 $c eq "\\") {
591 substr($res, $off, 2, 'XX');
592 $off++;
593 next;
594 }
595 # Regular quotes.
596 if ($c eq "'" || $c eq '"') {
597 if ($sanitise_quote eq '') {
598 $sanitise_quote = $c;
599
600 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700601 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700602 } elsif ($sanitise_quote eq $c) {
603 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700604 }
605 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700606
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800607 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700608 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
609 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700610 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
611 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700612 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
613 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700614 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700615 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700616 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800617 }
618
Daniel Walker113f04a2009-09-21 17:04:35 -0700619 if ($sanitise_quote eq '//') {
620 $sanitise_quote = '';
621 }
622
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800623 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700624 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800625 my $clean = 'X' x length($1);
626 $res =~ s@\<.*\>@<$clean>@;
627
628 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700629 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800630 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700631 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800632 }
633
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700634 return $res;
635}
636
Joe Perchesa6962d72013-04-29 16:18:13 -0700637sub get_quoted_string {
638 my ($line, $rawline) = @_;
639
640 return "" if ($line !~ m/(\"[X]+\")/g);
641 return substr($rawline, $-[0], $+[0] - $-[0]);
642}
643
Andy Whitcroft8905a672007-11-28 16:21:06 -0800644sub ctx_statement_block {
645 my ($linenr, $remain, $off) = @_;
646 my $line = $linenr - 1;
647 my $blk = '';
648 my $soff = $off;
649 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700650 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800651
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800652 my $loff = 0;
653
Andy Whitcroft8905a672007-11-28 16:21:06 -0800654 my $type = '';
655 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800656 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800657 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800658 my $c;
659 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800660
661 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800662 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800663 @stack = (['', 0]) if ($#stack == -1);
664
Andy Whitcroft773647a2008-03-28 14:15:58 -0700665 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800666 # If we are about to drop off the end, pull in more
667 # context.
668 if ($off >= $len) {
669 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700670 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800671 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800672 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800673 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800674 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800675 $len = length($blk);
676 $line++;
677 last;
678 }
679 # Bail if there is no further context.
680 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800681 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800682 last;
683 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800684 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
685 $level++;
686 $type = '#';
687 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800688 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800689 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800690 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800691 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800692
Andy Whitcroft773647a2008-03-28 14:15:58 -0700693 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800694
695 # Handle nested #if/#else.
696 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
697 push(@stack, [ $type, $level ]);
698 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
699 ($type, $level) = @{$stack[$#stack - 1]};
700 } elsif ($remainder =~ /^#\s*endif\b/) {
701 ($type, $level) = @{pop(@stack)};
702 }
703
Andy Whitcroft8905a672007-11-28 16:21:06 -0800704 # Statement ends at the ';' or a close '}' at the
705 # outermost level.
706 if ($level == 0 && $c eq ';') {
707 last;
708 }
709
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800710 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700711 if ($level == 0 && $coff_set == 0 &&
712 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
713 $remainder =~ /^(else)(?:\s|{)/ &&
714 $remainder !~ /^else\s+if\b/) {
715 $coff = $off + length($1) - 1;
716 $coff_set = 1;
717 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
718 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800719 }
720
Andy Whitcroft8905a672007-11-28 16:21:06 -0800721 if (($type eq '' || $type eq '(') && $c eq '(') {
722 $level++;
723 $type = '(';
724 }
725 if ($type eq '(' && $c eq ')') {
726 $level--;
727 $type = ($level != 0)? '(' : '';
728
729 if ($level == 0 && $coff < $soff) {
730 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700731 $coff_set = 1;
732 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800733 }
734 }
735 if (($type eq '' || $type eq '{') && $c eq '{') {
736 $level++;
737 $type = '{';
738 }
739 if ($type eq '{' && $c eq '}') {
740 $level--;
741 $type = ($level != 0)? '{' : '';
742
743 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700744 if (substr($blk, $off + 1, 1) eq ';') {
745 $off++;
746 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800747 last;
748 }
749 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800750 # Preprocessor commands end at the newline unless escaped.
751 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
752 $level--;
753 $type = '';
754 $off++;
755 last;
756 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800757 $off++;
758 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700759 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800760 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700761 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800762 $line++;
763 $remain--;
764 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800765
766 my $statement = substr($blk, $soff, $off - $soff + 1);
767 my $condition = substr($blk, $soff, $coff - $soff + 1);
768
769 #warn "STATEMENT<$statement>\n";
770 #warn "CONDITION<$condition>\n";
771
Andy Whitcroft773647a2008-03-28 14:15:58 -0700772 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800773
774 return ($statement, $condition,
775 $line, $remain + 1, $off - $loff + 1, $level);
776}
777
Andy Whitcroftcf655042008-03-04 14:28:20 -0800778sub statement_lines {
779 my ($stmt) = @_;
780
781 # Strip the diff line prefixes and rip blank lines at start and end.
782 $stmt =~ s/(^|\n)./$1/g;
783 $stmt =~ s/^\s*//;
784 $stmt =~ s/\s*$//;
785
786 my @stmt_lines = ($stmt =~ /\n/g);
787
788 return $#stmt_lines + 2;
789}
790
791sub statement_rawlines {
792 my ($stmt) = @_;
793
794 my @stmt_lines = ($stmt =~ /\n/g);
795
796 return $#stmt_lines + 2;
797}
798
799sub statement_block_size {
800 my ($stmt) = @_;
801
802 $stmt =~ s/(^|\n)./$1/g;
803 $stmt =~ s/^\s*{//;
804 $stmt =~ s/}\s*$//;
805 $stmt =~ s/^\s*//;
806 $stmt =~ s/\s*$//;
807
808 my @stmt_lines = ($stmt =~ /\n/g);
809 my @stmt_statements = ($stmt =~ /;/g);
810
811 my $stmt_lines = $#stmt_lines + 2;
812 my $stmt_statements = $#stmt_statements + 1;
813
814 if ($stmt_lines > $stmt_statements) {
815 return $stmt_lines;
816 } else {
817 return $stmt_statements;
818 }
819}
820
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800821sub ctx_statement_full {
822 my ($linenr, $remain, $off) = @_;
823 my ($statement, $condition, $level);
824
825 my (@chunks);
826
Andy Whitcroftcf655042008-03-04 14:28:20 -0800827 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800828 ($statement, $condition, $linenr, $remain, $off, $level) =
829 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700830 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800831 push(@chunks, [ $condition, $statement ]);
832 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
833 return ($level, $linenr, @chunks);
834 }
835
836 # Pull in the following conditional/block pairs and see if they
837 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800838 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800839 ($statement, $condition, $linenr, $remain, $off, $level) =
840 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800841 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700842 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800843 #print "C: push\n";
844 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800845 }
846
847 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800848}
849
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700850sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700851 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700852 my $line;
853 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700854 my $blk = '';
855 my @o;
856 my @c;
857 my @res = ();
858
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700859 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800860 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700861 for ($line = $start; $remain > 0; $line++) {
862 next if ($rawlines[$line] =~ /^-/);
863 $remain--;
864
865 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800866
867 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700868 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800869 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700870 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800871 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700872 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800873 $level = pop(@stack);
874 }
875
Andy Whitcroft01464f32010-10-26 14:23:19 -0700876 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700877 ##print "C<$c>L<$level><$open$close>O<$off>\n";
878 if ($off > 0) {
879 $off--;
880 next;
881 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700882
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700883 if ($c eq $close && $level > 0) {
884 $level--;
885 last if ($level == 0);
886 } elsif ($c eq $open) {
887 $level++;
888 }
889 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700890
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700891 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700892 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700893 }
894
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700895 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700896 }
897
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700898 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700899}
900sub ctx_block_outer {
901 my ($linenr, $remain) = @_;
902
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700903 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
904 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700905}
906sub ctx_block {
907 my ($linenr, $remain) = @_;
908
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700909 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
910 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700911}
912sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700913 my ($linenr, $remain, $off) = @_;
914
915 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
916 return @r;
917}
918sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700919 my ($linenr, $remain) = @_;
920
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700921 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700922}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700923sub ctx_statement_level {
924 my ($linenr, $remain, $off) = @_;
925
926 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
927}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700928
929sub ctx_locate_comment {
930 my ($first_line, $end_line) = @_;
931
932 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700933 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700934 return $current_comment if (defined $current_comment);
935
936 # Look through the context and try and figure out if there is a
937 # comment.
938 my $in_comment = 0;
939 $current_comment = '';
940 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700941 my $line = $rawlines[$linenr - 1];
942 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700943 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
944 $in_comment = 1;
945 }
946 if ($line =~ m@/\*@) {
947 $in_comment = 1;
948 }
949 if (!$in_comment && $current_comment ne '') {
950 $current_comment = '';
951 }
952 $current_comment .= $line . "\n" if ($in_comment);
953 if ($line =~ m@\*/@) {
954 $in_comment = 0;
955 }
956 }
957
958 chomp($current_comment);
959 return($current_comment);
960}
961sub ctx_has_comment {
962 my ($first_line, $end_line) = @_;
963 my $cmt = ctx_locate_comment($first_line, $end_line);
964
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700965 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700966 ##print "CMMT: $cmt\n";
967
968 return ($cmt ne '');
969}
970
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700971sub raw_line {
972 my ($linenr, $cnt) = @_;
973
974 my $offset = $linenr - 1;
975 $cnt++;
976
977 my $line;
978 while ($cnt) {
979 $line = $rawlines[$offset++];
980 next if (defined($line) && $line =~ /^-/);
981 $cnt--;
982 }
983
984 return $line;
985}
986
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700987sub cat_vet {
988 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700989 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700990
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700991 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700992 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
993 $res .= $1;
994 if ($2 ne '') {
995 $coded = sprintf("^%c", unpack('C', $2) + 64);
996 $res .= $coded;
997 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700998 }
999 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001000
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001001 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001002}
1003
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001004my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001005my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001006my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001007my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001008
1009sub annotate_reset {
1010 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001011 $av_pending = '_';
1012 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001013 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001014}
1015
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001016sub annotate_values {
1017 my ($stream, $type) = @_;
1018
1019 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001020 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001021 my $cur = $stream;
1022
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001023 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001025 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001026 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001027 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001028 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001029 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001030 print "WS($1)\n" if ($dbg_values > 1);
1031 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001032 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001033 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001034 }
1035
Florian Micklerc023e4732011-01-12 16:59:58 -08001036 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001037 print "CAST($1)\n" if ($dbg_values > 1);
1038 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001039 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001040
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001041 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001042 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001043 $type = 'T';
1044
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001045 } elsif ($cur =~ /^($Modifier)\s*/) {
1046 print "MODIFIER($1)\n" if ($dbg_values > 1);
1047 $type = 'T';
1048
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001049 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001050 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001051 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001052 push(@av_paren_type, $type);
1053 if ($2 ne '') {
1054 $av_pending = 'N';
1055 }
1056 $type = 'E';
1057
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001058 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001059 print "UNDEF($1)\n" if ($dbg_values > 1);
1060 $av_preprocessor = 1;
1061 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001062
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001063 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001064 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001065 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001066
1067 push(@av_paren_type, $type);
1068 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001069 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001070
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001071 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001072 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1073 $av_preprocessor = 1;
1074
1075 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1076
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*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001080 print "PRE_END($1)\n" if ($dbg_values > 1);
1081
1082 $av_preprocessor = 1;
1083
1084 # Assume all arms of the conditional end as this
1085 # one does, and continue as if the #endif was not here.
1086 pop(@av_paren_type);
1087 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001088 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001089
1090 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001091 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001092
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001093 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1094 print "ATTR($1)\n" if ($dbg_values > 1);
1095 $av_pending = $type;
1096 $type = 'N';
1097
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001098 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001099 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001100 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001101 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001102 }
1103 $type = 'N';
1104
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001105 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001106 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001107 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001108 $type = 'N';
1109
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001110 } elsif ($cur =~/^(case)/o) {
1111 print "CASE($1)\n" if ($dbg_values > 1);
1112 $av_pend_colon = 'C';
1113 $type = 'N';
1114
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001115 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001116 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001117 $type = 'N';
1118
1119 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001120 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001121 push(@av_paren_type, $av_pending);
1122 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001123 $type = 'N';
1124
1125 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001126 my $new_type = pop(@av_paren_type);
1127 if ($new_type ne '_') {
1128 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001129 print "PAREN('$1') -> $type\n"
1130 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001131 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001132 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001133 }
1134
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001135 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001136 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001137 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001138 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001139
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001140 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1141 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001142 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001143 } elsif ($type eq 'E') {
1144 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001145 }
1146 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1147 $type = 'V';
1148
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001149 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001150 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001151 $type = 'V';
1152
1153 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001154 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001155 $type = 'N';
1156
Andy Whitcroftcf655042008-03-04 14:28:20 -08001157 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001158 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001159 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001160 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001161
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001162 } elsif ($cur =~/^(,)/) {
1163 print "COMMA($1)\n" if ($dbg_values > 1);
1164 $type = 'C';
1165
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001166 } elsif ($cur =~ /^(\?)/o) {
1167 print "QUESTION($1)\n" if ($dbg_values > 1);
1168 $type = 'N';
1169
1170 } elsif ($cur =~ /^(:)/o) {
1171 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1172
1173 substr($var, length($res), 1, $av_pend_colon);
1174 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1175 $type = 'E';
1176 } else {
1177 $type = 'N';
1178 }
1179 $av_pend_colon = 'O';
1180
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001181 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001182 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001183 $type = 'N';
1184
Andy Whitcroft0d413862008-10-15 22:02:16 -07001185 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001186 my $variant;
1187
1188 print "OPV($1)\n" if ($dbg_values > 1);
1189 if ($type eq 'V') {
1190 $variant = 'B';
1191 } else {
1192 $variant = 'U';
1193 }
1194
1195 substr($var, length($res), 1, $variant);
1196 $type = 'N';
1197
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001198 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001199 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001200 if ($1 ne '++' && $1 ne '--') {
1201 $type = 'N';
1202 }
1203
1204 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001205 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001206 }
1207 if (defined $1) {
1208 $cur = substr($cur, length($1));
1209 $res .= $type x length($1);
1210 }
1211 }
1212
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001213 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001214}
1215
Andy Whitcroft8905a672007-11-28 16:21:06 -08001216sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001217 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001218 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001219 ^(?:
1220 $Modifier|
1221 $Storage|
1222 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001223 DEFINE_\S+
1224 )$|
1225 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001226 goto|
1227 return|
1228 case|
1229 else|
1230 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001231 do|
1232 \#|
1233 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001234 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001235 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001236 )}x;
1237 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1238 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001239 # Check for modifiers.
1240 $possible =~ s/\s*$Storage\s*//g;
1241 $possible =~ s/\s*$Sparse\s*//g;
1242 if ($possible =~ /^\s*$/) {
1243
1244 } elsif ($possible =~ /\s/) {
1245 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001246 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001247 if ($modifier !~ $notPermitted) {
1248 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1249 push(@modifierList, $modifier);
1250 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001251 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001252
1253 } else {
1254 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1255 push(@typeList, $possible);
1256 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001257 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001258 } else {
1259 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001260 }
1261}
1262
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001263my $prefix = '';
1264
Joe Perches000d1cc12011-07-25 17:13:25 -07001265sub show_type {
1266 return !defined $ignore_type{$_[0]};
1267}
1268
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001269sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001270 if (!show_type($_[1]) ||
1271 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001272 return 0;
1273 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001274 my $line;
1275 if ($show_types) {
1276 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1277 } else {
1278 $line = "$prefix$_[0]: $_[2]\n";
1279 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001280 $line = (split('\n', $line))[0] . "\n" if ($terse);
1281
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001282 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001283
1284 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001285}
1286sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001287 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001288}
Joe Perches000d1cc12011-07-25 17:13:25 -07001289
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001290sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001291 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001292 our $clean = 0;
1293 our $cnt_error++;
1294 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001295}
1296sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001297 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001298 our $clean = 0;
1299 our $cnt_warn++;
1300 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001301}
1302sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001303 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001304 our $clean = 0;
1305 our $cnt_chk++;
1306 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001307}
1308
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001309sub check_absolute_file {
1310 my ($absolute, $herecurr) = @_;
1311 my $file = $absolute;
1312
1313 ##print "absolute<$absolute>\n";
1314
1315 # See if any suffix of this path is a path within the tree.
1316 while ($file =~ s@^[^/]*/@@) {
1317 if (-f "$root/$file") {
1318 ##print "file<$file>\n";
1319 last;
1320 }
1321 }
1322 if (! -f _) {
1323 return 0;
1324 }
1325
1326 # It is, so see if the prefix is acceptable.
1327 my $prefix = $absolute;
1328 substr($prefix, -length($file)) = '';
1329
1330 ##print "prefix<$prefix>\n";
1331 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001332 WARN("USE_RELATIVE_PATH",
1333 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001334 }
1335}
1336
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001337sub pos_last_openparen {
1338 my ($line) = @_;
1339
1340 my $pos = 0;
1341
1342 my $opens = $line =~ tr/\(/\(/;
1343 my $closes = $line =~ tr/\)/\)/;
1344
1345 my $last_openparen = 0;
1346
1347 if (($opens == 0) || ($closes >= $opens)) {
1348 return -1;
1349 }
1350
1351 my $len = length($line);
1352
1353 for ($pos = 0; $pos < $len; $pos++) {
1354 my $string = substr($line, $pos);
1355 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1356 $pos += length($1) - 1;
1357 } elsif (substr($line, $pos, 1) eq '(') {
1358 $last_openparen = $pos;
1359 } elsif (index($string, '(') == -1) {
1360 last;
1361 }
1362 }
1363
1364 return $last_openparen + 1;
1365}
1366
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001367sub process {
1368 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001369
1370 my $linenr=0;
1371 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001372 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001373 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001374 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001375
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001376 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001377 my $indent;
1378 my $previndent=0;
1379 my $stashindent=0;
1380
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001381 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001382 my $signoff = 0;
1383 my $is_patch = 0;
1384
Joe Perches15662b32011-10-31 17:13:12 -07001385 my $in_header_lines = 1;
1386 my $in_commit_log = 0; #Scanning lines before patch
1387
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001388 my $non_utf8_charset = 0;
1389
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001390 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001391 our $cnt_lines = 0;
1392 our $cnt_error = 0;
1393 our $cnt_warn = 0;
1394 our $cnt_chk = 0;
1395
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001396 # Trace the real file/line as we go.
1397 my $realfile = '';
1398 my $realline = 0;
1399 my $realcnt = 0;
1400 my $here = '';
1401 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001402 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001403 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001404 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001405
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001406 my $prev_values = 'E';
1407
1408 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001409 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001410 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001411 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001412 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001413
Joe Perches323c1262012-12-17 16:02:07 -08001414 my %camelcase = ();
1415
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001416 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001417 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001418 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001419 my @setup_docs = ();
1420 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001421
1422 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001423 my $line;
1424 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001425 $linenr++;
1426 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001427
Andy Whitcroft773647a2008-03-28 14:15:58 -07001428 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001429 $setup_docs = 0;
1430 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1431 $setup_docs = 1;
1432 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001433 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001434 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001435 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1436 $realline=$1-1;
1437 if (defined $2) {
1438 $realcnt=$3+1;
1439 } else {
1440 $realcnt=1+1;
1441 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001442 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001443
1444 # Guestimate if this is a continuing comment. Run
1445 # the context looking for a comment "edge". If this
1446 # edge is a close comment then we must be in a comment
1447 # at context start.
1448 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001449 my $cnt = $realcnt;
1450 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1451 next if (defined $rawlines[$ln - 1] &&
1452 $rawlines[$ln - 1] =~ /^-/);
1453 $cnt--;
1454 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001455 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001456 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1457 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1458 ($edge) = $1;
1459 last;
1460 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001461 }
1462 if (defined $edge && $edge eq '*/') {
1463 $in_comment = 1;
1464 }
1465
1466 # Guestimate if this is a continuing comment. If this
1467 # is the start of a diff block and this line starts
1468 # ' *' then it is very likely a comment.
1469 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001470 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001471 {
1472 $in_comment = 1;
1473 }
1474
1475 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1476 sanitise_line_reset($in_comment);
1477
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001478 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001479 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001480 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001481 $line = sanitise_line($rawline);
1482 }
1483 push(@lines, $line);
1484
1485 if ($realcnt > 1) {
1486 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1487 } else {
1488 $realcnt = 0;
1489 }
1490
1491 #print "==>$rawline\n";
1492 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001493
1494 if ($setup_docs && $line =~ /^\+/) {
1495 push(@setup_docs, $line);
1496 }
1497 }
1498
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001499 $prefix = '';
1500
Andy Whitcroft773647a2008-03-28 14:15:58 -07001501 $realcnt = 0;
1502 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001503 foreach my $line (@lines) {
1504 $linenr++;
1505
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001506 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001507
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001508#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001509 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001510 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001511 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001512 $realline=$1-1;
1513 if (defined $2) {
1514 $realcnt=$3+1;
1515 } else {
1516 $realcnt=1+1;
1517 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001518 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001519 $prev_values = 'E';
1520
Andy Whitcroft773647a2008-03-28 14:15:58 -07001521 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001522 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001523 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001524 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001525 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001526
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001527# track the line number as we move through the hunk, note that
1528# new versions of GNU diff omit the leading space on completely
1529# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001530 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001531 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001532 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001533
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001534 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001535 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001536
1537 # Track the previous line.
1538 ($prevline, $stashline) = ($stashline, $line);
1539 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001540 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1541
Andy Whitcroft773647a2008-03-28 14:15:58 -07001542 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001543
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001544 } elsif ($realcnt == 1) {
1545 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001546 }
1547
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001548 my $hunk_line = ($realcnt != 0);
1549
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001550#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001551 $prefix = "$filename:$realline: " if ($emacs && $file);
1552 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1553
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001554 $here = "#$linenr: " if (!$file);
1555 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001556
1557 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001558 if ($line =~ /^diff --git.*?(\S+)$/) {
1559 $realfile = $1;
1560 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001561 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001562 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001563 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001564 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001565 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001566
1567 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001568 if (!$file && $tree && $p1_prefix ne '' &&
1569 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001570 WARN("PATCH_PREFIX",
1571 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001572 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001573
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001574 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001575 ERROR("MODIFIED_INCLUDE_ASM",
1576 "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 -07001577 }
1578 next;
1579 }
1580
Randy Dunlap389834b2007-06-08 13:47:03 -07001581 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001582
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001583 my $hereline = "$here\n$rawline\n";
1584 my $herecurr = "$here\n$rawline\n";
1585 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001586
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001587 $cnt_lines++ if ($realcnt != 0);
1588
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001589# Check for incorrect file permissions
1590 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1591 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001592 if ($realfile !~ m@scripts/@ &&
1593 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001594 ERROR("EXECUTE_PERMISSIONS",
1595 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001596 }
1597 }
1598
Joe Perches20112472011-07-25 17:13:23 -07001599# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001600 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001601 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001602 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001603 }
1604
1605# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001606 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001607 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001608 my $space_before = $1;
1609 my $sign_off = $2;
1610 my $space_after = $3;
1611 my $email = $4;
1612 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1613
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001614 if ($sign_off !~ /$signature_tags/) {
1615 WARN("BAD_SIGN_OFF",
1616 "Non-standard signature: $sign_off\n" . $herecurr);
1617 }
Joe Perches20112472011-07-25 17:13:23 -07001618 if (defined $space_before && $space_before ne "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001619 WARN("BAD_SIGN_OFF",
1620 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001621 }
Joe Perches20112472011-07-25 17:13:23 -07001622 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001623 WARN("BAD_SIGN_OFF",
1624 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001625 }
1626 if (!defined $space_after || $space_after ne " ") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001627 WARN("BAD_SIGN_OFF",
1628 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001629 }
1630
1631 my ($email_name, $email_address, $comment) = parse_email($email);
1632 my $suggested_email = format_email(($email_name, $email_address));
1633 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001634 ERROR("BAD_SIGN_OFF",
1635 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001636 } else {
1637 my $dequoted = $suggested_email;
1638 $dequoted =~ s/^"//;
1639 $dequoted =~ s/" </ </;
1640 # Don't force email to have quotes
1641 # Allow just an angle bracketed address
1642 if ("$dequoted$comment" ne $email &&
1643 "<$email_address>$comment" ne $email &&
1644 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001645 WARN("BAD_SIGN_OFF",
1646 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001647 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001648 }
1649 }
1650
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001651# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001652 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001653 ERROR("CORRUPTED_PATCH",
1654 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001655 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001656 }
1657
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001658# Check for absolute kernel paths.
1659 if ($tree) {
1660 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1661 my $file = $1;
1662
1663 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1664 check_absolute_file($1, $herecurr)) {
1665 #
1666 } else {
1667 check_absolute_file($file, $herecurr);
1668 }
1669 }
1670 }
1671
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001672# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1673 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001674 $rawline !~ m/^$UTF8*$/) {
1675 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1676
1677 my $blank = copy_spacing($rawline);
1678 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1679 my $hereptr = "$hereline$ptr\n";
1680
Joe Perches34d99212011-07-25 17:13:26 -07001681 CHK("INVALID_UTF8",
1682 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001683 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001684
Joe Perches15662b32011-10-31 17:13:12 -07001685# Check if it's the start of a commit log
1686# (not a header line and we haven't seen the patch filename)
1687 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001688 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001689 $in_header_lines = 0;
1690 $in_commit_log = 1;
1691 }
1692
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001693# Check if there is UTF-8 in a commit log when a mail header has explicitly
1694# declined it, i.e defined some charset where it is missing.
1695 if ($in_header_lines &&
1696 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1697 $1 !~ /utf-8/i) {
1698 $non_utf8_charset = 1;
1699 }
1700
1701 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001702 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001703 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001704 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1705 }
1706
Andy Whitcroft306708542008-10-15 22:02:28 -07001707# ignore non-hunk lines and lines being removed
1708 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001709
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001710#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001711 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001712 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001713 ERROR("DOS_LINE_ENDINGS",
1714 "DOS line endings\n" . $herevet);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001715
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001716 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1717 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001718 ERROR("TRAILING_WHITESPACE",
1719 "trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001720 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001721 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001722
Andi Kleen33549572010-05-24 14:33:29 -07001723# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001724# Only applies when adding the entry originally, after that we do not have
1725# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001726 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001727 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001728 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001729 my $cnt = $realcnt;
1730 my $ln = $linenr + 1;
1731 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001732 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001733 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001734 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001735 $f = $lines[$ln - 1];
1736 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1737 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001738
1739 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001740
1741 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1742 $is_start = 1;
1743 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1744 $length = -1;
1745 }
1746
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001747 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001748 $f =~ s/#.*//;
1749 $f =~ s/^\s+//;
1750 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001751 if ($f =~ /^\s*config\s/) {
1752 $is_end = 1;
1753 last;
1754 }
Andi Kleen33549572010-05-24 14:33:29 -07001755 $length++;
1756 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001757 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001758 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1759 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001760 }
1761
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001762# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1763 if ($realfile =~ /Kconfig/ &&
1764 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1765 WARN("CONFIG_EXPERIMENTAL",
1766 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1767 }
1768
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001769 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1770 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1771 my $flag = $1;
1772 my $replacement = {
1773 'EXTRA_AFLAGS' => 'asflags-y',
1774 'EXTRA_CFLAGS' => 'ccflags-y',
1775 'EXTRA_CPPFLAGS' => 'cppflags-y',
1776 'EXTRA_LDFLAGS' => 'ldflags-y',
1777 };
1778
1779 WARN("DEPRECATED_VARIABLE",
1780 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1781 }
1782
Andy Whitcroft5368df202008-10-15 22:02:27 -07001783# check we are in a valid source file if not then ignore this hunk
1784 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1785
Joe Perches6cd7f382012-12-17 16:01:54 -08001786#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001787 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001788 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001789 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001790 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08001791 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001792 {
Joe Perches000d1cc12011-07-25 17:13:25 -07001793 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08001794 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001795 }
1796
Josh Triplettca56dc02012-03-23 15:02:21 -07001797# Check for user-visible strings broken across lines, which breaks the ability
1798# to grep for the string. Limited to strings used as parameters (those
1799# following an open parenthesis), which almost completely eliminates false
1800# positives, as well as warning only once per parameter rather than once per
1801# line of the string. Make an exception when the previous string ends in a
1802# newline (multiple lines in one string constant) or \n\t (common in inline
1803# assembly to indent the instruction on the following line).
1804 if ($line =~ /^\+\s*"/ &&
1805 $prevline =~ /"\s*$/ &&
1806 $prevline =~ /\(/ &&
1807 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1808 WARN("SPLIT_STRING",
1809 "quoted string split across lines\n" . $hereprev);
1810 }
1811
Joe Perches5e79d962010-03-05 13:43:55 -08001812# check for spaces before a quoted newline
1813 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001814 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1815 "unnecessary whitespace before a quoted newline\n" . $herecurr);
Joe Perches5e79d962010-03-05 13:43:55 -08001816 }
1817
Andy Whitcroft8905a672007-11-28 16:21:06 -08001818# check for adding lines without a newline.
1819 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001820 WARN("MISSING_EOF_NEWLINE",
1821 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001822 }
1823
Mike Frysinger42e41c52009-09-21 17:04:40 -07001824# Blackfin: use hi/lo macros
1825 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1826 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1827 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001828 ERROR("LO_MACRO",
1829 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001830 }
1831 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1832 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001833 ERROR("HI_MACRO",
1834 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001835 }
1836 }
1837
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001838# check we are in a valid source file C or perl if not then ignore this hunk
1839 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001840
1841# at the beginning of a line any tabs must come first and anything
1842# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001843 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1844 $rawline =~ /^\+\s* \s*/) {
1845 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001846 ERROR("CODE_INDENT",
1847 "code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001848 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001849 }
1850
Alberto Panizzo08e44362010-03-05 13:43:54 -08001851# check for space before tabs.
1852 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1853 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001854 WARN("SPACE_BEFORE_TAB",
1855 "please, no space before tabs\n" . $herevet);
Alberto Panizzo08e44362010-03-05 13:43:54 -08001856 }
1857
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001858# check for && or || at the start of a line
1859 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1860 CHK("LOGICAL_CONTINUATIONS",
1861 "Logical continuations should be on the previous line\n" . $hereprev);
1862 }
1863
1864# check multi-line statement indentation matches previous line
1865 if ($^V && $^V ge 5.10.0 &&
1866 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1867 $prevline =~ /^\+(\t*)(.*)$/;
1868 my $oldindent = $1;
1869 my $rest = $2;
1870
1871 my $pos = pos_last_openparen($rest);
1872 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07001873 $line =~ /^(\+| )([ \t]*)/;
1874 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001875
1876 my $goodtabindent = $oldindent .
1877 "\t" x ($pos / 8) .
1878 " " x ($pos % 8);
1879 my $goodspaceindent = $oldindent . " " x $pos;
1880
1881 if ($newindent ne $goodtabindent &&
1882 $newindent ne $goodspaceindent) {
1883 CHK("PARENTHESIS_ALIGNMENT",
1884 "Alignment should match open parenthesis\n" . $hereprev);
1885 }
1886 }
1887 }
1888
Joe Perches23f780c2013-07-03 15:05:31 -07001889 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
Joe Perchesaad4f612012-03-23 15:02:19 -07001890 CHK("SPACING",
1891 "No space is necessary after a cast\n" . $hereprev);
1892 }
1893
Joe Perches05880602012-10-04 17:13:35 -07001894 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07001895 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1896 $rawline =~ /^\+[ \t]*\*/) {
Joe Perches05880602012-10-04 17:13:35 -07001897 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1898 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1899 }
1900
1901 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07001902 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
1903 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1904 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1905 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1906 "networking block comments start with * on subsequent lines\n" . $hereprev);
1907 }
1908
1909 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08001910 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1911 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1912 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1913 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07001914 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1915 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
1916 }
1917
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001918# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001919# Exceptions:
1920# 1) within comments
1921# 2) indented preprocessor commands
1922# 3) hanging labels
1923 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001924 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001925 WARN("LEADING_SPACE",
1926 "please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001927 }
1928
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001929# check we are in a valid C source file if not then ignore this hunk
1930 next if ($realfile !~ /\.(h|c)$/);
1931
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001932# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
1933 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
1934 WARN("CONFIG_EXPERIMENTAL",
1935 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1936 }
1937
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001938# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001939 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001940 WARN("CVS_KEYWORD",
1941 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001942 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001943
Mike Frysinger42e41c52009-09-21 17:04:40 -07001944# Blackfin: don't use __builtin_bfin_[cs]sync
1945 if ($line =~ /__builtin_bfin_csync/) {
1946 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001947 ERROR("CSYNC",
1948 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001949 }
1950 if ($line =~ /__builtin_bfin_ssync/) {
1951 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001952 ERROR("SSYNC",
1953 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001954 }
1955
Joe Perches56e77d72013-02-21 16:44:14 -08001956# check for old HOTPLUG __dev<foo> section markings
1957 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
1958 WARN("HOTPLUG_SECTION",
1959 "Using $1 is unnecessary\n" . $herecurr);
1960 }
1961
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001962# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001963 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1964 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001965#print "LINE<$line>\n";
1966 if ($linenr >= $suppress_statement &&
1967 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001968 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001969 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001970 $stat =~ s/\n./\n /g;
1971 $cond =~ s/\n./\n /g;
1972
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001973#print "linenr<$linenr> <$stat>\n";
1974 # If this statement has no statement boundaries within
1975 # it there is no point in retrying a statement scan
1976 # until we hit end of it.
1977 my $frag = $stat; $frag =~ s/;+\s*$//;
1978 if ($frag !~ /(?:{|;)/) {
1979#print "skip<$line_nr_next>\n";
1980 $suppress_statement = $line_nr_next;
1981 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001982
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001983 # Find the real next line.
1984 $realline_next = $line_nr_next;
1985 if (defined $realline_next &&
1986 (!defined $lines[$realline_next - 1] ||
1987 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1988 $realline_next++;
1989 }
1990
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001991 my $s = $stat;
1992 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001993
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001994 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001995 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001996
1997 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001998 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001999
Andy Whitcroft463f2862009-09-21 17:04:34 -07002000 } elsif ($s =~ /^.\s*else\b/s) {
2001
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002002 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002003 } 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 -07002004 my $type = $1;
2005 $type =~ s/\s+/ /g;
2006 possible($type, "A:" . $s);
2007
Andy Whitcroft8905a672007-11-28 16:21:06 -08002008 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002009 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002010 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002011 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002012
2013 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002014 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002015 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002016 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002017
2018 # Check for any sort of function declaration.
2019 # int foo(something bar, other baz);
2020 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002021 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 -08002022 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002023
Andy Whitcroftcf655042008-03-04 14:28:20 -08002024 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002025 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002026 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002027
Andy Whitcroft8905a672007-11-28 16:21:06 -08002028 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002029 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002030
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002031 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002032 }
2033 }
2034 }
2035
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002036 }
2037
Andy Whitcroft653d4872007-06-23 17:16:34 -07002038#
2039# Checks which may be anchored in the context.
2040#
2041
2042# Check for switch () and associated case and default
2043# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002044 if ($line=~/\bswitch\s*\(.*\)/) {
2045 my $err = '';
2046 my $sep = '';
2047 my @ctx = ctx_block_outer($linenr, $realcnt);
2048 shift(@ctx);
2049 for my $ctx (@ctx) {
2050 my ($clen, $cindent) = line_stats($ctx);
2051 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2052 $indent != $cindent) {
2053 $err .= "$sep$ctx\n";
2054 $sep = '';
2055 } else {
2056 $sep = "[...]\n";
2057 }
2058 }
2059 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002060 ERROR("SWITCH_CASE_INDENT_LEVEL",
2061 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002062 }
2063 }
2064
2065# if/while/etc brace do not go on next line, unless defining a do while loop,
2066# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002067 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002068 my $pre_ctx = "$1$2";
2069
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002070 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002071
2072 if ($line =~ /^\+\t{6,}/) {
2073 WARN("DEEP_INDENTATION",
2074 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2075 }
2076
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002077 my $ctx_cnt = $realcnt - $#ctx - 1;
2078 my $ctx = join("\n", @ctx);
2079
Andy Whitcroft548596d2008-07-23 21:29:01 -07002080 my $ctx_ln = $linenr;
2081 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002082
Andy Whitcroft548596d2008-07-23 21:29:01 -07002083 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2084 defined $lines[$ctx_ln - 1] &&
2085 $lines[$ctx_ln - 1] =~ /^-/)) {
2086 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2087 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002088 $ctx_ln++;
2089 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002090
Andy Whitcroft53210162008-07-23 21:29:03 -07002091 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2092 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002093
2094 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002095 ERROR("OPEN_BRACE",
2096 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002097 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002098 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002099 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2100 $ctx =~ /\)\s*\;\s*$/ &&
2101 defined $lines[$ctx_ln - 1])
2102 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002103 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2104 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002105 WARN("TRAILING_SEMICOLON",
2106 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002107 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002108 }
2109 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002110 }
2111
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002112# Check relative indent for conditionals and blocks.
2113 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002114 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2115 ctx_statement_block($linenr, $realcnt, 0)
2116 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002117 my ($s, $c) = ($stat, $cond);
2118
2119 substr($s, 0, length($c), '');
2120
2121 # Make sure we remove the line prefixes as we have
2122 # none on the first line, and are going to readd them
2123 # where necessary.
2124 $s =~ s/\n./\n/gs;
2125
2126 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002127 my @newlines = ($c =~ /\n/gs);
2128 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002129
2130 # We want to check the first line inside the block
2131 # starting at the end of the conditional, so remove:
2132 # 1) any blank line termination
2133 # 2) any opening brace { on end of the line
2134 # 3) any do (...) {
2135 my $continuation = 0;
2136 my $check = 0;
2137 $s =~ s/^.*\bdo\b//;
2138 $s =~ s/^\s*{//;
2139 if ($s =~ s/^\s*\\//) {
2140 $continuation = 1;
2141 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002142 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002143 $check = 1;
2144 $cond_lines++;
2145 }
2146
2147 # Also ignore a loop construct at the end of a
2148 # preprocessor statement.
2149 if (($prevline =~ /^.\s*#\s*define\s/ ||
2150 $prevline =~ /\\\s*$/) && $continuation == 0) {
2151 $check = 0;
2152 }
2153
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002154 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002155 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002156 while ($cond_ptr != $cond_lines) {
2157 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002158
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002159 # If we see an #else/#elif then the code
2160 # is not linear.
2161 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2162 $check = 0;
2163 }
2164
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002165 # Ignore:
2166 # 1) blank lines, they should be at 0,
2167 # 2) preprocessor lines, and
2168 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002169 if ($continuation ||
2170 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002171 $s =~ /^\s*#\s*?/ ||
2172 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002173 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002174 if ($s =~ s/^.*?\n//) {
2175 $cond_lines++;
2176 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002177 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002178 }
2179
2180 my (undef, $sindent) = line_stats("+" . $s);
2181 my $stat_real = raw_line($linenr, $cond_lines);
2182
2183 # Check if either of these lines are modified, else
2184 # this is not this patch's fault.
2185 if (!defined($stat_real) ||
2186 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2187 $check = 0;
2188 }
2189 if (defined($stat_real) && $cond_lines > 1) {
2190 $stat_real = "[...]\n$stat_real";
2191 }
2192
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002193 #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 -07002194
2195 if ($check && (($sindent % 8) != 0 ||
2196 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002197 WARN("SUSPECT_CODE_INDENT",
2198 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002199 }
2200 }
2201
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002202 # Track the 'values' across context and added lines.
2203 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002204 my ($curr_values, $curr_vars) =
2205 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002206 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002207 if ($dbg_values) {
2208 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002209 print "$linenr > .$outline\n";
2210 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002211 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002212 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002213 $prev_values = substr($curr_values, -1);
2214
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002215#ignore lines not being added
2216 if ($line=~/^[^\+]/) {next;}
2217
Andy Whitcroft653d4872007-06-23 17:16:34 -07002218# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002219 if ($dbg_type) {
2220 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002221 ERROR("TEST_TYPE",
2222 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002223 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002224 ERROR("TEST_NOT_TYPE",
2225 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002226 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002227 next;
2228 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002229# TEST: allow direct testing of the attribute matcher.
2230 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002231 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002232 ERROR("TEST_ATTR",
2233 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002234 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002235 ERROR("TEST_NOT_ATTR",
2236 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002237 }
2238 next;
2239 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002240
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002241# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002242 if ($line =~ /^.\s*{/ &&
2243 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002244 ERROR("OPEN_BRACE",
2245 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002246 }
2247
Andy Whitcroft653d4872007-06-23 17:16:34 -07002248#
2249# Checks which are anchored on the added line.
2250#
2251
2252# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002253 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002254 my $path = $1;
2255 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002256 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002257 "malformed #include filename\n" . $herecurr);
2258 }
2259 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2260 ERROR("UAPI_INCLUDE",
2261 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002262 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002263 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002264
2265# no C99 // comments
2266 if ($line =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002267 ERROR("C99_COMMENTS",
2268 "do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002269 }
2270 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002271 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002272 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002273
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002274# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2275# the whole statement.
2276#print "APW <$lines[$realline_next - 1]>\n";
2277 if (defined $realline_next &&
2278 exists $lines[$realline_next - 1] &&
2279 !defined $suppress_export{$realline_next} &&
2280 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2281 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002282 # Handle definitions which produce identifiers with
2283 # a prefix:
2284 # XXX(foo);
2285 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002286 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002287 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002288 $name =~ /^${Ident}_$2/) {
2289#print "FOO C name<$name>\n";
2290 $suppress_export{$realline_next} = 1;
2291
2292 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002293 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002294 ^.DEFINE_$Ident\(\Q$name\E\)|
2295 ^.DECLARE_$Ident\(\Q$name\E\)|
2296 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002297 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2298 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002299 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002300#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2301 $suppress_export{$realline_next} = 2;
2302 } else {
2303 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002304 }
2305 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002306 if (!defined $suppress_export{$linenr} &&
2307 $prevline =~ /^.\s*$/ &&
2308 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2309 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2310#print "FOO B <$lines[$linenr - 1]>\n";
2311 $suppress_export{$linenr} = 2;
2312 }
2313 if (defined $suppress_export{$linenr} &&
2314 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002315 WARN("EXPORT_SYMBOL",
2316 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002317 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002318
Joe Eloff5150bda2010-08-09 17:21:00 -07002319# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002320 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002321 ERROR("GLOBAL_INITIALISERS",
2322 "do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002323 $herecurr);
2324 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002325# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08002326 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002327 ERROR("INITIALISED_STATIC",
2328 "do not initialise statics to 0 or NULL\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002329 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002330 }
2331
Joe Perchescb710ec2010-10-26 14:23:20 -07002332# check for static const char * arrays.
2333 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002334 WARN("STATIC_CONST_CHAR_ARRAY",
2335 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002336 $herecurr);
2337 }
2338
2339# check for static char foo[] = "bar" declarations.
2340 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002341 WARN("STATIC_CONST_CHAR_ARRAY",
2342 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002343 $herecurr);
2344 }
2345
Joe Perches93ed0e22010-10-26 14:23:21 -07002346# check for declarations of struct pci_device_id
2347 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002348 WARN("DEFINE_PCI_DEVICE_TABLE",
2349 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002350 }
2351
Andy Whitcroft653d4872007-06-23 17:16:34 -07002352# check for new typedefs, only function parameters and sparse annotations
2353# make sense.
2354 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002355 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002356 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002357 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002358 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002359 WARN("NEW_TYPEDEFS",
2360 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002361 }
2362
2363# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002364 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002365 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2366 #print "AA<$1>\n";
2367 my ($from, $to) = ($2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002368
Andy Whitcroft65863862009-01-06 14:41:21 -08002369 # Should start with a space.
2370 $to =~ s/^(\S)/ $1/;
2371 # Should not end with a space.
2372 $to =~ s/\s+$//;
2373 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002374 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002375 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002376
Andy Whitcroft65863862009-01-06 14:41:21 -08002377 #print "from<$from> to<$to>\n";
2378 if ($from ne $to) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002379 ERROR("POINTER_LOCATION",
2380 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002381 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002382 }
2383 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2384 #print "BB<$1>\n";
2385 my ($from, $to, $ident) = ($2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002386
Andy Whitcroft65863862009-01-06 14:41:21 -08002387 # Should start with a space.
2388 $to =~ s/^(\S)/ $1/;
2389 # Should not end with a space.
2390 $to =~ s/\s+$//;
2391 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002392 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002393 }
2394 # Modifiers should have spaces.
2395 $to =~ s/(\b$Modifier$)/$1 /;
2396
Andy Whitcroft667026e2009-02-27 14:03:08 -08002397 #print "from<$from> to<$to> ident<$ident>\n";
2398 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002399 ERROR("POINTER_LOCATION",
2400 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002401 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002402 }
2403
2404# # no BUG() or BUG_ON()
2405# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2406# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2407# print "$herecurr";
2408# $clean = 0;
2409# }
2410
Andy Whitcroft8905a672007-11-28 16:21:06 -08002411 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002412 WARN("LINUX_VERSION_CODE",
2413 "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 -08002414 }
2415
Joe Perches17441222011-06-15 15:08:17 -07002416# check for uses of printk_ratelimit
2417 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002418 WARN("PRINTK_RATELIMITED",
2419"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002420 }
2421
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002422# printk should use KERN_* levels. Note that follow on printk's on the
2423# same line do not need a level, so we use the current block context
2424# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002425# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002426# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002427 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002428 my $ok = 0;
2429 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2430 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002431 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002432 # with "\n" ignore it, else it is to blame
2433 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2434 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2435 $ok = 1;
2436 }
2437 last;
2438 }
2439 }
2440 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002441 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2442 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002443 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002444 }
2445
Joe Perches243f3802012-05-31 16:26:09 -07002446 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2447 my $orig = $1;
2448 my $level = lc($orig);
2449 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002450 my $level2 = $level;
2451 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002452 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002453 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002454 }
2455
2456 if ($line =~ /\bpr_warning\s*\(/) {
2457 WARN("PREFER_PR_LEVEL",
2458 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2459 }
2460
Joe Perchesdc139312013-02-21 16:44:13 -08002461 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2462 my $orig = $1;
2463 my $level = lc($orig);
2464 $level = "warn" if ($level eq "warning");
2465 $level = "dbg" if ($level eq "debug");
2466 WARN("PREFER_DEV_LEVEL",
2467 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2468 }
2469
Andy Whitcroft653d4872007-06-23 17:16:34 -07002470# function brace can't be on same line, except for #defines of do while,
2471# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002472 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2473 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002474 ERROR("OPEN_BRACE",
2475 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002476 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002477
Andy Whitcroft8905a672007-11-28 16:21:06 -08002478# open braces for enum, union and struct go on the same line.
2479 if ($line =~ /^.\s*{/ &&
2480 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002481 ERROR("OPEN_BRACE",
2482 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002483 }
2484
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002485# missing space after union, struct or enum definition
2486 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002487 WARN("SPACING",
2488 "missing space after $1 definition\n" . $herecurr);
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002489 }
2490
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002491# check for spacing round square brackets; allowed:
2492# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002493# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2494# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002495 while ($line =~ /(.*?\s)\[/g) {
2496 my ($where, $prefix) = ($-[1], $1);
2497 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002498 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002499 $prefix !~ /[{,]\s+$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002500 ERROR("BRACKET_SPACE",
2501 "space prohibited before open square bracket '['\n" . $herecurr);
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002502 }
2503 }
2504
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002505# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002506 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002507 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002508 my $ctx_before = substr($line, 0, $-[1]);
2509 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002510
2511 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002512 if ($name =~ /^(?:
2513 if|for|while|switch|return|case|
2514 volatile|__volatile__|
2515 __attribute__|format|__extension__|
2516 asm|__asm__)$/x)
2517 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002518
2519 # cpp #define statements have non-optional spaces, ie
2520 # if there is a space between the name and the open
2521 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002522 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002523
2524 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002525 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002526
2527 # If this whole things ends with a type its most
2528 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002529 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002530
2531 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07002532 WARN("SPACING",
2533 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002534 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002535 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002536
2537# check for whitespace before a non-naked semicolon
2538 if ($line =~ /^\+.*\S\s+;/) {
Joe Perches5646bc72013-04-29 16:18:15 -07002539 WARN("SPACING",
2540 "space prohibited before semicolon\n" . $herecurr);
Eric Nelson9a4cad42012-05-31 16:26:09 -07002541 }
2542
Andy Whitcroft653d4872007-06-23 17:16:34 -07002543# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002544 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002545 my $ops = qr{
2546 <<=|>>=|<=|>=|==|!=|
2547 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2548 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002549 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2550 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002551 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002552 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002553 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002554
2555 my $blank = copy_spacing($opline);
2556
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002557 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002558 $off += length($elements[$n]);
2559
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002560 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002561 my $ca = substr($opline, 0, $off);
2562 my $cc = '';
2563 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2564 $cc = substr($opline, $off + length($elements[$n + 1]));
2565 }
2566 my $cb = "$ca$;$cc";
2567
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002568 my $a = '';
2569 $a = 'V' if ($elements[$n] ne '');
2570 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002571 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002572 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2573 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002574 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002575
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002576 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002577
2578 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002579 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002580 $c = 'V' if ($elements[$n + 2] ne '');
2581 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002582 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002583 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2584 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002585 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002586 } else {
2587 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002588 }
2589
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002590 my $ctx = "${a}x${c}";
2591
2592 my $at = "(ctx:$ctx)";
2593
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002594 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002595 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002596
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002597 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002598 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002599
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002600 # Get the full operator variant.
2601 my $opv = $op . substr($curr_vars, $off, 1);
2602
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002603 # Ignore operators passed as parameters.
2604 if ($op_type ne 'V' &&
2605 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2606
Andy Whitcroftcf655042008-03-04 14:28:20 -08002607# # Ignore comments
2608# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002609
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002610 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002611 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002612 if ($ctx !~ /.x[WEBC]/ &&
2613 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002614 ERROR("SPACING",
2615 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002616 }
2617
2618 # // is a comment
2619 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002620
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002621 # No spaces for:
2622 # ->
2623 # : when part of a bitfield
2624 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002625 if ($ctx =~ /Wx.|.xW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002626 ERROR("SPACING",
2627 "spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002628 }
2629
2630 # , must have a space on the right.
2631 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002632 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002633 ERROR("SPACING",
2634 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002635 }
2636
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002637 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002638 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002639 #warn "'*' is part of type\n";
2640
2641 # unary operators should have a space before and
2642 # none after. May be left adjacent to another
2643 # unary operator, or a cast
2644 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002645 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002646 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002647 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002648 ERROR("SPACING",
2649 "space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002650 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002651 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002652 # A unary '*' may be const
2653
2654 } elsif ($ctx =~ /.xW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002655 ERROR("SPACING",
2656 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002657 }
2658
2659 # unary ++ and unary -- are allowed no space on one side.
2660 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002661 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002662 ERROR("SPACING",
2663 "space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002664 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002665 if ($ctx =~ /Wx[BE]/ ||
2666 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002667 ERROR("SPACING",
2668 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002669 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002670 if ($ctx =~ /ExW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002671 ERROR("SPACING",
2672 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002673 }
2674
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002675
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002676 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002677 } elsif ($op eq '<<' or $op eq '>>' or
2678 $op eq '&' or $op eq '^' or $op eq '|' or
2679 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002680 $op eq '*' or $op eq '/' or
2681 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002682 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002683 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002684 ERROR("SPACING",
2685 "need consistent spacing around '$op' $at\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002686 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002687 }
2688
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002689 # A colon needs no spaces before when it is
2690 # terminating a case value or a label.
2691 } elsif ($opv eq ':C' || $opv eq ':L') {
2692 if ($ctx =~ /Wx./) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002693 ERROR("SPACING",
2694 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002695 }
2696
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002697 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002698 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002699 my $ok = 0;
2700
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002701 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002702 if (($op eq '<' &&
2703 $cc =~ /^\S+\@\S+>/) ||
2704 ($op eq '>' &&
2705 $ca =~ /<\S+\@\S+$/))
2706 {
2707 $ok = 1;
2708 }
2709
2710 # Ignore ?:
2711 if (($opv eq ':O' && $ca =~ /\?$/) ||
2712 ($op eq '?' && $cc =~ /^:/)) {
2713 $ok = 1;
2714 }
2715
2716 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002717 ERROR("SPACING",
2718 "spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002719 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002720 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002721 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002722 }
2723 }
2724
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002725# check for multiple assignments
2726 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002727 CHK("MULTIPLE_ASSIGNMENTS",
2728 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002729 }
2730
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002731## # check for multiple declarations, allowing for a function declaration
2732## # continuation.
2733## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2734## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2735##
2736## # Remove any bracketed sections to ensure we do not
2737## # falsly report the parameters of functions.
2738## my $ln = $line;
2739## while ($ln =~ s/\([^\(\)]*\)//g) {
2740## }
2741## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002742## WARN("MULTIPLE_DECLARATION",
2743## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002744## }
2745## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002746
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002747#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002748 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2749 $line =~ /do{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002750 ERROR("SPACING",
2751 "space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002752 }
2753
Joe Perchesc4a62ef2013-07-03 15:05:28 -07002754## # check for blank lines before declarations
2755## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
2756## $prevrawline =~ /^.\s*$/) {
2757## WARN("SPACING",
2758## "No blank lines before declarations\n" . $hereprev);
2759## }
2760##
2761
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002762# closing brace should have a space following it when it has anything
2763# on the line
2764 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002765 ERROR("SPACING",
2766 "space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002767 }
2768
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002769# check spacing on square brackets
2770 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002771 ERROR("SPACING",
2772 "space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002773 }
2774 if ($line =~ /\s\]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002775 ERROR("SPACING",
2776 "space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002777 }
2778
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002779# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002780 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2781 $line !~ /for\s*\(\s+;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002782 ERROR("SPACING",
2783 "space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002784 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002785 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002786 $line !~ /for\s*\(.*;\s+\)/ &&
2787 $line !~ /:\s+\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002788 ERROR("SPACING",
2789 "space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002790 }
2791
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002792#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002793 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002794 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002795 WARN("INDENTED_LABEL",
2796 "labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002797 }
2798
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002799# Return is not a function.
2800 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2801 my $spacing = $1;
2802 my $value = $2;
2803
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002804 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002805 $value =~ s/\(/ \(/g;
2806 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08002807 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002808 $value !~ /(?:$Ident|-?$Constant)\s*
2809 $Compare\s*
2810 (?:$Ident|-?$Constant)/x &&
2811 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002812 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002813#print "value<$value>\n";
2814 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002815 ERROR("RETURN_PARENTHESES",
2816 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002817
2818 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002819 ERROR("SPACING",
2820 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002821 }
2822 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002823# Return of what appears to be an errno should normally be -'ve
2824 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2825 my $name = $1;
2826 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002827 WARN("USE_NEGATIVE_ERRNO",
2828 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002829 }
2830 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002831
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002832# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002833 if ($line=~/\b(if|while|for|switch)\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002834 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002835 }
2836
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002837# Check for illegal assignment in if conditional -- and check for trailing
2838# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002839 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002840 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2841 ctx_statement_block($linenr, $realcnt, 0)
2842 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002843 my ($stat_next) = ctx_statement_block($line_nr_next,
2844 $remain_next, $off_next);
2845 $stat_next =~ s/\n./\n /g;
2846 ##print "stat<$stat> stat_next<$stat_next>\n";
2847
2848 if ($stat_next =~ /^\s*while\b/) {
2849 # If the statement carries leading newlines,
2850 # then count those as offsets.
2851 my ($whitespace) =
2852 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2853 my $offset =
2854 statement_rawlines($whitespace) - 1;
2855
2856 $suppress_whiletrailers{$line_nr_next +
2857 $offset} = 1;
2858 }
2859 }
2860 if (!defined $suppress_whiletrailers{$linenr} &&
2861 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002862 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002863
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002864 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002865 ERROR("ASSIGN_IN_IF",
2866 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002867 }
2868
2869 # Find out what is on the end of the line after the
2870 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002871 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002872 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002873 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002874 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2875 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002876 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002877 # Find out how long the conditional actually is.
2878 my @newlines = ($c =~ /\n/gs);
2879 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002880 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002881
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002882 $stat_real = raw_line($linenr, $cond_lines)
2883 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002884 if (defined($stat_real) && $cond_lines > 1) {
2885 $stat_real = "[...]\n$stat_real";
2886 }
2887
Joe Perches000d1cc12011-07-25 17:13:25 -07002888 ERROR("TRAILING_STATEMENTS",
2889 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002890 }
2891 }
2892
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002893# Check for bitwise tests written as boolean
2894 if ($line =~ /
2895 (?:
2896 (?:\[|\(|\&\&|\|\|)
2897 \s*0[xX][0-9]+\s*
2898 (?:\&\&|\|\|)
2899 |
2900 (?:\&\&|\|\|)
2901 \s*0[xX][0-9]+\s*
2902 (?:\&\&|\|\||\)|\])
2903 )/x)
2904 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002905 WARN("HEXADECIMAL_BOOLEAN_TEST",
2906 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002907 }
2908
Andy Whitcroft8905a672007-11-28 16:21:06 -08002909# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002910 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2911 my $s = $1;
2912 $s =~ s/$;//g; # Remove any comments
2913 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002914 ERROR("TRAILING_STATEMENTS",
2915 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002916 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002917 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002918# if should not continue a brace
2919 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002920 ERROR("TRAILING_STATEMENTS",
2921 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08002922 $herecurr);
2923 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002924# case and default should not have general statements after them
2925 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2926 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002927 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002928 \s*return\s+
2929 )/xg)
2930 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002931 ERROR("TRAILING_STATEMENTS",
2932 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002933 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002934
2935 # Check for }<nl>else {, these must be at the same
2936 # indent level to be relevant to each other.
2937 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2938 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002939 ERROR("ELSE_AFTER_BRACE",
2940 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002941 }
2942
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002943 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2944 $previndent == $indent) {
2945 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
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 Whitcroftc2fdda02008-02-08 04:20:54 -08002950 $s =~ s/\n.*//g;
2951
2952 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002953 ERROR("WHILE_AFTER_BRACE",
2954 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002955 }
2956 }
2957
Joe Perches95e2c602013-07-03 15:05:20 -07002958#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08002959 while ($line =~ m{($Constant|$Lval)}g) {
2960 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07002961
2962#gcc binary extension
2963 if ($var =~ /^$Binary$/) {
2964 WARN("GCC_BINARY_CONSTANT",
2965 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
2966 }
2967
2968#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07002969 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07002970 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches807bd262013-07-03 15:05:22 -07002971 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches323c1262012-12-17 16:02:07 -08002972 !defined $camelcase{$var}) {
2973 $camelcase{$var} = 1;
Joe Perchesbe797942013-07-03 15:05:20 -07002974 CHK("CAMELCASE",
2975 "Avoid CamelCase: <$var>\n" . $herecurr);
Joe Perches323c1262012-12-17 16:02:07 -08002976 }
2977 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002978
2979#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002980 if ($line=~/\#\s*define.*\\\s$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002981 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2982 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002983 }
2984
Andy Whitcroft653d4872007-06-23 17:16:34 -07002985#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002986 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002987 my $file = "$1.h";
2988 my $checkfile = "include/linux/$file";
2989 if (-f "$root/$checkfile" &&
2990 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002991 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002992 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002993 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002994 CHK("ARCH_INCLUDE_LINUX",
2995 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002996 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07002997 WARN("INCLUDE_LINUX",
2998 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002999 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003000 }
3001 }
3002
Andy Whitcroft653d4872007-06-23 17:16:34 -07003003# multi-statement macros should be enclosed in a do while loop, grab the
3004# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08003005# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07003006 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3007 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003008 my $ln = $linenr;
3009 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003010 my ($off, $dstat, $dcond, $rest);
3011 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003012 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003013 ctx_statement_block($linenr, $realcnt, 0);
3014 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003015 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07003016 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003017
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003018 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003019 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003020 $dstat =~ s/\\\n.//g;
3021 $dstat =~ s/^\s*//s;
3022 $dstat =~ s/\s*$//s;
3023
3024 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003025 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3026 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003027 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003028 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003029 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003030
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003031 # Flatten any obvious string concatentation.
3032 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3033 $dstat =~ s/$Ident\s*("X*")/$1/)
3034 {
3035 }
3036
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003037 my $exceptions = qr{
3038 $Declare|
3039 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003040 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003041 DECLARE_PER_CPU|
3042 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003043 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003044 union|
3045 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003046 \.$Ident\s*=\s*|
3047 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003048 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003049 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003050 if ($dstat ne '' &&
3051 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3052 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07003053 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003054 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003055 $dstat !~ /$exceptions/ &&
3056 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003057 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003058 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003059 $dstat !~ /^for\s*$Constant$/ && # for (...)
3060 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3061 $dstat !~ /^do\s*{/ && # do {...
3062 $dstat !~ /^\({/) # ({...
3063 {
3064 $ctx =~ s/\n*$//;
3065 my $herectx = $here . "\n";
3066 my $cnt = statement_rawlines($ctx);
3067
3068 for (my $n = 0; $n < $cnt; $n++) {
3069 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003070 }
3071
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003072 if ($dstat =~ /;/) {
3073 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3074 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3075 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003076 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003077 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003078 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003079 }
Joe Perches5023d342012-12-17 16:01:47 -08003080
Joe Perches481eb482012-12-17 16:01:56 -08003081# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003082
3083 } else {
3084 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003085 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3086 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003087 $line =~ /^\+.*\\$/) {
3088 WARN("LINE_CONTINUATIONS",
3089 "Avoid unnecessary line continuations\n" . $herecurr);
3090 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003091 }
3092
Joe Perchesb13edf72012-07-30 14:41:24 -07003093# do {} while (0) macro tests:
3094# single-statement macros do not need to be enclosed in do while (0) loop,
3095# macro should not end with a semicolon
3096 if ($^V && $^V ge 5.10.0 &&
3097 $realfile !~ m@/vmlinux.lds.h$@ &&
3098 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3099 my $ln = $linenr;
3100 my $cnt = $realcnt;
3101 my ($off, $dstat, $dcond, $rest);
3102 my $ctx = '';
3103 ($dstat, $dcond, $ln, $cnt, $off) =
3104 ctx_statement_block($linenr, $realcnt, 0);
3105 $ctx = $dstat;
3106
3107 $dstat =~ s/\\\n.//g;
3108
3109 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3110 my $stmts = $2;
3111 my $semis = $3;
3112
3113 $ctx =~ s/\n*$//;
3114 my $cnt = statement_rawlines($ctx);
3115 my $herectx = $here . "\n";
3116
3117 for (my $n = 0; $n < $cnt; $n++) {
3118 $herectx .= raw_line($linenr, $n) . "\n";
3119 }
3120
Joe Perchesac8e97f2012-08-21 16:15:53 -07003121 if (($stmts =~ tr/;/;/) == 1 &&
3122 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003123 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3124 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3125 }
3126 if (defined $semis && $semis ne "") {
3127 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3128 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3129 }
3130 }
3131 }
3132
Mike Frysinger080ba922009-01-06 14:41:25 -08003133# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3134# all assignments may have only one of the following with an assignment:
3135# .
3136# ALIGN(...)
3137# VMLINUX_SYMBOL(...)
3138 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003139 WARN("MISSING_VMLINUX_SYMBOL",
3140 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003141 }
3142
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003143# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003144 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3145 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003146 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003147 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003148 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3149 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003150 my @allowed = ();
3151 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003152 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003153 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003154 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003155 for my $chunk (@chunks) {
3156 my ($cond, $block) = @{$chunk};
3157
Andy Whitcroft773647a2008-03-28 14:15:58 -07003158 # If the condition carries leading newlines, then count those as offsets.
3159 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3160 my $offset = statement_rawlines($whitespace) - 1;
3161
Joe Perchesaad4f612012-03-23 15:02:19 -07003162 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003163 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3164
3165 # We have looked at and allowed this specific line.
3166 $suppress_ifbraces{$ln + $offset} = 1;
3167
3168 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003169 $ln += statement_rawlines($block) - 1;
3170
Andy Whitcroft773647a2008-03-28 14:15:58 -07003171 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003172
3173 $seen++ if ($block =~ /^\s*{/);
3174
Joe Perchesaad4f612012-03-23 15:02:19 -07003175 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003176 if (statement_lines($cond) > 1) {
3177 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003178 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003179 }
3180 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003181 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003182 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003183 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003184 if (statement_block_size($block) > 1) {
3185 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003186 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003187 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003188 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003189 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003190 if ($seen) {
3191 my $sum_allowed = 0;
3192 foreach (@allowed) {
3193 $sum_allowed += $_;
3194 }
3195 if ($sum_allowed == 0) {
3196 WARN("BRACES",
3197 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3198 } elsif ($sum_allowed != $allow &&
3199 $seen != $allow) {
3200 CHK("BRACES",
3201 "braces {} should be used on all arms of this statement\n" . $herectx);
3202 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003203 }
3204 }
3205 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003206 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003207 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003208 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003209
Andy Whitcroftcf655042008-03-04 14:28:20 -08003210 # Check the pre-context.
3211 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3212 #print "APW: ALLOWED: pre<$1>\n";
3213 $allowed = 1;
3214 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003215
3216 my ($level, $endln, @chunks) =
3217 ctx_statement_full($linenr, $realcnt, $-[0]);
3218
Andy Whitcroftcf655042008-03-04 14:28:20 -08003219 # Check the condition.
3220 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003221 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003222 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003223 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003224 }
3225 if (statement_lines($cond) > 1) {
3226 #print "APW: ALLOWED: cond<$cond>\n";
3227 $allowed = 1;
3228 }
3229 if ($block =~/\b(?:if|for|while)\b/) {
3230 #print "APW: ALLOWED: block<$block>\n";
3231 $allowed = 1;
3232 }
3233 if (statement_block_size($block) > 1) {
3234 #print "APW: ALLOWED: lines block<$block>\n";
3235 $allowed = 1;
3236 }
3237 # Check the post-context.
3238 if (defined $chunks[1]) {
3239 my ($cond, $block) = @{$chunks[1]};
3240 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003241 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003242 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003243 if ($block =~ /^\s*\{/) {
3244 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3245 $allowed = 1;
3246 }
3247 }
3248 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003249 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003250 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003251
Andy Whitcroftf0556632008-10-15 22:02:23 -07003252 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003253 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003254 }
3255
Joe Perches000d1cc12011-07-25 17:13:25 -07003256 WARN("BRACES",
3257 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003258 }
3259 }
3260
Joe Perches0979ae62012-12-17 16:01:59 -08003261# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07003262 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003263 CHK("BRACES",
3264 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3265 }
Joe Perches77b9a532013-07-03 15:05:29 -07003266 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003267 CHK("BRACES",
3268 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3269 }
3270
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003271# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003272 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3273 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003274 WARN("VOLATILE",
3275 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003276 }
3277
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003278# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003279 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003280 CHK("REDUNDANT_CODE",
3281 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003282 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003283 }
3284
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003285# check for needless "if (<foo>) fn(<foo>)" uses
3286 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3287 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3288 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3289 WARN('NEEDLESS_IF',
3290 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003291 }
3292 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003293
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003294# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003295 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003296 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003297 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003298 CHK("USLEEP_RANGE",
3299 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003300 }
3301 }
3302
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003303# warn about unexpectedly long msleep's
3304 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3305 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003306 WARN("MSLEEP",
3307 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003308 }
3309 }
3310
Joe Perches36ec1932013-07-03 15:05:25 -07003311# check for comparisons of jiffies
3312 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3313 WARN("JIFFIES_COMPARISON",
3314 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3315 }
3316
Joe Perches9d7a34a2013-07-03 15:05:26 -07003317# check for comparisons of get_jiffies_64()
3318 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3319 WARN("JIFFIES_COMPARISON",
3320 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3321 }
3322
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003323# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003324# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003325# print "#ifdef in C files should be avoided\n";
3326# print "$herecurr";
3327# $clean = 0;
3328# }
3329
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003330# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003331 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003332 ERROR("SPACING",
3333 "exactly one space required after that #$1\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003334 }
3335
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003336# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003337 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3338 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003339 my $which = $1;
3340 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003341 CHK("UNCOMMENTED_DEFINITION",
3342 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003343 }
3344 }
3345# check for memory barriers without a comment.
3346 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3347 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003348 CHK("MEMORY_BARRIER",
3349 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003350 }
3351 }
3352# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003353 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003354 CHK("ARCH_DEFINES",
3355 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003356 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003357
Tobias Klauserd4977c72010-05-24 14:33:30 -07003358# Check that the storage class is at the beginning of a declaration
3359 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003360 WARN("STORAGE_CLASS",
3361 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003362 }
3363
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003364# check the location of the inline attribute, that it is between
3365# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003366 if ($line =~ /\b$Type\s+$Inline\b/ ||
3367 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003368 ERROR("INLINE_LOCATION",
3369 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003370 }
3371
Andy Whitcroft8905a672007-11-28 16:21:06 -08003372# Check for __inline__ and __inline, prefer inline
3373 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003374 WARN("INLINE",
3375 "plain inline is preferred over $1\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003376 }
3377
Joe Perches3d130fd2011-01-12 17:00:00 -08003378# Check for __attribute__ packed, prefer __packed
3379 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003380 WARN("PREFER_PACKED",
3381 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003382 }
3383
Joe Perches39b7e282011-07-25 17:13:24 -07003384# Check for __attribute__ aligned, prefer __aligned
3385 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003386 WARN("PREFER_ALIGNED",
3387 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003388 }
3389
Joe Perches5f14d3b2012-01-10 15:09:52 -08003390# Check for __attribute__ format(printf, prefer __printf
3391 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3392 WARN("PREFER_PRINTF",
3393 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3394 }
3395
Joe Perches6061d942012-03-23 15:02:16 -07003396# Check for __attribute__ format(scanf, prefer __scanf
3397 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3398 WARN("PREFER_SCANF",
3399 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3400 }
3401
Joe Perches8f53a9b2010-03-05 13:43:48 -08003402# check for sizeof(&)
3403 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003404 WARN("SIZEOF_ADDRESS",
3405 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003406 }
3407
Joe Perches66c80b62012-07-30 14:41:22 -07003408# check for sizeof without parenthesis
3409 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3410 WARN("SIZEOF_PARENTHESIS",
3411 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3412 }
3413
Joe Perches428e2fd2011-05-24 17:13:39 -07003414# check for line continuations in quoted strings with odd counts of "
3415 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003416 WARN("LINE_CONTINUATIONS",
3417 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003418 }
3419
Joe Perches88982fe2012-12-17 16:02:00 -08003420# check for struct spinlock declarations
3421 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3422 WARN("USE_SPINLOCK_T",
3423 "struct spinlock should be spinlock_t\n" . $herecurr);
3424 }
3425
Joe Perchesa6962d72013-04-29 16:18:13 -07003426# check for seq_printf uses that could be seq_puts
3427 if ($line =~ /\bseq_printf\s*\(/) {
3428 my $fmt = get_quoted_string($line, $rawline);
3429 if ($fmt !~ /[^\\]\%/) {
3430 WARN("PREFER_SEQ_PUTS",
3431 "Prefer seq_puts to seq_printf\n" . $herecurr);
3432 }
3433 }
3434
Andy Whitcroft554e1652012-01-10 15:09:57 -08003435# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003436 if ($^V && $^V ge 5.10.0 &&
3437 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003438 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003439
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003440 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003441 my $ms_val = $7;
3442 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003443
Andy Whitcroft554e1652012-01-10 15:09:57 -08003444 if ($ms_size =~ /^(0x|)0$/i) {
3445 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003446 "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 -08003447 } elsif ($ms_size =~ /^(0x|)1$/i) {
3448 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003449 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3450 }
3451 }
3452
3453# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003454 if ($^V && $^V ge 5.10.0 &&
3455 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003456 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003457 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003458 my $call = $1;
3459 my $cast1 = deparenthesize($2);
3460 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003461 my $cast2 = deparenthesize($7);
3462 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003463 my $cast;
3464
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003465 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003466 $cast = "$cast1 or $cast2";
3467 } elsif ($cast1 ne "") {
3468 $cast = $cast1;
3469 } else {
3470 $cast = $cast2;
3471 }
3472 WARN("MINMAX",
3473 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003474 }
3475 }
3476
Joe Perches4a273192012-07-30 14:41:20 -07003477# check usleep_range arguments
3478 if ($^V && $^V ge 5.10.0 &&
3479 defined $stat &&
3480 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3481 my $min = $1;
3482 my $max = $7;
3483 if ($min eq $max) {
3484 WARN("USLEEP_RANGE",
3485 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3486 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3487 $min > $max) {
3488 WARN("USLEEP_RANGE",
3489 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3490 }
3491 }
3492
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003493# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003494 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003495 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003496 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003497 my $function_name = $1;
3498 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003499
3500 my $s = $stat;
3501 if (defined $cond) {
3502 substr($s, 0, length($cond), '');
3503 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003504 if ($s =~ /^\s*;/ &&
3505 $function_name ne 'uninitialized_var')
3506 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003507 WARN("AVOID_EXTERNS",
3508 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003509 }
3510
3511 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003512 WARN("FUNCTION_ARGUMENTS",
3513 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003514 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003515
3516 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3517 $stat =~ /^.\s*extern\s+/)
3518 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003519 WARN("AVOID_EXTERNS",
3520 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003521 }
3522
3523# checks for new __setup's
3524 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3525 my $name = $1;
3526
3527 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003528 CHK("UNDOCUMENTED_SETUP",
3529 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003530 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003531 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003532
3533# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003534 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003535 WARN("UNNECESSARY_CASTS",
3536 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003537 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003538
Joe Perchesa640d252013-07-03 15:05:21 -07003539# alloc style
3540# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3541 if ($^V && $^V ge 5.10.0 &&
3542 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3543 CHK("ALLOC_SIZEOF_STRUCT",
3544 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3545 }
3546
Joe Perches972fdea2013-04-29 16:18:12 -07003547# check for krealloc arg reuse
3548 if ($^V && $^V ge 5.10.0 &&
3549 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3550 WARN("KREALLOC_ARG_REUSE",
3551 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3552 }
3553
Joe Perches5ce59ae2013-02-21 16:44:18 -08003554# check for alloc argument mismatch
3555 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3556 WARN("ALLOC_ARRAY_ARGS",
3557 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3558 }
3559
Joe Perchescaf2a542011-01-12 16:59:56 -08003560# check for multiple semicolons
3561 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08003562 WARN("ONE_SEMICOLON",
3563 "Statements terminations use 1 semicolon\n" . $herecurr);
3564 }
3565
3566# check for switch/default statements without a break;
3567 if ($^V && $^V ge 5.10.0 &&
3568 defined $stat &&
3569 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3570 my $ctx = '';
3571 my $herectx = $here . "\n";
3572 my $cnt = statement_rawlines($stat);
3573 for (my $n = 0; $n < $cnt; $n++) {
3574 $herectx .= raw_line($linenr, $n) . "\n";
3575 }
3576 WARN("DEFAULT_NO_BREAK",
3577 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08003578 }
3579
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003580# check for gcc specific __FUNCTION__
3581 if ($line =~ /__FUNCTION__/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003582 WARN("USE_FUNC",
3583 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003584 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003585
Joe Perches2c924882012-03-23 15:02:20 -07003586# check for use of yield()
3587 if ($line =~ /\byield\s*\(\s*\)/) {
3588 WARN("YIELD",
3589 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3590 }
3591
Joe Perches179f8f42013-07-03 15:05:30 -07003592# check for comparisons against true and false
3593 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3594 my $lead = $1;
3595 my $arg = $2;
3596 my $test = $3;
3597 my $otype = $4;
3598 my $trail = $5;
3599 my $op = "!";
3600
3601 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3602
3603 my $type = lc($otype);
3604 if ($type =~ /^(?:true|false)$/) {
3605 if (("$test" eq "==" && "$type" eq "true") ||
3606 ("$test" eq "!=" && "$type" eq "false")) {
3607 $op = "";
3608 }
3609
3610 CHK("BOOL_COMPARISON",
3611 "Using comparison to $otype is error prone\n" . $herecurr);
3612
3613## maybe suggesting a correct construct would better
3614## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3615
3616 }
3617 }
3618
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003619# check for semaphores initialized locked
3620 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003621 WARN("CONSIDER_COMPLETION",
3622 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003623 }
Joe Perches6712d852012-03-23 15:02:20 -07003624
Joe Perches67d0a072011-10-31 17:13:10 -07003625# recommend kstrto* over simple_strto* and strict_strto*
3626 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003627 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07003628 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003629 }
Joe Perches6712d852012-03-23 15:02:20 -07003630
Michael Ellermanf3db6632008-07-23 21:28:57 -07003631# check for __initcall(), use device_initcall() explicitly please
3632 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003633 WARN("USE_DEVICE_INITCALL",
3634 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07003635 }
Joe Perches6712d852012-03-23 15:02:20 -07003636
Emese Revfy79404842010-03-05 13:43:53 -08003637# check for various ops structs, ensure they are const.
3638 my $struct_ops = qr{acpi_dock_ops|
3639 address_space_operations|
3640 backlight_ops|
3641 block_device_operations|
3642 dentry_operations|
3643 dev_pm_ops|
3644 dma_map_ops|
3645 extent_io_ops|
3646 file_lock_operations|
3647 file_operations|
3648 hv_ops|
3649 ide_dma_ops|
3650 intel_dvo_dev_ops|
3651 item_operations|
3652 iwl_ops|
3653 kgdb_arch|
3654 kgdb_io|
3655 kset_uevent_ops|
3656 lock_manager_operations|
3657 microcode_ops|
3658 mtrr_ops|
3659 neigh_ops|
3660 nlmsvc_binding|
3661 pci_raw_ops|
3662 pipe_buf_operations|
3663 platform_hibernation_ops|
3664 platform_suspend_ops|
3665 proto_ops|
3666 rpc_pipe_ops|
3667 seq_operations|
3668 snd_ac97_build_ops|
3669 soc_pcmcia_socket_ops|
3670 stacktrace_ops|
3671 sysfs_ops|
3672 tty_operations|
3673 usb_mon_operations|
3674 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003675 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003676 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003677 WARN("CONST_STRUCT",
3678 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003679 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003680 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003681
3682# use of NR_CPUS is usually wrong
3683# ignore definitions of NR_CPUS and usage to define arrays as likely right
3684 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003685 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3686 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003687 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3688 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3689 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003690 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003691 WARN("NR_CPUS",
3692 "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 -07003693 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003694
3695# check for %L{u,d,i} in strings
3696 my $string;
3697 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3698 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003699 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003700 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003701 WARN("PRINTF_L",
3702 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003703 last;
3704 }
3705 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003706
3707# whine mightly about in_atomic
3708 if ($line =~ /\bin_atomic\s*\(/) {
3709 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003710 ERROR("IN_ATOMIC",
3711 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003712 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003713 WARN("IN_ATOMIC",
3714 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003715 }
3716 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003717
3718# check for lockdep_set_novalidate_class
3719 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3720 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3721 if ($realfile !~ m@^kernel/lockdep@ &&
3722 $realfile !~ m@^include/linux/lockdep@ &&
3723 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003724 ERROR("LOCKDEP",
3725 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003726 }
3727 }
Dave Jones88f88312011-01-12 16:59:59 -08003728
3729 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3730 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003731 WARN("EXPORTED_WORLD_WRITABLE",
3732 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08003733 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003734 }
3735
3736 # If we have no input at all, then there is nothing to report on
3737 # so just keep quiet.
3738 if ($#rawlines == -1) {
3739 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003740 }
3741
Andy Whitcroft8905a672007-11-28 16:21:06 -08003742 # In mailback mode only produce a report in the negative, for
3743 # things that appear to be patches.
3744 if ($mailback && ($clean == 1 || !$is_patch)) {
3745 exit(0);
3746 }
3747
3748 # This is not a patch, and we are are in 'no-patch' mode so
3749 # just keep quiet.
3750 if (!$chk_patch && !$is_patch) {
3751 exit(0);
3752 }
3753
3754 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003755 ERROR("NOT_UNIFIED_DIFF",
3756 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003757 }
3758 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003759 ERROR("MISSING_SIGN_OFF",
3760 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003761 }
3762
Andy Whitcroft8905a672007-11-28 16:21:06 -08003763 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003764 if ($summary && !($clean == 1 && $quiet == 1)) {
3765 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003766 print "total: $cnt_error errors, $cnt_warn warnings, " .
3767 (($check)? "$cnt_chk checks, " : "") .
3768 "$cnt_lines lines checked\n";
3769 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003770 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003771
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003772 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003773
3774 if ($^V lt 5.10.0) {
3775 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3776 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3777 }
3778
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003779 # If there were whitespace errors which cleanpatch can fix
3780 # then suggest that.
3781 if ($rpt_cleaners) {
3782 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3783 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003784 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003785 }
3786 }
3787
Artem Bityutskiy11232682012-03-23 15:02:17 -07003788 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003789 print "NOTE: Ignored message types:";
3790 foreach my $ignore (sort keys %ignore_type) {
3791 print " $ignore";
3792 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07003793 print "\n\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003794 }
3795
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003796 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003797 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003798 }
3799 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003800 print << "EOM";
3801$vname has style problems, please review.
3802
3803If any of these errors are false positives, please report
3804them to the maintainer, see CHECKPATCH in MAINTAINERS.
3805EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003806 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003807
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003808 return $clean;
3809}