@@ -170,6 +170,109 @@ def extract_function_name_from_call(function_node: Node):
170170 return None
171171
172172
173+ def extract_function_return_type (function_node : Node ):
174+ """
175+ 从函数定义或声明节点中提取返回类型。
176+ 函数定义的结构:type declarator body
177+ 函数声明的结构:type declarator;
178+ """
179+ if not function_node :
180+ return None
181+
182+ # 支持 function_definition 和 declaration 两种节点类型
183+ if function_node .type not in ['function_definition' , 'declaration' ]:
184+ return None
185+
186+ type_node = function_node .child_by_field_name ('type' )
187+ if not type_node :
188+ return 'void' # 默认返回类型
189+
190+ # 提取类型文本
191+ return_type_text = type_node .text .decode ('utf8' , errors = 'ignore' ).strip ()
192+ return return_type_text if return_type_text else 'void'
193+
194+
195+ def extract_function_parameters (function_node : Node ):
196+ """
197+ 从函数定义节点中提取参数列表。
198+ 返回参数列表的字符串表示,格式如: "int a, char *b, void"
199+ """
200+ if not function_node :
201+ return []
202+
203+ # 获取 declarator
204+ declarator = None
205+ if function_node .type == 'function_definition' :
206+ declarator = function_node .child_by_field_name ('declarator' )
207+ elif function_node .type == 'declaration' :
208+ declarator = function_node .child_by_field_name ('declarator' )
209+
210+ if not declarator :
211+ return []
212+
213+ # 查找 function_declarator
214+ def find_function_declarator (n , depth = 0 , max_depth = 10 ):
215+ """递归查找 function_declarator 节点"""
216+ if depth > max_depth :
217+ return None
218+ if n .type == 'function_declarator' :
219+ return n
220+ for child in n .children :
221+ result = find_function_declarator (child , depth + 1 , max_depth )
222+ if result :
223+ return result
224+ return None
225+
226+ func_declarator = find_function_declarator (declarator )
227+ if not func_declarator :
228+ return []
229+
230+ # 获取 parameter_list
231+ param_list = func_declarator .child_by_field_name ('parameters' )
232+ if not param_list :
233+ return []
234+
235+ # 提取参数
236+ parameters = []
237+ for child in param_list .children :
238+ if child .type == 'parameter_declaration' :
239+ # 提取参数类型和名称
240+ param_type_node = child .child_by_field_name ('type' )
241+ param_declarator = child .child_by_field_name ('declarator' )
242+
243+ param_type = param_type_node .text .decode ('utf8' , errors = 'ignore' ).strip () if param_type_node else ''
244+
245+ # 提取参数名
246+ param_name = None
247+ if param_declarator :
248+ # 查找 identifier
249+ def find_identifier_in_declarator (n , depth = 0 , max_depth = 5 ):
250+ if depth > max_depth :
251+ return None
252+ if n .type == 'identifier' :
253+ return n .text .decode ('utf8' , errors = 'ignore' )
254+ for c in n .children :
255+ result = find_identifier_in_declarator (c , depth + 1 , max_depth )
256+ if result :
257+ return result
258+ return None
259+
260+ param_name = find_identifier_in_declarator (param_declarator )
261+
262+ # 构建参数字符串
263+ if param_name :
264+ param_str = f"{ param_type } { param_name } " .strip ()
265+ else :
266+ param_str = param_type if param_type else 'void'
267+
268+ if param_str :
269+ parameters .append (param_str )
270+ elif child .type == 'variadic_parameter' :
271+ parameters .append ('...' )
272+
273+ return parameters
274+
275+
173276def traverse_c_ast_and_record (client : AstVisitorClient , file_path : str ):
174277 """
175278 解析 C 代码文件并使用 Tree-sitter 的 cursor 进行深度优先遍历,
@@ -264,6 +367,10 @@ def find_identifier_in_node(n, depth=0, max_depth=10):
264367 # 直接记录整个函数定义(包含签名和函数体)到 code 属性
265368 func_text = node .text .decode ('utf8' , errors = 'ignore' )
266369
370+ # 提取返回类型和参数列表
371+ return_type = extract_function_return_type (node )
372+ parameters = extract_function_parameters (node )
373+
267374 name_hierarchy = NameHierarchy (func_name_short , client .current_context_name ())
268375 symbol_id = client .recordSymbol (
269376 name_hierarchy ,
@@ -275,11 +382,16 @@ def find_identifier_in_node(n, depth=0, max_depth=10):
275382 full_name = client .symbolId_to_Name [symbol_id ]
276383 client .symbol_data [full_name ]['code' ] = func_text
277384
278- # 标记为用户自定义函数并创建图节点
385+ # 标记为用户自定义函数并创建图节点,包含返回类型和参数列表
386+ attributes = {
387+ 'category' : FunctionCategory .USER_DEFINED .value ,
388+ 'return_type' : return_type ,
389+ 'parameters' : parameters
390+ }
279391 client .recordSymbolKind (
280392 symbol_id ,
281393 srctrl .SymbolKind .FUNCTION ,
282- { 'category' : FunctionCategory . USER_DEFINED . value }
394+ attributes
283395 )
284396
285397 # 将作用域范围记录为整个函数(含签名),确保 code 包含签名
@@ -353,6 +465,10 @@ def has_function_body(n, depth=0, max_depth=10):
353465 # 提取完整的函数定义文本(包括返回类型、函数名、参数列表和函数体)
354466 func_text = node .text .decode ('utf8' , errors = 'ignore' )
355467
468+ # 提取返回类型和参数列表
469+ return_type = extract_function_return_type (node )
470+ parameters = extract_function_parameters (node )
471+
356472 name_hierarchy = NameHierarchy (func_name_short , client .current_context_name ())
357473 symbol_id = client .recordSymbol (name_hierarchy , node_path = file_path , tree_node = node , kind_hint = symbolKindToString (srctrl .SymbolKind .FUNCTION )) # <--- 修改
358474
@@ -377,7 +493,14 @@ def has_function_body(n, depth=0, max_depth=10):
377493 signature_text = func_text
378494
379495 client .symbol_data [full_name ]['signature' ] = signature_text
380- client .recordSymbolKind (symbol_id , srctrl .SymbolKind .FUNCTION ) # <--- 修改
496+
497+ # 创建属性字典,包含返回类型和参数列表
498+ attributes = {
499+ 'signature' : signature_text ,
500+ 'return_type' : return_type ,
501+ 'parameters' : parameters
502+ }
503+ client .recordSymbolKind (symbol_id , srctrl .SymbolKind .FUNCTION , attributes ) # <--- 修改
381504
382505 # 记录函数的作用域位置
383506 client .recordSymbolScopeLocation (symbol_id , source_range )
@@ -393,14 +516,23 @@ def has_function_body(n, depth=0, max_depth=10):
393516 # 提取完整的函数声明文本(包括返回类型、函数名、参数列表)
394517 declaration_text = node .text .decode ('utf8' , errors = 'ignore' )
395518
519+ # 提取返回类型和参数列表
520+ return_type = extract_function_return_type (node )
521+ parameters = extract_function_parameters (node )
522+
396523 name_hierarchy = NameHierarchy (func_name_short , client .current_context_name ())
397524 symbol_id = client .recordSymbol (name_hierarchy , node_path = file_path , tree_node = node , kind_hint = symbolKindToString (srctrl .SymbolKind .FUNCTION_DECLARATION )) # <--- 修改
398525
399526 # 将完整的函数声明文本存储到 code 属性
400527 full_name = client .symbolId_to_Name [symbol_id ]
401528 client .symbol_data [full_name ]['code' ] = declaration_text
402529
403- client .recordSymbolKind (symbol_id , srctrl .SymbolKind .FUNCTION_DECLARATION ) # <--- 修改
530+ # 创建属性字典,包含返回类型和参数列表
531+ attributes = {
532+ 'return_type' : return_type ,
533+ 'parameters' : parameters
534+ }
535+ client .recordSymbolKind (symbol_id , srctrl .SymbolKind .FUNCTION_DECLARATION , attributes ) # <--- 修改
404536 # print(f" [CLIENT] Recorded FUNCTION_DECLARATION: {client.symbolId_to_Name[symbol_id]} in {file_name}")
405537
406538 elif declarator .type == 'init_declarator' or declarator .type == 'declarator' : # 变量声明 (可能带初始化)
0 commit comments