github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/callgraph/static/static.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package static computes the call graph of a Go program containing 6 // only static call edges. 7 package static // import "github.com/powerman/golang-tools/go/callgraph/static" 8 9 import ( 10 "github.com/powerman/golang-tools/go/callgraph" 11 "github.com/powerman/golang-tools/go/ssa" 12 "github.com/powerman/golang-tools/go/ssa/ssautil" 13 ) 14 15 // CallGraph computes the call graph of the specified program 16 // considering only static calls. 17 // 18 func CallGraph(prog *ssa.Program) *callgraph.Graph { 19 cg := callgraph.New(nil) // TODO(adonovan) eliminate concept of rooted callgraph 20 21 // TODO(adonovan): opt: use only a single pass over the ssa.Program. 22 // TODO(adonovan): opt: this is slower than RTA (perhaps because 23 // the lower precision means so many edges are allocated)! 24 for f := range ssautil.AllFunctions(prog) { 25 fnode := cg.CreateNode(f) 26 for _, b := range f.Blocks { 27 for _, instr := range b.Instrs { 28 if site, ok := instr.(ssa.CallInstruction); ok { 29 if g := site.Common().StaticCallee(); g != nil { 30 gnode := cg.CreateNode(g) 31 callgraph.AddEdge(fnode, site, gnode) 32 } 33 } 34 } 35 } 36 } 37 38 return cg 39 }