Search code examples
ctagsexuberant-ctags

How could I generate proper tags file for the c source as below using ctags?


I am using Exuberant Ctags v5.8 and the C source sample that I would like to generate tags file from is as below, thanks!

#define PACK(x) __packed x                                                                                                                                                                                  

typedef PACK(struct) {
    int a;
    int b;
    int c;
} my_struct;

PACK(void *) my_func(PACK(void *) var1, int var2)
{
    *var1 = var2;
    return var1;
}

And this is the contents of generated tag file:

!_TAG_FILE_FORMAT   2   /extended format; --format=1 will not append ;" to lines/ 
!_TAG_FILE_SORTED   1   /0=unsorted, 1=sorted, 2=foldcase/ 
!_TAG_PROGRAM_AUTHOR    Darren Hiebert  /[email protected]/ 
!_TAG_PROGRAM_NAME Exuberant Ctags  // 
!_TAG_PROGRAM_URL   http://ctags.sourceforge.net    /official site/ 
!_TAG_PROGRAM_VERSION   5.8 // 
PACK    test.c  /^#define PACK(/;"  d   file: 
PACK    test.c /^typedef PACK(struct) {$/;" f

while my expected contents of tag file should be as following:

!_TAG_FILE_FORMAT   2   /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED   1   /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_AUTHOR    Darren Hiebert  /[email protected]/
!_TAG_PROGRAM_NAME  Exuberant Ctags //
!_TAG_PROGRAM_URL   http://ctags.sourceforge.net    /official site/
!_TAG_PROGRAM_VERSION   5.8 //
a   test.c  /^    int a;$/;"    m   struct:__anon1  file:
b   test.c  /^    int b;$/;"    m   struct:__anon1  file:
c   test.c  /^    int c;$/;"    m   struct:__anon1  file:
my_func test.c  /^void * my_func(void * var1, int var2)$/;" f
my_struct   test.c  /^} my_struct;$/;"  t   typeref:struct:__anon1  file:

How can I get the latter tag file from original C source file? Thanks!


Solution

  • This cannot be done directly without preprocessing the files yourself (e.g. with awk, as I commented above) and having Exuberant Ctags operate on the preprocessed version.

    Exuberant Ctags uses the -I command line option to handle defines. It allows you to ignore preprocessor defines (e.g -I PACK), do substitution of defines (e.g. -I PACK=FOO), and it allows you to ignore the identifier and its argument list (e.g. -I PACK+), but none of these options handle replacing the macro with one of its arguments.

    You might consider requesting this as a new feature, but it doesn't appear to be actively developed anymore. The last revision was in July 2009.