changeset 9689:827726e8abb1

useless-if-before-free: New option: --list (-l).
author Jim Meyering <meyering@redhat.com>
date Sun, 10 Feb 2008 23:18:09 +0100
parents 7b54a95e20c1
children 521de8c5a745
files ChangeLog build-aux/useless-if-before-free
diffstat 2 files changed, 17 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2008-02-10  Jim Meyering  <meyering@redhat.com>
 
+	useless-if-before-free: New option: --list (-l).
+
 	useless-if-before-free: Don't exit immediately upon open failure.
 	* build-aux/useless-if-before-free: Exit 2 for errors.
 	Upon failure to open a file, don't exit immediately.
--- a/build-aux/useless-if-before-free
+++ b/build-aux/useless-if-before-free
@@ -2,7 +2,7 @@
 # Detect instances of "if (p) free (p);".
 # Likewise for "if (p != NULL) free (p);".  And with braces.
 
-my $VERSION = '2008-02-10 19:36'; # UTC
+my $VERSION = '2008-02-10 22:17'; # 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
@@ -51,6 +51,7 @@
 
 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;
                   may be repeated
 
@@ -81,11 +82,13 @@
   sub EXIT_ERROR {2}
   my $err = EXIT_NO_MATCH;
 
+  my $list;
   my @name = qw(free);
   GetOptions
     (
      help => sub { usage 0 },
      version => sub { print "$ME version $VERSION\n"; exit },
+     list => \$list,
      'name=s@' => \@name,
     ) or usage 1;
 
@@ -102,6 +105,7 @@
   $/ = '"';
 
   my $found_match = 0;
+ FILE:
   foreach my $file (@ARGV)
     {
       open FH, '<', $file
@@ -114,10 +118,15 @@
                (?:   \s*$regexp\s*\(\s*\2\s*\)|
                 \s*\{\s*$regexp\s*\(\s*\2\s*\)\s*;\s*\}))/sx)
             {
+              $found_match = 1;
+              $list
+                and (print "$file\0"), next FILE;
               print "$file: $1\n";
-              $found_match = 1;
             }
         }
+    }
+  continue
+    {
       close FH;
     }
 
@@ -135,6 +144,10 @@
 git ls-files -z |xargs -0 \
 perl -0x3b -pi -e 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(k?free\s*\(\s*\1\s*\))/$2/s'
 
+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*\1\s*\))/$2/s'
+
 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.