Commit 4635f4fb authored by Andy Whitcroft's avatar Andy Whitcroft Committed by Linus Torvalds

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.
Signed-off-by: default avatarAndy Whitcroft <apw@canonical.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 8b1b3378
...@@ -405,6 +405,7 @@ sub ctx_statement_block { ...@@ -405,6 +405,7 @@ sub ctx_statement_block {
my $type = ''; my $type = '';
my $level = 0; my $level = 0;
my @stack = ([$type, $level]);
my $p; my $p;
my $c; my $c;
my $len = 0; my $len = 0;
...@@ -436,6 +437,16 @@ sub ctx_statement_block { ...@@ -436,6 +437,16 @@ sub ctx_statement_block {
$remainder = substr($blk, $off); $remainder = substr($blk, $off);
#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; #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 # Statement ends at the ';' or a close '}' at the
# outermost level. # outermost level.
if ($level == 0 && $c eq ';') { if ($level == 0 && $c eq ';') {
...@@ -582,11 +593,22 @@ sub ctx_block_get { ...@@ -582,11 +593,22 @@ sub ctx_block_get {
my @res = (); my @res = ();
my $level = 0; my $level = 0;
my @stack = ($level);
for ($line = $start; $remain > 0; $line++) { for ($line = $start; $remain > 0; $line++) {
next if ($rawlines[$line] =~ /^-/); next if ($rawlines[$line] =~ /^-/);
$remain--; $remain--;
$blk .= $rawlines[$line]; $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])) { foreach my $c (split(//, $rawlines[$line])) {
##print "C<$c>L<$level><$open$close>O<$off>\n"; ##print "C<$c>L<$level><$open$close>O<$off>\n";
if ($off > 0) { if ($off > 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment