diff options
author | Joe Perches <joe@perches.com> | 2011-07-25 20:13:22 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2011-07-25 23:57:16 -0400 |
commit | 7d2367af0b09f8028dc5c1b1919bb82d141c2afb (patch) | |
tree | e2645027cae1b716443e2659bda5364cb101d4e0 /scripts | |
parent | 27c46a2546c75c6814562e85b751e3d64c188ad5 (diff) |
checkpatch: suggest using min_t or max_t
A common issue with min() or max() is using a cast on one or both of the
arguments when using min_t/max_t could be better.
Add cast detection to uses of min/max and suggest an appropriate use of
min_t or max_t instead.
Caveat: This only works for min() or max() on a single line.
It does not find min() or max() split across multiple lines.
This does find:
min((u32)foo, bar);
But it does not find:
max((unsigned long)foo,
bar);
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
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')
-rwxr-xr-x | scripts/checkpatch.pl | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index b0aa2c680593..5ba62d8b5e52 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -268,6 +268,20 @@ sub build_types { | |||
268 | } | 268 | } |
269 | build_types(); | 269 | build_types(); |
270 | 270 | ||
271 | our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/; | ||
272 | |||
273 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; | ||
274 | our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*}; | ||
275 | |||
276 | sub deparenthesize { | ||
277 | my ($string) = @_; | ||
278 | return "" if (!defined($string)); | ||
279 | $string =~ s@^\s*\(\s*@@g; | ||
280 | $string =~ s@\s*\)\s*$@@g; | ||
281 | $string =~ s@\s+@ @g; | ||
282 | return $string; | ||
283 | } | ||
284 | |||
271 | $chk_signoff = 0 if ($file); | 285 | $chk_signoff = 0 if ($file); |
272 | 286 | ||
273 | my @dep_includes = (); | 287 | my @dep_includes = (); |
@@ -2290,6 +2304,27 @@ sub process { | |||
2290 | } | 2304 | } |
2291 | } | 2305 | } |
2292 | 2306 | ||
2307 | # typecasts on min/max could be min_t/max_t | ||
2308 | if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) { | ||
2309 | if (defined $2 || defined $8) { | ||
2310 | my $call = $1; | ||
2311 | my $cast1 = deparenthesize($2); | ||
2312 | my $arg1 = $3; | ||
2313 | my $cast2 = deparenthesize($8); | ||
2314 | my $arg2 = $9; | ||
2315 | my $cast; | ||
2316 | |||
2317 | if ($cast1 ne "" && $cast2 ne "") { | ||
2318 | $cast = "$cast1 or $cast2"; | ||
2319 | } elsif ($cast1 ne "") { | ||
2320 | $cast = $cast1; | ||
2321 | } else { | ||
2322 | $cast = $cast2; | ||
2323 | } | ||
2324 | WARN("$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr); | ||
2325 | } | ||
2326 | } | ||
2327 | |||
2293 | # Need a space before open parenthesis after if, while etc | 2328 | # Need a space before open parenthesis after if, while etc |
2294 | if ($line=~/\b(if|while|for|switch)\(/) { | 2329 | if ($line=~/\b(if|while|for|switch)\(/) { |
2295 | ERROR("space required before the open parenthesis '('\n" . $herecurr); | 2330 | ERROR("space required before the open parenthesis '('\n" . $herecurr); |