[cfe-dev] libclang: diagnostics not reported for template instantiations

Ben Jackson via cfe-dev cfe-dev at lists.llvm.org
Mon Oct 26 15:09:07 PDT 2015


Hi there,

An issue was raised against ycmd that we were not reporting errors as a result of failed template instantiations. This is because, seemingly, libclang does not report diagnostics for template instantiations where instantiation fails (in the sense that the generated code is not valid).

A fairly canonical test case is as follows:

    template<typename T>
    void get_size(const T& t)
    {
        return t.size();
    }

    struct Test
    {
        void not_size() const;
    };

    int run_test()
    {
        Test t;

        get_size(t);

        return "not_an_int";
    }

Expected: a diagnostic reported at the line ‘get_size(t)’ as struct Test does not have a size()const method. However, no diagnostic is reported.

The ‘return “not_an_int”’ is just to prove that diagnostics reporting is working in general in the test case.

When running the above with ‘c-index-test -index-file’, grepping for ‘[diagnostic]’, the result is :

    $ ./bin/c-index-test -index-file ../llvm/tools/clang/test/Index/diagnostics-template-instantiation.cpp  | grep '\[diagnostic\]'
    ../llvm/tools/clang/test/Index/diagnostics-template-instantiation.cpp:18:12: error: cannot initialize return object of type 'int' with an lvalue of type 'const char [11]'
[diagnostic]: ../llvm/tools/clang/test/Index/diagnostics-template-instantiation.cpp:18:12: error: cannot initialize return object of type ‘int' with an lvalue of type 'const char [11]'

However, when running clang directly, we get the diagnostic for get_size(t):

     clang++ ../llvm/tools/clang/test/Index/diagnostics-template-instantiation.cpp
    ../llvm/tools/clang/test/Index/diagnostics-template-instantiation.cpp:18:12: error: cannot initialize return object of type 'int' with an lvalue of type 'const char [11]'
        return "not_an_int";
           ^~~~~~~~~~~~
    ../llvm/tools/clang/test/Index/diagnostics-template-instantiation.cpp:4:14: error: no member named 'size' in 'Test'
        return t.size();
           ~ ^
    ../llvm/tools/clang/test/Index/diagnostics-template-instantiation.cpp:16:5: note: in instantiation of function template specialization 'get_size<Test>' requested here
        get_size(t);
    ^
    2 errors generated.

Is this a bug or simply a limitation of the libclang interface? Or is there something required to get these diagnostics to report. Apologies, I must confess I have not looked into this in detail within clang.

Many thanks and kind regards,
Ben


More information about the cfe-dev mailing list