blob: b514a956d55012aec21d9ad38af1ddae03fc8c82 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -07004# checkincludes: find/remove files included more than once
5#
Linus Torvalds1da177e2005-04-16 15:20:36 -07006# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -07007# Copyright 2009 Luis R. Rodriguez <mcgrof@gmail.com>
8#
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Stephen Hemminger3da27152010-02-22 15:17:12 -080015use strict;
16
Luis R. Rodriguezf9d490a2009-09-18 12:49:26 -070017sub usage {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070018 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";
Luis R. Rodriguezf9d490a2009-09-18 12:49:26 -070021 exit 1;
22}
23
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070024my $remove = 0;
25
Luis R. Rodriguezf9d490a2009-09-18 12:49:26 -070026if ($#ARGV < 0) {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070027 usage();
28}
29
30if ($#ARGV >= 1) {
31 if ($ARGV[0] =~ /^-/) {
32 if ($ARGV[0] eq "-r") {
33 $remove = 1;
34 shift;
35 } else {
36 usage();
37 }
38 }
Luis R. Rodriguezf9d490a2009-09-18 12:49:26 -070039}
40
Cheah Kok Cheong8087a562017-02-22 15:40:26 -080041my $dup_counter = 0;
42
Stephen Hemminger3da27152010-02-22 15:17:12 -080043foreach my $file (@ARGV) {
44 open(my $f, '<', $file)
45 or die "Cannot open $file: $!.\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 my %includedfiles = ();
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070048 my @file_lines = ();
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stephen Hemminger3da27152010-02-22 15:17:12 -080050 while (<$f>) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
52 ++$includedfiles{$1};
53 }
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070054 push(@file_lines, $_);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
Luis R. Rodriguezd9a7a2b2009-09-18 12:49:25 -070056
Stephen Hemminger3da27152010-02-22 15:17:12 -080057 close($f);
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070058
59 if (!$remove) {
Stephen Hemminger3da27152010-02-22 15:17:12 -080060 foreach my $filename (keys %includedfiles) {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070061 if ($includedfiles{$filename} > 1) {
62 print "$file: $filename is included more than once.\n";
Cheah Kok Cheong8087a562017-02-22 15:40:26 -080063 ++$dup_counter;
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070064 }
65 }
66 next;
67 }
68
Stephen Hemminger3da27152010-02-22 15:17:12 -080069 open($f, '>', $file)
70 or die("Cannot write to $file: $!");
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070071
72 my $dups = 0;
73 foreach (@file_lines) {
74 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
Stephen Hemminger3da27152010-02-22 15:17:12 -080075 foreach my $filename (keys %includedfiles) {
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070076 if ($1 eq $filename) {
77 if ($includedfiles{$filename} > 1) {
78 $includedfiles{$filename}--;
79 $dups++;
Cheah Kok Cheong8087a562017-02-22 15:40:26 -080080 ++$dup_counter;
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070081 } else {
Stephen Hemminger3da27152010-02-22 15:17:12 -080082 print {$f} $_;
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070083 }
84 }
85 }
86 } else {
Stephen Hemminger3da27152010-02-22 15:17:12 -080087 print {$f} $_;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89 }
Luis R. Rodriguez92f3f192009-09-18 12:49:27 -070090 if ($dups > 0) {
91 print "$file: removed $dups duplicate includes\n";
92 }
Stephen Hemminger3da27152010-02-22 15:17:12 -080093 close($f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Cheah Kok Cheong8087a562017-02-22 15:40:26 -080095
96if ($dup_counter == 0) {
97 print "No duplicate includes found.\n";
98}