Finding where a type for a template argument originates

Consider the following code:


void f() {}

void g() { f(); }

void h() { g(); }

Given the FunctionDecl representing f (the specialization) and knowing that I’m interested in template argument at index 0, I would like to find the point where the type for the template argument originally came from. So in the above example, I would like to find the CallExpr to g in h().

I was hoping there was already an existing solution in clang for this, but I could not find one.

My best idea is to, going backwards, search for CallExpr-s pointing to the function, and then figure out if that CallExpr is inside a template function or a method inside a class template, and determine whether in the template the corresponding CallExpr points to an OverloadExpr, whose template argument list I could check… but this is pretty complicated.

What’s the easiest way to do this?

Thank you!