How to find out golang SSA function return type

402 views Asked by At

hello i am new to static analysis and try to use golang's SSA package to analyze our code. For example i want to print all function infos in the package, the example code is:

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)
}

then i found i have no way to access the type of return values for this function (refering to the documents https://pkg.go.dev/golang.org/x/tools/go/ssa#Function) Is there any way to analyze the type of function's return value by using ssa tools or other tools?

1

There are 1 answers

0
ekim boran On BEST ANSWER

You can get the signature and therefore return type of a function via

 f.Signature.Results()

If you don't need callgraph traversal you can directly get function info from ssa.package struct

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