Back to home page

LXR

 
 

    


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