def GetFunctionList(): FuncObjList = [] for FuncDef in FileProfile.FunctionDefinitionList: ParamIdList = [] DeclText = FuncDef.Declarator.strip() while DeclText.startswith('*'): FuncDef.Modifier += '*' DeclText = DeclText.lstrip('*').strip() FuncDef.Declarator = FuncDef.Declarator.lstrip('*') DeclSplitList = FuncDef.Declarator.split('(') if len(DeclSplitList) < 2: continue FuncName = DeclSplitList[0] FuncNamePartList = FuncName.split() if len(FuncNamePartList) > 1: FuncName = FuncNamePartList[-1] Index = 0 while Index < len(FuncNamePartList) - 1: FuncDef.Modifier += ' ' + FuncNamePartList[Index] Index += 1 FuncObj = DataClass.FunctionClass( -1, FuncDef.Declarator, FuncDef.Modifier, FuncName.strip(), '', FuncDef.StartPos[0], FuncDef.StartPos[1], FuncDef.EndPos[0], FuncDef.EndPos[1], FuncDef.LeftBracePos[0], FuncDef.LeftBracePos[1], -1, ParamIdList, []) FuncObjList.append(FuncObj) return FuncObjList
def CreateCCodeDB(FileNameList): FileObjList = [] ParseErrorFileList = [] for FullName in FileNameList: if os.path.splitext(FullName)[1] in ('.h', '.c'): EdkLogger.info("Parsing " + FullName) model = FullName.endswith( 'c') and DataClass.MODEL_FILE_C or DataClass.MODEL_FILE_H collector = CodeFragmentCollector.CodeFragmentCollector(FullName) try: collector.ParseFile() except UnicodeError: ParseErrorFileList.append(FullName) BaseName = os.path.basename(FullName) DirName = os.path.dirname(FullName) Ext = os.path.splitext(BaseName)[1].lstrip('.') ModifiedTime = os.path.getmtime(FullName) FileObj = DataClass.FileClass(-1, BaseName, Ext, DirName, FullName, model, ModifiedTime, GetFunctionList(), GetIdentifierList(), []) FileObjList.append(FileObj) collector.CleanFileProfileBuffer() if len(ParseErrorFileList) > 0: EdkLogger.info("Found unrecoverable error during parsing:\n\t%s\n" % "\n\t".join(ParseErrorFileList)) Db = EotGlobalData.gDb for file in FileObjList: Db.InsertOneFile(file) Db.UpdateIdentifierBelongsToFunction()
# This acts like the main() function for the script, unless it is 'import'ed into another # script. # if __name__ == '__main__': EdkLogger.Initialize() #EdkLogger.SetLevel(EdkLogger.VERBOSE) EdkLogger.SetLevel(EdkLogger.DEBUG_0) EdkLogger.verbose("Start at " + time.strftime('%H:%M:%S', time.localtime())) Db = Database(DATABASE_PATH) Db.InitDatabase() Db.QueryTable(Db.TblDataModel) identifier1 = DataClass.IdentifierClass(-1, '', '', "i''1", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 32, 43, 54, 43) identifier2 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 15, 43, 20, 43) identifier3 = DataClass.IdentifierClass(-1, '', '', 'i1', 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 55, 43, 58, 43) identifier4 = DataClass.IdentifierClass(-1, '', '', "i1'", 'aaa', DataClass.MODEL_IDENTIFIER_COMMENT, 1, -1, 77, 43, 88, 43) fun1 = DataClass.FunctionClass(-1, '', '', 'fun1', '', 21, 2, 60, 45, 1, 23, 0, [], []) file = DataClass.FileClass( -1, 'F1', 'c', 'C:\\', 'C:\\F1.exe', DataClass.MODEL_FILE_C, '2007-12-28', [fun1],
def GetParamList(FuncDeclarator, FuncNameLine=0, FuncNameOffset=0): ParamIdList = [] DeclSplitList = FuncDeclarator.split('(') if len(DeclSplitList) < 2: return ParamIdList FuncName = DeclSplitList[0] ParamStr = DeclSplitList[1].rstrip(')') LineSkipped = 0 OffsetSkipped = 0 Start = 0 while FuncName.find('\n', Start) != -1: LineSkipped += 1 OffsetSkipped = 0 Start += FuncName.find('\n', Start) Start += 1 OffsetSkipped += len(FuncName[Start:]) OffsetSkipped += 1 #skip '(' ParamBeginLine = FuncNameLine + LineSkipped ParamBeginOffset = OffsetSkipped for p in ParamStr.split(','): ListP = p.split() if len(ListP) == 0: continue ParamName = ListP[-1] DeclText = ParamName.strip() RightSpacePos = p.rfind(ParamName) ParamModifier = p[0:RightSpacePos] if ParamName == 'OPTIONAL': if ParamModifier == '': ParamModifier += ' ' + 'OPTIONAL' DeclText = '' else: ParamName = ListP[-2] DeclText = ParamName.strip() RightSpacePos = p.rfind(ParamName) ParamModifier = p[0:RightSpacePos] ParamModifier += 'OPTIONAL' while DeclText.startswith('*'): ParamModifier += ' ' + '*' DeclText = DeclText.lstrip('*').strip() ParamName = DeclText Start = 0 while p.find('\n', Start) != -1: LineSkipped += 1 OffsetSkipped = 0 Start += p.find('\n', Start) Start += 1 OffsetSkipped += len(p[Start:]) ParamEndLine = ParamBeginLine + LineSkipped ParamEndOffset = OffsetSkipped IdParam = DataClass.IdentifierClass( -1, ParamModifier, '', ParamName, '', DataClass.MODEL_IDENTIFIER_PARAMETER, -1, -1, ParamBeginLine, ParamBeginOffset, ParamEndLine, ParamEndOffset) ParamIdList.append(IdParam) ParamBeginLine = ParamEndLine ParamBeginOffset = OffsetSkipped + 1 #skip ',' return ParamIdList
def GetIdentifierList(): IdList = [] for pp in FileProfile.PPDirectiveList: Type = GetIdType(pp.Content) IdPP = DataClass.IdentifierClass(-1, '', '', '', pp.Content, Type, -1, -1, pp.StartPos[0], pp.StartPos[1], pp.EndPos[0], pp.EndPos[1]) IdList.append(IdPP) for ae in FileProfile.AssignmentExpressionList: IdAE = DataClass.IdentifierClass( -1, ae.Operator, '', ae.Name, ae.Value, DataClass.MODEL_IDENTIFIER_ASSIGNMENT_EXPRESSION, -1, -1, ae.StartPos[0], ae.StartPos[1], ae.EndPos[0], ae.EndPos[1]) IdList.append(IdAE) FuncDeclPattern = GetFuncDeclPattern() ArrayPattern = GetArrayPattern() for var in FileProfile.VariableDeclarationList: DeclText = var.Declarator.strip() while DeclText.startswith('*'): var.Modifier += '*' DeclText = DeclText.lstrip('*').strip() var.Declarator = DeclText if FuncDeclPattern.match(var.Declarator): DeclSplitList = var.Declarator.split('(') FuncName = DeclSplitList[0] FuncNamePartList = FuncName.split() if len(FuncNamePartList) > 1: FuncName = FuncNamePartList[-1] Index = 0 while Index < len(FuncNamePartList) - 1: var.Modifier += ' ' + FuncNamePartList[Index] var.Declarator = var.Declarator.lstrip().lstrip( FuncNamePartList[Index]) Index += 1 IdVar = DataClass.IdentifierClass( -1, var.Modifier, '', var.Declarator, '', DataClass.MODEL_IDENTIFIER_FUNCTION_DECLARATION, -1, -1, var.StartPos[0], var.StartPos[1], var.EndPos[0], var.EndPos[1]) IdList.append(IdVar) continue if var.Declarator.find('{') == -1: for decl in var.Declarator.split(','): DeclList = decl.split('=') Name = DeclList[0].strip() if ArrayPattern.match(Name): LSBPos = var.Declarator.find('[') var.Modifier += ' ' + Name[LSBPos:] Name = Name[0:LSBPos] IdVar = DataClass.IdentifierClass( -1, var.Modifier, '', Name, (len(DeclList) > 1 and [DeclList[1]] or [''])[0], DataClass.MODEL_IDENTIFIER_VARIABLE, -1, -1, var.StartPos[0], var.StartPos[1], var.EndPos[0], var.EndPos[1]) IdList.append(IdVar) else: DeclList = var.Declarator.split('=') Name = DeclList[0].strip() if ArrayPattern.match(Name): LSBPos = var.Declarator.find('[') var.Modifier += ' ' + Name[LSBPos:] Name = Name[0:LSBPos] IdVar = DataClass.IdentifierClass( -1, var.Modifier, '', Name, (len(DeclList) > 1 and [DeclList[1]] or [''])[0], DataClass.MODEL_IDENTIFIER_VARIABLE, -1, -1, var.StartPos[0], var.StartPos[1], var.EndPos[0], var.EndPos[1]) IdList.append(IdVar) for enum in FileProfile.EnumerationDefinitionList: LBPos = enum.Content.find('{') RBPos = enum.Content.find('}') Name = enum.Content[4:LBPos].strip() Value = enum.Content[LBPos + 1:RBPos] IdEnum = DataClass.IdentifierClass( -1, '', '', Name, Value, DataClass.MODEL_IDENTIFIER_ENUMERATE, -1, -1, enum.StartPos[0], enum.StartPos[1], enum.EndPos[0], enum.EndPos[1]) IdList.append(IdEnum) for su in FileProfile.StructUnionDefinitionList: Type = DataClass.MODEL_IDENTIFIER_STRUCTURE SkipLen = 6 if su.Content.startswith('union'): Type = DataClass.MODEL_IDENTIFIER_UNION SkipLen = 5 LBPos = su.Content.find('{') RBPos = su.Content.find('}') if LBPos == -1 or RBPos == -1: Name = su.Content[SkipLen:].strip() Value = '' else: Name = su.Content[SkipLen:LBPos].strip() Value = su.Content[LBPos + 1:RBPos] IdPE = DataClass.IdentifierClass(-1, '', '', Name, Value, Type, -1, -1, su.StartPos[0], su.StartPos[1], su.EndPos[0], su.EndPos[1]) IdList.append(IdPE) TdFuncPointerPattern = GetTypedefFuncPointerPattern() for td in FileProfile.TypedefDefinitionList: Modifier = '' Name = td.ToType Value = td.FromType if TdFuncPointerPattern.match(td.ToType): Modifier = td.FromType LBPos = td.ToType.find('(') TmpStr = td.ToType[LBPos + 1:].strip() StarPos = TmpStr.find('*') if StarPos != -1: Modifier += ' ' + TmpStr[0:StarPos] while TmpStr[StarPos] == '*': Modifier += ' ' + '*' StarPos += 1 TmpStr = TmpStr[StarPos:].strip() RBPos = TmpStr.find(')') Name = TmpStr[0:RBPos] Value = 'FP' + TmpStr[RBPos + 1:] IdTd = DataClass.IdentifierClass(-1, Modifier, '', Name, Value, DataClass.MODEL_IDENTIFIER_TYPEDEF, -1, -1, td.StartPos[0], td.StartPos[1], td.EndPos[0], td.EndPos[1]) IdList.append(IdTd) for funcCall in FileProfile.FunctionCallingList: IdFC = DataClass.IdentifierClass( -1, '', '', funcCall.FuncName, funcCall.ParamList, DataClass.MODEL_IDENTIFIER_FUNCTION_CALLING, -1, -1, funcCall.StartPos[0], funcCall.StartPos[1], funcCall.EndPos[0], funcCall.EndPos[1]) IdList.append(IdFC) return IdList