Skip to content

Commit b13edf7

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: add checks for do {} while (0) macro misuses
These types of macros should not be used for either a single statement nor should the macro end with a semi-colon. Add tests for these conditions. Signed-off-by: Joe Perches <joe@perches.com> Cc: Andy Whitcroft <apw@canonical.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 66c80b6 commit b13edf7

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2988,6 +2988,45 @@ sub process {
29882988
}
29892989
}
29902990

2991+
# do {} while (0) macro tests:
2992+
# single-statement macros do not need to be enclosed in do while (0) loop,
2993+
# macro should not end with a semicolon
2994+
if ($^V && $^V ge 5.10.0 &&
2995+
$realfile !~ m@/vmlinux.lds.h$@ &&
2996+
$line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
2997+
my $ln = $linenr;
2998+
my $cnt = $realcnt;
2999+
my ($off, $dstat, $dcond, $rest);
3000+
my $ctx = '';
3001+
($dstat, $dcond, $ln, $cnt, $off) =
3002+
ctx_statement_block($linenr, $realcnt, 0);
3003+
$ctx = $dstat;
3004+
3005+
$dstat =~ s/\\\n.//g;
3006+
3007+
if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3008+
my $stmts = $2;
3009+
my $semis = $3;
3010+
3011+
$ctx =~ s/\n*$//;
3012+
my $cnt = statement_rawlines($ctx);
3013+
my $herectx = $here . "\n";
3014+
3015+
for (my $n = 0; $n < $cnt; $n++) {
3016+
$herectx .= raw_line($linenr, $n) . "\n";
3017+
}
3018+
3019+
if (($stmts =~ tr/;/;/) == 1) {
3020+
WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3021+
"Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3022+
}
3023+
if (defined $semis && $semis ne "") {
3024+
WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3025+
"do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3026+
}
3027+
}
3028+
}
3029+
29913030
# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
29923031
# all assignments may have only one of the following with an assignment:
29933032
# .

0 commit comments

Comments
 (0)