History

checkpatch: track #ifdef/#else/#endif when tracking blocks

When picking up a complete statement or block for analysis we cannot simply track open/close/etc parenthesis we must take into account preprocessor section boundaries.

Andy Whitcroft committed 17 years ago

Commit 4635f4f1 parent 8b1b3371 file · +22 −0Browse files

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

Unified · Split

scripts/checkpatch.pl+22−0
@@ -405,6 +405,7 @@ sub ctx_statement_block { · 404 unmodified lines
405 405
406 my $type = '';406 my $type = '';
407 my $level = 0;407 my $level = 0;
408 my @stack = ([$type, $level]);
408 my $p;409 my $p;
409 my $c;410 my $c;
410 my $len = 0;411 my $len = 0;
@@ -436,6 +437,16 @@ sub ctx_statement_block { · 25 unmodified lines
436 $remainder = substr($blk, $off);437 $remainder = substr($blk, $off);
437 438
438 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";439 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
440
441 # Handle nested #if/#else.
442 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
443 push(@stack, [ $type, $level ]);
444 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
445 ($type, $level) = @{$stack[$#stack - 1]};
446 } elsif ($remainder =~ /^#\s*endif\b/) {
447 ($type, $level) = @{pop(@stack)};
448 }
449
439 # Statement ends at the ';' or a close '}' at the450 # Statement ends at the ';' or a close '}' at the
440 # outermost level.451 # outermost level.
441 if ($level == 0 && $c eq ';') {452 if ($level == 0 && $c eq ';') {
@@ -582,11 +593,22 @@ sub ctx_block_get { · 140 unmodified lines
582 my @res = ();593 my @res = ();
583 594
584 my $level = 0;595 my $level = 0;
596 my @stack = ($level);
585 for ($line = $start; $remain > 0; $line++) {597 for ($line = $start; $remain > 0; $line++) {
586 next if ($rawlines[$line] =~ /^-/);598 next if ($rawlines[$line] =~ /^-/);
587 $remain--;599 $remain--;
588 600
589 $blk .= $rawlines[$line];601 $blk .= $rawlines[$line];
602
603 # Handle nested #if/#else.
604 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
605 push(@stack, $level);
606 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
607 $level = $stack[$#stack - 1];
608 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
609 $level = pop(@stack);
610 }
611
590 foreach my $c (split(//, $rawlines[$line])) {612 foreach my $c (split(//, $rawlines[$line])) {
591 ##print "C<$c>L<$level><$open$close>O<$off>\n";613 ##print "C<$c>L<$level><$open$close>O<$off>\n";
592 if ($off > 0) {614 if ($off > 0) {