X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=build-aux%2Fuseless-if-before-free;h=696c62147c85d48331f2f2a6c65bd8098ccca996;hb=636b8d6abf5b75d3cd96949c58519f100779c7e2;hp=f08b6cba6c2ef579fc33c84ce19ccc2f5de3964d;hpb=6d3f150872497bf8fd6c74aa309f2509476a13a8;p=gnulib.git diff --git a/build-aux/useless-if-before-free b/build-aux/useless-if-before-free index f08b6cba6..696c62147 100755 --- a/build-aux/useless-if-before-free +++ b/build-aux/useless-if-before-free @@ -1,14 +1,16 @@ #!/usr/bin/perl -T # Detect instances of "if (p) free (p);". # Likewise for "if (p != NULL) free (p);". And with braces. +# Also detect "if (NULL != p) free (p);". +# And with 0 in place of NULL. -my $VERSION = '2008-05-25 16:59'; # UTC +my $VERSION = '2009-01-28 08:23'; # UTC # The definition above must lie within the first 8 lines in order # for the Emacs time-stamp write hook (at end) to update it. # If you change this file with Emacs, please let the write hook # do its job. Otherwise, update this string manually. -# Copyright (C) 2008 Free Software Foundation, Inc. +# Copyright (C) 2008, 2009 Free Software Foundation, Inc. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -60,7 +62,7 @@ detect free-like functions named FOO and BAR. OPTIONS: --list print only the name of each matching FILE (\0-terminated) - --name=N add name N to the list of `free'-like functions to detect; + --name=N add name N to the list of \`free\'-like functions to detect; may be repeated --help display this help and exit @@ -84,6 +86,12 @@ EOF exit $exit_code; } +sub is_NULL ($) +{ + my ($expr) = @_; + return ($expr eq 'NULL' || $expr eq '0'); +} + { sub EXIT_MATCH {0} sub EXIT_NO_MATCH {1} @@ -122,14 +130,32 @@ EOF while (defined (my $line = )) { while ($line =~ - /\b(?:if\s*\(\s*([^)]+?)(?:\s*!=\s*NULL)?\s*\) - (?: \s*$regexp\s*\((?:\s*\([^)]+\))?\s*\1\s*\)| - \s*\{\s*$regexp\s*\((?:\s*\([^)]+\))?\s*\1\s*\)\s*;\s*\}))/sxg) + /\b(if\s*\(\s*([^)]+?)(?:\s*!=\s*([^)]+?))?\s*\) + # 1 2 3 + (?: \s*$regexp\s*\((?:\s*\([^)]+\))?\s*([^)]+)\)| + \s*\{\s*$regexp\s*\((?:\s*\([^)]+\))?\s*([^)]+)\)\s*;\s*\}))/sxg) { - $found_match = 1; - $list - and (print "$file\0"), next FILE; - print "$file: $1\n"; + my $all = $1; + my ($lhs, $rhs) = ($2, $3); + my ($free_opnd, $braced_free_opnd) = ($4, $5); + my $non_NULL; + if (!defined $rhs) { $non_NULL = $lhs } + elsif (is_NULL $rhs) { $non_NULL = $lhs } + elsif (is_NULL $lhs) { $non_NULL = $rhs } + else { next } + + # Compare the non-NULL part of the "if" expression and the + # free'd expression, without regard to white space. + $non_NULL =~ tr/ \t//d; + my $e2 = defined $free_opnd ? $free_opnd : $braced_free_opnd; + $e2 =~ tr/ \t//d; + if ($non_NULL eq $e2) + { + $found_match = 1; + $list + and (print "$file\0"), next FILE; + print "$file: $all\n"; + } } } }