From c6a7908bd270f9d8d94219bcde7081db64f86bb4 Mon Sep 17 00:00:00 2001 From: zhangwuf Date: Wed, 21 Feb 2024 11:22:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3CAPI=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96=E5=AE=8F?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=B3=A8=E9=87=8A=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangwuf --- .../src/coreImpl/diff/diff_processor_node.py | 35 +++++++++++------ .../src/coreImpl/parser/generating_tables.py | 3 +- .../src/coreImpl/parser/parse_include.py | 39 +++++++++++++++---- .../capi_parser/src/utils/constants.py | 1 + 4 files changed, 57 insertions(+), 21 deletions(-) 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 765bdacf3..dc8362526 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 @@ -26,6 +26,8 @@ current_file = os.path.dirname(__file__) def wrap_diff_info(old_info, new_info, diff_info: DiffInfo): if old_info is not None: + if 'temporary_name' in old_info['name']: + old_info['name'] = '' 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']) @@ -34,6 +36,8 @@ def wrap_diff_info(old_info, new_info, diff_info: DiffInfo): 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: + if 'temporary_name' in new_info['name']: + new_info['name'] = '' 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']) @@ -47,9 +51,12 @@ def wrap_diff_info(old_info, new_info, diff_info: DiffInfo): def parse_file_result(result): result_map = {} + key = 1 for member in result: if member["name"] == '': - continue + name = 'temporary_name' + member["name"] = '{}{}'.format(name, key) + key += 1 result_map.setdefault(f'{member["name"]}-{member["kind"]}', member) return result_map @@ -192,10 +199,12 @@ def process_struct_member_scene(old_member, new_member, diff_struct_list): special_data = process_enum(old_member, new_member) diff_struct_list.extend(special_data) - if old_member['type'] != new_member['type']: - diff_info = wrap_diff_info(old_member, new_member, - DiffInfo(DiffType.STRUCT_MEMBER_TYPE_CHANGE)) - diff_struct_list.append(diff_info) + if (not(old_member['location']['location_path'] in old_member['type'])) and \ + (not(new_member['location']['location_path'] in new_member['type'])): + if old_member['type'] != new_member['type']: + diff_info = wrap_diff_info(old_member, new_member, + DiffInfo(DiffType.STRUCT_MEMBER_TYPE_CHANGE)) + diff_struct_list.append(diff_info) if old_member['name'] != new_member['name']: diff_info = wrap_diff_info(old_member, new_member, @@ -257,10 +266,12 @@ def process_union_member_scene(old_member, new_member, diff_union_list): special_data = process_enum(old_member, new_member) diff_union_list.extend(special_data) - if old_member['type'] != new_member['type']: - diff_info = wrap_diff_info(old_member, new_member, - DiffInfo(DiffType.UNION_MEMBER_TYPE_CHANGE)) - diff_union_list.append(diff_info) + if (not(old_member['location']['location_path'] in old_member['type'])) and \ + (not(new_member['location']['location_path'] in new_member['type'])): + if old_member['type'] != new_member['type']: + diff_info = wrap_diff_info(old_member, new_member, + DiffInfo(DiffType.UNION_MEMBER_TYPE_CHANGE)) + diff_union_list.append(diff_info) if old_member['name'] != new_member['name']: diff_info = wrap_diff_info(old_member, new_member, @@ -401,7 +412,7 @@ def process_typedef(old, new): diff_typedef_list = [] process_typedef_name(old, new, diff_typedef_list) # 处理命名 - if 'children' in old and 'children' in old: # 处理子节点 + if 'children' in old and 'children' in new: # 处理子节点 process_typedef_child(old['children'], new['children'], diff_typedef_list) return diff_typedef_list @@ -699,10 +710,10 @@ def process_doc_list(old_doc_list: list, new_doc_list: list, old_info, new_info) new_length = len(new_doc_list) index = 0 while index < max(old_length, new_length): - if index > old_length: + if index >= old_length != new_length: diff_info_list.append(wrap_diff_info(old_info, new_info, DiffInfo(DiffType.ADD_DOC))) break - if index > new_length: + if index >= new_length != old_length: diff_info_list.append(wrap_diff_info(old_info, new_info, DiffInfo(DiffType.REDUCE_DOC))) break diff_info_list.extend(process_doc(old_doc_list[index], new_doc_list[index], old_info, new_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 9e14eade6..80a89d675 100644 --- a/build-tools/capi_parser/src/coreImpl/parser/generating_tables.py +++ b/build-tools/capi_parser/src/coreImpl/parser/generating_tables.py @@ -88,6 +88,7 @@ def get_parm(item, parm): def filter_func(item): del item["is_extern"] # 剔除is_extern键值对,过滤后都是extern del item["comment"] + del item["syscap"] if "type_ref" in list(item.keys()): del item["type_ref"] if "children" in list(item.keys()): @@ -117,7 +118,7 @@ def generate_excel(array, name, only_file1, only_file2): 'location': '位置行', 'return_type': '返回类型', 'parm': '参数', - 'location_path': '文件相对路径', + 'location_path': '文件相对路径' } workbook = openpyxl.Workbook() 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 14c84d2ce..f6272aa9b 100644 --- a/build-tools/capi_parser/src/coreImpl/parser/parse_include.py +++ b/build-tools/capi_parser/src/coreImpl/parser/parse_include.py @@ -241,6 +241,17 @@ def node_extent(cursor, current_file): return extent +def define_comment(cursor, current_file, data): + line = cursor.location.line + with open(current_file, mode='r', encoding='utf-8') as file: + file_content = file.readlines()[:line] + file_content = ''.join(file_content) + pattern = '{} {})'.format(RegularExpressions.DEFINE_COMMENT.value, cursor.spelling) + matches = re.search(pattern, file_content) + if matches: + data['comment'] = matches.group() + + def ast_to_dict(cursor, current_file, gn_path=None, comment=None, key=0): # 解析数据的整理 # 通用 data = { @@ -249,7 +260,8 @@ def ast_to_dict(cursor, current_file, gn_path=None, comment=None, key=0): # 解 "type": cursor.type.spelling, "gn_path": gn_path, "node_content": {}, - "comment": '' + "comment": '', + "syscap": '' } if cursor.raw_comment: # 是否有注释信息,有就取,没有过 data["comment"] = cursor.raw_comment @@ -267,6 +279,14 @@ def ast_to_dict(cursor, current_file, gn_path=None, comment=None, key=0): # 解 content = node_extent(cursor, current_file) data["node_content"] = content data["kind"] = cursor.kind.name + if cursor.kind.name == CursorKind.MACRO_DEFINITION.name: + define_comment(cursor, current_file, data) + + if 'none_comment' != data["comment"]: + pattern = r'@([Ss]yscap).*?(?=\n)' + matches = re.search(pattern, data['comment']) + if matches: + data["syscap"] = matches.group(0) processing_special_node(cursor, data, key, gn_path) # 节点处理 children = list(cursor.get_children()) # 判断是否有子节点,有就追加children,没有根据情况来 @@ -286,8 +306,8 @@ def ast_to_dict(cursor, current_file, gn_path=None, comment=None, key=0): # 解 for child in children: # 剔除多余宏定义和跳过UNEXPOSED_ATTR节点 - if child.location.file is not None and child.kind != CursorKind.UNEXPOSED_ATTR \ - and child.location.file.name == current_file: + if (child.location.file is not None) and (not child.kind.is_attribute()) \ + and (child.location.file.name == current_file): processing_ast_node(child, current_file, data, name, gn_path) else: processing_no_child(cursor, data) # 处理没有子节点的节点 @@ -331,17 +351,21 @@ def get_start_comments(include_path): # 获取每个头文件的最开始注释 def open_file(include_path): with open(include_path, 'r', encoding='utf-8') as f: content = '' - loge = 0 + mark = 0 + if 'ffrt' in include_path: + end_line_mark = r'#ifndef' + else: + end_line_mark = r'#endif' for line in f: - if line.startswith('#ifdef __cplusplus'): - loge = 1 + if line.startswith(end_line_mark): + mark = 1 break else: inside_ifdef = True if inside_ifdef: content += line - if loge == 0: + if mark == 0: content = '' f.close() return content @@ -386,5 +410,4 @@ def get_include_file(include_file_path, link_path, gn_path=None): # 库路径 # 头文件链接路径 link_include_path = link_path # 可以通过列表传入 data = api_entrance(libclang_path, file_path, gn_path, link_include_path) # 调用接口 - return data diff --git a/build-tools/capi_parser/src/utils/constants.py b/build-tools/capi_parser/src/utils/constants.py index 628741ec3..9c606194f 100644 --- a/build-tools/capi_parser/src/utils/constants.py +++ b/build-tools/capi_parser/src/utils/constants.py @@ -39,3 +39,4 @@ class RegularExpressions(enum.Enum): SOURCES = r'sources\s*=\s*\[[^\]]*\]' DEST_DIR = r'dest_dir\s*=\s*"([^"]*)"' INCLUDE_H = r'"([^"]+h)"' + DEFINE_COMMENT = r'/\*[^/]*\*/\s*(?=#define' -- Gitee