| 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 staticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static`/ |
| 8 | |
| 9 | import ( |
| 10 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 11 | |
| 12 | "golang.org/x/tools/go/callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/" |
| 13 | "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `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 CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static`/CallGraph().(prog *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Programscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#) *callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph# { |
| 26 | cg := callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/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 *callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#) |
| 31 | visit = func(fnode *callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#) { |
| 32 | if !seen[fnode.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#ID.] { |
| 33 | seen[fnode.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#ID.] = true |
| 34 | |
| 35 | for _, b := range fnode.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#Func..Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. { |
| 36 | for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. { |
| 37 | if site, ok := instr.(ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.CallInstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#); ok { |
| 38 | if g := site.Commonscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallInstruction#Common.().StaticCalleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/CallCommon#StaticCallee().(); g != nil { |
| 39 | gnode := cg.CreateNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#CreateNode().(g) |
| 40 | callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.AddEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/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 typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#) { |
| 73 | if !typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.IsInterfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/IsInterface().(T) { |
| 74 | mset := prog.MethodSetsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodSets..MethodSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#MethodSet().(T) |
| 75 | for method := range mset.Methodsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Methods().() { |
| 76 | visit(cg.CreateNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#CreateNode().(prog.MethodValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#MethodValue().(method))) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Start from package-level symbols. |
| 82 | for _, pkg := range prog.AllPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#AllPackages().() { |
| 83 | for _, mem := range pkg.Membersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Members. { |
| 84 | switch mem := mem.(type) { |
| 85 | case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#: |
| 86 | // package-level function |
| 87 | visit(cg.CreateNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Graph#CreateNode().(mem)) |
| 88 | |
| 89 | case *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#: |
| 90 | // methods of package-level non-interface non-parameterized types |
| 91 | if !typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.IsInterfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/IsInterface().(mem.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Type().()) { |
| 92 | if named, ok := mem.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Type().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#); ok && |
| 93 | named.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#TypeParams().() == nil { |
| 94 | methodsOf(named) // T |
| 95 | methodsOf(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPointer().(named)) // *T |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return cg |
| 103 | } |