2 # Detect instances of "if (p) free (p);".
3 # Likewise for "if (p != NULL) free (p);". And with braces.
5 my $VERSION = '2008-02-10 19:36'; # UTC
6 # The definition above must lie within the first 8 lines in order
7 # for the Emacs time-stamp write hook (at end) to update it.
8 # If you change this file with Emacs, please let the write hook
9 # do its job. Otherwise, update this string manually.
11 # Copyright (C) 2008 Free Software Foundation, Inc.
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # Written by Jim Meyering
32 (my $ME = $0) =~ s|.*/||;
37 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
40 print $STREAM "Try `$ME --help' for more information.\n";
45 Usage: $ME [OPTIONS] FILE...
47 Detect any instance in FILE of a useless "if" test before a free call, e.g.,
48 "if (p) free (p);". Any such test may be safely removed without affecting
49 the semantics of the C code in FILE. Use --name=FOO --name=BAR to also
50 detect free-like functions named FOO and BAR.
54 --name=N add name N to the list of `free'-like functions to detect;
57 --help display this help and exit
58 --version output version information and exit
68 For example, this command prints all removable "if" tests before "free"
69 and "kfree" calls in the linux kernel sources:
71 git ls-files -z |xargs -0 $ME --name=kfree
82 my $err = EXIT_NO_MATCH;
87 help => sub { usage 0 },
88 version => sub { print "$ME version $VERSION\n"; exit },
92 # Make sure we have the right number of non-option arguments.
93 # Always tell the user why we fail.
95 and (warn "$ME: missing FILE argument\n"), usage EXIT_ERROR;
97 my $or = join '|', @name;
98 my $regexp = qr/(?:$or)/;
100 # Set the input record separator.
101 # Note: this makes it impractical to print line numbers.
105 foreach my $file (@ARGV)
108 or (warn "$ME: can't open `$file' for reading: $!\n"),
109 $err = EXIT_ERROR, next;
110 while (defined (my $line = <FH>))
113 /\b(if\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)
114 (?: \s*$regexp\s*\(\s*\2\s*\)|
115 \s*\{\s*$regexp\s*\(\s*\2\s*\)\s*;\s*\}))/sx)
124 $found_match && $err == EXIT_NO_MATCH
125 and $err = EXIT_MATCH;
131 # The above is to *find* them.
132 # This adjusts them, removing the unnecessary "if (p)" part.
134 # FIXME: do something like this as an option (doesn't do braces):
135 git ls-files -z |xargs -0 \
136 perl -0x3b -pi -e 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(k?free\s*\(\s*\1\s*\))/$2/s'
138 Be careful that the result of the above transformation is valid.
139 If the matched string is followed by "else", then obviously, it won't be.
141 When modifying files, refuse to process anything other than a regular file.
145 ## indent-tabs-mode: nil
146 ## eval: (add-hook 'write-file-hooks 'time-stamp)
147 ## time-stamp-start: "my $VERSION = '"
148 ## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
149 ## time-stamp-time-zone: "UTC"
150 ## time-stamp-end: "'; # UTC"