How can I find the definition of a structure with cscope

1,388 views
Skip to first unread message

wiseker

unread,
Apr 29, 2008, 1:21:43 AM4/29/08
to vim...@googlegroups.com
In vim + cscope, we can use command ":cs find g name" to find the definition of a function or structure. But there are some problems for structures.

For example, when we want to find the definition of struct device in linux kernel tree,the result is:
# line filename / context / line
1 115 Documentation/lguest/lguest.c <<device>>
struct device
2 32 Documentation/spi/spidev_test.c <<device>>
static const char *device = "/dev/spidev1.1";
3 117 arch/x86/kernel/early-quirks.c <<device>>
u32 device;
4 48 arch/x86/kernel/reboot_fixups_32.c <<device>>
unsigned int device;
5 46 arch/x86/pci/irq.c <<device>>
u16 vendor, device;
6 53 arch/x86/pci/irq.c <<device>>
int (*probe)(struct irq_router *r, struct pci_dev *router, u16 device);\
......

That is because: 1) there are lots of "struct device" in header files (to avoid including headers); 2) Some locale variables with same name exists.

How can we avoid this and find the right definition quickly?

Thanks!

Gary Johnson

unread,
May 1, 2008, 4:05:59 PM5/1/08
to vim...@googlegroups.com

If you want to limit the scope of the search to just those files
included by the current source file, you may find what you're
looking for in

:help include-search

HTH,
Gary

Jason Lau

unread,
May 4, 2008, 9:27:49 PM5/4/08
to vim...@googlegroups.com

        Thanks!
 


HTH,
Gary




Mark Wilden

unread,
May 4, 2008, 11:08:11 PM5/4/08
to vim...@googlegroups.com
Unfortunately, I think the desire is actually the opposite - to search includes that -aren't- included in the current file. The current file includes a line like

struct foo;

and what's needed is to find the file that has

struct foo {
   // foo's definition
};

///ark

Baroudi Safwen

unread,
Jan 28, 2018, 1:48:52 AM1/28/18
to vim_use
I wrote a patch for vim that will sort the matches returned by cscope to put the matches that end with '{' at the top. I work on the kernel and boy this was useful. I'll be happy to share the patch if anyone is interested.

Dominique Pellé

unread,
Jan 28, 2018, 10:01:51 AM1/28/18
to Vim List
cscope and ctags use heuristics which cannot tell for sure what is the
correct definition.

Instead of those tools, you can use rtags and/or YouCompleteMe which
are based on
the clang library. Those tools understand c or c++ and can jump to the correct
definition.

See:

https://github.com/Valloric/YouCompleteMe

https://github.com/Andersbakken/rtags
https://github.com/lyuts/vim-rtags

Regards
Dominique
Reply all
Reply to author
Forward
0 new messages