试试 gcc 选项-fdump-tree-fixupcfg-lineno
。
它将以一种可以使用相对简单的词法分析器或任何正则表达式引擎轻松解析的方式“漂亮地打印”解析 AST(带有行号)。只需找到所有以 '=' 开头并后跟 '(' 的非关键字 - 这将是函数调用。
所有复杂的表达式将被分成几行,因此不会有两个函数调用出现在一行上。
采取简单的程序:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI (3.1415926536)
int main(int argc, char *argv[]) {
double angle = PI / 2.0;
printf("Sine = %lf, cosine = %lf\n", sin(angle), cos(angle));
return EXIT_SUCCESS;
}
编译它,-fdump-tree-fixupcfg-lineno
你会得到这样的东西:
main (argc, argv)
{
double angle;
int D.3381;
double D.3380;
double D.3379;
# BLOCK 2, starting at line 8
# PRED: ENTRY (fallthru)
[test.c : 8] angle = 1.57079632680000003119857865385711193084716796875e+0;
[test.c : 9] D.3379 = [test.c : 9] cos (angle);
[test.c : 9] D.3380 = [test.c : 9] sin (angle);
[test.c : 9] printf (&"Sine = %lf, cosine = %lf\n"[0], D.3380, D.3379);
[test.c : 10] D.3381 = 0;
return D.3381;
# SUCC: EXIT
}
你不会得到任何复杂的表达式——只有赋值和函数调用,没有 CPP 宏,很容易解析。循环和条件不会使它变得更加困难。