此插件解决的问题
Keil 插件 -- Python 代码
import chardet, sys, glob
import re
# 变量名字典 key--数据名 value--数据s类型
variable_dic = {}
# 报错变量名字典
error_dic = {}
def add_key(key, value):
# 若key存在
if key in variable_dic:
if variable_dic[key] == value+'extern':
variable_dic[key] = value
return
error_dic[key] = value
else:
variable_dic[key] = value
def compare_key(key, value):
# 若不存在, 则添加, 用extern标识
if key not in variable_dic:
variable_dic[key] = value + 'extern'
return
# 若key存在,值不相等,则报错
if key in variable_dic:
if value+'extern' == variable_dic[key] or value == variable_dic[key]:
return
if key not in error_dic:
error_dic[key] = value
# uint AAA
# uint AAA,BBB
# uint code AAA,BBB
def type_variale_name_split(row_text):
type_header = row_text.split()[0]
if 'xdata' in row_text or 'code' in row_text:
type_header = ' '.join(row_text.split()[0:2])
if ',' in row_text: # uchar data A,B uint a,c,d
for item in row_text.split(','):
T = list(map(str.strip, item.split()))[-1]
add_key(list(map(str.strip, item.split()))[-1], type_header)
else: # uchar data A uint B
row_text_list = row_text.split()
if 'xdata' in row_text or 'code' in row_text:
add_key(row_text_list[2].strip(),type_header)
else:
add_key(row_text_list[1].strip(),type_header)
# uint AA=0
# uint AA=0,BB=0
# uint xdata AAA=0
# uint xdata AAA[BBB]={0}
def type_variable_name_equal_split(row_text):
# 裁剪定义的数组
if '{' in row_text:
row_text = row_text.split('{')[0]
type_header = row_text.split()[0]
if 'xdata' in row_text or 'code' in row_text:
type_header = ' '.join(row_text.split()[0:2])
# uint AA=0,BB=0
# uint code AA=0,BB=0,CC=1
# uint code A=0;
if ',' in row_text:
for item in row_text.split(','):
type_header_variable = item.split('=')[0]
# uint AA
if type_header in type_header_variable:
add_key(type_header_variable.split(type_header)[1].strip(), type_header)
else: # AA
add_key(type_header_variable.strip(), type_header)
else: # uint AA=0
# uchar xdata B=0
type_header_variable_else = row_text.split('=')[0]
type_header_variable_else_list = list(map(str.strip, type_header_variable_else.split()))
add_key(type_header_variable_else_list[-1],type_header)
# extern uint AA;
# extern uchar xdata BB,CC;
def extern_type_variale_name_split(row_text):
row_text = row_text.split('extern')[1]
type_header = row_text.split()[0]
if 'xdata' in row_text or 'code' in row_text:
type_header = ' '.join(row_text.split()[0:2])
if ',' in row_text:
for item in row_text.split(','):
if type_header in item:
compare_key(item.split(type_header)[1].strip(), type_header)
else:
compare_key(item.strip(), type_header)
else:
compare_key(row_text.split(type_header)[1].strip(), type_header)
def format_file(filename, default_encoding='gb2312'):
file = ''
content = ''
try:
file = open(filename, 'rb')
content = file.read() # 以字节方式读取
except:
print("文件"+filename+"打开失败")
finally:
file.close()
source_encoding = chardet.detect(content)['encoding']
string = content.decode(source_encoding if source_encoding else default_encoding, 'ignore')
pattern_type = r"(?:\n\b(?:bit|uchar|uint|ulong|int|long|float|char|double)\b[^;)]+)"
pattern_extern = r"(\n(?:extern)\s+\b(?:bit|uchar|uint|ulong|int|long|float|char|double)\b[^;)]+)"
matches = re.findall(pattern_type, string)
# 变量名添加
for matche in matches:
# 剔除函数
if '(' in matche:
continue
# 剔除 =0
# print(matche+"-------")
matche = matche.lstrip('\n').strip()
if '=' in matche:
# uint AAA=0,B=0
type_variable_name_equal_split(matche)
else:
# uint AA
type_variale_name_split(matche)
# extern 变量名比对
matche_extern = re.findall(pattern_extern, string)
for matche_e in matche_extern:
# 剔除函数
if '(' in matche_e:
continue
# 剔除 = 0
matche_e = matche_e.strip()
extern_type_variale_name_split(matche_e)
if __name__ == "__main__":
try:
filename = sys.argv
filelist = sum(list(map(glob.glob, filename[1:])), []) #获取所有文件名
print('检测到的文件:')
for item in filelist:
print(item)
list(map(format_file, filelist))
if not error_dic:
print('未查询到extern引用不匹配的')
else:
print('以下是变量类型未统一的')
for k,v in error_dic.items():
print(f'{k} --- {v}')
except Exception as e:
print(str(e))
需要修改的地方
上面代码,所需要的包,自行导入
Python 生成 exe
pip install pyinstaller -- 自行安装包
pyinstaller -Fw ./Incorrect_Variable_Reference.py
Keil C51 插件安装
"$E*.c" "$E*.h" -- 表示当前的所有文件
效果
快捷键添加
不会改的,自行留言