I'm trying to extract the template arguments from class instantiations in C++ using python-clang
, ie. the Python bindings for libclang
(with clang
3.9). For example, for
template <typename T> class X {};
X<bool> x;
I'd like to be able to figure out that X
is instantiated with a bool
as its template parameter.
First, it seems that some functions, such as get_num_template_arguments
, are not exposed via python-clang
in the first place, which is where cymbal seems to come into play to monkey-patch python-clang
.
With this, I was able to get this far:
#!/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)
This outputs:
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
I was expecting the node.get_template_argument_type(0).kind
in template_finder
to return something to lead me to bool
, but I couldn't find anything. Is this the right way to go? Is it even possible to get the template arguments in the current state of python-clang
?
I think all you're really missing is
.type
in a couple of places in template finder, but for reference, this works for me, even on clang as old as 3.7Which gives: