X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=build-aux%2Fuseless-if-before-free;h=abcdef2cda289b36f696324883b2cd899978eac8;hb=ecda40afafa59d9503f80e0bf869220c218c73a8;hp=a2fc773c350b9e10f0de0457812312944cfdeed2;hpb=471cbb075f76e489fa96fad80bca65da7e01f683;p=gnulib.git diff --git a/build-aux/useless-if-before-free b/build-aux/useless-if-before-free index a2fc773c3..abcdef2cd 100755 --- a/build-aux/useless-if-before-free +++ b/build-aux/useless-if-before-free @@ -2,9 +2,9 @@ # 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 or "(void *)0" in place of NULL. +# And with 0 in place of NULL. -my $VERSION = '2009-01-28 08:16'; # UTC +my $VERSION = '2009-04-16 15:57'; # 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 @@ -89,9 +89,7 @@ EOF sub is_NULL ($) { my ($expr) = @_; - return ($expr eq 'NULL' - || $expr eq '0' - || $expr =~ /^\(\s*(char|void)\s*\*\s*\)\s*0$/); + return ($expr eq 'NULL' || $expr eq '0'); } { @@ -177,12 +175,20 @@ my $foo = <<'EOF'; # This adjusts them, removing the unnecessary "if (p)" part. # FIXME: do something like this as an option (doesn't do braces): -useless-if-before-free -l $(lid -knone free) | xargs -0 \ - perl -0x3b -pi -e \ - 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(free\s*\((?:\s*\([^)]+\))?\s*\1\s*\))/$2/s' - -# Or, with git: -git ls-files -z |xargs -0 perl -0x3b -pi -e '...' +free=xfree +git grep -l -z "$free *(" \ + | xargs -0 useless-if-before-free -l --name="$free" \ + | xargs -0 perl -0x3b -pi -e \ + 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*(?:0|NULL))?\s*\)\s+('"$free"'\s*\((?:\s*\([^)]+\))?\s*\1\s*\))/$2/s' + +# Use the following to remove redundant uses of kfree inside braces. +# Note that -0777 puts perl in slurp-whole-file mode; +# but we have plenty of memory, these days... +free=kfree +git grep -l -z "$free *(" \ + | xargs -0 useless-if-before-free -l --name="$free" \ + | xargs -0 perl -0777 -pi -e \ + 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*(?:0|NULL))?\s*\)\s*\{\s*('"$free"'\s*\((?:\s*\([^)]+\))?\s*\1\s*\);)\s*\}[^\n]*$/$2/gms' Be careful that the result of the above transformation is valid. If the matched string is followed by "else", then obviously, it won't be.