report-include-statistics 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/perl -w
  2. # Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. #
  8. # 1. Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. # its contributors may be used to endorse or promote products derived
  15. # from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. # "report-include-statistics" script for WebKit Open Source Project
  28. use strict;
  29. use File::Find;
  30. find(\&wanted, @ARGV ? @ARGV : ".");
  31. my %paths;
  32. my %sources;
  33. my %includes;
  34. sub wanted
  35. {
  36. my $file = $_;
  37. if ($file eq "icu") {
  38. $File::Find::prune = 1;
  39. return;
  40. }
  41. if ($file !~ /^\./ && $file =~ /\.(h|cpp|c|mm|m)$/) {
  42. $paths{$file} = $File::Find::name;
  43. $sources{$file} = $File::Find::name if $file !~ /\.h/;
  44. open FILE, $file or die;
  45. while (<FILE>) {
  46. if (m-^\s*#\s*(include|import)\s+["<]((\S+/)*)(\S+)[">]-) {
  47. my $include = ($2 eq "sys/" ? $2 : "") . $4;
  48. $includes{$file}{$include}++;
  49. }
  50. }
  51. close FILE;
  52. }
  53. }
  54. my %totalIncludes;
  55. sub fillOut
  56. {
  57. my ($file) = @_;
  58. return if defined $totalIncludes{$file};
  59. for my $include (keys %{ $includes{$file} }) {
  60. $totalIncludes{$file}{$include} = 1;
  61. fillOut($include);
  62. for my $i (keys %{ $totalIncludes{$include} }) {
  63. $totalIncludes{$file}{$i} = 1;
  64. }
  65. }
  66. }
  67. my %inclusionCounts;
  68. for my $file (keys %includes) {
  69. $inclusionCounts{$file} = 0;
  70. fillOut($file);
  71. }
  72. for my $file (keys %sources) {
  73. for my $include (keys %{ $totalIncludes{$file} }) {
  74. $inclusionCounts{$include}++;
  75. }
  76. }
  77. for my $file (sort mostincludedcmp keys %includes) {
  78. next if !$paths{$file};
  79. my $count = $inclusionCounts{$file};
  80. my $numIncludes = keys %{ $includes{$file} };
  81. my $numTotalIncludes = keys %{ $totalIncludes{$file} };
  82. print "$file is included $count times, includes $numIncludes files directly, $numTotalIncludes files total.\n"
  83. }
  84. # Sort most-included files first.
  85. sub mostincludedcmp($$)
  86. {
  87. my ($filea, $fileb) = @_;
  88. my $counta = $inclusionCounts{$filea} || 0;
  89. my $countb = $inclusionCounts{$fileb} || 0;
  90. return $countb <=> $counta if $counta != $countb;
  91. my $ta = keys %{ $totalIncludes{$filea} };
  92. my $tb = keys %{ $totalIncludes{$fileb} };
  93. return $ta <=> $tb if $ta != $tb;
  94. return $filea cmp $fileb;
  95. }