如何从源文件中提取单个函数 [英] How to extract a single function from a source file
问题描述
I'm working on a small academic research about extremely long and complicated functions in the Linux kernel. I'm trying to figure out if there is a good reason to write 600 or 800 lines-long functions.
为此,我想找到一个可以从.c文件中提取函数的工具,以便可以对该函数运行一些自动化测试.
For that purpose, I would like to find a tool that can extract a function from a .c file, so I can run some automated tests on the function.
例如,如果我具有函数 <文件connect.c
中的c0> ,我正在寻找一种大致可以像这样工作的解决方案:
For example, If I have the function cifs_parse_mount_options()
within the file connect.c
, I'm seeking a solution that would roughly work like:
extract /fs/cifs/connect.c cifs_parse_mount_options
并返回函数的523行代码(!),从大括号到闭括号.
and return the 523 lines of code(!) of the function, from the opening braces to the closing braces.
当然,可以使用任何方式来操纵现有软件包,例如 gcc
来做到这一点.也是最有帮助的.
Of course, any way of manipulating existing software packages like gcc
to do that, would be most helpful too.
谢谢
Udi
Regex的答案退出C函数原型声明?让我相信,正则表达式匹配的函数声明绝非易事.
EDIT : The answers to Regex to pull out C function prototype declarations? convinced me that matching function declaration by regex is far from trivial.
推荐答案
为什么不编写一个小型的PERL/PHP/Python脚本,甚至不编写一个小型的C ++,Java或C#程序来做到这一点?
Why don't you write a small PERL/PHP/Python script or even a small C++,Java or C# program that does that?
我不知道有任何现成的工具可以做到这一点,但是编写代码以解析文本文件并从C ++代码文件中提取函数体应该不超过20行代码. 困难部分将定位函数的开头,使用RegEx应该是相对简单的任务.之后,您需要做的就是遍历文件的其余部分,以跟踪花括号的打开和关闭,并在到达函数主体的闭合括号时完成操作.
I don't know of any already-made tools to do that but writing the code to parse out the text file and extract a function body from a C++ code file should not take more than 20 lines of code.. The only difficult part will be locating the beginning of the function and that should be a relatively simple task using RegEx. After that, all you need is to iterate through the rest of the file keeping track of opening and closing curly braces and when you reach the function body closing brace you're done.
这篇关于如何从源文件中提取单个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!