[Linux-kernel-mentees] [PATCH RFC] checkpatch: fix multistatement macro checks

Dwaipayan Ray dwaipayanray1 at gmail.com
Tue Sep 29 20:57:31 UTC 2020


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.

Signed-off-by: Dwaipayan Ray <dwaipayanray1 at gmail.com>
---
 scripts/checkpatch.pl | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 9e65d21456f1..4fb612200c27 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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/) {
-- 
2.27.0



More information about the Linux-kernel-mentees mailing list