From 21dd4273e0fd0e50a827cedfcb1daec8889083b5 Mon Sep 17 00:00:00 2001 From: zhangwuf Date: Sat, 23 Dec 2023 17:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=88=E5=AF=B9CAPIdiff=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=A3=E6=9E=90=E5=BC=95=E5=85=A5=E5=A4=B4?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangwuf --- .../src/coreImpl/diff/diff_file.py | 21 +++++++++------ .../src/coreImpl/diff/diff_processor_node.py | 10 +++++-- .../src/coreImpl/parser/generating_tables.py | 9 +++++-- .../src/coreImpl/parser/parse_include.py | 16 ++++++++--- .../capi_parser/src/coreImpl/parser/parser.py | 26 +++++++++--------- build-tools/capi_parser/src/main.py | 5 ++++ .../capi_parser/src/typedef/diff/diff.py | 27 +++++++++++++++++++ .../capi_parser/src/utils/constants.py | 5 +++- 8 files changed, 91 insertions(+), 28 deletions(-) diff --git a/build-tools/capi_parser/src/coreImpl/diff/diff_file.py b/build-tools/capi_parser/src/coreImpl/diff/diff_file.py index a0ba88c79..81b6edd9d 100644 --- a/build-tools/capi_parser/src/coreImpl/diff/diff_file.py +++ b/build-tools/capi_parser/src/coreImpl/diff/diff_file.py @@ -40,6 +40,9 @@ def generate_excel(result_info_list): for diff_info in result_info_list: info_data = [] info_data.append(diff_info.api_name) + info_data.append(diff_info.api_line) + info_data.append(diff_info.api_column) + info_data.append(diff_info.api_file_path) info_data.append(diff_info.api_type) info_data.append(diff_info.diff_type.name) info_data.append(diff_info.diff_message) @@ -50,9 +53,11 @@ def generate_excel(result_info_list): data.append(info_data) wb = op.Workbook() ws = wb['Sheet'] - ws.append(['api名称', '节点类型', '变更类型', '变更信息', '旧版节点内容', '新版节点内容', '兼容']) + ws.append(['api名称', '所在行', '所在列', '所在文件', '节点类型', + '变更类型', '变更信息', '旧版节点内容', '新版节点内容', '兼容']) for i in range(len(data)): - d = data[i][0], data[i][1], data[i][2], data[i][3], data[i][4], data[i][5], data[i][6] + d = data[i][0], data[i][1], data[i][2], data[i][3], data[i][4],\ + data[i][5], data[i][6], data[i][7], data[i][8], data[i][9] ws.append(d) wb.save('diff.xlsx') @@ -114,7 +119,7 @@ def add_new_file(diff_file_path): if os.path.isdir(diff_file_path): add_file(diff_file_path) else: - result_map = parse_file_result(parser_include_ast(global_new_dir, [diff_file_path])) + result_map = parse_file_result(parser_include_ast(global_new_dir, [diff_file_path], flag=1)) for new_info in result_map.values(): diff_info_list.extend(judgment_entrance(None, new_info)) @@ -123,7 +128,7 @@ def del_old_file(diff_file_path): if os.path.isdir(diff_file_path): del_file(diff_file_path) else: - result_map = parse_file_result(parser_include_ast(global_old_dir, [diff_file_path])) + result_map = parse_file_result(parser_include_ast(global_old_dir, [diff_file_path], flag=0)) for old_info in result_map.values(): diff_info_list.extend(judgment_entrance(old_info, None)) @@ -145,8 +150,8 @@ def get_same_file_diff(target_file, old_file_list, new_file_list, old_dir, new_d def get_file_result_diff(old_target_file, new_target_file): - old_file_result_map = parse_file_result(parser_include_ast(global_old_dir, [old_target_file])) - new_file_result_map = parse_file_result(parser_include_ast(global_new_dir, [new_target_file])) + old_file_result_map = parse_file_result(parser_include_ast(global_old_dir, [old_target_file], flag=0)) + new_file_result_map = parse_file_result(parser_include_ast(global_new_dir, [new_target_file], flag=1)) merged_dict = OrderedDict(list(old_file_result_map.items()) + list(new_file_result_map.items())) all_key_list = merged_dict.keys() for key in all_key_list: @@ -162,7 +167,7 @@ def del_file(dir_path): if os.path.isdir(file_path): del_file(file_path) if get_file_ext(i) == '.h': - result_map = parse_file_result(parser_include_ast(global_old_dir, [file_path])) + result_map = parse_file_result(parser_include_ast(global_old_dir, [file_path], flag=0)) for old_info in result_map.values(): diff_info_list.extend(judgment_entrance(old_info, None)) @@ -176,7 +181,7 @@ def add_file(dir_path): if os.path.isdir(file_path): add_file(file_path) if get_file_ext(i) == '.h': - result_map = parse_file_result(parser_include_ast(global_new_dir, [file_path])) + result_map = parse_file_result(parser_include_ast(global_new_dir, [file_path], flag=1)) for new_info in result_map.values(): diff_info_list.extend(judgment_entrance(None, new_info)) diff --git a/build-tools/capi_parser/src/coreImpl/diff/diff_processor_node.py b/build-tools/capi_parser/src/coreImpl/diff/diff_processor_node.py index f45f3eab2..765bdacf3 100644 --- a/build-tools/capi_parser/src/coreImpl/diff/diff_processor_node.py +++ b/build-tools/capi_parser/src/coreImpl/diff/diff_processor_node.py @@ -28,11 +28,17 @@ def wrap_diff_info(old_info, new_info, diff_info: DiffInfo): if old_info is not None: diff_info.set_api_name(old_info['name']) diff_info.set_api_type(old_info['kind']) + diff_info.set_api_line(old_info['location']['location_line']) + diff_info.set_api_column(old_info['location']['location_column']) + diff_info.set_api_file_path(old_info['location']['location_path']) if 'content' in old_info['node_content']: diff_info.set_old_api_full_text(old_info['node_content']['content']) if new_info is not None: diff_info.set_api_name(new_info['name']) diff_info.set_api_type(new_info['kind']) + diff_info.set_api_line(new_info['location']['location_line']) + diff_info.set_api_column(new_info['location']['location_column']) + diff_info.set_api_file_path(new_info['location']['location_path']) if 'content' in new_info['node_content']: diff_info.set_new_api_full_text(new_info['node_content']['content']) @@ -380,12 +386,12 @@ def process_constant_value(old, new, diff_constant_list): DiffInfo(DiffType.CONSTANT_VALUE_CHANGE)) diff_constant_list.append(diff_info) - elif 'children' not in old and 'children' in new: + elif 'children' not in old and 'children' in new and len(new['children']): diff_info = wrap_diff_info(old, new['children'][0], DiffInfo(DiffType.CONSTANT_VALUE_CHANGE)) diff_constant_list.append(diff_info) - elif 'children' in old and 'children' not in new: + elif 'children' in old and 'children' not in new and len(old['children']): diff_info = wrap_diff_info(old['children'][0], new, DiffInfo(DiffType.CONSTANT_VALUE_CHANGE)) diff_constant_list.append(diff_info) diff --git a/build-tools/capi_parser/src/coreImpl/parser/generating_tables.py b/build-tools/capi_parser/src/coreImpl/parser/generating_tables.py index 0de49d4be..1a266e50c 100644 --- a/build-tools/capi_parser/src/coreImpl/parser/generating_tables.py +++ b/build-tools/capi_parser/src/coreImpl/parser/generating_tables.py @@ -62,11 +62,16 @@ def get_difference_data(compare_result, data2): def filter_compare(data1): # 获取函数和变量 result_api = [] for it in data1: - for item1 in it["children"]: # 抛开根节点 + get_result_api(it, result_api) + return result_api + + +def get_result_api(file_data, result_api): + if 'children' in file_data: + for item1 in file_data["children"]: # 抛开根节点 if (item1["kind"] == 'FUNCTION_DECL' or item1["kind"] == 'VAR_DECL') and item1["is_extern"]: item = filter_func(item1) result_api.append(item) - return result_api def get_parm(item, parm): diff --git a/build-tools/capi_parser/src/coreImpl/parser/parse_include.py b/build-tools/capi_parser/src/coreImpl/parser/parse_include.py index 40e02e42e..ec4132637 100644 --- a/build-tools/capi_parser/src/coreImpl/parser/parse_include.py +++ b/build-tools/capi_parser/src/coreImpl/parser/parse_include.py @@ -198,8 +198,8 @@ def processing_def(cursor, data): # 处理宏定义 data['name'] = split_data[0] data['text'] = split_data[1] elif len(split_data_three) == 2: - data['name'] = split_data[0] - data['text'] = split_data[1] + data['name'] = split_data_three[0] + data['text'] = split_data_three[1] else: marco_ext = cursor.extent tokens = cursor.translation_unit.get_tokens(extent=marco_ext) # 找到对应的宏定义位置 @@ -209,6 +209,17 @@ def processing_def(cursor, data): # 处理宏定义 print('mar_define error, its content is none') data["type"] = "def_no_type" + judgment_def_func(data) + + +def judgment_def_func(data): + data['is_def_func'] = False + if '(' in data['name'] and ')' in data['name']: + data['is_def_func'] = True + index = data['name'].index('(') + data['def_func_name'] = data['name'][:index] + data['def_func_param'] = data['name'][index:] + def processing_func(cursor, data): # 处理函数 data["return_type"] = cursor.result_type.spelling # 增加返回类型键值对 @@ -412,7 +423,6 @@ def api_entrance(share_lib, include_path, gn_path=None, link_path=None): # 统 args = ['-I{}'.format(path) for path in link_path] args.append('-std=c99') options = clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD - print(args) data_total = [] # 列表对象-用于统计 for item in include_path: # 对每个头文件做处理 diff --git a/build-tools/capi_parser/src/coreImpl/parser/parser.py b/build-tools/capi_parser/src/coreImpl/parser/parser.py index cdf69a0d0..45d5696dd 100644 --- a/build-tools/capi_parser/src/coreImpl/parser/parser.py +++ b/build-tools/capi_parser/src/coreImpl/parser/parser.py @@ -249,20 +249,23 @@ def copy_std_lib(link_include_file): def find_include(link_include_path): - for dir_path, _, _ in os.walk(RegularExpressions.CREATE_LIB_PATH.value): + for dir_path, _, _ in os.walk(StringConstant.CREATE_LIB_PATH.value): link_include_path.append(dir_path) -def copy_self_include(link_include_file, self_include_file): - std_include = StringConstant.SELF_INCLUDE.value - if not os.path.exists(std_include): - os.makedirs(std_include) +def copy_self_include(link_include_path, self_include_file, flag=-1): + if flag == 0: + std_include = StringConstant.SELF_INCLUDE_OLD.value + elif flag == 1: + std_include = StringConstant.SELF_INCLUDE_NEW.value + else: + std_include = StringConstant.SELF_INCLUDE.value - if self_include_file: - for item in self_include_file: - shutil.copy(item, std_include) + if std_include and not os.path.exists(std_include): + shutil.copytree(self_include_file, std_include) - link_include_file.append(std_include) + for dir_path, _, _ in os.walk(std_include): + link_include_path.append(dir_path) def delete_typedef_child(child): @@ -286,13 +289,13 @@ def parser(directory_path): # 目录路径 return data_total -def parser_include_ast(gn_file_path, include_path, self_include=None): # 对于单独的.h解析接口 +def parser_include_ast(gn_file_path, include_path, flag=-1): # 对于单独的.h解析接口 correct_include_path = [] link_include_path = [] copy_std_lib(link_include_path) find_include(link_include_path) - copy_self_include(link_include_path, self_include) + copy_self_include(link_include_path, gn_file_path, flag) modes = stat.S_IRWXO | stat.S_IRWXG | stat.S_IRWXU fd = os.open('include_file_suffix.txt', os.O_WRONLY | os.O_CREAT, mode=modes) @@ -312,5 +315,4 @@ def parser_include_ast(gn_file_path, include_path, self_include=None): # for child in item['children']: delete_typedef_child(child) - shutil.rmtree(StringConstant.SELF_INCLUDE.value) return data diff --git a/build-tools/capi_parser/src/main.py b/build-tools/capi_parser/src/main.py index fd804e3b7..70d318857 100644 --- a/build-tools/capi_parser/src/main.py +++ b/build-tools/capi_parser/src/main.py @@ -16,7 +16,10 @@ ############################################## import argparse +import shutil +import os from bin import config +from utils import constants def main_function(): @@ -38,3 +41,5 @@ def main_function(): if __name__ == '__main__': main_function() + if os.path.exists(constants.StringConstant.SYSROOT.value): + shutil.rmtree(constants.StringConstant.SYSROOT.value) diff --git a/build-tools/capi_parser/src/typedef/diff/diff.py b/build-tools/capi_parser/src/typedef/diff/diff.py index fa5bf48c6..363230efc 100644 --- a/build-tools/capi_parser/src/typedef/diff/diff.py +++ b/build-tools/capi_parser/src/typedef/diff/diff.py @@ -161,6 +161,9 @@ class DiffInfo: diff_message: str = '' old_api_full_text: str = '' new_api_full_text: str = '' + api_line: int = 0 + api_column: int = 0 + api_file_path: str = '' is_compatible = False def __init__(self, diff_type: DiffType): @@ -168,6 +171,24 @@ class DiffInfo: self.diff_message = diff_type.value self.set_diff_type(diff_type) + def set_api_line(self, api_line): + self.api_line = api_line + + def get_api_line(self): + return self.api_line + + def set_api_column(self, api_column): + self.api_column = api_column + + def get_api_column(self): + return self.api_column + + def set_api_file_path(self, api_file_path): + self.api_file_path = api_file_path + + def get_api_file_path(self): + return self.api_file_path + def set_api_name(self, api_name): self.api_name = api_name @@ -220,6 +241,9 @@ class OutputJson: diff_message: str = '' old_api_full_text: str = '' new_api_full_text: str = '' + api_line: int = 0 + api_column: int = 0 + api_file_path: str = '' is_compatible = False def __init__(self, diff_info): @@ -229,4 +253,7 @@ class OutputJson: self.diff_message = diff_info.diff_message self.old_api_full_text = diff_info.old_api_full_text self.new_api_full_text = diff_info.new_api_full_text + self.api_line = diff_info.api_line + self.api_column = diff_info.api_column + self.api_file_path = diff_info.api_file_path self.is_compatible = diff_info.is_compatible diff --git a/build-tools/capi_parser/src/utils/constants.py b/build-tools/capi_parser/src/utils/constants.py index d8c08425f..b88370294 100644 --- a/build-tools/capi_parser/src/utils/constants.py +++ b/build-tools/capi_parser/src/utils/constants.py @@ -25,7 +25,11 @@ class StringConstant(enum.Enum): # 拉到本地仓的三方库绝对路径 INCLUDE_LIB = r'.\third_party\musl\ndk_musl_include' STD_INCLUDE = r'.\sysroot\ndk_musl_include_files' + CREATE_LIB_PATH = r'.\sysroot\$ndk_headers_out_dir' SELF_INCLUDE = r'.\sysroot\self_include_files' + SELF_INCLUDE_OLD = r'.\sysroot\self_include_files_old' + SELF_INCLUDE_NEW = r'.\sysroot\self_include_files_new' + SYSROOT = r'.\sysroot' RESULT_HEAD_NAME = "result_total.xlsx" @@ -35,4 +39,3 @@ class RegularExpressions(enum.Enum): SOURCES = r'sources\s*=\s*\[[^\]]*\]' DEST_DIR = r'dest_dir\s*=\s*"([^"]*)"' INCLUDE_H = r'"([^"]+h)"' - CREATE_LIB_PATH = r'sysroot\$ndk_headers_out_dir' -- Gitee