diff options
| author | Joe Perches <joe@perches.com> | 2014-12-10 18:52:05 -0500 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-12-10 20:41:12 -0500 |
| commit | 5e4f6ba5eb7facb0dbf2734f5a12033c70f8eb53 (patch) | |
| tree | 58fc4a135d201935f44464a82dd8a6dd412accd5 /scripts | |
| parent | b75ac618df751b927469ddbca63cf151a62f0f9d (diff) | |
checkpatch: add ability to --fix (coalesce) string fragments on multiple lines
Add --fix option to coalesce string fragments.
This does not coalesce string fragments that have newline terminations or
are otherwise exempted.
Other miscellanea:
o move all the string tests together.
o fix get_quoted_string function for tab characters
o fix concatination typo
Signed-off-by: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@canonical.com>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/checkpatch.pl | 115 |
1 files changed, 69 insertions, 46 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 6d85c7b1b27e..f0bb6d60c07b 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
| @@ -946,7 +946,7 @@ sub sanitise_line { | |||
| 946 | sub get_quoted_string { | 946 | sub get_quoted_string { |
| 947 | my ($line, $rawline) = @_; | 947 | my ($line, $rawline) = @_; |
| 948 | 948 | ||
| 949 | return "" if ($line !~ m/(\"[X]+\")/g); | 949 | return "" if ($line !~ m/(\"[X\t]+\")/g); |
| 950 | return substr($rawline, $-[0], $+[0] - $-[0]); | 950 | return substr($rawline, $-[0], $+[0] - $-[0]); |
| 951 | } | 951 | } |
| 952 | 952 | ||
| @@ -1847,6 +1847,7 @@ sub process { | |||
| 1847 | my $non_utf8_charset = 0; | 1847 | my $non_utf8_charset = 0; |
| 1848 | 1848 | ||
| 1849 | my $last_blank_line = 0; | 1849 | my $last_blank_line = 0; |
| 1850 | my $last_coalesced_string_linenr = -1; | ||
| 1850 | 1851 | ||
| 1851 | our @report = (); | 1852 | our @report = (); |
| 1852 | our $cnt_lines = 0; | 1853 | our $cnt_lines = 0; |
| @@ -2413,33 +2414,6 @@ sub process { | |||
| 2413 | "line over $max_line_length characters\n" . $herecurr); | 2414 | "line over $max_line_length characters\n" . $herecurr); |
| 2414 | } | 2415 | } |
| 2415 | 2416 | ||
| 2416 | # Check for user-visible strings broken across lines, which breaks the ability | ||
| 2417 | # to grep for the string. Make exceptions when the previous string ends in a | ||
| 2418 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' | ||
| 2419 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value | ||
| 2420 | if ($line =~ /^\+\s*"/ && | ||
| 2421 | $prevline =~ /"\s*$/ && | ||
| 2422 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { | ||
| 2423 | WARN("SPLIT_STRING", | ||
| 2424 | "quoted string split across lines\n" . $hereprev); | ||
| 2425 | } | ||
| 2426 | |||
| 2427 | # check for missing a space in a string concatination | ||
| 2428 | if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { | ||
| 2429 | WARN('MISSING_SPACE', | ||
| 2430 | "break quoted strings at a space character\n" . $hereprev); | ||
| 2431 | } | ||
| 2432 | |||
| 2433 | # check for spaces before a quoted newline | ||
| 2434 | if ($rawline =~ /^.*\".*\s\\n/) { | ||
| 2435 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", | ||
| 2436 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && | ||
| 2437 | $fix) { | ||
| 2438 | $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; | ||
| 2439 | } | ||
| 2440 | |||
| 2441 | } | ||
| 2442 | |||
| 2443 | # check for adding lines without a newline. | 2417 | # check for adding lines without a newline. |
| 2444 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { | 2418 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
| 2445 | WARN("MISSING_EOF_NEWLINE", | 2419 | WARN("MISSING_EOF_NEWLINE", |
| @@ -4458,6 +4432,55 @@ sub process { | |||
| 4458 | "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); | 4432 | "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); |
| 4459 | } | 4433 | } |
| 4460 | 4434 | ||
| 4435 | # Check for user-visible strings broken across lines, which breaks the ability | ||
| 4436 | # to grep for the string. Make exceptions when the previous string ends in a | ||
| 4437 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' | ||
| 4438 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value | ||
| 4439 | if ($line =~ /^\+\s*"[X\t]*"/ && | ||
| 4440 | $prevline =~ /"\s*$/ && | ||
| 4441 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { | ||
| 4442 | if (WARN("SPLIT_STRING", | ||
| 4443 | "quoted string split across lines\n" . $hereprev) && | ||
| 4444 | $fix && | ||
| 4445 | $prevrawline =~ /^\+.*"\s*$/ && | ||
| 4446 | $last_coalesced_string_linenr != $linenr - 1) { | ||
| 4447 | my $extracted_string = get_quoted_string($line, $rawline); | ||
| 4448 | my $comma_close = ""; | ||
| 4449 | if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { | ||
| 4450 | $comma_close = $1; | ||
| 4451 | } | ||
| 4452 | |||
| 4453 | fix_delete_line($fixlinenr - 1, $prevrawline); | ||
| 4454 | fix_delete_line($fixlinenr, $rawline); | ||
| 4455 | my $fixedline = $prevrawline; | ||
| 4456 | $fixedline =~ s/"\s*$//; | ||
| 4457 | $fixedline .= substr($extracted_string, 1) . trim($comma_close); | ||
| 4458 | fix_insert_line($fixlinenr - 1, $fixedline); | ||
| 4459 | $fixedline = $rawline; | ||
| 4460 | $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; | ||
| 4461 | if ($fixedline !~ /\+\s*$/) { | ||
| 4462 | fix_insert_line($fixlinenr, $fixedline); | ||
| 4463 | } | ||
| 4464 | $last_coalesced_string_linenr = $linenr; | ||
| 4465 | } | ||
| 4466 | } | ||
| 4467 | |||
| 4468 | # check for missing a space in a string concatenation | ||
| 4469 | if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { | ||
| 4470 | WARN('MISSING_SPACE', | ||
| 4471 | "break quoted strings at a space character\n" . $hereprev); | ||
| 4472 | } | ||
| 4473 | |||
| 4474 | # check for spaces before a quoted newline | ||
| 4475 | if ($rawline =~ /^.*\".*\s\\n/) { | ||
| 4476 | if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", | ||
| 4477 | "unnecessary whitespace before a quoted newline\n" . $herecurr) && | ||
| 4478 | $fix) { | ||
| 4479 | $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; | ||
| 4480 | } | ||
| 4481 | |||
| 4482 | } | ||
| 4483 | |||
| 4461 | # concatenated string without spaces between elements | 4484 | # concatenated string without spaces between elements |
| 4462 | if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) { | 4485 | if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) { |
| 4463 | CHK("CONCATENATED_STRING", | 4486 | CHK("CONCATENATED_STRING", |
| @@ -4470,6 +4493,24 @@ sub process { | |||
| 4470 | "Consecutive strings are generally better as a single string\n" . $herecurr); | 4493 | "Consecutive strings are generally better as a single string\n" . $herecurr); |
| 4471 | } | 4494 | } |
| 4472 | 4495 | ||
| 4496 | # check for %L{u,d,i} in strings | ||
| 4497 | my $string; | ||
| 4498 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { | ||
| 4499 | $string = substr($rawline, $-[1], $+[1] - $-[1]); | ||
| 4500 | $string =~ s/%%/__/g; | ||
| 4501 | if ($string =~ /(?<!%)%L[udi]/) { | ||
| 4502 | WARN("PRINTF_L", | ||
| 4503 | "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); | ||
| 4504 | last; | ||
| 4505 | } | ||
| 4506 | } | ||
| 4507 | |||
| 4508 | # check for line continuations in quoted strings with odd counts of " | ||
| 4509 | if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { | ||
| 4510 | WARN("LINE_CONTINUATIONS", | ||
| 4511 | "Avoid line continuations in quoted strings\n" . $herecurr); | ||
| 4512 | } | ||
| 4513 | |||
| 4473 | # warn about #if 0 | 4514 | # warn about #if 0 |
| 4474 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { | 4515 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
| 4475 | CHK("REDUNDANT_CODE", | 4516 | CHK("REDUNDANT_CODE", |
| @@ -4754,12 +4795,6 @@ sub process { | |||
| 4754 | } | 4795 | } |
| 4755 | } | 4796 | } |
| 4756 | 4797 | ||
| 4757 | # check for line continuations in quoted strings with odd counts of " | ||
| 4758 | if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { | ||
| 4759 | WARN("LINE_CONTINUATIONS", | ||
| 4760 | "Avoid line continuations in quoted strings\n" . $herecurr); | ||
| 4761 | } | ||
| 4762 | |||
| 4763 | # check for struct spinlock declarations | 4798 | # check for struct spinlock declarations |
| 4764 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { | 4799 | if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { |
| 4765 | WARN("USE_SPINLOCK_T", | 4800 | WARN("USE_SPINLOCK_T", |
| @@ -5169,18 +5204,6 @@ sub process { | |||
| 5169 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); | 5204 | "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); |
| 5170 | } | 5205 | } |
| 5171 | 5206 | ||
| 5172 | # check for %L{u,d,i} in strings | ||
| 5173 | my $string; | ||
| 5174 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { | ||
| 5175 | $string = substr($rawline, $-[1], $+[1] - $-[1]); | ||
| 5176 | $string =~ s/%%/__/g; | ||
| 5177 | if ($string =~ /(?<!%)%L[udi]/) { | ||
| 5178 | WARN("PRINTF_L", | ||
| 5179 | "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); | ||
| 5180 | last; | ||
| 5181 | } | ||
| 5182 | } | ||
| 5183 | |||
| 5184 | # whine mightly about in_atomic | 5207 | # whine mightly about in_atomic |
| 5185 | if ($line =~ /\bin_atomic\s*\(/) { | 5208 | if ($line =~ /\bin_atomic\s*\(/) { |
| 5186 | if ($realfile =~ m@^drivers/@) { | 5209 | if ($realfile =~ m@^drivers/@) { |
