Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "follow reference" to go to typedef #519

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Mortal
Copy link

@Mortal Mortal commented Mar 1, 2017

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.

@adam900710
Copy link

adam900710 commented May 12, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants