[cfe-dev] With libclang, no way to get cursor to typedef in template from a reference to that typedef

Seth Fowler mark.seth.fowler at gmail.com
Tue Apr 8 14:23:09 PDT 2014


So I’ve encountered what appears to be an issue in libclang 3.4. Given this test case (which I apologize isn’t minimized, but it’s short enough that I figured it’d be OK anyway):

> struct clazz { typedef int clazz_typedef; };
> namespace ns { typedef int ns_typedef; }
> typedef int global_typedef;
> typedef clazz::clazz_typedef (*func_typedef)(ns::ns_typedef a, global_typedef b);
> 
> clazz::clazz_typedef foo(ns::ns_typedef a, global_typedef b) { return a + b; }
> 
> template <typename T> struct templat { typedef T templat_typedef; };  // LINE A
> 
> int main(int argc, char** argv)
> {
>   typedef templat<clazz::clazz_typedef>::templat_typedef local_typedef;  // LINE B
> 
>   func_typedef foo_ptr = foo;
>   return local_typedef(foo_ptr(1, 1));
> };


I want to get a cursor to (and ultimately the USR for) ‘templat_typedef' as defined on line A, from the reference to ‘templat_typedef' on line B.

Using clang_getCursorReferenced/clang_getCursorDefinition on the line B ’templat_typedef’ TypeRef, I can get a cursor which superficially appears to be the right one, but it’s not. The USR is different. The USR for ‘templat_typedef’ on line A is 'c:typedefs.cpp at 316@ST>1#T at templat@T at templat_typedef’, but the USR for the definition/reference cursors I get on line B is 'c:typedefs.cpp at 316@S at templat>#I at T@templat_typedef’. Both cursors have the same type (‘templat_typedef’) and kind (‘Typedef’).

I thought that clang_getSpecializedCursorTemplate might be the solution to this problem, but it just returns a null cursor.

I’m not 100% sure which aspect of this behavior is the bug, but there’s clearly a bug here. Either clang_getCursorReferenced/clang_getCursorDefinition should be usable to get to the line A ‘templat_typedef’ definition from the line B reference, or else clang_getSpecializedCursorTemplate should work. Or possibly there’s a bug in the code that constructs USRs.

- Seth



More information about the cfe-dev mailing list