Possible false positive from checkpatch.pl
Andy,
In preparing a vendor driver for submission to staging, I am getting the following from checkpatch.pl:
ERROR: Macros with multiple statements should be enclosed in a do - while loop #377: FILE: staging/rtl8712/rtl871x_mp_ioctl.h:377:
+#define GEN_MP_IOCTL_HANDLER(sz, hdl, oid) {sz, hdl, oid},
ERROR: Macros with multiple statements should be enclosed in a do - while loop #378: FILE: staging/rtl8712/rtl871x_mp_ioctl.h:378:
+#define EXT_MP_IOCTL_HANDLER(sz, subcode, oid) {sz, &mp_ioctl_ \
+ ## subcode ## _hdl, oid},
total: 2 errors, 0 warnings, 466 lines checked
Enclosing these macros in a do {...} while (0) is definitely wrong and will not compile. Moving the comma from the end of the macro to the lines that invoke it fixes the problem, but should not be necessary.
Thanks,
Joe Perches
maintainerQuoted 8 lines · Larry Finger
Andy,
In preparing a vendor driver for submission to staging, I am getting the following from checkpatch.pl:
ERROR: Macros with multiple statements should be enclosed in a do - while loop #377: FILE:
staging/rtl8712/rtl871x_mp_ioctl.h:377:
+#defineGEN_MP_IOCTL_HANDLER(sz, hdl, oid){sz, hdl, oid},
I think you should leave off the trailing comma from the macros and C99 might be better. Maybe something like:
(whatever the field names really are)
#define GEN_MP_IOCTL_HANDLER(sz, hdl, oid) \
{.field1 = sz, .field2 = hdl, .field3 = oid}Review
Quoted 4 lines
ERROR: Macros with multiple statements should be enclosed in a do - while loop #378: FILE:
staging/rtl8712/rtl871x_mp_ioctl.h:378:(quoted diff) +2
-0+#define EXT_MP_IOCTL_HANDLER(sz, subcode, oid) {sz, &mp_ioctl_ \ + ## subcode ## _hdl, oid},
The line continuation is rather ugly too. Perhaps it's better as:
#define EXT_MP_IOCTL_HANDLER(sz, subcode, oid) \
{.field1 = sz, .field2 = &mp_ioctl_##subcode##_hdl, .field3 = oid}They pass checkpatch without error.
$ cat foo.h
#define GEN_MP_IOCTL_HANDLER(sz, hdl, oid) \
{.field1 = sz, .field2 = hdl, .field3 = oid}
#define EXT_MP_IOCTL_HANDLER(sz, subcode, oid) \
{.field1 = sz, .field2 = &mp_ioctl_##subcode##_hdl, .field3 = oid}
$ ./scripts/checkpatch.pl -f foo.h
total: 0 errors, 0 warnings, 4 lines checkedfoo.h has no obvious style problems and is ready for submission.
Quoted 38 lines · Joe Perches
Quoted 8 lines · Larry Finger
Andy,
In preparing a vendor driver for submission to staging, I am getting the following from checkpatch.pl:
ERROR: Macros with multiple statements should be enclosed in a do - while loop #377: FILE:
staging/rtl8712/rtl871x_mp_ioctl.h:377:
+#defineGEN_MP_IOCTL_HANDLER(sz, hdl, oid){sz, hdl, oid},I think you should leave off the trailing comma from the macros and C99 might be better. Maybe something like:
(whatever the field names really are)
#define GEN_MP_IOCTL_HANDLER(sz, hdl, oid) \ {.field1 = sz, .field2 = hdl, .field3 = oid}Review
Quoted 4 lines
ERROR: Macros with multiple statements should be enclosed in a do - while loop #378: FILE:
staging/rtl8712/rtl871x_mp_ioctl.h:378:(quoted diff) +2
-0+#define EXT_MP_IOCTL_HANDLER(sz, subcode, oid) {sz,&mp_ioctl_ \ + ## subcode ## _hdl, oid},The line continuation is rather ugly too. Perhaps it's better as:
#define EXT_MP_IOCTL_HANDLER(sz, subcode, oid) \ {.field1 = sz, .field2 =&mp_ioctl_##subcode##_hdl, .field3 = oid}They pass checkpatch without error.
$ cat foo.h #define GEN_MP_IOCTL_HANDLER(sz, hdl, oid) \ {.field1 = sz, .field2 = hdl, .field3 = oid} #define EXT_MP_IOCTL_HANDLER(sz, subcode, oid) \ {.field1 = sz, .field2 =&mp_ioctl_##subcode##_hdl, .field3 = oid} $ ./scripts/checkpatch.pl -f foo.h total: 0 errors, 0 warnings, 4 lines checked
Quoted 1 line
foo.hhas no obvious style problems and is ready for submission.
These are ugly macros that will be eliminated, but for the moment they are in the code. As I stated in my original email, removing the comma from the definition and adding it to the code does fix the checkpatch error, but it should not be necessary.
Larry
Joe Perches
maintainerQuoted 4 lines · Larry Finger
These are ugly macros that will be eliminated, but for the moment they are in the code. As I stated in my original email, removing the comma from the definition and adding it to the code does fix the checkpatch error, but it should not be necessary.
Hi Larry.
Using checkpatch is not necessary.
If you want generally conforming kernel style,
the macro should not end in a trailing comma.
Feel free to ignore the checkpatch message,
I think the warning is reasonable, though it
could be made more specific.
cheers, Joe
Maybe something like:
---
scripts/checkpatch.pl +4 -2
@@ -2394,8 +2394,10 @@ sub process { }x; #print "REST<$rest> dstat<$dstat>\n"; if ($rest ne '') { - if ($rest !~ /while\s*\(/ && - $dstat !~ /$exceptions/) + if ($rest eq ",") { + ERROR("Macros should not end with a trailing comma\n" . "$here\n$ctx\n"); + } elsif ($rest !~ /while\s*\(/ && + $dstat !~ /$exceptions/) { ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); }
Quoted 29 lines · Joe Perches
Quoted 4 lines · Larry Finger
These are ugly macros that will be eliminated, but for the moment they are in the code. As I stated in my original email, removing the comma from the definition and adding it to the code does fix the checkpatch error, but it should not be necessary.
Hi Larry.
Using checkpatch is not necessary.
If you want generally conforming kernel style,
the macro should not end in a trailing comma.Feel free to ignore the checkpatch message,
I think the warning is reasonable, though it
could be made more specific.cheers, Joe
Maybe something like:
---scripts/checkpatch.pl +0
-0@@ -2394,8 +2394,10 @@ sub process { }x; #print "REST<$rest> dstat<$dstat>\n";
scripts/checkpatch.pl sub process { · line 2403
Quoted 10 lines
scripts/checkpatch.pl +4
-2if ($rest ne '') { - if ($rest !~ /while\s*\(/&& - $dstat !~ /$exceptions/) + if ($rest eq ",") { + ERROR("Macros should not end with a trailing comma\n" . "$here\n$ctx\n"); + } elsif ($rest !~ /while\s*\(/&& + $dstat !~ /$exceptions/) { ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); }
That looks good. At least it makes clear what is wrong.
Should it be an error, or just a warning?
Larry
Joe Perches
maintainerQuoted 13 lines · Larry Finger
Quoted 12 lines · Joe Perches
scripts/checkpatch.pl +2
-2@@ -2394,8 +2394,10 @@ sub process { }x; #print "REST<$rest> dstat<$dstat>\n"; if ($rest ne '') { - if ($rest !~ /while\s*\(/&& - $dstat !~ /$exceptions/) + if ($rest eq ",") { + ERROR("Macros should not end with a trailing comma\n" . "$here\n$ctx\n");
Quoted 8 lines
scripts/checkpatch.pl sub process { · line 2403
Quoted 5 lines
scripts/checkpatch.pl +2
-0+ } elsif ($rest !~ /while\s*\(/&& + $dstat !~ /$exceptions/) { ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); }That looks good. At least it makes clear what is wrong. Should it be an error, or just a warning?
I don't much care.
If Andy wants to do anything, let him decide.
Perhaps the new test should be
if ($rest =~ /\s*,\s*$/)
cheers, Joe
Andy Whitcroft
maintainerQuoted 23 lines · Joe Perches
Quoted 13 lines · Larry Finger
Quoted 12 lines · Joe Perches
scripts/checkpatch.pl +2
-2@@ -2394,8 +2394,10 @@ sub process { }x; #print "REST<$rest> dstat<$dstat>\n"; if ($rest ne '') { - if ($rest !~ /while\s*\(/&& - $dstat !~ /$exceptions/) + if ($rest eq ",") { + ERROR("Macros should not end with a trailing comma\n" . "$here\n$ctx\n");Quoted 8 lines
scripts/checkpatch.pl sub process { · line 2403
Quoted 5 lines
scripts/checkpatch.pl +2
-0+ } elsif ($rest !~ /while\s*\(/&& + $dstat !~ /$exceptions/) { ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); }That looks good. At least it makes clear what is wrong. Should it be an error, or just a warning?
Quoted 6 lines
I don't much care.
If Andy wants to do anything, let him decide.
Perhaps the new test should be
if ($rest =~ /\s*,\s*$/)
I am not sure these are even invalid are they? Macro abuse of this sort is always going to throw up exceptions. Hrmm.
-apw
Joe Perches
maintainerQuoted 10 lines · Andy Whitcroft
Quoted 7 lines · Joe Perches
Quoted 2 lines · Larry Finger
That looks good. At least it makes clear what is wrong. Should it be an error, or just a warning?
I don't much care.
If Andy wants to do anything, let him decide.
Perhaps the new test should beif ($rest =~ /\s*,\s*$/)I am not sure these are even invalid are they? Macro abuse of this sort is always going to throw up exceptions. Hrmm.
This seems to work better.
---
scripts/checkpatch.pl +3 -0
@@ -2393,6 +2393,9 @@ sub process { ^\"|\"$ }x; #print "REST<$rest> dstat<$dstat>\n"; + if ($rest =~ /,\s*$/ || $dstat =~ /,\s*$/) { + ERROR("Macros should not end with a trailing comma\n" . "$here\n$ctx\n"); + } if ($rest ne '') { if ($rest !~ /while\s*\(/ && $dstat !~ /$exceptions/)
Andy Whitcroft
maintainerQuoted 14 lines · Larry Finger
Andy,
In preparing a vendor driver for submission to staging, I am getting the following from checkpatch.pl:
ERROR: Macros with multiple statements should be enclosed in a do - while loop #377: FILE:
staging/rtl8712/rtl871x_mp_ioctl.h:377:
+#defineGEN_MP_IOCTL_HANDLER(sz, hdl, oid){sz, hdl, oid},ERROR: Macros with multiple statements should be enclosed in a do - while loop #378: FILE:
staging/rtl8712/rtl871x_mp_ioctl.h:378:(quoted diff) +2
-0+#define EXT_MP_IOCTL_HANDLER(sz, subcode, oid) {sz, &mp_ioctl_ \ + ## subcode ## _hdl, oid},
Quoted 6 lines
total: 2 errors, 0 warnings, 466 lines checked
Enclosing these macros in a do {...} while (0) is definitely wrong and will not compile. Moving the comma from the end of the macro to the lines that invoke it fixes the problem, but should not be necessary.
They do look like a false positive to me. Its not entirly obvious id we cna detect them as valid exceptions. Always remember that checkpatch is an advisor, if its wrong it can and should be ignored.
I will think on this some.
-apw