mcpField
G
grepai-trace-callees

grepai-trace-callees

github.com/yoanbernabeu开发20772 次安装38 周前更新
代码仓库

关于 grepai-trace-callees

grepai-trace-callees 是 grepai 的代码分析技能,用于查询指定函数直接调用的函数和方法。它提供多种输出格式与提取模式,帮助开发者梳理行为和依赖。

主要功能

  1. 01查询函数直接调用项
  2. 02支持快速与精确模式
  3. 03输出 JSON、紧凑 JSON 或 TOON
  4. 04可用于依赖脚本与筛选

适用场景

  1. 01理解复杂函数的行为
  2. 02检查函数调用及副作用
  3. 03生成函数依赖文档

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/yoanbernabeu/grepai-skills --skill grepai-trace-callees

安装后在 Claude Code 中输入/grepai-trace-callees确认已加载。

详细介绍

GrepAI 跟踪被调用函数

本技能介绍如何使用 grepai trace callees 查找特定函数调用的所有函数。

何时使用此技能

  • 了解函数依赖关系
  • 梳理函数行为
  • 查找深层嵌套的依赖项
  • 理解和记录代码

什么是跟踪被调用函数?

grepai trace callees 回答:“此函数调用了什么?”

func ProcessOrder(order) {
    validateOrder(order)
    calculateTotal(order)
    sendConfirmation(order.email)
}
        │
        ↓
┌───────┴───────────────────┐
│  What does ProcessOrder   │
│  call?                    │
├───────────────────────────┤
│ • validateOrder           │
│ • calculateTotal          │
│ • sendConfirmation        │
└───────────────────────────┘

基本用法

grepai trace callees "FunctionName"
示例
grepai trace callees "ProcessOrder"

输出:

🔍 Callees of "ProcessOrder"

Found 4 callees:

1. validateOrder
   File: services/order.go:45
   Context: validateOrder(order)

2. calculateTotal
   File: services/order.go:48
   Context: total := calculateTotal(order.Items)

3. applyDiscount
   File: services/order.go:51
   Context: total = applyDiscount(total, order.Coupon)

4. sendConfirmation
   File: services/order.go:55
   Context: sendConfirmation(order.Email, total)

JSON 输出

grepai trace callees "ProcessOrder" --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(针对 AI 优化)

grepai trace callees "ProcessOrder" --json --compact

输出:

{
  "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"}
  ]
}

TOON 输出(v0.26.0+)

TOON 格式比 JSON 减少约 50% 的令牌:

grepai trace callees "ProcessOrder" --toon

注意: --json 和 --toon 不能同时使用。

提取模式

快速模式(默认)
grepai trace callees "ProcessOrder" --mode fast
精确模式
grepai trace callees "ProcessOrder" --mode precise
模式速度准确度依赖项
fast⚡⚡⚡良好无
precise⚡⚡极佳tree-sitter

使用场景

了解函数行为
# What does this complex function do?
grepai trace callees "handleRequest"

# Map the data flow
grepai trace callees "processPayment"
查找依赖项
# What external services does this call?
grepai trace callees "syncData"

# What database operations happen?
grepai trace callees "saveUser"
代码审查
# What side effects does this function have?
grepai trace callees "updateProfile"

# Is this function doing too much?
grepai trace callees "doEverything"  # Lots of callees = code smell
文档编写
# Generate dependency list for docs
grepai trace callees "initialize" --json | jq '.results[].callee'

调用者与被调用函数

命令问题使用场景
trace callers谁调用了我?影响分析
trace callees我调用了什么?行为分析
# Combined analysis
grepai trace callers "processOrder"   # Who uses this?
grepai trace callees "processOrder"   # What does it do?

筛选结果

按文件类型筛选
# Get callees and filter to only .go files
grepai trace callees "main" --json | jq '.results[] | select(.file | endswith(".go"))'
排除测试函数
grepai trace callees "Login" --json | jq '.results[] | select(.callee | startswith("Test") | not)'
按类别计数
# Count how many database vs. API calls
grepai trace callees "processOrder" --json | jq '.results[].callee' | grep -c "db"

被调用函数包含的内容

跟踪结果包括:

  • 直接函数调用
  • 方法调用
  • 内置函数调用(取决于模式)
示例
func ProcessOrder(order Order) error {
    // Direct call
    validateOrder(order)

    // Method call
    order.Validate()

    // Package function
    utils.Log("processing")

    // Built-in (may or may not be captured)
    fmt.Println("done")

    return nil
}

找到的被调用函数:

  • validateOrder
  • Validate(方法)
  • Log(来自 utils)
  • Println(取决于模式)

局限性

被调用函数跟踪可能遗漏的内容
  • 动态/运行时调用
  • 回调和闭包
  • 接口方法调用(可能显示接口,而非实现)
  • 基于反射的调用
未检测到调用的示例
func process(fn func()) {
    fn()  // Callee is unknown at static analysis time
}

与跟踪图结合使用

如需递归依赖分析,请使用跟踪图:

# Direct callees only
grepai trace callees "main"

# Full dependency tree (recursive)
grepai trace graph "main" --depth 3

脚本示例

列出所有被调用函数
grepai trace callees "main" --json | jq -r '.results[].callee' | sort -u
检查特定被调用函数
# Does processOrder call sendEmail?
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

常见问题

❌ 问题: 找不到函数
✅ 解决方案: 检查拼写,并确保该函数存在于已索引的文件中

❌ 问题: 未找到被调用函数(但函数确实有调用) ✅ 解决方案:

  • 尝试 --mode precise
  • 检查语言是否列在 enabled_languages 中
  • 确保 symbols.gob 是最新的(grepai watch)

❌ 问题: 遗漏了部分被调用函数 ✅ 解决方案: 使用 --mode precise 提高准确度

最佳实践

  1. 用于理解代码: 非常适合学习新的代码库
  2. 结合调用者分析: 全面了解依赖关系
  3. 使用图进行深度分析: 需要递归分析时使用
  4. 筛选结果: 聚焦于相关的被调用函数
  5. 记录发现: 导出为 Markdown 文档

输出格式

跟踪被调用函数的结果:

🔍 Callees of "ProcessOrder"

Mode: fast
Function found in: services/order.go:40

Found 4 callees:

1. validateOrder
   File: services/order.go:45
   Context: validateOrder(order)

2. calculateTotal
   File: services/order.go:48
   Context: total := calculateTotal(order.Items)

3. applyDiscount
   File: services/order.go:51
   Context: total = applyDiscount(total, order.Coupon)

4. sendConfirmation
   File: services/order.go:55
   Context: sendConfirmation(order.Email, total)

Tip: Use 'grepai trace graph ProcessOrder' for recursive analysis

评论

支持 Markdown