blob: 34f1415ae964513115aefbc6c18be881f059e5e2 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070012
13my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080014my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070015
Joe Perches000d1cc12011-07-25 17:13:25 -070016my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $quiet = 0;
21my $tree = 1;
22my $chk_signoff = 1;
23my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070024my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070027my $file = 0;
28my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070029my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080030my $summary = 1;
31my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080032my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070033my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070034my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080035my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080037my %debug;
Joe Perches34456862013-07-03 15:05:34 -070038my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070039my %use_type = ();
40my @use = ();
41my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070042my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070043my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070044my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080045my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070046my $ignore_perl_version = 0;
47my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070048my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070049my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070050my $codespell = 0;
51my $codespellfile = "/usr/local/share/codespell/dictionary.txt";
Hannes Eder77f5b102009-09-21 17:04:37 -070052
53sub help {
54 my ($exitcode) = @_;
55
56 print << "EOM";
57Usage: $P [OPTION]... [FILE]...
58Version: $V
59
60Options:
61 -q, --quiet quiet
62 --no-tree run without a kernel tree
63 --no-signoff do not check for 'Signed-off-by' line
64 --patch treat FILE as patchfile (default)
65 --emacs emacs compile window format
66 --terse one line per report
67 -f, --file treat FILE as regular source file
68 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070069 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070070 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080071 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070072 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070073 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070074 --root=PATH PATH to the kernel tree root
75 --no-summary suppress the per-file summary
76 --mailback only produce a report in case of warnings/errors
77 --summary-file include the filename in summary
78 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
79 'values', 'possible', 'type', and 'attr' (default
80 is all off)
81 --test-only=WORD report only warnings/errors containing WORD
82 literally
Joe Perches3705ce52013-07-03 15:05:31 -070083 --fix EXPERIMENTAL - may create horrible results
84 If correctable single-line errors exist, create
85 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
86 with potential errors corrected to the preferred
87 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080088 --fix-inplace EXPERIMENTAL - may create horrible results
89 Is the same as --fix, but overwrites the input
90 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070091 --ignore-perl-version override checking of perl version. expect
92 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -070093 --codespell Use the codespell dictionary for spelling/typos
94 (default:/usr/local/share/codespell/dictionary.txt)
95 --codespellfile Use this codespell dictionary
Hannes Eder77f5b102009-09-21 17:04:37 -070096 -h, --help, --version display this help and exit
97
98When FILE is - read standard input.
99EOM
100
101 exit($exitcode);
102}
103
Joe Perches000d1cc12011-07-25 17:13:25 -0700104my $conf = which_conf($configuration_file);
105if (-f $conf) {
106 my @conf_args;
107 open(my $conffile, '<', "$conf")
108 or warn "$P: Can't find a readable $configuration_file file $!\n";
109
110 while (<$conffile>) {
111 my $line = $_;
112
113 $line =~ s/\s*\n?$//g;
114 $line =~ s/^\s*//g;
115 $line =~ s/\s+/ /g;
116
117 next if ($line =~ m/^\s*#/);
118 next if ($line =~ m/^\s*$/);
119
120 my @words = split(" ", $line);
121 foreach my $word (@words) {
122 last if ($word =~ m/^#/);
123 push (@conf_args, $word);
124 }
125 }
126 close($conffile);
127 unshift(@ARGV, @conf_args) if @conf_args;
128}
129
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700130GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700131 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700132 'tree!' => \$tree,
133 'signoff!' => \$chk_signoff,
134 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700135 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800136 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700137 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700138 'subjective!' => \$check,
139 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700140 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700141 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700142 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800143 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700144 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700145 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800146 'summary!' => \$summary,
147 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800148 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700149 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800150 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700151 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800152 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700153 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700154 'codespell!' => \$codespell,
155 'codespellfile=s' => \$codespellfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700156 'h|help' => \$help,
157 'version' => \$help
158) or help(1);
159
160help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700161
Joe Perches9624b8d2014-01-23 15:54:44 -0800162$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700163$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800164
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700165my $exit = 0;
166
Dave Hansend62a2012013-09-11 14:23:56 -0700167if ($^V && $^V lt $minimum_perl_version) {
168 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
169 if (!$ignore_perl_version) {
170 exit(1);
171 }
172}
173
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700174if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700175 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700176 exit(1);
177}
178
Joe Perches91bfe482013-09-11 14:23:59 -0700179sub hash_save_array_words {
180 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700181
Joe Perches91bfe482013-09-11 14:23:59 -0700182 my @array = split(/,/, join(',', @$arrayRef));
183 foreach my $word (@array) {
184 $word =~ s/\s*\n?$//g;
185 $word =~ s/^\s*//g;
186 $word =~ s/\s+/ /g;
187 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700188
Joe Perches91bfe482013-09-11 14:23:59 -0700189 next if ($word =~ m/^\s*#/);
190 next if ($word =~ m/^\s*$/);
191
192 $hashRef->{$word}++;
193 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700194}
195
Joe Perches91bfe482013-09-11 14:23:59 -0700196sub hash_show_words {
197 my ($hashRef, $prefix) = @_;
198
Joe Perches58cb3cf2013-09-11 14:24:04 -0700199 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700200 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700201 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700202 print " $word";
203 }
204 print "\n\n";
205 }
206}
207
208hash_save_array_words(\%ignore_type, \@ignore);
209hash_save_array_words(\%use_type, \@use);
210
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800211my $dbg_values = 0;
212my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700213my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700214my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800215for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800216 ## no critic
217 eval "\${dbg_$key} = '$debug{$key}';";
218 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800219}
220
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700221my $rpt_cleaners = 0;
222
Andy Whitcroft8905a672007-11-28 16:21:06 -0800223if ($terse) {
224 $emacs = 1;
225 $quiet++;
226}
227
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228if ($tree) {
229 if (defined $root) {
230 if (!top_of_kernel_tree($root)) {
231 die "$P: $root: --root does not point at a valid tree\n";
232 }
233 } else {
234 if (top_of_kernel_tree('.')) {
235 $root = '.';
236 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
237 top_of_kernel_tree($1)) {
238 $root = $1;
239 }
240 }
241
242 if (!defined $root) {
243 print "Must be run from the top-level dir. of a kernel tree\n";
244 exit(2);
245 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700246}
247
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700248my $emitted_corrupt = 0;
249
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700250our $Ident = qr{
251 [A-Za-z_][A-Za-z\d_]*
252 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
253 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700254our $Storage = qr{extern|static|asmlinkage};
255our $Sparse = qr{
256 __user|
257 __kernel|
258 __force|
259 __iomem|
260 __must_check|
261 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800262 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700263 __ref|
264 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700265 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800266our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
267our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
268our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
269our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
270our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700271
Wolfram Sang52131292010-03-05 13:43:51 -0800272# Notes to $Attribute:
273# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700274our $Attribute = qr{
275 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700276 __percpu|
277 __nocast|
278 __safe|
279 __bitwise__|
280 __packed__|
281 __packed2__|
282 __naked|
283 __maybe_unused|
284 __always_unused|
285 __noreturn|
286 __used|
287 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800288 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700289 __noclone|
290 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700291 __read_mostly|
292 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700293 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700294 ____cacheline_aligned|
295 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800296 ____cacheline_internodealigned_in_smp|
297 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700298 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700299our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700300our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700301our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
302our $Lval = qr{$Ident(?:$Member)*};
303
Joe Perches95e2c602013-07-03 15:05:20 -0700304our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
305our $Binary = qr{(?i)0b[01]+$Int_type?};
306our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
307our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700308our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800309our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800310our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
311our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
312our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800313our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700314our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800315our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700316our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700317our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700318our $Operators = qr{
319 <=|>=|==|!=|
320 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700321 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700322 }x;
323
Joe Perches91cb5192014-04-03 14:49:32 -0700324our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
325
Joe Perchesab7e23f2015-04-16 12:44:22 -0700326our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800327our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700328our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700329our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700331our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800332our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700333our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800334
Joe Perches15662b32011-10-31 17:13:12 -0700335our $NON_ASCII_UTF8 = qr{
336 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700337 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
338 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
339 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
340 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
341 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
342 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
343}x;
344
Joe Perches15662b32011-10-31 17:13:12 -0700345our $UTF8 = qr{
346 [\x09\x0A\x0D\x20-\x7E] # ASCII
347 | $NON_ASCII_UTF8
348}x;
349
Joe Perchese6176fa2015-06-25 15:02:49 -0700350our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800351our $typeOtherOSTypedefs = qr{(?x:
352 u_(?:char|short|int|long) | # bsd
353 u(?:nchar|short|int|long) # sysv
354)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700355our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700356 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700357 atomic_t
358)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700359our $typeTypedefs = qr{(?x:
360 $typeC99Typedefs\b|
361 $typeOtherOSTypedefs\b|
362 $typeKernelTypedefs\b
363)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700364
Joe Perches691e6692010-03-05 13:43:51 -0800365our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700366 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700367 (?:[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 -0700368 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700369 panic|
Joe Perches06668722013-11-12 15:10:07 -0800370 MODULE_[A-Z_]+|
371 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800372)};
373
Joe Perches20112472011-07-25 17:13:23 -0700374our $signature_tags = qr{(?xi:
375 Signed-off-by:|
376 Acked-by:|
377 Tested-by:|
378 Reviewed-by:|
379 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700380 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700381 To:|
382 Cc:
383)};
384
Joe Perches18130872014-08-06 16:11:22 -0700385our @typeListMisordered = (
386 qr{char\s+(?:un)?signed},
387 qr{int\s+(?:(?:un)?signed\s+)?short\s},
388 qr{int\s+short(?:\s+(?:un)?signed)},
389 qr{short\s+int(?:\s+(?:un)?signed)},
390 qr{(?:un)?signed\s+int\s+short},
391 qr{short\s+(?:un)?signed},
392 qr{long\s+int\s+(?:un)?signed},
393 qr{int\s+long\s+(?:un)?signed},
394 qr{long\s+(?:un)?signed\s+int},
395 qr{int\s+(?:un)?signed\s+long},
396 qr{int\s+(?:un)?signed},
397 qr{int\s+long\s+long\s+(?:un)?signed},
398 qr{long\s+long\s+int\s+(?:un)?signed},
399 qr{long\s+long\s+(?:un)?signed\s+int},
400 qr{long\s+long\s+(?:un)?signed},
401 qr{long\s+(?:un)?signed},
402);
403
Andy Whitcroft8905a672007-11-28 16:21:06 -0800404our @typeList = (
405 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700406 qr{(?:(?:un)?signed\s+)?char},
407 qr{(?:(?:un)?signed\s+)?short\s+int},
408 qr{(?:(?:un)?signed\s+)?short},
409 qr{(?:(?:un)?signed\s+)?int},
410 qr{(?:(?:un)?signed\s+)?long\s+int},
411 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
412 qr{(?:(?:un)?signed\s+)?long\s+long},
413 qr{(?:(?:un)?signed\s+)?long},
414 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800415 qr{float},
416 qr{double},
417 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800418 qr{struct\s+$Ident},
419 qr{union\s+$Ident},
420 qr{enum\s+$Ident},
421 qr{${Ident}_t},
422 qr{${Ident}_handler},
423 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700424 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800425);
Alex Dowad485ff232015-06-25 15:02:52 -0700426our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700427our @typeListWithAttr = (
428 @typeList,
429 qr{struct\s+$InitAttribute\s+$Ident},
430 qr{union\s+$InitAttribute\s+$Ident},
431);
432
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700433our @modifierList = (
434 qr{fastcall},
435);
Alex Dowad485ff232015-06-25 15:02:52 -0700436our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800437
Joe Perches24358802014-04-03 14:49:13 -0700438our @mode_permission_funcs = (
439 ["module_param", 3],
440 ["module_param_(?:array|named|string)", 4],
441 ["module_param_array_named", 5],
442 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
443 ["proc_create(?:_data|)", 2],
444 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
445);
446
Joe Perches515a2352014-04-03 14:49:24 -0700447#Create a search pattern for all these functions to speed up a loop below
448our $mode_perms_search = "";
449foreach my $entry (@mode_permission_funcs) {
450 $mode_perms_search .= '|' if ($mode_perms_search ne "");
451 $mode_perms_search .= $entry->[0];
452}
453
Joe Perchesb392c642015-04-16 12:44:16 -0700454our $mode_perms_world_writable = qr{
455 S_IWUGO |
456 S_IWOTH |
457 S_IRWXUGO |
458 S_IALLUGO |
459 0[0-7][0-7][2367]
460}x;
461
Wolfram Sang7840a942010-08-09 17:20:57 -0700462our $allowed_asm_includes = qr{(?x:
463 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700464 memory|
465 time|
466 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700467)};
468# memory.h: ARM has a custom one
469
Kees Cook66b47b42014-10-13 15:51:57 -0700470# Load common spelling mistakes and build regular expression list.
471my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700472my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700473
Joe Perches36061e32014-12-10 15:51:43 -0800474if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800475 while (<$spelling>) {
476 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700477
Joe Perches36061e32014-12-10 15:51:43 -0800478 $line =~ s/\s*\n?$//g;
479 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700480
Joe Perches36061e32014-12-10 15:51:43 -0800481 next if ($line =~ m/^\s*#/);
482 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700483
Joe Perches36061e32014-12-10 15:51:43 -0800484 my ($suspect, $fix) = split(/\|\|/, $line);
485
Joe Perches36061e32014-12-10 15:51:43 -0800486 $spelling_fix{$suspect} = $fix;
487 }
488 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800489} else {
490 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700491}
Kees Cook66b47b42014-10-13 15:51:57 -0700492
Joe Perchesebfd7d62015-04-16 12:44:14 -0700493if ($codespell) {
494 if (open(my $spelling, '<', $codespellfile)) {
495 while (<$spelling>) {
496 my $line = $_;
497
498 $line =~ s/\s*\n?$//g;
499 $line =~ s/^\s*//g;
500
501 next if ($line =~ m/^\s*#/);
502 next if ($line =~ m/^\s*$/);
503 next if ($line =~ m/, disabled/i);
504
505 $line =~ s/,.*$//;
506
507 my ($suspect, $fix) = split(/->/, $line);
508
509 $spelling_fix{$suspect} = $fix;
510 }
511 close($spelling);
512 } else {
513 warn "No codespell typos will be found - file '$codespellfile': $!\n";
514 }
515}
516
517$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
518
Andy Whitcroft8905a672007-11-28 16:21:06 -0800519sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700520 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
521 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700522 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700523 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700524 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700525 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700526 (?:$typeTypedefs\b)|
527 (?:${all}\b)
528 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800529 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700530 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800531 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800532 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700533 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700534 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800535 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700536 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800537 }x;
Joe Perches18130872014-08-06 16:11:22 -0700538 $NonptrTypeMisordered = qr{
539 (?:$Modifier\s+|const\s+)*
540 (?:
541 (?:${Misordered}\b)
542 )
543 (?:\s+$Modifier|\s+const)*
544 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700545 $NonptrTypeWithAttr = qr{
546 (?:$Modifier\s+|const\s+)*
547 (?:
548 (?:typeof|__typeof__)\s*\([^\)]*\)|
549 (?:$typeTypedefs\b)|
550 (?:${allWithAttr}\b)
551 )
552 (?:\s+$Modifier|\s+const)*
553 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800554 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700555 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700556 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700557 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800558 }x;
Joe Perches18130872014-08-06 16:11:22 -0700559 $TypeMisordered = qr{
560 $NonptrTypeMisordered
561 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
562 (?:\s+$Inline|\s+$Modifier)*
563 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700564 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700565 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800566}
567build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700568
Joe Perches7d2367a2011-07-25 17:13:22 -0700569our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700570
571# Using $balanced_parens, $LvalOrFunc, or $FuncArg
572# requires at least perl version v5.10.0
573# Any use must be runtime checked with $^V
574
575our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700576our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800577our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700578
Joe Perchesf8422302014-08-06 16:11:31 -0700579our $declaration_macros = qr{(?x:
580 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
581 (?:$Storage\s+)?LIST_HEAD\s*\(|
582 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
583)};
584
Joe Perches7d2367a2011-07-25 17:13:22 -0700585sub deparenthesize {
586 my ($string) = @_;
587 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700588
589 while ($string =~ /^\s*\(.*\)\s*$/) {
590 $string =~ s@^\s*\(\s*@@;
591 $string =~ s@\s*\)\s*$@@;
592 }
593
Joe Perches7d2367a2011-07-25 17:13:22 -0700594 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700595
Joe Perches7d2367a2011-07-25 17:13:22 -0700596 return $string;
597}
598
Joe Perches34456862013-07-03 15:05:34 -0700599sub seed_camelcase_file {
600 my ($file) = @_;
601
602 return if (!(-f $file));
603
604 local $/;
605
606 open(my $include_file, '<', "$file")
607 or warn "$P: Can't read '$file' $!\n";
608 my $text = <$include_file>;
609 close($include_file);
610
611 my @lines = split('\n', $text);
612
613 foreach my $line (@lines) {
614 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
615 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
616 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800617 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
618 $camelcase{$1} = 1;
619 } 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 -0700620 $camelcase{$1} = 1;
621 }
622 }
623}
624
625my $camelcase_seeded = 0;
626sub seed_camelcase_includes {
627 return if ($camelcase_seeded);
628
629 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700630 my $camelcase_cache = "";
631 my @include_files = ();
632
633 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700634
Richard Genoud3645e322014-02-10 14:25:32 -0800635 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700636 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
637 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700638 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700639 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700640 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700641 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700642 @include_files = split('\n', $files);
643 foreach my $file (@include_files) {
644 my $date = POSIX::strftime("%Y%m%d%H%M",
645 localtime((stat $file)[9]));
646 $last_mod_date = $date if ($last_mod_date < $date);
647 }
648 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700649 }
Joe Perchesc707a812013-07-08 16:00:43 -0700650
651 if ($camelcase_cache ne "" && -f $camelcase_cache) {
652 open(my $camelcase_file, '<', "$camelcase_cache")
653 or warn "$P: Can't read '$camelcase_cache' $!\n";
654 while (<$camelcase_file>) {
655 chomp;
656 $camelcase{$_} = 1;
657 }
658 close($camelcase_file);
659
660 return;
661 }
662
Richard Genoud3645e322014-02-10 14:25:32 -0800663 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700664 $files = `git ls-files "include/*.h"`;
665 @include_files = split('\n', $files);
666 }
667
Joe Perches34456862013-07-03 15:05:34 -0700668 foreach my $file (@include_files) {
669 seed_camelcase_file($file);
670 }
Joe Perches351b2a12013-07-03 15:05:36 -0700671
Joe Perchesc707a812013-07-08 16:00:43 -0700672 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700673 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700674 open(my $camelcase_file, '>', "$camelcase_cache")
675 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700676 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
677 print $camelcase_file ("$_\n");
678 }
679 close($camelcase_file);
680 }
Joe Perches34456862013-07-03 15:05:34 -0700681}
682
Joe Perchesd311cd42014-08-06 16:10:57 -0700683sub git_commit_info {
684 my ($commit, $id, $desc) = @_;
685
686 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
687
688 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
689 $output =~ s/^\s*//gm;
690 my @lines = split("\n", $output);
691
Joe Perches0d7835f2015-02-13 14:38:35 -0800692 return ($id, $desc) if ($#lines < 0);
693
Joe Perchesd311cd42014-08-06 16:10:57 -0700694 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
695# Maybe one day convert this block of bash into something that returns
696# all matching commit ids, but it's very slow...
697#
698# echo "checking commits $1..."
699# git rev-list --remotes | grep -i "^$1" |
700# while read line ; do
701# git log --format='%H %s' -1 $line |
702# echo "commit $(cut -c 1-12,41-)"
703# done
704 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
705 } else {
706 $id = substr($lines[0], 0, 12);
707 $desc = substr($lines[0], 41);
708 }
709
710 return ($id, $desc);
711}
712
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700713$chk_signoff = 0 if ($file);
714
Andy Whitcroft00df3442007-06-08 13:47:06 -0700715my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800716my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700717my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700718my @fixed_inserted = ();
719my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700720my $fixlinenr = -1;
721
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800722my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700723for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800724 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700725 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800726 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700727 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800728 } elsif ($filename eq '-') {
729 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700730 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800731 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700732 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700733 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800734 if ($filename eq '-') {
735 $vname = 'Your patch';
736 } else {
737 $vname = $filename;
738 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800739 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700740 chomp;
741 push(@rawlines, $_);
742 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800743 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800744 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700745 $exit = 1;
746 }
747 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800748 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700749 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700750 @fixed_inserted = ();
751 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700752 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700753 @modifierListFile = ();
754 @typeListFile = ();
755 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700756}
757
758exit($exit);
759
760sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700761 my ($root) = @_;
762
763 my @tree_check = (
764 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
765 "README", "Documentation", "arch", "include", "drivers",
766 "fs", "init", "ipc", "kernel", "lib", "scripts",
767 );
768
769 foreach my $check (@tree_check) {
770 if (! -e $root . '/' . $check) {
771 return 0;
772 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700773 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700774 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700775}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700776
Joe Perches20112472011-07-25 17:13:23 -0700777sub parse_email {
778 my ($formatted_email) = @_;
779
780 my $name = "";
781 my $address = "";
782 my $comment = "";
783
784 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
785 $name = $1;
786 $address = $2;
787 $comment = $3 if defined $3;
788 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
789 $address = $1;
790 $comment = $2 if defined $2;
791 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
792 $address = $1;
793 $comment = $2 if defined $2;
794 $formatted_email =~ s/$address.*$//;
795 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700796 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700797 $name =~ s/^\"|\"$//g;
798 # If there's a name left after stripping spaces and
799 # leading quotes, and the address doesn't have both
800 # leading and trailing angle brackets, the address
801 # is invalid. ie:
802 # "joe smith joe@smith.com" bad
803 # "joe smith <joe@smith.com" bad
804 if ($name ne "" && $address !~ /^<[^>]+>$/) {
805 $name = "";
806 $address = "";
807 $comment = "";
808 }
809 }
810
Joe Perches3705ce52013-07-03 15:05:31 -0700811 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700812 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700813 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700814 $address =~ s/^\<|\>$//g;
815
816 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
817 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
818 $name = "\"$name\"";
819 }
820
821 return ($name, $address, $comment);
822}
823
824sub format_email {
825 my ($name, $address) = @_;
826
827 my $formatted_email;
828
Joe Perches3705ce52013-07-03 15:05:31 -0700829 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700830 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700831 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700832
833 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
834 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
835 $name = "\"$name\"";
836 }
837
838 if ("$name" eq "") {
839 $formatted_email = "$address";
840 } else {
841 $formatted_email = "$name <$address>";
842 }
843
844 return $formatted_email;
845}
846
Joe Perchesd311cd42014-08-06 16:10:57 -0700847sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700848 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700849
Joe Perchesbd474ca2014-08-06 16:11:10 -0700850 foreach my $path (split(/:/, $ENV{PATH})) {
851 if (-e "$path/$bin") {
852 return "$path/$bin";
853 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700854 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700855
Joe Perchesbd474ca2014-08-06 16:11:10 -0700856 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700857}
858
Joe Perches000d1cc12011-07-25 17:13:25 -0700859sub which_conf {
860 my ($conf) = @_;
861
862 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
863 if (-e "$path/$conf") {
864 return "$path/$conf";
865 }
866 }
867
868 return "";
869}
870
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700871sub expand_tabs {
872 my ($str) = @_;
873
874 my $res = '';
875 my $n = 0;
876 for my $c (split(//, $str)) {
877 if ($c eq "\t") {
878 $res .= ' ';
879 $n++;
880 for (; ($n % 8) != 0; $n++) {
881 $res .= ' ';
882 }
883 next;
884 }
885 $res .= $c;
886 $n++;
887 }
888
889 return $res;
890}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700891sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700892 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700893 return $res;
894}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700895
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700896sub line_stats {
897 my ($line) = @_;
898
899 # Drop the diff line leader and expand tabs
900 $line =~ s/^.//;
901 $line = expand_tabs($line);
902
903 # Pick the indent from the front of the line.
904 my ($white) = ($line =~ /^(\s*)/);
905
906 return (length($line), length($white));
907}
908
Andy Whitcroft773647a2008-03-28 14:15:58 -0700909my $sanitise_quote = '';
910
911sub sanitise_line_reset {
912 my ($in_comment) = @_;
913
914 if ($in_comment) {
915 $sanitise_quote = '*/';
916 } else {
917 $sanitise_quote = '';
918 }
919}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700920sub sanitise_line {
921 my ($line) = @_;
922
923 my $res = '';
924 my $l = '';
925
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800926 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700927 my $off = 0;
928 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700929
Andy Whitcroft773647a2008-03-28 14:15:58 -0700930 # Always copy over the diff marker.
931 $res = substr($line, 0, 1);
932
933 for ($off = 1; $off < length($line); $off++) {
934 $c = substr($line, $off, 1);
935
936 # Comments we are wacking completly including the begin
937 # and end, all to $;.
938 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
939 $sanitise_quote = '*/';
940
941 substr($res, $off, 2, "$;$;");
942 $off++;
943 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800944 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700945 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700946 $sanitise_quote = '';
947 substr($res, $off, 2, "$;$;");
948 $off++;
949 next;
950 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700951 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
952 $sanitise_quote = '//';
953
954 substr($res, $off, 2, $sanitise_quote);
955 $off++;
956 next;
957 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700958
959 # A \ in a string means ignore the next character.
960 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
961 $c eq "\\") {
962 substr($res, $off, 2, 'XX');
963 $off++;
964 next;
965 }
966 # Regular quotes.
967 if ($c eq "'" || $c eq '"') {
968 if ($sanitise_quote eq '') {
969 $sanitise_quote = $c;
970
971 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700972 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700973 } elsif ($sanitise_quote eq $c) {
974 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700975 }
976 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700977
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800978 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700979 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
980 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700981 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
982 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700983 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
984 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700985 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700986 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700987 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800988 }
989
Daniel Walker113f04a2009-09-21 17:04:35 -0700990 if ($sanitise_quote eq '//') {
991 $sanitise_quote = '';
992 }
993
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800994 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700995 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800996 my $clean = 'X' x length($1);
997 $res =~ s@\<.*\>@<$clean>@;
998
999 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001000 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001001 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001002 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001003 }
1004
Andy Whitcroft00df3442007-06-08 13:47:06 -07001005 return $res;
1006}
1007
Joe Perchesa6962d72013-04-29 16:18:13 -07001008sub get_quoted_string {
1009 my ($line, $rawline) = @_;
1010
Joe Perches33acb542015-06-25 15:02:54 -07001011 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001012 return substr($rawline, $-[0], $+[0] - $-[0]);
1013}
1014
Andy Whitcroft8905a672007-11-28 16:21:06 -08001015sub ctx_statement_block {
1016 my ($linenr, $remain, $off) = @_;
1017 my $line = $linenr - 1;
1018 my $blk = '';
1019 my $soff = $off;
1020 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001021 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001022
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001023 my $loff = 0;
1024
Andy Whitcroft8905a672007-11-28 16:21:06 -08001025 my $type = '';
1026 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001027 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001028 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001029 my $c;
1030 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001031
1032 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001033 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001034 @stack = (['', 0]) if ($#stack == -1);
1035
Andy Whitcroft773647a2008-03-28 14:15:58 -07001036 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001037 # If we are about to drop off the end, pull in more
1038 # context.
1039 if ($off >= $len) {
1040 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001041 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001042 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001043 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001044 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001045 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001046 $len = length($blk);
1047 $line++;
1048 last;
1049 }
1050 # Bail if there is no further context.
1051 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001052 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001053 last;
1054 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001055 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1056 $level++;
1057 $type = '#';
1058 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001059 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001060 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001061 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001062 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001063
Andy Whitcroft773647a2008-03-28 14:15:58 -07001064 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001065
1066 # Handle nested #if/#else.
1067 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1068 push(@stack, [ $type, $level ]);
1069 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1070 ($type, $level) = @{$stack[$#stack - 1]};
1071 } elsif ($remainder =~ /^#\s*endif\b/) {
1072 ($type, $level) = @{pop(@stack)};
1073 }
1074
Andy Whitcroft8905a672007-11-28 16:21:06 -08001075 # Statement ends at the ';' or a close '}' at the
1076 # outermost level.
1077 if ($level == 0 && $c eq ';') {
1078 last;
1079 }
1080
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001081 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001082 if ($level == 0 && $coff_set == 0 &&
1083 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1084 $remainder =~ /^(else)(?:\s|{)/ &&
1085 $remainder !~ /^else\s+if\b/) {
1086 $coff = $off + length($1) - 1;
1087 $coff_set = 1;
1088 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1089 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001090 }
1091
Andy Whitcroft8905a672007-11-28 16:21:06 -08001092 if (($type eq '' || $type eq '(') && $c eq '(') {
1093 $level++;
1094 $type = '(';
1095 }
1096 if ($type eq '(' && $c eq ')') {
1097 $level--;
1098 $type = ($level != 0)? '(' : '';
1099
1100 if ($level == 0 && $coff < $soff) {
1101 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001102 $coff_set = 1;
1103 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001104 }
1105 }
1106 if (($type eq '' || $type eq '{') && $c eq '{') {
1107 $level++;
1108 $type = '{';
1109 }
1110 if ($type eq '{' && $c eq '}') {
1111 $level--;
1112 $type = ($level != 0)? '{' : '';
1113
1114 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001115 if (substr($blk, $off + 1, 1) eq ';') {
1116 $off++;
1117 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001118 last;
1119 }
1120 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001121 # Preprocessor commands end at the newline unless escaped.
1122 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1123 $level--;
1124 $type = '';
1125 $off++;
1126 last;
1127 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001128 $off++;
1129 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001130 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001131 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001132 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001133 $line++;
1134 $remain--;
1135 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001136
1137 my $statement = substr($blk, $soff, $off - $soff + 1);
1138 my $condition = substr($blk, $soff, $coff - $soff + 1);
1139
1140 #warn "STATEMENT<$statement>\n";
1141 #warn "CONDITION<$condition>\n";
1142
Andy Whitcroft773647a2008-03-28 14:15:58 -07001143 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001144
1145 return ($statement, $condition,
1146 $line, $remain + 1, $off - $loff + 1, $level);
1147}
1148
Andy Whitcroftcf655042008-03-04 14:28:20 -08001149sub statement_lines {
1150 my ($stmt) = @_;
1151
1152 # Strip the diff line prefixes and rip blank lines at start and end.
1153 $stmt =~ s/(^|\n)./$1/g;
1154 $stmt =~ s/^\s*//;
1155 $stmt =~ s/\s*$//;
1156
1157 my @stmt_lines = ($stmt =~ /\n/g);
1158
1159 return $#stmt_lines + 2;
1160}
1161
1162sub statement_rawlines {
1163 my ($stmt) = @_;
1164
1165 my @stmt_lines = ($stmt =~ /\n/g);
1166
1167 return $#stmt_lines + 2;
1168}
1169
1170sub statement_block_size {
1171 my ($stmt) = @_;
1172
1173 $stmt =~ s/(^|\n)./$1/g;
1174 $stmt =~ s/^\s*{//;
1175 $stmt =~ s/}\s*$//;
1176 $stmt =~ s/^\s*//;
1177 $stmt =~ s/\s*$//;
1178
1179 my @stmt_lines = ($stmt =~ /\n/g);
1180 my @stmt_statements = ($stmt =~ /;/g);
1181
1182 my $stmt_lines = $#stmt_lines + 2;
1183 my $stmt_statements = $#stmt_statements + 1;
1184
1185 if ($stmt_lines > $stmt_statements) {
1186 return $stmt_lines;
1187 } else {
1188 return $stmt_statements;
1189 }
1190}
1191
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001192sub ctx_statement_full {
1193 my ($linenr, $remain, $off) = @_;
1194 my ($statement, $condition, $level);
1195
1196 my (@chunks);
1197
Andy Whitcroftcf655042008-03-04 14:28:20 -08001198 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001199 ($statement, $condition, $linenr, $remain, $off, $level) =
1200 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001201 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001202 push(@chunks, [ $condition, $statement ]);
1203 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1204 return ($level, $linenr, @chunks);
1205 }
1206
1207 # Pull in the following conditional/block pairs and see if they
1208 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001209 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001210 ($statement, $condition, $linenr, $remain, $off, $level) =
1211 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001212 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001213 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001214 #print "C: push\n";
1215 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001216 }
1217
1218 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001219}
1220
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001221sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001222 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001223 my $line;
1224 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001225 my $blk = '';
1226 my @o;
1227 my @c;
1228 my @res = ();
1229
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001230 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001231 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001232 for ($line = $start; $remain > 0; $line++) {
1233 next if ($rawlines[$line] =~ /^-/);
1234 $remain--;
1235
1236 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001237
1238 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001239 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001240 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001241 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001242 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001243 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001244 $level = pop(@stack);
1245 }
1246
Andy Whitcroft01464f32010-10-26 14:23:19 -07001247 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001248 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1249 if ($off > 0) {
1250 $off--;
1251 next;
1252 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001253
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001254 if ($c eq $close && $level > 0) {
1255 $level--;
1256 last if ($level == 0);
1257 } elsif ($c eq $open) {
1258 $level++;
1259 }
1260 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001261
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001262 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001263 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001264 }
1265
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001266 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001267 }
1268
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001269 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001270}
1271sub ctx_block_outer {
1272 my ($linenr, $remain) = @_;
1273
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001274 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1275 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001276}
1277sub ctx_block {
1278 my ($linenr, $remain) = @_;
1279
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001280 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1281 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001282}
1283sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001284 my ($linenr, $remain, $off) = @_;
1285
1286 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1287 return @r;
1288}
1289sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001290 my ($linenr, $remain) = @_;
1291
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001292 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001293}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001294sub ctx_statement_level {
1295 my ($linenr, $remain, $off) = @_;
1296
1297 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1298}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001299
1300sub ctx_locate_comment {
1301 my ($first_line, $end_line) = @_;
1302
1303 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001304 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001305 return $current_comment if (defined $current_comment);
1306
1307 # Look through the context and try and figure out if there is a
1308 # comment.
1309 my $in_comment = 0;
1310 $current_comment = '';
1311 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001312 my $line = $rawlines[$linenr - 1];
1313 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001314 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1315 $in_comment = 1;
1316 }
1317 if ($line =~ m@/\*@) {
1318 $in_comment = 1;
1319 }
1320 if (!$in_comment && $current_comment ne '') {
1321 $current_comment = '';
1322 }
1323 $current_comment .= $line . "\n" if ($in_comment);
1324 if ($line =~ m@\*/@) {
1325 $in_comment = 0;
1326 }
1327 }
1328
1329 chomp($current_comment);
1330 return($current_comment);
1331}
1332sub ctx_has_comment {
1333 my ($first_line, $end_line) = @_;
1334 my $cmt = ctx_locate_comment($first_line, $end_line);
1335
Andy Whitcroft00df3442007-06-08 13:47:06 -07001336 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001337 ##print "CMMT: $cmt\n";
1338
1339 return ($cmt ne '');
1340}
1341
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001342sub raw_line {
1343 my ($linenr, $cnt) = @_;
1344
1345 my $offset = $linenr - 1;
1346 $cnt++;
1347
1348 my $line;
1349 while ($cnt) {
1350 $line = $rawlines[$offset++];
1351 next if (defined($line) && $line =~ /^-/);
1352 $cnt--;
1353 }
1354
1355 return $line;
1356}
1357
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001358sub cat_vet {
1359 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001360 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001361
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001362 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001363 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1364 $res .= $1;
1365 if ($2 ne '') {
1366 $coded = sprintf("^%c", unpack('C', $2) + 64);
1367 $res .= $coded;
1368 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001369 }
1370 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001371
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001372 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001373}
1374
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001375my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001376my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001377my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001378my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001379
1380sub annotate_reset {
1381 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001382 $av_pending = '_';
1383 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001384 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001385}
1386
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001387sub annotate_values {
1388 my ($stream, $type) = @_;
1389
1390 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001391 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001392 my $cur = $stream;
1393
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001394 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001395
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001396 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001397 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001398 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001399 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001400 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001401 print "WS($1)\n" if ($dbg_values > 1);
1402 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001403 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001404 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001405 }
1406
Florian Micklerc023e4732011-01-12 16:59:58 -08001407 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001408 print "CAST($1)\n" if ($dbg_values > 1);
1409 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001410 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001411
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001412 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001413 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001414 $type = 'T';
1415
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001416 } elsif ($cur =~ /^($Modifier)\s*/) {
1417 print "MODIFIER($1)\n" if ($dbg_values > 1);
1418 $type = 'T';
1419
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001420 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001421 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001422 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001423 push(@av_paren_type, $type);
1424 if ($2 ne '') {
1425 $av_pending = 'N';
1426 }
1427 $type = 'E';
1428
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001429 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001430 print "UNDEF($1)\n" if ($dbg_values > 1);
1431 $av_preprocessor = 1;
1432 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001433
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001434 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001435 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001436 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001437
1438 push(@av_paren_type, $type);
1439 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001440 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001441
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001442 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001443 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1444 $av_preprocessor = 1;
1445
1446 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1447
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001448 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001449
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001450 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001451 print "PRE_END($1)\n" if ($dbg_values > 1);
1452
1453 $av_preprocessor = 1;
1454
1455 # Assume all arms of the conditional end as this
1456 # one does, and continue as if the #endif was not here.
1457 pop(@av_paren_type);
1458 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001459 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001460
1461 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001462 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001463
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001464 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1465 print "ATTR($1)\n" if ($dbg_values > 1);
1466 $av_pending = $type;
1467 $type = 'N';
1468
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001469 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001470 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001471 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001472 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001473 }
1474 $type = 'N';
1475
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001476 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001477 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001478 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001479 $type = 'N';
1480
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001481 } elsif ($cur =~/^(case)/o) {
1482 print "CASE($1)\n" if ($dbg_values > 1);
1483 $av_pend_colon = 'C';
1484 $type = 'N';
1485
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001486 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001487 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001488 $type = 'N';
1489
1490 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001491 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001492 push(@av_paren_type, $av_pending);
1493 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001494 $type = 'N';
1495
1496 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001497 my $new_type = pop(@av_paren_type);
1498 if ($new_type ne '_') {
1499 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001500 print "PAREN('$1') -> $type\n"
1501 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001502 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001503 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001504 }
1505
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001506 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001507 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001508 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001509 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001510
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001511 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1512 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001513 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001514 } elsif ($type eq 'E') {
1515 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001516 }
1517 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1518 $type = 'V';
1519
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001520 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001521 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001522 $type = 'V';
1523
1524 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001525 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001526 $type = 'N';
1527
Andy Whitcroftcf655042008-03-04 14:28:20 -08001528 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001529 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001530 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001531 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001532
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001533 } elsif ($cur =~/^(,)/) {
1534 print "COMMA($1)\n" if ($dbg_values > 1);
1535 $type = 'C';
1536
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001537 } elsif ($cur =~ /^(\?)/o) {
1538 print "QUESTION($1)\n" if ($dbg_values > 1);
1539 $type = 'N';
1540
1541 } elsif ($cur =~ /^(:)/o) {
1542 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1543
1544 substr($var, length($res), 1, $av_pend_colon);
1545 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1546 $type = 'E';
1547 } else {
1548 $type = 'N';
1549 }
1550 $av_pend_colon = 'O';
1551
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001552 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001553 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001554 $type = 'N';
1555
Andy Whitcroft0d413862008-10-15 22:02:16 -07001556 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001557 my $variant;
1558
1559 print "OPV($1)\n" if ($dbg_values > 1);
1560 if ($type eq 'V') {
1561 $variant = 'B';
1562 } else {
1563 $variant = 'U';
1564 }
1565
1566 substr($var, length($res), 1, $variant);
1567 $type = 'N';
1568
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001569 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001570 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001571 if ($1 ne '++' && $1 ne '--') {
1572 $type = 'N';
1573 }
1574
1575 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001576 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001577 }
1578 if (defined $1) {
1579 $cur = substr($cur, length($1));
1580 $res .= $type x length($1);
1581 }
1582 }
1583
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001584 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001585}
1586
Andy Whitcroft8905a672007-11-28 16:21:06 -08001587sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001588 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001589 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001590 ^(?:
1591 $Modifier|
1592 $Storage|
1593 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001594 DEFINE_\S+
1595 )$|
1596 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001597 goto|
1598 return|
1599 case|
1600 else|
1601 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001602 do|
1603 \#|
1604 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001605 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001606 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001607 )}x;
1608 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1609 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001610 # Check for modifiers.
1611 $possible =~ s/\s*$Storage\s*//g;
1612 $possible =~ s/\s*$Sparse\s*//g;
1613 if ($possible =~ /^\s*$/) {
1614
1615 } elsif ($possible =~ /\s/) {
1616 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001617 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001618 if ($modifier !~ $notPermitted) {
1619 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001620 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001621 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001622 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001623
1624 } else {
1625 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001626 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001627 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001628 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001629 } else {
1630 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001631 }
1632}
1633
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001634my $prefix = '';
1635
Joe Perches000d1cc12011-07-25 17:13:25 -07001636sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001637 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001638
Joe Perchescbec18a2014-04-03 14:49:19 -07001639 return defined $use_type{$type} if (scalar keys %use_type > 0);
1640
1641 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001642}
1643
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001644sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001645 my ($level, $type, $msg) = @_;
1646
1647 if (!show_type($type) ||
1648 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001649 return 0;
1650 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001651 my $line;
1652 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001653 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001654 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001655 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001656 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001657 $line = (split('\n', $line))[0] . "\n" if ($terse);
1658
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001659 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001660
1661 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001662}
Joe Perchescbec18a2014-04-03 14:49:19 -07001663
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001664sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001665 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001666}
Joe Perches000d1cc12011-07-25 17:13:25 -07001667
Joe Perchesd752fcc2014-08-06 16:11:05 -07001668sub fixup_current_range {
1669 my ($lineRef, $offset, $length) = @_;
1670
1671 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1672 my $o = $1;
1673 my $l = $2;
1674 my $no = $o + $offset;
1675 my $nl = $l + $length;
1676 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1677 }
1678}
1679
1680sub fix_inserted_deleted_lines {
1681 my ($linesRef, $insertedRef, $deletedRef) = @_;
1682
1683 my $range_last_linenr = 0;
1684 my $delta_offset = 0;
1685
1686 my $old_linenr = 0;
1687 my $new_linenr = 0;
1688
1689 my $next_insert = 0;
1690 my $next_delete = 0;
1691
1692 my @lines = ();
1693
1694 my $inserted = @{$insertedRef}[$next_insert++];
1695 my $deleted = @{$deletedRef}[$next_delete++];
1696
1697 foreach my $old_line (@{$linesRef}) {
1698 my $save_line = 1;
1699 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001700 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001701 $delta_offset = 0;
1702 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1703 $range_last_linenr = $new_linenr;
1704 fixup_current_range(\$line, $delta_offset, 0);
1705 }
1706
1707 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1708 $deleted = @{$deletedRef}[$next_delete++];
1709 $save_line = 0;
1710 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1711 }
1712
1713 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1714 push(@lines, ${$inserted}{'LINE'});
1715 $inserted = @{$insertedRef}[$next_insert++];
1716 $new_linenr++;
1717 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1718 }
1719
1720 if ($save_line) {
1721 push(@lines, $line);
1722 $new_linenr++;
1723 }
1724
1725 $old_linenr++;
1726 }
1727
1728 return @lines;
1729}
1730
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001731sub fix_insert_line {
1732 my ($linenr, $line) = @_;
1733
1734 my $inserted = {
1735 LINENR => $linenr,
1736 LINE => $line,
1737 };
1738 push(@fixed_inserted, $inserted);
1739}
1740
1741sub fix_delete_line {
1742 my ($linenr, $line) = @_;
1743
1744 my $deleted = {
1745 LINENR => $linenr,
1746 LINE => $line,
1747 };
1748
1749 push(@fixed_deleted, $deleted);
1750}
1751
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001752sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001753 my ($type, $msg) = @_;
1754
1755 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001756 our $clean = 0;
1757 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001758 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001759 }
Joe Perches3705ce52013-07-03 15:05:31 -07001760 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001761}
1762sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001763 my ($type, $msg) = @_;
1764
1765 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001766 our $clean = 0;
1767 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001768 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001769 }
Joe Perches3705ce52013-07-03 15:05:31 -07001770 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001771}
1772sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001773 my ($type, $msg) = @_;
1774
1775 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001776 our $clean = 0;
1777 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001778 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001779 }
Joe Perches3705ce52013-07-03 15:05:31 -07001780 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001781}
1782
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001783sub check_absolute_file {
1784 my ($absolute, $herecurr) = @_;
1785 my $file = $absolute;
1786
1787 ##print "absolute<$absolute>\n";
1788
1789 # See if any suffix of this path is a path within the tree.
1790 while ($file =~ s@^[^/]*/@@) {
1791 if (-f "$root/$file") {
1792 ##print "file<$file>\n";
1793 last;
1794 }
1795 }
1796 if (! -f _) {
1797 return 0;
1798 }
1799
1800 # It is, so see if the prefix is acceptable.
1801 my $prefix = $absolute;
1802 substr($prefix, -length($file)) = '';
1803
1804 ##print "prefix<$prefix>\n";
1805 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001806 WARN("USE_RELATIVE_PATH",
1807 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001808 }
1809}
1810
Joe Perches3705ce52013-07-03 15:05:31 -07001811sub trim {
1812 my ($string) = @_;
1813
Joe Perchesb34c6482013-09-11 14:24:01 -07001814 $string =~ s/^\s+|\s+$//g;
1815
1816 return $string;
1817}
1818
1819sub ltrim {
1820 my ($string) = @_;
1821
1822 $string =~ s/^\s+//;
1823
1824 return $string;
1825}
1826
1827sub rtrim {
1828 my ($string) = @_;
1829
1830 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001831
1832 return $string;
1833}
1834
Joe Perches52ea8502013-11-12 15:10:09 -08001835sub string_find_replace {
1836 my ($string, $find, $replace) = @_;
1837
1838 $string =~ s/$find/$replace/g;
1839
1840 return $string;
1841}
1842
Joe Perches3705ce52013-07-03 15:05:31 -07001843sub tabify {
1844 my ($leading) = @_;
1845
1846 my $source_indent = 8;
1847 my $max_spaces_before_tab = $source_indent - 1;
1848 my $spaces_to_tab = " " x $source_indent;
1849
1850 #convert leading spaces to tabs
1851 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1852 #Remove spaces before a tab
1853 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1854
1855 return "$leading";
1856}
1857
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001858sub pos_last_openparen {
1859 my ($line) = @_;
1860
1861 my $pos = 0;
1862
1863 my $opens = $line =~ tr/\(/\(/;
1864 my $closes = $line =~ tr/\)/\)/;
1865
1866 my $last_openparen = 0;
1867
1868 if (($opens == 0) || ($closes >= $opens)) {
1869 return -1;
1870 }
1871
1872 my $len = length($line);
1873
1874 for ($pos = 0; $pos < $len; $pos++) {
1875 my $string = substr($line, $pos);
1876 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1877 $pos += length($1) - 1;
1878 } elsif (substr($line, $pos, 1) eq '(') {
1879 $last_openparen = $pos;
1880 } elsif (index($string, '(') == -1) {
1881 last;
1882 }
1883 }
1884
Joe Perches91cb5192014-04-03 14:49:32 -07001885 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001886}
1887
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001888sub process {
1889 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001890
1891 my $linenr=0;
1892 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001893 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001894 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001895 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001896
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001897 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001898 my $indent;
1899 my $previndent=0;
1900 my $stashindent=0;
1901
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001902 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001903 my $signoff = 0;
1904 my $is_patch = 0;
1905
Joe Perches29ee1b02014-08-06 16:10:35 -07001906 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001907 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches2a076f42015-04-16 12:44:28 -07001908 my $commit_log_long_line = 0;
Joe Perches13f19372014-08-06 16:10:59 -07001909 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001910 my $non_utf8_charset = 0;
1911
Joe Perches365dd4e2014-08-06 16:10:42 -07001912 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001913 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001914
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001915 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001916 our $cnt_lines = 0;
1917 our $cnt_error = 0;
1918 our $cnt_warn = 0;
1919 our $cnt_chk = 0;
1920
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001921 # Trace the real file/line as we go.
1922 my $realfile = '';
1923 my $realline = 0;
1924 my $realcnt = 0;
1925 my $here = '';
1926 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001927 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001928 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001929 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001930
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001931 my $prev_values = 'E';
1932
1933 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001934 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001935 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001936 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001937 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001938
Joe Perches7e51f192013-09-11 14:23:57 -07001939 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001940
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001941 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001942 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001943 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001944 my @setup_docs = ();
1945 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001946
Joe Perchesd8b07712013-11-12 15:10:06 -08001947 my $camelcase_file_seeded = 0;
1948
Andy Whitcroft773647a2008-03-28 14:15:58 -07001949 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001950 my $line;
1951 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001952 $linenr++;
1953 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001954
Joe Perches3705ce52013-07-03 15:05:31 -07001955 push(@fixed, $rawline) if ($fix);
1956
Andy Whitcroft773647a2008-03-28 14:15:58 -07001957 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001958 $setup_docs = 0;
1959 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1960 $setup_docs = 1;
1961 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001962 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001963 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001964 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1965 $realline=$1-1;
1966 if (defined $2) {
1967 $realcnt=$3+1;
1968 } else {
1969 $realcnt=1+1;
1970 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001971 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001972
1973 # Guestimate if this is a continuing comment. Run
1974 # the context looking for a comment "edge". If this
1975 # edge is a close comment then we must be in a comment
1976 # at context start.
1977 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001978 my $cnt = $realcnt;
1979 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1980 next if (defined $rawlines[$ln - 1] &&
1981 $rawlines[$ln - 1] =~ /^-/);
1982 $cnt--;
1983 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001984 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001985 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1986 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1987 ($edge) = $1;
1988 last;
1989 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001990 }
1991 if (defined $edge && $edge eq '*/') {
1992 $in_comment = 1;
1993 }
1994
1995 # Guestimate if this is a continuing comment. If this
1996 # is the start of a diff block and this line starts
1997 # ' *' then it is very likely a comment.
1998 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001999 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002000 {
2001 $in_comment = 1;
2002 }
2003
2004 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2005 sanitise_line_reset($in_comment);
2006
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002007 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002008 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002009 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002010 $line = sanitise_line($rawline);
2011 }
2012 push(@lines, $line);
2013
2014 if ($realcnt > 1) {
2015 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2016 } else {
2017 $realcnt = 0;
2018 }
2019
2020 #print "==>$rawline\n";
2021 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002022
2023 if ($setup_docs && $line =~ /^\+/) {
2024 push(@setup_docs, $line);
2025 }
2026 }
2027
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002028 $prefix = '';
2029
Andy Whitcroft773647a2008-03-28 14:15:58 -07002030 $realcnt = 0;
2031 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002032 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002033 foreach my $line (@lines) {
2034 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002035 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002036 my $sline = $line; #copy of $line
2037 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002038
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002039 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002040
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002041#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002042 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002043 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002044 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002045 $realline=$1-1;
2046 if (defined $2) {
2047 $realcnt=$3+1;
2048 } else {
2049 $realcnt=1+1;
2050 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002051 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002052 $prev_values = 'E';
2053
Andy Whitcroft773647a2008-03-28 14:15:58 -07002054 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002055 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002056 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002057 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002058 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002059
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002060# track the line number as we move through the hunk, note that
2061# new versions of GNU diff omit the leading space on completely
2062# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002063 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002064 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002065 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002066
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002067 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002068 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002069
2070 # Track the previous line.
2071 ($prevline, $stashline) = ($stashline, $line);
2072 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002073 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2074
Andy Whitcroft773647a2008-03-28 14:15:58 -07002075 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002076
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002077 } elsif ($realcnt == 1) {
2078 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002079 }
2080
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002081 my $hunk_line = ($realcnt != 0);
2082
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002083#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002084 $prefix = "$filename:$realline: " if ($emacs && $file);
2085 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2086
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002087 $here = "#$linenr: " if (!$file);
2088 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002089
Joe Perches2ac73b42014-06-04 16:12:05 -07002090 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002091 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002092 if ($line =~ /^diff --git.*?(\S+)$/) {
2093 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002094 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002095 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002096 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002097 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002098 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002099 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002100 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002101
2102 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002103 if (!$file && $tree && $p1_prefix ne '' &&
2104 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002105 WARN("PATCH_PREFIX",
2106 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002107 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002108
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002109 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002110 ERROR("MODIFIED_INCLUDE_ASM",
2111 "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 -07002112 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002113 $found_file = 1;
2114 }
2115
2116 if ($found_file) {
2117 if ($realfile =~ m@^(drivers/net/|net/)@) {
2118 $check = 1;
2119 } else {
2120 $check = $check_orig;
2121 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002122 next;
2123 }
2124
Randy Dunlap389834b2007-06-08 13:47:03 -07002125 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002126
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002127 my $hereline = "$here\n$rawline\n";
2128 my $herecurr = "$here\n$rawline\n";
2129 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002130
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002131 $cnt_lines++ if ($realcnt != 0);
2132
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002133# Check for incorrect file permissions
2134 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2135 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002136 if ($realfile !~ m@scripts/@ &&
2137 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002138 ERROR("EXECUTE_PERMISSIONS",
2139 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002140 }
2141 }
2142
Joe Perches20112472011-07-25 17:13:23 -07002143# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002144 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002145 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002146 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002147 }
2148
Joe Perchese0d975b2014-12-10 15:51:49 -08002149# Check if MAINTAINERS is being updated. If so, there's probably no need to
2150# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2151 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2152 $reported_maintainer_file = 1;
2153 }
2154
Joe Perches20112472011-07-25 17:13:23 -07002155# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002156 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002157 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002158 my $space_before = $1;
2159 my $sign_off = $2;
2160 my $space_after = $3;
2161 my $email = $4;
2162 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2163
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002164 if ($sign_off !~ /$signature_tags/) {
2165 WARN("BAD_SIGN_OFF",
2166 "Non-standard signature: $sign_off\n" . $herecurr);
2167 }
Joe Perches20112472011-07-25 17:13:23 -07002168 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002169 if (WARN("BAD_SIGN_OFF",
2170 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2171 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002172 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002173 "$ucfirst_sign_off $email";
2174 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002175 }
Joe Perches20112472011-07-25 17:13:23 -07002176 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002177 if (WARN("BAD_SIGN_OFF",
2178 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2179 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002180 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002181 "$ucfirst_sign_off $email";
2182 }
2183
Joe Perches20112472011-07-25 17:13:23 -07002184 }
2185 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002186 if (WARN("BAD_SIGN_OFF",
2187 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2188 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002189 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002190 "$ucfirst_sign_off $email";
2191 }
Joe Perches20112472011-07-25 17:13:23 -07002192 }
2193
2194 my ($email_name, $email_address, $comment) = parse_email($email);
2195 my $suggested_email = format_email(($email_name, $email_address));
2196 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002197 ERROR("BAD_SIGN_OFF",
2198 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002199 } else {
2200 my $dequoted = $suggested_email;
2201 $dequoted =~ s/^"//;
2202 $dequoted =~ s/" </ </;
2203 # Don't force email to have quotes
2204 # Allow just an angle bracketed address
2205 if ("$dequoted$comment" ne $email &&
2206 "<$email_address>$comment" ne $email &&
2207 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002208 WARN("BAD_SIGN_OFF",
2209 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002210 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002211 }
Joe Perches7e51f192013-09-11 14:23:57 -07002212
2213# Check for duplicate signatures
2214 my $sig_nospace = $line;
2215 $sig_nospace =~ s/\s//g;
2216 $sig_nospace = lc($sig_nospace);
2217 if (defined $signatures{$sig_nospace}) {
2218 WARN("BAD_SIGN_OFF",
2219 "Duplicate signature\n" . $herecurr);
2220 } else {
2221 $signatures{$sig_nospace} = 1;
2222 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002223 }
2224
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002225# Check email subject for common tools that don't need to be mentioned
2226 if ($in_header_lines &&
2227 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2228 WARN("EMAIL_SUBJECT",
2229 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2230 }
2231
Joe Perches9b3189e2014-06-04 16:12:10 -07002232# Check for old stable address
2233 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2234 ERROR("STABLE_ADDRESS",
2235 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2236 }
2237
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002238# Check for unwanted Gerrit info
2239 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2240 ERROR("GERRIT_CHANGE_ID",
2241 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2242 }
2243
Joe Perches2a076f42015-04-16 12:44:28 -07002244# Check for line lengths > 75 in commit log, warn once
2245 if ($in_commit_log && !$commit_log_long_line &&
2246 length($line) > 75) {
2247 WARN("COMMIT_LOG_LONG_LINE",
2248 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2249 $commit_log_long_line = 1;
2250 }
2251
Joe Perches0d7835f2015-02-13 14:38:35 -08002252# Check for git id commit length and improperly formed commit descriptions
2253 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002254 my $init_char = $1;
2255 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002256 my $short = 1;
2257 my $long = 0;
2258 my $case = 1;
2259 my $space = 1;
2260 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002261 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002262 my $id = '0123456789ab';
2263 my $orig_desc = "commit description";
2264 my $description = "";
2265
2266 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2267 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2268 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2269 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2270 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2271 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002272 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002273 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2274 defined $rawlines[$linenr] &&
2275 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2276 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002277 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002278 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2279 defined $rawlines[$linenr] &&
2280 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2281 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2282 $orig_desc = $1;
2283 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2284 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002285 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002286 }
2287
2288 ($id, $description) = git_commit_info($orig_commit,
2289 $id, $orig_desc);
2290
Joe Perches19c146a2015-02-13 14:39:00 -08002291 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002292 ERROR("GIT_COMMIT_ID",
2293 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2294 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002295 }
2296
Joe Perches13f19372014-08-06 16:10:59 -07002297# Check for added, moved or deleted files
2298 if (!$reported_maintainer_file && !$in_commit_log &&
2299 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2300 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2301 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2302 (defined($1) || defined($2))))) {
2303 $reported_maintainer_file = 1;
2304 WARN("FILE_PATH_CHANGES",
2305 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2306 }
2307
Andy Whitcroft00df3442007-06-08 13:47:06 -07002308# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002309 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002310 ERROR("CORRUPTED_PATCH",
2311 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002312 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002313 }
2314
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002315# Check for absolute kernel paths.
2316 if ($tree) {
2317 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2318 my $file = $1;
2319
2320 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2321 check_absolute_file($1, $herecurr)) {
2322 #
2323 } else {
2324 check_absolute_file($file, $herecurr);
2325 }
2326 }
2327 }
2328
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002329# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2330 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002331 $rawline !~ m/^$UTF8*$/) {
2332 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2333
2334 my $blank = copy_spacing($rawline);
2335 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2336 my $hereptr = "$hereline$ptr\n";
2337
Joe Perches34d99212011-07-25 17:13:26 -07002338 CHK("INVALID_UTF8",
2339 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002340 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002341
Joe Perches15662b32011-10-31 17:13:12 -07002342# Check if it's the start of a commit log
2343# (not a header line and we haven't seen the patch filename)
2344 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002345 !($rawline =~ /^\s+\S/ ||
2346 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002347 $in_header_lines = 0;
2348 $in_commit_log = 1;
2349 }
2350
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002351# Check if there is UTF-8 in a commit log when a mail header has explicitly
2352# declined it, i.e defined some charset where it is missing.
2353 if ($in_header_lines &&
2354 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2355 $1 !~ /utf-8/i) {
2356 $non_utf8_charset = 1;
2357 }
2358
2359 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002360 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002361 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002362 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2363 }
2364
Kees Cook66b47b42014-10-13 15:51:57 -07002365# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002366 if (defined($misspellings) &&
2367 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002368 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002369 my $typo = $1;
2370 my $typo_fix = $spelling_fix{lc($typo)};
2371 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2372 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2373 my $msg_type = \&WARN;
2374 $msg_type = \&CHK if ($file);
2375 if (&{$msg_type}("TYPO_SPELLING",
2376 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2377 $fix) {
2378 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2379 }
2380 }
2381 }
2382
Andy Whitcroft306708542008-10-15 22:02:28 -07002383# ignore non-hunk lines and lines being removed
2384 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002385
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002386#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002387 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002388 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002389 if (ERROR("DOS_LINE_ENDINGS",
2390 "DOS line endings\n" . $herevet) &&
2391 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002392 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002393 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002394 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2395 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002396 if (ERROR("TRAILING_WHITESPACE",
2397 "trailing whitespace\n" . $herevet) &&
2398 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002399 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002400 }
2401
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002402 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002403 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002404
Josh Triplett4783f892013-11-12 15:10:12 -08002405# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002406 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002407 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2408 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002409 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2410 my $msg_type = \&ERROR;
2411 $msg_type = \&CHK if ($file);
2412 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002413 "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 -08002414 }
2415
Andi Kleen33549572010-05-24 14:33:29 -07002416# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002417# Only applies when adding the entry originally, after that we do not have
2418# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002419 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002420 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002421 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002422 my $cnt = $realcnt;
2423 my $ln = $linenr + 1;
2424 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002425 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002426 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002427 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002428 $f = $lines[$ln - 1];
2429 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2430 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002431
2432 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002433 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002434
Joe Perches8d73e0e2014-08-06 16:10:46 -07002435 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002436 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002437 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002438 $length = -1;
2439 }
2440
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002441 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002442 $f =~ s/#.*//;
2443 $f =~ s/^\s+//;
2444 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002445 if ($f =~ /^\s*config\s/) {
2446 $is_end = 1;
2447 last;
2448 }
Andi Kleen33549572010-05-24 14:33:29 -07002449 $length++;
2450 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002451 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2452 WARN("CONFIG_DESCRIPTION",
2453 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2454 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002455 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002456 }
2457
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002458# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2459 if ($realfile =~ /Kconfig/ &&
2460 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2461 WARN("CONFIG_EXPERIMENTAL",
2462 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2463 }
2464
Christoph Jaeger327953e2015-02-13 14:38:29 -08002465# discourage the use of boolean for type definition attributes of Kconfig options
2466 if ($realfile =~ /Kconfig/ &&
2467 $line =~ /^\+\s*\bboolean\b/) {
2468 WARN("CONFIG_TYPE_BOOLEAN",
2469 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2470 }
2471
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002472 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2473 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2474 my $flag = $1;
2475 my $replacement = {
2476 'EXTRA_AFLAGS' => 'asflags-y',
2477 'EXTRA_CFLAGS' => 'ccflags-y',
2478 'EXTRA_CPPFLAGS' => 'cppflags-y',
2479 'EXTRA_LDFLAGS' => 'ldflags-y',
2480 };
2481
2482 WARN("DEPRECATED_VARIABLE",
2483 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2484 }
2485
Rob Herringbff5da42014-01-23 15:54:51 -08002486# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002487 if (defined $root &&
2488 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2489 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2490
Rob Herringbff5da42014-01-23 15:54:51 -08002491 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2492
Florian Vaussardcc933192014-04-03 14:49:27 -07002493 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2494 my $vp_file = $dt_path . "vendor-prefixes.txt";
2495
Rob Herringbff5da42014-01-23 15:54:51 -08002496 foreach my $compat (@compats) {
2497 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002498 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2499 my $compat3 = $compat;
2500 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2501 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002502 if ( $? >> 8 ) {
2503 WARN("UNDOCUMENTED_DT_STRING",
2504 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2505 }
2506
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002507 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2508 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002509 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002510 if ( $? >> 8 ) {
2511 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002512 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002513 }
2514 }
2515 }
2516
Andy Whitcroft5368df202008-10-15 22:02:27 -07002517# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002518 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002519
Joe Perches47e0c882015-06-25 15:02:57 -07002520# line length limit (with some exclusions)
2521#
2522# There are a few types of lines that may extend beyond $max_line_length:
2523# logging functions like pr_info that end in a string
2524# lines with a single string
2525# #defines that are a single string
2526#
2527# There are 3 different line length message types:
2528# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2529# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2530# LONG_LINE all other lines longer than $max_line_length
2531#
2532# if LONG_LINE is ignored, the other 2 types are also ignored
2533#
2534
2535 if ($length > $max_line_length) {
2536 my $msg_type = "LONG_LINE";
2537
2538 # Check the allowed long line types first
2539
2540 # logging functions that end in a string that starts
2541 # before $max_line_length
2542 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2543 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2544 $msg_type = "";
2545
2546 # lines with only strings (w/ possible termination)
2547 # #defines with only strings
2548 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2549 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2550 $msg_type = "";
2551
2552 # Otherwise set the alternate message types
2553
2554 # a comment starts before $max_line_length
2555 } elsif ($line =~ /($;[\s$;]*)$/ &&
2556 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2557 $msg_type = "LONG_LINE_COMMENT"
2558
2559 # a quoted string starts before $max_line_length
2560 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2561 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2562 $msg_type = "LONG_LINE_STRING"
2563 }
2564
2565 if ($msg_type ne "" &&
2566 (show_type("LONG_LINE") || show_type($msg_type))) {
2567 WARN($msg_type,
2568 "line over $max_line_length characters\n" . $herecurr);
2569 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002570 }
2571
Andy Whitcroft8905a672007-11-28 16:21:06 -08002572# check for adding lines without a newline.
2573 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002574 WARN("MISSING_EOF_NEWLINE",
2575 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002576 }
2577
Mike Frysinger42e41c52009-09-21 17:04:40 -07002578# Blackfin: use hi/lo macros
2579 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2580 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2581 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002582 ERROR("LO_MACRO",
2583 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002584 }
2585 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2586 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002587 ERROR("HI_MACRO",
2588 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002589 }
2590 }
2591
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002592# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002593 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002594
2595# at the beginning of a line any tabs must come first and anything
2596# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002597 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2598 $rawline =~ /^\+\s* \s*/) {
2599 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002600 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002601 if (ERROR("CODE_INDENT",
2602 "code indent should use tabs where possible\n" . $herevet) &&
2603 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002604 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002605 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002606 }
2607
Alberto Panizzo08e44362010-03-05 13:43:54 -08002608# check for space before tabs.
2609 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2610 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002611 if (WARN("SPACE_BEFORE_TAB",
2612 "please, no space before tabs\n" . $herevet) &&
2613 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002614 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002615 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002616 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002617 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002618 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002619 }
2620
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002621# check for && or || at the start of a line
2622 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2623 CHK("LOGICAL_CONTINUATIONS",
2624 "Logical continuations should be on the previous line\n" . $hereprev);
2625 }
2626
2627# check multi-line statement indentation matches previous line
2628 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002629 $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 -07002630 $prevline =~ /^\+(\t*)(.*)$/;
2631 my $oldindent = $1;
2632 my $rest = $2;
2633
2634 my $pos = pos_last_openparen($rest);
2635 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002636 $line =~ /^(\+| )([ \t]*)/;
2637 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002638
2639 my $goodtabindent = $oldindent .
2640 "\t" x ($pos / 8) .
2641 " " x ($pos % 8);
2642 my $goodspaceindent = $oldindent . " " x $pos;
2643
2644 if ($newindent ne $goodtabindent &&
2645 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002646
2647 if (CHK("PARENTHESIS_ALIGNMENT",
2648 "Alignment should match open parenthesis\n" . $hereprev) &&
2649 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002650 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002651 s/^\+[ \t]*/\+$goodtabindent/;
2652 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002653 }
2654 }
2655 }
2656
Joe Perches6ab3a972015-04-16 12:44:05 -07002657# check for space after cast like "(int) foo" or "(struct foo) bar"
2658# avoid checking a few false positives:
2659# "sizeof(<type>)" or "__alignof__(<type>)"
2660# function pointer declarations like "(*foo)(int) = bar;"
2661# structure definitions like "(struct foo) { 0 };"
2662# multiline macros that define functions
2663# known attributes or the __attribute__ keyword
2664 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2665 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002666 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002667 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002668 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002669 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002670 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002671 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002672 }
2673
Joe Perches05880602012-10-04 17:13:35 -07002674 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002675 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002676 $rawline =~ /^\+[ \t]*\*/ &&
2677 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002678 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2679 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2680 }
2681
2682 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002683 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2684 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002685 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002686 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2687 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2688 "networking block comments start with * on subsequent lines\n" . $hereprev);
2689 }
2690
2691 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002692 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2693 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2694 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2695 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002696 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2697 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2698 }
2699
Joe Perches7f619192014-08-06 16:10:39 -07002700# check for missing blank lines after struct/union declarations
2701# with exceptions for various attributes and macros
2702 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2703 $line =~ /^\+/ &&
2704 !($line =~ /^\+\s*$/ ||
2705 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2706 $line =~ /^\+\s*MODULE_/i ||
2707 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2708 $line =~ /^\+[a-z_]*init/ ||
2709 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2710 $line =~ /^\+\s*DECLARE/ ||
2711 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002712 if (CHK("LINE_SPACING",
2713 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2714 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002715 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002716 }
Joe Perches7f619192014-08-06 16:10:39 -07002717 }
2718
Joe Perches365dd4e2014-08-06 16:10:42 -07002719# check for multiple consecutive blank lines
2720 if ($prevline =~ /^[\+ ]\s*$/ &&
2721 $line =~ /^\+\s*$/ &&
2722 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002723 if (CHK("LINE_SPACING",
2724 "Please don't use multiple blank lines\n" . $hereprev) &&
2725 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002726 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002727 }
2728
Joe Perches365dd4e2014-08-06 16:10:42 -07002729 $last_blank_line = $linenr;
2730 }
2731
Joe Perches3b617e32014-04-03 14:49:28 -07002732# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002733 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2734 # actual declarations
2735 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002736 # function pointer declarations
2737 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002738 # foo bar; where foo is some local typedef or #define
2739 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2740 # known declaration macros
2741 $prevline =~ /^\+\s+$declaration_macros/) &&
2742 # for "else if" which can look like "$Ident $Ident"
2743 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2744 # other possible extensions of declaration lines
2745 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2746 # not starting a section or a macro "\" extended line
2747 $prevline =~ /(?:\{\s*|\\)$/) &&
2748 # looks like a declaration
2749 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002750 # function pointer declarations
2751 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002752 # foo bar; where foo is some local typedef or #define
2753 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2754 # known declaration macros
2755 $sline =~ /^\+\s+$declaration_macros/ ||
2756 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002757 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002758 # start or end of block or continuation of declaration
2759 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2760 # bitfield continuation
2761 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2762 # other possible extensions of declaration lines
2763 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2764 # indentation of previous and current line are the same
2765 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002766 if (WARN("LINE_SPACING",
2767 "Missing a blank line after declarations\n" . $hereprev) &&
2768 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002769 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002770 }
Joe Perches3b617e32014-04-03 14:49:28 -07002771 }
2772
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002773# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002774# Exceptions:
2775# 1) within comments
2776# 2) indented preprocessor commands
2777# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002778 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002779 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002780 if (WARN("LEADING_SPACE",
2781 "please, no spaces at the start of a line\n" . $herevet) &&
2782 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002783 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002784 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002785 }
2786
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002787# check we are in a valid C source file if not then ignore this hunk
2788 next if ($realfile !~ /\.(h|c)$/);
2789
Joe Perches032a4c02014-08-06 16:10:29 -07002790# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002791# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002792# if the previous line is a break or return and is indented 1 tab more...
2793 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2794 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002795 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2796 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2797 defined $lines[$linenr] &&
2798 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002799 WARN("UNNECESSARY_ELSE",
2800 "else is not generally useful after a break or return\n" . $hereprev);
2801 }
2802 }
2803
Joe Perchesc00df192014-08-06 16:11:01 -07002804# check indentation of a line with a break;
2805# if the previous line is a goto or return and is indented the same # of tabs
2806 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2807 my $tabs = $1;
2808 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2809 WARN("UNNECESSARY_BREAK",
2810 "break is not useful after a goto or return\n" . $hereprev);
2811 }
2812 }
2813
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002814# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2815 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2816 WARN("CONFIG_EXPERIMENTAL",
2817 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2818 }
2819
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002820# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002821 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002822 WARN("CVS_KEYWORD",
2823 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002824 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002825
Mike Frysinger42e41c52009-09-21 17:04:40 -07002826# Blackfin: don't use __builtin_bfin_[cs]sync
2827 if ($line =~ /__builtin_bfin_csync/) {
2828 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002829 ERROR("CSYNC",
2830 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002831 }
2832 if ($line =~ /__builtin_bfin_ssync/) {
2833 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002834 ERROR("SSYNC",
2835 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002836 }
2837
Joe Perches56e77d72013-02-21 16:44:14 -08002838# check for old HOTPLUG __dev<foo> section markings
2839 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2840 WARN("HOTPLUG_SECTION",
2841 "Using $1 is unnecessary\n" . $herecurr);
2842 }
2843
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002844# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002845 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2846 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002847#print "LINE<$line>\n";
2848 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002849 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002850 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002851 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002852 $stat =~ s/\n./\n /g;
2853 $cond =~ s/\n./\n /g;
2854
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002855#print "linenr<$linenr> <$stat>\n";
2856 # If this statement has no statement boundaries within
2857 # it there is no point in retrying a statement scan
2858 # until we hit end of it.
2859 my $frag = $stat; $frag =~ s/;+\s*$//;
2860 if ($frag !~ /(?:{|;)/) {
2861#print "skip<$line_nr_next>\n";
2862 $suppress_statement = $line_nr_next;
2863 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002864
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002865 # Find the real next line.
2866 $realline_next = $line_nr_next;
2867 if (defined $realline_next &&
2868 (!defined $lines[$realline_next - 1] ||
2869 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2870 $realline_next++;
2871 }
2872
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002873 my $s = $stat;
2874 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002875
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002876 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002877 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002878
2879 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002880 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002881
Andy Whitcroft463f2862009-09-21 17:04:34 -07002882 } elsif ($s =~ /^.\s*else\b/s) {
2883
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002884 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002885 } 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 -07002886 my $type = $1;
2887 $type =~ s/\s+/ /g;
2888 possible($type, "A:" . $s);
2889
Andy Whitcroft8905a672007-11-28 16:21:06 -08002890 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002891 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002892 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002893 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002894
2895 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002896 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002897 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002898 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002899
2900 # Check for any sort of function declaration.
2901 # int foo(something bar, other baz);
2902 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002903 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 -08002904 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002905
Andy Whitcroftcf655042008-03-04 14:28:20 -08002906 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002907 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002908 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002909
Andy Whitcroft8905a672007-11-28 16:21:06 -08002910 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002911 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002912
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002913 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002914 }
2915 }
2916 }
2917
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002918 }
2919
Andy Whitcroft653d4872007-06-23 17:16:34 -07002920#
2921# Checks which may be anchored in the context.
2922#
2923
2924# Check for switch () and associated case and default
2925# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002926 if ($line=~/\bswitch\s*\(.*\)/) {
2927 my $err = '';
2928 my $sep = '';
2929 my @ctx = ctx_block_outer($linenr, $realcnt);
2930 shift(@ctx);
2931 for my $ctx (@ctx) {
2932 my ($clen, $cindent) = line_stats($ctx);
2933 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2934 $indent != $cindent) {
2935 $err .= "$sep$ctx\n";
2936 $sep = '';
2937 } else {
2938 $sep = "[...]\n";
2939 }
2940 }
2941 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002942 ERROR("SWITCH_CASE_INDENT_LEVEL",
2943 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002944 }
2945 }
2946
2947# if/while/etc brace do not go on next line, unless defining a do while loop,
2948# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002949 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 -07002950 my $pre_ctx = "$1$2";
2951
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002952 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002953
2954 if ($line =~ /^\+\t{6,}/) {
2955 WARN("DEEP_INDENTATION",
2956 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2957 }
2958
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002959 my $ctx_cnt = $realcnt - $#ctx - 1;
2960 my $ctx = join("\n", @ctx);
2961
Andy Whitcroft548596d2008-07-23 21:29:01 -07002962 my $ctx_ln = $linenr;
2963 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002964
Andy Whitcroft548596d2008-07-23 21:29:01 -07002965 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2966 defined $lines[$ctx_ln - 1] &&
2967 $lines[$ctx_ln - 1] =~ /^-/)) {
2968 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2969 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002970 $ctx_ln++;
2971 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002972
Andy Whitcroft53210162008-07-23 21:29:03 -07002973 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2974 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002975
Joe Perchesd752fcc2014-08-06 16:11:05 -07002976 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002977 ERROR("OPEN_BRACE",
2978 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002979 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002980 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002981 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2982 $ctx =~ /\)\s*\;\s*$/ &&
2983 defined $lines[$ctx_ln - 1])
2984 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002985 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2986 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002987 WARN("TRAILING_SEMICOLON",
2988 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002989 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002990 }
2991 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002992 }
2993
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002994# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002995 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 -08002996 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2997 ctx_statement_block($linenr, $realcnt, 0)
2998 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002999 my ($s, $c) = ($stat, $cond);
3000
3001 substr($s, 0, length($c), '');
3002
3003 # Make sure we remove the line prefixes as we have
3004 # none on the first line, and are going to readd them
3005 # where necessary.
3006 $s =~ s/\n./\n/gs;
3007
3008 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003009 my @newlines = ($c =~ /\n/gs);
3010 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003011
3012 # We want to check the first line inside the block
3013 # starting at the end of the conditional, so remove:
3014 # 1) any blank line termination
3015 # 2) any opening brace { on end of the line
3016 # 3) any do (...) {
3017 my $continuation = 0;
3018 my $check = 0;
3019 $s =~ s/^.*\bdo\b//;
3020 $s =~ s/^\s*{//;
3021 if ($s =~ s/^\s*\\//) {
3022 $continuation = 1;
3023 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003024 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003025 $check = 1;
3026 $cond_lines++;
3027 }
3028
3029 # Also ignore a loop construct at the end of a
3030 # preprocessor statement.
3031 if (($prevline =~ /^.\s*#\s*define\s/ ||
3032 $prevline =~ /\\\s*$/) && $continuation == 0) {
3033 $check = 0;
3034 }
3035
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003036 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003037 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003038 while ($cond_ptr != $cond_lines) {
3039 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003040
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003041 # If we see an #else/#elif then the code
3042 # is not linear.
3043 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3044 $check = 0;
3045 }
3046
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003047 # Ignore:
3048 # 1) blank lines, they should be at 0,
3049 # 2) preprocessor lines, and
3050 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003051 if ($continuation ||
3052 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003053 $s =~ /^\s*#\s*?/ ||
3054 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003055 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003056 if ($s =~ s/^.*?\n//) {
3057 $cond_lines++;
3058 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003059 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003060 }
3061
3062 my (undef, $sindent) = line_stats("+" . $s);
3063 my $stat_real = raw_line($linenr, $cond_lines);
3064
3065 # Check if either of these lines are modified, else
3066 # this is not this patch's fault.
3067 if (!defined($stat_real) ||
3068 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3069 $check = 0;
3070 }
3071 if (defined($stat_real) && $cond_lines > 1) {
3072 $stat_real = "[...]\n$stat_real";
3073 }
3074
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003075 #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 -07003076
3077 if ($check && (($sindent % 8) != 0 ||
3078 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003079 WARN("SUSPECT_CODE_INDENT",
3080 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003081 }
3082 }
3083
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003084 # Track the 'values' across context and added lines.
3085 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003086 my ($curr_values, $curr_vars) =
3087 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003088 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003089 if ($dbg_values) {
3090 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003091 print "$linenr > .$outline\n";
3092 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003093 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003094 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003095 $prev_values = substr($curr_values, -1);
3096
Andy Whitcroft00df3442007-06-08 13:47:06 -07003097#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003098 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003099
Andy Whitcroft653d4872007-06-23 17:16:34 -07003100# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003101 if ($dbg_type) {
3102 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003103 ERROR("TEST_TYPE",
3104 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003105 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003106 ERROR("TEST_NOT_TYPE",
3107 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003108 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003109 next;
3110 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003111# TEST: allow direct testing of the attribute matcher.
3112 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003113 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003114 ERROR("TEST_ATTR",
3115 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003116 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003117 ERROR("TEST_NOT_ATTR",
3118 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003119 }
3120 next;
3121 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003122
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003123# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003124 if ($line =~ /^.\s*{/ &&
3125 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003126 if (ERROR("OPEN_BRACE",
3127 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003128 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3129 fix_delete_line($fixlinenr - 1, $prevrawline);
3130 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003131 my $fixedline = $prevrawline;
3132 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003133 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003134 $fixedline = $line;
3135 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003136 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003137 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003138 }
3139
Andy Whitcroft653d4872007-06-23 17:16:34 -07003140#
3141# Checks which are anchored on the added line.
3142#
3143
3144# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003145 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003146 my $path = $1;
3147 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003148 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003149 "malformed #include filename\n" . $herecurr);
3150 }
3151 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3152 ERROR("UAPI_INCLUDE",
3153 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003154 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003155 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003156
3157# no C99 // comments
3158 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003159 if (ERROR("C99_COMMENTS",
3160 "do not use C99 // comments\n" . $herecurr) &&
3161 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003162 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003163 if ($line =~ /\/\/(.*)$/) {
3164 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003165 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003166 }
3167 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003168 }
3169 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003170 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003171 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003172
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003173# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3174# the whole statement.
3175#print "APW <$lines[$realline_next - 1]>\n";
3176 if (defined $realline_next &&
3177 exists $lines[$realline_next - 1] &&
3178 !defined $suppress_export{$realline_next} &&
3179 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3180 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003181 # Handle definitions which produce identifiers with
3182 # a prefix:
3183 # XXX(foo);
3184 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003185 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003186 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003187 $name =~ /^${Ident}_$2/) {
3188#print "FOO C name<$name>\n";
3189 $suppress_export{$realline_next} = 1;
3190
3191 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003192 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003193 ^.DEFINE_$Ident\(\Q$name\E\)|
3194 ^.DECLARE_$Ident\(\Q$name\E\)|
3195 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003196 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3197 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003198 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003199#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3200 $suppress_export{$realline_next} = 2;
3201 } else {
3202 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003203 }
3204 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003205 if (!defined $suppress_export{$linenr} &&
3206 $prevline =~ /^.\s*$/ &&
3207 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3208 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3209#print "FOO B <$lines[$linenr - 1]>\n";
3210 $suppress_export{$linenr} = 2;
3211 }
3212 if (defined $suppress_export{$linenr} &&
3213 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003214 WARN("EXPORT_SYMBOL",
3215 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003216 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003217
Joe Eloff5150bda2010-08-09 17:21:00 -07003218# check for global initialisers.
Joe Perches5129e872015-06-10 11:14:52 -07003219 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003220 if (ERROR("GLOBAL_INITIALISERS",
3221 "do not initialise globals to 0 or NULL\n" .
3222 $herecurr) &&
3223 $fix) {
Joe Perches5129e872015-06-10 11:14:52 -07003224 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003225 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003226 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003227# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003228 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3229 if (ERROR("INITIALISED_STATIC",
3230 "do not initialise statics to 0 or NULL\n" .
3231 $herecurr) &&
3232 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003233 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003234 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003235 }
3236
Joe Perches18130872014-08-06 16:11:22 -07003237# check for misordered declarations of char/short/int/long with signed/unsigned
3238 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3239 my $tmp = trim($1);
3240 WARN("MISORDERED_TYPE",
3241 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3242 }
3243
Joe Perchescb710ec2010-10-26 14:23:20 -07003244# check for static const char * arrays.
3245 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003246 WARN("STATIC_CONST_CHAR_ARRAY",
3247 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003248 $herecurr);
3249 }
3250
3251# check for static char foo[] = "bar" declarations.
3252 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003253 WARN("STATIC_CONST_CHAR_ARRAY",
3254 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003255 $herecurr);
3256 }
3257
Joe Perchesab7e23f2015-04-16 12:44:22 -07003258# check for const <foo> const where <foo> is not a pointer or array type
3259 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3260 my $found = $1;
3261 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3262 WARN("CONST_CONST",
3263 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3264 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3265 WARN("CONST_CONST",
3266 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3267 }
3268 }
3269
Joe Perches9b0fa602014-04-03 14:49:18 -07003270# check for non-global char *foo[] = {"bar", ...} declarations.
3271 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3272 WARN("STATIC_CONST_CHAR_ARRAY",
3273 "char * array declaration might be better as static const\n" .
3274 $herecurr);
3275 }
3276
Joe Perchesb598b672015-04-16 12:44:36 -07003277# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3278 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3279 my $array = $1;
3280 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3281 my $array_div = $1;
3282 if (WARN("ARRAY_SIZE",
3283 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3284 $fix) {
3285 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3286 }
3287 }
3288 }
3289
Joe Perchesb36190c2014-01-27 17:07:18 -08003290# check for function declarations without arguments like "int foo()"
3291 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3292 if (ERROR("FUNCTION_WITHOUT_ARGS",
3293 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3294 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003295 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003296 }
3297 }
3298
Joe Perches92e112f2013-12-13 11:36:22 -07003299# check for uses of DEFINE_PCI_DEVICE_TABLE
3300 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3301 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3302 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3303 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003304 $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 -07003305 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003306 }
3307
Andy Whitcroft653d4872007-06-23 17:16:34 -07003308# check for new typedefs, only function parameters and sparse annotations
3309# make sense.
3310 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003311 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003312 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003313 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003314 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003315 WARN("NEW_TYPEDEFS",
3316 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003317 }
3318
3319# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003320 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003321 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3322 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003323 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003324
Andy Whitcroft65863862009-01-06 14:41:21 -08003325 # Should start with a space.
3326 $to =~ s/^(\S)/ $1/;
3327 # Should not end with a space.
3328 $to =~ s/\s+$//;
3329 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003330 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003331 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003332
Joe Perches3705ce52013-07-03 15:05:31 -07003333## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003334 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003335 if (ERROR("POINTER_LOCATION",
3336 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3337 $fix) {
3338 my $sub_from = $ident;
3339 my $sub_to = $ident;
3340 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003341 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003342 s@\Q$sub_from\E@$sub_to@;
3343 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003344 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003345 }
3346 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3347 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003348 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003349
Andy Whitcroft65863862009-01-06 14:41:21 -08003350 # Should start with a space.
3351 $to =~ s/^(\S)/ $1/;
3352 # Should not end with a space.
3353 $to =~ s/\s+$//;
3354 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003355 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003356 }
3357 # Modifiers should have spaces.
3358 $to =~ s/(\b$Modifier$)/$1 /;
3359
Joe Perches3705ce52013-07-03 15:05:31 -07003360## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003361 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003362 if (ERROR("POINTER_LOCATION",
3363 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3364 $fix) {
3365
3366 my $sub_from = $match;
3367 my $sub_to = $match;
3368 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003369 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003370 s@\Q$sub_from\E@$sub_to@;
3371 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003372 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003373 }
3374
3375# # no BUG() or BUG_ON()
3376# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3377# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3378# print "$herecurr";
3379# $clean = 0;
3380# }
3381
Andy Whitcroft8905a672007-11-28 16:21:06 -08003382 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003383 WARN("LINUX_VERSION_CODE",
3384 "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 -08003385 }
3386
Joe Perches17441222011-06-15 15:08:17 -07003387# check for uses of printk_ratelimit
3388 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003389 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003390 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003391 }
3392
Andy Whitcroft00df3442007-06-08 13:47:06 -07003393# printk should use KERN_* levels. Note that follow on printk's on the
3394# same line do not need a level, so we use the current block context
3395# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003396# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003397# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003398 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003399 my $ok = 0;
3400 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3401 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003402 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003403 # with "\n" ignore it, else it is to blame
3404 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3405 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3406 $ok = 1;
3407 }
3408 last;
3409 }
3410 }
3411 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003412 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3413 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003414 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003415 }
3416
Joe Perches243f3802012-05-31 16:26:09 -07003417 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3418 my $orig = $1;
3419 my $level = lc($orig);
3420 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003421 my $level2 = $level;
3422 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003423 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003424 "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 -07003425 }
3426
3427 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003428 if (WARN("PREFER_PR_LEVEL",
3429 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3430 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003431 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003432 s/\bpr_warning\b/pr_warn/;
3433 }
Joe Perches243f3802012-05-31 16:26:09 -07003434 }
3435
Joe Perchesdc139312013-02-21 16:44:13 -08003436 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3437 my $orig = $1;
3438 my $level = lc($orig);
3439 $level = "warn" if ($level eq "warning");
3440 $level = "dbg" if ($level eq "debug");
3441 WARN("PREFER_DEV_LEVEL",
3442 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3443 }
3444
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003445# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3446# number of false positives, but assembly files are not checked, so at
3447# least the arch entry code will not trigger this warning.
3448 if ($line =~ /\bENOSYS\b/) {
3449 WARN("ENOSYS",
3450 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3451 }
3452
Andy Whitcroft653d4872007-06-23 17:16:34 -07003453# function brace can't be on same line, except for #defines of do while,
3454# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003455 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003456 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003457 if (ERROR("OPEN_BRACE",
3458 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3459 $fix) {
3460 fix_delete_line($fixlinenr, $rawline);
3461 my $fixed_line = $rawline;
3462 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3463 my $line1 = $1;
3464 my $line2 = $2;
3465 fix_insert_line($fixlinenr, ltrim($line1));
3466 fix_insert_line($fixlinenr, "\+{");
3467 if ($line2 !~ /^\s*$/) {
3468 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3469 }
3470 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003471 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003472
Andy Whitcroft8905a672007-11-28 16:21:06 -08003473# open braces for enum, union and struct go on the same line.
3474 if ($line =~ /^.\s*{/ &&
3475 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003476 if (ERROR("OPEN_BRACE",
3477 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3478 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3479 fix_delete_line($fixlinenr - 1, $prevrawline);
3480 fix_delete_line($fixlinenr, $rawline);
3481 my $fixedline = rtrim($prevrawline) . " {";
3482 fix_insert_line($fixlinenr, $fixedline);
3483 $fixedline = $rawline;
3484 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3485 if ($fixedline !~ /^\+\s*$/) {
3486 fix_insert_line($fixlinenr, $fixedline);
3487 }
3488 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003489 }
3490
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003491# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003492 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3493 if (WARN("SPACING",
3494 "missing space after $1 definition\n" . $herecurr) &&
3495 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003496 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003497 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3498 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003499 }
3500
Joe Perches31070b52014-01-23 15:54:49 -08003501# Function pointer declarations
3502# check spacing between type, funcptr, and args
3503# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003504 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003505 my $declare = $1;
3506 my $pre_pointer_space = $2;
3507 my $post_pointer_space = $3;
3508 my $funcname = $4;
3509 my $post_funcname_space = $5;
3510 my $pre_args_space = $6;
3511
Joe Perches91f72e92014-04-03 14:49:12 -07003512# the $Declare variable will capture all spaces after the type
3513# so check it for a missing trailing missing space but pointer return types
3514# don't need a space so don't warn for those.
3515 my $post_declare_space = "";
3516 if ($declare =~ /(\s+)$/) {
3517 $post_declare_space = $1;
3518 $declare = rtrim($declare);
3519 }
3520 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003521 WARN("SPACING",
3522 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003523 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003524 }
3525
3526# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003527# This test is not currently implemented because these declarations are
3528# equivalent to
3529# int foo(int bar, ...)
3530# and this is form shouldn't/doesn't generate a checkpatch warning.
3531#
3532# elsif ($declare =~ /\s{2,}$/) {
3533# WARN("SPACING",
3534# "Multiple spaces after return type\n" . $herecurr);
3535# }
Joe Perches31070b52014-01-23 15:54:49 -08003536
3537# unnecessary space "type ( *funcptr)(args...)"
3538 if (defined $pre_pointer_space &&
3539 $pre_pointer_space =~ /^\s/) {
3540 WARN("SPACING",
3541 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3542 }
3543
3544# unnecessary space "type (* funcptr)(args...)"
3545 if (defined $post_pointer_space &&
3546 $post_pointer_space =~ /^\s/) {
3547 WARN("SPACING",
3548 "Unnecessary space before function pointer name\n" . $herecurr);
3549 }
3550
3551# unnecessary space "type (*funcptr )(args...)"
3552 if (defined $post_funcname_space &&
3553 $post_funcname_space =~ /^\s/) {
3554 WARN("SPACING",
3555 "Unnecessary space after function pointer name\n" . $herecurr);
3556 }
3557
3558# unnecessary space "type (*funcptr) (args...)"
3559 if (defined $pre_args_space &&
3560 $pre_args_space =~ /^\s/) {
3561 WARN("SPACING",
3562 "Unnecessary space before function pointer arguments\n" . $herecurr);
3563 }
3564
3565 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003566 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003567 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003568 }
3569 }
3570
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003571# check for spacing round square brackets; allowed:
3572# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003573# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3574# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003575 while ($line =~ /(.*?\s)\[/g) {
3576 my ($where, $prefix) = ($-[1], $1);
3577 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003578 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003579 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003580 if (ERROR("BRACKET_SPACE",
3581 "space prohibited before open square bracket '['\n" . $herecurr) &&
3582 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003583 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003584 s/^(\+.*?)\s+\[/$1\[/;
3585 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003586 }
3587 }
3588
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003589# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003590 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003591 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003592 my $ctx_before = substr($line, 0, $-[1]);
3593 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003594
3595 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003596 if ($name =~ /^(?:
3597 if|for|while|switch|return|case|
3598 volatile|__volatile__|
3599 __attribute__|format|__extension__|
3600 asm|__asm__)$/x)
3601 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003602 # cpp #define statements have non-optional spaces, ie
3603 # if there is a space between the name and the open
3604 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003605 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003606
3607 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003608 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003609
3610 # If this whole things ends with a type its most
3611 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003612 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003613
3614 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003615 if (WARN("SPACING",
3616 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3617 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003618 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003619 s/\b$name\s+\(/$name\(/;
3620 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003621 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003622 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003623
Andy Whitcroft653d4872007-06-23 17:16:34 -07003624# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003625 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003626 my $fixed_line = "";
3627 my $line_fixed = 0;
3628
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003629 my $ops = qr{
3630 <<=|>>=|<=|>=|==|!=|
3631 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3632 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003633 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003634 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003635 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003636 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003637
3638## print("element count: <" . $#elements . ">\n");
3639## foreach my $el (@elements) {
3640## print("el: <$el>\n");
3641## }
3642
3643 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003644 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003645
Joe Perches3705ce52013-07-03 15:05:31 -07003646 foreach my $el (@elements) {
3647 push(@fix_elements, substr($rawline, $off, length($el)));
3648 $off += length($el);
3649 }
3650
3651 $off = 0;
3652
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003653 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003654 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003655
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003656 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003657
3658 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3659
3660## print("n: <$n> good: <$good>\n");
3661
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003662 $off += length($elements[$n]);
3663
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003664 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003665 my $ca = substr($opline, 0, $off);
3666 my $cc = '';
3667 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3668 $cc = substr($opline, $off + length($elements[$n + 1]));
3669 }
3670 my $cb = "$ca$;$cc";
3671
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003672 my $a = '';
3673 $a = 'V' if ($elements[$n] ne '');
3674 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003675 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003676 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3677 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003678 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003679
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003680 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003681
3682 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003683 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003684 $c = 'V' if ($elements[$n + 2] ne '');
3685 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003686 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003687 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3688 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003689 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003690 } else {
3691 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003692 }
3693
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003694 my $ctx = "${a}x${c}";
3695
3696 my $at = "(ctx:$ctx)";
3697
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003698 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003699 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003700
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003701 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003702 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003703
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003704 # Get the full operator variant.
3705 my $opv = $op . substr($curr_vars, $off, 1);
3706
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003707 # Ignore operators passed as parameters.
3708 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07003709 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003710
Andy Whitcroftcf655042008-03-04 14:28:20 -08003711# # Ignore comments
3712# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003713
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003714 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003715 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003716 if ($ctx !~ /.x[WEBC]/ &&
3717 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003718 if (ERROR("SPACING",
3719 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003720 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003721 $line_fixed = 1;
3722 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003723 }
3724
3725 # // is a comment
3726 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003727
Joe Perchesb00e4812014-04-03 14:49:33 -07003728 # : when part of a bitfield
3729 } elsif ($opv eq ':B') {
3730 # skip the bitfield test for now
3731
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003732 # No spaces for:
3733 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003734 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003735 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003736 if (ERROR("SPACING",
3737 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003738 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003739 if (defined $fix_elements[$n + 2]) {
3740 $fix_elements[$n + 2] =~ s/^\s+//;
3741 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003742 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003743 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003744 }
3745
Joe Perches23810972014-12-10 15:51:32 -08003746 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003747 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003748 my $rtrim_before = 0;
3749 my $space_after = 0;
3750 if ($ctx =~ /Wx./) {
3751 if (ERROR("SPACING",
3752 "space prohibited before that '$op' $at\n" . $hereptr)) {
3753 $line_fixed = 1;
3754 $rtrim_before = 1;
3755 }
3756 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003757 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003758 if (ERROR("SPACING",
3759 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003760 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003761 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003762 $space_after = 1;
3763 }
3764 }
3765 if ($rtrim_before || $space_after) {
3766 if ($rtrim_before) {
3767 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3768 } else {
3769 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3770 }
3771 if ($space_after) {
3772 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003773 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003774 }
3775
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003776 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003777 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003778 #warn "'*' is part of type\n";
3779
3780 # unary operators should have a space before and
3781 # none after. May be left adjacent to another
3782 # unary operator, or a cast
3783 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003784 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003785 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003786 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003787 if (ERROR("SPACING",
3788 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003789 if ($n != $last_after + 2) {
3790 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3791 $line_fixed = 1;
3792 }
Joe Perches3705ce52013-07-03 15:05:31 -07003793 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003794 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003795 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003796 # A unary '*' may be const
3797
3798 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003799 if (ERROR("SPACING",
3800 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003801 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003802 if (defined $fix_elements[$n + 2]) {
3803 $fix_elements[$n + 2] =~ s/^\s+//;
3804 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003805 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003806 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003807 }
3808
3809 # unary ++ and unary -- are allowed no space on one side.
3810 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003811 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003812 if (ERROR("SPACING",
3813 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003814 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003815 $line_fixed = 1;
3816 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003817 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003818 if ($ctx =~ /Wx[BE]/ ||
3819 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003820 if (ERROR("SPACING",
3821 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003822 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003823 $line_fixed = 1;
3824 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003825 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003826 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003827 if (ERROR("SPACING",
3828 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003829 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003830 if (defined $fix_elements[$n + 2]) {
3831 $fix_elements[$n + 2] =~ s/^\s+//;
3832 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003833 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003834 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003835 }
3836
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003837 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003838 } elsif ($op eq '<<' or $op eq '>>' or
3839 $op eq '&' or $op eq '^' or $op eq '|' or
3840 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003841 $op eq '*' or $op eq '/' or
3842 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003843 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08003844 if ($check) {
3845 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3846 if (CHK("SPACING",
3847 "spaces preferred around that '$op' $at\n" . $hereptr)) {
3848 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3849 $fix_elements[$n + 2] =~ s/^\s+//;
3850 $line_fixed = 1;
3851 }
3852 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3853 if (CHK("SPACING",
3854 "space preferred before that '$op' $at\n" . $hereptr)) {
3855 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3856 $line_fixed = 1;
3857 }
3858 }
3859 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003860 if (ERROR("SPACING",
3861 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003862 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3863 if (defined $fix_elements[$n + 2]) {
3864 $fix_elements[$n + 2] =~ s/^\s+//;
3865 }
Joe Perches3705ce52013-07-03 15:05:31 -07003866 $line_fixed = 1;
3867 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003868 }
3869
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003870 # A colon needs no spaces before when it is
3871 # terminating a case value or a label.
3872 } elsif ($opv eq ':C' || $opv eq ':L') {
3873 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003874 if (ERROR("SPACING",
3875 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003876 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003877 $line_fixed = 1;
3878 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003879 }
3880
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003881 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003882 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003883 my $ok = 0;
3884
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003885 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003886 if (($op eq '<' &&
3887 $cc =~ /^\S+\@\S+>/) ||
3888 ($op eq '>' &&
3889 $ca =~ /<\S+\@\S+$/))
3890 {
3891 $ok = 1;
3892 }
3893
Joe Perchese0df7e12015-04-16 12:44:53 -07003894 # for asm volatile statements
3895 # ignore a colon with another
3896 # colon immediately before or after
3897 if (($op eq ':') &&
3898 ($ca =~ /:$/ || $cc =~ /^:/)) {
3899 $ok = 1;
3900 }
3901
Joe Perches84731622013-11-12 15:10:05 -08003902 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003903 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003904 my $msg_type = \&ERROR;
3905 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3906
3907 if (&{$msg_type}("SPACING",
3908 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003909 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3910 if (defined $fix_elements[$n + 2]) {
3911 $fix_elements[$n + 2] =~ s/^\s+//;
3912 }
Joe Perches3705ce52013-07-03 15:05:31 -07003913 $line_fixed = 1;
3914 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003915 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003916 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003917 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003918
3919## print("n: <$n> GOOD: <$good>\n");
3920
3921 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003922 }
Joe Perches3705ce52013-07-03 15:05:31 -07003923
3924 if (($#elements % 2) == 0) {
3925 $fixed_line = $fixed_line . $fix_elements[$#elements];
3926 }
3927
Joe Perches194f66f2014-08-06 16:11:03 -07003928 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3929 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003930 }
3931
3932
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003933 }
3934
Joe Perches786b6322013-07-03 15:05:32 -07003935# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003936 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003937 if (WARN("SPACING",
3938 "space prohibited before semicolon\n" . $herecurr) &&
3939 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003940 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003941 s/^(\+.*\S)\s+;/$1;/;
3942 }
3943 }
3944
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003945# check for multiple assignments
3946 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003947 CHK("MULTIPLE_ASSIGNMENTS",
3948 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003949 }
3950
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003951## # check for multiple declarations, allowing for a function declaration
3952## # continuation.
3953## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3954## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3955##
3956## # Remove any bracketed sections to ensure we do not
3957## # falsly report the parameters of functions.
3958## my $ln = $line;
3959## while ($ln =~ s/\([^\(\)]*\)//g) {
3960## }
3961## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003962## WARN("MULTIPLE_DECLARATION",
3963## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003964## }
3965## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003966
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003967#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003968 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3969 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003970 if (ERROR("SPACING",
3971 "space required before the open brace '{'\n" . $herecurr) &&
3972 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003973 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003974 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003975 }
3976
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003977## # check for blank lines before declarations
3978## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3979## $prevrawline =~ /^.\s*$/) {
3980## WARN("SPACING",
3981## "No blank lines before declarations\n" . $hereprev);
3982## }
3983##
3984
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003985# closing brace should have a space following it when it has anything
3986# on the line
3987 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003988 if (ERROR("SPACING",
3989 "space required after that close brace '}'\n" . $herecurr) &&
3990 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003991 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003992 s/}((?!(?:,|;|\)))\S)/} $1/;
3993 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003994 }
3995
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003996# check spacing on square brackets
3997 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003998 if (ERROR("SPACING",
3999 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4000 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004001 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004002 s/\[\s+/\[/;
4003 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004004 }
4005 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004006 if (ERROR("SPACING",
4007 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4008 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004009 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004010 s/\s+\]/\]/;
4011 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004012 }
4013
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004014# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004015 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4016 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004017 if (ERROR("SPACING",
4018 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4019 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004020 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004021 s/\(\s+/\(/;
4022 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004023 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004024 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004025 $line !~ /for\s*\(.*;\s+\)/ &&
4026 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004027 if (ERROR("SPACING",
4028 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4029 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004030 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004031 s/\s+\)/\)/;
4032 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004033 }
4034
Joe Perchese2826fd2014-08-06 16:10:48 -07004035# check unnecessary parentheses around addressof/dereference single $Lvals
4036# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4037
4038 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004039 my $var = $1;
4040 if (CHK("UNNECESSARY_PARENTHESES",
4041 "Unnecessary parentheses around $var\n" . $herecurr) &&
4042 $fix) {
4043 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4044 }
4045 }
4046
4047# check for unnecessary parentheses around function pointer uses
4048# ie: (foo->bar)(); should be foo->bar();
4049# but not "if (foo->bar) (" to avoid some false positives
4050 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4051 my $var = $2;
4052 if (CHK("UNNECESSARY_PARENTHESES",
4053 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4054 $fix) {
4055 my $var2 = deparenthesize($var);
4056 $var2 =~ s/\s//g;
4057 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4058 }
4059 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004060
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004061#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004062 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004063 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004064 if (WARN("INDENTED_LABEL",
4065 "labels should not be indented\n" . $herecurr) &&
4066 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004067 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004068 s/^(.)\s+/$1/;
4069 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004070 }
4071
Joe Perches5b9553a2014-04-03 14:49:21 -07004072# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004073 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004074 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004075 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004076 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4077 my $value = $1;
4078 $value = deparenthesize($value);
4079 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4080 ERROR("RETURN_PARENTHESES",
4081 "return is not a function, parentheses are not required\n" . $herecurr);
4082 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004083 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004084 ERROR("SPACING",
4085 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004086 }
4087 }
Joe Perches507e5142013-11-12 15:10:13 -08004088
Joe Perchesb43ae212014-06-23 13:22:07 -07004089# unnecessary return in a void function
4090# at end-of-function, with the previous line a single leading tab, then return;
4091# and the line before that not a goto label target like "out:"
4092 if ($sline =~ /^[ \+]}\s*$/ &&
4093 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4094 $linenr >= 3 &&
4095 $lines[$linenr - 3] =~ /^[ +]/ &&
4096 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004097 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004098 "void function return statements are not generally useful\n" . $hereprev);
4099 }
Joe Perches9819cf22014-06-04 16:12:09 -07004100
Joe Perches189248d2014-01-23 15:54:47 -08004101# if statements using unnecessary parentheses - ie: if ((foo == bar))
4102 if ($^V && $^V ge 5.10.0 &&
4103 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4104 my $openparens = $1;
4105 my $count = $openparens =~ tr@\(@\(@;
4106 my $msg = "";
4107 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4108 my $comp = $4; #Not $1 because of $LvalOrFunc
4109 $msg = " - maybe == should be = ?" if ($comp eq "==");
4110 WARN("UNNECESSARY_PARENTHESES",
4111 "Unnecessary parentheses$msg\n" . $herecurr);
4112 }
4113 }
4114
Joe Perchesf34e4a42015-04-16 12:44:19 -07004115# Return of what appears to be an errno should normally be negative
4116 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004117 my $name = $1;
4118 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004119 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004120 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004121 }
4122 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004123
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004124# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004125 if ($line =~ /\b(if|while|for|switch)\(/) {
4126 if (ERROR("SPACING",
4127 "space required before the open parenthesis '('\n" . $herecurr) &&
4128 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004129 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004130 s/\b(if|while|for|switch)\(/$1 \(/;
4131 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004132 }
4133
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004134# Check for illegal assignment in if conditional -- and check for trailing
4135# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004136 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004137 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4138 ctx_statement_block($linenr, $realcnt, 0)
4139 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004140 my ($stat_next) = ctx_statement_block($line_nr_next,
4141 $remain_next, $off_next);
4142 $stat_next =~ s/\n./\n /g;
4143 ##print "stat<$stat> stat_next<$stat_next>\n";
4144
4145 if ($stat_next =~ /^\s*while\b/) {
4146 # If the statement carries leading newlines,
4147 # then count those as offsets.
4148 my ($whitespace) =
4149 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4150 my $offset =
4151 statement_rawlines($whitespace) - 1;
4152
4153 $suppress_whiletrailers{$line_nr_next +
4154 $offset} = 1;
4155 }
4156 }
4157 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004158 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004159 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004160 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004161
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004162 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004163 ERROR("ASSIGN_IN_IF",
4164 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004165 }
4166
4167 # Find out what is on the end of the line after the
4168 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004169 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004170 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004171 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004172 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4173 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004174 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004175 # Find out how long the conditional actually is.
4176 my @newlines = ($c =~ /\n/gs);
4177 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004178 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004179
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004180 $stat_real = raw_line($linenr, $cond_lines)
4181 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004182 if (defined($stat_real) && $cond_lines > 1) {
4183 $stat_real = "[...]\n$stat_real";
4184 }
4185
Joe Perches000d1cc12011-07-25 17:13:25 -07004186 ERROR("TRAILING_STATEMENTS",
4187 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004188 }
4189 }
4190
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004191# Check for bitwise tests written as boolean
4192 if ($line =~ /
4193 (?:
4194 (?:\[|\(|\&\&|\|\|)
4195 \s*0[xX][0-9]+\s*
4196 (?:\&\&|\|\|)
4197 |
4198 (?:\&\&|\|\|)
4199 \s*0[xX][0-9]+\s*
4200 (?:\&\&|\|\||\)|\])
4201 )/x)
4202 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004203 WARN("HEXADECIMAL_BOOLEAN_TEST",
4204 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004205 }
4206
Andy Whitcroft8905a672007-11-28 16:21:06 -08004207# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004208 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4209 my $s = $1;
4210 $s =~ s/$;//g; # Remove any comments
4211 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004212 ERROR("TRAILING_STATEMENTS",
4213 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004214 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004215 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004216# if should not continue a brace
4217 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004218 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004219 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004220 $herecurr);
4221 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004222# case and default should not have general statements after them
4223 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4224 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004225 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004226 \s*return\s+
4227 )/xg)
4228 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004229 ERROR("TRAILING_STATEMENTS",
4230 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004231 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004232
4233 # Check for }<nl>else {, these must be at the same
4234 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004235 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4236 $previndent == $indent) {
4237 if (ERROR("ELSE_AFTER_BRACE",
4238 "else should follow close brace '}'\n" . $hereprev) &&
4239 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4240 fix_delete_line($fixlinenr - 1, $prevrawline);
4241 fix_delete_line($fixlinenr, $rawline);
4242 my $fixedline = $prevrawline;
4243 $fixedline =~ s/}\s*$//;
4244 if ($fixedline !~ /^\+\s*$/) {
4245 fix_insert_line($fixlinenr, $fixedline);
4246 }
4247 $fixedline = $rawline;
4248 $fixedline =~ s/^(.\s*)else/$1} else/;
4249 fix_insert_line($fixlinenr, $fixedline);
4250 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004251 }
4252
Joe Perches8b8856f2014-08-06 16:11:14 -07004253 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4254 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004255 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4256
4257 # Find out what is on the end of the line after the
4258 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004259 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004260 $s =~ s/\n.*//g;
4261
4262 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004263 if (ERROR("WHILE_AFTER_BRACE",
4264 "while should follow close brace '}'\n" . $hereprev) &&
4265 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4266 fix_delete_line($fixlinenr - 1, $prevrawline);
4267 fix_delete_line($fixlinenr, $rawline);
4268 my $fixedline = $prevrawline;
4269 my $trailing = $rawline;
4270 $trailing =~ s/^\+//;
4271 $trailing = trim($trailing);
4272 $fixedline =~ s/}\s*$/} $trailing/;
4273 fix_insert_line($fixlinenr, $fixedline);
4274 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004275 }
4276 }
4277
Joe Perches95e2c602013-07-03 15:05:20 -07004278#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004279 while ($line =~ m{($Constant|$Lval)}g) {
4280 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004281
4282#gcc binary extension
4283 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004284 if (WARN("GCC_BINARY_CONSTANT",
4285 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4286 $fix) {
4287 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004288 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004289 s/\b$var\b/$hexval/;
4290 }
Joe Perches95e2c602013-07-03 15:05:20 -07004291 }
4292
4293#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004294 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004295 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004296#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004297 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004298#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004299 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4300#Ignore some three character SI units explicitly, like MiB and KHz
4301 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004302 while ($var =~ m{($Ident)}g) {
4303 my $word = $1;
4304 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004305 if ($check) {
4306 seed_camelcase_includes();
4307 if (!$file && !$camelcase_file_seeded) {
4308 seed_camelcase_file($realfile);
4309 $camelcase_file_seeded = 1;
4310 }
4311 }
Joe Perches7e781f62013-09-11 14:23:55 -07004312 if (!defined $camelcase{$word}) {
4313 $camelcase{$word} = 1;
4314 CHK("CAMELCASE",
4315 "Avoid CamelCase: <$word>\n" . $herecurr);
4316 }
Joe Perches34456862013-07-03 15:05:34 -07004317 }
Joe Perches323c1262012-12-17 16:02:07 -08004318 }
4319 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004320
4321#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004322 if ($line =~ /\#\s*define.*\\\s+$/) {
4323 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4324 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4325 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004326 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004327 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004328 }
4329
Fabian Frederick0e212e02015-04-16 12:44:25 -07004330# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4331# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004332 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004333 my $file = "$1.h";
4334 my $checkfile = "include/linux/$file";
4335 if (-f "$root/$checkfile" &&
4336 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004337 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004338 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004339 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4340 if ($asminclude > 0) {
4341 if ($realfile =~ m{^arch/}) {
4342 CHK("ARCH_INCLUDE_LINUX",
4343 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4344 } else {
4345 WARN("INCLUDE_LINUX",
4346 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4347 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004348 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004349 }
4350 }
4351
Andy Whitcroft653d4872007-06-23 17:16:34 -07004352# multi-statement macros should be enclosed in a do while loop, grab the
4353# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004354# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004355 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4356 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004357 my $ln = $linenr;
4358 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004359 my ($off, $dstat, $dcond, $rest);
4360 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004361 my $has_flow_statement = 0;
4362 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004363 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004364 ctx_statement_block($linenr, $realcnt, 0);
4365 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004366 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004367 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004368
Joe Perches08a28432014-10-13 15:51:55 -07004369 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4370 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4371
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004372 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004373 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004374 $dstat =~ s/\\\n.//g;
4375 $dstat =~ s/^\s*//s;
4376 $dstat =~ s/\s*$//s;
4377
4378 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004379 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4380 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004381 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004382 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004383 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004384
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004385 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004386 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4387 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004388 {
4389 }
4390
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004391 my $exceptions = qr{
4392 $Declare|
4393 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004394 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004395 DECLARE_PER_CPU|
4396 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004397 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004398 union|
4399 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004400 \.$Ident\s*=\s*|
4401 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004402 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004403 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004404 if ($dstat ne '' &&
4405 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4406 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004407 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004408 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004409 $dstat !~ /$exceptions/ &&
4410 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004411 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004412 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004413 $dstat !~ /^for\s*$Constant$/ && # for (...)
4414 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4415 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004416 $dstat !~ /^\({/ && # ({...
4417 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004418 {
4419 $ctx =~ s/\n*$//;
4420 my $herectx = $here . "\n";
4421 my $cnt = statement_rawlines($ctx);
4422
4423 for (my $n = 0; $n < $cnt; $n++) {
4424 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004425 }
4426
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004427 if ($dstat =~ /;/) {
4428 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4429 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4430 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004431 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004432 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004433 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004434 }
Joe Perches5023d342012-12-17 16:01:47 -08004435
Joe Perches08a28432014-10-13 15:51:55 -07004436# check for macros with flow control, but without ## concatenation
4437# ## concatenation is commonly a macro that defines a function so ignore those
4438 if ($has_flow_statement && !$has_arg_concat) {
4439 my $herectx = $here . "\n";
4440 my $cnt = statement_rawlines($ctx);
4441
4442 for (my $n = 0; $n < $cnt; $n++) {
4443 $herectx .= raw_line($linenr, $n) . "\n";
4444 }
4445 WARN("MACRO_WITH_FLOW_CONTROL",
4446 "Macros with flow control statements should be avoided\n" . "$herectx");
4447 }
4448
Joe Perches481eb482012-12-17 16:01:56 -08004449# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004450
4451 } else {
4452 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004453 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4454 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004455 $line =~ /^\+.*\\$/) {
4456 WARN("LINE_CONTINUATIONS",
4457 "Avoid unnecessary line continuations\n" . $herecurr);
4458 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004459 }
4460
Joe Perchesb13edf72012-07-30 14:41:24 -07004461# do {} while (0) macro tests:
4462# single-statement macros do not need to be enclosed in do while (0) loop,
4463# macro should not end with a semicolon
4464 if ($^V && $^V ge 5.10.0 &&
4465 $realfile !~ m@/vmlinux.lds.h$@ &&
4466 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4467 my $ln = $linenr;
4468 my $cnt = $realcnt;
4469 my ($off, $dstat, $dcond, $rest);
4470 my $ctx = '';
4471 ($dstat, $dcond, $ln, $cnt, $off) =
4472 ctx_statement_block($linenr, $realcnt, 0);
4473 $ctx = $dstat;
4474
4475 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004476 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004477
4478 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4479 my $stmts = $2;
4480 my $semis = $3;
4481
4482 $ctx =~ s/\n*$//;
4483 my $cnt = statement_rawlines($ctx);
4484 my $herectx = $here . "\n";
4485
4486 for (my $n = 0; $n < $cnt; $n++) {
4487 $herectx .= raw_line($linenr, $n) . "\n";
4488 }
4489
Joe Perchesac8e97f2012-08-21 16:15:53 -07004490 if (($stmts =~ tr/;/;/) == 1 &&
4491 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004492 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4493 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4494 }
4495 if (defined $semis && $semis ne "") {
4496 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4497 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4498 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004499 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4500 $ctx =~ s/\n*$//;
4501 my $cnt = statement_rawlines($ctx);
4502 my $herectx = $here . "\n";
4503
4504 for (my $n = 0; $n < $cnt; $n++) {
4505 $herectx .= raw_line($linenr, $n) . "\n";
4506 }
4507
4508 WARN("TRAILING_SEMICOLON",
4509 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004510 }
4511 }
4512
Mike Frysinger080ba922009-01-06 14:41:25 -08004513# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4514# all assignments may have only one of the following with an assignment:
4515# .
4516# ALIGN(...)
4517# VMLINUX_SYMBOL(...)
4518 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004519 WARN("MISSING_VMLINUX_SYMBOL",
4520 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004521 }
4522
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004523# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004524 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4525 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004526 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004527 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004528 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4529 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004530 my @allowed = ();
4531 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004532 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004533 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004534 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004535 for my $chunk (@chunks) {
4536 my ($cond, $block) = @{$chunk};
4537
Andy Whitcroft773647a2008-03-28 14:15:58 -07004538 # If the condition carries leading newlines, then count those as offsets.
4539 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4540 my $offset = statement_rawlines($whitespace) - 1;
4541
Joe Perchesaad4f612012-03-23 15:02:19 -07004542 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004543 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4544
4545 # We have looked at and allowed this specific line.
4546 $suppress_ifbraces{$ln + $offset} = 1;
4547
4548 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004549 $ln += statement_rawlines($block) - 1;
4550
Andy Whitcroft773647a2008-03-28 14:15:58 -07004551 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004552
4553 $seen++ if ($block =~ /^\s*{/);
4554
Joe Perchesaad4f612012-03-23 15:02:19 -07004555 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004556 if (statement_lines($cond) > 1) {
4557 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004558 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004559 }
4560 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004561 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004562 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004563 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004564 if (statement_block_size($block) > 1) {
4565 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004566 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004567 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004568 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004569 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004570 if ($seen) {
4571 my $sum_allowed = 0;
4572 foreach (@allowed) {
4573 $sum_allowed += $_;
4574 }
4575 if ($sum_allowed == 0) {
4576 WARN("BRACES",
4577 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4578 } elsif ($sum_allowed != $allow &&
4579 $seen != $allow) {
4580 CHK("BRACES",
4581 "braces {} should be used on all arms of this statement\n" . $herectx);
4582 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004583 }
4584 }
4585 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004586 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004587 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004588 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004589
Andy Whitcroftcf655042008-03-04 14:28:20 -08004590 # Check the pre-context.
4591 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4592 #print "APW: ALLOWED: pre<$1>\n";
4593 $allowed = 1;
4594 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004595
4596 my ($level, $endln, @chunks) =
4597 ctx_statement_full($linenr, $realcnt, $-[0]);
4598
Andy Whitcroftcf655042008-03-04 14:28:20 -08004599 # Check the condition.
4600 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004601 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004602 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004603 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004604 }
4605 if (statement_lines($cond) > 1) {
4606 #print "APW: ALLOWED: cond<$cond>\n";
4607 $allowed = 1;
4608 }
4609 if ($block =~/\b(?:if|for|while)\b/) {
4610 #print "APW: ALLOWED: block<$block>\n";
4611 $allowed = 1;
4612 }
4613 if (statement_block_size($block) > 1) {
4614 #print "APW: ALLOWED: lines block<$block>\n";
4615 $allowed = 1;
4616 }
4617 # Check the post-context.
4618 if (defined $chunks[1]) {
4619 my ($cond, $block) = @{$chunks[1]};
4620 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004621 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004622 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004623 if ($block =~ /^\s*\{/) {
4624 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4625 $allowed = 1;
4626 }
4627 }
4628 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004629 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004630 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004631
Andy Whitcroftf0556632008-10-15 22:02:23 -07004632 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004633 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004634 }
4635
Joe Perches000d1cc12011-07-25 17:13:25 -07004636 WARN("BRACES",
4637 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004638 }
4639 }
4640
Joe Perches0979ae62012-12-17 16:01:59 -08004641# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004642 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004643 if (CHK("BRACES",
4644 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4645 $fix && $prevrawline =~ /^\+/) {
4646 fix_delete_line($fixlinenr - 1, $prevrawline);
4647 }
Joe Perches0979ae62012-12-17 16:01:59 -08004648 }
Joe Perches77b9a532013-07-03 15:05:29 -07004649 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004650 if (CHK("BRACES",
4651 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4652 $fix) {
4653 fix_delete_line($fixlinenr, $rawline);
4654 }
Joe Perches0979ae62012-12-17 16:01:59 -08004655 }
4656
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004657# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004658 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4659 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004660 WARN("VOLATILE",
4661 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004662 }
4663
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004664# Check for user-visible strings broken across lines, which breaks the ability
4665# to grep for the string. Make exceptions when the previous string ends in a
4666# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4667# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07004668 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004669 $prevline =~ /"\s*$/ &&
4670 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4671 if (WARN("SPLIT_STRING",
4672 "quoted string split across lines\n" . $hereprev) &&
4673 $fix &&
4674 $prevrawline =~ /^\+.*"\s*$/ &&
4675 $last_coalesced_string_linenr != $linenr - 1) {
4676 my $extracted_string = get_quoted_string($line, $rawline);
4677 my $comma_close = "";
4678 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4679 $comma_close = $1;
4680 }
4681
4682 fix_delete_line($fixlinenr - 1, $prevrawline);
4683 fix_delete_line($fixlinenr, $rawline);
4684 my $fixedline = $prevrawline;
4685 $fixedline =~ s/"\s*$//;
4686 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4687 fix_insert_line($fixlinenr - 1, $fixedline);
4688 $fixedline = $rawline;
4689 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4690 if ($fixedline !~ /\+\s*$/) {
4691 fix_insert_line($fixlinenr, $fixedline);
4692 }
4693 $last_coalesced_string_linenr = $linenr;
4694 }
4695 }
4696
4697# check for missing a space in a string concatenation
4698 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4699 WARN('MISSING_SPACE',
4700 "break quoted strings at a space character\n" . $hereprev);
4701 }
4702
4703# check for spaces before a quoted newline
4704 if ($rawline =~ /^.*\".*\s\\n/) {
4705 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4706 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4707 $fix) {
4708 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4709 }
4710
4711 }
4712
Joe Perchesf17dba42014-10-13 15:51:51 -07004713# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07004714 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07004715 CHK("CONCATENATED_STRING",
4716 "Concatenated strings should use spaces between elements\n" . $herecurr);
4717 }
4718
Joe Perches90ad30e2014-12-10 15:51:59 -08004719# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07004720 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08004721 WARN("STRING_FRAGMENTS",
4722 "Consecutive strings are generally better as a single string\n" . $herecurr);
4723 }
4724
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004725# check for %L{u,d,i} in strings
4726 my $string;
4727 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4728 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4729 $string =~ s/%%/__/g;
4730 if ($string =~ /(?<!%)%L[udi]/) {
4731 WARN("PRINTF_L",
4732 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4733 last;
4734 }
4735 }
4736
4737# check for line continuations in quoted strings with odd counts of "
4738 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4739 WARN("LINE_CONTINUATIONS",
4740 "Avoid line continuations in quoted strings\n" . $herecurr);
4741 }
4742
Andy Whitcroft00df3442007-06-08 13:47:06 -07004743# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004744 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004745 CHK("REDUNDANT_CODE",
4746 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004747 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004748 }
4749
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004750# check for needless "if (<foo>) fn(<foo>)" uses
4751 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4752 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4753 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4754 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004755 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004756 }
4757 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004758
Joe Perchesebfdc402014-08-06 16:10:27 -07004759# check for unnecessary "Out of Memory" messages
4760 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4761 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4762 (defined $1 || defined $3) &&
4763 $linenr > 3) {
4764 my $testval = $2;
4765 my $testline = $lines[$linenr - 3];
4766
4767 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4768# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4769
4770 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4771 WARN("OOM_MESSAGE",
4772 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4773 }
4774 }
4775
Joe Perchesf78d98f2014-10-13 15:52:01 -07004776# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004777 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004778 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4779 my $level = $1;
4780 if (WARN("UNNECESSARY_KERN_LEVEL",
4781 "Possible unnecessary $level\n" . $herecurr) &&
4782 $fix) {
4783 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4784 }
4785 }
4786
Joe Perchesabb08a52014-12-10 15:51:46 -08004787# check for mask then right shift without a parentheses
4788 if ($^V && $^V ge 5.10.0 &&
4789 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4790 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4791 WARN("MASK_THEN_SHIFT",
4792 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4793 }
4794
Joe Perchesb75ac612014-12-10 15:52:02 -08004795# check for pointer comparisons to NULL
4796 if ($^V && $^V ge 5.10.0) {
4797 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4798 my $val = $1;
4799 my $equal = "!";
4800 $equal = "" if ($4 eq "!=");
4801 if (CHK("COMPARISON_TO_NULL",
4802 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4803 $fix) {
4804 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4805 }
4806 }
4807 }
4808
Joe Perches8716de32013-09-11 14:24:05 -07004809# check for bad placement of section $InitAttribute (e.g.: __initdata)
4810 if ($line =~ /(\b$InitAttribute\b)/) {
4811 my $attr = $1;
4812 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4813 my $ptr = $1;
4814 my $var = $2;
4815 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4816 ERROR("MISPLACED_INIT",
4817 "$attr should be placed after $var\n" . $herecurr)) ||
4818 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4819 WARN("MISPLACED_INIT",
4820 "$attr should be placed after $var\n" . $herecurr))) &&
4821 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004822 $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 -07004823 }
4824 }
4825 }
4826
Joe Perchese970b8842013-11-12 15:10:10 -08004827# check for $InitAttributeData (ie: __initdata) with const
4828 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4829 my $attr = $1;
4830 $attr =~ /($InitAttributePrefix)(.*)/;
4831 my $attr_prefix = $1;
4832 my $attr_type = $2;
4833 if (ERROR("INIT_ATTRIBUTE",
4834 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4835 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004836 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004837 s/$InitAttributeData/${attr_prefix}initconst/;
4838 }
4839 }
4840
4841# check for $InitAttributeConst (ie: __initconst) without const
4842 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4843 my $attr = $1;
4844 if (ERROR("INIT_ATTRIBUTE",
4845 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4846 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004847 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004848 /(^\+\s*(?:static\s+))/;
4849 $lead = rtrim($1);
4850 $lead = "$lead " if ($lead !~ /^\+$/);
4851 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004852 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004853 }
4854 }
4855
Joe Perchesc17893c2015-04-16 12:44:42 -07004856# check for __read_mostly with const non-pointer (should just be const)
4857 if ($line =~ /\b__read_mostly\b/ &&
4858 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
4859 if (ERROR("CONST_READ_MOSTLY",
4860 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
4861 $fix) {
4862 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
4863 }
4864 }
4865
Joe Perchesfbdb8132014-04-03 14:49:14 -07004866# don't use __constant_<foo> functions outside of include/uapi/
4867 if ($realfile !~ m@^include/uapi/@ &&
4868 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4869 my $constant_func = $1;
4870 my $func = $constant_func;
4871 $func =~ s/^__constant_//;
4872 if (WARN("CONSTANT_CONVERSION",
4873 "$constant_func should be $func\n" . $herecurr) &&
4874 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004875 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004876 }
4877 }
4878
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004879# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004880 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004881 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004882 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004883 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004884 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004885 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4886 }
4887 if ($delay > 2000) {
4888 WARN("LONG_UDELAY",
4889 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004890 }
4891 }
4892
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004893# warn about unexpectedly long msleep's
4894 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4895 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004896 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004897 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004898 }
4899 }
4900
Joe Perches36ec1932013-07-03 15:05:25 -07004901# check for comparisons of jiffies
4902 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4903 WARN("JIFFIES_COMPARISON",
4904 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4905 }
4906
Joe Perches9d7a34a2013-07-03 15:05:26 -07004907# check for comparisons of get_jiffies_64()
4908 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4909 WARN("JIFFIES_COMPARISON",
4910 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4911 }
4912
Andy Whitcroft00df3442007-06-08 13:47:06 -07004913# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004914# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004915# print "#ifdef in C files should be avoided\n";
4916# print "$herecurr";
4917# $clean = 0;
4918# }
4919
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004920# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004921 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004922 if (ERROR("SPACING",
4923 "exactly one space required after that #$1\n" . $herecurr) &&
4924 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004925 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004926 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4927 }
4928
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004929 }
4930
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004931# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004932 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4933 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004934 my $which = $1;
4935 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004936 CHK("UNCOMMENTED_DEFINITION",
4937 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004938 }
4939 }
4940# check for memory barriers without a comment.
4941 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4942 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004943 WARN("MEMORY_BARRIER",
4944 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004945 }
4946 }
Joe Perchescb426e92015-06-25 15:02:46 -07004947# check for waitqueue_active without a comment.
4948 if ($line =~ /\bwaitqueue_active\s*\(/) {
4949 if (!ctx_has_comment($first_line, $linenr)) {
4950 WARN("WAITQUEUE_ACTIVE",
4951 "waitqueue_active without comment\n" . $herecurr);
4952 }
4953 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004954# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004955 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004956 CHK("ARCH_DEFINES",
4957 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004958 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004959
Tobias Klauserd4977c72010-05-24 14:33:30 -07004960# Check that the storage class is at the beginning of a declaration
4961 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004962 WARN("STORAGE_CLASS",
4963 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004964 }
4965
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004966# check the location of the inline attribute, that it is between
4967# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004968 if ($line =~ /\b$Type\s+$Inline\b/ ||
4969 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004970 ERROR("INLINE_LOCATION",
4971 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004972 }
4973
Andy Whitcroft8905a672007-11-28 16:21:06 -08004974# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004975 if ($realfile !~ m@\binclude/uapi/@ &&
4976 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004977 if (WARN("INLINE",
4978 "plain inline is preferred over $1\n" . $herecurr) &&
4979 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004980 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004981
4982 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004983 }
4984
Joe Perches3d130fd2011-01-12 17:00:00 -08004985# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004986 if ($realfile !~ m@\binclude/uapi/@ &&
4987 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004988 WARN("PREFER_PACKED",
4989 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004990 }
4991
Joe Perches39b7e282011-07-25 17:13:24 -07004992# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004993 if ($realfile !~ m@\binclude/uapi/@ &&
4994 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004995 WARN("PREFER_ALIGNED",
4996 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004997 }
4998
Joe Perches5f14d3b2012-01-10 15:09:52 -08004999# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005000 if ($realfile !~ m@\binclude/uapi/@ &&
5001 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005002 if (WARN("PREFER_PRINTF",
5003 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5004 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005005 $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 -07005006
5007 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005008 }
5009
Joe Perches6061d942012-03-23 15:02:16 -07005010# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005011 if ($realfile !~ m@\binclude/uapi/@ &&
5012 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005013 if (WARN("PREFER_SCANF",
5014 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5015 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005016 $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 -07005017 }
Joe Perches6061d942012-03-23 15:02:16 -07005018 }
5019
Joe Perches619a9082014-12-10 15:51:35 -08005020# Check for __attribute__ weak, or __weak declarations (may have link issues)
5021 if ($^V && $^V ge 5.10.0 &&
5022 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5023 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5024 $line =~ /\b__weak\b/)) {
5025 ERROR("WEAK_DECLARATION",
5026 "Using weak declarations can have unintended link defects\n" . $herecurr);
5027 }
5028
Joe Perchese6176fa2015-06-25 15:02:49 -07005029# check for c99 types like uint8_t used outside of uapi/
5030 if ($realfile !~ m@\binclude/uapi/@ &&
5031 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5032 my $type = $1;
5033 if ($type =~ /\b($typeC99Typedefs)\b/) {
5034 $type = $1;
5035 my $kernel_type = 'u';
5036 $kernel_type = 's' if ($type =~ /^_*[si]/);
5037 $type =~ /(\d+)/;
5038 $kernel_type .= $1;
5039 if (CHK("PREFER_KERNEL_TYPES",
5040 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5041 $fix) {
5042 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5043 }
5044 }
5045 }
5046
Joe Perches8f53a9b2010-03-05 13:43:48 -08005047# check for sizeof(&)
5048 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005049 WARN("SIZEOF_ADDRESS",
5050 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005051 }
5052
Joe Perches66c80b62012-07-30 14:41:22 -07005053# check for sizeof without parenthesis
5054 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005055 if (WARN("SIZEOF_PARENTHESIS",
5056 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5057 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005058 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005059 }
Joe Perches66c80b62012-07-30 14:41:22 -07005060 }
5061
Joe Perches88982fe2012-12-17 16:02:00 -08005062# check for struct spinlock declarations
5063 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5064 WARN("USE_SPINLOCK_T",
5065 "struct spinlock should be spinlock_t\n" . $herecurr);
5066 }
5067
Joe Perchesa6962d72013-04-29 16:18:13 -07005068# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005069 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005070 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005071 $fmt =~ s/%%//g;
5072 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005073 if (WARN("PREFER_SEQ_PUTS",
5074 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5075 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005076 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005077 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005078 }
5079 }
5080
Andy Whitcroft554e1652012-01-10 15:09:57 -08005081# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005082 if ($^V && $^V ge 5.10.0 &&
5083 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005084 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005085
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005086 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005087 my $ms_val = $7;
5088 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005089
Andy Whitcroft554e1652012-01-10 15:09:57 -08005090 if ($ms_size =~ /^(0x|)0$/i) {
5091 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005092 "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 -08005093 } elsif ($ms_size =~ /^(0x|)1$/i) {
5094 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005095 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5096 }
5097 }
5098
Joe Perches98a9bba2014-01-23 15:54:52 -08005099# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5100 if ($^V && $^V ge 5.10.0 &&
5101 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
5102 if (WARN("PREFER_ETHER_ADDR_COPY",
5103 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
5104 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005105 $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 -08005106 }
5107 }
5108
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005109# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005110 if ($^V && $^V ge 5.10.0 &&
5111 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005112 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005113 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005114 my $call = $1;
5115 my $cast1 = deparenthesize($2);
5116 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005117 my $cast2 = deparenthesize($7);
5118 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005119 my $cast;
5120
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005121 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005122 $cast = "$cast1 or $cast2";
5123 } elsif ($cast1 ne "") {
5124 $cast = $cast1;
5125 } else {
5126 $cast = $cast2;
5127 }
5128 WARN("MINMAX",
5129 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005130 }
5131 }
5132
Joe Perches4a273192012-07-30 14:41:20 -07005133# check usleep_range arguments
5134 if ($^V && $^V ge 5.10.0 &&
5135 defined $stat &&
5136 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5137 my $min = $1;
5138 my $max = $7;
5139 if ($min eq $max) {
5140 WARN("USLEEP_RANGE",
5141 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5142 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5143 $min > $max) {
5144 WARN("USLEEP_RANGE",
5145 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5146 }
5147 }
5148
Joe Perches823b7942013-11-12 15:10:15 -08005149# check for naked sscanf
5150 if ($^V && $^V ge 5.10.0 &&
5151 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005152 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005153 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5154 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5155 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5156 my $lc = $stat =~ tr@\n@@;
5157 $lc = $lc + $linenr;
5158 my $stat_real = raw_line($linenr, 0);
5159 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5160 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5161 }
5162 WARN("NAKED_SSCANF",
5163 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5164 }
5165
Joe Perchesafc819a2014-06-04 16:12:08 -07005166# check for simple sscanf that should be kstrto<foo>
5167 if ($^V && $^V ge 5.10.0 &&
5168 defined $stat &&
5169 $line =~ /\bsscanf\b/) {
5170 my $lc = $stat =~ tr@\n@@;
5171 $lc = $lc + $linenr;
5172 my $stat_real = raw_line($linenr, 0);
5173 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5174 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5175 }
5176 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5177 my $format = $6;
5178 my $count = $format =~ tr@%@%@;
5179 if ($count == 1 &&
5180 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5181 WARN("SSCANF_TO_KSTRTO",
5182 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5183 }
5184 }
5185 }
5186
Joe Perches70dc8a42013-09-11 14:23:58 -07005187# check for new externs in .h files.
5188 if ($realfile =~ /\.h$/ &&
5189 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005190 if (CHK("AVOID_EXTERNS",
5191 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005192 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005193 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005194 }
5195 }
5196
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005197# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005198 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005199 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005200 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005201 my $function_name = $1;
5202 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005203
5204 my $s = $stat;
5205 if (defined $cond) {
5206 substr($s, 0, length($cond), '');
5207 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005208 if ($s =~ /^\s*;/ &&
5209 $function_name ne 'uninitialized_var')
5210 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005211 WARN("AVOID_EXTERNS",
5212 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005213 }
5214
5215 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005216 WARN("FUNCTION_ARGUMENTS",
5217 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005218 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005219
5220 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5221 $stat =~ /^.\s*extern\s+/)
5222 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005223 WARN("AVOID_EXTERNS",
5224 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005225 }
5226
5227# checks for new __setup's
5228 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5229 my $name = $1;
5230
5231 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005232 CHK("UNDOCUMENTED_SETUP",
5233 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005234 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005235 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005236
5237# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005238 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005239 WARN("UNNECESSARY_CASTS",
5240 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005241 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005242
Joe Perchesa640d252013-07-03 15:05:21 -07005243# alloc style
5244# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5245 if ($^V && $^V ge 5.10.0 &&
5246 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5247 CHK("ALLOC_SIZEOF_STRUCT",
5248 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5249 }
5250
Joe Perches60a55362014-06-04 16:12:07 -07005251# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5252 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005253 $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 -07005254 my $oldfunc = $3;
5255 my $a1 = $4;
5256 my $a2 = $10;
5257 my $newfunc = "kmalloc_array";
5258 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005259 my $r1 = $a1;
5260 my $r2 = $a2;
5261 if ($a1 =~ /^sizeof\s*\S/) {
5262 $r1 = $a2;
5263 $r2 = $a1;
5264 }
5265 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5266 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005267 if (WARN("ALLOC_WITH_MULTIPLY",
5268 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5269 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005270 $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 -07005271
5272 }
5273 }
5274 }
5275
Joe Perches972fdea2013-04-29 16:18:12 -07005276# check for krealloc arg reuse
5277 if ($^V && $^V ge 5.10.0 &&
5278 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5279 WARN("KREALLOC_ARG_REUSE",
5280 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5281 }
5282
Joe Perches5ce59ae2013-02-21 16:44:18 -08005283# check for alloc argument mismatch
5284 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5285 WARN("ALLOC_ARRAY_ARGS",
5286 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5287 }
5288
Joe Perchescaf2a542011-01-12 16:59:56 -08005289# check for multiple semicolons
5290 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005291 if (WARN("ONE_SEMICOLON",
5292 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5293 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005294 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005295 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005296 }
5297
Joe Perches0ab90192014-12-10 15:51:57 -08005298# check for #defines like: 1 << <digit> that could be BIT(digit)
5299 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5300 my $ull = "";
5301 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5302 if (CHK("BIT_MACRO",
5303 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5304 $fix) {
5305 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5306 }
5307 }
5308
Joe Perchese81f2392014-08-06 16:11:25 -07005309# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005310 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5311 my $has_break = 0;
5312 my $has_statement = 0;
5313 my $count = 0;
5314 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005315 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005316 $prevline--;
5317 my $rline = $rawlines[$prevline - 1];
5318 my $fline = $lines[$prevline - 1];
5319 last if ($fline =~ /^\@\@/);
5320 next if ($fline =~ /^\-/);
5321 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5322 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5323 next if ($fline =~ /^.[\s$;]*$/);
5324 $has_statement = 1;
5325 $count++;
5326 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5327 }
5328 if (!$has_break && $has_statement) {
5329 WARN("MISSING_BREAK",
5330 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5331 }
5332 }
5333
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005334# check for switch/default statements without a break;
5335 if ($^V && $^V ge 5.10.0 &&
5336 defined $stat &&
5337 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5338 my $ctx = '';
5339 my $herectx = $here . "\n";
5340 my $cnt = statement_rawlines($stat);
5341 for (my $n = 0; $n < $cnt; $n++) {
5342 $herectx .= raw_line($linenr, $n) . "\n";
5343 }
5344 WARN("DEFAULT_NO_BREAK",
5345 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005346 }
5347
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005348# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005349 if ($line =~ /\b__FUNCTION__\b/) {
5350 if (WARN("USE_FUNC",
5351 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5352 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005353 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005354 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005355 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005356
Joe Perches62ec8182015-02-13 14:38:18 -08005357# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5358 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5359 ERROR("DATE_TIME",
5360 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5361 }
5362
Joe Perches2c924882012-03-23 15:02:20 -07005363# check for use of yield()
5364 if ($line =~ /\byield\s*\(\s*\)/) {
5365 WARN("YIELD",
5366 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5367 }
5368
Joe Perches179f8f42013-07-03 15:05:30 -07005369# check for comparisons against true and false
5370 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5371 my $lead = $1;
5372 my $arg = $2;
5373 my $test = $3;
5374 my $otype = $4;
5375 my $trail = $5;
5376 my $op = "!";
5377
5378 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5379
5380 my $type = lc($otype);
5381 if ($type =~ /^(?:true|false)$/) {
5382 if (("$test" eq "==" && "$type" eq "true") ||
5383 ("$test" eq "!=" && "$type" eq "false")) {
5384 $op = "";
5385 }
5386
5387 CHK("BOOL_COMPARISON",
5388 "Using comparison to $otype is error prone\n" . $herecurr);
5389
5390## maybe suggesting a correct construct would better
5391## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5392
5393 }
5394 }
5395
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005396# check for semaphores initialized locked
5397 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005398 WARN("CONSIDER_COMPLETION",
5399 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005400 }
Joe Perches6712d852012-03-23 15:02:20 -07005401
Joe Perches67d0a072011-10-31 17:13:10 -07005402# recommend kstrto* over simple_strto* and strict_strto*
5403 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005404 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005405 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005406 }
Joe Perches6712d852012-03-23 15:02:20 -07005407
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005408# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005409 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005410 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005411 "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 -07005412 }
Joe Perches6712d852012-03-23 15:02:20 -07005413
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005414# check for various structs that are normally const (ops, kgdb, device_tree)
5415 my $const_structs = qr{
5416 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005417 address_space_operations|
5418 backlight_ops|
5419 block_device_operations|
5420 dentry_operations|
5421 dev_pm_ops|
5422 dma_map_ops|
5423 extent_io_ops|
5424 file_lock_operations|
5425 file_operations|
5426 hv_ops|
5427 ide_dma_ops|
5428 intel_dvo_dev_ops|
5429 item_operations|
5430 iwl_ops|
5431 kgdb_arch|
5432 kgdb_io|
5433 kset_uevent_ops|
5434 lock_manager_operations|
5435 microcode_ops|
5436 mtrr_ops|
5437 neigh_ops|
5438 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005439 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08005440 pci_raw_ops|
5441 pipe_buf_operations|
5442 platform_hibernation_ops|
5443 platform_suspend_ops|
5444 proto_ops|
5445 rpc_pipe_ops|
5446 seq_operations|
5447 snd_ac97_build_ops|
5448 soc_pcmcia_socket_ops|
5449 stacktrace_ops|
5450 sysfs_ops|
5451 tty_operations|
Joe Perches6d07d01b2015-04-16 12:44:33 -07005452 uart_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005453 usb_mon_operations|
5454 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005455 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005456 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005457 WARN("CONST_STRUCT",
5458 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005459 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005460 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005461
5462# use of NR_CPUS is usually wrong
5463# ignore definitions of NR_CPUS and usage to define arrays as likely right
5464 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005465 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5466 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005467 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5468 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5469 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005470 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005471 WARN("NR_CPUS",
5472 "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 -07005473 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005474
Joe Perches52ea8502013-11-12 15:10:09 -08005475# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5476 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5477 ERROR("DEFINE_ARCH_HAS",
5478 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5479 }
5480
Joe Perchesacd93622015-02-13 14:38:38 -08005481# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5482 if ($^V && $^V ge 5.10.0 &&
5483 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5484 WARN("LIKELY_MISUSE",
5485 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5486 }
5487
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005488# whine mightly about in_atomic
5489 if ($line =~ /\bin_atomic\s*\(/) {
5490 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005491 ERROR("IN_ATOMIC",
5492 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005493 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005494 WARN("IN_ATOMIC",
5495 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005496 }
5497 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005498
5499# check for lockdep_set_novalidate_class
5500 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5501 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5502 if ($realfile !~ m@^kernel/lockdep@ &&
5503 $realfile !~ m@^include/linux/lockdep@ &&
5504 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005505 ERROR("LOCKDEP",
5506 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005507 }
5508 }
Dave Jones88f88312011-01-12 16:59:59 -08005509
Joe Perchesb392c642015-04-16 12:44:16 -07005510 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5511 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005512 WARN("EXPORTED_WORLD_WRITABLE",
5513 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005514 }
Joe Perches24358802014-04-03 14:49:13 -07005515
Joe Perches515a2352014-04-03 14:49:24 -07005516# Mode permission misuses where it seems decimal should be octal
5517# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5518 if ($^V && $^V ge 5.10.0 &&
5519 $line =~ /$mode_perms_search/) {
5520 foreach my $entry (@mode_permission_funcs) {
5521 my $func = $entry->[0];
5522 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005523
Joe Perches515a2352014-04-03 14:49:24 -07005524 my $skip_args = "";
5525 if ($arg_pos > 1) {
5526 $arg_pos--;
5527 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5528 }
5529 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5530 if ($line =~ /$test/) {
5531 my $val = $1;
5532 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005533
Joe Perches515a2352014-04-03 14:49:24 -07005534 if ($val !~ /^0$/ &&
5535 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5536 length($val) ne 4)) {
5537 ERROR("NON_OCTAL_PERMISSIONS",
5538 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005539 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5540 ERROR("EXPORTED_WORLD_WRITABLE",
5541 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005542 }
Joe Perches24358802014-04-03 14:49:13 -07005543 }
5544 }
5545 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005546 }
5547
5548 # If we have no input at all, then there is nothing to report on
5549 # so just keep quiet.
5550 if ($#rawlines == -1) {
5551 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005552 }
5553
Andy Whitcroft8905a672007-11-28 16:21:06 -08005554 # In mailback mode only produce a report in the negative, for
5555 # things that appear to be patches.
5556 if ($mailback && ($clean == 1 || !$is_patch)) {
5557 exit(0);
5558 }
5559
5560 # This is not a patch, and we are are in 'no-patch' mode so
5561 # just keep quiet.
5562 if (!$chk_patch && !$is_patch) {
5563 exit(0);
5564 }
5565
5566 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005567 ERROR("NOT_UNIFIED_DIFF",
5568 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005569 }
5570 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005571 ERROR("MISSING_SIGN_OFF",
5572 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005573 }
5574
Andy Whitcroft8905a672007-11-28 16:21:06 -08005575 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005576 if ($summary && !($clean == 1 && $quiet == 1)) {
5577 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005578 print "total: $cnt_error errors, $cnt_warn warnings, " .
5579 (($check)? "$cnt_chk checks, " : "") .
5580 "$cnt_lines lines checked\n";
5581 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005582 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005583
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005584 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005585
5586 if ($^V lt 5.10.0) {
5587 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5588 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5589 }
5590
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005591 # If there were whitespace errors which cleanpatch can fix
5592 # then suggest that.
5593 if ($rpt_cleaners) {
5594 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5595 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005596 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005597 }
5598 }
5599
Joe Perches91bfe482013-09-11 14:23:59 -07005600 hash_show_words(\%use_type, "Used");
5601 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005602
Joe Perchesd752fcc2014-08-06 16:11:05 -07005603 if ($clean == 0 && $fix &&
5604 ("@rawlines" ne "@fixed" ||
5605 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005606 my $newfile = $filename;
5607 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005608 my $linecount = 0;
5609 my $f;
5610
Joe Perchesd752fcc2014-08-06 16:11:05 -07005611 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5612
Joe Perches3705ce52013-07-03 15:05:31 -07005613 open($f, '>', $newfile)
5614 or die "$P: Can't open $newfile for write\n";
5615 foreach my $fixed_line (@fixed) {
5616 $linecount++;
5617 if ($file) {
5618 if ($linecount > 3) {
5619 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005620 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005621 }
5622 } else {
5623 print $f $fixed_line . "\n";
5624 }
5625 }
5626 close($f);
5627
5628 if (!$quiet) {
5629 print << "EOM";
5630Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5631
5632Do _NOT_ trust the results written to this file.
5633Do _NOT_ submit these changes without inspecting them for correctness.
5634
5635This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5636No warranties, expressed or implied...
5637
5638EOM
5639 }
5640 }
5641
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005642 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005643 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005644 }
5645 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005646 print << "EOM";
5647$vname has style problems, please review.
5648
5649If any of these errors are false positives, please report
5650them to the maintainer, see CHECKPATCH in MAINTAINERS.
5651EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005652 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005653
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005654 return $clean;
5655}