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
2727 my $mailback = 0;
2828 my $summary_file = 0;
2929 my $show_types = 0;
30+my $fix = 0;
3031 my $root;
3132 my %debug;
3233 my %ignore_type = ();
@@ -63,6 +64,11 @@ Options: · 30 unmodified lines
6364 is all off)
6465 --test-only=WORD report only warnings/errors containing WORD
6566 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
6672 -h, --help, --version display this help and exit
6773
6874 When FILE is - read standard input.
@@ -114,7 +120,7 @@ GetOptions( · 45 unmodified lines
114120 'summary!' => \$summary,
115121 'mailback!' => \$mailback,
116122 'summary-file!' => \$summary_file,
117−
123+ 'fix!' => \$fix,
118124 'debug=s' => \%debug,
119125 'test-only=s' => \$tst_only,
120126 'h|help' => \$help,
@@ -367,6 +373,7 @@ $chk_signoff = 0 if ($file); · 246 unmodified lines
367373
368374 my @rawlines = ();
369375 my @lines = ();
376+my @fixed = ();
370377 my $vname;
371378 for my $filename (@ARGV) {
372379 my $FILE;
@@ -394,6 +401,7 @@ for my $filename (@ARGV) { · 21 unmodified lines
394401 }
395402 @rawlines = ();
396403 @lines = ();
404+ @fixed = ();
397405 }
398406
399407 exit($exit);
@@ -434,7 +442,7 @@ sub parse_email { · 34 unmodified lines
434442 $comment = $2 if defined $2;
435443 $formatted_email =~ s/$address.*$//;
436444 $name = $formatted_email;
437− $name =~ s/^\s+|\s+$//g;
445+ $name = trim($name);
438446 $name =~ s/^\"|\"$//g;
439447 # If there's a name left after stripping spaces and
440448 # leading quotes, and the address doesn't have both
@@ -449,9 +457,9 @@ sub parse_email { · 8 unmodified lines
449457 }
450458 }
451459
452− $name =~ s/^\s+|\s+$//g;
460+ $name = trim($name);
453461 $name =~ s/^\"|\"$//g;
454− $address =~ s/^\s+|\s+$//g;
462+ $address = trim($address);
455463 $address =~ s/^\<|\>$//g;
456464
457465 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
@@ -467,9 +475,9 @@ sub format_email { · 9 unmodified lines
467475
468476 my $formatted_email;
469477
470− $name =~ s/^\s+|\s+$//g;
478+ $name = trim($name);
471479 $name =~ s/^\"|\"$//g;
472− $address =~ s/^\s+|\s+$//g;
480+ $address = trim($address);
473481
474482 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
475483 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
@@ -1291,19 +1299,25 @@ sub ERROR { · 815 unmodified lines
12911299 if (report("ERROR", $_[0], $_[1])) {
12921300 our $clean = 0;
12931301 our $cnt_error++;
1302+ return 1;
12941303 }
1304+ return 0;
12951305 }
12961306 sub WARN {
12971307 if (report("WARNING", $_[0], $_[1])) {
12981308 our $clean = 0;
12991309 our $cnt_warn++;
1310+ return 1;
13001311 }
1312+ return 0;
13011313 }
13021314 sub CHK {
13031315 if ($check && report("CHECK", $_[0], $_[1])) {
13041316 our $clean = 0;
13051317 our $cnt_chk++;
1318+ return 1;
13061319 }
1320+ return 0;
13071321 }
13081322
13091323 sub check_absolute_file {
@@ -1334,6 +1348,29 @@ sub check_absolute_file { · 24 unmodified lines
13341348 }
13351349 }
13361350
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+
13371374 sub pos_last_openparen {
13381375 my ($line) = @_;
13391376
@@ -1425,6 +1462,8 @@ sub process { · 85 unmodified lines
14251462 $linenr++;
14261463 $line = $rawline;
14271464
1465+ push(@fixed, $rawline) if ($fix);
1466+
14281467 if ($rawline=~/^\+\+\+\s+(\S+)/) {
14291468 $setup_docs = 0;
14301469 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
@@ -1616,16 +1655,29 @@ sub process { · 185 unmodified lines
16161655 "Non-standard signature: $sign_off\n" . $herecurr);
16171656 }
16181657 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+ }
16211664 }
16221665 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+
16251673 }
16261674 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+ }
16291681 }
16301682
16311683 my ($email_name, $email_address, $comment) = parse_email($email);
@@ -1715,8 +1767,12 @@ sub process { · 83 unmodified lines
17151767
17161768 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
17171769 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+
17201776 $rpt_cleaners = 1;
17211777 }
17221778
@@ -1811,8 +1867,12 @@ sub process { · 88 unmodified lines
18111867
18121868 # check for spaces before a quoted newline
18131869 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+
18161876 }
18171877
18181878 # check for adding lines without a newline.
@@ -1843,16 +1903,23 @@ sub process { · 24 unmodified lines
18431903 if ($rawline =~ /^\+\s* \t\s*\S/ ||
18441904 $rawline =~ /^\+\s* \s*/) {
18451905 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1846− ERROR("CODE_INDENT",
1847− "code indent should use tabs where possible\n" . $herevet);
18481906 $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+ }
18491912 }
18501913
18511914 # check for space before tabs.
18521915 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
18531916 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+ }
18561923 }
18571924
18581925 # check for && or || at the start of a line
@@ -1880,15 +1947,24 @@ sub process { · 21 unmodified lines
18801947
18811948 if ($newindent ne $goodtabindent &&
18821949 $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+ }
18851957 }
18861958 }
18871959 }
18881960
18891961 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+ }
18921968 }
18931969
18941970 if ($realfile =~ m@^(drivers/net/|net/)@ &&
@@ -1920,10 +1996,13 @@ sub process { · 25 unmodified lines
19201996 # 1) within comments
19211997 # 2) indented preprocessor commands
19221998 # 3) hanging labels
1923− if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1999+ if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
19242000 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+ }
19272006 }
19282007
19292008 # check we are in a valid C source file if not then ignore this hunk
@@ -2213,7 +2292,7 @@ sub process { · 283 unmodified lines
22132292 $prev_values = substr($curr_values, -1);
22142293
22152294 #ignore lines not being added
2216− if ($line=~/^[^\+]/) {next;}
2295+ next if ($line =~ /^[^\+]/);
22172296
22182297 # TEST: allow direct testing of the type matcher.
22192298 if ($dbg_type) {
@@ -2264,8 +2343,15 @@ sub process { · 44 unmodified lines
22642343
22652344 # no C99 // comments
22662345 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+ }
22692355 }
22702356 # Remove C99 comments.
22712357 $line =~ s@//.*@@;
@@ -2364,7 +2450,7 @@ sub process { · 92 unmodified lines
23642450 # (char*[ const])
23652451 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
23662452 #print "AA<$1>\n";
2367− my ($from, $to) = ($2, $2);
2453+ my ($ident, $from, $to) = ($1, $2, $2);
23682454
23692455 # Should start with a space.
23702456 $to =~ s/^(\S)/ $1/;
@@ -2374,15 +2460,22 @@ sub process { · 3 unmodified lines
23742460 while ($to =~ s/\*\s+\*/\*\*/) {
23752461 }
23762462
2377− #print "from<$from> to<$to>\n";
2463+## print "1: from<$from> to<$to> ident<$ident>\n";
23782464 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+ }
23812474 }
23822475 }
23832476 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
23842477 #print "BB<$1>\n";
2385− my ($from, $to, $ident) = ($2, $2, $3);
2478+ my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
23862479
23872480 # Should start with a space.
23882481 $to =~ s/^(\S)/ $1/;
@@ -2394,10 +2487,18 @@ sub process { · 5 unmodified lines
23942487 # Modifiers should have spaces.
23952488 $to =~ s/(\b$Modifier$)/$1 /;
23962489
2397− #print "from<$from> to<$to> ident<$ident>\n";
2490+## print "2: from<$from> to<$to> ident<$ident>\n";
23982491 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+ }
24012502 }
24022503 }
24032504
@@ -2483,9 +2584,13 @@ sub process { · 79 unmodified lines
24832584 }
24842585
24852586 # 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+ }
24892594 }
24902595
24912596 # check for spacing round square brackets; allowed:
@@ -2497,8 +2602,12 @@ sub process { · 5 unmodified lines
24972602 if ($prefix !~ /$Type\s+$/ &&
24982603 ($where != 0 || $prefix !~ /^.\s+$/) &&
24992604 $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+ }
25022611 }
25032612 }
25042613
@@ -2515,7 +2624,6 @@ sub process { · 10 unmodified lines
25152624 __attribute__|format|__extension__|
25162625 asm|__asm__)$/x)
25172626 {
2518−
25192627 # cpp #define statements have non-optional spaces, ie
25202628 # if there is a space between the name and the open
25212629 # parenthesis it is simply not a parameter group.
@@ -2529,19 +2637,30 @@ sub process { · 7 unmodified lines
25292637 } elsif ($ctx =~ /$Type$/) {
25302638
25312639 } 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+ }
25342646 }
25352647 }
25362648
25372649 # check for whitespace before a non-naked semicolon
25382650 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+ }
25412657 }
25422658
25432659 # Check operator spacing.
25442660 if (!($line=~/\#\s*include/)) {
2661+ my $fixed_line = "";
2662+ my $line_fixed = 0;
2663+
25452664 my $ops = qr{
25462665 <<=|>>=|<=|>=|==|!=|
25472666 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
@@ -2550,11 +2669,30 @@ sub process { · 2 unmodified lines
25502669 \?|:
25512670 }x;
25522671 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 = ();
25532679 my $off = 0;
25542680
2681+ foreach my $el (@elements) {
2682+ push(@fix_elements, substr($rawline, $off, length($el)));
2683+ $off += length($el);
2684+ }
2685+
2686+ $off = 0;
2687+
25552688 my $blank = copy_spacing($opline);
25562689
25572690 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+
25582696 $off += length($elements[$n]);
25592697
25602698 # Pick up the preceding and succeeding characters.
@@ -2611,8 +2749,11 @@ sub process { · 50 unmodified lines
26112749 } elsif ($op eq ';') {
26122750 if ($ctx !~ /.x[WEBC]/ &&
26132751 $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+ }
26162757 }
26172758
26182759 # // is a comment
@@ -2623,15 +2764,24 @@ sub process { · 4 unmodified lines
26232764 # : when part of a bitfield
26242765 } elsif ($op eq '->' || $opv eq ':B') {
26252766 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+ }
26282775 }
26292776
26302777 # , must have a space on the right.
26312778 } elsif ($op eq ',') {
26322779 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+ }
26352785 }
26362786
26372787 # '*' as part of a type definition -- reported already.
@@ -2645,34 +2795,58 @@ sub process { · 7 unmodified lines
26452795 $opv eq '*U' || $opv eq '-U' ||
26462796 $opv eq '&U' || $opv eq '&&U') {
26472797 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+ }
26502803 }
26512804 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
26522805 # A unary '*' may be const
26532806
26542807 } 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+ }
26572817 }
26582818
26592819 # unary ++ and unary -- are allowed no space on one side.
26602820 } elsif ($op eq '++' or $op eq '--') {
26612821 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+ }
26642828 }
26652829 if ($ctx =~ /Wx[BE]/ ||
26662830 ($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+ }
26692837 }
26702838 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+ }
26732848 }
26742849
2675−
26762850 # << and >> may either have or not have spaces both sides
26772851 } elsif ($op eq '<<' or $op eq '>>' or
26782852 $op eq '&' or $op eq '^' or $op eq '|' or
@@ -2681,17 +2855,23 @@ sub process { · 2 unmodified lines
26812855 $op eq '%')
26822856 {
26832857 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+ }
26872864 }
26882865
26892866 # A colon needs no spaces before when it is
26902867 # terminating a case value or a label.
26912868 } elsif ($opv eq ':C' || $opv eq ':L') {
26922869 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+ }
26952875 }
26962876
26972877 # All the others need spaces both sides.
@@ -2714,12 +2894,30 @@ sub process { · 16 unmodified lines
27142894 }
27152895
27162896 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+ }
27192903 }
27202904 }
27212905 $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];
27222914 }
2915+
2916+ if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
2917+ $fixed[$linenr - 1] = $fixed_line;
2918+ }
2919+
2920+
27232921 }
27242922
27252923 # check for multiple assignments
@@ -2747,8 +2945,12 @@ sub process { · 21 unmodified lines
27472945 #need space before brace following if, while, etc
27482946 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
27492947 $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+ }
27522954 }
27532955
27542956 ## # check for blank lines before declarations
@@ -2768,32 +2970,52 @@ sub process { · 13 unmodified lines
27682970
27692971 # check spacing on square brackets
27702972 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+ }
27732979 }
27742980 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+ }
27772987 }
27782988
27792989 # check spacing on parentheses
27802990 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
27812991 $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+ }
27842998 }
27852999 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
27863000 $line !~ /for\s*\(.*;\s+\)/ &&
27873001 $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+ }
27903008 }
27913009
27923010 #goto labels aren't indented, allow a single space however
27933011 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
27943012 !($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+ }
27973019 }
27983020
27993021 # Return is not a function.
@@ -2830,8 +3052,13 @@ sub process { · 30 unmodified lines
28303052 }
28313053
28323054 # 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+ }
28353062 }
28363063
28373064 # Check for illegal assignment in if conditional -- and check for trailing
@@ -3329,8 +3556,13 @@ sub process { · 491 unmodified lines
33293556
33303557 # warn about spacing in #ifdefs
33313558 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+
33343566 }
33353567
33363568 # check for spinlock_t definitions without a comment.
@@ -3793,6 +4025,40 @@ sub process { · 456 unmodified lines
37934025 print "\n\n";
37944026 }
37954027
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+
37964062 if ($clean == 1 && $quiet == 0) {
37974063 print "$vname has no obvious style problems and is ready for submission.\n"
37984064 }

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