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 (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+22−0
@@ -405,6 +405,7 @@ sub ctx_statement_block { · 404 unmodified lines
405405
406406 my $type = '';
407407 my $level = 0;
408+ my @stack = ([$type, $level]);
408409 my $p;
409410 my $c;
410411 my $len = 0;
@@ -436,6 +437,16 @@ sub ctx_statement_block { · 25 unmodified lines
436437 $remainder = substr($blk, $off);
437438
438439 #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+
439450 # Statement ends at the ';' or a close '}' at the
440451 # outermost level.
441452 if ($level == 0 && $c eq ';') {
@@ -582,11 +593,22 @@ sub ctx_block_get { · 140 unmodified lines
582593 my @res = ();
583594
584595 my $level = 0;
596+ my @stack = ($level);
585597 for ($line = $start; $remain > 0; $line++) {
586598 next if ($rawlines[$line] =~ /^-/);
587599 $remain--;
588600
589601 $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+
590612 foreach my $c (split(//, $rawlines[$line])) {
591613 ##print "C<$c>L<$level><$open$close>O<$off>\n";
592614 if ($off > 0) {