aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
authorDmitry Torokhov <dmitry.torokhov@gmail.com>2013-11-14 20:38:05 -0500
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2013-11-14 20:38:05 -0500
commit42249094f79422fbf5ed4b54eeb48ff096809b8f (patch)
tree91e6850c8c7e8cc284cf8bb6363f8662f84011f4 /scripts/checkpatch.pl
parent936816161978ca716a56c5e553c68f25972b1e3a (diff)
parent2c027b7c48a888ab173ba45babb4525e278375d9 (diff)
Merge branch 'next' into for-linus
Merge first round of changes for 3.13 merge window.
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl929
1 files changed, 766 insertions, 163 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b954de58304f..66cad506b8a2 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6,6 +6,7 @@
6# Licensed under the terms of the GNU GPL License version 2 6# Licensed under the terms of the GNU GPL License version 2
7 7
8use strict; 8use strict;
9use POSIX;
9 10
10my $P = $0; 11my $P = $0;
11$P =~ s@.*/@@g; 12$P =~ s@.*/@@g;
@@ -27,13 +28,19 @@ my $summary = 1;
27my $mailback = 0; 28my $mailback = 0;
28my $summary_file = 0; 29my $summary_file = 0;
29my $show_types = 0; 30my $show_types = 0;
31my $fix = 0;
30my $root; 32my $root;
31my %debug; 33my %debug;
34my %camelcase = ();
35my %use_type = ();
36my @use = ();
32my %ignore_type = (); 37my %ignore_type = ();
33my @ignore = (); 38my @ignore = ();
34my $help = 0; 39my $help = 0;
35my $configuration_file = ".checkpatch.conf"; 40my $configuration_file = ".checkpatch.conf";
36my $max_line_length = 80; 41my $max_line_length = 80;
42my $ignore_perl_version = 0;
43my $minimum_perl_version = 5.10.0;
37 44
38sub help { 45sub help {
39 my ($exitcode) = @_; 46 my ($exitcode) = @_;
@@ -51,6 +58,7 @@ Options:
51 --terse one line per report 58 --terse one line per report
52 -f, --file treat FILE as regular source file 59 -f, --file treat FILE as regular source file
53 --subjective, --strict enable more subjective tests 60 --subjective, --strict enable more subjective tests
61 --types TYPE(,TYPE2...) show only these comma separated message types
54 --ignore TYPE(,TYPE2...) ignore various comma separated message types 62 --ignore TYPE(,TYPE2...) ignore various comma separated message types
55 --max-line-length=n set the maximum line length, if exceeded, warn 63 --max-line-length=n set the maximum line length, if exceeded, warn
56 --show-types show the message "types" in the output 64 --show-types show the message "types" in the output
@@ -63,6 +71,13 @@ Options:
63 is all off) 71 is all off)
64 --test-only=WORD report only warnings/errors containing WORD 72 --test-only=WORD report only warnings/errors containing WORD
65 literally 73 literally
74 --fix EXPERIMENTAL - may create horrible results
75 If correctable single-line errors exist, create
76 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
77 with potential errors corrected to the preferred
78 checkpatch style
79 --ignore-perl-version override checking of perl version. expect
80 runtime errors.
66 -h, --help, --version display this help and exit 81 -h, --help, --version display this help and exit
67 82
68When FILE is - read standard input. 83When FILE is - read standard input.
@@ -108,13 +123,15 @@ GetOptions(
108 'subjective!' => \$check, 123 'subjective!' => \$check,
109 'strict!' => \$check, 124 'strict!' => \$check,
110 'ignore=s' => \@ignore, 125 'ignore=s' => \@ignore,
126 'types=s' => \@use,
111 'show-types!' => \$show_types, 127 'show-types!' => \$show_types,
112 'max-line-length=i' => \$max_line_length, 128 'max-line-length=i' => \$max_line_length,
113 'root=s' => \$root, 129 'root=s' => \$root,
114 'summary!' => \$summary, 130 'summary!' => \$summary,
115 'mailback!' => \$mailback, 131 'mailback!' => \$mailback,
116 'summary-file!' => \$summary_file, 132 'summary-file!' => \$summary_file,
117 133 'fix!' => \$fix,
134 'ignore-perl-version!' => \$ignore_perl_version,
118 'debug=s' => \%debug, 135 'debug=s' => \%debug,
119 'test-only=s' => \$tst_only, 136 'test-only=s' => \$tst_only,
120 'h|help' => \$help, 137 'h|help' => \$help,
@@ -125,24 +142,50 @@ help(0) if ($help);
125 142
126my $exit = 0; 143my $exit = 0;
127 144
145if ($^V && $^V lt $minimum_perl_version) {
146 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
147 if (!$ignore_perl_version) {
148 exit(1);
149 }
150}
151
128if ($#ARGV < 0) { 152if ($#ARGV < 0) {
129 print "$P: no input files\n"; 153 print "$P: no input files\n";
130 exit(1); 154 exit(1);
131} 155}
132 156
133@ignore = split(/,/, join(',',@ignore)); 157sub hash_save_array_words {
134foreach my $word (@ignore) { 158 my ($hashRef, $arrayRef) = @_;
135 $word =~ s/\s*\n?$//g; 159
136 $word =~ s/^\s*//g; 160 my @array = split(/,/, join(',', @$arrayRef));
137 $word =~ s/\s+/ /g; 161 foreach my $word (@array) {
138 $word =~ tr/[a-z]/[A-Z]/; 162 $word =~ s/\s*\n?$//g;
163 $word =~ s/^\s*//g;
164 $word =~ s/\s+/ /g;
165 $word =~ tr/[a-z]/[A-Z]/;
166
167 next if ($word =~ m/^\s*#/);
168 next if ($word =~ m/^\s*$/);
169
170 $hashRef->{$word}++;
171 }
172}
139 173
140 next if ($word =~ m/^\s*#/); 174sub hash_show_words {
141 next if ($word =~ m/^\s*$/); 175 my ($hashRef, $prefix) = @_;
142 176
143 $ignore_type{$word}++; 177 if ($quiet == 0 && keys %$hashRef) {
178 print "NOTE: $prefix message types:";
179 foreach my $word (sort keys %$hashRef) {
180 print " $word";
181 }
182 print "\n\n";
183 }
144} 184}
145 185
186hash_save_array_words(\%ignore_type, \@ignore);
187hash_save_array_words(\%use_type, \@use);
188
146my $dbg_values = 0; 189my $dbg_values = 0;
147my $dbg_possible = 0; 190my $dbg_possible = 0;
148my $dbg_type = 0; 191my $dbg_type = 0;
@@ -199,6 +242,8 @@ our $Sparse = qr{
199 __rcu 242 __rcu
200 }x; 243 }x;
201 244
245our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};
246
202# Notes to $Attribute: 247# Notes to $Attribute:
203# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 248# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
204our $Attribute = qr{ 249our $Attribute = qr{
@@ -219,7 +264,7 @@ our $Attribute = qr{
219 __deprecated| 264 __deprecated|
220 __read_mostly| 265 __read_mostly|
221 __kprobes| 266 __kprobes|
222 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| 267 $InitAttribute|
223 ____cacheline_aligned| 268 ____cacheline_aligned|
224 ____cacheline_aligned_in_smp| 269 ____cacheline_aligned_in_smp|
225 ____cacheline_internodealigned_in_smp| 270 ____cacheline_internodealigned_in_smp|
@@ -230,20 +275,26 @@ our $Inline = qr{inline|__always_inline|noinline};
230our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 275our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
231our $Lval = qr{$Ident(?:$Member)*}; 276our $Lval = qr{$Ident(?:$Member)*};
232 277
278our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
279our $Binary = qr{(?i)0b[01]+$Int_type?};
280our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
281our $Int = qr{[0-9]+$Int_type?};
233our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 282our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
234our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 283our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
235our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 284our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
236our $Float = qr{$Float_hex|$Float_dec|$Float_int}; 285our $Float = qr{$Float_hex|$Float_dec|$Float_int};
237our $Constant = qr{$Float|(?i)(?:0x[0-9a-f]+|[0-9]+)[ul]*}; 286our $Constant = qr{$Float|$Binary|$Hex|$Int};
238our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 287our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
239our $Compare = qr{<=|>=|==|!=|<|>}; 288our $Compare = qr{<=|>=|==|!=|<|>};
289our $Arithmetic = qr{\+|-|\*|\/|%};
240our $Operators = qr{ 290our $Operators = qr{
241 <=|>=|==|!=| 291 <=|>=|==|!=|
242 =>|->|<<|>>|<|>|!|~| 292 =>|->|<<|>>|<|>|!|~|
243 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 293 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
244 }x; 294 }x;
245 295
246our $NonptrType; 296our $NonptrType;
297our $NonptrTypeWithAttr;
247our $Type; 298our $Type;
248our $Declare; 299our $Declare;
249 300
@@ -269,7 +320,7 @@ our $typeTypedefs = qr{(?x:
269 320
270our $logFunctions = qr{(?x: 321our $logFunctions = qr{(?x:
271 printk(?:_ratelimited|_once|)| 322 printk(?:_ratelimited|_once|)|
272 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 323 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
273 WARN(?:_RATELIMIT|_ONCE|)| 324 WARN(?:_RATELIMIT|_ONCE|)|
274 panic| 325 panic|
275 MODULE_[A-Z_]+ 326 MODULE_[A-Z_]+
@@ -306,6 +357,12 @@ our @typeList = (
306 qr{${Ident}_handler}, 357 qr{${Ident}_handler},
307 qr{${Ident}_handler_fn}, 358 qr{${Ident}_handler_fn},
308); 359);
360our @typeListWithAttr = (
361 @typeList,
362 qr{struct\s+$InitAttribute\s+$Ident},
363 qr{union\s+$InitAttribute\s+$Ident},
364);
365
309our @modifierList = ( 366our @modifierList = (
310 qr{fastcall}, 367 qr{fastcall},
311); 368);
@@ -319,6 +376,7 @@ our $allowed_asm_includes = qr{(?x:
319sub build_types { 376sub build_types {
320 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 377 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
321 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 378 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
379 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
322 $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 380 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
323 $NonptrType = qr{ 381 $NonptrType = qr{
324 (?:$Modifier\s+|const\s+)* 382 (?:$Modifier\s+|const\s+)*
@@ -329,6 +387,15 @@ sub build_types {
329 ) 387 )
330 (?:\s+$Modifier|\s+const)* 388 (?:\s+$Modifier|\s+const)*
331 }x; 389 }x;
390 $NonptrTypeWithAttr = qr{
391 (?:$Modifier\s+|const\s+)*
392 (?:
393 (?:typeof|__typeof__)\s*\([^\)]*\)|
394 (?:$typeTypedefs\b)|
395 (?:${allWithAttr}\b)
396 )
397 (?:\s+$Modifier|\s+const)*
398 }x;
332 $Type = qr{ 399 $Type = qr{
333 $NonptrType 400 $NonptrType
334 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? 401 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
@@ -338,7 +405,6 @@ sub build_types {
338} 405}
339build_types(); 406build_types();
340 407
341
342our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 408our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
343 409
344# Using $balanced_parens, $LvalOrFunc, or $FuncArg 410# Using $balanced_parens, $LvalOrFunc, or $FuncArg
@@ -358,10 +424,94 @@ sub deparenthesize {
358 return $string; 424 return $string;
359} 425}
360 426
427sub seed_camelcase_file {
428 my ($file) = @_;
429
430 return if (!(-f $file));
431
432 local $/;
433
434 open(my $include_file, '<', "$file")
435 or warn "$P: Can't read '$file' $!\n";
436 my $text = <$include_file>;
437 close($include_file);
438
439 my @lines = split('\n', $text);
440
441 foreach my $line (@lines) {
442 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
443 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
444 $camelcase{$1} = 1;
445 }
446 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
447 $camelcase{$1} = 1;
448 }
449 }
450}
451
452my $camelcase_seeded = 0;
453sub seed_camelcase_includes {
454 return if ($camelcase_seeded);
455
456 my $files;
457 my $camelcase_cache = "";
458 my @include_files = ();
459
460 $camelcase_seeded = 1;
461
462 if (-d ".git") {
463 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
464 chomp $git_last_include_commit;
465 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
466 } else {
467 my $last_mod_date = 0;
468 $files = `find $root/include -name "*.h"`;
469 @include_files = split('\n', $files);
470 foreach my $file (@include_files) {
471 my $date = POSIX::strftime("%Y%m%d%H%M",
472 localtime((stat $file)[9]));
473 $last_mod_date = $date if ($last_mod_date < $date);
474 }
475 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
476 }
477
478 if ($camelcase_cache ne "" && -f $camelcase_cache) {
479 open(my $camelcase_file, '<', "$camelcase_cache")
480 or warn "$P: Can't read '$camelcase_cache' $!\n";
481 while (<$camelcase_file>) {
482 chomp;
483 $camelcase{$_} = 1;
484 }
485 close($camelcase_file);
486
487 return;
488 }
489
490 if (-d ".git") {
491 $files = `git ls-files "include/*.h"`;
492 @include_files = split('\n', $files);
493 }
494
495 foreach my $file (@include_files) {
496 seed_camelcase_file($file);
497 }
498
499 if ($camelcase_cache ne "") {
500 unlink glob ".checkpatch-camelcase.*";
501 open(my $camelcase_file, '>', "$camelcase_cache")
502 or warn "$P: Can't write '$camelcase_cache' $!\n";
503 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
504 print $camelcase_file ("$_\n");
505 }
506 close($camelcase_file);
507 }
508}
509
361$chk_signoff = 0 if ($file); 510$chk_signoff = 0 if ($file);
362 511
363my @rawlines = (); 512my @rawlines = ();
364my @lines = (); 513my @lines = ();
514my @fixed = ();
365my $vname; 515my $vname;
366for my $filename (@ARGV) { 516for my $filename (@ARGV) {
367 my $FILE; 517 my $FILE;
@@ -389,6 +539,7 @@ for my $filename (@ARGV) {
389 } 539 }
390 @rawlines = (); 540 @rawlines = ();
391 @lines = (); 541 @lines = ();
542 @fixed = ();
392} 543}
393 544
394exit($exit); 545exit($exit);
@@ -429,7 +580,7 @@ sub parse_email {
429 $comment = $2 if defined $2; 580 $comment = $2 if defined $2;
430 $formatted_email =~ s/$address.*$//; 581 $formatted_email =~ s/$address.*$//;
431 $name = $formatted_email; 582 $name = $formatted_email;
432 $name =~ s/^\s+|\s+$//g; 583 $name = trim($name);
433 $name =~ s/^\"|\"$//g; 584 $name =~ s/^\"|\"$//g;
434 # If there's a name left after stripping spaces and 585 # If there's a name left after stripping spaces and
435 # leading quotes, and the address doesn't have both 586 # leading quotes, and the address doesn't have both
@@ -444,9 +595,9 @@ sub parse_email {
444 } 595 }
445 } 596 }
446 597
447 $name =~ s/^\s+|\s+$//g; 598 $name = trim($name);
448 $name =~ s/^\"|\"$//g; 599 $name =~ s/^\"|\"$//g;
449 $address =~ s/^\s+|\s+$//g; 600 $address = trim($address);
450 $address =~ s/^\<|\>$//g; 601 $address =~ s/^\<|\>$//g;
451 602
452 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 603 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
@@ -462,9 +613,9 @@ sub format_email {
462 613
463 my $formatted_email; 614 my $formatted_email;
464 615
465 $name =~ s/^\s+|\s+$//g; 616 $name = trim($name);
466 $name =~ s/^\"|\"$//g; 617 $name =~ s/^\"|\"$//g;
467 $address =~ s/^\s+|\s+$//g; 618 $address = trim($address);
468 619
469 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 620 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
470 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 621 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
@@ -1258,7 +1409,9 @@ sub possible {
1258my $prefix = ''; 1409my $prefix = '';
1259 1410
1260sub show_type { 1411sub show_type {
1261 return !defined $ignore_type{$_[0]}; 1412 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1413
1414 return !defined $ignore_type{$_[0]};
1262} 1415}
1263 1416
1264sub report { 1417sub report {
@@ -1286,19 +1439,25 @@ sub ERROR {
1286 if (report("ERROR", $_[0], $_[1])) { 1439 if (report("ERROR", $_[0], $_[1])) {
1287 our $clean = 0; 1440 our $clean = 0;
1288 our $cnt_error++; 1441 our $cnt_error++;
1442 return 1;
1289 } 1443 }
1444 return 0;
1290} 1445}
1291sub WARN { 1446sub WARN {
1292 if (report("WARNING", $_[0], $_[1])) { 1447 if (report("WARNING", $_[0], $_[1])) {
1293 our $clean = 0; 1448 our $clean = 0;
1294 our $cnt_warn++; 1449 our $cnt_warn++;
1450 return 1;
1295 } 1451 }
1452 return 0;
1296} 1453}
1297sub CHK { 1454sub CHK {
1298 if ($check && report("CHECK", $_[0], $_[1])) { 1455 if ($check && report("CHECK", $_[0], $_[1])) {
1299 our $clean = 0; 1456 our $clean = 0;
1300 our $cnt_chk++; 1457 our $cnt_chk++;
1458 return 1;
1301 } 1459 }
1460 return 0;
1302} 1461}
1303 1462
1304sub check_absolute_file { 1463sub check_absolute_file {
@@ -1329,6 +1488,45 @@ sub check_absolute_file {
1329 } 1488 }
1330} 1489}
1331 1490
1491sub trim {
1492 my ($string) = @_;
1493
1494 $string =~ s/^\s+|\s+$//g;
1495
1496 return $string;
1497}
1498
1499sub ltrim {
1500 my ($string) = @_;
1501
1502 $string =~ s/^\s+//;
1503
1504 return $string;
1505}
1506
1507sub rtrim {
1508 my ($string) = @_;
1509
1510 $string =~ s/\s+$//;
1511
1512 return $string;
1513}
1514
1515sub tabify {
1516 my ($leading) = @_;
1517
1518 my $source_indent = 8;
1519 my $max_spaces_before_tab = $source_indent - 1;
1520 my $spaces_to_tab = " " x $source_indent;
1521
1522 #convert leading spaces to tabs
1523 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1524 #Remove spaces before a tab
1525 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1526
1527 return "$leading";
1528}
1529
1332sub pos_last_openparen { 1530sub pos_last_openparen {
1333 my ($line) = @_; 1531 my ($line) = @_;
1334 1532
@@ -1406,7 +1604,7 @@ sub process {
1406 my %suppress_export; 1604 my %suppress_export;
1407 my $suppress_statement = 0; 1605 my $suppress_statement = 0;
1408 1606
1409 my %camelcase = (); 1607 my %signatures = ();
1410 1608
1411 # Pre-scan the patch sanitizing the lines. 1609 # Pre-scan the patch sanitizing the lines.
1412 # Pre-scan the patch looking for any __setup documentation. 1610 # Pre-scan the patch looking for any __setup documentation.
@@ -1420,6 +1618,8 @@ sub process {
1420 $linenr++; 1618 $linenr++;
1421 $line = $rawline; 1619 $line = $rawline;
1422 1620
1621 push(@fixed, $rawline) if ($fix);
1622
1423 if ($rawline=~/^\+\+\+\s+(\S+)/) { 1623 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1424 $setup_docs = 0; 1624 $setup_docs = 0;
1425 if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1625 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
@@ -1497,6 +1697,8 @@ sub process {
1497 $linenr = 0; 1697 $linenr = 0;
1498 foreach my $line (@lines) { 1698 foreach my $line (@lines) {
1499 $linenr++; 1699 $linenr++;
1700 my $sline = $line; #copy of $line
1701 $sline =~ s/$;/ /g; #with comments as spaces
1500 1702
1501 my $rawline = $rawlines[$linenr - 1]; 1703 my $rawline = $rawlines[$linenr - 1];
1502 1704
@@ -1611,16 +1813,29 @@ sub process {
1611 "Non-standard signature: $sign_off\n" . $herecurr); 1813 "Non-standard signature: $sign_off\n" . $herecurr);
1612 } 1814 }
1613 if (defined $space_before && $space_before ne "") { 1815 if (defined $space_before && $space_before ne "") {
1614 WARN("BAD_SIGN_OFF", 1816 if (WARN("BAD_SIGN_OFF",
1615 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr); 1817 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1818 $fix) {
1819 $fixed[$linenr - 1] =
1820 "$ucfirst_sign_off $email";
1821 }
1616 } 1822 }
1617 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 1823 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1618 WARN("BAD_SIGN_OFF", 1824 if (WARN("BAD_SIGN_OFF",
1619 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr); 1825 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1826 $fix) {
1827 $fixed[$linenr - 1] =
1828 "$ucfirst_sign_off $email";
1829 }
1830
1620 } 1831 }
1621 if (!defined $space_after || $space_after ne " ") { 1832 if (!defined $space_after || $space_after ne " ") {
1622 WARN("BAD_SIGN_OFF", 1833 if (WARN("BAD_SIGN_OFF",
1623 "Use a single space after $ucfirst_sign_off\n" . $herecurr); 1834 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1835 $fix) {
1836 $fixed[$linenr - 1] =
1837 "$ucfirst_sign_off $email";
1838 }
1624 } 1839 }
1625 1840
1626 my ($email_name, $email_address, $comment) = parse_email($email); 1841 my ($email_name, $email_address, $comment) = parse_email($email);
@@ -1641,6 +1856,17 @@ sub process {
1641 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 1856 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1642 } 1857 }
1643 } 1858 }
1859
1860# Check for duplicate signatures
1861 my $sig_nospace = $line;
1862 $sig_nospace =~ s/\s//g;
1863 $sig_nospace = lc($sig_nospace);
1864 if (defined $signatures{$sig_nospace}) {
1865 WARN("BAD_SIGN_OFF",
1866 "Duplicate signature\n" . $herecurr);
1867 } else {
1868 $signatures{$sig_nospace} = 1;
1869 }
1644 } 1870 }
1645 1871
1646# Check for wrappage within a valid hunk of the file 1872# Check for wrappage within a valid hunk of the file
@@ -1705,13 +1931,19 @@ sub process {
1705#trailing whitespace 1931#trailing whitespace
1706 if ($line =~ /^\+.*\015/) { 1932 if ($line =~ /^\+.*\015/) {
1707 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1933 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1708 ERROR("DOS_LINE_ENDINGS", 1934 if (ERROR("DOS_LINE_ENDINGS",
1709 "DOS line endings\n" . $herevet); 1935 "DOS line endings\n" . $herevet) &&
1710 1936 $fix) {
1937 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1938 }
1711 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1939 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1712 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1940 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1713 ERROR("TRAILING_WHITESPACE", 1941 if (ERROR("TRAILING_WHITESPACE",
1714 "trailing whitespace\n" . $herevet); 1942 "trailing whitespace\n" . $herevet) &&
1943 $fix) {
1944 $fixed[$linenr - 1] =~ s/\s+$//;
1945 }
1946
1715 $rpt_cleaners = 1; 1947 $rpt_cleaners = 1;
1716 } 1948 }
1717 1949
@@ -1806,8 +2038,12 @@ sub process {
1806 2038
1807# check for spaces before a quoted newline 2039# check for spaces before a quoted newline
1808 if ($rawline =~ /^.*\".*\s\\n/) { 2040 if ($rawline =~ /^.*\".*\s\\n/) {
1809 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 2041 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1810 "unnecessary whitespace before a quoted newline\n" . $herecurr); 2042 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2043 $fix) {
2044 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2045 }
2046
1811 } 2047 }
1812 2048
1813# check for adding lines without a newline. 2049# check for adding lines without a newline.
@@ -1838,16 +2074,23 @@ sub process {
1838 if ($rawline =~ /^\+\s* \t\s*\S/ || 2074 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1839 $rawline =~ /^\+\s* \s*/) { 2075 $rawline =~ /^\+\s* \s*/) {
1840 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2076 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1841 ERROR("CODE_INDENT",
1842 "code indent should use tabs where possible\n" . $herevet);
1843 $rpt_cleaners = 1; 2077 $rpt_cleaners = 1;
2078 if (ERROR("CODE_INDENT",
2079 "code indent should use tabs where possible\n" . $herevet) &&
2080 $fix) {
2081 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2082 }
1844 } 2083 }
1845 2084
1846# check for space before tabs. 2085# check for space before tabs.
1847 if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 2086 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1848 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2087 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1849 WARN("SPACE_BEFORE_TAB", 2088 if (WARN("SPACE_BEFORE_TAB",
1850 "please, no space before tabs\n" . $herevet); 2089 "please, no space before tabs\n" . $herevet) &&
2090 $fix) {
2091 $fixed[$linenr - 1] =~
2092 s/(^\+.*) +\t/$1\t/;
2093 }
1851 } 2094 }
1852 2095
1853# check for && or || at the start of a line 2096# check for && or || at the start of a line
@@ -1875,25 +2118,43 @@ sub process {
1875 2118
1876 if ($newindent ne $goodtabindent && 2119 if ($newindent ne $goodtabindent &&
1877 $newindent ne $goodspaceindent) { 2120 $newindent ne $goodspaceindent) {
1878 CHK("PARENTHESIS_ALIGNMENT", 2121
1879 "Alignment should match open parenthesis\n" . $hereprev); 2122 if (CHK("PARENTHESIS_ALIGNMENT",
2123 "Alignment should match open parenthesis\n" . $hereprev) &&
2124 $fix && $line =~ /^\+/) {
2125 $fixed[$linenr - 1] =~
2126 s/^\+[ \t]*/\+$goodtabindent/;
2127 }
1880 } 2128 }
1881 } 2129 }
1882 } 2130 }
1883 2131
1884 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) { 2132 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
1885 CHK("SPACING", 2133 if (CHK("SPACING",
1886 "No space is necessary after a cast\n" . $hereprev); 2134 "No space is necessary after a cast\n" . $hereprev) &&
2135 $fix) {
2136 $fixed[$linenr - 1] =~
2137 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2138 }
1887 } 2139 }
1888 2140
1889 if ($realfile =~ m@^(drivers/net/|net/)@ && 2141 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1890 $rawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 2142 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1891 $prevrawline =~ /^\+[ \t]*$/) { 2143 $rawline =~ /^\+[ \t]*\*/) {
1892 WARN("NETWORKING_BLOCK_COMMENT_STYLE", 2144 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1893 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 2145 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1894 } 2146 }
1895 2147
1896 if ($realfile =~ m@^(drivers/net/|net/)@ && 2148 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2149 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2150 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2151 $rawline =~ /^\+/ && #line is new
2152 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2153 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2154 "networking block comments start with * on subsequent lines\n" . $hereprev);
2155 }
2156
2157 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1897 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2158 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1898 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2159 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1899 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2160 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
@@ -1907,10 +2168,13 @@ sub process {
1907# 1) within comments 2168# 1) within comments
1908# 2) indented preprocessor commands 2169# 2) indented preprocessor commands
1909# 3) hanging labels 2170# 3) hanging labels
1910 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { 2171 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
1911 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2172 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1912 WARN("LEADING_SPACE", 2173 if (WARN("LEADING_SPACE",
1913 "please, no spaces at the start of a line\n" . $herevet); 2174 "please, no spaces at the start of a line\n" . $herevet) &&
2175 $fix) {
2176 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2177 }
1914 } 2178 }
1915 2179
1916# check we are in a valid C source file if not then ignore this hunk 2180# check we are in a valid C source file if not then ignore this hunk
@@ -1951,7 +2215,7 @@ sub process {
1951 $realline_next); 2215 $realline_next);
1952#print "LINE<$line>\n"; 2216#print "LINE<$line>\n";
1953 if ($linenr >= $suppress_statement && 2217 if ($linenr >= $suppress_statement &&
1954 $realcnt && $line =~ /.\s*\S/) { 2218 $realcnt && $sline =~ /.\s*\S/) {
1955 ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 2219 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1956 ctx_statement_block($linenr, $realcnt, 0); 2220 ctx_statement_block($linenr, $realcnt, 0);
1957 $stat =~ s/\n./\n /g; 2221 $stat =~ s/\n./\n /g;
@@ -2200,7 +2464,7 @@ sub process {
2200 $prev_values = substr($curr_values, -1); 2464 $prev_values = substr($curr_values, -1);
2201 2465
2202#ignore lines not being added 2466#ignore lines not being added
2203 if ($line=~/^[^\+]/) {next;} 2467 next if ($line =~ /^[^\+]/);
2204 2468
2205# TEST: allow direct testing of the type matcher. 2469# TEST: allow direct testing of the type matcher.
2206 if ($dbg_type) { 2470 if ($dbg_type) {
@@ -2251,8 +2515,15 @@ sub process {
2251 2515
2252# no C99 // comments 2516# no C99 // comments
2253 if ($line =~ m{//}) { 2517 if ($line =~ m{//}) {
2254 ERROR("C99_COMMENTS", 2518 if (ERROR("C99_COMMENTS",
2255 "do not use C99 // comments\n" . $herecurr); 2519 "do not use C99 // comments\n" . $herecurr) &&
2520 $fix) {
2521 my $line = $fixed[$linenr - 1];
2522 if ($line =~ /\/\/(.*)$/) {
2523 my $comment = trim($1);
2524 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2525 }
2526 }
2256 } 2527 }
2257 # Remove C99 comments. 2528 # Remove C99 comments.
2258 $line =~ s@//.*@@; 2529 $line =~ s@//.*@@;
@@ -2304,16 +2575,22 @@ sub process {
2304 } 2575 }
2305 2576
2306# check for global initialisers. 2577# check for global initialisers.
2307 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 2578 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2308 ERROR("GLOBAL_INITIALISERS", 2579 if (ERROR("GLOBAL_INITIALISERS",
2309 "do not initialise globals to 0 or NULL\n" . 2580 "do not initialise globals to 0 or NULL\n" .
2310 $herecurr); 2581 $herecurr) &&
2582 $fix) {
2583 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2584 }
2311 } 2585 }
2312# check for static initialisers. 2586# check for static initialisers.
2313 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2587 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2314 ERROR("INITIALISED_STATIC", 2588 if (ERROR("INITIALISED_STATIC",
2315 "do not initialise statics to 0 or NULL\n" . 2589 "do not initialise statics to 0 or NULL\n" .
2316 $herecurr); 2590 $herecurr) &&
2591 $fix) {
2592 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2593 }
2317 } 2594 }
2318 2595
2319# check for static const char * arrays. 2596# check for static const char * arrays.
@@ -2351,7 +2628,7 @@ sub process {
2351 # (char*[ const]) 2628 # (char*[ const])
2352 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 2629 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2353 #print "AA<$1>\n"; 2630 #print "AA<$1>\n";
2354 my ($from, $to) = ($2, $2); 2631 my ($ident, $from, $to) = ($1, $2, $2);
2355 2632
2356 # Should start with a space. 2633 # Should start with a space.
2357 $to =~ s/^(\S)/ $1/; 2634 $to =~ s/^(\S)/ $1/;
@@ -2361,15 +2638,22 @@ sub process {
2361 while ($to =~ s/\*\s+\*/\*\*/) { 2638 while ($to =~ s/\*\s+\*/\*\*/) {
2362 } 2639 }
2363 2640
2364 #print "from<$from> to<$to>\n"; 2641## print "1: from<$from> to<$to> ident<$ident>\n";
2365 if ($from ne $to) { 2642 if ($from ne $to) {
2366 ERROR("POINTER_LOCATION", 2643 if (ERROR("POINTER_LOCATION",
2367 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 2644 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2645 $fix) {
2646 my $sub_from = $ident;
2647 my $sub_to = $ident;
2648 $sub_to =~ s/\Q$from\E/$to/;
2649 $fixed[$linenr - 1] =~
2650 s@\Q$sub_from\E@$sub_to@;
2651 }
2368 } 2652 }
2369 } 2653 }
2370 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 2654 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2371 #print "BB<$1>\n"; 2655 #print "BB<$1>\n";
2372 my ($from, $to, $ident) = ($2, $2, $3); 2656 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2373 2657
2374 # Should start with a space. 2658 # Should start with a space.
2375 $to =~ s/^(\S)/ $1/; 2659 $to =~ s/^(\S)/ $1/;
@@ -2381,10 +2665,18 @@ sub process {
2381 # Modifiers should have spaces. 2665 # Modifiers should have spaces.
2382 $to =~ s/(\b$Modifier$)/$1 /; 2666 $to =~ s/(\b$Modifier$)/$1 /;
2383 2667
2384 #print "from<$from> to<$to> ident<$ident>\n"; 2668## print "2: from<$from> to<$to> ident<$ident>\n";
2385 if ($from ne $to && $ident !~ /^$Modifier$/) { 2669 if ($from ne $to && $ident !~ /^$Modifier$/) {
2386 ERROR("POINTER_LOCATION", 2670 if (ERROR("POINTER_LOCATION",
2387 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 2671 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2672 $fix) {
2673
2674 my $sub_from = $match;
2675 my $sub_to = $match;
2676 $sub_to =~ s/\Q$from\E/$to/;
2677 $fixed[$linenr - 1] =~
2678 s@\Q$sub_from\E@$sub_to@;
2679 }
2388 } 2680 }
2389 } 2681 }
2390 2682
@@ -2441,8 +2733,12 @@ sub process {
2441 } 2733 }
2442 2734
2443 if ($line =~ /\bpr_warning\s*\(/) { 2735 if ($line =~ /\bpr_warning\s*\(/) {
2444 WARN("PREFER_PR_LEVEL", 2736 if (WARN("PREFER_PR_LEVEL",
2445 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr); 2737 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2738 $fix) {
2739 $fixed[$linenr - 1] =~
2740 s/\bpr_warning\b/pr_warn/;
2741 }
2446 } 2742 }
2447 2743
2448 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 2744 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
@@ -2470,9 +2766,13 @@ sub process {
2470 } 2766 }
2471 2767
2472# missing space after union, struct or enum definition 2768# missing space after union, struct or enum definition
2473 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 2769 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2474 WARN("SPACING", 2770 if (WARN("SPACING",
2475 "missing space after $1 definition\n" . $herecurr); 2771 "missing space after $1 definition\n" . $herecurr) &&
2772 $fix) {
2773 $fixed[$linenr - 1] =~
2774 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2775 }
2476 } 2776 }
2477 2777
2478# check for spacing round square brackets; allowed: 2778# check for spacing round square brackets; allowed:
@@ -2484,8 +2784,12 @@ sub process {
2484 if ($prefix !~ /$Type\s+$/ && 2784 if ($prefix !~ /$Type\s+$/ &&
2485 ($where != 0 || $prefix !~ /^.\s+$/) && 2785 ($where != 0 || $prefix !~ /^.\s+$/) &&
2486 $prefix !~ /[{,]\s+$/) { 2786 $prefix !~ /[{,]\s+$/) {
2487 ERROR("BRACKET_SPACE", 2787 if (ERROR("BRACKET_SPACE",
2488 "space prohibited before open square bracket '['\n" . $herecurr); 2788 "space prohibited before open square bracket '['\n" . $herecurr) &&
2789 $fix) {
2790 $fixed[$linenr - 1] =~
2791 s/^(\+.*?)\s+\[/$1\[/;
2792 }
2489 } 2793 }
2490 } 2794 }
2491 2795
@@ -2502,7 +2806,6 @@ sub process {
2502 __attribute__|format|__extension__| 2806 __attribute__|format|__extension__|
2503 asm|__asm__)$/x) 2807 asm|__asm__)$/x)
2504 { 2808 {
2505
2506 # cpp #define statements have non-optional spaces, ie 2809 # cpp #define statements have non-optional spaces, ie
2507 # if there is a space between the name and the open 2810 # if there is a space between the name and the open
2508 # parenthesis it is simply not a parameter group. 2811 # parenthesis it is simply not a parameter group.
@@ -2516,19 +2819,20 @@ sub process {
2516 } elsif ($ctx =~ /$Type$/) { 2819 } elsif ($ctx =~ /$Type$/) {
2517 2820
2518 } else { 2821 } else {
2519 WARN("SPACING", 2822 if (WARN("SPACING",
2520 "space prohibited between function name and open parenthesis '('\n" . $herecurr); 2823 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2824 $fix) {
2825 $fixed[$linenr - 1] =~
2826 s/\b$name\s+\(/$name\(/;
2827 }
2521 } 2828 }
2522 } 2829 }
2523 2830
2524# check for whitespace before a non-naked semicolon
2525 if ($line =~ /^\+.*\S\s+;/) {
2526 WARN("SPACING",
2527 "space prohibited before semicolon\n" . $herecurr);
2528 }
2529
2530# Check operator spacing. 2831# Check operator spacing.
2531 if (!($line=~/\#\s*include/)) { 2832 if (!($line=~/\#\s*include/)) {
2833 my $fixed_line = "";
2834 my $line_fixed = 0;
2835
2532 my $ops = qr{ 2836 my $ops = qr{
2533 <<=|>>=|<=|>=|==|!=| 2837 <<=|>>=|<=|>=|==|!=|
2534 \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 2838 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
@@ -2537,11 +2841,31 @@ sub process {
2537 \?|: 2841 \?|:
2538 }x; 2842 }x;
2539 my @elements = split(/($ops|;)/, $opline); 2843 my @elements = split(/($ops|;)/, $opline);
2844
2845## print("element count: <" . $#elements . ">\n");
2846## foreach my $el (@elements) {
2847## print("el: <$el>\n");
2848## }
2849
2850 my @fix_elements = ();
2540 my $off = 0; 2851 my $off = 0;
2541 2852
2853 foreach my $el (@elements) {
2854 push(@fix_elements, substr($rawline, $off, length($el)));
2855 $off += length($el);
2856 }
2857
2858 $off = 0;
2859
2542 my $blank = copy_spacing($opline); 2860 my $blank = copy_spacing($opline);
2861 my $last_after = -1;
2543 2862
2544 for (my $n = 0; $n < $#elements; $n += 2) { 2863 for (my $n = 0; $n < $#elements; $n += 2) {
2864
2865 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2866
2867## print("n: <$n> good: <$good>\n");
2868
2545 $off += length($elements[$n]); 2869 $off += length($elements[$n]);
2546 2870
2547 # Pick up the preceding and succeeding characters. 2871 # Pick up the preceding and succeeding characters.
@@ -2598,8 +2922,11 @@ sub process {
2598 } elsif ($op eq ';') { 2922 } elsif ($op eq ';') {
2599 if ($ctx !~ /.x[WEBC]/ && 2923 if ($ctx !~ /.x[WEBC]/ &&
2600 $cc !~ /^\\/ && $cc !~ /^;/) { 2924 $cc !~ /^\\/ && $cc !~ /^;/) {
2601 ERROR("SPACING", 2925 if (ERROR("SPACING",
2602 "space required after that '$op' $at\n" . $hereptr); 2926 "space required after that '$op' $at\n" . $hereptr)) {
2927 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
2928 $line_fixed = 1;
2929 }
2603 } 2930 }
2604 2931
2605 # // is a comment 2932 # // is a comment
@@ -2610,15 +2937,25 @@ sub process {
2610 # : when part of a bitfield 2937 # : when part of a bitfield
2611 } elsif ($op eq '->' || $opv eq ':B') { 2938 } elsif ($op eq '->' || $opv eq ':B') {
2612 if ($ctx =~ /Wx.|.xW/) { 2939 if ($ctx =~ /Wx.|.xW/) {
2613 ERROR("SPACING", 2940 if (ERROR("SPACING",
2614 "spaces prohibited around that '$op' $at\n" . $hereptr); 2941 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2942 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2943 if (defined $fix_elements[$n + 2]) {
2944 $fix_elements[$n + 2] =~ s/^\s+//;
2945 }
2946 $line_fixed = 1;
2947 }
2615 } 2948 }
2616 2949
2617 # , must have a space on the right. 2950 # , must have a space on the right.
2618 } elsif ($op eq ',') { 2951 } elsif ($op eq ',') {
2619 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 2952 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2620 ERROR("SPACING", 2953 if (ERROR("SPACING",
2621 "space required after that '$op' $at\n" . $hereptr); 2954 "space required after that '$op' $at\n" . $hereptr)) {
2955 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
2956 $line_fixed = 1;
2957 $last_after = $n;
2958 }
2622 } 2959 }
2623 2960
2624 # '*' as part of a type definition -- reported already. 2961 # '*' as part of a type definition -- reported already.
@@ -2632,34 +2969,56 @@ sub process {
2632 $opv eq '*U' || $opv eq '-U' || 2969 $opv eq '*U' || $opv eq '-U' ||
2633 $opv eq '&U' || $opv eq '&&U') { 2970 $opv eq '&U' || $opv eq '&&U') {
2634 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2971 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2635 ERROR("SPACING", 2972 if (ERROR("SPACING",
2636 "space required before that '$op' $at\n" . $hereptr); 2973 "space required before that '$op' $at\n" . $hereptr)) {
2974 if ($n != $last_after + 2) {
2975 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
2976 $line_fixed = 1;
2977 }
2978 }
2637 } 2979 }
2638 if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2980 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2639 # A unary '*' may be const 2981 # A unary '*' may be const
2640 2982
2641 } elsif ($ctx =~ /.xW/) { 2983 } elsif ($ctx =~ /.xW/) {
2642 ERROR("SPACING", 2984 if (ERROR("SPACING",
2643 "space prohibited after that '$op' $at\n" . $hereptr); 2985 "space prohibited after that '$op' $at\n" . $hereptr)) {
2986 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
2987 if (defined $fix_elements[$n + 2]) {
2988 $fix_elements[$n + 2] =~ s/^\s+//;
2989 }
2990 $line_fixed = 1;
2991 }
2644 } 2992 }
2645 2993
2646 # unary ++ and unary -- are allowed no space on one side. 2994 # unary ++ and unary -- are allowed no space on one side.
2647 } elsif ($op eq '++' or $op eq '--') { 2995 } elsif ($op eq '++' or $op eq '--') {
2648 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2996 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2649 ERROR("SPACING", 2997 if (ERROR("SPACING",
2650 "space required one side of that '$op' $at\n" . $hereptr); 2998 "space required one side of that '$op' $at\n" . $hereptr)) {
2999 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3000 $line_fixed = 1;
3001 }
2651 } 3002 }
2652 if ($ctx =~ /Wx[BE]/ || 3003 if ($ctx =~ /Wx[BE]/ ||
2653 ($ctx =~ /Wx./ && $cc =~ /^;/)) { 3004 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2654 ERROR("SPACING", 3005 if (ERROR("SPACING",
2655 "space prohibited before that '$op' $at\n" . $hereptr); 3006 "space prohibited before that '$op' $at\n" . $hereptr)) {
3007 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3008 $line_fixed = 1;
3009 }
2656 } 3010 }
2657 if ($ctx =~ /ExW/) { 3011 if ($ctx =~ /ExW/) {
2658 ERROR("SPACING", 3012 if (ERROR("SPACING",
2659 "space prohibited after that '$op' $at\n" . $hereptr); 3013 "space prohibited after that '$op' $at\n" . $hereptr)) {
3014 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3015 if (defined $fix_elements[$n + 2]) {
3016 $fix_elements[$n + 2] =~ s/^\s+//;
3017 }
3018 $line_fixed = 1;
3019 }
2660 } 3020 }
2661 3021
2662
2663 # << and >> may either have or not have spaces both sides 3022 # << and >> may either have or not have spaces both sides
2664 } elsif ($op eq '<<' or $op eq '>>' or 3023 } elsif ($op eq '<<' or $op eq '>>' or
2665 $op eq '&' or $op eq '^' or $op eq '|' or 3024 $op eq '&' or $op eq '^' or $op eq '|' or
@@ -2668,17 +3027,25 @@ sub process {
2668 $op eq '%') 3027 $op eq '%')
2669 { 3028 {
2670 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 3029 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2671 ERROR("SPACING", 3030 if (ERROR("SPACING",
2672 "need consistent spacing around '$op' $at\n" . 3031 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2673 $hereptr); 3032 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3033 if (defined $fix_elements[$n + 2]) {
3034 $fix_elements[$n + 2] =~ s/^\s+//;
3035 }
3036 $line_fixed = 1;
3037 }
2674 } 3038 }
2675 3039
2676 # A colon needs no spaces before when it is 3040 # A colon needs no spaces before when it is
2677 # terminating a case value or a label. 3041 # terminating a case value or a label.
2678 } elsif ($opv eq ':C' || $opv eq ':L') { 3042 } elsif ($opv eq ':C' || $opv eq ':L') {
2679 if ($ctx =~ /Wx./) { 3043 if ($ctx =~ /Wx./) {
2680 ERROR("SPACING", 3044 if (ERROR("SPACING",
2681 "space prohibited before that '$op' $at\n" . $hereptr); 3045 "space prohibited before that '$op' $at\n" . $hereptr)) {
3046 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3047 $line_fixed = 1;
3048 }
2682 } 3049 }
2683 3050
2684 # All the others need spaces both sides. 3051 # All the others need spaces both sides.
@@ -2701,11 +3068,41 @@ sub process {
2701 } 3068 }
2702 3069
2703 if ($ok == 0) { 3070 if ($ok == 0) {
2704 ERROR("SPACING", 3071 if (ERROR("SPACING",
2705 "spaces required around that '$op' $at\n" . $hereptr); 3072 "spaces required around that '$op' $at\n" . $hereptr)) {
3073 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3074 if (defined $fix_elements[$n + 2]) {
3075 $fix_elements[$n + 2] =~ s/^\s+//;
3076 }
3077 $line_fixed = 1;
3078 }
2706 } 3079 }
2707 } 3080 }
2708 $off += length($elements[$n + 1]); 3081 $off += length($elements[$n + 1]);
3082
3083## print("n: <$n> GOOD: <$good>\n");
3084
3085 $fixed_line = $fixed_line . $good;
3086 }
3087
3088 if (($#elements % 2) == 0) {
3089 $fixed_line = $fixed_line . $fix_elements[$#elements];
3090 }
3091
3092 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
3093 $fixed[$linenr - 1] = $fixed_line;
3094 }
3095
3096
3097 }
3098
3099# check for whitespace before a non-naked semicolon
3100 if ($line =~ /^\+.*\S\s+;/) {
3101 if (WARN("SPACING",
3102 "space prohibited before semicolon\n" . $herecurr) &&
3103 $fix) {
3104 1 while $fixed[$linenr - 1] =~
3105 s/^(\+.*\S)\s+;/$1;/;
2709 } 3106 }
2710 } 3107 }
2711 3108
@@ -2734,45 +3131,80 @@ sub process {
2734#need space before brace following if, while, etc 3131#need space before brace following if, while, etc
2735 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 3132 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2736 $line =~ /do{/) { 3133 $line =~ /do{/) {
2737 ERROR("SPACING", 3134 if (ERROR("SPACING",
2738 "space required before the open brace '{'\n" . $herecurr); 3135 "space required before the open brace '{'\n" . $herecurr) &&
3136 $fix) {
3137 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
3138 }
2739 } 3139 }
2740 3140
3141## # check for blank lines before declarations
3142## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3143## $prevrawline =~ /^.\s*$/) {
3144## WARN("SPACING",
3145## "No blank lines before declarations\n" . $hereprev);
3146## }
3147##
3148
2741# closing brace should have a space following it when it has anything 3149# closing brace should have a space following it when it has anything
2742# on the line 3150# on the line
2743 if ($line =~ /}(?!(?:,|;|\)))\S/) { 3151 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2744 ERROR("SPACING", 3152 if (ERROR("SPACING",
2745 "space required after that close brace '}'\n" . $herecurr); 3153 "space required after that close brace '}'\n" . $herecurr) &&
3154 $fix) {
3155 $fixed[$linenr - 1] =~
3156 s/}((?!(?:,|;|\)))\S)/} $1/;
3157 }
2746 } 3158 }
2747 3159
2748# check spacing on square brackets 3160# check spacing on square brackets
2749 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 3161 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2750 ERROR("SPACING", 3162 if (ERROR("SPACING",
2751 "space prohibited after that open square bracket '['\n" . $herecurr); 3163 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3164 $fix) {
3165 $fixed[$linenr - 1] =~
3166 s/\[\s+/\[/;
3167 }
2752 } 3168 }
2753 if ($line =~ /\s\]/) { 3169 if ($line =~ /\s\]/) {
2754 ERROR("SPACING", 3170 if (ERROR("SPACING",
2755 "space prohibited before that close square bracket ']'\n" . $herecurr); 3171 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3172 $fix) {
3173 $fixed[$linenr - 1] =~
3174 s/\s+\]/\]/;
3175 }
2756 } 3176 }
2757 3177
2758# check spacing on parentheses 3178# check spacing on parentheses
2759 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 3179 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2760 $line !~ /for\s*\(\s+;/) { 3180 $line !~ /for\s*\(\s+;/) {
2761 ERROR("SPACING", 3181 if (ERROR("SPACING",
2762 "space prohibited after that open parenthesis '('\n" . $herecurr); 3182 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3183 $fix) {
3184 $fixed[$linenr - 1] =~
3185 s/\(\s+/\(/;
3186 }
2763 } 3187 }
2764 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 3188 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2765 $line !~ /for\s*\(.*;\s+\)/ && 3189 $line !~ /for\s*\(.*;\s+\)/ &&
2766 $line !~ /:\s+\)/) { 3190 $line !~ /:\s+\)/) {
2767 ERROR("SPACING", 3191 if (ERROR("SPACING",
2768 "space prohibited before that close parenthesis ')'\n" . $herecurr); 3192 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3193 $fix) {
3194 $fixed[$linenr - 1] =~
3195 s/\s+\)/\)/;
3196 }
2769 } 3197 }
2770 3198
2771#goto labels aren't indented, allow a single space however 3199#goto labels aren't indented, allow a single space however
2772 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 3200 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2773 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 3201 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2774 WARN("INDENTED_LABEL", 3202 if (WARN("INDENTED_LABEL",
2775 "labels should not be indented\n" . $herecurr); 3203 "labels should not be indented\n" . $herecurr) &&
3204 $fix) {
3205 $fixed[$linenr - 1] =~
3206 s/^(.)\s+/$1/;
3207 }
2776 } 3208 }
2777 3209
2778# Return is not a function. 3210# Return is not a function.
@@ -2809,8 +3241,13 @@ sub process {
2809 } 3241 }
2810 3242
2811# Need a space before open parenthesis after if, while etc 3243# Need a space before open parenthesis after if, while etc
2812 if ($line=~/\b(if|while|for|switch)\(/) { 3244 if ($line =~ /\b(if|while|for|switch)\(/) {
2813 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); 3245 if (ERROR("SPACING",
3246 "space required before the open parenthesis '('\n" . $herecurr) &&
3247 $fix) {
3248 $fixed[$linenr - 1] =~
3249 s/\b(if|while|for|switch)\(/$1 \(/;
3250 }
2814 } 3251 }
2815 3252
2816# Check for illegal assignment in if conditional -- and check for trailing 3253# Check for illegal assignment in if conditional -- and check for trailing
@@ -2934,23 +3371,48 @@ sub process {
2934 } 3371 }
2935 } 3372 }
2936 3373
2937#CamelCase 3374#Specific variable tests
2938 while ($line =~ m{($Constant|$Lval)}g) { 3375 while ($line =~ m{($Constant|$Lval)}g) {
2939 my $var = $1; 3376 my $var = $1;
2940 if ($var !~ /$Constant/ && 3377
2941 $var =~ /[A-Z]\w*[a-z]|[a-z]\w*[A-Z]/ && 3378#gcc binary extension
2942 $var !~ /"^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 3379 if ($var =~ /^$Binary$/) {
2943 !defined $camelcase{$var}) { 3380 if (WARN("GCC_BINARY_CONSTANT",
2944 $camelcase{$var} = 1; 3381 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
2945 WARN("CAMELCASE", 3382 $fix) {
2946 "Avoid CamelCase: <$var>\n" . $herecurr); 3383 my $hexval = sprintf("0x%x", oct($var));
3384 $fixed[$linenr - 1] =~
3385 s/\b$var\b/$hexval/;
3386 }
3387 }
3388
3389#CamelCase
3390 if ($var !~ /^$Constant$/ &&
3391 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
3392#Ignore Page<foo> variants
3393 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
3394#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
3395 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3396 while ($var =~ m{($Ident)}g) {
3397 my $word = $1;
3398 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3399 seed_camelcase_includes() if ($check);
3400 if (!defined $camelcase{$word}) {
3401 $camelcase{$word} = 1;
3402 CHK("CAMELCASE",
3403 "Avoid CamelCase: <$word>\n" . $herecurr);
3404 }
3405 }
2947 } 3406 }
2948 } 3407 }
2949 3408
2950#no spaces allowed after \ in define 3409#no spaces allowed after \ in define
2951 if ($line=~/\#\s*define.*\\\s$/) { 3410 if ($line =~ /\#\s*define.*\\\s+$/) {
2952 WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 3411 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
2953 "Whitepspace after \\ makes next lines useless\n" . $herecurr); 3412 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3413 $fix) {
3414 $fixed[$linenr - 1] =~ s/\s+$//;
3415 }
2954 } 3416 }
2955 3417
2956#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 3418#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
@@ -3021,7 +3483,7 @@ sub process {
3021 if ($dstat ne '' && 3483 if ($dstat ne '' &&
3022 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 3484 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3023 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 3485 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
3024 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo 3486 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3025 $dstat !~ /^'X'$/ && # character constants 3487 $dstat !~ /^'X'$/ && # character constants
3026 $dstat !~ /$exceptions/ && 3488 $dstat !~ /$exceptions/ &&
3027 $dstat !~ /^\.$Ident\s*=/ && # .foo = 3489 $dstat !~ /^\.$Ident\s*=/ && # .foo =
@@ -3030,7 +3492,8 @@ sub process {
3030 $dstat !~ /^for\s*$Constant$/ && # for (...) 3492 $dstat !~ /^for\s*$Constant$/ && # for (...)
3031 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 3493 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3032 $dstat !~ /^do\s*{/ && # do {... 3494 $dstat !~ /^do\s*{/ && # do {...
3033 $dstat !~ /^\({/) # ({... 3495 $dstat !~ /^\({/ && # ({...
3496 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
3034 { 3497 {
3035 $ctx =~ s/\n*$//; 3498 $ctx =~ s/\n*$//;
3036 my $herectx = $here . "\n"; 3499 my $herectx = $here . "\n";
@@ -3230,11 +3693,11 @@ sub process {
3230 } 3693 }
3231 3694
3232# check for unnecessary blank lines around braces 3695# check for unnecessary blank lines around braces
3233 if (($line =~ /^.\s*}\s*$/ && $prevline =~ /^.\s*$/)) { 3696 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
3234 CHK("BRACES", 3697 CHK("BRACES",
3235 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); 3698 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3236 } 3699 }
3237 if (($line =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 3700 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3238 CHK("BRACES", 3701 CHK("BRACES",
3239 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); 3702 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3240 } 3703 }
@@ -3262,6 +3725,32 @@ sub process {
3262 } 3725 }
3263 } 3726 }
3264 3727
3728sub string_find_replace {
3729 my ($string, $find, $replace) = @_;
3730
3731 $string =~ s/$find/$replace/g;
3732
3733 return $string;
3734}
3735
3736# check for bad placement of section $InitAttribute (e.g.: __initdata)
3737 if ($line =~ /(\b$InitAttribute\b)/) {
3738 my $attr = $1;
3739 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
3740 my $ptr = $1;
3741 my $var = $2;
3742 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
3743 ERROR("MISPLACED_INIT",
3744 "$attr should be placed after $var\n" . $herecurr)) ||
3745 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
3746 WARN("MISPLACED_INIT",
3747 "$attr should be placed after $var\n" . $herecurr))) &&
3748 $fix) {
3749 $fixed[$linenr - 1] =~ 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;
3750 }
3751 }
3752 }
3753
3265# prefer usleep_range over udelay 3754# prefer usleep_range over udelay
3266 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 3755 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3267 # ignore udelay's < 10, however 3756 # ignore udelay's < 10, however
@@ -3279,6 +3768,18 @@ sub process {
3279 } 3768 }
3280 } 3769 }
3281 3770
3771# check for comparisons of jiffies
3772 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3773 WARN("JIFFIES_COMPARISON",
3774 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3775 }
3776
3777# check for comparisons of get_jiffies_64()
3778 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3779 WARN("JIFFIES_COMPARISON",
3780 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3781 }
3782
3282# warn about #ifdefs in C files 3783# warn about #ifdefs in C files
3283# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 3784# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3284# print "#ifdef in C files should be avoided\n"; 3785# print "#ifdef in C files should be avoided\n";
@@ -3288,8 +3789,13 @@ sub process {
3288 3789
3289# warn about spacing in #ifdefs 3790# warn about spacing in #ifdefs
3290 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 3791 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3291 ERROR("SPACING", 3792 if (ERROR("SPACING",
3292 "exactly one space required after that #$1\n" . $herecurr); 3793 "exactly one space required after that #$1\n" . $herecurr) &&
3794 $fix) {
3795 $fixed[$linenr - 1] =~
3796 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3797 }
3798
3293 } 3799 }
3294 3800
3295# check for spinlock_t definitions without a comment. 3801# check for spinlock_t definitions without a comment.
@@ -3330,8 +3836,12 @@ sub process {
3330 3836
3331# Check for __inline__ and __inline, prefer inline 3837# Check for __inline__ and __inline, prefer inline
3332 if ($line =~ /\b(__inline__|__inline)\b/) { 3838 if ($line =~ /\b(__inline__|__inline)\b/) {
3333 WARN("INLINE", 3839 if (WARN("INLINE",
3334 "plain inline is preferred over $1\n" . $herecurr); 3840 "plain inline is preferred over $1\n" . $herecurr) &&
3841 $fix) {
3842 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3843
3844 }
3335 } 3845 }
3336 3846
3337# Check for __attribute__ packed, prefer __packed 3847# Check for __attribute__ packed, prefer __packed
@@ -3348,14 +3858,21 @@ sub process {
3348 3858
3349# Check for __attribute__ format(printf, prefer __printf 3859# Check for __attribute__ format(printf, prefer __printf
3350 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 3860 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3351 WARN("PREFER_PRINTF", 3861 if (WARN("PREFER_PRINTF",
3352 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr); 3862 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3863 $fix) {
3864 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3865
3866 }
3353 } 3867 }
3354 3868
3355# Check for __attribute__ format(scanf, prefer __scanf 3869# Check for __attribute__ format(scanf, prefer __scanf
3356 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 3870 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3357 WARN("PREFER_SCANF", 3871 if (WARN("PREFER_SCANF",
3358 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr); 3872 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3873 $fix) {
3874 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3875 }
3359 } 3876 }
3360 3877
3361# check for sizeof(&) 3878# check for sizeof(&)
@@ -3366,8 +3883,11 @@ sub process {
3366 3883
3367# check for sizeof without parenthesis 3884# check for sizeof without parenthesis
3368 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 3885 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3369 WARN("SIZEOF_PARENTHESIS", 3886 if (WARN("SIZEOF_PARENTHESIS",
3370 "sizeof $1 should be sizeof($1)\n" . $herecurr); 3887 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3888 $fix) {
3889 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3890 }
3371 } 3891 }
3372 3892
3373# check for line continuations in quoted strings with odd counts of " 3893# check for line continuations in quoted strings with odd counts of "
@@ -3386,8 +3906,11 @@ sub process {
3386 if ($line =~ /\bseq_printf\s*\(/) { 3906 if ($line =~ /\bseq_printf\s*\(/) {
3387 my $fmt = get_quoted_string($line, $rawline); 3907 my $fmt = get_quoted_string($line, $rawline);
3388 if ($fmt !~ /[^\\]\%/) { 3908 if ($fmt !~ /[^\\]\%/) {
3389 WARN("PREFER_SEQ_PUTS", 3909 if (WARN("PREFER_SEQ_PUTS",
3390 "Prefer seq_puts to seq_printf\n" . $herecurr); 3910 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3911 $fix) {
3912 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3913 }
3391 } 3914 }
3392 } 3915 }
3393 3916
@@ -3449,6 +3972,16 @@ sub process {
3449 } 3972 }
3450 } 3973 }
3451 3974
3975# check for new externs in .h files.
3976 if ($realfile =~ /\.h$/ &&
3977 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
3978 if (CHK("AVOID_EXTERNS",
3979 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
3980 $fix) {
3981 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3982 }
3983 }
3984
3452# check for new externs in .c files. 3985# check for new externs in .c files.
3453 if ($realfile =~ /\.c$/ && defined $stat && 3986 if ($realfile =~ /\.c$/ && defined $stat &&
3454 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3987 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
@@ -3495,6 +4028,14 @@ sub process {
3495 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 4028 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3496 } 4029 }
3497 4030
4031# alloc style
4032# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4033 if ($^V && $^V ge 5.10.0 &&
4034 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4035 CHK("ALLOC_SIZEOF_STRUCT",
4036 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4037 }
4038
3498# check for krealloc arg reuse 4039# check for krealloc arg reuse
3499 if ($^V && $^V ge 5.10.0 && 4040 if ($^V && $^V ge 5.10.0 &&
3500 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 4041 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
@@ -3510,8 +4051,11 @@ sub process {
3510 4051
3511# check for multiple semicolons 4052# check for multiple semicolons
3512 if ($line =~ /;\s*;\s*$/) { 4053 if ($line =~ /;\s*;\s*$/) {
3513 WARN("ONE_SEMICOLON", 4054 if (WARN("ONE_SEMICOLON",
3514 "Statements terminations use 1 semicolon\n" . $herecurr); 4055 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4056 $fix) {
4057 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4058 }
3515 } 4059 }
3516 4060
3517# check for switch/default statements without a break; 4061# check for switch/default statements without a break;
@@ -3529,9 +4073,12 @@ sub process {
3529 } 4073 }
3530 4074
3531# check for gcc specific __FUNCTION__ 4075# check for gcc specific __FUNCTION__
3532 if ($line =~ /__FUNCTION__/) { 4076 if ($line =~ /\b__FUNCTION__\b/) {
3533 WARN("USE_FUNC", 4077 if (WARN("USE_FUNC",
3534 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 4078 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4079 $fix) {
4080 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4081 }
3535 } 4082 }
3536 4083
3537# check for use of yield() 4084# check for use of yield()
@@ -3540,6 +4087,33 @@ sub process {
3540 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 4087 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3541 } 4088 }
3542 4089
4090# check for comparisons against true and false
4091 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4092 my $lead = $1;
4093 my $arg = $2;
4094 my $test = $3;
4095 my $otype = $4;
4096 my $trail = $5;
4097 my $op = "!";
4098
4099 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4100
4101 my $type = lc($otype);
4102 if ($type =~ /^(?:true|false)$/) {
4103 if (("$test" eq "==" && "$type" eq "true") ||
4104 ("$test" eq "!=" && "$type" eq "false")) {
4105 $op = "";
4106 }
4107
4108 CHK("BOOL_COMPARISON",
4109 "Using comparison to $otype is error prone\n" . $herecurr);
4110
4111## maybe suggesting a correct construct would better
4112## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4113
4114 }
4115 }
4116
3543# check for semaphores initialized locked 4117# check for semaphores initialized locked
3544 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 4118 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3545 WARN("CONSIDER_COMPLETION", 4119 WARN("CONSIDER_COMPLETION",
@@ -3709,12 +4283,41 @@ sub process {
3709 } 4283 }
3710 } 4284 }
3711 4285
3712 if ($quiet == 0 && keys %ignore_type) { 4286 hash_show_words(\%use_type, "Used");
3713 print "NOTE: Ignored message types:"; 4287 hash_show_words(\%ignore_type, "Ignored");
3714 foreach my $ignore (sort keys %ignore_type) { 4288
3715 print " $ignore"; 4289 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
3716 } 4290 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
3717 print "\n\n"; 4291 my $linecount = 0;
4292 my $f;
4293
4294 open($f, '>', $newfile)
4295 or die "$P: Can't open $newfile for write\n";
4296 foreach my $fixed_line (@fixed) {
4297 $linecount++;
4298 if ($file) {
4299 if ($linecount > 3) {
4300 $fixed_line =~ s/^\+//;
4301 print $f $fixed_line. "\n";
4302 }
4303 } else {
4304 print $f $fixed_line . "\n";
4305 }
4306 }
4307 close($f);
4308
4309 if (!$quiet) {
4310 print << "EOM";
4311Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4312
4313Do _NOT_ trust the results written to this file.
4314Do _NOT_ submit these changes without inspecting them for correctness.
4315
4316This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4317No warranties, expressed or implied...
4318
4319EOM
4320 }
3718 } 4321 }
3719 4322
3720 if ($clean == 1 && $quiet == 0) { 4323 if ($clean == 1 && $quiet == 0) {