File: calltree.pl

package info (click to toggle)
espresso 5.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 146,004 kB
  • ctags: 17,245
  • sloc: f90: 253,041; sh: 51,271; ansic: 27,494; tcl: 15,570; xml: 14,508; makefile: 2,958; perl: 2,035; fortran: 1,924; python: 337; cpp: 200; awk: 57
file content (145 lines) | stat: -rwxr-xr-x 2,842 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl -w

use strict;

{
    my $maxdepth = 2; # default
    if ($#ARGV > 0 && $ARGV[0] eq "-d")
    {
	$maxdepth = $ARGV[1];
	if ($maxdepth !~ /^\d+$/)
	{
	    print STDERR "error: non-numeric maxdepth\n";
	    print STDERR "usage: $0 [-d maxdepth] [targets]\n";
	    exit 1;
	}
	shift @ARGV;
	shift @ARGV;
    }

    # $basedir is directory where this script is
    my $basedir = $0;
    $basedir =~ s/(.*)\/.*/$1/;
    my @sources = split(/ /, `echo $basedir/*/*.f90`);

    # grab program, function and subroutine declarations
    my (%place, %fname, %pname);
    foreach my $file (@sources)
    {
	open(IN, "$file");
	while (<IN>)
	{
	    $_ = "\L$_"; # cast everything to lowercase
	    if (/^[^!'""']*\bfunction\s+(\w+)/o && ! /^\s*end\s+function\b/o)
	    {
		$fname{$1} = 1;
		push_place(\%place, $1, $file);
	    }
	    elsif (/^\s*program\s+(\w+)/o)
	    {
		$pname{$1} = 1;
		push_place(\%place, $1, $file);
	    }
	    elsif (/^\s*(?:(?:pure|recursive)\s+)?subroutine\s+(\w+)/o)
	    {
		push_place(\%place, $1, $file);
	    }
	}
	close(IN);
    }
    my @names = sort keys %place;
    my @functions = sort keys %fname;

    # if no arguments are specified, stat all programs
    my @targets = @ARGV;
    if ($#targets < 0)
    {
	@targets = sort keys %pname;
    }

    my %cache;
    foreach my $name (@targets)
    {
	stat_name($name, \%place, \@functions, \%cache, 0, "", $maxdepth);
    }
}

sub push_place
{
    my ($place, $name, $file) = @_;
    if (defined $$place{$name})
    {
	$$place{$name} = "$$place{$name} $file";
    }
    else
    {
	$$place{$name} = "$file";
    }
}

sub stat_name
{
    my ($name, $place, $functions, $cache, $depth, $indent, $maxdepth) = @_;
    print "$indent$name\n";

    if ($depth >= $maxdepth || ! defined $$place{$name})
    {
	return;
    }

    if (! defined $$cache{$name})
    {
	my %cname;
	my @files = split(/ /, $$place{$name});
	foreach my $file (@files)
	{
	    my $current = "";
	    open(IN, $file);
	    while (<IN>)
	    {
		$_ = "\L$_";
		if (/^\s*program\s+(\w+)/o)
		{
		    $current = "$1";
		}
		elsif (/^\s*(?:(?:pure|recursive)\s+)?subroutine\s+(\w+)/o)
		{
		    $current = "$1";
		}
		elsif (/^[^!'""']*\bfunction\s+(\w+)/o)
		{
		    $current = "$1";
		}
		# here we are inside the relevant program/subroutine/function
		elsif ($current eq $name)
		{
		    # subroutine calls
		    if (/^\s*call\s+(\w+)/o)
		    {
			$cname{$1} = 1;
		    }
		    # function calls
		    foreach my $fun (@$functions)
		    {
			if (/^[^!'""']*\b$fun\b/)
			{
			    $cname{$fun} = 1;
			}
		    }
		}
	    }
	    close(IN);
	}
	my @calls = sort keys %cname;
	$$cache{$name} = \@calls;
    }

    foreach my $call (@{$$cache{$name}})
    {
	if ($call ne $name)
	{
	    stat_name($call, $place, $functions, $cache,
		      $depth+1, "  $indent", $maxdepth);
	}
    }
}