blob: f7f222a1e908aaeaca2279f897cf0279cd6af401 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
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 Perches2ac73b4f2014-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 Perches2ac73b4f2014-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);
Joe Perches8716de32013-09-11 14:24:05 -0700426our @typeListWithAttr = (
427 @typeList,
428 qr{struct\s+$InitAttribute\s+$Ident},
429 qr{union\s+$InitAttribute\s+$Ident},
430);
431
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700432our @modifierList = (
433 qr{fastcall},
434);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800435
Joe Perches24358802014-04-03 14:49:13 -0700436our @mode_permission_funcs = (
437 ["module_param", 3],
438 ["module_param_(?:array|named|string)", 4],
439 ["module_param_array_named", 5],
440 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
441 ["proc_create(?:_data|)", 2],
442 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
443);
444
Joe Perches515a2352014-04-03 14:49:24 -0700445#Create a search pattern for all these functions to speed up a loop below
446our $mode_perms_search = "";
447foreach my $entry (@mode_permission_funcs) {
448 $mode_perms_search .= '|' if ($mode_perms_search ne "");
449 $mode_perms_search .= $entry->[0];
450}
451
Joe Perchesb392c642015-04-16 12:44:16 -0700452our $mode_perms_world_writable = qr{
453 S_IWUGO |
454 S_IWOTH |
455 S_IRWXUGO |
456 S_IALLUGO |
457 0[0-7][0-7][2367]
458}x;
459
Wolfram Sang7840a942010-08-09 17:20:57 -0700460our $allowed_asm_includes = qr{(?x:
461 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700462 memory|
463 time|
464 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700465)};
466# memory.h: ARM has a custom one
467
Kees Cook66b47b42014-10-13 15:51:57 -0700468# Load common spelling mistakes and build regular expression list.
469my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700470my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700471
Joe Perches36061e32014-12-10 15:51:43 -0800472if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800473 while (<$spelling>) {
474 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700475
Joe Perches36061e32014-12-10 15:51:43 -0800476 $line =~ s/\s*\n?$//g;
477 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700478
Joe Perches36061e32014-12-10 15:51:43 -0800479 next if ($line =~ m/^\s*#/);
480 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700481
Joe Perches36061e32014-12-10 15:51:43 -0800482 my ($suspect, $fix) = split(/\|\|/, $line);
483
Joe Perches36061e32014-12-10 15:51:43 -0800484 $spelling_fix{$suspect} = $fix;
485 }
486 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800487} else {
488 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700489}
Kees Cook66b47b42014-10-13 15:51:57 -0700490
Joe Perchesebfd7d62015-04-16 12:44:14 -0700491if ($codespell) {
492 if (open(my $spelling, '<', $codespellfile)) {
493 while (<$spelling>) {
494 my $line = $_;
495
496 $line =~ s/\s*\n?$//g;
497 $line =~ s/^\s*//g;
498
499 next if ($line =~ m/^\s*#/);
500 next if ($line =~ m/^\s*$/);
501 next if ($line =~ m/, disabled/i);
502
503 $line =~ s/,.*$//;
504
505 my ($suspect, $fix) = split(/->/, $line);
506
507 $spelling_fix{$suspect} = $fix;
508 }
509 close($spelling);
510 } else {
511 warn "No codespell typos will be found - file '$codespellfile': $!\n";
512 }
513}
514
515$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
516
Andy Whitcroft8905a672007-11-28 16:21:06 -0800517sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700518 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
519 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700520 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700521 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700522 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700523 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700524 (?:$typeTypedefs\b)|
525 (?:${all}\b)
526 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800527 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700528 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800529 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800530 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700531 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700532 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800533 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700534 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800535 }x;
Joe Perches18130872014-08-06 16:11:22 -0700536 $NonptrTypeMisordered = qr{
537 (?:$Modifier\s+|const\s+)*
538 (?:
539 (?:${Misordered}\b)
540 )
541 (?:\s+$Modifier|\s+const)*
542 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700543 $NonptrTypeWithAttr = qr{
544 (?:$Modifier\s+|const\s+)*
545 (?:
546 (?:typeof|__typeof__)\s*\([^\)]*\)|
547 (?:$typeTypedefs\b)|
548 (?:${allWithAttr}\b)
549 )
550 (?:\s+$Modifier|\s+const)*
551 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800552 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700553 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700554 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700555 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800556 }x;
Joe Perches18130872014-08-06 16:11:22 -0700557 $TypeMisordered = qr{
558 $NonptrTypeMisordered
559 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
560 (?:\s+$Inline|\s+$Modifier)*
561 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700562 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700563 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800564}
565build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700566
Joe Perches7d2367a2011-07-25 17:13:22 -0700567our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700568
569# Using $balanced_parens, $LvalOrFunc, or $FuncArg
570# requires at least perl version v5.10.0
571# Any use must be runtime checked with $^V
572
573our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700574our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800575our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700576
Joe Perchesf8422302014-08-06 16:11:31 -0700577our $declaration_macros = qr{(?x:
578 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
579 (?:$Storage\s+)?LIST_HEAD\s*\(|
580 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
581)};
582
Joe Perches7d2367a2011-07-25 17:13:22 -0700583sub deparenthesize {
584 my ($string) = @_;
585 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700586
587 while ($string =~ /^\s*\(.*\)\s*$/) {
588 $string =~ s@^\s*\(\s*@@;
589 $string =~ s@\s*\)\s*$@@;
590 }
591
Joe Perches7d2367a2011-07-25 17:13:22 -0700592 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700593
Joe Perches7d2367a2011-07-25 17:13:22 -0700594 return $string;
595}
596
Joe Perches34456862013-07-03 15:05:34 -0700597sub seed_camelcase_file {
598 my ($file) = @_;
599
600 return if (!(-f $file));
601
602 local $/;
603
604 open(my $include_file, '<', "$file")
605 or warn "$P: Can't read '$file' $!\n";
606 my $text = <$include_file>;
607 close($include_file);
608
609 my @lines = split('\n', $text);
610
611 foreach my $line (@lines) {
612 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
613 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
614 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800615 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
616 $camelcase{$1} = 1;
617 } 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 -0700618 $camelcase{$1} = 1;
619 }
620 }
621}
622
623my $camelcase_seeded = 0;
624sub seed_camelcase_includes {
625 return if ($camelcase_seeded);
626
627 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700628 my $camelcase_cache = "";
629 my @include_files = ();
630
631 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700632
Richard Genoud3645e322014-02-10 14:25:32 -0800633 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700634 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
635 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700636 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700637 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700638 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700639 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700640 @include_files = split('\n', $files);
641 foreach my $file (@include_files) {
642 my $date = POSIX::strftime("%Y%m%d%H%M",
643 localtime((stat $file)[9]));
644 $last_mod_date = $date if ($last_mod_date < $date);
645 }
646 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700647 }
Joe Perchesc707a812013-07-08 16:00:43 -0700648
649 if ($camelcase_cache ne "" && -f $camelcase_cache) {
650 open(my $camelcase_file, '<', "$camelcase_cache")
651 or warn "$P: Can't read '$camelcase_cache' $!\n";
652 while (<$camelcase_file>) {
653 chomp;
654 $camelcase{$_} = 1;
655 }
656 close($camelcase_file);
657
658 return;
659 }
660
Richard Genoud3645e322014-02-10 14:25:32 -0800661 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700662 $files = `git ls-files "include/*.h"`;
663 @include_files = split('\n', $files);
664 }
665
Joe Perches34456862013-07-03 15:05:34 -0700666 foreach my $file (@include_files) {
667 seed_camelcase_file($file);
668 }
Joe Perches351b2a12013-07-03 15:05:36 -0700669
Joe Perchesc707a812013-07-08 16:00:43 -0700670 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700671 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700672 open(my $camelcase_file, '>', "$camelcase_cache")
673 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700674 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
675 print $camelcase_file ("$_\n");
676 }
677 close($camelcase_file);
678 }
Joe Perches34456862013-07-03 15:05:34 -0700679}
680
Joe Perchesd311cd42014-08-06 16:10:57 -0700681sub git_commit_info {
682 my ($commit, $id, $desc) = @_;
683
684 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
685
686 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
687 $output =~ s/^\s*//gm;
688 my @lines = split("\n", $output);
689
Joe Perches0d7835f2015-02-13 14:38:35 -0800690 return ($id, $desc) if ($#lines < 0);
691
Joe Perchesd311cd42014-08-06 16:10:57 -0700692 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
693# Maybe one day convert this block of bash into something that returns
694# all matching commit ids, but it's very slow...
695#
696# echo "checking commits $1..."
697# git rev-list --remotes | grep -i "^$1" |
698# while read line ; do
699# git log --format='%H %s' -1 $line |
700# echo "commit $(cut -c 1-12,41-)"
701# done
702 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
703 } else {
704 $id = substr($lines[0], 0, 12);
705 $desc = substr($lines[0], 41);
706 }
707
708 return ($id, $desc);
709}
710
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700711$chk_signoff = 0 if ($file);
712
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700713my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800714my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700715my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700716my @fixed_inserted = ();
717my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700718my $fixlinenr = -1;
719
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800720my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700721for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800722 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700723 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800724 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700725 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800726 } elsif ($filename eq '-') {
727 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700728 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800729 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700730 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700731 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800732 if ($filename eq '-') {
733 $vname = 'Your patch';
734 } else {
735 $vname = $filename;
736 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800737 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700738 chomp;
739 push(@rawlines, $_);
740 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800741 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800742 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700743 $exit = 1;
744 }
745 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800746 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700747 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700748 @fixed_inserted = ();
749 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700750 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700751}
752
753exit($exit);
754
755sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700756 my ($root) = @_;
757
758 my @tree_check = (
759 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
760 "README", "Documentation", "arch", "include", "drivers",
761 "fs", "init", "ipc", "kernel", "lib", "scripts",
762 );
763
764 foreach my $check (@tree_check) {
765 if (! -e $root . '/' . $check) {
766 return 0;
767 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700768 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700769 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700770}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700771
Joe Perches20112472011-07-25 17:13:23 -0700772sub parse_email {
773 my ($formatted_email) = @_;
774
775 my $name = "";
776 my $address = "";
777 my $comment = "";
778
779 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
780 $name = $1;
781 $address = $2;
782 $comment = $3 if defined $3;
783 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
784 $address = $1;
785 $comment = $2 if defined $2;
786 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
787 $address = $1;
788 $comment = $2 if defined $2;
789 $formatted_email =~ s/$address.*$//;
790 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700791 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700792 $name =~ s/^\"|\"$//g;
793 # If there's a name left after stripping spaces and
794 # leading quotes, and the address doesn't have both
795 # leading and trailing angle brackets, the address
796 # is invalid. ie:
797 # "joe smith joe@smith.com" bad
798 # "joe smith <joe@smith.com" bad
799 if ($name ne "" && $address !~ /^<[^>]+>$/) {
800 $name = "";
801 $address = "";
802 $comment = "";
803 }
804 }
805
Joe Perches3705ce52013-07-03 15:05:31 -0700806 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700807 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700808 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700809 $address =~ s/^\<|\>$//g;
810
811 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
812 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
813 $name = "\"$name\"";
814 }
815
816 return ($name, $address, $comment);
817}
818
819sub format_email {
820 my ($name, $address) = @_;
821
822 my $formatted_email;
823
Joe Perches3705ce52013-07-03 15:05:31 -0700824 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700825 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700826 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700827
828 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
829 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
830 $name = "\"$name\"";
831 }
832
833 if ("$name" eq "") {
834 $formatted_email = "$address";
835 } else {
836 $formatted_email = "$name <$address>";
837 }
838
839 return $formatted_email;
840}
841
Joe Perchesd311cd42014-08-06 16:10:57 -0700842sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700843 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700844
Joe Perchesbd474ca2014-08-06 16:11:10 -0700845 foreach my $path (split(/:/, $ENV{PATH})) {
846 if (-e "$path/$bin") {
847 return "$path/$bin";
848 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700849 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700850
Joe Perchesbd474ca2014-08-06 16:11:10 -0700851 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700852}
853
Joe Perches000d1cc12011-07-25 17:13:25 -0700854sub which_conf {
855 my ($conf) = @_;
856
857 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
858 if (-e "$path/$conf") {
859 return "$path/$conf";
860 }
861 }
862
863 return "";
864}
865
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700866sub expand_tabs {
867 my ($str) = @_;
868
869 my $res = '';
870 my $n = 0;
871 for my $c (split(//, $str)) {
872 if ($c eq "\t") {
873 $res .= ' ';
874 $n++;
875 for (; ($n % 8) != 0; $n++) {
876 $res .= ' ';
877 }
878 next;
879 }
880 $res .= $c;
881 $n++;
882 }
883
884 return $res;
885}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700886sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700887 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700888 return $res;
889}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700890
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700891sub line_stats {
892 my ($line) = @_;
893
894 # Drop the diff line leader and expand tabs
895 $line =~ s/^.//;
896 $line = expand_tabs($line);
897
898 # Pick the indent from the front of the line.
899 my ($white) = ($line =~ /^(\s*)/);
900
901 return (length($line), length($white));
902}
903
Andy Whitcroft773647a2008-03-28 14:15:58 -0700904my $sanitise_quote = '';
905
906sub sanitise_line_reset {
907 my ($in_comment) = @_;
908
909 if ($in_comment) {
910 $sanitise_quote = '*/';
911 } else {
912 $sanitise_quote = '';
913 }
914}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700915sub sanitise_line {
916 my ($line) = @_;
917
918 my $res = '';
919 my $l = '';
920
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800921 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700922 my $off = 0;
923 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700924
Andy Whitcroft773647a2008-03-28 14:15:58 -0700925 # Always copy over the diff marker.
926 $res = substr($line, 0, 1);
927
928 for ($off = 1; $off < length($line); $off++) {
929 $c = substr($line, $off, 1);
930
931 # Comments we are wacking completly including the begin
932 # and end, all to $;.
933 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
934 $sanitise_quote = '*/';
935
936 substr($res, $off, 2, "$;$;");
937 $off++;
938 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800939 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700940 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700941 $sanitise_quote = '';
942 substr($res, $off, 2, "$;$;");
943 $off++;
944 next;
945 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700946 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
947 $sanitise_quote = '//';
948
949 substr($res, $off, 2, $sanitise_quote);
950 $off++;
951 next;
952 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700953
954 # A \ in a string means ignore the next character.
955 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
956 $c eq "\\") {
957 substr($res, $off, 2, 'XX');
958 $off++;
959 next;
960 }
961 # Regular quotes.
962 if ($c eq "'" || $c eq '"') {
963 if ($sanitise_quote eq '') {
964 $sanitise_quote = $c;
965
966 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700967 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700968 } elsif ($sanitise_quote eq $c) {
969 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700970 }
971 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700972
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800973 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700974 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
975 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700976 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
977 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700978 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
979 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700980 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700981 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700982 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800983 }
984
Daniel Walker113f04a2009-09-21 17:04:35 -0700985 if ($sanitise_quote eq '//') {
986 $sanitise_quote = '';
987 }
988
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800989 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700990 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800991 my $clean = 'X' x length($1);
992 $res =~ s@\<.*\>@<$clean>@;
993
994 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700995 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800996 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700997 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800998 }
999
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001000 return $res;
1001}
1002
Joe Perchesa6962d72013-04-29 16:18:13 -07001003sub get_quoted_string {
1004 my ($line, $rawline) = @_;
1005
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001006 return "" if ($line !~ m/(\"[X\t]+\")/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001007 return substr($rawline, $-[0], $+[0] - $-[0]);
1008}
1009
Andy Whitcroft8905a672007-11-28 16:21:06 -08001010sub ctx_statement_block {
1011 my ($linenr, $remain, $off) = @_;
1012 my $line = $linenr - 1;
1013 my $blk = '';
1014 my $soff = $off;
1015 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001016 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001017
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001018 my $loff = 0;
1019
Andy Whitcroft8905a672007-11-28 16:21:06 -08001020 my $type = '';
1021 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001022 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001023 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001024 my $c;
1025 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001026
1027 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001028 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001029 @stack = (['', 0]) if ($#stack == -1);
1030
Andy Whitcroft773647a2008-03-28 14:15:58 -07001031 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001032 # If we are about to drop off the end, pull in more
1033 # context.
1034 if ($off >= $len) {
1035 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001036 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001037 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001038 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001039 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001040 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001041 $len = length($blk);
1042 $line++;
1043 last;
1044 }
1045 # Bail if there is no further context.
1046 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001047 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001048 last;
1049 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001050 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1051 $level++;
1052 $type = '#';
1053 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001054 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001055 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001056 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001057 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001058
Andy Whitcroft773647a2008-03-28 14:15:58 -07001059 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001060
1061 # Handle nested #if/#else.
1062 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1063 push(@stack, [ $type, $level ]);
1064 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1065 ($type, $level) = @{$stack[$#stack - 1]};
1066 } elsif ($remainder =~ /^#\s*endif\b/) {
1067 ($type, $level) = @{pop(@stack)};
1068 }
1069
Andy Whitcroft8905a672007-11-28 16:21:06 -08001070 # Statement ends at the ';' or a close '}' at the
1071 # outermost level.
1072 if ($level == 0 && $c eq ';') {
1073 last;
1074 }
1075
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001076 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001077 if ($level == 0 && $coff_set == 0 &&
1078 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1079 $remainder =~ /^(else)(?:\s|{)/ &&
1080 $remainder !~ /^else\s+if\b/) {
1081 $coff = $off + length($1) - 1;
1082 $coff_set = 1;
1083 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1084 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001085 }
1086
Andy Whitcroft8905a672007-11-28 16:21:06 -08001087 if (($type eq '' || $type eq '(') && $c eq '(') {
1088 $level++;
1089 $type = '(';
1090 }
1091 if ($type eq '(' && $c eq ')') {
1092 $level--;
1093 $type = ($level != 0)? '(' : '';
1094
1095 if ($level == 0 && $coff < $soff) {
1096 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001097 $coff_set = 1;
1098 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001099 }
1100 }
1101 if (($type eq '' || $type eq '{') && $c eq '{') {
1102 $level++;
1103 $type = '{';
1104 }
1105 if ($type eq '{' && $c eq '}') {
1106 $level--;
1107 $type = ($level != 0)? '{' : '';
1108
1109 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001110 if (substr($blk, $off + 1, 1) eq ';') {
1111 $off++;
1112 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001113 last;
1114 }
1115 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001116 # Preprocessor commands end at the newline unless escaped.
1117 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1118 $level--;
1119 $type = '';
1120 $off++;
1121 last;
1122 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001123 $off++;
1124 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001125 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001126 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001127 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001128 $line++;
1129 $remain--;
1130 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001131
1132 my $statement = substr($blk, $soff, $off - $soff + 1);
1133 my $condition = substr($blk, $soff, $coff - $soff + 1);
1134
1135 #warn "STATEMENT<$statement>\n";
1136 #warn "CONDITION<$condition>\n";
1137
Andy Whitcroft773647a2008-03-28 14:15:58 -07001138 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001139
1140 return ($statement, $condition,
1141 $line, $remain + 1, $off - $loff + 1, $level);
1142}
1143
Andy Whitcroftcf655042008-03-04 14:28:20 -08001144sub statement_lines {
1145 my ($stmt) = @_;
1146
1147 # Strip the diff line prefixes and rip blank lines at start and end.
1148 $stmt =~ s/(^|\n)./$1/g;
1149 $stmt =~ s/^\s*//;
1150 $stmt =~ s/\s*$//;
1151
1152 my @stmt_lines = ($stmt =~ /\n/g);
1153
1154 return $#stmt_lines + 2;
1155}
1156
1157sub statement_rawlines {
1158 my ($stmt) = @_;
1159
1160 my @stmt_lines = ($stmt =~ /\n/g);
1161
1162 return $#stmt_lines + 2;
1163}
1164
1165sub statement_block_size {
1166 my ($stmt) = @_;
1167
1168 $stmt =~ s/(^|\n)./$1/g;
1169 $stmt =~ s/^\s*{//;
1170 $stmt =~ s/}\s*$//;
1171 $stmt =~ s/^\s*//;
1172 $stmt =~ s/\s*$//;
1173
1174 my @stmt_lines = ($stmt =~ /\n/g);
1175 my @stmt_statements = ($stmt =~ /;/g);
1176
1177 my $stmt_lines = $#stmt_lines + 2;
1178 my $stmt_statements = $#stmt_statements + 1;
1179
1180 if ($stmt_lines > $stmt_statements) {
1181 return $stmt_lines;
1182 } else {
1183 return $stmt_statements;
1184 }
1185}
1186
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001187sub ctx_statement_full {
1188 my ($linenr, $remain, $off) = @_;
1189 my ($statement, $condition, $level);
1190
1191 my (@chunks);
1192
Andy Whitcroftcf655042008-03-04 14:28:20 -08001193 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001194 ($statement, $condition, $linenr, $remain, $off, $level) =
1195 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001196 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001197 push(@chunks, [ $condition, $statement ]);
1198 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1199 return ($level, $linenr, @chunks);
1200 }
1201
1202 # Pull in the following conditional/block pairs and see if they
1203 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001204 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001205 ($statement, $condition, $linenr, $remain, $off, $level) =
1206 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001207 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001208 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001209 #print "C: push\n";
1210 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001211 }
1212
1213 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001214}
1215
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001216sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001217 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001218 my $line;
1219 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001220 my $blk = '';
1221 my @o;
1222 my @c;
1223 my @res = ();
1224
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001225 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001226 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001227 for ($line = $start; $remain > 0; $line++) {
1228 next if ($rawlines[$line] =~ /^-/);
1229 $remain--;
1230
1231 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001232
1233 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001234 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001235 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001236 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001237 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001238 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001239 $level = pop(@stack);
1240 }
1241
Andy Whitcroft01464f32010-10-26 14:23:19 -07001242 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001243 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1244 if ($off > 0) {
1245 $off--;
1246 next;
1247 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001248
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001249 if ($c eq $close && $level > 0) {
1250 $level--;
1251 last if ($level == 0);
1252 } elsif ($c eq $open) {
1253 $level++;
1254 }
1255 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001256
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001257 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001258 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001259 }
1260
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001261 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001262 }
1263
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001264 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001265}
1266sub ctx_block_outer {
1267 my ($linenr, $remain) = @_;
1268
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001269 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1270 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001271}
1272sub ctx_block {
1273 my ($linenr, $remain) = @_;
1274
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001275 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1276 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001277}
1278sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001279 my ($linenr, $remain, $off) = @_;
1280
1281 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1282 return @r;
1283}
1284sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001285 my ($linenr, $remain) = @_;
1286
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001287 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001288}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001289sub ctx_statement_level {
1290 my ($linenr, $remain, $off) = @_;
1291
1292 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1293}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001294
1295sub ctx_locate_comment {
1296 my ($first_line, $end_line) = @_;
1297
1298 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001299 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001300 return $current_comment if (defined $current_comment);
1301
1302 # Look through the context and try and figure out if there is a
1303 # comment.
1304 my $in_comment = 0;
1305 $current_comment = '';
1306 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001307 my $line = $rawlines[$linenr - 1];
1308 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001309 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1310 $in_comment = 1;
1311 }
1312 if ($line =~ m@/\*@) {
1313 $in_comment = 1;
1314 }
1315 if (!$in_comment && $current_comment ne '') {
1316 $current_comment = '';
1317 }
1318 $current_comment .= $line . "\n" if ($in_comment);
1319 if ($line =~ m@\*/@) {
1320 $in_comment = 0;
1321 }
1322 }
1323
1324 chomp($current_comment);
1325 return($current_comment);
1326}
1327sub ctx_has_comment {
1328 my ($first_line, $end_line) = @_;
1329 my $cmt = ctx_locate_comment($first_line, $end_line);
1330
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001331 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001332 ##print "CMMT: $cmt\n";
1333
1334 return ($cmt ne '');
1335}
1336
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001337sub raw_line {
1338 my ($linenr, $cnt) = @_;
1339
1340 my $offset = $linenr - 1;
1341 $cnt++;
1342
1343 my $line;
1344 while ($cnt) {
1345 $line = $rawlines[$offset++];
1346 next if (defined($line) && $line =~ /^-/);
1347 $cnt--;
1348 }
1349
1350 return $line;
1351}
1352
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001353sub cat_vet {
1354 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001355 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001356
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001357 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001358 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1359 $res .= $1;
1360 if ($2 ne '') {
1361 $coded = sprintf("^%c", unpack('C', $2) + 64);
1362 $res .= $coded;
1363 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001364 }
1365 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001366
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001367 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001368}
1369
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001370my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001371my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001372my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001373my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001374
1375sub annotate_reset {
1376 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001377 $av_pending = '_';
1378 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001379 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001380}
1381
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001382sub annotate_values {
1383 my ($stream, $type) = @_;
1384
1385 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001386 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001387 my $cur = $stream;
1388
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001389 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001390
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001391 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001392 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001393 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001394 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001395 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001396 print "WS($1)\n" if ($dbg_values > 1);
1397 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001398 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001399 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001400 }
1401
Florian Micklerc023e4732011-01-12 16:59:58 -08001402 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001403 print "CAST($1)\n" if ($dbg_values > 1);
1404 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001405 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001406
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001407 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001408 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001409 $type = 'T';
1410
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001411 } elsif ($cur =~ /^($Modifier)\s*/) {
1412 print "MODIFIER($1)\n" if ($dbg_values > 1);
1413 $type = 'T';
1414
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001415 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001416 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001417 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001418 push(@av_paren_type, $type);
1419 if ($2 ne '') {
1420 $av_pending = 'N';
1421 }
1422 $type = 'E';
1423
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001424 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001425 print "UNDEF($1)\n" if ($dbg_values > 1);
1426 $av_preprocessor = 1;
1427 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001428
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001429 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001430 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001431 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001432
1433 push(@av_paren_type, $type);
1434 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001435 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001437 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001438 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1439 $av_preprocessor = 1;
1440
1441 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1442
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001443 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001444
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001445 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001446 print "PRE_END($1)\n" if ($dbg_values > 1);
1447
1448 $av_preprocessor = 1;
1449
1450 # Assume all arms of the conditional end as this
1451 # one does, and continue as if the #endif was not here.
1452 pop(@av_paren_type);
1453 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001454 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001455
1456 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001457 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001458
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001459 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1460 print "ATTR($1)\n" if ($dbg_values > 1);
1461 $av_pending = $type;
1462 $type = 'N';
1463
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001464 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001465 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001466 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001467 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001468 }
1469 $type = 'N';
1470
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001471 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001472 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001473 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001474 $type = 'N';
1475
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001476 } elsif ($cur =~/^(case)/o) {
1477 print "CASE($1)\n" if ($dbg_values > 1);
1478 $av_pend_colon = 'C';
1479 $type = 'N';
1480
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001481 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001482 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001483 $type = 'N';
1484
1485 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001486 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001487 push(@av_paren_type, $av_pending);
1488 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001489 $type = 'N';
1490
1491 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001492 my $new_type = pop(@av_paren_type);
1493 if ($new_type ne '_') {
1494 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001495 print "PAREN('$1') -> $type\n"
1496 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001497 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001498 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001499 }
1500
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001501 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001502 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001503 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001504 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001505
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001506 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1507 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001508 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001509 } elsif ($type eq 'E') {
1510 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001511 }
1512 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1513 $type = 'V';
1514
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001515 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001516 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001517 $type = 'V';
1518
1519 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001520 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001521 $type = 'N';
1522
Andy Whitcroftcf655042008-03-04 14:28:20 -08001523 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001524 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001525 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001526 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001527
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001528 } elsif ($cur =~/^(,)/) {
1529 print "COMMA($1)\n" if ($dbg_values > 1);
1530 $type = 'C';
1531
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001532 } elsif ($cur =~ /^(\?)/o) {
1533 print "QUESTION($1)\n" if ($dbg_values > 1);
1534 $type = 'N';
1535
1536 } elsif ($cur =~ /^(:)/o) {
1537 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1538
1539 substr($var, length($res), 1, $av_pend_colon);
1540 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1541 $type = 'E';
1542 } else {
1543 $type = 'N';
1544 }
1545 $av_pend_colon = 'O';
1546
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001547 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001548 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001549 $type = 'N';
1550
Andy Whitcroft0d413862008-10-15 22:02:16 -07001551 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001552 my $variant;
1553
1554 print "OPV($1)\n" if ($dbg_values > 1);
1555 if ($type eq 'V') {
1556 $variant = 'B';
1557 } else {
1558 $variant = 'U';
1559 }
1560
1561 substr($var, length($res), 1, $variant);
1562 $type = 'N';
1563
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001564 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001565 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001566 if ($1 ne '++' && $1 ne '--') {
1567 $type = 'N';
1568 }
1569
1570 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001571 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001572 }
1573 if (defined $1) {
1574 $cur = substr($cur, length($1));
1575 $res .= $type x length($1);
1576 }
1577 }
1578
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001579 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001580}
1581
Andy Whitcroft8905a672007-11-28 16:21:06 -08001582sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001583 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001584 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001585 ^(?:
1586 $Modifier|
1587 $Storage|
1588 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001589 DEFINE_\S+
1590 )$|
1591 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001592 goto|
1593 return|
1594 case|
1595 else|
1596 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001597 do|
1598 \#|
1599 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001600 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001601 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001602 )}x;
1603 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1604 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001605 # Check for modifiers.
1606 $possible =~ s/\s*$Storage\s*//g;
1607 $possible =~ s/\s*$Sparse\s*//g;
1608 if ($possible =~ /^\s*$/) {
1609
1610 } elsif ($possible =~ /\s/) {
1611 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001612 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001613 if ($modifier !~ $notPermitted) {
1614 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1615 push(@modifierList, $modifier);
1616 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001617 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001618
1619 } else {
1620 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1621 push(@typeList, $possible);
1622 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001623 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001624 } else {
1625 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001626 }
1627}
1628
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001629my $prefix = '';
1630
Joe Perches000d1cc12011-07-25 17:13:25 -07001631sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001632 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001633
Joe Perchescbec18a2014-04-03 14:49:19 -07001634 return defined $use_type{$type} if (scalar keys %use_type > 0);
1635
1636 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001637}
1638
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001639sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001640 my ($level, $type, $msg) = @_;
1641
1642 if (!show_type($type) ||
1643 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001644 return 0;
1645 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001646 my $line;
1647 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001648 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001649 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001650 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001651 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001652 $line = (split('\n', $line))[0] . "\n" if ($terse);
1653
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001654 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001655
1656 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001657}
Joe Perchescbec18a2014-04-03 14:49:19 -07001658
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001659sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001660 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001661}
Joe Perches000d1cc12011-07-25 17:13:25 -07001662
Joe Perchesd752fcc2014-08-06 16:11:05 -07001663sub fixup_current_range {
1664 my ($lineRef, $offset, $length) = @_;
1665
1666 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1667 my $o = $1;
1668 my $l = $2;
1669 my $no = $o + $offset;
1670 my $nl = $l + $length;
1671 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1672 }
1673}
1674
1675sub fix_inserted_deleted_lines {
1676 my ($linesRef, $insertedRef, $deletedRef) = @_;
1677
1678 my $range_last_linenr = 0;
1679 my $delta_offset = 0;
1680
1681 my $old_linenr = 0;
1682 my $new_linenr = 0;
1683
1684 my $next_insert = 0;
1685 my $next_delete = 0;
1686
1687 my @lines = ();
1688
1689 my $inserted = @{$insertedRef}[$next_insert++];
1690 my $deleted = @{$deletedRef}[$next_delete++];
1691
1692 foreach my $old_line (@{$linesRef}) {
1693 my $save_line = 1;
1694 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001695 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001696 $delta_offset = 0;
1697 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1698 $range_last_linenr = $new_linenr;
1699 fixup_current_range(\$line, $delta_offset, 0);
1700 }
1701
1702 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1703 $deleted = @{$deletedRef}[$next_delete++];
1704 $save_line = 0;
1705 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1706 }
1707
1708 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1709 push(@lines, ${$inserted}{'LINE'});
1710 $inserted = @{$insertedRef}[$next_insert++];
1711 $new_linenr++;
1712 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1713 }
1714
1715 if ($save_line) {
1716 push(@lines, $line);
1717 $new_linenr++;
1718 }
1719
1720 $old_linenr++;
1721 }
1722
1723 return @lines;
1724}
1725
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001726sub fix_insert_line {
1727 my ($linenr, $line) = @_;
1728
1729 my $inserted = {
1730 LINENR => $linenr,
1731 LINE => $line,
1732 };
1733 push(@fixed_inserted, $inserted);
1734}
1735
1736sub fix_delete_line {
1737 my ($linenr, $line) = @_;
1738
1739 my $deleted = {
1740 LINENR => $linenr,
1741 LINE => $line,
1742 };
1743
1744 push(@fixed_deleted, $deleted);
1745}
1746
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001747sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001748 my ($type, $msg) = @_;
1749
1750 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001751 our $clean = 0;
1752 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001753 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001754 }
Joe Perches3705ce52013-07-03 15:05:31 -07001755 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001756}
1757sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001758 my ($type, $msg) = @_;
1759
1760 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001761 our $clean = 0;
1762 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001763 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001764 }
Joe Perches3705ce52013-07-03 15:05:31 -07001765 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001766}
1767sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001768 my ($type, $msg) = @_;
1769
1770 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001771 our $clean = 0;
1772 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001773 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001774 }
Joe Perches3705ce52013-07-03 15:05:31 -07001775 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001776}
1777
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001778sub check_absolute_file {
1779 my ($absolute, $herecurr) = @_;
1780 my $file = $absolute;
1781
1782 ##print "absolute<$absolute>\n";
1783
1784 # See if any suffix of this path is a path within the tree.
1785 while ($file =~ s@^[^/]*/@@) {
1786 if (-f "$root/$file") {
1787 ##print "file<$file>\n";
1788 last;
1789 }
1790 }
1791 if (! -f _) {
1792 return 0;
1793 }
1794
1795 # It is, so see if the prefix is acceptable.
1796 my $prefix = $absolute;
1797 substr($prefix, -length($file)) = '';
1798
1799 ##print "prefix<$prefix>\n";
1800 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001801 WARN("USE_RELATIVE_PATH",
1802 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001803 }
1804}
1805
Joe Perches3705ce52013-07-03 15:05:31 -07001806sub trim {
1807 my ($string) = @_;
1808
Joe Perchesb34c6482013-09-11 14:24:01 -07001809 $string =~ s/^\s+|\s+$//g;
1810
1811 return $string;
1812}
1813
1814sub ltrim {
1815 my ($string) = @_;
1816
1817 $string =~ s/^\s+//;
1818
1819 return $string;
1820}
1821
1822sub rtrim {
1823 my ($string) = @_;
1824
1825 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001826
1827 return $string;
1828}
1829
Joe Perches52ea8502013-11-12 15:10:09 -08001830sub string_find_replace {
1831 my ($string, $find, $replace) = @_;
1832
1833 $string =~ s/$find/$replace/g;
1834
1835 return $string;
1836}
1837
Joe Perches3705ce52013-07-03 15:05:31 -07001838sub tabify {
1839 my ($leading) = @_;
1840
1841 my $source_indent = 8;
1842 my $max_spaces_before_tab = $source_indent - 1;
1843 my $spaces_to_tab = " " x $source_indent;
1844
1845 #convert leading spaces to tabs
1846 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1847 #Remove spaces before a tab
1848 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1849
1850 return "$leading";
1851}
1852
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001853sub pos_last_openparen {
1854 my ($line) = @_;
1855
1856 my $pos = 0;
1857
1858 my $opens = $line =~ tr/\(/\(/;
1859 my $closes = $line =~ tr/\)/\)/;
1860
1861 my $last_openparen = 0;
1862
1863 if (($opens == 0) || ($closes >= $opens)) {
1864 return -1;
1865 }
1866
1867 my $len = length($line);
1868
1869 for ($pos = 0; $pos < $len; $pos++) {
1870 my $string = substr($line, $pos);
1871 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1872 $pos += length($1) - 1;
1873 } elsif (substr($line, $pos, 1) eq '(') {
1874 $last_openparen = $pos;
1875 } elsif (index($string, '(') == -1) {
1876 last;
1877 }
1878 }
1879
Joe Perches91cb5192014-04-03 14:49:32 -07001880 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001881}
1882
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001883sub process {
1884 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001885
1886 my $linenr=0;
1887 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001888 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001889 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001890 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001891
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001892 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001893 my $indent;
1894 my $previndent=0;
1895 my $stashindent=0;
1896
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001897 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001898 my $signoff = 0;
1899 my $is_patch = 0;
1900
Joe Perches29ee1b02014-08-06 16:10:35 -07001901 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001902 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches2a076f42015-04-16 12:44:28 -07001903 my $commit_log_long_line = 0;
Joe Perches13f19372014-08-06 16:10:59 -07001904 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001905 my $non_utf8_charset = 0;
1906
Joe Perches365dd4e2014-08-06 16:10:42 -07001907 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001908 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001909
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001910 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001911 our $cnt_lines = 0;
1912 our $cnt_error = 0;
1913 our $cnt_warn = 0;
1914 our $cnt_chk = 0;
1915
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001916 # Trace the real file/line as we go.
1917 my $realfile = '';
1918 my $realline = 0;
1919 my $realcnt = 0;
1920 my $here = '';
1921 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001922 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001923 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001924 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001925
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001926 my $prev_values = 'E';
1927
1928 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001929 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001930 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001931 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001932 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001933
Joe Perches7e51f192013-09-11 14:23:57 -07001934 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001935
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001936 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001937 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001938 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001939 my @setup_docs = ();
1940 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001941
Joe Perchesd8b07712013-11-12 15:10:06 -08001942 my $camelcase_file_seeded = 0;
1943
Andy Whitcroft773647a2008-03-28 14:15:58 -07001944 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001945 my $line;
1946 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001947 $linenr++;
1948 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001949
Joe Perches3705ce52013-07-03 15:05:31 -07001950 push(@fixed, $rawline) if ($fix);
1951
Andy Whitcroft773647a2008-03-28 14:15:58 -07001952 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001953 $setup_docs = 0;
1954 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1955 $setup_docs = 1;
1956 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001957 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001958 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001959 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1960 $realline=$1-1;
1961 if (defined $2) {
1962 $realcnt=$3+1;
1963 } else {
1964 $realcnt=1+1;
1965 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001966 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001967
1968 # Guestimate if this is a continuing comment. Run
1969 # the context looking for a comment "edge". If this
1970 # edge is a close comment then we must be in a comment
1971 # at context start.
1972 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001973 my $cnt = $realcnt;
1974 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1975 next if (defined $rawlines[$ln - 1] &&
1976 $rawlines[$ln - 1] =~ /^-/);
1977 $cnt--;
1978 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001979 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001980 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1981 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1982 ($edge) = $1;
1983 last;
1984 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001985 }
1986 if (defined $edge && $edge eq '*/') {
1987 $in_comment = 1;
1988 }
1989
1990 # Guestimate if this is a continuing comment. If this
1991 # is the start of a diff block and this line starts
1992 # ' *' then it is very likely a comment.
1993 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001994 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001995 {
1996 $in_comment = 1;
1997 }
1998
1999 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2000 sanitise_line_reset($in_comment);
2001
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002002 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002003 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002004 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002005 $line = sanitise_line($rawline);
2006 }
2007 push(@lines, $line);
2008
2009 if ($realcnt > 1) {
2010 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2011 } else {
2012 $realcnt = 0;
2013 }
2014
2015 #print "==>$rawline\n";
2016 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002017
2018 if ($setup_docs && $line =~ /^\+/) {
2019 push(@setup_docs, $line);
2020 }
2021 }
2022
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002023 $prefix = '';
2024
Andy Whitcroft773647a2008-03-28 14:15:58 -07002025 $realcnt = 0;
2026 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002027 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002028 foreach my $line (@lines) {
2029 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002030 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002031 my $sline = $line; #copy of $line
2032 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002033
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002034 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002035
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002036#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002037 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002038 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002039 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002040 $realline=$1-1;
2041 if (defined $2) {
2042 $realcnt=$3+1;
2043 } else {
2044 $realcnt=1+1;
2045 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002046 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002047 $prev_values = 'E';
2048
Andy Whitcroft773647a2008-03-28 14:15:58 -07002049 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002050 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002051 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002052 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002053 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002054
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002055# track the line number as we move through the hunk, note that
2056# new versions of GNU diff omit the leading space on completely
2057# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002058 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002059 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002060 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002061
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002062 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002063 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002064
2065 # Track the previous line.
2066 ($prevline, $stashline) = ($stashline, $line);
2067 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002068 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2069
Andy Whitcroft773647a2008-03-28 14:15:58 -07002070 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002071
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002072 } elsif ($realcnt == 1) {
2073 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002074 }
2075
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002076 my $hunk_line = ($realcnt != 0);
2077
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002078#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002079 $prefix = "$filename:$realline: " if ($emacs && $file);
2080 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2081
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002082 $here = "#$linenr: " if (!$file);
2083 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002084
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002085 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002086 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002087 if ($line =~ /^diff --git.*?(\S+)$/) {
2088 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002089 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002090 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002091 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002092 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002093 $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;
Wolfram Sang1e855722009-01-06 14:41:24 -08002096
2097 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002098 if (!$file && $tree && $p1_prefix ne '' &&
2099 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002100 WARN("PATCH_PREFIX",
2101 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002102 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002103
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002104 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002105 ERROR("MODIFIED_INCLUDE_ASM",
2106 "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 -07002107 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002108 $found_file = 1;
2109 }
2110
2111 if ($found_file) {
2112 if ($realfile =~ m@^(drivers/net/|net/)@) {
2113 $check = 1;
2114 } else {
2115 $check = $check_orig;
2116 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002117 next;
2118 }
2119
Randy Dunlap389834b2007-06-08 13:47:03 -07002120 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002121
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002122 my $hereline = "$here\n$rawline\n";
2123 my $herecurr = "$here\n$rawline\n";
2124 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002125
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002126 $cnt_lines++ if ($realcnt != 0);
2127
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002128# Check for incorrect file permissions
2129 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2130 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002131 if ($realfile !~ m@scripts/@ &&
2132 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002133 ERROR("EXECUTE_PERMISSIONS",
2134 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002135 }
2136 }
2137
Joe Perches20112472011-07-25 17:13:23 -07002138# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002139 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002140 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002141 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002142 }
2143
Joe Perchese0d975b2014-12-10 15:51:49 -08002144# Check if MAINTAINERS is being updated. If so, there's probably no need to
2145# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2146 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2147 $reported_maintainer_file = 1;
2148 }
2149
Joe Perches20112472011-07-25 17:13:23 -07002150# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002151 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002152 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002153 my $space_before = $1;
2154 my $sign_off = $2;
2155 my $space_after = $3;
2156 my $email = $4;
2157 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2158
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002159 if ($sign_off !~ /$signature_tags/) {
2160 WARN("BAD_SIGN_OFF",
2161 "Non-standard signature: $sign_off\n" . $herecurr);
2162 }
Joe Perches20112472011-07-25 17:13:23 -07002163 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002164 if (WARN("BAD_SIGN_OFF",
2165 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2166 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002167 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002168 "$ucfirst_sign_off $email";
2169 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002170 }
Joe Perches20112472011-07-25 17:13:23 -07002171 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002172 if (WARN("BAD_SIGN_OFF",
2173 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2174 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002175 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002176 "$ucfirst_sign_off $email";
2177 }
2178
Joe Perches20112472011-07-25 17:13:23 -07002179 }
2180 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002181 if (WARN("BAD_SIGN_OFF",
2182 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2183 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002184 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002185 "$ucfirst_sign_off $email";
2186 }
Joe Perches20112472011-07-25 17:13:23 -07002187 }
2188
2189 my ($email_name, $email_address, $comment) = parse_email($email);
2190 my $suggested_email = format_email(($email_name, $email_address));
2191 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002192 ERROR("BAD_SIGN_OFF",
2193 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002194 } else {
2195 my $dequoted = $suggested_email;
2196 $dequoted =~ s/^"//;
2197 $dequoted =~ s/" </ </;
2198 # Don't force email to have quotes
2199 # Allow just an angle bracketed address
2200 if ("$dequoted$comment" ne $email &&
2201 "<$email_address>$comment" ne $email &&
2202 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002203 WARN("BAD_SIGN_OFF",
2204 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002205 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002206 }
Joe Perches7e51f192013-09-11 14:23:57 -07002207
2208# Check for duplicate signatures
2209 my $sig_nospace = $line;
2210 $sig_nospace =~ s/\s//g;
2211 $sig_nospace = lc($sig_nospace);
2212 if (defined $signatures{$sig_nospace}) {
2213 WARN("BAD_SIGN_OFF",
2214 "Duplicate signature\n" . $herecurr);
2215 } else {
2216 $signatures{$sig_nospace} = 1;
2217 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002218 }
2219
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002220# Check email subject for common tools that don't need to be mentioned
2221 if ($in_header_lines &&
2222 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2223 WARN("EMAIL_SUBJECT",
2224 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2225 }
2226
Joe Perches9b3189eb2014-06-04 16:12:10 -07002227# Check for old stable address
2228 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2229 ERROR("STABLE_ADDRESS",
2230 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2231 }
2232
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002233# Check for unwanted Gerrit info
2234 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2235 ERROR("GERRIT_CHANGE_ID",
2236 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2237 }
2238
Joe Perches2a076f42015-04-16 12:44:28 -07002239# Check for line lengths > 75 in commit log, warn once
2240 if ($in_commit_log && !$commit_log_long_line &&
2241 length($line) > 75) {
2242 WARN("COMMIT_LOG_LONG_LINE",
2243 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2244 $commit_log_long_line = 1;
2245 }
2246
Joe Perches0d7835f2015-02-13 14:38:35 -08002247# Check for git id commit length and improperly formed commit descriptions
2248 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002249 my $init_char = $1;
2250 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002251 my $short = 1;
2252 my $long = 0;
2253 my $case = 1;
2254 my $space = 1;
2255 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002256 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002257 my $id = '0123456789ab';
2258 my $orig_desc = "commit description";
2259 my $description = "";
2260
2261 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2262 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2263 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2264 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2265 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2266 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002267 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002268 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2269 defined $rawlines[$linenr] &&
2270 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2271 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002272 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002273 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2274 defined $rawlines[$linenr] &&
2275 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2276 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2277 $orig_desc = $1;
2278 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2279 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002280 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002281 }
2282
2283 ($id, $description) = git_commit_info($orig_commit,
2284 $id, $orig_desc);
2285
Joe Perches19c146a2015-02-13 14:39:00 -08002286 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002287 ERROR("GIT_COMMIT_ID",
2288 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2289 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002290 }
2291
Joe Perches13f19372014-08-06 16:10:59 -07002292# Check for added, moved or deleted files
2293 if (!$reported_maintainer_file && !$in_commit_log &&
2294 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2295 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2296 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2297 (defined($1) || defined($2))))) {
2298 $reported_maintainer_file = 1;
2299 WARN("FILE_PATH_CHANGES",
2300 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2301 }
2302
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002303# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002304 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002305 ERROR("CORRUPTED_PATCH",
2306 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002307 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002308 }
2309
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002310# Check for absolute kernel paths.
2311 if ($tree) {
2312 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2313 my $file = $1;
2314
2315 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2316 check_absolute_file($1, $herecurr)) {
2317 #
2318 } else {
2319 check_absolute_file($file, $herecurr);
2320 }
2321 }
2322 }
2323
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002324# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2325 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002326 $rawline !~ m/^$UTF8*$/) {
2327 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2328
2329 my $blank = copy_spacing($rawline);
2330 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2331 my $hereptr = "$hereline$ptr\n";
2332
Joe Perches34d99212011-07-25 17:13:26 -07002333 CHK("INVALID_UTF8",
2334 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002335 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002336
Joe Perches15662b32011-10-31 17:13:12 -07002337# Check if it's the start of a commit log
2338# (not a header line and we haven't seen the patch filename)
2339 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002340 !($rawline =~ /^\s+\S/ ||
2341 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002342 $in_header_lines = 0;
2343 $in_commit_log = 1;
2344 }
2345
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002346# Check if there is UTF-8 in a commit log when a mail header has explicitly
2347# declined it, i.e defined some charset where it is missing.
2348 if ($in_header_lines &&
2349 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2350 $1 !~ /utf-8/i) {
2351 $non_utf8_charset = 1;
2352 }
2353
2354 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002355 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002356 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002357 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2358 }
2359
Kees Cook66b47b42014-10-13 15:51:57 -07002360# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002361 if (defined($misspellings) &&
2362 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002363 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002364 my $typo = $1;
2365 my $typo_fix = $spelling_fix{lc($typo)};
2366 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2367 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2368 my $msg_type = \&WARN;
2369 $msg_type = \&CHK if ($file);
2370 if (&{$msg_type}("TYPO_SPELLING",
2371 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2372 $fix) {
2373 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2374 }
2375 }
2376 }
2377
Andy Whitcroft306708542008-10-15 22:02:28 -07002378# ignore non-hunk lines and lines being removed
2379 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002380
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002381#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002382 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002383 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002384 if (ERROR("DOS_LINE_ENDINGS",
2385 "DOS line endings\n" . $herevet) &&
2386 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002387 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002388 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002389 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2390 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002391 if (ERROR("TRAILING_WHITESPACE",
2392 "trailing whitespace\n" . $herevet) &&
2393 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002394 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002395 }
2396
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002397 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002398 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002399
Josh Triplett4783f892013-11-12 15:10:12 -08002400# Check for FSF mailing addresses.
Alexander Duyck109d8cb22014-01-23 15:54:50 -08002401 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002402 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2403 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002404 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2405 my $msg_type = \&ERROR;
2406 $msg_type = \&CHK if ($file);
2407 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002408 "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 -08002409 }
2410
Andi Kleen33549572010-05-24 14:33:29 -07002411# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002412# Only applies when adding the entry originally, after that we do not have
2413# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002414 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002415 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002416 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002417 my $cnt = $realcnt;
2418 my $ln = $linenr + 1;
2419 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002420 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002421 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002422 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002423 $f = $lines[$ln - 1];
2424 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2425 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002426
2427 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002428 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002429
Joe Perches8d73e0e2014-08-06 16:10:46 -07002430 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002431 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002432 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002433 $length = -1;
2434 }
2435
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002436 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002437 $f =~ s/#.*//;
2438 $f =~ s/^\s+//;
2439 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002440 if ($f =~ /^\s*config\s/) {
2441 $is_end = 1;
2442 last;
2443 }
Andi Kleen33549572010-05-24 14:33:29 -07002444 $length++;
2445 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002446 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2447 WARN("CONFIG_DESCRIPTION",
2448 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2449 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002450 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002451 }
2452
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002453# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2454 if ($realfile =~ /Kconfig/ &&
2455 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2456 WARN("CONFIG_EXPERIMENTAL",
2457 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2458 }
2459
Christoph Jaeger327953e2015-02-13 14:38:29 -08002460# discourage the use of boolean for type definition attributes of Kconfig options
2461 if ($realfile =~ /Kconfig/ &&
2462 $line =~ /^\+\s*\bboolean\b/) {
2463 WARN("CONFIG_TYPE_BOOLEAN",
2464 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2465 }
2466
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002467 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2468 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2469 my $flag = $1;
2470 my $replacement = {
2471 'EXTRA_AFLAGS' => 'asflags-y',
2472 'EXTRA_CFLAGS' => 'ccflags-y',
2473 'EXTRA_CPPFLAGS' => 'cppflags-y',
2474 'EXTRA_LDFLAGS' => 'ldflags-y',
2475 };
2476
2477 WARN("DEPRECATED_VARIABLE",
2478 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2479 }
2480
Rob Herringbff5da42014-01-23 15:54:51 -08002481# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002482 if (defined $root &&
2483 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2484 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2485
Rob Herringbff5da42014-01-23 15:54:51 -08002486 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2487
Florian Vaussardcc933192014-04-03 14:49:27 -07002488 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2489 my $vp_file = $dt_path . "vendor-prefixes.txt";
2490
Rob Herringbff5da42014-01-23 15:54:51 -08002491 foreach my $compat (@compats) {
2492 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002493 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2494 my $compat3 = $compat;
2495 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2496 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002497 if ( $? >> 8 ) {
2498 WARN("UNDOCUMENTED_DT_STRING",
2499 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2500 }
2501
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002502 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2503 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002504 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002505 if ( $? >> 8 ) {
2506 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002507 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002508 }
2509 }
2510 }
2511
Andy Whitcroft5368df202008-10-15 22:02:27 -07002512# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002513 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002514
Joe Perches6cd7f382012-12-17 16:01:54 -08002515#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002516 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002517 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches29a3c462015-04-16 12:44:30 -07002518 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?$String\s*(?:|,|\)\s*;)\s*$/ ||
2519 $line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2520 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002521 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002522 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002523 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002524 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002525 }
2526
Andy Whitcroft8905a672007-11-28 16:21:06 -08002527# check for adding lines without a newline.
2528 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002529 WARN("MISSING_EOF_NEWLINE",
2530 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002531 }
2532
Mike Frysinger42e41c52009-09-21 17:04:40 -07002533# Blackfin: use hi/lo macros
2534 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2535 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2536 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002537 ERROR("LO_MACRO",
2538 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002539 }
2540 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2541 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002542 ERROR("HI_MACRO",
2543 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002544 }
2545 }
2546
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002547# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002548 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002549
2550# at the beginning of a line any tabs must come first and anything
2551# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002552 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2553 $rawline =~ /^\+\s* \s*/) {
2554 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002555 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002556 if (ERROR("CODE_INDENT",
2557 "code indent should use tabs where possible\n" . $herevet) &&
2558 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002559 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002560 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002561 }
2562
Alberto Panizzo08e44362010-03-05 13:43:54 -08002563# check for space before tabs.
2564 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2565 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002566 if (WARN("SPACE_BEFORE_TAB",
2567 "please, no space before tabs\n" . $herevet) &&
2568 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002569 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002570 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002571 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002572 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002573 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002574 }
2575
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002576# check for && or || at the start of a line
2577 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2578 CHK("LOGICAL_CONTINUATIONS",
2579 "Logical continuations should be on the previous line\n" . $hereprev);
2580 }
2581
2582# check multi-line statement indentation matches previous line
2583 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002584 $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 -07002585 $prevline =~ /^\+(\t*)(.*)$/;
2586 my $oldindent = $1;
2587 my $rest = $2;
2588
2589 my $pos = pos_last_openparen($rest);
2590 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002591 $line =~ /^(\+| )([ \t]*)/;
2592 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002593
2594 my $goodtabindent = $oldindent .
2595 "\t" x ($pos / 8) .
2596 " " x ($pos % 8);
2597 my $goodspaceindent = $oldindent . " " x $pos;
2598
2599 if ($newindent ne $goodtabindent &&
2600 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002601
2602 if (CHK("PARENTHESIS_ALIGNMENT",
2603 "Alignment should match open parenthesis\n" . $hereprev) &&
2604 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002605 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002606 s/^\+[ \t]*/\+$goodtabindent/;
2607 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002608 }
2609 }
2610 }
2611
Joe Perches6ab3a972015-04-16 12:44:05 -07002612# check for space after cast like "(int) foo" or "(struct foo) bar"
2613# avoid checking a few false positives:
2614# "sizeof(<type>)" or "__alignof__(<type>)"
2615# function pointer declarations like "(*foo)(int) = bar;"
2616# structure definitions like "(struct foo) { 0 };"
2617# multiline macros that define functions
2618# known attributes or the __attribute__ keyword
2619 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2620 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002621 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002622 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002623 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002624 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002625 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002626 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002627 }
2628
Joe Perches05880602012-10-04 17:13:35 -07002629 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002630 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002631 $rawline =~ /^\+[ \t]*\*/ &&
2632 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002633 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2634 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2635 }
2636
2637 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002638 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2639 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002640 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002641 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2642 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2643 "networking block comments start with * on subsequent lines\n" . $hereprev);
2644 }
2645
2646 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002647 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2648 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2649 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2650 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002651 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2652 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2653 }
2654
Joe Perches7f619192014-08-06 16:10:39 -07002655# check for missing blank lines after struct/union declarations
2656# with exceptions for various attributes and macros
2657 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2658 $line =~ /^\+/ &&
2659 !($line =~ /^\+\s*$/ ||
2660 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2661 $line =~ /^\+\s*MODULE_/i ||
2662 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2663 $line =~ /^\+[a-z_]*init/ ||
2664 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2665 $line =~ /^\+\s*DECLARE/ ||
2666 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002667 if (CHK("LINE_SPACING",
2668 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2669 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002670 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002671 }
Joe Perches7f619192014-08-06 16:10:39 -07002672 }
2673
Joe Perches365dd4e2014-08-06 16:10:42 -07002674# check for multiple consecutive blank lines
2675 if ($prevline =~ /^[\+ ]\s*$/ &&
2676 $line =~ /^\+\s*$/ &&
2677 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002678 if (CHK("LINE_SPACING",
2679 "Please don't use multiple blank lines\n" . $hereprev) &&
2680 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002681 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002682 }
2683
Joe Perches365dd4e2014-08-06 16:10:42 -07002684 $last_blank_line = $linenr;
2685 }
2686
Joe Perches3b617e32014-04-03 14:49:28 -07002687# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002688 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2689 # actual declarations
2690 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002691 # function pointer declarations
2692 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002693 # foo bar; where foo is some local typedef or #define
2694 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2695 # known declaration macros
2696 $prevline =~ /^\+\s+$declaration_macros/) &&
2697 # for "else if" which can look like "$Ident $Ident"
2698 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2699 # other possible extensions of declaration lines
2700 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2701 # not starting a section or a macro "\" extended line
2702 $prevline =~ /(?:\{\s*|\\)$/) &&
2703 # looks like a declaration
2704 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002705 # function pointer declarations
2706 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002707 # foo bar; where foo is some local typedef or #define
2708 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2709 # known declaration macros
2710 $sline =~ /^\+\s+$declaration_macros/ ||
2711 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002712 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002713 # start or end of block or continuation of declaration
2714 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2715 # bitfield continuation
2716 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2717 # other possible extensions of declaration lines
2718 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2719 # indentation of previous and current line are the same
2720 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002721 if (WARN("LINE_SPACING",
2722 "Missing a blank line after declarations\n" . $hereprev) &&
2723 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002724 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002725 }
Joe Perches3b617e32014-04-03 14:49:28 -07002726 }
2727
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002728# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002729# Exceptions:
2730# 1) within comments
2731# 2) indented preprocessor commands
2732# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002733 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002734 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002735 if (WARN("LEADING_SPACE",
2736 "please, no spaces at the start of a line\n" . $herevet) &&
2737 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002738 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002739 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002740 }
2741
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002742# check we are in a valid C source file if not then ignore this hunk
2743 next if ($realfile !~ /\.(h|c)$/);
2744
Joe Perches032a4c02014-08-06 16:10:29 -07002745# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002746# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002747# if the previous line is a break or return and is indented 1 tab more...
2748 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2749 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002750 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2751 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2752 defined $lines[$linenr] &&
2753 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002754 WARN("UNNECESSARY_ELSE",
2755 "else is not generally useful after a break or return\n" . $hereprev);
2756 }
2757 }
2758
Joe Perchesc00df192014-08-06 16:11:01 -07002759# check indentation of a line with a break;
2760# if the previous line is a goto or return and is indented the same # of tabs
2761 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2762 my $tabs = $1;
2763 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2764 WARN("UNNECESSARY_BREAK",
2765 "break is not useful after a goto or return\n" . $hereprev);
2766 }
2767 }
2768
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002769# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2770 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2771 WARN("CONFIG_EXPERIMENTAL",
2772 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2773 }
2774
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002775# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002776 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002777 WARN("CVS_KEYWORD",
2778 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002779 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002780
Mike Frysinger42e41c52009-09-21 17:04:40 -07002781# Blackfin: don't use __builtin_bfin_[cs]sync
2782 if ($line =~ /__builtin_bfin_csync/) {
2783 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002784 ERROR("CSYNC",
2785 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002786 }
2787 if ($line =~ /__builtin_bfin_ssync/) {
2788 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002789 ERROR("SSYNC",
2790 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002791 }
2792
Joe Perches56e77d72013-02-21 16:44:14 -08002793# check for old HOTPLUG __dev<foo> section markings
2794 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2795 WARN("HOTPLUG_SECTION",
2796 "Using $1 is unnecessary\n" . $herecurr);
2797 }
2798
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002799# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002800 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2801 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002802#print "LINE<$line>\n";
2803 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002804 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002805 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002806 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002807 $stat =~ s/\n./\n /g;
2808 $cond =~ s/\n./\n /g;
2809
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002810#print "linenr<$linenr> <$stat>\n";
2811 # If this statement has no statement boundaries within
2812 # it there is no point in retrying a statement scan
2813 # until we hit end of it.
2814 my $frag = $stat; $frag =~ s/;+\s*$//;
2815 if ($frag !~ /(?:{|;)/) {
2816#print "skip<$line_nr_next>\n";
2817 $suppress_statement = $line_nr_next;
2818 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002819
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002820 # Find the real next line.
2821 $realline_next = $line_nr_next;
2822 if (defined $realline_next &&
2823 (!defined $lines[$realline_next - 1] ||
2824 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2825 $realline_next++;
2826 }
2827
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002828 my $s = $stat;
2829 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002830
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002831 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002832 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002833
2834 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002835 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002836
Andy Whitcroft463f2862009-09-21 17:04:34 -07002837 } elsif ($s =~ /^.\s*else\b/s) {
2838
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002839 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002840 } 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 -07002841 my $type = $1;
2842 $type =~ s/\s+/ /g;
2843 possible($type, "A:" . $s);
2844
Andy Whitcroft8905a672007-11-28 16:21:06 -08002845 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002846 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002847 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002848 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002849
2850 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002851 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002852 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002853 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002854
2855 # Check for any sort of function declaration.
2856 # int foo(something bar, other baz);
2857 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002858 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 -08002859 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002860
Andy Whitcroftcf655042008-03-04 14:28:20 -08002861 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002862 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002863 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002864
Andy Whitcroft8905a672007-11-28 16:21:06 -08002865 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002866 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002867
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002868 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002869 }
2870 }
2871 }
2872
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002873 }
2874
Andy Whitcroft653d4872007-06-23 17:16:34 -07002875#
2876# Checks which may be anchored in the context.
2877#
2878
2879# Check for switch () and associated case and default
2880# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002881 if ($line=~/\bswitch\s*\(.*\)/) {
2882 my $err = '';
2883 my $sep = '';
2884 my @ctx = ctx_block_outer($linenr, $realcnt);
2885 shift(@ctx);
2886 for my $ctx (@ctx) {
2887 my ($clen, $cindent) = line_stats($ctx);
2888 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2889 $indent != $cindent) {
2890 $err .= "$sep$ctx\n";
2891 $sep = '';
2892 } else {
2893 $sep = "[...]\n";
2894 }
2895 }
2896 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002897 ERROR("SWITCH_CASE_INDENT_LEVEL",
2898 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002899 }
2900 }
2901
2902# if/while/etc brace do not go on next line, unless defining a do while loop,
2903# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002904 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 -07002905 my $pre_ctx = "$1$2";
2906
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002907 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002908
2909 if ($line =~ /^\+\t{6,}/) {
2910 WARN("DEEP_INDENTATION",
2911 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2912 }
2913
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002914 my $ctx_cnt = $realcnt - $#ctx - 1;
2915 my $ctx = join("\n", @ctx);
2916
Andy Whitcroft548596d2008-07-23 21:29:01 -07002917 my $ctx_ln = $linenr;
2918 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002919
Andy Whitcroft548596d2008-07-23 21:29:01 -07002920 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2921 defined $lines[$ctx_ln - 1] &&
2922 $lines[$ctx_ln - 1] =~ /^-/)) {
2923 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2924 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002925 $ctx_ln++;
2926 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002927
Andy Whitcroft53210162008-07-23 21:29:03 -07002928 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2929 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002930
Joe Perchesd752fcc2014-08-06 16:11:05 -07002931 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002932 ERROR("OPEN_BRACE",
2933 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002934 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002935 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002936 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2937 $ctx =~ /\)\s*\;\s*$/ &&
2938 defined $lines[$ctx_ln - 1])
2939 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002940 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2941 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002942 WARN("TRAILING_SEMICOLON",
2943 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002944 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002945 }
2946 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002947 }
2948
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002949# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002950 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 -08002951 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2952 ctx_statement_block($linenr, $realcnt, 0)
2953 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002954 my ($s, $c) = ($stat, $cond);
2955
2956 substr($s, 0, length($c), '');
2957
2958 # Make sure we remove the line prefixes as we have
2959 # none on the first line, and are going to readd them
2960 # where necessary.
2961 $s =~ s/\n./\n/gs;
2962
2963 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002964 my @newlines = ($c =~ /\n/gs);
2965 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002966
2967 # We want to check the first line inside the block
2968 # starting at the end of the conditional, so remove:
2969 # 1) any blank line termination
2970 # 2) any opening brace { on end of the line
2971 # 3) any do (...) {
2972 my $continuation = 0;
2973 my $check = 0;
2974 $s =~ s/^.*\bdo\b//;
2975 $s =~ s/^\s*{//;
2976 if ($s =~ s/^\s*\\//) {
2977 $continuation = 1;
2978 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002979 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002980 $check = 1;
2981 $cond_lines++;
2982 }
2983
2984 # Also ignore a loop construct at the end of a
2985 # preprocessor statement.
2986 if (($prevline =~ /^.\s*#\s*define\s/ ||
2987 $prevline =~ /\\\s*$/) && $continuation == 0) {
2988 $check = 0;
2989 }
2990
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002991 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002992 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002993 while ($cond_ptr != $cond_lines) {
2994 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002995
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002996 # If we see an #else/#elif then the code
2997 # is not linear.
2998 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2999 $check = 0;
3000 }
3001
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003002 # Ignore:
3003 # 1) blank lines, they should be at 0,
3004 # 2) preprocessor lines, and
3005 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003006 if ($continuation ||
3007 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003008 $s =~ /^\s*#\s*?/ ||
3009 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003010 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003011 if ($s =~ s/^.*?\n//) {
3012 $cond_lines++;
3013 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003014 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003015 }
3016
3017 my (undef, $sindent) = line_stats("+" . $s);
3018 my $stat_real = raw_line($linenr, $cond_lines);
3019
3020 # Check if either of these lines are modified, else
3021 # this is not this patch's fault.
3022 if (!defined($stat_real) ||
3023 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3024 $check = 0;
3025 }
3026 if (defined($stat_real) && $cond_lines > 1) {
3027 $stat_real = "[...]\n$stat_real";
3028 }
3029
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003030 #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 -07003031
3032 if ($check && (($sindent % 8) != 0 ||
3033 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003034 WARN("SUSPECT_CODE_INDENT",
3035 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003036 }
3037 }
3038
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003039 # Track the 'values' across context and added lines.
3040 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003041 my ($curr_values, $curr_vars) =
3042 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003043 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003044 if ($dbg_values) {
3045 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003046 print "$linenr > .$outline\n";
3047 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003048 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003049 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003050 $prev_values = substr($curr_values, -1);
3051
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003052#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003053 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003054
Andy Whitcroft653d4872007-06-23 17:16:34 -07003055# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003056 if ($dbg_type) {
3057 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003058 ERROR("TEST_TYPE",
3059 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003060 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003061 ERROR("TEST_NOT_TYPE",
3062 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003063 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003064 next;
3065 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003066# TEST: allow direct testing of the attribute matcher.
3067 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003068 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003069 ERROR("TEST_ATTR",
3070 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003071 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003072 ERROR("TEST_NOT_ATTR",
3073 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003074 }
3075 next;
3076 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003077
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003078# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003079 if ($line =~ /^.\s*{/ &&
3080 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003081 if (ERROR("OPEN_BRACE",
3082 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003083 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3084 fix_delete_line($fixlinenr - 1, $prevrawline);
3085 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003086 my $fixedline = $prevrawline;
3087 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003088 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003089 $fixedline = $line;
3090 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003091 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003092 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003093 }
3094
Andy Whitcroft653d4872007-06-23 17:16:34 -07003095#
3096# Checks which are anchored on the added line.
3097#
3098
3099# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003100 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003101 my $path = $1;
3102 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003103 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003104 "malformed #include filename\n" . $herecurr);
3105 }
3106 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3107 ERROR("UAPI_INCLUDE",
3108 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003109 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003110 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003111
3112# no C99 // comments
3113 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003114 if (ERROR("C99_COMMENTS",
3115 "do not use C99 // comments\n" . $herecurr) &&
3116 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003117 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003118 if ($line =~ /\/\/(.*)$/) {
3119 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003120 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003121 }
3122 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003123 }
3124 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003125 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003126 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003127
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003128# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3129# the whole statement.
3130#print "APW <$lines[$realline_next - 1]>\n";
3131 if (defined $realline_next &&
3132 exists $lines[$realline_next - 1] &&
3133 !defined $suppress_export{$realline_next} &&
3134 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3135 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003136 # Handle definitions which produce identifiers with
3137 # a prefix:
3138 # XXX(foo);
3139 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003140 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003141 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003142 $name =~ /^${Ident}_$2/) {
3143#print "FOO C name<$name>\n";
3144 $suppress_export{$realline_next} = 1;
3145
3146 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003147 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003148 ^.DEFINE_$Ident\(\Q$name\E\)|
3149 ^.DECLARE_$Ident\(\Q$name\E\)|
3150 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003151 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3152 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003153 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003154#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3155 $suppress_export{$realline_next} = 2;
3156 } else {
3157 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003158 }
3159 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003160 if (!defined $suppress_export{$linenr} &&
3161 $prevline =~ /^.\s*$/ &&
3162 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3163 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3164#print "FOO B <$lines[$linenr - 1]>\n";
3165 $suppress_export{$linenr} = 2;
3166 }
3167 if (defined $suppress_export{$linenr} &&
3168 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003169 WARN("EXPORT_SYMBOL",
3170 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003171 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003172
Joe Eloff5150bda2010-08-09 17:21:00 -07003173# check for global initialisers.
Joe Perches5129e872015-06-10 11:14:52 -07003174 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003175 if (ERROR("GLOBAL_INITIALISERS",
3176 "do not initialise globals to 0 or NULL\n" .
3177 $herecurr) &&
3178 $fix) {
Joe Perches5129e872015-06-10 11:14:52 -07003179 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003180 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003181 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003182# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003183 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3184 if (ERROR("INITIALISED_STATIC",
3185 "do not initialise statics to 0 or NULL\n" .
3186 $herecurr) &&
3187 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003188 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003189 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003190 }
3191
Joe Perches18130872014-08-06 16:11:22 -07003192# check for misordered declarations of char/short/int/long with signed/unsigned
3193 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3194 my $tmp = trim($1);
3195 WARN("MISORDERED_TYPE",
3196 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3197 }
3198
Joe Perchescb710ec2010-10-26 14:23:20 -07003199# check for static const char * arrays.
3200 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003201 WARN("STATIC_CONST_CHAR_ARRAY",
3202 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003203 $herecurr);
3204 }
3205
3206# check for static char foo[] = "bar" declarations.
3207 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003208 WARN("STATIC_CONST_CHAR_ARRAY",
3209 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003210 $herecurr);
3211 }
3212
Joe Perchesab7e23f2015-04-16 12:44:22 -07003213# check for const <foo> const where <foo> is not a pointer or array type
3214 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3215 my $found = $1;
3216 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3217 WARN("CONST_CONST",
3218 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3219 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3220 WARN("CONST_CONST",
3221 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3222 }
3223 }
3224
Joe Perches9b0fa602014-04-03 14:49:18 -07003225# check for non-global char *foo[] = {"bar", ...} declarations.
3226 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3227 WARN("STATIC_CONST_CHAR_ARRAY",
3228 "char * array declaration might be better as static const\n" .
3229 $herecurr);
3230 }
3231
Joe Perchesb598b672015-04-16 12:44:36 -07003232# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3233 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3234 my $array = $1;
3235 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3236 my $array_div = $1;
3237 if (WARN("ARRAY_SIZE",
3238 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3239 $fix) {
3240 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3241 }
3242 }
3243 }
3244
Joe Perchesb36190c2014-01-27 17:07:18 -08003245# check for function declarations without arguments like "int foo()"
3246 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3247 if (ERROR("FUNCTION_WITHOUT_ARGS",
3248 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3249 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003250 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003251 }
3252 }
3253
Joe Perches92e112f2013-12-13 11:36:22 -07003254# check for uses of DEFINE_PCI_DEVICE_TABLE
3255 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3256 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3257 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3258 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003259 $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 -07003260 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003261 }
3262
Andy Whitcroft653d4872007-06-23 17:16:34 -07003263# check for new typedefs, only function parameters and sparse annotations
3264# make sense.
3265 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003266 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003267 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003268 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003269 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003270 WARN("NEW_TYPEDEFS",
3271 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003272 }
3273
3274# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003275 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003276 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3277 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003278 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003279
Andy Whitcroft65863862009-01-06 14:41:21 -08003280 # Should start with a space.
3281 $to =~ s/^(\S)/ $1/;
3282 # Should not end with a space.
3283 $to =~ s/\s+$//;
3284 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003285 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003286 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003287
Joe Perches3705ce52013-07-03 15:05:31 -07003288## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003289 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003290 if (ERROR("POINTER_LOCATION",
3291 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3292 $fix) {
3293 my $sub_from = $ident;
3294 my $sub_to = $ident;
3295 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003296 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003297 s@\Q$sub_from\E@$sub_to@;
3298 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003299 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003300 }
3301 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3302 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003303 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003304
Andy Whitcroft65863862009-01-06 14:41:21 -08003305 # Should start with a space.
3306 $to =~ s/^(\S)/ $1/;
3307 # Should not end with a space.
3308 $to =~ s/\s+$//;
3309 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003310 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003311 }
3312 # Modifiers should have spaces.
3313 $to =~ s/(\b$Modifier$)/$1 /;
3314
Joe Perches3705ce52013-07-03 15:05:31 -07003315## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003316 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003317 if (ERROR("POINTER_LOCATION",
3318 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3319 $fix) {
3320
3321 my $sub_from = $match;
3322 my $sub_to = $match;
3323 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003324 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003325 s@\Q$sub_from\E@$sub_to@;
3326 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003327 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003328 }
3329
3330# # no BUG() or BUG_ON()
3331# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3332# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3333# print "$herecurr";
3334# $clean = 0;
3335# }
3336
Andy Whitcroft8905a672007-11-28 16:21:06 -08003337 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003338 WARN("LINUX_VERSION_CODE",
3339 "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 -08003340 }
3341
Joe Perches17441222011-06-15 15:08:17 -07003342# check for uses of printk_ratelimit
3343 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003344 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003345 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003346 }
3347
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003348# printk should use KERN_* levels. Note that follow on printk's on the
3349# same line do not need a level, so we use the current block context
3350# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003351# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003352# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003353 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003354 my $ok = 0;
3355 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3356 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003357 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003358 # with "\n" ignore it, else it is to blame
3359 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3360 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3361 $ok = 1;
3362 }
3363 last;
3364 }
3365 }
3366 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003367 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3368 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003369 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003370 }
3371
Joe Perches243f3802012-05-31 16:26:09 -07003372 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3373 my $orig = $1;
3374 my $level = lc($orig);
3375 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003376 my $level2 = $level;
3377 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003378 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003379 "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 -07003380 }
3381
3382 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003383 if (WARN("PREFER_PR_LEVEL",
3384 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3385 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003386 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003387 s/\bpr_warning\b/pr_warn/;
3388 }
Joe Perches243f3802012-05-31 16:26:09 -07003389 }
3390
Joe Perchesdc139312013-02-21 16:44:13 -08003391 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3392 my $orig = $1;
3393 my $level = lc($orig);
3394 $level = "warn" if ($level eq "warning");
3395 $level = "dbg" if ($level eq "debug");
3396 WARN("PREFER_DEV_LEVEL",
3397 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3398 }
3399
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003400# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3401# number of false positives, but assembly files are not checked, so at
3402# least the arch entry code will not trigger this warning.
3403 if ($line =~ /\bENOSYS\b/) {
3404 WARN("ENOSYS",
3405 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3406 }
3407
Andy Whitcroft653d4872007-06-23 17:16:34 -07003408# function brace can't be on same line, except for #defines of do while,
3409# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003410 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003411 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003412 if (ERROR("OPEN_BRACE",
3413 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3414 $fix) {
3415 fix_delete_line($fixlinenr, $rawline);
3416 my $fixed_line = $rawline;
3417 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3418 my $line1 = $1;
3419 my $line2 = $2;
3420 fix_insert_line($fixlinenr, ltrim($line1));
3421 fix_insert_line($fixlinenr, "\+{");
3422 if ($line2 !~ /^\s*$/) {
3423 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3424 }
3425 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003426 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003427
Andy Whitcroft8905a672007-11-28 16:21:06 -08003428# open braces for enum, union and struct go on the same line.
3429 if ($line =~ /^.\s*{/ &&
3430 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003431 if (ERROR("OPEN_BRACE",
3432 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3433 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3434 fix_delete_line($fixlinenr - 1, $prevrawline);
3435 fix_delete_line($fixlinenr, $rawline);
3436 my $fixedline = rtrim($prevrawline) . " {";
3437 fix_insert_line($fixlinenr, $fixedline);
3438 $fixedline = $rawline;
3439 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3440 if ($fixedline !~ /^\+\s*$/) {
3441 fix_insert_line($fixlinenr, $fixedline);
3442 }
3443 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003444 }
3445
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003446# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003447 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3448 if (WARN("SPACING",
3449 "missing space after $1 definition\n" . $herecurr) &&
3450 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003451 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003452 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3453 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003454 }
3455
Joe Perches31070b52014-01-23 15:54:49 -08003456# Function pointer declarations
3457# check spacing between type, funcptr, and args
3458# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003459 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003460 my $declare = $1;
3461 my $pre_pointer_space = $2;
3462 my $post_pointer_space = $3;
3463 my $funcname = $4;
3464 my $post_funcname_space = $5;
3465 my $pre_args_space = $6;
3466
Joe Perches91f72e92014-04-03 14:49:12 -07003467# the $Declare variable will capture all spaces after the type
3468# so check it for a missing trailing missing space but pointer return types
3469# don't need a space so don't warn for those.
3470 my $post_declare_space = "";
3471 if ($declare =~ /(\s+)$/) {
3472 $post_declare_space = $1;
3473 $declare = rtrim($declare);
3474 }
3475 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003476 WARN("SPACING",
3477 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003478 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003479 }
3480
3481# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003482# This test is not currently implemented because these declarations are
3483# equivalent to
3484# int foo(int bar, ...)
3485# and this is form shouldn't/doesn't generate a checkpatch warning.
3486#
3487# elsif ($declare =~ /\s{2,}$/) {
3488# WARN("SPACING",
3489# "Multiple spaces after return type\n" . $herecurr);
3490# }
Joe Perches31070b52014-01-23 15:54:49 -08003491
3492# unnecessary space "type ( *funcptr)(args...)"
3493 if (defined $pre_pointer_space &&
3494 $pre_pointer_space =~ /^\s/) {
3495 WARN("SPACING",
3496 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3497 }
3498
3499# unnecessary space "type (* funcptr)(args...)"
3500 if (defined $post_pointer_space &&
3501 $post_pointer_space =~ /^\s/) {
3502 WARN("SPACING",
3503 "Unnecessary space before function pointer name\n" . $herecurr);
3504 }
3505
3506# unnecessary space "type (*funcptr )(args...)"
3507 if (defined $post_funcname_space &&
3508 $post_funcname_space =~ /^\s/) {
3509 WARN("SPACING",
3510 "Unnecessary space after function pointer name\n" . $herecurr);
3511 }
3512
3513# unnecessary space "type (*funcptr) (args...)"
3514 if (defined $pre_args_space &&
3515 $pre_args_space =~ /^\s/) {
3516 WARN("SPACING",
3517 "Unnecessary space before function pointer arguments\n" . $herecurr);
3518 }
3519
3520 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003521 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003522 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003523 }
3524 }
3525
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003526# check for spacing round square brackets; allowed:
3527# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003528# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3529# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003530 while ($line =~ /(.*?\s)\[/g) {
3531 my ($where, $prefix) = ($-[1], $1);
3532 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003533 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003534 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003535 if (ERROR("BRACKET_SPACE",
3536 "space prohibited before open square bracket '['\n" . $herecurr) &&
3537 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003538 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003539 s/^(\+.*?)\s+\[/$1\[/;
3540 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003541 }
3542 }
3543
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003544# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003545 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003546 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003547 my $ctx_before = substr($line, 0, $-[1]);
3548 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003549
3550 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003551 if ($name =~ /^(?:
3552 if|for|while|switch|return|case|
3553 volatile|__volatile__|
3554 __attribute__|format|__extension__|
3555 asm|__asm__)$/x)
3556 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003557 # cpp #define statements have non-optional spaces, ie
3558 # if there is a space between the name and the open
3559 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003560 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003561
3562 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003563 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003564
3565 # If this whole things ends with a type its most
3566 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003567 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003568
3569 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003570 if (WARN("SPACING",
3571 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3572 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003573 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003574 s/\b$name\s+\(/$name\(/;
3575 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003576 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003577 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003578
Andy Whitcroft653d4872007-06-23 17:16:34 -07003579# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003580 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003581 my $fixed_line = "";
3582 my $line_fixed = 0;
3583
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003584 my $ops = qr{
3585 <<=|>>=|<=|>=|==|!=|
3586 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3587 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003588 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003589 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003590 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003591 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003592
3593## print("element count: <" . $#elements . ">\n");
3594## foreach my $el (@elements) {
3595## print("el: <$el>\n");
3596## }
3597
3598 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003599 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003600
Joe Perches3705ce52013-07-03 15:05:31 -07003601 foreach my $el (@elements) {
3602 push(@fix_elements, substr($rawline, $off, length($el)));
3603 $off += length($el);
3604 }
3605
3606 $off = 0;
3607
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003608 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003609 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003610
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003611 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003612
3613 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3614
3615## print("n: <$n> good: <$good>\n");
3616
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003617 $off += length($elements[$n]);
3618
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003619 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003620 my $ca = substr($opline, 0, $off);
3621 my $cc = '';
3622 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3623 $cc = substr($opline, $off + length($elements[$n + 1]));
3624 }
3625 my $cb = "$ca$;$cc";
3626
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003627 my $a = '';
3628 $a = 'V' if ($elements[$n] ne '');
3629 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003630 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003631 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3632 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003633 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003634
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003635 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003636
3637 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003638 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003639 $c = 'V' if ($elements[$n + 2] ne '');
3640 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003641 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003642 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3643 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003644 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003645 } else {
3646 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003647 }
3648
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003649 my $ctx = "${a}x${c}";
3650
3651 my $at = "(ctx:$ctx)";
3652
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003653 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003654 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003655
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003656 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003657 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003658
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003659 # Get the full operator variant.
3660 my $opv = $op . substr($curr_vars, $off, 1);
3661
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003662 # Ignore operators passed as parameters.
3663 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07003664 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003665
Andy Whitcroftcf655042008-03-04 14:28:20 -08003666# # Ignore comments
3667# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003668
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003669 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003670 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003671 if ($ctx !~ /.x[WEBC]/ &&
3672 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003673 if (ERROR("SPACING",
3674 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003675 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003676 $line_fixed = 1;
3677 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003678 }
3679
3680 # // is a comment
3681 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003682
Joe Perchesb00e4812014-04-03 14:49:33 -07003683 # : when part of a bitfield
3684 } elsif ($opv eq ':B') {
3685 # skip the bitfield test for now
3686
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003687 # No spaces for:
3688 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003689 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003690 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003691 if (ERROR("SPACING",
3692 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003693 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003694 if (defined $fix_elements[$n + 2]) {
3695 $fix_elements[$n + 2] =~ s/^\s+//;
3696 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003697 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003698 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003699 }
3700
Joe Perches23810972014-12-10 15:51:32 -08003701 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003702 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003703 my $rtrim_before = 0;
3704 my $space_after = 0;
3705 if ($ctx =~ /Wx./) {
3706 if (ERROR("SPACING",
3707 "space prohibited before that '$op' $at\n" . $hereptr)) {
3708 $line_fixed = 1;
3709 $rtrim_before = 1;
3710 }
3711 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003712 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003713 if (ERROR("SPACING",
3714 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003715 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003716 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003717 $space_after = 1;
3718 }
3719 }
3720 if ($rtrim_before || $space_after) {
3721 if ($rtrim_before) {
3722 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3723 } else {
3724 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3725 }
3726 if ($space_after) {
3727 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003728 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003729 }
3730
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003731 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003732 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003733 #warn "'*' is part of type\n";
3734
3735 # unary operators should have a space before and
3736 # none after. May be left adjacent to another
3737 # unary operator, or a cast
3738 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003739 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003740 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003741 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003742 if (ERROR("SPACING",
3743 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003744 if ($n != $last_after + 2) {
3745 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3746 $line_fixed = 1;
3747 }
Joe Perches3705ce52013-07-03 15:05:31 -07003748 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003749 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003750 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003751 # A unary '*' may be const
3752
3753 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003754 if (ERROR("SPACING",
3755 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003756 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003757 if (defined $fix_elements[$n + 2]) {
3758 $fix_elements[$n + 2] =~ s/^\s+//;
3759 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003760 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003761 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003762 }
3763
3764 # unary ++ and unary -- are allowed no space on one side.
3765 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003766 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003767 if (ERROR("SPACING",
3768 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003769 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003770 $line_fixed = 1;
3771 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003772 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003773 if ($ctx =~ /Wx[BE]/ ||
3774 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003775 if (ERROR("SPACING",
3776 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003777 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003778 $line_fixed = 1;
3779 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003780 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003781 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003782 if (ERROR("SPACING",
3783 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003784 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003785 if (defined $fix_elements[$n + 2]) {
3786 $fix_elements[$n + 2] =~ s/^\s+//;
3787 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003788 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003789 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003790 }
3791
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003792 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003793 } elsif ($op eq '<<' or $op eq '>>' or
3794 $op eq '&' or $op eq '^' or $op eq '|' or
3795 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003796 $op eq '*' or $op eq '/' or
3797 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003798 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08003799 if ($check) {
3800 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3801 if (CHK("SPACING",
3802 "spaces preferred around that '$op' $at\n" . $hereptr)) {
3803 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3804 $fix_elements[$n + 2] =~ s/^\s+//;
3805 $line_fixed = 1;
3806 }
3807 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3808 if (CHK("SPACING",
3809 "space preferred before that '$op' $at\n" . $hereptr)) {
3810 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3811 $line_fixed = 1;
3812 }
3813 }
3814 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003815 if (ERROR("SPACING",
3816 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003817 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3818 if (defined $fix_elements[$n + 2]) {
3819 $fix_elements[$n + 2] =~ s/^\s+//;
3820 }
Joe Perches3705ce52013-07-03 15:05:31 -07003821 $line_fixed = 1;
3822 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003823 }
3824
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003825 # A colon needs no spaces before when it is
3826 # terminating a case value or a label.
3827 } elsif ($opv eq ':C' || $opv eq ':L') {
3828 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003829 if (ERROR("SPACING",
3830 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003831 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003832 $line_fixed = 1;
3833 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003834 }
3835
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003836 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003837 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003838 my $ok = 0;
3839
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003840 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003841 if (($op eq '<' &&
3842 $cc =~ /^\S+\@\S+>/) ||
3843 ($op eq '>' &&
3844 $ca =~ /<\S+\@\S+$/))
3845 {
3846 $ok = 1;
3847 }
3848
Joe Perchese0df7e12015-04-16 12:44:53 -07003849 # for asm volatile statements
3850 # ignore a colon with another
3851 # colon immediately before or after
3852 if (($op eq ':') &&
3853 ($ca =~ /:$/ || $cc =~ /^:/)) {
3854 $ok = 1;
3855 }
3856
Joe Perches84731622013-11-12 15:10:05 -08003857 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003858 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003859 my $msg_type = \&ERROR;
3860 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3861
3862 if (&{$msg_type}("SPACING",
3863 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003864 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3865 if (defined $fix_elements[$n + 2]) {
3866 $fix_elements[$n + 2] =~ s/^\s+//;
3867 }
Joe Perches3705ce52013-07-03 15:05:31 -07003868 $line_fixed = 1;
3869 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003870 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003871 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003872 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003873
3874## print("n: <$n> GOOD: <$good>\n");
3875
3876 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003877 }
Joe Perches3705ce52013-07-03 15:05:31 -07003878
3879 if (($#elements % 2) == 0) {
3880 $fixed_line = $fixed_line . $fix_elements[$#elements];
3881 }
3882
Joe Perches194f66f2014-08-06 16:11:03 -07003883 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3884 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003885 }
3886
3887
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003888 }
3889
Joe Perches786b6322013-07-03 15:05:32 -07003890# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003891 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003892 if (WARN("SPACING",
3893 "space prohibited before semicolon\n" . $herecurr) &&
3894 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003895 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003896 s/^(\+.*\S)\s+;/$1;/;
3897 }
3898 }
3899
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003900# check for multiple assignments
3901 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003902 CHK("MULTIPLE_ASSIGNMENTS",
3903 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003904 }
3905
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003906## # check for multiple declarations, allowing for a function declaration
3907## # continuation.
3908## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3909## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3910##
3911## # Remove any bracketed sections to ensure we do not
3912## # falsly report the parameters of functions.
3913## my $ln = $line;
3914## while ($ln =~ s/\([^\(\)]*\)//g) {
3915## }
3916## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003917## WARN("MULTIPLE_DECLARATION",
3918## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003919## }
3920## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003921
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003922#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003923 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3924 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003925 if (ERROR("SPACING",
3926 "space required before the open brace '{'\n" . $herecurr) &&
3927 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003928 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003929 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003930 }
3931
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003932## # check for blank lines before declarations
3933## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3934## $prevrawline =~ /^.\s*$/) {
3935## WARN("SPACING",
3936## "No blank lines before declarations\n" . $hereprev);
3937## }
3938##
3939
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003940# closing brace should have a space following it when it has anything
3941# on the line
3942 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003943 if (ERROR("SPACING",
3944 "space required after that close brace '}'\n" . $herecurr) &&
3945 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003946 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003947 s/}((?!(?:,|;|\)))\S)/} $1/;
3948 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003949 }
3950
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003951# check spacing on square brackets
3952 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003953 if (ERROR("SPACING",
3954 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3955 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003956 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003957 s/\[\s+/\[/;
3958 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003959 }
3960 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003961 if (ERROR("SPACING",
3962 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3963 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003964 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003965 s/\s+\]/\]/;
3966 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003967 }
3968
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003969# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003970 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3971 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003972 if (ERROR("SPACING",
3973 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3974 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003975 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003976 s/\(\s+/\(/;
3977 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003978 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003979 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003980 $line !~ /for\s*\(.*;\s+\)/ &&
3981 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003982 if (ERROR("SPACING",
3983 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3984 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003985 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003986 s/\s+\)/\)/;
3987 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003988 }
3989
Joe Perchese2826fd2014-08-06 16:10:48 -07003990# check unnecessary parentheses around addressof/dereference single $Lvals
3991# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3992
3993 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003994 my $var = $1;
3995 if (CHK("UNNECESSARY_PARENTHESES",
3996 "Unnecessary parentheses around $var\n" . $herecurr) &&
3997 $fix) {
3998 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3999 }
4000 }
4001
4002# check for unnecessary parentheses around function pointer uses
4003# ie: (foo->bar)(); should be foo->bar();
4004# but not "if (foo->bar) (" to avoid some false positives
4005 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4006 my $var = $2;
4007 if (CHK("UNNECESSARY_PARENTHESES",
4008 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4009 $fix) {
4010 my $var2 = deparenthesize($var);
4011 $var2 =~ s/\s//g;
4012 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4013 }
4014 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004015
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004016#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004017 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004018 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004019 if (WARN("INDENTED_LABEL",
4020 "labels should not be indented\n" . $herecurr) &&
4021 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004022 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004023 s/^(.)\s+/$1/;
4024 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004025 }
4026
Joe Perches5b9553a2014-04-03 14:49:21 -07004027# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004028 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004029 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004030 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004031 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4032 my $value = $1;
4033 $value = deparenthesize($value);
4034 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4035 ERROR("RETURN_PARENTHESES",
4036 "return is not a function, parentheses are not required\n" . $herecurr);
4037 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004038 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004039 ERROR("SPACING",
4040 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004041 }
4042 }
Joe Perches507e5142013-11-12 15:10:13 -08004043
Joe Perchesb43ae212014-06-23 13:22:07 -07004044# unnecessary return in a void function
4045# at end-of-function, with the previous line a single leading tab, then return;
4046# and the line before that not a goto label target like "out:"
4047 if ($sline =~ /^[ \+]}\s*$/ &&
4048 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4049 $linenr >= 3 &&
4050 $lines[$linenr - 3] =~ /^[ +]/ &&
4051 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004052 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004053 "void function return statements are not generally useful\n" . $hereprev);
4054 }
Joe Perches9819cf22014-06-04 16:12:09 -07004055
Joe Perches189248d2014-01-23 15:54:47 -08004056# if statements using unnecessary parentheses - ie: if ((foo == bar))
4057 if ($^V && $^V ge 5.10.0 &&
4058 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4059 my $openparens = $1;
4060 my $count = $openparens =~ tr@\(@\(@;
4061 my $msg = "";
4062 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4063 my $comp = $4; #Not $1 because of $LvalOrFunc
4064 $msg = " - maybe == should be = ?" if ($comp eq "==");
4065 WARN("UNNECESSARY_PARENTHESES",
4066 "Unnecessary parentheses$msg\n" . $herecurr);
4067 }
4068 }
4069
Joe Perchesf34e4a42015-04-16 12:44:19 -07004070# Return of what appears to be an errno should normally be negative
4071 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004072 my $name = $1;
4073 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004074 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004075 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004076 }
4077 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004078
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004079# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004080 if ($line =~ /\b(if|while|for|switch)\(/) {
4081 if (ERROR("SPACING",
4082 "space required before the open parenthesis '('\n" . $herecurr) &&
4083 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004084 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004085 s/\b(if|while|for|switch)\(/$1 \(/;
4086 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004087 }
4088
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004089# Check for illegal assignment in if conditional -- and check for trailing
4090# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004091 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004092 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4093 ctx_statement_block($linenr, $realcnt, 0)
4094 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004095 my ($stat_next) = ctx_statement_block($line_nr_next,
4096 $remain_next, $off_next);
4097 $stat_next =~ s/\n./\n /g;
4098 ##print "stat<$stat> stat_next<$stat_next>\n";
4099
4100 if ($stat_next =~ /^\s*while\b/) {
4101 # If the statement carries leading newlines,
4102 # then count those as offsets.
4103 my ($whitespace) =
4104 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4105 my $offset =
4106 statement_rawlines($whitespace) - 1;
4107
4108 $suppress_whiletrailers{$line_nr_next +
4109 $offset} = 1;
4110 }
4111 }
4112 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004113 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004114 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004115 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004116
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004117 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004118 ERROR("ASSIGN_IN_IF",
4119 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004120 }
4121
4122 # Find out what is on the end of the line after the
4123 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004124 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004125 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004126 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004127 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4128 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004129 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004130 # Find out how long the conditional actually is.
4131 my @newlines = ($c =~ /\n/gs);
4132 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004133 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004134
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004135 $stat_real = raw_line($linenr, $cond_lines)
4136 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004137 if (defined($stat_real) && $cond_lines > 1) {
4138 $stat_real = "[...]\n$stat_real";
4139 }
4140
Joe Perches000d1cc12011-07-25 17:13:25 -07004141 ERROR("TRAILING_STATEMENTS",
4142 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004143 }
4144 }
4145
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004146# Check for bitwise tests written as boolean
4147 if ($line =~ /
4148 (?:
4149 (?:\[|\(|\&\&|\|\|)
4150 \s*0[xX][0-9]+\s*
4151 (?:\&\&|\|\|)
4152 |
4153 (?:\&\&|\|\|)
4154 \s*0[xX][0-9]+\s*
4155 (?:\&\&|\|\||\)|\])
4156 )/x)
4157 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004158 WARN("HEXADECIMAL_BOOLEAN_TEST",
4159 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004160 }
4161
Andy Whitcroft8905a672007-11-28 16:21:06 -08004162# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004163 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4164 my $s = $1;
4165 $s =~ s/$;//g; # Remove any comments
4166 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004167 ERROR("TRAILING_STATEMENTS",
4168 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004169 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004170 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004171# if should not continue a brace
4172 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004173 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004174 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004175 $herecurr);
4176 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004177# case and default should not have general statements after them
4178 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4179 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004180 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004181 \s*return\s+
4182 )/xg)
4183 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004184 ERROR("TRAILING_STATEMENTS",
4185 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004186 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004187
4188 # Check for }<nl>else {, these must be at the same
4189 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004190 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4191 $previndent == $indent) {
4192 if (ERROR("ELSE_AFTER_BRACE",
4193 "else should follow close brace '}'\n" . $hereprev) &&
4194 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4195 fix_delete_line($fixlinenr - 1, $prevrawline);
4196 fix_delete_line($fixlinenr, $rawline);
4197 my $fixedline = $prevrawline;
4198 $fixedline =~ s/}\s*$//;
4199 if ($fixedline !~ /^\+\s*$/) {
4200 fix_insert_line($fixlinenr, $fixedline);
4201 }
4202 $fixedline = $rawline;
4203 $fixedline =~ s/^(.\s*)else/$1} else/;
4204 fix_insert_line($fixlinenr, $fixedline);
4205 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004206 }
4207
Joe Perches8b8856f2014-08-06 16:11:14 -07004208 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4209 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004210 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4211
4212 # Find out what is on the end of the line after the
4213 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004214 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004215 $s =~ s/\n.*//g;
4216
4217 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004218 if (ERROR("WHILE_AFTER_BRACE",
4219 "while should follow close brace '}'\n" . $hereprev) &&
4220 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4221 fix_delete_line($fixlinenr - 1, $prevrawline);
4222 fix_delete_line($fixlinenr, $rawline);
4223 my $fixedline = $prevrawline;
4224 my $trailing = $rawline;
4225 $trailing =~ s/^\+//;
4226 $trailing = trim($trailing);
4227 $fixedline =~ s/}\s*$/} $trailing/;
4228 fix_insert_line($fixlinenr, $fixedline);
4229 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004230 }
4231 }
4232
Joe Perches95e2c602013-07-03 15:05:20 -07004233#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004234 while ($line =~ m{($Constant|$Lval)}g) {
4235 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004236
4237#gcc binary extension
4238 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004239 if (WARN("GCC_BINARY_CONSTANT",
4240 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4241 $fix) {
4242 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004243 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004244 s/\b$var\b/$hexval/;
4245 }
Joe Perches95e2c602013-07-03 15:05:20 -07004246 }
4247
4248#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004249 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004250 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004251#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004252 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004253#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004254 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4255#Ignore some three character SI units explicitly, like MiB and KHz
4256 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004257 while ($var =~ m{($Ident)}g) {
4258 my $word = $1;
4259 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004260 if ($check) {
4261 seed_camelcase_includes();
4262 if (!$file && !$camelcase_file_seeded) {
4263 seed_camelcase_file($realfile);
4264 $camelcase_file_seeded = 1;
4265 }
4266 }
Joe Perches7e781f62013-09-11 14:23:55 -07004267 if (!defined $camelcase{$word}) {
4268 $camelcase{$word} = 1;
4269 CHK("CAMELCASE",
4270 "Avoid CamelCase: <$word>\n" . $herecurr);
4271 }
Joe Perches34456862013-07-03 15:05:34 -07004272 }
Joe Perches323c1262012-12-17 16:02:07 -08004273 }
4274 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004275
4276#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004277 if ($line =~ /\#\s*define.*\\\s+$/) {
4278 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4279 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4280 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004281 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004282 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004283 }
4284
Fabian Frederick0e212e02015-04-16 12:44:25 -07004285# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4286# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004287 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004288 my $file = "$1.h";
4289 my $checkfile = "include/linux/$file";
4290 if (-f "$root/$checkfile" &&
4291 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004292 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004293 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004294 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4295 if ($asminclude > 0) {
4296 if ($realfile =~ m{^arch/}) {
4297 CHK("ARCH_INCLUDE_LINUX",
4298 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4299 } else {
4300 WARN("INCLUDE_LINUX",
4301 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4302 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004303 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004304 }
4305 }
4306
Andy Whitcroft653d4872007-06-23 17:16:34 -07004307# multi-statement macros should be enclosed in a do while loop, grab the
4308# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004309# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004310 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4311 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004312 my $ln = $linenr;
4313 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004314 my ($off, $dstat, $dcond, $rest);
4315 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004316 my $has_flow_statement = 0;
4317 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004318 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004319 ctx_statement_block($linenr, $realcnt, 0);
4320 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004321 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004322 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004323
Joe Perches08a28432014-10-13 15:51:55 -07004324 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4325 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4326
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004327 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004328 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004329 $dstat =~ s/\\\n.//g;
4330 $dstat =~ s/^\s*//s;
4331 $dstat =~ s/\s*$//s;
4332
4333 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004334 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4335 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004336 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004337 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004338 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004339
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004340 # Flatten any obvious string concatentation.
4341 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4342 $dstat =~ s/$Ident\s*("X*")/$1/)
4343 {
4344 }
4345
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004346 my $exceptions = qr{
4347 $Declare|
4348 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004349 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004350 DECLARE_PER_CPU|
4351 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004352 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004353 union|
4354 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004355 \.$Ident\s*=\s*|
4356 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004357 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004358 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004359 if ($dstat ne '' &&
4360 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4361 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004362 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004363 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004364 $dstat !~ /$exceptions/ &&
4365 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004366 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004367 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004368 $dstat !~ /^for\s*$Constant$/ && # for (...)
4369 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4370 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004371 $dstat !~ /^\({/ && # ({...
4372 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004373 {
4374 $ctx =~ s/\n*$//;
4375 my $herectx = $here . "\n";
4376 my $cnt = statement_rawlines($ctx);
4377
4378 for (my $n = 0; $n < $cnt; $n++) {
4379 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004380 }
4381
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004382 if ($dstat =~ /;/) {
4383 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4384 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4385 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004386 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004387 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004388 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004389 }
Joe Perches5023d342012-12-17 16:01:47 -08004390
Joe Perches08a28432014-10-13 15:51:55 -07004391# check for macros with flow control, but without ## concatenation
4392# ## concatenation is commonly a macro that defines a function so ignore those
4393 if ($has_flow_statement && !$has_arg_concat) {
4394 my $herectx = $here . "\n";
4395 my $cnt = statement_rawlines($ctx);
4396
4397 for (my $n = 0; $n < $cnt; $n++) {
4398 $herectx .= raw_line($linenr, $n) . "\n";
4399 }
4400 WARN("MACRO_WITH_FLOW_CONTROL",
4401 "Macros with flow control statements should be avoided\n" . "$herectx");
4402 }
4403
Joe Perches481eb482012-12-17 16:01:56 -08004404# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004405
4406 } else {
4407 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004408 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4409 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004410 $line =~ /^\+.*\\$/) {
4411 WARN("LINE_CONTINUATIONS",
4412 "Avoid unnecessary line continuations\n" . $herecurr);
4413 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004414 }
4415
Joe Perchesb13edf72012-07-30 14:41:24 -07004416# do {} while (0) macro tests:
4417# single-statement macros do not need to be enclosed in do while (0) loop,
4418# macro should not end with a semicolon
4419 if ($^V && $^V ge 5.10.0 &&
4420 $realfile !~ m@/vmlinux.lds.h$@ &&
4421 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4422 my $ln = $linenr;
4423 my $cnt = $realcnt;
4424 my ($off, $dstat, $dcond, $rest);
4425 my $ctx = '';
4426 ($dstat, $dcond, $ln, $cnt, $off) =
4427 ctx_statement_block($linenr, $realcnt, 0);
4428 $ctx = $dstat;
4429
4430 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004431 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004432
4433 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4434 my $stmts = $2;
4435 my $semis = $3;
4436
4437 $ctx =~ s/\n*$//;
4438 my $cnt = statement_rawlines($ctx);
4439 my $herectx = $here . "\n";
4440
4441 for (my $n = 0; $n < $cnt; $n++) {
4442 $herectx .= raw_line($linenr, $n) . "\n";
4443 }
4444
Joe Perchesac8e97f2012-08-21 16:15:53 -07004445 if (($stmts =~ tr/;/;/) == 1 &&
4446 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004447 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4448 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4449 }
4450 if (defined $semis && $semis ne "") {
4451 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4452 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4453 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004454 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4455 $ctx =~ s/\n*$//;
4456 my $cnt = statement_rawlines($ctx);
4457 my $herectx = $here . "\n";
4458
4459 for (my $n = 0; $n < $cnt; $n++) {
4460 $herectx .= raw_line($linenr, $n) . "\n";
4461 }
4462
4463 WARN("TRAILING_SEMICOLON",
4464 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004465 }
4466 }
4467
Mike Frysinger080ba922009-01-06 14:41:25 -08004468# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4469# all assignments may have only one of the following with an assignment:
4470# .
4471# ALIGN(...)
4472# VMLINUX_SYMBOL(...)
4473 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004474 WARN("MISSING_VMLINUX_SYMBOL",
4475 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004476 }
4477
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004478# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004479 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4480 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004481 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004482 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004483 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4484 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004485 my @allowed = ();
4486 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004487 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004488 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004489 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004490 for my $chunk (@chunks) {
4491 my ($cond, $block) = @{$chunk};
4492
Andy Whitcroft773647a2008-03-28 14:15:58 -07004493 # If the condition carries leading newlines, then count those as offsets.
4494 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4495 my $offset = statement_rawlines($whitespace) - 1;
4496
Joe Perchesaad4f612012-03-23 15:02:19 -07004497 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004498 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4499
4500 # We have looked at and allowed this specific line.
4501 $suppress_ifbraces{$ln + $offset} = 1;
4502
4503 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004504 $ln += statement_rawlines($block) - 1;
4505
Andy Whitcroft773647a2008-03-28 14:15:58 -07004506 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004507
4508 $seen++ if ($block =~ /^\s*{/);
4509
Joe Perchesaad4f612012-03-23 15:02:19 -07004510 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004511 if (statement_lines($cond) > 1) {
4512 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004513 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004514 }
4515 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004516 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004517 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004518 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004519 if (statement_block_size($block) > 1) {
4520 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004521 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004522 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004523 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004524 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004525 if ($seen) {
4526 my $sum_allowed = 0;
4527 foreach (@allowed) {
4528 $sum_allowed += $_;
4529 }
4530 if ($sum_allowed == 0) {
4531 WARN("BRACES",
4532 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4533 } elsif ($sum_allowed != $allow &&
4534 $seen != $allow) {
4535 CHK("BRACES",
4536 "braces {} should be used on all arms of this statement\n" . $herectx);
4537 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004538 }
4539 }
4540 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004541 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004542 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004543 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004544
Andy Whitcroftcf655042008-03-04 14:28:20 -08004545 # Check the pre-context.
4546 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4547 #print "APW: ALLOWED: pre<$1>\n";
4548 $allowed = 1;
4549 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004550
4551 my ($level, $endln, @chunks) =
4552 ctx_statement_full($linenr, $realcnt, $-[0]);
4553
Andy Whitcroftcf655042008-03-04 14:28:20 -08004554 # Check the condition.
4555 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004556 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004557 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004558 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004559 }
4560 if (statement_lines($cond) > 1) {
4561 #print "APW: ALLOWED: cond<$cond>\n";
4562 $allowed = 1;
4563 }
4564 if ($block =~/\b(?:if|for|while)\b/) {
4565 #print "APW: ALLOWED: block<$block>\n";
4566 $allowed = 1;
4567 }
4568 if (statement_block_size($block) > 1) {
4569 #print "APW: ALLOWED: lines block<$block>\n";
4570 $allowed = 1;
4571 }
4572 # Check the post-context.
4573 if (defined $chunks[1]) {
4574 my ($cond, $block) = @{$chunks[1]};
4575 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004576 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004577 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004578 if ($block =~ /^\s*\{/) {
4579 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4580 $allowed = 1;
4581 }
4582 }
4583 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004584 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004585 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004586
Andy Whitcroftf0556632008-10-15 22:02:23 -07004587 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004588 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004589 }
4590
Joe Perches000d1cc12011-07-25 17:13:25 -07004591 WARN("BRACES",
4592 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004593 }
4594 }
4595
Joe Perches0979ae62012-12-17 16:01:59 -08004596# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004597 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004598 if (CHK("BRACES",
4599 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4600 $fix && $prevrawline =~ /^\+/) {
4601 fix_delete_line($fixlinenr - 1, $prevrawline);
4602 }
Joe Perches0979ae62012-12-17 16:01:59 -08004603 }
Joe Perches77b9a532013-07-03 15:05:29 -07004604 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004605 if (CHK("BRACES",
4606 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4607 $fix) {
4608 fix_delete_line($fixlinenr, $rawline);
4609 }
Joe Perches0979ae62012-12-17 16:01:59 -08004610 }
4611
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004612# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004613 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4614 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004615 WARN("VOLATILE",
4616 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004617 }
4618
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004619# Check for user-visible strings broken across lines, which breaks the ability
4620# to grep for the string. Make exceptions when the previous string ends in a
4621# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4622# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4623 if ($line =~ /^\+\s*"[X\t]*"/ &&
4624 $prevline =~ /"\s*$/ &&
4625 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4626 if (WARN("SPLIT_STRING",
4627 "quoted string split across lines\n" . $hereprev) &&
4628 $fix &&
4629 $prevrawline =~ /^\+.*"\s*$/ &&
4630 $last_coalesced_string_linenr != $linenr - 1) {
4631 my $extracted_string = get_quoted_string($line, $rawline);
4632 my $comma_close = "";
4633 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4634 $comma_close = $1;
4635 }
4636
4637 fix_delete_line($fixlinenr - 1, $prevrawline);
4638 fix_delete_line($fixlinenr, $rawline);
4639 my $fixedline = $prevrawline;
4640 $fixedline =~ s/"\s*$//;
4641 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4642 fix_insert_line($fixlinenr - 1, $fixedline);
4643 $fixedline = $rawline;
4644 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4645 if ($fixedline !~ /\+\s*$/) {
4646 fix_insert_line($fixlinenr, $fixedline);
4647 }
4648 $last_coalesced_string_linenr = $linenr;
4649 }
4650 }
4651
4652# check for missing a space in a string concatenation
4653 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4654 WARN('MISSING_SPACE',
4655 "break quoted strings at a space character\n" . $hereprev);
4656 }
4657
4658# check for spaces before a quoted newline
4659 if ($rawline =~ /^.*\".*\s\\n/) {
4660 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4661 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4662 $fix) {
4663 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4664 }
4665
4666 }
4667
Joe Perchesf17dba42014-10-13 15:51:51 -07004668# concatenated string without spaces between elements
4669 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4670 CHK("CONCATENATED_STRING",
4671 "Concatenated strings should use spaces between elements\n" . $herecurr);
4672 }
4673
Joe Perches90ad30e2014-12-10 15:51:59 -08004674# uncoalesced string fragments
4675 if ($line =~ /"X*"\s*"/) {
4676 WARN("STRING_FRAGMENTS",
4677 "Consecutive strings are generally better as a single string\n" . $herecurr);
4678 }
4679
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004680# check for %L{u,d,i} in strings
4681 my $string;
4682 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4683 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4684 $string =~ s/%%/__/g;
4685 if ($string =~ /(?<!%)%L[udi]/) {
4686 WARN("PRINTF_L",
4687 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4688 last;
4689 }
4690 }
4691
4692# check for line continuations in quoted strings with odd counts of "
4693 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4694 WARN("LINE_CONTINUATIONS",
4695 "Avoid line continuations in quoted strings\n" . $herecurr);
4696 }
4697
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004698# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004699 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004700 CHK("REDUNDANT_CODE",
4701 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004702 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004703 }
4704
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004705# check for needless "if (<foo>) fn(<foo>)" uses
4706 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4707 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4708 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4709 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004710 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004711 }
4712 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004713
Joe Perchesebfdc402014-08-06 16:10:27 -07004714# check for unnecessary "Out of Memory" messages
4715 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4716 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4717 (defined $1 || defined $3) &&
4718 $linenr > 3) {
4719 my $testval = $2;
4720 my $testline = $lines[$linenr - 3];
4721
4722 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4723# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4724
4725 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4726 WARN("OOM_MESSAGE",
4727 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4728 }
4729 }
4730
Joe Perchesf78d98f2014-10-13 15:52:01 -07004731# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004732 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004733 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4734 my $level = $1;
4735 if (WARN("UNNECESSARY_KERN_LEVEL",
4736 "Possible unnecessary $level\n" . $herecurr) &&
4737 $fix) {
4738 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4739 }
4740 }
4741
Joe Perchesabb08a52014-12-10 15:51:46 -08004742# check for mask then right shift without a parentheses
4743 if ($^V && $^V ge 5.10.0 &&
4744 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4745 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4746 WARN("MASK_THEN_SHIFT",
4747 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4748 }
4749
Joe Perchesb75ac612014-12-10 15:52:02 -08004750# check for pointer comparisons to NULL
4751 if ($^V && $^V ge 5.10.0) {
4752 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4753 my $val = $1;
4754 my $equal = "!";
4755 $equal = "" if ($4 eq "!=");
4756 if (CHK("COMPARISON_TO_NULL",
4757 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4758 $fix) {
4759 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4760 }
4761 }
4762 }
4763
Joe Perches8716de32013-09-11 14:24:05 -07004764# check for bad placement of section $InitAttribute (e.g.: __initdata)
4765 if ($line =~ /(\b$InitAttribute\b)/) {
4766 my $attr = $1;
4767 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4768 my $ptr = $1;
4769 my $var = $2;
4770 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4771 ERROR("MISPLACED_INIT",
4772 "$attr should be placed after $var\n" . $herecurr)) ||
4773 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4774 WARN("MISPLACED_INIT",
4775 "$attr should be placed after $var\n" . $herecurr))) &&
4776 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004777 $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 -07004778 }
4779 }
4780 }
4781
Joe Perchese970b8842013-11-12 15:10:10 -08004782# check for $InitAttributeData (ie: __initdata) with const
4783 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4784 my $attr = $1;
4785 $attr =~ /($InitAttributePrefix)(.*)/;
4786 my $attr_prefix = $1;
4787 my $attr_type = $2;
4788 if (ERROR("INIT_ATTRIBUTE",
4789 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4790 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004791 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004792 s/$InitAttributeData/${attr_prefix}initconst/;
4793 }
4794 }
4795
4796# check for $InitAttributeConst (ie: __initconst) without const
4797 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4798 my $attr = $1;
4799 if (ERROR("INIT_ATTRIBUTE",
4800 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4801 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004802 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004803 /(^\+\s*(?:static\s+))/;
4804 $lead = rtrim($1);
4805 $lead = "$lead " if ($lead !~ /^\+$/);
4806 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004807 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004808 }
4809 }
4810
Joe Perchesc17893c2015-04-16 12:44:42 -07004811# check for __read_mostly with const non-pointer (should just be const)
4812 if ($line =~ /\b__read_mostly\b/ &&
4813 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
4814 if (ERROR("CONST_READ_MOSTLY",
4815 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
4816 $fix) {
4817 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
4818 }
4819 }
4820
Joe Perchesfbdb8132014-04-03 14:49:14 -07004821# don't use __constant_<foo> functions outside of include/uapi/
4822 if ($realfile !~ m@^include/uapi/@ &&
4823 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4824 my $constant_func = $1;
4825 my $func = $constant_func;
4826 $func =~ s/^__constant_//;
4827 if (WARN("CONSTANT_CONVERSION",
4828 "$constant_func should be $func\n" . $herecurr) &&
4829 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004830 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004831 }
4832 }
4833
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004834# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004835 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004836 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004837 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004838 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004839 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004840 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4841 }
4842 if ($delay > 2000) {
4843 WARN("LONG_UDELAY",
4844 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004845 }
4846 }
4847
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004848# warn about unexpectedly long msleep's
4849 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4850 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004851 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004852 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004853 }
4854 }
4855
Joe Perches36ec1932013-07-03 15:05:25 -07004856# check for comparisons of jiffies
4857 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4858 WARN("JIFFIES_COMPARISON",
4859 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4860 }
4861
Joe Perches9d7a34a2013-07-03 15:05:26 -07004862# check for comparisons of get_jiffies_64()
4863 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4864 WARN("JIFFIES_COMPARISON",
4865 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4866 }
4867
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004868# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004869# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004870# print "#ifdef in C files should be avoided\n";
4871# print "$herecurr";
4872# $clean = 0;
4873# }
4874
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004875# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004876 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004877 if (ERROR("SPACING",
4878 "exactly one space required after that #$1\n" . $herecurr) &&
4879 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004880 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004881 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4882 }
4883
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004884 }
4885
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004886# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004887 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4888 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004889 my $which = $1;
4890 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004891 CHK("UNCOMMENTED_DEFINITION",
4892 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004893 }
4894 }
4895# check for memory barriers without a comment.
4896 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4897 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004898 WARN("MEMORY_BARRIER",
4899 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004900 }
4901 }
Joe Perchescb426e92015-06-25 15:02:46 -07004902# check for waitqueue_active without a comment.
4903 if ($line =~ /\bwaitqueue_active\s*\(/) {
4904 if (!ctx_has_comment($first_line, $linenr)) {
4905 WARN("WAITQUEUE_ACTIVE",
4906 "waitqueue_active without comment\n" . $herecurr);
4907 }
4908 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004909# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004910 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004911 CHK("ARCH_DEFINES",
4912 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004913 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004914
Tobias Klauserd4977c72010-05-24 14:33:30 -07004915# Check that the storage class is at the beginning of a declaration
4916 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004917 WARN("STORAGE_CLASS",
4918 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004919 }
4920
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004921# check the location of the inline attribute, that it is between
4922# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004923 if ($line =~ /\b$Type\s+$Inline\b/ ||
4924 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004925 ERROR("INLINE_LOCATION",
4926 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004927 }
4928
Andy Whitcroft8905a672007-11-28 16:21:06 -08004929# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004930 if ($realfile !~ m@\binclude/uapi/@ &&
4931 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004932 if (WARN("INLINE",
4933 "plain inline is preferred over $1\n" . $herecurr) &&
4934 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004935 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004936
4937 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004938 }
4939
Joe Perches3d130fd2011-01-12 17:00:00 -08004940# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004941 if ($realfile !~ m@\binclude/uapi/@ &&
4942 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004943 WARN("PREFER_PACKED",
4944 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004945 }
4946
Joe Perches39b7e282011-07-25 17:13:24 -07004947# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004948 if ($realfile !~ m@\binclude/uapi/@ &&
4949 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004950 WARN("PREFER_ALIGNED",
4951 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004952 }
4953
Joe Perches5f14d3b2012-01-10 15:09:52 -08004954# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004955 if ($realfile !~ m@\binclude/uapi/@ &&
4956 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004957 if (WARN("PREFER_PRINTF",
4958 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4959 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004960 $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 -07004961
4962 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004963 }
4964
Joe Perches6061d942012-03-23 15:02:16 -07004965# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004966 if ($realfile !~ m@\binclude/uapi/@ &&
4967 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004968 if (WARN("PREFER_SCANF",
4969 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4970 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004971 $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 -07004972 }
Joe Perches6061d942012-03-23 15:02:16 -07004973 }
4974
Joe Perches619a9082014-12-10 15:51:35 -08004975# Check for __attribute__ weak, or __weak declarations (may have link issues)
4976 if ($^V && $^V ge 5.10.0 &&
4977 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4978 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4979 $line =~ /\b__weak\b/)) {
4980 ERROR("WEAK_DECLARATION",
4981 "Using weak declarations can have unintended link defects\n" . $herecurr);
4982 }
4983
Joe Perchese6176fa2015-06-25 15:02:49 -07004984# check for c99 types like uint8_t used outside of uapi/
4985 if ($realfile !~ m@\binclude/uapi/@ &&
4986 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
4987 my $type = $1;
4988 if ($type =~ /\b($typeC99Typedefs)\b/) {
4989 $type = $1;
4990 my $kernel_type = 'u';
4991 $kernel_type = 's' if ($type =~ /^_*[si]/);
4992 $type =~ /(\d+)/;
4993 $kernel_type .= $1;
4994 if (CHK("PREFER_KERNEL_TYPES",
4995 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
4996 $fix) {
4997 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
4998 }
4999 }
5000 }
5001
Joe Perches8f53a9b2010-03-05 13:43:48 -08005002# check for sizeof(&)
5003 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005004 WARN("SIZEOF_ADDRESS",
5005 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005006 }
5007
Joe Perches66c80b62012-07-30 14:41:22 -07005008# check for sizeof without parenthesis
5009 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005010 if (WARN("SIZEOF_PARENTHESIS",
5011 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5012 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005013 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005014 }
Joe Perches66c80b62012-07-30 14:41:22 -07005015 }
5016
Joe Perches88982fe2012-12-17 16:02:00 -08005017# check for struct spinlock declarations
5018 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5019 WARN("USE_SPINLOCK_T",
5020 "struct spinlock should be spinlock_t\n" . $herecurr);
5021 }
5022
Joe Perchesa6962d72013-04-29 16:18:13 -07005023# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005024 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005025 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005026 $fmt =~ s/%%//g;
5027 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005028 if (WARN("PREFER_SEQ_PUTS",
5029 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5030 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005031 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005032 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005033 }
5034 }
5035
Andy Whitcroft554e1652012-01-10 15:09:57 -08005036# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005037 if ($^V && $^V ge 5.10.0 &&
5038 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005039 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005040
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005041 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005042 my $ms_val = $7;
5043 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005044
Andy Whitcroft554e1652012-01-10 15:09:57 -08005045 if ($ms_size =~ /^(0x|)0$/i) {
5046 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005047 "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 -08005048 } elsif ($ms_size =~ /^(0x|)1$/i) {
5049 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005050 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5051 }
5052 }
5053
Joe Perches98a9bba2014-01-23 15:54:52 -08005054# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5055 if ($^V && $^V ge 5.10.0 &&
5056 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
5057 if (WARN("PREFER_ETHER_ADDR_COPY",
5058 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
5059 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005060 $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 -08005061 }
5062 }
5063
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005064# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005065 if ($^V && $^V ge 5.10.0 &&
5066 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005067 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005068 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005069 my $call = $1;
5070 my $cast1 = deparenthesize($2);
5071 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005072 my $cast2 = deparenthesize($7);
5073 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005074 my $cast;
5075
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005076 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005077 $cast = "$cast1 or $cast2";
5078 } elsif ($cast1 ne "") {
5079 $cast = $cast1;
5080 } else {
5081 $cast = $cast2;
5082 }
5083 WARN("MINMAX",
5084 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005085 }
5086 }
5087
Joe Perches4a273192012-07-30 14:41:20 -07005088# check usleep_range arguments
5089 if ($^V && $^V ge 5.10.0 &&
5090 defined $stat &&
5091 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5092 my $min = $1;
5093 my $max = $7;
5094 if ($min eq $max) {
5095 WARN("USLEEP_RANGE",
5096 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5097 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5098 $min > $max) {
5099 WARN("USLEEP_RANGE",
5100 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5101 }
5102 }
5103
Joe Perches823b7942013-11-12 15:10:15 -08005104# check for naked sscanf
5105 if ($^V && $^V ge 5.10.0 &&
5106 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005107 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005108 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5109 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5110 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5111 my $lc = $stat =~ tr@\n@@;
5112 $lc = $lc + $linenr;
5113 my $stat_real = raw_line($linenr, 0);
5114 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5115 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5116 }
5117 WARN("NAKED_SSCANF",
5118 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5119 }
5120
Joe Perchesafc819a2014-06-04 16:12:08 -07005121# check for simple sscanf that should be kstrto<foo>
5122 if ($^V && $^V ge 5.10.0 &&
5123 defined $stat &&
5124 $line =~ /\bsscanf\b/) {
5125 my $lc = $stat =~ tr@\n@@;
5126 $lc = $lc + $linenr;
5127 my $stat_real = raw_line($linenr, 0);
5128 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5129 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5130 }
5131 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5132 my $format = $6;
5133 my $count = $format =~ tr@%@%@;
5134 if ($count == 1 &&
5135 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5136 WARN("SSCANF_TO_KSTRTO",
5137 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5138 }
5139 }
5140 }
5141
Joe Perches70dc8a42013-09-11 14:23:58 -07005142# check for new externs in .h files.
5143 if ($realfile =~ /\.h$/ &&
5144 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005145 if (CHK("AVOID_EXTERNS",
5146 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005147 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005148 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005149 }
5150 }
5151
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005152# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005153 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005154 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005155 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005156 my $function_name = $1;
5157 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005158
5159 my $s = $stat;
5160 if (defined $cond) {
5161 substr($s, 0, length($cond), '');
5162 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005163 if ($s =~ /^\s*;/ &&
5164 $function_name ne 'uninitialized_var')
5165 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005166 WARN("AVOID_EXTERNS",
5167 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005168 }
5169
5170 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005171 WARN("FUNCTION_ARGUMENTS",
5172 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005173 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005174
5175 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5176 $stat =~ /^.\s*extern\s+/)
5177 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005178 WARN("AVOID_EXTERNS",
5179 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005180 }
5181
5182# checks for new __setup's
5183 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5184 my $name = $1;
5185
5186 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005187 CHK("UNDOCUMENTED_SETUP",
5188 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005189 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005190 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005191
5192# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005193 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005194 WARN("UNNECESSARY_CASTS",
5195 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005196 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005197
Joe Perchesa640d252013-07-03 15:05:21 -07005198# alloc style
5199# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5200 if ($^V && $^V ge 5.10.0 &&
5201 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5202 CHK("ALLOC_SIZEOF_STRUCT",
5203 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5204 }
5205
Joe Perches60a55362014-06-04 16:12:07 -07005206# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5207 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005208 $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 -07005209 my $oldfunc = $3;
5210 my $a1 = $4;
5211 my $a2 = $10;
5212 my $newfunc = "kmalloc_array";
5213 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005214 my $r1 = $a1;
5215 my $r2 = $a2;
5216 if ($a1 =~ /^sizeof\s*\S/) {
5217 $r1 = $a2;
5218 $r2 = $a1;
5219 }
5220 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5221 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005222 if (WARN("ALLOC_WITH_MULTIPLY",
5223 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5224 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005225 $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 -07005226
5227 }
5228 }
5229 }
5230
Joe Perches972fdea2013-04-29 16:18:12 -07005231# check for krealloc arg reuse
5232 if ($^V && $^V ge 5.10.0 &&
5233 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5234 WARN("KREALLOC_ARG_REUSE",
5235 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5236 }
5237
Joe Perches5ce59ae2013-02-21 16:44:18 -08005238# check for alloc argument mismatch
5239 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5240 WARN("ALLOC_ARRAY_ARGS",
5241 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5242 }
5243
Joe Perchescaf2a542011-01-12 16:59:56 -08005244# check for multiple semicolons
5245 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005246 if (WARN("ONE_SEMICOLON",
5247 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5248 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005249 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005250 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005251 }
5252
Joe Perches0ab90192014-12-10 15:51:57 -08005253# check for #defines like: 1 << <digit> that could be BIT(digit)
5254 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5255 my $ull = "";
5256 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5257 if (CHK("BIT_MACRO",
5258 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5259 $fix) {
5260 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5261 }
5262 }
5263
Joe Perchese81f2392014-08-06 16:11:25 -07005264# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005265 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5266 my $has_break = 0;
5267 my $has_statement = 0;
5268 my $count = 0;
5269 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005270 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005271 $prevline--;
5272 my $rline = $rawlines[$prevline - 1];
5273 my $fline = $lines[$prevline - 1];
5274 last if ($fline =~ /^\@\@/);
5275 next if ($fline =~ /^\-/);
5276 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5277 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5278 next if ($fline =~ /^.[\s$;]*$/);
5279 $has_statement = 1;
5280 $count++;
5281 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5282 }
5283 if (!$has_break && $has_statement) {
5284 WARN("MISSING_BREAK",
5285 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5286 }
5287 }
5288
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005289# check for switch/default statements without a break;
5290 if ($^V && $^V ge 5.10.0 &&
5291 defined $stat &&
5292 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5293 my $ctx = '';
5294 my $herectx = $here . "\n";
5295 my $cnt = statement_rawlines($stat);
5296 for (my $n = 0; $n < $cnt; $n++) {
5297 $herectx .= raw_line($linenr, $n) . "\n";
5298 }
5299 WARN("DEFAULT_NO_BREAK",
5300 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005301 }
5302
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005303# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005304 if ($line =~ /\b__FUNCTION__\b/) {
5305 if (WARN("USE_FUNC",
5306 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5307 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005308 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005309 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005310 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005311
Joe Perches62ec8182015-02-13 14:38:18 -08005312# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5313 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5314 ERROR("DATE_TIME",
5315 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5316 }
5317
Joe Perches2c924882012-03-23 15:02:20 -07005318# check for use of yield()
5319 if ($line =~ /\byield\s*\(\s*\)/) {
5320 WARN("YIELD",
5321 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5322 }
5323
Joe Perches179f8f42013-07-03 15:05:30 -07005324# check for comparisons against true and false
5325 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5326 my $lead = $1;
5327 my $arg = $2;
5328 my $test = $3;
5329 my $otype = $4;
5330 my $trail = $5;
5331 my $op = "!";
5332
5333 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5334
5335 my $type = lc($otype);
5336 if ($type =~ /^(?:true|false)$/) {
5337 if (("$test" eq "==" && "$type" eq "true") ||
5338 ("$test" eq "!=" && "$type" eq "false")) {
5339 $op = "";
5340 }
5341
5342 CHK("BOOL_COMPARISON",
5343 "Using comparison to $otype is error prone\n" . $herecurr);
5344
5345## maybe suggesting a correct construct would better
5346## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5347
5348 }
5349 }
5350
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005351# check for semaphores initialized locked
5352 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005353 WARN("CONSIDER_COMPLETION",
5354 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005355 }
Joe Perches6712d852012-03-23 15:02:20 -07005356
Joe Perches67d0a072011-10-31 17:13:10 -07005357# recommend kstrto* over simple_strto* and strict_strto*
5358 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005359 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005360 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005361 }
Joe Perches6712d852012-03-23 15:02:20 -07005362
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005363# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005364 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005365 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005366 "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 -07005367 }
Joe Perches6712d852012-03-23 15:02:20 -07005368
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005369# check for various structs that are normally const (ops, kgdb, device_tree)
5370 my $const_structs = qr{
5371 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005372 address_space_operations|
5373 backlight_ops|
5374 block_device_operations|
5375 dentry_operations|
5376 dev_pm_ops|
5377 dma_map_ops|
5378 extent_io_ops|
5379 file_lock_operations|
5380 file_operations|
5381 hv_ops|
5382 ide_dma_ops|
5383 intel_dvo_dev_ops|
5384 item_operations|
5385 iwl_ops|
5386 kgdb_arch|
5387 kgdb_io|
5388 kset_uevent_ops|
5389 lock_manager_operations|
5390 microcode_ops|
5391 mtrr_ops|
5392 neigh_ops|
5393 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005394 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08005395 pci_raw_ops|
5396 pipe_buf_operations|
5397 platform_hibernation_ops|
5398 platform_suspend_ops|
5399 proto_ops|
5400 rpc_pipe_ops|
5401 seq_operations|
5402 snd_ac97_build_ops|
5403 soc_pcmcia_socket_ops|
5404 stacktrace_ops|
5405 sysfs_ops|
5406 tty_operations|
Joe Perches6d07d01b2015-04-16 12:44:33 -07005407 uart_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005408 usb_mon_operations|
5409 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005410 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005411 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005412 WARN("CONST_STRUCT",
5413 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005414 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005415 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005416
5417# use of NR_CPUS is usually wrong
5418# ignore definitions of NR_CPUS and usage to define arrays as likely right
5419 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005420 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5421 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005422 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5423 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5424 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005425 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005426 WARN("NR_CPUS",
5427 "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 -07005428 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005429
Joe Perches52ea8502013-11-12 15:10:09 -08005430# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5431 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5432 ERROR("DEFINE_ARCH_HAS",
5433 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5434 }
5435
Joe Perchesacd93622015-02-13 14:38:38 -08005436# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5437 if ($^V && $^V ge 5.10.0 &&
5438 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5439 WARN("LIKELY_MISUSE",
5440 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5441 }
5442
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005443# whine mightly about in_atomic
5444 if ($line =~ /\bin_atomic\s*\(/) {
5445 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005446 ERROR("IN_ATOMIC",
5447 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005448 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005449 WARN("IN_ATOMIC",
5450 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005451 }
5452 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005453
5454# check for lockdep_set_novalidate_class
5455 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5456 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5457 if ($realfile !~ m@^kernel/lockdep@ &&
5458 $realfile !~ m@^include/linux/lockdep@ &&
5459 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005460 ERROR("LOCKDEP",
5461 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005462 }
5463 }
Dave Jones88f88312011-01-12 16:59:59 -08005464
Joe Perchesb392c642015-04-16 12:44:16 -07005465 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5466 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005467 WARN("EXPORTED_WORLD_WRITABLE",
5468 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005469 }
Joe Perches24358802014-04-03 14:49:13 -07005470
Joe Perches515a2352014-04-03 14:49:24 -07005471# Mode permission misuses where it seems decimal should be octal
5472# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5473 if ($^V && $^V ge 5.10.0 &&
5474 $line =~ /$mode_perms_search/) {
5475 foreach my $entry (@mode_permission_funcs) {
5476 my $func = $entry->[0];
5477 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005478
Joe Perches515a2352014-04-03 14:49:24 -07005479 my $skip_args = "";
5480 if ($arg_pos > 1) {
5481 $arg_pos--;
5482 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5483 }
5484 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5485 if ($line =~ /$test/) {
5486 my $val = $1;
5487 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005488
Joe Perches515a2352014-04-03 14:49:24 -07005489 if ($val !~ /^0$/ &&
5490 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5491 length($val) ne 4)) {
5492 ERROR("NON_OCTAL_PERMISSIONS",
5493 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005494 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5495 ERROR("EXPORTED_WORLD_WRITABLE",
5496 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005497 }
Joe Perches24358802014-04-03 14:49:13 -07005498 }
5499 }
5500 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005501 }
5502
5503 # If we have no input at all, then there is nothing to report on
5504 # so just keep quiet.
5505 if ($#rawlines == -1) {
5506 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005507 }
5508
Andy Whitcroft8905a672007-11-28 16:21:06 -08005509 # In mailback mode only produce a report in the negative, for
5510 # things that appear to be patches.
5511 if ($mailback && ($clean == 1 || !$is_patch)) {
5512 exit(0);
5513 }
5514
5515 # This is not a patch, and we are are in 'no-patch' mode so
5516 # just keep quiet.
5517 if (!$chk_patch && !$is_patch) {
5518 exit(0);
5519 }
5520
5521 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005522 ERROR("NOT_UNIFIED_DIFF",
5523 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005524 }
5525 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005526 ERROR("MISSING_SIGN_OFF",
5527 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005528 }
5529
Andy Whitcroft8905a672007-11-28 16:21:06 -08005530 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005531 if ($summary && !($clean == 1 && $quiet == 1)) {
5532 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005533 print "total: $cnt_error errors, $cnt_warn warnings, " .
5534 (($check)? "$cnt_chk checks, " : "") .
5535 "$cnt_lines lines checked\n";
5536 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005537 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005538
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005539 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005540
5541 if ($^V lt 5.10.0) {
5542 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5543 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5544 }
5545
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005546 # If there were whitespace errors which cleanpatch can fix
5547 # then suggest that.
5548 if ($rpt_cleaners) {
5549 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5550 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005551 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005552 }
5553 }
5554
Joe Perches91bfe482013-09-11 14:23:59 -07005555 hash_show_words(\%use_type, "Used");
5556 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005557
Joe Perchesd752fcc2014-08-06 16:11:05 -07005558 if ($clean == 0 && $fix &&
5559 ("@rawlines" ne "@fixed" ||
5560 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005561 my $newfile = $filename;
5562 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005563 my $linecount = 0;
5564 my $f;
5565
Joe Perchesd752fcc2014-08-06 16:11:05 -07005566 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5567
Joe Perches3705ce52013-07-03 15:05:31 -07005568 open($f, '>', $newfile)
5569 or die "$P: Can't open $newfile for write\n";
5570 foreach my $fixed_line (@fixed) {
5571 $linecount++;
5572 if ($file) {
5573 if ($linecount > 3) {
5574 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005575 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005576 }
5577 } else {
5578 print $f $fixed_line . "\n";
5579 }
5580 }
5581 close($f);
5582
5583 if (!$quiet) {
5584 print << "EOM";
5585Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5586
5587Do _NOT_ trust the results written to this file.
5588Do _NOT_ submit these changes without inspecting them for correctness.
5589
5590This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5591No warranties, expressed or implied...
5592
5593EOM
5594 }
5595 }
5596
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005597 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005598 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005599 }
5600 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005601 print << "EOM";
5602$vname has style problems, please review.
5603
5604If any of these errors are false positives, please report
5605them to the maintainer, see CHECKPATCH in MAINTAINERS.
5606EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005607 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005608
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005609 return $clean;
5610}