Clang 项目的 AST [英] AST of a project by Clang

查看:42
本文介绍了Clang 项目的 AST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Clang python 绑定来提取 c/c++ 文件的 AST.它非常适合我编写的一个简单程序.问题是当我想将它用于像 openssl 这样的大项目时.我可以为项目的任何单个文件运行 clang,但是 clang 似乎错过了项目的一些标题,并且只给了我文件的几个函数的 AST,而不是所有函数.我通过 -I 设置了包含文件夹,但仍然获得了部分功能.

这是我的代码:

import clang.cindex 作为 clcl.Config.set_library_path(clang_lib_dir)index = cl.Index.create()lib = '包含文件夹的路径'args = ['-I{}'.format(lib)]translation_unit = index.parse(source_file, args=args)my_get_info(translation_unit.cursor)

我收到太多未找到的头文件错误.

更新

我用Make通过clang编译openssl?我可以将 -emit-ast 选项传递给 clang 以转储每个文件的 ast,但我现在无法通过 clang python 绑定读取它.

有什么线索可以保存翻译单元的序列化表示,以便我能够通过 index.read() 读取它吗?

谢谢!

解决方案

您只需要"提供正确的args.但请注意两个可能的问题.

不同的文件可能需要不同的解析参数.最简单的解决方案是获取编译数据库,然后从中提取编译命令.如果您这样做,请注意您需要稍微过滤掉参数并删除诸如 -c FooBar.cpp 之类的内容(可能还有其他一些内容),否则您可能会得到类似 ASTReadError 的信息.

另一个问题是包含路径 (-I ...) 可能相对于源目录.即,如果从目录 /opt/project/ 编译的文件 main.cpp 带有 -I include/path 参数,则在调用 index.parse(source_file, args=args) 你需要在(chdir) 进入/opt/project,当你完成您可能需要返回原始工作目录.所以代码可能是这样的(伪代码):

cwd = getcwd()chdir('/选择/项目')translation_unit = index.parse(source_file, args=args)chdir(cwd)

希望能帮到你.

I use the Clang python binding to extract the AST of c/c++ files. It works perfectly for a simple program I wrote. The problem is when I want to employ it for a big project like openssl. I can run clang for any single file of the project, but clang seems to miss some headers of the project, and just gives me the AST of a few functions of the file, not all of the functions. I set the include folder by -I, but still getting part of the functions.

This is my code:

import clang.cindex as cl    
cl.Config.set_library_path(clang_lib_dir)
index = cl.Index.create()
lib = 'Path to include folder'
args = ['-I{}'.format(lib)]
translation_unit = index.parse(source_file, args=args)
my_get_info(translation_unit.cursor)

I receive too many header files not found errors.

UPDATE

I used Make to compile openssl by clang? I can pass -emit-ast option to clang to dump the ast of each file, but I cannot read it now by the clang python binding.

Any clues how I can save the the serialized representation of the translation units so that I will be able to read it by index.read()?

Thank you!

解决方案

You would "simply" need to provide the right args. But be aware of two possible issues.

Different files may require different arguments for parsing. The easiest solution is to obtain compilation database and then extract compile commands from it. If you go this way be aware that you would need to filter out the arguments a bit and remove things like -c FooBar.cpp (potentially some others), otherwise you may get something like ASTReadError.

Another issue is that the include paths (-I ...) may be relative to the source directory. I.e., if a file main.cpp compiled from a directory /opt/project/ with -I include/path argument, then before calling index.parse(source_file, args=args) you need to step in (chdir) into the /opt/project, and when you are done you will probably need to go back to the original working directory. So the code may look like this (pseudocode):

cwd = getcwd()
chdir('/opt/project')
translation_unit = index.parse(source_file, args=args)
chdir(cwd)

I hope it helps.

这篇关于Clang 项目的 AST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆