分析单个文件的调用关系
(
&self,
symbols: &[crate::codegraph::treesitter::AstSymbolInstanceArc],
functions: &[FunctionInfo],
code_graph: &mut CodeGraph
)
| 1031 | |
| 1032 | /// 分析单个文件的调用关系 |
| 1033 | fn _analyze_file_call_relations( |
| 1034 | &self, |
| 1035 | symbols: &[crate::codegraph::treesitter::AstSymbolInstanceArc], |
| 1036 | functions: &[FunctionInfo], |
| 1037 | code_graph: &mut CodeGraph |
| 1038 | ) { |
| 1039 | // 分析每个AST符号 |
| 1040 | for symbol in symbols { |
| 1041 | let symbol_guard = symbol.read(); |
| 1042 | let symbol_ref = symbol_guard.as_ref(); |
| 1043 | |
| 1044 | // 检查是否为函数调用 |
| 1045 | if symbol_ref.symbol_type() == crate::codegraph::treesitter::structs::SymbolType::FunctionCall { |
| 1046 | let call_name = symbol_ref.name(); |
| 1047 | let call_file = symbol_ref.file_path(); |
| 1048 | let call_line = symbol_ref.full_range().start_point.row + 1; |
| 1049 | // 1. 先在本文件查找被调用函数 |
| 1050 | if let Some(callee_idx) = self._find_function_by_name_in_list(call_name, functions) { |
| 1051 | // 查找调用者函数(通过分析调用位置) |
| 1052 | if let Some(caller_idx) = self._find_caller_function_by_line(call_file, call_line, functions) { |
| 1053 | let callee = &functions[callee_idx]; |
| 1054 | let caller = &functions[caller_idx]; |
| 1055 | let relation = CallRelation { |
| 1056 | caller_id: caller.id, |
| 1057 | callee_id: callee.id, |
| 1058 | caller_name: caller.name.clone(), |
| 1059 | callee_name: callee.name.clone(), |
| 1060 | caller_file: caller.file_path.clone(), |
| 1061 | callee_file: callee.file_path.clone(), |
| 1062 | line_number: call_line, |
| 1063 | is_resolved: true, |
| 1064 | }; |
| 1065 | code_graph.add_call_relation(relation); |
| 1066 | continue; |
| 1067 | } |
| 1068 | } |
| 1069 | // 2. 跨文件查找被调用函数 |
| 1070 | if let Some(callee) = self._find_function_by_name_global(call_name) { |
| 1071 | // 查找调用者函数(通过分析调用位置) |
| 1072 | if let Some(caller_idx) = self._find_caller_function_by_line(call_file, call_line, functions) { |
| 1073 | let caller = &functions[caller_idx]; |
| 1074 | let relation = CallRelation { |
| 1075 | caller_id: caller.id, |
| 1076 | callee_id: callee.id, |
| 1077 | caller_name: caller.name.clone(), |
| 1078 | callee_name: callee.name.clone(), |
| 1079 | caller_file: caller.file_path.clone(), |
| 1080 | callee_file: callee.file_path.clone(), |
| 1081 | line_number: call_line, |
| 1082 | is_resolved: true, |
| 1083 | }; |
| 1084 | code_graph.add_call_relation(relation); |
| 1085 | continue; |
| 1086 | } |
| 1087 | } |
| 1088 | // 3. 无法解析的调用 |
| 1089 | self._handle_unresolved_call_legacy(call_name, call_file, call_line, functions, code_graph); |
| 1090 | } |
no test coverage detected