aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl652
1 files changed, 541 insertions, 111 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b954de58304f..2ee9eb750560 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,9 +28,11 @@ 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;
32my %ignore_type = (); 34my %ignore_type = ();
35my %camelcase = ();
33my @ignore = (); 36my @ignore = ();
34my $help = 0; 37my $help = 0;
35my $configuration_file = ".checkpatch.conf"; 38my $configuration_file = ".checkpatch.conf";
@@ -63,6 +66,11 @@ Options:
63 is all off) 66 is all off)
64 --test-only=WORD report only warnings/errors containing WORD 67 --test-only=WORD report only warnings/errors containing WORD
65 literally 68 literally
69 --fix EXPERIMENTAL - may create horrible results
70 If correctable single-line errors exist, create
71 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
72 with potential errors corrected to the preferred
73 checkpatch style
66 -h, --help, --version display this help and exit 74 -h, --help, --version display this help and exit
67 75
68When FILE is - read standard input. 76When FILE is - read standard input.
@@ -114,7 +122,7 @@ GetOptions(
114 'summary!' => \$summary, 122 'summary!' => \$summary,
115 'mailback!' => \$mailback, 123 'mailback!' => \$mailback,
116 'summary-file!' => \$summary_file, 124 'summary-file!' => \$summary_file,
117 125 'fix!' => \$fix,
118 'debug=s' => \%debug, 126 'debug=s' => \%debug,
119 'test-only=s' => \$tst_only, 127 'test-only=s' => \$tst_only,
120 'h|help' => \$help, 128 'h|help' => \$help,
@@ -230,17 +238,22 @@ our $Inline = qr{inline|__always_inline|noinline};
230our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 238our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
231our $Lval = qr{$Ident(?:$Member)*}; 239our $Lval = qr{$Ident(?:$Member)*};
232 240
241our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
242our $Binary = qr{(?i)0b[01]+$Int_type?};
243our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
244our $Int = qr{[0-9]+$Int_type?};
233our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 245our $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]?}; 246our $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]?}; 247our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
236our $Float = qr{$Float_hex|$Float_dec|$Float_int}; 248our $Float = qr{$Float_hex|$Float_dec|$Float_int};
237our $Constant = qr{$Float|(?i)(?:0x[0-9a-f]+|[0-9]+)[ul]*}; 249our $Constant = qr{$Float|$Binary|$Hex|$Int};
238our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 250our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
239our $Compare = qr{<=|>=|==|!=|<|>}; 251our $Compare = qr{<=|>=|==|!=|<|>};
252our $Arithmetic = qr{\+|-|\*|\/|%};
240our $Operators = qr{ 253our $Operators = qr{
241 <=|>=|==|!=| 254 <=|>=|==|!=|
242 =>|->|<<|>>|<|>|!|~| 255 =>|->|<<|>>|<|>|!|~|
243 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 256 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
244 }x; 257 }x;
245 258
246our $NonptrType; 259our $NonptrType;
@@ -269,7 +282,7 @@ our $typeTypedefs = qr{(?x:
269 282
270our $logFunctions = qr{(?x: 283our $logFunctions = qr{(?x:
271 printk(?:_ratelimited|_once|)| 284 printk(?:_ratelimited|_once|)|
272 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 285 (?:[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|)| 286 WARN(?:_RATELIMIT|_ONCE|)|
274 panic| 287 panic|
275 MODULE_[A-Z_]+ 288 MODULE_[A-Z_]+
@@ -338,7 +351,6 @@ sub build_types {
338} 351}
339build_types(); 352build_types();
340 353
341
342our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 354our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
343 355
344# Using $balanced_parens, $LvalOrFunc, or $FuncArg 356# Using $balanced_parens, $LvalOrFunc, or $FuncArg
@@ -358,10 +370,94 @@ sub deparenthesize {
358 return $string; 370 return $string;
359} 371}
360 372
373sub seed_camelcase_file {
374 my ($file) = @_;
375
376 return if (!(-f $file));
377
378 local $/;
379
380 open(my $include_file, '<', "$file")
381 or warn "$P: Can't read '$file' $!\n";
382 my $text = <$include_file>;
383 close($include_file);
384
385 my @lines = split('\n', $text);
386
387 foreach my $line (@lines) {
388 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
389 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
390 $camelcase{$1} = 1;
391 }
392 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
393 $camelcase{$1} = 1;
394 }
395 }
396}
397
398my $camelcase_seeded = 0;
399sub seed_camelcase_includes {
400 return if ($camelcase_seeded);
401
402 my $files;
403 my $camelcase_cache = "";
404 my @include_files = ();
405
406 $camelcase_seeded = 1;
407
408 if (-d ".git") {
409 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
410 chomp $git_last_include_commit;
411 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
412 } else {
413 my $last_mod_date = 0;
414 $files = `find $root/include -name "*.h"`;
415 @include_files = split('\n', $files);
416 foreach my $file (@include_files) {
417 my $date = POSIX::strftime("%Y%m%d%H%M",
418 localtime((stat $file)[9]));
419 $last_mod_date = $date if ($last_mod_date < $date);
420 }
421 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
422 }
423
424 if ($camelcase_cache ne "" && -f $camelcase_cache) {
425 open(my $camelcase_file, '<', "$camelcase_cache")
426 or warn "$P: Can't read '$camelcase_cache' $!\n";
427 while (<$camelcase_file>) {
428 chomp;
429 $camelcase{$_} = 1;
430 }
431 close($camelcase_file);
432
433 return;
434 }
435
436 if (-d ".git") {
437 $files = `git ls-files "include/*.h"`;
438 @include_files = split('\n', $files);
439 }
440
441 foreach my $file (@include_files) {
442 seed_camelcase_file($file);
443 }
444
445 if ($camelcase_cache ne "") {
446 unlink glob ".checkpatch-camelcase.*";
447 open(my $camelcase_file, '>', "$camelcase_cache")
448 or warn "$P: Can't write '$camelcase_cache' $!\n";
449 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
450 print $camelcase_file ("$_\n");
451 }
452 close($camelcase_file);
453 }
454}
455
361$chk_signoff = 0 if ($file); 456$chk_signoff = 0 if ($file);
362 457
363my @rawlines = (); 458my @rawlines = ();
364my @lines = (); 459my @lines = ();
460my @fixed = ();
365my $vname; 461my $vname;
366for my $filename (@ARGV) { 462for my $filename (@ARGV) {
367 my $FILE; 463 my $FILE;
@@ -389,6 +485,7 @@ for my $filename (@ARGV) {
389 } 485 }
390 @rawlines = (); 486 @rawlines = ();
391 @lines = (); 487 @lines = ();
488 @fixed = ();
392} 489}
393 490
394exit($exit); 491exit($exit);
@@ -429,7 +526,7 @@ sub parse_email {
429 $comment = $2 if defined $2; 526 $comment = $2 if defined $2;
430 $formatted_email =~ s/$address.*$//; 527 $formatted_email =~ s/$address.*$//;
431 $name = $formatted_email; 528 $name = $formatted_email;
432 $name =~ s/^\s+|\s+$//g; 529 $name = trim($name);
433 $name =~ s/^\"|\"$//g; 530 $name =~ s/^\"|\"$//g;
434 # If there's a name left after stripping spaces and 531 # If there's a name left after stripping spaces and
435 # leading quotes, and the address doesn't have both 532 # leading quotes, and the address doesn't have both
@@ -444,9 +541,9 @@ sub parse_email {
444 } 541 }
445 } 542 }
446 543
447 $name =~ s/^\s+|\s+$//g; 544 $name = trim($name);
448 $name =~ s/^\"|\"$//g; 545 $name =~ s/^\"|\"$//g;
449 $address =~ s/^\s+|\s+$//g; 546 $address = trim($address);
450 $address =~ s/^\<|\>$//g; 547 $address =~ s/^\<|\>$//g;
451 548
452 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 549 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
@@ -462,9 +559,9 @@ sub format_email {
462 559
463 my $formatted_email; 560 my $formatted_email;
464 561
465 $name =~ s/^\s+|\s+$//g; 562 $name = trim($name);
466 $name =~ s/^\"|\"$//g; 563 $name =~ s/^\"|\"$//g;
467 $address =~ s/^\s+|\s+$//g; 564 $address = trim($address);
468 565
469 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 566 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
470 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 567 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
@@ -1286,19 +1383,25 @@ sub ERROR {
1286 if (report("ERROR", $_[0], $_[1])) { 1383 if (report("ERROR", $_[0], $_[1])) {
1287 our $clean = 0; 1384 our $clean = 0;
1288 our $cnt_error++; 1385 our $cnt_error++;
1386 return 1;
1289 } 1387 }
1388 return 0;
1290} 1389}
1291sub WARN { 1390sub WARN {
1292 if (report("WARNING", $_[0], $_[1])) { 1391 if (report("WARNING", $_[0], $_[1])) {
1293 our $clean = 0; 1392 our $clean = 0;
1294 our $cnt_warn++; 1393 our $cnt_warn++;
1394 return 1;
1295 } 1395 }
1396 return 0;
1296} 1397}
1297sub CHK { 1398sub CHK {
1298 if ($check && report("CHECK", $_[0], $_[1])) { 1399 if ($check && report("CHECK", $_[0], $_[1])) {
1299 our $clean = 0; 1400 our $clean = 0;
1300 our $cnt_chk++; 1401 our $cnt_chk++;
1402 return 1;
1301 } 1403 }
1404 return 0;
1302} 1405}
1303 1406
1304sub check_absolute_file { 1407sub check_absolute_file {
@@ -1329,6 +1432,29 @@ sub check_absolute_file {
1329 } 1432 }
1330} 1433}
1331 1434
1435sub trim {
1436 my ($string) = @_;
1437
1438 $string =~ s/(^\s+|\s+$)//g;
1439
1440 return $string;
1441}
1442
1443sub tabify {
1444 my ($leading) = @_;
1445
1446 my $source_indent = 8;
1447 my $max_spaces_before_tab = $source_indent - 1;
1448 my $spaces_to_tab = " " x $source_indent;
1449
1450 #convert leading spaces to tabs
1451 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1452 #Remove spaces before a tab
1453 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1454
1455 return "$leading";
1456}
1457
1332sub pos_last_openparen { 1458sub pos_last_openparen {
1333 my ($line) = @_; 1459 my ($line) = @_;
1334 1460
@@ -1406,7 +1532,6 @@ sub process {
1406 my %suppress_export; 1532 my %suppress_export;
1407 my $suppress_statement = 0; 1533 my $suppress_statement = 0;
1408 1534
1409 my %camelcase = ();
1410 1535
1411 # Pre-scan the patch sanitizing the lines. 1536 # Pre-scan the patch sanitizing the lines.
1412 # Pre-scan the patch looking for any __setup documentation. 1537 # Pre-scan the patch looking for any __setup documentation.
@@ -1420,6 +1545,8 @@ sub process {
1420 $linenr++; 1545 $linenr++;
1421 $line = $rawline; 1546 $line = $rawline;
1422 1547
1548 push(@fixed, $rawline) if ($fix);
1549
1423 if ($rawline=~/^\+\+\+\s+(\S+)/) { 1550 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1424 $setup_docs = 0; 1551 $setup_docs = 0;
1425 if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1552 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
@@ -1611,16 +1738,29 @@ sub process {
1611 "Non-standard signature: $sign_off\n" . $herecurr); 1738 "Non-standard signature: $sign_off\n" . $herecurr);
1612 } 1739 }
1613 if (defined $space_before && $space_before ne "") { 1740 if (defined $space_before && $space_before ne "") {
1614 WARN("BAD_SIGN_OFF", 1741 if (WARN("BAD_SIGN_OFF",
1615 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr); 1742 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1743 $fix) {
1744 $fixed[$linenr - 1] =
1745 "$ucfirst_sign_off $email";
1746 }
1616 } 1747 }
1617 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 1748 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1618 WARN("BAD_SIGN_OFF", 1749 if (WARN("BAD_SIGN_OFF",
1619 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr); 1750 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1751 $fix) {
1752 $fixed[$linenr - 1] =
1753 "$ucfirst_sign_off $email";
1754 }
1755
1620 } 1756 }
1621 if (!defined $space_after || $space_after ne " ") { 1757 if (!defined $space_after || $space_after ne " ") {
1622 WARN("BAD_SIGN_OFF", 1758 if (WARN("BAD_SIGN_OFF",
1623 "Use a single space after $ucfirst_sign_off\n" . $herecurr); 1759 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1760 $fix) {
1761 $fixed[$linenr - 1] =
1762 "$ucfirst_sign_off $email";
1763 }
1624 } 1764 }
1625 1765
1626 my ($email_name, $email_address, $comment) = parse_email($email); 1766 my ($email_name, $email_address, $comment) = parse_email($email);
@@ -1710,8 +1850,12 @@ sub process {
1710 1850
1711 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1851 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1712 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1852 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1713 ERROR("TRAILING_WHITESPACE", 1853 if (ERROR("TRAILING_WHITESPACE",
1714 "trailing whitespace\n" . $herevet); 1854 "trailing whitespace\n" . $herevet) &&
1855 $fix) {
1856 $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
1857 }
1858
1715 $rpt_cleaners = 1; 1859 $rpt_cleaners = 1;
1716 } 1860 }
1717 1861
@@ -1806,8 +1950,12 @@ sub process {
1806 1950
1807# check for spaces before a quoted newline 1951# check for spaces before a quoted newline
1808 if ($rawline =~ /^.*\".*\s\\n/) { 1952 if ($rawline =~ /^.*\".*\s\\n/) {
1809 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 1953 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1810 "unnecessary whitespace before a quoted newline\n" . $herecurr); 1954 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
1955 $fix) {
1956 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
1957 }
1958
1811 } 1959 }
1812 1960
1813# check for adding lines without a newline. 1961# check for adding lines without a newline.
@@ -1838,16 +1986,23 @@ sub process {
1838 if ($rawline =~ /^\+\s* \t\s*\S/ || 1986 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1839 $rawline =~ /^\+\s* \s*/) { 1987 $rawline =~ /^\+\s* \s*/) {
1840 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1988 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; 1989 $rpt_cleaners = 1;
1990 if (ERROR("CODE_INDENT",
1991 "code indent should use tabs where possible\n" . $herevet) &&
1992 $fix) {
1993 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
1994 }
1844 } 1995 }
1845 1996
1846# check for space before tabs. 1997# check for space before tabs.
1847 if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 1998 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1848 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1999 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1849 WARN("SPACE_BEFORE_TAB", 2000 if (WARN("SPACE_BEFORE_TAB",
1850 "please, no space before tabs\n" . $herevet); 2001 "please, no space before tabs\n" . $herevet) &&
2002 $fix) {
2003 $fixed[$linenr - 1] =~
2004 s/(^\+.*) +\t/$1\t/;
2005 }
1851 } 2006 }
1852 2007
1853# check for && or || at the start of a line 2008# check for && or || at the start of a line
@@ -1875,25 +2030,42 @@ sub process {
1875 2030
1876 if ($newindent ne $goodtabindent && 2031 if ($newindent ne $goodtabindent &&
1877 $newindent ne $goodspaceindent) { 2032 $newindent ne $goodspaceindent) {
1878 CHK("PARENTHESIS_ALIGNMENT", 2033
1879 "Alignment should match open parenthesis\n" . $hereprev); 2034 if (CHK("PARENTHESIS_ALIGNMENT",
2035 "Alignment should match open parenthesis\n" . $hereprev) &&
2036 $fix && $line =~ /^\+/) {
2037 $fixed[$linenr - 1] =~
2038 s/^\+[ \t]*/\+$goodtabindent/;
2039 }
1880 } 2040 }
1881 } 2041 }
1882 } 2042 }
1883 2043
1884 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) { 2044 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
1885 CHK("SPACING", 2045 if (CHK("SPACING",
1886 "No space is necessary after a cast\n" . $hereprev); 2046 "No space is necessary after a cast\n" . $hereprev) &&
2047 $fix) {
2048 $fixed[$linenr - 1] =~
2049 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
2050 }
1887 } 2051 }
1888 2052
1889 if ($realfile =~ m@^(drivers/net/|net/)@ && 2053 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1890 $rawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 2054 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1891 $prevrawline =~ /^\+[ \t]*$/) { 2055 $rawline =~ /^\+[ \t]*\*/) {
1892 WARN("NETWORKING_BLOCK_COMMENT_STYLE", 2056 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1893 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 2057 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1894 } 2058 }
1895 2059
1896 if ($realfile =~ m@^(drivers/net/|net/)@ && 2060 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2061 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2062 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2063 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2064 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2065 "networking block comments start with * on subsequent lines\n" . $hereprev);
2066 }
2067
2068 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1897 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2069 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1898 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2070 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1899 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2071 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
@@ -1907,10 +2079,13 @@ sub process {
1907# 1) within comments 2079# 1) within comments
1908# 2) indented preprocessor commands 2080# 2) indented preprocessor commands
1909# 3) hanging labels 2081# 3) hanging labels
1910 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { 2082 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
1911 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2083 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1912 WARN("LEADING_SPACE", 2084 if (WARN("LEADING_SPACE",
1913 "please, no spaces at the start of a line\n" . $herevet); 2085 "please, no spaces at the start of a line\n" . $herevet) &&
2086 $fix) {
2087 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2088 }
1914 } 2089 }
1915 2090
1916# check we are in a valid C source file if not then ignore this hunk 2091# check we are in a valid C source file if not then ignore this hunk
@@ -2200,7 +2375,7 @@ sub process {
2200 $prev_values = substr($curr_values, -1); 2375 $prev_values = substr($curr_values, -1);
2201 2376
2202#ignore lines not being added 2377#ignore lines not being added
2203 if ($line=~/^[^\+]/) {next;} 2378 next if ($line =~ /^[^\+]/);
2204 2379
2205# TEST: allow direct testing of the type matcher. 2380# TEST: allow direct testing of the type matcher.
2206 if ($dbg_type) { 2381 if ($dbg_type) {
@@ -2251,8 +2426,15 @@ sub process {
2251 2426
2252# no C99 // comments 2427# no C99 // comments
2253 if ($line =~ m{//}) { 2428 if ($line =~ m{//}) {
2254 ERROR("C99_COMMENTS", 2429 if (ERROR("C99_COMMENTS",
2255 "do not use C99 // comments\n" . $herecurr); 2430 "do not use C99 // comments\n" . $herecurr) &&
2431 $fix) {
2432 my $line = $fixed[$linenr - 1];
2433 if ($line =~ /\/\/(.*)$/) {
2434 my $comment = trim($1);
2435 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2436 }
2437 }
2256 } 2438 }
2257 # Remove C99 comments. 2439 # Remove C99 comments.
2258 $line =~ s@//.*@@; 2440 $line =~ s@//.*@@;
@@ -2351,7 +2533,7 @@ sub process {
2351 # (char*[ const]) 2533 # (char*[ const])
2352 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 2534 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2353 #print "AA<$1>\n"; 2535 #print "AA<$1>\n";
2354 my ($from, $to) = ($2, $2); 2536 my ($ident, $from, $to) = ($1, $2, $2);
2355 2537
2356 # Should start with a space. 2538 # Should start with a space.
2357 $to =~ s/^(\S)/ $1/; 2539 $to =~ s/^(\S)/ $1/;
@@ -2361,15 +2543,22 @@ sub process {
2361 while ($to =~ s/\*\s+\*/\*\*/) { 2543 while ($to =~ s/\*\s+\*/\*\*/) {
2362 } 2544 }
2363 2545
2364 #print "from<$from> to<$to>\n"; 2546## print "1: from<$from> to<$to> ident<$ident>\n";
2365 if ($from ne $to) { 2547 if ($from ne $to) {
2366 ERROR("POINTER_LOCATION", 2548 if (ERROR("POINTER_LOCATION",
2367 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 2549 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2550 $fix) {
2551 my $sub_from = $ident;
2552 my $sub_to = $ident;
2553 $sub_to =~ s/\Q$from\E/$to/;
2554 $fixed[$linenr - 1] =~
2555 s@\Q$sub_from\E@$sub_to@;
2556 }
2368 } 2557 }
2369 } 2558 }
2370 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 2559 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2371 #print "BB<$1>\n"; 2560 #print "BB<$1>\n";
2372 my ($from, $to, $ident) = ($2, $2, $3); 2561 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2373 2562
2374 # Should start with a space. 2563 # Should start with a space.
2375 $to =~ s/^(\S)/ $1/; 2564 $to =~ s/^(\S)/ $1/;
@@ -2381,10 +2570,18 @@ sub process {
2381 # Modifiers should have spaces. 2570 # Modifiers should have spaces.
2382 $to =~ s/(\b$Modifier$)/$1 /; 2571 $to =~ s/(\b$Modifier$)/$1 /;
2383 2572
2384 #print "from<$from> to<$to> ident<$ident>\n"; 2573## print "2: from<$from> to<$to> ident<$ident>\n";
2385 if ($from ne $to && $ident !~ /^$Modifier$/) { 2574 if ($from ne $to && $ident !~ /^$Modifier$/) {
2386 ERROR("POINTER_LOCATION", 2575 if (ERROR("POINTER_LOCATION",
2387 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 2576 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2577 $fix) {
2578
2579 my $sub_from = $match;
2580 my $sub_to = $match;
2581 $sub_to =~ s/\Q$from\E/$to/;
2582 $fixed[$linenr - 1] =~
2583 s@\Q$sub_from\E@$sub_to@;
2584 }
2388 } 2585 }
2389 } 2586 }
2390 2587
@@ -2470,9 +2667,13 @@ sub process {
2470 } 2667 }
2471 2668
2472# missing space after union, struct or enum definition 2669# missing space after union, struct or enum definition
2473 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 2670 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2474 WARN("SPACING", 2671 if (WARN("SPACING",
2475 "missing space after $1 definition\n" . $herecurr); 2672 "missing space after $1 definition\n" . $herecurr) &&
2673 $fix) {
2674 $fixed[$linenr - 1] =~
2675 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2676 }
2476 } 2677 }
2477 2678
2478# check for spacing round square brackets; allowed: 2679# check for spacing round square brackets; allowed:
@@ -2484,8 +2685,12 @@ sub process {
2484 if ($prefix !~ /$Type\s+$/ && 2685 if ($prefix !~ /$Type\s+$/ &&
2485 ($where != 0 || $prefix !~ /^.\s+$/) && 2686 ($where != 0 || $prefix !~ /^.\s+$/) &&
2486 $prefix !~ /[{,]\s+$/) { 2687 $prefix !~ /[{,]\s+$/) {
2487 ERROR("BRACKET_SPACE", 2688 if (ERROR("BRACKET_SPACE",
2488 "space prohibited before open square bracket '['\n" . $herecurr); 2689 "space prohibited before open square bracket '['\n" . $herecurr) &&
2690 $fix) {
2691 $fixed[$linenr - 1] =~
2692 s/^(\+.*?)\s+\[/$1\[/;
2693 }
2489 } 2694 }
2490 } 2695 }
2491 2696
@@ -2502,7 +2707,6 @@ sub process {
2502 __attribute__|format|__extension__| 2707 __attribute__|format|__extension__|
2503 asm|__asm__)$/x) 2708 asm|__asm__)$/x)
2504 { 2709 {
2505
2506 # cpp #define statements have non-optional spaces, ie 2710 # cpp #define statements have non-optional spaces, ie
2507 # if there is a space between the name and the open 2711 # if there is a space between the name and the open
2508 # parenthesis it is simply not a parameter group. 2712 # parenthesis it is simply not a parameter group.
@@ -2516,19 +2720,20 @@ sub process {
2516 } elsif ($ctx =~ /$Type$/) { 2720 } elsif ($ctx =~ /$Type$/) {
2517 2721
2518 } else { 2722 } else {
2519 WARN("SPACING", 2723 if (WARN("SPACING",
2520 "space prohibited between function name and open parenthesis '('\n" . $herecurr); 2724 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2725 $fix) {
2726 $fixed[$linenr - 1] =~
2727 s/\b$name\s+\(/$name\(/;
2728 }
2521 } 2729 }
2522 } 2730 }
2523 2731
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. 2732# Check operator spacing.
2531 if (!($line=~/\#\s*include/)) { 2733 if (!($line=~/\#\s*include/)) {
2734 my $fixed_line = "";
2735 my $line_fixed = 0;
2736
2532 my $ops = qr{ 2737 my $ops = qr{
2533 <<=|>>=|<=|>=|==|!=| 2738 <<=|>>=|<=|>=|==|!=|
2534 \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 2739 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
@@ -2537,11 +2742,30 @@ sub process {
2537 \?|: 2742 \?|:
2538 }x; 2743 }x;
2539 my @elements = split(/($ops|;)/, $opline); 2744 my @elements = split(/($ops|;)/, $opline);
2745
2746## print("element count: <" . $#elements . ">\n");
2747## foreach my $el (@elements) {
2748## print("el: <$el>\n");
2749## }
2750
2751 my @fix_elements = ();
2540 my $off = 0; 2752 my $off = 0;
2541 2753
2754 foreach my $el (@elements) {
2755 push(@fix_elements, substr($rawline, $off, length($el)));
2756 $off += length($el);
2757 }
2758
2759 $off = 0;
2760
2542 my $blank = copy_spacing($opline); 2761 my $blank = copy_spacing($opline);
2543 2762
2544 for (my $n = 0; $n < $#elements; $n += 2) { 2763 for (my $n = 0; $n < $#elements; $n += 2) {
2764
2765 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2766
2767## print("n: <$n> good: <$good>\n");
2768
2545 $off += length($elements[$n]); 2769 $off += length($elements[$n]);
2546 2770
2547 # Pick up the preceding and succeeding characters. 2771 # Pick up the preceding and succeeding characters.
@@ -2598,8 +2822,11 @@ sub process {
2598 } elsif ($op eq ';') { 2822 } elsif ($op eq ';') {
2599 if ($ctx !~ /.x[WEBC]/ && 2823 if ($ctx !~ /.x[WEBC]/ &&
2600 $cc !~ /^\\/ && $cc !~ /^;/) { 2824 $cc !~ /^\\/ && $cc !~ /^;/) {
2601 ERROR("SPACING", 2825 if (ERROR("SPACING",
2602 "space required after that '$op' $at\n" . $hereptr); 2826 "space required after that '$op' $at\n" . $hereptr)) {
2827 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2828 $line_fixed = 1;
2829 }
2603 } 2830 }
2604 2831
2605 # // is a comment 2832 # // is a comment
@@ -2610,15 +2837,24 @@ sub process {
2610 # : when part of a bitfield 2837 # : when part of a bitfield
2611 } elsif ($op eq '->' || $opv eq ':B') { 2838 } elsif ($op eq '->' || $opv eq ':B') {
2612 if ($ctx =~ /Wx.|.xW/) { 2839 if ($ctx =~ /Wx.|.xW/) {
2613 ERROR("SPACING", 2840 if (ERROR("SPACING",
2614 "spaces prohibited around that '$op' $at\n" . $hereptr); 2841 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2842 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2843 $line_fixed = 1;
2844 if (defined $fix_elements[$n + 2]) {
2845 $fix_elements[$n + 2] =~ s/^\s+//;
2846 }
2847 }
2615 } 2848 }
2616 2849
2617 # , must have a space on the right. 2850 # , must have a space on the right.
2618 } elsif ($op eq ',') { 2851 } elsif ($op eq ',') {
2619 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 2852 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2620 ERROR("SPACING", 2853 if (ERROR("SPACING",
2621 "space required after that '$op' $at\n" . $hereptr); 2854 "space required after that '$op' $at\n" . $hereptr)) {
2855 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2856 $line_fixed = 1;
2857 }
2622 } 2858 }
2623 2859
2624 # '*' as part of a type definition -- reported already. 2860 # '*' as part of a type definition -- reported already.
@@ -2632,34 +2868,58 @@ sub process {
2632 $opv eq '*U' || $opv eq '-U' || 2868 $opv eq '*U' || $opv eq '-U' ||
2633 $opv eq '&U' || $opv eq '&&U') { 2869 $opv eq '&U' || $opv eq '&&U') {
2634 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2870 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2635 ERROR("SPACING", 2871 if (ERROR("SPACING",
2636 "space required before that '$op' $at\n" . $hereptr); 2872 "space required before that '$op' $at\n" . $hereptr)) {
2873 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
2874 $line_fixed = 1;
2875 }
2637 } 2876 }
2638 if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2877 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2639 # A unary '*' may be const 2878 # A unary '*' may be const
2640 2879
2641 } elsif ($ctx =~ /.xW/) { 2880 } elsif ($ctx =~ /.xW/) {
2642 ERROR("SPACING", 2881 if (ERROR("SPACING",
2643 "space prohibited after that '$op' $at\n" . $hereptr); 2882 "space prohibited after that '$op' $at\n" . $hereptr)) {
2883 $fixed_line =~ s/\s+$//;
2884 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2885 $line_fixed = 1;
2886 if (defined $fix_elements[$n + 2]) {
2887 $fix_elements[$n + 2] =~ s/^\s+//;
2888 }
2889 }
2644 } 2890 }
2645 2891
2646 # unary ++ and unary -- are allowed no space on one side. 2892 # unary ++ and unary -- are allowed no space on one side.
2647 } elsif ($op eq '++' or $op eq '--') { 2893 } elsif ($op eq '++' or $op eq '--') {
2648 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2894 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2649 ERROR("SPACING", 2895 if (ERROR("SPACING",
2650 "space required one side of that '$op' $at\n" . $hereptr); 2896 "space required one side of that '$op' $at\n" . $hereptr)) {
2897 $fixed_line =~ s/\s+$//;
2898 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2899 $line_fixed = 1;
2900 }
2651 } 2901 }
2652 if ($ctx =~ /Wx[BE]/ || 2902 if ($ctx =~ /Wx[BE]/ ||
2653 ($ctx =~ /Wx./ && $cc =~ /^;/)) { 2903 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2654 ERROR("SPACING", 2904 if (ERROR("SPACING",
2655 "space prohibited before that '$op' $at\n" . $hereptr); 2905 "space prohibited before that '$op' $at\n" . $hereptr)) {
2906 $fixed_line =~ s/\s+$//;
2907 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2908 $line_fixed = 1;
2909 }
2656 } 2910 }
2657 if ($ctx =~ /ExW/) { 2911 if ($ctx =~ /ExW/) {
2658 ERROR("SPACING", 2912 if (ERROR("SPACING",
2659 "space prohibited after that '$op' $at\n" . $hereptr); 2913 "space prohibited after that '$op' $at\n" . $hereptr)) {
2914 $fixed_line =~ s/\s+$//;
2915 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2916 $line_fixed = 1;
2917 if (defined $fix_elements[$n + 2]) {
2918 $fix_elements[$n + 2] =~ s/^\s+//;
2919 }
2920 }
2660 } 2921 }
2661 2922
2662
2663 # << and >> may either have or not have spaces both sides 2923 # << and >> may either have or not have spaces both sides
2664 } elsif ($op eq '<<' or $op eq '>>' or 2924 } elsif ($op eq '<<' or $op eq '>>' or
2665 $op eq '&' or $op eq '^' or $op eq '|' or 2925 $op eq '&' or $op eq '^' or $op eq '|' or
@@ -2668,17 +2928,23 @@ sub process {
2668 $op eq '%') 2928 $op eq '%')
2669 { 2929 {
2670 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 2930 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2671 ERROR("SPACING", 2931 if (ERROR("SPACING",
2672 "need consistent spacing around '$op' $at\n" . 2932 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2673 $hereptr); 2933 $fixed_line =~ s/\s+$//;
2934 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2935 $line_fixed = 1;
2936 }
2674 } 2937 }
2675 2938
2676 # A colon needs no spaces before when it is 2939 # A colon needs no spaces before when it is
2677 # terminating a case value or a label. 2940 # terminating a case value or a label.
2678 } elsif ($opv eq ':C' || $opv eq ':L') { 2941 } elsif ($opv eq ':C' || $opv eq ':L') {
2679 if ($ctx =~ /Wx./) { 2942 if ($ctx =~ /Wx./) {
2680 ERROR("SPACING", 2943 if (ERROR("SPACING",
2681 "space prohibited before that '$op' $at\n" . $hereptr); 2944 "space prohibited before that '$op' $at\n" . $hereptr)) {
2945 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2946 $line_fixed = 1;
2947 }
2682 } 2948 }
2683 2949
2684 # All the others need spaces both sides. 2950 # All the others need spaces both sides.
@@ -2701,11 +2967,39 @@ sub process {
2701 } 2967 }
2702 2968
2703 if ($ok == 0) { 2969 if ($ok == 0) {
2704 ERROR("SPACING", 2970 if (ERROR("SPACING",
2705 "spaces required around that '$op' $at\n" . $hereptr); 2971 "spaces required around that '$op' $at\n" . $hereptr)) {
2972 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2973 $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
2974 $line_fixed = 1;
2975 }
2706 } 2976 }
2707 } 2977 }
2708 $off += length($elements[$n + 1]); 2978 $off += length($elements[$n + 1]);
2979
2980## print("n: <$n> GOOD: <$good>\n");
2981
2982 $fixed_line = $fixed_line . $good;
2983 }
2984
2985 if (($#elements % 2) == 0) {
2986 $fixed_line = $fixed_line . $fix_elements[$#elements];
2987 }
2988
2989 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
2990 $fixed[$linenr - 1] = $fixed_line;
2991 }
2992
2993
2994 }
2995
2996# check for whitespace before a non-naked semicolon
2997 if ($line =~ /^\+.*\S\s+;/) {
2998 if (WARN("SPACING",
2999 "space prohibited before semicolon\n" . $herecurr) &&
3000 $fix) {
3001 1 while $fixed[$linenr - 1] =~
3002 s/^(\+.*\S)\s+;/$1;/;
2709 } 3003 }
2710 } 3004 }
2711 3005
@@ -2734,10 +3028,22 @@ sub process {
2734#need space before brace following if, while, etc 3028#need space before brace following if, while, etc
2735 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 3029 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2736 $line =~ /do{/) { 3030 $line =~ /do{/) {
2737 ERROR("SPACING", 3031 if (ERROR("SPACING",
2738 "space required before the open brace '{'\n" . $herecurr); 3032 "space required before the open brace '{'\n" . $herecurr) &&
3033 $fix) {
3034 $fixed[$linenr - 1] =~
3035 s/^(\+.*(?:do|\))){/$1 {/;
3036 }
2739 } 3037 }
2740 3038
3039## # check for blank lines before declarations
3040## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3041## $prevrawline =~ /^.\s*$/) {
3042## WARN("SPACING",
3043## "No blank lines before declarations\n" . $hereprev);
3044## }
3045##
3046
2741# closing brace should have a space following it when it has anything 3047# closing brace should have a space following it when it has anything
2742# on the line 3048# on the line
2743 if ($line =~ /}(?!(?:,|;|\)))\S/) { 3049 if ($line =~ /}(?!(?:,|;|\)))\S/) {
@@ -2747,32 +3053,52 @@ sub process {
2747 3053
2748# check spacing on square brackets 3054# check spacing on square brackets
2749 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 3055 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2750 ERROR("SPACING", 3056 if (ERROR("SPACING",
2751 "space prohibited after that open square bracket '['\n" . $herecurr); 3057 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3058 $fix) {
3059 $fixed[$linenr - 1] =~
3060 s/\[\s+/\[/;
3061 }
2752 } 3062 }
2753 if ($line =~ /\s\]/) { 3063 if ($line =~ /\s\]/) {
2754 ERROR("SPACING", 3064 if (ERROR("SPACING",
2755 "space prohibited before that close square bracket ']'\n" . $herecurr); 3065 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3066 $fix) {
3067 $fixed[$linenr - 1] =~
3068 s/\s+\]/\]/;
3069 }
2756 } 3070 }
2757 3071
2758# check spacing on parentheses 3072# check spacing on parentheses
2759 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 3073 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2760 $line !~ /for\s*\(\s+;/) { 3074 $line !~ /for\s*\(\s+;/) {
2761 ERROR("SPACING", 3075 if (ERROR("SPACING",
2762 "space prohibited after that open parenthesis '('\n" . $herecurr); 3076 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3077 $fix) {
3078 $fixed[$linenr - 1] =~
3079 s/\(\s+/\(/;
3080 }
2763 } 3081 }
2764 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 3082 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2765 $line !~ /for\s*\(.*;\s+\)/ && 3083 $line !~ /for\s*\(.*;\s+\)/ &&
2766 $line !~ /:\s+\)/) { 3084 $line !~ /:\s+\)/) {
2767 ERROR("SPACING", 3085 if (ERROR("SPACING",
2768 "space prohibited before that close parenthesis ')'\n" . $herecurr); 3086 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3087 $fix) {
3088 $fixed[$linenr - 1] =~
3089 s/\s+\)/\)/;
3090 }
2769 } 3091 }
2770 3092
2771#goto labels aren't indented, allow a single space however 3093#goto labels aren't indented, allow a single space however
2772 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 3094 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2773 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 3095 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2774 WARN("INDENTED_LABEL", 3096 if (WARN("INDENTED_LABEL",
2775 "labels should not be indented\n" . $herecurr); 3097 "labels should not be indented\n" . $herecurr) &&
3098 $fix) {
3099 $fixed[$linenr - 1] =~
3100 s/^(.)\s+/$1/;
3101 }
2776 } 3102 }
2777 3103
2778# Return is not a function. 3104# Return is not a function.
@@ -2809,8 +3135,13 @@ sub process {
2809 } 3135 }
2810 3136
2811# Need a space before open parenthesis after if, while etc 3137# Need a space before open parenthesis after if, while etc
2812 if ($line=~/\b(if|while|for|switch)\(/) { 3138 if ($line =~ /\b(if|while|for|switch)\(/) {
2813 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); 3139 if (ERROR("SPACING",
3140 "space required before the open parenthesis '('\n" . $herecurr) &&
3141 $fix) {
3142 $fixed[$linenr - 1] =~
3143 s/\b(if|while|for|switch)\(/$1 \(/;
3144 }
2814 } 3145 }
2815 3146
2816# Check for illegal assignment in if conditional -- and check for trailing 3147# Check for illegal assignment in if conditional -- and check for trailing
@@ -2934,16 +3265,29 @@ sub process {
2934 } 3265 }
2935 } 3266 }
2936 3267
2937#CamelCase 3268#Specific variable tests
2938 while ($line =~ m{($Constant|$Lval)}g) { 3269 while ($line =~ m{($Constant|$Lval)}g) {
2939 my $var = $1; 3270 my $var = $1;
2940 if ($var !~ /$Constant/ && 3271
2941 $var =~ /[A-Z]\w*[a-z]|[a-z]\w*[A-Z]/ && 3272#gcc binary extension
2942 $var !~ /"^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 3273 if ($var =~ /^$Binary$/) {
2943 !defined $camelcase{$var}) { 3274 WARN("GCC_BINARY_CONSTANT",
2944 $camelcase{$var} = 1; 3275 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
2945 WARN("CAMELCASE", 3276 }
2946 "Avoid CamelCase: <$var>\n" . $herecurr); 3277
3278#CamelCase
3279 if ($var !~ /^$Constant$/ &&
3280 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
3281#Ignore Page<foo> variants
3282 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
3283#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
3284 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3285 seed_camelcase_includes() if ($check);
3286 if (!defined $camelcase{$var}) {
3287 $camelcase{$var} = 1;
3288 CHK("CAMELCASE",
3289 "Avoid CamelCase: <$var>\n" . $herecurr);
3290 }
2947 } 3291 }
2948 } 3292 }
2949 3293
@@ -3021,7 +3365,7 @@ sub process {
3021 if ($dstat ne '' && 3365 if ($dstat ne '' &&
3022 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 3366 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3023 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 3367 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
3024 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo 3368 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3025 $dstat !~ /^'X'$/ && # character constants 3369 $dstat !~ /^'X'$/ && # character constants
3026 $dstat !~ /$exceptions/ && 3370 $dstat !~ /$exceptions/ &&
3027 $dstat !~ /^\.$Ident\s*=/ && # .foo = 3371 $dstat !~ /^\.$Ident\s*=/ && # .foo =
@@ -3230,11 +3574,11 @@ sub process {
3230 } 3574 }
3231 3575
3232# check for unnecessary blank lines around braces 3576# check for unnecessary blank lines around braces
3233 if (($line =~ /^.\s*}\s*$/ && $prevline =~ /^.\s*$/)) { 3577 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
3234 CHK("BRACES", 3578 CHK("BRACES",
3235 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); 3579 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3236 } 3580 }
3237 if (($line =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 3581 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3238 CHK("BRACES", 3582 CHK("BRACES",
3239 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); 3583 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3240 } 3584 }
@@ -3279,6 +3623,18 @@ sub process {
3279 } 3623 }
3280 } 3624 }
3281 3625
3626# check for comparisons of jiffies
3627 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3628 WARN("JIFFIES_COMPARISON",
3629 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3630 }
3631
3632# check for comparisons of get_jiffies_64()
3633 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3634 WARN("JIFFIES_COMPARISON",
3635 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3636 }
3637
3282# warn about #ifdefs in C files 3638# warn about #ifdefs in C files
3283# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 3639# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3284# print "#ifdef in C files should be avoided\n"; 3640# print "#ifdef in C files should be avoided\n";
@@ -3288,8 +3644,13 @@ sub process {
3288 3644
3289# warn about spacing in #ifdefs 3645# warn about spacing in #ifdefs
3290 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 3646 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3291 ERROR("SPACING", 3647 if (ERROR("SPACING",
3292 "exactly one space required after that #$1\n" . $herecurr); 3648 "exactly one space required after that #$1\n" . $herecurr) &&
3649 $fix) {
3650 $fixed[$linenr - 1] =~
3651 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3652 }
3653
3293 } 3654 }
3294 3655
3295# check for spinlock_t definitions without a comment. 3656# check for spinlock_t definitions without a comment.
@@ -3495,6 +3856,14 @@ sub process {
3495 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 3856 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3496 } 3857 }
3497 3858
3859# alloc style
3860# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3861 if ($^V && $^V ge 5.10.0 &&
3862 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3863 CHK("ALLOC_SIZEOF_STRUCT",
3864 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3865 }
3866
3498# check for krealloc arg reuse 3867# check for krealloc arg reuse
3499 if ($^V && $^V ge 5.10.0 && 3868 if ($^V && $^V ge 5.10.0 &&
3500 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 3869 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
@@ -3540,6 +3909,33 @@ sub process {
3540 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 3909 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3541 } 3910 }
3542 3911
3912# check for comparisons against true and false
3913 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3914 my $lead = $1;
3915 my $arg = $2;
3916 my $test = $3;
3917 my $otype = $4;
3918 my $trail = $5;
3919 my $op = "!";
3920
3921 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3922
3923 my $type = lc($otype);
3924 if ($type =~ /^(?:true|false)$/) {
3925 if (("$test" eq "==" && "$type" eq "true") ||
3926 ("$test" eq "!=" && "$type" eq "false")) {
3927 $op = "";
3928 }
3929
3930 CHK("BOOL_COMPARISON",
3931 "Using comparison to $otype is error prone\n" . $herecurr);
3932
3933## maybe suggesting a correct construct would better
3934## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3935
3936 }
3937 }
3938
3543# check for semaphores initialized locked 3939# check for semaphores initialized locked
3544 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 3940 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3545 WARN("CONSIDER_COMPLETION", 3941 WARN("CONSIDER_COMPLETION",
@@ -3717,6 +4113,40 @@ sub process {
3717 print "\n\n"; 4113 print "\n\n";
3718 } 4114 }
3719 4115
4116 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4117 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4118 my $linecount = 0;
4119 my $f;
4120
4121 open($f, '>', $newfile)
4122 or die "$P: Can't open $newfile for write\n";
4123 foreach my $fixed_line (@fixed) {
4124 $linecount++;
4125 if ($file) {
4126 if ($linecount > 3) {
4127 $fixed_line =~ s/^\+//;
4128 print $f $fixed_line. "\n";
4129 }
4130 } else {
4131 print $f $fixed_line . "\n";
4132 }
4133 }
4134 close($f);
4135
4136 if (!$quiet) {
4137 print << "EOM";
4138Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4139
4140Do _NOT_ trust the results written to this file.
4141Do _NOT_ submit these changes without inspecting them for correctness.
4142
4143This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4144No warranties, expressed or implied...
4145
4146EOM
4147 }
4148 }
4149
3720 if ($clean == 1 && $quiet == 0) { 4150 if ($clean == 1 && $quiet == 0) {
3721 print "$vname has no obvious style problems and is ready for submission.\n" 4151 print "$vname has no obvious style problems and is ready for submission.\n"
3722 } 4152 }