I am trying to write a python script to extract starting line numbers of function definitions of a C program. I used a C parsing library in python, called "Pyclibrary", and using it to extract function names from my C file. I am then putting these names in a list, iterating through it, and searching line numbers where they are found, and deleting duplicates, by storing only the first instance of the search. But it fails for those cases where the first instance is not the function definition. I need to refine my logic for the same. Any leads would be appreciated.
Here is my code:
from pyclibrary import CParser
from pyclibrary import CLibrary
import pandas as pd
parser = CParser(['path/to/c/file/sample.c'])
my_list = []
list_of_func = []
d1 = []
d2 = []
d3 = []
func1 = parser.defs['functions']
inside_function = 0
left_brack_num = 0
for i in func1:
my_list.append(str(i))
with open('path/to/c/file/sample.c') as myFile:
for num, line in enumerate(myFile, 1):
for i in range(len(my_list)):
if my_list[i] in line:
list_of_func.append([my_list[i], num])
d1.append(my_list[i])
d2.append(num)
inside_function = 1
if inside_function == 1:
left_brack_num += line.count("{")
if "}" in line:
left_brack_num -= line.count("}")
if left_brack_num == 0:
d3.append(num)
inside_function = 0
Data ={'Function Name': d1, 'Starting Line number': d2}
df2d = pd.DataFrame(Data)
df2d.drop_duplicates(subset = 'Function Name',
keep = 'first', inplace = True)
snd = pd.Series(list_of_func)
print(df2d)