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.
Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all
Unified · Split
| @@ -405,6 +405,7 @@ sub ctx_statement_block { · 404 unmodified lines | ||
| 405 | 405 | |
| 406 | 406 | my $type = ''; |
| 407 | 407 | my $level = 0; |
| 408 | + my @stack = ([$type, $level]); | |
| 408 | 409 | my $p; |
| 409 | 410 | my $c; |
| 410 | 411 | my $len = 0; |
| @@ -436,6 +437,16 @@ sub ctx_statement_block { · 25 unmodified lines | ||
| 436 | 437 | $remainder = substr($blk, $off); |
| 437 | 438 | |
| 438 | 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 | 450 | # Statement ends at the ';' or a close '}' at the |
| 440 | 451 | # outermost level. |
| 441 | 452 | if ($level == 0 && $c eq ';') { |
| @@ -582,11 +593,22 @@ sub ctx_block_get { · 140 unmodified lines | ||
| 582 | 593 | my @res = (); |
| 583 | 594 | |
| 584 | 595 | my $level = 0; |
| 596 | + my @stack = ($level); | |
| 585 | 597 | for ($line = $start; $remain > 0; $line++) { |
| 586 | 598 | next if ($rawlines[$line] =~ /^-/); |
| 587 | 599 | $remain--; |
| 588 | 600 | |
| 589 | 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 | 612 | foreach my $c (split(//, $rawlines[$line])) { |
| 591 | 613 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 592 | 614 | if ($off > 0) { |