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 names 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 names 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 naming of enumeration ' 167 'types should follow the big hump rule.') 168 API_NAME_UNIVERSAL_04 = ('API check error of [naming errors]:The naming 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 naming of struct should ' 171 'follow the rules of the Great Hump.') 172 API_NAME_UNIVERSAL_06 = ('API check error of [naming errors]:The naming of members in the ' 173 'structure should follow the small hump format.') 174 API_NAME_UNIVERSAL_07 = ('API check error of [naming errors]:The naming of the consortium ' 175 'should follow the format of the large camel hump.') 176 API_NAME_UNIVERSAL_08 = ('API check error of [naming errors]:The naming of members in the ' 177 'consortium should follow the small hump format.') 178 API_NAME_UNIVERSAL_09 = ('API check error of [naming errors]:The names 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 naming of general functions ' 181 'should follow the big hump format.') 182 API_NAME_UNIVERSAL_11 = ('API check error of [naming errors]:Function parameter names should ' 183 'follow the small hump format.') 184 API_NAME_UNIVERSAL_12 = ('API check error of [naming errors]:Macro naming should follow all ' 185 'uppercase format and separated by underscores.') 186 API_NAME_UNIVERSAL_13 = ('API check error of [naming errors]:Functional macro naming 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 self.codeContextStartLine = code_context_start_line 267 self.defectLevel = 2 268 self.defectType = defect_type 269 self.description = description 270 self.language = 'c' 271 self.mainBuggyCode = main_buggy_code 272 self.mainBuggyLine = main_buggy_line 273 274 275class OutputTxt: 276 id = -1 277 level = -1 278 since = '' 279 location = '' 280 file_path = '' 281 message = '' 282 283 def __init__(self, id_param, level_param, api_since, location_param, file_path_param, message_param): 284 self.id = id_param 285 self.level = level_param 286 self.since = api_since 287 self.location = location_param 288 self.file_path = file_path_param 289 self.message = message_param 290 291 def get_id(self): 292 return self.id 293 294 def set_id(self, id_param): 295 self.id = id_param 296 297 def get_level(self): 298 return self.level 299 300 def set_level(self, level_param): 301 self.level = level_param 302 303 def get_since(self): 304 return self.since 305 306 def set_since(self, api_since): 307 self.level = api_since 308 309 def get_location(self): 310 return self.location 311 312 def set_location(self, location_param): 313 self.location = location_param 314 315 def get_file_path(self): 316 return self.file_path 317 318 def set_file_path(self, file_path_param): 319 self.file_path = file_path_param 320 321 def get_message(self): 322 return self.message 323 324 def set_message(self, message_param): 325 self.message = message_param 326 327 328class ApiResultInfo: 329 error_type: ErrorType = ErrorType.DEFAULT.value 330 error_info = '' 331 level: ErrorLevel = -1 332 api_name = '' 333 api_since = '' 334 api_full_text = '' 335 file_name = '' 336 location = '' 337 location_line = -1 338 location_column = -1 339 type: LogType = LogType.DEFAULT.value 340 version = -1 341 basename = '' 342 343 def __init__(self, error_type_param=None, error_info_param='', api_name_param=''): 344 if error_type_param is None: 345 error_type_param = ErrorType.DEFAULT.value 346 self.error_type = error_type_param 347 self.error_info = error_info_param 348 self.api_name = api_name_param 349 350 def get_error_type(self): 351 return self.error_type 352 353 def set_error_type(self, error_type_param): 354 self.error_type = error_type_param 355 356 def get_file_name(self): 357 return self.file_name 358 359 def set_file_name(self, file_name_param): 360 self.file_name = file_name_param 361 362 def get_type(self): 363 return self.type 364 365 def set_type(self, type_param): 366 self.type = type_param 367 368 def get_error_info(self): 369 return self.error_info 370 371 def set_error_info(self, error_info_param): 372 self.error_info = error_info_param 373 374 def get_version(self): 375 return self.version 376 377 def set_version(self, version_param): 378 self.version = version_param 379 380 def get_basename(self): 381 return self.basename 382 383 def set_basename(self, basename_param): 384 self.basename = basename_param 385 386 def get_level(self): 387 return self.level 388 389 def set_level(self, level_param): 390 self.level = level_param 391 392 def get_api_name(self): 393 return self.api_name 394 395 def set_api_name(self, api_name_param): 396 self.api_name = api_name_param 397 398 def get_api_since(self): 399 return self.api_since 400 401 def set_api_since(self, api_since): 402 self.api_since = api_since 403 404 def get_api_full_text(self): 405 return self.api_full_text 406 407 def set_api_full_text(self, api_full_text_param): 408 self.api_full_text = api_full_text_param 409 410 def get_location_line(self): 411 return self.location_line 412 413 def set_location_line(self, location_line_param): 414 self.location_line = location_line_param 415 416 def get_location_column(self): 417 return self.location_column 418 419 def set_location_column(self, location_column_param): 420 self.location_column = location_column_param 421 422 def get_location(self): 423 return self.location 424 425 def set_location(self, location_param): 426 self.location = location_param 427 428 429class DocInfo: 430 group = '' 431 brief = '' 432 deprecated = '' 433 file = '' 434 permission = '' 435 since = '' 436 syscap = '' 437 param_index = -1 438 throw_index = -1 439 440 441class FileDocInfo: 442 is_in_group_tag = False 443 group_name = None 444 group_brief = None 445 group_library = None 446 group_syscap = None 447 group_comment_str = '' 448 has_group_start = False 449 has_group_end = False 450 is_in_file_tag = False 451 file_name = None 452 file_brief = None 453 file_library = None 454 file_syscap = None 455 file_since = False 456 file_comment_str = '' 457 curr_doc_info = DocInfo() 458