Skip to content

Instantly share code, notes, and snippets.

@callmexss
Created September 7, 2023 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callmexss/c4279652b2e137dd9c7c525e3747560f to your computer and use it in GitHub Desktop.
Save callmexss/c4279652b2e137dd9c7c525e3747560f to your computer and use it in GitHub Desktop.
extract c functions
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