18c2ecf20Sopenharmony_ci#!/usr/bin/env perl 28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0 38c2ecf20Sopenharmony_ci# 48c2ecf20Sopenharmony_ci# checkincludes: find/remove files included more than once 58c2ecf20Sopenharmony_ci# 68c2ecf20Sopenharmony_ci# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>. 78c2ecf20Sopenharmony_ci# Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com> 88c2ecf20Sopenharmony_ci# 98c2ecf20Sopenharmony_ci# This script checks for duplicate includes. It also has support 108c2ecf20Sopenharmony_ci# to remove them in place. Note that this will not take into 118c2ecf20Sopenharmony_ci# consideration macros so you should run this only if you know 128c2ecf20Sopenharmony_ci# you do have real dups and do not have them under #ifdef's. You 138c2ecf20Sopenharmony_ci# could also just review the results. 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ciuse strict; 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cisub usage { 188c2ecf20Sopenharmony_ci print "Usage: checkincludes.pl [-r]\n"; 198c2ecf20Sopenharmony_ci print "By default we just warn of duplicates\n"; 208c2ecf20Sopenharmony_ci print "To remove duplicated includes in place use -r\n"; 218c2ecf20Sopenharmony_ci exit 1; 228c2ecf20Sopenharmony_ci} 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cimy $remove = 0; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciif ($#ARGV < 0) { 278c2ecf20Sopenharmony_ci usage(); 288c2ecf20Sopenharmony_ci} 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciif ($#ARGV >= 1) { 318c2ecf20Sopenharmony_ci if ($ARGV[0] =~ /^-/) { 328c2ecf20Sopenharmony_ci if ($ARGV[0] eq "-r") { 338c2ecf20Sopenharmony_ci $remove = 1; 348c2ecf20Sopenharmony_ci shift; 358c2ecf20Sopenharmony_ci } else { 368c2ecf20Sopenharmony_ci usage(); 378c2ecf20Sopenharmony_ci } 388c2ecf20Sopenharmony_ci } 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cimy $dup_counter = 0; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ciforeach my $file (@ARGV) { 448c2ecf20Sopenharmony_ci open(my $f, '<', $file) 458c2ecf20Sopenharmony_ci or die "Cannot open $file: $!.\n"; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci my %includedfiles = (); 488c2ecf20Sopenharmony_ci my @file_lines = (); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci while (<$f>) { 518c2ecf20Sopenharmony_ci if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { 528c2ecf20Sopenharmony_ci ++$includedfiles{$1}; 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci push(@file_lines, $_); 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci close($f); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci if (!$remove) { 608c2ecf20Sopenharmony_ci foreach my $filename (keys %includedfiles) { 618c2ecf20Sopenharmony_ci if ($includedfiles{$filename} > 1) { 628c2ecf20Sopenharmony_ci print "$file: $filename is included more than once.\n"; 638c2ecf20Sopenharmony_ci ++$dup_counter; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci } 668c2ecf20Sopenharmony_ci next; 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci open($f, '>', $file) 708c2ecf20Sopenharmony_ci or die("Cannot write to $file: $!"); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci my $dups = 0; 738c2ecf20Sopenharmony_ci foreach (@file_lines) { 748c2ecf20Sopenharmony_ci if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) { 758c2ecf20Sopenharmony_ci foreach my $filename (keys %includedfiles) { 768c2ecf20Sopenharmony_ci if ($1 eq $filename) { 778c2ecf20Sopenharmony_ci if ($includedfiles{$filename} > 1) { 788c2ecf20Sopenharmony_ci $includedfiles{$filename}--; 798c2ecf20Sopenharmony_ci $dups++; 808c2ecf20Sopenharmony_ci ++$dup_counter; 818c2ecf20Sopenharmony_ci } else { 828c2ecf20Sopenharmony_ci print {$f} $_; 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci } 858c2ecf20Sopenharmony_ci } 868c2ecf20Sopenharmony_ci } else { 878c2ecf20Sopenharmony_ci print {$f} $_; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci } 908c2ecf20Sopenharmony_ci if ($dups > 0) { 918c2ecf20Sopenharmony_ci print "$file: removed $dups duplicate includes\n"; 928c2ecf20Sopenharmony_ci } 938c2ecf20Sopenharmony_ci close($f); 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ciif ($dup_counter == 0) { 978c2ecf20Sopenharmony_ci print "No duplicate includes found.\n"; 988c2ecf20Sopenharmony_ci} 99