python-clang:获取模板参数

时间:2016-12-26 15:21:25

标签: python c++ libclang

我试图使用python-clang从C ++中的类实例化中提取模板参数,即。 libclang的Python绑定(clang 3.9)。例如,对于

template <typename T> class X {};
X<bool> x;

我希望能够确定X是以bool为模板参数进行实例化的。

首先,似乎某些功能(例如get_num_template_arguments)并未首先通过python-clang公开,这是cymbal似乎对猴子补丁发挥作用的地方python-clang

有了这个,我能够做到这一点:

#!/usr/bin/env python

import clang.cindex

clang.cindex.Config.set_library_file('/usr/lib/x86_64-linux-gnu/libclang-3.9.so.1')

index = clang.cindex.Index.create()

source = """
template <typename T> class X {};
X<bool> x;
"""

######### stolen from cymbal

from ctypes import c_uint, c_int

def find_libclang_function(function):
    return getattr(clang.cindex.conf.lib, function)

def monkeypatch_helper(classtype, name, library_function, args, result):
    if hasattr(classtype, name):
        raise ('failed to add method, %s is already available' % name) 
    f = find_libclang_function(library_function)
    f.argtypes = args
    f.restype = result
    def impl(*args):
        return f(*args)
    setattr(classtype, name, impl)

def monkeypatch_type(method_name, library_function, args, result):
    monkeypatch_helper(clang.cindex.Type, method_name, library_function, args, result)



monkeypatch_type('get_template_argument_type',
                        'clang_Type_getTemplateArgumentAsType',
                        [clang.cindex.Type, c_uint],
                        clang.cindex.Type)

monkeypatch_type('get_num_template_arguments',
                        'clang_Type_getNumTemplateArguments',
                        [clang.cindex.Type],
                        c_int)
######### /stolen from cymbal

# helpers for visiting the AST recursively
def visit(node, func):
    func(node)
    for c in node.get_children():
        visit(c, func)

def visit_depth(node, func, depth=0):
    func(node, depth)
    for c in node.get_children():
        visit_depth(c, func, depth+1)

# parse the TU
tu = clang.cindex.TranslationUnit.from_source('t.cpp', ['-std=c++11'], unsaved_files=[('t.cpp', source)])

# show the AST
def astprinter(node, depth):
    print " "*depth, node.kind, node.spelling
visit_depth(tu.cursor, astprinter)    

# find everything with a template and try to extract the template argument
def template_finder(node):
    if hasattr(node, 'type') and node.type.get_num_template_arguments() != -1:
        print node.type.get_num_template_arguments(), node.spelling, node.kind, node.get_template_argument_type(0).kind
visit(tu.cursor, template_finder)

输出:

 CursorKind.TRANSLATION_UNIT t.cpp
  CursorKind.CLASS_TEMPLATE X
   CursorKind.TEMPLATE_TYPE_PARAMETER T
  CursorKind.VAR_DECL x
   CursorKind.TEMPLATE_REF X
   CursorKind.CALL_EXPR X
1 x CursorKind.VAR_DECL TypeKind.INVALID
1 X CursorKind.CALL_EXPR TypeKind.INVALID

我期待node.get_template_argument_type(0).kind中的template_finder返回一些内容引导我bool,但我无法找到任何内容。这是正确的方法吗?甚至可以将模板参数设置为当前状态python-clang

1 个答案:

答案 0 :(得分:1)

我认为你真正缺少的是模板查找器中的几个地方的.type,但作为参考,这对我来说很有用,即使是在3.7岁的铿锵声中

import clang.cindex
from clang.cindex import *
import cymbal
from ctypes import *

cymbal.monkeypatch_type('get_template_argument_type',
                        'clang_Type_getTemplateArgumentAsType',
                        [Type, c_uint],
                        Type)

cymbal.monkeypatch_type('get_num_template_arguments',
                        'clang_Type_getNumTemplateArguments',
                        [Type],
                        c_int)


# check if the cursor's type is a template
def is_template(node):
    return hasattr(node, 'type') and node.type.get_num_template_arguments() != -1

index = clang.cindex.Index.create()

source = """
template <typename T> class X {};
X<bool> x;
"""
# parse the TU
tu = clang.cindex.TranslationUnit.from_source('t.cpp', ['-std=c++11'], unsaved_files=[('t.cpp', source)])

for c in tu.cursor.walk_preorder():
    if is_template(c):
        t = c.type
        print t.kind, t.spelling, t.get_num_template_arguments()
        print t.get_template_argument_type(0).spelling

给出了:

TypeKind.UNEXPOSED X<bool> 1
bool
TypeKind.UNEXPOSED X<bool> 1
bool