Skip to contentSkip to conversation

checkincludes.pl: add removal option

PATCH In review Luis R. Rodriguez 3 participants 7 messages Aug 6, 2009

lore
Share & tools
lore
#0 lore raw

cover checkincludes.pl: add removal option

This lets you remove duplicate includes headers in place with checkincludes.pl, that is, the same file is overwritten with -r. This actually makes this useful for me for porting crap drivers where I have to do this a lot.

I don't know who these go through so sending to a few of you.

Luis R. Rodriguez (3):
checkincludes.pl: close file as soon as we're done with it checkincludes.pl: provide usage helper checkincludes.pl: add option to remove duplicates in place

 scripts/checkincludes.pl |   62 +++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 58 insertions(+), 4 deletions(-)
#1 lore raw

PATCH 1/3 checkincludes.pl: close file as soon as we're done with it

---
 scripts/checkincludes.pl |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
scripts/checkincludes.pl +2 -2
index 8e6b716..32ebff6 100755
@@ -13,12 +13,12 @@ foreach $file (@ARGV) {
 			++$includedfiles{$1};
 		}
 	}
+
+	close(FILE);
 	
 	foreach $filename (keys %includedfiles) {
 		if ($includedfiles{$filename} > 1) {
 			print "$file: $filename is included more than once.\n";
 		}
 	}
-
-	close(FILE);
 }
Signature · 1 line
1.6.3.3
#2 lore raw

PATCH 2/3 checkincludes.pl: provide usage helper

In reply to Luis R. Rodriguez
---
 scripts/checkincludes.pl |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
scripts/checkincludes.pl +9 -0
index 32ebff6..4bff139 100755
@@ -3,6 +3,15 @@
 # checkincludes: Find files included more than once in (other) files.
 # Copyright abandoned, 2000, Niels Kristian Bech Jensen <[email protected]>.
 
+sub usage {
+	print "Usage: checkincludes.pl <file list>\n";
+	exit 1;
+}
+
+if ($#ARGV < 0) {
+        usage();
+}
+
 foreach $file (@ARGV) {
 	open(FILE, $file) or die "Cannot open $file: $!.\n";
 
Signature · 1 line
1.6.3.3
#3 lore raw

PATCH 3/3 checkincludes.pl: add option to remove duplicates in place

In reply to Luis R. Rodriguez

checkincludes.pl is more useful if it actually removed the lines. This adds support for that with -r.

---
 scripts/checkincludes.pl |   57 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 51 insertions(+), 6 deletions(-)
scripts/checkincludes.pl +51 -6
index 4bff139..5a53407 100755
@@ -2,32 +2,77 @@
 #
 # checkincludes: Find files included more than once in (other) files.
 # Copyright abandoned, 2000, Niels Kristian Bech Jensen <[email protected]>.
+# Copyright abandoned, 2009, Luis R. Rodriguez <[email protected]>
 
 sub usage {
-	print "Usage: checkincludes.pl <file list>\n";
+	print "Usage: checkincludes.pl [-r]\n";
+	print "By default we just warn of duplicates\n";
+	print "To remove files in place use -r\n";
 	exit 1;
 }
 
+my $remove = 0;
+
 if ($#ARGV < 0) {
-        usage();
+	usage();
+}
+
+if ($#ARGV >= 1) {
+	if ($ARGV[0] =~ /^-/) {
+		if ($ARGV[0] eq "-r") {
+			$remove = 1;
+			shift;
+		} else {
+			usage();
+		}
+	}
 }
 
 foreach $file (@ARGV) {
 	open(FILE, $file) or die "Cannot open $file: $!.\n";
 
 	my %includedfiles = ();
+	my @file_lines = ();
 
 	while (<FILE>) {
 		if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
 			++$includedfiles{$1};
 		}
+		push(@file_lines, $_);
 	}
 
 	close(FILE);
-	
-	foreach $filename (keys %includedfiles) {
-		if ($includedfiles{$filename} > 1) {
-			print "$file: $filename is included more than once.\n";
+
+	if (!$remove) {
+		foreach $filename (keys %includedfiles) {
+			if ($includedfiles{$filename} > 1) {
+				print "$file: $filename is included more than once.\n";
+			}
 		}
+		next;
 	}
+
+	open(FILE,">$file") || die("Cannot write to $file: $!");
+
+	my $dups = 0;
+	foreach (@file_lines) {
+		if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
+			foreach $filename (keys %includedfiles) {
+				if ($1 eq $filename) {
+					if ($includedfiles{$filename} > 1) {
+						$includedfiles{$filename}--;
+						$dups++;
+					} else {
+						print FILE $_;
+					}
+				}
+			}
+		} else {
+			print FILE $_;
+		}
+	}
+	if ($dups > 0) {
+		print "$file: removed $dups duplicate includes\n";
+	}
+	close(FILE);
 }
Signature · 1 line
1.6.3.3
#4 lore raw

Re: [PATCH 3/3] checkincludes.pl: add option to remove duplicates in place

Quoted 3 lines · On Wed, 5 Aug 2009 17:51:12 -0700 "Luis R. Rodriguez" <[email protected]>…

checkincludes.pl is more useful if it actually removed the lines. This adds support for that with -r.

hm, spose so. It can't do any harm.

Plus it's about time we edited that file - it hasn't been changed since we started the bk tree.

Quoted 9 lines
---
 scripts/checkincludes.pl |   57 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 51 insertions(+), 6 deletions(-)
scripts/checkincludes.pl +0 -0
index 4bff139..5a53407 100755

scripts/checkincludes.pl line 10

Quoted 10 lines
scripts/checkincludes.pl +4 -1
@@ -2,32 +2,77 @@
 #
 # checkincludes: Find files included more than once in (other) files.
 # Copyright abandoned, 2000, Niels Kristian Bech Jensen <[email protected]>.
+# Copyright abandoned, 2009, Luis R. Rodriguez <[email protected]>
 
 sub usage {
-	print "Usage: checkincludes.pl <file list>\n";
+	print "Usage: checkincludes.pl [-r]\n";
+	print "By default we just warn of duplicates\n";
+	print "To remove files in place use -r\n";

I'm not sure I like the wording here - I don't _want_ my files removed!

This?

scripts/checkincludes.pl renamed from scripts/checkincludes.pl~checkincludespl-add-option-to-remove-duplicates-in-place-fix +1 -1
@@ -7,7 +7,7 @@
 sub usage {
 	print "Usage: checkincludes.pl [-r]\n";
 	print "By default we just warn of duplicates\n";
-	print "To remove files in place use -r\n";
+	print "To remove duplicated includes in place use -r\n";
 	exit 1;
 }
 

_

#5 lore raw

Re: [PATCH 3/3] checkincludes.pl: add option to remove duplicates in place

Quoted 10 lines · Andrew Morton
Quoted 3 lines · On Wed, 5 Aug 2009 17:51:12 -0700 "Luis R. Rodriguez" <[email protected]>…

checkincludes.pl is more useful if it actually removed the lines. This adds support for that with -r.

hm, spose so. It can't do any harm.

Plus it's about time we edited that file - it hasn't been changed since we started the bk tree.

Hehe yeah.. Anno Domini

Quoted 27 lines
Quoted 9 lines
---
 scripts/checkincludes.pl |   57 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 51 insertions(+), 6 deletions(-)
scripts/checkincludes.pl +0 -0
index 4bff139..5a53407 100755

scripts/checkincludes.pl line 10

Quoted 10 lines
scripts/checkincludes.pl +4 -1
@@ -2,32 +2,77 @@
 #
 # checkincludes: Find files included more than once in (other) files.
 # Copyright abandoned, 2000, Niels Kristian Bech Jensen <[email protected]>.
+# Copyright abandoned, 2009, Luis R. Rodriguez <[email protected]>
 
 sub usage {
-     print "Usage: checkincludes.pl <file list>\n";
+     print "Usage: checkincludes.pl [-r]\n";
+     print "By default we just warn of duplicates\n";
+     print "To remove files in place use -r\n";

I'm not sure I like the wording here - I don't _want_ my files removed!

This?

scripts/checkincludes.pl line 12

Quoted 7 lines
scripts/checkincludes.pl renamed from scripts/checkincludes.pl~checkincludespl-add-option-to-remove-duplicates-in-place-fix +1 -1
@@ -7,7 +7,7 @@
 sub usage {
        print "Usage: checkincludes.pl [-r]\n";
        print "By default we just warn of duplicates\n";
-       print "To remove files in place use -r\n";
+       print "To remove duplicated includes in place use -r\n";
        exit 1;
 }

Sure, attached new v2

From: Luis R. Rodriguez <[email protected]>
Subject: [PATCH v2] checkincludes.pl: add option to remove duplicates in place

checkincludes.pl is more useful if it actually removed the lines. This adds support for that with -r.

---

Now with Andrew's change in place and some sort of documentation added.

 scripts/checkincludes.pl |   66 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 59 insertions(+), 7 deletions(-)
scripts/checkincludes.pl +59 -7
index 4bff139..676ddc0 100755
@@ -1,33 +1,85 @@
 #!/usr/bin/perl
 #
-# checkincludes: Find files included more than once in (other) files.
+# checkincludes: find/remove files included more than once
+#
 # Copyright abandoned, 2000, Niels Kristian Bech Jensen <[email protected]>.
+# Copyright 2009 Luis R. Rodriguez <[email protected]>
+#
+# This script checks for duplicate includes. It also has support
+# to remove them in place. Note that this will not take into
+# consideration macros so you should run this only if you know
+# you do have real dups and do not have them under #ifdef's. You
+# could also just review the results.
 
 sub usage {
-	print "Usage: checkincludes.pl <file list>\n";
+	print "Usage: checkincludes.pl [-r]\n";
+	print "By default we just warn of duplicates\n";
+	print "To remove duplicated includes in place use -r\n";
 	exit 1;
 }
 
+my $remove = 0;
+
 if ($#ARGV < 0) {
-        usage();
+	usage();
+}
+
+if ($#ARGV >= 1) {
+	if ($ARGV[0] =~ /^-/) {
+		if ($ARGV[0] eq "-r") {
+			$remove = 1;
+			shift;
+		} else {
+			usage();
+		}
+	}
 }
 
 foreach $file (@ARGV) {
 	open(FILE, $file) or die "Cannot open $file: $!.\n";
 
 	my %includedfiles = ();
+	my @file_lines = ();
 
 	while (<FILE>) {
 		if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
 			++$includedfiles{$1};
 		}
+		push(@file_lines, $_);
 	}
 
 	close(FILE);
-	
-	foreach $filename (keys %includedfiles) {
-		if ($includedfiles{$filename} > 1) {
-			print "$file: $filename is included more than once.\n";
+
+	if (!$remove) {
+		foreach $filename (keys %includedfiles) {
+			if ($includedfiles{$filename} > 1) {
+				print "$file: $filename is included more than once.\n";
+			}
 		}
+		next;
 	}
+
+	open(FILE,">$file") || die("Cannot write to $file: $!");
+
+	my $dups = 0;
+	foreach (@file_lines) {
+		if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
+			foreach $filename (keys %includedfiles) {
+				if ($1 eq $filename) {
+					if ($includedfiles{$filename} > 1) {
+						$includedfiles{$filename}--;
+						$dups++;
+					} else {
+						print FILE $_;
+					}
+				}
+			}
+		} else {
+			print FILE $_;
+		}
+	}
+	if ($dups > 0) {
+		print "$file: removed $dups duplicate includes\n";
+	}
+	close(FILE);
 }
Signature · 1 line
1.6.3.3
#6 lore raw

Re: [PATCH 3/3] checkincludes.pl: add option to remove duplicates in place

In reply to Andrew Morton
Quoted 13 lines · Andrew Morton

Review

Quoted 4 lines · On Wed, 5 Aug 2009 17:51:12 -0700 "Luis R. Rodriguez" <[email protected]>…
(quoted diff) +3 -1
-	print "Usage: checkincludes.pl <file list>\n";
+	print "Usage: checkincludes.pl [-r]\n";
+	print "By default we just warn of duplicates\n";
+	print "To remove files in place use -r\n";

I'm not sure I like the wording here - I don't _want_ my files removed!

This?

scripts/checkincludes.pl line 13

Quoted 8 lines
scripts/checkincludes.pl renamed from scripts/checkincludes.pl~checkincludespl-add-option-to-remove-duplicates-in-place-fix +1 -1
@@ -7,7 +7,7 @@
 sub usage {
 	print "Usage: checkincludes.pl [-r]\n";
 	print "By default we just warn of duplicates\n";
-	print "To remove files in place use -r\n";
+	print "To remove duplicated includes in place use -r\n";
 	exit 1;
 }
 

And perhaps the '<file list>' should be kept as well, for documentation?

scripts/checkincludes.pl +0 -0
@@ -5,7 +5,7 @@

# Copyright abandoned, 2009, Luis R. Rodriguez <[email protected]>

sub usage {
- print "Usage: checkincludes.pl [-r]\n";

+	print "Usage: checkincludes.pl [-r] <file list>\n";
	print "By default we just warn of duplicates\n";
	print "To remove duplicated includes in place use -r\n";
	exit 1;

Daniel K.