History

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.

Joe Perches committed 14 years ago

Commit b13edf71 parent 66c80b61 file · +39 −0Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+39−0
@@ -2988,6 +2988,45 @@ sub process { · 2987 unmodified lines
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 # .

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t