blob: 1b999cc035ff8ad95b2bef5bd08f2af674ef619a [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070012
13my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080014my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070015
Joe Perches000d1cc12011-07-25 17:13:25 -070016my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $quiet = 0;
21my $tree = 1;
22my $chk_signoff = 1;
23my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070024my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070027my $file = 0;
28my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070029my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080030my $summary = 1;
31my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080032my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070033my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070034my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080035my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080037my %debug;
Joe Perches34456862013-07-03 15:05:34 -070038my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070039my %use_type = ();
40my @use = ();
41my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070042my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070043my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070044my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080045my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070046my $ignore_perl_version = 0;
47my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070048my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070049my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070050my $codespell = 0;
51my $codespellfile = "/usr/local/share/codespell/dictionary.txt";
Hannes Eder77f5b102009-09-21 17:04:37 -070052
53sub help {
54 my ($exitcode) = @_;
55
56 print << "EOM";
57Usage: $P [OPTION]... [FILE]...
58Version: $V
59
60Options:
61 -q, --quiet quiet
62 --no-tree run without a kernel tree
63 --no-signoff do not check for 'Signed-off-by' line
64 --patch treat FILE as patchfile (default)
65 --emacs emacs compile window format
66 --terse one line per report
67 -f, --file treat FILE as regular source file
68 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070069 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070070 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080071 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070072 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070073 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070074 --root=PATH PATH to the kernel tree root
75 --no-summary suppress the per-file summary
76 --mailback only produce a report in case of warnings/errors
77 --summary-file include the filename in summary
78 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
79 'values', 'possible', 'type', and 'attr' (default
80 is all off)
81 --test-only=WORD report only warnings/errors containing WORD
82 literally
Joe Perches3705ce52013-07-03 15:05:31 -070083 --fix EXPERIMENTAL - may create horrible results
84 If correctable single-line errors exist, create
85 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
86 with potential errors corrected to the preferred
87 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080088 --fix-inplace EXPERIMENTAL - may create horrible results
89 Is the same as --fix, but overwrites the input
90 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070091 --ignore-perl-version override checking of perl version. expect
92 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -070093 --codespell Use the codespell dictionary for spelling/typos
94 (default:/usr/local/share/codespell/dictionary.txt)
95 --codespellfile Use this codespell dictionary
Hannes Eder77f5b102009-09-21 17:04:37 -070096 -h, --help, --version display this help and exit
97
98When FILE is - read standard input.
99EOM
100
101 exit($exitcode);
102}
103
Joe Perches000d1cc12011-07-25 17:13:25 -0700104my $conf = which_conf($configuration_file);
105if (-f $conf) {
106 my @conf_args;
107 open(my $conffile, '<', "$conf")
108 or warn "$P: Can't find a readable $configuration_file file $!\n";
109
110 while (<$conffile>) {
111 my $line = $_;
112
113 $line =~ s/\s*\n?$//g;
114 $line =~ s/^\s*//g;
115 $line =~ s/\s+/ /g;
116
117 next if ($line =~ m/^\s*#/);
118 next if ($line =~ m/^\s*$/);
119
120 my @words = split(" ", $line);
121 foreach my $word (@words) {
122 last if ($word =~ m/^#/);
123 push (@conf_args, $word);
124 }
125 }
126 close($conffile);
127 unshift(@ARGV, @conf_args) if @conf_args;
128}
129
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700130GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700131 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700132 'tree!' => \$tree,
133 'signoff!' => \$chk_signoff,
134 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700135 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800136 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700137 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700138 'subjective!' => \$check,
139 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700140 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700141 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700142 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800143 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700144 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700145 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800146 'summary!' => \$summary,
147 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800148 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700149 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800150 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700151 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800152 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700153 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700154 'codespell!' => \$codespell,
155 'codespellfile=s' => \$codespellfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700156 'h|help' => \$help,
157 'version' => \$help
158) or help(1);
159
160help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700161
Joe Perches9624b8d2014-01-23 15:54:44 -0800162$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700163$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800164
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700165my $exit = 0;
166
Dave Hansend62a2012013-09-11 14:23:56 -0700167if ($^V && $^V lt $minimum_perl_version) {
168 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
169 if (!$ignore_perl_version) {
170 exit(1);
171 }
172}
173
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700174if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700175 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700176 exit(1);
177}
178
Joe Perches91bfe482013-09-11 14:23:59 -0700179sub hash_save_array_words {
180 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700181
Joe Perches91bfe482013-09-11 14:23:59 -0700182 my @array = split(/,/, join(',', @$arrayRef));
183 foreach my $word (@array) {
184 $word =~ s/\s*\n?$//g;
185 $word =~ s/^\s*//g;
186 $word =~ s/\s+/ /g;
187 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700188
Joe Perches91bfe482013-09-11 14:23:59 -0700189 next if ($word =~ m/^\s*#/);
190 next if ($word =~ m/^\s*$/);
191
192 $hashRef->{$word}++;
193 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700194}
195
Joe Perches91bfe482013-09-11 14:23:59 -0700196sub hash_show_words {
197 my ($hashRef, $prefix) = @_;
198
Joe Perches58cb3cf2013-09-11 14:24:04 -0700199 if ($quiet == 0 && keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700200 print "\nNOTE: $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 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700204 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700205 }
206}
207
208hash_save_array_words(\%ignore_type, \@ignore);
209hash_save_array_words(\%use_type, \@use);
210
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800211my $dbg_values = 0;
212my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700213my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700214my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800215for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800216 ## no critic
217 eval "\${dbg_$key} = '$debug{$key}';";
218 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800219}
220
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700221my $rpt_cleaners = 0;
222
Andy Whitcroft8905a672007-11-28 16:21:06 -0800223if ($terse) {
224 $emacs = 1;
225 $quiet++;
226}
227
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700228if ($tree) {
229 if (defined $root) {
230 if (!top_of_kernel_tree($root)) {
231 die "$P: $root: --root does not point at a valid tree\n";
232 }
233 } else {
234 if (top_of_kernel_tree('.')) {
235 $root = '.';
236 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
237 top_of_kernel_tree($1)) {
238 $root = $1;
239 }
240 }
241
242 if (!defined $root) {
243 print "Must be run from the top-level dir. of a kernel tree\n";
244 exit(2);
245 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700246}
247
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700248my $emitted_corrupt = 0;
249
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700250our $Ident = qr{
251 [A-Za-z_][A-Za-z\d_]*
252 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
253 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700254our $Storage = qr{extern|static|asmlinkage};
255our $Sparse = qr{
256 __user|
257 __kernel|
258 __force|
259 __iomem|
260 __must_check|
261 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800262 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700263 __ref|
264 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700265 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800266our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
267our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
268our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
269our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
270our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700271
Wolfram Sang52131292010-03-05 13:43:51 -0800272# Notes to $Attribute:
273# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700274our $Attribute = qr{
275 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700276 __percpu|
277 __nocast|
278 __safe|
279 __bitwise__|
280 __packed__|
281 __packed2__|
282 __naked|
283 __maybe_unused|
284 __always_unused|
285 __noreturn|
286 __used|
287 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800288 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700289 __noclone|
290 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700291 __read_mostly|
292 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700293 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700294 ____cacheline_aligned|
295 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800296 ____cacheline_internodealigned_in_smp|
297 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700298 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700299our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700300our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700301our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
302our $Lval = qr{$Ident(?:$Member)*};
303
Joe Perches95e2c602013-07-03 15:05:20 -0700304our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
305our $Binary = qr{(?i)0b[01]+$Int_type?};
306our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
307our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700308our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800309our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800310our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
311our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
312our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800313our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700314our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800315our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700316our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700317our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700318our $Operators = qr{
319 <=|>=|==|!=|
320 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700321 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700322 }x;
323
Joe Perches91cb5192014-04-03 14:49:32 -0700324our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
325
Joe Perchesab7e23f2015-04-16 12:44:22 -0700326our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800327our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700328our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700329our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800330our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700331our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800332our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700333our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800334
Joe Perches15662b32011-10-31 17:13:12 -0700335our $NON_ASCII_UTF8 = qr{
336 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700337 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
338 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
339 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
340 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
341 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
342 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
343}x;
344
Joe Perches15662b32011-10-31 17:13:12 -0700345our $UTF8 = qr{
346 [\x09\x0A\x0D\x20-\x7E] # ASCII
347 | $NON_ASCII_UTF8
348}x;
349
Joe Perchese6176fa2015-06-25 15:02:49 -0700350our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800351our $typeOtherOSTypedefs = qr{(?x:
352 u_(?:char|short|int|long) | # bsd
353 u(?:nchar|short|int|long) # sysv
354)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700355our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700356 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700357 atomic_t
358)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700359our $typeTypedefs = qr{(?x:
360 $typeC99Typedefs\b|
361 $typeOtherOSTypedefs\b|
362 $typeKernelTypedefs\b
363)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700364
Joe Perches691e6692010-03-05 13:43:51 -0800365our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700366 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700367 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches6e60c022011-07-25 17:13:27 -0700368 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700369 panic|
Joe Perches06668722013-11-12 15:10:07 -0800370 MODULE_[A-Z_]+|
371 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800372)};
373
Joe Perches20112472011-07-25 17:13:23 -0700374our $signature_tags = qr{(?xi:
375 Signed-off-by:|
376 Acked-by:|
377 Tested-by:|
378 Reviewed-by:|
379 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700380 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700381 To:|
382 Cc:
383)};
384
Joe Perches18130872014-08-06 16:11:22 -0700385our @typeListMisordered = (
386 qr{char\s+(?:un)?signed},
387 qr{int\s+(?:(?:un)?signed\s+)?short\s},
388 qr{int\s+short(?:\s+(?:un)?signed)},
389 qr{short\s+int(?:\s+(?:un)?signed)},
390 qr{(?:un)?signed\s+int\s+short},
391 qr{short\s+(?:un)?signed},
392 qr{long\s+int\s+(?:un)?signed},
393 qr{int\s+long\s+(?:un)?signed},
394 qr{long\s+(?:un)?signed\s+int},
395 qr{int\s+(?:un)?signed\s+long},
396 qr{int\s+(?:un)?signed},
397 qr{int\s+long\s+long\s+(?:un)?signed},
398 qr{long\s+long\s+int\s+(?:un)?signed},
399 qr{long\s+long\s+(?:un)?signed\s+int},
400 qr{long\s+long\s+(?:un)?signed},
401 qr{long\s+(?:un)?signed},
402);
403
Andy Whitcroft8905a672007-11-28 16:21:06 -0800404our @typeList = (
405 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700406 qr{(?:(?:un)?signed\s+)?char},
407 qr{(?:(?:un)?signed\s+)?short\s+int},
408 qr{(?:(?:un)?signed\s+)?short},
409 qr{(?:(?:un)?signed\s+)?int},
410 qr{(?:(?:un)?signed\s+)?long\s+int},
411 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
412 qr{(?:(?:un)?signed\s+)?long\s+long},
413 qr{(?:(?:un)?signed\s+)?long},
414 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800415 qr{float},
416 qr{double},
417 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800418 qr{struct\s+$Ident},
419 qr{union\s+$Ident},
420 qr{enum\s+$Ident},
421 qr{${Ident}_t},
422 qr{${Ident}_handler},
423 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700424 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800425);
Alex Dowad485ff232015-06-25 15:02:52 -0700426our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700427our @typeListWithAttr = (
428 @typeList,
429 qr{struct\s+$InitAttribute\s+$Ident},
430 qr{union\s+$InitAttribute\s+$Ident},
431);
432
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700433our @modifierList = (
434 qr{fastcall},
435);
Alex Dowad485ff232015-06-25 15:02:52 -0700436our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800437
Joe Perches24358802014-04-03 14:49:13 -0700438our @mode_permission_funcs = (
439 ["module_param", 3],
440 ["module_param_(?:array|named|string)", 4],
441 ["module_param_array_named", 5],
442 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
443 ["proc_create(?:_data|)", 2],
444 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
445);
446
Joe Perches515a2352014-04-03 14:49:24 -0700447#Create a search pattern for all these functions to speed up a loop below
448our $mode_perms_search = "";
449foreach my $entry (@mode_permission_funcs) {
450 $mode_perms_search .= '|' if ($mode_perms_search ne "");
451 $mode_perms_search .= $entry->[0];
452}
453
Joe Perchesb392c642015-04-16 12:44:16 -0700454our $mode_perms_world_writable = qr{
455 S_IWUGO |
456 S_IWOTH |
457 S_IRWXUGO |
458 S_IALLUGO |
459 0[0-7][0-7][2367]
460}x;
461
Wolfram Sang7840a942010-08-09 17:20:57 -0700462our $allowed_asm_includes = qr{(?x:
463 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700464 memory|
465 time|
466 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700467)};
468# memory.h: ARM has a custom one
469
Kees Cook66b47b42014-10-13 15:51:57 -0700470# Load common spelling mistakes and build regular expression list.
471my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700472my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700473
Joe Perches36061e32014-12-10 15:51:43 -0800474if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800475 while (<$spelling>) {
476 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700477
Joe Perches36061e32014-12-10 15:51:43 -0800478 $line =~ s/\s*\n?$//g;
479 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700480
Joe Perches36061e32014-12-10 15:51:43 -0800481 next if ($line =~ m/^\s*#/);
482 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700483
Joe Perches36061e32014-12-10 15:51:43 -0800484 my ($suspect, $fix) = split(/\|\|/, $line);
485
Joe Perches36061e32014-12-10 15:51:43 -0800486 $spelling_fix{$suspect} = $fix;
487 }
488 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800489} else {
490 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700491}
Kees Cook66b47b42014-10-13 15:51:57 -0700492
Joe Perchesebfd7d62015-04-16 12:44:14 -0700493if ($codespell) {
494 if (open(my $spelling, '<', $codespellfile)) {
495 while (<$spelling>) {
496 my $line = $_;
497
498 $line =~ s/\s*\n?$//g;
499 $line =~ s/^\s*//g;
500
501 next if ($line =~ m/^\s*#/);
502 next if ($line =~ m/^\s*$/);
503 next if ($line =~ m/, disabled/i);
504
505 $line =~ s/,.*$//;
506
507 my ($suspect, $fix) = split(/->/, $line);
508
509 $spelling_fix{$suspect} = $fix;
510 }
511 close($spelling);
512 } else {
513 warn "No codespell typos will be found - file '$codespellfile': $!\n";
514 }
515}
516
517$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
518
Andy Whitcroft8905a672007-11-28 16:21:06 -0800519sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700520 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
521 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700522 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700523 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700524 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700525 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700526 (?:$typeTypedefs\b)|
527 (?:${all}\b)
528 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800529 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700530 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800531 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800532 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700533 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700534 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800535 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700536 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800537 }x;
Joe Perches18130872014-08-06 16:11:22 -0700538 $NonptrTypeMisordered = qr{
539 (?:$Modifier\s+|const\s+)*
540 (?:
541 (?:${Misordered}\b)
542 )
543 (?:\s+$Modifier|\s+const)*
544 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700545 $NonptrTypeWithAttr = qr{
546 (?:$Modifier\s+|const\s+)*
547 (?:
548 (?:typeof|__typeof__)\s*\([^\)]*\)|
549 (?:$typeTypedefs\b)|
550 (?:${allWithAttr}\b)
551 )
552 (?:\s+$Modifier|\s+const)*
553 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800554 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700555 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700556 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700557 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800558 }x;
Joe Perches18130872014-08-06 16:11:22 -0700559 $TypeMisordered = qr{
560 $NonptrTypeMisordered
561 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
562 (?:\s+$Inline|\s+$Modifier)*
563 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700564 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700565 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800566}
567build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700568
Joe Perches7d2367a2011-07-25 17:13:22 -0700569our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700570
571# Using $balanced_parens, $LvalOrFunc, or $FuncArg
572# requires at least perl version v5.10.0
573# Any use must be runtime checked with $^V
574
575our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700576our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800577our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700578
Joe Perchesf8422302014-08-06 16:11:31 -0700579our $declaration_macros = qr{(?x:
580 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
581 (?:$Storage\s+)?LIST_HEAD\s*\(|
582 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
583)};
584
Joe Perches7d2367a2011-07-25 17:13:22 -0700585sub deparenthesize {
586 my ($string) = @_;
587 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700588
589 while ($string =~ /^\s*\(.*\)\s*$/) {
590 $string =~ s@^\s*\(\s*@@;
591 $string =~ s@\s*\)\s*$@@;
592 }
593
Joe Perches7d2367a2011-07-25 17:13:22 -0700594 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700595
Joe Perches7d2367a2011-07-25 17:13:22 -0700596 return $string;
597}
598
Joe Perches34456862013-07-03 15:05:34 -0700599sub seed_camelcase_file {
600 my ($file) = @_;
601
602 return if (!(-f $file));
603
604 local $/;
605
606 open(my $include_file, '<', "$file")
607 or warn "$P: Can't read '$file' $!\n";
608 my $text = <$include_file>;
609 close($include_file);
610
611 my @lines = split('\n', $text);
612
613 foreach my $line (@lines) {
614 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
615 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
616 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800617 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
618 $camelcase{$1} = 1;
619 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700620 $camelcase{$1} = 1;
621 }
622 }
623}
624
625my $camelcase_seeded = 0;
626sub seed_camelcase_includes {
627 return if ($camelcase_seeded);
628
629 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700630 my $camelcase_cache = "";
631 my @include_files = ();
632
633 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700634
Richard Genoud3645e322014-02-10 14:25:32 -0800635 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700636 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
637 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700638 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700639 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700640 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700641 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700642 @include_files = split('\n', $files);
643 foreach my $file (@include_files) {
644 my $date = POSIX::strftime("%Y%m%d%H%M",
645 localtime((stat $file)[9]));
646 $last_mod_date = $date if ($last_mod_date < $date);
647 }
648 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700649 }
Joe Perchesc707a812013-07-08 16:00:43 -0700650
651 if ($camelcase_cache ne "" && -f $camelcase_cache) {
652 open(my $camelcase_file, '<', "$camelcase_cache")
653 or warn "$P: Can't read '$camelcase_cache' $!\n";
654 while (<$camelcase_file>) {
655 chomp;
656 $camelcase{$_} = 1;
657 }
658 close($camelcase_file);
659
660 return;
661 }
662
Richard Genoud3645e322014-02-10 14:25:32 -0800663 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700664 $files = `git ls-files "include/*.h"`;
665 @include_files = split('\n', $files);
666 }
667
Joe Perches34456862013-07-03 15:05:34 -0700668 foreach my $file (@include_files) {
669 seed_camelcase_file($file);
670 }
Joe Perches351b2a12013-07-03 15:05:36 -0700671
Joe Perchesc707a812013-07-08 16:00:43 -0700672 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700673 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700674 open(my $camelcase_file, '>', "$camelcase_cache")
675 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700676 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
677 print $camelcase_file ("$_\n");
678 }
679 close($camelcase_file);
680 }
Joe Perches34456862013-07-03 15:05:34 -0700681}
682
Joe Perchesd311cd42014-08-06 16:10:57 -0700683sub git_commit_info {
684 my ($commit, $id, $desc) = @_;
685
686 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
687
688 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
689 $output =~ s/^\s*//gm;
690 my @lines = split("\n", $output);
691
Joe Perches0d7835f2015-02-13 14:38:35 -0800692 return ($id, $desc) if ($#lines < 0);
693
Joe Perchesd311cd42014-08-06 16:10:57 -0700694 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
695# Maybe one day convert this block of bash into something that returns
696# all matching commit ids, but it's very slow...
697#
698# echo "checking commits $1..."
699# git rev-list --remotes | grep -i "^$1" |
700# while read line ; do
701# git log --format='%H %s' -1 $line |
702# echo "commit $(cut -c 1-12,41-)"
703# done
704 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
705 } else {
706 $id = substr($lines[0], 0, 12);
707 $desc = substr($lines[0], 41);
708 }
709
710 return ($id, $desc);
711}
712
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700713$chk_signoff = 0 if ($file);
714
Andy Whitcroft00df3442007-06-08 13:47:06 -0700715my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800716my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700717my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700718my @fixed_inserted = ();
719my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700720my $fixlinenr = -1;
721
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800722my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700723for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800724 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700725 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800726 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700727 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800728 } elsif ($filename eq '-') {
729 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700730 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800731 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700732 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700733 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800734 if ($filename eq '-') {
735 $vname = 'Your patch';
736 } else {
737 $vname = $filename;
738 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800739 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700740 chomp;
741 push(@rawlines, $_);
742 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800743 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700744
745 if ($#ARGV > 0 && $quiet == 0) {
746 print '-' x length($vname) . "\n";
747 print "$vname\n";
748 print '-' x length($vname) . "\n";
749 }
750
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800751 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700752 $exit = 1;
753 }
754 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800755 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700756 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700757 @fixed_inserted = ();
758 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700759 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700760 @modifierListFile = ();
761 @typeListFile = ();
762 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700763}
764
Joe Perchesd8469f12015-06-25 15:03:00 -0700765if (!$quiet) {
766 if ($^V lt 5.10.0) {
767 print << "EOM"
768
769NOTE: perl $^V is not modern enough to detect all possible issues.
770 An upgrade to at least perl v5.10.0 is suggested.
771EOM
772 }
773 if ($exit) {
774 print << "EOM"
775
776NOTE: If any of the errors are false positives, please report
777 them to the maintainer, see CHECKPATCH in MAINTAINERS.
778EOM
779 }
780}
781
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700782exit($exit);
783
784sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700785 my ($root) = @_;
786
787 my @tree_check = (
788 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
789 "README", "Documentation", "arch", "include", "drivers",
790 "fs", "init", "ipc", "kernel", "lib", "scripts",
791 );
792
793 foreach my $check (@tree_check) {
794 if (! -e $root . '/' . $check) {
795 return 0;
796 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700797 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700798 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700799}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700800
Joe Perches20112472011-07-25 17:13:23 -0700801sub parse_email {
802 my ($formatted_email) = @_;
803
804 my $name = "";
805 my $address = "";
806 my $comment = "";
807
808 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
809 $name = $1;
810 $address = $2;
811 $comment = $3 if defined $3;
812 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
813 $address = $1;
814 $comment = $2 if defined $2;
815 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
816 $address = $1;
817 $comment = $2 if defined $2;
818 $formatted_email =~ s/$address.*$//;
819 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700820 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700821 $name =~ s/^\"|\"$//g;
822 # If there's a name left after stripping spaces and
823 # leading quotes, and the address doesn't have both
824 # leading and trailing angle brackets, the address
825 # is invalid. ie:
826 # "joe smith joe@smith.com" bad
827 # "joe smith <joe@smith.com" bad
828 if ($name ne "" && $address !~ /^<[^>]+>$/) {
829 $name = "";
830 $address = "";
831 $comment = "";
832 }
833 }
834
Joe Perches3705ce52013-07-03 15:05:31 -0700835 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700836 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700837 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700838 $address =~ s/^\<|\>$//g;
839
840 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
841 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
842 $name = "\"$name\"";
843 }
844
845 return ($name, $address, $comment);
846}
847
848sub format_email {
849 my ($name, $address) = @_;
850
851 my $formatted_email;
852
Joe Perches3705ce52013-07-03 15:05:31 -0700853 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700854 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700855 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700856
857 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
858 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
859 $name = "\"$name\"";
860 }
861
862 if ("$name" eq "") {
863 $formatted_email = "$address";
864 } else {
865 $formatted_email = "$name <$address>";
866 }
867
868 return $formatted_email;
869}
870
Joe Perchesd311cd42014-08-06 16:10:57 -0700871sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700872 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700873
Joe Perchesbd474ca2014-08-06 16:11:10 -0700874 foreach my $path (split(/:/, $ENV{PATH})) {
875 if (-e "$path/$bin") {
876 return "$path/$bin";
877 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700878 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700879
Joe Perchesbd474ca2014-08-06 16:11:10 -0700880 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700881}
882
Joe Perches000d1cc12011-07-25 17:13:25 -0700883sub which_conf {
884 my ($conf) = @_;
885
886 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
887 if (-e "$path/$conf") {
888 return "$path/$conf";
889 }
890 }
891
892 return "";
893}
894
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700895sub expand_tabs {
896 my ($str) = @_;
897
898 my $res = '';
899 my $n = 0;
900 for my $c (split(//, $str)) {
901 if ($c eq "\t") {
902 $res .= ' ';
903 $n++;
904 for (; ($n % 8) != 0; $n++) {
905 $res .= ' ';
906 }
907 next;
908 }
909 $res .= $c;
910 $n++;
911 }
912
913 return $res;
914}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700915sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700916 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700917 return $res;
918}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700919
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700920sub line_stats {
921 my ($line) = @_;
922
923 # Drop the diff line leader and expand tabs
924 $line =~ s/^.//;
925 $line = expand_tabs($line);
926
927 # Pick the indent from the front of the line.
928 my ($white) = ($line =~ /^(\s*)/);
929
930 return (length($line), length($white));
931}
932
Andy Whitcroft773647a2008-03-28 14:15:58 -0700933my $sanitise_quote = '';
934
935sub sanitise_line_reset {
936 my ($in_comment) = @_;
937
938 if ($in_comment) {
939 $sanitise_quote = '*/';
940 } else {
941 $sanitise_quote = '';
942 }
943}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700944sub sanitise_line {
945 my ($line) = @_;
946
947 my $res = '';
948 my $l = '';
949
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800950 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700951 my $off = 0;
952 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700953
Andy Whitcroft773647a2008-03-28 14:15:58 -0700954 # Always copy over the diff marker.
955 $res = substr($line, 0, 1);
956
957 for ($off = 1; $off < length($line); $off++) {
958 $c = substr($line, $off, 1);
959
960 # Comments we are wacking completly including the begin
961 # and end, all to $;.
962 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
963 $sanitise_quote = '*/';
964
965 substr($res, $off, 2, "$;$;");
966 $off++;
967 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800968 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700969 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700970 $sanitise_quote = '';
971 substr($res, $off, 2, "$;$;");
972 $off++;
973 next;
974 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700975 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
976 $sanitise_quote = '//';
977
978 substr($res, $off, 2, $sanitise_quote);
979 $off++;
980 next;
981 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700982
983 # A \ in a string means ignore the next character.
984 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
985 $c eq "\\") {
986 substr($res, $off, 2, 'XX');
987 $off++;
988 next;
989 }
990 # Regular quotes.
991 if ($c eq "'" || $c eq '"') {
992 if ($sanitise_quote eq '') {
993 $sanitise_quote = $c;
994
995 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700996 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700997 } elsif ($sanitise_quote eq $c) {
998 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700999 }
1000 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001001
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001002 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001003 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1004 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001005 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1006 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001007 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1008 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001009 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001010 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001011 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001012 }
1013
Daniel Walker113f04a2009-09-21 17:04:35 -07001014 if ($sanitise_quote eq '//') {
1015 $sanitise_quote = '';
1016 }
1017
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001018 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001019 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001020 my $clean = 'X' x length($1);
1021 $res =~ s@\<.*\>@<$clean>@;
1022
1023 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001024 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001025 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001026 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001027 }
1028
Andy Whitcroft00df3442007-06-08 13:47:06 -07001029 return $res;
1030}
1031
Joe Perchesa6962d72013-04-29 16:18:13 -07001032sub get_quoted_string {
1033 my ($line, $rawline) = @_;
1034
Joe Perches33acb542015-06-25 15:02:54 -07001035 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001036 return substr($rawline, $-[0], $+[0] - $-[0]);
1037}
1038
Andy Whitcroft8905a672007-11-28 16:21:06 -08001039sub ctx_statement_block {
1040 my ($linenr, $remain, $off) = @_;
1041 my $line = $linenr - 1;
1042 my $blk = '';
1043 my $soff = $off;
1044 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001045 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001046
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001047 my $loff = 0;
1048
Andy Whitcroft8905a672007-11-28 16:21:06 -08001049 my $type = '';
1050 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001051 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001052 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001053 my $c;
1054 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001055
1056 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001057 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001058 @stack = (['', 0]) if ($#stack == -1);
1059
Andy Whitcroft773647a2008-03-28 14:15:58 -07001060 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001061 # If we are about to drop off the end, pull in more
1062 # context.
1063 if ($off >= $len) {
1064 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001065 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001066 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001067 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001068 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001069 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001070 $len = length($blk);
1071 $line++;
1072 last;
1073 }
1074 # Bail if there is no further context.
1075 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001076 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001077 last;
1078 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001079 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1080 $level++;
1081 $type = '#';
1082 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001083 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001084 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001085 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001086 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001087
Andy Whitcroft773647a2008-03-28 14:15:58 -07001088 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001089
1090 # Handle nested #if/#else.
1091 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1092 push(@stack, [ $type, $level ]);
1093 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1094 ($type, $level) = @{$stack[$#stack - 1]};
1095 } elsif ($remainder =~ /^#\s*endif\b/) {
1096 ($type, $level) = @{pop(@stack)};
1097 }
1098
Andy Whitcroft8905a672007-11-28 16:21:06 -08001099 # Statement ends at the ';' or a close '}' at the
1100 # outermost level.
1101 if ($level == 0 && $c eq ';') {
1102 last;
1103 }
1104
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001105 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001106 if ($level == 0 && $coff_set == 0 &&
1107 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1108 $remainder =~ /^(else)(?:\s|{)/ &&
1109 $remainder !~ /^else\s+if\b/) {
1110 $coff = $off + length($1) - 1;
1111 $coff_set = 1;
1112 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1113 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001114 }
1115
Andy Whitcroft8905a672007-11-28 16:21:06 -08001116 if (($type eq '' || $type eq '(') && $c eq '(') {
1117 $level++;
1118 $type = '(';
1119 }
1120 if ($type eq '(' && $c eq ')') {
1121 $level--;
1122 $type = ($level != 0)? '(' : '';
1123
1124 if ($level == 0 && $coff < $soff) {
1125 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001126 $coff_set = 1;
1127 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001128 }
1129 }
1130 if (($type eq '' || $type eq '{') && $c eq '{') {
1131 $level++;
1132 $type = '{';
1133 }
1134 if ($type eq '{' && $c eq '}') {
1135 $level--;
1136 $type = ($level != 0)? '{' : '';
1137
1138 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001139 if (substr($blk, $off + 1, 1) eq ';') {
1140 $off++;
1141 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001142 last;
1143 }
1144 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001145 # Preprocessor commands end at the newline unless escaped.
1146 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1147 $level--;
1148 $type = '';
1149 $off++;
1150 last;
1151 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001152 $off++;
1153 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001154 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001155 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001156 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001157 $line++;
1158 $remain--;
1159 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001160
1161 my $statement = substr($blk, $soff, $off - $soff + 1);
1162 my $condition = substr($blk, $soff, $coff - $soff + 1);
1163
1164 #warn "STATEMENT<$statement>\n";
1165 #warn "CONDITION<$condition>\n";
1166
Andy Whitcroft773647a2008-03-28 14:15:58 -07001167 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001168
1169 return ($statement, $condition,
1170 $line, $remain + 1, $off - $loff + 1, $level);
1171}
1172
Andy Whitcroftcf655042008-03-04 14:28:20 -08001173sub statement_lines {
1174 my ($stmt) = @_;
1175
1176 # Strip the diff line prefixes and rip blank lines at start and end.
1177 $stmt =~ s/(^|\n)./$1/g;
1178 $stmt =~ s/^\s*//;
1179 $stmt =~ s/\s*$//;
1180
1181 my @stmt_lines = ($stmt =~ /\n/g);
1182
1183 return $#stmt_lines + 2;
1184}
1185
1186sub statement_rawlines {
1187 my ($stmt) = @_;
1188
1189 my @stmt_lines = ($stmt =~ /\n/g);
1190
1191 return $#stmt_lines + 2;
1192}
1193
1194sub statement_block_size {
1195 my ($stmt) = @_;
1196
1197 $stmt =~ s/(^|\n)./$1/g;
1198 $stmt =~ s/^\s*{//;
1199 $stmt =~ s/}\s*$//;
1200 $stmt =~ s/^\s*//;
1201 $stmt =~ s/\s*$//;
1202
1203 my @stmt_lines = ($stmt =~ /\n/g);
1204 my @stmt_statements = ($stmt =~ /;/g);
1205
1206 my $stmt_lines = $#stmt_lines + 2;
1207 my $stmt_statements = $#stmt_statements + 1;
1208
1209 if ($stmt_lines > $stmt_statements) {
1210 return $stmt_lines;
1211 } else {
1212 return $stmt_statements;
1213 }
1214}
1215
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001216sub ctx_statement_full {
1217 my ($linenr, $remain, $off) = @_;
1218 my ($statement, $condition, $level);
1219
1220 my (@chunks);
1221
Andy Whitcroftcf655042008-03-04 14:28:20 -08001222 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001223 ($statement, $condition, $linenr, $remain, $off, $level) =
1224 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001225 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001226 push(@chunks, [ $condition, $statement ]);
1227 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1228 return ($level, $linenr, @chunks);
1229 }
1230
1231 # Pull in the following conditional/block pairs and see if they
1232 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001233 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001234 ($statement, $condition, $linenr, $remain, $off, $level) =
1235 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001236 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001237 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001238 #print "C: push\n";
1239 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001240 }
1241
1242 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001243}
1244
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001245sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001246 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001247 my $line;
1248 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001249 my $blk = '';
1250 my @o;
1251 my @c;
1252 my @res = ();
1253
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001254 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001255 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001256 for ($line = $start; $remain > 0; $line++) {
1257 next if ($rawlines[$line] =~ /^-/);
1258 $remain--;
1259
1260 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001261
1262 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001263 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001264 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001265 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001266 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001267 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001268 $level = pop(@stack);
1269 }
1270
Andy Whitcroft01464f32010-10-26 14:23:19 -07001271 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001272 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1273 if ($off > 0) {
1274 $off--;
1275 next;
1276 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001277
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001278 if ($c eq $close && $level > 0) {
1279 $level--;
1280 last if ($level == 0);
1281 } elsif ($c eq $open) {
1282 $level++;
1283 }
1284 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001285
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001286 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001287 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001288 }
1289
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001290 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001291 }
1292
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001293 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001294}
1295sub ctx_block_outer {
1296 my ($linenr, $remain) = @_;
1297
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001298 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1299 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001300}
1301sub ctx_block {
1302 my ($linenr, $remain) = @_;
1303
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001304 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1305 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001306}
1307sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001308 my ($linenr, $remain, $off) = @_;
1309
1310 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1311 return @r;
1312}
1313sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001314 my ($linenr, $remain) = @_;
1315
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001316 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001317}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001318sub ctx_statement_level {
1319 my ($linenr, $remain, $off) = @_;
1320
1321 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1322}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001323
1324sub ctx_locate_comment {
1325 my ($first_line, $end_line) = @_;
1326
1327 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001328 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001329 return $current_comment if (defined $current_comment);
1330
1331 # Look through the context and try and figure out if there is a
1332 # comment.
1333 my $in_comment = 0;
1334 $current_comment = '';
1335 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001336 my $line = $rawlines[$linenr - 1];
1337 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001338 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1339 $in_comment = 1;
1340 }
1341 if ($line =~ m@/\*@) {
1342 $in_comment = 1;
1343 }
1344 if (!$in_comment && $current_comment ne '') {
1345 $current_comment = '';
1346 }
1347 $current_comment .= $line . "\n" if ($in_comment);
1348 if ($line =~ m@\*/@) {
1349 $in_comment = 0;
1350 }
1351 }
1352
1353 chomp($current_comment);
1354 return($current_comment);
1355}
1356sub ctx_has_comment {
1357 my ($first_line, $end_line) = @_;
1358 my $cmt = ctx_locate_comment($first_line, $end_line);
1359
Andy Whitcroft00df3442007-06-08 13:47:06 -07001360 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001361 ##print "CMMT: $cmt\n";
1362
1363 return ($cmt ne '');
1364}
1365
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001366sub raw_line {
1367 my ($linenr, $cnt) = @_;
1368
1369 my $offset = $linenr - 1;
1370 $cnt++;
1371
1372 my $line;
1373 while ($cnt) {
1374 $line = $rawlines[$offset++];
1375 next if (defined($line) && $line =~ /^-/);
1376 $cnt--;
1377 }
1378
1379 return $line;
1380}
1381
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001382sub cat_vet {
1383 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001384 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001385
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001386 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001387 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1388 $res .= $1;
1389 if ($2 ne '') {
1390 $coded = sprintf("^%c", unpack('C', $2) + 64);
1391 $res .= $coded;
1392 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001393 }
1394 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001395
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001396 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001397}
1398
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001399my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001400my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001401my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001402my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001403
1404sub annotate_reset {
1405 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001406 $av_pending = '_';
1407 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001408 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001409}
1410
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001411sub annotate_values {
1412 my ($stream, $type) = @_;
1413
1414 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001415 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001416 my $cur = $stream;
1417
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001418 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001419
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001420 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001421 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001422 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001423 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001424 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001425 print "WS($1)\n" if ($dbg_values > 1);
1426 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001427 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001428 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001429 }
1430
Florian Micklerc023e4732011-01-12 16:59:58 -08001431 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001432 print "CAST($1)\n" if ($dbg_values > 1);
1433 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001434 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001435
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001436 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001437 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001438 $type = 'T';
1439
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001440 } elsif ($cur =~ /^($Modifier)\s*/) {
1441 print "MODIFIER($1)\n" if ($dbg_values > 1);
1442 $type = 'T';
1443
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001444 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001445 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001446 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001447 push(@av_paren_type, $type);
1448 if ($2 ne '') {
1449 $av_pending = 'N';
1450 }
1451 $type = 'E';
1452
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001453 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001454 print "UNDEF($1)\n" if ($dbg_values > 1);
1455 $av_preprocessor = 1;
1456 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001457
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001458 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001459 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001460 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001461
1462 push(@av_paren_type, $type);
1463 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001464 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001465
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001466 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001467 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1468 $av_preprocessor = 1;
1469
1470 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1471
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001472 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001473
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001474 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001475 print "PRE_END($1)\n" if ($dbg_values > 1);
1476
1477 $av_preprocessor = 1;
1478
1479 # Assume all arms of the conditional end as this
1480 # one does, and continue as if the #endif was not here.
1481 pop(@av_paren_type);
1482 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001483 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001484
1485 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001486 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001487
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001488 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1489 print "ATTR($1)\n" if ($dbg_values > 1);
1490 $av_pending = $type;
1491 $type = 'N';
1492
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001493 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001494 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001495 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001496 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001497 }
1498 $type = 'N';
1499
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001500 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001501 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001502 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001503 $type = 'N';
1504
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001505 } elsif ($cur =~/^(case)/o) {
1506 print "CASE($1)\n" if ($dbg_values > 1);
1507 $av_pend_colon = 'C';
1508 $type = 'N';
1509
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001510 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001511 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001512 $type = 'N';
1513
1514 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001515 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001516 push(@av_paren_type, $av_pending);
1517 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001518 $type = 'N';
1519
1520 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001521 my $new_type = pop(@av_paren_type);
1522 if ($new_type ne '_') {
1523 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001524 print "PAREN('$1') -> $type\n"
1525 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001526 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001527 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001528 }
1529
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001530 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001531 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001532 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001533 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001534
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001535 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1536 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001537 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001538 } elsif ($type eq 'E') {
1539 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001540 }
1541 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1542 $type = 'V';
1543
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001544 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001545 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001546 $type = 'V';
1547
1548 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001549 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001550 $type = 'N';
1551
Andy Whitcroftcf655042008-03-04 14:28:20 -08001552 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001553 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001554 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001555 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001556
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001557 } elsif ($cur =~/^(,)/) {
1558 print "COMMA($1)\n" if ($dbg_values > 1);
1559 $type = 'C';
1560
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001561 } elsif ($cur =~ /^(\?)/o) {
1562 print "QUESTION($1)\n" if ($dbg_values > 1);
1563 $type = 'N';
1564
1565 } elsif ($cur =~ /^(:)/o) {
1566 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1567
1568 substr($var, length($res), 1, $av_pend_colon);
1569 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1570 $type = 'E';
1571 } else {
1572 $type = 'N';
1573 }
1574 $av_pend_colon = 'O';
1575
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001576 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001577 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001578 $type = 'N';
1579
Andy Whitcroft0d413862008-10-15 22:02:16 -07001580 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001581 my $variant;
1582
1583 print "OPV($1)\n" if ($dbg_values > 1);
1584 if ($type eq 'V') {
1585 $variant = 'B';
1586 } else {
1587 $variant = 'U';
1588 }
1589
1590 substr($var, length($res), 1, $variant);
1591 $type = 'N';
1592
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001593 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001594 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001595 if ($1 ne '++' && $1 ne '--') {
1596 $type = 'N';
1597 }
1598
1599 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001600 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001601 }
1602 if (defined $1) {
1603 $cur = substr($cur, length($1));
1604 $res .= $type x length($1);
1605 }
1606 }
1607
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001608 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001609}
1610
Andy Whitcroft8905a672007-11-28 16:21:06 -08001611sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001612 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001613 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001614 ^(?:
1615 $Modifier|
1616 $Storage|
1617 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001618 DEFINE_\S+
1619 )$|
1620 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001621 goto|
1622 return|
1623 case|
1624 else|
1625 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001626 do|
1627 \#|
1628 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001629 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001630 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001631 )}x;
1632 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1633 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001634 # Check for modifiers.
1635 $possible =~ s/\s*$Storage\s*//g;
1636 $possible =~ s/\s*$Sparse\s*//g;
1637 if ($possible =~ /^\s*$/) {
1638
1639 } elsif ($possible =~ /\s/) {
1640 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001641 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001642 if ($modifier !~ $notPermitted) {
1643 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001644 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001645 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001646 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001647
1648 } else {
1649 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001650 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001651 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001652 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001653 } else {
1654 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001655 }
1656}
1657
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001658my $prefix = '';
1659
Joe Perches000d1cc12011-07-25 17:13:25 -07001660sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001661 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001662
Joe Perchescbec18a2014-04-03 14:49:19 -07001663 return defined $use_type{$type} if (scalar keys %use_type > 0);
1664
1665 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001666}
1667
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001668sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001669 my ($level, $type, $msg) = @_;
1670
1671 if (!show_type($type) ||
1672 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001673 return 0;
1674 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001675 my $line;
1676 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001677 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001678 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001679 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001680 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001681 $line = (split('\n', $line))[0] . "\n" if ($terse);
1682
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001683 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001684
1685 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001686}
Joe Perchescbec18a2014-04-03 14:49:19 -07001687
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001688sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001689 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001690}
Joe Perches000d1cc12011-07-25 17:13:25 -07001691
Joe Perchesd752fcc2014-08-06 16:11:05 -07001692sub fixup_current_range {
1693 my ($lineRef, $offset, $length) = @_;
1694
1695 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1696 my $o = $1;
1697 my $l = $2;
1698 my $no = $o + $offset;
1699 my $nl = $l + $length;
1700 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1701 }
1702}
1703
1704sub fix_inserted_deleted_lines {
1705 my ($linesRef, $insertedRef, $deletedRef) = @_;
1706
1707 my $range_last_linenr = 0;
1708 my $delta_offset = 0;
1709
1710 my $old_linenr = 0;
1711 my $new_linenr = 0;
1712
1713 my $next_insert = 0;
1714 my $next_delete = 0;
1715
1716 my @lines = ();
1717
1718 my $inserted = @{$insertedRef}[$next_insert++];
1719 my $deleted = @{$deletedRef}[$next_delete++];
1720
1721 foreach my $old_line (@{$linesRef}) {
1722 my $save_line = 1;
1723 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001724 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001725 $delta_offset = 0;
1726 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1727 $range_last_linenr = $new_linenr;
1728 fixup_current_range(\$line, $delta_offset, 0);
1729 }
1730
1731 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1732 $deleted = @{$deletedRef}[$next_delete++];
1733 $save_line = 0;
1734 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1735 }
1736
1737 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1738 push(@lines, ${$inserted}{'LINE'});
1739 $inserted = @{$insertedRef}[$next_insert++];
1740 $new_linenr++;
1741 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1742 }
1743
1744 if ($save_line) {
1745 push(@lines, $line);
1746 $new_linenr++;
1747 }
1748
1749 $old_linenr++;
1750 }
1751
1752 return @lines;
1753}
1754
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001755sub fix_insert_line {
1756 my ($linenr, $line) = @_;
1757
1758 my $inserted = {
1759 LINENR => $linenr,
1760 LINE => $line,
1761 };
1762 push(@fixed_inserted, $inserted);
1763}
1764
1765sub fix_delete_line {
1766 my ($linenr, $line) = @_;
1767
1768 my $deleted = {
1769 LINENR => $linenr,
1770 LINE => $line,
1771 };
1772
1773 push(@fixed_deleted, $deleted);
1774}
1775
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001776sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001777 my ($type, $msg) = @_;
1778
1779 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001780 our $clean = 0;
1781 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001782 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001783 }
Joe Perches3705ce52013-07-03 15:05:31 -07001784 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001785}
1786sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001787 my ($type, $msg) = @_;
1788
1789 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001790 our $clean = 0;
1791 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001792 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001793 }
Joe Perches3705ce52013-07-03 15:05:31 -07001794 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001795}
1796sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001797 my ($type, $msg) = @_;
1798
1799 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001800 our $clean = 0;
1801 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001802 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001803 }
Joe Perches3705ce52013-07-03 15:05:31 -07001804 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001805}
1806
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001807sub check_absolute_file {
1808 my ($absolute, $herecurr) = @_;
1809 my $file = $absolute;
1810
1811 ##print "absolute<$absolute>\n";
1812
1813 # See if any suffix of this path is a path within the tree.
1814 while ($file =~ s@^[^/]*/@@) {
1815 if (-f "$root/$file") {
1816 ##print "file<$file>\n";
1817 last;
1818 }
1819 }
1820 if (! -f _) {
1821 return 0;
1822 }
1823
1824 # It is, so see if the prefix is acceptable.
1825 my $prefix = $absolute;
1826 substr($prefix, -length($file)) = '';
1827
1828 ##print "prefix<$prefix>\n";
1829 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001830 WARN("USE_RELATIVE_PATH",
1831 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001832 }
1833}
1834
Joe Perches3705ce52013-07-03 15:05:31 -07001835sub trim {
1836 my ($string) = @_;
1837
Joe Perchesb34c6482013-09-11 14:24:01 -07001838 $string =~ s/^\s+|\s+$//g;
1839
1840 return $string;
1841}
1842
1843sub ltrim {
1844 my ($string) = @_;
1845
1846 $string =~ s/^\s+//;
1847
1848 return $string;
1849}
1850
1851sub rtrim {
1852 my ($string) = @_;
1853
1854 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001855
1856 return $string;
1857}
1858
Joe Perches52ea8502013-11-12 15:10:09 -08001859sub string_find_replace {
1860 my ($string, $find, $replace) = @_;
1861
1862 $string =~ s/$find/$replace/g;
1863
1864 return $string;
1865}
1866
Joe Perches3705ce52013-07-03 15:05:31 -07001867sub tabify {
1868 my ($leading) = @_;
1869
1870 my $source_indent = 8;
1871 my $max_spaces_before_tab = $source_indent - 1;
1872 my $spaces_to_tab = " " x $source_indent;
1873
1874 #convert leading spaces to tabs
1875 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1876 #Remove spaces before a tab
1877 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1878
1879 return "$leading";
1880}
1881
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001882sub pos_last_openparen {
1883 my ($line) = @_;
1884
1885 my $pos = 0;
1886
1887 my $opens = $line =~ tr/\(/\(/;
1888 my $closes = $line =~ tr/\)/\)/;
1889
1890 my $last_openparen = 0;
1891
1892 if (($opens == 0) || ($closes >= $opens)) {
1893 return -1;
1894 }
1895
1896 my $len = length($line);
1897
1898 for ($pos = 0; $pos < $len; $pos++) {
1899 my $string = substr($line, $pos);
1900 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1901 $pos += length($1) - 1;
1902 } elsif (substr($line, $pos, 1) eq '(') {
1903 $last_openparen = $pos;
1904 } elsif (index($string, '(') == -1) {
1905 last;
1906 }
1907 }
1908
Joe Perches91cb5192014-04-03 14:49:32 -07001909 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001910}
1911
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001912sub process {
1913 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001914
1915 my $linenr=0;
1916 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001917 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001918 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001919 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001920
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001921 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001922 my $indent;
1923 my $previndent=0;
1924 my $stashindent=0;
1925
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001926 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001927 my $signoff = 0;
1928 my $is_patch = 0;
1929
Joe Perches29ee1b02014-08-06 16:10:35 -07001930 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001931 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches2a076f42015-04-16 12:44:28 -07001932 my $commit_log_long_line = 0;
Joe Perches13f19372014-08-06 16:10:59 -07001933 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001934 my $non_utf8_charset = 0;
1935
Joe Perches365dd4e2014-08-06 16:10:42 -07001936 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001937 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001938
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001939 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001940 our $cnt_lines = 0;
1941 our $cnt_error = 0;
1942 our $cnt_warn = 0;
1943 our $cnt_chk = 0;
1944
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001945 # Trace the real file/line as we go.
1946 my $realfile = '';
1947 my $realline = 0;
1948 my $realcnt = 0;
1949 my $here = '';
1950 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001951 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001952 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001953 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001954
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001955 my $prev_values = 'E';
1956
1957 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001958 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001959 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001960 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001961 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001962
Joe Perches7e51f192013-09-11 14:23:57 -07001963 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001964
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001965 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001966 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001967 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001968 my @setup_docs = ();
1969 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001970
Joe Perchesd8b07712013-11-12 15:10:06 -08001971 my $camelcase_file_seeded = 0;
1972
Andy Whitcroft773647a2008-03-28 14:15:58 -07001973 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001974 my $line;
1975 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001976 $linenr++;
1977 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001978
Joe Perches3705ce52013-07-03 15:05:31 -07001979 push(@fixed, $rawline) if ($fix);
1980
Andy Whitcroft773647a2008-03-28 14:15:58 -07001981 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001982 $setup_docs = 0;
1983 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1984 $setup_docs = 1;
1985 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001986 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001987 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001988 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1989 $realline=$1-1;
1990 if (defined $2) {
1991 $realcnt=$3+1;
1992 } else {
1993 $realcnt=1+1;
1994 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001995 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001996
1997 # Guestimate if this is a continuing comment. Run
1998 # the context looking for a comment "edge". If this
1999 # edge is a close comment then we must be in a comment
2000 # at context start.
2001 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002002 my $cnt = $realcnt;
2003 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2004 next if (defined $rawlines[$ln - 1] &&
2005 $rawlines[$ln - 1] =~ /^-/);
2006 $cnt--;
2007 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002008 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002009 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2010 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2011 ($edge) = $1;
2012 last;
2013 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002014 }
2015 if (defined $edge && $edge eq '*/') {
2016 $in_comment = 1;
2017 }
2018
2019 # Guestimate if this is a continuing comment. If this
2020 # is the start of a diff block and this line starts
2021 # ' *' then it is very likely a comment.
2022 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002023 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002024 {
2025 $in_comment = 1;
2026 }
2027
2028 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2029 sanitise_line_reset($in_comment);
2030
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002031 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002032 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002033 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002034 $line = sanitise_line($rawline);
2035 }
2036 push(@lines, $line);
2037
2038 if ($realcnt > 1) {
2039 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2040 } else {
2041 $realcnt = 0;
2042 }
2043
2044 #print "==>$rawline\n";
2045 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002046
2047 if ($setup_docs && $line =~ /^\+/) {
2048 push(@setup_docs, $line);
2049 }
2050 }
2051
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002052 $prefix = '';
2053
Andy Whitcroft773647a2008-03-28 14:15:58 -07002054 $realcnt = 0;
2055 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002056 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002057 foreach my $line (@lines) {
2058 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002059 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002060 my $sline = $line; #copy of $line
2061 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002062
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002063 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002064
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002065#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002066 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002067 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002068 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002069 $realline=$1-1;
2070 if (defined $2) {
2071 $realcnt=$3+1;
2072 } else {
2073 $realcnt=1+1;
2074 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002075 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002076 $prev_values = 'E';
2077
Andy Whitcroft773647a2008-03-28 14:15:58 -07002078 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002079 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002080 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002081 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002082 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002083
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002084# track the line number as we move through the hunk, note that
2085# new versions of GNU diff omit the leading space on completely
2086# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002087 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002088 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002089 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002090
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002091 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002092 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002093
2094 # Track the previous line.
2095 ($prevline, $stashline) = ($stashline, $line);
2096 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002097 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2098
Andy Whitcroft773647a2008-03-28 14:15:58 -07002099 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002100
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002101 } elsif ($realcnt == 1) {
2102 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002103 }
2104
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002105 my $hunk_line = ($realcnt != 0);
2106
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002107#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002108 $prefix = "$filename:$realline: " if ($emacs && $file);
2109 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2110
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002111 $here = "#$linenr: " if (!$file);
2112 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002113
Joe Perches2ac73b42014-06-04 16:12:05 -07002114 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002115 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002116 if ($line =~ /^diff --git.*?(\S+)$/) {
2117 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002118 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002119 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002120 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002121 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002122 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002123 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002124 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002125
2126 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002127 if (!$file && $tree && $p1_prefix ne '' &&
2128 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002129 WARN("PATCH_PREFIX",
2130 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002131 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002132
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002133 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002134 ERROR("MODIFIED_INCLUDE_ASM",
2135 "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 -07002136 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002137 $found_file = 1;
2138 }
2139
2140 if ($found_file) {
2141 if ($realfile =~ m@^(drivers/net/|net/)@) {
2142 $check = 1;
2143 } else {
2144 $check = $check_orig;
2145 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002146 next;
2147 }
2148
Randy Dunlap389834b2007-06-08 13:47:03 -07002149 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002150
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002151 my $hereline = "$here\n$rawline\n";
2152 my $herecurr = "$here\n$rawline\n";
2153 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002154
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002155 $cnt_lines++ if ($realcnt != 0);
2156
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002157# Check for incorrect file permissions
2158 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2159 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002160 if ($realfile !~ m@scripts/@ &&
2161 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002162 ERROR("EXECUTE_PERMISSIONS",
2163 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002164 }
2165 }
2166
Joe Perches20112472011-07-25 17:13:23 -07002167# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002168 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002169 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002170 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002171 }
2172
Joe Perchese0d975b2014-12-10 15:51:49 -08002173# Check if MAINTAINERS is being updated. If so, there's probably no need to
2174# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2175 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2176 $reported_maintainer_file = 1;
2177 }
2178
Joe Perches20112472011-07-25 17:13:23 -07002179# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002180 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002181 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002182 my $space_before = $1;
2183 my $sign_off = $2;
2184 my $space_after = $3;
2185 my $email = $4;
2186 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2187
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002188 if ($sign_off !~ /$signature_tags/) {
2189 WARN("BAD_SIGN_OFF",
2190 "Non-standard signature: $sign_off\n" . $herecurr);
2191 }
Joe Perches20112472011-07-25 17:13:23 -07002192 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002193 if (WARN("BAD_SIGN_OFF",
2194 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2195 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002196 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002197 "$ucfirst_sign_off $email";
2198 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002199 }
Joe Perches20112472011-07-25 17:13:23 -07002200 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002201 if (WARN("BAD_SIGN_OFF",
2202 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2203 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002204 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002205 "$ucfirst_sign_off $email";
2206 }
2207
Joe Perches20112472011-07-25 17:13:23 -07002208 }
2209 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002210 if (WARN("BAD_SIGN_OFF",
2211 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2212 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002213 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002214 "$ucfirst_sign_off $email";
2215 }
Joe Perches20112472011-07-25 17:13:23 -07002216 }
2217
2218 my ($email_name, $email_address, $comment) = parse_email($email);
2219 my $suggested_email = format_email(($email_name, $email_address));
2220 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002221 ERROR("BAD_SIGN_OFF",
2222 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002223 } else {
2224 my $dequoted = $suggested_email;
2225 $dequoted =~ s/^"//;
2226 $dequoted =~ s/" </ </;
2227 # Don't force email to have quotes
2228 # Allow just an angle bracketed address
2229 if ("$dequoted$comment" ne $email &&
2230 "<$email_address>$comment" ne $email &&
2231 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002232 WARN("BAD_SIGN_OFF",
2233 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002234 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002235 }
Joe Perches7e51f192013-09-11 14:23:57 -07002236
2237# Check for duplicate signatures
2238 my $sig_nospace = $line;
2239 $sig_nospace =~ s/\s//g;
2240 $sig_nospace = lc($sig_nospace);
2241 if (defined $signatures{$sig_nospace}) {
2242 WARN("BAD_SIGN_OFF",
2243 "Duplicate signature\n" . $herecurr);
2244 } else {
2245 $signatures{$sig_nospace} = 1;
2246 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002247 }
2248
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002249# Check email subject for common tools that don't need to be mentioned
2250 if ($in_header_lines &&
2251 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2252 WARN("EMAIL_SUBJECT",
2253 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2254 }
2255
Joe Perches9b3189e2014-06-04 16:12:10 -07002256# Check for old stable address
2257 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2258 ERROR("STABLE_ADDRESS",
2259 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2260 }
2261
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002262# Check for unwanted Gerrit info
2263 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2264 ERROR("GERRIT_CHANGE_ID",
2265 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2266 }
2267
Joe Perches2a076f42015-04-16 12:44:28 -07002268# Check for line lengths > 75 in commit log, warn once
2269 if ($in_commit_log && !$commit_log_long_line &&
2270 length($line) > 75) {
2271 WARN("COMMIT_LOG_LONG_LINE",
2272 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2273 $commit_log_long_line = 1;
2274 }
2275
Joe Perches0d7835f2015-02-13 14:38:35 -08002276# Check for git id commit length and improperly formed commit descriptions
2277 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002278 my $init_char = $1;
2279 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002280 my $short = 1;
2281 my $long = 0;
2282 my $case = 1;
2283 my $space = 1;
2284 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002285 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002286 my $id = '0123456789ab';
2287 my $orig_desc = "commit description";
2288 my $description = "";
2289
2290 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2291 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2292 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2293 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2294 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2295 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002296 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002297 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2298 defined $rawlines[$linenr] &&
2299 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2300 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002301 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002302 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2303 defined $rawlines[$linenr] &&
2304 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2305 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2306 $orig_desc = $1;
2307 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2308 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002309 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002310 }
2311
2312 ($id, $description) = git_commit_info($orig_commit,
2313 $id, $orig_desc);
2314
Joe Perches19c146a2015-02-13 14:39:00 -08002315 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002316 ERROR("GIT_COMMIT_ID",
2317 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2318 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002319 }
2320
Joe Perches13f19372014-08-06 16:10:59 -07002321# Check for added, moved or deleted files
2322 if (!$reported_maintainer_file && !$in_commit_log &&
2323 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2324 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2325 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2326 (defined($1) || defined($2))))) {
2327 $reported_maintainer_file = 1;
2328 WARN("FILE_PATH_CHANGES",
2329 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2330 }
2331
Andy Whitcroft00df3442007-06-08 13:47:06 -07002332# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002333 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002334 ERROR("CORRUPTED_PATCH",
2335 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002336 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002337 }
2338
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002339# Check for absolute kernel paths.
2340 if ($tree) {
2341 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2342 my $file = $1;
2343
2344 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2345 check_absolute_file($1, $herecurr)) {
2346 #
2347 } else {
2348 check_absolute_file($file, $herecurr);
2349 }
2350 }
2351 }
2352
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002353# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2354 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002355 $rawline !~ m/^$UTF8*$/) {
2356 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2357
2358 my $blank = copy_spacing($rawline);
2359 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2360 my $hereptr = "$hereline$ptr\n";
2361
Joe Perches34d99212011-07-25 17:13:26 -07002362 CHK("INVALID_UTF8",
2363 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002364 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002365
Joe Perches15662b32011-10-31 17:13:12 -07002366# Check if it's the start of a commit log
2367# (not a header line and we haven't seen the patch filename)
2368 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002369 !($rawline =~ /^\s+\S/ ||
2370 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002371 $in_header_lines = 0;
2372 $in_commit_log = 1;
2373 }
2374
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002375# Check if there is UTF-8 in a commit log when a mail header has explicitly
2376# declined it, i.e defined some charset where it is missing.
2377 if ($in_header_lines &&
2378 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2379 $1 !~ /utf-8/i) {
2380 $non_utf8_charset = 1;
2381 }
2382
2383 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002384 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002385 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002386 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2387 }
2388
Kees Cook66b47b42014-10-13 15:51:57 -07002389# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002390 if (defined($misspellings) &&
2391 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002392 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002393 my $typo = $1;
2394 my $typo_fix = $spelling_fix{lc($typo)};
2395 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2396 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2397 my $msg_type = \&WARN;
2398 $msg_type = \&CHK if ($file);
2399 if (&{$msg_type}("TYPO_SPELLING",
2400 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2401 $fix) {
2402 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2403 }
2404 }
2405 }
2406
Andy Whitcroft306708542008-10-15 22:02:28 -07002407# ignore non-hunk lines and lines being removed
2408 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002409
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002410#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002411 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002412 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002413 if (ERROR("DOS_LINE_ENDINGS",
2414 "DOS line endings\n" . $herevet) &&
2415 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002416 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002417 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002418 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2419 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002420 if (ERROR("TRAILING_WHITESPACE",
2421 "trailing whitespace\n" . $herevet) &&
2422 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002423 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002424 }
2425
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002426 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002427 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002428
Josh Triplett4783f892013-11-12 15:10:12 -08002429# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002430 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002431 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2432 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002433 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2434 my $msg_type = \&ERROR;
2435 $msg_type = \&CHK if ($file);
2436 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002437 "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 -08002438 }
2439
Andi Kleen33549572010-05-24 14:33:29 -07002440# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002441# Only applies when adding the entry originally, after that we do not have
2442# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002443 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002444 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002445 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002446 my $cnt = $realcnt;
2447 my $ln = $linenr + 1;
2448 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002449 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002450 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002451 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002452 $f = $lines[$ln - 1];
2453 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2454 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002455
2456 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002457 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002458
Joe Perches8d73e0e2014-08-06 16:10:46 -07002459 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002460 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002461 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002462 $length = -1;
2463 }
2464
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002465 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002466 $f =~ s/#.*//;
2467 $f =~ s/^\s+//;
2468 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002469 if ($f =~ /^\s*config\s/) {
2470 $is_end = 1;
2471 last;
2472 }
Andi Kleen33549572010-05-24 14:33:29 -07002473 $length++;
2474 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002475 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2476 WARN("CONFIG_DESCRIPTION",
2477 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2478 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002479 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002480 }
2481
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002482# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2483 if ($realfile =~ /Kconfig/ &&
2484 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2485 WARN("CONFIG_EXPERIMENTAL",
2486 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2487 }
2488
Christoph Jaeger327953e2015-02-13 14:38:29 -08002489# discourage the use of boolean for type definition attributes of Kconfig options
2490 if ($realfile =~ /Kconfig/ &&
2491 $line =~ /^\+\s*\bboolean\b/) {
2492 WARN("CONFIG_TYPE_BOOLEAN",
2493 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2494 }
2495
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002496 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2497 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2498 my $flag = $1;
2499 my $replacement = {
2500 'EXTRA_AFLAGS' => 'asflags-y',
2501 'EXTRA_CFLAGS' => 'ccflags-y',
2502 'EXTRA_CPPFLAGS' => 'cppflags-y',
2503 'EXTRA_LDFLAGS' => 'ldflags-y',
2504 };
2505
2506 WARN("DEPRECATED_VARIABLE",
2507 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2508 }
2509
Rob Herringbff5da42014-01-23 15:54:51 -08002510# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002511 if (defined $root &&
2512 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2513 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2514
Rob Herringbff5da42014-01-23 15:54:51 -08002515 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2516
Florian Vaussardcc933192014-04-03 14:49:27 -07002517 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2518 my $vp_file = $dt_path . "vendor-prefixes.txt";
2519
Rob Herringbff5da42014-01-23 15:54:51 -08002520 foreach my $compat (@compats) {
2521 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002522 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2523 my $compat3 = $compat;
2524 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2525 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002526 if ( $? >> 8 ) {
2527 WARN("UNDOCUMENTED_DT_STRING",
2528 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2529 }
2530
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002531 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2532 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002533 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002534 if ( $? >> 8 ) {
2535 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002536 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002537 }
2538 }
2539 }
2540
Andy Whitcroft5368df202008-10-15 22:02:27 -07002541# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002542 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002543
Joe Perches47e0c882015-06-25 15:02:57 -07002544# line length limit (with some exclusions)
2545#
2546# There are a few types of lines that may extend beyond $max_line_length:
2547# logging functions like pr_info that end in a string
2548# lines with a single string
2549# #defines that are a single string
2550#
2551# There are 3 different line length message types:
2552# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2553# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2554# LONG_LINE all other lines longer than $max_line_length
2555#
2556# if LONG_LINE is ignored, the other 2 types are also ignored
2557#
2558
2559 if ($length > $max_line_length) {
2560 my $msg_type = "LONG_LINE";
2561
2562 # Check the allowed long line types first
2563
2564 # logging functions that end in a string that starts
2565 # before $max_line_length
2566 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2567 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2568 $msg_type = "";
2569
2570 # lines with only strings (w/ possible termination)
2571 # #defines with only strings
2572 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2573 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2574 $msg_type = "";
2575
2576 # Otherwise set the alternate message types
2577
2578 # a comment starts before $max_line_length
2579 } elsif ($line =~ /($;[\s$;]*)$/ &&
2580 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2581 $msg_type = "LONG_LINE_COMMENT"
2582
2583 # a quoted string starts before $max_line_length
2584 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2585 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2586 $msg_type = "LONG_LINE_STRING"
2587 }
2588
2589 if ($msg_type ne "" &&
2590 (show_type("LONG_LINE") || show_type($msg_type))) {
2591 WARN($msg_type,
2592 "line over $max_line_length characters\n" . $herecurr);
2593 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002594 }
2595
Andy Whitcroft8905a672007-11-28 16:21:06 -08002596# check for adding lines without a newline.
2597 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002598 WARN("MISSING_EOF_NEWLINE",
2599 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002600 }
2601
Mike Frysinger42e41c52009-09-21 17:04:40 -07002602# Blackfin: use hi/lo macros
2603 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2604 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2605 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002606 ERROR("LO_MACRO",
2607 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002608 }
2609 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2610 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002611 ERROR("HI_MACRO",
2612 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002613 }
2614 }
2615
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002616# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002617 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002618
2619# at the beginning of a line any tabs must come first and anything
2620# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002621 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2622 $rawline =~ /^\+\s* \s*/) {
2623 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002624 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002625 if (ERROR("CODE_INDENT",
2626 "code indent should use tabs where possible\n" . $herevet) &&
2627 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002628 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002629 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002630 }
2631
Alberto Panizzo08e44362010-03-05 13:43:54 -08002632# check for space before tabs.
2633 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2634 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002635 if (WARN("SPACE_BEFORE_TAB",
2636 "please, no space before tabs\n" . $herevet) &&
2637 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002638 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002639 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002640 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002641 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002642 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002643 }
2644
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002645# check for && or || at the start of a line
2646 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2647 CHK("LOGICAL_CONTINUATIONS",
2648 "Logical continuations should be on the previous line\n" . $hereprev);
2649 }
2650
2651# check multi-line statement indentation matches previous line
2652 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002653 $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 -07002654 $prevline =~ /^\+(\t*)(.*)$/;
2655 my $oldindent = $1;
2656 my $rest = $2;
2657
2658 my $pos = pos_last_openparen($rest);
2659 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002660 $line =~ /^(\+| )([ \t]*)/;
2661 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002662
2663 my $goodtabindent = $oldindent .
2664 "\t" x ($pos / 8) .
2665 " " x ($pos % 8);
2666 my $goodspaceindent = $oldindent . " " x $pos;
2667
2668 if ($newindent ne $goodtabindent &&
2669 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002670
2671 if (CHK("PARENTHESIS_ALIGNMENT",
2672 "Alignment should match open parenthesis\n" . $hereprev) &&
2673 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002674 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002675 s/^\+[ \t]*/\+$goodtabindent/;
2676 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002677 }
2678 }
2679 }
2680
Joe Perches6ab3a972015-04-16 12:44:05 -07002681# check for space after cast like "(int) foo" or "(struct foo) bar"
2682# avoid checking a few false positives:
2683# "sizeof(<type>)" or "__alignof__(<type>)"
2684# function pointer declarations like "(*foo)(int) = bar;"
2685# structure definitions like "(struct foo) { 0 };"
2686# multiline macros that define functions
2687# known attributes or the __attribute__ keyword
2688 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2689 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002690 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002691 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002692 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002693 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002694 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002695 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002696 }
2697
Joe Perches05880602012-10-04 17:13:35 -07002698 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002699 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002700 $rawline =~ /^\+[ \t]*\*/ &&
2701 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002702 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2703 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2704 }
2705
2706 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002707 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2708 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002709 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002710 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2711 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2712 "networking block comments start with * on subsequent lines\n" . $hereprev);
2713 }
2714
2715 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002716 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2717 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2718 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2719 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002720 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2721 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2722 }
2723
Joe Perches7f619192014-08-06 16:10:39 -07002724# check for missing blank lines after struct/union declarations
2725# with exceptions for various attributes and macros
2726 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2727 $line =~ /^\+/ &&
2728 !($line =~ /^\+\s*$/ ||
2729 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2730 $line =~ /^\+\s*MODULE_/i ||
2731 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2732 $line =~ /^\+[a-z_]*init/ ||
2733 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2734 $line =~ /^\+\s*DECLARE/ ||
2735 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002736 if (CHK("LINE_SPACING",
2737 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2738 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002739 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002740 }
Joe Perches7f619192014-08-06 16:10:39 -07002741 }
2742
Joe Perches365dd4e2014-08-06 16:10:42 -07002743# check for multiple consecutive blank lines
2744 if ($prevline =~ /^[\+ ]\s*$/ &&
2745 $line =~ /^\+\s*$/ &&
2746 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002747 if (CHK("LINE_SPACING",
2748 "Please don't use multiple blank lines\n" . $hereprev) &&
2749 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002750 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002751 }
2752
Joe Perches365dd4e2014-08-06 16:10:42 -07002753 $last_blank_line = $linenr;
2754 }
2755
Joe Perches3b617e32014-04-03 14:49:28 -07002756# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002757 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2758 # actual declarations
2759 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002760 # function pointer declarations
2761 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002762 # foo bar; where foo is some local typedef or #define
2763 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2764 # known declaration macros
2765 $prevline =~ /^\+\s+$declaration_macros/) &&
2766 # for "else if" which can look like "$Ident $Ident"
2767 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2768 # other possible extensions of declaration lines
2769 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2770 # not starting a section or a macro "\" extended line
2771 $prevline =~ /(?:\{\s*|\\)$/) &&
2772 # looks like a declaration
2773 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002774 # function pointer declarations
2775 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002776 # foo bar; where foo is some local typedef or #define
2777 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2778 # known declaration macros
2779 $sline =~ /^\+\s+$declaration_macros/ ||
2780 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002781 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002782 # start or end of block or continuation of declaration
2783 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2784 # bitfield continuation
2785 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2786 # other possible extensions of declaration lines
2787 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2788 # indentation of previous and current line are the same
2789 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002790 if (WARN("LINE_SPACING",
2791 "Missing a blank line after declarations\n" . $hereprev) &&
2792 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002793 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002794 }
Joe Perches3b617e32014-04-03 14:49:28 -07002795 }
2796
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002797# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002798# Exceptions:
2799# 1) within comments
2800# 2) indented preprocessor commands
2801# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002802 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002803 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002804 if (WARN("LEADING_SPACE",
2805 "please, no spaces at the start of a line\n" . $herevet) &&
2806 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002807 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002808 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002809 }
2810
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002811# check we are in a valid C source file if not then ignore this hunk
2812 next if ($realfile !~ /\.(h|c)$/);
2813
Joe Perches032a4c02014-08-06 16:10:29 -07002814# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002815# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002816# if the previous line is a break or return and is indented 1 tab more...
2817 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2818 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002819 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2820 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2821 defined $lines[$linenr] &&
2822 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002823 WARN("UNNECESSARY_ELSE",
2824 "else is not generally useful after a break or return\n" . $hereprev);
2825 }
2826 }
2827
Joe Perchesc00df192014-08-06 16:11:01 -07002828# check indentation of a line with a break;
2829# if the previous line is a goto or return and is indented the same # of tabs
2830 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2831 my $tabs = $1;
2832 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2833 WARN("UNNECESSARY_BREAK",
2834 "break is not useful after a goto or return\n" . $hereprev);
2835 }
2836 }
2837
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002838# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2839 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2840 WARN("CONFIG_EXPERIMENTAL",
2841 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2842 }
2843
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002844# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002845 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002846 WARN("CVS_KEYWORD",
2847 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002848 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002849
Mike Frysinger42e41c52009-09-21 17:04:40 -07002850# Blackfin: don't use __builtin_bfin_[cs]sync
2851 if ($line =~ /__builtin_bfin_csync/) {
2852 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002853 ERROR("CSYNC",
2854 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002855 }
2856 if ($line =~ /__builtin_bfin_ssync/) {
2857 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002858 ERROR("SSYNC",
2859 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002860 }
2861
Joe Perches56e77d72013-02-21 16:44:14 -08002862# check for old HOTPLUG __dev<foo> section markings
2863 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2864 WARN("HOTPLUG_SECTION",
2865 "Using $1 is unnecessary\n" . $herecurr);
2866 }
2867
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002868# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002869 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2870 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002871#print "LINE<$line>\n";
2872 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002873 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002874 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002875 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002876 $stat =~ s/\n./\n /g;
2877 $cond =~ s/\n./\n /g;
2878
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002879#print "linenr<$linenr> <$stat>\n";
2880 # If this statement has no statement boundaries within
2881 # it there is no point in retrying a statement scan
2882 # until we hit end of it.
2883 my $frag = $stat; $frag =~ s/;+\s*$//;
2884 if ($frag !~ /(?:{|;)/) {
2885#print "skip<$line_nr_next>\n";
2886 $suppress_statement = $line_nr_next;
2887 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002888
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002889 # Find the real next line.
2890 $realline_next = $line_nr_next;
2891 if (defined $realline_next &&
2892 (!defined $lines[$realline_next - 1] ||
2893 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2894 $realline_next++;
2895 }
2896
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002897 my $s = $stat;
2898 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002899
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002900 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002901 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002902
2903 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002904 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002905
Andy Whitcroft463f2862009-09-21 17:04:34 -07002906 } elsif ($s =~ /^.\s*else\b/s) {
2907
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002908 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002909 } 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 -07002910 my $type = $1;
2911 $type =~ s/\s+/ /g;
2912 possible($type, "A:" . $s);
2913
Andy Whitcroft8905a672007-11-28 16:21:06 -08002914 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002915 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002916 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002917 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002918
2919 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002920 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002921 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002922 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002923
2924 # Check for any sort of function declaration.
2925 # int foo(something bar, other baz);
2926 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002927 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 -08002928 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002929
Andy Whitcroftcf655042008-03-04 14:28:20 -08002930 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002931 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002932 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002933
Andy Whitcroft8905a672007-11-28 16:21:06 -08002934 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002935 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002936
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002937 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002938 }
2939 }
2940 }
2941
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002942 }
2943
Andy Whitcroft653d4872007-06-23 17:16:34 -07002944#
2945# Checks which may be anchored in the context.
2946#
2947
2948# Check for switch () and associated case and default
2949# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002950 if ($line=~/\bswitch\s*\(.*\)/) {
2951 my $err = '';
2952 my $sep = '';
2953 my @ctx = ctx_block_outer($linenr, $realcnt);
2954 shift(@ctx);
2955 for my $ctx (@ctx) {
2956 my ($clen, $cindent) = line_stats($ctx);
2957 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2958 $indent != $cindent) {
2959 $err .= "$sep$ctx\n";
2960 $sep = '';
2961 } else {
2962 $sep = "[...]\n";
2963 }
2964 }
2965 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002966 ERROR("SWITCH_CASE_INDENT_LEVEL",
2967 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002968 }
2969 }
2970
2971# if/while/etc brace do not go on next line, unless defining a do while loop,
2972# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002973 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 -07002974 my $pre_ctx = "$1$2";
2975
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002976 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002977
2978 if ($line =~ /^\+\t{6,}/) {
2979 WARN("DEEP_INDENTATION",
2980 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2981 }
2982
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002983 my $ctx_cnt = $realcnt - $#ctx - 1;
2984 my $ctx = join("\n", @ctx);
2985
Andy Whitcroft548596d2008-07-23 21:29:01 -07002986 my $ctx_ln = $linenr;
2987 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002988
Andy Whitcroft548596d2008-07-23 21:29:01 -07002989 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2990 defined $lines[$ctx_ln - 1] &&
2991 $lines[$ctx_ln - 1] =~ /^-/)) {
2992 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2993 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002994 $ctx_ln++;
2995 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002996
Andy Whitcroft53210162008-07-23 21:29:03 -07002997 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2998 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002999
Joe Perchesd752fcc2014-08-06 16:11:05 -07003000 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003001 ERROR("OPEN_BRACE",
3002 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003003 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003004 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003005 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3006 $ctx =~ /\)\s*\;\s*$/ &&
3007 defined $lines[$ctx_ln - 1])
3008 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003009 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3010 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003011 WARN("TRAILING_SEMICOLON",
3012 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003013 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003014 }
3015 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003016 }
3017
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003018# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003019 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 -08003020 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3021 ctx_statement_block($linenr, $realcnt, 0)
3022 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003023 my ($s, $c) = ($stat, $cond);
3024
3025 substr($s, 0, length($c), '');
3026
3027 # Make sure we remove the line prefixes as we have
3028 # none on the first line, and are going to readd them
3029 # where necessary.
3030 $s =~ s/\n./\n/gs;
3031
3032 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003033 my @newlines = ($c =~ /\n/gs);
3034 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003035
3036 # We want to check the first line inside the block
3037 # starting at the end of the conditional, so remove:
3038 # 1) any blank line termination
3039 # 2) any opening brace { on end of the line
3040 # 3) any do (...) {
3041 my $continuation = 0;
3042 my $check = 0;
3043 $s =~ s/^.*\bdo\b//;
3044 $s =~ s/^\s*{//;
3045 if ($s =~ s/^\s*\\//) {
3046 $continuation = 1;
3047 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003048 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003049 $check = 1;
3050 $cond_lines++;
3051 }
3052
3053 # Also ignore a loop construct at the end of a
3054 # preprocessor statement.
3055 if (($prevline =~ /^.\s*#\s*define\s/ ||
3056 $prevline =~ /\\\s*$/) && $continuation == 0) {
3057 $check = 0;
3058 }
3059
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003060 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003061 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003062 while ($cond_ptr != $cond_lines) {
3063 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003064
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003065 # If we see an #else/#elif then the code
3066 # is not linear.
3067 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3068 $check = 0;
3069 }
3070
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003071 # Ignore:
3072 # 1) blank lines, they should be at 0,
3073 # 2) preprocessor lines, and
3074 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003075 if ($continuation ||
3076 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003077 $s =~ /^\s*#\s*?/ ||
3078 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003079 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003080 if ($s =~ s/^.*?\n//) {
3081 $cond_lines++;
3082 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003083 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003084 }
3085
3086 my (undef, $sindent) = line_stats("+" . $s);
3087 my $stat_real = raw_line($linenr, $cond_lines);
3088
3089 # Check if either of these lines are modified, else
3090 # this is not this patch's fault.
3091 if (!defined($stat_real) ||
3092 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3093 $check = 0;
3094 }
3095 if (defined($stat_real) && $cond_lines > 1) {
3096 $stat_real = "[...]\n$stat_real";
3097 }
3098
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003099 #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 -07003100
3101 if ($check && (($sindent % 8) != 0 ||
3102 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003103 WARN("SUSPECT_CODE_INDENT",
3104 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003105 }
3106 }
3107
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003108 # Track the 'values' across context and added lines.
3109 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003110 my ($curr_values, $curr_vars) =
3111 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003112 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003113 if ($dbg_values) {
3114 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003115 print "$linenr > .$outline\n";
3116 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003117 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003118 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003119 $prev_values = substr($curr_values, -1);
3120
Andy Whitcroft00df3442007-06-08 13:47:06 -07003121#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003122 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003123
Andy Whitcroft653d4872007-06-23 17:16:34 -07003124# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003125 if ($dbg_type) {
3126 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003127 ERROR("TEST_TYPE",
3128 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003129 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003130 ERROR("TEST_NOT_TYPE",
3131 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003132 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003133 next;
3134 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003135# TEST: allow direct testing of the attribute matcher.
3136 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003137 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003138 ERROR("TEST_ATTR",
3139 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003140 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003141 ERROR("TEST_NOT_ATTR",
3142 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003143 }
3144 next;
3145 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003146
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003147# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003148 if ($line =~ /^.\s*{/ &&
3149 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003150 if (ERROR("OPEN_BRACE",
3151 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003152 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3153 fix_delete_line($fixlinenr - 1, $prevrawline);
3154 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003155 my $fixedline = $prevrawline;
3156 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003157 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003158 $fixedline = $line;
3159 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003160 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003161 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003162 }
3163
Andy Whitcroft653d4872007-06-23 17:16:34 -07003164#
3165# Checks which are anchored on the added line.
3166#
3167
3168# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003169 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003170 my $path = $1;
3171 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003172 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003173 "malformed #include filename\n" . $herecurr);
3174 }
3175 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3176 ERROR("UAPI_INCLUDE",
3177 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003178 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003179 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003180
3181# no C99 // comments
3182 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003183 if (ERROR("C99_COMMENTS",
3184 "do not use C99 // comments\n" . $herecurr) &&
3185 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003186 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003187 if ($line =~ /\/\/(.*)$/) {
3188 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003189 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003190 }
3191 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003192 }
3193 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003194 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003195 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003196
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003197# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3198# the whole statement.
3199#print "APW <$lines[$realline_next - 1]>\n";
3200 if (defined $realline_next &&
3201 exists $lines[$realline_next - 1] &&
3202 !defined $suppress_export{$realline_next} &&
3203 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3204 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003205 # Handle definitions which produce identifiers with
3206 # a prefix:
3207 # XXX(foo);
3208 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003209 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003210 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003211 $name =~ /^${Ident}_$2/) {
3212#print "FOO C name<$name>\n";
3213 $suppress_export{$realline_next} = 1;
3214
3215 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003216 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003217 ^.DEFINE_$Ident\(\Q$name\E\)|
3218 ^.DECLARE_$Ident\(\Q$name\E\)|
3219 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003220 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3221 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003222 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003223#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3224 $suppress_export{$realline_next} = 2;
3225 } else {
3226 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003227 }
3228 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003229 if (!defined $suppress_export{$linenr} &&
3230 $prevline =~ /^.\s*$/ &&
3231 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3232 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3233#print "FOO B <$lines[$linenr - 1]>\n";
3234 $suppress_export{$linenr} = 2;
3235 }
3236 if (defined $suppress_export{$linenr} &&
3237 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003238 WARN("EXPORT_SYMBOL",
3239 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003240 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003241
Joe Eloff5150bda2010-08-09 17:21:00 -07003242# check for global initialisers.
Joe Perches5129e872015-06-10 11:14:52 -07003243 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003244 if (ERROR("GLOBAL_INITIALISERS",
3245 "do not initialise globals to 0 or NULL\n" .
3246 $herecurr) &&
3247 $fix) {
Joe Perches5129e872015-06-10 11:14:52 -07003248 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003249 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003250 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003251# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003252 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3253 if (ERROR("INITIALISED_STATIC",
3254 "do not initialise statics to 0 or NULL\n" .
3255 $herecurr) &&
3256 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003257 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003258 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003259 }
3260
Joe Perches18130872014-08-06 16:11:22 -07003261# check for misordered declarations of char/short/int/long with signed/unsigned
3262 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3263 my $tmp = trim($1);
3264 WARN("MISORDERED_TYPE",
3265 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3266 }
3267
Joe Perchescb710ec2010-10-26 14:23:20 -07003268# check for static const char * arrays.
3269 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003270 WARN("STATIC_CONST_CHAR_ARRAY",
3271 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003272 $herecurr);
3273 }
3274
3275# check for static char foo[] = "bar" declarations.
3276 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003277 WARN("STATIC_CONST_CHAR_ARRAY",
3278 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003279 $herecurr);
3280 }
3281
Joe Perchesab7e23f2015-04-16 12:44:22 -07003282# check for const <foo> const where <foo> is not a pointer or array type
3283 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3284 my $found = $1;
3285 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3286 WARN("CONST_CONST",
3287 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3288 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3289 WARN("CONST_CONST",
3290 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3291 }
3292 }
3293
Joe Perches9b0fa602014-04-03 14:49:18 -07003294# check for non-global char *foo[] = {"bar", ...} declarations.
3295 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3296 WARN("STATIC_CONST_CHAR_ARRAY",
3297 "char * array declaration might be better as static const\n" .
3298 $herecurr);
3299 }
3300
Joe Perchesb598b672015-04-16 12:44:36 -07003301# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3302 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3303 my $array = $1;
3304 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3305 my $array_div = $1;
3306 if (WARN("ARRAY_SIZE",
3307 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3308 $fix) {
3309 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3310 }
3311 }
3312 }
3313
Joe Perchesb36190c2014-01-27 17:07:18 -08003314# check for function declarations without arguments like "int foo()"
3315 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3316 if (ERROR("FUNCTION_WITHOUT_ARGS",
3317 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3318 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003319 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003320 }
3321 }
3322
Joe Perches92e112f2013-12-13 11:36:22 -07003323# check for uses of DEFINE_PCI_DEVICE_TABLE
3324 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3325 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3326 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3327 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003328 $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 -07003329 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003330 }
3331
Andy Whitcroft653d4872007-06-23 17:16:34 -07003332# check for new typedefs, only function parameters and sparse annotations
3333# make sense.
3334 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003335 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003336 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003337 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003338 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003339 WARN("NEW_TYPEDEFS",
3340 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003341 }
3342
3343# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003344 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003345 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3346 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003347 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003348
Andy Whitcroft65863862009-01-06 14:41:21 -08003349 # Should start with a space.
3350 $to =~ s/^(\S)/ $1/;
3351 # Should not end with a space.
3352 $to =~ s/\s+$//;
3353 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003354 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003355 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003356
Joe Perches3705ce52013-07-03 15:05:31 -07003357## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003358 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003359 if (ERROR("POINTER_LOCATION",
3360 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3361 $fix) {
3362 my $sub_from = $ident;
3363 my $sub_to = $ident;
3364 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003365 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003366 s@\Q$sub_from\E@$sub_to@;
3367 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003368 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003369 }
3370 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3371 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003372 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003373
Andy Whitcroft65863862009-01-06 14:41:21 -08003374 # Should start with a space.
3375 $to =~ s/^(\S)/ $1/;
3376 # Should not end with a space.
3377 $to =~ s/\s+$//;
3378 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003379 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003380 }
3381 # Modifiers should have spaces.
3382 $to =~ s/(\b$Modifier$)/$1 /;
3383
Joe Perches3705ce52013-07-03 15:05:31 -07003384## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003385 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003386 if (ERROR("POINTER_LOCATION",
3387 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3388 $fix) {
3389
3390 my $sub_from = $match;
3391 my $sub_to = $match;
3392 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003393 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003394 s@\Q$sub_from\E@$sub_to@;
3395 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003396 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003397 }
3398
3399# # no BUG() or BUG_ON()
3400# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3401# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3402# print "$herecurr";
3403# $clean = 0;
3404# }
3405
Andy Whitcroft8905a672007-11-28 16:21:06 -08003406 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003407 WARN("LINUX_VERSION_CODE",
3408 "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 -08003409 }
3410
Joe Perches17441222011-06-15 15:08:17 -07003411# check for uses of printk_ratelimit
3412 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003413 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003414 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003415 }
3416
Andy Whitcroft00df3442007-06-08 13:47:06 -07003417# printk should use KERN_* levels. Note that follow on printk's on the
3418# same line do not need a level, so we use the current block context
3419# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003420# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003421# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003422 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003423 my $ok = 0;
3424 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3425 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003426 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003427 # with "\n" ignore it, else it is to blame
3428 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3429 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3430 $ok = 1;
3431 }
3432 last;
3433 }
3434 }
3435 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003436 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3437 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003438 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003439 }
3440
Joe Perches243f3802012-05-31 16:26:09 -07003441 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3442 my $orig = $1;
3443 my $level = lc($orig);
3444 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003445 my $level2 = $level;
3446 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003447 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003448 "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 -07003449 }
3450
3451 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003452 if (WARN("PREFER_PR_LEVEL",
3453 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3454 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003455 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003456 s/\bpr_warning\b/pr_warn/;
3457 }
Joe Perches243f3802012-05-31 16:26:09 -07003458 }
3459
Joe Perchesdc139312013-02-21 16:44:13 -08003460 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3461 my $orig = $1;
3462 my $level = lc($orig);
3463 $level = "warn" if ($level eq "warning");
3464 $level = "dbg" if ($level eq "debug");
3465 WARN("PREFER_DEV_LEVEL",
3466 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3467 }
3468
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003469# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3470# number of false positives, but assembly files are not checked, so at
3471# least the arch entry code will not trigger this warning.
3472 if ($line =~ /\bENOSYS\b/) {
3473 WARN("ENOSYS",
3474 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3475 }
3476
Andy Whitcroft653d4872007-06-23 17:16:34 -07003477# function brace can't be on same line, except for #defines of do while,
3478# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003479 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003480 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003481 if (ERROR("OPEN_BRACE",
3482 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3483 $fix) {
3484 fix_delete_line($fixlinenr, $rawline);
3485 my $fixed_line = $rawline;
3486 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3487 my $line1 = $1;
3488 my $line2 = $2;
3489 fix_insert_line($fixlinenr, ltrim($line1));
3490 fix_insert_line($fixlinenr, "\+{");
3491 if ($line2 !~ /^\s*$/) {
3492 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3493 }
3494 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003495 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003496
Andy Whitcroft8905a672007-11-28 16:21:06 -08003497# open braces for enum, union and struct go on the same line.
3498 if ($line =~ /^.\s*{/ &&
3499 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003500 if (ERROR("OPEN_BRACE",
3501 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3502 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3503 fix_delete_line($fixlinenr - 1, $prevrawline);
3504 fix_delete_line($fixlinenr, $rawline);
3505 my $fixedline = rtrim($prevrawline) . " {";
3506 fix_insert_line($fixlinenr, $fixedline);
3507 $fixedline = $rawline;
3508 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3509 if ($fixedline !~ /^\+\s*$/) {
3510 fix_insert_line($fixlinenr, $fixedline);
3511 }
3512 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003513 }
3514
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003515# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003516 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3517 if (WARN("SPACING",
3518 "missing space after $1 definition\n" . $herecurr) &&
3519 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003520 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003521 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3522 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003523 }
3524
Joe Perches31070b52014-01-23 15:54:49 -08003525# Function pointer declarations
3526# check spacing between type, funcptr, and args
3527# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003528 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003529 my $declare = $1;
3530 my $pre_pointer_space = $2;
3531 my $post_pointer_space = $3;
3532 my $funcname = $4;
3533 my $post_funcname_space = $5;
3534 my $pre_args_space = $6;
3535
Joe Perches91f72e92014-04-03 14:49:12 -07003536# the $Declare variable will capture all spaces after the type
3537# so check it for a missing trailing missing space but pointer return types
3538# don't need a space so don't warn for those.
3539 my $post_declare_space = "";
3540 if ($declare =~ /(\s+)$/) {
3541 $post_declare_space = $1;
3542 $declare = rtrim($declare);
3543 }
3544 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003545 WARN("SPACING",
3546 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003547 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003548 }
3549
3550# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003551# This test is not currently implemented because these declarations are
3552# equivalent to
3553# int foo(int bar, ...)
3554# and this is form shouldn't/doesn't generate a checkpatch warning.
3555#
3556# elsif ($declare =~ /\s{2,}$/) {
3557# WARN("SPACING",
3558# "Multiple spaces after return type\n" . $herecurr);
3559# }
Joe Perches31070b52014-01-23 15:54:49 -08003560
3561# unnecessary space "type ( *funcptr)(args...)"
3562 if (defined $pre_pointer_space &&
3563 $pre_pointer_space =~ /^\s/) {
3564 WARN("SPACING",
3565 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3566 }
3567
3568# unnecessary space "type (* funcptr)(args...)"
3569 if (defined $post_pointer_space &&
3570 $post_pointer_space =~ /^\s/) {
3571 WARN("SPACING",
3572 "Unnecessary space before function pointer name\n" . $herecurr);
3573 }
3574
3575# unnecessary space "type (*funcptr )(args...)"
3576 if (defined $post_funcname_space &&
3577 $post_funcname_space =~ /^\s/) {
3578 WARN("SPACING",
3579 "Unnecessary space after function pointer name\n" . $herecurr);
3580 }
3581
3582# unnecessary space "type (*funcptr) (args...)"
3583 if (defined $pre_args_space &&
3584 $pre_args_space =~ /^\s/) {
3585 WARN("SPACING",
3586 "Unnecessary space before function pointer arguments\n" . $herecurr);
3587 }
3588
3589 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003590 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003591 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003592 }
3593 }
3594
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003595# check for spacing round square brackets; allowed:
3596# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003597# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3598# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003599 while ($line =~ /(.*?\s)\[/g) {
3600 my ($where, $prefix) = ($-[1], $1);
3601 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003602 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003603 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003604 if (ERROR("BRACKET_SPACE",
3605 "space prohibited before open square bracket '['\n" . $herecurr) &&
3606 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003607 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003608 s/^(\+.*?)\s+\[/$1\[/;
3609 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003610 }
3611 }
3612
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003613# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003614 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003615 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003616 my $ctx_before = substr($line, 0, $-[1]);
3617 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003618
3619 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003620 if ($name =~ /^(?:
3621 if|for|while|switch|return|case|
3622 volatile|__volatile__|
3623 __attribute__|format|__extension__|
3624 asm|__asm__)$/x)
3625 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003626 # cpp #define statements have non-optional spaces, ie
3627 # if there is a space between the name and the open
3628 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003629 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003630
3631 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003632 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003633
3634 # If this whole things ends with a type its most
3635 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003636 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003637
3638 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003639 if (WARN("SPACING",
3640 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3641 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003642 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003643 s/\b$name\s+\(/$name\(/;
3644 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003645 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003646 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003647
Andy Whitcroft653d4872007-06-23 17:16:34 -07003648# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003649 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003650 my $fixed_line = "";
3651 my $line_fixed = 0;
3652
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003653 my $ops = qr{
3654 <<=|>>=|<=|>=|==|!=|
3655 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3656 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003657 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003658 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003659 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003660 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003661
3662## print("element count: <" . $#elements . ">\n");
3663## foreach my $el (@elements) {
3664## print("el: <$el>\n");
3665## }
3666
3667 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003668 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003669
Joe Perches3705ce52013-07-03 15:05:31 -07003670 foreach my $el (@elements) {
3671 push(@fix_elements, substr($rawline, $off, length($el)));
3672 $off += length($el);
3673 }
3674
3675 $off = 0;
3676
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003677 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003678 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003679
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003680 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003681
3682 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3683
3684## print("n: <$n> good: <$good>\n");
3685
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003686 $off += length($elements[$n]);
3687
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003688 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003689 my $ca = substr($opline, 0, $off);
3690 my $cc = '';
3691 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3692 $cc = substr($opline, $off + length($elements[$n + 1]));
3693 }
3694 my $cb = "$ca$;$cc";
3695
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003696 my $a = '';
3697 $a = 'V' if ($elements[$n] ne '');
3698 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003699 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003700 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3701 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003702 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003703
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003704 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003705
3706 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003707 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003708 $c = 'V' if ($elements[$n + 2] ne '');
3709 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003710 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003711 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3712 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003713 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003714 } else {
3715 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003716 }
3717
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003718 my $ctx = "${a}x${c}";
3719
3720 my $at = "(ctx:$ctx)";
3721
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003722 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003723 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003724
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003725 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003726 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003727
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003728 # Get the full operator variant.
3729 my $opv = $op . substr($curr_vars, $off, 1);
3730
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003731 # Ignore operators passed as parameters.
3732 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07003733 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003734
Andy Whitcroftcf655042008-03-04 14:28:20 -08003735# # Ignore comments
3736# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003737
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003738 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003739 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003740 if ($ctx !~ /.x[WEBC]/ &&
3741 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003742 if (ERROR("SPACING",
3743 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003744 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003745 $line_fixed = 1;
3746 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003747 }
3748
3749 # // is a comment
3750 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003751
Joe Perchesb00e4812014-04-03 14:49:33 -07003752 # : when part of a bitfield
3753 } elsif ($opv eq ':B') {
3754 # skip the bitfield test for now
3755
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003756 # No spaces for:
3757 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003758 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003759 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003760 if (ERROR("SPACING",
3761 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003762 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003763 if (defined $fix_elements[$n + 2]) {
3764 $fix_elements[$n + 2] =~ s/^\s+//;
3765 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003766 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003767 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003768 }
3769
Joe Perches23810972014-12-10 15:51:32 -08003770 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003771 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003772 my $rtrim_before = 0;
3773 my $space_after = 0;
3774 if ($ctx =~ /Wx./) {
3775 if (ERROR("SPACING",
3776 "space prohibited before that '$op' $at\n" . $hereptr)) {
3777 $line_fixed = 1;
3778 $rtrim_before = 1;
3779 }
3780 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003781 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003782 if (ERROR("SPACING",
3783 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003784 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003785 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003786 $space_after = 1;
3787 }
3788 }
3789 if ($rtrim_before || $space_after) {
3790 if ($rtrim_before) {
3791 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3792 } else {
3793 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3794 }
3795 if ($space_after) {
3796 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003797 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003798 }
3799
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003800 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003801 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003802 #warn "'*' is part of type\n";
3803
3804 # unary operators should have a space before and
3805 # none after. May be left adjacent to another
3806 # unary operator, or a cast
3807 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003808 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003809 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003810 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003811 if (ERROR("SPACING",
3812 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003813 if ($n != $last_after + 2) {
3814 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3815 $line_fixed = 1;
3816 }
Joe Perches3705ce52013-07-03 15:05:31 -07003817 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003818 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003819 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003820 # A unary '*' may be const
3821
3822 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003823 if (ERROR("SPACING",
3824 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003825 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003826 if (defined $fix_elements[$n + 2]) {
3827 $fix_elements[$n + 2] =~ s/^\s+//;
3828 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003829 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003830 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003831 }
3832
3833 # unary ++ and unary -- are allowed no space on one side.
3834 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003835 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003836 if (ERROR("SPACING",
3837 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003838 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003839 $line_fixed = 1;
3840 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003841 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003842 if ($ctx =~ /Wx[BE]/ ||
3843 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003844 if (ERROR("SPACING",
3845 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003846 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003847 $line_fixed = 1;
3848 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003849 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003850 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003851 if (ERROR("SPACING",
3852 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003853 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003854 if (defined $fix_elements[$n + 2]) {
3855 $fix_elements[$n + 2] =~ s/^\s+//;
3856 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003857 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003858 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003859 }
3860
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003861 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003862 } elsif ($op eq '<<' or $op eq '>>' or
3863 $op eq '&' or $op eq '^' or $op eq '|' or
3864 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003865 $op eq '*' or $op eq '/' or
3866 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003867 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08003868 if ($check) {
3869 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3870 if (CHK("SPACING",
3871 "spaces preferred around that '$op' $at\n" . $hereptr)) {
3872 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3873 $fix_elements[$n + 2] =~ s/^\s+//;
3874 $line_fixed = 1;
3875 }
3876 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3877 if (CHK("SPACING",
3878 "space preferred before that '$op' $at\n" . $hereptr)) {
3879 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3880 $line_fixed = 1;
3881 }
3882 }
3883 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003884 if (ERROR("SPACING",
3885 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003886 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3887 if (defined $fix_elements[$n + 2]) {
3888 $fix_elements[$n + 2] =~ s/^\s+//;
3889 }
Joe Perches3705ce52013-07-03 15:05:31 -07003890 $line_fixed = 1;
3891 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003892 }
3893
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003894 # A colon needs no spaces before when it is
3895 # terminating a case value or a label.
3896 } elsif ($opv eq ':C' || $opv eq ':L') {
3897 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003898 if (ERROR("SPACING",
3899 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003900 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003901 $line_fixed = 1;
3902 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003903 }
3904
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003905 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003906 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003907 my $ok = 0;
3908
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003909 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003910 if (($op eq '<' &&
3911 $cc =~ /^\S+\@\S+>/) ||
3912 ($op eq '>' &&
3913 $ca =~ /<\S+\@\S+$/))
3914 {
3915 $ok = 1;
3916 }
3917
Joe Perchese0df7e12015-04-16 12:44:53 -07003918 # for asm volatile statements
3919 # ignore a colon with another
3920 # colon immediately before or after
3921 if (($op eq ':') &&
3922 ($ca =~ /:$/ || $cc =~ /^:/)) {
3923 $ok = 1;
3924 }
3925
Joe Perches84731622013-11-12 15:10:05 -08003926 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003927 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003928 my $msg_type = \&ERROR;
3929 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3930
3931 if (&{$msg_type}("SPACING",
3932 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003933 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3934 if (defined $fix_elements[$n + 2]) {
3935 $fix_elements[$n + 2] =~ s/^\s+//;
3936 }
Joe Perches3705ce52013-07-03 15:05:31 -07003937 $line_fixed = 1;
3938 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003939 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003940 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003941 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003942
3943## print("n: <$n> GOOD: <$good>\n");
3944
3945 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003946 }
Joe Perches3705ce52013-07-03 15:05:31 -07003947
3948 if (($#elements % 2) == 0) {
3949 $fixed_line = $fixed_line . $fix_elements[$#elements];
3950 }
3951
Joe Perches194f66f2014-08-06 16:11:03 -07003952 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3953 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003954 }
3955
3956
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003957 }
3958
Joe Perches786b6322013-07-03 15:05:32 -07003959# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003960 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003961 if (WARN("SPACING",
3962 "space prohibited before semicolon\n" . $herecurr) &&
3963 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003964 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003965 s/^(\+.*\S)\s+;/$1;/;
3966 }
3967 }
3968
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003969# check for multiple assignments
3970 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003971 CHK("MULTIPLE_ASSIGNMENTS",
3972 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003973 }
3974
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003975## # check for multiple declarations, allowing for a function declaration
3976## # continuation.
3977## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3978## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3979##
3980## # Remove any bracketed sections to ensure we do not
3981## # falsly report the parameters of functions.
3982## my $ln = $line;
3983## while ($ln =~ s/\([^\(\)]*\)//g) {
3984## }
3985## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003986## WARN("MULTIPLE_DECLARATION",
3987## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003988## }
3989## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003990
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003991#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003992 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3993 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003994 if (ERROR("SPACING",
3995 "space required before the open brace '{'\n" . $herecurr) &&
3996 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003997 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003998 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003999 }
4000
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004001## # check for blank lines before declarations
4002## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4003## $prevrawline =~ /^.\s*$/) {
4004## WARN("SPACING",
4005## "No blank lines before declarations\n" . $hereprev);
4006## }
4007##
4008
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004009# closing brace should have a space following it when it has anything
4010# on the line
4011 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004012 if (ERROR("SPACING",
4013 "space required after that close brace '}'\n" . $herecurr) &&
4014 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004015 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004016 s/}((?!(?:,|;|\)))\S)/} $1/;
4017 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004018 }
4019
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004020# check spacing on square brackets
4021 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004022 if (ERROR("SPACING",
4023 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4024 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004025 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004026 s/\[\s+/\[/;
4027 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004028 }
4029 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004030 if (ERROR("SPACING",
4031 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4032 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004033 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004034 s/\s+\]/\]/;
4035 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004036 }
4037
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004038# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004039 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4040 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004041 if (ERROR("SPACING",
4042 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4043 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004044 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004045 s/\(\s+/\(/;
4046 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004047 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004048 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004049 $line !~ /for\s*\(.*;\s+\)/ &&
4050 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004051 if (ERROR("SPACING",
4052 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4053 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004054 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004055 s/\s+\)/\)/;
4056 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004057 }
4058
Joe Perchese2826fd2014-08-06 16:10:48 -07004059# check unnecessary parentheses around addressof/dereference single $Lvals
4060# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4061
4062 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004063 my $var = $1;
4064 if (CHK("UNNECESSARY_PARENTHESES",
4065 "Unnecessary parentheses around $var\n" . $herecurr) &&
4066 $fix) {
4067 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4068 }
4069 }
4070
4071# check for unnecessary parentheses around function pointer uses
4072# ie: (foo->bar)(); should be foo->bar();
4073# but not "if (foo->bar) (" to avoid some false positives
4074 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4075 my $var = $2;
4076 if (CHK("UNNECESSARY_PARENTHESES",
4077 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4078 $fix) {
4079 my $var2 = deparenthesize($var);
4080 $var2 =~ s/\s//g;
4081 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4082 }
4083 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004084
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004085#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004086 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004087 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004088 if (WARN("INDENTED_LABEL",
4089 "labels should not be indented\n" . $herecurr) &&
4090 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004091 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004092 s/^(.)\s+/$1/;
4093 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004094 }
4095
Joe Perches5b9553a2014-04-03 14:49:21 -07004096# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004097 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004098 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004099 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004100 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4101 my $value = $1;
4102 $value = deparenthesize($value);
4103 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4104 ERROR("RETURN_PARENTHESES",
4105 "return is not a function, parentheses are not required\n" . $herecurr);
4106 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004107 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004108 ERROR("SPACING",
4109 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004110 }
4111 }
Joe Perches507e5142013-11-12 15:10:13 -08004112
Joe Perchesb43ae212014-06-23 13:22:07 -07004113# unnecessary return in a void function
4114# at end-of-function, with the previous line a single leading tab, then return;
4115# and the line before that not a goto label target like "out:"
4116 if ($sline =~ /^[ \+]}\s*$/ &&
4117 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4118 $linenr >= 3 &&
4119 $lines[$linenr - 3] =~ /^[ +]/ &&
4120 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004121 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004122 "void function return statements are not generally useful\n" . $hereprev);
4123 }
Joe Perches9819cf22014-06-04 16:12:09 -07004124
Joe Perches189248d2014-01-23 15:54:47 -08004125# if statements using unnecessary parentheses - ie: if ((foo == bar))
4126 if ($^V && $^V ge 5.10.0 &&
4127 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4128 my $openparens = $1;
4129 my $count = $openparens =~ tr@\(@\(@;
4130 my $msg = "";
4131 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4132 my $comp = $4; #Not $1 because of $LvalOrFunc
4133 $msg = " - maybe == should be = ?" if ($comp eq "==");
4134 WARN("UNNECESSARY_PARENTHESES",
4135 "Unnecessary parentheses$msg\n" . $herecurr);
4136 }
4137 }
4138
Joe Perchesf34e4a42015-04-16 12:44:19 -07004139# Return of what appears to be an errno should normally be negative
4140 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004141 my $name = $1;
4142 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004143 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004144 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004145 }
4146 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004147
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004148# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004149 if ($line =~ /\b(if|while|for|switch)\(/) {
4150 if (ERROR("SPACING",
4151 "space required before the open parenthesis '('\n" . $herecurr) &&
4152 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004153 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004154 s/\b(if|while|for|switch)\(/$1 \(/;
4155 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004156 }
4157
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004158# Check for illegal assignment in if conditional -- and check for trailing
4159# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004160 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004161 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4162 ctx_statement_block($linenr, $realcnt, 0)
4163 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004164 my ($stat_next) = ctx_statement_block($line_nr_next,
4165 $remain_next, $off_next);
4166 $stat_next =~ s/\n./\n /g;
4167 ##print "stat<$stat> stat_next<$stat_next>\n";
4168
4169 if ($stat_next =~ /^\s*while\b/) {
4170 # If the statement carries leading newlines,
4171 # then count those as offsets.
4172 my ($whitespace) =
4173 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4174 my $offset =
4175 statement_rawlines($whitespace) - 1;
4176
4177 $suppress_whiletrailers{$line_nr_next +
4178 $offset} = 1;
4179 }
4180 }
4181 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004182 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004183 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004184 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004185
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004186 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004187 ERROR("ASSIGN_IN_IF",
4188 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004189 }
4190
4191 # Find out what is on the end of the line after the
4192 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004193 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004194 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004195 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004196 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4197 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004198 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004199 # Find out how long the conditional actually is.
4200 my @newlines = ($c =~ /\n/gs);
4201 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004202 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004203
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004204 $stat_real = raw_line($linenr, $cond_lines)
4205 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004206 if (defined($stat_real) && $cond_lines > 1) {
4207 $stat_real = "[...]\n$stat_real";
4208 }
4209
Joe Perches000d1cc12011-07-25 17:13:25 -07004210 ERROR("TRAILING_STATEMENTS",
4211 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004212 }
4213 }
4214
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004215# Check for bitwise tests written as boolean
4216 if ($line =~ /
4217 (?:
4218 (?:\[|\(|\&\&|\|\|)
4219 \s*0[xX][0-9]+\s*
4220 (?:\&\&|\|\|)
4221 |
4222 (?:\&\&|\|\|)
4223 \s*0[xX][0-9]+\s*
4224 (?:\&\&|\|\||\)|\])
4225 )/x)
4226 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004227 WARN("HEXADECIMAL_BOOLEAN_TEST",
4228 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004229 }
4230
Andy Whitcroft8905a672007-11-28 16:21:06 -08004231# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004232 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4233 my $s = $1;
4234 $s =~ s/$;//g; # Remove any comments
4235 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004236 ERROR("TRAILING_STATEMENTS",
4237 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004238 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004239 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004240# if should not continue a brace
4241 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004242 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004243 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004244 $herecurr);
4245 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004246# case and default should not have general statements after them
4247 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4248 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004249 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004250 \s*return\s+
4251 )/xg)
4252 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004253 ERROR("TRAILING_STATEMENTS",
4254 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004255 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004256
4257 # Check for }<nl>else {, these must be at the same
4258 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004259 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4260 $previndent == $indent) {
4261 if (ERROR("ELSE_AFTER_BRACE",
4262 "else should follow close brace '}'\n" . $hereprev) &&
4263 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4264 fix_delete_line($fixlinenr - 1, $prevrawline);
4265 fix_delete_line($fixlinenr, $rawline);
4266 my $fixedline = $prevrawline;
4267 $fixedline =~ s/}\s*$//;
4268 if ($fixedline !~ /^\+\s*$/) {
4269 fix_insert_line($fixlinenr, $fixedline);
4270 }
4271 $fixedline = $rawline;
4272 $fixedline =~ s/^(.\s*)else/$1} else/;
4273 fix_insert_line($fixlinenr, $fixedline);
4274 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004275 }
4276
Joe Perches8b8856f2014-08-06 16:11:14 -07004277 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4278 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004279 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4280
4281 # Find out what is on the end of the line after the
4282 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004283 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004284 $s =~ s/\n.*//g;
4285
4286 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004287 if (ERROR("WHILE_AFTER_BRACE",
4288 "while should follow close brace '}'\n" . $hereprev) &&
4289 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4290 fix_delete_line($fixlinenr - 1, $prevrawline);
4291 fix_delete_line($fixlinenr, $rawline);
4292 my $fixedline = $prevrawline;
4293 my $trailing = $rawline;
4294 $trailing =~ s/^\+//;
4295 $trailing = trim($trailing);
4296 $fixedline =~ s/}\s*$/} $trailing/;
4297 fix_insert_line($fixlinenr, $fixedline);
4298 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004299 }
4300 }
4301
Joe Perches95e2c602013-07-03 15:05:20 -07004302#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004303 while ($line =~ m{($Constant|$Lval)}g) {
4304 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004305
4306#gcc binary extension
4307 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004308 if (WARN("GCC_BINARY_CONSTANT",
4309 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4310 $fix) {
4311 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004312 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004313 s/\b$var\b/$hexval/;
4314 }
Joe Perches95e2c602013-07-03 15:05:20 -07004315 }
4316
4317#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004318 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004319 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004320#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004321 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004322#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004323 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4324#Ignore some three character SI units explicitly, like MiB and KHz
4325 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004326 while ($var =~ m{($Ident)}g) {
4327 my $word = $1;
4328 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004329 if ($check) {
4330 seed_camelcase_includes();
4331 if (!$file && !$camelcase_file_seeded) {
4332 seed_camelcase_file($realfile);
4333 $camelcase_file_seeded = 1;
4334 }
4335 }
Joe Perches7e781f62013-09-11 14:23:55 -07004336 if (!defined $camelcase{$word}) {
4337 $camelcase{$word} = 1;
4338 CHK("CAMELCASE",
4339 "Avoid CamelCase: <$word>\n" . $herecurr);
4340 }
Joe Perches34456862013-07-03 15:05:34 -07004341 }
Joe Perches323c1262012-12-17 16:02:07 -08004342 }
4343 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004344
4345#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004346 if ($line =~ /\#\s*define.*\\\s+$/) {
4347 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4348 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4349 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004350 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004351 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004352 }
4353
Fabian Frederick0e212e02015-04-16 12:44:25 -07004354# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4355# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004356 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004357 my $file = "$1.h";
4358 my $checkfile = "include/linux/$file";
4359 if (-f "$root/$checkfile" &&
4360 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004361 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004362 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004363 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4364 if ($asminclude > 0) {
4365 if ($realfile =~ m{^arch/}) {
4366 CHK("ARCH_INCLUDE_LINUX",
4367 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4368 } else {
4369 WARN("INCLUDE_LINUX",
4370 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4371 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004372 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004373 }
4374 }
4375
Andy Whitcroft653d4872007-06-23 17:16:34 -07004376# multi-statement macros should be enclosed in a do while loop, grab the
4377# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004378# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004379 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4380 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004381 my $ln = $linenr;
4382 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004383 my ($off, $dstat, $dcond, $rest);
4384 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004385 my $has_flow_statement = 0;
4386 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004387 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004388 ctx_statement_block($linenr, $realcnt, 0);
4389 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004390 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004391 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004392
Joe Perches08a28432014-10-13 15:51:55 -07004393 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4394 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4395
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004396 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004397 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004398 $dstat =~ s/\\\n.//g;
4399 $dstat =~ s/^\s*//s;
4400 $dstat =~ s/\s*$//s;
4401
4402 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004403 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4404 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004405 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004406 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004407 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004408
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004409 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004410 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4411 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004412 {
4413 }
4414
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004415 my $exceptions = qr{
4416 $Declare|
4417 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004418 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004419 DECLARE_PER_CPU|
4420 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004421 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004422 union|
4423 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004424 \.$Ident\s*=\s*|
4425 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004426 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004427 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004428 if ($dstat ne '' &&
4429 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4430 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004431 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004432 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004433 $dstat !~ /$exceptions/ &&
4434 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004435 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004436 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004437 $dstat !~ /^for\s*$Constant$/ && # for (...)
4438 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4439 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004440 $dstat !~ /^\({/ && # ({...
4441 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004442 {
4443 $ctx =~ s/\n*$//;
4444 my $herectx = $here . "\n";
4445 my $cnt = statement_rawlines($ctx);
4446
4447 for (my $n = 0; $n < $cnt; $n++) {
4448 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004449 }
4450
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004451 if ($dstat =~ /;/) {
4452 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4453 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4454 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004455 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004456 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004457 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004458 }
Joe Perches5023d342012-12-17 16:01:47 -08004459
Joe Perches08a28432014-10-13 15:51:55 -07004460# check for macros with flow control, but without ## concatenation
4461# ## concatenation is commonly a macro that defines a function so ignore those
4462 if ($has_flow_statement && !$has_arg_concat) {
4463 my $herectx = $here . "\n";
4464 my $cnt = statement_rawlines($ctx);
4465
4466 for (my $n = 0; $n < $cnt; $n++) {
4467 $herectx .= raw_line($linenr, $n) . "\n";
4468 }
4469 WARN("MACRO_WITH_FLOW_CONTROL",
4470 "Macros with flow control statements should be avoided\n" . "$herectx");
4471 }
4472
Joe Perches481eb482012-12-17 16:01:56 -08004473# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004474
4475 } else {
4476 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004477 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4478 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004479 $line =~ /^\+.*\\$/) {
4480 WARN("LINE_CONTINUATIONS",
4481 "Avoid unnecessary line continuations\n" . $herecurr);
4482 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004483 }
4484
Joe Perchesb13edf72012-07-30 14:41:24 -07004485# do {} while (0) macro tests:
4486# single-statement macros do not need to be enclosed in do while (0) loop,
4487# macro should not end with a semicolon
4488 if ($^V && $^V ge 5.10.0 &&
4489 $realfile !~ m@/vmlinux.lds.h$@ &&
4490 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4491 my $ln = $linenr;
4492 my $cnt = $realcnt;
4493 my ($off, $dstat, $dcond, $rest);
4494 my $ctx = '';
4495 ($dstat, $dcond, $ln, $cnt, $off) =
4496 ctx_statement_block($linenr, $realcnt, 0);
4497 $ctx = $dstat;
4498
4499 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004500 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004501
4502 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4503 my $stmts = $2;
4504 my $semis = $3;
4505
4506 $ctx =~ s/\n*$//;
4507 my $cnt = statement_rawlines($ctx);
4508 my $herectx = $here . "\n";
4509
4510 for (my $n = 0; $n < $cnt; $n++) {
4511 $herectx .= raw_line($linenr, $n) . "\n";
4512 }
4513
Joe Perchesac8e97f2012-08-21 16:15:53 -07004514 if (($stmts =~ tr/;/;/) == 1 &&
4515 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004516 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4517 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4518 }
4519 if (defined $semis && $semis ne "") {
4520 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4521 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4522 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004523 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4524 $ctx =~ s/\n*$//;
4525 my $cnt = statement_rawlines($ctx);
4526 my $herectx = $here . "\n";
4527
4528 for (my $n = 0; $n < $cnt; $n++) {
4529 $herectx .= raw_line($linenr, $n) . "\n";
4530 }
4531
4532 WARN("TRAILING_SEMICOLON",
4533 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004534 }
4535 }
4536
Mike Frysinger080ba922009-01-06 14:41:25 -08004537# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4538# all assignments may have only one of the following with an assignment:
4539# .
4540# ALIGN(...)
4541# VMLINUX_SYMBOL(...)
4542 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004543 WARN("MISSING_VMLINUX_SYMBOL",
4544 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004545 }
4546
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004547# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004548 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4549 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004550 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004551 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004552 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4553 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004554 my @allowed = ();
4555 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004556 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004557 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004558 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004559 for my $chunk (@chunks) {
4560 my ($cond, $block) = @{$chunk};
4561
Andy Whitcroft773647a2008-03-28 14:15:58 -07004562 # If the condition carries leading newlines, then count those as offsets.
4563 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4564 my $offset = statement_rawlines($whitespace) - 1;
4565
Joe Perchesaad4f612012-03-23 15:02:19 -07004566 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004567 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4568
4569 # We have looked at and allowed this specific line.
4570 $suppress_ifbraces{$ln + $offset} = 1;
4571
4572 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004573 $ln += statement_rawlines($block) - 1;
4574
Andy Whitcroft773647a2008-03-28 14:15:58 -07004575 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004576
4577 $seen++ if ($block =~ /^\s*{/);
4578
Joe Perchesaad4f612012-03-23 15:02:19 -07004579 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004580 if (statement_lines($cond) > 1) {
4581 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004582 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004583 }
4584 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004585 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004586 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004587 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004588 if (statement_block_size($block) > 1) {
4589 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004590 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004591 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004592 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004593 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004594 if ($seen) {
4595 my $sum_allowed = 0;
4596 foreach (@allowed) {
4597 $sum_allowed += $_;
4598 }
4599 if ($sum_allowed == 0) {
4600 WARN("BRACES",
4601 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4602 } elsif ($sum_allowed != $allow &&
4603 $seen != $allow) {
4604 CHK("BRACES",
4605 "braces {} should be used on all arms of this statement\n" . $herectx);
4606 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004607 }
4608 }
4609 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004610 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004611 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004612 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004613
Andy Whitcroftcf655042008-03-04 14:28:20 -08004614 # Check the pre-context.
4615 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4616 #print "APW: ALLOWED: pre<$1>\n";
4617 $allowed = 1;
4618 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004619
4620 my ($level, $endln, @chunks) =
4621 ctx_statement_full($linenr, $realcnt, $-[0]);
4622
Andy Whitcroftcf655042008-03-04 14:28:20 -08004623 # Check the condition.
4624 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004625 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004626 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004627 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004628 }
4629 if (statement_lines($cond) > 1) {
4630 #print "APW: ALLOWED: cond<$cond>\n";
4631 $allowed = 1;
4632 }
4633 if ($block =~/\b(?:if|for|while)\b/) {
4634 #print "APW: ALLOWED: block<$block>\n";
4635 $allowed = 1;
4636 }
4637 if (statement_block_size($block) > 1) {
4638 #print "APW: ALLOWED: lines block<$block>\n";
4639 $allowed = 1;
4640 }
4641 # Check the post-context.
4642 if (defined $chunks[1]) {
4643 my ($cond, $block) = @{$chunks[1]};
4644 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004645 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004646 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004647 if ($block =~ /^\s*\{/) {
4648 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4649 $allowed = 1;
4650 }
4651 }
4652 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004653 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004654 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004655
Andy Whitcroftf0556632008-10-15 22:02:23 -07004656 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004657 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004658 }
4659
Joe Perches000d1cc12011-07-25 17:13:25 -07004660 WARN("BRACES",
4661 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004662 }
4663 }
4664
Joe Perches0979ae62012-12-17 16:01:59 -08004665# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004666 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004667 if (CHK("BRACES",
4668 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4669 $fix && $prevrawline =~ /^\+/) {
4670 fix_delete_line($fixlinenr - 1, $prevrawline);
4671 }
Joe Perches0979ae62012-12-17 16:01:59 -08004672 }
Joe Perches77b9a532013-07-03 15:05:29 -07004673 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004674 if (CHK("BRACES",
4675 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4676 $fix) {
4677 fix_delete_line($fixlinenr, $rawline);
4678 }
Joe Perches0979ae62012-12-17 16:01:59 -08004679 }
4680
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004681# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004682 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4683 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004684 WARN("VOLATILE",
4685 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004686 }
4687
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004688# Check for user-visible strings broken across lines, which breaks the ability
4689# to grep for the string. Make exceptions when the previous string ends in a
4690# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4691# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07004692 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004693 $prevline =~ /"\s*$/ &&
4694 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4695 if (WARN("SPLIT_STRING",
4696 "quoted string split across lines\n" . $hereprev) &&
4697 $fix &&
4698 $prevrawline =~ /^\+.*"\s*$/ &&
4699 $last_coalesced_string_linenr != $linenr - 1) {
4700 my $extracted_string = get_quoted_string($line, $rawline);
4701 my $comma_close = "";
4702 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4703 $comma_close = $1;
4704 }
4705
4706 fix_delete_line($fixlinenr - 1, $prevrawline);
4707 fix_delete_line($fixlinenr, $rawline);
4708 my $fixedline = $prevrawline;
4709 $fixedline =~ s/"\s*$//;
4710 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4711 fix_insert_line($fixlinenr - 1, $fixedline);
4712 $fixedline = $rawline;
4713 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4714 if ($fixedline !~ /\+\s*$/) {
4715 fix_insert_line($fixlinenr, $fixedline);
4716 }
4717 $last_coalesced_string_linenr = $linenr;
4718 }
4719 }
4720
4721# check for missing a space in a string concatenation
4722 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4723 WARN('MISSING_SPACE',
4724 "break quoted strings at a space character\n" . $hereprev);
4725 }
4726
4727# check for spaces before a quoted newline
4728 if ($rawline =~ /^.*\".*\s\\n/) {
4729 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4730 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4731 $fix) {
4732 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4733 }
4734
4735 }
4736
Joe Perchesf17dba42014-10-13 15:51:51 -07004737# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07004738 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07004739 CHK("CONCATENATED_STRING",
4740 "Concatenated strings should use spaces between elements\n" . $herecurr);
4741 }
4742
Joe Perches90ad30e2014-12-10 15:51:59 -08004743# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07004744 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08004745 WARN("STRING_FRAGMENTS",
4746 "Consecutive strings are generally better as a single string\n" . $herecurr);
4747 }
4748
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004749# check for %L{u,d,i} in strings
4750 my $string;
4751 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4752 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4753 $string =~ s/%%/__/g;
4754 if ($string =~ /(?<!%)%L[udi]/) {
4755 WARN("PRINTF_L",
4756 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4757 last;
4758 }
4759 }
4760
4761# check for line continuations in quoted strings with odd counts of "
4762 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4763 WARN("LINE_CONTINUATIONS",
4764 "Avoid line continuations in quoted strings\n" . $herecurr);
4765 }
4766
Andy Whitcroft00df3442007-06-08 13:47:06 -07004767# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004768 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004769 CHK("REDUNDANT_CODE",
4770 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004771 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004772 }
4773
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004774# check for needless "if (<foo>) fn(<foo>)" uses
4775 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4776 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4777 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4778 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004779 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004780 }
4781 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004782
Joe Perchesebfdc402014-08-06 16:10:27 -07004783# check for unnecessary "Out of Memory" messages
4784 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4785 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4786 (defined $1 || defined $3) &&
4787 $linenr > 3) {
4788 my $testval = $2;
4789 my $testline = $lines[$linenr - 3];
4790
4791 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4792# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4793
4794 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4795 WARN("OOM_MESSAGE",
4796 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4797 }
4798 }
4799
Joe Perchesf78d98f2014-10-13 15:52:01 -07004800# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004801 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004802 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4803 my $level = $1;
4804 if (WARN("UNNECESSARY_KERN_LEVEL",
4805 "Possible unnecessary $level\n" . $herecurr) &&
4806 $fix) {
4807 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4808 }
4809 }
4810
Joe Perchesabb08a52014-12-10 15:51:46 -08004811# check for mask then right shift without a parentheses
4812 if ($^V && $^V ge 5.10.0 &&
4813 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4814 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4815 WARN("MASK_THEN_SHIFT",
4816 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4817 }
4818
Joe Perchesb75ac612014-12-10 15:52:02 -08004819# check for pointer comparisons to NULL
4820 if ($^V && $^V ge 5.10.0) {
4821 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4822 my $val = $1;
4823 my $equal = "!";
4824 $equal = "" if ($4 eq "!=");
4825 if (CHK("COMPARISON_TO_NULL",
4826 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4827 $fix) {
4828 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4829 }
4830 }
4831 }
4832
Joe Perches8716de32013-09-11 14:24:05 -07004833# check for bad placement of section $InitAttribute (e.g.: __initdata)
4834 if ($line =~ /(\b$InitAttribute\b)/) {
4835 my $attr = $1;
4836 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4837 my $ptr = $1;
4838 my $var = $2;
4839 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4840 ERROR("MISPLACED_INIT",
4841 "$attr should be placed after $var\n" . $herecurr)) ||
4842 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4843 WARN("MISPLACED_INIT",
4844 "$attr should be placed after $var\n" . $herecurr))) &&
4845 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004846 $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 -07004847 }
4848 }
4849 }
4850
Joe Perchese970b8842013-11-12 15:10:10 -08004851# check for $InitAttributeData (ie: __initdata) with const
4852 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4853 my $attr = $1;
4854 $attr =~ /($InitAttributePrefix)(.*)/;
4855 my $attr_prefix = $1;
4856 my $attr_type = $2;
4857 if (ERROR("INIT_ATTRIBUTE",
4858 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4859 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004860 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004861 s/$InitAttributeData/${attr_prefix}initconst/;
4862 }
4863 }
4864
4865# check for $InitAttributeConst (ie: __initconst) without const
4866 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4867 my $attr = $1;
4868 if (ERROR("INIT_ATTRIBUTE",
4869 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4870 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004871 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004872 /(^\+\s*(?:static\s+))/;
4873 $lead = rtrim($1);
4874 $lead = "$lead " if ($lead !~ /^\+$/);
4875 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004876 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004877 }
4878 }
4879
Joe Perchesc17893c2015-04-16 12:44:42 -07004880# check for __read_mostly with const non-pointer (should just be const)
4881 if ($line =~ /\b__read_mostly\b/ &&
4882 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
4883 if (ERROR("CONST_READ_MOSTLY",
4884 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
4885 $fix) {
4886 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
4887 }
4888 }
4889
Joe Perchesfbdb8132014-04-03 14:49:14 -07004890# don't use __constant_<foo> functions outside of include/uapi/
4891 if ($realfile !~ m@^include/uapi/@ &&
4892 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4893 my $constant_func = $1;
4894 my $func = $constant_func;
4895 $func =~ s/^__constant_//;
4896 if (WARN("CONSTANT_CONVERSION",
4897 "$constant_func should be $func\n" . $herecurr) &&
4898 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004899 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004900 }
4901 }
4902
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004903# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004904 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004905 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004906 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004907 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004908 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004909 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4910 }
4911 if ($delay > 2000) {
4912 WARN("LONG_UDELAY",
4913 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004914 }
4915 }
4916
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004917# warn about unexpectedly long msleep's
4918 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4919 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004920 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004921 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004922 }
4923 }
4924
Joe Perches36ec1932013-07-03 15:05:25 -07004925# check for comparisons of jiffies
4926 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4927 WARN("JIFFIES_COMPARISON",
4928 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4929 }
4930
Joe Perches9d7a34a2013-07-03 15:05:26 -07004931# check for comparisons of get_jiffies_64()
4932 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4933 WARN("JIFFIES_COMPARISON",
4934 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4935 }
4936
Andy Whitcroft00df3442007-06-08 13:47:06 -07004937# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004938# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004939# print "#ifdef in C files should be avoided\n";
4940# print "$herecurr";
4941# $clean = 0;
4942# }
4943
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004944# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004945 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004946 if (ERROR("SPACING",
4947 "exactly one space required after that #$1\n" . $herecurr) &&
4948 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004949 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004950 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4951 }
4952
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004953 }
4954
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004955# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004956 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4957 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004958 my $which = $1;
4959 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004960 CHK("UNCOMMENTED_DEFINITION",
4961 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004962 }
4963 }
4964# check for memory barriers without a comment.
4965 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4966 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004967 WARN("MEMORY_BARRIER",
4968 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004969 }
4970 }
Joe Perchescb426e92015-06-25 15:02:46 -07004971# check for waitqueue_active without a comment.
4972 if ($line =~ /\bwaitqueue_active\s*\(/) {
4973 if (!ctx_has_comment($first_line, $linenr)) {
4974 WARN("WAITQUEUE_ACTIVE",
4975 "waitqueue_active without comment\n" . $herecurr);
4976 }
4977 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004978# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004979 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004980 CHK("ARCH_DEFINES",
4981 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004982 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004983
Tobias Klauserd4977c72010-05-24 14:33:30 -07004984# Check that the storage class is at the beginning of a declaration
4985 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004986 WARN("STORAGE_CLASS",
4987 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004988 }
4989
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004990# check the location of the inline attribute, that it is between
4991# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004992 if ($line =~ /\b$Type\s+$Inline\b/ ||
4993 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004994 ERROR("INLINE_LOCATION",
4995 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004996 }
4997
Andy Whitcroft8905a672007-11-28 16:21:06 -08004998# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004999 if ($realfile !~ m@\binclude/uapi/@ &&
5000 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005001 if (WARN("INLINE",
5002 "plain inline is preferred over $1\n" . $herecurr) &&
5003 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005004 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005005
5006 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005007 }
5008
Joe Perches3d130fd2011-01-12 17:00:00 -08005009# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005010 if ($realfile !~ m@\binclude/uapi/@ &&
5011 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005012 WARN("PREFER_PACKED",
5013 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005014 }
5015
Joe Perches39b7e282011-07-25 17:13:24 -07005016# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005017 if ($realfile !~ m@\binclude/uapi/@ &&
5018 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005019 WARN("PREFER_ALIGNED",
5020 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005021 }
5022
Joe Perches5f14d3b2012-01-10 15:09:52 -08005023# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005024 if ($realfile !~ m@\binclude/uapi/@ &&
5025 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005026 if (WARN("PREFER_PRINTF",
5027 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5028 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005029 $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 -07005030
5031 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005032 }
5033
Joe Perches6061d942012-03-23 15:02:16 -07005034# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005035 if ($realfile !~ m@\binclude/uapi/@ &&
5036 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005037 if (WARN("PREFER_SCANF",
5038 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5039 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005040 $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 -07005041 }
Joe Perches6061d942012-03-23 15:02:16 -07005042 }
5043
Joe Perches619a9082014-12-10 15:51:35 -08005044# Check for __attribute__ weak, or __weak declarations (may have link issues)
5045 if ($^V && $^V ge 5.10.0 &&
5046 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5047 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5048 $line =~ /\b__weak\b/)) {
5049 ERROR("WEAK_DECLARATION",
5050 "Using weak declarations can have unintended link defects\n" . $herecurr);
5051 }
5052
Joe Perchese6176fa2015-06-25 15:02:49 -07005053# check for c99 types like uint8_t used outside of uapi/
5054 if ($realfile !~ m@\binclude/uapi/@ &&
5055 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5056 my $type = $1;
5057 if ($type =~ /\b($typeC99Typedefs)\b/) {
5058 $type = $1;
5059 my $kernel_type = 'u';
5060 $kernel_type = 's' if ($type =~ /^_*[si]/);
5061 $type =~ /(\d+)/;
5062 $kernel_type .= $1;
5063 if (CHK("PREFER_KERNEL_TYPES",
5064 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5065 $fix) {
5066 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5067 }
5068 }
5069 }
5070
Joe Perches8f53a9b2010-03-05 13:43:48 -08005071# check for sizeof(&)
5072 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005073 WARN("SIZEOF_ADDRESS",
5074 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005075 }
5076
Joe Perches66c80b62012-07-30 14:41:22 -07005077# check for sizeof without parenthesis
5078 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005079 if (WARN("SIZEOF_PARENTHESIS",
5080 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5081 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005082 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005083 }
Joe Perches66c80b62012-07-30 14:41:22 -07005084 }
5085
Joe Perches88982fe2012-12-17 16:02:00 -08005086# check for struct spinlock declarations
5087 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5088 WARN("USE_SPINLOCK_T",
5089 "struct spinlock should be spinlock_t\n" . $herecurr);
5090 }
5091
Joe Perchesa6962d72013-04-29 16:18:13 -07005092# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005093 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005094 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005095 $fmt =~ s/%%//g;
5096 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005097 if (WARN("PREFER_SEQ_PUTS",
5098 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5099 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005100 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005101 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005102 }
5103 }
5104
Andy Whitcroft554e1652012-01-10 15:09:57 -08005105# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005106 if ($^V && $^V ge 5.10.0 &&
5107 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005108 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005109
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005110 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005111 my $ms_val = $7;
5112 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005113
Andy Whitcroft554e1652012-01-10 15:09:57 -08005114 if ($ms_size =~ /^(0x|)0$/i) {
5115 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005116 "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 -08005117 } elsif ($ms_size =~ /^(0x|)1$/i) {
5118 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005119 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5120 }
5121 }
5122
Joe Perches98a9bba2014-01-23 15:54:52 -08005123# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5124 if ($^V && $^V ge 5.10.0 &&
5125 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
5126 if (WARN("PREFER_ETHER_ADDR_COPY",
5127 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
5128 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005129 $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 -08005130 }
5131 }
5132
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005133# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005134 if ($^V && $^V ge 5.10.0 &&
5135 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005136 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005137 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005138 my $call = $1;
5139 my $cast1 = deparenthesize($2);
5140 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005141 my $cast2 = deparenthesize($7);
5142 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005143 my $cast;
5144
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005145 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005146 $cast = "$cast1 or $cast2";
5147 } elsif ($cast1 ne "") {
5148 $cast = $cast1;
5149 } else {
5150 $cast = $cast2;
5151 }
5152 WARN("MINMAX",
5153 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005154 }
5155 }
5156
Joe Perches4a273192012-07-30 14:41:20 -07005157# check usleep_range arguments
5158 if ($^V && $^V ge 5.10.0 &&
5159 defined $stat &&
5160 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5161 my $min = $1;
5162 my $max = $7;
5163 if ($min eq $max) {
5164 WARN("USLEEP_RANGE",
5165 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5166 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5167 $min > $max) {
5168 WARN("USLEEP_RANGE",
5169 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5170 }
5171 }
5172
Joe Perches823b7942013-11-12 15:10:15 -08005173# check for naked sscanf
5174 if ($^V && $^V ge 5.10.0 &&
5175 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005176 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005177 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5178 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5179 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5180 my $lc = $stat =~ tr@\n@@;
5181 $lc = $lc + $linenr;
5182 my $stat_real = raw_line($linenr, 0);
5183 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5184 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5185 }
5186 WARN("NAKED_SSCANF",
5187 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5188 }
5189
Joe Perchesafc819a2014-06-04 16:12:08 -07005190# check for simple sscanf that should be kstrto<foo>
5191 if ($^V && $^V ge 5.10.0 &&
5192 defined $stat &&
5193 $line =~ /\bsscanf\b/) {
5194 my $lc = $stat =~ tr@\n@@;
5195 $lc = $lc + $linenr;
5196 my $stat_real = raw_line($linenr, 0);
5197 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5198 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5199 }
5200 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5201 my $format = $6;
5202 my $count = $format =~ tr@%@%@;
5203 if ($count == 1 &&
5204 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5205 WARN("SSCANF_TO_KSTRTO",
5206 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5207 }
5208 }
5209 }
5210
Joe Perches70dc8a42013-09-11 14:23:58 -07005211# check for new externs in .h files.
5212 if ($realfile =~ /\.h$/ &&
5213 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005214 if (CHK("AVOID_EXTERNS",
5215 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005216 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005217 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005218 }
5219 }
5220
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005221# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005222 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005223 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005224 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005225 my $function_name = $1;
5226 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005227
5228 my $s = $stat;
5229 if (defined $cond) {
5230 substr($s, 0, length($cond), '');
5231 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005232 if ($s =~ /^\s*;/ &&
5233 $function_name ne 'uninitialized_var')
5234 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005235 WARN("AVOID_EXTERNS",
5236 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005237 }
5238
5239 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005240 WARN("FUNCTION_ARGUMENTS",
5241 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005242 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005243
5244 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5245 $stat =~ /^.\s*extern\s+/)
5246 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005247 WARN("AVOID_EXTERNS",
5248 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005249 }
5250
5251# checks for new __setup's
5252 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5253 my $name = $1;
5254
5255 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005256 CHK("UNDOCUMENTED_SETUP",
5257 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005258 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005259 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005260
5261# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005262 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005263 WARN("UNNECESSARY_CASTS",
5264 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005265 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005266
Joe Perchesa640d252013-07-03 15:05:21 -07005267# alloc style
5268# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5269 if ($^V && $^V ge 5.10.0 &&
5270 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5271 CHK("ALLOC_SIZEOF_STRUCT",
5272 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5273 }
5274
Joe Perches60a55362014-06-04 16:12:07 -07005275# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5276 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005277 $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 -07005278 my $oldfunc = $3;
5279 my $a1 = $4;
5280 my $a2 = $10;
5281 my $newfunc = "kmalloc_array";
5282 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005283 my $r1 = $a1;
5284 my $r2 = $a2;
5285 if ($a1 =~ /^sizeof\s*\S/) {
5286 $r1 = $a2;
5287 $r2 = $a1;
5288 }
5289 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5290 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005291 if (WARN("ALLOC_WITH_MULTIPLY",
5292 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5293 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005294 $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 -07005295
5296 }
5297 }
5298 }
5299
Joe Perches972fdea2013-04-29 16:18:12 -07005300# check for krealloc arg reuse
5301 if ($^V && $^V ge 5.10.0 &&
5302 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5303 WARN("KREALLOC_ARG_REUSE",
5304 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5305 }
5306
Joe Perches5ce59ae2013-02-21 16:44:18 -08005307# check for alloc argument mismatch
5308 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5309 WARN("ALLOC_ARRAY_ARGS",
5310 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5311 }
5312
Joe Perchescaf2a542011-01-12 16:59:56 -08005313# check for multiple semicolons
5314 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005315 if (WARN("ONE_SEMICOLON",
5316 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5317 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005318 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005319 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005320 }
5321
Joe Perches0ab90192014-12-10 15:51:57 -08005322# check for #defines like: 1 << <digit> that could be BIT(digit)
5323 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5324 my $ull = "";
5325 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5326 if (CHK("BIT_MACRO",
5327 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5328 $fix) {
5329 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5330 }
5331 }
5332
Joe Perchese81f2392014-08-06 16:11:25 -07005333# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005334 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5335 my $has_break = 0;
5336 my $has_statement = 0;
5337 my $count = 0;
5338 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005339 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005340 $prevline--;
5341 my $rline = $rawlines[$prevline - 1];
5342 my $fline = $lines[$prevline - 1];
5343 last if ($fline =~ /^\@\@/);
5344 next if ($fline =~ /^\-/);
5345 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5346 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5347 next if ($fline =~ /^.[\s$;]*$/);
5348 $has_statement = 1;
5349 $count++;
5350 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5351 }
5352 if (!$has_break && $has_statement) {
5353 WARN("MISSING_BREAK",
5354 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5355 }
5356 }
5357
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005358# check for switch/default statements without a break;
5359 if ($^V && $^V ge 5.10.0 &&
5360 defined $stat &&
5361 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5362 my $ctx = '';
5363 my $herectx = $here . "\n";
5364 my $cnt = statement_rawlines($stat);
5365 for (my $n = 0; $n < $cnt; $n++) {
5366 $herectx .= raw_line($linenr, $n) . "\n";
5367 }
5368 WARN("DEFAULT_NO_BREAK",
5369 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005370 }
5371
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005372# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005373 if ($line =~ /\b__FUNCTION__\b/) {
5374 if (WARN("USE_FUNC",
5375 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5376 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005377 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005378 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005379 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005380
Joe Perches62ec8182015-02-13 14:38:18 -08005381# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5382 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5383 ERROR("DATE_TIME",
5384 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5385 }
5386
Joe Perches2c924882012-03-23 15:02:20 -07005387# check for use of yield()
5388 if ($line =~ /\byield\s*\(\s*\)/) {
5389 WARN("YIELD",
5390 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5391 }
5392
Joe Perches179f8f42013-07-03 15:05:30 -07005393# check for comparisons against true and false
5394 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5395 my $lead = $1;
5396 my $arg = $2;
5397 my $test = $3;
5398 my $otype = $4;
5399 my $trail = $5;
5400 my $op = "!";
5401
5402 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5403
5404 my $type = lc($otype);
5405 if ($type =~ /^(?:true|false)$/) {
5406 if (("$test" eq "==" && "$type" eq "true") ||
5407 ("$test" eq "!=" && "$type" eq "false")) {
5408 $op = "";
5409 }
5410
5411 CHK("BOOL_COMPARISON",
5412 "Using comparison to $otype is error prone\n" . $herecurr);
5413
5414## maybe suggesting a correct construct would better
5415## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5416
5417 }
5418 }
5419
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005420# check for semaphores initialized locked
5421 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005422 WARN("CONSIDER_COMPLETION",
5423 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005424 }
Joe Perches6712d852012-03-23 15:02:20 -07005425
Joe Perches67d0a072011-10-31 17:13:10 -07005426# recommend kstrto* over simple_strto* and strict_strto*
5427 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005428 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005429 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005430 }
Joe Perches6712d852012-03-23 15:02:20 -07005431
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005432# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005433 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005434 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005435 "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 -07005436 }
Joe Perches6712d852012-03-23 15:02:20 -07005437
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005438# check for various structs that are normally const (ops, kgdb, device_tree)
5439 my $const_structs = qr{
5440 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005441 address_space_operations|
5442 backlight_ops|
5443 block_device_operations|
5444 dentry_operations|
5445 dev_pm_ops|
5446 dma_map_ops|
5447 extent_io_ops|
5448 file_lock_operations|
5449 file_operations|
5450 hv_ops|
5451 ide_dma_ops|
5452 intel_dvo_dev_ops|
5453 item_operations|
5454 iwl_ops|
5455 kgdb_arch|
5456 kgdb_io|
5457 kset_uevent_ops|
5458 lock_manager_operations|
5459 microcode_ops|
5460 mtrr_ops|
5461 neigh_ops|
5462 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005463 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08005464 pci_raw_ops|
5465 pipe_buf_operations|
5466 platform_hibernation_ops|
5467 platform_suspend_ops|
5468 proto_ops|
5469 rpc_pipe_ops|
5470 seq_operations|
5471 snd_ac97_build_ops|
5472 soc_pcmcia_socket_ops|
5473 stacktrace_ops|
5474 sysfs_ops|
5475 tty_operations|
Joe Perches6d07d01b2015-04-16 12:44:33 -07005476 uart_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005477 usb_mon_operations|
5478 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005479 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005480 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005481 WARN("CONST_STRUCT",
5482 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005483 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005484 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005485
5486# use of NR_CPUS is usually wrong
5487# ignore definitions of NR_CPUS and usage to define arrays as likely right
5488 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005489 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5490 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005491 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5492 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5493 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005494 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005495 WARN("NR_CPUS",
5496 "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 -07005497 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005498
Joe Perches52ea8502013-11-12 15:10:09 -08005499# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5500 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5501 ERROR("DEFINE_ARCH_HAS",
5502 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5503 }
5504
Joe Perchesacd93622015-02-13 14:38:38 -08005505# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5506 if ($^V && $^V ge 5.10.0 &&
5507 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5508 WARN("LIKELY_MISUSE",
5509 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5510 }
5511
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005512# whine mightly about in_atomic
5513 if ($line =~ /\bin_atomic\s*\(/) {
5514 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005515 ERROR("IN_ATOMIC",
5516 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005517 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005518 WARN("IN_ATOMIC",
5519 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005520 }
5521 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005522
5523# check for lockdep_set_novalidate_class
5524 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5525 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5526 if ($realfile !~ m@^kernel/lockdep@ &&
5527 $realfile !~ m@^include/linux/lockdep@ &&
5528 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005529 ERROR("LOCKDEP",
5530 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005531 }
5532 }
Dave Jones88f88312011-01-12 16:59:59 -08005533
Joe Perchesb392c642015-04-16 12:44:16 -07005534 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5535 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005536 WARN("EXPORTED_WORLD_WRITABLE",
5537 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005538 }
Joe Perches24358802014-04-03 14:49:13 -07005539
Joe Perches515a2352014-04-03 14:49:24 -07005540# Mode permission misuses where it seems decimal should be octal
5541# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5542 if ($^V && $^V ge 5.10.0 &&
5543 $line =~ /$mode_perms_search/) {
5544 foreach my $entry (@mode_permission_funcs) {
5545 my $func = $entry->[0];
5546 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005547
Joe Perches515a2352014-04-03 14:49:24 -07005548 my $skip_args = "";
5549 if ($arg_pos > 1) {
5550 $arg_pos--;
5551 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5552 }
5553 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5554 if ($line =~ /$test/) {
5555 my $val = $1;
5556 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005557
Joe Perches515a2352014-04-03 14:49:24 -07005558 if ($val !~ /^0$/ &&
5559 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5560 length($val) ne 4)) {
5561 ERROR("NON_OCTAL_PERMISSIONS",
5562 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005563 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5564 ERROR("EXPORTED_WORLD_WRITABLE",
5565 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005566 }
Joe Perches24358802014-04-03 14:49:13 -07005567 }
5568 }
5569 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005570 }
5571
5572 # If we have no input at all, then there is nothing to report on
5573 # so just keep quiet.
5574 if ($#rawlines == -1) {
5575 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005576 }
5577
Andy Whitcroft8905a672007-11-28 16:21:06 -08005578 # In mailback mode only produce a report in the negative, for
5579 # things that appear to be patches.
5580 if ($mailback && ($clean == 1 || !$is_patch)) {
5581 exit(0);
5582 }
5583
5584 # This is not a patch, and we are are in 'no-patch' mode so
5585 # just keep quiet.
5586 if (!$chk_patch && !$is_patch) {
5587 exit(0);
5588 }
5589
5590 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005591 ERROR("NOT_UNIFIED_DIFF",
5592 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005593 }
5594 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005595 ERROR("MISSING_SIGN_OFF",
5596 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005597 }
5598
Andy Whitcroft8905a672007-11-28 16:21:06 -08005599 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005600 if ($summary && !($clean == 1 && $quiet == 1)) {
5601 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005602 print "total: $cnt_error errors, $cnt_warn warnings, " .
5603 (($check)? "$cnt_chk checks, " : "") .
5604 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005605 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005606
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005607 if ($quiet == 0) {
5608 # If there were whitespace errors which cleanpatch can fix
5609 # then suggest that.
5610 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07005611 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07005612 print << "EOM"
5613
5614NOTE: Whitespace errors detected.
5615 You may wish to use scripts/cleanpatch or scripts/cleanfile
5616EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005617 }
5618 }
5619
Joe Perches91bfe482013-09-11 14:23:59 -07005620 hash_show_words(\%use_type, "Used");
5621 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005622
Joe Perchesd752fcc2014-08-06 16:11:05 -07005623 if ($clean == 0 && $fix &&
5624 ("@rawlines" ne "@fixed" ||
5625 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005626 my $newfile = $filename;
5627 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005628 my $linecount = 0;
5629 my $f;
5630
Joe Perchesd752fcc2014-08-06 16:11:05 -07005631 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5632
Joe Perches3705ce52013-07-03 15:05:31 -07005633 open($f, '>', $newfile)
5634 or die "$P: Can't open $newfile for write\n";
5635 foreach my $fixed_line (@fixed) {
5636 $linecount++;
5637 if ($file) {
5638 if ($linecount > 3) {
5639 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005640 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005641 }
5642 } else {
5643 print $f $fixed_line . "\n";
5644 }
5645 }
5646 close($f);
5647
5648 if (!$quiet) {
5649 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07005650
Joe Perches3705ce52013-07-03 15:05:31 -07005651Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5652
5653Do _NOT_ trust the results written to this file.
5654Do _NOT_ submit these changes without inspecting them for correctness.
5655
5656This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5657No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07005658EOM
5659 }
5660 }
5661
Joe Perchesd8469f12015-06-25 15:03:00 -07005662 if ($quiet == 0) {
5663 print "\n";
5664 if ($clean == 1) {
5665 print "$vname has no obvious style problems and is ready for submission.\n";
5666 } else {
5667 print "$vname has style problems, please review.\n";
5668 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005669 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005670 return $clean;
5671}