blob: cfdfa02d4d923b04acf21c9614995e62bdafe769 [file] [log] [blame]
Finn Behrens2d1f7c92020-11-23 15:15:33 +01001#!/usr/bin/env perl
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -07003# checkincludes: find/remove files included more than once
4#
Linus Torvalds1da177e2005-04-16 15:20:36 -07005# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -07006# Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com>
7#
8# This script checks for duplicate includes. It also has support
9# to remove them in place. Note that this will not take into
10# consideration macros so you should run this only if you know
11# you do have real dups and do not have them under #ifdef's. You
12# could also just review the results.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Stephen Hemminger3da27152010-02-22 15:17:12 -080014use strict;
15
Luis R. Rodriguezf9d490a2009-09-18 12:49:26 -070016sub usage {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070017 print "Usage: checkincludes.pl [-r]\n";
18 print "By default we just warn of duplicates\n";
19 print "To remove duplicated includes in place use -r\n";
Luis R. Rodriguezf9d490a2009-09-18 12:49:26 -070020 exit 1;
21}
22
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070023my $remove = 0;
24
Luis R. Rodriguezf9d490a2009-09-18 12:49:26 -070025if ($#ARGV < 0) {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070026 usage();
27}
28
29if ($#ARGV >= 1) {
30 if ($ARGV[0] =~ /^-/) {
31 if ($ARGV[0] eq "-r") {
32 $remove = 1;
33 shift;
34 } else {
35 usage();
36 }
37 }
Luis R. Rodriguezf9d490a2009-09-18 12:49:26 -070038}
39
Stephen Hemminger3da27152010-02-22 15:17:12 -080040foreach my $file (@ARGV) {
41 open(my $f, '<', $file)
42 or die "Cannot open $file: $!.\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 my %includedfiles = ();
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070045 my @file_lines = ();
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Stephen Hemminger3da27152010-02-22 15:17:12 -080047 while (<$f>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
49 ++$includedfiles{$1};
50 }
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070051 push(@file_lines, $_);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
Luis R. Rodriguezd9a7a2b2009-09-18 12:49:25 -070053
Stephen Hemminger3da27152010-02-22 15:17:12 -080054 close($f);
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070055
56 if (!$remove) {
Stephen Hemminger3da27152010-02-22 15:17:12 -080057 foreach my $filename (keys %includedfiles) {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070058 if ($includedfiles{$filename} > 1) {
59 print "$file: $filename is included more than once.\n";
60 }
61 }
62 next;
63 }
64
Stephen Hemminger3da27152010-02-22 15:17:12 -080065 open($f, '>', $file)
66 or die("Cannot write to $file: $!");
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070067
68 my $dups = 0;
69 foreach (@file_lines) {
70 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
Stephen Hemminger3da27152010-02-22 15:17:12 -080071 foreach my $filename (keys %includedfiles) {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070072 if ($1 eq $filename) {
73 if ($includedfiles{$filename} > 1) {
74 $includedfiles{$filename}--;
75 $dups++;
76 } else {
Stephen Hemminger3da27152010-02-22 15:17:12 -080077 print {$f} $_;
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070078 }
79 }
80 }
81 } else {
Stephen Hemminger3da27152010-02-22 15:17:12 -080082 print {$f} $_;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84 }
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070085 if ($dups > 0) {
86 print "$file: removed $dups duplicate includes\n";
87 }
Stephen Hemminger3da27152010-02-22 15:17:12 -080088 close($f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}