Skip to content

Commit c6156d0

Browse files
committed
fix(parser): 增加 Keil 项目文件解析的错误处理
检查 Target 和 TargetOption 元素是否存在 检查 FileType 和 FilePath 元素是否存在并有效 检查 Define 和 IncludePath 元素是否存在 检查 AppData 环境变量是否存在
1 parent 055050d commit c6156d0

1 file changed

Lines changed: 43 additions & 12 deletions

File tree

main.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,36 @@ def parse_keil_project(file_path):
1616

1717
# 提取目标信息
1818
target = root.find('.//Target')
19+
if target is None:
20+
print("Error: Target element not found in Keil project file.")
21+
return []
1922

2023
# 提取编译选项
2124
target_option = target.find('.//TargetOption')
25+
if target_option is None:
26+
print("Error: TargetOption element not found in Keil project file.")
27+
return []
2228

2329
# 宏定义
2430
cads = target_option.find('.//Cads')
25-
define = cads.find('VariousControls/Define').text if cads.find('VariousControls/Define') is not None else ''
31+
define = ''
32+
include_path = ''
33+
if cads is not None:
34+
define_element = cads.find('VariousControls/Define')
35+
if define_element is not None and define_element.text is not None:
36+
define = define_element.text
37+
38+
include_path_element = cads.find('VariousControls/IncludePath')
39+
if include_path_element is not None and include_path_element.text is not None:
40+
include_path = include_path_element.text
41+
2642
macros = [f"-D{macro}" for macro in define.split(',') if macro]
2743

2844

2945
# 包含路径
3046
uvprojx_dir = os.path.dirname(os.path.abspath(file_path)) # Keil 项目文件所在目录
3147
output_dir = os.getcwd() # 假设 compile_commands.json 在当前目录生成
3248

33-
include_path = cads.find('VariousControls/IncludePath').text if cads.find('VariousControls/IncludePath') is not None else ''
3449
raw_includes = [inc for inc in include_path.split(';') if inc]
3550
includes = []
3651
for inc in raw_includes:
@@ -52,10 +67,24 @@ def parse_keil_project(file_path):
5267
for group in groups:
5368
group_files = group.findall('.//File')
5469
for file in group_files:
55-
file_type = int(file.find('FileType').text)
56-
# 只处理C/C++源文件和汇编文件
57-
if file_type == 1 or file_type == 2:
58-
file_path = file.find('FilePath').text
70+
file_type_element = file.find('FileType')
71+
file_path_element = file.find('FilePath')
72+
73+
file_type = None
74+
if file_type_element is not None and file_type_element.text is not None:
75+
try:
76+
file_type = int(file_type_element.text)
77+
except ValueError:
78+
# Handle cases where text is not a valid integer
79+
print(f"Warning: Invalid FileType value: {file_type_element.text}")
80+
continue # Skip this file
81+
82+
file_path = None
83+
if file_path_element is not None and file_path_element.text is not None:
84+
file_path = file_path_element.text
85+
86+
# Only process C/C++ source files and assembly files if file_type and file_path are valid
87+
if file_type in [1, 2] and file_path is not None:
5988
if file_path.endswith('.c') or file_path.endswith('.s'):
6089
files.append(file_path)
6190

@@ -82,10 +111,10 @@ def parse_keil_project(file_path):
82111

83112
except ET.ParseError as e:
84113
print(f"Failed to parse XML: {e}")
85-
return {}
114+
return []
86115
except Exception as e:
87116
print(f"An error occurred: {e}")
88-
return {}
117+
return []
89118

90119
def write_compile_commands(compile_commands, output_file='compile_commands.json'):
91120
"""
@@ -143,10 +172,12 @@ def find_compiler_in_settings(settings_path):
143172
return compiler
144173

145174
# 2. 尝试读取全局settings.json
146-
global_settings = os.path.join(os.getenv('AppData'), 'Code', 'User', 'settings.json')
147-
compiler = find_compiler_in_settings(global_settings)
148-
if compiler:
149-
return compiler
175+
app_data = os.getenv('AppData')
176+
if app_data:
177+
global_settings = os.path.join(app_data, 'Code', 'User', 'settings.json')
178+
compiler = find_compiler_in_settings(global_settings)
179+
if compiler:
180+
return compiler
150181

151182
# 3. 如果都没找到,返回默认值
152183
print("Please add the following to your VSCode settings.json (use absolute path):")

0 commit comments

Comments
 (0)