Skip to content

fix(golang): mark GlobalVar dependencies as IsInvoked when directly c…#190

Merged
wzekin merged 1 commit into
cloudwego:mainfrom
wzekin:fix/golang-globalvar-isinvoked
May 12, 2026
Merged

fix(golang): mark GlobalVar dependencies as IsInvoked when directly c…#190
wzekin merged 1 commit into
cloudwego:mainfrom
wzekin:fix/golang-globalvar-isinvoked

Conversation

@wzekin
Copy link
Copy Markdown
Collaborator

@wzekin wzekin commented May 11, 2026

What type of PR is this?

fix

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo

(Optional) Translate the PR title into Chinese.

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:
zh(optional):
在 golang parser 中,当一个包级变量持有函数值(如 var Foo = func() {...}),并在另一个函数体内通过 Foo() 直接调用时,abcoder 之前只会把 Foo 记为普通的 GlobalVar 依赖。函数/方法调用依赖已经通过
directCalls 匹配获得了 Extra["IsInvoked"]=true 标记,但同样的逻辑没有应用到 GlobalVars 上。下游消费者因此无法区分"引用一个全局变量"和"把全局变量作为可调用对象调用"。

Example

Input source (pkg/util.go):

package pkg

var Var6 = func() {}

// 仅引用,不调用
func Case_Func_Global() {
    _ = Var6
}

// 直接调用
func Case_Invoke_GlobalFuncVar() {
    Var6()
}

Before this PR — 两个函数的 GlobalVars 条目完全一样,无法区分"引用"和"调用":

"Case_Func_Global": {
  "GlobalVars": [
    { "ModPath": "a.b/c", "PkgPath": "a.b/c/pkg", "Name": "Var6",
      "File": "pkg/util.go", "Line": 121 }
  ]
},
"Case_Invoke_GlobalFuncVar": {
  "GlobalVars": [
    { "ModPath": "a.b/c", "PkgPath": "a.b/c/pkg", "Name": "Var6",
      "File": "pkg/util.go", "Line": 132 }
  ]
}

After this PR — 直接调用点会带上 Extra: { "IsInvoked": true },纯引用点保持原样:

"Case_Func_Global": {
  "GlobalVars": [
    { "ModPath": "a.b/c", "PkgPath": "a.b/c/pkg", "Name": "Var6",
      "File": "pkg/util.go", "Line": 121 }                       // 无 Extra
  ]
},
"Case_Invoke_GlobalFuncVar": {
  "GlobalVars": [
    { "ModPath": "a.b/c", "PkgPath": "a.b/c/pkg", "Name": "Var6",
      "File": "pkg/util.go", "Line": 132,
      "Extra": { "IsInvoked": true } }                           // 新增标记
  ]
}

(Optional) Which issue(s) this PR fixes:

(optional) The PR that updates user documentation:

…alled

When a package-scope variable holds a function value (e.g. `var Foo = func() {...}`)
and is invoked from another function body via `Foo()`, the parser previously only
recorded `Foo` as a plain GlobalVar dependency. Extend the existing directCalls
matching in parseFunc to also stamp Extra[IsInvoked]=true on GlobalVars, mirroring
the behavior already in place for FunctionCalls and MethodCalls.
@wzekin wzekin force-pushed the fix/golang-globalvar-isinvoked branch from b3dcdd2 to ed6a588 Compare May 11, 2026 11:51
@wzekin wzekin merged commit b6f1fb5 into cloudwego:main May 12, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants