【问题标题】:Issue with ctags and indent (extract function definitions/declarations from source)ctags 和缩进问题(从源代码中提取函数定义/声明)
【发布时间】: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


【解决方案1】:

这里更大的目标是将头文件中定义的参数名称与实际实现中使用的参数名称进行交叉检查。

为此,您可以将 clang-tidy 与 https://clang.llvm.org/extra/clang-tidy/checks/readability-inconsistent-declaration-parameter-name.html 一起使用

【讨论】:

    【解决方案2】:

    Universal Ctags 提供自定义外部参照输出的功能。

    $ 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,...)
    

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

    要根据需要格式化返回类型,您可能需要使用 sed。

    【讨论】:

    • 非常感谢。我去看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    相关资源
    最近更新 更多