| 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 |
| 8 | |
| 9 | import ( |
| 10 | "go/types" |
| 11 | |
| 12 | "golang.org/x/tools/go/callgraph" |
| 13 | "golang.org/x/tools/go/ssa" |
| 14 | ) |
| 15 | |
| 16 | // CallGraph computes the static call graph of the specified program. |
| 17 | // |
| 18 | // The resulting graph includes: |
| 19 | // - all package-level functions; |
| 20 | // - all methods of package-level non-parameterized non-interface types; |
| 21 | // - pointer wrappers (*C).F for source-level methods C.F; |
| 22 | // - and all functions reachable from them following only static calls. |
| 23 | // |
| 24 | // It does not consider exportedness, nor treat main packages specially. |
| 25 | func CallGraph(prog *ssa.Program) *callgraph.Graph { |
| 26 | cg := callgraph.New(nil) |
| 27 | |
| 28 | // Recursively follow all static calls. |
| 29 | seen := make(map[int]bool) // node IDs already seen |
| 30 | var visit func(fnode *callgraph.Node) |
| 31 | visit = func(fnode *callgraph.Node) { |
| 32 | if !seen[fnode.ID] { |
| 33 | seen[fnode.ID] = true |
| 34 | |
| 35 | for _, b := range fnode.Func.Blocks { |
| 36 | for _, instr := range b.Instrs { |
| 37 | if site, ok := instr.(ssa.CallInstruction); ok { |
| 38 | if g := site.Common().StaticCallee(); g != nil { |
| 39 | gnode := cg.CreateNode(g) |
| 40 | callgraph.AddEdge(fnode, site, gnode) |
| 41 | visit(gnode) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // If we were ever to redesign this function, we should allow |
| 50 | // the caller to provide the set of root functions and just |
| 51 | // perform the reachability step. This would allow them to |
| 52 | // work forwards from main entry points: |
| 53 | // |
| 54 | // rootNames := []string{"init", "main"} |
| 55 | // for _, main := range ssautil.MainPackages(prog.AllPackages()) { |
| 56 | // for _, rootName := range rootNames { |
| 57 | // visit(cg.CreateNode(main.Func(rootName))) |
| 58 | // } |
| 59 | // } |
| 60 | // |
| 61 | // or to control whether to include non-exported |
| 62 | // functions/methods, wrapper methods, and so on. |
| 63 | // Unfortunately that's not consistent with its historical |
| 64 | // behavior and existing tests. |
| 65 | // |
| 66 | // The logic below is a slight simplification and |
| 67 | // rationalization of ssautil.AllFunctions. (Having to include |
| 68 | // (*T).F wrapper methods is unfortunate--they are not source |
| 69 | // functions, and if they're reachable, they'll be in the |
| 70 | // graph--but the existing tests will break without it.) |
| 71 | |
| 72 | methodsOf := func(T types.Type) { |
| 73 | if !types.IsInterface(T) { |
| 74 | mset := prog.MethodSets.MethodSet(T) |
| 75 | for method := range mset.Methods() { |
| 76 | visit(cg.CreateNode(prog.MethodValue(method))) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Start from package-level symbols. |
| 82 | for _, pkg := range prog.AllPackages() { |
| 83 | for _, mem := range pkg.Members { |
| 84 | switch mem := mem.(type) { |
| 85 | case *ssa.Function: |
| 86 | // package-level function |
| 87 | visit(cg.CreateNode(mem)) |
| 88 | |
| 89 | case *ssa.Type: |
| 90 | // methods of package-level non-interface non-parameterized types |
| 91 | if !types.IsInterface(mem.Type()) { |
| 92 | if named, ok := mem.Type().(*types.Named); ok && |
| 93 | named.TypeParams() == nil { |
| 94 | methodsOf(named) // T |
| 95 | methodsOf(types.NewPointer(named)) // *T |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return cg |
| 103 | } |