0

我想打印所有函数定义的大小。

样本文件(真实文件很大)

....
VideoChainT* ChainCtrlBuildChain(...)
{
   abcd
   edfg
   dfdsh
}
eErrorT ChainCtrlInit(...)
{
   sdgfg
   dsgd
}
....

我已经使用 doxygen 将函数名称(还有更多)提取到文本文件中

函数.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.

将不胜感激这个问题的帮助,或者如果有任何其他方式..

4

2 回答 2

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
于 2013-10-28T14:04:35.467 回答
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";
    }
}
于 2013-10-28T15:02:10.027 回答