github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/go/callgraph/static/static.go (about)

     1  // Package static computes the call graph of a Go program containing
     2  // only static call edges.
     3  package static // import "llvm.org/llgo/third_party/gotools/go/callgraph/static"
     4  
     5  import (
     6  	"llvm.org/llgo/third_party/gotools/go/callgraph"
     7  	"llvm.org/llgo/third_party/gotools/go/ssa"
     8  	"llvm.org/llgo/third_party/gotools/go/ssa/ssautil"
     9  )
    10  
    11  // CallGraph computes the call graph of the specified program
    12  // considering only static calls.
    13  //
    14  func CallGraph(prog *ssa.Program) *callgraph.Graph {
    15  	cg := callgraph.New(nil) // TODO(adonovan) eliminate concept of rooted callgraph
    16  
    17  	// TODO(adonovan): opt: use only a single pass over the ssa.Program.
    18  	for f := range ssautil.AllFunctions(prog) {
    19  		fnode := cg.CreateNode(f)
    20  		for _, b := range f.Blocks {
    21  			for _, instr := range b.Instrs {
    22  				if site, ok := instr.(ssa.CallInstruction); ok {
    23  					if g := site.Common().StaticCallee(); g != nil {
    24  						gnode := cg.CreateNode(g)
    25  						callgraph.AddEdge(fnode, site, gnode)
    26  					}
    27  				}
    28  			}
    29  		}
    30  	}
    31  
    32  	return cg
    33  }