• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import enum
17import os.path
18import re
19from typedef.check.check import (CheckOutPut, CheckErrorMessage)
20
21
22def check_large_hump(api_info, kind, parent_kind):
23    api_result_info_list = []
24    # 结构体
25    if kind == 'STRUCT_DECL':
26        api_result_info_list = processing_check_data('LARGE_HUMP', api_info,
27                                                     CheckErrorMessage.API_NAME_UNIVERSAL_05.name)
28    # 枚举
29    if kind == 'ENUM_DECL':
30        api_result_info_list = processing_check_data('LARGE_HUMP', api_info,
31                                                     CheckErrorMessage.API_NAME_UNIVERSAL_03.name)
32    # 联合体
33    if kind == 'UNION_DECL':
34        api_result_info_list = processing_check_data('LARGE_HUMP', api_info,
35                                                     CheckErrorMessage.API_NAME_UNIVERSAL_07.name)
36    return api_result_info_list
37
38
39def check_function_name(api_info, kind, parent_kind):
40    api_result_info_list = []
41    function_name = api_info['name']
42    # 自研函数
43    if is_self_developed_function(function_name):
44        self_developed_function_result = re.match(r'^[OH|OS]+([\_]([A-Z]+[a-z0-9]*)+)*$', function_name)
45        if self_developed_function_result is None:
46            chck_output = set_value_to_result(api_info, CheckErrorMessage.API_NAME_UNIVERSAL_01.name)
47            api_result_info_list.append(chck_output)
48    # 一般函数
49    if not is_self_developed_function(function_name):
50        ordinary_function_result = re.match(r'^([A-Z][a-z0-9]*)*$', function_name)
51        if ordinary_function_result is None:
52            chck_output = set_value_to_result(api_info, CheckErrorMessage.API_NAME_UNIVERSAL_10.name)
53            api_result_info_list.append(chck_output)
54    return api_result_info_list
55
56
57def set_value_to_result(api_info, command):
58    content = api_info['node_content']['content']
59    node_name = api_info['name']
60    if api_info['kind'] == 'MACRO_DEFINITION' and api_info.get('def_func_name'):
61        node_name = api_info.get('def_func_name')
62    descriptive_message = CheckErrorMessage.__getitem__(command).value.replace('$$', node_name)
63    return CheckOutPut(os.path.abspath(os.path.join(api_info['gn_path'], api_info['location']['location_path'])),
64                       api_info['location']['location_line'], command, descriptive_message,
65                       content, api_info['location']['location_line'])
66
67
68def is_self_developed_function(function_name):
69    return function_name.startswith('OH_') or function_name.startswith('OS_') or function_name.startswith('HMS_')
70
71
72def check_variable_name(api_info, kind, parent_kind):
73    api_result_info_list = []
74    is_const = api_info['is_const']
75    # 常量
76    if is_const:
77        api_result_info_list = processing_check_data('ALL_UPPERCASE_HUMP',
78                                                     api_info, CheckErrorMessage.API_NAME_UNIVERSAL_02.name)
79    # 全局变量
80    if not is_const:
81        api_result_info_list = processing_check_data('GLOBAL_VARIABLE', api_info,
82                                                     CheckErrorMessage.API_NAME_UNIVERSAL_09.name)
83    return api_result_info_list
84
85
86def check_small_hump(api_info, kind, parent_kind):
87    api_result_info_list = []
88    # 函数参数
89    if parent_kind == 'FUNCTION_DECL':
90        api_result_info_list = processing_check_data('SMALL_HUMP', api_info,
91                                                     CheckErrorMessage.API_NAME_UNIVERSAL_11.name)
92    # 结构体成员
93    if parent_kind == 'STRUCT_DECL':
94        api_result_info_list = processing_check_data('SMALL_HUMP', api_info,
95                                                     CheckErrorMessage.API_NAME_UNIVERSAL_06.name)
96    # 联合体成员
97    if parent_kind == 'UNION_DECL':
98        api_result_info_list = processing_check_data('SMALL_HUMP', api_info,
99                                                     CheckErrorMessage.API_NAME_UNIVERSAL_08.name)
100    return api_result_info_list
101
102
103def check_macro_definition(api_info, kind, parent_kind):
104    api_result_info_list = []
105    name = api_info['name']
106    # 宏定义
107    if not api_info['is_def_func']:
108        result = re.match(CheckName['MACRO_DEFINITION'].value, name)
109        if result is None:
110            api_result_info_list.append(set_value_to_result(api_info, CheckErrorMessage.API_NAME_UNIVERSAL_12.name))
111    # 函数式宏
112    if api_info['is_def_func']:
113        name = api_info['def_func_name']
114        result = re.match(CheckName['MACRO_DEFINITION'].value, name)
115        if result is None:
116            api_result_info_list.append(set_value_to_result(api_info, CheckErrorMessage.API_NAME_UNIVERSAL_13.name))
117    return api_result_info_list
118
119
120def check_all_uppercase_hump(api_info, kind, parent_kind):
121    # 枚举值
122    api_result_info_list = processing_check_data('ALL_UPPERCASE_HUMP', api_info,
123                                                 CheckErrorMessage.API_NAME_UNIVERSAL_04.name)
124    return api_result_info_list
125
126
127def check_file_name(file_info):
128    api_result_info_list = []
129    file_name = os.path.basename(file_info['name'])
130    result = re.match(CheckName['FILE_NAME'].value, file_name)
131    if result is None:
132        check_output = CheckOutPut(os.path.abspath(os.path.join(file_info['gn_path'], file_info['name'])), 0,
133                                   CheckErrorMessage.API_NAME_UNIVERSAL_14.name,
134                                   CheckErrorMessage.API_NAME_UNIVERSAL_14.value.replace('$$', file_name), '', 0)
135        api_result_info_list.append(check_output)
136    return api_result_info_list
137
138
139def processing_check_data(function_type, api_info, command):
140    api_result_info_list = []
141    name = api_info['name']
142    result = re.match(CheckName[function_type].value, name)
143    if result is None:
144        chck_output = set_value_to_result(api_info, command)
145        api_result_info_list.append(chck_output)
146    return api_result_info_list
147
148
149class CheckName(enum.Enum):
150    LARGE_HUMP = r'^([A-Z][a-z0-9]*)*([\_]([A-Z][a-z0-9]*)*)*$'
151    SMALL_HUMP = r'^([a-z][A-Z0-9]*)*([\_]([a-z][A-Z0-9]*)*)*$'
152    ALL_UPPERCASE_HUMP = r'^[A-Z]+[0-9]*([\_][A-Z0-9]*)*$'
153    GLOBAL_VARIABLE = r'^g_([a-z][A-Z0-9]*)*$'
154    FILE_NAME = r'^[a-z]+[a-z0-9]+([\_][a-z0-9]+)*\.h$'
155    MACRO_DEFINITION = r'^[\_]*[A-Z]+[0-9]*([\_][A-Z0-9]*)*$'
156
157
158process_tag_function = {
159    # 函数
160    'FUNCTION_DECL': check_function_name,
161    # 结构体
162    'STRUCT_DECL': check_large_hump,
163    # 枚举
164    'ENUM_DECL': check_large_hump,
165    # 联合体
166    'UNION_DECL': check_large_hump,
167    # 变量
168    'VAR_DECL': check_variable_name,
169    # 参数
170    'PARM_DECL': check_small_hump,
171    # 结构体、联合体成员
172    'FIELD_DECL': check_small_hump,
173    # 宏定义
174    'MACRO_DEFINITION': check_macro_definition,
175    # 枚举值
176    'ENUM_CONSTANT_DECL': check_all_uppercase_hump,
177}
178
179
180def check_api_name(api_info, parent_kind):
181    api_result_info_list = []
182    kind = api_info['kind']
183    if kind not in process_tag_function.keys():
184        return api_result_info_list
185    name_process = process_tag_function[kind]
186    return name_process(api_info, kind, parent_kind)
187