首页 > 解决方案 > python中的libclang不会解析超过2000 LOC的C文件

问题描述

我想在 python 脚本(windows 10 && Ubuntu18.04)中使用 libclang 来解析一个超过 2000 行的 C 文件。该脚本可以运行,但它不能完美地解析 C 文件。我不知道为什么会这样。源代码中是否有一些参数需要修改?脚本如下:

import clang.cindex

def parse_decl(node):
    reference_node = node.get_definition()
    if node.kind.is_declaration():
        if(node.kind.name == 'FUNCTION_DECL'):
            if(node.is_definition()):
                print(node.kind.name, node.location.line, reference_node.displayname)
    for ch in node.get_children():
        parse_decl(ch)
if __name__ == '__main__':
    clang.cindex.Config.set_library_file('***\\clang\\native\\libclang.dll')
    index = clang.cindex.Index.create()
    trans_unit = index.parse(r'***\linux-fc6a5d0601c5ac1d02f283a46f60b87b2033e5ca\net\bridge\netfilter\ebtables.c', args=['-std=c++11'])#
    parse_decl(trans_unit.cursor)

脚本的输出是: enter image description here enter image description here

但是 C 文件有脚本不检查的其他功能。 在此处输入图像描述

你能帮我解决这个问题吗?谢谢。

标签: pythonlibclang

解决方案


推荐阅读