Skip to contentSkip to conversation

checkpatch: fix for stripping brackets from macros.

PATCH In review Jeremy Sowden 2 participants 3 messages Dec 18, 2017 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: fix for stripping brackets from macros.

When checking macros, checkpatch.pl strips parentheses, square brackets and braces. However, the search-and-replace expression was not correct, and instead of replacing the brackets and their contents with just the contents, it was replacing them with literal 1's.

---
 scripts/checkpatch.pl | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
scripts/checkpatch.pl +3 -3
index 168687ae24fa..3b67646df845 100755
@@ -4874,9 +4874,9 @@ sub process {
 			$dstat =~ s/\s*$//s;
 
 			# Flatten any parentheses and braces
-			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
-			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
-			       $dstat =~ s/.\[[^\[\]]*\]/1/)
+			while ($dstat =~ s/\(([^\(\)]*)\)/$1/ ||
+			       $dstat =~ s/\{([^\{\}]*)\}/$1/ ||
+			       $dstat =~ s/.\[([^\[\]]*)\]/$1/)
 			{
 			}
 

base-commit: 53600ecfb6004f355bd3551bee180caf4b42d7a7

Signature · 1 line
2.15.1
maintainer
#1 lore raw
Quoted 4 lines · Jeremy Sowden

When checking macros, checkpatch.pl strips parentheses, square brackets and braces. However, the search-and-replace expression was not correct, and instead of replacing the brackets and their contents with just the contents, it was replacing them with literal 1's.

Jeremy:

What is the effect on the rest of the block that
uses this substituted
$dstat? Why should this be
done?

Andy:

I believe this is intentional as it simplifies
the macro analysis and has no other effect on the
rest of the block. Correct?

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 12 lines
scripts/checkpatch.pl +3 -3
@@ -4874,9 +4874,9 @@ sub process {
 			$dstat =~ s/\s*$//s;
 
 			# Flatten any parentheses and braces
-			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
-			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
-			       $dstat =~ s/.\[[^\[\]]*\]/1/)
+			while ($dstat =~ s/\(([^\(\)]*)\)/$1/ ||
+			       $dstat =~ s/\{([^\{\}]*)\}/$1/ ||
+			       $dstat =~ s/.\[([^\[\]]*)\]/$1/)
 			{
 			}
#2 lore raw
Quoted 11 lines · Joe Perches
Quoted 5 lines · Jeremy Sowden

When checking macros, checkpatch.pl strips parentheses, square brackets and braces. However, the search-and-replace expression was not correct, and instead of replacing the brackets and their contents with just the contents, it was replacing them with literal 1's.

Jeremy:

What is the effect on the rest of the block that uses this substituted $dstat? Why should this be done?

I had some macros which defined compound literals, e.g.:

  #define TEST (struct test) { .member = 1 }

and checkpatch.pl complained that macros with complex values should be enclosed in parentheses. When I had a look at the checkpatch.pl source I noticed that there were a number of exceptions against which $dstat was matched and that they included the struct and union keywords. These matches failed, however, 'cause the compound-literal had been turned into:

1 1

which didn't seem to make much sense. Given that the while-loop was immediately followed by another that did the more obvious thing:

  # Flatten any parentheses and braces
  while ($dstat =~ s/\([^\(\)]*\)/1/ ||
         $dstat =~ s/\{[^\{\}]*\}/1/ ||
         $dstat =~ s/.\[[^\[\]]*\]/1/)
  {
  }

  # Flatten any obvious string concatentation.
  while ($dstat =~ s/($String)\s*$Ident/$1/ ||
         $dstat =~ s/$Ident\s*($String)/$1/)
  {
  }

it occurred to me that this might be a bug.

Quoted 20 lines

Andy:

I believe this is intentional as it simplifies
the macro analysis and has no other effect on the
rest of the block. Correct?

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 12 lines
scripts/checkpatch.pl +3 -3
@@ -4874,9 +4874,9 @@ sub process {
 			$dstat =~ s/\s*$//s;
 
 			# Flatten any parentheses and braces
-			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
-			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
-			       $dstat =~ s/.\[[^\[\]]*\]/1/)
+			while ($dstat =~ s/\(([^\(\)]*)\)/$1/ ||
+			       $dstat =~ s/\{([^\{\}]*)\}/$1/ ||
+			       $dstat =~ s/.\[([^\[\]]*)\]/$1/)
 			{
 			}