blob: 6185eb67df94ba24300893634207f53a830a462c [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{<=|>=|==|!=|<|>};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700244our $Operators = qr{
245 <=|>=|==|!=|
246 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800247 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700248 }x;
249
Andy Whitcroft8905a672007-11-28 16:21:06 -0800250our $NonptrType;
251our $Type;
252our $Declare;
253
Joe Perches15662b32011-10-31 17:13:12 -0700254our $NON_ASCII_UTF8 = qr{
255 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700256 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
257 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
258 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
259 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
260 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
261 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
262}x;
263
Joe Perches15662b32011-10-31 17:13:12 -0700264our $UTF8 = qr{
265 [\x09\x0A\x0D\x20-\x7E] # ASCII
266 | $NON_ASCII_UTF8
267}x;
268
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700269our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700270 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700271 atomic_t
272)};
273
Joe Perches691e6692010-03-05 13:43:51 -0800274our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700275 printk(?:_ratelimited|_once|)|
276 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
277 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700278 panic|
279 MODULE_[A-Z_]+
Joe Perches691e6692010-03-05 13:43:51 -0800280)};
281
Joe Perches20112472011-07-25 17:13:23 -0700282our $signature_tags = qr{(?xi:
283 Signed-off-by:|
284 Acked-by:|
285 Tested-by:|
286 Reviewed-by:|
287 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700288 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700289 To:|
290 Cc:
291)};
292
Andy Whitcroft8905a672007-11-28 16:21:06 -0800293our @typeList = (
294 qr{void},
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700295 qr{(?:unsigned\s+)?char},
296 qr{(?:unsigned\s+)?short},
297 qr{(?:unsigned\s+)?int},
298 qr{(?:unsigned\s+)?long},
299 qr{(?:unsigned\s+)?long\s+int},
300 qr{(?:unsigned\s+)?long\s+long},
301 qr{(?:unsigned\s+)?long\s+long\s+int},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800302 qr{unsigned},
303 qr{float},
304 qr{double},
305 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800306 qr{struct\s+$Ident},
307 qr{union\s+$Ident},
308 qr{enum\s+$Ident},
309 qr{${Ident}_t},
310 qr{${Ident}_handler},
311 qr{${Ident}_handler_fn},
312);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700313our @modifierList = (
314 qr{fastcall},
315);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800316
Wolfram Sang7840a942010-08-09 17:20:57 -0700317our $allowed_asm_includes = qr{(?x:
318 irq|
319 memory
320)};
321# memory.h: ARM has a custom one
322
Andy Whitcroft8905a672007-11-28 16:21:06 -0800323sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700324 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
325 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700326 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800327 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700328 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800329 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800330 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700331 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700332 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800333 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700334 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800335 }x;
336 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700337 $NonptrType
Andy Whitcroftb337d8b2012-03-23 15:02:18 -0700338 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700339 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800340 }x;
341 $Declare = qr{(?:$Storage\s+)?$Type};
342}
343build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700344
Joe Perches7d2367a2011-07-25 17:13:22 -0700345
346our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700347
348# Using $balanced_parens, $LvalOrFunc, or $FuncArg
349# requires at least perl version v5.10.0
350# Any use must be runtime checked with $^V
351
352our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
353our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800354our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700355
356sub deparenthesize {
357 my ($string) = @_;
358 return "" if (!defined($string));
359 $string =~ s@^\s*\(\s*@@g;
360 $string =~ s@\s*\)\s*$@@g;
361 $string =~ s@\s+@ @g;
362 return $string;
363}
364
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700365$chk_signoff = 0 if ($file);
366
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700367my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800368my @lines = ();
369my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700370for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800371 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700372 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800373 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700374 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800375 } elsif ($filename eq '-') {
376 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700377 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800378 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700379 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700380 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800381 if ($filename eq '-') {
382 $vname = 'Your patch';
383 } else {
384 $vname = $filename;
385 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800386 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700387 chomp;
388 push(@rawlines, $_);
389 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800390 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800391 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392 $exit = 1;
393 }
394 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800395 @lines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700396}
397
398exit($exit);
399
400sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700401 my ($root) = @_;
402
403 my @tree_check = (
404 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
405 "README", "Documentation", "arch", "include", "drivers",
406 "fs", "init", "ipc", "kernel", "lib", "scripts",
407 );
408
409 foreach my $check (@tree_check) {
410 if (! -e $root . '/' . $check) {
411 return 0;
412 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700413 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700414 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700415}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700416
Joe Perches20112472011-07-25 17:13:23 -0700417sub parse_email {
418 my ($formatted_email) = @_;
419
420 my $name = "";
421 my $address = "";
422 my $comment = "";
423
424 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
425 $name = $1;
426 $address = $2;
427 $comment = $3 if defined $3;
428 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
429 $address = $1;
430 $comment = $2 if defined $2;
431 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
432 $address = $1;
433 $comment = $2 if defined $2;
434 $formatted_email =~ s/$address.*$//;
435 $name = $formatted_email;
436 $name =~ s/^\s+|\s+$//g;
437 $name =~ s/^\"|\"$//g;
438 # If there's a name left after stripping spaces and
439 # leading quotes, and the address doesn't have both
440 # leading and trailing angle brackets, the address
441 # is invalid. ie:
442 # "joe smith joe@smith.com" bad
443 # "joe smith <joe@smith.com" bad
444 if ($name ne "" && $address !~ /^<[^>]+>$/) {
445 $name = "";
446 $address = "";
447 $comment = "";
448 }
449 }
450
451 $name =~ s/^\s+|\s+$//g;
452 $name =~ s/^\"|\"$//g;
453 $address =~ s/^\s+|\s+$//g;
454 $address =~ s/^\<|\>$//g;
455
456 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
457 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
458 $name = "\"$name\"";
459 }
460
461 return ($name, $address, $comment);
462}
463
464sub format_email {
465 my ($name, $address) = @_;
466
467 my $formatted_email;
468
469 $name =~ s/^\s+|\s+$//g;
470 $name =~ s/^\"|\"$//g;
471 $address =~ s/^\s+|\s+$//g;
472
473 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
474 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
475 $name = "\"$name\"";
476 }
477
478 if ("$name" eq "") {
479 $formatted_email = "$address";
480 } else {
481 $formatted_email = "$name <$address>";
482 }
483
484 return $formatted_email;
485}
486
Joe Perches000d1cc12011-07-25 17:13:25 -0700487sub which_conf {
488 my ($conf) = @_;
489
490 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
491 if (-e "$path/$conf") {
492 return "$path/$conf";
493 }
494 }
495
496 return "";
497}
498
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700499sub expand_tabs {
500 my ($str) = @_;
501
502 my $res = '';
503 my $n = 0;
504 for my $c (split(//, $str)) {
505 if ($c eq "\t") {
506 $res .= ' ';
507 $n++;
508 for (; ($n % 8) != 0; $n++) {
509 $res .= ' ';
510 }
511 next;
512 }
513 $res .= $c;
514 $n++;
515 }
516
517 return $res;
518}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700519sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700520 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700521 return $res;
522}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700523
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700524sub line_stats {
525 my ($line) = @_;
526
527 # Drop the diff line leader and expand tabs
528 $line =~ s/^.//;
529 $line = expand_tabs($line);
530
531 # Pick the indent from the front of the line.
532 my ($white) = ($line =~ /^(\s*)/);
533
534 return (length($line), length($white));
535}
536
Andy Whitcroft773647a2008-03-28 14:15:58 -0700537my $sanitise_quote = '';
538
539sub sanitise_line_reset {
540 my ($in_comment) = @_;
541
542 if ($in_comment) {
543 $sanitise_quote = '*/';
544 } else {
545 $sanitise_quote = '';
546 }
547}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700548sub sanitise_line {
549 my ($line) = @_;
550
551 my $res = '';
552 my $l = '';
553
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800554 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700555 my $off = 0;
556 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700557
Andy Whitcroft773647a2008-03-28 14:15:58 -0700558 # Always copy over the diff marker.
559 $res = substr($line, 0, 1);
560
561 for ($off = 1; $off < length($line); $off++) {
562 $c = substr($line, $off, 1);
563
564 # Comments we are wacking completly including the begin
565 # and end, all to $;.
566 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
567 $sanitise_quote = '*/';
568
569 substr($res, $off, 2, "$;$;");
570 $off++;
571 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800572 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700573 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700574 $sanitise_quote = '';
575 substr($res, $off, 2, "$;$;");
576 $off++;
577 next;
578 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700579 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
580 $sanitise_quote = '//';
581
582 substr($res, $off, 2, $sanitise_quote);
583 $off++;
584 next;
585 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700586
587 # A \ in a string means ignore the next character.
588 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
589 $c eq "\\") {
590 substr($res, $off, 2, 'XX');
591 $off++;
592 next;
593 }
594 # Regular quotes.
595 if ($c eq "'" || $c eq '"') {
596 if ($sanitise_quote eq '') {
597 $sanitise_quote = $c;
598
599 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700600 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700601 } elsif ($sanitise_quote eq $c) {
602 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700603 }
604 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700605
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800606 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700607 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
608 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700609 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
610 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700611 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
612 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700613 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700614 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700615 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800616 }
617
Daniel Walker113f04a2009-09-21 17:04:35 -0700618 if ($sanitise_quote eq '//') {
619 $sanitise_quote = '';
620 }
621
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800622 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700623 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800624 my $clean = 'X' x length($1);
625 $res =~ s@\<.*\>@<$clean>@;
626
627 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700628 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800629 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700630 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800631 }
632
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700633 return $res;
634}
635
Joe Perchesa6962d72013-04-29 16:18:13 -0700636sub get_quoted_string {
637 my ($line, $rawline) = @_;
638
639 return "" if ($line !~ m/(\"[X]+\")/g);
640 return substr($rawline, $-[0], $+[0] - $-[0]);
641}
642
Andy Whitcroft8905a672007-11-28 16:21:06 -0800643sub ctx_statement_block {
644 my ($linenr, $remain, $off) = @_;
645 my $line = $linenr - 1;
646 my $blk = '';
647 my $soff = $off;
648 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700649 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800650
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800651 my $loff = 0;
652
Andy Whitcroft8905a672007-11-28 16:21:06 -0800653 my $type = '';
654 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800655 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800656 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800657 my $c;
658 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800659
660 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800661 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800662 @stack = (['', 0]) if ($#stack == -1);
663
Andy Whitcroft773647a2008-03-28 14:15:58 -0700664 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800665 # If we are about to drop off the end, pull in more
666 # context.
667 if ($off >= $len) {
668 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700669 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800670 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800671 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800672 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800673 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800674 $len = length($blk);
675 $line++;
676 last;
677 }
678 # Bail if there is no further context.
679 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800680 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800681 last;
682 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800683 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
684 $level++;
685 $type = '#';
686 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800687 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800688 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800689 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800690 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800691
Andy Whitcroft773647a2008-03-28 14:15:58 -0700692 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800693
694 # Handle nested #if/#else.
695 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
696 push(@stack, [ $type, $level ]);
697 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
698 ($type, $level) = @{$stack[$#stack - 1]};
699 } elsif ($remainder =~ /^#\s*endif\b/) {
700 ($type, $level) = @{pop(@stack)};
701 }
702
Andy Whitcroft8905a672007-11-28 16:21:06 -0800703 # Statement ends at the ';' or a close '}' at the
704 # outermost level.
705 if ($level == 0 && $c eq ';') {
706 last;
707 }
708
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800709 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700710 if ($level == 0 && $coff_set == 0 &&
711 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
712 $remainder =~ /^(else)(?:\s|{)/ &&
713 $remainder !~ /^else\s+if\b/) {
714 $coff = $off + length($1) - 1;
715 $coff_set = 1;
716 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
717 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800718 }
719
Andy Whitcroft8905a672007-11-28 16:21:06 -0800720 if (($type eq '' || $type eq '(') && $c eq '(') {
721 $level++;
722 $type = '(';
723 }
724 if ($type eq '(' && $c eq ')') {
725 $level--;
726 $type = ($level != 0)? '(' : '';
727
728 if ($level == 0 && $coff < $soff) {
729 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700730 $coff_set = 1;
731 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800732 }
733 }
734 if (($type eq '' || $type eq '{') && $c eq '{') {
735 $level++;
736 $type = '{';
737 }
738 if ($type eq '{' && $c eq '}') {
739 $level--;
740 $type = ($level != 0)? '{' : '';
741
742 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -0700743 if (substr($blk, $off + 1, 1) eq ';') {
744 $off++;
745 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800746 last;
747 }
748 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800749 # Preprocessor commands end at the newline unless escaped.
750 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
751 $level--;
752 $type = '';
753 $off++;
754 last;
755 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800756 $off++;
757 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700758 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800759 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -0700760 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800761 $line++;
762 $remain--;
763 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800764
765 my $statement = substr($blk, $soff, $off - $soff + 1);
766 my $condition = substr($blk, $soff, $coff - $soff + 1);
767
768 #warn "STATEMENT<$statement>\n";
769 #warn "CONDITION<$condition>\n";
770
Andy Whitcroft773647a2008-03-28 14:15:58 -0700771 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800772
773 return ($statement, $condition,
774 $line, $remain + 1, $off - $loff + 1, $level);
775}
776
Andy Whitcroftcf655042008-03-04 14:28:20 -0800777sub statement_lines {
778 my ($stmt) = @_;
779
780 # Strip the diff line prefixes and rip blank lines at start and end.
781 $stmt =~ s/(^|\n)./$1/g;
782 $stmt =~ s/^\s*//;
783 $stmt =~ s/\s*$//;
784
785 my @stmt_lines = ($stmt =~ /\n/g);
786
787 return $#stmt_lines + 2;
788}
789
790sub statement_rawlines {
791 my ($stmt) = @_;
792
793 my @stmt_lines = ($stmt =~ /\n/g);
794
795 return $#stmt_lines + 2;
796}
797
798sub statement_block_size {
799 my ($stmt) = @_;
800
801 $stmt =~ s/(^|\n)./$1/g;
802 $stmt =~ s/^\s*{//;
803 $stmt =~ s/}\s*$//;
804 $stmt =~ s/^\s*//;
805 $stmt =~ s/\s*$//;
806
807 my @stmt_lines = ($stmt =~ /\n/g);
808 my @stmt_statements = ($stmt =~ /;/g);
809
810 my $stmt_lines = $#stmt_lines + 2;
811 my $stmt_statements = $#stmt_statements + 1;
812
813 if ($stmt_lines > $stmt_statements) {
814 return $stmt_lines;
815 } else {
816 return $stmt_statements;
817 }
818}
819
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800820sub ctx_statement_full {
821 my ($linenr, $remain, $off) = @_;
822 my ($statement, $condition, $level);
823
824 my (@chunks);
825
Andy Whitcroftcf655042008-03-04 14:28:20 -0800826 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800827 ($statement, $condition, $linenr, $remain, $off, $level) =
828 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700829 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800830 push(@chunks, [ $condition, $statement ]);
831 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
832 return ($level, $linenr, @chunks);
833 }
834
835 # Pull in the following conditional/block pairs and see if they
836 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800837 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800838 ($statement, $condition, $linenr, $remain, $off, $level) =
839 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800840 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700841 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800842 #print "C: push\n";
843 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800844 }
845
846 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800847}
848
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700849sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700850 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700851 my $line;
852 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700853 my $blk = '';
854 my @o;
855 my @c;
856 my @res = ();
857
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700858 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800859 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700860 for ($line = $start; $remain > 0; $line++) {
861 next if ($rawlines[$line] =~ /^-/);
862 $remain--;
863
864 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800865
866 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -0700867 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800868 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -0700869 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800870 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -0700871 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800872 $level = pop(@stack);
873 }
874
Andy Whitcroft01464f32010-10-26 14:23:19 -0700875 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700876 ##print "C<$c>L<$level><$open$close>O<$off>\n";
877 if ($off > 0) {
878 $off--;
879 next;
880 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700881
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700882 if ($c eq $close && $level > 0) {
883 $level--;
884 last if ($level == 0);
885 } elsif ($c eq $open) {
886 $level++;
887 }
888 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700889
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700890 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700891 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700892 }
893
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700894 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700895 }
896
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700897 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700898}
899sub ctx_block_outer {
900 my ($linenr, $remain) = @_;
901
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700902 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
903 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700904}
905sub ctx_block {
906 my ($linenr, $remain) = @_;
907
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700908 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
909 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700910}
911sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700912 my ($linenr, $remain, $off) = @_;
913
914 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
915 return @r;
916}
917sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700918 my ($linenr, $remain) = @_;
919
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700920 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700921}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700922sub ctx_statement_level {
923 my ($linenr, $remain, $off) = @_;
924
925 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
926}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700927
928sub ctx_locate_comment {
929 my ($first_line, $end_line) = @_;
930
931 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -0700932 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700933 return $current_comment if (defined $current_comment);
934
935 # Look through the context and try and figure out if there is a
936 # comment.
937 my $in_comment = 0;
938 $current_comment = '';
939 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700940 my $line = $rawlines[$linenr - 1];
941 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700942 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
943 $in_comment = 1;
944 }
945 if ($line =~ m@/\*@) {
946 $in_comment = 1;
947 }
948 if (!$in_comment && $current_comment ne '') {
949 $current_comment = '';
950 }
951 $current_comment .= $line . "\n" if ($in_comment);
952 if ($line =~ m@\*/@) {
953 $in_comment = 0;
954 }
955 }
956
957 chomp($current_comment);
958 return($current_comment);
959}
960sub ctx_has_comment {
961 my ($first_line, $end_line) = @_;
962 my $cmt = ctx_locate_comment($first_line, $end_line);
963
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700964 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700965 ##print "CMMT: $cmt\n";
966
967 return ($cmt ne '');
968}
969
Andy Whitcroft4d001e42008-10-15 22:02:21 -0700970sub raw_line {
971 my ($linenr, $cnt) = @_;
972
973 my $offset = $linenr - 1;
974 $cnt++;
975
976 my $line;
977 while ($cnt) {
978 $line = $rawlines[$offset++];
979 next if (defined($line) && $line =~ /^-/);
980 $cnt--;
981 }
982
983 return $line;
984}
985
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700986sub cat_vet {
987 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700988 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700989
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700990 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700991 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
992 $res .= $1;
993 if ($2 ne '') {
994 $coded = sprintf("^%c", unpack('C', $2) + 64);
995 $res .= $coded;
996 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700997 }
998 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700999
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001000 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001001}
1002
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001003my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001004my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001005my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001006my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001007
1008sub annotate_reset {
1009 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001010 $av_pending = '_';
1011 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001012 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001013}
1014
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001015sub annotate_values {
1016 my ($stream, $type) = @_;
1017
1018 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001019 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001020 my $cur = $stream;
1021
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001022 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001023
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001025 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001026 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001027 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001028 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001029 print "WS($1)\n" if ($dbg_values > 1);
1030 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001031 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001032 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001033 }
1034
Florian Micklerc023e4732011-01-12 16:59:58 -08001035 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001036 print "CAST($1)\n" if ($dbg_values > 1);
1037 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001038 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001039
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001040 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001041 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001042 $type = 'T';
1043
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001044 } elsif ($cur =~ /^($Modifier)\s*/) {
1045 print "MODIFIER($1)\n" if ($dbg_values > 1);
1046 $type = 'T';
1047
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001048 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001049 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001050 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001051 push(@av_paren_type, $type);
1052 if ($2 ne '') {
1053 $av_pending = 'N';
1054 }
1055 $type = 'E';
1056
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001057 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001058 print "UNDEF($1)\n" if ($dbg_values > 1);
1059 $av_preprocessor = 1;
1060 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001061
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001062 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001063 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001064 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001065
1066 push(@av_paren_type, $type);
1067 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001068 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001069
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001070 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001071 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1072 $av_preprocessor = 1;
1073
1074 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1075
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001076 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001077
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001078 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001079 print "PRE_END($1)\n" if ($dbg_values > 1);
1080
1081 $av_preprocessor = 1;
1082
1083 # Assume all arms of the conditional end as this
1084 # one does, and continue as if the #endif was not here.
1085 pop(@av_paren_type);
1086 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001087 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001088
1089 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001090 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001091
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001092 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1093 print "ATTR($1)\n" if ($dbg_values > 1);
1094 $av_pending = $type;
1095 $type = 'N';
1096
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001097 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001098 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001099 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001100 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001101 }
1102 $type = 'N';
1103
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001104 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001105 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001106 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001107 $type = 'N';
1108
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001109 } elsif ($cur =~/^(case)/o) {
1110 print "CASE($1)\n" if ($dbg_values > 1);
1111 $av_pend_colon = 'C';
1112 $type = 'N';
1113
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001114 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001115 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001116 $type = 'N';
1117
1118 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001119 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001120 push(@av_paren_type, $av_pending);
1121 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001122 $type = 'N';
1123
1124 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001125 my $new_type = pop(@av_paren_type);
1126 if ($new_type ne '_') {
1127 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001128 print "PAREN('$1') -> $type\n"
1129 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001130 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001131 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001132 }
1133
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001134 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001135 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001136 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001137 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001138
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001139 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1140 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001141 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001142 } elsif ($type eq 'E') {
1143 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001144 }
1145 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1146 $type = 'V';
1147
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001148 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001149 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001150 $type = 'V';
1151
1152 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001153 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001154 $type = 'N';
1155
Andy Whitcroftcf655042008-03-04 14:28:20 -08001156 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001157 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001158 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001159 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001160
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001161 } elsif ($cur =~/^(,)/) {
1162 print "COMMA($1)\n" if ($dbg_values > 1);
1163 $type = 'C';
1164
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001165 } elsif ($cur =~ /^(\?)/o) {
1166 print "QUESTION($1)\n" if ($dbg_values > 1);
1167 $type = 'N';
1168
1169 } elsif ($cur =~ /^(:)/o) {
1170 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1171
1172 substr($var, length($res), 1, $av_pend_colon);
1173 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1174 $type = 'E';
1175 } else {
1176 $type = 'N';
1177 }
1178 $av_pend_colon = 'O';
1179
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001180 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001181 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001182 $type = 'N';
1183
Andy Whitcroft0d413862008-10-15 22:02:16 -07001184 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001185 my $variant;
1186
1187 print "OPV($1)\n" if ($dbg_values > 1);
1188 if ($type eq 'V') {
1189 $variant = 'B';
1190 } else {
1191 $variant = 'U';
1192 }
1193
1194 substr($var, length($res), 1, $variant);
1195 $type = 'N';
1196
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001197 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001198 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001199 if ($1 ne '++' && $1 ne '--') {
1200 $type = 'N';
1201 }
1202
1203 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001204 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001205 }
1206 if (defined $1) {
1207 $cur = substr($cur, length($1));
1208 $res .= $type x length($1);
1209 }
1210 }
1211
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001212 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001213}
1214
Andy Whitcroft8905a672007-11-28 16:21:06 -08001215sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001216 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001217 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001218 ^(?:
1219 $Modifier|
1220 $Storage|
1221 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001222 DEFINE_\S+
1223 )$|
1224 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001225 goto|
1226 return|
1227 case|
1228 else|
1229 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001230 do|
1231 \#|
1232 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001233 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001234 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001235 )}x;
1236 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1237 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001238 # Check for modifiers.
1239 $possible =~ s/\s*$Storage\s*//g;
1240 $possible =~ s/\s*$Sparse\s*//g;
1241 if ($possible =~ /^\s*$/) {
1242
1243 } elsif ($possible =~ /\s/) {
1244 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001245 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001246 if ($modifier !~ $notPermitted) {
1247 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1248 push(@modifierList, $modifier);
1249 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001250 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001251
1252 } else {
1253 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1254 push(@typeList, $possible);
1255 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001256 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001257 } else {
1258 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001259 }
1260}
1261
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001262my $prefix = '';
1263
Joe Perches000d1cc12011-07-25 17:13:25 -07001264sub show_type {
1265 return !defined $ignore_type{$_[0]};
1266}
1267
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001268sub report {
Joe Perches000d1cc12011-07-25 17:13:25 -07001269 if (!show_type($_[1]) ||
1270 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001271 return 0;
1272 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001273 my $line;
1274 if ($show_types) {
1275 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1276 } else {
1277 $line = "$prefix$_[0]: $_[2]\n";
1278 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001279 $line = (split('\n', $line))[0] . "\n" if ($terse);
1280
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001281 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001282
1283 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001284}
1285sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001286 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001287}
Joe Perches000d1cc12011-07-25 17:13:25 -07001288
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001289sub ERROR {
Joe Perches000d1cc12011-07-25 17:13:25 -07001290 if (report("ERROR", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001291 our $clean = 0;
1292 our $cnt_error++;
1293 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001294}
1295sub WARN {
Joe Perches000d1cc12011-07-25 17:13:25 -07001296 if (report("WARNING", $_[0], $_[1])) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001297 our $clean = 0;
1298 our $cnt_warn++;
1299 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001300}
1301sub CHK {
Joe Perches000d1cc12011-07-25 17:13:25 -07001302 if ($check && report("CHECK", $_[0], $_[1])) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001303 our $clean = 0;
1304 our $cnt_chk++;
1305 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001306}
1307
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001308sub check_absolute_file {
1309 my ($absolute, $herecurr) = @_;
1310 my $file = $absolute;
1311
1312 ##print "absolute<$absolute>\n";
1313
1314 # See if any suffix of this path is a path within the tree.
1315 while ($file =~ s@^[^/]*/@@) {
1316 if (-f "$root/$file") {
1317 ##print "file<$file>\n";
1318 last;
1319 }
1320 }
1321 if (! -f _) {
1322 return 0;
1323 }
1324
1325 # It is, so see if the prefix is acceptable.
1326 my $prefix = $absolute;
1327 substr($prefix, -length($file)) = '';
1328
1329 ##print "prefix<$prefix>\n";
1330 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001331 WARN("USE_RELATIVE_PATH",
1332 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001333 }
1334}
1335
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001336sub pos_last_openparen {
1337 my ($line) = @_;
1338
1339 my $pos = 0;
1340
1341 my $opens = $line =~ tr/\(/\(/;
1342 my $closes = $line =~ tr/\)/\)/;
1343
1344 my $last_openparen = 0;
1345
1346 if (($opens == 0) || ($closes >= $opens)) {
1347 return -1;
1348 }
1349
1350 my $len = length($line);
1351
1352 for ($pos = 0; $pos < $len; $pos++) {
1353 my $string = substr($line, $pos);
1354 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1355 $pos += length($1) - 1;
1356 } elsif (substr($line, $pos, 1) eq '(') {
1357 $last_openparen = $pos;
1358 } elsif (index($string, '(') == -1) {
1359 last;
1360 }
1361 }
1362
1363 return $last_openparen + 1;
1364}
1365
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001366sub process {
1367 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001368
1369 my $linenr=0;
1370 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001371 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001372 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001373 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001374
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001375 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001376 my $indent;
1377 my $previndent=0;
1378 my $stashindent=0;
1379
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001380 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001381 my $signoff = 0;
1382 my $is_patch = 0;
1383
Joe Perches15662b32011-10-31 17:13:12 -07001384 my $in_header_lines = 1;
1385 my $in_commit_log = 0; #Scanning lines before patch
1386
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001387 my $non_utf8_charset = 0;
1388
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001389 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001390 our $cnt_lines = 0;
1391 our $cnt_error = 0;
1392 our $cnt_warn = 0;
1393 our $cnt_chk = 0;
1394
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001395 # Trace the real file/line as we go.
1396 my $realfile = '';
1397 my $realline = 0;
1398 my $realcnt = 0;
1399 my $here = '';
1400 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001401 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001402 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001403 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001404
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001405 my $prev_values = 'E';
1406
1407 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001408 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001409 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001410 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001411 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001412
Joe Perches323c1262012-12-17 16:02:07 -08001413 my %camelcase = ();
1414
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001415 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001416 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001417 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001418 my @setup_docs = ();
1419 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001420
1421 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001422 my $line;
1423 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001424 $linenr++;
1425 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001426
Andy Whitcroft773647a2008-03-28 14:15:58 -07001427 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001428 $setup_docs = 0;
1429 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1430 $setup_docs = 1;
1431 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001432 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001433 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001434 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1435 $realline=$1-1;
1436 if (defined $2) {
1437 $realcnt=$3+1;
1438 } else {
1439 $realcnt=1+1;
1440 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001441 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001442
1443 # Guestimate if this is a continuing comment. Run
1444 # the context looking for a comment "edge". If this
1445 # edge is a close comment then we must be in a comment
1446 # at context start.
1447 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001448 my $cnt = $realcnt;
1449 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1450 next if (defined $rawlines[$ln - 1] &&
1451 $rawlines[$ln - 1] =~ /^-/);
1452 $cnt--;
1453 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001454 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001455 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1456 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1457 ($edge) = $1;
1458 last;
1459 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001460 }
1461 if (defined $edge && $edge eq '*/') {
1462 $in_comment = 1;
1463 }
1464
1465 # Guestimate if this is a continuing comment. If this
1466 # is the start of a diff block and this line starts
1467 # ' *' then it is very likely a comment.
1468 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001469 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001470 {
1471 $in_comment = 1;
1472 }
1473
1474 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1475 sanitise_line_reset($in_comment);
1476
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001477 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001478 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001479 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001480 $line = sanitise_line($rawline);
1481 }
1482 push(@lines, $line);
1483
1484 if ($realcnt > 1) {
1485 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1486 } else {
1487 $realcnt = 0;
1488 }
1489
1490 #print "==>$rawline\n";
1491 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001492
1493 if ($setup_docs && $line =~ /^\+/) {
1494 push(@setup_docs, $line);
1495 }
1496 }
1497
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001498 $prefix = '';
1499
Andy Whitcroft773647a2008-03-28 14:15:58 -07001500 $realcnt = 0;
1501 $linenr = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001502 foreach my $line (@lines) {
1503 $linenr++;
1504
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001505 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001506
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001507#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001508 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001509 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001510 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001511 $realline=$1-1;
1512 if (defined $2) {
1513 $realcnt=$3+1;
1514 } else {
1515 $realcnt=1+1;
1516 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001517 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001518 $prev_values = 'E';
1519
Andy Whitcroft773647a2008-03-28 14:15:58 -07001520 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001521 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001522 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001523 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001524 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001525
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001526# track the line number as we move through the hunk, note that
1527# new versions of GNU diff omit the leading space on completely
1528# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001529 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001530 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001531 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001532
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001533 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001534 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001535
1536 # Track the previous line.
1537 ($prevline, $stashline) = ($stashline, $line);
1538 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001539 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1540
Andy Whitcroft773647a2008-03-28 14:15:58 -07001541 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001542
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001543 } elsif ($realcnt == 1) {
1544 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001545 }
1546
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001547 my $hunk_line = ($realcnt != 0);
1548
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001549#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001550 $prefix = "$filename:$realline: " if ($emacs && $file);
1551 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1552
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001553 $here = "#$linenr: " if (!$file);
1554 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001555
1556 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001557 if ($line =~ /^diff --git.*?(\S+)$/) {
1558 $realfile = $1;
1559 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001560 $in_commit_log = 0;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001561 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001562 $realfile = $1;
Wolfram Sang1e855722009-01-06 14:41:24 -08001563 $realfile =~ s@^([^/]*)/@@;
Joe Perches270c49a2012-01-10 15:09:50 -08001564 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001565
1566 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08001567 if (!$file && $tree && $p1_prefix ne '' &&
1568 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001569 WARN("PATCH_PREFIX",
1570 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08001571 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001572
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07001573 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001574 ERROR("MODIFIED_INCLUDE_ASM",
1575 "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 -07001576 }
1577 next;
1578 }
1579
Randy Dunlap389834b2007-06-08 13:47:03 -07001580 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001581
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001582 my $hereline = "$here\n$rawline\n";
1583 my $herecurr = "$here\n$rawline\n";
1584 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001585
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001586 $cnt_lines++ if ($realcnt != 0);
1587
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001588# Check for incorrect file permissions
1589 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1590 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07001591 if ($realfile !~ m@scripts/@ &&
1592 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001593 ERROR("EXECUTE_PERMISSIONS",
1594 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001595 }
1596 }
1597
Joe Perches20112472011-07-25 17:13:23 -07001598# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001599 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001600 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07001601 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07001602 }
1603
1604# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08001605 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001606 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07001607 my $space_before = $1;
1608 my $sign_off = $2;
1609 my $space_after = $3;
1610 my $email = $4;
1611 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1612
Joe Perchesce0338df3c2012-07-30 14:41:18 -07001613 if ($sign_off !~ /$signature_tags/) {
1614 WARN("BAD_SIGN_OFF",
1615 "Non-standard signature: $sign_off\n" . $herecurr);
1616 }
Joe Perches20112472011-07-25 17:13:23 -07001617 if (defined $space_before && $space_before ne "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001618 WARN("BAD_SIGN_OFF",
1619 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001620 }
Joe Perches20112472011-07-25 17:13:23 -07001621 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001622 WARN("BAD_SIGN_OFF",
1623 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001624 }
1625 if (!defined $space_after || $space_after ne " ") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001626 WARN("BAD_SIGN_OFF",
1627 "Use a single space after $ucfirst_sign_off\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001628 }
1629
1630 my ($email_name, $email_address, $comment) = parse_email($email);
1631 my $suggested_email = format_email(($email_name, $email_address));
1632 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001633 ERROR("BAD_SIGN_OFF",
1634 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001635 } else {
1636 my $dequoted = $suggested_email;
1637 $dequoted =~ s/^"//;
1638 $dequoted =~ s/" </ </;
1639 # Don't force email to have quotes
1640 # Allow just an angle bracketed address
1641 if ("$dequoted$comment" ne $email &&
1642 "<$email_address>$comment" ne $email &&
1643 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001644 WARN("BAD_SIGN_OFF",
1645 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07001646 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001647 }
1648 }
1649
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001650# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001651 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001652 ERROR("CORRUPTED_PATCH",
1653 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001654 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001655 }
1656
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001657# Check for absolute kernel paths.
1658 if ($tree) {
1659 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1660 my $file = $1;
1661
1662 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1663 check_absolute_file($1, $herecurr)) {
1664 #
1665 } else {
1666 check_absolute_file($file, $herecurr);
1667 }
1668 }
1669 }
1670
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001671# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1672 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001673 $rawline !~ m/^$UTF8*$/) {
1674 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1675
1676 my $blank = copy_spacing($rawline);
1677 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1678 my $hereptr = "$hereline$ptr\n";
1679
Joe Perches34d99212011-07-25 17:13:26 -07001680 CHK("INVALID_UTF8",
1681 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001682 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001683
Joe Perches15662b32011-10-31 17:13:12 -07001684# Check if it's the start of a commit log
1685# (not a header line and we haven't seen the patch filename)
1686 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches270c49a2012-01-10 15:09:50 -08001687 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
Joe Perches15662b32011-10-31 17:13:12 -07001688 $in_header_lines = 0;
1689 $in_commit_log = 1;
1690 }
1691
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001692# Check if there is UTF-8 in a commit log when a mail header has explicitly
1693# declined it, i.e defined some charset where it is missing.
1694 if ($in_header_lines &&
1695 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1696 $1 !~ /utf-8/i) {
1697 $non_utf8_charset = 1;
1698 }
1699
1700 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07001701 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001702 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07001703 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1704 }
1705
Andy Whitcroft306708542008-10-15 22:02:28 -07001706# ignore non-hunk lines and lines being removed
1707 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001708
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001709#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001710 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001711 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001712 ERROR("DOS_LINE_ENDINGS",
1713 "DOS line endings\n" . $herevet);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001714
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001715 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1716 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001717 ERROR("TRAILING_WHITESPACE",
1718 "trailing whitespace\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001719 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001720 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07001721
Andi Kleen33549572010-05-24 14:33:29 -07001722# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001723# Only applies when adding the entry originally, after that we do not have
1724# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07001725 if ($realfile =~ /Kconfig/ &&
Andy Whitcrofta1385802012-01-10 15:10:03 -08001726 $line =~ /.\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07001727 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001728 my $cnt = $realcnt;
1729 my $ln = $linenr + 1;
1730 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001731 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001732 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08001733 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001734 $f = $lines[$ln - 1];
1735 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1736 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001737
1738 next if ($f =~ /^-/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08001739
1740 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1741 $is_start = 1;
1742 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1743 $length = -1;
1744 }
1745
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001746 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07001747 $f =~ s/#.*//;
1748 $f =~ s/^\s+//;
1749 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07001750 if ($f =~ /^\s*config\s/) {
1751 $is_end = 1;
1752 last;
1753 }
Andi Kleen33549572010-05-24 14:33:29 -07001754 $length++;
1755 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001756 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08001757 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1758 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07001759 }
1760
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001761# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1762 if ($realfile =~ /Kconfig/ &&
1763 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1764 WARN("CONFIG_EXPERIMENTAL",
1765 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1766 }
1767
Arnaud Lacombec68e5872011-08-15 01:07:14 -04001768 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1769 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1770 my $flag = $1;
1771 my $replacement = {
1772 'EXTRA_AFLAGS' => 'asflags-y',
1773 'EXTRA_CFLAGS' => 'ccflags-y',
1774 'EXTRA_CPPFLAGS' => 'cppflags-y',
1775 'EXTRA_LDFLAGS' => 'ldflags-y',
1776 };
1777
1778 WARN("DEPRECATED_VARIABLE",
1779 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1780 }
1781
Andy Whitcroft5368df202008-10-15 22:02:27 -07001782# check we are in a valid source file if not then ignore this hunk
1783 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1784
Joe Perches6cd7f382012-12-17 16:01:54 -08001785#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001786 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07001787 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07001788 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07001789 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08001790 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001791 {
Joe Perches000d1cc12011-07-25 17:13:25 -07001792 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08001793 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001794 }
1795
Josh Triplettca56dc02012-03-23 15:02:21 -07001796# Check for user-visible strings broken across lines, which breaks the ability
1797# to grep for the string. Limited to strings used as parameters (those
1798# following an open parenthesis), which almost completely eliminates false
1799# positives, as well as warning only once per parameter rather than once per
1800# line of the string. Make an exception when the previous string ends in a
1801# newline (multiple lines in one string constant) or \n\t (common in inline
1802# assembly to indent the instruction on the following line).
1803 if ($line =~ /^\+\s*"/ &&
1804 $prevline =~ /"\s*$/ &&
1805 $prevline =~ /\(/ &&
1806 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1807 WARN("SPLIT_STRING",
1808 "quoted string split across lines\n" . $hereprev);
1809 }
1810
Joe Perches5e79d962010-03-05 13:43:55 -08001811# check for spaces before a quoted newline
1812 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001813 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1814 "unnecessary whitespace before a quoted newline\n" . $herecurr);
Joe Perches5e79d962010-03-05 13:43:55 -08001815 }
1816
Andy Whitcroft8905a672007-11-28 16:21:06 -08001817# check for adding lines without a newline.
1818 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001819 WARN("MISSING_EOF_NEWLINE",
1820 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001821 }
1822
Mike Frysinger42e41c52009-09-21 17:04:40 -07001823# Blackfin: use hi/lo macros
1824 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1825 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1826 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001827 ERROR("LO_MACRO",
1828 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001829 }
1830 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1831 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001832 ERROR("HI_MACRO",
1833 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001834 }
1835 }
1836
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001837# check we are in a valid source file C or perl if not then ignore this hunk
1838 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001839
1840# at the beginning of a line any tabs must come first and anything
1841# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001842 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1843 $rawline =~ /^\+\s* \s*/) {
1844 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001845 ERROR("CODE_INDENT",
1846 "code indent should use tabs where possible\n" . $herevet);
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07001847 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001848 }
1849
Alberto Panizzo08e44362010-03-05 13:43:54 -08001850# check for space before tabs.
1851 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1852 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001853 WARN("SPACE_BEFORE_TAB",
1854 "please, no space before tabs\n" . $herevet);
Alberto Panizzo08e44362010-03-05 13:43:54 -08001855 }
1856
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001857# check for && or || at the start of a line
1858 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1859 CHK("LOGICAL_CONTINUATIONS",
1860 "Logical continuations should be on the previous line\n" . $hereprev);
1861 }
1862
1863# check multi-line statement indentation matches previous line
1864 if ($^V && $^V ge 5.10.0 &&
1865 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1866 $prevline =~ /^\+(\t*)(.*)$/;
1867 my $oldindent = $1;
1868 my $rest = $2;
1869
1870 my $pos = pos_last_openparen($rest);
1871 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07001872 $line =~ /^(\+| )([ \t]*)/;
1873 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001874
1875 my $goodtabindent = $oldindent .
1876 "\t" x ($pos / 8) .
1877 " " x ($pos % 8);
1878 my $goodspaceindent = $oldindent . " " x $pos;
1879
1880 if ($newindent ne $goodtabindent &&
1881 $newindent ne $goodspaceindent) {
1882 CHK("PARENTHESIS_ALIGNMENT",
1883 "Alignment should match open parenthesis\n" . $hereprev);
1884 }
1885 }
1886 }
1887
Joe Perchesaad4f612012-03-23 15:02:19 -07001888 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1889 CHK("SPACING",
1890 "No space is necessary after a cast\n" . $hereprev);
1891 }
1892
Joe Perches05880602012-10-04 17:13:35 -07001893 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1894 $rawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1895 $prevrawline =~ /^\+[ \t]*$/) {
1896 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1897 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1898 }
1899
1900 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08001901 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1902 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1903 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1904 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07001905 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1906 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
1907 }
1908
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001909# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07001910# Exceptions:
1911# 1) within comments
1912# 2) indented preprocessor commands
1913# 3) hanging labels
1914 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001915 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001916 WARN("LEADING_SPACE",
1917 "please, no spaces at the start of a line\n" . $herevet);
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07001918 }
1919
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07001920# check we are in a valid C source file if not then ignore this hunk
1921 next if ($realfile !~ /\.(h|c)$/);
1922
Kees Cook1ba8dfd2012-12-17 16:01:48 -08001923# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
1924 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
1925 WARN("CONFIG_EXPERIMENTAL",
1926 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1927 }
1928
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001929# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001930 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07001931 WARN("CVS_KEYWORD",
1932 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001933 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001934
Mike Frysinger42e41c52009-09-21 17:04:40 -07001935# Blackfin: don't use __builtin_bfin_[cs]sync
1936 if ($line =~ /__builtin_bfin_csync/) {
1937 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001938 ERROR("CSYNC",
1939 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001940 }
1941 if ($line =~ /__builtin_bfin_ssync/) {
1942 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001943 ERROR("SSYNC",
1944 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07001945 }
1946
Joe Perches56e77d72013-02-21 16:44:14 -08001947# check for old HOTPLUG __dev<foo> section markings
1948 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
1949 WARN("HOTPLUG_SECTION",
1950 "Using $1 is unnecessary\n" . $herecurr);
1951 }
1952
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001953# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001954 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1955 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001956#print "LINE<$line>\n";
1957 if ($linenr >= $suppress_statement &&
1958 $realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001959 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07001960 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001961 $stat =~ s/\n./\n /g;
1962 $cond =~ s/\n./\n /g;
1963
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001964#print "linenr<$linenr> <$stat>\n";
1965 # If this statement has no statement boundaries within
1966 # it there is no point in retrying a statement scan
1967 # until we hit end of it.
1968 my $frag = $stat; $frag =~ s/;+\s*$//;
1969 if ($frag !~ /(?:{|;)/) {
1970#print "skip<$line_nr_next>\n";
1971 $suppress_statement = $line_nr_next;
1972 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001973
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001974 # Find the real next line.
1975 $realline_next = $line_nr_next;
1976 if (defined $realline_next &&
1977 (!defined $lines[$realline_next - 1] ||
1978 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1979 $realline_next++;
1980 }
1981
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001982 my $s = $stat;
1983 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001984
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001985 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001986 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001987
1988 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001989 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001990
Andy Whitcroft463f2862009-09-21 17:04:34 -07001991 } elsif ($s =~ /^.\s*else\b/s) {
1992
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001993 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07001994 } 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 -07001995 my $type = $1;
1996 $type =~ s/\s+/ /g;
1997 possible($type, "A:" . $s);
1998
Andy Whitcroft8905a672007-11-28 16:21:06 -08001999 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002000 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002001 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002002 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002003
2004 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002005 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002006 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002007 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002008
2009 # Check for any sort of function declaration.
2010 # int foo(something bar, other baz);
2011 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002012 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 -08002013 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002014
Andy Whitcroftcf655042008-03-04 14:28:20 -08002015 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002016 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002017 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002018
Andy Whitcroft8905a672007-11-28 16:21:06 -08002019 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002020 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002021
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002022 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002023 }
2024 }
2025 }
2026
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002027 }
2028
Andy Whitcroft653d4872007-06-23 17:16:34 -07002029#
2030# Checks which may be anchored in the context.
2031#
2032
2033# Check for switch () and associated case and default
2034# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002035 if ($line=~/\bswitch\s*\(.*\)/) {
2036 my $err = '';
2037 my $sep = '';
2038 my @ctx = ctx_block_outer($linenr, $realcnt);
2039 shift(@ctx);
2040 for my $ctx (@ctx) {
2041 my ($clen, $cindent) = line_stats($ctx);
2042 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2043 $indent != $cindent) {
2044 $err .= "$sep$ctx\n";
2045 $sep = '';
2046 } else {
2047 $sep = "[...]\n";
2048 }
2049 }
2050 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002051 ERROR("SWITCH_CASE_INDENT_LEVEL",
2052 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002053 }
2054 }
2055
2056# if/while/etc brace do not go on next line, unless defining a do while loop,
2057# or if that brace on the next line is for something else
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002058 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002059 my $pre_ctx = "$1$2";
2060
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002061 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002062
2063 if ($line =~ /^\+\t{6,}/) {
2064 WARN("DEEP_INDENTATION",
2065 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2066 }
2067
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002068 my $ctx_cnt = $realcnt - $#ctx - 1;
2069 my $ctx = join("\n", @ctx);
2070
Andy Whitcroft548596d2008-07-23 21:29:01 -07002071 my $ctx_ln = $linenr;
2072 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002073
Andy Whitcroft548596d2008-07-23 21:29:01 -07002074 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2075 defined $lines[$ctx_ln - 1] &&
2076 $lines[$ctx_ln - 1] =~ /^-/)) {
2077 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2078 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002079 $ctx_ln++;
2080 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002081
Andy Whitcroft53210162008-07-23 21:29:03 -07002082 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2083 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002084
2085 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002086 ERROR("OPEN_BRACE",
2087 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002088 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002089 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002090 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2091 $ctx =~ /\)\s*\;\s*$/ &&
2092 defined $lines[$ctx_ln - 1])
2093 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002094 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2095 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002096 WARN("TRAILING_SEMICOLON",
2097 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002098 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002099 }
2100 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002101 }
2102
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002103# Check relative indent for conditionals and blocks.
2104 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002105 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2106 ctx_statement_block($linenr, $realcnt, 0)
2107 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002108 my ($s, $c) = ($stat, $cond);
2109
2110 substr($s, 0, length($c), '');
2111
2112 # Make sure we remove the line prefixes as we have
2113 # none on the first line, and are going to readd them
2114 # where necessary.
2115 $s =~ s/\n./\n/gs;
2116
2117 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002118 my @newlines = ($c =~ /\n/gs);
2119 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002120
2121 # We want to check the first line inside the block
2122 # starting at the end of the conditional, so remove:
2123 # 1) any blank line termination
2124 # 2) any opening brace { on end of the line
2125 # 3) any do (...) {
2126 my $continuation = 0;
2127 my $check = 0;
2128 $s =~ s/^.*\bdo\b//;
2129 $s =~ s/^\s*{//;
2130 if ($s =~ s/^\s*\\//) {
2131 $continuation = 1;
2132 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002133 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002134 $check = 1;
2135 $cond_lines++;
2136 }
2137
2138 # Also ignore a loop construct at the end of a
2139 # preprocessor statement.
2140 if (($prevline =~ /^.\s*#\s*define\s/ ||
2141 $prevline =~ /\\\s*$/) && $continuation == 0) {
2142 $check = 0;
2143 }
2144
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002145 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002146 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002147 while ($cond_ptr != $cond_lines) {
2148 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002149
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002150 # If we see an #else/#elif then the code
2151 # is not linear.
2152 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2153 $check = 0;
2154 }
2155
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002156 # Ignore:
2157 # 1) blank lines, they should be at 0,
2158 # 2) preprocessor lines, and
2159 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002160 if ($continuation ||
2161 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002162 $s =~ /^\s*#\s*?/ ||
2163 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002164 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002165 if ($s =~ s/^.*?\n//) {
2166 $cond_lines++;
2167 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002168 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002169 }
2170
2171 my (undef, $sindent) = line_stats("+" . $s);
2172 my $stat_real = raw_line($linenr, $cond_lines);
2173
2174 # Check if either of these lines are modified, else
2175 # this is not this patch's fault.
2176 if (!defined($stat_real) ||
2177 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2178 $check = 0;
2179 }
2180 if (defined($stat_real) && $cond_lines > 1) {
2181 $stat_real = "[...]\n$stat_real";
2182 }
2183
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002184 #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 -07002185
2186 if ($check && (($sindent % 8) != 0 ||
2187 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002188 WARN("SUSPECT_CODE_INDENT",
2189 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002190 }
2191 }
2192
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002193 # Track the 'values' across context and added lines.
2194 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002195 my ($curr_values, $curr_vars) =
2196 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002197 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002198 if ($dbg_values) {
2199 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002200 print "$linenr > .$outline\n";
2201 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002202 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002203 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002204 $prev_values = substr($curr_values, -1);
2205
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002206#ignore lines not being added
2207 if ($line=~/^[^\+]/) {next;}
2208
Andy Whitcroft653d4872007-06-23 17:16:34 -07002209# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002210 if ($dbg_type) {
2211 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002212 ERROR("TEST_TYPE",
2213 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002214 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002215 ERROR("TEST_NOT_TYPE",
2216 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002217 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002218 next;
2219 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002220# TEST: allow direct testing of the attribute matcher.
2221 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002222 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002223 ERROR("TEST_ATTR",
2224 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002225 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002226 ERROR("TEST_NOT_ATTR",
2227 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002228 }
2229 next;
2230 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002231
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002232# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002233 if ($line =~ /^.\s*{/ &&
2234 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002235 ERROR("OPEN_BRACE",
2236 "that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002237 }
2238
Andy Whitcroft653d4872007-06-23 17:16:34 -07002239#
2240# Checks which are anchored on the added line.
2241#
2242
2243# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002244 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002245 my $path = $1;
2246 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002247 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002248 "malformed #include filename\n" . $herecurr);
2249 }
2250 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2251 ERROR("UAPI_INCLUDE",
2252 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002253 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002254 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002255
2256# no C99 // comments
2257 if ($line =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002258 ERROR("C99_COMMENTS",
2259 "do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002260 }
2261 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002262 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002263 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002264
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002265# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2266# the whole statement.
2267#print "APW <$lines[$realline_next - 1]>\n";
2268 if (defined $realline_next &&
2269 exists $lines[$realline_next - 1] &&
2270 !defined $suppress_export{$realline_next} &&
2271 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2272 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002273 # Handle definitions which produce identifiers with
2274 # a prefix:
2275 # XXX(foo);
2276 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002277 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002278 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002279 $name =~ /^${Ident}_$2/) {
2280#print "FOO C name<$name>\n";
2281 $suppress_export{$realline_next} = 1;
2282
2283 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002284 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002285 ^.DEFINE_$Ident\(\Q$name\E\)|
2286 ^.DECLARE_$Ident\(\Q$name\E\)|
2287 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002288 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2289 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002290 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002291#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2292 $suppress_export{$realline_next} = 2;
2293 } else {
2294 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002295 }
2296 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002297 if (!defined $suppress_export{$linenr} &&
2298 $prevline =~ /^.\s*$/ &&
2299 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2300 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2301#print "FOO B <$lines[$linenr - 1]>\n";
2302 $suppress_export{$linenr} = 2;
2303 }
2304 if (defined $suppress_export{$linenr} &&
2305 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002306 WARN("EXPORT_SYMBOL",
2307 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002308 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002309
Joe Eloff5150bda2010-08-09 17:21:00 -07002310# check for global initialisers.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002311 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002312 ERROR("GLOBAL_INITIALISERS",
2313 "do not initialise globals to 0 or NULL\n" .
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002314 $herecurr);
2315 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002316# check for static initialisers.
Andy Whitcroft2d1bafd2009-01-06 14:41:28 -08002317 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002318 ERROR("INITIALISED_STATIC",
2319 "do not initialise statics to 0 or NULL\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002320 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002321 }
2322
Joe Perchescb710ec2010-10-26 14:23:20 -07002323# check for static const char * arrays.
2324 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002325 WARN("STATIC_CONST_CHAR_ARRAY",
2326 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002327 $herecurr);
2328 }
2329
2330# check for static char foo[] = "bar" declarations.
2331 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002332 WARN("STATIC_CONST_CHAR_ARRAY",
2333 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07002334 $herecurr);
2335 }
2336
Joe Perches93ed0e22010-10-26 14:23:21 -07002337# check for declarations of struct pci_device_id
2338 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002339 WARN("DEFINE_PCI_DEVICE_TABLE",
2340 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
Joe Perches93ed0e22010-10-26 14:23:21 -07002341 }
2342
Andy Whitcroft653d4872007-06-23 17:16:34 -07002343# check for new typedefs, only function parameters and sparse annotations
2344# make sense.
2345 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08002346 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002347 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07002348 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07002349 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002350 WARN("NEW_TYPEDEFS",
2351 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002352 }
2353
2354# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08002355 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002356 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2357 #print "AA<$1>\n";
2358 my ($from, $to) = ($2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002359
Andy Whitcroft65863862009-01-06 14:41:21 -08002360 # Should start with a space.
2361 $to =~ s/^(\S)/ $1/;
2362 # Should not end with a space.
2363 $to =~ s/\s+$//;
2364 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002365 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002366 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002367
Andy Whitcroft65863862009-01-06 14:41:21 -08002368 #print "from<$from> to<$to>\n";
2369 if ($from ne $to) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002370 ERROR("POINTER_LOCATION",
2371 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002372 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08002373 }
2374 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2375 #print "BB<$1>\n";
2376 my ($from, $to, $ident) = ($2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002377
Andy Whitcroft65863862009-01-06 14:41:21 -08002378 # Should start with a space.
2379 $to =~ s/^(\S)/ $1/;
2380 # Should not end with a space.
2381 $to =~ s/\s+$//;
2382 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08002383 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08002384 }
2385 # Modifiers should have spaces.
2386 $to =~ s/(\b$Modifier$)/$1 /;
2387
Andy Whitcroft667026e2009-02-27 14:03:08 -08002388 #print "from<$from> to<$to> ident<$ident>\n";
2389 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002390 ERROR("POINTER_LOCATION",
2391 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
Andy Whitcroft65863862009-01-06 14:41:21 -08002392 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002393 }
2394
2395# # no BUG() or BUG_ON()
2396# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2397# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2398# print "$herecurr";
2399# $clean = 0;
2400# }
2401
Andy Whitcroft8905a672007-11-28 16:21:06 -08002402 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002403 WARN("LINUX_VERSION_CODE",
2404 "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 -08002405 }
2406
Joe Perches17441222011-06-15 15:08:17 -07002407# check for uses of printk_ratelimit
2408 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002409 WARN("PRINTK_RATELIMITED",
2410"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07002411 }
2412
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002413# printk should use KERN_* levels. Note that follow on printk's on the
2414# same line do not need a level, so we use the current block context
2415# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002416# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002417# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002418 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002419 my $ok = 0;
2420 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2421 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002422 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002423 # with "\n" ignore it, else it is to blame
2424 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2425 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2426 $ok = 1;
2427 }
2428 last;
2429 }
2430 }
2431 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002432 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2433 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002434 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002435 }
2436
Joe Perches243f3802012-05-31 16:26:09 -07002437 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2438 my $orig = $1;
2439 my $level = lc($orig);
2440 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07002441 my $level2 = $level;
2442 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07002443 WARN("PREFER_PR_LEVEL",
Joe Perches8f26b832012-10-04 17:13:32 -07002444 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07002445 }
2446
2447 if ($line =~ /\bpr_warning\s*\(/) {
2448 WARN("PREFER_PR_LEVEL",
2449 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2450 }
2451
Joe Perchesdc139312013-02-21 16:44:13 -08002452 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2453 my $orig = $1;
2454 my $level = lc($orig);
2455 $level = "warn" if ($level eq "warning");
2456 $level = "dbg" if ($level eq "debug");
2457 WARN("PREFER_DEV_LEVEL",
2458 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2459 }
2460
Andy Whitcroft653d4872007-06-23 17:16:34 -07002461# function brace can't be on same line, except for #defines of do while,
2462# or if closed on same line
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002463 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2464 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002465 ERROR("OPEN_BRACE",
2466 "open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002467 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002468
Andy Whitcroft8905a672007-11-28 16:21:06 -08002469# open braces for enum, union and struct go on the same line.
2470 if ($line =~ /^.\s*{/ &&
2471 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002472 ERROR("OPEN_BRACE",
2473 "open brace '{' following $1 go on the same line\n" . $hereprev);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002474 }
2475
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002476# missing space after union, struct or enum definition
2477 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002478 WARN("SPACING",
2479 "missing space after $1 definition\n" . $herecurr);
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07002480 }
2481
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002482# check for spacing round square brackets; allowed:
2483# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002484# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2485# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002486 while ($line =~ /(.*?\s)\[/g) {
2487 my ($where, $prefix) = ($-[1], $1);
2488 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07002489 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07002490 $prefix !~ /[{,]\s+$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002491 ERROR("BRACKET_SPACE",
2492 "space prohibited before open square bracket '['\n" . $herecurr);
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07002493 }
2494 }
2495
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002496# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002497 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002498 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002499 my $ctx_before = substr($line, 0, $-[1]);
2500 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002501
2502 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002503 if ($name =~ /^(?:
2504 if|for|while|switch|return|case|
2505 volatile|__volatile__|
2506 __attribute__|format|__extension__|
2507 asm|__asm__)$/x)
2508 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002509
2510 # cpp #define statements have non-optional spaces, ie
2511 # if there is a space between the name and the open
2512 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002513 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002514
2515 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002516 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002517
2518 # If this whole things ends with a type its most
2519 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002520 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002521
2522 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07002523 WARN("SPACING",
2524 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002525 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002526 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07002527
2528# check for whitespace before a non-naked semicolon
2529 if ($line =~ /^\+.*\S\s+;/) {
Joe Perches5646bc72013-04-29 16:18:15 -07002530 WARN("SPACING",
2531 "space prohibited before semicolon\n" . $herecurr);
Eric Nelson9a4cad42012-05-31 16:26:09 -07002532 }
2533
Andy Whitcroft653d4872007-06-23 17:16:34 -07002534# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002535 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002536 my $ops = qr{
2537 <<=|>>=|<=|>=|==|!=|
2538 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2539 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002540 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2541 \?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002542 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002543 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002544 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002545
2546 my $blank = copy_spacing($opline);
2547
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002548 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002549 $off += length($elements[$n]);
2550
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002551 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002552 my $ca = substr($opline, 0, $off);
2553 my $cc = '';
2554 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2555 $cc = substr($opline, $off + length($elements[$n + 1]));
2556 }
2557 my $cb = "$ca$;$cc";
2558
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002559 my $a = '';
2560 $a = 'V' if ($elements[$n] ne '');
2561 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002562 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002563 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2564 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07002565 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002566
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002567 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002568
2569 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002570 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002571 $c = 'V' if ($elements[$n + 2] ne '');
2572 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08002573 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002574 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2575 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08002576 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002577 } else {
2578 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002579 }
2580
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002581 my $ctx = "${a}x${c}";
2582
2583 my $at = "(ctx:$ctx)";
2584
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002585 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002586 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002587
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002588 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002589 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002590
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002591 # Get the full operator variant.
2592 my $opv = $op . substr($curr_vars, $off, 1);
2593
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002594 # Ignore operators passed as parameters.
2595 if ($op_type ne 'V' &&
2596 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2597
Andy Whitcroftcf655042008-03-04 14:28:20 -08002598# # Ignore comments
2599# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002600
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002601 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002602 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002603 if ($ctx !~ /.x[WEBC]/ &&
2604 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002605 ERROR("SPACING",
2606 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002607 }
2608
2609 # // is a comment
2610 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002611
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002612 # No spaces for:
2613 # ->
2614 # : when part of a bitfield
2615 } elsif ($op eq '->' || $opv eq ':B') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002616 if ($ctx =~ /Wx.|.xW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002617 ERROR("SPACING",
2618 "spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002619 }
2620
2621 # , must have a space on the right.
2622 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002623 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002624 ERROR("SPACING",
2625 "space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002626 }
2627
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002628 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002629 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002630 #warn "'*' is part of type\n";
2631
2632 # unary operators should have a space before and
2633 # none after. May be left adjacent to another
2634 # unary operator, or a cast
2635 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07002636 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07002637 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08002638 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002639 ERROR("SPACING",
2640 "space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002641 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08002642 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002643 # A unary '*' may be const
2644
2645 } elsif ($ctx =~ /.xW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002646 ERROR("SPACING",
2647 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002648 }
2649
2650 # unary ++ and unary -- are allowed no space on one side.
2651 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002652 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002653 ERROR("SPACING",
2654 "space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002655 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002656 if ($ctx =~ /Wx[BE]/ ||
2657 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002658 ERROR("SPACING",
2659 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002660 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002661 if ($ctx =~ /ExW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002662 ERROR("SPACING",
2663 "space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002664 }
2665
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002666
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002667 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002668 } elsif ($op eq '<<' or $op eq '>>' or
2669 $op eq '&' or $op eq '^' or $op eq '|' or
2670 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002671 $op eq '*' or $op eq '/' or
2672 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002673 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002674 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002675 ERROR("SPACING",
2676 "need consistent spacing around '$op' $at\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002677 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002678 }
2679
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002680 # A colon needs no spaces before when it is
2681 # terminating a case value or a label.
2682 } elsif ($opv eq ':C' || $opv eq ':L') {
2683 if ($ctx =~ /Wx./) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002684 ERROR("SPACING",
2685 "space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002686 }
2687
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002688 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08002689 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002690 my $ok = 0;
2691
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002692 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002693 if (($op eq '<' &&
2694 $cc =~ /^\S+\@\S+>/) ||
2695 ($op eq '>' &&
2696 $ca =~ /<\S+\@\S+$/))
2697 {
2698 $ok = 1;
2699 }
2700
2701 # Ignore ?:
2702 if (($opv eq ':O' && $ca =~ /\?$/) ||
2703 ($op eq '?' && $cc =~ /^:/)) {
2704 $ok = 1;
2705 }
2706
2707 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002708 ERROR("SPACING",
2709 "spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002710 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002711 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002712 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002713 }
2714 }
2715
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002716# check for multiple assignments
2717 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002718 CHK("MULTIPLE_ASSIGNMENTS",
2719 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002720 }
2721
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002722## # check for multiple declarations, allowing for a function declaration
2723## # continuation.
2724## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2725## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2726##
2727## # Remove any bracketed sections to ensure we do not
2728## # falsly report the parameters of functions.
2729## my $ln = $line;
2730## while ($ln =~ s/\([^\(\)]*\)//g) {
2731## }
2732## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002733## WARN("MULTIPLE_DECLARATION",
2734## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002735## }
2736## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002737
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002738#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002739 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2740 $line =~ /do{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002741 ERROR("SPACING",
2742 "space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002743 }
2744
2745# closing brace should have a space following it when it has anything
2746# on the line
2747 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002748 ERROR("SPACING",
2749 "space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002750 }
2751
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002752# check spacing on square brackets
2753 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002754 ERROR("SPACING",
2755 "space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002756 }
2757 if ($line =~ /\s\]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002758 ERROR("SPACING",
2759 "space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002760 }
2761
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002762# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002763 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2764 $line !~ /for\s*\(\s+;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002765 ERROR("SPACING",
2766 "space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002767 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002768 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002769 $line !~ /for\s*\(.*;\s+\)/ &&
2770 $line !~ /:\s+\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002771 ERROR("SPACING",
2772 "space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002773 }
2774
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002775#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002776 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002777 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002778 WARN("INDENTED_LABEL",
2779 "labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002780 }
2781
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002782# Return is not a function.
2783 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2784 my $spacing = $1;
2785 my $value = $2;
2786
Andy Whitcroft86f9d052009-01-06 14:41:24 -08002787 # Flatten any parentheses
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002788 $value =~ s/\(/ \(/g;
2789 $value =~ s/\)/\) /g;
Andy Whitcrofte01886a2012-01-10 15:10:08 -08002790 while ($value =~ s/\[[^\[\]]*\]/1/ ||
Andy Whitcroft63f17f82009-01-15 13:51:06 -08002791 $value !~ /(?:$Ident|-?$Constant)\s*
2792 $Compare\s*
2793 (?:$Ident|-?$Constant)/x &&
2794 $value =~ s/\([^\(\)]*\)/1/) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002795 }
Andy Whitcroftfb2d2c12010-10-26 14:23:12 -07002796#print "value<$value>\n";
2797 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002798 ERROR("RETURN_PARENTHESES",
2799 "return is not a function, parentheses are not required\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002800
2801 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002802 ERROR("SPACING",
2803 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002804 }
2805 }
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002806# Return of what appears to be an errno should normally be -'ve
2807 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2808 my $name = $1;
2809 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002810 WARN("USE_NEGATIVE_ERRNO",
2811 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07002812 }
2813 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002814
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002815# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002816 if ($line=~/\b(if|while|for|switch)\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002817 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002818 }
2819
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002820# Check for illegal assignment in if conditional -- and check for trailing
2821# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002822 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002823 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2824 ctx_statement_block($linenr, $realcnt, 0)
2825 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002826 my ($stat_next) = ctx_statement_block($line_nr_next,
2827 $remain_next, $off_next);
2828 $stat_next =~ s/\n./\n /g;
2829 ##print "stat<$stat> stat_next<$stat_next>\n";
2830
2831 if ($stat_next =~ /^\s*while\b/) {
2832 # If the statement carries leading newlines,
2833 # then count those as offsets.
2834 my ($whitespace) =
2835 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2836 my $offset =
2837 statement_rawlines($whitespace) - 1;
2838
2839 $suppress_whiletrailers{$line_nr_next +
2840 $offset} = 1;
2841 }
2842 }
2843 if (!defined $suppress_whiletrailers{$linenr} &&
2844 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002845 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002846
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08002847 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002848 ERROR("ASSIGN_IN_IF",
2849 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002850 }
2851
2852 # Find out what is on the end of the line after the
2853 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002854 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002855 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002856 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07002857 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2858 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002859 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002860 # Find out how long the conditional actually is.
2861 my @newlines = ($c =~ /\n/gs);
2862 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002863 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002864
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08002865 $stat_real = raw_line($linenr, $cond_lines)
2866 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07002867 if (defined($stat_real) && $cond_lines > 1) {
2868 $stat_real = "[...]\n$stat_real";
2869 }
2870
Joe Perches000d1cc12011-07-25 17:13:25 -07002871 ERROR("TRAILING_STATEMENTS",
2872 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002873 }
2874 }
2875
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002876# Check for bitwise tests written as boolean
2877 if ($line =~ /
2878 (?:
2879 (?:\[|\(|\&\&|\|\|)
2880 \s*0[xX][0-9]+\s*
2881 (?:\&\&|\|\|)
2882 |
2883 (?:\&\&|\|\|)
2884 \s*0[xX][0-9]+\s*
2885 (?:\&\&|\|\||\)|\])
2886 )/x)
2887 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002888 WARN("HEXADECIMAL_BOOLEAN_TEST",
2889 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002890 }
2891
Andy Whitcroft8905a672007-11-28 16:21:06 -08002892# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002893 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2894 my $s = $1;
2895 $s =~ s/$;//g; # Remove any comments
2896 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002897 ERROR("TRAILING_STATEMENTS",
2898 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002899 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002900 }
Andy Whitcroft39667782009-01-15 13:51:06 -08002901# if should not continue a brace
2902 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002903 ERROR("TRAILING_STATEMENTS",
2904 "trailing statements should be on next line\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08002905 $herecurr);
2906 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002907# case and default should not have general statements after them
2908 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2909 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07002910 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002911 \s*return\s+
2912 )/xg)
2913 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002914 ERROR("TRAILING_STATEMENTS",
2915 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07002916 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002917
2918 # Check for }<nl>else {, these must be at the same
2919 # indent level to be relevant to each other.
2920 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2921 $previndent == $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002922 ERROR("ELSE_AFTER_BRACE",
2923 "else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002924 }
2925
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002926 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2927 $previndent == $indent) {
2928 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2929
2930 # Find out what is on the end of the line after the
2931 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002932 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002933 $s =~ s/\n.*//g;
2934
2935 if ($s =~ /^\s*;/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002936 ERROR("WHILE_AFTER_BRACE",
2937 "while should follow close brace '}'\n" . $hereprev);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002938 }
2939 }
2940
Joe Perches95e2c602013-07-03 15:05:20 -07002941#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08002942 while ($line =~ m{($Constant|$Lval)}g) {
2943 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07002944
2945#gcc binary extension
2946 if ($var =~ /^$Binary$/) {
2947 WARN("GCC_BINARY_CONSTANT",
2948 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
2949 }
2950
2951#CamelCase
Joe Perches323c1262012-12-17 16:02:07 -08002952 if ($var !~ /$Constant/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07002953 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perchesbe987d92013-02-27 17:02:38 -08002954 $var !~ /"^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches323c1262012-12-17 16:02:07 -08002955 !defined $camelcase{$var}) {
2956 $camelcase{$var} = 1;
Joe Perchesbe797942013-07-03 15:05:20 -07002957 CHK("CAMELCASE",
2958 "Avoid CamelCase: <$var>\n" . $herecurr);
Joe Perches323c1262012-12-17 16:02:07 -08002959 }
2960 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002961
2962#no spaces allowed after \ in define
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002963 if ($line=~/\#\s*define.*\\\s$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002964 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2965 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002966 }
2967
Andy Whitcroft653d4872007-06-23 17:16:34 -07002968#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002969 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002970 my $file = "$1.h";
2971 my $checkfile = "include/linux/$file";
2972 if (-f "$root/$checkfile" &&
2973 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07002974 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002975 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002976 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002977 CHK("ARCH_INCLUDE_LINUX",
2978 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002979 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07002980 WARN("INCLUDE_LINUX",
2981 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07002982 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002983 }
2984 }
2985
Andy Whitcroft653d4872007-06-23 17:16:34 -07002986# multi-statement macros should be enclosed in a do while loop, grab the
2987# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08002988# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07002989 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2990 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002991 my $ln = $linenr;
2992 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002993 my ($off, $dstat, $dcond, $rest);
2994 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002995 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002996 ctx_statement_block($linenr, $realcnt, 0);
2997 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002998 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07002999 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003000
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003001 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07003002 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003003 $dstat =~ s/\\\n.//g;
3004 $dstat =~ s/^\s*//s;
3005 $dstat =~ s/\s*$//s;
3006
3007 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003008 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3009 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08003010 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07003011 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003012 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003013
Andy Whitcrofte45bab82012-03-23 15:02:18 -07003014 # Flatten any obvious string concatentation.
3015 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3016 $dstat =~ s/$Ident\s*("X*")/$1/)
3017 {
3018 }
3019
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003020 my $exceptions = qr{
3021 $Declare|
3022 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07003023 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003024 DECLARE_PER_CPU|
3025 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08003026 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08003027 union|
3028 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07003029 \.$Ident\s*=\s*|
3030 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003031 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07003032 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003033 if ($dstat ne '' &&
3034 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3035 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Andy Whitcroftfd1b57a2012-03-23 15:02:18 -07003036 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo
Andy Whitcroftb9df76a2012-03-23 15:02:17 -07003037 $dstat !~ /^'X'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003038 $dstat !~ /$exceptions/ &&
3039 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07003040 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08003041 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003042 $dstat !~ /^for\s*$Constant$/ && # for (...)
3043 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3044 $dstat !~ /^do\s*{/ && # do {...
3045 $dstat !~ /^\({/) # ({...
3046 {
3047 $ctx =~ s/\n*$//;
3048 my $herectx = $here . "\n";
3049 my $cnt = statement_rawlines($ctx);
3050
3051 for (my $n = 0; $n < $cnt; $n++) {
3052 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003053 }
3054
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003055 if ($dstat =~ /;/) {
3056 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3057 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3058 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07003059 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003060 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003061 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003062 }
Joe Perches5023d342012-12-17 16:01:47 -08003063
Joe Perches481eb482012-12-17 16:01:56 -08003064# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08003065
3066 } else {
3067 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08003068 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3069 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08003070 $line =~ /^\+.*\\$/) {
3071 WARN("LINE_CONTINUATIONS",
3072 "Avoid unnecessary line continuations\n" . $herecurr);
3073 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003074 }
3075
Joe Perchesb13edf72012-07-30 14:41:24 -07003076# do {} while (0) macro tests:
3077# single-statement macros do not need to be enclosed in do while (0) loop,
3078# macro should not end with a semicolon
3079 if ($^V && $^V ge 5.10.0 &&
3080 $realfile !~ m@/vmlinux.lds.h$@ &&
3081 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3082 my $ln = $linenr;
3083 my $cnt = $realcnt;
3084 my ($off, $dstat, $dcond, $rest);
3085 my $ctx = '';
3086 ($dstat, $dcond, $ln, $cnt, $off) =
3087 ctx_statement_block($linenr, $realcnt, 0);
3088 $ctx = $dstat;
3089
3090 $dstat =~ s/\\\n.//g;
3091
3092 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3093 my $stmts = $2;
3094 my $semis = $3;
3095
3096 $ctx =~ s/\n*$//;
3097 my $cnt = statement_rawlines($ctx);
3098 my $herectx = $here . "\n";
3099
3100 for (my $n = 0; $n < $cnt; $n++) {
3101 $herectx .= raw_line($linenr, $n) . "\n";
3102 }
3103
Joe Perchesac8e97f2012-08-21 16:15:53 -07003104 if (($stmts =~ tr/;/;/) == 1 &&
3105 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07003106 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3107 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3108 }
3109 if (defined $semis && $semis ne "") {
3110 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3111 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3112 }
3113 }
3114 }
3115
Mike Frysinger080ba922009-01-06 14:41:25 -08003116# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3117# all assignments may have only one of the following with an assignment:
3118# .
3119# ALIGN(...)
3120# VMLINUX_SYMBOL(...)
3121 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003122 WARN("MISSING_VMLINUX_SYMBOL",
3123 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08003124 }
3125
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003126# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003127 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3128 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08003129 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003130 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003131 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3132 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07003133 my @allowed = ();
3134 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003135 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003136 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003137 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003138 for my $chunk (@chunks) {
3139 my ($cond, $block) = @{$chunk};
3140
Andy Whitcroft773647a2008-03-28 14:15:58 -07003141 # If the condition carries leading newlines, then count those as offsets.
3142 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3143 my $offset = statement_rawlines($whitespace) - 1;
3144
Joe Perchesaad4f612012-03-23 15:02:19 -07003145 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003146 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3147
3148 # We have looked at and allowed this specific line.
3149 $suppress_ifbraces{$ln + $offset} = 1;
3150
3151 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003152 $ln += statement_rawlines($block) - 1;
3153
Andy Whitcroft773647a2008-03-28 14:15:58 -07003154 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003155
3156 $seen++ if ($block =~ /^\s*{/);
3157
Joe Perchesaad4f612012-03-23 15:02:19 -07003158 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003159 if (statement_lines($cond) > 1) {
3160 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003161 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003162 }
3163 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003164 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003165 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003166 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003167 if (statement_block_size($block) > 1) {
3168 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07003169 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003170 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003171 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003172 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003173 if ($seen) {
3174 my $sum_allowed = 0;
3175 foreach (@allowed) {
3176 $sum_allowed += $_;
3177 }
3178 if ($sum_allowed == 0) {
3179 WARN("BRACES",
3180 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3181 } elsif ($sum_allowed != $allow &&
3182 $seen != $allow) {
3183 CHK("BRACES",
3184 "braces {} should be used on all arms of this statement\n" . $herectx);
3185 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003186 }
3187 }
3188 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003189 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003190 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003191 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003192
Andy Whitcroftcf655042008-03-04 14:28:20 -08003193 # Check the pre-context.
3194 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3195 #print "APW: ALLOWED: pre<$1>\n";
3196 $allowed = 1;
3197 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003198
3199 my ($level, $endln, @chunks) =
3200 ctx_statement_full($linenr, $realcnt, $-[0]);
3201
Andy Whitcroftcf655042008-03-04 14:28:20 -08003202 # Check the condition.
3203 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07003204 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003205 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003206 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08003207 }
3208 if (statement_lines($cond) > 1) {
3209 #print "APW: ALLOWED: cond<$cond>\n";
3210 $allowed = 1;
3211 }
3212 if ($block =~/\b(?:if|for|while)\b/) {
3213 #print "APW: ALLOWED: block<$block>\n";
3214 $allowed = 1;
3215 }
3216 if (statement_block_size($block) > 1) {
3217 #print "APW: ALLOWED: lines block<$block>\n";
3218 $allowed = 1;
3219 }
3220 # Check the post-context.
3221 if (defined $chunks[1]) {
3222 my ($cond, $block) = @{$chunks[1]};
3223 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003224 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003225 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003226 if ($block =~ /^\s*\{/) {
3227 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3228 $allowed = 1;
3229 }
3230 }
3231 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003232 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07003233 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003234
Andy Whitcroftf0556632008-10-15 22:02:23 -07003235 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07003236 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08003237 }
3238
Joe Perches000d1cc12011-07-25 17:13:25 -07003239 WARN("BRACES",
3240 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003241 }
3242 }
3243
Joe Perches0979ae62012-12-17 16:01:59 -08003244# check for unnecessary blank lines around braces
Matthijs Kooijman74c8f432013-04-29 16:18:16 -07003245 if (($line =~ /^.\s*}\s*$/ && $prevline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08003246 CHK("BRACES",
3247 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3248 }
3249 if (($line =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3250 CHK("BRACES",
3251 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3252 }
3253
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003254# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003255 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3256 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003257 WARN("VOLATILE",
3258 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003259 }
3260
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003261# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003262 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003263 CHK("REDUNDANT_CODE",
3264 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003265 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003266 }
3267
Andy Whitcroft03df4b52012-12-17 16:01:52 -08003268# check for needless "if (<foo>) fn(<foo>)" uses
3269 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3270 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3271 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3272 WARN('NEEDLESS_IF',
3273 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07003274 }
3275 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003276
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003277# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08003278 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003279 # ignore udelay's < 10, however
Bruce Allan37581c22013-02-21 16:44:19 -08003280 if (! ($1 < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003281 CHK("USLEEP_RANGE",
3282 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07003283 }
3284 }
3285
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003286# warn about unexpectedly long msleep's
3287 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3288 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003289 WARN("MSLEEP",
3290 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07003291 }
3292 }
3293
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003294# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003295# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003296# print "#ifdef in C files should be avoided\n";
3297# print "$herecurr";
3298# $clean = 0;
3299# }
3300
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003301# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003302 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003303 ERROR("SPACING",
3304 "exactly one space required after that #$1\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003305 }
3306
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003307# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003308 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3309 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003310 my $which = $1;
3311 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003312 CHK("UNCOMMENTED_DEFINITION",
3313 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003314 }
3315 }
3316# check for memory barriers without a comment.
3317 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3318 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003319 CHK("MEMORY_BARRIER",
3320 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003321 }
3322 }
3323# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003324 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003325 CHK("ARCH_DEFINES",
3326 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003327 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003328
Tobias Klauserd4977c72010-05-24 14:33:30 -07003329# Check that the storage class is at the beginning of a declaration
3330 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003331 WARN("STORAGE_CLASS",
3332 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07003333 }
3334
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003335# check the location of the inline attribute, that it is between
3336# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003337 if ($line =~ /\b$Type\s+$Inline\b/ ||
3338 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003339 ERROR("INLINE_LOCATION",
3340 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003341 }
3342
Andy Whitcroft8905a672007-11-28 16:21:06 -08003343# Check for __inline__ and __inline, prefer inline
3344 if ($line =~ /\b(__inline__|__inline)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003345 WARN("INLINE",
3346 "plain inline is preferred over $1\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003347 }
3348
Joe Perches3d130fd2011-01-12 17:00:00 -08003349# Check for __attribute__ packed, prefer __packed
3350 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003351 WARN("PREFER_PACKED",
3352 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08003353 }
3354
Joe Perches39b7e282011-07-25 17:13:24 -07003355# Check for __attribute__ aligned, prefer __aligned
3356 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003357 WARN("PREFER_ALIGNED",
3358 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07003359 }
3360
Joe Perches5f14d3b2012-01-10 15:09:52 -08003361# Check for __attribute__ format(printf, prefer __printf
3362 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3363 WARN("PREFER_PRINTF",
3364 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3365 }
3366
Joe Perches6061d942012-03-23 15:02:16 -07003367# Check for __attribute__ format(scanf, prefer __scanf
3368 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3369 WARN("PREFER_SCANF",
3370 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3371 }
3372
Joe Perches8f53a9b2010-03-05 13:43:48 -08003373# check for sizeof(&)
3374 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003375 WARN("SIZEOF_ADDRESS",
3376 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08003377 }
3378
Joe Perches66c80b62012-07-30 14:41:22 -07003379# check for sizeof without parenthesis
3380 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3381 WARN("SIZEOF_PARENTHESIS",
3382 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3383 }
3384
Joe Perches428e2fd2011-05-24 17:13:39 -07003385# check for line continuations in quoted strings with odd counts of "
3386 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003387 WARN("LINE_CONTINUATIONS",
3388 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07003389 }
3390
Joe Perches88982fe2012-12-17 16:02:00 -08003391# check for struct spinlock declarations
3392 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3393 WARN("USE_SPINLOCK_T",
3394 "struct spinlock should be spinlock_t\n" . $herecurr);
3395 }
3396
Joe Perchesa6962d72013-04-29 16:18:13 -07003397# check for seq_printf uses that could be seq_puts
3398 if ($line =~ /\bseq_printf\s*\(/) {
3399 my $fmt = get_quoted_string($line, $rawline);
3400 if ($fmt !~ /[^\\]\%/) {
3401 WARN("PREFER_SEQ_PUTS",
3402 "Prefer seq_puts to seq_printf\n" . $herecurr);
3403 }
3404 }
3405
Andy Whitcroft554e1652012-01-10 15:09:57 -08003406# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003407 if ($^V && $^V ge 5.10.0 &&
3408 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003409 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08003410
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003411 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003412 my $ms_val = $7;
3413 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003414
Andy Whitcroft554e1652012-01-10 15:09:57 -08003415 if ($ms_size =~ /^(0x|)0$/i) {
3416 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003417 "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 -08003418 } elsif ($ms_size =~ /^(0x|)1$/i) {
3419 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003420 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3421 }
3422 }
3423
3424# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003425 if ($^V && $^V ge 5.10.0 &&
3426 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003427 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003428 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003429 my $call = $1;
3430 my $cast1 = deparenthesize($2);
3431 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003432 my $cast2 = deparenthesize($7);
3433 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003434 my $cast;
3435
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003436 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08003437 $cast = "$cast1 or $cast2";
3438 } elsif ($cast1 ne "") {
3439 $cast = $cast1;
3440 } else {
3441 $cast = $cast2;
3442 }
3443 WARN("MINMAX",
3444 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08003445 }
3446 }
3447
Joe Perches4a273192012-07-30 14:41:20 -07003448# check usleep_range arguments
3449 if ($^V && $^V ge 5.10.0 &&
3450 defined $stat &&
3451 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3452 my $min = $1;
3453 my $max = $7;
3454 if ($min eq $max) {
3455 WARN("USLEEP_RANGE",
3456 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3457 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3458 $min > $max) {
3459 WARN("USLEEP_RANGE",
3460 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3461 }
3462 }
3463
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003464# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003465 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003466 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003467 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003468 my $function_name = $1;
3469 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003470
3471 my $s = $stat;
3472 if (defined $cond) {
3473 substr($s, 0, length($cond), '');
3474 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003475 if ($s =~ /^\s*;/ &&
3476 $function_name ne 'uninitialized_var')
3477 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003478 WARN("AVOID_EXTERNS",
3479 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003480 }
3481
3482 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003483 WARN("FUNCTION_ARGUMENTS",
3484 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003485 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003486
3487 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3488 $stat =~ /^.\s*extern\s+/)
3489 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003490 WARN("AVOID_EXTERNS",
3491 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003492 }
3493
3494# checks for new __setup's
3495 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3496 my $name = $1;
3497
3498 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003499 CHK("UNDOCUMENTED_SETUP",
3500 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003501 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003502 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003503
3504# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08003505 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003506 WARN("UNNECESSARY_CASTS",
3507 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003508 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003509
Joe Perchesa640d252013-07-03 15:05:21 -07003510# alloc style
3511# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3512 if ($^V && $^V ge 5.10.0 &&
3513 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3514 CHK("ALLOC_SIZEOF_STRUCT",
3515 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3516 }
3517
Joe Perches972fdea2013-04-29 16:18:12 -07003518# check for krealloc arg reuse
3519 if ($^V && $^V ge 5.10.0 &&
3520 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3521 WARN("KREALLOC_ARG_REUSE",
3522 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3523 }
3524
Joe Perches5ce59ae2013-02-21 16:44:18 -08003525# check for alloc argument mismatch
3526 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3527 WARN("ALLOC_ARRAY_ARGS",
3528 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3529 }
3530
Joe Perchescaf2a542011-01-12 16:59:56 -08003531# check for multiple semicolons
3532 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08003533 WARN("ONE_SEMICOLON",
3534 "Statements terminations use 1 semicolon\n" . $herecurr);
3535 }
3536
3537# check for switch/default statements without a break;
3538 if ($^V && $^V ge 5.10.0 &&
3539 defined $stat &&
3540 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3541 my $ctx = '';
3542 my $herectx = $here . "\n";
3543 my $cnt = statement_rawlines($stat);
3544 for (my $n = 0; $n < $cnt; $n++) {
3545 $herectx .= raw_line($linenr, $n) . "\n";
3546 }
3547 WARN("DEFAULT_NO_BREAK",
3548 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08003549 }
3550
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003551# check for gcc specific __FUNCTION__
3552 if ($line =~ /__FUNCTION__/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003553 WARN("USE_FUNC",
3554 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003555 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003556
Joe Perches2c924882012-03-23 15:02:20 -07003557# check for use of yield()
3558 if ($line =~ /\byield\s*\(\s*\)/) {
3559 WARN("YIELD",
3560 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3561 }
3562
Thomas Gleixner4882720b2010-09-07 14:34:01 +00003563# check for semaphores initialized locked
3564 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003565 WARN("CONSIDER_COMPLETION",
3566 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003567 }
Joe Perches6712d852012-03-23 15:02:20 -07003568
Joe Perches67d0a072011-10-31 17:13:10 -07003569# recommend kstrto* over simple_strto* and strict_strto*
3570 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003571 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07003572 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003573 }
Joe Perches6712d852012-03-23 15:02:20 -07003574
Michael Ellermanf3db6632008-07-23 21:28:57 -07003575# check for __initcall(), use device_initcall() explicitly please
3576 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003577 WARN("USE_DEVICE_INITCALL",
3578 "please use device_initcall() instead of __initcall()\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07003579 }
Joe Perches6712d852012-03-23 15:02:20 -07003580
Emese Revfy79404842010-03-05 13:43:53 -08003581# check for various ops structs, ensure they are const.
3582 my $struct_ops = qr{acpi_dock_ops|
3583 address_space_operations|
3584 backlight_ops|
3585 block_device_operations|
3586 dentry_operations|
3587 dev_pm_ops|
3588 dma_map_ops|
3589 extent_io_ops|
3590 file_lock_operations|
3591 file_operations|
3592 hv_ops|
3593 ide_dma_ops|
3594 intel_dvo_dev_ops|
3595 item_operations|
3596 iwl_ops|
3597 kgdb_arch|
3598 kgdb_io|
3599 kset_uevent_ops|
3600 lock_manager_operations|
3601 microcode_ops|
3602 mtrr_ops|
3603 neigh_ops|
3604 nlmsvc_binding|
3605 pci_raw_ops|
3606 pipe_buf_operations|
3607 platform_hibernation_ops|
3608 platform_suspend_ops|
3609 proto_ops|
3610 rpc_pipe_ops|
3611 seq_operations|
3612 snd_ac97_build_ops|
3613 soc_pcmcia_socket_ops|
3614 stacktrace_ops|
3615 sysfs_ops|
3616 tty_operations|
3617 usb_mon_operations|
3618 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003619 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08003620 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003621 WARN("CONST_STRUCT",
3622 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08003623 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08003624 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003625
3626# use of NR_CPUS is usually wrong
3627# ignore definitions of NR_CPUS and usage to define arrays as likely right
3628 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003629 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3630 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003631 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3632 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3633 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003634 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003635 WARN("NR_CPUS",
3636 "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 -07003637 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003638
3639# check for %L{u,d,i} in strings
3640 my $string;
3641 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3642 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07003643 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003644 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003645 WARN("PRINTF_L",
3646 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07003647 last;
3648 }
3649 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003650
3651# whine mightly about in_atomic
3652 if ($line =~ /\bin_atomic\s*\(/) {
3653 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003654 ERROR("IN_ATOMIC",
3655 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08003656 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003657 WARN("IN_ATOMIC",
3658 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08003659 }
3660 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01003661
3662# check for lockdep_set_novalidate_class
3663 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3664 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3665 if ($realfile !~ m@^kernel/lockdep@ &&
3666 $realfile !~ m@^include/linux/lockdep@ &&
3667 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003668 ERROR("LOCKDEP",
3669 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003670 }
3671 }
Dave Jones88f88312011-01-12 16:59:59 -08003672
3673 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3674 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003675 WARN("EXPORTED_WORLD_WRITABLE",
3676 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08003677 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003678 }
3679
3680 # If we have no input at all, then there is nothing to report on
3681 # so just keep quiet.
3682 if ($#rawlines == -1) {
3683 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003684 }
3685
Andy Whitcroft8905a672007-11-28 16:21:06 -08003686 # In mailback mode only produce a report in the negative, for
3687 # things that appear to be patches.
3688 if ($mailback && ($clean == 1 || !$is_patch)) {
3689 exit(0);
3690 }
3691
3692 # This is not a patch, and we are are in 'no-patch' mode so
3693 # just keep quiet.
3694 if (!$chk_patch && !$is_patch) {
3695 exit(0);
3696 }
3697
3698 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003699 ERROR("NOT_UNIFIED_DIFF",
3700 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003701 }
3702 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003703 ERROR("MISSING_SIGN_OFF",
3704 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003705 }
3706
Andy Whitcroft8905a672007-11-28 16:21:06 -08003707 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003708 if ($summary && !($clean == 1 && $quiet == 1)) {
3709 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003710 print "total: $cnt_error errors, $cnt_warn warnings, " .
3711 (($check)? "$cnt_chk checks, " : "") .
3712 "$cnt_lines lines checked\n";
3713 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003714 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003715
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003716 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003717
3718 if ($^V lt 5.10.0) {
3719 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3720 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3721 }
3722
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003723 # If there were whitespace errors which cleanpatch can fix
3724 # then suggest that.
3725 if ($rpt_cleaners) {
3726 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
3727 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07003728 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003729 }
3730 }
3731
Artem Bityutskiy11232682012-03-23 15:02:17 -07003732 if ($quiet == 0 && keys %ignore_type) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003733 print "NOTE: Ignored message types:";
3734 foreach my $ignore (sort keys %ignore_type) {
3735 print " $ignore";
3736 }
Artem Bityutskiy11232682012-03-23 15:02:17 -07003737 print "\n\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003738 }
3739
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003740 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003741 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003742 }
3743 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003744 print << "EOM";
3745$vname has style problems, please review.
3746
3747If any of these errors are false positives, please report
3748them to the maintainer, see CHECKPATCH in MAINTAINERS.
3749EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003750 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003751
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003752 return $clean;
3753}