[Reprint] C code reading tool (2)-ctags

Source: Internet
Author: User
Tags tagname
C code reading tool (2) --- ctags

 

(09:26:17) -- Chen Hua

The calltree tool was introduced in the last week. It helps you understand the code as a whole without having to get started with the source code. For example, starting from the main function, you can know the approximate branches of the code and several. c files that are important in the source code. After a rough understanding of the entire source code of the project, we need to analyze the implementation details of the function. In this way, we need to read some function bodies in the code in more detail. Next we will introduce two source code reading tools in Linux:

VIM:The tool edited in Linux, VIM and emace, known as the programmer's Yi tianjian and Tu longdao, have a lot of problems related to VIM on the Internet, and everyone is quite clear about it.

When we simply use Vim to edit the. c file, it will be a little troublesome to view a function implementation body called inside the function in a function. For example, if the setup () function is called in the main function, but the function is not in the same file as the main function, it is quite difficult to search for it using vim, next we will introduce ctags, a tool that can flexibly locate functions, variables, types, and struct.

 

Ctags:Is a software used to generate tags files,

Use: first enter the source code directory you need to read, and then use the ctags-R command to generate the Tag file. The-r option is similar to the general shell command to indicate recursive operations, in this way, a Tag file is generated for the subdirectories in the current directory.

The tags files in each directory mainly include the following lists:

Macro defined by # define

Enumerated variable value

Function Definition, prototype, and Declaration

Namespace)

Type Definition (typedefs)

Variables (including definitions and declarations)

Class, struct, Enum, and Union)

Class, structure, and union member variables or functions

From the above table, we can see that this includes a lot of content not reflected by the calltree tool.

Example:

Timer Controlconf. c/^ Isc_timer_t * timer; $/; "m struct: controlconnection file:

TimerInclude/named/client. h/^ Isc_timer_t * timer; $/; "m     Struct: ns_client

The above two variables indicate that the timer variables are defined in the above two fields, both of which are defined in the form of isc_timer_t * timer.

 

Gslb_find gslb. c/^ gslb_find (ns_client_t * client, isc_netaddr_t * ADDRP, dns_name_t * Name, $ /;"  F

Gslb_find_acl gslb. c/^ Gslb_find_acl (gslb_acl_t * ACLs, isc_netaddr_t * reqaddr) $ /;" F     File:

The above two functions indicate that the gslb_find function and the gslb_find_acl function are defined in the gslb. c file.

The following describes how to use the tags file in vim.

First, use Vim to open a. c file, and then use the: Tag func command to locate the function func. When using the tag command, you can also use the tab key for matching and downward switching.

: Tag:For example, if Vim opens the main. c file and runs the: Tag gslb_find command, it is directly located at the prototype of the gslb_find function in the galb. c file. When multiple matching points are met, for example, tag timer, you can select multiple matching points. For example:

# Pri kind tag             File

1 F M  Timer          Include/named/client. h

Struct: ns_client

Isc_timer_t *Timer;

2 FSM  Timer           Controlconf. c

Struct: controlconnection

Isc_timer_t * Timer;

Choice number (<enter> cancels ):

 

: TnextWhen we select the first one based on the above prompt, the cursor will locate isc_timer_t * of include/named/client. h *Timer;, and here we first check the next matching point, we use the: tnext command.

: TfirstWhen multiple matches exist, it is used to locate the first match;

: TlastWhen there are multiple matches, it is used to locate the last match.

: Tselect tagnameSimilar to tag tagname, all tagnames are displayed and provided to users.

CTRL +] And Ctrl + T

In the general source code reading process, the simplest way is to move the cursor over the variable name or function name, and then press "Ctrl +]", in this way, you can directly jump to the source file defined by this variable or function, and position the cursor to this line. After reading this function, you can use "Ctrl + T" to return to the original place. Even if you use "Ctrl +]" for n Different tags, pressing "Ctrl + T" for n times can return to the originally positioned optical punctuation, it returns the result based on the original path.

 

Appendix: in Linux, the source code reading tools include cssag, global, lxr, kscope, and sourcenav. I will not describe them one by one here. I have little access to other tools, however, you can try to compare these tools with tags later.

Related Article

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.