[cfe-dev] Find information for a function call in a function template

Borges, Matt via cfe-dev cfe-dev at lists.llvm.org
Thu May 7 07:22:54 PDT 2020


Hi,

I’m not sure whether I am running into a bug or using libclang wrong.  I’m trying to get function call locations and can’t get all the information for this code:

template< class T >
class MyClass
{
    public:
        MyClass( int a )
        {
        }

        void func1()
        {
        }
};

template< class T >
static void funcOk()
{
}

template< class T >
static void func()
{
    MyClass< T > b( 5 );
    funcOk< T >();
    b.func1();
}

int main()
//--------
{
    func< int >();
}

I’m using clang_visitChildren to traverse the AST.  For non-template functions and methods I can get all the information I need from the CXCursor_CallExpr cursor.  For the funcOk< T >() call I can get all the information from the CXCursor_OverloadedDeclRef cursor.  However, I can’t seem to get any information from the cursors visited for the b.func1() line.  I get a CXCursor_CallExpr but trying to get any information (for example using clang_getCursorReferenced) returns nothing.  I also don’t get any cursors indicating a call to the constructor of MyClass in the code above.  If I change func to not be a template function, for example,

static void func()
{
    MyClass< int > b( 5 );
    funcOk< int >();
    b.func1();
}

I get 3 CXCursor_CallExpr which have all the information I need (one for the constructor call, one for the funcOk call and one for b.func1).  If I introduce a compile time issue in the template version of the function, for example,

template< class T >
static void func()
{
    MyClass< T > b( 5 );
    funcOk< T >();
    b.func1( 10 ); // Not valid because func1 does not take any arguments.
}

I get an error from libclang when parsing and then checking the diagnostic information stating that there are too many arguments to func1.

So it seems that within libclang it has the information about the calls in this template function or else it wouldn’t be able to give the error when I introduce a bug yet I can’t seem to get the information I need.  Is this a bug or do I need to use a different API to get the information about the b.func1 call in this case?

Thanks a lot in advance,
Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200507/44b0f3f8/attachment-0001.html>


More information about the cfe-dev mailing list