Skip to content

Commit

Permalink
checkpatch: track #ifdef/#else/#endif when tracking blocks
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Andy Whitcroft <apw@canonical.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Andy Whitcroft authored and Linus Torvalds committed Jan 6, 2009
1 parent 8b1b337 commit 4635f4f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ sub ctx_statement_block {

my $type = '';
my $level = 0;
my @stack = ([$type, $level]);
my $p;
my $c;
my $len = 0;
Expand Down Expand Up @@ -436,6 +437,16 @@ sub ctx_statement_block {
$remainder = substr($blk, $off);

#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";

# Handle nested #if/#else.
if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
push(@stack, [ $type, $level ]);
} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
($type, $level) = @{$stack[$#stack - 1]};
} elsif ($remainder =~ /^#\s*endif\b/) {
($type, $level) = @{pop(@stack)};
}

# Statement ends at the ';' or a close '}' at the
# outermost level.
if ($level == 0 && $c eq ';') {
Expand Down Expand Up @@ -582,11 +593,22 @@ sub ctx_block_get {
my @res = ();

my $level = 0;
my @stack = ($level);
for ($line = $start; $remain > 0; $line++) {
next if ($rawlines[$line] =~ /^-/);
$remain--;

$blk .= $rawlines[$line];

# Handle nested #if/#else.
if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
push(@stack, $level);
} elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
$level = $stack[$#stack - 1];
} elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
$level = pop(@stack);
}

foreach my $c (split(//, $rawlines[$line])) {
##print "C<$c>L<$level><$open$close>O<$off>\n";
if ($off > 0) {
Expand Down

0 comments on commit 4635f4f

Please sign in to comment.