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

Time:12-13

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.

CodePudding user response:

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

CodePudding user response:

Universal Ctags provides a feature customizing the xref output.

$ cat /tmp/bar.c
int f(char /* int */ a,
      // int b, ...
          int
          *c,
          ...
      )
  ;
$ ~/bin/ctags -x --_xformat="%-16N %-10K %4n %-16F %{typeref} %{name}%{signature}" --kinds-C= p -o - /tmp/bar.c |sed -e 's/typename://'
f                prototype     1 /tmp/bar.c       int f(char a,int * c,...)

See also https://docs.ctags.io/en/latest/output-xref.html?highlight=_xformat#xformat

For formatting the return type as you need, you may want to use sed.

  • Related