仅提取函数名称及其来自C代码的注释

piy*_*ush 2 c perl awk comments sed

我想提取注释,并想知道它们是哪些函数.我有很多这样的C文件如下:

输入:

void main()
{
    //sdgs
    call A;
    /*
    sdfgs
    dfhdfh
    */
    call b;
    some code;
}

/* this function adds
 something */
int add()
{
    //sgsd
    some code;
    //more comments
    some code;
}
Run Code Online (Sandbox Code Playgroud)

输出应该是:

void main()
{
    //sdgs

    /*
    sdfgs
    dfhdfh
    */

}

/* this function adds
 something */
int add()
{
    //sgsd

    //more comments

}
Run Code Online (Sandbox Code Playgroud)

输入代码格式整齐,"功能代码" {在下一行后开始.基本上,我只需要知道哪个"评论"来自哪个函数.此外,它还应包括功能名称或其他地方的任何其他注释.注意:这是不同的,因为顶层的函数名称应该在那里.

为了简化我的要求:

  1. 打印所有评论
  2. 检测包含(在第一行上的块,其中一行仅包含{一到三行后的第一列,并打印上面的行.

orl*_*rlp 6

对于正则表达式,这是不可能的,您需要编写一个小C解析器.

为什么?

首先,需要首先替换宏.其次,因为函数定义在正则表达式中有点"难".一些法律功能定义:

int f() {}
const int f() {}
const char* f(int);
void f(double t);
void f(t,a) int t; int (*a)(float, char, char) {}
Run Code Online (Sandbox Code Playgroud)