Debian Codegraph @

build-aux/useless-if-before-free

raw
1 #!/bin/sh
2 #! -*-perl-*-
3
4 # Detect instances of "if (p) free (p);".
5 # Likewise "if (p != 0)", "if (0 != p)", or with NULL; and with braces.
6
7 # Copyright (C) 2008-2024 Free Software Foundation, Inc.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 #
22 # Written by Jim Meyering
23
24 # This is a prologue that allows to run a perl script as an executable
25 # on systems that are compliant to a POSIX version before POSIX:2017.
26 # On such systems, the usual invocation of an executable through execlp()
27 # or execvp() fails with ENOEXEC if it is a script that does not start
28 # with a #! line. The script interpreter mentioned in the #! line has
29 # to be /bin/sh, because on GuixSD systems that is the only program that
30 # has a fixed file name. The second line is essential for perl and is
31 # also useful for editing this file in Emacs. The next two lines below
32 # are valid code in both sh and perl. When executed by sh, they re-execute
33 # the script through the perl program found in $PATH. The '-x' option
34 # is essential as well; without it, perl would re-execute the script
35 # through /bin/sh. When executed by perl, the next two lines are a no-op.
36 eval 'exec perl -wSx "$0" "$@"'
37 if 0;
38
39 my $VERSION = '2024-07-17 02:10'; # UTC
40 # The definition above must lie within the first 8 lines in order
41 # for the Emacs time-stamp write hook (at end) to update it.
42 # If you change this file with Emacs, please let the write hook
43 # do its job. Otherwise, update this string manually.
44
45 (my $copyright_year = $VERSION) =~ s/^(\d*)-.*$/$1/;
46
47 use strict;
48 use warnings;
49 use Getopt::Long;
50
51 (my $ME = $0) =~ s|.*/||;
52
53 # use File::Coda; # https://meyering.net/code/Coda/
54 END {
55 defined fileno STDOUT or return;
56 close STDOUT and return;
57 warn "$ME: failed to close standard output: $!\n";
58 $? ||= 1;
59 }
60
61 sub usage ($)
62 {
63 my ($exit_code) = @_;
64 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
65 if ($exit_code != 0)
66 {
67 print $STREAM "Try '$ME --help' for more information.\n";
68 }
69 else
70 {
71 print $STREAM <<EOF;
72 Usage: $ME [OPTIONS] FILE...
73
74 Detect any instance in FILE of a useless "if" test before a free call, e.g.,
75 "if (p) free (p);". Any such test may be safely removed without affecting
76 the semantics of the C code in FILE. Use --name=FOO --name=BAR to also
77 detect free-like functions named FOO and BAR.
78
79 OPTIONS:
80
81 --list print only the name of each matching FILE (\\0-terminated)
82 --name=N add name N to the list of \'free\'-like functions to detect;
83 may be repeated
84
85 --help display this help and exit
86 --version output version information and exit
87
88 Exit status:
89
90 0 one or more matches
91 1 no match
92 2 an error
93
94 EXAMPLE:
95
96 For example, this command prints all removable "if" tests before "free"
97 and "kfree" calls in the linux kernel sources:
98
99 git ls-files -z |xargs -0 $ME --name=kfree
100
101 Send patches and bug reports to <bug-gnulib\@gnu.org>.
102 EOF
103 }
104 exit $exit_code;
105 }
106
107 sub is_NULL ($)
108 {
109 my ($expr) = @_;
110 return ($expr eq 'NULL' || $expr eq 'nullptr' || $expr eq '0');
111 }
112
113 {
114 sub EXIT_MATCH {0}
115 sub EXIT_NO_MATCH {1}
116 sub EXIT_ERROR {2}
117 my $err = EXIT_NO_MATCH;
118
119 my $list;
120 my @name = qw(free);
121 GetOptions
122 (
123 help => sub { usage 0 },
124 version =>
125 sub
126 {
127 print "$ME version $VERSION\n";
128 print "Copyright (C) $copyright_year Free Software Foundation, Inc.\n";
129 print "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n"
130 . "This is free software: you are free to change and redistribute it.\n"
131 . "There is NO WARRANTY, to the extent permitted by law.\n";
132 print "\n";
133 my $author = "Jim Meyering";
134 print "Written by $author.\n";
135 exit
136 },
137 list => \$list,
138 'name=s@' => \@name,
139 ) or usage 1;
140
141 # Make sure we have the right number of non-option arguments.
142 # Always tell the user why we fail.
143 @ARGV < 1
144 and (warn "$ME: missing FILE argument\n"), usage EXIT_ERROR;
145
146 my $or = join '|', @name;
147 my $regexp = qr/(?:$or)/;
148
149 # Set the input record separator.
150 # Note: this makes it impractical to print line numbers.
151 $/ = '"';
152
153 my $found_match = 0;
154 FILE:
155 foreach my $file (@ARGV)
156 {
157 open FH, '<', $file
158 or (warn "$ME: can't open '$file' for reading: $!\n"),
159 $err = EXIT_ERROR, next;
160 while (defined (my $line = <FH>))
161 {
162 # Skip non-matching lines early to save time
163 $line =~ /\bif\b/
164 or next;
165 while ($line =~
166 /\b(if\s*\(\s*([^)]+?)(?:\s*!=\s*([^)]+?))?\s*\)
167 # 1 2 3
168 (?: \s*$regexp\s*\((?:\s*\([^)]+\))?\s*([^)]+)\)\s*;|
169 \s*\{\s*$regexp\s*\((?:\s*\([^)]+\))?\s*([^)]+)\)\s*;\s*\}))/sxg)
170 {
171 my $all = $1;
172 my ($lhs, $rhs) = ($2, $3);
173 my ($free_opnd, $braced_free_opnd) = ($4, $5);
174 my $non_NULL;
175 if (!defined $rhs) { $non_NULL = $lhs }
176 elsif (is_NULL $rhs) { $non_NULL = $lhs }
177 elsif (is_NULL $lhs) { $non_NULL = $rhs }
178 else { next }
179
180 # Compare the non-NULL part of the "if" expression and the
181 # free'd expression, without regard to white space.
182 $non_NULL =~ tr/ \t//d;
183 my $e2 = defined $free_opnd ? $free_opnd : $braced_free_opnd;
184 $e2 =~ tr/ \t//d;
185 if ($non_NULL eq $e2)
186 {
187 $found_match = 1;
188 $list
189 and (print "$file\0"), next FILE;
190 print "$file: $all\n";
191 }
192 }
193 }
194 }
195 continue
196 {
197 close FH;
198 }
199
200 $found_match && $err == EXIT_NO_MATCH
201 and $err = EXIT_MATCH;
202
203 exit $err;
204 }
205
206 my $foo = <<'EOF';
207 # The above is to *find* them.
208 # This adjusts them, removing the unnecessary "if (p)" part.
209
210 # FIXME: do something like this as an option (doesn't do braces):
211 free=xfree
212 git grep -l -z "$free *(" \
213 | xargs -0 useless-if-before-free -l --name="$free" \
214 | xargs -0 perl -0x3b -pi -e \
215 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*(?:0|NULL|nullptr))?\s*\)\s+('"$free"'\s*\((?:\s*\([^)]+\))?\s*\1\s*\)\s*;)/$2/s'
216
217 # Use the following to remove redundant uses of kfree inside braces.
218 # Note that -0777 puts perl in slurp-whole-file mode;
219 # but we have plenty of memory, these days...
220 free=kfree
221 git grep -l -z "$free *(" \
222 | xargs -0 useless-if-before-free -l --name="$free" \
223 | xargs -0 perl -0777 -pi -e \
224 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*(?:0|NULL|nullptr))?\s*\)\s*\{\s*('"$free"'\s*\((?:\s*\([^)]+\))?\s*\1\s*\);)\s*\}[^\n]*$/$2/gms'
225
226 Be careful that the result of the above transformation is valid.
227 If the matched string is followed by "else", then obviously, it won't be.
228
229 When modifying files, refuse to process anything other than a regular file.
230 EOF
231
232 ## Local Variables:
233 ## mode: perl
234 ## indent-tabs-mode: nil
235 ## eval: (add-hook 'before-save-hook 'time-stamp nil t)
236 ## time-stamp-line-limit: 50
237 ## time-stamp-start: "my $VERSION = '"
238 ## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
239 ## time-stamp-time-zone: "UTC0"
240 ## time-stamp-end: "'; # UTC"
241 ## End: