Keil C51 插件 检测变量名引用不统一

此插件解决的问题

Keil 插件 -- Python 代码

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 -- 自行安装包

python 复制代码
pyinstaller -Fw ./Incorrect_Variable_Reference.py

Keil C51 插件安装

"$E*.c" "$E*.h" -- 表示当前的所有文件

效果

不会改的,自行留言

相关推荐
叫我:松哥4 分钟前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap
Eiceblue35 分钟前
Python 复制Excel 中的行、列、单元格
开发语言·python·excel
NLP工程化1 小时前
对 Python 中 GIL 的理解
python·gil
Whappy0011 小时前
51单片机-DA(数字转模拟)
单片机·嵌入式硬件·51单片机
极客代码1 小时前
OpenCV Python 深度指南
开发语言·人工智能·python·opencv·计算机视觉
liO_Oil1 小时前
(2024.9.19)在Python的虚拟环境中安装GDAL
开发语言·python·gdal安装
Whappy0011 小时前
51单片机-AD(模拟信号转数字信号)-实验()
单片机·嵌入式硬件·51单片机
奈斯。zs1 小时前
yjs08——矩阵、数组的运算
人工智能·python·线性代数·矩阵·numpy
Melody20501 小时前
tensorflow-dataset 内网下载 指定目录
人工智能·python·tensorflow
学步_技术1 小时前
Python编码系列—Python抽象工厂模式:构建复杂对象家族的蓝图
开发语言·python·抽象工厂模式