| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #!/usr/bin/perl | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | # checkincludes: find/remove files included more than once | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | # Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>. | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | # Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # 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. | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | use strict; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:26 -07:00
										 |  |  | sub usage { | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 	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"; | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:26 -07:00
										 |  |  | 	exit 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | my $remove = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:26 -07:00
										 |  |  | if ($#ARGV < 0) { | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 	usage(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ($#ARGV >= 1) { | 
					
						
							|  |  |  | 	if ($ARGV[0] =~ /^-/) { | 
					
						
							|  |  |  | 		if ($ARGV[0] eq "-r") { | 
					
						
							|  |  |  | 			$remove = 1; | 
					
						
							|  |  |  | 			shift; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			usage(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:26 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | foreach my $file (@ARGV) { | 
					
						
							|  |  |  | 	open(my $f, '<', $file) | 
					
						
							|  |  |  | 	    or die "Cannot open $file: $!.\n"; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	my %includedfiles = (); | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 	my @file_lines = (); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | 	while (<$f>) { | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { | 
					
						
							|  |  |  | 			++$includedfiles{$1}; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 		push(@file_lines, $_); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | 	close($f); | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!$remove) { | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | 		foreach my $filename (keys %includedfiles) { | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 			if ($includedfiles{$filename} > 1) { | 
					
						
							|  |  |  | 				print "$file: $filename is included more than once.\n"; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 		next; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | 	open($f, '>', $file) | 
					
						
							|  |  |  | 	    or die("Cannot write to $file: $!"); | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	my $dups = 0; | 
					
						
							|  |  |  | 	foreach (@file_lines) { | 
					
						
							|  |  |  | 		if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | 			foreach my $filename (keys %includedfiles) { | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 				if ($1 eq $filename) { | 
					
						
							|  |  |  | 					if ($includedfiles{$filename} > 1) { | 
					
						
							|  |  |  | 						$includedfiles{$filename}--; | 
					
						
							|  |  |  | 						$dups++; | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | 						print {$f} $_; | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | 			print {$f} $_; | 
					
						
							| 
									
										
										
										
											2009-09-18 12:49:27 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ($dups > 0) { | 
					
						
							|  |  |  | 		print "$file: removed $dups duplicate includes\n"; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-02-22 15:17:12 -08:00
										 |  |  | 	close($f); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } |