-4

我希望能够分析 .c 文件并

  1. 列出文件中的所有方法
  2. 列出每个方法在行中的长度

IE:如果文件是(使用伪代码):

int add(){
....Function Actions 1
....Function Actions 2
....Function Actions 3
}

int subtract{
....Function Action 1
}

结果将类似于:

add -3
subtract -1

我一直在将 CTAGS 用于其他事情,但我不确定它是否具有此功能。

谢谢大家。

4

2 回答 2

2

您可以通过自定义 GCC 来做类似的事情。你可以很容易地为此目的编写一个MELT扩展。MELT 是一种扩展 GCC 的领域特定语言。

我建议测量每个函数的 Gimple 指令数量。它可能比数行更有意义。

您可能可以将最后一个 Gimple 的行号减去第一个 gimple 的行号。

行数并没有真正的意义(想象一下大括号之前或之后的注释,你如何计算它们?还有可以扩展到多行、语句甚至函数的宏......)。例如,D.Wheeler 的 SLOCcount 给出的结果与 eg 大不相同wc

如果文件缩进适当,您可能需要测量第 1 列的左大括号和右大括号之间的差异。

于 2013-05-25T20:58:26.090 回答
1

如果代码是好的,一个小的 perl hack 可能就足够了。就像是:

perl -ne 'if(/^\w/){$i=0;chomp;print}elsif(/^}/){print " - $i\n"}else{$i++}' *.c

Yes, this is not very precise, but it gives you an idea why counting lines might cause problems. After all C allows you to add newlines and comments pretty much everywhere.

I would recommend the gcc answer nexdoor. ;)

Whoever got this line counting idea. Please convince him, it is nonsense.

于 2013-05-25T22:09:02.623 回答