Wednesday, November 10, 2010

CScope and CTags

CSCOPE:
Cscope can be a particularly useful tool if you need to wade into a large code base. You can save yourself a lot of time by being able to do fast, targeted searches rather than randomly grepping through the source files by hand (especially since grep starts to take a while with a truly large code base).

Steps:
1. Get the source. First get the source code.
2. Figure out where you want to put your Cscope database files.
3. Generate cscope.files with a list of files to be scanned.

   find . -name '*.h' > cscope.files
   find . -name '*.cpp' >> cscope.files


4. Generate the Cscope database.
   cscope -b -q

The -b flag tells Cscope to just build the database, and not launch the Cscope GUI. The -q causes an additional, 'inverted index' file to be created, which makes searches run much faster for large databases.

5. Using the database
Append following line to your ~/.bashrc:
alias csd='cscope -d'

This tells Cscope not to regenerate the database. Otherwise you'll have to wait while Cscope checks for modified files, which can take a while for large projects, even when no files have changed. If you accidentally run 'cscope', without any flags, you will also cause the database to be recreated from scratch without the fast index or kernel modes being used, so you'll probably need to rerun your original cscope command above to correctly recreate the database.


Now, use command: csd


6. Regenerating the database when the source code changes.
If there are new files in your project, rerun your 'find' command to update cscope.files if you're using it.

Then simply invoke cscope the same way (and in the same directory) as you did to generate the database initially (i.e. cscope -b -q)


CTAGS:
Here are few useful tips. Refer to the manual page for more details using "man 1 ctags"

1. Build tags database:
ctags -R *
It creates a tags file

Create a tags file for Perl
ctags --languages=Perl -R

2. Add it to your editor (in ~/.vimrc file in my case):
set tags={Path to your view (since you might want to have separate tags file for each view)}/tags

3. Other options:
--exclude=[pattern] 
Add pattern to a list of excluded files and directories. This is used to avoid creating tags on specified files or files under directories.