Skip to content

Commit 15d4734

Browse files
jimcakpm00
authored andcommitted
checkpatch: qualify do-while-0 advice
Add a paragraph of advice qualifying the general do-while-0 advice, noting 3 possible misguidings. reduce one ERROR to WARN, for the case I actually encountered. And add 'static_assert' to named exceptions, along with some additional comments about named exceptions vs (detection of) declarative construction primitives (union, struct, [], etc). Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Jim Cromie <[email protected]> Cc: Andy Whitcroft <[email protected]> Cc: Joe Perches <[email protected]> Cc: Dwaipayan Ray <[email protected]> Cc: Lukas Bulwahn <[email protected]> Cc: Louis Chauvet <[email protected]> Cc: Viresh Kumar <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent df3d527 commit 15d4734

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

scripts/checkpatch.pl

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ sub help {
151151
exit($exitcode);
152152
}
153153

154+
my $DO_WHILE_0_ADVICE = q{
155+
do {} while (0) advice is over-stated in a few situations:
156+
157+
The more obvious case is macros, like MODULE_PARM_DESC, invoked at
158+
file-scope, where C disallows code (it must be in functions). See
159+
$exceptions if you have one to add by name.
160+
161+
More troublesome is declarative macros used at top of new scope,
162+
like DECLARE_PER_CPU. These might just compile with a do-while-0
163+
wrapper, but would be incorrect. Most of these are handled by
164+
detecting struct,union,etc declaration primitives in $exceptions.
165+
166+
Theres also macros called inside an if (block), which "return" an
167+
expression. These cannot do-while, and need a ({}) wrapper.
168+
169+
Enjoy this qualification while we work to improve our heuristics.
170+
};
171+
154172
sub uniq {
155173
my %seen;
156174
return grep { !$seen{$_}++ } @_;
@@ -5883,9 +5901,9 @@ sub process {
58835901
}
58845902
}
58855903

5886-
# multi-statement macros should be enclosed in a do while loop, grab the
5887-
# first statement and ensure its the whole macro if its not enclosed
5888-
# in a known good container
5904+
# Usually multi-statement macros should be enclosed in a do {} while
5905+
# (0) loop. Grab the first statement and ensure its the whole macro
5906+
# if its not enclosed in a known good container
58895907
if ($realfile !~ m@/vmlinux.lds.h$@ &&
58905908
$line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
58915909
my $ln = $linenr;
@@ -5938,10 +5956,13 @@ sub process {
59385956

59395957
my $exceptions = qr{
59405958
$Declare|
5959+
# named exceptions
59415960
module_param_named|
59425961
MODULE_PARM_DESC|
59435962
DECLARE_PER_CPU|
59445963
DEFINE_PER_CPU|
5964+
static_assert|
5965+
# declaration primitives
59455966
__typeof__\(|
59465967
union|
59475968
struct|
@@ -5976,11 +5997,11 @@ sub process {
59765997
ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
59775998
"Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
59785999
} elsif ($dstat =~ /;/) {
5979-
ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5980-
"Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
6000+
WARN("MULTISTATEMENT_MACRO_USE_DO_WHILE",
6001+
"Non-declarative macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx\nBUT SEE:\n$DO_WHILE_0_ADVICE");
59816002
} else {
59826003
ERROR("COMPLEX_MACRO",
5983-
"Macros with complex values should be enclosed in parentheses\n" . "$herectx");
6004+
"Macros with complex values should be enclosed in parentheses\n" . "$herectx\nBUT SEE:\n$DO_WHILE_0_ADVICE");
59846005
}
59856006

59866007
}

0 commit comments

Comments
 (0)