通过libclang在Python解析C++源文件时,我试图找到(行和列)的特定函数声明的所有引用特定函数声明的所有引用。查找libclang(Python)的
例如:
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main()
{
int z, q;
z = addition (5,3);
q = addition (5,5);
cout << "The first result is " << z;
cout << "The second result is " << q;
}
因此,对于上面的源文件,我想在第5行的函数声明为addition
,我想find_all_function_decl_references
(见下文),以返回addition
的参考文献在线路15和16
我曾尝试这个(改编自here)
import clang.cindex
import ccsyspath
index = clang.cindex.Index.create()
translation_unit = index.parse(filename, args=args)
for node in translation_unit.cursor.walk_preorder():
node_definition = node.get_definition()
if node.location.file is None:
continue
if node.location.file.name != sourcefile:
continue
if node_def is None:
pass
if node.kind.name == 'FUNCTION_DECL':
if node.kind.is_reference():
find_all_function_decl_references(node_definition.displayname) # TODO
另一种方法是存储列表中的所有函数声明,并在每个函数上运行find_all_function_decl_references
方法。
有没有人有任何想法如何解决这个问题?这个find_all_function_decl_references
方法将如何? (我对libclang
和Python非常感兴趣。)
我看过thisdef find_typerefs
找到了某种类型的所有引用,但我不知道如何实现它以满足我的需要。
理想情况下,我希望能够获取任何声明的所有引用;不仅是功能,但是变量声明,声明的参数(如在本例中第7行a
和b
以上),类声明等
编辑 继Andrew's评论,这里有一些细节对我的设置规范:
- LLVM 3.8.0-Win64的
- libclang-PY 3 3.8.1
- Python3.5.1(在Windows中,我假设的CPython)
- 对于
args
,我尝试了回答here中提出的问题以及another答案中的问题。
*请注意,鉴于我的小编程经验,我可以通过简短的解释它的工作原理来欣赏答案。
不为铛/ Python,但看到http://stackoverflow.com/a/37149988/120163另一个变化上如何“找到所有对特定符号/运算符的引用” –
@IraBaxter感谢您的链接,但我只关心通过'libclang'实现这一点。 – Karim