Skip to contentSkip to conversation

checkpatch: fix multistatement macro checks

RFC PATCH In review Dwaipayan Ray 2 participants 4 messages Sep 29, 2020 – Sep 30, 2020 Checkpatch

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 checkpatch: fix multistatement macro checks

Checkpatch generates incorrect error message for certain macros.

When checkpatch was run on commit 4649079b9de1 "tracing: Make ftrace packed events have align of 1"), it generated multiple errors of the form:

ERROR: Macros with multiple statements should be enclosed in a do - while loop
+#define __field_packed(type, container, item) type item;

This should certainly not be enclosed in a do - while loop. The problem was with the trailing semicolon. Any statement with trailing semicolon was marked as a multi-line macro. This was fixed by having a seperate rule for excluding single statements terminated by semicolon.

The second problem is due to while (...) {...} macros not being handled.
For example, on commit fe557319aa06 ("maccess: rename probe_kernel_ {read,write} to copy_{from,to}_kernel_nofault"), checkpatch generated following error:

ERROR: Macros with multiple statements should be enclosed in a do - while loop
+#define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \

 	while (len >= sizeof(type)) {
        ....

This was fixed by having a seperate rule to match such while blocks.

---
 scripts/checkpatch.pl | 2 ++
 1 file changed, 2 insertions(+)
scripts/checkpatch.pl +2 -0
index 9e65d21456f1..4fb612200c27 100755
@@ -5342,10 +5342,12 @@ sub process {
 			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
 			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
 			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
+				$dstat !~ /^while\s*$Constant\s*$Constant$/ &&			# while (...) {...}
 			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
 			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
 			    $dstat !~ /^do\s*{/ &&					# do {...
 			    $dstat !~ /^\(\{/ &&						# ({...
+				$dstat !~ /;$/ &&						# statement;
 			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
 			{
 				if ($dstat =~ /^\s*if\b/) {
Signature · 6 lines
2.27.0

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#1 lore raw

Dwaipayan, did you checkpatch.pl your patch before sending it out?

Quoted 5 lines · Dwaipayan Ray

Checkpatch generates incorrect error message for certain macros.

When checkpatch was run on commit 4649079b9de1 "tracing: Make ftrace packed events have align of 1"), it

brace ( is missing, checkpatch should warn you.

Quoted 2 lines

generated multiple errors of the form:

Present tense is fine. checkpatch runs on ...,
it generates ...

Quoted 22 lines

ERROR: Macros with multiple statements should be enclosed in a do - while loop
+#define __field_packed(type, container, item) type item;

This should certainly not be enclosed in a do - while loop. The problem was with the trailing semicolon. Any statement with trailing semicolon was marked as a multi-line macro. This was fixed by having a seperate rule for excluding single statements terminated by semicolon.

The second problem is due to while (...) {...} macros not being handled.
For example, on commit fe557319aa06 ("maccess: rename probe_kernel_ {read,write} to copy_{from,to}_kernel_nofault"), checkpatch generated following error:

ERROR: Macros with multiple statements should be enclosed in a do - while loop

(quoted diff) +1 -0
+#define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \
 	while (len >= sizeof(type)) {
....
Quoted 2 lines

This was fixed by having a seperate rule to match such while blocks.

s/seperate/separate/

If you address two different problems, make two patches not one.

Kernel developers can handle many patches...

Create two patches in a patch series, checkpatch.pl them, send them out here; then we will do a quick review and out they go to Joe and lkml.

Lukas

Quoted 10 lines
---
 scripts/checkpatch.pl | 2 ++
 1 file changed, 2 insertions(+)
scripts/checkpatch.pl +0 -0
index 9e65d21456f1..4fb612200c27 100755

scripts/checkpatch.pl sub process { · line 5345

Quoted 4 lines
scripts/checkpatch.pl +1 -0
@@ -5342,10 +5342,12 @@ sub process {
 			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
 			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
 			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
+				$dstat !~ /^while\s*$Constant\s*$Constant$/ &&			# while (...) {...}

tab and spacing might be different than lines around it?

Quoted 12 lines
scripts/checkpatch.pl +1 -0
 			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
 			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
 			    $dstat !~ /^do\s*{/ &&					# do {...
 			    $dstat !~ /^\(\{/ &&						# ({...
+				$dstat !~ /;$/ &&						# statement;
 			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
 			{
 				if ($dstat =~ /^\s*if\b/) {
Signature · 1 line
2.27.0

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#2 lore raw
Quoted 4 lines · Lukas Bulwahn

Dwaipayan, did you checkpatch.pl your patch before sending it out?

Oh right! I goofed up! Sorry about that.

Quoted 10 lines · Dwaipayan Ray
Quoted 6 lines

Checkpatch generates incorrect error message for certain macros.

When checkpatch was run on commit 4649079b9de1 "tracing: Make ftrace packed events have align of 1"), it

brace ( is missing, checkpatch should warn you.

Again, I may have been too hasty in sending out the patch :(

Quoted 32 lines
Quoted 2 lines

generated multiple errors of the form:

Present tense is fine. checkpatch runs on ...,
it generates ...

Quoted 16 lines

ERROR: Macros with multiple statements should be enclosed in a do - while loop
+#define __field_packed(type, container, item) type item;

This should certainly not be enclosed in a do - while loop. The problem was with the trailing semicolon. Any statement with trailing semicolon was marked as a multi-line macro. This was fixed by having a seperate rule for excluding single statements terminated by semicolon.

The second problem is due to while (...) {...} macros not being handled.
For example, on commit fe557319aa06 ("maccess: rename probe_kernel_ {read,write} to copy_{from,to}_kernel_nofault"), checkpatch generated following error:

Quoted 8 lines

ERROR: Macros with multiple statements should be enclosed in a do - while loop

+#define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \
      while (len >= sizeof(type)) {
        ....

This was fixed by having a seperate rule to match such while blocks.

s/seperate/separate/

Quoted 7 lines

If you address two different problems, make two patches not one.

Kernel developers can handle many patches...

Create two patches in a patch series, checkpatch.pl them, send them out here; then we will do a quick review and out they go to Joe and lkml.

Sure, I will do that.

Quoted 7 lines

Lukas

Quoted 5 lines
---
 scripts/checkpatch.pl | 2 ++
 1 file changed, 2 insertions(+)
Quoted 11 lines

scripts/checkpatch.pl sub process { · line 5345

Quoted 9 lines
scripts/checkpatch.pl +1 -0
index 9e65d21456f1..4fb612200c27 100755
@@ -5342,10 +5342,12 @@ sub process {
                          $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
                          $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&          # stringification #foo
                          $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
+                             $dstat !~ /^while\s*$Constant\s*$Constant$/ &&                  # while (...) {...}

tab and spacing might be different than lines around it?

Weirdly, the other lines had 4 spaces instead of one of the tabs. Will correct that.

Quoted 13 lines
Quoted 10 lines
scripts/checkpatch.pl +1 -1
                          $dstat !~ /^for\s*$Constant$/ &&                            # for (...)
                          $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&   # for (...) bar()
                          $dstat !~ /^do\s*{/ &&                                      # do {...
                          $dstat !~ /^\(\{/ &&                                                # ({...
+                             $dstat !~ /;$/ &&                                               # statement;
                          $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
                      {
                              if ($dstat =~ /^\s*if\b/) {
--
Quoted 3 lines

2.27.0

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#3 lore raw
Quoted 13 lines · Dwaipayan Ray
Quoted 4 lines · Lukas Bulwahn

Dwaipayan, did you checkpatch.pl your patch before sending it out?

Oh right! I goofed up! Sorry about that.

Quoted 5 lines · Dwaipayan Ray
Quoted 4 lines

Checkpatch generates incorrect error message for certain macros.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

When checkpatch was run on commit 4649079b9de1 "tracing: Make ftrace packed events have align of 1"), it

brace ( is missing, checkpatch should warn you.

Again, I may have been too hasty in sending out the patch :(

That is what the linux-kernel-mentees list is for. Send something out quickly and have some people with patience check it.

Lukas
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees