【发布时间】:2021-12-12 23:02:02
【问题描述】:
我在谷歌上找不到关于这个问题的信息。
我想从 .c/.h 文件中解析(获取所有)函数定义和声明。
我知道 ctags、cscope 和 clang (AST)。
因为我喜欢 ctags 的简单性,所以我决定坚持下去。
我遇到的一个问题是,如果它们之间有换行符,ctags 不会输出整个参数列表,如下所示:
int my_function(
int a, int b
);
给出输出:
$ ctags -x --c-kinds=fp so.c
my_function prototype 1 so.c int my_function(
这可以通过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);
好的,效果很好。直到您必须处理可变参数函数,例如:
int my_function(
int a, int b, ...
);
那么indent 的输出对我来说不再可用:
int my_function(int a, int b, ...
);
这里更大的目标是将头文件中定义的参数名称与实际实现中使用的参数名称进行交叉检查。
这样的话:
header.h
void my_function(int param);
impl.c
void my_function(int something_else) {
}
会被抓到。
最终我知道我可以将 clang 与它的 AST 输出一起使用。
但是,由于 AST 的复杂性,我希望尽可能避免这种情况。
【问题讨论】:
-
我目前看到两个解决此问题的选项:愚蠢地将所有
...替换为int variadic_fn_param之类的占位符或使用ctags获取函数名称并使用 clang 的 AST获取详细信息(如参数名称等) -
ctags 的目的是给出一个符号的位置,因此它只包含行的内容(因为行号可能会改变)。但最后我检查了一下,ctags 没有维护。
-
这是 stackoverflow.com/questions/1570917/… 的副本吗?
is no more usable to me:所以只需删除...之后的换行符,对sed -z 's/\.\.\.[[:space:]]*);/...);/'来说似乎微不足道。ross-check parameter names defined in header files with the ones used in the actual implementation.使用clang.llvm.org/extra/clang-tidy/checks/… -
@KamilCuk 感谢您的链接!我之前看过
clang-tidy,但找不到这样的支票。我认为这解决了我的问题 -
@KamilCuk 好的,看起来我已经按照我的意愿工作了。如果您发表评论作为答案,我会接受! :-)
标签: c indentation ctags