| 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_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 10 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 11 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 12 | |
| 13 | "golang.org/x/tools/go/callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/" |
| 14 | "golang.org/x/tools/go/callgraph/staticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static`/" |
| 15 | "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/" |
| 16 | "golang.org/x/tools/go/ssa/ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/" |
| 17 | "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 18 | "golang.org/x/tools/internal/testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/" |
| 19 | "golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/" |
| 20 | ) |
| 21 | |
| 22 | const inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static_test`/input. = ` |
| 23 | |
| 24 | -- go.mod -- |
| 25 | module x.io |
| 26 | |
| 27 | go 1.22 |
| 28 | |
| 29 | -- p/p.go -- |
| 30 | package main |
| 31 | |
| 32 | type C int |
| 33 | func (C) f() |
| 34 | |
| 35 | type I interface{f()} |
| 36 | |
| 37 | func f() { |
| 38 | p := func() {} |
| 39 | g() |
| 40 | p() // SSA constant propagation => static |
| 41 | |
| 42 | if unknown { |
| 43 | p = h |
| 44 | } |
| 45 | p() // dynamic |
| 46 | |
| 47 | C(0).f() |
| 48 | } |
| 49 | |
| 50 | func g() { |
| 51 | var i I = C(0) |
| 52 | i.f() |
| 53 | } |
| 54 | |
| 55 | func h() |
| 56 | |
| 57 | var unknown bool |
| 58 | |
| 59 | func main() { |
| 60 | } |
| 61 | ` |
| 62 | |
| 63 | const genericsInputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static_test`/genericsInput. = ` |
| 64 | |
| 65 | -- go.mod -- |
| 66 | module x.io |
| 67 | |
| 68 | go 1.22 |
| 69 | |
| 70 | -- p/p.go -- |
| 71 | package p |
| 72 | |
| 73 | type I interface { |
| 74 | F() |
| 75 | } |
| 76 | |
| 77 | type A struct{} |
| 78 | |
| 79 | func (a A) F() {} |
| 80 | |
| 81 | type B struct{} |
| 82 | |
| 83 | func (b B) F() {} |
| 84 | |
| 85 | func instantiated[X I](x X) { |
| 86 | x.F() |
| 87 | } |
| 88 | |
| 89 | func Bar() {} |
| 90 | |
| 91 | func f(h func(), a A, b B) { |
| 92 | h() |
| 93 | |
| 94 | instantiated[A](a) |
| 95 | instantiated[B](b) |
| 96 | } |
| 97 | |
| 98 | func j[T any]() { k[T]() } |
| 99 | func k[T any]() {} |
| 100 | ` |
| 101 | |
| 102 | const genericMethodsInputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static_test`/genericMethodsInput. = ` |
| 103 | -- go.mod -- |
| 104 | module example.com |
| 105 | go 1.27 |
| 106 | |
| 107 | -- p/p.go -- |
| 108 | package p |
| 109 | |
| 110 | type C struct{} |
| 111 | |
| 112 | func (C) F[T any]() {} |
| 113 | |
| 114 | func f() { |
| 115 | var c C |
| 116 | c.F[string]() |
| 117 | c.F[int]() |
| 118 | } |
| 119 | |
| 120 | func g[T any]() { |
| 121 | new(C).F[T]() |
| 122 | } |
| 123 | ` |
| 124 | |
| 125 | func TestStaticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static_test`/TestStatic().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 126 | type testcase struct { |
| 127 | input string |
| 128 | want []string |
| 129 | } |
| 130 | tests := []testcase{ |
| 131 | {inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static_test`/input., []string{ |
| 132 | "(*C).f -> (C).f", |
| 133 | "f -> (C).f", |
| 134 | "f -> f$1", |
| 135 | "f -> g", |
| 136 | }}, |
| 137 | {genericsInputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static_test`/genericsInput., []string{ |
| 138 | "(*A).F -> (A).F", |
| 139 | "(*B).F -> (B).F", |
| 140 | "f -> instantiated[x.io/p.A]", |
| 141 | "f -> instantiated[x.io/p.B]", |
| 142 | "instantiated[x.io/p.A] -> (A).F", |
| 143 | "instantiated[x.io/p.B] -> (B).F", |
| 144 | "j -> k[T]", |
| 145 | "k[T] -> k", |
| 146 | }}, |
| 147 | } |
| 148 | if testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.Go1Pointscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/Go1Point().() >= 27 { |
| 149 | tests = append(tests, testcase{genericMethodsInputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static_test`/genericMethodsInput., []string{ |
| 150 | "(C).F[T] -> (C).F", |
| 151 | "f -> (C).F[int]", |
| 152 | "f -> (C).F[string]", |
| 153 | "g -> (C).F[T]", |
| 154 | }}) |
| 155 | } |
| 156 | |
| 157 | for _, test := range tests { |
| 158 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().("", func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 159 | pkgs := testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/.LoadPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/LoadPackages().(t, txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Parse().([]byte(test.input)), "./p") |
| 160 | prog, _ := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.Packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/Packages().(pkgs, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.InstantiateGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/InstantiateGenerics.) |
| 161 | prog.Buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#Build().() |
| 162 | p := pkgs[0].Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types. |
| 163 | |
| 164 | cg := staticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static`/.CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/static`/CallGraph().(prog) |
| 165 | |
| 166 | var edges []string |
| 167 | callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.GraphVisitEdgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/GraphVisitEdges().(cg, func(e *callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/.Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#) error { |
| 168 | edges = append(edges, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s -> %s", |
| 169 | e.Callerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Caller..Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#Func..RelStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#RelString().(p), |
| 170 | e.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Callee..Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Node#Func..RelStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#RelString().(p))) |
| 171 | return nil |
| 172 | }) // ignore error |
| 173 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(edges) |
| 174 | |
| 175 | if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(edges, test.want) { |
| 176 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Got edges %v, want %v", edges, test.want) |
| 177 | } |
| 178 | }) |
| 179 | } |
| 180 | } |