X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FEcc%2Fc.py;h=12dbf664ba9bda6590a05fbf6063d7e05adad93a;hb=f7496d717357b9af78414d19679b073403812340;hp=a25532af06b5ffde2ae39ba70bd1326bc8983c21;hpb=8c3f9b4e3c639b6a694bdb8099267f646681a34c;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/Ecc/c.py b/BaseTools/Source/Python/Ecc/c.py index a25532af06..12dbf664ba 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,6 +11,7 @@ # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. # +from __future__ import print_function import sys import Common.LongFilePathOs as os import re @@ -271,7 +272,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 +551,7 @@ def CollectSourceCodeDataIntoDB(RootDir): Db.UpdateIdentifierBelongsToFunction() def GetTableID(FullFileName, ErrorMsgList=None): - if ErrorMsgList == None: + if ErrorMsgList is None: ErrorMsgList = [] Db = GetDB() @@ -575,7 +576,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 +602,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 +619,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 +630,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 +831,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 +854,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 +899,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 +984,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 +995,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 +1026,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 +1044,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 +1152,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) @@ -1172,7 +1173,9 @@ def GetVarInfo(PredVarList, FuncRecord, FullFileName, IsFuncCall=False, TargetTy else: TypeList = GetDataTypeFromModifier(Param.Modifier).split() Type = TypeList[-1] - if len(TypeList) > 1 and StarList != None: + if Type == '*' and len(TypeList) >= 2: + Type = TypeList[-2] + if len(TypeList) > 1 and StarList is not None: for Star in StarList: Type = Type.strip() Type = Type.rstrip(Star) @@ -1197,7 +1200,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) @@ -1228,7 +1231,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) @@ -1269,7 +1272,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]) @@ -1287,13 +1293,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 = [] @@ -1311,9 +1317,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]) @@ -1325,9 +1332,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]) @@ -1561,7 +1569,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) @@ -1626,12 +1634,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 @@ -1846,7 +1859,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 @@ -1921,12 +1940,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 @@ -1948,7 +1967,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 @@ -1959,7 +1978,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*': @@ -2000,12 +2019,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 @@ -2028,7 +2047,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 @@ -2038,7 +2057,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]) @@ -2079,7 +2098,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]): @@ -2107,7 +2126,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 @@ -2118,7 +2137,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]) @@ -2204,7 +2223,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() @@ -2216,6 +2236,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]) @@ -2262,7 +2286,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: @@ -2325,13 +2349,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'): @@ -2354,12 +2378,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(): @@ -2376,9 +2403,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 @@ -2412,7 +2439,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 @@ -2443,7 +2470,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 @@ -2607,10 +2634,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!')