def to_token_id(kind): if (kind == "namespace"): return ASTNodeId.getNamespaceId() if (kind == "class"): return ASTNodeId.getClassId() if (kind == "struct"): return ASTNodeId.getStructId() if (kind == "enum"): return ASTNodeId.getEnumId() if (kind == "enumerator"): return ASTNodeId.getEnumValueId() if (kind == "union"): return ASTNodeId.getUnionId() if (kind == "member"): return ASTNodeId.getClassStructUnionMemberId() if (kind == "local"): return ASTNodeId.getLocalVariableId() if (kind == "variable"): return ASTNodeId.getVariableDefinitionId() if (kind == "prototype"): return ASTNodeId.getFunctionPrototypeId() if (kind == "function"): return ASTNodeId.getFunctionDefinitionId() if (kind == "macro"): return ASTNodeId.getMacroId() if (kind == "typedef"): return ASTNodeId.getTypedefId() if (kind == "externvar"): return ASTNodeId.getExternFwdDeclarationId()
def visitor(ast_node, ast_parent_node, parser): ast_node_location = ast_node.location ast_node_tunit_spelling = ast_node.translation_unit.spelling if (ast_node_location.file and ast_node_location.file.name == ast_node_tunit_spelling): # we are not interested in symbols which got into this TU via includes id = parser.get_ast_node_id(ast_node) usr = ast_node.referenced.get_usr() if ast_node.referenced else ast_node.get_usr() line = int(parser.get_ast_node_line(ast_node)) column = int(parser.get_ast_node_column(ast_node)) if id in [ ASTNodeId.getClassId(), ASTNodeId.getStructId(), ASTNodeId.getEnumId(), ASTNodeId.getEnumValueId(), # handle user-defined types ASTNodeId.getUnionId(), ASTNodeId.getTypedefId(), ASTNodeId.getUsingDeclarationId(), ASTNodeId.getFunctionId(), ASTNodeId.getMethodId(), # handle functions and methods ASTNodeId.getLocalVariableId(), ASTNodeId.getFunctionParameterId(), ASTNodeId.getFieldId(), # handle local/function variables and member variables ASTNodeId.getMacroDefinitionId(), ASTNodeId.getMacroInstantiationId() # handle macros ]: symbol_db.insert_single( get_basename(root_directory, ast_node_tunit_spelling), line, column, usr, extract_cursor_context(ast_node_tunit_spelling, line), ast_node.referenced._kind_id if ast_node.referenced else ast_node._kind_id, ast_node.is_definition() ) else: pass return ChildVisitResult.RECURSE.value # If we are positioned in TU of interest, then we'll traverse through all descendants return ChildVisitResult.CONTINUE.value # Otherwise, we'll skip to the next sibling
def to_ast_node_id(kind): if (kind == clang.cindex.CursorKind.NAMESPACE): return ASTNodeId.getNamespaceId() if (kind in [ clang.cindex.CursorKind.CLASS_DECL, clang.cindex.CursorKind.CLASS_TEMPLATE, clang.cindex.CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION ]): return ASTNodeId.getClassId() if (kind == clang.cindex.CursorKind.STRUCT_DECL): return ASTNodeId.getStructId() if (kind == clang.cindex.CursorKind.ENUM_DECL): return ASTNodeId.getEnumId() if (kind == clang.cindex.CursorKind.ENUM_CONSTANT_DECL): return ASTNodeId.getEnumValueId() if (kind == clang.cindex.CursorKind.UNION_DECL): return ASTNodeId.getUnionId() if (kind == clang.cindex.CursorKind.FIELD_DECL): return ASTNodeId.getFieldId() if (kind == clang.cindex.CursorKind.VAR_DECL): return ASTNodeId.getLocalVariableId() if (kind in [ clang.cindex.CursorKind.FUNCTION_DECL, clang.cindex.CursorKind.FUNCTION_TEMPLATE ]): return ASTNodeId.getFunctionId() if (kind in [ clang.cindex.CursorKind.CXX_METHOD, clang.cindex.CursorKind.CONSTRUCTOR, clang.cindex.CursorKind.DESTRUCTOR ]): return ASTNodeId.getMethodId() if (kind == clang.cindex.CursorKind.PARM_DECL): return ASTNodeId.getFunctionParameterId() if (kind == clang.cindex.CursorKind.TEMPLATE_TYPE_PARAMETER): return ASTNodeId.getTemplateTypeParameterId() if (kind == clang.cindex.CursorKind.TEMPLATE_NON_TYPE_PARAMETER): return ASTNodeId.getTemplateNonTypeParameterId() if (kind == clang.cindex.CursorKind.TEMPLATE_TEMPLATE_PARAMETER): return ASTNodeId.getTemplateTemplateParameterId() if (kind == clang.cindex.CursorKind.MACRO_DEFINITION): return ASTNodeId.getMacroDefinitionId() if (kind == clang.cindex.CursorKind.MACRO_INSTANTIATION): return ASTNodeId.getMacroInstantiationId() if (kind in [ clang.cindex.CursorKind.TYPEDEF_DECL, clang.cindex.CursorKind.TYPE_ALIAS_DECL ]): return ASTNodeId.getTypedefId() if (kind == clang.cindex.CursorKind.NAMESPACE_ALIAS): return ASTNodeId.getNamespaceAliasId() if (kind == clang.cindex.CursorKind.USING_DIRECTIVE): return ASTNodeId.getUsingDirectiveId() if (kind == clang.cindex.CursorKind.USING_DECLARATION): return ASTNodeId.getUsingDeclarationId() return ASTNodeId.getUnsupportedId()
def __find_all_references(self, id, args): start = time.clock() references = () tunit = self.parser.parse(str(args[0]), str(args[0])) if tunit: cursor = self.parser.get_cursor(tunit, int(args[1]), int(args[2])) if cursor: # TODO In order to make find-all-references work on edited (and not yet saved) files, # we would need to manipulate directly with USR. # In case of edited files, USR contains a name of a temporary file we serialized # the contents in and therefore will not match the USR in the database (which in # contrast contains an original filename). usr = cursor.referenced.get_usr( ) if cursor.referenced else cursor.get_usr() ast_node_id = self.parser.get_ast_node_id(cursor) if ast_node_id in [ ASTNodeId.getFunctionId(), ASTNodeId.getMethodId() ]: references = self.symbol_db.get_by_id(usr).fetchall() elif ast_node_id in [ ASTNodeId.getClassId(), ASTNodeId.getStructId(), ASTNodeId.getEnumId(), ASTNodeId.getEnumValueId(), ASTNodeId.getUnionId(), ASTNodeId.getTypedefId() ]: references = self.symbol_db.get_by_id(usr).fetchall() elif ast_node_id in [ ASTNodeId.getLocalVariableId(), ASTNodeId.getFunctionParameterId(), ASTNodeId.getFieldId() ]: references = self.symbol_db.get_by_id(usr).fetchall() elif ast_node_id in [ ASTNodeId.getMacroDefinitionId(), ASTNodeId.getMacroInstantiationId() ]: references = self.symbol_db.get_by_id(usr).fetchall() else: pass logging.info( "Find-all-references operation of '{0}', [{1}, {2}], '{3}' took {4}" .format(cursor.displayname, cursor.location.line, cursor.location.column, tunit.spelling, time.clock() - start)) if self.callback: self.callback(id, [args, references]) logging.info("\n{0}".format('\n'.join(str(ref) for ref in references)))
def to_ast_node_id(kind): if (kind == clang.cindex.CursorKind.NAMESPACE): return ASTNodeId.getNamespaceId() if (kind in [clang.cindex.CursorKind.CLASS_DECL, clang.cindex.CursorKind.CLASS_TEMPLATE, clang.cindex.CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION]): return ASTNodeId.getClassId() if (kind == clang.cindex.CursorKind.STRUCT_DECL): return ASTNodeId.getStructId() if (kind == clang.cindex.CursorKind.ENUM_DECL): return ASTNodeId.getEnumId() if (kind == clang.cindex.CursorKind.ENUM_CONSTANT_DECL): return ASTNodeId.getEnumValueId() if (kind == clang.cindex.CursorKind.UNION_DECL): return ASTNodeId.getUnionId() if (kind == clang.cindex.CursorKind.FIELD_DECL): return ASTNodeId.getFieldId() if (kind == clang.cindex.CursorKind.VAR_DECL): return ASTNodeId.getLocalVariableId() if (kind in [clang.cindex.CursorKind.FUNCTION_DECL, clang.cindex.CursorKind.FUNCTION_TEMPLATE]): return ASTNodeId.getFunctionId() if (kind in [clang.cindex.CursorKind.CXX_METHOD, clang.cindex.CursorKind.CONSTRUCTOR, clang.cindex.CursorKind.DESTRUCTOR]): return ASTNodeId.getMethodId() if (kind == clang.cindex.CursorKind.PARM_DECL): return ASTNodeId.getFunctionParameterId() if (kind == clang.cindex.CursorKind.TEMPLATE_TYPE_PARAMETER): return ASTNodeId.getTemplateTypeParameterId() if (kind == clang.cindex.CursorKind.TEMPLATE_NON_TYPE_PARAMETER): return ASTNodeId.getTemplateNonTypeParameterId() if (kind == clang.cindex.CursorKind.TEMPLATE_TEMPLATE_PARAMETER): return ASTNodeId.getTemplateTemplateParameterId() if (kind == clang.cindex.CursorKind.MACRO_DEFINITION): return ASTNodeId.getMacroDefinitionId() if (kind == clang.cindex.CursorKind.MACRO_INSTANTIATION): return ASTNodeId.getMacroInstantiationId() if (kind in [clang.cindex.CursorKind.TYPEDEF_DECL, clang.cindex.CursorKind.TYPE_ALIAS_DECL]): return ASTNodeId.getTypedefId() if (kind == clang.cindex.CursorKind.NAMESPACE_ALIAS): return ASTNodeId.getNamespaceAliasId() if (kind == clang.cindex.CursorKind.USING_DIRECTIVE): return ASTNodeId.getUsingDirectiveId() if (kind == clang.cindex.CursorKind.USING_DECLARATION): return ASTNodeId.getUsingDeclarationId() return ASTNodeId.getUnsupportedId()
def __tag_id_to_vim_syntax_group(tag_identifier): if tag_identifier == ASTNodeId.getNamespaceId(): return "yavideCppNamespace" if tag_identifier == ASTNodeId.getNamespaceAliasId(): return "yavideCppNamespaceAlias" if tag_identifier == ASTNodeId.getClassId(): return "yavideCppClass" if tag_identifier == ASTNodeId.getStructId(): return "yavideCppStructure" if tag_identifier == ASTNodeId.getEnumId(): return "yavideCppEnum" if tag_identifier == ASTNodeId.getEnumValueId(): return "yavideCppEnumValue" if tag_identifier == ASTNodeId.getUnionId(): return "yavideCppUnion" if tag_identifier == ASTNodeId.getFieldId(): return "yavideCppField" if tag_identifier == ASTNodeId.getLocalVariableId(): return "yavideCppLocalVariable" if tag_identifier == ASTNodeId.getFunctionId(): return "yavideCppFunction" if tag_identifier == ASTNodeId.getMethodId(): return "yavideCppMethod" if tag_identifier == ASTNodeId.getFunctionParameterId(): return "yavideCppFunctionParameter" if tag_identifier == ASTNodeId.getTemplateTypeParameterId(): return "yavideCppTemplateTypeParameter" if tag_identifier == ASTNodeId.getTemplateNonTypeParameterId(): return "yavideCppTemplateNonTypeParameter" if tag_identifier == ASTNodeId.getTemplateTemplateParameterId(): return "yavideCppTemplateTemplateParameter" if tag_identifier == ASTNodeId.getMacroDefinitionId(): return "yavideCppMacroDefinition" if tag_identifier == ASTNodeId.getMacroInstantiationId(): return "yavideCppMacroInstantiation" if tag_identifier == ASTNodeId.getTypedefId(): return "yavideCppTypedef" if tag_identifier == ASTNodeId.getUsingDirectiveId(): return "yavideCppUsingDirective" if tag_identifier == ASTNodeId.getUsingDeclarationId(): return "yavideCppUsingDeclaration"
def visitor(ast_node, ast_parent_node, parser): ast_node_location = ast_node.location ast_node_tunit_spelling = ast_node.translation_unit.spelling if ( ast_node_location.file and ast_node_location.file.name == ast_node_tunit_spelling ): # we are not interested in symbols which got into this TU via includes id = parser.get_ast_node_id(ast_node) usr = ast_node.referenced.get_usr( ) if ast_node.referenced else ast_node.get_usr() line = int(parser.get_ast_node_line(ast_node)) column = int(parser.get_ast_node_column(ast_node)) if id in [ ASTNodeId.getClassId(), ASTNodeId.getStructId(), ASTNodeId.getEnumId(), ASTNodeId.getEnumValueId(), # handle user-defined types ASTNodeId.getUnionId(), ASTNodeId.getTypedefId(), ASTNodeId.getUsingDeclarationId(), ASTNodeId.getFunctionId(), ASTNodeId.getMethodId(), # handle functions and methods ASTNodeId.getLocalVariableId(), ASTNodeId.getFunctionParameterId(), ASTNodeId.getFieldId( ), # handle local/function variables and member variables ASTNodeId.getMacroDefinitionId(), ASTNodeId.getMacroInstantiationId() # handle macros ]: symbol_db.insert_single( get_basename(root_directory, ast_node_tunit_spelling), line, column, usr, extract_cursor_context(ast_node_tunit_spelling, line), ast_node.referenced._kind_id if ast_node.referenced else ast_node._kind_id, ast_node.is_definition()) else: pass return ChildVisitResult.RECURSE.value # If we are positioned in TU of interest, then we'll traverse through all descendants return ChildVisitResult.CONTINUE.value # Otherwise, we'll skip to the next sibling
def __find_all_references(self, id, args): start = time.clock() references = () tunit = self.parser.parse(str(args[0]), str(args[0])) if tunit: cursor = self.parser.get_cursor(tunit, int(args[1]), int(args[2])) if cursor: # TODO In order to make find-all-references work on edited (and not yet saved) files, # we would need to manipulate directly with USR. # In case of edited files, USR contains a name of a temporary file we serialized # the contents in and therefore will not match the USR in the database (which in # contrast contains an original filename). usr = cursor.referenced.get_usr() if cursor.referenced else cursor.get_usr() ast_node_id = self.parser.get_ast_node_id(cursor) if ast_node_id in [ASTNodeId.getFunctionId(), ASTNodeId.getMethodId()]: references = self.symbol_db.get_by_id(usr).fetchall() elif ast_node_id in [ASTNodeId.getClassId(), ASTNodeId.getStructId(), ASTNodeId.getEnumId(), ASTNodeId.getEnumValueId(), ASTNodeId.getUnionId(), ASTNodeId.getTypedefId()]: references = self.symbol_db.get_by_id(usr).fetchall() elif ast_node_id in [ASTNodeId.getLocalVariableId(), ASTNodeId.getFunctionParameterId(), ASTNodeId.getFieldId()]: references = self.symbol_db.get_by_id(usr).fetchall() elif ast_node_id in [ASTNodeId.getMacroDefinitionId(), ASTNodeId.getMacroInstantiationId()]: references = self.symbol_db.get_by_id(usr).fetchall() else: pass logging.info("Find-all-references operation of '{0}', [{1}, {2}], '{3}' took {4}".format(cursor.displayname, cursor.location.line, cursor.location.column, tunit.spelling, time.clock() - start)) if self.callback: self.callback(id, [args, references]) logging.info("\n{0}".format('\n'.join(str(ref) for ref in references)))