I am using the abstract syntax tree that clang provides through the python interface, trying to parse a simple structure containing a std::vector:
#include <vector>
struct outer_t
{
std::vector<int> vec_of_ints;
};
I would like to get the template argument of the vector, but I cannot find a reference to it in the respective node of the AST. The get_num_template_arguments() member function returns -1. I therefore think that the get_template_* functions cannot be used. I tried the following:
import sys
import clang.cindex
clang.cindex.Config.set_library_file("/usr/lib/llvm-6.0/lib/libclang.so.1")
class Walker:
def __init__(self, filename):
self.filename = filename
def walk(self, node):
node_in_file = bool(node.location.file and node.location.file.name == self.filename)
if node_in_file:
print(f"node.spelling = {node.spelling:14}, node.kind = {node.kind}")
if node.kind == clang.cindex.CursorKind.TEMPLATE_REF:
print(f"node.get_num_template_arguments = {node.get_num_template_arguments()}")
for child in node.get_children():
self.walk(child)
filename = sys.argv[1]
index = clang.cindex.Index.create()
translation_unit = index.parse(filename)
root = translation_unit.cursor
walker = Walker(filename)
walker.walk(root)
This produces the following result:
node.spelling = vec_of_ints , node.kind = CursorKind.FIELD_DECL
node.spelling = std , node.kind = CursorKind.NAMESPACE_REF
node.spelling = vector , node.kind = CursorKind.TEMPLATE_REF
node.get_num_template_arguments = -1
Is there another way to get the template argument or am I doing something wrong ?