Ok so i have a bunch of C and C++ code which i need to filter through and find function defenitions. I don't know the function type/return value and i don't know the number of parameters etc in the function defenition or function calls.
So far i have:
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
This however, produces some unwanted results. The regexp must be fault taulrant for example these combinations:
Finding "msg" defenition but not "msg()" calls in all mutations of say:
void
shapex_msg (struct shaper *s)
{
msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
s->bytes_per_second);
}
or
void shapex_msg (struct shaper *s)
{
msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
s->bytes_per_second);
}
or
void shapex_msg (struct shaper *s) {
msg (M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
s->bytes_per_second);
}