2 # SPDX-License-Identifier: GPL-2.0
4 # checkincludes: find/remove files included more than once
6 # Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
7 # Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com>
9 # This script checks for duplicate includes. It also has support
10 # to remove them in place. Note that this will not take into
11 # consideration macros so you should run this only if you know
12 # you do have real dups and do not have them under #ifdef's. You
13 # could also just review the results.
18 print "Usage: checkincludes.pl [-r]\n";
19 print "By default we just warn of duplicates\n";
20 print "To remove duplicated includes in place use -r\n";
31 if ($ARGV[0] =~ /^-/) {
32 if ($ARGV[0] eq "-r") {
43 foreach my $file (@ARGV) {
44 open(my $f, '<', $file)
45 or die "Cannot open $file: $!.\n";
47 my %includedfiles = ();
51 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
54 push(@file_lines, $_);
60 foreach my $filename (keys %includedfiles) {
61 if ($includedfiles{$filename} > 1) {
62 print "$file: $filename is included more than once.\n";
70 or die("Cannot write to $file: $!");
73 foreach (@file_lines) {
74 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
75 foreach my $filename (keys %includedfiles) {
76 if ($1 eq $filename) {
77 if ($includedfiles{$filename} > 1) {
78 $includedfiles{$filename}--;
91 print "$file: removed $dups duplicate includes\n";
96 if ($dup_counter == 0) {
97 print "No duplicate includes found.\n";