-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduce_graph.pl
executable file
·174 lines (143 loc) · 4.12 KB
/
produce_graph.pl
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
#!/usr/bin/env perl
use strict;
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($ERROR);
# perl -ne 'if($_ !~ /\s*\*/){print;}'|perl -ne 'if(/([a-zA-Z0-9]+)\s*[=;]/){print "$1\n";}'|perl -ne 'if($_ !~ /[0-9]+/){print;}' > variables.txt
my %var_dict = ();
if($#ARGV + 1 != 1){
print STDERR "Usage: ./produce_graph.pl source_file\n";
exit(1);
}
my @lines = ();
open(INPUT, $ARGV[0]) or die "Cannot open file $ARGV[0].\n";
my $symbol = '[a-zA-Z_]\w*';
while(<INPUT>){
if($_ =~ /^\s*\*/){
next;
}
# add the current line to the array @lines
push(@lines, $_);
# remove constructor funcitions
$_ =~ s/\([^\)]\)//g;
# parse variables from the current line
while($_ =~ /($symbol)\s*[\[,=;]/g){
DEBUG "vars:$1\n";
$var_dict{$1} = 1;
}
# parse C++ function parameters
if($_ =~ /::/){
while($_ =~ /($symbol)\s*[\),]/g){
$var_dict{$1} = 1;
}
}
# parse pointers
while($_ =~ /($symbol)\s*->/g){
$var_dict{$1} = 1;
}
# parse struct
# if($_ =~ /struct\s+\S+\s+($symbol)/){
# $var_dict{$1} = 1;
# }
}
close(INPUT);
sub search_for_var{
my $expression = shift;
DEBUG "left expression:$expression ";
# first, we need to trim member variables
while($expression =~ s/\.$symbol//g){};
use vars qw(%var_dict);
#my $matched_variable_length = -1;
#my $matched_variable;
foreach my $variable (keys %var_dict){
if($expression =~ /\b$variable\W/){ # do a word search of teh variable
#if(length($variable) > $matched_variable_length){
#$matched_variable_length = length($variable);
#$matched_variable = $variable;
#}
DEBUG "matched:$variable";
return $variable;
}
}
return "";
#return $matched_variable;
}
sub search_for_vars{
my $expression = shift;
chomp $expression;
use vars qw(%var_dict);
#print "right:$expression ";
my @var_list = ();
foreach my $variable (keys %var_dict){
#print ",var:$variable ";
if($expression =~ /\b$variable\b/){ # do a word search of the variable
#print ",captured var:$variable ";
push(@var_list, $variable);
next;
}
# special, support pure numerical values on the right side
if($expression =~ /(\d+)\s*;/){
push(@var_list, $1);
}
}
return @var_list;
}
my @remaining_line = ();
my %result_hash = ();
# parse function calls first
foreach (@lines) {
if ($_ =~ /($symbol)(?:\.|->)($symbol)\s*\((.*)$/) {
#print "Function call:$_";
my $left_var = "$1";
my $right_expression = $3;
my @right_vars = search_for_vars($right_expression);
foreach my $right_var (@right_vars) {
$result_hash{"\t$right_var -> $left_var;\n"} = 1;
}
# this line is also an assignment line
if ($_ =~ /([^=]*)=/) {
push(@remaining_line, "$1 = $left_var;");
#print "Complex statement $_ -------> $1 = $left_var;\n";
}
#push(@remaining_line, )
} else {
push(@remaining_line, $_);
}
}
# then parse assignments
foreach(@remaining_line){
if($_ =~ /=/){
# assignment operation
#print "Remaining line:$_\n";
my ($left_expression, $right_expression) = split(/=/, $_);
#print "left_expression:$left_expression right_expression:$right_expression\n";
my $left_var = search_for_var($left_expression);
DEBUG "left_var:$left_var ";
if($left_var ne ""){
DEBUG "right_expression:$right_expression,";
my @right_vars = search_for_vars($right_expression);
DEBUG "right_vars: @right_vars $#right_vars\n";
if($#right_vars + 1 > 0){
foreach my $right_var (@right_vars){
$result_hash{"\t$right_var -> $left_var;\n"} = 1;
}
}
}
}
}
my $output_dot = "";
my $output_pdf = "";
if($ARGV[0] =~ /([^\.]+)\./){
$output_dot = "$1.dot";
$output_pdf = "$1.pdf";
}else{
$output_dot = $ARGV[0].".dot";
$output_pdf = $ARGV[0].".pdf";
}
open(DOT_OUTPUT, ">$output_dot") or die "Cannot open $output_dot for output";
print DOT_OUTPUT "digraph G{\n";
foreach my $line (keys %result_hash){
print DOT_OUTPUT $line;
}
print DOT_OUTPUT "}\n";
close(DOT_OUTPUT);
system("dot -Tpdf -o $output_pdf $output_dot");