Skip to contentSkip to conversation

checkpatch: fix multi-statement macro checks

RFC PATCH In review Dwaipayan Ray 2 participants 5 messages Sep 30, 2020 – Oct 1, 2020 Checkpatch

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 checkpatch: fix multi-statement macro checks

Checkpatch.pl generates errors which are false positive for certain multi-statemenent macros.

The specific case investigated was whitespace separated statement terminated by a semicolon. Checkpatch wrongly classifies such as a multi-statement macro.

For example, commit 4649079b9de1 ("tracing: Make ftrace packed events have align of 1") when analyzed with checkpatch generates:

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

The error is misleading in this case and should not be produced. The solution undertaken is to exclude any macro which doesn't have any non-WSP character after the first and only semicolon(;) present in it. This shall allow macros of the form "type item;", "type item[size];", etc. to not generate the multi-statement macro error.

---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)
scripts/checkpatch.pl +1 -0
index 9e65d21456f1..72c4072307ea 100755
@@ -5346,6 +5346,7 @@ sub process {
 			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
 			    $dstat !~ /^do\s*{/ &&					# do {...
 			    $dstat !~ /^\(\{/ &&						# ({...
+			    $dstat !~ /^[^;]*;\s*$/ &&				# space separated 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

RFC PATCH 2/2 checkpatch: fix multi-statement macro checks

Checkpatch.pl doesn't have a check for excluding while (...) {...} blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.

For example, running checkpatch.pl on the file mm/access.c in the kernel generates the following error:

ERROR: Macros with complex values should be enclosed in parentheses
+#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
+ while (len >= sizeof(type)) { \
+ __get_kernel_nofault(dst, src, type, err_label); \
+ dst += sizeof(type); \
+ src += sizeof(type); \
+ len -= sizeof(type); \
+ }

The error is misleading for this case. Enclosing it in a paranthesis doesn't make any sense.

Checkpatch already has an exception list for such common macro types. Added a new exception for while (...) {...} style blocks to the same. This effectively fixed the wrong error message.

---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)
scripts/checkpatch.pl +1 -0
index 72c4072307ea..c2c211374662 100755
@@ -5342,6 +5342,7 @@ 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\s*$/ &&			# while (...) {...}
 			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
 			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
 			    $dstat !~ /^do\s*{/ &&					# do {...
Signature · 6 lines
2.27.0

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#2 lore raw
Quoted 15 lines · Dwaipayan Ray

Checkpatch.pl doesn't have a check for excluding while (...) {...} blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.

For example, running checkpatch.pl on the file mm/access.c in the kernel generates the following error:

ERROR: Macros with complex values should be enclosed in parentheses

(quoted diff) +7 -0
+#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)	\
+	while (len >= sizeof(type)) {					\
+		__get_kernel_nofault(dst, src, type, err_label);	\
+		dst += sizeof(type);					\
+		src += sizeof(type);					\
+		len -= sizeof(type);					\
+	}
Quoted 3 lines

The error is misleading for this case. Enclosing it in a paranthesis doesn't make any sense.

s/a paranthesis/parentheses/

Quoted 4 lines

Checkpatch already has an exception list for such common macro types. Added a new exception for while (...) {...} style blocks to the same. This effectively fixed the wrong error message.

This seems easier to understand and to judge than the other patch.

We will still need an evaluation, though. Theodoros, maybe you can help us here.

Dwaipayan, looks good to me. Do you fix up the spelling mistake and then send that patch to Joe and lkml?

Quoted 10 lines
---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)
scripts/checkpatch.pl +0 -0
index 72c4072307ea..c2c211374662 100755

scripts/checkpatch.pl sub process { · line 5345

Quoted 4 lines
scripts/checkpatch.pl +1 -0
@@ -5342,6 +5342,7 @@ 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\s*$/ &&			# while (...) {...}

Is it just the tab setting in my email client or is there a tab too much that it does not align with the other comments?

Quoted 7 lines
scripts/checkpatch.pl +0 -0
 			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
 			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
 			    $dstat !~ /^do\s*{/ &&					# do {...
Signature · 1 line
2.27.0

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

#3 lore raw
Quoted 20 lines · Lukas Bulwahn
Quoted 15 lines · Dwaipayan Ray

Checkpatch.pl doesn't have a check for excluding while (...) {...} blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.

For example, running checkpatch.pl on the file mm/access.c in the kernel generates the following error:

ERROR: Macros with complex values should be enclosed in parentheses

(quoted diff) +7 -0
+#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)        \
+     while (len >= sizeof(type)) {                                   \
+             __get_kernel_nofault(dst, src, type, err_label);        \
+             dst += sizeof(type);                                    \
+             src += sizeof(type);                                    \
+             len -= sizeof(type);                                    \
+     }
Quoted 6 lines
Quoted 3 lines

The error is misleading for this case. Enclosing it in a paranthesis doesn't make any sense.

s/a paranthesis/parentheses/

I trusted checkpatch too much. Seems spelling.txt does not have an entry for parentheses. (maybe not the most common spelling error).

Quoted 5 lines
Quoted 4 lines

Checkpatch already has an exception list for such common macro types. Added a new exception for while (...) {...} style blocks to the same. This effectively fixed the wrong error message.

Quoted 8 lines

This seems easier to understand and to judge than the other patch.

We will still need an evaluation, though. Theodoros, maybe you can help us here.

Dwaipayan, looks good to me. Do you fix up the spelling mistake and then send that patch to Joe and lkml?

Thanks, I will do that. Sending just this patch for now. The other one needs more testing. I will send in the report once done.

Quoted 2 lines

Is it just the tab setting in my email client or is there a tab too much that it does not align with the other comments?

Yes, it was an extra tab. The tabs at that particular place are too non-uniform, causing confusion :(

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

#4 lore raw
In reply to Dwaipayan Ray
Quoted 10 lines · Dwaipayan Ray

Checkpatch.pl generates errors which are false positive for certain multi-statemenent macros.

The specific case investigated was whitespace separated statement terminated by a semicolon. Checkpatch wrongly classifies such as a multi-statement macro.

For example, commit 4649079b9de1 ("tracing: Make ftrace packed events have align of 1") when analyzed with checkpatch generates:

Quoted 7 lines

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

The error is misleading in this case and should not be produced. The solution undertaken is to exclude any macro which doesn't have any non-WSP character after the first and only semicolon(;)

We all know what a semicolon is; so, you do not need to put that in braces.

Quoted 4 lines

present in it. This shall allow macros of the form "type item;", "type item[size];", etc. to not generate the multi-statement macro error.

I think the core reason this might work is that it is a string without any semicolon and then semicolon is at the very end of this expression.

Okay, we need an evaluation if this makes the whole situation better or worse...

Theodoros, can you help us and run checkpatch.pl for this one rule on the current kernel source code before and after the patch is applied, and let us know which differences you observe?

Dwaipayan, can you try to do a similar evaluation on git commits?

Quoted 10 lines
---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)
scripts/checkpatch.pl +0 -0
index 9e65d21456f1..72c4072307ea 100755

scripts/checkpatch.pl sub process { · line 5349

Quoted 4 lines
scripts/checkpatch.pl +1 -0
@@ -5346,6 +5346,7 @@ sub process {
 			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
 			    $dstat !~ /^do\s*{/ &&					# do {...
 			    $dstat !~ /^\(\{/ &&						# ({...
+			    $dstat !~ /^[^;]*;\s*$/ &&				# space separated statement;

I do not understand this comment "space separated statement". The example above was a macro for a type definition. Isn't that what you have in mind?

Give it another iteration, but I think the patch is pretty clear and so let us send it out to Joe and lkml with the next patch version.

Lukas

Quoted 7 lines
scripts/checkpatch.pl +0 -0
 			    $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