diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/checkpatch.pl | 141 |
1 files changed, 86 insertions, 55 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index cc61cf7187ef..e3ae79ab2cab 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -673,6 +673,22 @@ sub ctx_has_comment { | |||
673 | return ($cmt ne ''); | 673 | return ($cmt ne ''); |
674 | } | 674 | } |
675 | 675 | ||
676 | sub raw_line { | ||
677 | my ($linenr, $cnt) = @_; | ||
678 | |||
679 | my $offset = $linenr - 1; | ||
680 | $cnt++; | ||
681 | |||
682 | my $line; | ||
683 | while ($cnt) { | ||
684 | $line = $rawlines[$offset++]; | ||
685 | next if (defined($line) && $line =~ /^-/); | ||
686 | $cnt--; | ||
687 | } | ||
688 | |||
689 | return $line; | ||
690 | } | ||
691 | |||
676 | sub cat_vet { | 692 | sub cat_vet { |
677 | my ($vet) = @_; | 693 | my ($vet) = @_; |
678 | my ($res, $coded); | 694 | my ($res, $coded); |
@@ -1392,6 +1408,76 @@ sub process { | |||
1392 | } | 1408 | } |
1393 | } | 1409 | } |
1394 | 1410 | ||
1411 | # Check relative indent for conditionals and blocks. | ||
1412 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { | ||
1413 | my ($s, $c) = ($stat, $cond); | ||
1414 | |||
1415 | substr($s, 0, length($c), ''); | ||
1416 | |||
1417 | # Make sure we remove the line prefixes as we have | ||
1418 | # none on the first line, and are going to readd them | ||
1419 | # where necessary. | ||
1420 | $s =~ s/\n./\n/gs; | ||
1421 | |||
1422 | # Find out how long the conditional actually is. | ||
1423 | my $cond_lines = 0 + $c =~ /\n/gs; | ||
1424 | |||
1425 | # We want to check the first line inside the block | ||
1426 | # starting at the end of the conditional, so remove: | ||
1427 | # 1) any blank line termination | ||
1428 | # 2) any opening brace { on end of the line | ||
1429 | # 3) any do (...) { | ||
1430 | my $continuation = 0; | ||
1431 | my $check = 0; | ||
1432 | $s =~ s/^.*\bdo\b//; | ||
1433 | $s =~ s/^\s*{//; | ||
1434 | if ($s =~ s/^\s*\\//) { | ||
1435 | $continuation = 1; | ||
1436 | } | ||
1437 | if ($s =~ s/^\s*\n//) { | ||
1438 | $check = 1; | ||
1439 | $cond_lines++; | ||
1440 | } | ||
1441 | |||
1442 | # Also ignore a loop construct at the end of a | ||
1443 | # preprocessor statement. | ||
1444 | if (($prevline =~ /^.\s*#\s*define\s/ || | ||
1445 | $prevline =~ /\\\s*$/) && $continuation == 0) { | ||
1446 | $check = 0; | ||
1447 | } | ||
1448 | |||
1449 | # Ignore the current line if its is a preprocessor | ||
1450 | # line. | ||
1451 | if ($s =~ /^\s*#\s*/) { | ||
1452 | $check = 0; | ||
1453 | } | ||
1454 | |||
1455 | # Ignore the current line if it is label. | ||
1456 | if ($s =~ /^\s*$Ident\s*:/) { | ||
1457 | $check = 0; | ||
1458 | } | ||
1459 | |||
1460 | my (undef, $sindent) = line_stats("+" . $s); | ||
1461 | my $stat_real = raw_line($linenr, $cond_lines); | ||
1462 | |||
1463 | # Check if either of these lines are modified, else | ||
1464 | # this is not this patch's fault. | ||
1465 | if (!defined($stat_real) || | ||
1466 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { | ||
1467 | $check = 0; | ||
1468 | } | ||
1469 | if (defined($stat_real) && $cond_lines > 1) { | ||
1470 | $stat_real = "[...]\n$stat_real"; | ||
1471 | } | ||
1472 | |||
1473 | ##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n"; | ||
1474 | |||
1475 | if ($check && (($sindent % 8) != 0 || | ||
1476 | ($sindent <= $indent && $s ne ''))) { | ||
1477 | WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); | ||
1478 | } | ||
1479 | } | ||
1480 | |||
1395 | # Track the 'values' across context and added lines. | 1481 | # Track the 'values' across context and added lines. |
1396 | my $opline = $line; $opline =~ s/^./ /; | 1482 | my $opline = $line; $opline =~ s/^./ /; |
1397 | my ($curr_values, $curr_vars) = | 1483 | my ($curr_values, $curr_vars) = |
@@ -1869,61 +1955,6 @@ sub process { | |||
1869 | } | 1955 | } |
1870 | } | 1956 | } |
1871 | 1957 | ||
1872 | # Check relative indent for conditionals and blocks. | ||
1873 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { | ||
1874 | my ($s, $c) = ($stat, $cond); | ||
1875 | |||
1876 | substr($s, 0, length($c), ''); | ||
1877 | |||
1878 | # Make sure we remove the line prefixes as we have | ||
1879 | # none on the first line, and are going to readd them | ||
1880 | # where necessary. | ||
1881 | $s =~ s/\n./\n/gs; | ||
1882 | |||
1883 | # We want to check the first line inside the block | ||
1884 | # starting at the end of the conditional, so remove: | ||
1885 | # 1) any blank line termination | ||
1886 | # 2) any opening brace { on end of the line | ||
1887 | # 3) any do (...) { | ||
1888 | my $continuation = 0; | ||
1889 | my $check = 0; | ||
1890 | $s =~ s/^.*\bdo\b//; | ||
1891 | $s =~ s/^\s*{//; | ||
1892 | if ($s =~ s/^\s*\\//) { | ||
1893 | $continuation = 1; | ||
1894 | } | ||
1895 | if ($s =~ s/^\s*\n//) { | ||
1896 | $check = 1; | ||
1897 | } | ||
1898 | |||
1899 | # Also ignore a loop construct at the end of a | ||
1900 | # preprocessor statement. | ||
1901 | if (($prevline =~ /^.\s*#\s*define\s/ || | ||
1902 | $prevline =~ /\\\s*$/) && $continuation == 0) { | ||
1903 | $check = 0; | ||
1904 | } | ||
1905 | |||
1906 | # Ignore the current line if its is a preprocessor | ||
1907 | # line. | ||
1908 | if ($s =~ /^\s*#\s*/) { | ||
1909 | $check = 0; | ||
1910 | } | ||
1911 | |||
1912 | # Ignore the current line if it is label. | ||
1913 | if ($s =~ /^\s*$Ident\s*:/) { | ||
1914 | $check = 0; | ||
1915 | } | ||
1916 | |||
1917 | my (undef, $sindent) = line_stats("+" . $s); | ||
1918 | |||
1919 | ##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s>\n"; | ||
1920 | |||
1921 | if ($check && (($sindent % 8) != 0 || | ||
1922 | ($sindent <= $indent && $s ne ''))) { | ||
1923 | WARN("suspect code indent for conditional statements\n" . $herecurr); | ||
1924 | } | ||
1925 | } | ||
1926 | |||
1927 | # Check for bitwise tests written as boolean | 1958 | # Check for bitwise tests written as boolean |
1928 | if ($line =~ / | 1959 | if ($line =~ / |
1929 | (?: | 1960 | (?: |