X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FEcc%2Fc.py;h=5616c108533e47b51cdd062dfed64ea8df90acba;hb=f33d5d68abc02727dc828c1079e72ab65e1d63af;hp=997e631c97c195357eab18d4622ce9d0575732a3;hpb=83461d2ce88bdeb12b25c74beaca84dcd457e5a3;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/Ecc/c.py b/BaseTools/Source/Python/Ecc/c.py index 997e631c97..5616c10853 100644 --- a/BaseTools/Source/Python/Ecc/c.py +++ b/BaseTools/Source/Python/Ecc/c.py @@ -1,7 +1,7 @@ ## @file # This file is used to be the c coding style checking of ECC tool # -# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.
+# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at @@ -11,18 +11,20 @@ # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. # +from __future__ import print_function +from __future__ import absolute_import import sys import Common.LongFilePathOs as os import re import string -import CodeFragmentCollector -import FileProfile +from . import CodeFragmentCollector +from . import FileProfile from CommonDataClass import DataClass -import Database +from . import Database from Common import EdkLogger -from EccToolError import * -import EccGlobalData -import MetaDataParser +from .EccToolError import * +from . import EccGlobalData +from . import MetaDataParser IncludeFileListDict = {} AllIncludeFileListDict = {} @@ -271,7 +273,7 @@ def GetIdentifierList(): def StripNonAlnumChars(Str): StrippedStr = '' for Char in Str: - if Char.isalnum(): + if Char.isalnum() or Char == '_': StrippedStr += Char return StrippedStr @@ -550,7 +552,7 @@ def CollectSourceCodeDataIntoDB(RootDir): Db.UpdateIdentifierBelongsToFunction() def GetTableID(FullFileName, ErrorMsgList=None): - if ErrorMsgList == None: + if ErrorMsgList is None: ErrorMsgList = [] Db = GetDB() @@ -575,7 +577,7 @@ def GetIncludeFileList(FullFileName): if os.path.splitext(FullFileName)[1].upper() not in ('.H'): return [] IFList = IncludeFileListDict.get(FullFileName) - if IFList != None: + if IFList is not None: return IFList FileID = GetTableID(FullFileName) @@ -601,12 +603,12 @@ def GetFullPathOfIncludeFile(Str, IncludePathList): return None def GetAllIncludeFiles(FullFileName): - if AllIncludeFileListDict.get(FullFileName) != None: + if AllIncludeFileListDict.get(FullFileName) is not None: return AllIncludeFileListDict.get(FullFileName) FileDirName = os.path.dirname(FullFileName) IncludePathList = IncludePathListDict.get(FileDirName) - if IncludePathList == None: + if IncludePathList is None: IncludePathList = MetaDataParser.GetIncludeListOfFile(EccGlobalData.gWorkspace, FullFileName, GetDB()) if FileDirName not in IncludePathList: IncludePathList.insert(0, FileDirName) @@ -618,7 +620,7 @@ def GetAllIncludeFiles(FullFileName): FileName = FileName.strip('\"') FileName = FileName.lstrip('<').rstrip('>').strip() FullPath = GetFullPathOfIncludeFile(FileName, IncludePathList) - if FullPath != None: + if FullPath is not None: IncludeFileQueue.append(FullPath) i = 0 @@ -629,7 +631,7 @@ def GetAllIncludeFiles(FullFileName): FileName = FileName.strip('\"') FileName = FileName.lstrip('<').rstrip('>').strip() FullPath = GetFullPathOfIncludeFile(FileName, IncludePathList) - if FullPath != None and FullPath not in IncludeFileQueue: + if FullPath is not None and FullPath not in IncludeFileQueue: IncludeFileQueue.insert(i + 1, FullPath) i += 1 @@ -830,7 +832,7 @@ def GetDataTypeFromModifier(ModifierStr): MList = ModifierStr.split() ReturnType = '' for M in MList: - if M in EccGlobalData.gConfig.ModifierList: + if M in EccGlobalData.gConfig.ModifierSet: continue # remove array sufix if M.startswith('[') or M.endswith(']'): @@ -853,7 +855,7 @@ def DiffModifier(Str1, Str2): def GetTypedefDict(FullFileName): Dict = ComplexTypeDict.get(FullFileName) - if Dict != None: + if Dict is not None: return Dict FileID = GetTableID(FullFileName) @@ -898,7 +900,7 @@ def GetTypedefDict(FullFileName): def GetSUDict(FullFileName): Dict = SUDict.get(FullFileName) - if Dict != None: + if Dict is not None: return Dict FileID = GetTableID(FullFileName) @@ -983,9 +985,9 @@ def StripComments(Str): def GetFinalTypeValue(Type, FieldName, TypedefDict, SUDict): Value = TypedefDict.get(Type) - if Value == None: + if Value is None: Value = SUDict.get(Type) - if Value == None: + if Value is None: return None LBPos = Value.find('{') @@ -994,11 +996,11 @@ def GetFinalTypeValue(Type, FieldName, TypedefDict, SUDict): for FT in FTList: if FT not in ('struct', 'union'): Value = TypedefDict.get(FT) - if Value == None: + if Value is None: Value = SUDict.get(FT) break - if Value == None: + if Value is None: return None LBPos = Value.find('{') @@ -1025,11 +1027,11 @@ def GetFinalTypeValue(Type, FieldName, TypedefDict, SUDict): return None def GetRealType(Type, TypedefDict, TargetType=None): - if TargetType != None and Type == TargetType: + if TargetType is not None and Type == TargetType: return Type while TypedefDict.get(Type): Type = TypedefDict.get(Type) - if TargetType != None and Type == TargetType: + if TargetType is not None and Type == TargetType: return Type return Type @@ -1043,10 +1045,10 @@ def GetTypeInfo(RefList, Modifier, FullFileName, TargetType=None): while Index < len(RefList): FieldName = RefList[Index] FromType = GetFinalTypeValue(Type, FieldName, TypedefDict, SUDict) - if FromType == None: + if FromType is None: return None # we want to determine the exact type. - if TargetType != None: + if TargetType is not None: Type = FromType.split()[0] # we only want to check if it is a pointer else: @@ -1151,7 +1153,7 @@ def GetVarInfo(PredVarList, FuncRecord, FullFileName, IsFuncCall=False, TargetTy # Type = GetDataTypeFromModifier(Result[0]).split()[-1] TypeList = GetDataTypeFromModifier(Result[0]).split() Type = TypeList[-1] - if len(TypeList) > 1 and StarList != None: + if len(TypeList) > 1 and StarList is not None: for Star in StarList: Type = Type.strip() Type = Type.rstrip(Star) @@ -1174,7 +1176,7 @@ def GetVarInfo(PredVarList, FuncRecord, FullFileName, IsFuncCall=False, TargetTy Type = TypeList[-1] if Type == '*' and len(TypeList) >= 2: Type = TypeList[-2] - if len(TypeList) > 1 and StarList != None: + if len(TypeList) > 1 and StarList is not None: for Star in StarList: Type = Type.strip() Type = Type.rstrip(Star) @@ -1199,7 +1201,7 @@ def GetVarInfo(PredVarList, FuncRecord, FullFileName, IsFuncCall=False, TargetTy else: TypeList = GetDataTypeFromModifier(Result[0]).split() Type = TypeList[-1] - if len(TypeList) > 1 and StarList != None: + if len(TypeList) > 1 and StarList is not None: for Star in StarList: Type = Type.strip() Type = Type.rstrip(Star) @@ -1230,7 +1232,7 @@ def GetVarInfo(PredVarList, FuncRecord, FullFileName, IsFuncCall=False, TargetTy else: TypeList = GetDataTypeFromModifier(Result[0]).split() Type = TypeList[-1] - if len(TypeList) > 1 and StarList != None: + if len(TypeList) > 1 and StarList is not None: for Star in StarList: Type = Type.strip() Type = Type.rstrip(Star) @@ -1271,7 +1273,10 @@ def CheckFuncLayoutReturnType(FullFileName): FuncName = Result[5] if EccGlobalData.gException.IsException(ERROR_C_FUNCTION_LAYOUT_CHECK_RETURN_TYPE, FuncName): continue - Index = Result[0].find(TypeStart) + Result0 = Result[0] + if Result0.upper().startswith('STATIC'): + Result0 = Result0[6:].strip() + Index = Result0.find(TypeStart) if Index != 0 or Result[3] != 0: PrintErrorMsg(ERROR_C_FUNCTION_LAYOUT_CHECK_RETURN_TYPE, '[%s] Return Type should appear at the start of line' % FuncName, FileTable, Result[1]) @@ -1289,13 +1294,13 @@ def CheckFuncLayoutReturnType(FullFileName): FuncName = Result[5] if EccGlobalData.gException.IsException(ERROR_C_FUNCTION_LAYOUT_CHECK_RETURN_TYPE, FuncName): continue - Index = Result[0].find(ReturnType) + Result0 = Result[0] + if Result0.upper().startswith('STATIC'): + Result0 = Result0[6:].strip() + Index = Result0.find(ReturnType) if Index != 0 or Result[3] != 0: PrintErrorMsg(ERROR_C_FUNCTION_LAYOUT_CHECK_RETURN_TYPE, '[%s] Return Type should appear at the start of line' % FuncName, 'Function', Result[1]) - if Result[2] == Result[4]: - PrintErrorMsg(ERROR_C_FUNCTION_LAYOUT_CHECK_RETURN_TYPE, '[%s] Return Type should appear on its own line' % FuncName, 'Function', Result[1]) - def CheckFuncLayoutModifier(FullFileName): ErrorMsgList = [] @@ -1313,9 +1318,10 @@ def CheckFuncLayoutModifier(FullFileName): for Result in ResultSet: ReturnType = GetDataTypeFromModifier(Result[0]) TypeStart = ReturnType.split()[0] -# if len(ReturnType) == 0: -# continue - Index = Result[0].find(TypeStart) + Result0 = Result[0] + if Result0.upper().startswith('STATIC'): + Result0 = Result0[6:].strip() + Index = Result0.find(TypeStart) if Index != 0: PrintErrorMsg(ERROR_C_FUNCTION_LAYOUT_CHECK_OPTIONAL_FUNCTIONAL_MODIFIER, '', FileTable, Result[1]) @@ -1327,9 +1333,10 @@ def CheckFuncLayoutModifier(FullFileName): for Result in ResultSet: ReturnType = GetDataTypeFromModifier(Result[0]) TypeStart = ReturnType.split()[0] -# if len(ReturnType) == 0: -# continue - Index = Result[0].find(TypeStart) + Result0 = Result[0] + if Result0.upper().startswith('STATIC'): + Result0 = Result0[6:].strip() + Index = Result0.find(TypeStart) if Index != 0: PrintErrorMsg(ERROR_C_FUNCTION_LAYOUT_CHECK_OPTIONAL_FUNCTIONAL_MODIFIER, '', 'Function', Result[1]) @@ -1563,7 +1570,7 @@ def CheckMemberVariableFormat(Name, Value, FileTable, TdId, ModelId): Fields = Value[LBPos + 1 : RBPos] Fields = StripComments(Fields).strip() NestPos = Fields.find ('struct') - if NestPos != -1 and (NestPos + len('struct') < len(Fields)): + if NestPos != -1 and (NestPos + len('struct') < len(Fields)) and ModelId != DataClass.MODEL_IDENTIFIER_UNION: if not Fields[NestPos + len('struct') + 1].isalnum(): if not EccGlobalData.gException.IsException(ERROR_DECLARATION_DATA_TYPE_CHECK_NESTED_STRUCTURE, Name): PrintErrorMsg(ERROR_DECLARATION_DATA_TYPE_CHECK_NESTED_STRUCTURE, 'Nested struct in [%s].' % (Name), FileTable, TdId) @@ -1628,12 +1635,17 @@ def CheckMemberVariableFormat(Name, Value, FileTable, TdId, ModelId): Field = Field.strip() if Field == '': continue + if Field.startswith("#"): + continue # Enum could directly assign value to variable Field = Field.split('=')[0].strip() TokenList = Field.split() # Remove pointers before variable - if not Pattern.match(TokenList[-1].lstrip('*')): - ErrMsgList.append(TokenList[-1].lstrip('*')) + Token = TokenList[-1] + if Token in ['OPTIONAL']: + Token = TokenList[-2] + if not Pattern.match(Token.lstrip('*')): + ErrMsgList.append(Token.lstrip('*')) return ErrMsgList @@ -1848,7 +1860,13 @@ def CheckDeclNoUseCType(FullFileName): for Result in ResultSet: for Type in CTypeTuple: if PatternInModifier(Result[0], Type): - PrintErrorMsg(ERROR_DECLARATION_DATA_TYPE_CHECK_NO_USE_C_TYPE, 'Variable type %s' % Type, FileTable, Result[2]) + if EccGlobalData.gException.IsException(ERROR_DECLARATION_DATA_TYPE_CHECK_NO_USE_C_TYPE, + Result[0] + ' ' + Result[1]): + continue + PrintErrorMsg(ERROR_DECLARATION_DATA_TYPE_CHECK_NO_USE_C_TYPE, + 'Invalid variable type (%s) in definition [%s]' % (Type, Result[0] + ' ' + Result[1]), + FileTable, + Result[2]) break SqlStatement = """ select Modifier, Name, ID, Value @@ -1923,12 +1941,12 @@ def CheckPointerNullComparison(FullFileName): p = GetFuncDeclPattern() for Str in PSL: FuncRecord = GetFuncContainsPE(Str[1], FL) - if FuncRecord == None: + if FuncRecord is None: continue for Exp in GetPredicateListFromPredicateExpStr(Str[0]): PredInfo = SplitPredicateStr(Exp) - if PredInfo[1] == None: + if PredInfo[1] is None: PredVarStr = PredInfo[0][0].strip() IsFuncCall = False SearchInCache = False @@ -1950,7 +1968,7 @@ def CheckPointerNullComparison(FullFileName): continue if SearchInCache: Type = FuncReturnTypeDict.get(PredVarStr) - if Type != None: + if Type is not None: if Type.find('*') != -1 and Type != 'BOOLEAN*': PrintErrorMsg(ERROR_PREDICATE_EXPRESSION_CHECK_COMPARISON_NULL_TYPE, 'Predicate Expression: %s' % Exp, FileTable, Str[2]) continue @@ -1961,7 +1979,7 @@ def CheckPointerNullComparison(FullFileName): Type = GetVarInfo(PredVarList, FuncRecord, FullFileName, IsFuncCall, None, StarList) if SearchInCache: FuncReturnTypeDict[PredVarStr] = Type - if Type == None: + if Type is None: continue Type = GetTypeFromArray(Type, PredVarStr) if Type.find('*') != -1 and Type != 'BOOLEAN*': @@ -2002,12 +2020,12 @@ def CheckNonBooleanValueComparison(FullFileName): p = GetFuncDeclPattern() for Str in PSL: FuncRecord = GetFuncContainsPE(Str[1], FL) - if FuncRecord == None: + if FuncRecord is None: continue for Exp in GetPredicateListFromPredicateExpStr(Str[0]): PredInfo = SplitPredicateStr(Exp) - if PredInfo[1] == None: + if PredInfo[1] is None: PredVarStr = PredInfo[0][0].strip() IsFuncCall = False SearchInCache = False @@ -2030,7 +2048,7 @@ def CheckNonBooleanValueComparison(FullFileName): if SearchInCache: Type = FuncReturnTypeDict.get(PredVarStr) - if Type != None: + if Type is not None: if Type.find('BOOLEAN') == -1: PrintErrorMsg(ERROR_PREDICATE_EXPRESSION_CHECK_NO_BOOLEAN_OPERATOR, 'Predicate Expression: %s' % Exp, FileTable, Str[2]) continue @@ -2040,7 +2058,7 @@ def CheckNonBooleanValueComparison(FullFileName): Type = GetVarInfo(PredVarList, FuncRecord, FullFileName, IsFuncCall, 'BOOLEAN', StarList) if SearchInCache: FuncReturnTypeDict[PredVarStr] = Type - if Type == None: + if Type is None: continue if Type.find('BOOLEAN') == -1: PrintErrorMsg(ERROR_PREDICATE_EXPRESSION_CHECK_NO_BOOLEAN_OPERATOR, 'Predicate Expression: %s' % Exp, FileTable, Str[2]) @@ -2081,7 +2099,7 @@ def CheckBooleanValueComparison(FullFileName): p = GetFuncDeclPattern() for Str in PSL: FuncRecord = GetFuncContainsPE(Str[1], FL) - if FuncRecord == None: + if FuncRecord is None: continue for Exp in GetPredicateListFromPredicateExpStr(Str[0]): @@ -2109,7 +2127,7 @@ def CheckBooleanValueComparison(FullFileName): if SearchInCache: Type = FuncReturnTypeDict.get(PredVarStr) - if Type != None: + if Type is not None: if Type.find('BOOLEAN') != -1: PrintErrorMsg(ERROR_PREDICATE_EXPRESSION_CHECK_BOOLEAN_VALUE, 'Predicate Expression: %s' % Exp, FileTable, Str[2]) continue @@ -2120,7 +2138,7 @@ def CheckBooleanValueComparison(FullFileName): Type = GetVarInfo(PredVarList, FuncRecord, FullFileName, IsFuncCall, 'BOOLEAN', StarList) if SearchInCache: FuncReturnTypeDict[PredVarStr] = Type - if Type == None: + if Type is None: continue if Type.find('BOOLEAN') != -1: PrintErrorMsg(ERROR_PREDICATE_EXPRESSION_CHECK_BOOLEAN_VALUE, 'Predicate Expression: %s' % Exp, FileTable, Str[2]) @@ -2206,7 +2224,8 @@ def CheckDoxygenCommand(FullFileName): where Model = %d or Model = %d """ % (FileTable, DataClass.MODEL_IDENTIFIER_COMMENT, DataClass.MODEL_IDENTIFIER_FUNCTION_HEADER) ResultSet = Db.TblFile.Exec(SqlStatement) - DoxygenCommandList = ['bug', 'todo', 'example', 'file', 'attention', 'param', 'post', 'pre', 'retval', 'return', 'sa', 'since', 'test', 'note', 'par'] + DoxygenCommandList = ['bug', 'todo', 'example', 'file', 'attention', 'param', 'post', 'pre', 'retval', + 'return', 'sa', 'since', 'test', 'note', 'par', 'endcode', 'code'] for Result in ResultSet: CommentStr = Result[0] CommentPartList = CommentStr.split() @@ -2218,6 +2237,10 @@ def CheckDoxygenCommand(FullFileName): if Part.startswith('@'): if EccGlobalData.gException.IsException(ERROR_DOXYGEN_CHECK_COMMAND, Part): continue + if not Part.replace('@', '').strip(): + continue + if Part.lstrip('@') in ['{', '}']: + continue if Part.lstrip('@').isalpha(): if Part.lstrip('@') not in DoxygenCommandList: PrintErrorMsg(ERROR_DOXYGEN_CHECK_COMMAND, 'Unknown doxygen command %s' % Part, FileTable, Result[1]) @@ -2264,7 +2287,7 @@ def CheckDoxygenTripleForwardSlash(FullFileName): for Result in ResultSet: CommentSet.append(Result) except: - print 'Unrecognized chars in comment of file %s', FullFileName + print('Unrecognized chars in comment of file %s', FullFileName) for Result in CommentSet: @@ -2327,13 +2350,13 @@ def CheckFileHeaderDoxygenComments(FullFileName): if (len(CommentStrListTemp) <= 1): # For Mac CommentStrListTemp = CommentStr.split('\r') - # Skip the content before the file header + # Skip the content before the file header for CommentLine in CommentStrListTemp: if CommentLine.strip().startswith('/** @file'): FileStartFlag = True if FileStartFlag == True: CommentStrList.append(CommentLine) - + ID = Result[1] Index = 0 if CommentStrList and CommentStrList[0].strip().startswith('/** @file'): @@ -2356,12 +2379,15 @@ def CheckFileHeaderDoxygenComments(FullFileName): if EccGlobalData.gConfig.HeaderCheckCFileCommentStartSpacesNum == '1' or EccGlobalData.gConfig.HeaderCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1': if CommentLine.startswith('/** @file') == False and CommentLine.startswith('**/') == False and CommentLine.strip() and CommentLine.startswith(' ') == False: PrintErrorMsg(ERROR_HEADER_CHECK_FILE, 'File header comment content should start with two spaces at each line', FileTable, ID) - + CommentLine = CommentLine.strip() if CommentLine.startswith('Copyright'): NoCopyrightFlag = False if CommentLine.find('All rights reserved') == -1: - PrintErrorMsg(ERROR_HEADER_CHECK_FILE, '""All rights reserved"" announcement should be following the ""Copyright"" at the same line', FileTable, ID) + for Copyright in EccGlobalData.gConfig.Copyright: + if CommentLine.find(Copyright) > -1: + PrintErrorMsg(ERROR_HEADER_CHECK_FILE, '""All rights reserved"" announcement should be following the ""Copyright"" at the same line', FileTable, ID) + break if CommentLine.endswith('
') == -1: PrintErrorMsg(ERROR_HEADER_CHECK_FILE, 'The ""
"" at the end of the Copyright line is required', FileTable, ID) if NextLineIndex < len(CommentStrList) and CommentStrList[NextLineIndex].strip().startswith('Copyright') == False and CommentStrList[NextLineIndex].strip(): @@ -2378,9 +2404,9 @@ def CheckFileHeaderDoxygenComments(FullFileName): # Check whether C File header Comment's each reference at list should begin with a bullet character. if EccGlobalData.gConfig.HeaderCheckCFileCommentReferenceFormat == '1' or EccGlobalData.gConfig.HeaderCheckAll == '1' or EccGlobalData.gConfig.CheckAll == '1': if RefListFlag == True: - if RefLine.strip() and RefLine.strip().startswith('**/') == False and RefLine.startswith(' -') == False: - PrintErrorMsg(ERROR_HEADER_CHECK_FILE, 'Each reference on a separate line should begin with a bullet character ""-"" ', FileTable, ID) - + if RefLine.strip() and RefLine.strip().startswith('**/') == False and RefLine.startswith(' -') == False: + PrintErrorMsg(ERROR_HEADER_CHECK_FILE, 'Each reference on a separate line should begin with a bullet character ""-"" ', FileTable, ID) + if NoHeaderCommentStartFlag: PrintErrorMsg(ERROR_DOXYGEN_CHECK_FILE_HEADER, 'File header comment should begin with ""/** @file""', FileTable, ID) return @@ -2414,7 +2440,7 @@ def CheckFuncHeaderDoxygenComments(FullFileName): for Result in ResultSet: CommentSet.append(Result) except: - print 'Unrecognized chars in comment of file %s', FullFileName + print('Unrecognized chars in comment of file %s', FullFileName) # Func Decl check SqlStatement = """ select Modifier, Name, StartLine, ID, Value @@ -2445,7 +2471,7 @@ def CheckFuncHeaderDoxygenComments(FullFileName): for Result in ResultSet: CommentSet.append(Result) except: - print 'Unrecognized chars in comment of file %s', FullFileName + print('Unrecognized chars in comment of file %s', FullFileName) SqlStatement = """ select Modifier, Header, StartLine, ID, Name from Function @@ -2609,10 +2635,10 @@ if __name__ == '__main__': # CollectSourceCodeDataIntoDB(sys.argv[1]) try: test_file = sys.argv[1] - except IndexError, v: - print "Usage: %s filename" % sys.argv[0] + except IndexError as v: + print("Usage: %s filename" % sys.argv[0]) sys.exit(1) MsgList = CheckFuncHeaderDoxygenComments(test_file) for Msg in MsgList: - print Msg - print 'Done!' + print(Msg) + print('Done!')