useless-if-before-free: show how to remove braced useless free, too
authorJim Meyering <meyering@redhat.com>
Tue, 7 Apr 2009 08:38:35 +0000 (10:38 +0200)
committerJim Meyering <meyering@redhat.com>
Tue, 7 Apr 2009 09:37:12 +0000 (11:37 +0200)
* build-aux/useless-if-before-free: still only in a comment, though.

ChangeLog
build-aux/useless-if-before-free

index 069ae1f..e4c9bf6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2009-04-07  Jim Meyering  <meyering@redhat.com>
+
+       useless-if-before-free: show how to remove braced useless free, too
+       * build-aux/useless-if-before-free: still only in a comment, though.
+
 2009-04-07  Reuben Thomas  <rrt@sc3d.org>
 
        maint.mk: import changes to syntax-check macros from coreutils
 2009-04-07  Reuben Thomas  <rrt@sc3d.org>
 
        maint.mk: import changes to syntax-check macros from coreutils
index 696c621..29d4917 100755 (executable)
@@ -4,7 +4,7 @@
 # Also detect "if (NULL != p) free (p);".
 # And with 0 in place of NULL.
 
 # Also detect "if (NULL != p) free (p);".
 # And with 0 in place of NULL.
 
-my $VERSION = '2009-01-28 08:23'; # UTC
+my $VERSION = '2009-04-07 08:05'; # 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
 # 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
@@ -179,8 +179,14 @@ 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'
 
   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 '...'
+# 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*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.
 
 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.