aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
authorJoe Perches <joe@perches.com>2014-10-13 18:51:55 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-10-13 20:18:15 -0400
commit08a2843e77fc581d204c1e83de4678b746cdbd6e (patch)
treee1cddaa649c00e4181f19c01d76fead28b5af8e0 /scripts/checkpatch.pl
parentd2207ccbc59900311c88bb9150b24253cd4ddd49 (diff)
checkpatch: warn on macros with flow control statements
Macros with flow control statements (goto and return) are not very nice to read as any flow movement is unexpected. Try to highlight them and emit a warning on their definition. Avoid warning on macros that use argument concatenation as those macros commonly create another function where the concatenation is used in the function name definition like: #define FOO_FUNC(name, rtn_type) \ rtn_type func##name(arg1, ...) \ { \ rtn_type rtn; \ [code...] \ return rtn; \ } Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl18
1 files changed, 18 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 0c520f7bf095..7a360a8c1b91 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4066,12 +4066,17 @@ sub process {
4066 my $cnt = $realcnt; 4066 my $cnt = $realcnt;
4067 my ($off, $dstat, $dcond, $rest); 4067 my ($off, $dstat, $dcond, $rest);
4068 my $ctx = ''; 4068 my $ctx = '';
4069 my $has_flow_statement = 0;
4070 my $has_arg_concat = 0;
4069 ($dstat, $dcond, $ln, $cnt, $off) = 4071 ($dstat, $dcond, $ln, $cnt, $off) =
4070 ctx_statement_block($linenr, $realcnt, 0); 4072 ctx_statement_block($linenr, $realcnt, 0);
4071 $ctx = $dstat; 4073 $ctx = $dstat;
4072 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4074 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4073 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4075 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4074 4076
4077 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4078 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4079
4075 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 4080 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4076 $dstat =~ s/$;//g; 4081 $dstat =~ s/$;//g;
4077 $dstat =~ s/\\\n.//g; 4082 $dstat =~ s/\\\n.//g;
@@ -4136,6 +4141,19 @@ sub process {
4136 } 4141 }
4137 } 4142 }
4138 4143
4144# check for macros with flow control, but without ## concatenation
4145# ## concatenation is commonly a macro that defines a function so ignore those
4146 if ($has_flow_statement && !$has_arg_concat) {
4147 my $herectx = $here . "\n";
4148 my $cnt = statement_rawlines($ctx);
4149
4150 for (my $n = 0; $n < $cnt; $n++) {
4151 $herectx .= raw_line($linenr, $n) . "\n";
4152 }
4153 WARN("MACRO_WITH_FLOW_CONTROL",
4154 "Macros with flow control statements should be avoided\n" . "$herectx");
4155 }
4156
4139# check for line continuations outside of #defines, preprocessor #, and asm 4157# check for line continuations outside of #defines, preprocessor #, and asm
4140 4158
4141 } else { 4159 } else {