Skip to content

Commit 5b57980

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: improve runtime execution speed a little
checkpatch repeatedly uses a runtime minimum version check that validates the minimum perl version required for a regex match by using a "$^V ge 5.10.0" runtime string match. Only perform that minimum version test once and store the result to reduce string matching time. This reduces runtime execution time for patches or files with high line counts. An example runtime improvement: new: $ time ./scripts/checkpatch.pl -f drivers/net/ethernet/intel/i40e/i40e_main.c > /dev/null real 0m11.856s user 0m11.831s sys 0m0.025s old: $ time ./scripts/checkpatch.pl -f drivers/net/ethernet/intel/i40e/i40e_main.c > /dev/null real 0m13.330s user 0m13.282s sys 0m0.049s Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Joe Perches <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 79682c0 commit 5b57980

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

scripts/checkpatch.pl

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ sub list_types {
240240

241241
my $exit = 0;
242242

243+
my $perl_version_ok = 1;
243244
if ($^V && $^V lt $minimum_perl_version) {
245+
$perl_version_ok = 0;
244246
printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
245-
if (!$ignore_perl_version) {
246-
exit(1);
247-
}
247+
exit(1) if (!$ignore_perl_version);
248248
}
249249

250250
#if no filenames are given, push '-' to read patch from stdin
@@ -1026,11 +1026,11 @@ sub git_commit_info {
10261026
hash_show_words(\%use_type, "Used");
10271027
hash_show_words(\%ignore_type, "Ignored");
10281028

1029-
if ($^V lt 5.10.0) {
1029+
if (!$perl_version_ok) {
10301030
print << "EOM"
10311031
10321032
NOTE: perl $^V is not modern enough to detect all possible issues.
1033-
An upgrade to at least perl v5.10.0 is suggested.
1033+
An upgrade to at least perl $minimum_perl_version is suggested.
10341034
EOM
10351035
}
10361036
if ($exit) {
@@ -3079,7 +3079,7 @@ sub process {
30793079
}
30803080

30813081
# check indentation starts on a tab stop
3082-
if ($^V && $^V ge 5.10.0 &&
3082+
if ($perl_version_ok &&
30833083
$sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
30843084
my $indent = length($1);
30853085
if ($indent % 8) {
@@ -3092,7 +3092,7 @@ sub process {
30923092
}
30933093

30943094
# check multi-line statement indentation matches previous line
3095-
if ($^V && $^V ge 5.10.0 &&
3095+
if ($perl_version_ok &&
30963096
$prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
30973097
$prevline =~ /^\+(\t*)(.*)$/;
30983098
my $oldindent = $1;
@@ -3967,7 +3967,7 @@ sub process {
39673967

39683968
# function brace can't be on same line, except for #defines of do while,
39693969
# or if closed on same line
3970-
if ($^V && $^V ge 5.10.0 &&
3970+
if ($perl_version_ok &&
39713971
$sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
39723972
$sline !~ /\#\s*define\b.*do\s*\{/ &&
39733973
$sline !~ /}/) {
@@ -4578,7 +4578,7 @@ sub process {
45784578
# check for unnecessary parentheses around comparisons in if uses
45794579
# when !drivers/staging or command-line uses --strict
45804580
if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
4581-
$^V && $^V ge 5.10.0 && defined($stat) &&
4581+
$perl_version_ok && defined($stat) &&
45824582
$stat =~ /(^.\s*if\s*($balanced_parens))/) {
45834583
my $if_stat = $1;
45844584
my $test = substr($2, 1, -1);
@@ -4615,7 +4615,7 @@ sub process {
46154615
# return is not a function
46164616
if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
46174617
my $spacing = $1;
4618-
if ($^V && $^V ge 5.10.0 &&
4618+
if ($perl_version_ok &&
46194619
$stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
46204620
my $value = $1;
46214621
$value = deparenthesize($value);
@@ -4642,7 +4642,7 @@ sub process {
46424642
}
46434643

46444644
# if statements using unnecessary parentheses - ie: if ((foo == bar))
4645-
if ($^V && $^V ge 5.10.0 &&
4645+
if ($perl_version_ok &&
46464646
$line =~ /\bif\s*((?:\(\s*){2,})/) {
46474647
my $openparens = $1;
46484648
my $count = $openparens =~ tr@\(@\(@;
@@ -4659,7 +4659,7 @@ sub process {
46594659
# avoid cases like "foo + BAR < baz"
46604660
# only fix matches surrounded by parentheses to avoid incorrect
46614661
# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4662-
if ($^V && $^V ge 5.10.0 &&
4662+
if ($perl_version_ok &&
46634663
$line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
46644664
my $lead = $1;
46654665
my $const = $2;
@@ -5084,7 +5084,7 @@ sub process {
50845084
# do {} while (0) macro tests:
50855085
# single-statement macros do not need to be enclosed in do while (0) loop,
50865086
# macro should not end with a semicolon
5087-
if ($^V && $^V ge 5.10.0 &&
5087+
if ($perl_version_ok &&
50885088
$realfile !~ m@/vmlinux.lds.h$@ &&
50895089
$line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
50905090
my $ln = $linenr;
@@ -5460,15 +5460,15 @@ sub process {
54605460
}
54615461

54625462
# check for mask then right shift without a parentheses
5463-
if ($^V && $^V ge 5.10.0 &&
5463+
if ($perl_version_ok &&
54645464
$line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
54655465
$4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
54665466
WARN("MASK_THEN_SHIFT",
54675467
"Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
54685468
}
54695469

54705470
# check for pointer comparisons to NULL
5471-
if ($^V && $^V ge 5.10.0) {
5471+
if ($perl_version_ok) {
54725472
while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
54735473
my $val = $1;
54745474
my $equal = "!";
@@ -5740,7 +5740,7 @@ sub process {
57405740
}
57415741

57425742
# Check for __attribute__ weak, or __weak declarations (may have link issues)
5743-
if ($^V && $^V ge 5.10.0 &&
5743+
if ($perl_version_ok &&
57445744
$line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
57455745
($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
57465746
$line =~ /\b__weak\b/)) {
@@ -5822,7 +5822,7 @@ sub process {
58225822
}
58235823

58245824
# check for vsprintf extension %p<foo> misuses
5825-
if ($^V && $^V ge 5.10.0 &&
5825+
if ($perl_version_ok &&
58265826
defined $stat &&
58275827
$stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
58285828
$1 !~ /^_*volatile_*$/) {
@@ -5869,7 +5869,7 @@ sub process {
58695869
}
58705870

58715871
# Check for misused memsets
5872-
if ($^V && $^V ge 5.10.0 &&
5872+
if ($perl_version_ok &&
58735873
defined $stat &&
58745874
$stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
58755875

@@ -5887,7 +5887,7 @@ sub process {
58875887
}
58885888

58895889
# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5890-
# if ($^V && $^V ge 5.10.0 &&
5890+
# if ($perl_version_ok &&
58915891
# defined $stat &&
58925892
# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
58935893
# if (WARN("PREFER_ETHER_ADDR_COPY",
@@ -5898,7 +5898,7 @@ sub process {
58985898
# }
58995899

59005900
# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5901-
# if ($^V && $^V ge 5.10.0 &&
5901+
# if ($perl_version_ok &&
59025902
# defined $stat &&
59035903
# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
59045904
# WARN("PREFER_ETHER_ADDR_EQUAL",
@@ -5907,7 +5907,7 @@ sub process {
59075907

59085908
# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
59095909
# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5910-
# if ($^V && $^V ge 5.10.0 &&
5910+
# if ($perl_version_ok &&
59115911
# defined $stat &&
59125912
# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
59135913
#
@@ -5929,7 +5929,7 @@ sub process {
59295929
# }
59305930

59315931
# typecasts on min/max could be min_t/max_t
5932-
if ($^V && $^V ge 5.10.0 &&
5932+
if ($perl_version_ok &&
59335933
defined $stat &&
59345934
$stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
59355935
if (defined $2 || defined $7) {
@@ -5953,7 +5953,7 @@ sub process {
59535953
}
59545954

59555955
# check usleep_range arguments
5956-
if ($^V && $^V ge 5.10.0 &&
5956+
if ($perl_version_ok &&
59575957
defined $stat &&
59585958
$stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
59595959
my $min = $1;
@@ -5969,7 +5969,7 @@ sub process {
59695969
}
59705970

59715971
# check for naked sscanf
5972-
if ($^V && $^V ge 5.10.0 &&
5972+
if ($perl_version_ok &&
59735973
defined $stat &&
59745974
$line =~ /\bsscanf\b/ &&
59755975
($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
@@ -5983,7 +5983,7 @@ sub process {
59835983
}
59845984

59855985
# check for simple sscanf that should be kstrto<foo>
5986-
if ($^V && $^V ge 5.10.0 &&
5986+
if ($perl_version_ok &&
59875987
defined $stat &&
59885988
$line =~ /\bsscanf\b/) {
59895989
my $lc = $stat =~ tr@\n@@;
@@ -6055,7 +6055,7 @@ sub process {
60556055
}
60566056

60576057
# check for function definitions
6058-
if ($^V && $^V ge 5.10.0 &&
6058+
if ($perl_version_ok &&
60596059
defined $stat &&
60606060
$stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
60616061
$context_function = $1;
@@ -6095,14 +6095,14 @@ sub process {
60956095

60966096
# alloc style
60976097
# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6098-
if ($^V && $^V ge 5.10.0 &&
6098+
if ($perl_version_ok &&
60996099
$line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
61006100
CHK("ALLOC_SIZEOF_STRUCT",
61016101
"Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
61026102
}
61036103

61046104
# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6105-
if ($^V && $^V ge 5.10.0 &&
6105+
if ($perl_version_ok &&
61066106
defined $stat &&
61076107
$stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
61086108
my $oldfunc = $3;
@@ -6131,7 +6131,7 @@ sub process {
61316131
}
61326132

61336133
# check for krealloc arg reuse
6134-
if ($^V && $^V ge 5.10.0 &&
6134+
if ($perl_version_ok &&
61356135
$line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
61366136
WARN("KREALLOC_ARG_REUSE",
61376137
"Reusing the krealloc arg is almost always a bug\n" . $herecurr);
@@ -6200,7 +6200,7 @@ sub process {
62006200
}
62016201

62026202
# check for switch/default statements without a break;
6203-
if ($^V && $^V ge 5.10.0 &&
6203+
if ($perl_version_ok &&
62046204
defined $stat &&
62056205
$stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
62066206
my $cnt = statement_rawlines($stat);
@@ -6317,7 +6317,7 @@ sub process {
63176317
}
63186318

63196319
# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6320-
if ($^V && $^V ge 5.10.0 &&
6320+
if ($perl_version_ok &&
63216321
$line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
63226322
WARN("LIKELY_MISUSE",
63236323
"Using $1 should generally have parentheses around the comparison\n" . $herecurr);
@@ -6360,7 +6360,7 @@ sub process {
63606360
# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
63616361
# and whether or not function naming is typical and if
63626362
# DEVICE_ATTR permissions uses are unusual too
6363-
if ($^V && $^V ge 5.10.0 &&
6363+
if ($perl_version_ok &&
63646364
defined $stat &&
63656365
$stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(?:${multi_mode_perms_string_search}|0[0-7]{3,3})\s*)\s*\)?\s*,\s*(\w+)\s*,\s*(\w+)\s*\)/) {
63666366
my $var = $1;
@@ -6420,7 +6420,7 @@ sub process {
64206420
# specific definition of not visible in sysfs.
64216421
# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
64226422
# use the default permissions
6423-
if ($^V && $^V ge 5.10.0 &&
6423+
if ($perl_version_ok &&
64246424
defined $stat &&
64256425
$line =~ /$mode_perms_search/) {
64266426
foreach my $entry (@mode_permission_funcs) {

0 commit comments

Comments
 (0)