ctags多行C函数原型

11
有没有办法让ctags处理C语言中的多行函数原型?
我已经搜索了一下,--fields=+S应该可以处理多行函数原型,但是我无法让它正常工作:
ctags -x --c-kinds=pf --fields=+S file

文件:

int 
foo(int
    x, int y
    );

ctags只会返回:

foo(int
(请注意,返回类型也缺失)
最终我想要获得类似于输出
int foo(int x, int y);
或者
int foo(int x, int y

--fields=+S 不是正确的方式吗?还有哪些ctags字段我漏掉了吗?总的来说有什么指导意见吗?

如果在ctags中没有相应的方法,有推荐的程序吗?(我目前正在看uncrustify)


编译器是应该具备这种功能的地方。它已经完成了预处理,手头上有语法树和符号表。现在是2018年,但仍然没有像20多年前DEC编译器中那样的功能(cc --proto foo.c > foo.h)被加入到gcc中,这太荒谬了。你甚至可以选择只生成静态或外部函数和/或变量来产生公共和私有的.h文件! - John Hascall
4个回答

1
--_xformat选项可能会对你有帮助。
[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)

1

我的代码也遇到了同样的问题,但我无法修改它。 当我使用--fields=+S参数时,似乎可以工作,因为我在标签文件中得到了一个额外的行。signature:部分包含函数的所有参数。

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 )


0

我无法使用ctags找到任何内容,因此我编写了一个Python脚本来重新排列我的文件,以便ctags可以捕获原型。

注意:我的代码有注释,因此大部分工作都是删除它们(否则它们会妨碍ctags)。

操作按照以下顺序进行:

# 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() + ' ')

-1

您可以使用简单的过滤器删除不必要的换行符,例如:

 tr '\n' ' '  | sed 's/\([{};]\)/\1\n/g'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接