Search code examples
cindentationctags

Issue with ctags and indent (extract function definitions/declarations from source)


I can't really find something on google regarding this issue.

I want to parse (get all) function definitions and declarations from .c/.h files.

I know about ctags, cscope and clang (AST).

Since I liked the simplicity of ctags I decided to stick with that.

One problem that I encountered is that ctags doesn't output the whole parameter list if there is a line break in between them like so:

int my_function(
    int a, int b
);

Gives the output:

 $ ctags -x --c-kinds=fp so.c
 my_function      prototype     1 so.c             int my_function(

This can be remedied with a tool like indent:

 $ indent -kr so.c -o so-fixed.c
 $ ctags -x --c-kinds=fp so-fixed.c

 my_function      prototype     1 so-fixed.c       int my_function(int a, int b);

Ok, that works perfectly. Until you have to deal with variadic functions like:

int my_function(
    int a, int b, ...
);

Then the output of indent is no more usable to me:

int my_function(int a, int b, ...
    );

The bigger goal here is to cross-check parameter names defined in header files with the ones used in the actual implementation.

So that something like :

header.h

void my_function(int param);

impl.c

void my_function(int something_else) {
}

Would be caught.

Ultimately I know I could use clang with its AST output.

However, due to the complexity of the AST, this is something I want to avoid, if possible.


Solution

  • The bigger goal here is to cross-check parameter names defined in header files with the ones used in the actual implementation.

    For that, you can use clang-tidy with https://clang.llvm.org/extra/clang-tidy/checks/readability-inconsistent-declaration-parameter-name.html