11

I want to know how can I easily click (or maybe use some easy shortcuts) on a function name and find all its callee or open where it has been defined. Most of the web manuals in web are really hard to follow or don't happen to work out. Say I want to click on allocuvm and see where it has been defined?

uint newstk=allocuvm(pgdir, USERTOP-PGSIZE, USERTOP); 
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • 5
    For jumping to a definition, use *ctags*. For finding the callers of a function, use *cscope*. Google these two terms, you'll find plenty of info. – glts Oct 19 '13 at 20:31

3 Answers3

22

cscope minimal example

Ingo mentioned it, here is an example.

First you should set on your .vimrc:

set cscopequickfix=s-,c-,d-,i-,t-,e-

Then to the base directory of your project and run:

cscope -Rb

This generates a cscope.out file which contains the parsed information. Generation is reasonably fast, even for huge projects like the Linux kernel.

Open vim and run:

:cs add cscope.out
:cs find c my_func

c is a mnemonic for callers. The other cscope provided queries are also possible, mnemonics are listed under:

help cscope

This adds a list of the callers to the quickfix list, which you can open with:

:copen

Go to the line that interests you and hit enter to jump there.

To find callers of the function name currently under the cursor, add to your .vimrc:

function! Csc()
  cscope find c <cword>
  copen
endfunction
command! Csc call Csc()

and enter :Csc<enter> when the cursor is on top of the function.

TODO:

A word of advice: I love vim, but it is too complicated for me to setup this kind of thing. And it does not take into account classes e.g. in C++. If a project matters enough to you, try to get the project working on some "IDE". It may involve some overhead if the project does not track the IDE configuration files (which are auto-changing blobs that pollute the repo...), but it is worth it to me. For C / C++, my favorite so far was KDevelop 4.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 2
    `` (`ctrl-r ctrl-w`) will insert the word under the cursor in vim. That makes the `cs find c my_func` a bit easier to use. – pattivacek Jul 31 '17 at 13:33
  • @patrickvacek yes that is another option. But since `:cs find c ` is a lot of chars, I prefer to use a command with `` ;-) – Ciro Santilli OurBigBook.com Jul 31 '17 at 14:26
  • One thing is missing. After runnging `:cs find c my_func`, the results are not automatically added to quickfix. Running `copen` gives an empty quickfix window.You have to add `set cscopequickfix=s-,c-,d-,i-,t-,e-` in your `.vimrc` to get it to work. – Salahuddin Nov 18 '20 at 08:30
  • @Salahuddin thanks for the info, it must have been set in one of my plugins, added now. – Ciro Santilli OurBigBook.com Nov 18 '20 at 09:39
12

For that, Vim integrates with the cscope tool; see :help cscope for more information.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 1
    I found that cscope cannot find callees of a C++ method. The "find functions called by this function" command only works with C functions. In the C++ case it always gives me "This is not a C symbol" if I give ClassName::MethodName, while it gives me "Egrep y in this pattern:MethodName" if I just give the method name. However, the "Find functions calling this function" (caller) works for both C functions and C++ methods. – kakyo Jan 03 '14 at 05:47
-1

vi / . --- / is the search function in vi, and . will repeat the same command.

you could also use sed ( stream editor ) if it is a large file sed grep can get you the line numbers

read the man page