【问题标题】:How to retrieve function call argument values using libclang如何使用 libclang 检索函数调用参数值
【发布时间】:2014-10-20 16:07:07
【问题描述】:

是否可以检索 clang.cindex.CursorKind.CALL_EXPR 游标的参数值?

当我使用编译器 (clang++ -ast-dump source.cpp) 转储 AST 时,我会获得有关函数调用(调用表达式)及其参数的信息。但我无法使用 python 的绑定来复制它(使用 libclang 的解析器检索 AST)。

这是我正在使用的源代码:

#include <iostream>
#include <GL/glut.h>
#include <EGL/egl.h>

#define MULTILINE(...) #__VA_ARGS__

void renderFunction()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    glBegin(GL_QUADS);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
    glEnd();
    glFlush();
}

int main(int argc, char *argv[])
{          
    glutInit(&argc, argv);       
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("OpenGL - First window demo");
    glutDisplayFunc(renderFunction);
    glutMainLoop();    

    return 0;
}

这是它的 AST 转储的一部分:

|   |-CallExpr 0x430b540 <line:10:5, col:32> 'void'
|   | |-ImplicitCastExpr 0x430b528 <col:5> 'void (*)(GLbitfield)' <FunctionToPointerDecay>
|   | | `-DeclRefExpr 0x430b4d0 <col:5> 'void (GLbitfield)' lvalue Function 0x3d3b060 'glClear' 'void (GLbitfield)'
|   | `-ImplicitCastExpr 0x430b570 </usr/include/GL/gl.h:691:31> 'GLbitfield':'unsigned int' <IntegralCast>
|   |   `-IntegerLiteral 0x430b4b0 <col:31> 'int' 16384

我想通过评估调用表达式光标来检索最后一行中的 IntegerLiteral 值部分。

【问题讨论】:

    标签: python libclang


    【解决方案1】:

    您可以从令牌列表中获取此信息,对于 IntegerLiteral,第一个令牌将是您的号码(不是很整洁,但总比没有好!)。

    示例 cpp 程序:

    #define FOO 6
    
    void foo(int x) {}
    
    int main()
    {
        foo(FOO);
        return 0;
    }
    

    解析它并打印出 IntegerLiteral 值的示例 python 代码(使用 lib clang):

    import clang.cindex
    import sys
    
    path = '/your/path/to/libclang.so'
    clang.cindex.Config.set_library_file(path)
    
    def get_ts(source_path):
        index = clang.cindex.Index.create()
        return index.parse(source_path)
    
    def print_node(node):
        if node.kind == clang.cindex.CursorKind.INTEGER_LITERAL:
            print node.type.kind, node.get_tokens().next().spelling
        map(print_node, node.get_children())
    
    ts = get_ts('test.cpp')
    map(print_node, ts.cursor.get_children())
    

    输出:

    TypeKind.INT 6
    TypeKind.INT 0
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多