-1

我想使用C程序来查找指令的总数,如#include, #define, #ifdef,#typedef等。你能建议任何逻辑吗?我对使用任何脚本或工具不感兴趣。我希望它完全使用C程序来完成。

4

3 回答 3

1

假设您不想解析它们,或任何其他类型的句法/语义分析,您可以简单地计算以 0 个或多个空格字符开头的行数,然后是一个#字符(经过简单测试,应该可以正常工作):

#include <stdio.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
    FILE *f = fopen(argv[1], "r");
    char line[1024];
    unsigned ncppdirs = 0;

    while (feof(f) == 0) {
        fgets(line, sizeof(line), f);

        char *p = line;
        while (isspace(*p))
            p++;

        if (*p == '#') ncppdirs++;
    }

    printf("%u preprocessor directives found\n", ncppdirs);

    return 0;
}
于 2012-12-24T18:25:18.633 回答
1

所有指令存储在指针数组(或数组)中。

逐行读取 C 文件并检查第一个单词是否以列表中的任何指令开头,不包括开头的任何空格。

char *directives[]={"#assert", "#define#, ......};

int count[NUM_DIRS]= { 0 };

每次找到匹配项时,都会增加count数组的对应索引。您还可以维护另一个计数器total以避免在count数组中添加值。

于 2012-12-24T18:29:00.033 回答
0

您可能会利用gcc -H向您显示每个包含文件的优势,然后您可以popen使用该命令,并(简单地)解析其输出。

您还可以解析由gcc -C -E;给出的预处理输出。它包含行信息 - 作为以开头的行#

仅按词法计算 of 的出现#include是不够的,因为<features.h>某些包含的文件确实会发生类似

  #if SOME_SYMBOL > 2
  #include "some-internal-header.h"
  #define SOME_OTHER_SYMBOL (SOME_SYMBOL+1)
  #endif

and some later include would have #if SOME_OTHER_SYMBOL > 4

And the compilation command might BTW define SOME_SYMBOL with e.g. gcc -DSOME_SYMBOL=3 (and such tricks happen a lot, often in Makefile-s, and just optimizing with -O2 makes __OPTIMIZE__ a preprocessor defined symbol).

If you want some more deep information about source programs, consider making GCC plugins or extensions, e.g. with MELT (a domain specific language to extend GCC). For instance, counting Gimple instructions in the intermediate representation is more sensible than counting lines of code.

Also, some macros might do some typedef; some programs may have

 #define MYSTRUCTYPE(Name) typedef struct Name##_st Name##_t;

and later use e.g. MYSTRUCTYPE(point); what does that mean about counting typedef-s?

于 2012-12-24T18:29:39.677 回答