useless-if-before-free: New option: --list (-l).
[gnulib.git] / build-aux / useless-if-before-free
1 #!/usr/bin/perl -T
2 # Detect instances of "if (p) free (p);".
3 # Likewise for "if (p != NULL) free (p);".  And with braces.
4
5 my $VERSION = '2008-02-10 22:17'; # 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.
10
11 # Copyright (C) 2008 Free Software Foundation, Inc.
12
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.
17
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.
22
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/>.
25
26 # Written by Jim Meyering
27
28 use strict;
29 use warnings;
30 use Getopt::Long;
31
32 (my $ME = $0) =~ s|.*/||;
33
34 sub usage ($)
35 {
36   my ($exit_code) = @_;
37   my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
38   if ($exit_code != 0)
39     {
40       print $STREAM "Try `$ME --help' for more information.\n";
41     }
42   else
43     {
44       print $STREAM <<EOF;
45 Usage: $ME [OPTIONS] FILE...
46
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.
51
52 OPTIONS:
53
54    --list       print only the name of each matching FILE (\0-terminated)
55    --name=N     add name N to the list of `free'-like functions to detect;
56                   may be repeated
57
58    --help       display this help and exit
59    --version    output version information and exit
60
61 Exit status:
62
63   0   no match
64   1   one or more matches
65   2   an error
66
67 EXAMPLE:
68
69 For example, this command prints all removable "if" tests before "free"
70 and "kfree" calls in the linux kernel sources:
71
72     git ls-files -z |xargs -0 $ME --name=kfree
73
74 EOF
75     }
76   exit $exit_code;
77 }
78
79 {
80   sub EXIT_NO_MATCH {0}
81   sub EXIT_MATCH {1}
82   sub EXIT_ERROR {2}
83   my $err = EXIT_NO_MATCH;
84
85   my $list;
86   my @name = qw(free);
87   GetOptions
88     (
89      help => sub { usage 0 },
90      version => sub { print "$ME version $VERSION\n"; exit },
91      list => \$list,
92      'name=s@' => \@name,
93     ) or usage 1;
94
95   # Make sure we have the right number of non-option arguments.
96   # Always tell the user why we fail.
97   @ARGV < 1
98     and (warn "$ME: missing FILE argument\n"), usage EXIT_ERROR;
99
100   my $or = join '|', @name;
101   my $regexp = qr/(?:$or)/;
102
103   # Set the input record separator.
104   # Note: this makes it impractical to print line numbers.
105   $/ = '"';
106
107   my $found_match = 0;
108  FILE:
109   foreach my $file (@ARGV)
110     {
111       open FH, '<', $file
112         or (warn "$ME: can't open `$file' for reading: $!\n"),
113           $err = EXIT_ERROR, next;
114       while (defined (my $line = <FH>))
115         {
116           if ($line =~
117               /\b(if\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)
118                (?:   \s*$regexp\s*\(\s*\2\s*\)|
119                 \s*\{\s*$regexp\s*\(\s*\2\s*\)\s*;\s*\}))/sx)
120             {
121               $found_match = 1;
122               $list
123                 and (print "$file\0"), next FILE;
124               print "$file: $1\n";
125             }
126         }
127     }
128   continue
129     {
130       close FH;
131     }
132
133   $found_match && $err == EXIT_NO_MATCH
134     and $err = EXIT_MATCH;
135
136   exit $err;
137 }
138
139 my $foo = <<'EOF';
140 # The above is to *find* them.
141 # This adjusts them, removing the unnecessary "if (p)" part.
142
143 # FIXME: do something like this as an option (doesn't do braces):
144 git ls-files -z |xargs -0 \
145 perl -0x3b -pi -e 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(k?free\s*\(\s*\1\s*\))/$2/s'
146
147 useless-if-before-free -l $(lid -knone free) | xargs -0 \
148   perl -0x3b -pi -e \
149    's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(free\s*\(\s*\1\s*\))/$2/s'
150
151 Be careful that the result of the above transformation is valid.
152 If the matched string is followed by "else", then obviously, it won't be.
153
154 When modifying files, refuse to process anything other than a regular file.
155 EOF
156
157 ## Local Variables:
158 ## indent-tabs-mode: nil
159 ## eval: (add-hook 'write-file-hooks 'time-stamp)
160 ## time-stamp-start: "my $VERSION = '"
161 ## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
162 ## time-stamp-time-zone: "UTC"
163 ## time-stamp-end: "'; # UTC"
164 ## End: