python - 获取指定函数中调用的所有函数

标签 python c pycparser

我正在使用 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 *

最佳答案

嵌套访问者就可以了。 在 FuncDef Visitor 中访问 FuncCall,如下所示。

您的代码将仅访问 1 层深度的 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

关于python - 获取指定函数中调用的所有函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56959907/

相关文章:

javascript - Django:启动服务器端计算并动态更新客户端

c# - P-Invoke 解码分配在 DLL 中的结构数组

python-3.6 - 如何使用诗歌解决这个 pycparser 安装错误?

c - extern 后跟字符串文字

python - 查找函数输出的输入依赖关系

python - 我收到错误 400 : Bad Request on custom Heroku domain, 但在 foo.herokuapp.com 上工作正常

python 崩溃并出现错误函数需要 2 个参数(给定 3 个参数)

python - 如何让 SymPy 将 1.0 之类的整数替换为 1

c++ - 如何使用来自另一个 XMM 寄存器条目的 4 个相同 float 填充 x86 XMM 寄存器?

c - 即使使用 '\0' 字符,字符串也不会在 C 中打印