我想打印所有函数定义的大小。
示例文件(真实文件非常大)
....
VideoChainT* ChainCtrlBuildChain(...)
{
abcd
edfg
dfdsh
}
eErrorT ChainCtrlInit(...)
{
sdgfg
dsgd
}
....
我使用doxygen
将函数名称(还有更多)提取到文本文件中function.txt
ChainCtrlBuildChain
ChainCtrlInit
我的代码
use strict;
use warnings;
open(FILE, "< chaincontroller.c") or die $!;
my @lines = <FILE>;
open(FILE1, "< functions.txt") or die $!;
my @lines1 = <FILE1>;
foreach my $func (@lines1){
foreach my $src_line (@lines){
if ($src_line =~ /$func\([^;]+$/){ # points to function definitions
#need to extract function body into an array and get its size
}
}
}
需要输出
function ChainCtrlBuildChain has 3 lines.
function ChainCtrlInit has 2 lines.
会很感激这个问题的帮助,或者有任何其他方式..
答案 0 :(得分:2)
这可能会在很多方面失败,具体取决于您的代码格式,但您可以尝试使用触发器,它在函数定义中激活,并在结束大括号中取消激活。您可以在激活时计算线数并减去三个
foreach my $func (@lines1){
chomp $func;
foreach my $src_line (@lines){
if (my $flipflop = ($src_line =~ /$func\([^;]+$/ .. $src_line =~ /^}/)){
++$lines;
if ($flipflop =~ /E0$/) {
printf qq|function %s has %d lines\n|, $func, $lines - 3;
$lines = 0;
}
next;
}
}
}
在我的测试中它产生:
function ChainCtrlBuildChain has 3 lines
function ChainCtrlInit has 2 lines
答案 1 :(得分:1)
use strict;
use warnings;
open(FILE, "< chaincontroller.c") or die $!;
my @source_lines = <FILE>;
open(FILE1, "< functions.txt") or die $!;
chomp( my @functions = <FILE1> );
my $match_function = qr/\b(@{[ join '|', map quotemeta, sort { length($b) <=> length($a) } @functions ]})\([^;]+$/;
my %function_size;
my $function = '';
for my $source_line (@source_lines) {
if ( $source_line =~ $match_function ) {
$function = $1;
$function_size{$function} = 0;
}
elsif ( $source_line =~ /^}/ ) {
$function = '';
}
# ignore first {
elsif ( $function && ( $source_line !~ /^{/ || $function_size{$function} ) ) {
++$function_size{$function};
}
}
for my $function (@functions) {
if ( exists $function_size{$function} ) {
print "function $function has $function_size{$function} lines.\n";
}
else {
print "function $function not found.\n";
}
}