用途
grepai-trace-callees 用于查找某个函数调用了哪些函数(callees),即回答“这个函数调用了什么?”。它适合在理解函数依赖、映射函数行为、查找深层嵌套依赖、代码理解与文档编写等场景中使用。
主要能力
- 查找指定函数的直接被调用函数(callees)。
- 输出每个被调用函数的文件路径、行号和调用上下文。
- 支持 fast(默认)和 precise 两种提取模式。
- 支持多种输出格式:普通文本、JSON、紧凑 JSON(AI 优化)、TOON(v0.26.0+)。
- 可结合 jq 等工具过滤结果,例如按文件类型筛选、排除测试函数、按类别计数。
- 可与 trace callers(谁调用我)配合,形成完整的依赖分析。
- 可与 trace graph 结合进行递归依赖分析。
使用方式
基本用法:
grepai trace callees "FunctionName"
示例:
grepai trace callees "ProcessOrder"
JSON 输出:
grepai trace callees "ProcessOrder" --json
紧凑 JSON(AI 优化):
grepai trace callees "ProcessOrder" --json --compact
TOON 输出(v0.26.0+):
grepai trace callees "ProcessOrder" --toon
注意:--json 与 --toon 互斥。
提取模式:
grepai trace callees "ProcessOrder" --mode fast grepai trace callees "ProcessOrder" --mode precise
模式对比:fast 速度快、准确度良好、无额外依赖;precise 速度稍慢、准确度优秀、依赖 tree-sitter。
适用场景
- 理解函数行为:查看复杂函数调用了哪些函数,映射数据流。
- 查找依赖:查看函数调用了哪些外部服务或数据库操作。
- 代码审查:识别函数的副作用,判断函数是否承担过多职责(被调用函数过多可能是代码异味)。
- 文档生成:导出依赖列表用于文档,例如:
grepai trace callees "initialize" --json | jq '.results[].callee'
- 脚本化分析:列出所有被调用函数、检查是否存在特定被调用函数、生成依赖报告。
包含的调用类型
trace 可发现:直接函数调用、方法调用、内置函数调用(取决于模式)。
限制
可能遗漏:动态/运行时调用、回调和闭包、接口方法调用(可能只显示接口而非实现)、基于反射的调用。
例如以下情况在静态分析时无法确定被调用者:
func process(fn func()) { fn() // Callee is unknown at static analysis time }
常见问题
- 函数未找到:检查拼写,确保函数存在于已索引文件中。
- 未找到被调用函数(但函数确实有调用):尝试 --mode precise;检查语言是否在 enabled_languages 中;确保 symbols.gob 是最新的(grepai watch)。
- 遗漏部分被调用函数:使用 --mode precise 提高准确度。
最佳实践
- 用于理解:非常适合学习新代码库。
- 与 callers 结合:获得完整依赖图景。
- 深度分析用 graph:需要递归时使用 trace graph。
- 过滤结果:聚焦相关被调用函数。
- 记录发现:导出为 markdown 用于文档。
输出格式示例
普通文本输出:
🔍 Callees of "ProcessOrder"
Mode: fast Function found in: services/order.go:40
Found 4 callees:
-
validateOrder File: services/order.go:45 Context: validateOrder(order)
-
calculateTotal File: services/order.go:48 Context: total := calculateTotal(order.Items)
-
applyDiscount File: services/order.go:51 Context: total = applyDiscount(total, order.Coupon)
-
sendConfirmation File: services/order.go:55 Context: sendConfirmation(order.Email, total)
Tip: Use 'grepai trace graph ProcessOrder' for recursive analysis
JSON 输出示例:
{ "query": "ProcessOrder", "mode": "callees", "count": 4, "results": [ { "file": "services/order.go", "line": 45, "callee": "validateOrder", "context": "validateOrder(order)" }, { "file": "services/order.go", "line": 48, "callee": "calculateTotal", "context": "total := calculateTotal(order.Items)" }, { "file": "services/order.go", "line": 51, "callee": "applyDiscount", "context": "total = applyDiscount(total, order.Coupon)" }, { "file": "services/order.go", "line": 55, "callee": "sendConfirmation", "context": "sendConfirmation(order.Email, total)" } ] }
紧凑 JSON 输出示例:
{ "q": "ProcessOrder", "m": "callees", "c": 4, "r": [ {"f": "services/order.go", "l": 45, "fn": "validateOrder"}, {"f": "services/order.go", "l": 48, "fn": "calculateTotal"}, {"f": "services/order.go", "l": 51, "fn": "applyDiscount"}, {"f": "services/order.go", "l": 55, "fn": "sendConfirmation"} ] }
脚本示例
列出所有被调用函数:
grepai trace callees "main" --json | jq -r '.results[].callee' | sort -u
检查是否存在特定被调用函数:
grepai trace callees "processOrder" --json | jq -e '.results[] | select(.callee == "sendEmail")' && echo "Yes" || echo "No"
生成依赖报告:
#!/bin/bash echo "# Function Dependencies Report" echo "" for fn in main initialize processOrder; do echo "## $fn" grepai trace callees "$fn" --json | jq -r '.results[].callee' | sed 's/^/- /' echo "" done
与 callers 的对比
trace callers:谁调用我?用于影响分析。 trace callees:我调用谁?用于行为分析。
组合分析:
grepai trace callers "processOrder" # Who uses this? grepai trace callees "processOrder" # What does it do?
过滤结果示例
按文件类型:
grepai trace callees "main" --json | jq '.results[] | select(.file | endswith(".go"))'
排除测试函数:
grepai trace callees "Login" --json | jq '.results[] | select(.callee | startswith("Test") | not)'
按类别计数:
grepai trace callees "processOrder" --json | jq '.results[].callee' | grep -c "db"
与 trace graph 结合
仅直接被调用函数:
grepai trace callees "main"
完整依赖树(递归):
grepai trace graph "main" --depth 3