The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/scripts/checkincludes.pl

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 #!/usr/bin/perl
    2 #
    3 # checkincludes: find/remove files included more than once
    4 #
    5 # Copyright abandoned, 2000, Niels Kristian Bech Jensen <nkbj@image.dk>.
    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.
   13 
   14 use strict;
   15 
   16 sub usage {
   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";
   20         exit 1;
   21 }
   22 
   23 my $remove = 0;
   24 
   25 if ($#ARGV < 0) {
   26         usage();
   27 }
   28 
   29 if ($#ARGV >= 1) {
   30         if ($ARGV[0] =~ /^-/) {
   31                 if ($ARGV[0] eq "-r") {
   32                         $remove = 1;
   33                         shift;
   34                 } else {
   35                         usage();
   36                 }
   37         }
   38 }
   39 
   40 foreach my $file (@ARGV) {
   41         open(my $f, '<', $file)
   42             or die "Cannot open $file: $!.\n";
   43 
   44         my %includedfiles = ();
   45         my @file_lines = ();
   46 
   47         while (<$f>) {
   48                 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
   49                         ++$includedfiles{$1};
   50                 }
   51                 push(@file_lines, $_);
   52         }
   53 
   54         close($f);
   55 
   56         if (!$remove) {
   57                 foreach my $filename (keys %includedfiles) {
   58                         if ($includedfiles{$filename} > 1) {
   59                                 print "$file: $filename is included more than once.\n";
   60                         }
   61                 }
   62                 next;
   63         }
   64 
   65         open($f, '>', $file)
   66             or die("Cannot write to $file: $!");
   67 
   68         my $dups = 0;
   69         foreach (@file_lines) {
   70                 if (m/^\s*#\s*include\s*[<"](\S*)[>"]/o) {
   71                         foreach my $filename (keys %includedfiles) {
   72                                 if ($1 eq $filename) {
   73                                         if ($includedfiles{$filename} > 1) {
   74                                                 $includedfiles{$filename}--;
   75                                                 $dups++;
   76                                         } else {
   77                                                 print {$f} $_;
   78                                         }
   79                                 }
   80                         }
   81                 } else {
   82                         print {$f} $_;
   83                 }
   84         }
   85         if ($dups > 0) {
   86                 print "$file: removed $dups duplicate includes\n";
   87         }
   88         close($f);
   89 }

Cache object: 5b649a2888b5b612db6691829150374c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.