1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
#!/usr/bin/perl
use strict 'vars';
use File::Spec;
my $traceLevel = 3;
# whether to box the clusters by sub-folder, but always color nodes regardless
my @clusterlist = qw(
/xml
/export
/menus
/effects/VST
/effects/ladspa
/effects/lv2
/effects/nyquist
/effects/vamp
);
my %clusters;
@clusters{@clusterlist} = ();
sub clustering
{
return exists( $clusters{ $_[0] } );
}
# whether to prune redundant arcs implied in transitive closure
my $pruning = 1;
# whether to insert hyperlinks
my $links = 1;
# Step 1: collect short names and paths to .cpp files
# We assume that final path components uniquely identify the files!
my $dir = "../src";
my %names; # string to string
{
foreach my $file (`find $dir -name '*.cpp' -o -name '*.h' -o -name '*.mm'`) {
my $short = $file;
chop $short;
$short =~ s|\.cpp$||;
$short =~ s|\.h$||;
$short =~ s|\.mm$||;
my $shorter = ($short =~ s|^.*/||r);
$names{$shorter} = $short;
}
}
#my $linkroot = "https://github.com/audacity/audacity/tree/master/src";
my $linkroot = "file://" . File::Spec->rel2abs( $dir );
print STDERR "Found ", scalar( keys %names ), " filename(s)\n" if $traceLevel >= 1;
# Step 2: collect inclusions in each .cpp/.h pair, and folder information,
# and build a graph
my $arcs = 0;
my %graph; # hash from names to sets of names
my $grepcmd = "grep '^ *# *include[^\"]*\"[^\"]*\\.h\"'"; # find include directives with quotes
my $sedcmd = "sed -E 's|^[^\"]*\"([^\"]*)\\.h\".*\$|\\1|'"; # extract quoted path
my %folders; # build our own tree like the directories
my $nFolders = 1;
while( my ($shorter, $short) = each(%names) ) {
# find relevant files (.cpp and .h, and sometimes .mm too)
my $pat = "${short}.*";
my @files = glob $pat;
# store path information, for subgraph clustering later
$short = substr $short, length( $dir ) + 1;
my @ownComponents = split '/', $short;
my $last = pop @ownComponents;
my $folder = \%folders;
# this improves the graph in some ways:
# files that we just put directly under src should be treated as if in
# a separate subfolder.
@ownComponents = ("UNCLASSIFIED") if not @ownComponents;
# store paths in a hash from strings to references to hashes from strings to references to ...
# (ensuring a nonempty set at key "" for each node of this tree)
while (@ownComponents) {
my $component = shift @ownComponents;
if (not exists $$folder{ $component }) {
my %empty = ("",());
$$folder{ $component } = \%empty;
++$nFolders;
}
$folder = $$folder{ $component };
}
# at the last folder, hash empty string specially, to the set of files
if (not exists $$folder{ "" }) {
my %empty = ("",());
$$folder{ "" } = \%empty;
}
$$folder{""}{$last} = ();
my %empty;
$graph{$shorter} = \%empty; # be sure leaf nodes are not omitted from hash
foreach (`cat @files | $grepcmd | $sedcmd`) {
chop;
my @components = split '/';
my $include = $components[-1];
# omit self-arcs and arcs to .h files external to the project
if (($shorter ne $include) && (exists $names{$include})) {
$graph{$shorter}{$include} = (), ++$arcs;
}
}
}
print STDERR "Found ", scalar( keys %graph ), " node(s) and ${arcs} arc(s)\n" if $traceLevel >= 1;
# Step 3: compute an acyclic quotient graph
my %quotientMap; # from node name to reference to array of node names
sub SCCID {
# given reference to an array of names
# use the first name in the array as an ID
my $scc = shift;
return $$scc[0];
}
sub SCCLabel {
# given reference to an array of names
# use concatenation of names as the displayed label
my $scc = shift;
return join "\n", @$scc;
}
my %quotientGraph; # to be populated, from SCC ID to array of:
# [ array of immediately reachable SCC IDs,
# array of transitively reachable SCC ids,
# rank number ]
# The first member may be pruned to only those nodes reachable by a longest
# path of length one
# find strongly connected components with Tarjan's algorithm, which discovers
# the nodes of the quotient graph in a bottom-up topologically sorted order
my %temp; # assigns numbers to node names
my $count = 1;
my @stack; # names
my $traceDepth = 0;
$arcs = 0;
my $prunedArcs = 0;
my $maxRank = -1;
my $largest = 0;
# three utility procedures for discovery of one s.c.c.
sub merge {
my ($a, $b) = @_;
my $na = @$a;
my $nb = @$b;
my @result;
while ($na && $nb) {
if ($$a[-$na] lt $$b[-$nb]) {
push @result, $$a[-($na--)];
}
elsif ($$b[-$nb] lt $$a[-$na]) {
push @result, $$b[-($nb--)];
}
else {
push @result, $$a[-($na--)]; $nb--;
}
}
push @result, $$a[-($na--)] while $na;
push @result, $$b[-($nb--)] while $nb;
@result;
}
sub diff {
my ($a, $b) = @_;
my $na = @$a;
my $nb = @$b;
my @result;
while ($na && $nb) {
if ($$a[-$na] lt $$b[-$nb]) {
push @result, $$a[-($na--)];
}
elsif ($$b[-$nb] lt $$a[-$na]) {
$nb--;
}
else {
$na--; $nb--;
}
}
push @result, $$a[-($na--)] while $na;
@result;
}
sub discoverOneComponent {
my ($sorted, $traceIndent) = @_; # reference to sorted array of names
# first populate the quotient map
foreach my $node (@$sorted) {
$quotientMap{ $node } = $sorted;
}
# now add arcs to the quotient graph
my $qhead = $$sorted[0]; # identifier of quotient node, agreeing with sub SCCID
$#{$quotientGraph{ $qhead }} = 2; # reserve results
my $data = $quotientGraph{ $qhead }; # reference to results
my $rank = -1;
my @reachable;
my %direct;
my @merged;
foreach my $node (@$sorted) {
my $tails = $graph{ $node };
foreach my $tail ( keys %$tails ) {
# it is guaranteed that all destination nodes are already in quotientMap,
# because of the bottom-up discovery sequence, so this works:
my $qtail = SCCID( $quotientMap{ $tail } );
$direct{ $qtail } = () if ( $qhead ne $qtail );
my $tailData = $quotientGraph{ $qtail };
my $tailRank = $$tailData[2];
$rank = $tailRank if $tailRank > $rank;
@reachable = merge( $$tailData[1], \@reachable );
}
}
++$rank;
my @direct = sort ( keys %direct ); # all direct arcs
my @pruned = diff( \@direct, \@reachable ); # all nonredundant direct arcs
$prunedArcs += @pruned; # count for trace information
$arcs += @direct; # count for trace information
@reachable = merge( \@pruned, \@reachable ); # all nodes reachable (excluding self)
$$data[0] = $pruning ? \@pruned : \@direct;
$$data[1] = \@reachable;
$$data[2] = $rank;
if ($traceLevel >= 3) {
print STDERR "${traceIndent}${qhead}";
print STDERR " and ", (scalar(@$sorted) - 1), " other(s)" if scalar(@$sorted) > 1;
print STDERR " discovered at rank ${rank}\n";
}
$maxRank = $rank if $rank > $maxRank;
$largest = @$sorted if @$sorted > $largest;
}
#recursive procedure
sub tarjan {
my ($name, $num) = @_;
my $traceIndent = " " x $traceDepth;
if ( exists( $temp{$name} ) ) {
# have visited
my $number = $temp{$name};
if ($number > 0) {
#scc not fully known
print STDERR "${traceIndent}${name} ${number} revisited\n" if $traceLevel >= 3;
return $number;
}
else {
#scc known
return $num; # unchanged
}
}
else {
# first visit
push @stack, $name;
$temp{$name} = my $number = $count++;
print STDERR "${traceIndent}${name} ${number} discovering\n" if $traceLevel >= 3;
# recur on directly reachable nodes
my $least = $number;
my $tails = $graph{$name};
++$traceDepth;
foreach my $name2 ( keys %$tails ) {
my $result = tarjan( $name2, $number );
$least = $result if $result < $least;
}
--$traceDepth;
if ($least == $number) {
# finished a component (this was the first discovered node in it)
my $node;
my @scc;
do {
$node = pop @stack;
$temp{ $node } = 0;
push @scc, $node;
} while( $node ne $name );
my @sorted = sort @scc;
discoverOneComponent( \@sorted, $traceIndent );
return $num; # unchanged
}
else {
# not finished
print STDERR "${traceIndent}${name} deferred to ${least}\n" if $traceLevel >= 3;
return $least;
}
}
}
# top invocation of recursive procedure discovers all
foreach my $node ( keys %graph ) {
tarjan( $node, 0 );
}
#give trace information
if ($traceLevel >= 1) {
print STDERR "Found ", scalar(keys(%quotientGraph)), " strongly connected component(s) in ", (1 + $maxRank), " rank(s)\n";
print STDERR "Largest component size is ${largest}\n";
print STDERR "${arcs} arc(s) found (${prunedArcs} after pruning)\n";
}
# Step 4: output the graph in dot language
print STDERR "Generating .dot file\n" if $traceLevel >= 1;
# temporary redirection
*OLD_STDOUT = *STDOUT;
my $fname = "graph.dot";
open my $fh, ">", $fname or die "Can't open file";
*STDOUT = $fh;
# header
my $graphAttr =
# $clustering ?
"labeljust=l labelloc=b"
# : ""
;
print "strict digraph{ graph [";
print $graphAttr;
print " newrank=true";
#print " mclimit=0.01";
#print " nslimit=1";
#print " rank=max";
#print " rankdir=LR";
print "]\n";
print "node [style=filled]";
# nodes and their clusters
# group the nodes into subgraphs corresponding to directories
print "\n";
print "// Nodes\n";
my $hue = 0;
my $saturation = 1.0;
my $huestep = 1.0 / $nFolders;
sub subgraph{
my ($foldername, $hashref) = @_;
my $clustered = clustering( $foldername );
my $cluster = $clustered ? "cluster" : "";
my $clusterAttr = $clustered ? "style=bold color=\"blue\"" : "";
print STDERR "subgraph \"$foldername\"\n" if $traceLevel >= 3;
my $color = "${hue},${saturation},1.0";
$hue += $huestep;
$saturation = 1.5 - $saturation; # alternate bold and pale
my $attrs = $clusterAttr . "label=\"$foldername\"";
print "\nsubgraph \"${cluster}${foldername}\" { $attrs node [fillcolor=\"${color}\"]\n";
# describe the nodes at this level, stored as a set (i.e. a hash to
# don't-care values) at key ""
foreach my $name (sort (keys %{$$hashref{""}})) {
next unless $name; # ignore dummy element
my $scc = $quotientMap{ $name };
my $id = SCCID( $scc );
# only want the name that is the representative of its s.c.c.
# equivalence class
next unless $name eq $id;
my $label = SCCLabel( $scc );
print " \"${id}\" [label=\"$label\"";
# insert other node attributes here as key=value pairs,
print " URL=\"${linkroot}${foldername}/${id}.cpp\"" if $links;
# separated by spaces
print"]\n";
}
# now recur, to describe nested clusters
foreach my $name ( sort( keys %$hashref ) ) {
next unless $name; # we just did the special entry at key "" above,
# which is a set of leaves at this level, not a subtree
subgraph( "${foldername}/${name}", $$hashref{ $name } );
}
print "}\n";
}
subgraph( "", \%folders );
# now describe the arcs
print "\n";
print "// Arcs\n";
while( my ($head, $data) = each( %quotientGraph ) ) {
foreach my $tail ( @{$$data[0]} ) {
print " \"$head\" -> \"$tail\" [";
# insert arc attributes here as key=value pairs,
print "penwidth=2.0";
# separated by spaces
print"]\n";
}
}
#footer
print "}\n";
# restore
*STDOUT = *OLD_STDOUT;
# Step 5: generate image
print STDERR "Generating image...\n" if $traceLevel >= 1;
my $verbosity = ($traceLevel >= 2) ? "-v" : "";
`dot $verbosity -O -Tsvg $fname`;
print STDERR "done\n" if $traceLevel >= 1;
|