ublk_drv: add device parameter UBLK_PARAM_TYPE_DEVT
[linux-block.git] / scripts / checkincludes.pl
CommitLineData
cb77f0d6 1#!/usr/bin/env perl
b2441318 2# SPDX-License-Identifier: GPL-2.0
1da177e4 3#
92f3f19c
LR
4# checkincludes: find/remove files included more than once
5#
1da177e4 6# Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
92f3f19c
LR
7# 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.
1da177e4 14
3da27157
SH
15use strict;
16
f9d490ab 17sub usage {
92f3f19c
LR
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";
f9d490ab
LR
21 exit 1;
22}
23
92f3f19c
LR
24my $remove = 0;
25
f9d490ab 26if ($#ARGV < 0) {
92f3f19c
LR
27 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 }
f9d490ab
LR
39}
40
8087a560
CKC
41my $dup_counter = 0;
42
3da27157
SH
43foreach my $file (@ARGV) {
44 open(my $f, '<', $file)
45 or die "Cannot open $file: $!.\n";
1da177e4
LT
46
47 my %includedfiles = ();
92f3f19c 48 my @file_lines = ();
1da177e4 49
3da27157 50 while (<$f>) {
1da177e4
LT
51 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
52 ++$includedfiles{$1};
53 }
92f3f19c 54 push(@file_lines, $_);
1da177e4 55 }
d9a7a2bd 56
3da27157 57 close($f);
92f3f19c
LR
58
59 if (!$remove) {
3da27157 60 foreach my $filename (keys %includedfiles) {
92f3f19c
LR
61 if ($includedfiles{$filename} > 1) {
62 print "$file: $filename is included more than once.\n";
8087a560 63 ++$dup_counter;
92f3f19c 64 }
1da177e4 65 }
92f3f19c 66 next;
1da177e4 67 }
92f3f19c 68
3da27157
SH
69 open($f, '>', $file)
70 or die("Cannot write to $file: $!");
92f3f19c
LR
71
72 my $dups = 0;
73 foreach (@file_lines) {
74 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
3da27157 75 foreach my $filename (keys %includedfiles) {
92f3f19c
LR
76 if ($1 eq $filename) {
77 if ($includedfiles{$filename} > 1) {
78 $includedfiles{$filename}--;
79 $dups++;
8087a560 80 ++$dup_counter;
92f3f19c 81 } else {
3da27157 82 print {$f} $_;
92f3f19c
LR
83 }
84 }
85 }
86 } else {
3da27157 87 print {$f} $_;
92f3f19c
LR
88 }
89 }
90 if ($dups > 0) {
91 print "$file: removed $dups duplicate includes\n";
92 }
3da27157 93 close($f);
1da177e4 94}
8087a560
CKC
95
96if ($dup_counter == 0) {
97 print "No duplicate includes found.\n";
98}