0001
0002
0003
0004
0005
0006
0007
0008 use strict;
0009 use warnings;
0010
0011 use Getopt::Long;
0012
0013 my $opt_all;
0014 my @opt_include;
0015 my $opt_graph;
0016
0017 &Getopt::Long::Configure();
0018 &GetOptions(
0019 help => \&help,
0020 version => \&version,
0021
0022 all => \$opt_all,
0023 "I=s" => \@opt_include,
0024 graph => \$opt_graph,
0025 );
0026
0027 push @opt_include, 'include';
0028 my %deps = ();
0029 my %linenos = ();
0030
0031 my @headers = grep { strip($_) } @ARGV;
0032
0033 parse_all(@headers);
0034
0035 if($opt_graph) {
0036 graph();
0037 } else {
0038 detect_cycles(@headers);
0039 }
0040
0041
0042 sub help {
0043 print "Usage: $0 [options] file...\n";
0044 print "\n";
0045 print "Options:\n";
0046 print " --all\n";
0047 print " --graph\n";
0048 print "\n";
0049 print " -I includedir\n";
0050 print "\n";
0051 print "To make nice graphs, try:\n";
0052 print " $0 --graph include/linux/kernel.h | dot -Tpng -o graph.png\n";
0053 exit;
0054 }
0055
0056 sub version {
0057 print "headerdep version 2\n";
0058 exit;
0059 }
0060
0061
0062 sub strip {
0063 my $filename = shift;
0064
0065 for my $i (@opt_include) {
0066 my $stripped = $filename;
0067 $stripped =~ s/^$i\///;
0068
0069 return $stripped if $stripped ne $filename;
0070 }
0071
0072 return $filename;
0073 }
0074
0075
0076 sub search {
0077 my $filename = shift;
0078 return $filename if -f $filename;
0079
0080 for my $i (@opt_include) {
0081 my $path = "$i/$filename";
0082 return $path if -f $path;
0083 }
0084 return;
0085 }
0086
0087 sub parse_all {
0088
0089 my @queue = @_;
0090 while(@queue) {
0091 my $header = pop @queue;
0092 next if exists $deps{$header};
0093
0094 $deps{$header} = [] unless exists $deps{$header};
0095
0096 my $path = search($header);
0097 next unless $path;
0098
0099 open(my $file, '<', $path) or die($!);
0100 chomp(my @lines = <$file>);
0101 close($file);
0102
0103 for my $i (0 .. $#lines) {
0104 my $line = $lines[$i];
0105 if(my($dep) = ($line =~ )) {
0106 push @queue, $dep;
0107 push @{$deps{$header}}, [$i + 1, $dep];
0108 }
0109 }
0110 }
0111 }
0112
0113 sub print_cycle {
0114
0115
0116 my $cycle = shift;
0117
0118
0119 for my $i (0 .. $
0120 $cycle->[$i]->[0] = $cycle->[$i + 1]->[0];
0121 }
0122 $cycle->[-1]->[0] = 0;
0123
0124 my $first = shift @$cycle;
0125 my $last = pop @$cycle;
0126
0127 my $msg = "In file included";
0128 printf "%s from %s,\n", $msg, $last->[1] if defined $last;
0129
0130 for my $header (reverse @$cycle) {
0131 printf "%s from %s:%d%s\n",
0132 " " x length $msg,
0133 $header->[1], $header->[0],
0134 $header->[1] eq $last->[1] ? ' <-- here' : '';
0135 }
0136
0137 printf "%s:%d: warning: recursive header inclusion\n",
0138 $first->[1], $first->[0];
0139 }
0140
0141
0142 sub detect_cycles {
0143 my @queue = map { [[0, $_]] } @_;
0144 while(@queue) {
0145 my $top = pop @queue;
0146 my $name = $top->[-1]->[1];
0147
0148 for my $dep (@{$deps{$name}}) {
0149 my $chain = [@$top, [$dep->[0], $dep->[1]]];
0150
0151
0152
0153 if(grep { $_->[1] eq $dep->[1] } @$top) {
0154 print_cycle($chain);
0155 next if $opt_all;
0156 return;
0157 }
0158
0159 push @queue, $chain;
0160 }
0161 }
0162 }
0163
0164 sub mangle {
0165 $_ = shift;
0166 s/\//__/g;
0167 s/\./_/g;
0168 s/-/_/g;
0169 $_;
0170 }
0171
0172
0173 sub graph {
0174 print "digraph {\n";
0175
0176 print "\t/* vertices */\n";
0177 for my $header (keys %deps) {
0178 printf "\t%s [label=\"%s\"];\n",
0179 mangle($header), $header;
0180 }
0181
0182 print "\n";
0183
0184 print "\t/* edges */\n";
0185 for my $header (keys %deps) {
0186 for my $dep (@{$deps{$header}}) {
0187 printf "\t%s -> %s;\n",
0188 mangle($header), mangle($dep->[1]);
0189 }
0190 }
0191
0192 print "}\n";
0193 }