The use of Cscope

Source: Internet
Author: User
Tags function definition egrep

reproduced from: http://blog.csdn.net/dengxiayehu/article/details/6330200


First set up the Cscope index file under the directory
Find-name ' *.c ' > Cscope.file
Cscope-rbkq
This command generates three files: Cscope.out, Cscope.in.out, Cscope.po.out.
Where Cscope.out is the basic symbolic index, the latter two files are generated using the "-q" option to speed up indexing of cscope.
The command arguments used above have the following meanings:

-r: Searching for code in a subdirectory tree when generating index files

-B: Only index files are generated and do not enter the Cscope interface

-K: Do not search the/usr/include directory when generating index files

-Q: Generate Cscope.in.out and cscope.po.out files to speed up cscope indexing
Next, you can read the code in vim.
No
The class, function definition, and invocation relationships of C + + cannot be found during use. After a careful reading of Cscope's manual, it was found that the original Cscope when the index file was generated, only the search type was
C, Lex and yacc files (suffix named. C,. h,. L,
. y), the files for C + + do not generate indexes at all. However, according to the instructions in the manual, Cscope supports the C + + and Java language files. Then follow the methods provided in the Cscope manual to produce a list of files and then let Cscope index each file in the list. For ease of use, the following script was written to update the index files for Cscope and ctags:

#!/bin/sh


Find. -name "*.h"-o-name "*.c"-o-name "*.cc" > Cscope.files

Cscope-bkq-i Cscope.files

Ctags-r
This script, first using the Find command, finds all the files in the current directory and subdirectories named ". H", ". C" and ". CC", and redirects the lookup results to the file cscope.files.
Cscope then generates the symbol index file based on all the files in the cscope.files.
The last command uses the Ctags command, generates a tags file, in vim executes ": Help tags" command query its usage. It can be used with cscope.
-r: Searching for code in a subdirectory tree when generating index files
-B: Only index files are generated and do not enter the Cscope interface
-Q: Generate Cscope.in.out and cscope.po.out files to speed up cscope indexing
-K: Do not search the/usr/include directory when generating index files
-I: If you save the file list with a filename that is not cscope.files, you need to add this option to tell Cscope where to find the source file list. You can use "-" to indicate that a list of files is obtained from standard input.
-I dir: Find header file in the directory indicated by-i option
-U: Scans all files, rebuilds the cross index files
-C: Ignore case when searching
-P Path: path in front of a file represented by a relative path, so you can use it without switching to the directory where your database files are located.
3 reading code in VIM
Using Cscope in Vim is very simple, first call the "cscope Add" command to add a cscope database, and then you can call the "cscope find" command to look up. VIM supports 8 kinds of cscope query functions, as follows: For example, we want to find a function in the code to call the work () function, we can enter: ": CS Find C Work", carriage return found no matching function, may not have function call work (). We then enter ": CS find s work", looking for where this symbol appears, now vim lists all the locations where this symbol appears. We can also do a string lookup, which can be found in double quotes or in single quotes. You can also enter a regular expression, which is similar to the functionality of the EGREP program.
S: Find the C language symbol, that is, find the function name, macro, enumeration value, etc. where it appears
G: Find locations defined by functions, macros, enumerations, and so on, similar to the functionality provided by Ctags
D: Find the function called by this function
C: Find functions that call this function
T: Find the specified string
E: Find Egrep mode, which is equivalent to Egrep function, but the search speed is much faster
F: Find and open files, similar to the VIM found function
I: Find the article containing this document
CS Help
Find's Options
0 or S: find this symbol
1 or G: find this definition
2 or D: Find the function called by this function
3 or C: Find the function that calls this function
4 or T: Find this string
6 or E: find this egrep mode
7 or F: find this file
8 or I: Find a file containing this file
After the heat can be used in Vim cscope, the specific use of the method reference




1, Cscope introduction

Cscope is similar to ctags, but it can be thought that she is an enhanced version of Ctags, because she can do more than ctags. In Vim, by Cscope query, jump to the specified place like jump to any tag; she can save the tag stack, so with the appropriate keyboard mapping bindings, you can jump backwards or forwards in the function, just as you would normally use tags.

The first time you use Cscope, he generates a symbolic database from the source file. Then, in future use, Cscope rebuilds the database only if the source file has changed or the source file list is different. When you rebuild the database, the database information for the unchanged file is copied from the old database, so the rebuild database is faster than the first new database.

When you lower the command line with Cscope, you get a full-screen selection window that allows you to query for specific content. However, once your query has a match, you will edit the source file with your default editor, but you cannot simply use CTRL +] or the tag command to jump from one tag to another.

The Cscope interface in Vim is completed by calling as a command line, and then resolves the results returned by the query. The end result is cscope query results like the usual tags, so you can jump freely, just like in the use of the usual tags (ctrl +] OR: Tag jump.

2, cscope related orders

All Cscope commands pass parameter options to the primary cscope command ": Cscope". Her shortest abbreviation is ": cs". The ": Scscope" command also does the same thing and separates the window horizontally ("SCS").

The available abbreviations are:

Add : Add a new Cscope database/link library

How to use:

: cs Add {File|dir} [Pre-path] [flags]

which

[Pre-path] is a file path that is passed to cscope with the-p option and is a relative path file

Before you add the path, so you do not switch to the directory where your database files can also use it.

[Flags] The extra flag you want to pass to Cscope

Instance:

: Cscope add/root/code/vimtest/ftpd

: Cscope Add/project/vim/cscope.out/usr/local/vim

: Cscope Add Cscope.out/usr/local/vim–c

Find: Query cscope. All Cscope query options are available in addition to the number 5 ("Modify this matching pattern").

How to use:

: CS Find {QueryType} {name}

which

{QueryType} corresponds to the actual Cscope line interface number and also corresponds to the NVI command:

0 or s--find this C symbol

1 or g--to find this definition

2 or d--to find functions that are called by this function (members)

3 or c--to find the function that calls this function (members)

4 or t--find this string

6 or e--find this egrep matching pattern

7 or f--find this file

8 or i--to find the file #include this file (they)

Example: (#号后为注释)

: Cscope find C Ftpd_send_resp # finds all functions that call this function (members)

: Cscope Find 3 Ftpd_send_resp # Just like the results above

: Cscope find 0 Ftpd_check_login # look ftpd_check_login this symbol

The results of the implementation are as follows:

Cscope Tag:ftpd_check_login

# line Filename/context/line

1 Ftpd.h <<GLOBAL>>

#define FTPD_CHECK_LOGIN ()/

2 648 ftpd.c <<ftpd_do_pwd>>

Ftpd_check_login ();

3 661 FTPD.C <<ftpd_do_cwd>>

Ftpd_check_login ();

4 799 FTPD.C <<ftpd_do_list>>

Ftpd_check_login ();

5 856 FTPD.C <<ftpd_do_nlst>>

Ftpd_check_login ();

6 931 FTPD.C <<ftpd_do_syst>>

Ftpd_check_login ();

7 943 FTPD.C <<ftpd_do_size>>

Ftpd_check_login ();

8 960 FTPD.C <<ftpd_do_dele>>

Ftpd_check_login ();

9 981 ftpd.c <<ftpd_do_pasv>>

Ftpd_check_login ();

Enter nr of choice (<CR> to abort):

Then enter the first sequence number.

Help: Displays a short summary.

How to use:

: CS Help

Kill: Destroy a cscope link (or kill all cscope links)

How to use:

: cs Kill {Num|partial_name}

To kill a cscope link, the link number or a partial name must be specified. Partial name

It is easy to say that it is part of the Cscope database file path. Be especially careful with a partial path to kill a cscope link.

If the specified link number is-1, then all cscope links will be killed.

Reset: Reinitialize all the cscope links.

How to use:

: CS Reset

Show: Display links to Cscope

How to use:

: CS Show

If you are using Cscope, you can also use Ctags,|:cstag| to allow you to specify a lookup from one or the other before you jump. For example, you can choose to search from the Cscope database first, and then look for your tags file (generated by ctags). The order of the above execution depends on the value of the |csto|.

|:cstag| when looking up the identity from the Cscope database characters equivalent to ": CS Find G".

|:cstag| when you look for identification from your tags file characters equivalent to "|:tjump|".

3, cscope option

Use the |:set| command to set all the options for Cscope. Ideally, you can do this in your startup file (for example:. VIMRC). Some cscope related variables only in |. The vimrc| is legal. It doesn't make any difference if Vim is started and then set up.

'cscopeprg' specifies the command to execute the CSCPOE. The default is "Cscope". For example:

: Set Csprg=/usr/local/bin/cscope

'cscopequickfix' Specifies whether to use the Quickfix window to display the Cscope results. This is a comma-separated set of values. Each item is contained in the |csope-find| command (S, G, D, C, T, E, F, or I) and the flag (+,-or 0).

' + ' indicates that the display results must be appended to the Quickfix window.

'-' implies emptying previous display results, ' 0 ' or not setting means not using the Quickfix window. The lookup will appear from the beginning until the first command. The default value is "" (Do not use the Quickfix window). The following values seem to be useful: "s-,c-,d-,i-,t-,e-".

If 'cscopetag' is set, then commands such as ": Tag" and "Ctrl +" and "vim-t" always use |:cstag| instead of the default: Tag behavior. By setting up ' CST ', you will always find both Cscope database and tags file. The default is off, for example:

: Set CST

: Set NOCST

'csto'

The value of ' csto ' determines the order in which |:cstag| performs the lookup. If ' Csto ' is set to 0, then cscope data will be prioritized, if Cscope does not return a match, then the tag file will be found. Conversely, the lookup order is reversed. The default value is 0, for example:

: Set csto=0

: Set Csto=1

If 'cscopeverbose' is not set (by default), it will not display information that represents execution success or failure when adding a cscope database. Ideally, you should be in your | before adding a cscope database. Reset this option in vimrc|, set it up after adding it. Since then, when you add more databases to Vim, you will get (hopefully) information to show that the database has failed to increase. For example:

: Set Csverb

: Set Nocsverb

The value of 'cspc' determines how many portions of a file's path are displayed. The default value is 0, so the entire path will be displayed. Value of 1, then only the file name is displayed, without the path. The other values show different parts. For example:

: Set cspc=3

The last 3 sections of the file path will be displayed, including the file name itself.

4. How to use Cscope in Vim

The first step you need to make is to create a cscope database for your source files. In most cases, you can simply use "cscope–b".

Assuming you have a cscope database, you need to "add" the database to vim. That would create a cscope "link" and enable it to be used by VIM. You can do this in your. vimrc file, or do it manually after Vim starts. For example, to increase the database "Cscope.out", you can do this:

: CS Add Cscope.out

You can check the results of the above execution again by executing ": CS show". This will produce the following output:

# PID Database name prepend path

0 11453 Cscope.out <none>

Tips:

Due to Microsoft's RTL restrictions, the WIN32 version will show 0 instead of the true PID.

Once a cscope link is established, you can query cscope and the results will be fed back to you. Search by command ": CS find". For example:

: CS Find G Ftpd_check_login

It might get a little stupid to execute the above command.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.