0

我正在使用 pycparser 来解析 C 代码。我的目标是给定一个 C 代码和一个函数名,列出指定函数中调用的所有函数。

我查看了 pycparser 的文档,但找不到任何专门解决此问题的方法。

我想要与 cscope 相同的功能:

Functions called by this function: ksw_extd2_sse41

  File             Function          Line
0 ksw2_extd2_sse.c ksw_reset_extz     59 ksw_reset_extz(ez);
1 ksw2_extd2_sse.c _mm_set1_epi8      64 zero_ = _mm_set1_epi8(0);
2 ksw2_extd2_sse.c _mm_set1_epi8      65 q_ = _mm_set1_epi8(q);
3 ksw2_extd2_sse.c _mm_set1_epi8      66 q2_ = _mm_set1_epi8(q2);
4 ksw2_extd2_sse.c _mm_set1_epi8      67 qe_ = _mm_set1_epi8(q + e);
5 ksw2_extd2_sse.c _mm_set1_epi8      68 qe2_ = _mm_set1_epi8(q2 + e2);
6 ksw2_extd2_sse.c _mm_set1_epi8      69 sc_mch_ = _mm_set1_epi8(mat[0]);
7 ksw2_extd2_sse.c _mm_set1_epi8      70 sc_mis_ = _mm_set1_epi8(mat[1]);
8 ksw2_extd2_sse.c _mm_set1_epi8      71 sc_N_ = mat[m*m-1] == 0? _mm_set1_epi8(-e2) : _mm_set1_epi8(mat[m*m-1]);
9 ksw2_extd2_sse.c _mm_set1_epi8      72 m1_ = _mm_set1_epi8(m - 1);
a ksw2_extd2_sse.c kcalloc            92 mem = (uint8_t*)kcalloc(km, tlen_ * 8 + qlen_ + 1, 16);
b ksw2_extd2_sse.c memset             97 memset(u, -q - e, tlen_ * 16);
c ksw2_extd2_sse.c memset             98 memset(v, -q - e, tlen_ * 16);
d ksw2_extd2_sse.c memset             99 memset(x, -q - e, tlen_ * 16);
e ksw2_extd2_sse.c memset            100 memset(y, -q - e, tlen_ * 16);

* Lines 1-16 of 278, 263 more - press the space bar to display more *
4

2 回答 2

1

嵌套的访问者会做。在 FuncDef 访问者中访问 FuncCall,如下所示。

您的代码将仅访问 1-depth funccall。

class FuncCallVisitor(c_ast.NodeVisitor):
    def __init__(self):
        self.callees = []  
    def visit_FuncCall(self, node):
        self.callees.append(node.name.name)

        # nested funccall
        if node.args:
            self.visit(node.args)

class FuncDefVisitor(c_ast.NodeVisitor):
    def visit_FuncDef(self, node):
        fcv = FuncCallVisitor()
        fcv.visit(node.body)

        print(fcv.callees) # calles has all funccall in this funcdef
于 2019-07-14T11:21:49.313 回答
0

我解决了。下面的访问者类打印代码库中所有函数的每个函数调用

class Visitor(c_ast.NodeVisitor):
    def visit_FuncDef(self, node):
        for stmt in node.body.block_items or []:
            if isinstance(stmt, c_ast.FuncCall):
                print (f"Function {node.decl.name} calls {stmt.name.name}")
于 2019-07-10T19:29:56.547 回答