• 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
17
18
19class TAGS(enum.Enum):
20    ADD_TO_GROUP = 'addtogroup'
21    BRIEF = 'brief'
22    DEPRECATED = 'deprecated'
23    FILE = 'file'
24    LIBRARY = 'library'
25    PARAM = 'param'
26    PERMISSION = 'permission'
27    RETURN = 'return'
28    SINCE = 'since'
29    SYSCAP = 'syscap'
30    LEFT_BRACE = '{'
31    RIGHT_BRACE = '}'
32
33
34class ErrorType(enum.Enum):
35    DEFAULT = {
36        'id': -1,
37        'description': '',
38    }
39    UNKNOW_DECORATOR = {
40        'id': 0,
41        'description': 'unknow decorator',
42    }
43    MISSPELL_WORDS = {
44        'id': 1,
45        'description': 'misspell words',
46    }
47    NAMING_ERRORS = {
48        'id': 2,
49        'description': 'naming errors',
50    }
51    UNKNOW_PERMISSION = {
52        'id': 3,
53        'description': 'unknow permission',
54    }
55    UNKNOW_SYSCAP = {
56        'id': 4,
57        'description': 'unknow syscap',
58    }
59    UNKNOW_DEPRECATED = {
60        'id': 5,
61        'description': 'unknow deprecated',
62    }
63    WRONG_ORDER = {
64        'id': 6,
65        'description': 'wrong order',
66    }
67    WRONG_VALUE = {
68        'id': 7,
69        'description': 'wrong value',
70    }
71    WRONG_SCENE = {
72        'id': 8,
73        'description': 'wrong scene',
74    }
75    PARAMETER_ERRORS = {
76        'id': 9,
77        'description': 'wrong parameter',
78    }
79    API_PAIR_ERRORS = {
80        'id': 10,
81        'description': 'limited api pair errors',
82    }
83    ILLEGAL_ANY = {
84        'id': 11,
85        'description': 'illegal any',
86    }
87    API_CHANGE_ERRORS = {
88        'id': 12,
89        'description': 'api change errors',
90    }
91    EMPTY_TAG = {
92        'id': 13,
93        'description': 'miss tag value',
94    }
95    ERROR_TAG = {
96        'id': 14,
97        'description': 'error tag name',
98    }
99    SYNTAX_ERRORS = {
100        'id': 15,
101        'description': 'syntax errors',
102    }
103
104
105class LogType(enum.Enum):
106    DEFAULT = ''
107    LOG_API = 'Api'
108    LOG_JSDOC = 'JsDoc'
109    LOG_FILE = 'File'
110
111
112class ErrorLevel(enum.Enum):
113    HIGH = 3
114    MIDDLE = 2
115    LOW = 1
116
117
118class ErrorMessage(enum.Enum):
119    EMPTY_TAG = 'the [$$] tag value is empty. Please supplement the default value'
120    USE_UPPER_TAG = "the [$$] tag is upper. Should use lower: [$$]"
121    REPEAT_FILE_TAG = 'the [$$] tag is repeat. Please check the tag in file'
122    ERROR_INFO_VALUE_TAG = 'the [$$] tag value is incorrect. Please check the usage method'
123    ERROR_INFO_VALUE_LIBRARY = 'the [library] tag value is incorrect. This tag must be end with .so or .a, \
124or is NA. Please check the usage method'
125    ERROR_INFO_VALUE_PARAM = 'the value of the [$$] [param] tag is incorrect. Please check if it matches \
126the [$$] parameter name'
127    ERROR_INFO_COUNT_PARAM = 'the count of the [param] tag is wrong. Please check the parameters and Doc'
128    ERROR_INFO_VALUE_PERMISSION = 'the [permission] tag value is incorrect. Please check if the permission \
129field has been configured or update the configuration file'
130    ERROR_INFO_VALUE_SINCE = 'the [since] tag value is incorrect. Please check if the tag value is a numerical value'
131    ERROR_INFO_VALUE_SYSCAP = 'the [syscap] tag value is incorrect. Please check if the syscap field is configured'
132    ERROR_USE_LEFT_BRACE = 'the validity verification of the Doc tag failed. The [{] tag is not allowed to used \
133in Doc which not has addtogroup tag, or used in the wrong place.'
134    ERROR_REPEAT_LEFT_BRACE = 'the validity verification of the Doc tag failed. The [{] tag is not allowed to \
135reuse in Doc which has addtogroup tag.'
136    ERROR_USE_RIGHT_BRACE = 'the validity verification of the JSDoc tag failed. The [}] tag is not allowed to \
137be reused please delete the extra tags.'
138    ERROR_FILE_HAS_ONE_LOSE_OTHER = 'the file has the $$, but do not has the $$.'
139    ERROR_FILE_LOSE_ONE = 'the file missing $$'
140    ERROR_LOST_LABEL = 'JSDoc tag validity verification failed. Please confirm if the [$$] tag is missing'
141
142    FUNCTION_DECL = 'This name [$$] should use the big hump naming style or beginning with OH/OS,and \
143        using "_" segmentation.'
144    STRUCT_DECL = 'This name [$$] should use the big hump naming style.'
145    ENUM_DECL = 'This name [$$] should use the big hump naming style.'
146    UNION_DECL = 'This name [$$] should use the big hump naming style.'
147    VAR_DECL = 'This name [$$] should use the small hump naming style.'
148    PARM_DECL = 'This name [$$] should use the small hump naming style.'
149    MACRO_PARAMETERS_TYPE_NAMING_ERROR = 'This name [$$] should use the small hump naming style.'
150    FIELD_DECL = 'This name [$$] should use the small hump naming style.'
151    MEMBERS_OF_THE_CONSORTIUM_TYPE_NAMING_ERROR = 'This name [$$] should use the small hump naming style.'
152    MACRO_DEFINITION = 'This name [$$] should use all uppercase, separated by underscores naming style.'
153    ENUM_CONSTANT_DECL = 'This name [$$] should use all uppercase, separated by underscores naming style.'
154    GOTO_LABEL_TYPE_NAMING_ERROR = 'This name [$$] should use all uppercase, separated by \
155        underscores naming style.'
156    GLOBAL_VARIABLE_TYPE_NAMING_ERROR = 'This name [$$] should increase "g_" prefix.'
157    TRANSLATION_UNIT = 'This name [$$] should be all lowercase, separated by underscores.'
158
159
160class CheckErrorMessage(enum.Enum):
161    API_NAME_UNIVERSAL_01 = ('API check error of [naming errors]:The [$$] of self-developed '
162                             'functions in APIs must start with OH_ or OS_ or HMS_ and comply '
163                             'with the large hump case format.')
164    API_NAME_UNIVERSAL_02 = ('API check error of [naming errors]:The [$$] of constants in '
165                             'APIs must be in uppercase and separated by underscores.')
166    API_NAME_UNIVERSAL_03 = ('API check error of [naming errors]:The [$$] of enumeration '
167                             'types should follow the big hump rule.')
168    API_NAME_UNIVERSAL_04 = ('API check error of [naming errors]:The [$$] of enumeration '
169                             'values should follow the all uppercase rule and separated by underscores.')
170    API_NAME_UNIVERSAL_05 = ('API check error of [naming errors]:The [$$] of struct should '
171                             'follow the rules of the Great Hump.')
172    API_NAME_UNIVERSAL_06 = ('API check error of [naming errors]:The [$$] of members in the '
173                             'structure should follow the small hump format.')
174    API_NAME_UNIVERSAL_07 = ('API check error of [naming errors]:The [$$] of the consortium '
175                             'should follow the format of the Great hump.')
176    API_NAME_UNIVERSAL_08 = ('API check error of [naming errors]:The [$$] of members in the '
177                             'consortium should follow the small hump format.')
178    API_NAME_UNIVERSAL_09 = ('API check error of [naming errors]:The [$$] of a global variable '
179                             'must be prefixed with g_ in the small camel case format.')
180    API_NAME_UNIVERSAL_10 = ('API check error of [naming errors]:The [$$] of general functions '
181                             'should follow the big hump format.')
182    API_NAME_UNIVERSAL_11 = ('API check error of [naming errors]:Function parameter [$$] should '
183                             'follow the small hump format.')
184    API_NAME_UNIVERSAL_12 = ('API check error of [naming errors]:Macro [$$] should follow all '
185                             'uppercase format and separated by underscores.')
186    API_NAME_UNIVERSAL_13 = ('API check error of [naming errors]:Functional macro [$$] should '
187                             'follow all uppercase format and separated by underscores.')
188    API_NAME_UNIVERSAL_14 = ('API check error of [naming errors]:The [$$] file name should be all '
189                             'lowercase and separated by underscores.')
190    API_DOC_GLOBAL_01 = ('API check error of [api doc errors]:The [file] tag is repeat. Please '
191                         'check the tag in file.')
192    API_DOC_GLOBAL_02 = ('API check error of [api doc errors]:The file has the [addtogroup] tag, '
193                         'but do not has the [brief] tag.')
194    API_DOC_GLOBAL_03 = ('API check error of [api doc errors]:The file miss [file] tag, '
195                         'or the file comment is not at the beginning of the file')
196    API_DOC_GLOBAL_04 = ('API check error of [api doc errors]:The [addtogroup] tag is repeat. '
197                         'Please check the tag in file.')
198    API_DOC_GLOBAL_05 = ('API check error of [api doc errors]:The file has the [addtogroup] tag,'
199                         'but do not has the start tag [{].')
200    API_DOC_GLOBAL_06 = ('API check error of [api doc errors]:The [{] tag is not allowed to reuse '
201                         'in Doc which has [addtogroup] tag.')
202    API_DOC_GLOBAL_09 = ('API check error of [api doc errors]:The file missing [addtogroup] tag, '
203                         'or the file comment is not at the beginning of the file')
204    API_DOC_GLOBAL_10 = ('API check error of [api doc errors]:The file has the [addtogroup] tag,'
205                         'but do not has the end tag [}].')
206    API_DOC_GLOBAL_11 = ('API check error of [api doc errors]:The [addtogroup] tag value is empty,'
207                         'please supplement the default value.')
208    API_DOC_GLOBAL_12 = ('API check error of [api doc errors]:The [file] tag value is empty,please '
209                         'supplement the default value.')
210    API_DOC_GLOBAL_13 = ('API check error of [api doc errors]:The [library] tag value must be end '
211                         'with .so or .a, please check the usage method.')
212    API_DOC_GLOBAL_14 = ('API check error of [api doc errors]:The [{] tag is not allowed to used '
213                         'in Doc which not the [addtogroup] tag,or used in the wrong place.'),
214    API_DOC_GLOBAL_15 = 'API check error of [api doc errors]:Non lowercase labels used in API Doc information.'
215    API_DOC_GLOBAL_16 = 'API check error of [api doc errors]:The name after the [library] tag should be in lowercase.'
216    API_DOC_GLOBAL_17 = ('API check error of [api doc errors]:The file has the [file] tag, but do '
217                         'not has the [brief] tag.')
218    API_DOC_GLOBAL_18 = ('API check error of [api doc errors]:The file has the [file] tag, but do not '
219                         'has the [library] tag.')
220    API_DOC_GLOBAL_19 = ('API check error of [api doc errors]:The file has the [file] tag, but do not '
221                         'has the [syscap] tag.')
222    API_DOC_GLOBAL_20 = ('API check error of [api doc errors]:APIDoc tag validity verification failed. '
223                         'Please confirm if the [since] tag is missing.')
224    API_DOC_GLOBAL_21 = ('API check error of [api doc errors]:The validity verification of the APIDoc tag '
225                         'failed. The [}] tag is not allowed to be reused please delete the extra tags')
226    API_DOC_FUNCTION_01 = ('API check error of [api doc errors]:The count of the [param] tag is wrong.'
227                           'Please check the parameters and Doc.')
228    API_DOC_FUNCTION_02 = ('API check error of [api doc errors]:The value of the [$$] [param] tag is '
229                           'incorrect. Please check if it matches the [&&] parameter name.')
230    API_DOC_UNIVERSAL_01 = ('API check error of [api doc errors]:The [deprecated] tag value is incorrect.'
231                            'Please check the usage method.')
232    API_DOC_UNIVERSAL_02 = ('API check error of [api doc errors]:The [permission] tag value is incorrect. '
233                            'Please check if the permission field has been configured or update the '
234                            'configuration file.')
235    API_DOC_UNIVERSAL_03 = ('API check error of [api doc errors]:The [since] tag value is empty.Please '
236                            'supplement the default value.')
237    API_DOC_UNIVERSAL_04 = ('API check error of [api doc errors]:The [since] tag value is incorrect.'
238                            'Please check if the tag value is a numerical value or version number(number).')
239    API_DOC_UNIVERSAL_05 = ('API check error of [api doc errors]:The [syscap] tag value is empty.'
240                            'Please supplement the default value.')
241    API_DOC_UNIVERSAL_06 = ('API check error of [api doc errors]:The [syscap] tag value is incorrect.'
242                            'Please check if the syscap field is configured.')
243
244
245check_command_message = [
246    member.name for name_tool,
247    member in CheckErrorMessage.__members__.items()
248]
249
250
251class CheckOutPut:
252    analyzerName = 'apiengine'
253    buggyFilePath = ''
254    codeContextStartLine = -1
255    defectLevel = 0
256    defectType = ''
257    description = ''
258    language = 'c'
259    mainBuggyCode = ''
260    mainBuggyLine = -1
261
262    def __init__(self, buggy_file_path, code_context_start_line, defect_type, description,
263                 main_buggy_code, main_buggy_line):
264        self.analyzerName = 'apiengine'
265        self.buggyFilePath = buggy_file_path
266        if code_context_start_line == -1:
267            code_context_start_line = 0
268        self.codeContextStartLine = code_context_start_line
269        self.defectLevel = 2
270        self.defectType = defect_type
271        self.description = description
272        self.language = 'c++'
273        self.mainBuggyCode = main_buggy_code
274        if main_buggy_line == -1:
275            main_buggy_line = 0
276        self.mainBuggyLine = main_buggy_line
277
278
279class OutputTxt:
280    id = -1
281    level = -1
282    since = ''
283    location = ''
284    file_path = ''
285    message = ''
286
287    def __init__(self, id_param, level_param, api_since, location_param, file_path_param, message_param):
288        self.id = id_param
289        self.level = level_param
290        self.since = api_since
291        self.location = location_param
292        self.file_path = file_path_param
293        self.message = message_param
294
295    def get_id(self):
296        return self.id
297
298    def set_id(self, id_param):
299        self.id = id_param
300
301    def get_level(self):
302        return self.level
303
304    def set_level(self, level_param):
305        self.level = level_param
306
307    def get_since(self):
308        return self.since
309
310    def set_since(self, api_since):
311        self.level = api_since
312
313    def get_location(self):
314        return self.location
315
316    def set_location(self, location_param):
317        self.location = location_param
318
319    def get_file_path(self):
320        return self.file_path
321
322    def set_file_path(self, file_path_param):
323        self.file_path = file_path_param
324
325    def get_message(self):
326        return self.message
327
328    def set_message(self, message_param):
329        self.message = message_param
330
331
332class ApiResultInfo:
333    error_type: ErrorType = ErrorType.DEFAULT.value
334    error_info = ''
335    error_content = ''
336    level: ErrorLevel = -1
337    api_name = ''
338    api_since = ''
339    api_full_text = ''
340    file_name = ''
341    location = ''
342    location_line = -1
343    location_column = -1
344    type: LogType = LogType.DEFAULT.value
345    version = -1
346    basename = ''
347
348    def __init__(self, error_type_param=None, error_info_param='', api_name_param=''):
349        if error_type_param is None:
350            error_type_param = ErrorType.DEFAULT.value
351        self.error_type = error_type_param
352        self.error_info = error_info_param
353        self.api_name = api_name_param
354
355    def get_error_type(self):
356        return self.error_type
357
358    def set_error_type(self, error_type_param):
359        self.error_type = error_type_param
360
361    def get_file_name(self):
362        return self.file_name
363
364    def set_file_name(self, file_name_param):
365        self.file_name = file_name_param
366
367    def get_type(self):
368        return self.type
369
370    def set_type(self, type_param):
371        self.type = type_param
372
373    def get_error_info(self):
374        return self.error_info
375
376    def set_error_info(self, error_info_param):
377        self.error_info = error_info_param
378
379    def get_version(self):
380        return self.version
381
382    def set_version(self, version_param):
383        self.version = version_param
384
385    def get_basename(self):
386        return self.basename
387
388    def set_basename(self, basename_param):
389        self.basename = basename_param
390
391    def get_level(self):
392        return self.level
393
394    def set_level(self, level_param):
395        self.level = level_param
396
397    def get_api_name(self):
398        return self.api_name
399
400    def set_api_name(self, api_name_param):
401        self.api_name = api_name_param
402
403    def get_api_since(self):
404        return self.api_since
405
406    def set_api_since(self, api_since):
407        self.api_since = api_since
408
409    def get_api_full_text(self):
410        return self.api_full_text
411
412    def set_api_full_text(self, api_full_text_param):
413        self.api_full_text = api_full_text_param
414
415    def get_location_line(self):
416        return self.location_line
417
418    def set_location_line(self, location_line_param):
419        self.location_line = location_line_param
420
421    def get_location_column(self):
422        return self.location_column
423
424    def set_location_column(self, location_column_param):
425        self.location_column = location_column_param
426
427    def get_location(self):
428        return self.location
429
430    def set_location(self, location_param):
431        self.location = location_param
432
433    def get_error_content(self):
434        return self.error_content
435
436    def set_error_content(self, error_content):
437        self.error_content = error_content
438
439
440class DocInfo:
441    group = ''
442    brief = ''
443    deprecated = ''
444    file = ''
445    permission = ''
446    since = ''
447    syscap = ''
448    param_index = -1
449    throw_index = -1
450
451
452class FileDocInfo:
453    is_in_group_tag = False
454    group_name = None
455    group_brief = None
456    group_library = None
457    group_syscap = None
458    group_comment_str = ''
459    has_group_start = False
460    has_group_end = False
461    is_in_file_tag = False
462    file_name = None
463    file_brief = None
464    file_library = None
465    file_syscap = None
466    file_since = False
467    file_comment_str = ''
468    curr_doc_info = DocInfo()
469
470
471class CommentStartEndValue(enum.Enum):
472    DEFAULT_START = 0,
473    DEFAULT_END = 0
474
475