gpt4 book ai didi

linux - 使用脚本从函数中提取参数

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:59 24 4
gpt4 key购买 nike

我有一个函数原型(prototype)文件是这样的:

int func1(type1 arg, int x);

type2 funct2(int z, char* buffer);

我想创建一个脚本(bash、sed、awk 等)来打印

function = func1 // first argument type = type1// second argument type = int
function = func1 // first argument type = int// second argument type = char*

换句话说,标记每一行并打印函数名称和参数。此外,我想将这些标记保存为变量以便稍后打印它们,例如 echo $4

最佳答案

另一种方法是使用“-g”进行编译并读取调试信息。
This answer可能会帮助您阅读调试信息并找出函数参数(它是 Python,不是 bash,但我还是建议使用 Python 或 Perl 而不是 bash)。

由此产生的解决方案将比基于文本解析的任何解决方案都更加稳健。它将处理定义函数的所有不同方式,甚至处理宏中定义的函数等疯狂的事情。

为了更好地说服您(或者在您不相信时帮助您做对),这里列出了可能会破坏您的解析的测试用例:

// Many lines
const
char
*

f
(
int
x
)
{
}

// Count parenthesis!
void f(void (*f)(void *f)) {}

// Old style
void f(a, b)
int a;
char *b
{
}

// Not a function
int f=sizeof(int);

// Nesting
int f() {
int g() { return 1; }
return g();
}

// Just one
void f(int x /*, int y */) { }

// what if?
void (int x
#ifdef ALSO_Y
, int y
#endif
) { }

// A function called __attribute__?
static int __attribute__((always_inline)) f(int x) {}

关于linux - 使用脚本从函数中提取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3342125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com