| 1 |
package Analizo::Extractor::Doxyparse; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use parent qw(Analizo::Extractor); |
| 7 |
|
| 8 |
use File::Temp qw/ tempfile /; |
| 9 |
use Cwd; |
| 10 |
use YAML::XS; |
| 11 |
use File::Spec::Functions qw/ tmpdir /; |
| 12 |
|
| 13 |
sub new { |
| 14 |
my ($package, @options) = @_; |
| 15 |
return bless { files => [], @options }, $package; |
| 16 |
} |
| 17 |
|
| 18 |
sub _add_file { |
| 19 |
my ($self, $file) = @_; |
| 20 |
push(@{$self->{files}}, $file); |
| 21 |
} |
| 22 |
|
| 23 |
sub _cpp_hack { |
| 24 |
my ($self, $module) = @_; |
| 25 |
my $current = $self->current_file; |
| 26 |
if (defined($current) && $current =~ /^(.*)\.(h|hpp)$/) { |
| 27 |
my $prefix = $1; |
| 28 |
# look for a previously added .cpp/.cc/etc |
| 29 |
my @implementations = grep { /^$prefix\.(cpp|cxx|cc)$/ } @{$self->{files}}; |
| 30 |
foreach my $file (@implementations) { |
| 31 |
$self->model->declare_module($module, $file); |
| 32 |
} |
| 33 |
} |
| 34 |
if (defined($current) && $current =~ /^(.*)\.(cpp|cxx|cc)$/) { |
| 35 |
my $prefix = $1; |
| 36 |
# look for a previously added .h/.hpp/etc |
| 37 |
my @implementations = grep { /^$prefix\.(h|hpp)$/ } @{$self->{files}}; |
| 38 |
foreach my $file (@implementations) { |
| 39 |
$self->model->declare_module($module, $file); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
sub feed { |
| 45 |
my ($self, $doxyparse_output, $line) = @_; |
| 46 |
my $yaml = undef; |
| 47 |
eval { $yaml = Load($doxyparse_output) }; |
| 48 |
if ($@) { |
| 49 |
die $!; |
| 50 |
} |
| 51 |
foreach my $full_filename (sort keys %$yaml) { |
| 52 |
|
| 53 |
# current file declaration |
| 54 |
my $file = _strip_current_directory($full_filename); |
| 55 |
$self->current_file($file); |
| 56 |
$self->_add_file($file); |
| 57 |
|
| 58 |
# current module declaration |
| 59 |
foreach my $module (keys %{$yaml->{$full_filename}}) { |
| 60 |
my $modulename = _file_to_module($module); |
| 61 |
next if defined $yaml->{$full_filename}->{$module} && ref($yaml->{$full_filename}->{$module}) ne 'HASH'; |
| 62 |
|
| 63 |
$self->current_module($modulename); |
| 64 |
$self->_cpp_hack($modulename); |
| 65 |
|
| 66 |
# inheritance |
| 67 |
if (defined $yaml->{$full_filename}->{$module}->{inherits}) { |
| 68 |
if (ref $yaml->{$full_filename}->{$module}->{inherits} eq 'ARRAY') { |
| 69 |
foreach my $inherits (@{ $yaml->{$full_filename}->{$module}->{inherits} }) { |
| 70 |
$self->model->add_inheritance($self->current_module, $inherits); |
| 71 |
} |
| 72 |
} |
| 73 |
else { |
| 74 |
my $inherits = $yaml->{$full_filename}->{$module}->{inherits}; |
| 75 |
$self->model->add_inheritance($self->current_module, $inherits); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
# abstract class |
| 80 |
if (defined $yaml->{$full_filename}->{$module}->{information}) { |
| 81 |
if ($yaml->{$full_filename}->{$module}->{information} eq 'abstract class') { |
| 82 |
$self->model->add_abstract_class($self->current_module); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
foreach my $definition (@{$yaml->{$full_filename}->{$module}->{defines}}) { |
| 87 |
my ($name) = keys %$definition; |
| 88 |
next if $definition->{$name}->{prototype} and $definition->{$name}->{prototype} eq 'yes'; |
| 89 |
my $type = $definition->{$name}->{type}; |
| 90 |
my $qualified_name = _qualified_name($self->current_module, $name); |
| 91 |
$self->{current_member} = $qualified_name; |
| 92 |
|
| 93 |
# function declarations |
| 94 |
if ($type eq 'function') { |
| 95 |
$self->model->declare_function($self->current_module, $qualified_name); |
| 96 |
} |
| 97 |
# variable declarations |
| 98 |
elsif ($type eq 'variable') { |
| 99 |
$self->model->declare_variable($self->current_module, $qualified_name); |
| 100 |
} |
| 101 |
#FIXME: Implement define treatment (no novo doxyparse identifica como type = "macro definition") |
| 102 |
# define declarations |
| 103 |
elsif ($type eq 'macro definition') { |
| 104 |
#$self->{current_member} = $qualified_name; |
| 105 |
} |
| 106 |
|
| 107 |
# public members |
| 108 |
if (defined $definition->{$name}->{protection}) { |
| 109 |
my $protection = $definition->{$name}->{protection}; |
| 110 |
$self->model->add_protection($self->current_member, $protection); |
| 111 |
} |
| 112 |
|
| 113 |
# method LOC |
| 114 |
if (defined $definition->{$name}->{lines_of_code}) { |
| 115 |
$self->model->add_loc($self->current_member, $definition->{$name}->{lines_of_code}); |
| 116 |
} |
| 117 |
|
| 118 |
# method parameters |
| 119 |
if (defined $definition->{$name}->{parameters}) { |
| 120 |
$self->model->add_parameters($self->current_member, $definition->{$name}->{parameters}); |
| 121 |
} |
| 122 |
|
| 123 |
# method conditional paths |
| 124 |
if (defined $definition->{$name}->{conditional_paths}) { |
| 125 |
$self->model->add_conditional_paths($self->current_member, $definition->{$name}->{conditional_paths}); |
| 126 |
} |
| 127 |
|
| 128 |
foreach my $uses (@{ $definition->{$name}->{uses} }) { |
| 129 |
my ($uses_name) = keys %$uses; |
| 130 |
my $uses_type = $uses->{$uses_name}->{type}; |
| 131 |
my $defined_in = $uses->{$uses_name}->{defined_in}; |
| 132 |
my $qualified_uses_name = _qualified_name($defined_in, $uses_name); |
| 133 |
# function calls/uses |
| 134 |
if ($uses_type eq 'function') { |
| 135 |
$self->model->add_call($self->current_member, $qualified_uses_name, 'direct'); |
| 136 |
} |
| 137 |
# variable references |
| 138 |
elsif ($uses_type eq 'variable') { |
| 139 |
$self->model->add_variable_use($self->current_member, $qualified_uses_name); |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
# concat module with symbol (e.g. main::to_string) |
| 149 |
sub _qualified_name { |
| 150 |
my ($file, $symbol) = @_; |
| 151 |
_file_to_module($file) . '::' . $symbol; |
| 152 |
} |
| 153 |
|
| 154 |
# discard file suffix (e.g. .c or .h) |
| 155 |
sub _file_to_module { |
| 156 |
my ($filename) = @_; |
| 157 |
$filename ||= 'unknown'; |
| 158 |
$filename =~ s/\.\w+$//; |
| 159 |
return $filename; |
| 160 |
} |
| 161 |
|
| 162 |
sub _strip_current_directory { |
| 163 |
my ($file) = @_; |
| 164 |
my $pwd = getcwd(); |
| 165 |
$file =~ s#^$pwd/##; |
| 166 |
return $file; |
| 167 |
} |
| 168 |
|
| 169 |
sub actually_process { |
| 170 |
my ($self, @input_files) = @_; |
| 171 |
my ($temp_handle, $temp_filename) = tempfile(); |
| 172 |
foreach my $input_file (@input_files) { |
| 173 |
print $temp_handle "$input_file\n" |
| 174 |
} |
| 175 |
close $temp_handle; |
| 176 |
|
| 177 |
eval 'use Alien::Doxyparse'; |
| 178 |
$ENV{PATH} = join(':', $ENV{PATH}, Alien::Doxyparse->bin_dir) unless $@; |
| 179 |
|
| 180 |
eval { |
| 181 |
local $ENV{TEMP} = tmpdir(); |
| 182 |
open DOXYPARSE, "doxyparse - < $temp_filename |" or die "can't run doxyparse: $!"; |
| 183 |
local $/ = undef; |
| 184 |
my $doxyparse_output = <DOXYPARSE>; |
| 185 |
close DOXYPARSE or die "doxyparse error"; |
| 186 |
$self->feed($doxyparse_output); |
| 187 |
unlink $temp_filename; |
| 188 |
}; |
| 189 |
if($@) { |
| 190 |
warn($@); |
| 191 |
exit -1; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
1; |