• 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    return CheckOutPut(os.path.abspath(os.path.join(api_info['gn_path'], api_info['location']['location_path'])),
59                       api_info['location']['location_line'], command, CheckErrorMessage.__getitem__(command).value,
60                       api_info['node_content']['content'], api_info['location']['location_line'])
61
62
63def is_self_developed_function(function_name):
64    return function_name.startswith('OH_') or function_name.startswith('OS_') or function_name.startswith('HMS_')
65
66
67def check_variable_name(api_info, kind, parent_kind):
68    api_result_info_list = []
69    is_const = api_info['is_const']
70    # 常量
71    if is_const:
72        api_result_info_list = processing_check_data('ALL_UPPERCASE_HUMP',
73                                                     api_info, CheckErrorMessage.API_NAME_UNIVERSAL_02.name)
74    # 全局变量
75    if not is_const:
76        api_result_info_list = processing_check_data('GLOBAL_VARIABLE', api_info,
77                                                     CheckErrorMessage.API_NAME_UNIVERSAL_09.name)
78    return api_result_info_list
79
80
81def check_small_hump(api_info, kind, parent_kind):
82    api_result_info_list = []
83    # 函数参数
84    if parent_kind == 'FUNCTION_DECL':
85        api_result_info_list = processing_check_data('SMALL_HUMP', api_info,
86                                                     CheckErrorMessage.API_NAME_UNIVERSAL_11.name)
87    # 结构体成员
88    if parent_kind == 'STRUCT_DECL':
89        api_result_info_list = processing_check_data('SMALL_HUMP', api_info,
90                                                     CheckErrorMessage.API_NAME_UNIVERSAL_06.name)
91    # 联合体成员
92    if parent_kind == 'UNION_DECL':
93        api_result_info_list = processing_check_data('SMALL_HUMP', api_info,
94                                                     CheckErrorMessage.API_NAME_UNIVERSAL_08.name)
95    return api_result_info_list
96
97
98def check_macro_definition(api_info, kind, parent_kind):
99    api_result_info_list = []
100    name = api_info['name']
101    # 宏定义
102    if not api_info['is_def_func']:
103        result = re.match(CheckName['MACRO_DEFINITION'].value, name)
104        if result is None:
105            api_result_info_list.append(set_value_to_result(api_info, CheckErrorMessage.API_NAME_UNIVERSAL_12.name))
106    # 函数式宏
107    if api_info['is_def_func']:
108        name = api_info['def_func_name']
109        result = re.match(CheckName['MACRO_DEFINITION'].value, name)
110        if result is None:
111            api_result_info_list.append(set_value_to_result(api_info, CheckErrorMessage.API_NAME_UNIVERSAL_13.name))
112    return api_result_info_list
113
114
115def check_all_uppercase_hump(api_info, kind, parent_kind):
116    # 枚举值
117    api_result_info_list = processing_check_data('ALL_UPPERCASE_HUMP', api_info,
118                                                 CheckErrorMessage.API_NAME_UNIVERSAL_04.name)
119    return api_result_info_list
120
121
122def check_file_name(file_info):
123    api_result_info_list = []
124    file_name = os.path.basename(file_info['name'])
125    result = re.match(CheckName['FILE_NAME'].value, file_name)
126    if result is None:
127        chck_output = CheckOutPut(os.path.abspath(os.path.join(file_info['gn_path'], file_info['name'])), 0,
128                                  CheckErrorMessage.API_NAME_UNIVERSAL_14.name,
129                                  CheckErrorMessage.API_NAME_UNIVERSAL_14.value, file_name, 0)
130        api_result_info_list.append(chck_output)
131    return api_result_info_list
132
133
134def processing_check_data(function_type, api_info, command):
135    api_result_info_list = []
136    name = api_info['name']
137    result = re.match(CheckName[function_type].value, name)
138    if result is None:
139        chck_output = set_value_to_result(api_info, command)
140        api_result_info_list.append(chck_output)
141    return api_result_info_list
142
143
144class CheckName(enum.Enum):
145    LARGE_HUMP = r'^([A-Z][a-z0-9]*)*([\_]([A-Z][a-z0-9]*)*)*$'
146    SMALL_HUMP = r'^([a-z][A-Z0-9]*)*([\_]([a-z][A-Z0-9]*)*)*$'
147    ALL_UPPERCASE_HUMP = r'^[A-Z]+[0-9]*([\_][A-Z0-9]*)*$'
148    GLOBAL_VARIABLE = r'^g_([a-z][A-Z0-9]*)*$'
149    FILE_NAME = r'^[a-z]+[a-z0-9]+([\_][a-z0-9]+)*\.h$'
150    MACRO_DEFINITION = r'^[\_]*[A-Z]+[0-9]*([\_][A-Z0-9]*)*$'
151
152
153process_tag_function = {
154    # 函数
155    'FUNCTION_DECL': check_function_name,
156    # 结构体
157    'STRUCT_DECL': check_large_hump,
158    # 枚举
159    'ENUM_DECL': check_large_hump,
160    # 联合体
161    'UNION_DECL': check_large_hump,
162    # 变量
163    'VAR_DECL': check_variable_name,
164    # 参数
165    'PARM_DECL': check_small_hump,
166    # 结构体、联合体成员
167    'FIELD_DECL': check_small_hump,
168    # 宏定义
169    'MACRO_DEFINITION': check_macro_definition,
170    # 枚举值
171    'ENUM_CONSTANT_DECL': check_all_uppercase_hump,
172}
173
174
175def check_api_name(api_info, parent_kind):
176    api_result_info_list = []
177    kind = api_info['kind']
178    if kind not in process_tag_function.keys():
179        return api_result_info_list
180    name_process = process_tag_function[kind]
181    return name_process(api_info, kind, parent_kind)
182