Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ctags chokes on source file with unbalanced braces due to #ifdef

I am using ctags to generate a tags file for a C project I am working on, but many functions are missing in the file. This appears to be caused by unbalanced braces in the source files due to using #ifdef. A (simplified) example:

#include <stdio.h>

struct mystruct {
        long member;
#ifndef _MSC_VER
}__attribute__ ((packed));
#else /* _MSC_VER */
};
#pragma pack(pop)
#endif /* _MSC_VER */

char* greeting_text(){
  return "Hello world\n";
}

int main( int argc, const char* argv[] ){
  char * greeting = greeting_text();
  printf(greeting);
  return 0;
}

This compiles and works flawlessly with gcc -Wall under Linux. However, if I parse it using ctags problem.c, the tags file only contains entries for mystruct -- the functions are missing.

ctags --verbose reports:

OPENING problem.c as C language file
problem.c: unexpected closing brace at line 8
problem.c: retrying file with fallback brace matching algorithm
OPENING problem.c as C language file
problem.c: unexpected closing brace at line 8

so apparently ctags does not like the preprocessor tricks in the file.

Is there a way to make ctags handle this?

The manpage of ctags even explicitly mentions this problem, but indicates ctags can work around this. However, this does not appear to work...

This is with Exuberant Ctags 5.8 (Debian package 1:5.8-4).

Edit:

I'm also interested in alternatives to ctags that handle these kinds of constructs.

like image 587
sleske Avatar asked Dec 07 '25 04:12

sleske


2 Answers

Because of the problems with ctags, I ended up using cscope instead.

While it's not perfect, it handles macros better than ctags, and it can integrate with vim just like ctags can (see http://vimdoc.sourceforge.net/htmldoc/if_cscop.html#:cscope ).

like image 104
sleske Avatar answered Dec 08 '25 18:12

sleske


I would try running the preprocessor (gcc -E) on the files before giving them to ctags. Whether this will produce a good result I am not certain, but it would be worth a try. Certainly all components of your code should appear then, but will ctags recognize the other-file references that gcc leaves in the output? Not sure.

like image 21
John Zwinck Avatar answered Dec 08 '25 17:12

John Zwinck