zoukankan      html  css  js  c++  java
  • 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

     1 #!/usr/bin/env python
     2 """ Usage: call with <filename> <typename>
     3 """
     4 
     5 import sys
     6 import clang.cindex
     7 
     8 def find_typerefs(node, typename):
     9     """ Find all references to the type named 'typename'
    10     """
    11     if node.kind.is_reference():
    12         ref_node = node.get_definition()
    13         if ref_node.spelling == typename:
    14             print('Found %s [line=%s, col=%s]' % (typename, node.location.line, node.location.column))
    15 
    16     # Recurse for children of this node
    17     for c in node.get_children():
    18         find_typerefs(c, typename)
    19 
    20 index = clang.cindex.Index.create()
    21 tu = index.parse(sys.argv[1])
    22 print('Translation unit:', tu.spelling)
    23 find_typerefs(tu.cursor, sys.argv[2])

    C++代码person.cpp

     1 class Person {
     2 };
     3 
     4 
     5 class Room {
     6 public:
     7     void add_person(Person person)
     8     {
     9         // do stuff
    10     }
    11 
    12 private:
    13     Person* people_in_room;
    14 };
    15 
    16 
    17 template <class T, int N>
    18 class Bag<T, N> {
    19 };
    20 
    21 
    22 int main()
    23 {
    24     Person* p = new Person();
    25     Bag<Person, 42> bagofpersons;
    26 
    27     return 0;
    28 }

     

    运行

    python3 find_ref.py person.cpp Person

  • 相关阅读:
    获取从链接传来的id
    通过域名直接访问Tomcat项目解决方法
    线程与高并发
    阿里云部署javaWeb项目全过程
    前后端分离项目,支持跨域,session不丢失
    python函数
    装饰器
    迭代器和生成器
    C/C++ I/O处理
    C++虚函数
  • 原文地址:https://www.cnblogs.com/lilei9110/p/12061325.html
Copyright © 2011-2022 走看看