-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_functions.py
79 lines (60 loc) · 2.38 KB
/
get_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import tree_sitter_c as tsc
from tree_sitter import Language, Parser
def extract_functions_from_c_file(file_path):
# Initialize the C language parser
C_LANGUAGE = Language(tsc.language())
# Create a parser instance
parser = Parser(C_LANGUAGE)
# Read the C file
with open(file_path, 'r') as f:
code = f.read()
# Parse the C file
tree = parser.parse(bytes(code, 'utf8'))
# Function to extract function nodes
def extract_functions(node):
functions = []
if node.type == 'function_definition':
functions.append(node)
for child in node.children:
functions.extend(extract_functions(child))
return functions
# Extract function nodes
root_node = tree.root_node
functions = extract_functions(root_node)
# Extract and return function snippets
function_snippets = []
for func in functions:
start_byte = func.start_byte
end_byte = func.end_byte
snippet = code[start_byte:end_byte]
# Extract function name
declarator = func.child_by_field_name('declarator')
if declarator is not None:
for child in declarator.children:
if child.type == 'identifier':
function_name = child.text.decode('utf-8')
function_snippets.append((function_name, snippet))
break
return function_snippets
def get_function_snippet(file_path, function_name)->str:
'''
Obtain the function snippet for the specified function name from the given C file
Note: If the function is not found, return a space character.
'''
# Extract all function snippets from the file
function_snippets = extract_functions_from_c_file(file_path)
# Filter and return the snippet for the specified function name
for name, snippet in function_snippets:
if name == function_name:
return snippet
# print(f"Function {function_name} not found in {file_path}")
return ' '
if __name__ == "__main__":
# Example usage:
file_path = './rca/test_parser.c'
function_snippets = extract_functions_from_c_file(file_path)
for name, snippet in function_snippets:
print(f"Function Name: {name}\n{snippet}\n")
func_name = 'add'
res = get_function_snippet(file_path, func_name)
print(f"Snippet for {func_name}:\n{res}")