Thursday, May 7, 2009

Tips for reading code


Using cscope instead of grep


In gcc code base, the definition of a function is always writen like:
int
some_func(int *)
{
...
}
So that when you wanner find a certain definition, you can use something like:
grep *.c ^some_func
This is not always convenient especially when you want to find definition in a code based not styled like gcc style. With cscope, you can do more...
  • Find global definition
  • Find functions calling a function
  • Find egrep pattern (I like this, it's super nice)
  • More...
Sometimes it doesn't work when using cscope finding definition in gcc.
Yes, this happens when you trying to find the definition of struct which marked by GTY(()), which is related to a sophisticated memory management techniques. But whenever their is a question, there is always a solution just not far away. We can simply find these definitions with cscope's finding egrep pattern method, then you can find patterns like struct GTY ((chain_next ("%h.next"))) loop. It's easy, hmm :)
Cscope supported emacs and it's integrated in a kind interface. But I think it'll be better if it can do something like etags works in emacs -- Go back as farther from where I start the find command as it can do... I like this very much, you may want to go back to your place after you read the definition's definition's definition...

Sometimes we need to use find+grep

Sometimes it's more convenient to using unix's super good cmd tool like find grep etc. to search the code base.
If you want to find in gcc/testsuite 's expect file (*.exp) a certain pattern, say DEFAULT_CFLAGS. You can simply use:
find . -iname "*.exp" -exec grep DEFAULT_CFLAGS {} \;

^L :)

No comments: