【问题标题】:How to find out golang SSA function return type如何找出golang SSA函数返回类型
【发布时间】:2021-05-20 04:09:11
【问题描述】:

你好,我是静态分析的新手,尝试使用 golang 的 SSA 包来分析我们的代码。例如我想打印包中的所有函数信息,示例代码为:

dir := PROJECT_PATH
cfg := packages.Config{
    Mode: packages.LoadAllSyntax,
    Dir: dir,
}

initial, _ := packages.Load(&cfg, "./")

// Create SSA packages for well-typed packages and their dependencies.
prog, _ := ssautil.AllPackages(initial, 0)

// Build SSA code for the whole program.
prog.Build()
callGraph = cha.CallGraph(prog)

for f, node := range callGraph.Nodes{
    // f is of ssa.Function 
    fmt.Println("func:", f, f.Name(), f.Syntax(), f.Params)
}

然后我发现我无法访问此函数的返回值类型(参考文档https://pkg.go.dev/golang.org/x/tools/go/ssa#Function) 有没有办法通过ssa工具或者其他工具来分析函数返回值的类型?

【问题讨论】:

    标签: go static-analysis ssa


    【解决方案1】:

    您可以通过

    获取签名并因此返回函数的类型
     f.Signature.Results()
    

    如果不需要调用图遍历,可以直接从ssa.packagestruct获取函数信息

    for k, v := range pckg.Members {
       c, ok := v.(*ssa.Function)
       if ok{
        ...
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多