!83 CAPI语法检查工具上库

Merge pull request !83 from 姜前进/1122
This commit is contained in:
openharmony_ci 2023-11-23 12:37:56 +00:00 committed by Gitee
commit 88af4abac2
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 128 additions and 10 deletions

View File

@ -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):

View File

@ -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]

View File

@ -51,6 +51,10 @@ class ErrorType(enum.Enum):
'id': 2,
'description': 'naming errors',
}
SYNTAX_ERRORS = {
'id': 3,
'description': 'syntax errors',
}
UNKNOW_DEPRECATED = {
'id': 5,
'description': 'unknow deprecated',