Skip to content

Commit 3e469cd

Browse files
Andy Whitcrofttorvalds
authored andcommitted
checkpatch: optimise statement scanner when mid-statement
In the middle of a long definition or similar, there is no possibility of finding a smaller sub-statement. Optimise this case by skipping statement aquirey where there are no starts of statement (open brace '{' or semi-colon ';'). We are likely to scan slightly more than needed still but this is safest. Signed-off-by: Andy Whitcroft <apw@canonical.com> Cc: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 89a8835 commit 3e469cd

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

scripts/checkpatch.pl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ sub process {
13731373
my %suppress_ifbraces;
13741374
my %suppress_whiletrailers;
13751375
my %suppress_export;
1376+
my $suppress_statement = 0;
13761377

13771378
# Pre-scan the patch sanitizing the lines.
13781379
# Pre-scan the patch looking for any __setup documentation.
@@ -1482,6 +1483,7 @@ sub process {
14821483
%suppress_ifbraces = ();
14831484
%suppress_whiletrailers = ();
14841485
%suppress_export = ();
1486+
$suppress_statement = 0;
14851487
next;
14861488

14871489
# track the line number as we move through the hunk, note that
@@ -1809,13 +1811,23 @@ sub process {
18091811
# Check for potential 'bare' types
18101812
my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
18111813
$realline_next);
1812-
if ($realcnt && $line =~ /.\s*\S/) {
1814+
#print "LINE<$line>\n";
1815+
if ($linenr >= $suppress_statement &&
1816+
$realcnt && $line =~ /.\s*\S/) {
18131817
($stat, $cond, $line_nr_next, $remain_next, $off_next) =
18141818
ctx_statement_block($linenr, $realcnt, 0);
18151819
$stat =~ s/\n./\n /g;
18161820
$cond =~ s/\n./\n /g;
18171821

1818-
#print "stat<$stat>\n";
1822+
#print "linenr<$linenr> <$stat>\n";
1823+
# If this statement has no statement boundaries within
1824+
# it there is no point in retrying a statement scan
1825+
# until we hit end of it.
1826+
my $frag = $stat; $frag =~ s/;+\s*$//;
1827+
if ($frag !~ /(?:{|;)/) {
1828+
#print "skip<$line_nr_next>\n";
1829+
$suppress_statement = $line_nr_next;
1830+
}
18191831

18201832
# Find the real next line.
18211833
$realline_next = $line_nr_next;
@@ -1942,6 +1954,9 @@ sub process {
19421954

19431955
# Check relative indent for conditionals and blocks.
19441956
if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1957+
($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1958+
ctx_statement_block($linenr, $realcnt, 0)
1959+
if (!defined $stat);
19451960
my ($s, $c) = ($stat, $cond);
19461961

19471962
substr($s, 0, length($c), '');
@@ -2620,6 +2635,9 @@ sub process {
26202635
# Check for illegal assignment in if conditional -- and check for trailing
26212636
# statements after the conditional.
26222637
if ($line =~ /do\s*(?!{)/) {
2638+
($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2639+
ctx_statement_block($linenr, $realcnt, 0)
2640+
if (!defined $stat);
26232641
my ($stat_next) = ctx_statement_block($line_nr_next,
26242642
$remain_next, $off_next);
26252643
$stat_next =~ s/\n./\n /g;

0 commit comments

Comments
 (0)