useless-if-before-free: New option: --list (-l).
authorJim Meyering <meyering@redhat.com>
Sun, 10 Feb 2008 22:18:09 +0000 (23:18 +0100)
committerJim Meyering <meyering@redhat.com>
Sun, 10 Feb 2008 22:18:09 +0000 (23:18 +0100)
ChangeLog
build-aux/useless-if-before-free

index 1eb10be9a5da14be28a0c5c716e05e0c78ae45ed..93fc2ce11b43831ca70534aeb71e5c7d2e59c3af 100644 (file)
--- 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.
index 1cd5bc35dfcd1717e627d2af83d782320c4c64d3..1cf4cb03f643859ab6f18274185b91abb47c788a 100755 (executable)
@@ -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 @@ 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;
                   may be repeated
 
@@ -81,11 +82,13 @@ EOF
   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 @@ EOF
   $/ = '"';
 
   my $found_match = 0;
+ FILE:
   foreach my $file (@ARGV)
     {
       open FH, '<', $file
@@ -114,10 +118,15 @@ EOF
                (?:   \s*$regexp\s*\(\s*\2\s*\)|
                 \s*\{\s*$regexp\s*\(\s*\2\s*\)\s*;\s*\}))/sx)
             {
-              print "$file: $1\n";
               $found_match = 1;
+              $list
+                and (print "$file\0"), next FILE;
+              print "$file: $1\n";
             }
         }
+    }
+  continue
+    {
       close FH;
     }
 
@@ -135,6 +144,10 @@ my $foo = <<'EOF';
 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.