Created
September 7, 2023 13:11
-
-
Save callmexss/c4279652b2e137dd9c7c525e3747560f to your computer and use it in GitHub Desktop.
extract c functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from pathlib import Path | |
import clang.cindex | |
def extract_functions(filename): | |
index = clang.cindex.Index.create() | |
tu = index.parse(filename) | |
functions = [] | |
for node in tu.cursor.walk_preorder(): | |
if node.kind == clang.cindex.CursorKind.FUNCTION_DECL: | |
functions.append(node.spelling) | |
return functions | |
def extract_functions_containing(filename, target): | |
index = clang.cindex.Index.create() | |
tu = index.parse(filename) | |
functions_containing_target = [] | |
for node in tu.cursor.get_children(): | |
if node.kind == clang.cindex.CursorKind.FUNCTION_DECL: | |
function_code = node.extent.start.file.name | |
with open(function_code, 'r') as f: | |
lines = f.readlines() | |
start_line, end_line = node.extent.start.line, node.extent.end.line | |
function_text = ''.join(lines[start_line - 1:end_line]) | |
if target in function_text: | |
functions_containing_target.append(function_text) | |
return functions_containing_target | |
def extract_functions_containing(code, cond): | |
functions = [] | |
stack = [] | |
func = "" | |
capture = False | |
# Split the code into lines | |
lines = code.split("\n") | |
# Regular expression to identify function signatures | |
func_signature_re = re.compile(r'\w+\s+\w+\s*\(.*\)\s*{') | |
for line in lines: | |
# Check for function signature | |
if func_signature_re.match(line): | |
capture = True | |
# Capture lines if inside a function | |
if capture: | |
func += line + "\n" | |
# Update stack based on braces | |
stack += [c for c in line if c == '{'] | |
for c in line: | |
if c == '}': | |
if stack: | |
stack.pop() | |
# Check if we have captured a complete function | |
if capture and not stack: | |
if cond(func): | |
functions.append(func) | |
func = "" | |
capture = False | |
return functions | |
# Sample C code as a string | |
sample_code = ''' | |
int foo(int a, int b) { | |
if (a > b) { | |
// Nested block | |
} | |
for (int i = 0; i < 100; i++) { | |
if (i % 2 == 0) { | |
printf("even"); | |
} | |
} | |
// This is function foo | |
return a + b; | |
} | |
void bar() { | |
// This is function bar | |
foo(1, 2); | |
} | |
int baz(int x) { | |
// This is function baz | |
return x * x; | |
} | |
''' | |
# Extract functions that contain the string "foo" | |
result = extract_functions_containing(sample_code, lambda x: "foo" in x) | |
print("Functions containing 'foo':") | |
for i, func in enumerate(result): | |
print(f"----------------{i}-----------------\n{func}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment