From 24255dabc55c67fe585bc1e3c242e445371cb1f5 Mon Sep 17 00:00:00 2001 From: jwx1102601 Date: Wed, 22 Nov 2023 16:28:03 +0800 Subject: [PATCH] =?UTF-8?q?CAPI=E8=AF=AD=E6=B3=95=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jwx1102601 --- capi_parser/src/coreImpl/check/check.py | 31 ++++-- .../src/coreImpl/check/check_syntax.py | 103 ++++++++++++++++++ capi_parser/src/typedef/check/check.py | 4 + 3 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 capi_parser/src/coreImpl/check/check_syntax.py diff --git a/capi_parser/src/coreImpl/check/check.py b/capi_parser/src/coreImpl/check/check.py index d608301354..abd6b39b81 100644 --- a/capi_parser/src/coreImpl/check/check.py +++ b/capi_parser/src/coreImpl/check/check.py @@ -19,6 +19,7 @@ from typedef.check.check import ApiResultInfo, FileDocInfo, OutputTxt from coreImpl.check.check_doc import process_comment, process_file_doc_info from coreImpl.check.check_name import check_file_name, check_ndk_name from coreImpl.parser.parser import parser_include_ast +from coreImpl.check.check_syntax import check_syntax def process_api_json(api_info, file_doc_info: FileDocInfo, api_result_info_list): @@ -59,31 +60,41 @@ def process_all_json(python_obj): return api_result_info_list -def write_in_txt(check_result): +def write_in_txt(check_result, output_path): + result_json = result_to_json(check_result) + fs = open(output_path, 'w', encoding='utf-8') + fs.write(result_json) + fs.close() + + +def result_to_json(check_result): txtResul = [] if len(check_result) == 0: txtResul.append('api_check: false') else: for result in check_result: - location = '{}(line:{}, col:{})'.format(result.location, result.locationLine, result.locationColumn) + location = f'{result.location}(line:{result.locationLine}, col:{result.locationColumn})' message = 'API check error of [{}]:{}'.format(result.errorType['description'], result.errorInfo) txtResul.append(OutputTxt(result.errorType['id'], result.level, location, result.fileName, message)) txtResul.append('api_check: false') - result_json = json.dumps(txtResul, default=lambda obj: obj.__dict__, indent=4) - fs = open(r'./Error.txt', 'w', encoding='utf-8') - fs.write(result_json) - fs.close() + return json.dumps(txtResul, default=lambda obj: obj.__dict__, indent=4) def curr_entry(pr_id): - file_path = os.path.abspath(os.path.join(os.getcwd(), "..{}..{}all_files.txt".format(os.sep, os.sep))) + file_path = os.path.abspath(os.path.join(os.getcwd(), f'..{os.sep}..{os.sep}all_files.txt')) file_list = get_md_files(file_path) - check_result = [] + check_result_list = get_check_result_list(file_list) + write_in_txt(check_result_list, r'./Error.txt') + + +def get_check_result_list(file_list): + check_result_list = [] for file in file_list: root_path = file.split('sdk_c')[0] + 'sdk_c' python_obj = parser_include_ast(root_path, [file]) - check_result.extend(process_all_json(python_obj)) - write_in_txt(check_result) + check_result_list.extend(process_all_json(python_obj)) + check_result_list.extend(check_syntax(file)) + return check_result_list def get_md_files(url): diff --git a/capi_parser/src/coreImpl/check/check_syntax.py b/capi_parser/src/coreImpl/check/check_syntax.py new file mode 100644 index 0000000000..ca0b0fe9a7 --- /dev/null +++ b/capi_parser/src/coreImpl/check/check_syntax.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import re +import subprocess +from typedef.check.check import ApiResultInfo, ErrorMessage, ErrorType, LogType + + +def check_syntax(file_path): + cmd_list = ['clang', r'-I sysroot\ndk_musl_include_files', '-std=c99'] + result_list = [] + command = cmd_list + [file_path] + run_result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + result_list.extend(processing_data(run_result, file_path)) + return result_list + + +def processing_data(run_result, result_file): + api_result_info_list = [] + for run_result_child in run_result.stderr.decode().split('^'): + result_child = run_result_child.replace('~', '') + ret = re.findall('\\d+:\\d+', result_child) + if len(ret) != 0: + error_message = get_specified_string(result_child) + if len(error_message) == 1: + continue + position = ret[0] + api_result_info = ApiResultInfo(ErrorType.SYNTAX_ERRORS.value, + error_message[1], result_file) + line_column = get_line_and_column(position) + api_result_info.set_location_line(line_column[0]) + api_result_info.set_location_column(line_column[1]) + api_result_info.set_location(result_file) + api_result_info.set_type(LogType.LOG_API.value) + api_result_info.set_level(2) + api_result_info.set_file_name(result_file) + api_result_info_list.append(api_result_info) + return api_result_info_list + + +def get_line_and_column(location): + if location is not None: + return location.split(':') + return ['', ''] + + +def get_original(result_child): + if len(result_child) == 0: + return result_child + original = result_child.lstrip().split("\r\n") + if len(original) == 2: + return '' + if len(original) == 3: + return original[1] + if len(original) == 4: + return original[2] + + +def get_specified_string(target_string): + message_type = 'error' + function_result = [] + pattern = r'error: (.*?)\r\n' + global matches + matches = re.findall(pattern, target_string, re.DOTALL) + if len(matches) == 0: + pattern = r'warning: (.*?)\r\n' + matches = re.findall(pattern, target_string, re.DOTALL) + message_type = 'warning' + if len(matches) == 0: + pattern = r'note: (.*?)\r\n' + matches = re.findall(pattern, target_string, re.DOTALL) + message_type = 'note' + function_result.append(message_type) + for match in matches: + function_result.append(match) + if len(function_result[1]) == 0: + function_result.append('') + return function_result + + +def get_file_path(file_path): + if len(file_path) == 0: + return file_path + path_split_len = len(file_path.split('\r\n')) + path_list = file_path.split('\r\n') + if path_split_len == 1: + return file_path + if path_split_len == 2: + return path_list[1] + if path_split_len == 3: + return path_list[2] diff --git a/capi_parser/src/typedef/check/check.py b/capi_parser/src/typedef/check/check.py index 576295f06f..964a3de42d 100644 --- a/capi_parser/src/typedef/check/check.py +++ b/capi_parser/src/typedef/check/check.py @@ -47,6 +47,10 @@ class ErrorType(enum.Enum): 'id': 2, 'description': 'naming errors', } + SYNTAX_ERRORS = { + 'id': 3, + 'description': 'syntax errors', + } UNKNOW_DEPRECATED = { 'id': 5, 'description': 'unknow deprecated', -- Gitee