I'm trying to parse C++ code to create some documentation. In the code, templates are used, but the overloaded function does not seem to get properly linked to the function template.
I'm using the pypi packages libclang and clang, with clang 16.
My issue can be explained with the following example:
import clang.cindex
def walk(node, t):
print(f'{" "*t} name: {node.spelling or node.displayname}, type: {node.type.spelling}, kind {node.kind}' )
if(node.referenced):
print(f"{" "*t} Has a definition: {node.referenced.location.line}")
for c in node.get_children():
walk(c, t + 1)
s = """
#include "stdio.h"
template <class T>
void foo(T bar)
{
bar++;
}
int main(int argv, char** argc){
foo(10);
}
"""
index = clang.cindex.Index.create()
tu = index.parse('tmp.cpp', unsaved_files=[('tmp.cpp', s)])
print('Translation unit:', tu.spelling)
walk(tu.cursor, 0)
This seems to properly parse the overloading of the template, as seen in the output:
name: foo, type: void, kind CursorKind.CALL_EXPR
Has a definition: 5
name: foo, type: void (*)(int), kind CursorKind.UNEXPOSED_EXPR
Has a definition: 5
name: foo, type: void (int), kind CursorKind.DECL_REF_EXPR
Has a definition: 5
However if I call the swap function is called with an overloaded function type, i.e. foo<int>(10), the output becomes:
name: , type: void, kind CursorKind.UNEXPOSED_EXPR
name: , type: <overloaded function type>, kind CursorKind.DECL_REF_EXPR
Has a definition: 11
name: foo, type: , kind CursorKind.OVERLOADED_DECL_REF
Has a definition: 11
I don't fully understand what the difference is between overloading a template by the type of the parameter vs overloading it with a specific type (). Should clang treat this as the same thing?