Alternatively, ctags can generate a cross reference file which lists, in human readable form, information about the various objects found in a set of C language files.
Tag index files are supported by the vi(1) editor and its derivatives (such as vim, elvis, stevie, and xvi) through the use of the ":ta" command, which locates the object associated with a name appearing in a source file and jumps to the file and line which defines the name. The following types of tags are supported by ctags:
macro definitions (i.e. names created by #define)
enumerated values (i.e. the values inside enum{...})
function definitions
function prototypes or declarations (optional)
class, enum, struct and union tags
typedefs
variables
Ctags only generates tags for objects which have global scoping (file-wide visibility). This means that, with the exception of macro definitions, only objects defined outside of brace enclosed function blocks are candidates for a tag. For every one of the qualified objects which are discovered in the source files supplied to ctags, a separate line is added to the tags file in the following format:
.
or line number- tag name (a C language identifier)
.- a single tab character
.- the name of the file in which the object associated with the tag is located
.- a single tab character
.- an Ex command to locate the tag within the file; generally either a search pattern (either /pattern/ or ?pattern?)
Note that, unless changed via -n or -N, line numbers are only used for tags from macro definitions, while patterns are used for all other tags.
Note that the name of each source file will be recorded in the tag file exactly as it appears on the command line. Therefore, if the path you specified on the command line was relative to some directory, then it will be recorded in that same manner in the tag file.
This version of ctags imposes no formatting requirements. Other versions of ctags tended to rely upon certain formatting assumptions in order to help it resolve coding dilemmas caused by preprocessor conditionals.
In general, ctags tries to be smart about conditional preprocessor directives. If a preprocessor conditional is encountered within a statement which defines a tag, ctags follows only the first branch of that conditional (except in the special case of "#if 0", in which case it follows only the last branch). The reason for this is that failing to pursue only one branch can result in ambiguous syntax, as in the following example:
#ifdef TWO_ALTERNATIVES
struct {
#else
union {
#endifshort a;}
long b;
Both branches cannot be followed, or braces become unbalanced and ctags would be unable to make sense of the syntax.
If the application of this heuristic fails to properly parse a file, generally due to complicated and inconsistent pairing within the conditionals, ctags will retry the file using a different heuristic which does not selectively follow conditional preprocessor branches, but instead falls back to relying upon a closing brace ("}") in column 1 as indicating the end of a block once any brace imbalance is detected within a #if conditional.
Ctags will also try to specially handle arguments lists enclosed in double sets of parentheses in order to accept the following conditional construct:
extern void foo __ARGS((int one, char two));
Any name immediately preceding the "((" will be automatically ignored and the previous name will be used.
After creating or appending to the tag file, it is sorted by the tag name, removing identical tag lines.
Note that the path recorded for filenames in the tag file and utilized by the editor to search for tags are identical to the paths specified for file(s) on the command line. This means the if you want the paths for files to be relative to some directory, you must invoke ctags with the same pathnames for file(s).
Note that spaces separating options from their parameters are optional.
In addition, the following two modifiers are accepted:
- P
- Prefix static tags (if included) in the tag file with the filename in which they appear, followed by a colon (Elvis style; not widely supported).
- S
- Include static tags (those not visible outside of a single source file). Function and variable definitions are considered static only when their definitions are preceded with the "static" keyword. All other types of tags are considered static when they appear in a non-header file (see the -h option).
The default value of list is "=defgtvS" (i.e all tag types except for function prototypes; include static tags but do not prefix them).
This feature is useful when preprocessor macros are used in such a way that they cause syntactic confusion due to their presence. Some examples will illustrate this point.
/* creates a global version string in module */ MODULE_VERSION("$Revision: 1.25 $")
In this example, the macro invocation looks to much like a function definition because it is not followed by a semicolon (indeed, it could even be followed by a global variable definition that would look exactly like a K&R style function parameter declaration). In fact, this seeming function definition would likely cause the rest of the file to be skipped over while trying to complete the definition. Ignoring "MODULE_ID" would avoid such a problem.
int foo ARGDECL2(void *, ptr, long int, nbytes)
In this example, the macro "ARGDECL2" would be mistakenly interpreted to be the name of the function instead of the correct name of "foo". Ignoring the name "ARGDECL2" results in the correct behavior.
.- Significantly reduces the size of the resulting tag file.
.- Eliminates failures to find tags because the line defining the tag has changed, causing the pattern match to fail (note that some editors, such as vim, are able to recover in many such instances).
.- Eliminates finding identical matching, but incorrect, source lines (see BUGS, below).
However, this option has one significant drawback: changes to the source files can cause the line numbers recorded in the tag file to no longer correspond to the lines in the source file, causing jumps to some tags to miss the target definition by one or more lines. Basically, this option is best used when the source code to which it is applied is not subject to change. See also the -N option. Selecting this option causes the following options to be ignored: -F, -B and -N.
Because ctags does not look inside brace enclosed function blocks, local declarations of class/enum/struct/union tags and enumeration values within a function will not have tags generated for them.
Note that because ctags generates search patterns for non-macro tags, it is entirely possible that the wrong line may be found by your editor if there exists another, identical, line (whose context prevents it from generating a tag) which is identical to the line containing the tag. The following example demonstrates this condition:
int variable;/* ... */
void foo(variable)
int variable;
{/* ... */}
Depending upon which editor you use and where in the code you happen to be, it is possible that the search pattern may locate the local parameter declaration in foo() before it finds the actual global variable definition, since the lines (and therefore their search patterns are identical). This can be avoided by use of the -n option.
Because ctags is neither a preprocessor nor a compiler, some complex or obscure constructs can fool ctags into either missing a tag or improperly generating an inappropriate tag. In particular, the use of preprocessor constructs which alter the textual syntax of C can fool ctags, as demonstrated by the following example:
#ifdef GLOBAL
#define EXTERN
#define INIT(assign) assign
#else
#define EXTERN extern
#define INIT(assign)
#endif
EXTERN BUF *firstbuf INIT(= NULL);
This looks too much like a declaration for a function called "INIT", which returns a pointer to a typedef "firstbuf", rather than the actual variable definition that it is, since this distinction can only be resolved by the preprocessor. The moral of the story: don't do this if you want a tag generated for it, or use the -I option to specify "INIT" as a keyword to be ignored.
http://fly.hiwaay.net/~darren/ctags.html
Also ex(1) , vi(1) , elvis, or, better yet, vim, the official editor of ctags. For more information on vim, see the VIM Pages web site at:
http://www.math.fu-berlin.de/~guckes/vim/
"All effort and exertion put forth by man from the fullness of his heart is worship, if it is prompted by the highest motives and the will to do service to humanity."
-- From the Baha'i Writings
Credit is also due Bram Moolenaar, the author of vim, who has devoted so much of his time and energy both to developing the editor as a service to others, and to helping the orphans of Uganda.