headerdep.pl 3.5 KB

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