blob: ce9edefd6e0be32ca34b26cb47f61d35a3a2fa9d [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/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
Cheah Kok Cheong8087a562017-02-22 15:40:26 -080040my $dup_counter = 0;
41
Stephen Hemminger3da27152010-02-22 15:17:12 -080042foreach my $file (@ARGV) {
43 open(my $f, '<', $file)
44 or die "Cannot open $file: $!.\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 my %includedfiles = ();
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070047 my @file_lines = ();
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Stephen Hemminger3da27152010-02-22 15:17:12 -080049 while (<$f>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
51 ++$includedfiles{$1};
52 }
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070053 push(@file_lines, $_);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
Luis R. Rodriguezd9a7a2b2009-09-18 12:49:25 -070055
Stephen Hemminger3da27152010-02-22 15:17:12 -080056 close($f);
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070057
58 if (!$remove) {
Stephen Hemminger3da27152010-02-22 15:17:12 -080059 foreach my $filename (keys %includedfiles) {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070060 if ($includedfiles{$filename} > 1) {
61 print "$file: $filename is included more than once.\n";
Cheah Kok Cheong8087a562017-02-22 15:40:26 -080062 ++$dup_counter;
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070063 }
64 }
65 next;
66 }
67
Stephen Hemminger3da27152010-02-22 15:17:12 -080068 open($f, '>', $file)
69 or die("Cannot write to $file: $!");
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070070
71 my $dups = 0;
72 foreach (@file_lines) {
73 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
Stephen Hemminger3da27152010-02-22 15:17:12 -080074 foreach my $filename (keys %includedfiles) {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070075 if ($1 eq $filename) {
76 if ($includedfiles{$filename} > 1) {
77 $includedfiles{$filename}--;
78 $dups++;
Cheah Kok Cheong8087a562017-02-22 15:40:26 -080079 ++$dup_counter;
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070080 } else {
Stephen Hemminger3da27152010-02-22 15:17:12 -080081 print {$f} $_;
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070082 }
83 }
84 }
85 } else {
Stephen Hemminger3da27152010-02-22 15:17:12 -080086 print {$f} $_;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 }
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070089 if ($dups > 0) {
90 print "$file: removed $dups duplicate includes\n";
91 }
Stephen Hemminger3da27152010-02-22 15:17:12 -080092 close($f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
Cheah Kok Cheong8087a562017-02-22 15:40:26 -080094
95if ($dup_counter == 0) {
96 print "No duplicate includes found.\n";
97}