]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - scripts/checkincludes.pl
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-bionic-kernel.git] / scripts / checkincludes.pl
CommitLineData
1da177e4
LT
1#!/usr/bin/perl
2#
92f3f19c
LR
3# checkincludes: find/remove files included more than once
4#
1da177e4 5# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
92f3f19c
LR
6# 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.
1da177e4 13
3da27157
SH
14use strict;
15
f9d490ab 16sub usage {
92f3f19c
LR
17 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";
f9d490ab
LR
20 exit 1;
21}
22
92f3f19c
LR
23my $remove = 0;
24
f9d490ab 25if ($#ARGV < 0) {
92f3f19c
LR
26 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 }
f9d490ab
LR
38}
39
3da27157
SH
40foreach my $file (@ARGV) {
41 open(my $f, '<', $file)
42 or die "Cannot open $file: $!.\n";
1da177e4
LT
43
44 my %includedfiles = ();
92f3f19c 45 my @file_lines = ();
1da177e4 46
3da27157 47 while (<$f>) {
1da177e4
LT
48 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
49 ++$includedfiles{$1};
50 }
92f3f19c 51 push(@file_lines, $_);
1da177e4 52 }
d9a7a2bd 53
3da27157 54 close($f);
92f3f19c
LR
55
56 if (!$remove) {
3da27157 57 foreach my $filename (keys %includedfiles) {
92f3f19c
LR
58 if ($includedfiles{$filename} > 1) {
59 print "$file: $filename is included more than once.\n";
60 }
1da177e4 61 }
92f3f19c 62 next;
1da177e4 63 }
92f3f19c 64
3da27157
SH
65 open($f, '>', $file)
66 or die("Cannot write to $file: $!");
92f3f19c
LR
67
68 my $dups = 0;
69 foreach (@file_lines) {
70 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
3da27157 71 foreach my $filename (keys %includedfiles) {
92f3f19c
LR
72 if ($1 eq $filename) {
73 if ($includedfiles{$filename} > 1) {
74 $includedfiles{$filename}--;
75 $dups++;
76 } else {
3da27157 77 print {$f} $_;
92f3f19c
LR
78 }
79 }
80 }
81 } else {
3da27157 82 print {$f} $_;
92f3f19c
LR
83 }
84 }
85 if ($dups > 0) {
86 print "$file: removed $dups duplicate includes\n";
87 }
3da27157 88 close($f);
1da177e4 89}