better completion for c++

by on
2 minute read

I have landed some improvements to Clang so that it can provide better code completion for C++:

YouCompleteMe GIF

This comes in Clang 3.7 revision 227309. The specific area improved regards parametric information that can be useful when inserting arguments.

This functionality can be easily tried in the command line using the c-index-test tool that comes with Clang. For example, for the following code:

#include <iostream>

template <class T>
class SadBart {
public:
    SadBart(T lesson) : lesson(lesson) {
    }

    template <class U>
    void operator()(std::ostream &os, U width, U height) {
        while (height --> 0) {
            auto w = width;
            while (w --> 0)
                if (w > 0)
                    os << lesson << ' ';
                else
                    os << lesson;
            os << '\n';
        }
    }

private:
    T lesson;
};

int main() {
    SadBart<int>(42)(std::cout, 4, 2);
}                ^   ^         ^  ^

these are the relevant results when running c-index-test using the four spots highlighted in the source code:

c-index-test -code-completion-at=playground.cpp:27:18 playground.cpp
OverloadCandidate:{Text SadBart}{LeftParen (}{CurrentParameter int lesson}{RightParen )} (1)
OverloadCandidate:{Text SadBart}{LeftParen (}{CurrentParameter const SadBart<int> &}{RightParen )} (1))})}

c-index-test -code-completion-at=playground.cpp:27:22 playground.cpp
OverloadCandidate:{ResultType void}{Text operator()}{LeftParen (}{CurrentParameter std::ostream &os}{Comma , }{Placeholder U width}{Comma , }{Placeholder U height}{RightParen )} (1)

c-index-test -code-completion-at=playground.cpp:27:32 playground.cpp
OverloadCandidate:{ResultType void}{Text operator()}{LeftParen (}{Placeholder std::ostream &os}{Comma , }{CurrentParameter U width}{Comma , }{Placeholder U height}{RightParen )} (1)

c-index-test -code-completion-at=playground.cpp:27:35 playground.cpp
OverloadCandidate:{ResultType void}{Text operator()}{LeftParen (}{Placeholder std::ostream &os}{Comma , }{Placeholder int width}{Comma , }{CurrentParameter int height}{RightParen )} (1))}

A new CXCursorKind is provided in the libclang API, CXCursor_OverloadCandidate, so that one can grab the results that refer specifically to the overloads that apply for the current context.

For more details about the Vim plugin check my YouCompleteMe fork.

For argument hints a little bit faster than official Clang take a look at this change I'm mantaining at a branch in my own fork. You can use it if you care about some milliseconds.

Knows someone that needs a programmer? Please drop me a line!

cpp, clang, libclang, completion, intellisense, vim, ycm
Spotted a mistake in this article? Why not suggest an edit!