Return an array with all calls to function function_name in string source_code. Preprocessor directives and C++ style comments ("//") in source_code are removed. >>> len(parse_function_calls("foo", "foo();bar();foo();bar();")) 2 >>> parse_function_calls("foo", "foo(1);bar(1);foo(2);
(function_name, source_code)
| 26 | |
| 27 | |
| 28 | def parse_function_calls(function_name, source_code): |
| 29 | """Return an array with all calls to function function_name in string source_code. |
| 30 | Preprocessor directives and C++ style comments ("//") in source_code are removed. |
| 31 | |
| 32 | >>> len(parse_function_calls("foo", "foo();bar();foo();bar();")) |
| 33 | 2 |
| 34 | >>> parse_function_calls("foo", "foo(1);bar(1);foo(2);bar(2);")[0].startswith("foo(1);") |
| 35 | True |
| 36 | >>> parse_function_calls("foo", "foo(1);bar(1);foo(2);bar(2);")[1].startswith("foo(2);") |
| 37 | True |
| 38 | >>> len(parse_function_calls("foo", "foo();bar();// foo();bar();")) |
| 39 | 1 |
| 40 | >>> len(parse_function_calls("foo", "#define FOO foo();")) |
| 41 | 0 |
| 42 | """ |
| 43 | assert type(function_name) is str and type(source_code) is str and function_name |
| 44 | lines = [re.sub("// .*", " ", line).strip() |
| 45 | for line in source_code.split("\n") |
| 46 | if not line.strip().startswith("#")] |
| 47 | return re.findall(r"[^a-zA-Z_](?=({}\(.*).*)".format(function_name), " " + " ".join(lines)) |
| 48 | |
| 49 | |
| 50 | def normalize(s): |