[cfe-dev] problem with template class inheritance and cursor api

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Jan 18 09:43:45 PST 2013


On Jan 18, 2013, at 12:38 AM, Anders Bakken <agbakken at gmail.com> wrote:

> Hi
> 
> I'm writing a clang-based c++/c indexer
> (https://github.com/Andersbakken/rtags/) and I am having a problem
> with the following construct:
> 
> // -----------------
> template <typename T>
> class A
> {
> public:
>    int size() const { return 1; }
> };
> 
> template <typename T>
> class B : public A<T>
> {
> public:
>    bool isEmpty() const { return A<T>::size(); }
> };
> 
> // -----------------
> 
> There seems to be way to get from this cursor(s) for the call to
> A<T>::size() to the actual function (A<T>::size).
> 
> I provide functionality to jump to target cursor and this one has been
> me causing me some problems.
> 
> With the attached example I get the following output:
> 
> main.cpp:2:7 (28, 0-76) kind: ClassTemplate display name: A<T> usr:
> c:@CT>1#T at A definition
>  main.cpp:1:20 (19, 10-20) kind: TemplateTypeParameter display name:
> T usr: c:main.cpp at 10 definition
>  main.cpp:4:1 (32, 32-39) kind: CXXAccessSpecifier usr: c: definition
>  main.cpp:5:9 (48, 44-74) kind: CXXMethod display name: size() usr:
> c:@CT>1#T at A@F at size#1 definition
>    main.cpp:5:22 (61, 61-74) kind: CompoundStmt
>      main.cpp:5:24 (63, 63-71) kind: ReturnStmt
>        main.cpp:5:31 (70, 70-71) kind: IntegerLiteral
> main.cpp:9:7 (107, 79-184) kind: ClassTemplate display name: B<T> usr:
> c:@CT>1#T at B definition
>  main.cpp:8:20 (98, 89-99) kind: TemplateTypeParameter display name:
> T usr: c:main.cpp at 89 definition
>  main.cpp:9:18 (118, 111-122) kind: C++ base class specifier display
> name: A<T>
>  -> main.cpp:2:7 (28, 0-76) kind: ClassTemplate display name: A<T>
> usr: c:@CT>1#T at A definition
>    main.cpp:9:18 (118, 118-119) kind: TemplateRef display name: A
>    -> main.cpp:2:7 (28, 0-76) kind: ClassTemplate display name: A<T>
> usr: c:@CT>1#T at A definition
>    main.cpp:9:20 (120, 120-121) kind: TypeRef display name: T
>    -> main.cpp:8:20 (98, 89-99) kind: TemplateTypeParameter display
> name: T usr: c:main.cpp at 89 definition
>  main.cpp:11:1 (125, 125-132) kind: CXXAccessSpecifier usr: c: definition
>  main.cpp:12:10 (142, 137-182) kind: CXXMethod display name:
> isEmpty() usr: c:@CT>1#T at B@F at isEmpty#1 definition
>    main.cpp:12:26 (158, 158-182) kind: CompoundStmt
>      main.cpp:12:28 (160, 160-179) kind: ReturnStmt
>        main.cpp:12:35 (167, 167-179) kind: CallExpr
>          main.cpp:12:35 (167, 167-177) kind: MemberRefExpr
>            main.cpp:12:35 (167, 167-168) kind: TemplateRef display name: A
>            -> main.cpp:2:7 (28, 0-76) kind: ClassTemplate display
> name: A<T> usr: c:@CT>1#T at A definition
>            main.cpp:12:37 (169, 169-170) kind: TypeRef display name: T
>            -> main.cpp:8:20 (98, 89-99) kind: TemplateTypeParameter
> display name: T usr: c:main.cpp at 89 definition
> 
> I've tried out all the APIs I could find to look up the connection but
> I have been unsuccessful and had to resort to hacks to make it work.
> Is there way to do this? Is it a bug?

There is no way currently. From the compiler's perspective 'size' does not bind to any function until instantiation time, but libclang could try to do some extra lookups in clang_getCursorDefinition instead of giving up.
Please file a bug in bugzilla (and patches are welcome as always :-)

-Argyrios


> 
> Help would be greatly appreciated.
> 
> I really am very grateful for your excellent work on this project that
> allows me to bring my emacs c++ experience into the 21st century :-)
> 
> regards
> 
> Anders
> <clangtest.c><foo.cpp>_______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev





More information about the cfe-dev mailing list