blob: d52293f6537494cfb13a9a8ccc4a2d4bbb915cd9 [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070012use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
14my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080015my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070016
Joe Perches000d1cc12011-07-25 17:13:25 -070017my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070018
19use Getopt::Long qw(:config no_auto_abbrev);
20
21my $quiet = 0;
22my $tree = 1;
23my $chk_signoff = 1;
24my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070025my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070026my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080027my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070028my $file = 0;
29my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070030my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $summary = 1;
32my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080033my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070034my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070035my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080036my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070037my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080038my %debug;
Joe Perches34456862013-07-03 15:05:34 -070039my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070040my %use_type = ();
41my @use = ();
42my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070043my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070044my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070045my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080046my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070047my $ignore_perl_version = 0;
48my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070049my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070050my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070051my $codespell = 0;
52my $codespellfile = "/usr/local/share/codespell/dictionary.txt";
Joe Perches57230292015-06-25 15:03:03 -070053my $color = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070054
55sub help {
56 my ($exitcode) = @_;
57
58 print << "EOM";
59Usage: $P [OPTION]... [FILE]...
60Version: $V
61
62Options:
63 -q, --quiet quiet
64 --no-tree run without a kernel tree
65 --no-signoff do not check for 'Signed-off-by' line
66 --patch treat FILE as patchfile (default)
67 --emacs emacs compile window format
68 --terse one line per report
69 -f, --file treat FILE as regular source file
70 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070071 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070072 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080073 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070074 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070075 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070076 --root=PATH PATH to the kernel tree root
77 --no-summary suppress the per-file summary
78 --mailback only produce a report in case of warnings/errors
79 --summary-file include the filename in summary
80 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
81 'values', 'possible', 'type', and 'attr' (default
82 is all off)
83 --test-only=WORD report only warnings/errors containing WORD
84 literally
Joe Perches3705ce52013-07-03 15:05:31 -070085 --fix EXPERIMENTAL - may create horrible results
86 If correctable single-line errors exist, create
87 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
88 with potential errors corrected to the preferred
89 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080090 --fix-inplace EXPERIMENTAL - may create horrible results
91 Is the same as --fix, but overwrites the input
92 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070093 --ignore-perl-version override checking of perl version. expect
94 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -070095 --codespell Use the codespell dictionary for spelling/typos
96 (default:/usr/local/share/codespell/dictionary.txt)
97 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -070098 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -070099 -h, --help, --version display this help and exit
100
101When FILE is - read standard input.
102EOM
103
104 exit($exitcode);
105}
106
Joe Perches000d1cc12011-07-25 17:13:25 -0700107my $conf = which_conf($configuration_file);
108if (-f $conf) {
109 my @conf_args;
110 open(my $conffile, '<', "$conf")
111 or warn "$P: Can't find a readable $configuration_file file $!\n";
112
113 while (<$conffile>) {
114 my $line = $_;
115
116 $line =~ s/\s*\n?$//g;
117 $line =~ s/^\s*//g;
118 $line =~ s/\s+/ /g;
119
120 next if ($line =~ m/^\s*#/);
121 next if ($line =~ m/^\s*$/);
122
123 my @words = split(" ", $line);
124 foreach my $word (@words) {
125 last if ($word =~ m/^#/);
126 push (@conf_args, $word);
127 }
128 }
129 close($conffile);
130 unshift(@ARGV, @conf_args) if @conf_args;
131}
132
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700133GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700134 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700135 'tree!' => \$tree,
136 'signoff!' => \$chk_signoff,
137 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700138 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800139 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700140 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700141 'subjective!' => \$check,
142 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700143 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700144 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700145 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800146 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700147 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700148 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800149 'summary!' => \$summary,
150 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800151 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700152 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800153 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700154 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800155 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700156 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700157 'codespell!' => \$codespell,
158 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700159 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700160 'h|help' => \$help,
161 'version' => \$help
162) or help(1);
163
164help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700165
Joe Perches9624b8d2014-01-23 15:54:44 -0800166$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700167$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800168
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700169my $exit = 0;
170
Dave Hansend62a2012013-09-11 14:23:56 -0700171if ($^V && $^V lt $minimum_perl_version) {
172 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
173 if (!$ignore_perl_version) {
174 exit(1);
175 }
176}
177
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700178if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700179 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700180 exit(1);
181}
182
Joe Perches91bfe482013-09-11 14:23:59 -0700183sub hash_save_array_words {
184 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700185
Joe Perches91bfe482013-09-11 14:23:59 -0700186 my @array = split(/,/, join(',', @$arrayRef));
187 foreach my $word (@array) {
188 $word =~ s/\s*\n?$//g;
189 $word =~ s/^\s*//g;
190 $word =~ s/\s+/ /g;
191 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700192
Joe Perches91bfe482013-09-11 14:23:59 -0700193 next if ($word =~ m/^\s*#/);
194 next if ($word =~ m/^\s*$/);
195
196 $hashRef->{$word}++;
197 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700198}
199
Joe Perches91bfe482013-09-11 14:23:59 -0700200sub hash_show_words {
201 my ($hashRef, $prefix) = @_;
202
Joe Perches58cb3cf2013-09-11 14:24:04 -0700203 if ($quiet == 0 && keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700204 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700205 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700206 print " $word";
207 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700208 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700209 }
210}
211
212hash_save_array_words(\%ignore_type, \@ignore);
213hash_save_array_words(\%use_type, \@use);
214
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800215my $dbg_values = 0;
216my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700217my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700218my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800219for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800220 ## no critic
221 eval "\${dbg_$key} = '$debug{$key}';";
222 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800223}
224
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700225my $rpt_cleaners = 0;
226
Andy Whitcroft8905a672007-11-28 16:21:06 -0800227if ($terse) {
228 $emacs = 1;
229 $quiet++;
230}
231
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700232if ($tree) {
233 if (defined $root) {
234 if (!top_of_kernel_tree($root)) {
235 die "$P: $root: --root does not point at a valid tree\n";
236 }
237 } else {
238 if (top_of_kernel_tree('.')) {
239 $root = '.';
240 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
241 top_of_kernel_tree($1)) {
242 $root = $1;
243 }
244 }
245
246 if (!defined $root) {
247 print "Must be run from the top-level dir. of a kernel tree\n";
248 exit(2);
249 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700250}
251
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700252my $emitted_corrupt = 0;
253
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700254our $Ident = qr{
255 [A-Za-z_][A-Za-z\d_]*
256 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
257 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700258our $Storage = qr{extern|static|asmlinkage};
259our $Sparse = qr{
260 __user|
261 __kernel|
262 __force|
263 __iomem|
264 __must_check|
265 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800266 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700267 __ref|
268 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700269 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800270our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
271our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
272our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
273our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
274our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700275
Wolfram Sang52131292010-03-05 13:43:51 -0800276# Notes to $Attribute:
277# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700278our $Attribute = qr{
279 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700280 __percpu|
281 __nocast|
282 __safe|
283 __bitwise__|
284 __packed__|
285 __packed2__|
286 __naked|
287 __maybe_unused|
288 __always_unused|
289 __noreturn|
290 __used|
291 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800292 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700293 __noclone|
294 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700295 __read_mostly|
296 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700297 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700298 ____cacheline_aligned|
299 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800300 ____cacheline_internodealigned_in_smp|
301 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700302 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700303our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700304our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700305our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
306our $Lval = qr{$Ident(?:$Member)*};
307
Joe Perches95e2c602013-07-03 15:05:20 -0700308our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
309our $Binary = qr{(?i)0b[01]+$Int_type?};
310our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
311our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700312our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800313our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800314our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
315our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
316our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800317our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700318our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800319our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700320our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700321our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700322our $Operators = qr{
323 <=|>=|==|!=|
324 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700325 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700326 }x;
327
Joe Perches91cb5192014-04-03 14:49:32 -0700328our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
329
Joe Perchesab7e23f2015-04-16 12:44:22 -0700330our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800331our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700332our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700333our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800334our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700335our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800336our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700337our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800338
Joe Perches15662b32011-10-31 17:13:12 -0700339our $NON_ASCII_UTF8 = qr{
340 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700341 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
342 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
343 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
344 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
345 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
346 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
347}x;
348
Joe Perches15662b32011-10-31 17:13:12 -0700349our $UTF8 = qr{
350 [\x09\x0A\x0D\x20-\x7E] # ASCII
351 | $NON_ASCII_UTF8
352}x;
353
Joe Perchese6176fa2015-06-25 15:02:49 -0700354our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800355our $typeOtherOSTypedefs = qr{(?x:
356 u_(?:char|short|int|long) | # bsd
357 u(?:nchar|short|int|long) # sysv
358)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700359our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700360 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700361 atomic_t
362)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700363our $typeTypedefs = qr{(?x:
364 $typeC99Typedefs\b|
365 $typeOtherOSTypedefs\b|
366 $typeKernelTypedefs\b
367)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700368
Joe Perches691e6692010-03-05 13:43:51 -0800369our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700370 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700371 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches6e60c022011-07-25 17:13:27 -0700372 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700373 panic|
Joe Perches06668722013-11-12 15:10:07 -0800374 MODULE_[A-Z_]+|
375 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800376)};
377
Joe Perches20112472011-07-25 17:13:23 -0700378our $signature_tags = qr{(?xi:
379 Signed-off-by:|
380 Acked-by:|
381 Tested-by:|
382 Reviewed-by:|
383 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700384 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700385 To:|
386 Cc:
387)};
388
Joe Perches18130872014-08-06 16:11:22 -0700389our @typeListMisordered = (
390 qr{char\s+(?:un)?signed},
391 qr{int\s+(?:(?:un)?signed\s+)?short\s},
392 qr{int\s+short(?:\s+(?:un)?signed)},
393 qr{short\s+int(?:\s+(?:un)?signed)},
394 qr{(?:un)?signed\s+int\s+short},
395 qr{short\s+(?:un)?signed},
396 qr{long\s+int\s+(?:un)?signed},
397 qr{int\s+long\s+(?:un)?signed},
398 qr{long\s+(?:un)?signed\s+int},
399 qr{int\s+(?:un)?signed\s+long},
400 qr{int\s+(?:un)?signed},
401 qr{int\s+long\s+long\s+(?:un)?signed},
402 qr{long\s+long\s+int\s+(?:un)?signed},
403 qr{long\s+long\s+(?:un)?signed\s+int},
404 qr{long\s+long\s+(?:un)?signed},
405 qr{long\s+(?:un)?signed},
406);
407
Andy Whitcroft8905a672007-11-28 16:21:06 -0800408our @typeList = (
409 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700410 qr{(?:(?:un)?signed\s+)?char},
411 qr{(?:(?:un)?signed\s+)?short\s+int},
412 qr{(?:(?:un)?signed\s+)?short},
413 qr{(?:(?:un)?signed\s+)?int},
414 qr{(?:(?:un)?signed\s+)?long\s+int},
415 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
416 qr{(?:(?:un)?signed\s+)?long\s+long},
417 qr{(?:(?:un)?signed\s+)?long},
418 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800419 qr{float},
420 qr{double},
421 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800422 qr{struct\s+$Ident},
423 qr{union\s+$Ident},
424 qr{enum\s+$Ident},
425 qr{${Ident}_t},
426 qr{${Ident}_handler},
427 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700428 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800429);
Alex Dowad485ff232015-06-25 15:02:52 -0700430our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700431our @typeListWithAttr = (
432 @typeList,
433 qr{struct\s+$InitAttribute\s+$Ident},
434 qr{union\s+$InitAttribute\s+$Ident},
435);
436
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700437our @modifierList = (
438 qr{fastcall},
439);
Alex Dowad485ff232015-06-25 15:02:52 -0700440our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800441
Joe Perches24358802014-04-03 14:49:13 -0700442our @mode_permission_funcs = (
443 ["module_param", 3],
444 ["module_param_(?:array|named|string)", 4],
445 ["module_param_array_named", 5],
446 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
447 ["proc_create(?:_data|)", 2],
448 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
449);
450
Joe Perches515a2352014-04-03 14:49:24 -0700451#Create a search pattern for all these functions to speed up a loop below
452our $mode_perms_search = "";
453foreach my $entry (@mode_permission_funcs) {
454 $mode_perms_search .= '|' if ($mode_perms_search ne "");
455 $mode_perms_search .= $entry->[0];
456}
457
Joe Perchesb392c642015-04-16 12:44:16 -0700458our $mode_perms_world_writable = qr{
459 S_IWUGO |
460 S_IWOTH |
461 S_IRWXUGO |
462 S_IALLUGO |
463 0[0-7][0-7][2367]
464}x;
465
Wolfram Sang7840a942010-08-09 17:20:57 -0700466our $allowed_asm_includes = qr{(?x:
467 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700468 memory|
469 time|
470 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700471)};
472# memory.h: ARM has a custom one
473
Kees Cook66b47b42014-10-13 15:51:57 -0700474# Load common spelling mistakes and build regular expression list.
475my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700476my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700477
Joe Perches36061e32014-12-10 15:51:43 -0800478if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800479 while (<$spelling>) {
480 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700481
Joe Perches36061e32014-12-10 15:51:43 -0800482 $line =~ s/\s*\n?$//g;
483 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700484
Joe Perches36061e32014-12-10 15:51:43 -0800485 next if ($line =~ m/^\s*#/);
486 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700487
Joe Perches36061e32014-12-10 15:51:43 -0800488 my ($suspect, $fix) = split(/\|\|/, $line);
489
Joe Perches36061e32014-12-10 15:51:43 -0800490 $spelling_fix{$suspect} = $fix;
491 }
492 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800493} else {
494 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700495}
Kees Cook66b47b42014-10-13 15:51:57 -0700496
Joe Perchesebfd7d62015-04-16 12:44:14 -0700497if ($codespell) {
498 if (open(my $spelling, '<', $codespellfile)) {
499 while (<$spelling>) {
500 my $line = $_;
501
502 $line =~ s/\s*\n?$//g;
503 $line =~ s/^\s*//g;
504
505 next if ($line =~ m/^\s*#/);
506 next if ($line =~ m/^\s*$/);
507 next if ($line =~ m/, disabled/i);
508
509 $line =~ s/,.*$//;
510
511 my ($suspect, $fix) = split(/->/, $line);
512
513 $spelling_fix{$suspect} = $fix;
514 }
515 close($spelling);
516 } else {
517 warn "No codespell typos will be found - file '$codespellfile': $!\n";
518 }
519}
520
521$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
522
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700524 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
525 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700526 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700527 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700528 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700529 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700530 (?:$typeTypedefs\b)|
531 (?:${all}\b)
532 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800533 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700534 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800535 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800536 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700537 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700538 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800539 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700540 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800541 }x;
Joe Perches18130872014-08-06 16:11:22 -0700542 $NonptrTypeMisordered = qr{
543 (?:$Modifier\s+|const\s+)*
544 (?:
545 (?:${Misordered}\b)
546 )
547 (?:\s+$Modifier|\s+const)*
548 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700549 $NonptrTypeWithAttr = qr{
550 (?:$Modifier\s+|const\s+)*
551 (?:
552 (?:typeof|__typeof__)\s*\([^\)]*\)|
553 (?:$typeTypedefs\b)|
554 (?:${allWithAttr}\b)
555 )
556 (?:\s+$Modifier|\s+const)*
557 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800558 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700559 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700560 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700561 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800562 }x;
Joe Perches18130872014-08-06 16:11:22 -0700563 $TypeMisordered = qr{
564 $NonptrTypeMisordered
565 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
566 (?:\s+$Inline|\s+$Modifier)*
567 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700568 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700569 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800570}
571build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700572
Joe Perches7d2367a2011-07-25 17:13:22 -0700573our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700574
575# Using $balanced_parens, $LvalOrFunc, or $FuncArg
576# requires at least perl version v5.10.0
577# Any use must be runtime checked with $^V
578
579our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700580our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800581our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700582
Joe Perchesf8422302014-08-06 16:11:31 -0700583our $declaration_macros = qr{(?x:
584 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
585 (?:$Storage\s+)?LIST_HEAD\s*\(|
586 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
587)};
588
Joe Perches7d2367a2011-07-25 17:13:22 -0700589sub deparenthesize {
590 my ($string) = @_;
591 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700592
593 while ($string =~ /^\s*\(.*\)\s*$/) {
594 $string =~ s@^\s*\(\s*@@;
595 $string =~ s@\s*\)\s*$@@;
596 }
597
Joe Perches7d2367a2011-07-25 17:13:22 -0700598 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700599
Joe Perches7d2367a2011-07-25 17:13:22 -0700600 return $string;
601}
602
Joe Perches34456862013-07-03 15:05:34 -0700603sub seed_camelcase_file {
604 my ($file) = @_;
605
606 return if (!(-f $file));
607
608 local $/;
609
610 open(my $include_file, '<', "$file")
611 or warn "$P: Can't read '$file' $!\n";
612 my $text = <$include_file>;
613 close($include_file);
614
615 my @lines = split('\n', $text);
616
617 foreach my $line (@lines) {
618 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
619 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
620 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800621 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
622 $camelcase{$1} = 1;
623 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700624 $camelcase{$1} = 1;
625 }
626 }
627}
628
629my $camelcase_seeded = 0;
630sub seed_camelcase_includes {
631 return if ($camelcase_seeded);
632
633 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700634 my $camelcase_cache = "";
635 my @include_files = ();
636
637 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700638
Richard Genoud3645e322014-02-10 14:25:32 -0800639 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700640 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
641 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700642 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700643 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700644 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700645 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700646 @include_files = split('\n', $files);
647 foreach my $file (@include_files) {
648 my $date = POSIX::strftime("%Y%m%d%H%M",
649 localtime((stat $file)[9]));
650 $last_mod_date = $date if ($last_mod_date < $date);
651 }
652 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700653 }
Joe Perchesc707a812013-07-08 16:00:43 -0700654
655 if ($camelcase_cache ne "" && -f $camelcase_cache) {
656 open(my $camelcase_file, '<', "$camelcase_cache")
657 or warn "$P: Can't read '$camelcase_cache' $!\n";
658 while (<$camelcase_file>) {
659 chomp;
660 $camelcase{$_} = 1;
661 }
662 close($camelcase_file);
663
664 return;
665 }
666
Richard Genoud3645e322014-02-10 14:25:32 -0800667 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700668 $files = `git ls-files "include/*.h"`;
669 @include_files = split('\n', $files);
670 }
671
Joe Perches34456862013-07-03 15:05:34 -0700672 foreach my $file (@include_files) {
673 seed_camelcase_file($file);
674 }
Joe Perches351b2a12013-07-03 15:05:36 -0700675
Joe Perchesc707a812013-07-08 16:00:43 -0700676 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700677 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700678 open(my $camelcase_file, '>', "$camelcase_cache")
679 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700680 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
681 print $camelcase_file ("$_\n");
682 }
683 close($camelcase_file);
684 }
Joe Perches34456862013-07-03 15:05:34 -0700685}
686
Joe Perchesd311cd42014-08-06 16:10:57 -0700687sub git_commit_info {
688 my ($commit, $id, $desc) = @_;
689
690 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
691
692 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
693 $output =~ s/^\s*//gm;
694 my @lines = split("\n", $output);
695
Joe Perches0d7835f2015-02-13 14:38:35 -0800696 return ($id, $desc) if ($#lines < 0);
697
Joe Perchesd311cd42014-08-06 16:10:57 -0700698 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
699# Maybe one day convert this block of bash into something that returns
700# all matching commit ids, but it's very slow...
701#
702# echo "checking commits $1..."
703# git rev-list --remotes | grep -i "^$1" |
704# while read line ; do
705# git log --format='%H %s' -1 $line |
706# echo "commit $(cut -c 1-12,41-)"
707# done
708 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
709 } else {
710 $id = substr($lines[0], 0, 12);
711 $desc = substr($lines[0], 41);
712 }
713
714 return ($id, $desc);
715}
716
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700717$chk_signoff = 0 if ($file);
718
Andy Whitcroft00df3442007-06-08 13:47:06 -0700719my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800720my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700721my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700722my @fixed_inserted = ();
723my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700724my $fixlinenr = -1;
725
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800726my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700727for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800728 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700729 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800730 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700731 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800732 } elsif ($filename eq '-') {
733 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700734 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800735 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700736 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700737 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800738 if ($filename eq '-') {
739 $vname = 'Your patch';
740 } else {
741 $vname = $filename;
742 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800743 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700744 chomp;
745 push(@rawlines, $_);
746 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800747 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700748
749 if ($#ARGV > 0 && $quiet == 0) {
750 print '-' x length($vname) . "\n";
751 print "$vname\n";
752 print '-' x length($vname) . "\n";
753 }
754
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800755 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700756 $exit = 1;
757 }
758 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800759 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700760 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700761 @fixed_inserted = ();
762 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700763 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700764 @modifierListFile = ();
765 @typeListFile = ();
766 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700767}
768
Joe Perchesd8469f12015-06-25 15:03:00 -0700769if (!$quiet) {
770 if ($^V lt 5.10.0) {
771 print << "EOM"
772
773NOTE: perl $^V is not modern enough to detect all possible issues.
774 An upgrade to at least perl v5.10.0 is suggested.
775EOM
776 }
777 if ($exit) {
778 print << "EOM"
779
780NOTE: If any of the errors are false positives, please report
781 them to the maintainer, see CHECKPATCH in MAINTAINERS.
782EOM
783 }
784}
785
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700786exit($exit);
787
788sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700789 my ($root) = @_;
790
791 my @tree_check = (
792 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
793 "README", "Documentation", "arch", "include", "drivers",
794 "fs", "init", "ipc", "kernel", "lib", "scripts",
795 );
796
797 foreach my $check (@tree_check) {
798 if (! -e $root . '/' . $check) {
799 return 0;
800 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700801 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700802 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700803}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700804
Joe Perches20112472011-07-25 17:13:23 -0700805sub parse_email {
806 my ($formatted_email) = @_;
807
808 my $name = "";
809 my $address = "";
810 my $comment = "";
811
812 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
813 $name = $1;
814 $address = $2;
815 $comment = $3 if defined $3;
816 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
817 $address = $1;
818 $comment = $2 if defined $2;
819 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
820 $address = $1;
821 $comment = $2 if defined $2;
822 $formatted_email =~ s/$address.*$//;
823 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700824 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700825 $name =~ s/^\"|\"$//g;
826 # If there's a name left after stripping spaces and
827 # leading quotes, and the address doesn't have both
828 # leading and trailing angle brackets, the address
829 # is invalid. ie:
830 # "joe smith joe@smith.com" bad
831 # "joe smith <joe@smith.com" bad
832 if ($name ne "" && $address !~ /^<[^>]+>$/) {
833 $name = "";
834 $address = "";
835 $comment = "";
836 }
837 }
838
Joe Perches3705ce52013-07-03 15:05:31 -0700839 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700840 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700841 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700842 $address =~ s/^\<|\>$//g;
843
844 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
845 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
846 $name = "\"$name\"";
847 }
848
849 return ($name, $address, $comment);
850}
851
852sub format_email {
853 my ($name, $address) = @_;
854
855 my $formatted_email;
856
Joe Perches3705ce52013-07-03 15:05:31 -0700857 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700858 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700859 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700860
861 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
862 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
863 $name = "\"$name\"";
864 }
865
866 if ("$name" eq "") {
867 $formatted_email = "$address";
868 } else {
869 $formatted_email = "$name <$address>";
870 }
871
872 return $formatted_email;
873}
874
Joe Perchesd311cd42014-08-06 16:10:57 -0700875sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700876 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700877
Joe Perchesbd474ca2014-08-06 16:11:10 -0700878 foreach my $path (split(/:/, $ENV{PATH})) {
879 if (-e "$path/$bin") {
880 return "$path/$bin";
881 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700882 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700883
Joe Perchesbd474ca2014-08-06 16:11:10 -0700884 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700885}
886
Joe Perches000d1cc12011-07-25 17:13:25 -0700887sub which_conf {
888 my ($conf) = @_;
889
890 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
891 if (-e "$path/$conf") {
892 return "$path/$conf";
893 }
894 }
895
896 return "";
897}
898
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700899sub expand_tabs {
900 my ($str) = @_;
901
902 my $res = '';
903 my $n = 0;
904 for my $c (split(//, $str)) {
905 if ($c eq "\t") {
906 $res .= ' ';
907 $n++;
908 for (; ($n % 8) != 0; $n++) {
909 $res .= ' ';
910 }
911 next;
912 }
913 $res .= $c;
914 $n++;
915 }
916
917 return $res;
918}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700919sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700920 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700921 return $res;
922}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700923
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700924sub line_stats {
925 my ($line) = @_;
926
927 # Drop the diff line leader and expand tabs
928 $line =~ s/^.//;
929 $line = expand_tabs($line);
930
931 # Pick the indent from the front of the line.
932 my ($white) = ($line =~ /^(\s*)/);
933
934 return (length($line), length($white));
935}
936
Andy Whitcroft773647a2008-03-28 14:15:58 -0700937my $sanitise_quote = '';
938
939sub sanitise_line_reset {
940 my ($in_comment) = @_;
941
942 if ($in_comment) {
943 $sanitise_quote = '*/';
944 } else {
945 $sanitise_quote = '';
946 }
947}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700948sub sanitise_line {
949 my ($line) = @_;
950
951 my $res = '';
952 my $l = '';
953
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800954 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700955 my $off = 0;
956 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700957
Andy Whitcroft773647a2008-03-28 14:15:58 -0700958 # Always copy over the diff marker.
959 $res = substr($line, 0, 1);
960
961 for ($off = 1; $off < length($line); $off++) {
962 $c = substr($line, $off, 1);
963
964 # Comments we are wacking completly including the begin
965 # and end, all to $;.
966 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
967 $sanitise_quote = '*/';
968
969 substr($res, $off, 2, "$;$;");
970 $off++;
971 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800972 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700973 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700974 $sanitise_quote = '';
975 substr($res, $off, 2, "$;$;");
976 $off++;
977 next;
978 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700979 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
980 $sanitise_quote = '//';
981
982 substr($res, $off, 2, $sanitise_quote);
983 $off++;
984 next;
985 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700986
987 # A \ in a string means ignore the next character.
988 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
989 $c eq "\\") {
990 substr($res, $off, 2, 'XX');
991 $off++;
992 next;
993 }
994 # Regular quotes.
995 if ($c eq "'" || $c eq '"') {
996 if ($sanitise_quote eq '') {
997 $sanitise_quote = $c;
998
999 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001000 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001001 } elsif ($sanitise_quote eq $c) {
1002 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001003 }
1004 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001005
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001006 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001007 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1008 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001009 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1010 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001011 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1012 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001013 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001014 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001015 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001016 }
1017
Daniel Walker113f04a2009-09-21 17:04:35 -07001018 if ($sanitise_quote eq '//') {
1019 $sanitise_quote = '';
1020 }
1021
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001022 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001023 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001024 my $clean = 'X' x length($1);
1025 $res =~ s@\<.*\>@<$clean>@;
1026
1027 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001028 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001029 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001030 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001031 }
1032
Andy Whitcroft00df3442007-06-08 13:47:06 -07001033 return $res;
1034}
1035
Joe Perchesa6962d72013-04-29 16:18:13 -07001036sub get_quoted_string {
1037 my ($line, $rawline) = @_;
1038
Joe Perches33acb542015-06-25 15:02:54 -07001039 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001040 return substr($rawline, $-[0], $+[0] - $-[0]);
1041}
1042
Andy Whitcroft8905a672007-11-28 16:21:06 -08001043sub ctx_statement_block {
1044 my ($linenr, $remain, $off) = @_;
1045 my $line = $linenr - 1;
1046 my $blk = '';
1047 my $soff = $off;
1048 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001049 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001050
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001051 my $loff = 0;
1052
Andy Whitcroft8905a672007-11-28 16:21:06 -08001053 my $type = '';
1054 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001055 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001056 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001057 my $c;
1058 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001059
1060 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001061 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001062 @stack = (['', 0]) if ($#stack == -1);
1063
Andy Whitcroft773647a2008-03-28 14:15:58 -07001064 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001065 # If we are about to drop off the end, pull in more
1066 # context.
1067 if ($off >= $len) {
1068 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001069 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001070 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001071 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001072 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001073 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001074 $len = length($blk);
1075 $line++;
1076 last;
1077 }
1078 # Bail if there is no further context.
1079 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001080 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001081 last;
1082 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001083 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1084 $level++;
1085 $type = '#';
1086 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001087 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001088 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001089 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001090 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001091
Andy Whitcroft773647a2008-03-28 14:15:58 -07001092 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001093
1094 # Handle nested #if/#else.
1095 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1096 push(@stack, [ $type, $level ]);
1097 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1098 ($type, $level) = @{$stack[$#stack - 1]};
1099 } elsif ($remainder =~ /^#\s*endif\b/) {
1100 ($type, $level) = @{pop(@stack)};
1101 }
1102
Andy Whitcroft8905a672007-11-28 16:21:06 -08001103 # Statement ends at the ';' or a close '}' at the
1104 # outermost level.
1105 if ($level == 0 && $c eq ';') {
1106 last;
1107 }
1108
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001109 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001110 if ($level == 0 && $coff_set == 0 &&
1111 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1112 $remainder =~ /^(else)(?:\s|{)/ &&
1113 $remainder !~ /^else\s+if\b/) {
1114 $coff = $off + length($1) - 1;
1115 $coff_set = 1;
1116 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1117 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001118 }
1119
Andy Whitcroft8905a672007-11-28 16:21:06 -08001120 if (($type eq '' || $type eq '(') && $c eq '(') {
1121 $level++;
1122 $type = '(';
1123 }
1124 if ($type eq '(' && $c eq ')') {
1125 $level--;
1126 $type = ($level != 0)? '(' : '';
1127
1128 if ($level == 0 && $coff < $soff) {
1129 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001130 $coff_set = 1;
1131 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001132 }
1133 }
1134 if (($type eq '' || $type eq '{') && $c eq '{') {
1135 $level++;
1136 $type = '{';
1137 }
1138 if ($type eq '{' && $c eq '}') {
1139 $level--;
1140 $type = ($level != 0)? '{' : '';
1141
1142 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001143 if (substr($blk, $off + 1, 1) eq ';') {
1144 $off++;
1145 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001146 last;
1147 }
1148 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001149 # Preprocessor commands end at the newline unless escaped.
1150 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1151 $level--;
1152 $type = '';
1153 $off++;
1154 last;
1155 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001156 $off++;
1157 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001158 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001159 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001160 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001161 $line++;
1162 $remain--;
1163 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001164
1165 my $statement = substr($blk, $soff, $off - $soff + 1);
1166 my $condition = substr($blk, $soff, $coff - $soff + 1);
1167
1168 #warn "STATEMENT<$statement>\n";
1169 #warn "CONDITION<$condition>\n";
1170
Andy Whitcroft773647a2008-03-28 14:15:58 -07001171 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001172
1173 return ($statement, $condition,
1174 $line, $remain + 1, $off - $loff + 1, $level);
1175}
1176
Andy Whitcroftcf655042008-03-04 14:28:20 -08001177sub statement_lines {
1178 my ($stmt) = @_;
1179
1180 # Strip the diff line prefixes and rip blank lines at start and end.
1181 $stmt =~ s/(^|\n)./$1/g;
1182 $stmt =~ s/^\s*//;
1183 $stmt =~ s/\s*$//;
1184
1185 my @stmt_lines = ($stmt =~ /\n/g);
1186
1187 return $#stmt_lines + 2;
1188}
1189
1190sub statement_rawlines {
1191 my ($stmt) = @_;
1192
1193 my @stmt_lines = ($stmt =~ /\n/g);
1194
1195 return $#stmt_lines + 2;
1196}
1197
1198sub statement_block_size {
1199 my ($stmt) = @_;
1200
1201 $stmt =~ s/(^|\n)./$1/g;
1202 $stmt =~ s/^\s*{//;
1203 $stmt =~ s/}\s*$//;
1204 $stmt =~ s/^\s*//;
1205 $stmt =~ s/\s*$//;
1206
1207 my @stmt_lines = ($stmt =~ /\n/g);
1208 my @stmt_statements = ($stmt =~ /;/g);
1209
1210 my $stmt_lines = $#stmt_lines + 2;
1211 my $stmt_statements = $#stmt_statements + 1;
1212
1213 if ($stmt_lines > $stmt_statements) {
1214 return $stmt_lines;
1215 } else {
1216 return $stmt_statements;
1217 }
1218}
1219
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001220sub ctx_statement_full {
1221 my ($linenr, $remain, $off) = @_;
1222 my ($statement, $condition, $level);
1223
1224 my (@chunks);
1225
Andy Whitcroftcf655042008-03-04 14:28:20 -08001226 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001227 ($statement, $condition, $linenr, $remain, $off, $level) =
1228 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001229 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001230 push(@chunks, [ $condition, $statement ]);
1231 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1232 return ($level, $linenr, @chunks);
1233 }
1234
1235 # Pull in the following conditional/block pairs and see if they
1236 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001237 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001238 ($statement, $condition, $linenr, $remain, $off, $level) =
1239 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001240 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001241 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001242 #print "C: push\n";
1243 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001244 }
1245
1246 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001247}
1248
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001249sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001250 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001251 my $line;
1252 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001253 my $blk = '';
1254 my @o;
1255 my @c;
1256 my @res = ();
1257
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001258 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001259 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001260 for ($line = $start; $remain > 0; $line++) {
1261 next if ($rawlines[$line] =~ /^-/);
1262 $remain--;
1263
1264 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001265
1266 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001267 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001268 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001269 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001270 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001271 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001272 $level = pop(@stack);
1273 }
1274
Andy Whitcroft01464f32010-10-26 14:23:19 -07001275 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001276 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1277 if ($off > 0) {
1278 $off--;
1279 next;
1280 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001281
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001282 if ($c eq $close && $level > 0) {
1283 $level--;
1284 last if ($level == 0);
1285 } elsif ($c eq $open) {
1286 $level++;
1287 }
1288 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001289
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001290 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001291 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001292 }
1293
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001294 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001295 }
1296
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001297 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001298}
1299sub ctx_block_outer {
1300 my ($linenr, $remain) = @_;
1301
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001302 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1303 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001304}
1305sub ctx_block {
1306 my ($linenr, $remain) = @_;
1307
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001308 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1309 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001310}
1311sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001312 my ($linenr, $remain, $off) = @_;
1313
1314 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1315 return @r;
1316}
1317sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001318 my ($linenr, $remain) = @_;
1319
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001320 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001321}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001322sub ctx_statement_level {
1323 my ($linenr, $remain, $off) = @_;
1324
1325 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1326}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001327
1328sub ctx_locate_comment {
1329 my ($first_line, $end_line) = @_;
1330
1331 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001332 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001333 return $current_comment if (defined $current_comment);
1334
1335 # Look through the context and try and figure out if there is a
1336 # comment.
1337 my $in_comment = 0;
1338 $current_comment = '';
1339 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001340 my $line = $rawlines[$linenr - 1];
1341 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001342 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1343 $in_comment = 1;
1344 }
1345 if ($line =~ m@/\*@) {
1346 $in_comment = 1;
1347 }
1348 if (!$in_comment && $current_comment ne '') {
1349 $current_comment = '';
1350 }
1351 $current_comment .= $line . "\n" if ($in_comment);
1352 if ($line =~ m@\*/@) {
1353 $in_comment = 0;
1354 }
1355 }
1356
1357 chomp($current_comment);
1358 return($current_comment);
1359}
1360sub ctx_has_comment {
1361 my ($first_line, $end_line) = @_;
1362 my $cmt = ctx_locate_comment($first_line, $end_line);
1363
Andy Whitcroft00df3442007-06-08 13:47:06 -07001364 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001365 ##print "CMMT: $cmt\n";
1366
1367 return ($cmt ne '');
1368}
1369
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001370sub raw_line {
1371 my ($linenr, $cnt) = @_;
1372
1373 my $offset = $linenr - 1;
1374 $cnt++;
1375
1376 my $line;
1377 while ($cnt) {
1378 $line = $rawlines[$offset++];
1379 next if (defined($line) && $line =~ /^-/);
1380 $cnt--;
1381 }
1382
1383 return $line;
1384}
1385
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001386sub cat_vet {
1387 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001388 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001389
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001390 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001391 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1392 $res .= $1;
1393 if ($2 ne '') {
1394 $coded = sprintf("^%c", unpack('C', $2) + 64);
1395 $res .= $coded;
1396 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001397 }
1398 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001399
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001400 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001401}
1402
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001403my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001404my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001405my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001406my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001407
1408sub annotate_reset {
1409 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001410 $av_pending = '_';
1411 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001412 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001413}
1414
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001415sub annotate_values {
1416 my ($stream, $type) = @_;
1417
1418 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001419 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001420 my $cur = $stream;
1421
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001422 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001423
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001424 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001425 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001426 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001427 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001428 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001429 print "WS($1)\n" if ($dbg_values > 1);
1430 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001431 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001432 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001433 }
1434
Florian Micklerc023e4732011-01-12 16:59:58 -08001435 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001436 print "CAST($1)\n" if ($dbg_values > 1);
1437 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001438 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001439
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001440 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001441 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001442 $type = 'T';
1443
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001444 } elsif ($cur =~ /^($Modifier)\s*/) {
1445 print "MODIFIER($1)\n" if ($dbg_values > 1);
1446 $type = 'T';
1447
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001448 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001449 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001450 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001451 push(@av_paren_type, $type);
1452 if ($2 ne '') {
1453 $av_pending = 'N';
1454 }
1455 $type = 'E';
1456
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001457 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001458 print "UNDEF($1)\n" if ($dbg_values > 1);
1459 $av_preprocessor = 1;
1460 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001461
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001462 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001463 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001464 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001465
1466 push(@av_paren_type, $type);
1467 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001468 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001469
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001470 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001471 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1472 $av_preprocessor = 1;
1473
1474 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1475
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001476 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001477
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001478 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001479 print "PRE_END($1)\n" if ($dbg_values > 1);
1480
1481 $av_preprocessor = 1;
1482
1483 # Assume all arms of the conditional end as this
1484 # one does, and continue as if the #endif was not here.
1485 pop(@av_paren_type);
1486 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001487 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001488
1489 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001490 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001491
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001492 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1493 print "ATTR($1)\n" if ($dbg_values > 1);
1494 $av_pending = $type;
1495 $type = 'N';
1496
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001497 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001498 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001499 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001500 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001501 }
1502 $type = 'N';
1503
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001504 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001505 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001506 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001507 $type = 'N';
1508
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001509 } elsif ($cur =~/^(case)/o) {
1510 print "CASE($1)\n" if ($dbg_values > 1);
1511 $av_pend_colon = 'C';
1512 $type = 'N';
1513
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001514 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001515 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001516 $type = 'N';
1517
1518 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001519 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001520 push(@av_paren_type, $av_pending);
1521 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001522 $type = 'N';
1523
1524 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001525 my $new_type = pop(@av_paren_type);
1526 if ($new_type ne '_') {
1527 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001528 print "PAREN('$1') -> $type\n"
1529 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001530 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001531 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001532 }
1533
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001534 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001535 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001536 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001537 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001538
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001539 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1540 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001541 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001542 } elsif ($type eq 'E') {
1543 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001544 }
1545 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1546 $type = 'V';
1547
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001548 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001549 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001550 $type = 'V';
1551
1552 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001553 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001554 $type = 'N';
1555
Andy Whitcroftcf655042008-03-04 14:28:20 -08001556 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001557 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001558 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001559 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001560
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001561 } elsif ($cur =~/^(,)/) {
1562 print "COMMA($1)\n" if ($dbg_values > 1);
1563 $type = 'C';
1564
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001565 } elsif ($cur =~ /^(\?)/o) {
1566 print "QUESTION($1)\n" if ($dbg_values > 1);
1567 $type = 'N';
1568
1569 } elsif ($cur =~ /^(:)/o) {
1570 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1571
1572 substr($var, length($res), 1, $av_pend_colon);
1573 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1574 $type = 'E';
1575 } else {
1576 $type = 'N';
1577 }
1578 $av_pend_colon = 'O';
1579
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001580 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001581 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001582 $type = 'N';
1583
Andy Whitcroft0d413862008-10-15 22:02:16 -07001584 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001585 my $variant;
1586
1587 print "OPV($1)\n" if ($dbg_values > 1);
1588 if ($type eq 'V') {
1589 $variant = 'B';
1590 } else {
1591 $variant = 'U';
1592 }
1593
1594 substr($var, length($res), 1, $variant);
1595 $type = 'N';
1596
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001597 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001598 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001599 if ($1 ne '++' && $1 ne '--') {
1600 $type = 'N';
1601 }
1602
1603 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001604 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001605 }
1606 if (defined $1) {
1607 $cur = substr($cur, length($1));
1608 $res .= $type x length($1);
1609 }
1610 }
1611
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001612 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001613}
1614
Andy Whitcroft8905a672007-11-28 16:21:06 -08001615sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001616 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001617 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001618 ^(?:
1619 $Modifier|
1620 $Storage|
1621 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001622 DEFINE_\S+
1623 )$|
1624 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001625 goto|
1626 return|
1627 case|
1628 else|
1629 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001630 do|
1631 \#|
1632 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001633 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001634 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001635 )}x;
1636 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1637 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001638 # Check for modifiers.
1639 $possible =~ s/\s*$Storage\s*//g;
1640 $possible =~ s/\s*$Sparse\s*//g;
1641 if ($possible =~ /^\s*$/) {
1642
1643 } elsif ($possible =~ /\s/) {
1644 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001645 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001646 if ($modifier !~ $notPermitted) {
1647 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001648 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001649 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001650 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001651
1652 } else {
1653 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001654 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001655 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001656 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001657 } else {
1658 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001659 }
1660}
1661
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001662my $prefix = '';
1663
Joe Perches000d1cc12011-07-25 17:13:25 -07001664sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001665 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001666
Joe Perchescbec18a2014-04-03 14:49:19 -07001667 return defined $use_type{$type} if (scalar keys %use_type > 0);
1668
1669 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001670}
1671
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001672sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001673 my ($level, $type, $msg) = @_;
1674
1675 if (!show_type($type) ||
1676 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001677 return 0;
1678 }
Joe Perches57230292015-06-25 15:03:03 -07001679 my $output = '';
1680 if (-t STDOUT && $color) {
1681 if ($level eq 'ERROR') {
1682 $output .= RED;
1683 } elsif ($level eq 'WARNING') {
1684 $output .= YELLOW;
1685 } else {
1686 $output .= GREEN;
1687 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001688 }
Joe Perches57230292015-06-25 15:03:03 -07001689 $output .= $prefix . $level . ':';
1690 if ($show_types) {
1691 $output .= BLUE if (-t STDOUT && $color);
1692 $output .= "$type:";
1693 }
1694 $output .= RESET if (-t STDOUT && $color);
1695 $output .= ' ' . $msg . "\n";
1696 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001697
Joe Perches57230292015-06-25 15:03:03 -07001698 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001699
1700 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001701}
Joe Perchescbec18a2014-04-03 14:49:19 -07001702
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001703sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001704 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001705}
Joe Perches000d1cc12011-07-25 17:13:25 -07001706
Joe Perchesd752fcc2014-08-06 16:11:05 -07001707sub fixup_current_range {
1708 my ($lineRef, $offset, $length) = @_;
1709
1710 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1711 my $o = $1;
1712 my $l = $2;
1713 my $no = $o + $offset;
1714 my $nl = $l + $length;
1715 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1716 }
1717}
1718
1719sub fix_inserted_deleted_lines {
1720 my ($linesRef, $insertedRef, $deletedRef) = @_;
1721
1722 my $range_last_linenr = 0;
1723 my $delta_offset = 0;
1724
1725 my $old_linenr = 0;
1726 my $new_linenr = 0;
1727
1728 my $next_insert = 0;
1729 my $next_delete = 0;
1730
1731 my @lines = ();
1732
1733 my $inserted = @{$insertedRef}[$next_insert++];
1734 my $deleted = @{$deletedRef}[$next_delete++];
1735
1736 foreach my $old_line (@{$linesRef}) {
1737 my $save_line = 1;
1738 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001739 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001740 $delta_offset = 0;
1741 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1742 $range_last_linenr = $new_linenr;
1743 fixup_current_range(\$line, $delta_offset, 0);
1744 }
1745
1746 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1747 $deleted = @{$deletedRef}[$next_delete++];
1748 $save_line = 0;
1749 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1750 }
1751
1752 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1753 push(@lines, ${$inserted}{'LINE'});
1754 $inserted = @{$insertedRef}[$next_insert++];
1755 $new_linenr++;
1756 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1757 }
1758
1759 if ($save_line) {
1760 push(@lines, $line);
1761 $new_linenr++;
1762 }
1763
1764 $old_linenr++;
1765 }
1766
1767 return @lines;
1768}
1769
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001770sub fix_insert_line {
1771 my ($linenr, $line) = @_;
1772
1773 my $inserted = {
1774 LINENR => $linenr,
1775 LINE => $line,
1776 };
1777 push(@fixed_inserted, $inserted);
1778}
1779
1780sub fix_delete_line {
1781 my ($linenr, $line) = @_;
1782
1783 my $deleted = {
1784 LINENR => $linenr,
1785 LINE => $line,
1786 };
1787
1788 push(@fixed_deleted, $deleted);
1789}
1790
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001791sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001792 my ($type, $msg) = @_;
1793
1794 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001795 our $clean = 0;
1796 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001797 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001798 }
Joe Perches3705ce52013-07-03 15:05:31 -07001799 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001800}
1801sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001802 my ($type, $msg) = @_;
1803
1804 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001805 our $clean = 0;
1806 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001807 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001808 }
Joe Perches3705ce52013-07-03 15:05:31 -07001809 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001810}
1811sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001812 my ($type, $msg) = @_;
1813
1814 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001815 our $clean = 0;
1816 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001817 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001818 }
Joe Perches3705ce52013-07-03 15:05:31 -07001819 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001820}
1821
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001822sub check_absolute_file {
1823 my ($absolute, $herecurr) = @_;
1824 my $file = $absolute;
1825
1826 ##print "absolute<$absolute>\n";
1827
1828 # See if any suffix of this path is a path within the tree.
1829 while ($file =~ s@^[^/]*/@@) {
1830 if (-f "$root/$file") {
1831 ##print "file<$file>\n";
1832 last;
1833 }
1834 }
1835 if (! -f _) {
1836 return 0;
1837 }
1838
1839 # It is, so see if the prefix is acceptable.
1840 my $prefix = $absolute;
1841 substr($prefix, -length($file)) = '';
1842
1843 ##print "prefix<$prefix>\n";
1844 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001845 WARN("USE_RELATIVE_PATH",
1846 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001847 }
1848}
1849
Joe Perches3705ce52013-07-03 15:05:31 -07001850sub trim {
1851 my ($string) = @_;
1852
Joe Perchesb34c6482013-09-11 14:24:01 -07001853 $string =~ s/^\s+|\s+$//g;
1854
1855 return $string;
1856}
1857
1858sub ltrim {
1859 my ($string) = @_;
1860
1861 $string =~ s/^\s+//;
1862
1863 return $string;
1864}
1865
1866sub rtrim {
1867 my ($string) = @_;
1868
1869 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001870
1871 return $string;
1872}
1873
Joe Perches52ea8502013-11-12 15:10:09 -08001874sub string_find_replace {
1875 my ($string, $find, $replace) = @_;
1876
1877 $string =~ s/$find/$replace/g;
1878
1879 return $string;
1880}
1881
Joe Perches3705ce52013-07-03 15:05:31 -07001882sub tabify {
1883 my ($leading) = @_;
1884
1885 my $source_indent = 8;
1886 my $max_spaces_before_tab = $source_indent - 1;
1887 my $spaces_to_tab = " " x $source_indent;
1888
1889 #convert leading spaces to tabs
1890 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1891 #Remove spaces before a tab
1892 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1893
1894 return "$leading";
1895}
1896
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001897sub pos_last_openparen {
1898 my ($line) = @_;
1899
1900 my $pos = 0;
1901
1902 my $opens = $line =~ tr/\(/\(/;
1903 my $closes = $line =~ tr/\)/\)/;
1904
1905 my $last_openparen = 0;
1906
1907 if (($opens == 0) || ($closes >= $opens)) {
1908 return -1;
1909 }
1910
1911 my $len = length($line);
1912
1913 for ($pos = 0; $pos < $len; $pos++) {
1914 my $string = substr($line, $pos);
1915 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1916 $pos += length($1) - 1;
1917 } elsif (substr($line, $pos, 1) eq '(') {
1918 $last_openparen = $pos;
1919 } elsif (index($string, '(') == -1) {
1920 last;
1921 }
1922 }
1923
Joe Perches91cb5192014-04-03 14:49:32 -07001924 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001925}
1926
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001927sub process {
1928 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001929
1930 my $linenr=0;
1931 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001932 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001933 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001934 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001935
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001936 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001937 my $indent;
1938 my $previndent=0;
1939 my $stashindent=0;
1940
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001941 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001942 my $signoff = 0;
1943 my $is_patch = 0;
1944
Joe Perches29ee1b02014-08-06 16:10:35 -07001945 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001946 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches2a076f42015-04-16 12:44:28 -07001947 my $commit_log_long_line = 0;
Joe Perches13f19372014-08-06 16:10:59 -07001948 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001949 my $non_utf8_charset = 0;
1950
Joe Perches365dd4e2014-08-06 16:10:42 -07001951 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001952 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001953
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001954 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001955 our $cnt_lines = 0;
1956 our $cnt_error = 0;
1957 our $cnt_warn = 0;
1958 our $cnt_chk = 0;
1959
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001960 # Trace the real file/line as we go.
1961 my $realfile = '';
1962 my $realline = 0;
1963 my $realcnt = 0;
1964 my $here = '';
1965 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001966 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001967 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001968 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001969
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001970 my $prev_values = 'E';
1971
1972 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001973 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001974 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001975 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001976 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001977
Joe Perches7e51f192013-09-11 14:23:57 -07001978 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001979
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001980 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001981 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001982 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001983 my @setup_docs = ();
1984 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001985
Joe Perchesd8b07712013-11-12 15:10:06 -08001986 my $camelcase_file_seeded = 0;
1987
Andy Whitcroft773647a2008-03-28 14:15:58 -07001988 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001989 my $line;
1990 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001991 $linenr++;
1992 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001993
Joe Perches3705ce52013-07-03 15:05:31 -07001994 push(@fixed, $rawline) if ($fix);
1995
Andy Whitcroft773647a2008-03-28 14:15:58 -07001996 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001997 $setup_docs = 0;
1998 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1999 $setup_docs = 1;
2000 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002001 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002002 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002003 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2004 $realline=$1-1;
2005 if (defined $2) {
2006 $realcnt=$3+1;
2007 } else {
2008 $realcnt=1+1;
2009 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002010 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002011
2012 # Guestimate if this is a continuing comment. Run
2013 # the context looking for a comment "edge". If this
2014 # edge is a close comment then we must be in a comment
2015 # at context start.
2016 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002017 my $cnt = $realcnt;
2018 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2019 next if (defined $rawlines[$ln - 1] &&
2020 $rawlines[$ln - 1] =~ /^-/);
2021 $cnt--;
2022 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002023 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002024 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2025 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2026 ($edge) = $1;
2027 last;
2028 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002029 }
2030 if (defined $edge && $edge eq '*/') {
2031 $in_comment = 1;
2032 }
2033
2034 # Guestimate if this is a continuing comment. If this
2035 # is the start of a diff block and this line starts
2036 # ' *' then it is very likely a comment.
2037 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002038 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002039 {
2040 $in_comment = 1;
2041 }
2042
2043 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2044 sanitise_line_reset($in_comment);
2045
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002046 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002047 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002048 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002049 $line = sanitise_line($rawline);
2050 }
2051 push(@lines, $line);
2052
2053 if ($realcnt > 1) {
2054 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2055 } else {
2056 $realcnt = 0;
2057 }
2058
2059 #print "==>$rawline\n";
2060 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002061
2062 if ($setup_docs && $line =~ /^\+/) {
2063 push(@setup_docs, $line);
2064 }
2065 }
2066
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002067 $prefix = '';
2068
Andy Whitcroft773647a2008-03-28 14:15:58 -07002069 $realcnt = 0;
2070 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002071 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002072 foreach my $line (@lines) {
2073 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002074 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002075 my $sline = $line; #copy of $line
2076 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002077
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002078 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002079
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002080#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002081 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002082 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002083 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002084 $realline=$1-1;
2085 if (defined $2) {
2086 $realcnt=$3+1;
2087 } else {
2088 $realcnt=1+1;
2089 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002090 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002091 $prev_values = 'E';
2092
Andy Whitcroft773647a2008-03-28 14:15:58 -07002093 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002094 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002095 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002096 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002097 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002098
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002099# track the line number as we move through the hunk, note that
2100# new versions of GNU diff omit the leading space on completely
2101# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002102 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002103 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002104 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002105
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002106 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002107 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002108
2109 # Track the previous line.
2110 ($prevline, $stashline) = ($stashline, $line);
2111 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002112 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2113
Andy Whitcroft773647a2008-03-28 14:15:58 -07002114 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002115
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002116 } elsif ($realcnt == 1) {
2117 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002118 }
2119
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002120 my $hunk_line = ($realcnt != 0);
2121
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002122#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002123 $prefix = "$filename:$realline: " if ($emacs && $file);
2124 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2125
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002126 $here = "#$linenr: " if (!$file);
2127 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002128
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002129 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002130 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002131 if ($line =~ /^diff --git.*?(\S+)$/) {
2132 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002133 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002134 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002135 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002136 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002137 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002138 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002139 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002140
2141 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002142 if (!$file && $tree && $p1_prefix ne '' &&
2143 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002144 WARN("PATCH_PREFIX",
2145 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002146 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002147
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002148 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002149 ERROR("MODIFIED_INCLUDE_ASM",
2150 "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 -07002151 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002152 $found_file = 1;
2153 }
2154
2155 if ($found_file) {
2156 if ($realfile =~ m@^(drivers/net/|net/)@) {
2157 $check = 1;
2158 } else {
2159 $check = $check_orig;
2160 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002161 next;
2162 }
2163
Randy Dunlap389834b2007-06-08 13:47:03 -07002164 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002165
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002166 my $hereline = "$here\n$rawline\n";
2167 my $herecurr = "$here\n$rawline\n";
2168 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002169
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002170 $cnt_lines++ if ($realcnt != 0);
2171
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002172# Check for incorrect file permissions
2173 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2174 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002175 if ($realfile !~ m@scripts/@ &&
2176 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002177 ERROR("EXECUTE_PERMISSIONS",
2178 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002179 }
2180 }
2181
Joe Perches20112472011-07-25 17:13:23 -07002182# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002183 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002184 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002185 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002186 }
2187
Joe Perchese0d975b2014-12-10 15:51:49 -08002188# Check if MAINTAINERS is being updated. If so, there's probably no need to
2189# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2190 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2191 $reported_maintainer_file = 1;
2192 }
2193
Joe Perches20112472011-07-25 17:13:23 -07002194# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002195 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002196 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002197 my $space_before = $1;
2198 my $sign_off = $2;
2199 my $space_after = $3;
2200 my $email = $4;
2201 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2202
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002203 if ($sign_off !~ /$signature_tags/) {
2204 WARN("BAD_SIGN_OFF",
2205 "Non-standard signature: $sign_off\n" . $herecurr);
2206 }
Joe Perches20112472011-07-25 17:13:23 -07002207 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002208 if (WARN("BAD_SIGN_OFF",
2209 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2210 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002211 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002212 "$ucfirst_sign_off $email";
2213 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002214 }
Joe Perches20112472011-07-25 17:13:23 -07002215 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002216 if (WARN("BAD_SIGN_OFF",
2217 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2218 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002219 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002220 "$ucfirst_sign_off $email";
2221 }
2222
Joe Perches20112472011-07-25 17:13:23 -07002223 }
2224 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002225 if (WARN("BAD_SIGN_OFF",
2226 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2227 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002228 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002229 "$ucfirst_sign_off $email";
2230 }
Joe Perches20112472011-07-25 17:13:23 -07002231 }
2232
2233 my ($email_name, $email_address, $comment) = parse_email($email);
2234 my $suggested_email = format_email(($email_name, $email_address));
2235 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002236 ERROR("BAD_SIGN_OFF",
2237 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002238 } else {
2239 my $dequoted = $suggested_email;
2240 $dequoted =~ s/^"//;
2241 $dequoted =~ s/" </ </;
2242 # Don't force email to have quotes
2243 # Allow just an angle bracketed address
2244 if ("$dequoted$comment" ne $email &&
2245 "<$email_address>$comment" ne $email &&
2246 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002247 WARN("BAD_SIGN_OFF",
2248 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002249 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002250 }
Joe Perches7e51f192013-09-11 14:23:57 -07002251
2252# Check for duplicate signatures
2253 my $sig_nospace = $line;
2254 $sig_nospace =~ s/\s//g;
2255 $sig_nospace = lc($sig_nospace);
2256 if (defined $signatures{$sig_nospace}) {
2257 WARN("BAD_SIGN_OFF",
2258 "Duplicate signature\n" . $herecurr);
2259 } else {
2260 $signatures{$sig_nospace} = 1;
2261 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002262 }
2263
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002264# Check email subject for common tools that don't need to be mentioned
2265 if ($in_header_lines &&
2266 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2267 WARN("EMAIL_SUBJECT",
2268 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2269 }
2270
Joe Perches9b3189e2014-06-04 16:12:10 -07002271# Check for old stable address
2272 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2273 ERROR("STABLE_ADDRESS",
2274 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2275 }
2276
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002277# Check for unwanted Gerrit info
2278 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2279 ERROR("GERRIT_CHANGE_ID",
2280 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2281 }
2282
Joe Perches2a076f42015-04-16 12:44:28 -07002283# Check for line lengths > 75 in commit log, warn once
2284 if ($in_commit_log && !$commit_log_long_line &&
2285 length($line) > 75) {
2286 WARN("COMMIT_LOG_LONG_LINE",
2287 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2288 $commit_log_long_line = 1;
2289 }
2290
Joe Perches0d7835f2015-02-13 14:38:35 -08002291# Check for git id commit length and improperly formed commit descriptions
2292 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002293 my $init_char = $1;
2294 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002295 my $short = 1;
2296 my $long = 0;
2297 my $case = 1;
2298 my $space = 1;
2299 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002300 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002301 my $id = '0123456789ab';
2302 my $orig_desc = "commit description";
2303 my $description = "";
2304
2305 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2306 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2307 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2308 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2309 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2310 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002311 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002312 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2313 defined $rawlines[$linenr] &&
2314 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2315 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002316 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002317 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2318 defined $rawlines[$linenr] &&
2319 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2320 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2321 $orig_desc = $1;
2322 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2323 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002324 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002325 }
2326
2327 ($id, $description) = git_commit_info($orig_commit,
2328 $id, $orig_desc);
2329
Joe Perches19c146a2015-02-13 14:39:00 -08002330 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002331 ERROR("GIT_COMMIT_ID",
2332 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2333 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002334 }
2335
Joe Perches13f19372014-08-06 16:10:59 -07002336# Check for added, moved or deleted files
2337 if (!$reported_maintainer_file && !$in_commit_log &&
2338 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2339 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2340 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2341 (defined($1) || defined($2))))) {
2342 $reported_maintainer_file = 1;
2343 WARN("FILE_PATH_CHANGES",
2344 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2345 }
2346
Andy Whitcroft00df3442007-06-08 13:47:06 -07002347# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002348 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002349 ERROR("CORRUPTED_PATCH",
2350 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002351 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002352 }
2353
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002354# Check for absolute kernel paths.
2355 if ($tree) {
2356 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2357 my $file = $1;
2358
2359 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2360 check_absolute_file($1, $herecurr)) {
2361 #
2362 } else {
2363 check_absolute_file($file, $herecurr);
2364 }
2365 }
2366 }
2367
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002368# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2369 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002370 $rawline !~ m/^$UTF8*$/) {
2371 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2372
2373 my $blank = copy_spacing($rawline);
2374 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2375 my $hereptr = "$hereline$ptr\n";
2376
Joe Perches34d99212011-07-25 17:13:26 -07002377 CHK("INVALID_UTF8",
2378 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002379 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002380
Joe Perches15662b32011-10-31 17:13:12 -07002381# Check if it's the start of a commit log
2382# (not a header line and we haven't seen the patch filename)
2383 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002384 !($rawline =~ /^\s+\S/ ||
2385 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002386 $in_header_lines = 0;
2387 $in_commit_log = 1;
2388 }
2389
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002390# Check if there is UTF-8 in a commit log when a mail header has explicitly
2391# declined it, i.e defined some charset where it is missing.
2392 if ($in_header_lines &&
2393 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2394 $1 !~ /utf-8/i) {
2395 $non_utf8_charset = 1;
2396 }
2397
2398 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002399 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002400 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002401 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2402 }
2403
Kees Cook66b47b42014-10-13 15:51:57 -07002404# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002405 if (defined($misspellings) &&
2406 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002407 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002408 my $typo = $1;
2409 my $typo_fix = $spelling_fix{lc($typo)};
2410 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2411 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2412 my $msg_type = \&WARN;
2413 $msg_type = \&CHK if ($file);
2414 if (&{$msg_type}("TYPO_SPELLING",
2415 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2416 $fix) {
2417 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2418 }
2419 }
2420 }
2421
Andy Whitcroft306708542008-10-15 22:02:28 -07002422# ignore non-hunk lines and lines being removed
2423 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002424
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002425#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002426 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002427 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002428 if (ERROR("DOS_LINE_ENDINGS",
2429 "DOS line endings\n" . $herevet) &&
2430 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002431 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002432 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002433 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2434 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002435 if (ERROR("TRAILING_WHITESPACE",
2436 "trailing whitespace\n" . $herevet) &&
2437 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002438 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002439 }
2440
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002441 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002442 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002443
Josh Triplett4783f892013-11-12 15:10:12 -08002444# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002445 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002446 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2447 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002448 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2449 my $msg_type = \&ERROR;
2450 $msg_type = \&CHK if ($file);
2451 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002452 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
Josh Triplett4783f892013-11-12 15:10:12 -08002453 }
2454
Andi Kleen33549572010-05-24 14:33:29 -07002455# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002456# Only applies when adding the entry originally, after that we do not have
2457# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002458 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002459 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002460 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002461 my $cnt = $realcnt;
2462 my $ln = $linenr + 1;
2463 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002464 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002465 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002466 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002467 $f = $lines[$ln - 1];
2468 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2469 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002470
2471 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002472 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002473
Joe Perches8d73e0e2014-08-06 16:10:46 -07002474 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002475 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002476 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002477 $length = -1;
2478 }
2479
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002480 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002481 $f =~ s/#.*//;
2482 $f =~ s/^\s+//;
2483 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002484 if ($f =~ /^\s*config\s/) {
2485 $is_end = 1;
2486 last;
2487 }
Andi Kleen33549572010-05-24 14:33:29 -07002488 $length++;
2489 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002490 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2491 WARN("CONFIG_DESCRIPTION",
2492 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2493 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002494 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002495 }
2496
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002497# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2498 if ($realfile =~ /Kconfig/ &&
2499 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2500 WARN("CONFIG_EXPERIMENTAL",
2501 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2502 }
2503
Christoph Jaeger327953e2015-02-13 14:38:29 -08002504# discourage the use of boolean for type definition attributes of Kconfig options
2505 if ($realfile =~ /Kconfig/ &&
2506 $line =~ /^\+\s*\bboolean\b/) {
2507 WARN("CONFIG_TYPE_BOOLEAN",
2508 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2509 }
2510
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002511 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2512 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2513 my $flag = $1;
2514 my $replacement = {
2515 'EXTRA_AFLAGS' => 'asflags-y',
2516 'EXTRA_CFLAGS' => 'ccflags-y',
2517 'EXTRA_CPPFLAGS' => 'cppflags-y',
2518 'EXTRA_LDFLAGS' => 'ldflags-y',
2519 };
2520
2521 WARN("DEPRECATED_VARIABLE",
2522 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2523 }
2524
Rob Herringbff5da42014-01-23 15:54:51 -08002525# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002526 if (defined $root &&
2527 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2528 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2529
Rob Herringbff5da42014-01-23 15:54:51 -08002530 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2531
Florian Vaussardcc933192014-04-03 14:49:27 -07002532 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2533 my $vp_file = $dt_path . "vendor-prefixes.txt";
2534
Rob Herringbff5da42014-01-23 15:54:51 -08002535 foreach my $compat (@compats) {
2536 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002537 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2538 my $compat3 = $compat;
2539 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2540 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002541 if ( $? >> 8 ) {
2542 WARN("UNDOCUMENTED_DT_STRING",
2543 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2544 }
2545
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002546 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2547 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002548 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002549 if ( $? >> 8 ) {
2550 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002551 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002552 }
2553 }
2554 }
2555
Andy Whitcroft5368df22008-10-15 22:02:27 -07002556# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002557 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002558
Joe Perches47e0c882015-06-25 15:02:57 -07002559# line length limit (with some exclusions)
2560#
2561# There are a few types of lines that may extend beyond $max_line_length:
2562# logging functions like pr_info that end in a string
2563# lines with a single string
2564# #defines that are a single string
2565#
2566# There are 3 different line length message types:
2567# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2568# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2569# LONG_LINE all other lines longer than $max_line_length
2570#
2571# if LONG_LINE is ignored, the other 2 types are also ignored
2572#
2573
2574 if ($length > $max_line_length) {
2575 my $msg_type = "LONG_LINE";
2576
2577 # Check the allowed long line types first
2578
2579 # logging functions that end in a string that starts
2580 # before $max_line_length
2581 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2582 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2583 $msg_type = "";
2584
2585 # lines with only strings (w/ possible termination)
2586 # #defines with only strings
2587 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2588 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2589 $msg_type = "";
2590
2591 # Otherwise set the alternate message types
2592
2593 # a comment starts before $max_line_length
2594 } elsif ($line =~ /($;[\s$;]*)$/ &&
2595 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2596 $msg_type = "LONG_LINE_COMMENT"
2597
2598 # a quoted string starts before $max_line_length
2599 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2600 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2601 $msg_type = "LONG_LINE_STRING"
2602 }
2603
2604 if ($msg_type ne "" &&
2605 (show_type("LONG_LINE") || show_type($msg_type))) {
2606 WARN($msg_type,
2607 "line over $max_line_length characters\n" . $herecurr);
2608 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002609 }
2610
Andy Whitcroft8905a672007-11-28 16:21:06 -08002611# check for adding lines without a newline.
2612 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002613 WARN("MISSING_EOF_NEWLINE",
2614 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002615 }
2616
Mike Frysinger42e41c52009-09-21 17:04:40 -07002617# Blackfin: use hi/lo macros
2618 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2619 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2620 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002621 ERROR("LO_MACRO",
2622 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002623 }
2624 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2625 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002626 ERROR("HI_MACRO",
2627 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002628 }
2629 }
2630
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002631# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002632 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002633
2634# at the beginning of a line any tabs must come first and anything
2635# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002636 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2637 $rawline =~ /^\+\s* \s*/) {
2638 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002639 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002640 if (ERROR("CODE_INDENT",
2641 "code indent should use tabs where possible\n" . $herevet) &&
2642 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002643 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002644 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002645 }
2646
Alberto Panizzo08e44362010-03-05 13:43:54 -08002647# check for space before tabs.
2648 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2649 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002650 if (WARN("SPACE_BEFORE_TAB",
2651 "please, no space before tabs\n" . $herevet) &&
2652 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002653 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002654 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002655 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002656 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002657 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002658 }
2659
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002660# check for && or || at the start of a line
2661 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2662 CHK("LOGICAL_CONTINUATIONS",
2663 "Logical continuations should be on the previous line\n" . $hereprev);
2664 }
2665
2666# check multi-line statement indentation matches previous line
2667 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002668 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002669 $prevline =~ /^\+(\t*)(.*)$/;
2670 my $oldindent = $1;
2671 my $rest = $2;
2672
2673 my $pos = pos_last_openparen($rest);
2674 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002675 $line =~ /^(\+| )([ \t]*)/;
2676 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002677
2678 my $goodtabindent = $oldindent .
2679 "\t" x ($pos / 8) .
2680 " " x ($pos % 8);
2681 my $goodspaceindent = $oldindent . " " x $pos;
2682
2683 if ($newindent ne $goodtabindent &&
2684 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002685
2686 if (CHK("PARENTHESIS_ALIGNMENT",
2687 "Alignment should match open parenthesis\n" . $hereprev) &&
2688 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002689 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002690 s/^\+[ \t]*/\+$goodtabindent/;
2691 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002692 }
2693 }
2694 }
2695
Joe Perches6ab3a972015-04-16 12:44:05 -07002696# check for space after cast like "(int) foo" or "(struct foo) bar"
2697# avoid checking a few false positives:
2698# "sizeof(<type>)" or "__alignof__(<type>)"
2699# function pointer declarations like "(*foo)(int) = bar;"
2700# structure definitions like "(struct foo) { 0 };"
2701# multiline macros that define functions
2702# known attributes or the __attribute__ keyword
2703 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2704 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002705 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002706 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002707 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002708 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002709 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002710 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002711 }
2712
Joe Perches05880602012-10-04 17:13:35 -07002713 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002714 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002715 $rawline =~ /^\+[ \t]*\*/ &&
2716 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002717 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2718 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2719 }
2720
2721 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002722 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2723 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002724 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002725 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2726 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2727 "networking block comments start with * on subsequent lines\n" . $hereprev);
2728 }
2729
2730 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002731 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2732 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2733 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2734 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002735 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2736 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2737 }
2738
Joe Perches7f619192014-08-06 16:10:39 -07002739# check for missing blank lines after struct/union declarations
2740# with exceptions for various attributes and macros
2741 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2742 $line =~ /^\+/ &&
2743 !($line =~ /^\+\s*$/ ||
2744 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2745 $line =~ /^\+\s*MODULE_/i ||
2746 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2747 $line =~ /^\+[a-z_]*init/ ||
2748 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2749 $line =~ /^\+\s*DECLARE/ ||
2750 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002751 if (CHK("LINE_SPACING",
2752 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2753 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002754 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002755 }
Joe Perches7f619192014-08-06 16:10:39 -07002756 }
2757
Joe Perches365dd4e2014-08-06 16:10:42 -07002758# check for multiple consecutive blank lines
2759 if ($prevline =~ /^[\+ ]\s*$/ &&
2760 $line =~ /^\+\s*$/ &&
2761 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002762 if (CHK("LINE_SPACING",
2763 "Please don't use multiple blank lines\n" . $hereprev) &&
2764 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002765 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002766 }
2767
Joe Perches365dd4e2014-08-06 16:10:42 -07002768 $last_blank_line = $linenr;
2769 }
2770
Joe Perches3b617e32014-04-03 14:49:28 -07002771# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002772 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2773 # actual declarations
2774 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002775 # function pointer declarations
2776 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002777 # foo bar; where foo is some local typedef or #define
2778 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2779 # known declaration macros
2780 $prevline =~ /^\+\s+$declaration_macros/) &&
2781 # for "else if" which can look like "$Ident $Ident"
2782 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2783 # other possible extensions of declaration lines
2784 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2785 # not starting a section or a macro "\" extended line
2786 $prevline =~ /(?:\{\s*|\\)$/) &&
2787 # looks like a declaration
2788 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002789 # function pointer declarations
2790 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002791 # foo bar; where foo is some local typedef or #define
2792 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2793 # known declaration macros
2794 $sline =~ /^\+\s+$declaration_macros/ ||
2795 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002796 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002797 # start or end of block or continuation of declaration
2798 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2799 # bitfield continuation
2800 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2801 # other possible extensions of declaration lines
2802 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2803 # indentation of previous and current line are the same
2804 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002805 if (WARN("LINE_SPACING",
2806 "Missing a blank line after declarations\n" . $hereprev) &&
2807 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002808 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002809 }
Joe Perches3b617e32014-04-03 14:49:28 -07002810 }
2811
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002812# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002813# Exceptions:
2814# 1) within comments
2815# 2) indented preprocessor commands
2816# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002817 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002818 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002819 if (WARN("LEADING_SPACE",
2820 "please, no spaces at the start of a line\n" . $herevet) &&
2821 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002822 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002823 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002824 }
2825
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002826# check we are in a valid C source file if not then ignore this hunk
2827 next if ($realfile !~ /\.(h|c)$/);
2828
Joe Perches032a4c02014-08-06 16:10:29 -07002829# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002830# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002831# if the previous line is a break or return and is indented 1 tab more...
2832 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2833 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002834 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2835 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2836 defined $lines[$linenr] &&
2837 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002838 WARN("UNNECESSARY_ELSE",
2839 "else is not generally useful after a break or return\n" . $hereprev);
2840 }
2841 }
2842
Joe Perchesc00df192014-08-06 16:11:01 -07002843# check indentation of a line with a break;
2844# if the previous line is a goto or return and is indented the same # of tabs
2845 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2846 my $tabs = $1;
2847 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2848 WARN("UNNECESSARY_BREAK",
2849 "break is not useful after a goto or return\n" . $hereprev);
2850 }
2851 }
2852
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002853# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2854 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2855 WARN("CONFIG_EXPERIMENTAL",
2856 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2857 }
2858
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002859# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002860 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002861 WARN("CVS_KEYWORD",
2862 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002863 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002864
Mike Frysinger42e41c52009-09-21 17:04:40 -07002865# Blackfin: don't use __builtin_bfin_[cs]sync
2866 if ($line =~ /__builtin_bfin_csync/) {
2867 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002868 ERROR("CSYNC",
2869 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002870 }
2871 if ($line =~ /__builtin_bfin_ssync/) {
2872 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002873 ERROR("SSYNC",
2874 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002875 }
2876
Joe Perches56e77d72013-02-21 16:44:14 -08002877# check for old HOTPLUG __dev<foo> section markings
2878 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2879 WARN("HOTPLUG_SECTION",
2880 "Using $1 is unnecessary\n" . $herecurr);
2881 }
2882
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002883# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002884 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2885 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002886#print "LINE<$line>\n";
2887 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002888 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002889 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002890 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002891 $stat =~ s/\n./\n /g;
2892 $cond =~ s/\n./\n /g;
2893
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002894#print "linenr<$linenr> <$stat>\n";
2895 # If this statement has no statement boundaries within
2896 # it there is no point in retrying a statement scan
2897 # until we hit end of it.
2898 my $frag = $stat; $frag =~ s/;+\s*$//;
2899 if ($frag !~ /(?:{|;)/) {
2900#print "skip<$line_nr_next>\n";
2901 $suppress_statement = $line_nr_next;
2902 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002903
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002904 # Find the real next line.
2905 $realline_next = $line_nr_next;
2906 if (defined $realline_next &&
2907 (!defined $lines[$realline_next - 1] ||
2908 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2909 $realline_next++;
2910 }
2911
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002912 my $s = $stat;
2913 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002914
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002915 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002916 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002917
2918 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002919 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002920
Andy Whitcroft463f2862009-09-21 17:04:34 -07002921 } elsif ($s =~ /^.\s*else\b/s) {
2922
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002923 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002924 } 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 -07002925 my $type = $1;
2926 $type =~ s/\s+/ /g;
2927 possible($type, "A:" . $s);
2928
Andy Whitcroft8905a672007-11-28 16:21:06 -08002929 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002930 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002931 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002932 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002933
2934 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002935 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002936 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002937 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002938
2939 # Check for any sort of function declaration.
2940 # int foo(something bar, other baz);
2941 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002942 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 -08002943 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002944
Andy Whitcroftcf655042008-03-04 14:28:20 -08002945 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002946 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002947 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002948
Andy Whitcroft8905a672007-11-28 16:21:06 -08002949 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002950 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002951
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002952 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002953 }
2954 }
2955 }
2956
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002957 }
2958
Andy Whitcroft653d4872007-06-23 17:16:34 -07002959#
2960# Checks which may be anchored in the context.
2961#
2962
2963# Check for switch () and associated case and default
2964# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002965 if ($line=~/\bswitch\s*\(.*\)/) {
2966 my $err = '';
2967 my $sep = '';
2968 my @ctx = ctx_block_outer($linenr, $realcnt);
2969 shift(@ctx);
2970 for my $ctx (@ctx) {
2971 my ($clen, $cindent) = line_stats($ctx);
2972 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2973 $indent != $cindent) {
2974 $err .= "$sep$ctx\n";
2975 $sep = '';
2976 } else {
2977 $sep = "[...]\n";
2978 }
2979 }
2980 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002981 ERROR("SWITCH_CASE_INDENT_LEVEL",
2982 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002983 }
2984 }
2985
2986# if/while/etc brace do not go on next line, unless defining a do while loop,
2987# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002988 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002989 my $pre_ctx = "$1$2";
2990
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002991 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002992
2993 if ($line =~ /^\+\t{6,}/) {
2994 WARN("DEEP_INDENTATION",
2995 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2996 }
2997
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002998 my $ctx_cnt = $realcnt - $#ctx - 1;
2999 my $ctx = join("\n", @ctx);
3000
Andy Whitcroft548596d2008-07-23 21:29:01 -07003001 my $ctx_ln = $linenr;
3002 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003003
Andy Whitcroft548596d2008-07-23 21:29:01 -07003004 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3005 defined $lines[$ctx_ln - 1] &&
3006 $lines[$ctx_ln - 1] =~ /^-/)) {
3007 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3008 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003009 $ctx_ln++;
3010 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003011
Andy Whitcroft53210162008-07-23 21:29:03 -07003012 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3013 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003014
Joe Perchesd752fcc2014-08-06 16:11:05 -07003015 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003016 ERROR("OPEN_BRACE",
3017 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003018 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003019 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003020 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3021 $ctx =~ /\)\s*\;\s*$/ &&
3022 defined $lines[$ctx_ln - 1])
3023 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003024 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3025 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003026 WARN("TRAILING_SEMICOLON",
3027 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003028 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003029 }
3030 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003031 }
3032
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003033# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003034 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003035 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3036 ctx_statement_block($linenr, $realcnt, 0)
3037 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003038 my ($s, $c) = ($stat, $cond);
3039
3040 substr($s, 0, length($c), '');
3041
3042 # Make sure we remove the line prefixes as we have
3043 # none on the first line, and are going to readd them
3044 # where necessary.
3045 $s =~ s/\n./\n/gs;
3046
3047 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003048 my @newlines = ($c =~ /\n/gs);
3049 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003050
3051 # We want to check the first line inside the block
3052 # starting at the end of the conditional, so remove:
3053 # 1) any blank line termination
3054 # 2) any opening brace { on end of the line
3055 # 3) any do (...) {
3056 my $continuation = 0;
3057 my $check = 0;
3058 $s =~ s/^.*\bdo\b//;
3059 $s =~ s/^\s*{//;
3060 if ($s =~ s/^\s*\\//) {
3061 $continuation = 1;
3062 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003063 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003064 $check = 1;
3065 $cond_lines++;
3066 }
3067
3068 # Also ignore a loop construct at the end of a
3069 # preprocessor statement.
3070 if (($prevline =~ /^.\s*#\s*define\s/ ||
3071 $prevline =~ /\\\s*$/) && $continuation == 0) {
3072 $check = 0;
3073 }
3074
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003075 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003076 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003077 while ($cond_ptr != $cond_lines) {
3078 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003079
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003080 # If we see an #else/#elif then the code
3081 # is not linear.
3082 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3083 $check = 0;
3084 }
3085
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003086 # Ignore:
3087 # 1) blank lines, they should be at 0,
3088 # 2) preprocessor lines, and
3089 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003090 if ($continuation ||
3091 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003092 $s =~ /^\s*#\s*?/ ||
3093 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003094 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003095 if ($s =~ s/^.*?\n//) {
3096 $cond_lines++;
3097 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003098 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003099 }
3100
3101 my (undef, $sindent) = line_stats("+" . $s);
3102 my $stat_real = raw_line($linenr, $cond_lines);
3103
3104 # Check if either of these lines are modified, else
3105 # this is not this patch's fault.
3106 if (!defined($stat_real) ||
3107 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3108 $check = 0;
3109 }
3110 if (defined($stat_real) && $cond_lines > 1) {
3111 $stat_real = "[...]\n$stat_real";
3112 }
3113
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003114 #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 -07003115
3116 if ($check && (($sindent % 8) != 0 ||
3117 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003118 WARN("SUSPECT_CODE_INDENT",
3119 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003120 }
3121 }
3122
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003123 # Track the 'values' across context and added lines.
3124 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003125 my ($curr_values, $curr_vars) =
3126 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003127 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003128 if ($dbg_values) {
3129 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003130 print "$linenr > .$outline\n";
3131 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003132 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003133 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003134 $prev_values = substr($curr_values, -1);
3135
Andy Whitcroft00df3442007-06-08 13:47:06 -07003136#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003137 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003138
Andy Whitcroft653d4872007-06-23 17:16:34 -07003139# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003140 if ($dbg_type) {
3141 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003142 ERROR("TEST_TYPE",
3143 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003144 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003145 ERROR("TEST_NOT_TYPE",
3146 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003147 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003148 next;
3149 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003150# TEST: allow direct testing of the attribute matcher.
3151 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003152 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003153 ERROR("TEST_ATTR",
3154 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003155 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003156 ERROR("TEST_NOT_ATTR",
3157 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003158 }
3159 next;
3160 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003161
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003162# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003163 if ($line =~ /^.\s*{/ &&
3164 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003165 if (ERROR("OPEN_BRACE",
3166 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003167 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3168 fix_delete_line($fixlinenr - 1, $prevrawline);
3169 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003170 my $fixedline = $prevrawline;
3171 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003172 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003173 $fixedline = $line;
3174 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003175 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003176 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003177 }
3178
Andy Whitcroft653d4872007-06-23 17:16:34 -07003179#
3180# Checks which are anchored on the added line.
3181#
3182
3183# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003184 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003185 my $path = $1;
3186 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003187 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003188 "malformed #include filename\n" . $herecurr);
3189 }
3190 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3191 ERROR("UAPI_INCLUDE",
3192 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003193 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003194 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003195
3196# no C99 // comments
3197 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003198 if (ERROR("C99_COMMENTS",
3199 "do not use C99 // comments\n" . $herecurr) &&
3200 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003201 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003202 if ($line =~ /\/\/(.*)$/) {
3203 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003204 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003205 }
3206 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003207 }
3208 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003209 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003210 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003211
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003212# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3213# the whole statement.
3214#print "APW <$lines[$realline_next - 1]>\n";
3215 if (defined $realline_next &&
3216 exists $lines[$realline_next - 1] &&
3217 !defined $suppress_export{$realline_next} &&
3218 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3219 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003220 # Handle definitions which produce identifiers with
3221 # a prefix:
3222 # XXX(foo);
3223 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003224 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003225 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003226 $name =~ /^${Ident}_$2/) {
3227#print "FOO C name<$name>\n";
3228 $suppress_export{$realline_next} = 1;
3229
3230 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003231 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003232 ^.DEFINE_$Ident\(\Q$name\E\)|
3233 ^.DECLARE_$Ident\(\Q$name\E\)|
3234 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003235 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3236 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003237 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003238#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3239 $suppress_export{$realline_next} = 2;
3240 } else {
3241 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003242 }
3243 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003244 if (!defined $suppress_export{$linenr} &&
3245 $prevline =~ /^.\s*$/ &&
3246 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3247 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3248#print "FOO B <$lines[$linenr - 1]>\n";
3249 $suppress_export{$linenr} = 2;
3250 }
3251 if (defined $suppress_export{$linenr} &&
3252 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003253 WARN("EXPORT_SYMBOL",
3254 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003255 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003256
Joe Eloff5150bda2010-08-09 17:21:00 -07003257# check for global initialisers.
Joe Perches5129e872015-06-10 11:14:52 -07003258 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003259 if (ERROR("GLOBAL_INITIALISERS",
3260 "do not initialise globals to 0 or NULL\n" .
3261 $herecurr) &&
3262 $fix) {
Joe Perches5129e872015-06-10 11:14:52 -07003263 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003264 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003265 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003266# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003267 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3268 if (ERROR("INITIALISED_STATIC",
3269 "do not initialise statics to 0 or NULL\n" .
3270 $herecurr) &&
3271 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003272 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003273 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003274 }
3275
Joe Perches18130872014-08-06 16:11:22 -07003276# check for misordered declarations of char/short/int/long with signed/unsigned
3277 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3278 my $tmp = trim($1);
3279 WARN("MISORDERED_TYPE",
3280 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3281 }
3282
Joe Perchescb710ec2010-10-26 14:23:20 -07003283# check for static const char * arrays.
3284 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003285 WARN("STATIC_CONST_CHAR_ARRAY",
3286 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003287 $herecurr);
3288 }
3289
3290# check for static char foo[] = "bar" declarations.
3291 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003292 WARN("STATIC_CONST_CHAR_ARRAY",
3293 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003294 $herecurr);
3295 }
3296
Joe Perchesab7e23f2015-04-16 12:44:22 -07003297# check for const <foo> const where <foo> is not a pointer or array type
3298 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3299 my $found = $1;
3300 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3301 WARN("CONST_CONST",
3302 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3303 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3304 WARN("CONST_CONST",
3305 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3306 }
3307 }
3308
Joe Perches9b0fa602014-04-03 14:49:18 -07003309# check for non-global char *foo[] = {"bar", ...} declarations.
3310 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3311 WARN("STATIC_CONST_CHAR_ARRAY",
3312 "char * array declaration might be better as static const\n" .
3313 $herecurr);
3314 }
3315
Joe Perchesb598b672015-04-16 12:44:36 -07003316# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3317 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3318 my $array = $1;
3319 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3320 my $array_div = $1;
3321 if (WARN("ARRAY_SIZE",
3322 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3323 $fix) {
3324 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3325 }
3326 }
3327 }
3328
Joe Perchesb36190c2014-01-27 17:07:18 -08003329# check for function declarations without arguments like "int foo()"
3330 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3331 if (ERROR("FUNCTION_WITHOUT_ARGS",
3332 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3333 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003334 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003335 }
3336 }
3337
Joe Perches92e112f2013-12-13 11:36:22 -07003338# check for uses of DEFINE_PCI_DEVICE_TABLE
3339 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3340 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3341 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3342 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003343 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
Joe Perches92e112f2013-12-13 11:36:22 -07003344 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003345 }
3346
Andy Whitcroft653d4872007-06-23 17:16:34 -07003347# check for new typedefs, only function parameters and sparse annotations
3348# make sense.
3349 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003350 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003351 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003352 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003353 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003354 WARN("NEW_TYPEDEFS",
3355 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003356 }
3357
3358# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003359 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003360 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3361 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003362 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003363
Andy Whitcroft65863862009-01-06 14:41:21 -08003364 # Should start with a space.
3365 $to =~ s/^(\S)/ $1/;
3366 # Should not end with a space.
3367 $to =~ s/\s+$//;
3368 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003369 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003370 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003371
Joe Perches3705ce52013-07-03 15:05:31 -07003372## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003373 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003374 if (ERROR("POINTER_LOCATION",
3375 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3376 $fix) {
3377 my $sub_from = $ident;
3378 my $sub_to = $ident;
3379 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003380 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003381 s@\Q$sub_from\E@$sub_to@;
3382 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003383 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003384 }
3385 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3386 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003387 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003388
Andy Whitcroft65863862009-01-06 14:41:21 -08003389 # Should start with a space.
3390 $to =~ s/^(\S)/ $1/;
3391 # Should not end with a space.
3392 $to =~ s/\s+$//;
3393 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003394 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003395 }
3396 # Modifiers should have spaces.
3397 $to =~ s/(\b$Modifier$)/$1 /;
3398
Joe Perches3705ce52013-07-03 15:05:31 -07003399## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003400 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003401 if (ERROR("POINTER_LOCATION",
3402 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3403 $fix) {
3404
3405 my $sub_from = $match;
3406 my $sub_to = $match;
3407 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003408 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003409 s@\Q$sub_from\E@$sub_to@;
3410 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003411 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003412 }
3413
3414# # no BUG() or BUG_ON()
3415# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3416# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3417# print "$herecurr";
3418# $clean = 0;
3419# }
3420
Andy Whitcroft8905a672007-11-28 16:21:06 -08003421 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003422 WARN("LINUX_VERSION_CODE",
3423 "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 -08003424 }
3425
Joe Perches17441222011-06-15 15:08:17 -07003426# check for uses of printk_ratelimit
3427 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003428 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003429 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003430 }
3431
Andy Whitcroft00df3442007-06-08 13:47:06 -07003432# printk should use KERN_* levels. Note that follow on printk's on the
3433# same line do not need a level, so we use the current block context
3434# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003435# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003436# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003437 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003438 my $ok = 0;
3439 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3440 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003441 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003442 # with "\n" ignore it, else it is to blame
3443 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3444 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3445 $ok = 1;
3446 }
3447 last;
3448 }
3449 }
3450 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003451 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3452 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003453 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003454 }
3455
Joe Perches243f3802012-05-31 16:26:09 -07003456 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3457 my $orig = $1;
3458 my $level = lc($orig);
3459 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003460 my $level2 = $level;
3461 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003462 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003463 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07003464 }
3465
3466 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003467 if (WARN("PREFER_PR_LEVEL",
3468 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3469 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003470 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003471 s/\bpr_warning\b/pr_warn/;
3472 }
Joe Perches243f3802012-05-31 16:26:09 -07003473 }
3474
Joe Perchesdc139312013-02-21 16:44:13 -08003475 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3476 my $orig = $1;
3477 my $level = lc($orig);
3478 $level = "warn" if ($level eq "warning");
3479 $level = "dbg" if ($level eq "debug");
3480 WARN("PREFER_DEV_LEVEL",
3481 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3482 }
3483
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003484# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3485# number of false positives, but assembly files are not checked, so at
3486# least the arch entry code will not trigger this warning.
3487 if ($line =~ /\bENOSYS\b/) {
3488 WARN("ENOSYS",
3489 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3490 }
3491
Andy Whitcroft653d4872007-06-23 17:16:34 -07003492# function brace can't be on same line, except for #defines of do while,
3493# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003494 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003495 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003496 if (ERROR("OPEN_BRACE",
3497 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3498 $fix) {
3499 fix_delete_line($fixlinenr, $rawline);
3500 my $fixed_line = $rawline;
3501 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3502 my $line1 = $1;
3503 my $line2 = $2;
3504 fix_insert_line($fixlinenr, ltrim($line1));
3505 fix_insert_line($fixlinenr, "\+{");
3506 if ($line2 !~ /^\s*$/) {
3507 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3508 }
3509 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003510 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003511
Andy Whitcroft8905a672007-11-28 16:21:06 -08003512# open braces for enum, union and struct go on the same line.
3513 if ($line =~ /^.\s*{/ &&
3514 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003515 if (ERROR("OPEN_BRACE",
3516 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3517 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3518 fix_delete_line($fixlinenr - 1, $prevrawline);
3519 fix_delete_line($fixlinenr, $rawline);
3520 my $fixedline = rtrim($prevrawline) . " {";
3521 fix_insert_line($fixlinenr, $fixedline);
3522 $fixedline = $rawline;
3523 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3524 if ($fixedline !~ /^\+\s*$/) {
3525 fix_insert_line($fixlinenr, $fixedline);
3526 }
3527 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003528 }
3529
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003530# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003531 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3532 if (WARN("SPACING",
3533 "missing space after $1 definition\n" . $herecurr) &&
3534 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003535 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003536 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3537 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003538 }
3539
Joe Perches31070b52014-01-23 15:54:49 -08003540# Function pointer declarations
3541# check spacing between type, funcptr, and args
3542# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003543 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003544 my $declare = $1;
3545 my $pre_pointer_space = $2;
3546 my $post_pointer_space = $3;
3547 my $funcname = $4;
3548 my $post_funcname_space = $5;
3549 my $pre_args_space = $6;
3550
Joe Perches91f72e92014-04-03 14:49:12 -07003551# the $Declare variable will capture all spaces after the type
3552# so check it for a missing trailing missing space but pointer return types
3553# don't need a space so don't warn for those.
3554 my $post_declare_space = "";
3555 if ($declare =~ /(\s+)$/) {
3556 $post_declare_space = $1;
3557 $declare = rtrim($declare);
3558 }
3559 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003560 WARN("SPACING",
3561 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003562 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003563 }
3564
3565# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003566# This test is not currently implemented because these declarations are
3567# equivalent to
3568# int foo(int bar, ...)
3569# and this is form shouldn't/doesn't generate a checkpatch warning.
3570#
3571# elsif ($declare =~ /\s{2,}$/) {
3572# WARN("SPACING",
3573# "Multiple spaces after return type\n" . $herecurr);
3574# }
Joe Perches31070b52014-01-23 15:54:49 -08003575
3576# unnecessary space "type ( *funcptr)(args...)"
3577 if (defined $pre_pointer_space &&
3578 $pre_pointer_space =~ /^\s/) {
3579 WARN("SPACING",
3580 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3581 }
3582
3583# unnecessary space "type (* funcptr)(args...)"
3584 if (defined $post_pointer_space &&
3585 $post_pointer_space =~ /^\s/) {
3586 WARN("SPACING",
3587 "Unnecessary space before function pointer name\n" . $herecurr);
3588 }
3589
3590# unnecessary space "type (*funcptr )(args...)"
3591 if (defined $post_funcname_space &&
3592 $post_funcname_space =~ /^\s/) {
3593 WARN("SPACING",
3594 "Unnecessary space after function pointer name\n" . $herecurr);
3595 }
3596
3597# unnecessary space "type (*funcptr) (args...)"
3598 if (defined $pre_args_space &&
3599 $pre_args_space =~ /^\s/) {
3600 WARN("SPACING",
3601 "Unnecessary space before function pointer arguments\n" . $herecurr);
3602 }
3603
3604 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003605 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003606 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003607 }
3608 }
3609
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003610# check for spacing round square brackets; allowed:
3611# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003612# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3613# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003614 while ($line =~ /(.*?\s)\[/g) {
3615 my ($where, $prefix) = ($-[1], $1);
3616 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003617 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003618 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003619 if (ERROR("BRACKET_SPACE",
3620 "space prohibited before open square bracket '['\n" . $herecurr) &&
3621 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003622 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003623 s/^(\+.*?)\s+\[/$1\[/;
3624 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003625 }
3626 }
3627
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003628# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003629 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003630 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003631 my $ctx_before = substr($line, 0, $-[1]);
3632 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003633
3634 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003635 if ($name =~ /^(?:
3636 if|for|while|switch|return|case|
3637 volatile|__volatile__|
3638 __attribute__|format|__extension__|
3639 asm|__asm__)$/x)
3640 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003641 # cpp #define statements have non-optional spaces, ie
3642 # if there is a space between the name and the open
3643 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003644 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003645
3646 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003647 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003648
3649 # If this whole things ends with a type its most
3650 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003651 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003652
3653 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003654 if (WARN("SPACING",
3655 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3656 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003657 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003658 s/\b$name\s+\(/$name\(/;
3659 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003660 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003661 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003662
Andy Whitcroft653d4872007-06-23 17:16:34 -07003663# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003664 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003665 my $fixed_line = "";
3666 my $line_fixed = 0;
3667
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003668 my $ops = qr{
3669 <<=|>>=|<=|>=|==|!=|
3670 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3671 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003672 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003673 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003674 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003675 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003676
3677## print("element count: <" . $#elements . ">\n");
3678## foreach my $el (@elements) {
3679## print("el: <$el>\n");
3680## }
3681
3682 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003683 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003684
Joe Perches3705ce52013-07-03 15:05:31 -07003685 foreach my $el (@elements) {
3686 push(@fix_elements, substr($rawline, $off, length($el)));
3687 $off += length($el);
3688 }
3689
3690 $off = 0;
3691
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003692 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003693 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003694
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003695 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003696
3697 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3698
3699## print("n: <$n> good: <$good>\n");
3700
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003701 $off += length($elements[$n]);
3702
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003703 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003704 my $ca = substr($opline, 0, $off);
3705 my $cc = '';
3706 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3707 $cc = substr($opline, $off + length($elements[$n + 1]));
3708 }
3709 my $cb = "$ca$;$cc";
3710
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003711 my $a = '';
3712 $a = 'V' if ($elements[$n] ne '');
3713 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003714 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003715 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3716 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003717 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003718
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003719 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003720
3721 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003722 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003723 $c = 'V' if ($elements[$n + 2] ne '');
3724 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003725 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003726 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3727 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003728 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003729 } else {
3730 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003731 }
3732
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003733 my $ctx = "${a}x${c}";
3734
3735 my $at = "(ctx:$ctx)";
3736
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003737 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003738 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003739
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003740 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003741 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003742
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003743 # Get the full operator variant.
3744 my $opv = $op . substr($curr_vars, $off, 1);
3745
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003746 # Ignore operators passed as parameters.
3747 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07003748 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003749
Andy Whitcroftcf655042008-03-04 14:28:20 -08003750# # Ignore comments
3751# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003752
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003753 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003754 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003755 if ($ctx !~ /.x[WEBC]/ &&
3756 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003757 if (ERROR("SPACING",
3758 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003759 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003760 $line_fixed = 1;
3761 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003762 }
3763
3764 # // is a comment
3765 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003766
Joe Perchesb00e4812014-04-03 14:49:33 -07003767 # : when part of a bitfield
3768 } elsif ($opv eq ':B') {
3769 # skip the bitfield test for now
3770
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003771 # No spaces for:
3772 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003773 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003774 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003775 if (ERROR("SPACING",
3776 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003777 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003778 if (defined $fix_elements[$n + 2]) {
3779 $fix_elements[$n + 2] =~ s/^\s+//;
3780 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003781 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003782 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003783 }
3784
Joe Perches23810972014-12-10 15:51:32 -08003785 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003786 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003787 my $rtrim_before = 0;
3788 my $space_after = 0;
3789 if ($ctx =~ /Wx./) {
3790 if (ERROR("SPACING",
3791 "space prohibited before that '$op' $at\n" . $hereptr)) {
3792 $line_fixed = 1;
3793 $rtrim_before = 1;
3794 }
3795 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003796 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003797 if (ERROR("SPACING",
3798 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003799 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003800 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003801 $space_after = 1;
3802 }
3803 }
3804 if ($rtrim_before || $space_after) {
3805 if ($rtrim_before) {
3806 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3807 } else {
3808 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3809 }
3810 if ($space_after) {
3811 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003812 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003813 }
3814
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003815 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003816 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003817 #warn "'*' is part of type\n";
3818
3819 # unary operators should have a space before and
3820 # none after. May be left adjacent to another
3821 # unary operator, or a cast
3822 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003823 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003824 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003825 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003826 if (ERROR("SPACING",
3827 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003828 if ($n != $last_after + 2) {
3829 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3830 $line_fixed = 1;
3831 }
Joe Perches3705ce52013-07-03 15:05:31 -07003832 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003833 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003834 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003835 # A unary '*' may be const
3836
3837 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003838 if (ERROR("SPACING",
3839 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003840 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003841 if (defined $fix_elements[$n + 2]) {
3842 $fix_elements[$n + 2] =~ s/^\s+//;
3843 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003844 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003845 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003846 }
3847
3848 # unary ++ and unary -- are allowed no space on one side.
3849 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003850 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003851 if (ERROR("SPACING",
3852 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003853 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003854 $line_fixed = 1;
3855 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003856 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003857 if ($ctx =~ /Wx[BE]/ ||
3858 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003859 if (ERROR("SPACING",
3860 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003861 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003862 $line_fixed = 1;
3863 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003864 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003865 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003866 if (ERROR("SPACING",
3867 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003868 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003869 if (defined $fix_elements[$n + 2]) {
3870 $fix_elements[$n + 2] =~ s/^\s+//;
3871 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003872 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003873 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003874 }
3875
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003876 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003877 } elsif ($op eq '<<' or $op eq '>>' or
3878 $op eq '&' or $op eq '^' or $op eq '|' or
3879 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003880 $op eq '*' or $op eq '/' or
3881 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003882 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08003883 if ($check) {
3884 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3885 if (CHK("SPACING",
3886 "spaces preferred around that '$op' $at\n" . $hereptr)) {
3887 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3888 $fix_elements[$n + 2] =~ s/^\s+//;
3889 $line_fixed = 1;
3890 }
3891 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3892 if (CHK("SPACING",
3893 "space preferred before that '$op' $at\n" . $hereptr)) {
3894 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3895 $line_fixed = 1;
3896 }
3897 }
3898 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003899 if (ERROR("SPACING",
3900 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003901 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3902 if (defined $fix_elements[$n + 2]) {
3903 $fix_elements[$n + 2] =~ s/^\s+//;
3904 }
Joe Perches3705ce52013-07-03 15:05:31 -07003905 $line_fixed = 1;
3906 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003907 }
3908
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003909 # A colon needs no spaces before when it is
3910 # terminating a case value or a label.
3911 } elsif ($opv eq ':C' || $opv eq ':L') {
3912 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003913 if (ERROR("SPACING",
3914 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003915 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003916 $line_fixed = 1;
3917 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003918 }
3919
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003920 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003921 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003922 my $ok = 0;
3923
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003924 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003925 if (($op eq '<' &&
3926 $cc =~ /^\S+\@\S+>/) ||
3927 ($op eq '>' &&
3928 $ca =~ /<\S+\@\S+$/))
3929 {
3930 $ok = 1;
3931 }
3932
Joe Perchese0df7e12015-04-16 12:44:53 -07003933 # for asm volatile statements
3934 # ignore a colon with another
3935 # colon immediately before or after
3936 if (($op eq ':') &&
3937 ($ca =~ /:$/ || $cc =~ /^:/)) {
3938 $ok = 1;
3939 }
3940
Joe Perches84731622013-11-12 15:10:05 -08003941 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003942 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003943 my $msg_type = \&ERROR;
3944 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3945
3946 if (&{$msg_type}("SPACING",
3947 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003948 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3949 if (defined $fix_elements[$n + 2]) {
3950 $fix_elements[$n + 2] =~ s/^\s+//;
3951 }
Joe Perches3705ce52013-07-03 15:05:31 -07003952 $line_fixed = 1;
3953 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003954 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003955 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003956 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003957
3958## print("n: <$n> GOOD: <$good>\n");
3959
3960 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003961 }
Joe Perches3705ce52013-07-03 15:05:31 -07003962
3963 if (($#elements % 2) == 0) {
3964 $fixed_line = $fixed_line . $fix_elements[$#elements];
3965 }
3966
Joe Perches194f66f2014-08-06 16:11:03 -07003967 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3968 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003969 }
3970
3971
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003972 }
3973
Joe Perches786b6322013-07-03 15:05:32 -07003974# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003975 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003976 if (WARN("SPACING",
3977 "space prohibited before semicolon\n" . $herecurr) &&
3978 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003979 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003980 s/^(\+.*\S)\s+;/$1;/;
3981 }
3982 }
3983
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003984# check for multiple assignments
3985 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003986 CHK("MULTIPLE_ASSIGNMENTS",
3987 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003988 }
3989
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003990## # check for multiple declarations, allowing for a function declaration
3991## # continuation.
3992## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3993## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3994##
3995## # Remove any bracketed sections to ensure we do not
3996## # falsly report the parameters of functions.
3997## my $ln = $line;
3998## while ($ln =~ s/\([^\(\)]*\)//g) {
3999## }
4000## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004001## WARN("MULTIPLE_DECLARATION",
4002## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004003## }
4004## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004005
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004006#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004007 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
4008 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004009 if (ERROR("SPACING",
4010 "space required before the open brace '{'\n" . $herecurr) &&
4011 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004012 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004013 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004014 }
4015
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004016## # check for blank lines before declarations
4017## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4018## $prevrawline =~ /^.\s*$/) {
4019## WARN("SPACING",
4020## "No blank lines before declarations\n" . $hereprev);
4021## }
4022##
4023
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004024# closing brace should have a space following it when it has anything
4025# on the line
4026 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004027 if (ERROR("SPACING",
4028 "space required after that close brace '}'\n" . $herecurr) &&
4029 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004030 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004031 s/}((?!(?:,|;|\)))\S)/} $1/;
4032 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004033 }
4034
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004035# check spacing on square brackets
4036 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004037 if (ERROR("SPACING",
4038 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4039 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004040 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004041 s/\[\s+/\[/;
4042 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004043 }
4044 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004045 if (ERROR("SPACING",
4046 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4047 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004048 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004049 s/\s+\]/\]/;
4050 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004051 }
4052
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004053# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004054 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4055 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004056 if (ERROR("SPACING",
4057 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4058 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004059 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004060 s/\(\s+/\(/;
4061 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004062 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004063 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004064 $line !~ /for\s*\(.*;\s+\)/ &&
4065 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004066 if (ERROR("SPACING",
4067 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4068 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004069 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004070 s/\s+\)/\)/;
4071 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004072 }
4073
Joe Perchese2826fd2014-08-06 16:10:48 -07004074# check unnecessary parentheses around addressof/dereference single $Lvals
4075# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4076
4077 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004078 my $var = $1;
4079 if (CHK("UNNECESSARY_PARENTHESES",
4080 "Unnecessary parentheses around $var\n" . $herecurr) &&
4081 $fix) {
4082 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4083 }
4084 }
4085
4086# check for unnecessary parentheses around function pointer uses
4087# ie: (foo->bar)(); should be foo->bar();
4088# but not "if (foo->bar) (" to avoid some false positives
4089 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4090 my $var = $2;
4091 if (CHK("UNNECESSARY_PARENTHESES",
4092 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4093 $fix) {
4094 my $var2 = deparenthesize($var);
4095 $var2 =~ s/\s//g;
4096 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4097 }
4098 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004099
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004100#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004101 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004102 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004103 if (WARN("INDENTED_LABEL",
4104 "labels should not be indented\n" . $herecurr) &&
4105 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004106 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004107 s/^(.)\s+/$1/;
4108 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004109 }
4110
Joe Perches5b9553a2014-04-03 14:49:21 -07004111# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004112 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004113 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004114 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004115 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4116 my $value = $1;
4117 $value = deparenthesize($value);
4118 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4119 ERROR("RETURN_PARENTHESES",
4120 "return is not a function, parentheses are not required\n" . $herecurr);
4121 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004122 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004123 ERROR("SPACING",
4124 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004125 }
4126 }
Joe Perches507e5142013-11-12 15:10:13 -08004127
Joe Perchesb43ae212014-06-23 13:22:07 -07004128# unnecessary return in a void function
4129# at end-of-function, with the previous line a single leading tab, then return;
4130# and the line before that not a goto label target like "out:"
4131 if ($sline =~ /^[ \+]}\s*$/ &&
4132 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4133 $linenr >= 3 &&
4134 $lines[$linenr - 3] =~ /^[ +]/ &&
4135 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004136 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004137 "void function return statements are not generally useful\n" . $hereprev);
4138 }
Joe Perches9819cf22014-06-04 16:12:09 -07004139
Joe Perches189248d2014-01-23 15:54:47 -08004140# if statements using unnecessary parentheses - ie: if ((foo == bar))
4141 if ($^V && $^V ge 5.10.0 &&
4142 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4143 my $openparens = $1;
4144 my $count = $openparens =~ tr@\(@\(@;
4145 my $msg = "";
4146 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4147 my $comp = $4; #Not $1 because of $LvalOrFunc
4148 $msg = " - maybe == should be = ?" if ($comp eq "==");
4149 WARN("UNNECESSARY_PARENTHESES",
4150 "Unnecessary parentheses$msg\n" . $herecurr);
4151 }
4152 }
4153
Joe Perchesf34e4a42015-04-16 12:44:19 -07004154# Return of what appears to be an errno should normally be negative
4155 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004156 my $name = $1;
4157 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004158 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004159 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004160 }
4161 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004162
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004163# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004164 if ($line =~ /\b(if|while|for|switch)\(/) {
4165 if (ERROR("SPACING",
4166 "space required before the open parenthesis '('\n" . $herecurr) &&
4167 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004168 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004169 s/\b(if|while|for|switch)\(/$1 \(/;
4170 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004171 }
4172
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004173# Check for illegal assignment in if conditional -- and check for trailing
4174# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004175 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004176 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4177 ctx_statement_block($linenr, $realcnt, 0)
4178 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004179 my ($stat_next) = ctx_statement_block($line_nr_next,
4180 $remain_next, $off_next);
4181 $stat_next =~ s/\n./\n /g;
4182 ##print "stat<$stat> stat_next<$stat_next>\n";
4183
4184 if ($stat_next =~ /^\s*while\b/) {
4185 # If the statement carries leading newlines,
4186 # then count those as offsets.
4187 my ($whitespace) =
4188 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4189 my $offset =
4190 statement_rawlines($whitespace) - 1;
4191
4192 $suppress_whiletrailers{$line_nr_next +
4193 $offset} = 1;
4194 }
4195 }
4196 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004197 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004198 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004199 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004200
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004201 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004202 ERROR("ASSIGN_IN_IF",
4203 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004204 }
4205
4206 # Find out what is on the end of the line after the
4207 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004208 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004209 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004210 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004211 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4212 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004213 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004214 # Find out how long the conditional actually is.
4215 my @newlines = ($c =~ /\n/gs);
4216 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004217 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004218
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004219 $stat_real = raw_line($linenr, $cond_lines)
4220 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004221 if (defined($stat_real) && $cond_lines > 1) {
4222 $stat_real = "[...]\n$stat_real";
4223 }
4224
Joe Perches000d1cc12011-07-25 17:13:25 -07004225 ERROR("TRAILING_STATEMENTS",
4226 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004227 }
4228 }
4229
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004230# Check for bitwise tests written as boolean
4231 if ($line =~ /
4232 (?:
4233 (?:\[|\(|\&\&|\|\|)
4234 \s*0[xX][0-9]+\s*
4235 (?:\&\&|\|\|)
4236 |
4237 (?:\&\&|\|\|)
4238 \s*0[xX][0-9]+\s*
4239 (?:\&\&|\|\||\)|\])
4240 )/x)
4241 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004242 WARN("HEXADECIMAL_BOOLEAN_TEST",
4243 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004244 }
4245
Andy Whitcroft8905a672007-11-28 16:21:06 -08004246# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004247 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4248 my $s = $1;
4249 $s =~ s/$;//g; # Remove any comments
4250 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004251 ERROR("TRAILING_STATEMENTS",
4252 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004253 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004254 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004255# if should not continue a brace
4256 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004257 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004258 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004259 $herecurr);
4260 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004261# case and default should not have general statements after them
4262 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4263 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004264 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004265 \s*return\s+
4266 )/xg)
4267 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004268 ERROR("TRAILING_STATEMENTS",
4269 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004270 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004271
4272 # Check for }<nl>else {, these must be at the same
4273 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004274 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4275 $previndent == $indent) {
4276 if (ERROR("ELSE_AFTER_BRACE",
4277 "else should follow close brace '}'\n" . $hereprev) &&
4278 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4279 fix_delete_line($fixlinenr - 1, $prevrawline);
4280 fix_delete_line($fixlinenr, $rawline);
4281 my $fixedline = $prevrawline;
4282 $fixedline =~ s/}\s*$//;
4283 if ($fixedline !~ /^\+\s*$/) {
4284 fix_insert_line($fixlinenr, $fixedline);
4285 }
4286 $fixedline = $rawline;
4287 $fixedline =~ s/^(.\s*)else/$1} else/;
4288 fix_insert_line($fixlinenr, $fixedline);
4289 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004290 }
4291
Joe Perches8b8856f2014-08-06 16:11:14 -07004292 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4293 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004294 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4295
4296 # Find out what is on the end of the line after the
4297 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004298 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004299 $s =~ s/\n.*//g;
4300
4301 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004302 if (ERROR("WHILE_AFTER_BRACE",
4303 "while should follow close brace '}'\n" . $hereprev) &&
4304 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4305 fix_delete_line($fixlinenr - 1, $prevrawline);
4306 fix_delete_line($fixlinenr, $rawline);
4307 my $fixedline = $prevrawline;
4308 my $trailing = $rawline;
4309 $trailing =~ s/^\+//;
4310 $trailing = trim($trailing);
4311 $fixedline =~ s/}\s*$/} $trailing/;
4312 fix_insert_line($fixlinenr, $fixedline);
4313 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004314 }
4315 }
4316
Joe Perches95e2c602013-07-03 15:05:20 -07004317#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004318 while ($line =~ m{($Constant|$Lval)}g) {
4319 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004320
4321#gcc binary extension
4322 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004323 if (WARN("GCC_BINARY_CONSTANT",
4324 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4325 $fix) {
4326 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004327 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004328 s/\b$var\b/$hexval/;
4329 }
Joe Perches95e2c602013-07-03 15:05:20 -07004330 }
4331
4332#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004333 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004334 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004335#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004336 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004337#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004338 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4339#Ignore some three character SI units explicitly, like MiB and KHz
4340 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004341 while ($var =~ m{($Ident)}g) {
4342 my $word = $1;
4343 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004344 if ($check) {
4345 seed_camelcase_includes();
4346 if (!$file && !$camelcase_file_seeded) {
4347 seed_camelcase_file($realfile);
4348 $camelcase_file_seeded = 1;
4349 }
4350 }
Joe Perches7e781f62013-09-11 14:23:55 -07004351 if (!defined $camelcase{$word}) {
4352 $camelcase{$word} = 1;
4353 CHK("CAMELCASE",
4354 "Avoid CamelCase: <$word>\n" . $herecurr);
4355 }
Joe Perches34456862013-07-03 15:05:34 -07004356 }
Joe Perches323c1262012-12-17 16:02:07 -08004357 }
4358 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004359
4360#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004361 if ($line =~ /\#\s*define.*\\\s+$/) {
4362 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4363 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4364 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004365 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004366 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004367 }
4368
Fabian Frederick0e212e02015-04-16 12:44:25 -07004369# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4370# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004371 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004372 my $file = "$1.h";
4373 my $checkfile = "include/linux/$file";
4374 if (-f "$root/$checkfile" &&
4375 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004376 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004377 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004378 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4379 if ($asminclude > 0) {
4380 if ($realfile =~ m{^arch/}) {
4381 CHK("ARCH_INCLUDE_LINUX",
4382 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4383 } else {
4384 WARN("INCLUDE_LINUX",
4385 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4386 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004387 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004388 }
4389 }
4390
Andy Whitcroft653d4872007-06-23 17:16:34 -07004391# multi-statement macros should be enclosed in a do while loop, grab the
4392# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004393# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004394 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4395 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004396 my $ln = $linenr;
4397 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004398 my ($off, $dstat, $dcond, $rest);
4399 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004400 my $has_flow_statement = 0;
4401 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004402 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004403 ctx_statement_block($linenr, $realcnt, 0);
4404 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004405 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004406 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004407
Joe Perches08a28432014-10-13 15:51:55 -07004408 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4409 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4410
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004411 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004412 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004413 $dstat =~ s/\\\n.//g;
4414 $dstat =~ s/^\s*//s;
4415 $dstat =~ s/\s*$//s;
4416
4417 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004418 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4419 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004420 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004421 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004422 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004423
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004424 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004425 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4426 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004427 {
4428 }
4429
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004430 my $exceptions = qr{
4431 $Declare|
4432 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004433 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004434 DECLARE_PER_CPU|
4435 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004436 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004437 union|
4438 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004439 \.$Ident\s*=\s*|
4440 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004441 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004442 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004443 if ($dstat ne '' &&
4444 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4445 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004446 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004447 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004448 $dstat !~ /$exceptions/ &&
4449 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004450 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004451 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004452 $dstat !~ /^for\s*$Constant$/ && # for (...)
4453 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4454 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004455 $dstat !~ /^\({/ && # ({...
4456 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004457 {
4458 $ctx =~ s/\n*$//;
4459 my $herectx = $here . "\n";
4460 my $cnt = statement_rawlines($ctx);
4461
4462 for (my $n = 0; $n < $cnt; $n++) {
4463 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004464 }
4465
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004466 if ($dstat =~ /;/) {
4467 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4468 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4469 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004470 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004471 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004472 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004473 }
Joe Perches5023d342012-12-17 16:01:47 -08004474
Joe Perches08a28432014-10-13 15:51:55 -07004475# check for macros with flow control, but without ## concatenation
4476# ## concatenation is commonly a macro that defines a function so ignore those
4477 if ($has_flow_statement && !$has_arg_concat) {
4478 my $herectx = $here . "\n";
4479 my $cnt = statement_rawlines($ctx);
4480
4481 for (my $n = 0; $n < $cnt; $n++) {
4482 $herectx .= raw_line($linenr, $n) . "\n";
4483 }
4484 WARN("MACRO_WITH_FLOW_CONTROL",
4485 "Macros with flow control statements should be avoided\n" . "$herectx");
4486 }
4487
Joe Perches481eb482012-12-17 16:01:56 -08004488# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004489
4490 } else {
4491 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004492 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4493 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004494 $line =~ /^\+.*\\$/) {
4495 WARN("LINE_CONTINUATIONS",
4496 "Avoid unnecessary line continuations\n" . $herecurr);
4497 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004498 }
4499
Joe Perchesb13edf72012-07-30 14:41:24 -07004500# do {} while (0) macro tests:
4501# single-statement macros do not need to be enclosed in do while (0) loop,
4502# macro should not end with a semicolon
4503 if ($^V && $^V ge 5.10.0 &&
4504 $realfile !~ m@/vmlinux.lds.h$@ &&
4505 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4506 my $ln = $linenr;
4507 my $cnt = $realcnt;
4508 my ($off, $dstat, $dcond, $rest);
4509 my $ctx = '';
4510 ($dstat, $dcond, $ln, $cnt, $off) =
4511 ctx_statement_block($linenr, $realcnt, 0);
4512 $ctx = $dstat;
4513
4514 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004515 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004516
4517 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4518 my $stmts = $2;
4519 my $semis = $3;
4520
4521 $ctx =~ s/\n*$//;
4522 my $cnt = statement_rawlines($ctx);
4523 my $herectx = $here . "\n";
4524
4525 for (my $n = 0; $n < $cnt; $n++) {
4526 $herectx .= raw_line($linenr, $n) . "\n";
4527 }
4528
Joe Perchesac8e97f2012-08-21 16:15:53 -07004529 if (($stmts =~ tr/;/;/) == 1 &&
4530 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004531 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4532 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4533 }
4534 if (defined $semis && $semis ne "") {
4535 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4536 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4537 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004538 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4539 $ctx =~ s/\n*$//;
4540 my $cnt = statement_rawlines($ctx);
4541 my $herectx = $here . "\n";
4542
4543 for (my $n = 0; $n < $cnt; $n++) {
4544 $herectx .= raw_line($linenr, $n) . "\n";
4545 }
4546
4547 WARN("TRAILING_SEMICOLON",
4548 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004549 }
4550 }
4551
Mike Frysinger080ba922009-01-06 14:41:25 -08004552# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4553# all assignments may have only one of the following with an assignment:
4554# .
4555# ALIGN(...)
4556# VMLINUX_SYMBOL(...)
4557 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004558 WARN("MISSING_VMLINUX_SYMBOL",
4559 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004560 }
4561
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004562# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004563 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4564 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004565 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004566 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004567 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4568 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004569 my @allowed = ();
4570 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004571 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004572 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004573 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004574 for my $chunk (@chunks) {
4575 my ($cond, $block) = @{$chunk};
4576
Andy Whitcroft773647a2008-03-28 14:15:58 -07004577 # If the condition carries leading newlines, then count those as offsets.
4578 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4579 my $offset = statement_rawlines($whitespace) - 1;
4580
Joe Perchesaad4f612012-03-23 15:02:19 -07004581 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004582 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4583
4584 # We have looked at and allowed this specific line.
4585 $suppress_ifbraces{$ln + $offset} = 1;
4586
4587 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004588 $ln += statement_rawlines($block) - 1;
4589
Andy Whitcroft773647a2008-03-28 14:15:58 -07004590 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004591
4592 $seen++ if ($block =~ /^\s*{/);
4593
Joe Perchesaad4f612012-03-23 15:02:19 -07004594 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004595 if (statement_lines($cond) > 1) {
4596 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004597 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004598 }
4599 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004600 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004601 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004602 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004603 if (statement_block_size($block) > 1) {
4604 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004605 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004606 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004607 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004608 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004609 if ($seen) {
4610 my $sum_allowed = 0;
4611 foreach (@allowed) {
4612 $sum_allowed += $_;
4613 }
4614 if ($sum_allowed == 0) {
4615 WARN("BRACES",
4616 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4617 } elsif ($sum_allowed != $allow &&
4618 $seen != $allow) {
4619 CHK("BRACES",
4620 "braces {} should be used on all arms of this statement\n" . $herectx);
4621 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004622 }
4623 }
4624 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004625 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004626 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004627 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004628
Andy Whitcroftcf655042008-03-04 14:28:20 -08004629 # Check the pre-context.
4630 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4631 #print "APW: ALLOWED: pre<$1>\n";
4632 $allowed = 1;
4633 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004634
4635 my ($level, $endln, @chunks) =
4636 ctx_statement_full($linenr, $realcnt, $-[0]);
4637
Andy Whitcroftcf655042008-03-04 14:28:20 -08004638 # Check the condition.
4639 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004640 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004641 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004642 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004643 }
4644 if (statement_lines($cond) > 1) {
4645 #print "APW: ALLOWED: cond<$cond>\n";
4646 $allowed = 1;
4647 }
4648 if ($block =~/\b(?:if|for|while)\b/) {
4649 #print "APW: ALLOWED: block<$block>\n";
4650 $allowed = 1;
4651 }
4652 if (statement_block_size($block) > 1) {
4653 #print "APW: ALLOWED: lines block<$block>\n";
4654 $allowed = 1;
4655 }
4656 # Check the post-context.
4657 if (defined $chunks[1]) {
4658 my ($cond, $block) = @{$chunks[1]};
4659 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004660 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004661 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004662 if ($block =~ /^\s*\{/) {
4663 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4664 $allowed = 1;
4665 }
4666 }
4667 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004668 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004669 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004670
Andy Whitcroftf0556632008-10-15 22:02:23 -07004671 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004672 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004673 }
4674
Joe Perches000d1cc12011-07-25 17:13:25 -07004675 WARN("BRACES",
4676 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004677 }
4678 }
4679
Joe Perches0979ae62012-12-17 16:01:59 -08004680# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004681 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004682 if (CHK("BRACES",
4683 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4684 $fix && $prevrawline =~ /^\+/) {
4685 fix_delete_line($fixlinenr - 1, $prevrawline);
4686 }
Joe Perches0979ae62012-12-17 16:01:59 -08004687 }
Joe Perches77b9a532013-07-03 15:05:29 -07004688 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004689 if (CHK("BRACES",
4690 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4691 $fix) {
4692 fix_delete_line($fixlinenr, $rawline);
4693 }
Joe Perches0979ae62012-12-17 16:01:59 -08004694 }
4695
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004696# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004697 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4698 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004699 WARN("VOLATILE",
4700 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004701 }
4702
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004703# Check for user-visible strings broken across lines, which breaks the ability
4704# to grep for the string. Make exceptions when the previous string ends in a
4705# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4706# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07004707 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004708 $prevline =~ /"\s*$/ &&
4709 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4710 if (WARN("SPLIT_STRING",
4711 "quoted string split across lines\n" . $hereprev) &&
4712 $fix &&
4713 $prevrawline =~ /^\+.*"\s*$/ &&
4714 $last_coalesced_string_linenr != $linenr - 1) {
4715 my $extracted_string = get_quoted_string($line, $rawline);
4716 my $comma_close = "";
4717 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4718 $comma_close = $1;
4719 }
4720
4721 fix_delete_line($fixlinenr - 1, $prevrawline);
4722 fix_delete_line($fixlinenr, $rawline);
4723 my $fixedline = $prevrawline;
4724 $fixedline =~ s/"\s*$//;
4725 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4726 fix_insert_line($fixlinenr - 1, $fixedline);
4727 $fixedline = $rawline;
4728 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4729 if ($fixedline !~ /\+\s*$/) {
4730 fix_insert_line($fixlinenr, $fixedline);
4731 }
4732 $last_coalesced_string_linenr = $linenr;
4733 }
4734 }
4735
4736# check for missing a space in a string concatenation
4737 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4738 WARN('MISSING_SPACE',
4739 "break quoted strings at a space character\n" . $hereprev);
4740 }
4741
4742# check for spaces before a quoted newline
4743 if ($rawline =~ /^.*\".*\s\\n/) {
4744 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4745 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4746 $fix) {
4747 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4748 }
4749
4750 }
4751
Joe Perchesf17dba42014-10-13 15:51:51 -07004752# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07004753 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07004754 CHK("CONCATENATED_STRING",
4755 "Concatenated strings should use spaces between elements\n" . $herecurr);
4756 }
4757
Joe Perches90ad30e2014-12-10 15:51:59 -08004758# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07004759 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08004760 WARN("STRING_FRAGMENTS",
4761 "Consecutive strings are generally better as a single string\n" . $herecurr);
4762 }
4763
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004764# check for %L{u,d,i} in strings
4765 my $string;
4766 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4767 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4768 $string =~ s/%%/__/g;
4769 if ($string =~ /(?<!%)%L[udi]/) {
4770 WARN("PRINTF_L",
4771 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4772 last;
4773 }
4774 }
4775
4776# check for line continuations in quoted strings with odd counts of "
4777 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4778 WARN("LINE_CONTINUATIONS",
4779 "Avoid line continuations in quoted strings\n" . $herecurr);
4780 }
4781
Andy Whitcroft00df3442007-06-08 13:47:06 -07004782# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004783 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004784 CHK("REDUNDANT_CODE",
4785 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004786 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004787 }
4788
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004789# check for needless "if (<foo>) fn(<foo>)" uses
4790 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4791 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4792 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4793 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004794 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004795 }
4796 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004797
Joe Perchesebfdc402014-08-06 16:10:27 -07004798# check for unnecessary "Out of Memory" messages
4799 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4800 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4801 (defined $1 || defined $3) &&
4802 $linenr > 3) {
4803 my $testval = $2;
4804 my $testline = $lines[$linenr - 3];
4805
4806 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4807# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4808
4809 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4810 WARN("OOM_MESSAGE",
4811 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4812 }
4813 }
4814
Joe Perchesf78d98f2014-10-13 15:52:01 -07004815# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004816 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004817 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4818 my $level = $1;
4819 if (WARN("UNNECESSARY_KERN_LEVEL",
4820 "Possible unnecessary $level\n" . $herecurr) &&
4821 $fix) {
4822 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4823 }
4824 }
4825
Joe Perchesabb08a52014-12-10 15:51:46 -08004826# check for mask then right shift without a parentheses
4827 if ($^V && $^V ge 5.10.0 &&
4828 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4829 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4830 WARN("MASK_THEN_SHIFT",
4831 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4832 }
4833
Joe Perchesb75ac612014-12-10 15:52:02 -08004834# check for pointer comparisons to NULL
4835 if ($^V && $^V ge 5.10.0) {
4836 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4837 my $val = $1;
4838 my $equal = "!";
4839 $equal = "" if ($4 eq "!=");
4840 if (CHK("COMPARISON_TO_NULL",
4841 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4842 $fix) {
4843 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4844 }
4845 }
4846 }
4847
Joe Perches8716de32013-09-11 14:24:05 -07004848# check for bad placement of section $InitAttribute (e.g.: __initdata)
4849 if ($line =~ /(\b$InitAttribute\b)/) {
4850 my $attr = $1;
4851 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4852 my $ptr = $1;
4853 my $var = $2;
4854 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4855 ERROR("MISPLACED_INIT",
4856 "$attr should be placed after $var\n" . $herecurr)) ||
4857 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4858 WARN("MISPLACED_INIT",
4859 "$attr should be placed after $var\n" . $herecurr))) &&
4860 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004861 $fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
Joe Perches8716de32013-09-11 14:24:05 -07004862 }
4863 }
4864 }
4865
Joe Perchese970b882013-11-12 15:10:10 -08004866# check for $InitAttributeData (ie: __initdata) with const
4867 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4868 my $attr = $1;
4869 $attr =~ /($InitAttributePrefix)(.*)/;
4870 my $attr_prefix = $1;
4871 my $attr_type = $2;
4872 if (ERROR("INIT_ATTRIBUTE",
4873 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4874 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004875 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004876 s/$InitAttributeData/${attr_prefix}initconst/;
4877 }
4878 }
4879
4880# check for $InitAttributeConst (ie: __initconst) without const
4881 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4882 my $attr = $1;
4883 if (ERROR("INIT_ATTRIBUTE",
4884 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4885 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004886 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004887 /(^\+\s*(?:static\s+))/;
4888 $lead = rtrim($1);
4889 $lead = "$lead " if ($lead !~ /^\+$/);
4890 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004891 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08004892 }
4893 }
4894
Joe Perchesc17893c2015-04-16 12:44:42 -07004895# check for __read_mostly with const non-pointer (should just be const)
4896 if ($line =~ /\b__read_mostly\b/ &&
4897 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
4898 if (ERROR("CONST_READ_MOSTLY",
4899 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
4900 $fix) {
4901 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
4902 }
4903 }
4904
Joe Perchesfbdb8132014-04-03 14:49:14 -07004905# don't use __constant_<foo> functions outside of include/uapi/
4906 if ($realfile !~ m@^include/uapi/@ &&
4907 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4908 my $constant_func = $1;
4909 my $func = $constant_func;
4910 $func =~ s/^__constant_//;
4911 if (WARN("CONSTANT_CONVERSION",
4912 "$constant_func should be $func\n" . $herecurr) &&
4913 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004914 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004915 }
4916 }
4917
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004918# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004919 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004920 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004921 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004922 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004923 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004924 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4925 }
4926 if ($delay > 2000) {
4927 WARN("LONG_UDELAY",
4928 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004929 }
4930 }
4931
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004932# warn about unexpectedly long msleep's
4933 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4934 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004935 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004936 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004937 }
4938 }
4939
Joe Perches36ec1932013-07-03 15:05:25 -07004940# check for comparisons of jiffies
4941 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4942 WARN("JIFFIES_COMPARISON",
4943 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4944 }
4945
Joe Perches9d7a34a2013-07-03 15:05:26 -07004946# check for comparisons of get_jiffies_64()
4947 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4948 WARN("JIFFIES_COMPARISON",
4949 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4950 }
4951
Andy Whitcroft00df3442007-06-08 13:47:06 -07004952# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004953# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004954# print "#ifdef in C files should be avoided\n";
4955# print "$herecurr";
4956# $clean = 0;
4957# }
4958
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004959# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004960 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004961 if (ERROR("SPACING",
4962 "exactly one space required after that #$1\n" . $herecurr) &&
4963 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004964 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004965 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4966 }
4967
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004968 }
4969
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004970# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004971 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4972 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004973 my $which = $1;
4974 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004975 CHK("UNCOMMENTED_DEFINITION",
4976 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004977 }
4978 }
4979# check for memory barriers without a comment.
4980 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4981 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004982 WARN("MEMORY_BARRIER",
4983 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004984 }
4985 }
Joe Perchescb426e92015-06-25 15:02:46 -07004986# check for waitqueue_active without a comment.
4987 if ($line =~ /\bwaitqueue_active\s*\(/) {
4988 if (!ctx_has_comment($first_line, $linenr)) {
4989 WARN("WAITQUEUE_ACTIVE",
4990 "waitqueue_active without comment\n" . $herecurr);
4991 }
4992 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004993# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004994 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004995 CHK("ARCH_DEFINES",
4996 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004997 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004998
Tobias Klauserd4977c72010-05-24 14:33:30 -07004999# Check that the storage class is at the beginning of a declaration
5000 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005001 WARN("STORAGE_CLASS",
5002 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005003 }
5004
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005005# check the location of the inline attribute, that it is between
5006# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005007 if ($line =~ /\b$Type\s+$Inline\b/ ||
5008 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005009 ERROR("INLINE_LOCATION",
5010 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005011 }
5012
Andy Whitcroft8905a672007-11-28 16:21:06 -08005013# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005014 if ($realfile !~ m@\binclude/uapi/@ &&
5015 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005016 if (WARN("INLINE",
5017 "plain inline is preferred over $1\n" . $herecurr) &&
5018 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005019 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005020
5021 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005022 }
5023
Joe Perches3d130fd2011-01-12 17:00:00 -08005024# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005025 if ($realfile !~ m@\binclude/uapi/@ &&
5026 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005027 WARN("PREFER_PACKED",
5028 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005029 }
5030
Joe Perches39b7e282011-07-25 17:13:24 -07005031# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005032 if ($realfile !~ m@\binclude/uapi/@ &&
5033 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005034 WARN("PREFER_ALIGNED",
5035 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005036 }
5037
Joe Perches5f14d3b2012-01-10 15:09:52 -08005038# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005039 if ($realfile !~ m@\binclude/uapi/@ &&
5040 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005041 if (WARN("PREFER_PRINTF",
5042 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5043 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005044 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005045
5046 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005047 }
5048
Joe Perches6061d942012-03-23 15:02:16 -07005049# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005050 if ($realfile !~ m@\binclude/uapi/@ &&
5051 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005052 if (WARN("PREFER_SCANF",
5053 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5054 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005055 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005056 }
Joe Perches6061d942012-03-23 15:02:16 -07005057 }
5058
Joe Perches619a9082014-12-10 15:51:35 -08005059# Check for __attribute__ weak, or __weak declarations (may have link issues)
5060 if ($^V && $^V ge 5.10.0 &&
5061 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5062 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5063 $line =~ /\b__weak\b/)) {
5064 ERROR("WEAK_DECLARATION",
5065 "Using weak declarations can have unintended link defects\n" . $herecurr);
5066 }
5067
Joe Perchese6176fa2015-06-25 15:02:49 -07005068# check for c99 types like uint8_t used outside of uapi/
5069 if ($realfile !~ m@\binclude/uapi/@ &&
5070 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5071 my $type = $1;
5072 if ($type =~ /\b($typeC99Typedefs)\b/) {
5073 $type = $1;
5074 my $kernel_type = 'u';
5075 $kernel_type = 's' if ($type =~ /^_*[si]/);
5076 $type =~ /(\d+)/;
5077 $kernel_type .= $1;
5078 if (CHK("PREFER_KERNEL_TYPES",
5079 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5080 $fix) {
5081 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5082 }
5083 }
5084 }
5085
Joe Perches8f53a9b2010-03-05 13:43:48 -08005086# check for sizeof(&)
5087 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005088 WARN("SIZEOF_ADDRESS",
5089 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005090 }
5091
Joe Perches66c80b62012-07-30 14:41:22 -07005092# check for sizeof without parenthesis
5093 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005094 if (WARN("SIZEOF_PARENTHESIS",
5095 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5096 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005097 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005098 }
Joe Perches66c80b62012-07-30 14:41:22 -07005099 }
5100
Joe Perches88982fe2012-12-17 16:02:00 -08005101# check for struct spinlock declarations
5102 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5103 WARN("USE_SPINLOCK_T",
5104 "struct spinlock should be spinlock_t\n" . $herecurr);
5105 }
5106
Joe Perchesa6962d72013-04-29 16:18:13 -07005107# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005108 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005109 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005110 $fmt =~ s/%%//g;
5111 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005112 if (WARN("PREFER_SEQ_PUTS",
5113 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5114 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005115 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005116 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005117 }
5118 }
5119
Andy Whitcroft554e1652012-01-10 15:09:57 -08005120# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005121 if ($^V && $^V ge 5.10.0 &&
5122 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005123 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005124
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005125 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005126 my $ms_val = $7;
5127 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005128
Andy Whitcroft554e1652012-01-10 15:09:57 -08005129 if ($ms_size =~ /^(0x|)0$/i) {
5130 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005131 "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 -08005132 } elsif ($ms_size =~ /^(0x|)1$/i) {
5133 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005134 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5135 }
5136 }
5137
Joe Perches98a9bba2014-01-23 15:54:52 -08005138# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5139 if ($^V && $^V ge 5.10.0 &&
5140 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
5141 if (WARN("PREFER_ETHER_ADDR_COPY",
5142 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
5143 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005144 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
Joe Perches98a9bba2014-01-23 15:54:52 -08005145 }
5146 }
5147
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005148# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005149 if ($^V && $^V ge 5.10.0 &&
5150 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005151 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005152 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005153 my $call = $1;
5154 my $cast1 = deparenthesize($2);
5155 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005156 my $cast2 = deparenthesize($7);
5157 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005158 my $cast;
5159
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005160 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005161 $cast = "$cast1 or $cast2";
5162 } elsif ($cast1 ne "") {
5163 $cast = $cast1;
5164 } else {
5165 $cast = $cast2;
5166 }
5167 WARN("MINMAX",
5168 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005169 }
5170 }
5171
Joe Perches4a273192012-07-30 14:41:20 -07005172# check usleep_range arguments
5173 if ($^V && $^V ge 5.10.0 &&
5174 defined $stat &&
5175 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5176 my $min = $1;
5177 my $max = $7;
5178 if ($min eq $max) {
5179 WARN("USLEEP_RANGE",
5180 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5181 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5182 $min > $max) {
5183 WARN("USLEEP_RANGE",
5184 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5185 }
5186 }
5187
Joe Perches823b7942013-11-12 15:10:15 -08005188# check for naked sscanf
5189 if ($^V && $^V ge 5.10.0 &&
5190 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005191 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005192 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5193 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5194 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5195 my $lc = $stat =~ tr@\n@@;
5196 $lc = $lc + $linenr;
5197 my $stat_real = raw_line($linenr, 0);
5198 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5199 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5200 }
5201 WARN("NAKED_SSCANF",
5202 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5203 }
5204
Joe Perchesafc819a2014-06-04 16:12:08 -07005205# check for simple sscanf that should be kstrto<foo>
5206 if ($^V && $^V ge 5.10.0 &&
5207 defined $stat &&
5208 $line =~ /\bsscanf\b/) {
5209 my $lc = $stat =~ tr@\n@@;
5210 $lc = $lc + $linenr;
5211 my $stat_real = raw_line($linenr, 0);
5212 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5213 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5214 }
5215 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5216 my $format = $6;
5217 my $count = $format =~ tr@%@%@;
5218 if ($count == 1 &&
5219 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5220 WARN("SSCANF_TO_KSTRTO",
5221 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5222 }
5223 }
5224 }
5225
Joe Perches70dc8a42013-09-11 14:23:58 -07005226# check for new externs in .h files.
5227 if ($realfile =~ /\.h$/ &&
5228 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005229 if (CHK("AVOID_EXTERNS",
5230 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005231 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005232 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005233 }
5234 }
5235
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005236# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005237 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005238 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005239 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005240 my $function_name = $1;
5241 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005242
5243 my $s = $stat;
5244 if (defined $cond) {
5245 substr($s, 0, length($cond), '');
5246 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005247 if ($s =~ /^\s*;/ &&
5248 $function_name ne 'uninitialized_var')
5249 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005250 WARN("AVOID_EXTERNS",
5251 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005252 }
5253
5254 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005255 WARN("FUNCTION_ARGUMENTS",
5256 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005257 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005258
5259 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5260 $stat =~ /^.\s*extern\s+/)
5261 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005262 WARN("AVOID_EXTERNS",
5263 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005264 }
5265
5266# checks for new __setup's
5267 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5268 my $name = $1;
5269
5270 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005271 CHK("UNDOCUMENTED_SETUP",
5272 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005273 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005274 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005275
5276# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005277 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005278 WARN("UNNECESSARY_CASTS",
5279 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005280 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005281
Joe Perchesa640d252013-07-03 15:05:21 -07005282# alloc style
5283# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5284 if ($^V && $^V ge 5.10.0 &&
5285 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5286 CHK("ALLOC_SIZEOF_STRUCT",
5287 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5288 }
5289
Joe Perches60a55362014-06-04 16:12:07 -07005290# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5291 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005292 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07005293 my $oldfunc = $3;
5294 my $a1 = $4;
5295 my $a2 = $10;
5296 my $newfunc = "kmalloc_array";
5297 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005298 my $r1 = $a1;
5299 my $r2 = $a2;
5300 if ($a1 =~ /^sizeof\s*\S/) {
5301 $r1 = $a2;
5302 $r2 = $a1;
5303 }
5304 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5305 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005306 if (WARN("ALLOC_WITH_MULTIPLY",
5307 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5308 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005309 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
Joe Perches60a55362014-06-04 16:12:07 -07005310
5311 }
5312 }
5313 }
5314
Joe Perches972fdea2013-04-29 16:18:12 -07005315# check for krealloc arg reuse
5316 if ($^V && $^V ge 5.10.0 &&
5317 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5318 WARN("KREALLOC_ARG_REUSE",
5319 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5320 }
5321
Joe Perches5ce59ae2013-02-21 16:44:18 -08005322# check for alloc argument mismatch
5323 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5324 WARN("ALLOC_ARRAY_ARGS",
5325 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5326 }
5327
Joe Perchescaf2a542011-01-12 16:59:56 -08005328# check for multiple semicolons
5329 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005330 if (WARN("ONE_SEMICOLON",
5331 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5332 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005333 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005334 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005335 }
5336
Joe Perches0ab90192014-12-10 15:51:57 -08005337# check for #defines like: 1 << <digit> that could be BIT(digit)
5338 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5339 my $ull = "";
5340 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5341 if (CHK("BIT_MACRO",
5342 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5343 $fix) {
5344 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5345 }
5346 }
5347
Joe Perchese81f2392014-08-06 16:11:25 -07005348# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005349 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5350 my $has_break = 0;
5351 my $has_statement = 0;
5352 my $count = 0;
5353 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005354 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005355 $prevline--;
5356 my $rline = $rawlines[$prevline - 1];
5357 my $fline = $lines[$prevline - 1];
5358 last if ($fline =~ /^\@\@/);
5359 next if ($fline =~ /^\-/);
5360 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5361 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5362 next if ($fline =~ /^.[\s$;]*$/);
5363 $has_statement = 1;
5364 $count++;
5365 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5366 }
5367 if (!$has_break && $has_statement) {
5368 WARN("MISSING_BREAK",
5369 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5370 }
5371 }
5372
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005373# check for switch/default statements without a break;
5374 if ($^V && $^V ge 5.10.0 &&
5375 defined $stat &&
5376 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5377 my $ctx = '';
5378 my $herectx = $here . "\n";
5379 my $cnt = statement_rawlines($stat);
5380 for (my $n = 0; $n < $cnt; $n++) {
5381 $herectx .= raw_line($linenr, $n) . "\n";
5382 }
5383 WARN("DEFAULT_NO_BREAK",
5384 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005385 }
5386
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005387# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005388 if ($line =~ /\b__FUNCTION__\b/) {
5389 if (WARN("USE_FUNC",
5390 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5391 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005392 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005393 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005394 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005395
Joe Perches62ec8182015-02-13 14:38:18 -08005396# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5397 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5398 ERROR("DATE_TIME",
5399 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5400 }
5401
Joe Perches2c924882012-03-23 15:02:20 -07005402# check for use of yield()
5403 if ($line =~ /\byield\s*\(\s*\)/) {
5404 WARN("YIELD",
5405 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5406 }
5407
Joe Perches179f8f42013-07-03 15:05:30 -07005408# check for comparisons against true and false
5409 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5410 my $lead = $1;
5411 my $arg = $2;
5412 my $test = $3;
5413 my $otype = $4;
5414 my $trail = $5;
5415 my $op = "!";
5416
5417 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5418
5419 my $type = lc($otype);
5420 if ($type =~ /^(?:true|false)$/) {
5421 if (("$test" eq "==" && "$type" eq "true") ||
5422 ("$test" eq "!=" && "$type" eq "false")) {
5423 $op = "";
5424 }
5425
5426 CHK("BOOL_COMPARISON",
5427 "Using comparison to $otype is error prone\n" . $herecurr);
5428
5429## maybe suggesting a correct construct would better
5430## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5431
5432 }
5433 }
5434
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005435# check for semaphores initialized locked
5436 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005437 WARN("CONSIDER_COMPLETION",
5438 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005439 }
Joe Perches6712d852012-03-23 15:02:20 -07005440
Joe Perches67d0a072011-10-31 17:13:10 -07005441# recommend kstrto* over simple_strto* and strict_strto*
5442 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005443 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005444 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005445 }
Joe Perches6712d852012-03-23 15:02:20 -07005446
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005447# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005448 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005449 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005450 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07005451 }
Joe Perches6712d852012-03-23 15:02:20 -07005452
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005453# check for various structs that are normally const (ops, kgdb, device_tree)
5454 my $const_structs = qr{
5455 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005456 address_space_operations|
5457 backlight_ops|
5458 block_device_operations|
5459 dentry_operations|
5460 dev_pm_ops|
5461 dma_map_ops|
5462 extent_io_ops|
5463 file_lock_operations|
5464 file_operations|
5465 hv_ops|
5466 ide_dma_ops|
5467 intel_dvo_dev_ops|
5468 item_operations|
5469 iwl_ops|
5470 kgdb_arch|
5471 kgdb_io|
5472 kset_uevent_ops|
5473 lock_manager_operations|
5474 microcode_ops|
5475 mtrr_ops|
5476 neigh_ops|
5477 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005478 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08005479 pci_raw_ops|
5480 pipe_buf_operations|
5481 platform_hibernation_ops|
5482 platform_suspend_ops|
5483 proto_ops|
5484 rpc_pipe_ops|
5485 seq_operations|
5486 snd_ac97_build_ops|
5487 soc_pcmcia_socket_ops|
5488 stacktrace_ops|
5489 sysfs_ops|
5490 tty_operations|
Joe Perches6d07d01b2015-04-16 12:44:33 -07005491 uart_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005492 usb_mon_operations|
5493 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005494 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005495 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005496 WARN("CONST_STRUCT",
5497 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005498 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005499 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005500
5501# use of NR_CPUS is usually wrong
5502# ignore definitions of NR_CPUS and usage to define arrays as likely right
5503 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005504 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5505 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005506 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5507 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5508 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005509 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005510 WARN("NR_CPUS",
5511 "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 -07005512 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005513
Joe Perches52ea8502013-11-12 15:10:09 -08005514# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5515 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5516 ERROR("DEFINE_ARCH_HAS",
5517 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5518 }
5519
Joe Perchesacd93622015-02-13 14:38:38 -08005520# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5521 if ($^V && $^V ge 5.10.0 &&
5522 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5523 WARN("LIKELY_MISUSE",
5524 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5525 }
5526
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005527# whine mightly about in_atomic
5528 if ($line =~ /\bin_atomic\s*\(/) {
5529 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005530 ERROR("IN_ATOMIC",
5531 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005532 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005533 WARN("IN_ATOMIC",
5534 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005535 }
5536 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005537
5538# check for lockdep_set_novalidate_class
5539 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5540 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5541 if ($realfile !~ m@^kernel/lockdep@ &&
5542 $realfile !~ m@^include/linux/lockdep@ &&
5543 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005544 ERROR("LOCKDEP",
5545 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005546 }
5547 }
Dave Jones88f88312011-01-12 16:59:59 -08005548
Joe Perchesb392c642015-04-16 12:44:16 -07005549 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5550 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005551 WARN("EXPORTED_WORLD_WRITABLE",
5552 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005553 }
Joe Perches24358802014-04-03 14:49:13 -07005554
Joe Perches515a2352014-04-03 14:49:24 -07005555# Mode permission misuses where it seems decimal should be octal
5556# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5557 if ($^V && $^V ge 5.10.0 &&
5558 $line =~ /$mode_perms_search/) {
5559 foreach my $entry (@mode_permission_funcs) {
5560 my $func = $entry->[0];
5561 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005562
Joe Perches515a2352014-04-03 14:49:24 -07005563 my $skip_args = "";
5564 if ($arg_pos > 1) {
5565 $arg_pos--;
5566 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5567 }
5568 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5569 if ($line =~ /$test/) {
5570 my $val = $1;
5571 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005572
Joe Perches515a2352014-04-03 14:49:24 -07005573 if ($val !~ /^0$/ &&
5574 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5575 length($val) ne 4)) {
5576 ERROR("NON_OCTAL_PERMISSIONS",
5577 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005578 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5579 ERROR("EXPORTED_WORLD_WRITABLE",
5580 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005581 }
Joe Perches24358802014-04-03 14:49:13 -07005582 }
5583 }
5584 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005585 }
5586
5587 # If we have no input at all, then there is nothing to report on
5588 # so just keep quiet.
5589 if ($#rawlines == -1) {
5590 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005591 }
5592
Andy Whitcroft8905a672007-11-28 16:21:06 -08005593 # In mailback mode only produce a report in the negative, for
5594 # things that appear to be patches.
5595 if ($mailback && ($clean == 1 || !$is_patch)) {
5596 exit(0);
5597 }
5598
5599 # This is not a patch, and we are are in 'no-patch' mode so
5600 # just keep quiet.
5601 if (!$chk_patch && !$is_patch) {
5602 exit(0);
5603 }
5604
5605 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005606 ERROR("NOT_UNIFIED_DIFF",
5607 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005608 }
5609 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005610 ERROR("MISSING_SIGN_OFF",
5611 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005612 }
5613
Andy Whitcroft8905a672007-11-28 16:21:06 -08005614 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005615 if ($summary && !($clean == 1 && $quiet == 1)) {
5616 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005617 print "total: $cnt_error errors, $cnt_warn warnings, " .
5618 (($check)? "$cnt_chk checks, " : "") .
5619 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005620 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005621
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005622 if ($quiet == 0) {
5623 # If there were whitespace errors which cleanpatch can fix
5624 # then suggest that.
5625 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07005626 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07005627 print << "EOM"
5628
5629NOTE: Whitespace errors detected.
5630 You may wish to use scripts/cleanpatch or scripts/cleanfile
5631EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005632 }
5633 }
5634
Joe Perches91bfe482013-09-11 14:23:59 -07005635 hash_show_words(\%use_type, "Used");
5636 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005637
Joe Perchesd752fcc2014-08-06 16:11:05 -07005638 if ($clean == 0 && $fix &&
5639 ("@rawlines" ne "@fixed" ||
5640 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005641 my $newfile = $filename;
5642 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005643 my $linecount = 0;
5644 my $f;
5645
Joe Perchesd752fcc2014-08-06 16:11:05 -07005646 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5647
Joe Perches3705ce52013-07-03 15:05:31 -07005648 open($f, '>', $newfile)
5649 or die "$P: Can't open $newfile for write\n";
5650 foreach my $fixed_line (@fixed) {
5651 $linecount++;
5652 if ($file) {
5653 if ($linecount > 3) {
5654 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005655 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005656 }
5657 } else {
5658 print $f $fixed_line . "\n";
5659 }
5660 }
5661 close($f);
5662
5663 if (!$quiet) {
5664 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07005665
Joe Perches3705ce52013-07-03 15:05:31 -07005666Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5667
5668Do _NOT_ trust the results written to this file.
5669Do _NOT_ submit these changes without inspecting them for correctness.
5670
5671This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5672No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07005673EOM
5674 }
5675 }
5676
Joe Perchesd8469f12015-06-25 15:03:00 -07005677 if ($quiet == 0) {
5678 print "\n";
5679 if ($clean == 1) {
5680 print "$vname has no obvious style problems and is ready for submission.\n";
5681 } else {
5682 print "$vname has style problems, please review.\n";
5683 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005684 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005685 return $clean;
5686}