clang_complete icon indicating copy to clipboard operation
clang_complete copied to clipboard

Add "follow reference" to go to typedef

Open Mortal opened this issue 7 years ago • 1 comments

Currently, clang_complete does not support following definitions very well in code like this:

template <typename T, typename A>
struct factory {
	A value;
	factory(A value) : value(value) {}
	T construct() { return T(value); }
};

struct Foo {
	Foo(int) {}
};

typedef factory<Foo, int> foo;

struct Bar {
	Bar(float) {}
};

typedef factory<Bar, float> bar;

int main() {
	auto x = foo(42).construct();
	auto y = bar(123.0).construct();
	return 0;
}

I want to go from foo in main() to the typedef factory<Foo, int> foo; line. However, C-] (or g:ClangGotoDeclaration) goes straight through the typedef to the definition of factory::factory, which is useless. After some print-debugging, it seems that in function gotoDeclaration, neither cursor.get_definition() nor cursor.referenced returns a cursor to the typedef; both return cursors to the constructor. From my limited experimentation it seems to only occur when the typedef is for a templated type.

(Of course, this is a trivial example. For a challenge, I dare you to try finding the definition of buffer used in this function. clang_complete currently can only take you to something called pipe_middle which is not what you want. grep is useless since the word buffer is used all over the codebase.)

Fortunately, cursor.type.get_declaration() goes to the typedef. This patch adds support for going to that cursor and thus fixes the problem.

I've added map \g :call g:ClangGotoDeclaration()<CR> to my vimrc (mimicking jedi-vim's function for that binding, and now I can go to the typedef using that binding.

Mortal avatar Mar 01 '17 16:03 Mortal

That's the function I'm always dreaming of. It's especially important for me to viewing qemu code (tons of typedef in a seperate header)

In fact I has already modified my local libclang.py to do extra type checks and follow canonical type to get its declaration.

Can't wait to see this get merged.

adam900710 avatar May 12 '17 01:05 adam900710