添加生成空函数的脚本
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -45,3 +45,4 @@ mfgtool/oem_tool/oem_tool
|
|||||||
mfgtool/*.o
|
mfgtool/*.o
|
||||||
out/
|
out/
|
||||||
src_files.txt
|
src_files.txt
|
||||||
|
__pycache__/
|
||||||
|
162
create_empty_fun.py
Normal file
162
create_empty_fun.py
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import shutil
|
||||||
|
from find_func_def import find_type
|
||||||
|
from find_func_def import node
|
||||||
|
from find_func_def import lex
|
||||||
|
from find_func_def import node_file
|
||||||
|
from find_func_def import find_sentence
|
||||||
|
from find_func_def import dist_node_type
|
||||||
|
|
||||||
|
|
||||||
|
# 找到函数声明
|
||||||
|
def find_func_decl_in_file(n:node,deep:int):
|
||||||
|
ret=[]
|
||||||
|
if(n.type=='func_decl'):
|
||||||
|
print(f"{n.type} {n.name}")
|
||||||
|
ret.append(n.name)
|
||||||
|
return ret
|
||||||
|
# n.complite()
|
||||||
|
if (not n.child is None) and len(n.child)>0:
|
||||||
|
for item in n.child:
|
||||||
|
ret+=find_func_decl_in_file(item,deep+1)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
# 找到文件中的所有函数声明
|
||||||
|
def find_func_decl(file_name:str):
|
||||||
|
with open(file_name,mode='rb') as f:
|
||||||
|
read_d=f.read()
|
||||||
|
if(read_d[:3]==bytes([0xef,0xbb,0xbf])):
|
||||||
|
read_d=read_d[3:]
|
||||||
|
token_list=lex(read_d,file_name)
|
||||||
|
file=node_file(name=file_name,token_list=token_list)
|
||||||
|
while len(token_list)>0:
|
||||||
|
node_d=None
|
||||||
|
try:
|
||||||
|
sentence=find_sentence(token_list)
|
||||||
|
node_d=dist_node_type(sentence)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"in {file_name}")
|
||||||
|
print(f"\t {e}")
|
||||||
|
break
|
||||||
|
if not node_d is None:
|
||||||
|
file.child.append(node_d)
|
||||||
|
token_list=token_list[len(sentence):]
|
||||||
|
# print_node(file,0)
|
||||||
|
return find_func_decl_in_file(file,0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 找到函数定义
|
||||||
|
def find_func_decl(file:str):
|
||||||
|
list_func=[]
|
||||||
|
with open(file,encoding="utf-8") as f:
|
||||||
|
list_str=f.readlines()
|
||||||
|
text=""
|
||||||
|
ignore=False
|
||||||
|
for i in list_str:
|
||||||
|
line=i.strip()
|
||||||
|
if(len(line)==0):
|
||||||
|
continue
|
||||||
|
if(line.find("#define")>=0):
|
||||||
|
ignore=True
|
||||||
|
continue
|
||||||
|
if(line[-1]=='\\'):
|
||||||
|
if(ignore):
|
||||||
|
continue
|
||||||
|
text+=line[:-1]
|
||||||
|
else:
|
||||||
|
if(ignore):
|
||||||
|
ignore=False
|
||||||
|
continue
|
||||||
|
if(line[-1]==';'):
|
||||||
|
text+=line[:-1]
|
||||||
|
par_start=text.find('(')
|
||||||
|
par_end=text.find(')')
|
||||||
|
if(par_start>0 and par_end>0 and par_start<par_end):
|
||||||
|
list_func.append(text)
|
||||||
|
text=''
|
||||||
|
return list_func
|
||||||
|
|
||||||
|
# 截取参数列表中的变量名
|
||||||
|
def split_par_name(par_str:str):
|
||||||
|
ret_str=""
|
||||||
|
if(par_str.count('*')>0):
|
||||||
|
ret_str=par_str.split("*")[1].strip()
|
||||||
|
else:
|
||||||
|
# print(par_str)
|
||||||
|
ret_str=par_str.split(" ")[1]
|
||||||
|
return ret_str
|
||||||
|
|
||||||
|
# 生成空函数的实现
|
||||||
|
def def_empty_fun(line:str):
|
||||||
|
# 删除多余空格
|
||||||
|
line=' '.join(line.split())
|
||||||
|
# print(line)
|
||||||
|
list_str=line.split('(')
|
||||||
|
ret_type=list_str[0].split(' ')[0]
|
||||||
|
fun_name=list_str[0].split(' ')[1]
|
||||||
|
param_str=list_str[1].split(')')[0]
|
||||||
|
params=[]
|
||||||
|
# 有","则至少有两个参数否则可能有一个参数,可能没有
|
||||||
|
if(param_str.count(',')>0):
|
||||||
|
params=param_str.split(',')
|
||||||
|
for i in range(len(params)):
|
||||||
|
params[i]=params[i].strip()
|
||||||
|
else:
|
||||||
|
t_str=param_str.strip()
|
||||||
|
if(len(t_str)>0)and(t_str!="void"):
|
||||||
|
params.append(t_str)
|
||||||
|
# print(fun_name,params)
|
||||||
|
params_num=len(params)
|
||||||
|
fun_str=""
|
||||||
|
fun_str+=f"{ret_type} {fun_name}("
|
||||||
|
for i in range(params_num):
|
||||||
|
fun_str+=params[i]
|
||||||
|
if(i<len(params)-1):
|
||||||
|
fun_str+=", "
|
||||||
|
fun_str+=")\n{\n"
|
||||||
|
# if(params_num-1>0):
|
||||||
|
# fun_str+=" uint32_t param[{d}];\n".format(d=params_num-1)
|
||||||
|
for i in range(params_num):
|
||||||
|
fun_str+=" (void){d2};\n".format(d2=split_par_name(params[i]))
|
||||||
|
if(ret_type!="void"):
|
||||||
|
fun_str+=" return 0;\n"
|
||||||
|
fun_str+="}\n\n"
|
||||||
|
# print(fun_str)
|
||||||
|
return fun_str
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
def ergodic_signal_fun(path):
|
||||||
|
fun_str=""
|
||||||
|
list_file=find_type(path,".h")
|
||||||
|
for i in list_file:
|
||||||
|
list_signal=find_func_decl(i)
|
||||||
|
for j in list_signal:
|
||||||
|
fun_str+=def_empty_fun(j)
|
||||||
|
return list_file,fun_str
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def create_empty_fun(file:str):
|
||||||
|
fun_str=''
|
||||||
|
func_list=find_func_decl(file)
|
||||||
|
for j in func_list:
|
||||||
|
fun_str+=def_empty_fun(j)
|
||||||
|
return fun_str
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
file_list=['driver/src/hw3/inc/ada_hw.h','driver/src/hw3/inc/mailbox_hw.h']
|
||||||
|
for item in file_list:
|
||||||
|
# find_func_decl(item)
|
||||||
|
text=create_empty_fun(item)
|
||||||
|
print(text)
|
||||||
|
|
||||||
|
|
@@ -922,10 +922,10 @@ def find_type(path:str,fix:str):
|
|||||||
# with open("build/build_log.log",mode="r",encoding="utf-8") as f:
|
# with open("build/build_log.log",mode="r",encoding="utf-8") as f:
|
||||||
# _out_text=f.readlines()
|
# _out_text=f.readlines()
|
||||||
|
|
||||||
_out_text=sys.stdin.readlines()
|
|
||||||
|
|
||||||
def get_func_list():
|
def get_func_list():
|
||||||
func_list=[]
|
func_list=[]
|
||||||
|
_out_text=sys.stdin.readlines()
|
||||||
for item in _out_text:
|
for item in _out_text:
|
||||||
key_str='undefined reference to `'
|
key_str='undefined reference to `'
|
||||||
index=item.find(key_str)
|
index=item.find(key_str)
|
||||||
|
Reference in New Issue
Block a user