好的,所以我有一堆 C 和 C++ 代码,我需要过滤并找到函数定义。我不知道函数类型/返回值,也不知道函数定义或函数调用中的参数数量等。
到目前为止,我有:
import re, sys
from os.path import abspath
from os import walk
function = 'msg'
regexp = r"(" + function + ".*[^;]){"
found = False
for root, folders, files in walk('C:\\codepath\\'):
for filename in files:
with open(abspath(root + '/' + filename)) as fh:
data = fh.read()
result = re.findall(regexp, data)
if len(result) > 0:
sys.stdout.write('\n Found function "' + config.function + '" in ' + filename + ':\n\t' + str(result))
sys.stdout.flush()
break
然而,这会产生一些不需要的结果。正则表达式必须是错误的,例如这些组合:
在say的所有突变中找到“msg”定义而不是“msg()”调用:
void
shapex_msg (struct shaper *s)
{
msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
s->bytes_per_second);
}
或者
void shapex_msg (struct shaper *s)
{
msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
s->bytes_per_second);
}
或者
void shapex_msg (struct shaper *s) {
msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
s->bytes_per_second);
}