1
eval '(exit $?0)' && eval 'exec perl -wST "$0" ${1+"$@"}'
2
& eval 'exec perl -wST "$0" $argv:q'
4
# Detect instances of "if (p) free (p);".
5
# Likewise "if (p != 0)", "if (0 != p)", or with NULL; and with braces.
7
my $VERSION = '2012-01-06 07:23'; # UTC
8
# The definition above must lie within the first 8 lines in order
9
# for the Emacs time-stamp write hook (at end) to update it.
10
# If you change this file with Emacs, please let the write hook
11
# do its job. Otherwise, update this string manually.
13
# Copyright (C) 2008-2012 Free Software Foundation, Inc.
15
# This program is free software: you can redistribute it and/or modify
16
# it under the terms of the GNU General Public License as published by
17
# the Free Software Foundation, either version 3 of the License, or
18
# (at your option) any later version.
20
# This program is distributed in the hope that it will be useful,
21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
# GNU General Public License for more details.
25
# You should have received a copy of the GNU General Public License
26
# along with this program. If not, see <http://www.gnu.org/licenses/>.
28
# Written by Jim Meyering
34
(my $ME = $0) =~ s|.*/||;
36
# use File::Coda; # http://meyering.net/code/Coda/
38
defined fileno STDOUT or return;
39
close STDOUT and return;
40
warn "$ME: failed to close standard output: $!\n";
47
my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
50
print $STREAM "Try '$ME --help' for more information.\n";
55
Usage: $ME [OPTIONS] FILE...
57
Detect any instance in FILE of a useless "if" test before a free call, e.g.,
58
"if (p) free (p);". Any such test may be safely removed without affecting
59
the semantics of the C code in FILE. Use --name=FOO --name=BAR to also
60
detect free-like functions named FOO and BAR.
64
--list print only the name of each matching FILE (\\0-terminated)
65
--name=N add name N to the list of \'free\'-like functions to detect;
68
--help display this help and exit
69
--version output version information and exit
79
For example, this command prints all removable "if" tests before "free"
80
and "kfree" calls in the linux kernel sources:
82
git ls-files -z |xargs -0 $ME --name=kfree
92
return ($expr eq 'NULL' || $expr eq '0');
99
my $err = EXIT_NO_MATCH;
105
help => sub { usage 0 },
106
version => sub { print "$ME version $VERSION\n"; exit },
111
# Make sure we have the right number of non-option arguments.
112
# Always tell the user why we fail.
114
and (warn "$ME: missing FILE argument\n"), usage EXIT_ERROR;
116
my $or = join '|', @name;
117
my $regexp = qr/(?:$or)/;
119
# Set the input record separator.
120
# Note: this makes it impractical to print line numbers.
125
foreach my $file (@ARGV)
128
or (warn "$ME: can't open '$file' for reading: $!\n"),
129
$err = EXIT_ERROR, next;
130
while (defined (my $line = <FH>))
133
/\b(if\s*\(\s*([^)]+?)(?:\s*!=\s*([^)]+?))?\s*\)
135
(?: \s*$regexp\s*\((?:\s*\([^)]+\))?\s*([^)]+)\)\s*;|
136
\s*\{\s*$regexp\s*\((?:\s*\([^)]+\))?\s*([^)]+)\)\s*;\s*\}))/sxg)
139
my ($lhs, $rhs) = ($2, $3);
140
my ($free_opnd, $braced_free_opnd) = ($4, $5);
142
if (!defined $rhs) { $non_NULL = $lhs }
143
elsif (is_NULL $rhs) { $non_NULL = $lhs }
144
elsif (is_NULL $lhs) { $non_NULL = $rhs }
147
# Compare the non-NULL part of the "if" expression and the
148
# free'd expression, without regard to white space.
149
$non_NULL =~ tr/ \t//d;
150
my $e2 = defined $free_opnd ? $free_opnd : $braced_free_opnd;
152
if ($non_NULL eq $e2)
156
and (print "$file\0"), next FILE;
157
print "$file: $all\n";
167
$found_match && $err == EXIT_NO_MATCH
168
and $err = EXIT_MATCH;
174
# The above is to *find* them.
175
# This adjusts them, removing the unnecessary "if (p)" part.
177
# FIXME: do something like this as an option (doesn't do braces):
179
git grep -l -z "$free *(" \
180
| xargs -0 useless-if-before-free -l --name="$free" \
181
| xargs -0 perl -0x3b -pi -e \
182
's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*(?:0|NULL))?\s*\)\s+('"$free"'\s*\((?:\s*\([^)]+\))?\s*\1\s*\)\s*;)/$2/s'
184
# Use the following to remove redundant uses of kfree inside braces.
185
# Note that -0777 puts perl in slurp-whole-file mode;
186
# but we have plenty of memory, these days...
188
git grep -l -z "$free *(" \
189
| xargs -0 useless-if-before-free -l --name="$free" \
190
| xargs -0 perl -0777 -pi -e \
191
's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*(?:0|NULL))?\s*\)\s*\{\s*('"$free"'\s*\((?:\s*\([^)]+\))?\s*\1\s*\);)\s*\}[^\n]*$/$2/gms'
193
Be careful that the result of the above transformation is valid.
194
If the matched string is followed by "else", then obviously, it won't be.
196
When modifying files, refuse to process anything other than a regular file.
201
## indent-tabs-mode: nil
202
## eval: (add-hook 'write-file-hooks 'time-stamp)
203
## time-stamp-start: "my $VERSION = '"
204
## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
205
## time-stamp-time-zone: "UTC"
206
## time-stamp-end: "'; # UTC"