添加简单宏处理
This commit is contained in:
@@ -191,6 +191,7 @@ class lex_class(object):
|
||||
self.token_list:list[lex_token]=[]
|
||||
self.token_buff=bytearray()
|
||||
self.file_name=file_name
|
||||
self.macro_table={}
|
||||
def save_char(self,c:int):
|
||||
self.token_buff.append(c&0xff)
|
||||
def save_token(self,token:lex_token):
|
||||
@@ -296,6 +297,32 @@ class lex_class(object):
|
||||
c=self.get_next_char()
|
||||
self.save_token(lex_token("string",self.token_buff,TOKEN_STRING,self.line,self.pos))
|
||||
return self.get_next_char()
|
||||
def deal_macro(self,buff:bytearray):
|
||||
self.macro_result=False
|
||||
sp=buff.decode('utf-8').split()
|
||||
if(len(sp)>0):
|
||||
if(sp[0]=='#define'):
|
||||
if(len(sp)>=3):
|
||||
if not (sp[1] in self.macro_table):
|
||||
self.macro_table[sp[1]]=' '.join(sp[2:])
|
||||
else:
|
||||
if not (sp[1] in self.macro_table):
|
||||
self.macro_table[sp[1]]=""
|
||||
elif(sp[0]=='#ifdef'):
|
||||
self.macro_result= (sp[1] in self.macro_table)
|
||||
return self.macro_result
|
||||
elif(sp[0]=='#if'):
|
||||
t=' '.join(sp[1:])# 判断条件比较复杂,暂时固定返回失败
|
||||
return self.macro_result
|
||||
elif(sp[0]=='#elif'):
|
||||
return self.macro_result
|
||||
elif(sp[0]=='#else'):
|
||||
self.macro_result= not self.macro_result
|
||||
return self.macro_result
|
||||
elif(sp[0]=='#endif'):
|
||||
return True
|
||||
else:
|
||||
return True
|
||||
|
||||
def lex(text:bytes,file_name:str=""):
|
||||
lex_obj = lex_class(text,file_name)
|
||||
@@ -326,15 +353,33 @@ def lex(text:bytes,file_name:str=""):
|
||||
raise Exception(f"符号 '\\' 必须在行末, {lex_obj.file_name}:{lex_obj.line},{lex_obj.pos}")
|
||||
elif isinstr(c,"#"): # 宏定义
|
||||
c_old=c
|
||||
buff=bytearray()
|
||||
while (c!=TOKEN("\n") and c!=-1):
|
||||
c=lex_obj.get_next_char()
|
||||
if(c_old==TOKEN('/') and c==TOKEN('*')):# 适配宏后面有注释的情况
|
||||
while not (c_old==TOKEN("*") and c==TOKEN("/")):
|
||||
while not (c_old==TOKEN("*") and c==TOKEN("/")) or c==-1:
|
||||
c_old=c
|
||||
c=lex_obj.get_next_char()
|
||||
if(c_old==TOKEN('\\') and c==TOKEN('\n')):# 适配多行
|
||||
elif(c_old==TOKEN('/') and c==TOKEN('/')):
|
||||
while not (c==TOKEN('\n') or c==-1):
|
||||
c=lex_obj.get_next_char()
|
||||
elif(c_old==TOKEN('\\') and c in [TOKEN('\n'),TOKEN('\r')]):# 适配多行
|
||||
c=lex_obj.get_next_char()
|
||||
else:
|
||||
buff.append(c_old&0xff)
|
||||
c_old=c
|
||||
if not (lex_obj.deal_macro(buff)): # 处理宏
|
||||
is_space=True
|
||||
while True:
|
||||
c=lex_obj.get_next_char()
|
||||
if(is_space and c==TOKEN('#')):
|
||||
break
|
||||
if(c==-1):
|
||||
break
|
||||
if not isspace(c):
|
||||
is_space=False
|
||||
elif(c==TOKEN('\n')):
|
||||
is_space=True
|
||||
elif isinstr(c,"/"):
|
||||
c=lex_obj.get_next_char()
|
||||
if(c==TOKEN("/")):
|
||||
@@ -848,13 +893,13 @@ def find_func_def(file_list:list,func_name_list:str):
|
||||
for item in file_list:
|
||||
sys.stdout.write('.')
|
||||
sys.stdout.flush()
|
||||
try:
|
||||
ack=check_func_def(item,func_name_list)
|
||||
if(ack):
|
||||
ret_list.append(item)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
err_list.append(item)
|
||||
# try:
|
||||
ack=check_func_def(item,func_name_list)
|
||||
if(ack):
|
||||
ret_list.append(item)
|
||||
# except Exception as e:
|
||||
# print(e)
|
||||
# err_list.append(item)
|
||||
return ret_list,err_list
|
||||
|
||||
# 找到指定后缀的文件
|
||||
@@ -879,18 +924,7 @@ def find_type(path:str,fix:str):
|
||||
|
||||
_out_text=sys.stdin.readlines()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 参数是扫描的目录列表
|
||||
if __name__=="__main__":
|
||||
file_list=[]
|
||||
for item in sys.argv[1:]:
|
||||
file_list+=find_type(item,'.c')
|
||||
# file_list=["./app/iot_plc_uart/iot_plc_demo.c"]
|
||||
print(f"there is {len(file_list)} .c file.")
|
||||
def get_func_list():
|
||||
func_list=[]
|
||||
for item in _out_text:
|
||||
key_str='undefined reference to `'
|
||||
@@ -902,6 +936,20 @@ if __name__=="__main__":
|
||||
func=item[index:index+index_end]
|
||||
if not (func in func_list):
|
||||
func_list.append(func)
|
||||
return func_list
|
||||
|
||||
|
||||
|
||||
|
||||
# 参数是扫描的目录列表
|
||||
if __name__=="__main__":
|
||||
file_list=[]
|
||||
for item in sys.argv[1:]:
|
||||
file_list+=find_type(item,'.c')
|
||||
# file_list=["./dtest/dtest3/kl3_core_mark/core_main.c"]
|
||||
print(f"there is {len(file_list)} .c file.")
|
||||
func_list=get_func_list()
|
||||
# func_list=['main']
|
||||
print(func_list)
|
||||
# find_func_def(['driver/src/hw3/efuse.c'],['efuse_get_d_bg_vbg_cntl'])
|
||||
ret_list,err_list=find_func_def(file_list,func_list)
|
||||
|
Reference in New Issue
Block a user