rendered paste body#!/usr/bin/perl -W
use strict;
sub deleteDoubleDots($) {
while($_[0] =~ m/\.\./) {
if (!($_[0] =~ s/\/[^\/]*\/\.\.//)) {
$_[0] =~ s/^[^\/]*\/\.\.\///;
}
}
}
my $spaces=" ";
my @files;
my $file;
my @ifdefstack;
my $commentsection;
my @tmp;
my %deptree;
open(FILES, 'find . -iname "*.h" -o -iname "*.cpp" -o -iname "*.c" |') || die ("Can't start find");
while (<FILES>) {
chomp;
s/^.\///;
@files=(@files,$_);
}
close FILES;
foreach $file (@files) {
my @includedfiles;
open(FILETOSCAN, "<$file") || die ("Can't open file '$file'");
$#ifdefstack=-1;
while (<FILETOSCAN>) {
if (/^\W*#if([n]?)def (\w*)/) {
@ifdefstack=(@ifdefstack,$2);
}
if (/^\W*#endif/) {
$#ifdefstack=$#ifdefstack-1;
}
if (/^\W*#include[^<"]*([<"][^">]*[">]*)/) {
# if (/^\W*#include[^"]*(["][^"]*["]*)/) {
my $fullincludeparams=$1;
my $includedfilename;
my $origincludedfilename;
if ($1=~/^"/) {
@tmp=split(/\//,$file);
$#tmp=$#tmp-1;
$includedfilename=join("/",@tmp);
$fullincludeparams=~s/"//g;
$includedfilename=$includedfilename."/".$fullincludeparams;
} else {
$includedfilename=$1;
}
$origincludedfilename=$includedfilename;
deleteDoubleDots($includedfilename);
@includedfiles=(@includedfiles,$includedfilename);
}
}
close FILETOSCAN;
$deptree{$file}=[@includedfiles];
}
foreach $file (keys(%deptree)) {
my $depth=0;
my %alreadyseen;
%alreadyseen=();
if ($file=~/.*\.h$/) {
# print "HEADER: $file\n";
} else {
# print "START: ".$file."\n";
print $file."\n";
recurseprint($file,$depth,%alreadyseen);
}
}
sub recurseprint {
my ($file, $depth,%alreadyseen) = @_;
my $depnumber;
$alreadyseen {$file}=$depth;
# if ($#{$deptree{$file}}>0) {
# print substr($spaces,0,(($depth)*4))."< ".($#{$deptree{$file}}+1)." deps\n";
if ($depth<32) {
$depth=$depth+1;
foreach $depnumber (0 .. $#{$deptree{$file}}) {
my $depfilename=$deptree{$file}[$depnumber];
if (!$alreadyseen{$depfilename}) {
$alreadyseen{$depfilename}=$depth;
print substr($spaces,0,($depth*4)).$depfilename."\n";
if ($deptree{$depfilename}) {
recurseprint($depfilename,$depth,%alreadyseen);
}
} else {
print substr($spaces,0,$depth*4)."$depfilename is already included at a lower level";
if ($deptree{$depfilename}) {
print " - Not descending\n";
} else {
print "\n";
}
}
}
} else {
print "WARNING: Possible dependancy loop in $file\n";
}
# print substr($spaces,0,(($depth-1)*4)).">\n";
# }
}