ctags multi-line C function prototypes

别来无恙 提交于 2019-12-03 06:14:11

I had the same issue with my code but I couldn't modify it. When I use the --fields=+S parameter, it seem to work because I get an additional line in the tag file. The signature: part contains all the parameters of the function.

CopyToTX26 D:\BiseL\My Dropbox\Work\0_BSW\PKG_CMD\Memory.c /^void CopyToTX26( uint16 memory_ID, uint32 ** pt_data_address, uint32 nb_recover, $/;" f signature:( uint16 memory_ID, uint32 ** pt_data_address, uint32 nb_recover, uint32 * nb_copied, uint32 max_length )

I was unable to find anything with ctags so I wrote a python script to re-arrange my file so that ctags could capture the prototypes.

Note: my code had comments and so much of this takes care with removing them (otherwise they would get in the way of ctags).

Manipulations were done in this order:

# concat to one line
file_str = ''
for line in read_from.readlines():
    file_str += line

# remove all /* */ comments
file_str = re.sub('/\*(.|[\r\n])*?\*/', '', file_str)

# remove '//' comments
file_str = re.sub('//.*', '', file_str)

# split on '\n'
file_as_list = file_str.splitlines(True)

# strip everything
for index in range(len(file_as_list)):
    file_as_list[index] = file_as_list[index].strip()

# add in newlines where appropriate
for line in file_as_list:
    # if the line ends in ';' or '}'
    if line.endswith(';') or line.endswith('}'):
        # append a newline to the stripped line
        write_to.write(line.strip() + '\n')
    else:
        # append a space to the stripped line
        write_to.write(line.strip() + ' ')

--_xformat option may help you.

[jet@localhost ~]$ cat /tmp/foo.h
int 
foo(int
    x, int y
    );
[jet@localhost ~]$ ~/var/ctags/ctags -x --c-kinds=pf --_xformat="%-16N %4n %-16F %C %S" /tmp/foo.h
foo                 2 /tmp/foo.h       foo(int (int x,int y)

You can remove unneeded line breaks with a simple filter, e.g.

 tr '\n' ' '  | sed 's/\([{};]\)/\1\n/g'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!