Debian Codegraph @

go/callgraph/static/static_test.go

raw symbols SCIP
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_test
6
7 import (
8 "fmt"
9 "reflect"
10 "sort"
11 "testing"
12
13 "golang.org/x/tools/go/callgraph"
14 "golang.org/x/tools/go/callgraph/static"
15 "golang.org/x/tools/go/ssa"
16 "golang.org/x/tools/go/ssa/ssautil"
17 "golang.org/x/tools/internal/testenv"
18 "golang.org/x/tools/internal/testfiles"
19 "golang.org/x/tools/txtar"
20 )
21
22 const 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 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 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 TestStatic(t *testing.T) {
126 type testcase struct {
127 input string
128 want []string
129 }
130 tests := []testcase{
131 {input, []string{
132 "(*C).f -> (C).f",
133 "f -> (C).f",
134 "f -> f$1",
135 "f -> g",
136 }},
137 {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 testenv.Go1Point() >= 27 {
149 tests = append(tests, testcase{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.Run("", func(t *testing.T) {
159 pkgs := testfiles.LoadPackages(t, txtar.Parse([]byte(test.input)), "./p")
160 prog, _ := ssautil.Packages(pkgs, ssa.InstantiateGenerics)
161 prog.Build()
162 p := pkgs[0].Types
163
164 cg := static.CallGraph(prog)
165
166 var edges []string
167 callgraph.GraphVisitEdges(cg, func(e *callgraph.Edge) error {
168 edges = append(edges, fmt.Sprintf("%s -> %s",
169 e.Caller.Func.RelString(p),
170 e.Callee.Func.RelString(p)))
171 return nil
172 }) // ignore error
173 sort.Strings(edges)
174
175 if !reflect.DeepEqual(edges, test.want) {
176 t.Errorf("Got edges %v, want %v", edges, test.want)
177 }
178 })
179 }
180 }