You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何简化Tree-sitter查询以提取C++所有函数名?

简化Tree-sitter查询提取C++函数名

我需要用Tree-sitter query提取C++源码中的所有函数名,当前使用的查询语句如下:

(function_definition
    declarator: [
        (reference_declarator (function_declarator declarator: (_) @name))
        (function_declarator declarator: (_) @name)
    ]
 ) @func

测试用的C++源码:

void sayHello() {
    cout << "Hello, World!" << endl;
}

blah::mlah cram::greet(const string& name) {
    cout << "Hello, " << name << "!" << endl;
}

blah::mlah& greet(const string& name) {
    cout << "Hello, " << name << "!" << endl;
}

blah::mlah& cram::greet(const string& name) {
    cout << "Hello, " << name << "!" << endl;
}

我遇到两个问题:

  • 当函数返回对象引用时,必须通过reference_declarator节点才能访问function_declarator节点的设计过于繁琐;
  • 命令式编程中可以可选遍历reference_declarator,但Tree-sitter查询是匹配而非遍历逻辑,我翻遍了Tree-sitter查询规范也找不到简化方式,不知道如何用现有语法实现“给定函数声明,找到declarator的名称”的逻辑。

内容的提问来源于stack exchange,提问作者Ari Sweedler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:32:47