headerdep.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #! /usr/bin/perl
  2. #
  3. # Detect cycles in the header file dependency graph
  4. # Vegard Nossum <vegardno@ifi.uio.no>
  5. #
  6. use strict;
  7. use warnings;
  8. use Getopt::Long;
  9. my $opt_all;
  10. my @opt_include;
  11. my $opt_graph;
  12. &Getopt::Long::Configure(qw(bundling pass_through));
  13. &GetOptions(
  14. help => \&help,
  15. version => \&version,
  16. all => \$opt_all,
  17. "I=s" => \@opt_include,
  18. graph => \$opt_graph,
  19. );
  20. push @opt_include, 'include';
  21. my %deps = ();
  22. my %linenos = ();
  23. my @headers = grep { strip($_) } @ARGV;
  24. parse_all(@headers);
  25. if($opt_graph) {
  26. graph();
  27. } else {
  28. detect_cycles(@headers);
  29. }
  30. sub help {
  31. print "Usage: $0 [options] file...\n";
  32. print "\n";
  33. print "Options:\n";
  34. print " --all\n";
  35. print " --graph\n";
  36. print "\n";
  37. print " -I includedir\n";
  38. print "\n";
  39. print "To make nice graphs, try:\n";
  40. print " $0 --graph include/linux/kernel.h | dot -Tpng -o graph.png\n";
  41. exit;
  42. }
  43. sub version {
  44. print "headerdep version 2\n";
  45. exit;
  46. }
  47. # Get a file name that is relative to our include paths
  48. sub strip {
  49. my $filename = shift;
  50. for my $i (@opt_include) {
  51. my $stripped = $filename;
  52. $stripped =~ s/^$i\///;
  53. return $stripped if $stripped ne $filename;
  54. }
  55. return $filename;
  56. }
  57. # Search for the file name in the list of include paths
  58. sub search {
  59. my $filename = shift;
  60. return $filename if -f $filename;
  61. for my $i (@opt_include) {
  62. my $path = "$i/$filename";
  63. return $path if -f $path;
  64. }
  65. return;
  66. }
  67. sub parse_all {
  68. # Parse all the headers.
  69. my @queue = @_;
  70. while(@queue) {
  71. my $header = pop @queue;
  72. next if exists $deps{$header};
  73. $deps{$header} = [] unless exists $deps{$header};
  74. my $path = search($header);
  75. next unless $path;
  76. open(my $file, '<', $path) or die($!);
  77. chomp(my @lines = <$file>);
  78. close($file);
  79. for my $i (0 .. $#lines) {
  80. my $line = $lines[$i];
  81. if(my($dep) = ($line =~ m/^#\s*include\s*<(.*?)>/)) {
  82. push @queue, $dep;
  83. push @{$deps{$header}}, [$i + 1, $dep];
  84. }
  85. }
  86. }
  87. }
  88. sub print_cycle {
  89. # $cycle[n] includes $cycle[n + 1];
  90. # $cycle[-1] will be the culprit
  91. my $cycle = shift;
  92. # Adjust the line numbers
  93. for my $i (0 .. $#$cycle - 1) {
  94. $cycle->[$i]->[0] = $cycle->[$i + 1]->[0];
  95. }
  96. $cycle->[-1]->[0] = 0;
  97. my $first = shift @$cycle;
  98. my $last = pop @$cycle;
  99. my $msg = "In file included";
  100. printf "%s from %s,\n", $msg, $last->[1] if defined $last;
  101. for my $header (reverse @$cycle) {
  102. printf "%s from %s:%d%s\n",
  103. " " x length $msg,
  104. $header->[1], $header->[0],
  105. $header->[1] eq $last->[1] ? ' <-- here' : '';
  106. }
  107. printf "%s:%d: warning: recursive header inclusion\n",
  108. $first->[1], $first->[0];
  109. }
  110. # Find and print the smallest cycle starting in the specified node.
  111. sub detect_cycles {
  112. my @queue = map { [[0, $_]] } @_;
  113. while(@queue) {
  114. my $top = pop @queue;
  115. my $name = $top->[-1]->[1];
  116. for my $dep (@{$deps{$name}}) {
  117. my $chain = [@$top, [$dep->[0], $dep->[1]]];
  118. # If the dep already exists in the chain, we have a
  119. # cycle...
  120. if(grep { $_->[1] eq $dep->[1] } @$top) {
  121. print_cycle($chain);
  122. next if $opt_all;
  123. return;
  124. }
  125. push @queue, $chain;
  126. }
  127. }
  128. }
  129. sub mangle {
  130. $_ = shift;
  131. s/\//__/g;
  132. s/\./_/g;
  133. s/-/_/g;
  134. $_;
  135. }
  136. # Output dependency graph in GraphViz language.
  137. sub graph {
  138. print "digraph {\n";
  139. print "\t/* vertices */\n";
  140. for my $header (keys %deps) {
  141. printf "\t%s [label=\"%s\"];\n",
  142. mangle($header), $header;
  143. }
  144. print "\n";
  145. print "\t/* edges */\n";
  146. for my $header (keys %deps) {
  147. for my $dep (@{$deps{$header}}) {
  148. printf "\t%s -> %s;\n",
  149. mangle($header), mangle($dep->[1]);
  150. }
  151. }
  152. print "}\n";
  153. }