Auto Generate Reflection Information for C++
https://www.reddit.com/r/gamedev/comments/3lh0ba/using_clang_to_generate_c_reflection_data/
https://eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-clang/
代码
https://github.com/chakaz/reflang
https://shaharmike.com/cpp/libclang/
文档
https://clang.llvm.org/doxygen/group__CINDEX.html
Windows上安装libclang的方法
在官网https://llvm.org/下载LLVM Win Installer。安装完成后,在安装目录下找libclang.dll。
Mac上安装libclang的python绑定的方法
pip install clang
Mac上libclang的位置
With the latest (appstore) XCode 4.3.2, the location changed, it can now be found in
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib
The /Developer
directory, among others, no longer exists by default. Everything is now packaged inside the XCode application, so that delta updates from the appstore work.
找不到Cursor_ref方法的问题
AttributeError: module 'clang.cindex' has no attribute 'Cursor_ref'
您需要将ref_node = clang.cindex.Cursor_ref(node)
更改为ref_node = node.get_definition()
,以避免获得AttributeError
,因为Cursor_ref
不再是clang.cindex
模块的属性。
Python代码find_ref.py
#!/usr/bin/env python
""" Usage: call with <filename> <typename>
""" import sys
import clang.cindex def find_typerefs(node, typename):
""" Find all references to the type named 'typename'
"""
if node.kind.is_reference():
ref_node = node.get_definition()
if ref_node.spelling == typename:
print('Found %s [line=%s, col=%s]' % (typename, node.location.line, node.location.column)) # Recurse for children of this node
for c in node.get_children():
find_typerefs(c, typename) index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1])
print('Translation unit:', tu.spelling)
find_typerefs(tu.cursor, sys.argv[2])
C++代码person.cpp
class Person {
}; class Room {
public:
void add_person(Person person)
{
// do stuff
} private:
Person* people_in_room;
}; template <class T, int N>
class Bag<T, N> {
}; int main()
{
Person* p = new Person();
Bag<Person, 42> bagofpersons; return 0;
}
运行
python3 find_ref.py person.cpp Person
最新文章
- Error Domain=NSURLErrorDomain Code=-999 “The operation couldn’t be completed.
- 用Java来写常见的排序算法
- 通过shell命令编辑xml文件--sed工具
- asp.net-枚举绑定控件
- jquery.cookie.js存与取以及过期时间设置
- Java中的GC操作及相关概念
- 【转】Linux高级字符设备之Poll操作
- 七行jquery代码实现图片渐变切换【兼容ie6+、 Chrome、 Firefox】
- WisDom.Net 框架设计(八) 持久层
- Linux Shell编程(13)——数字常量
- php浮点数计算比较及取整不准确解决方法
- css基础语法二(常用文本与背景属性)
- C# 发送电子邮件源码片段
- 简化版的AXI-LITE4和配合使用的RTL
- conda常用命令
- benthos 通过rest api 配置 stream 说明
- WinForm下ComboBox设定SelectedValue总结 (SelectedValue==null解决办法)[转]
- 【Git】Git与GitHub 入门【转】
- CMDB经验分享之 – 剖析CMDB的设计过程
- python使用set来去重碰到TypeError: unhashable type