History

checkpatch: create an EXPERIMENTAL --fix option to correct patches

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.

Joe Perches committed 13 years ago

Commit 3705ce51 parent 23f780c1 file · +354 −88Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+354−88
@@ -27,6 +27,7 @@ my $summary = 1; · 26 unmodified lines
27my $mailback = 0;27my $mailback = 0;
28my $summary_file = 0;28my $summary_file = 0;
29my $show_types = 0;29my $show_types = 0;
30my $fix = 0;
30my $root;31my $root;
31my %debug;32my %debug;
32my %ignore_type = ();33my %ignore_type = ();
@@ -63,6 +64,11 @@ Options: · 30 unmodified lines
63 is all off)64 is all off)
64 --test-only=WORD report only warnings/errors containing WORD65 --test-only=WORD report only warnings/errors containing WORD
65 literally66 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 -h, --help, --version display this help and exit72 -h, --help, --version display this help and exit
67 73
68When FILE is - read standard input.74When FILE is - read standard input.
@@ -114,7 +120,7 @@ GetOptions( · 45 unmodified lines
114 'summary!' => \$summary,120 'summary!' => \$summary,
115 'mailback!' => \$mailback,121 'mailback!' => \$mailback,
116 'summary-file!' => \$summary_file,122 'summary-file!' => \$summary_file,
117 123 'fix!' => \$fix,
118 'debug=s' => \%debug,124 'debug=s' => \%debug,
119 'test-only=s' => \$tst_only,125 'test-only=s' => \$tst_only,
120 'h|help' => \$help,126 'h|help' => \$help,
@@ -367,6 +373,7 @@ $chk_signoff = 0 if ($file); · 246 unmodified lines
367 373
368my @rawlines = ();374my @rawlines = ();
369my @lines = ();375my @lines = ();
376my @fixed = ();
370my $vname;377my $vname;
371for my $filename (@ARGV) {378for my $filename (@ARGV) {
372 my $FILE;379 my $FILE;
@@ -394,6 +401,7 @@ for my $filename (@ARGV) { · 21 unmodified lines
394 }401 }
395 @rawlines = ();402 @rawlines = ();
396 @lines = ();403 @lines = ();
404 @fixed = ();
397}405}
398 406
399exit($exit);407exit($exit);
@@ -434,7 +442,7 @@ sub parse_email { · 34 unmodified lines
434 $comment = $2 if defined $2;442 $comment = $2 if defined $2;
435 $formatted_email =~ s/$address.*$//;443 $formatted_email =~ s/$address.*$//;
436 $name = $formatted_email;444 $name = $formatted_email;
437 $name =~ s/^\s+|\s+$//g;445 $name = trim($name);
438 $name =~ s/^\"|\"$//g;446 $name =~ s/^\"|\"$//g;
439 # If there's a name left after stripping spaces and447 # If there's a name left after stripping spaces and
440 # leading quotes, and the address doesn't have both448 # 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 $name =~ s/^\"|\"$//g;461 $name =~ s/^\"|\"$//g;
454 $address =~ s/^\s+|\s+$//g;462 $address = trim($address);
455 $address =~ s/^\<|\>$//g;463 $address =~ s/^\<|\>$//g;
456 464
457 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars465 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
@@ -467,9 +475,9 @@ sub format_email { · 9 unmodified lines
467 475
468 my $formatted_email;476 my $formatted_email;
469 477
470 $name =~ s/^\s+|\s+$//g;478 $name = trim($name);
471 $name =~ s/^\"|\"$//g;479 $name =~ s/^\"|\"$//g;
472 $address =~ s/^\s+|\s+$//g;480 $address = trim($address);
473 481
474 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars482 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
475 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes483 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
@@ -1291,19 +1299,25 @@ sub ERROR { · 815 unmodified lines
1291 if (report("ERROR", $_[0], $_[1])) {1299 if (report("ERROR", $_[0], $_[1])) {
1292 our $clean = 0;1300 our $clean = 0;
1293 our $cnt_error++;1301 our $cnt_error++;
1302 return 1;
1294 }1303 }
1304 return 0;
1295}1305}
1296sub WARN {1306sub WARN {
1297 if (report("WARNING", $_[0], $_[1])) {1307 if (report("WARNING", $_[0], $_[1])) {
1298 our $clean = 0;1308 our $clean = 0;
1299 our $cnt_warn++;1309 our $cnt_warn++;
1310 return 1;
1300 }1311 }
1312 return 0;
1301}1313}
1302sub CHK {1314sub CHK {
1303 if ($check && report("CHECK", $_[0], $_[1])) {1315 if ($check && report("CHECK", $_[0], $_[1])) {
1304 our $clean = 0;1316 our $clean = 0;
1305 our $cnt_chk++;1317 our $cnt_chk++;
1318 return 1;
1306 }1319 }
1320 return 0;
1307}1321}
1308 1322
1309sub check_absolute_file {1323sub check_absolute_file {
@@ -1334,6 +1348,29 @@ sub check_absolute_file { · 24 unmodified lines
1334 }1348 }
1335}1349}
1336 1350
1351sub trim {
1352 my ($string) = @_;
1353
1354 $string =~ s/(^\s+|\s+$)//g;
1355
1356 return $string;
1357}
1358
1359sub 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
1337sub pos_last_openparen {1374sub pos_last_openparen {
1338 my ($line) = @_;1375 my ($line) = @_;
1339 1376
@@ -1425,6 +1462,8 @@ sub process { · 85 unmodified lines
1425 $linenr++;1462 $linenr++;
1426 $line = $rawline;1463 $line = $rawline;
1427 1464
1465 push(@fixed, $rawline) if ($fix);
1466
1428 if ($rawline=~/^\+\+\+\s+(\S+)/) {1467 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1429 $setup_docs = 0;1468 $setup_docs = 0;
1430 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {1469 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
@@ -1616,16 +1655,29 @@ sub process { · 185 unmodified lines
1616 "Non-standard signature: $sign_off\n" . $herecurr);1655 "Non-standard signature: $sign_off\n" . $herecurr);
1617 }1656 }
1618 if (defined $space_before && $space_before ne "") {1657 if (defined $space_before && $space_before ne "") {
1619 WARN("BAD_SIGN_OFF",1658 if (WARN("BAD_SIGN_OFF",
1620 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);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 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {1665 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1623 WARN("BAD_SIGN_OFF",1666 if (WARN("BAD_SIGN_OFF",
1624 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr);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 if (!defined $space_after || $space_after ne " ") {1674 if (!defined $space_after || $space_after ne " ") {
1627 WARN("BAD_SIGN_OFF",1675 if (WARN("BAD_SIGN_OFF",
1628 "Use a single space after $ucfirst_sign_off\n" . $herecurr);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 my ($email_name, $email_address, $comment) = parse_email($email);1683 my ($email_name, $email_address, $comment) = parse_email($email);
@@ -1715,8 +1767,12 @@ sub process { · 83 unmodified lines
1715 1767
1716 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {1768 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1717 my $herevet = "$here\n" . cat_vet($rawline) . "\n";1769 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1718 ERROR("TRAILING_WHITESPACE",1770 if (ERROR("TRAILING_WHITESPACE",
1719 "trailing whitespace\n" . $herevet);1771 "trailing whitespace\n" . $herevet) &&
1772 $fix) {
1773 $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
1774 }
1775
1720 $rpt_cleaners = 1;1776 $rpt_cleaners = 1;
1721 }1777 }
1722 1778
@@ -1811,8 +1867,12 @@ sub process { · 88 unmodified lines
1811 1867
1812# check for spaces before a quoted newline1868# check for spaces before a quoted newline
1813 if ($rawline =~ /^.*\".*\s\\n/) {1869 if ($rawline =~ /^.*\".*\s\\n/) {
1814 WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",1870 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1815 "unnecessary whitespace before a quoted newline\n" . $herecurr);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# check for adding lines without a newline.1878# check for adding lines without a newline.
@@ -1843,16 +1903,23 @@ sub process { · 24 unmodified lines
1843 if ($rawline =~ /^\+\s* \t\s*\S/ ||1903 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1844 $rawline =~ /^\+\s* \s*/) {1904 $rawline =~ /^\+\s* \s*/) {
1845 my $herevet = "$here\n" . cat_vet($rawline) . "\n";1905 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1846 ERROR("CODE_INDENT",
1847 "code indent should use tabs where possible\n" . $herevet);
1848 $rpt_cleaners = 1;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# check for space before tabs.1914# check for space before tabs.
1852 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {1915 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1853 my $herevet = "$here\n" . cat_vet($rawline) . "\n";1916 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1854 WARN("SPACE_BEFORE_TAB",1917 if (WARN("SPACE_BEFORE_TAB",
1855 "please, no space before tabs\n" . $herevet);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# check for && or || at the start of a line1925# check for && or || at the start of a line
@@ -1880,15 +1947,24 @@ sub process { · 21 unmodified lines
1880 1947
1881 if ($newindent ne $goodtabindent &&1948 if ($newindent ne $goodtabindent &&
1882 $newindent ne $goodspaceindent) {1949 $newindent ne $goodspaceindent) {
1883 CHK("PARENTHESIS_ALIGNMENT",1950
1884 "Alignment should match open parenthesis\n" . $hereprev);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 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {1961 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
1890 CHK("SPACING",1962 if (CHK("SPACING",
1891 "No space is necessary after a cast\n" . $hereprev);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 if ($realfile =~ m@^(drivers/net/|net/)@ &&1970 if ($realfile =~ m@^(drivers/net/|net/)@ &&
@@ -1920,10 +1996,13 @@ sub process { · 25 unmodified lines
1920# 1) within comments1996# 1) within comments
1921# 2) indented preprocessor commands1997# 2) indented preprocessor commands
1922# 3) hanging labels1998# 3) hanging labels
1923 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {1999 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
1924 my $herevet = "$here\n" . cat_vet($rawline) . "\n";2000 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1925 WARN("LEADING_SPACE",2001 if (WARN("LEADING_SPACE",
1926 "please, no spaces at the start of a line\n" . $herevet);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# check we are in a valid C source file if not then ignore this hunk2008# 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 $prev_values = substr($curr_values, -1);2292 $prev_values = substr($curr_values, -1);
2214 2293
2215#ignore lines not being added2294#ignore lines not being added
2216 if ($line=~/^[^\+]/) {next;}2295 next if ($line =~ /^[^\+]/);
2217 2296
2218# TEST: allow direct testing of the type matcher.2297# TEST: allow direct testing of the type matcher.
2219 if ($dbg_type) {2298 if ($dbg_type) {
@@ -2264,8 +2343,15 @@ sub process { · 44 unmodified lines
2264 2343
2265# no C99 // comments2344# no C99 // comments
2266 if ($line =~ m{//}) {2345 if ($line =~ m{//}) {
2267 ERROR("C99_COMMENTS",2346 if (ERROR("C99_COMMENTS",
2268 "do not use C99 // comments\n" . $herecurr);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 # Remove C99 comments.2356 # Remove C99 comments.
2271 $line =~ s@//.*@@;2357 $line =~ s@//.*@@;
@@ -2364,7 +2450,7 @@ sub process { · 92 unmodified lines
2364 # (char*[ const])2450 # (char*[ const])
2365 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {2451 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2366 #print "AA<$1>\n";2452 #print "AA<$1>\n";
2367 my ($from, $to) = ($2, $2);2453 my ($ident, $from, $to) = ($1, $2, $2);
2368 2454
2369 # Should start with a space.2455 # Should start with a space.
2370 $to =~ s/^(\S)/ $1/;2456 $to =~ s/^(\S)/ $1/;
@@ -2374,15 +2460,22 @@ sub process { · 3 unmodified lines
2374 while ($to =~ s/\*\s+\*/\*\*/) {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 if ($from ne $to) {2464 if ($from ne $to) {
2379 ERROR("POINTER_LOCATION",2465 if (ERROR("POINTER_LOCATION",
2380 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);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 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {2476 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2384 #print "BB<$1>\n";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 # Should start with a space.2480 # Should start with a space.
2388 $to =~ s/^(\S)/ $1/;2481 $to =~ s/^(\S)/ $1/;
@@ -2394,10 +2487,18 @@ sub process { · 5 unmodified lines
2394 # Modifiers should have spaces.2487 # Modifiers should have spaces.
2395 $to =~ s/(\b$Modifier$)/$1 /;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 if ($from ne $to && $ident !~ /^$Modifier$/) {2491 if ($from ne $to && $ident !~ /^$Modifier$/) {
2399 ERROR("POINTER_LOCATION",2492 if (ERROR("POINTER_LOCATION",
2400 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);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# missing space after union, struct or enum definition2586# missing space after union, struct or enum definition
2486 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {2587 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2487 WARN("SPACING",2588 if (WARN("SPACING",
2488 "missing space after $1 definition\n" . $herecurr);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# check for spacing round square brackets; allowed:2596# check for spacing round square brackets; allowed:
@@ -2497,8 +2602,12 @@ sub process { · 5 unmodified lines
2497 if ($prefix !~ /$Type\s+$/ &&2602 if ($prefix !~ /$Type\s+$/ &&
2498 ($where != 0 || $prefix !~ /^.\s+$/) &&2603 ($where != 0 || $prefix !~ /^.\s+$/) &&
2499 $prefix !~ /[{,]\s+$/) {2604 $prefix !~ /[{,]\s+$/) {
2500 ERROR("BRACKET_SPACE",2605 if (ERROR("BRACKET_SPACE",
2501 "space prohibited before open square bracket '['\n" . $herecurr);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 __attribute__|format|__extension__|2624 __attribute__|format|__extension__|
2516 asm|__asm__)$/x)2625 asm|__asm__)$/x)
2517 {2626 {
2518
2519 # cpp #define statements have non-optional spaces, ie2627 # cpp #define statements have non-optional spaces, ie
2520 # if there is a space between the name and the open2628 # if there is a space between the name and the open
2521 # parenthesis it is simply not a parameter group.2629 # parenthesis it is simply not a parameter group.
@@ -2529,19 +2637,30 @@ sub process { · 7 unmodified lines
2529 } elsif ($ctx =~ /$Type$/) {2637 } elsif ($ctx =~ /$Type$/) {
2530 2638
2531 } else {2639 } else {
2532 WARN("SPACING",2640 if (WARN("SPACING",
2533 "space prohibited between function name and open parenthesis '('\n" . $herecurr);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# check for whitespace before a non-naked semicolon2649# check for whitespace before a non-naked semicolon
2538 if ($line =~ /^\+.*\S\s+;/) {2650 if ($line =~ /^\+.*\S\s+;/) {
2539 WARN("SPACING",2651 if (WARN("SPACING",
2540 "space prohibited before semicolon\n" . $herecurr);2652 "space prohibited before semicolon\n" . $herecurr) &&
2653 $fix) {
2654 $fixed[$linenr - 1] =~
2655 s/^(\+.*\S)\s+;/$1;/;
2656 }
2541 }2657 }
2542 2658
2543# Check operator spacing.2659# Check operator spacing.
2544 if (!($line=~/\#\s*include/)) {2660 if (!($line=~/\#\s*include/)) {
2661 my $fixed_line = "";
2662 my $line_fixed = 0;
2663
2545 my $ops = qr{2664 my $ops = qr{
2546 <<=|>>=|<=|>=|==|!=|2665 <<=|>>=|<=|>=|==|!=|
2547 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|2666 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
@@ -2550,11 +2669,30 @@ sub process { · 2 unmodified lines
2550 \?|:2669 \?|:
2551 }x;2670 }x;
2552 my @elements = split(/($ops|;)/, $opline);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 my $off = 0;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 my $blank = copy_spacing($opline);2688 my $blank = copy_spacing($opline);
2556 2689
2557 for (my $n = 0; $n < $#elements; $n += 2) {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 $off += length($elements[$n]);2696 $off += length($elements[$n]);
2559 2697
2560 # Pick up the preceding and succeeding characters.2698 # Pick up the preceding and succeeding characters.
@@ -2611,8 +2749,11 @@ sub process { · 50 unmodified lines
2611 } elsif ($op eq ';') {2749 } elsif ($op eq ';') {
2612 if ($ctx !~ /.x[WEBC]/ &&2750 if ($ctx !~ /.x[WEBC]/ &&
2613 $cc !~ /^\\/ && $cc !~ /^;/) {2751 $cc !~ /^\\/ && $cc !~ /^;/) {
2614 ERROR("SPACING",2752 if (ERROR("SPACING",
2615 "space required after that '$op' $at\n" . $hereptr);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 # // is a comment2759 # // is a comment
@@ -2623,15 +2764,24 @@ sub process { · 4 unmodified lines
2623 # : when part of a bitfield2764 # : when part of a bitfield
2624 } elsif ($op eq '->' || $opv eq ':B') {2765 } elsif ($op eq '->' || $opv eq ':B') {
2625 if ($ctx =~ /Wx.|.xW/) {2766 if ($ctx =~ /Wx.|.xW/) {
2626 ERROR("SPACING",2767 if (ERROR("SPACING",
2627 "spaces prohibited around that '$op' $at\n" . $hereptr);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 # , must have a space on the right.2777 # , must have a space on the right.
2631 } elsif ($op eq ',') {2778 } elsif ($op eq ',') {
2632 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {2779 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2633 ERROR("SPACING",2780 if (ERROR("SPACING",
2634 "space required after that '$op' $at\n" . $hereptr);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 # '*' as part of a type definition -- reported already.2787 # '*' as part of a type definition -- reported already.
@@ -2645,34 +2795,58 @@ sub process { · 7 unmodified lines
2645 $opv eq '*U' || $opv eq '-U' ||2795 $opv eq '*U' || $opv eq '-U' ||
2646 $opv eq '&U' || $opv eq '&&U') {2796 $opv eq '&U' || $opv eq '&&U') {
2647 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {2797 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2648 ERROR("SPACING",2798 if (ERROR("SPACING",
2649 "space required before that '$op' $at\n" . $hereptr);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 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {2804 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2652 # A unary '*' may be const2805 # A unary '*' may be const
2653 2806
2654 } elsif ($ctx =~ /.xW/) {2807 } elsif ($ctx =~ /.xW/) {
2655 ERROR("SPACING",2808 if (ERROR("SPACING",
2656 "space prohibited after that '$op' $at\n" . $hereptr);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 # unary ++ and unary -- are allowed no space on one side.2819 # unary ++ and unary -- are allowed no space on one side.
2660 } elsif ($op eq '++' or $op eq '--') {2820 } elsif ($op eq '++' or $op eq '--') {
2661 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {2821 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2662 ERROR("SPACING",2822 if (ERROR("SPACING",
2663 "space required one side of that '$op' $at\n" . $hereptr);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 if ($ctx =~ /Wx[BE]/ ||2829 if ($ctx =~ /Wx[BE]/ ||
2666 ($ctx =~ /Wx./ && $cc =~ /^;/)) {2830 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2667 ERROR("SPACING",2831 if (ERROR("SPACING",
2668 "space prohibited before that '$op' $at\n" . $hereptr);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 if ($ctx =~ /ExW/) {2838 if ($ctx =~ /ExW/) {
2671 ERROR("SPACING",2839 if (ERROR("SPACING",
2672 "space prohibited after that '$op' $at\n" . $hereptr);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 # << and >> may either have or not have spaces both sides2850 # << and >> may either have or not have spaces both sides
2677 } elsif ($op eq '<<' or $op eq '>>' or2851 } elsif ($op eq '<<' or $op eq '>>' or
2678 $op eq '&' or $op eq '^' or $op eq '|' or2852 $op eq '&' or $op eq '^' or $op eq '|' or
@@ -2681,17 +2855,23 @@ sub process { · 2 unmodified lines
2681 $op eq '%')2855 $op eq '%')
2682 {2856 {
2683 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {2857 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2684 ERROR("SPACING",2858 if (ERROR("SPACING",
2685 "need consistent spacing around '$op' $at\n" .2859 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2686 $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 # A colon needs no spaces before when it is2866 # A colon needs no spaces before when it is
2690 # terminating a case value or a label.2867 # terminating a case value or a label.
2691 } elsif ($opv eq ':C' || $opv eq ':L') {2868 } elsif ($opv eq ':C' || $opv eq ':L') {
2692 if ($ctx =~ /Wx./) {2869 if ($ctx =~ /Wx./) {
2693 ERROR("SPACING",2870 if (ERROR("SPACING",
2694 "space prohibited before that '$op' $at\n" . $hereptr);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 # All the others need spaces both sides.2877 # All the others need spaces both sides.
@@ -2714,12 +2894,30 @@ sub process { · 16 unmodified lines
2714 }2894 }
2715 2895
2716 if ($ok == 0) {2896 if ($ok == 0) {
2717 ERROR("SPACING",2897 if (ERROR("SPACING",
2718 "spaces required around that '$op' $at\n" . $hereptr);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 $off += length($elements[$n + 1]);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# check for multiple assignments2923# check for multiple assignments
@@ -2747,8 +2945,12 @@ sub process { · 21 unmodified lines
2747#need space before brace following if, while, etc2945#need space before brace following if, while, etc
2748 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||2946 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2749 $line =~ /do{/) {2947 $line =~ /do{/) {
2750 ERROR("SPACING",2948 if (ERROR("SPACING",
2751 "space required before the open brace '{'\n" . $herecurr);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## # check for blank lines before declarations2956## # check for blank lines before declarations
@@ -2768,32 +2970,52 @@ sub process { · 13 unmodified lines
2768 2970
2769# check spacing on square brackets2971# check spacing on square brackets
2770 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {2972 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2771 ERROR("SPACING",2973 if (ERROR("SPACING",
2772 "space prohibited after that open square bracket '['\n" . $herecurr);2974 "space prohibited after that open square bracket '['\n" . $herecurr) &&
2975 $fix) {
2976 $fixed[$linenr - 1] =~
2977 s/\[\s+/\[/;
2978 }
2773 }2979 }
2774 if ($line =~ /\s\]/) {2980 if ($line =~ /\s\]/) {
2775 ERROR("SPACING",2981 if (ERROR("SPACING",
2776 "space prohibited before that close square bracket ']'\n" . $herecurr);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# check spacing on parentheses2989# check spacing on parentheses
2780 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&2990 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2781 $line !~ /for\s*\(\s+;/) {2991 $line !~ /for\s*\(\s+;/) {
2782 ERROR("SPACING",2992 if (ERROR("SPACING",
2783 "space prohibited after that open parenthesis '('\n" . $herecurr);2993 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
2994 $fix) {
2995 $fixed[$linenr - 1] =~
2996 s/\(\s+/\(/;
2997 }
2784 }2998 }
2785 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&2999 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2786 $line !~ /for\s*\(.*;\s+\)/ &&3000 $line !~ /for\s*\(.*;\s+\)/ &&
2787 $line !~ /:\s+\)/) {3001 $line !~ /:\s+\)/) {
2788 ERROR("SPACING",3002 if (ERROR("SPACING",
2789 "space prohibited before that close parenthesis ')'\n" . $herecurr);3003 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3004 $fix) {
3005 $fixed[$linenr - 1] =~
3006 s/\s+\)/\)/;
3007 }
2790 }3008 }
2791 3009
2792#goto labels aren't indented, allow a single space however3010#goto labels aren't indented, allow a single space however
2793 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and3011 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
2794 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {3012 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
2795 WARN("INDENTED_LABEL",3013 if (WARN("INDENTED_LABEL",
2796 "labels should not be indented\n" . $herecurr);3014 "labels should not be indented\n" . $herecurr) &&
3015 $fix) {
3016 $fixed[$linenr - 1] =~
3017 s/^(.)\s+/$1/;
3018 }
2797 }3019 }
2798 3020
2799# Return is not a function.3021# Return is not a function.
@@ -2830,8 +3052,13 @@ sub process { · 30 unmodified lines
2830 }3052 }
2831 3053
2832# Need a space before open parenthesis after if, while etc3054# Need a space before open parenthesis after if, while etc
2833 if ($line=~/\b(if|while|for|switch)\(/) {3055 if ($line =~ /\b(if|while|for|switch)\(/) {
2834 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);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# Check for illegal assignment in if conditional -- and check for trailing3064# Check for illegal assignment in if conditional -- and check for trailing
@@ -3329,8 +3556,13 @@ sub process { · 491 unmodified lines
3329 3556
3330# warn about spacing in #ifdefs3557# warn about spacing in #ifdefs
3331 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {3558 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3332 ERROR("SPACING",3559 if (ERROR("SPACING",
3333 "exactly one space required after that #$1\n" . $herecurr);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# check for spinlock_t definitions without a comment.3568# check for spinlock_t definitions without a comment.
@@ -3793,6 +4025,40 @@ sub process { · 456 unmodified lines
3793 print "\n\n";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";
4050Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4051
4052Do _NOT_ trust the results written to this file.
4053Do _NOT_ submit these changes without inspecting them for correctness.
4054
4055This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4056No warranties, expressed or implied...
4057
4058EOM
4059 }
4060 }
4061
3796 if ($clean == 1 && $quiet == 0) {4062 if ($clean == 1 && $quiet == 0) {
3797 print "$vname has no obvious style problems and is ready for submission.\n"4063 print "$vname has no obvious style problems and is ready for submission.\n"
3798 }4064 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t