Some patches have simple defects in whitespace and formatting that checkpatch could correct automatically. Attempt to do so. Add a --fix option to create a "<inputfile>.EXPERIMENTAL-checkpatch-fixes" file that tries to use normal kernel style for some of these formatting errors. Add warnings against using this file without verifying the changes.
Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all
Unified · Split
| @@ -27,6 +27,7 @@ my $summary = 1; · 26 unmodified lines | ||
| 27 | 27 | my $mailback = 0; |
| 28 | 28 | my $summary_file = 0; |
| 29 | 29 | my $show_types = 0; |
| 30 | +my $fix = 0; | |
| 30 | 31 | my $root; |
| 31 | 32 | my %debug; |
| 32 | 33 | my %ignore_type = (); |
| @@ -63,6 +64,11 @@ Options: · 30 unmodified lines | ||
| 63 | 64 | is all off) |
| 64 | 65 | --test-only=WORD report only warnings/errors containing WORD |
| 65 | 66 | literally |
| 67 | + --fix EXPERIMENTAL - may create horrible results | |
| 68 | + If correctable single-line errors exist, create | |
| 69 | + "<inputfile>.EXPERIMENTAL-checkpatch-fixes" | |
| 70 | + with potential errors corrected to the preferred | |
| 71 | + checkpatch style | |
| 66 | 72 | -h, --help, --version display this help and exit |
| 67 | 73 | |
| 68 | 74 | When FILE is - read standard input. |
| @@ -114,7 +120,7 @@ GetOptions( · 45 unmodified lines | ||
| 114 | 120 | 'summary!' => \$summary, |
| 115 | 121 | 'mailback!' => \$mailback, |
| 116 | 122 | 'summary-file!' => \$summary_file, |
| 117 | − | |
| 123 | + 'fix!' => \$fix, | |
| 118 | 124 | 'debug=s' => \%debug, |
| 119 | 125 | 'test-only=s' => \$tst_only, |
| 120 | 126 | 'h|help' => \$help, |
| @@ -367,6 +373,7 @@ $chk_signoff = 0 if ($file); · 246 unmodified lines | ||
| 367 | 373 | |
| 368 | 374 | my @rawlines = (); |
| 369 | 375 | my @lines = (); |
| 376 | +my @fixed = (); | |
| 370 | 377 | my $vname; |
| 371 | 378 | for my $filename (@ARGV) { |
| 372 | 379 | my $FILE; |
| @@ -394,6 +401,7 @@ for my $filename (@ARGV) { · 21 unmodified lines | ||
| 394 | 401 | } |
| 395 | 402 | @rawlines = (); |
| 396 | 403 | @lines = (); |
| 404 | + @fixed = (); | |
| 397 | 405 | } |
| 398 | 406 | |
| 399 | 407 | exit($exit); |
| @@ -434,7 +442,7 @@ sub parse_email { · 34 unmodified lines | ||
| 434 | 442 | $comment = $2 if defined $2; |
| 435 | 443 | $formatted_email =~ s/$address.*$//; |
| 436 | 444 | $name = $formatted_email; |
| 437 | − $name =~ s/^\s+|\s+$//g; | |
| 445 | + $name = trim($name); | |
| 438 | 446 | $name =~ s/^\"|\"$//g; |
| 439 | 447 | # If there's a name left after stripping spaces and |
| 440 | 448 | # leading quotes, and the address doesn't have both |
| @@ -449,9 +457,9 @@ sub parse_email { · 8 unmodified lines | ||
| 449 | 457 | } |
| 450 | 458 | } |
| 451 | 459 | |
| 452 | − $name =~ s/^\s+|\s+$//g; | |
| 460 | + $name = trim($name); | |
| 453 | 461 | $name =~ s/^\"|\"$//g; |
| 454 | − $address =~ s/^\s+|\s+$//g; | |
| 462 | + $address = trim($address); | |
| 455 | 463 | $address =~ s/^\<|\>$//g; |
| 456 | 464 | |
| 457 | 465 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| @@ -467,9 +475,9 @@ sub format_email { · 9 unmodified lines | ||
| 467 | 475 | |
| 468 | 476 | my $formatted_email; |
| 469 | 477 | |
| 470 | − $name =~ s/^\s+|\s+$//g; | |
| 478 | + $name = trim($name); | |
| 471 | 479 | $name =~ s/^\"|\"$//g; |
| 472 | − $address =~ s/^\s+|\s+$//g; | |
| 480 | + $address = trim($address); | |
| 473 | 481 | |
| 474 | 482 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
| 475 | 483 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| @@ -1291,19 +1299,25 @@ sub ERROR { · 815 unmodified lines | ||
| 1291 | 1299 | if (report("ERROR", $_[0], $_[1])) { |
| 1292 | 1300 | our $clean = 0; |
| 1293 | 1301 | our $cnt_error++; |
| 1302 | + return 1; | |
| 1294 | 1303 | } |
| 1304 | + return 0; | |
| 1295 | 1305 | } |
| 1296 | 1306 | sub WARN { |
| 1297 | 1307 | if (report("WARNING", $_[0], $_[1])) { |
| 1298 | 1308 | our $clean = 0; |
| 1299 | 1309 | our $cnt_warn++; |
| 1310 | + return 1; | |
| 1300 | 1311 | } |
| 1312 | + return 0; | |
| 1301 | 1313 | } |
| 1302 | 1314 | sub CHK { |
| 1303 | 1315 | if ($check && report("CHECK", $_[0], $_[1])) { |
| 1304 | 1316 | our $clean = 0; |
| 1305 | 1317 | our $cnt_chk++; |
| 1318 | + return 1; | |
| 1306 | 1319 | } |
| 1320 | + return 0; | |
| 1307 | 1321 | } |
| 1308 | 1322 | |
| 1309 | 1323 | sub check_absolute_file { |
| @@ -1334,6 +1348,29 @@ sub check_absolute_file { · 24 unmodified lines | ||
| 1334 | 1348 | } |
| 1335 | 1349 | } |
| 1336 | 1350 | |
| 1351 | +sub trim { | |
| 1352 | + my ($string) = @_; | |
| 1353 | + | |
| 1354 | + $string =~ s/(^\s+|\s+$)//g; | |
| 1355 | + | |
| 1356 | + return $string; | |
| 1357 | +} | |
| 1358 | + | |
| 1359 | +sub tabify { | |
| 1360 | + my ($leading) = @_; | |
| 1361 | + | |
| 1362 | + my $source_indent = 8; | |
| 1363 | + my $max_spaces_before_tab = $source_indent - 1; | |
| 1364 | + my $spaces_to_tab = " " x $source_indent; | |
| 1365 | + | |
| 1366 | + #convert leading spaces to tabs | |
| 1367 | + 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; | |
| 1368 | + #Remove spaces before a tab | |
| 1369 | + 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; | |
| 1370 | + | |
| 1371 | + return "$leading"; | |
| 1372 | +} | |
| 1373 | + | |
| 1337 | 1374 | sub pos_last_openparen { |
| 1338 | 1375 | my ($line) = @_; |
| 1339 | 1376 | |
| @@ -1425,6 +1462,8 @@ sub process { · 85 unmodified lines | ||
| 1425 | 1462 | $linenr++; |
| 1426 | 1463 | $line = $rawline; |
| 1427 | 1464 | |
| 1465 | + push(@fixed, $rawline) if ($fix); | |
| 1466 | + | |
| 1428 | 1467 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
| 1429 | 1468 | $setup_docs = 0; |
| 1430 | 1469 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| @@ -1616,16 +1655,29 @@ sub process { · 185 unmodified lines | ||
| 1616 | 1655 | "Non-standard signature: $sign_off\n" . $herecurr); |
| 1617 | 1656 | } |
| 1618 | 1657 | if (defined $space_before && $space_before ne "") { |
| 1619 | − WARN("BAD_SIGN_OFF", | |
| 1620 | − "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr); | |
| 1658 | + if (WARN("BAD_SIGN_OFF", | |
| 1659 | + "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && | |
| 1660 | + $fix) { | |
| 1661 | + $fixed[$linenr - 1] = | |
| 1662 | + "$ucfirst_sign_off $email"; | |
| 1663 | + } | |
| 1621 | 1664 | } |
| 1622 | 1665 | if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { |
| 1623 | − WARN("BAD_SIGN_OFF", | |
| 1624 | − "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr); | |
| 1666 | + if (WARN("BAD_SIGN_OFF", | |
| 1667 | + "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && | |
| 1668 | + $fix) { | |
| 1669 | + $fixed[$linenr - 1] = | |
| 1670 | + "$ucfirst_sign_off $email"; | |
| 1671 | + } | |
| 1672 | + | |
| 1625 | 1673 | } |
| 1626 | 1674 | if (!defined $space_after || $space_after ne " ") { |
| 1627 | − WARN("BAD_SIGN_OFF", | |
| 1628 | − "Use a single space after $ucfirst_sign_off\n" . $herecurr); | |
| 1675 | + if (WARN("BAD_SIGN_OFF", | |
| 1676 | + "Use a single space after $ucfirst_sign_off\n" . $herecurr) && | |
| 1677 | + $fix) { | |
| 1678 | + $fixed[$linenr - 1] = | |
| 1679 | + "$ucfirst_sign_off $email"; | |
| 1680 | + } | |
| 1629 | 1681 | } |
| 1630 | 1682 | |
| 1631 | 1683 | my ($email_name, $email_address, $comment) = parse_email($email); |
| @@ -1715,8 +1767,12 @@ sub process { · 83 unmodified lines | ||
| 1715 | 1767 | |
| 1716 | 1768 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 1717 | 1769 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
| 1718 | − ERROR("TRAILING_WHITESPACE", | |
| 1719 | − "trailing whitespace\n" . $herevet); | |
| 1770 | + if (ERROR("TRAILING_WHITESPACE", | |
| 1771 | + "trailing whitespace\n" . $herevet) && | |
| 1772 | + $fix) { | |
| 1773 | + $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/; | |
| 1774 | + } | |
| 1775 | + | |
| 1720 | 1776 | $rpt_cleaners = 1; |
| 1721 | 1777 | } |
| 1722 | 1778 | |
| @@ -1811,8 +1867,12 @@ sub process { · 88 unmodified lines | ||
| 1811 | 1867 | |
| 1812 | 1868 | # check for spaces before a quoted newline |
| 1813 | 1869 | if ($rawline =~ /^.*\".*\s\\n/) { |
| 1814 | − WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", | |
| 1815 | − "unnecessary whitespace before a quoted newline\n" . $herecurr); | |
| 1870 | + if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", | |
| 1871 | + "unnecessary whitespace before a quoted newline\n" . $herecurr) && | |
| 1872 | + $fix) { | |
| 1873 | + $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; | |
| 1874 | + } | |
| 1875 | + | |
| 1816 | 1876 | } |
| 1817 | 1877 | |
| 1818 | 1878 | # check for adding lines without a newline. |
| @@ -1843,16 +1903,23 @@ sub process { · 24 unmodified lines | ||
| 1843 | 1903 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 1844 | 1904 | $rawline =~ /^\+\s* \s*/) { |
| 1845 | 1905 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
| 1846 | − ERROR("CODE_INDENT", | |
| 1847 | − "code indent should use tabs where possible\n" . $herevet); | |
| 1848 | 1906 | $rpt_cleaners = 1; |
| 1907 | + if (ERROR("CODE_INDENT", | |
| 1908 | + "code indent should use tabs where possible\n" . $herevet) && | |
| 1909 | + $fix) { | |
| 1910 | + $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; | |
| 1911 | + } | |
| 1849 | 1912 | } |
| 1850 | 1913 | |
| 1851 | 1914 | # check for space before tabs. |
| 1852 | 1915 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 1853 | 1916 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
| 1854 | − WARN("SPACE_BEFORE_TAB", | |
| 1855 | − "please, no space before tabs\n" . $herevet); | |
| 1917 | + if (WARN("SPACE_BEFORE_TAB", | |
| 1918 | + "please, no space before tabs\n" . $herevet) && | |
| 1919 | + $fix) { | |
| 1920 | + $fixed[$linenr - 1] =~ | |
| 1921 | + s/(^\+.*) +\t/$1\t/; | |
| 1922 | + } | |
| 1856 | 1923 | } |
| 1857 | 1924 | |
| 1858 | 1925 | # check for && or || at the start of a line |
| @@ -1880,15 +1947,24 @@ sub process { · 21 unmodified lines | ||
| 1880 | 1947 | |
| 1881 | 1948 | if ($newindent ne $goodtabindent && |
| 1882 | 1949 | $newindent ne $goodspaceindent) { |
| 1883 | − CHK("PARENTHESIS_ALIGNMENT", | |
| 1884 | − "Alignment should match open parenthesis\n" . $hereprev); | |
| 1950 | + | |
| 1951 | + if (CHK("PARENTHESIS_ALIGNMENT", | |
| 1952 | + "Alignment should match open parenthesis\n" . $hereprev) && | |
| 1953 | + $fix && $line =~ /^\+/) { | |
| 1954 | + $fixed[$linenr - 1] =~ | |
| 1955 | + s/^\+[ \t]*/\+$goodtabindent/; | |
| 1956 | + } | |
| 1885 | 1957 | } |
| 1886 | 1958 | } |
| 1887 | 1959 | } |
| 1888 | 1960 | |
| 1889 | 1961 | if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) { |
| 1890 | − CHK("SPACING", | |
| 1891 | − "No space is necessary after a cast\n" . $hereprev); | |
| 1962 | + if (CHK("SPACING", | |
| 1963 | + "No space is necessary after a cast\n" . $hereprev) && | |
| 1964 | + $fix) { | |
| 1965 | + $fixed[$linenr - 1] =~ | |
| 1966 | + s/^(\+.*\*[ \t]*\))[ \t]+/$1/; | |
| 1967 | + } | |
| 1892 | 1968 | } |
| 1893 | 1969 | |
| 1894 | 1970 | if ($realfile =~ m@^(drivers/net/|net/)@ && |
| @@ -1920,10 +1996,13 @@ sub process { · 25 unmodified lines | ||
| 1920 | 1996 | # 1) within comments |
| 1921 | 1997 | # 2) indented preprocessor commands |
| 1922 | 1998 | # 3) hanging labels |
| 1923 | − if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { | |
| 1999 | + if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { | |
| 1924 | 2000 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
| 1925 | − WARN("LEADING_SPACE", | |
| 1926 | − "please, no spaces at the start of a line\n" . $herevet); | |
| 2001 | + if (WARN("LEADING_SPACE", | |
| 2002 | + "please, no spaces at the start of a line\n" . $herevet) && | |
| 2003 | + $fix) { | |
| 2004 | + $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; | |
| 2005 | + } | |
| 1927 | 2006 | } |
| 1928 | 2007 | |
| 1929 | 2008 | # check we are in a valid C source file if not then ignore this hunk |
| @@ -2213,7 +2292,7 @@ sub process { · 283 unmodified lines | ||
| 2213 | 2292 | $prev_values = substr($curr_values, -1); |
| 2214 | 2293 | |
| 2215 | 2294 | #ignore lines not being added |
| 2216 | − if ($line=~/^[^\+]/) {next;} | |
| 2295 | + next if ($line =~ /^[^\+]/); | |
| 2217 | 2296 | |
| 2218 | 2297 | # TEST: allow direct testing of the type matcher. |
| 2219 | 2298 | if ($dbg_type) { |
| @@ -2264,8 +2343,15 @@ sub process { · 44 unmodified lines | ||
| 2264 | 2343 | |
| 2265 | 2344 | # no C99 // comments |
| 2266 | 2345 | if ($line =~ m{//}) { |
| 2267 | − ERROR("C99_COMMENTS", | |
| 2268 | − "do not use C99 // comments\n" . $herecurr); | |
| 2346 | + if (ERROR("C99_COMMENTS", | |
| 2347 | + "do not use C99 // comments\n" . $herecurr) && | |
| 2348 | + $fix) { | |
| 2349 | + my $line = $fixed[$linenr - 1]; | |
| 2350 | + if ($line =~ /\/\/(.*)$/) { | |
| 2351 | + my $comment = trim($1); | |
| 2352 | + $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@; | |
| 2353 | + } | |
| 2354 | + } | |
| 2269 | 2355 | } |
| 2270 | 2356 | # Remove C99 comments. |
| 2271 | 2357 | $line =~ s@//.*@@; |
| @@ -2364,7 +2450,7 @@ sub process { · 92 unmodified lines | ||
| 2364 | 2450 | # (char*[ const]) |
| 2365 | 2451 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 2366 | 2452 | #print "AA<$1>\n"; |
| 2367 | − my ($from, $to) = ($2, $2); | |
| 2453 | + my ($ident, $from, $to) = ($1, $2, $2); | |
| 2368 | 2454 | |
| 2369 | 2455 | # Should start with a space. |
| 2370 | 2456 | $to =~ s/^(\S)/ $1/; |
| @@ -2374,15 +2460,22 @@ sub process { · 3 unmodified lines | ||
| 2374 | 2460 | while ($to =~ s/\*\s+\*/\*\*/) { |
| 2375 | 2461 | } |
| 2376 | 2462 | |
| 2377 | − #print "from<$from> to<$to>\n"; | |
| 2463 | +## print "1: from<$from> to<$to> ident<$ident>\n"; | |
| 2378 | 2464 | if ($from ne $to) { |
| 2379 | − ERROR("POINTER_LOCATION", | |
| 2380 | − "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); | |
| 2465 | + if (ERROR("POINTER_LOCATION", | |
| 2466 | + "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && | |
| 2467 | + $fix) { | |
| 2468 | + my $sub_from = $ident; | |
| 2469 | + my $sub_to = $ident; | |
| 2470 | + $sub_to =~ s/\Q$from\E/$to/; | |
| 2471 | + $fixed[$linenr - 1] =~ | |
| 2472 | + s@\Q$sub_from\E@$sub_to@; | |
| 2473 | + } | |
| 2381 | 2474 | } |
| 2382 | 2475 | } |
| 2383 | 2476 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 2384 | 2477 | #print "BB<$1>\n"; |
| 2385 | − my ($from, $to, $ident) = ($2, $2, $3); | |
| 2478 | + my ($match, $from, $to, $ident) = ($1, $2, $2, $3); | |
| 2386 | 2479 | |
| 2387 | 2480 | # Should start with a space. |
| 2388 | 2481 | $to =~ s/^(\S)/ $1/; |
| @@ -2394,10 +2487,18 @@ sub process { · 5 unmodified lines | ||
| 2394 | 2487 | # Modifiers should have spaces. |
| 2395 | 2488 | $to =~ s/(\b$Modifier$)/$1 /; |
| 2396 | 2489 | |
| 2397 | − #print "from<$from> to<$to> ident<$ident>\n"; | |
| 2490 | +## print "2: from<$from> to<$to> ident<$ident>\n"; | |
| 2398 | 2491 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
| 2399 | − ERROR("POINTER_LOCATION", | |
| 2400 | − "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); | |
| 2492 | + if (ERROR("POINTER_LOCATION", | |
| 2493 | + "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && | |
| 2494 | + $fix) { | |
| 2495 | + | |
| 2496 | + my $sub_from = $match; | |
| 2497 | + my $sub_to = $match; | |
| 2498 | + $sub_to =~ s/\Q$from\E/$to/; | |
| 2499 | + $fixed[$linenr - 1] =~ | |
| 2500 | + s@\Q$sub_from\E@$sub_to@; | |
| 2501 | + } | |
| 2401 | 2502 | } |
| 2402 | 2503 | } |
| 2403 | 2504 | |
| @@ -2483,9 +2584,13 @@ sub process { · 79 unmodified lines | ||
| 2483 | 2584 | } |
| 2484 | 2585 | |
| 2485 | 2586 | # missing space after union, struct or enum definition |
| 2486 | − if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { | |
| 2487 | − WARN("SPACING", | |
| 2488 | − "missing space after $1 definition\n" . $herecurr); | |
| 2587 | + if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { | |
| 2588 | + if (WARN("SPACING", | |
| 2589 | + "missing space after $1 definition\n" . $herecurr) && | |
| 2590 | + $fix) { | |
| 2591 | + $fixed[$linenr - 1] =~ | |
| 2592 | + s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; | |
| 2593 | + } | |
| 2489 | 2594 | } |
| 2490 | 2595 | |
| 2491 | 2596 | # check for spacing round square brackets; allowed: |
| @@ -2497,8 +2602,12 @@ sub process { · 5 unmodified lines | ||
| 2497 | 2602 | if ($prefix !~ /$Type\s+$/ && |
| 2498 | 2603 | ($where != 0 || $prefix !~ /^.\s+$/) && |
| 2499 | 2604 | $prefix !~ /[{,]\s+$/) { |
| 2500 | − ERROR("BRACKET_SPACE", | |
| 2501 | − "space prohibited before open square bracket '['\n" . $herecurr); | |
| 2605 | + if (ERROR("BRACKET_SPACE", | |
| 2606 | + "space prohibited before open square bracket '['\n" . $herecurr) && | |
| 2607 | + $fix) { | |
| 2608 | + $fixed[$linenr - 1] =~ | |
| 2609 | + s/^(\+.*?)\s+\[/$1\[/; | |
| 2610 | + } | |
| 2502 | 2611 | } |
| 2503 | 2612 | } |
| 2504 | 2613 | |
| @@ -2515,7 +2624,6 @@ sub process { · 10 unmodified lines | ||
| 2515 | 2624 | __attribute__|format|__extension__| |
| 2516 | 2625 | asm|__asm__)$/x) |
| 2517 | 2626 | { |
| 2518 | − | |
| 2519 | 2627 | # cpp #define statements have non-optional spaces, ie |
| 2520 | 2628 | # if there is a space between the name and the open |
| 2521 | 2629 | # parenthesis it is simply not a parameter group. |
| @@ -2529,19 +2637,30 @@ sub process { · 7 unmodified lines | ||
| 2529 | 2637 | } elsif ($ctx =~ /$Type$/) { |
| 2530 | 2638 | |
| 2531 | 2639 | } else { |
| 2532 | − WARN("SPACING", | |
| 2533 | − "space prohibited between function name and open parenthesis '('\n" . $herecurr); | |
| 2640 | + if (WARN("SPACING", | |
| 2641 | + "space prohibited between function name and open parenthesis '('\n" . $herecurr) && | |
| 2642 | + $fix) { | |
| 2643 | + $fixed[$linenr - 1] =~ | |
| 2644 | + s/\b$name\s+\(/$name\(/; | |
| 2645 | + } | |
| 2534 | 2646 | } |
| 2535 | 2647 | } |
| 2536 | 2648 | |
| 2537 | 2649 | # check for whitespace before a non-naked semicolon |
| 2538 | 2650 | if ($line =~ /^\+.*\S\s+;/) { |
| 2539 | − WARN("SPACING", | |
| 2540 | − "space prohibited before semicolon\n" . $herecurr); | |
| 2651 | + if (WARN("SPACING", | |
| 2652 | + "space prohibited before semicolon\n" . $herecurr) && | |
| 2653 | + $fix) { | |
| 2654 | + $fixed[$linenr - 1] =~ | |
| 2655 | + s/^(\+.*\S)\s+;/$1;/; | |
| 2656 | + } | |
| 2541 | 2657 | } |
| 2542 | 2658 | |
| 2543 | 2659 | # Check operator spacing. |
| 2544 | 2660 | if (!($line=~/\#\s*include/)) { |
| 2661 | + my $fixed_line = ""; | |
| 2662 | + my $line_fixed = 0; | |
| 2663 | + | |
| 2545 | 2664 | my $ops = qr{ |
| 2546 | 2665 | <<=|>>=|<=|>=|==|!=| |
| 2547 | 2666 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| @@ -2550,11 +2669,30 @@ sub process { · 2 unmodified lines | ||
| 2550 | 2669 | \?|: |
| 2551 | 2670 | }x; |
| 2552 | 2671 | my @elements = split(/($ops|;)/, $opline); |
| 2672 | + | |
| 2673 | +## print("element count: <" . $#elements . ">\n"); | |
| 2674 | +## foreach my $el (@elements) { | |
| 2675 | +## print("el: <$el>\n"); | |
| 2676 | +## } | |
| 2677 | + | |
| 2678 | + my @fix_elements = (); | |
| 2553 | 2679 | my $off = 0; |
| 2554 | 2680 | |
| 2681 | + foreach my $el (@elements) { | |
| 2682 | + push(@fix_elements, substr($rawline, $off, length($el))); | |
| 2683 | + $off += length($el); | |
| 2684 | + } | |
| 2685 | + | |
| 2686 | + $off = 0; | |
| 2687 | + | |
| 2555 | 2688 | my $blank = copy_spacing($opline); |
| 2556 | 2689 | |
| 2557 | 2690 | for (my $n = 0; $n < $#elements; $n += 2) { |
| 2691 | + | |
| 2692 | + my $good = $fix_elements[$n] . $fix_elements[$n + 1]; | |
| 2693 | + | |
| 2694 | +## print("n: <$n> good: <$good>\n"); | |
| 2695 | + | |
| 2558 | 2696 | $off += length($elements[$n]); |
| 2559 | 2697 | |
| 2560 | 2698 | # Pick up the preceding and succeeding characters. |
| @@ -2611,8 +2749,11 @@ sub process { · 50 unmodified lines | ||
| 2611 | 2749 | } elsif ($op eq ';') { |
| 2612 | 2750 | if ($ctx !~ /.x[WEBC]/ && |
| 2613 | 2751 | $cc !~ /^\\/ && $cc !~ /^;/) { |
| 2614 | − ERROR("SPACING", | |
| 2615 | − "space required after that '$op' $at\n" . $hereptr); | |
| 2752 | + if (ERROR("SPACING", | |
| 2753 | + "space required after that '$op' $at\n" . $hereptr)) { | |
| 2754 | + $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; | |
| 2755 | + $line_fixed = 1; | |
| 2756 | + } | |
| 2616 | 2757 | } |
| 2617 | 2758 | |
| 2618 | 2759 | # // is a comment |
| @@ -2623,15 +2764,24 @@ sub process { · 4 unmodified lines | ||
| 2623 | 2764 | # : when part of a bitfield |
| 2624 | 2765 | } elsif ($op eq '->' || $opv eq ':B') { |
| 2625 | 2766 | if ($ctx =~ /Wx.|.xW/) { |
| 2626 | − ERROR("SPACING", | |
| 2627 | − "spaces prohibited around that '$op' $at\n" . $hereptr); | |
| 2767 | + if (ERROR("SPACING", | |
| 2768 | + "spaces prohibited around that '$op' $at\n" . $hereptr)) { | |
| 2769 | + $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); | |
| 2770 | + $line_fixed = 1; | |
| 2771 | + if (defined $fix_elements[$n + 2]) { | |
| 2772 | + $fix_elements[$n + 2] =~ s/^\s+//; | |
| 2773 | + } | |
| 2774 | + } | |
| 2628 | 2775 | } |
| 2629 | 2776 | |
| 2630 | 2777 | # , must have a space on the right. |
| 2631 | 2778 | } elsif ($op eq ',') { |
| 2632 | 2779 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
| 2633 | − ERROR("SPACING", | |
| 2634 | − "space required after that '$op' $at\n" . $hereptr); | |
| 2780 | + if (ERROR("SPACING", | |
| 2781 | + "space required after that '$op' $at\n" . $hereptr)) { | |
| 2782 | + $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " "; | |
| 2783 | + $line_fixed = 1; | |
| 2784 | + } | |
| 2635 | 2785 | } |
| 2636 | 2786 | |
| 2637 | 2787 | # '*' as part of a type definition -- reported already. |
| @@ -2645,34 +2795,58 @@ sub process { · 7 unmodified lines | ||
| 2645 | 2795 | $opv eq '*U' || $opv eq '-U' || |
| 2646 | 2796 | $opv eq '&U' || $opv eq '&&U') { |
| 2647 | 2797 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
| 2648 | − ERROR("SPACING", | |
| 2649 | − "space required before that '$op' $at\n" . $hereptr); | |
| 2798 | + if (ERROR("SPACING", | |
| 2799 | + "space required before that '$op' $at\n" . $hereptr)) { | |
| 2800 | + $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); | |
| 2801 | + $line_fixed = 1; | |
| 2802 | + } | |
| 2650 | 2803 | } |
| 2651 | 2804 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
| 2652 | 2805 | # A unary '*' may be const |
| 2653 | 2806 | |
| 2654 | 2807 | } elsif ($ctx =~ /.xW/) { |
| 2655 | − ERROR("SPACING", | |
| 2656 | − "space prohibited after that '$op' $at\n" . $hereptr); | |
| 2808 | + if (ERROR("SPACING", | |
| 2809 | + "space prohibited after that '$op' $at\n" . $hereptr)) { | |
| 2810 | + $fixed_line =~ s/\s+$//; | |
| 2811 | + $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); | |
| 2812 | + $line_fixed = 1; | |
| 2813 | + if (defined $fix_elements[$n + 2]) { | |
| 2814 | + $fix_elements[$n + 2] =~ s/^\s+//; | |
| 2815 | + } | |
| 2816 | + } | |
| 2657 | 2817 | } |
| 2658 | 2818 | |
| 2659 | 2819 | # unary ++ and unary -- are allowed no space on one side. |
| 2660 | 2820 | } elsif ($op eq '++' or $op eq '--') { |
| 2661 | 2821 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
| 2662 | − ERROR("SPACING", | |
| 2663 | − "space required one side of that '$op' $at\n" . $hereptr); | |
| 2822 | + if (ERROR("SPACING", | |
| 2823 | + "space required one side of that '$op' $at\n" . $hereptr)) { | |
| 2824 | + $fixed_line =~ s/\s+$//; | |
| 2825 | + $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " "; | |
| 2826 | + $line_fixed = 1; | |
| 2827 | + } | |
| 2664 | 2828 | } |
| 2665 | 2829 | if ($ctx =~ /Wx[BE]/ || |
| 2666 | 2830 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
| 2667 | − ERROR("SPACING", | |
| 2668 | − "space prohibited before that '$op' $at\n" . $hereptr); | |
| 2831 | + if (ERROR("SPACING", | |
| 2832 | + "space prohibited before that '$op' $at\n" . $hereptr)) { | |
| 2833 | + $fixed_line =~ s/\s+$//; | |
| 2834 | + $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); | |
| 2835 | + $line_fixed = 1; | |
| 2836 | + } | |
| 2669 | 2837 | } |
| 2670 | 2838 | if ($ctx =~ /ExW/) { |
| 2671 | − ERROR("SPACING", | |
| 2672 | − "space prohibited after that '$op' $at\n" . $hereptr); | |
| 2839 | + if (ERROR("SPACING", | |
| 2840 | + "space prohibited after that '$op' $at\n" . $hereptr)) { | |
| 2841 | + $fixed_line =~ s/\s+$//; | |
| 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 | + } | |
| 2673 | 2848 | } |
| 2674 | 2849 | |
| 2675 | − | |
| 2676 | 2850 | # << and >> may either have or not have spaces both sides |
| 2677 | 2851 | } elsif ($op eq '<<' or $op eq '>>' or |
| 2678 | 2852 | $op eq '&' or $op eq '^' or $op eq '|' or |
| @@ -2681,17 +2855,23 @@ sub process { · 2 unmodified lines | ||
| 2681 | 2855 | $op eq '%') |
| 2682 | 2856 | { |
| 2683 | 2857 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
| 2684 | − ERROR("SPACING", | |
| 2685 | − "need consistent spacing around '$op' $at\n" . | |
| 2686 | − $hereptr); | |
| 2858 | + if (ERROR("SPACING", | |
| 2859 | + "need consistent spacing around '$op' $at\n" . $hereptr)) { | |
| 2860 | + $fixed_line =~ s/\s+$//; | |
| 2861 | + $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; | |
| 2862 | + $line_fixed = 1; | |
| 2863 | + } | |
| 2687 | 2864 | } |
| 2688 | 2865 | |
| 2689 | 2866 | # A colon needs no spaces before when it is |
| 2690 | 2867 | # terminating a case value or a label. |
| 2691 | 2868 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 2692 | 2869 | if ($ctx =~ /Wx./) { |
| 2693 | − ERROR("SPACING", | |
| 2694 | − "space prohibited before that '$op' $at\n" . $hereptr); | |
| 2870 | + if (ERROR("SPACING", | |
| 2871 | + "space prohibited before that '$op' $at\n" . $hereptr)) { | |
| 2872 | + $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); | |
| 2873 | + $line_fixed = 1; | |
| 2874 | + } | |
| 2695 | 2875 | } |
| 2696 | 2876 | |
| 2697 | 2877 | # All the others need spaces both sides. |
| @@ -2714,12 +2894,30 @@ sub process { · 16 unmodified lines | ||
| 2714 | 2894 | } |
| 2715 | 2895 | |
| 2716 | 2896 | if ($ok == 0) { |
| 2717 | − ERROR("SPACING", | |
| 2718 | − "spaces required around that '$op' $at\n" . $hereptr); | |
| 2897 | + if (ERROR("SPACING", | |
| 2898 | + "spaces required around that '$op' $at\n" . $hereptr)) { | |
| 2899 | + $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; | |
| 2900 | + $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " "; | |
| 2901 | + $line_fixed = 1; | |
| 2902 | + } | |
| 2719 | 2903 | } |
| 2720 | 2904 | } |
| 2721 | 2905 | $off += length($elements[$n + 1]); |
| 2906 | + | |
| 2907 | +## print("n: <$n> GOOD: <$good>\n"); | |
| 2908 | + | |
| 2909 | + $fixed_line = $fixed_line . $good; | |
| 2910 | + } | |
| 2911 | + | |
| 2912 | + if (($#elements % 2) == 0) { | |
| 2913 | + $fixed_line = $fixed_line . $fix_elements[$#elements]; | |
| 2722 | 2914 | } |
| 2915 | + | |
| 2916 | + if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) { | |
| 2917 | + $fixed[$linenr - 1] = $fixed_line; | |
| 2918 | + } | |
| 2919 | + | |
| 2920 | + | |
| 2723 | 2921 | } |
| 2724 | 2922 | |
| 2725 | 2923 | # check for multiple assignments |
| @@ -2747,8 +2945,12 @@ sub process { · 21 unmodified lines | ||
| 2747 | 2945 | #need space before brace following if, while, etc |
| 2748 | 2946 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 2749 | 2947 | $line =~ /do{/) { |
| 2750 | − ERROR("SPACING", | |
| 2751 | − "space required before the open brace '{'\n" . $herecurr); | |
| 2948 | + if (ERROR("SPACING", | |
| 2949 | + "space required before the open brace '{'\n" . $herecurr) && | |
| 2950 | + $fix) { | |
| 2951 | + $fixed[$linenr - 1] =~ | |
| 2952 | + s/^(\+.*(?:do|\))){/$1 {/; | |
| 2953 | + } | |
| 2752 | 2954 | } |
| 2753 | 2955 | |
| 2754 | 2956 | ## # check for blank lines before declarations |
| @@ -2768,32 +2970,52 @@ sub process { · 13 unmodified lines | ||
| 2768 | 2970 | |
| 2769 | 2971 | # check spacing on square brackets |
| 2770 | 2972 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
| 2771 | − ERROR("SPACING", | |
| 2772 | − "space prohibited after that open square bracket '['\n" . $herecurr); | |
| 2973 | + if (ERROR("SPACING", | |
| 2974 | + "space prohibited after that open square bracket '['\n" . $herecurr) && | |
| 2975 | + $fix) { | |
| 2976 | + $fixed[$linenr - 1] =~ | |
| 2977 | + s/\[\s+/\[/; | |
| 2978 | + } | |
| 2773 | 2979 | } |
| 2774 | 2980 | if ($line =~ /\s\]/) { |
| 2775 | − ERROR("SPACING", | |
| 2776 | − "space prohibited before that close square bracket ']'\n" . $herecurr); | |
| 2981 | + if (ERROR("SPACING", | |
| 2982 | + "space prohibited before that close square bracket ']'\n" . $herecurr) && | |
| 2983 | + $fix) { | |
| 2984 | + $fixed[$linenr - 1] =~ | |
| 2985 | + s/\s+\]/\]/; | |
| 2986 | + } | |
| 2777 | 2987 | } |
| 2778 | 2988 | |
| 2779 | 2989 | # check spacing on parentheses |
| 2780 | 2990 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 2781 | 2991 | $line !~ /for\s*\(\s+;/) { |
| 2782 | − ERROR("SPACING", | |
| 2783 | − "space prohibited after that open parenthesis '('\n" . $herecurr); | |
| 2992 | + if (ERROR("SPACING", | |
| 2993 | + "space prohibited after that open parenthesis '('\n" . $herecurr) && | |
| 2994 | + $fix) { | |
| 2995 | + $fixed[$linenr - 1] =~ | |
| 2996 | + s/\(\s+/\(/; | |
| 2997 | + } | |
| 2784 | 2998 | } |
| 2785 | 2999 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
| 2786 | 3000 | $line !~ /for\s*\(.*;\s+\)/ && |
| 2787 | 3001 | $line !~ /:\s+\)/) { |
| 2788 | − ERROR("SPACING", | |
| 2789 | − "space prohibited before that close parenthesis ')'\n" . $herecurr); | |
| 3002 | + if (ERROR("SPACING", | |
| 3003 | + "space prohibited before that close parenthesis ')'\n" . $herecurr) && | |
| 3004 | + $fix) { | |
| 3005 | + $fixed[$linenr - 1] =~ | |
| 3006 | + s/\s+\)/\)/; | |
| 3007 | + } | |
| 2790 | 3008 | } |
| 2791 | 3009 | |
| 2792 | 3010 | #goto labels aren't indented, allow a single space however |
| 2793 | 3011 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
| 2794 | 3012 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
| 2795 | − WARN("INDENTED_LABEL", | |
| 2796 | − "labels should not be indented\n" . $herecurr); | |
| 3013 | + if (WARN("INDENTED_LABEL", | |
| 3014 | + "labels should not be indented\n" . $herecurr) && | |
| 3015 | + $fix) { | |
| 3016 | + $fixed[$linenr - 1] =~ | |
| 3017 | + s/^(.)\s+/$1/; | |
| 3018 | + } | |
| 2797 | 3019 | } |
| 2798 | 3020 | |
| 2799 | 3021 | # Return is not a function. |
| @@ -2830,8 +3052,13 @@ sub process { · 30 unmodified lines | ||
| 2830 | 3052 | } |
| 2831 | 3053 | |
| 2832 | 3054 | # Need a space before open parenthesis after if, while etc |
| 2833 | − if ($line=~/\b(if|while|for|switch)\(/) { | |
| 2834 | − ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); | |
| 3055 | + if ($line =~ /\b(if|while|for|switch)\(/) { | |
| 3056 | + if (ERROR("SPACING", | |
| 3057 | + "space required before the open parenthesis '('\n" . $herecurr) && | |
| 3058 | + $fix) { | |
| 3059 | + $fixed[$linenr - 1] =~ | |
| 3060 | + s/\b(if|while|for|switch)\(/$1 \(/; | |
| 3061 | + } | |
| 2835 | 3062 | } |
| 2836 | 3063 | |
| 2837 | 3064 | # Check for illegal assignment in if conditional -- and check for trailing |
| @@ -3329,8 +3556,13 @@ sub process { · 491 unmodified lines | ||
| 3329 | 3556 | |
| 3330 | 3557 | # warn about spacing in #ifdefs |
| 3331 | 3558 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
| 3332 | − ERROR("SPACING", | |
| 3333 | − "exactly one space required after that #$1\n" . $herecurr); | |
| 3559 | + if (ERROR("SPACING", | |
| 3560 | + "exactly one space required after that #$1\n" . $herecurr) && | |
| 3561 | + $fix) { | |
| 3562 | + $fixed[$linenr - 1] =~ | |
| 3563 | + s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; | |
| 3564 | + } | |
| 3565 | + | |
| 3334 | 3566 | } |
| 3335 | 3567 | |
| 3336 | 3568 | # check for spinlock_t definitions without a comment. |
| @@ -3793,6 +4025,40 @@ sub process { · 456 unmodified lines | ||
| 3793 | 4025 | print "\n\n"; |
| 3794 | 4026 | } |
| 3795 | 4027 | |
| 4028 | + if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { | |
| 4029 | + my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; | |
| 4030 | + my $linecount = 0; | |
| 4031 | + my $f; | |
| 4032 | + | |
| 4033 | + open($f, '>', $newfile) | |
| 4034 | + or die "$P: Can't open $newfile for write\n"; | |
| 4035 | + foreach my $fixed_line (@fixed) { | |
| 4036 | + $linecount++; | |
| 4037 | + if ($file) { | |
| 4038 | + if ($linecount > 3) { | |
| 4039 | + $fixed_line =~ s/^\+//; | |
| 4040 | + print $f $fixed_line. "\n"; | |
| 4041 | + } | |
| 4042 | + } else { | |
| 4043 | + print $f $fixed_line . "\n"; | |
| 4044 | + } | |
| 4045 | + } | |
| 4046 | + close($f); | |
| 4047 | + | |
| 4048 | + if (!$quiet) { | |
| 4049 | + print << "EOM"; | |
| 4050 | +Wrote EXPERIMENTAL --fix correction(s) to '$newfile' | |
| 4051 | + | |
| 4052 | +Do _NOT_ trust the results written to this file. | |
| 4053 | +Do _NOT_ submit these changes without inspecting them for correctness. | |
| 4054 | + | |
| 4055 | +This EXPERIMENTAL file is simply a convenience to help rewrite patches. | |
| 4056 | +No warranties, expressed or implied... | |
| 4057 | + | |
| 4058 | +EOM | |
| 4059 | + } | |
| 4060 | + } | |
| 4061 | + | |
| 3796 | 4062 | if ($clean == 1 && $quiet == 0) { |
| 3797 | 4063 | print "$vname has no obvious style problems and is ready for submission.\n" |
| 3798 | 4064 | } |