Debian Codegraph @

go/callgraph/vta/vta_test.go

raw symbols SCIP
1 // Copyright 2021 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 vtascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/
6
7 import (
8 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
9 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
10
11 "github.com/google/go-cmp/cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/"
12 "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/"
13 "golang.org/x/tools/go/analysis/analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/"
14 "golang.org/x/tools/go/analysis/passes/buildssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/buildssa`/"
15 "golang.org/x/tools/go/callgraph/chascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/"
16 "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/"
17 "golang.org/x/tools/go/ssa/ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/"
18 "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/"
19 )
20
21 func TestVTACallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/TestVTACallGraph().(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#) {
22 errDiff := 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#, want, got, missing []string) {
23 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got:\n%s\n\nwant:\n%s\n\nmissing:\n%s\n\ndiff:\n%s",
24 stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(got, "\n"),
25 stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(want, "\n"),
26 stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(missing, "\n"),
27 cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Diff().(got, want)) // to aid debugging
28 }
29
30 files := []string{
31 "testdata/src/callgraph_static.go",
32 "testdata/src/callgraph_ho.go",
33 "testdata/src/callgraph_interfaces.go",
34 "testdata/src/callgraph_pointers.go",
35 "testdata/src/callgraph_collections.go",
36 "testdata/src/callgraph_fields.go",
37 "testdata/src/callgraph_field_funcs.go",
38 "testdata/src/callgraph_recursive_types.go",
39 "testdata/src/callgraph_issue_57756.go",
40 "testdata/src/callgraph_comma_maps.go",
41 "testdata/src/callgraph_type_aliases.go", // https://github.com/golang/go/issues/68799
42 }
43 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().() >= 23 {
44 files = append(files, "testdata/src/callgraph_range_over_func.go")
45 }
46
47 for _, file := range files {
48 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(file, 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#) {
49 prog, want, err := testProgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/testProg().(t, file, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0))
50 if err != nil {
51 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't load test file '%s': %s", file, err)
52 }
53 if len(want) == 0 {
54 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't find want in `%s`", file)
55 }
56
57 // First test VTA with lazy-CHA initial call graph.
58 g := CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/CallGraph().(ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(prog), nil)
59 got := callGraphStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/callGraphStr().(g)
60 if missing := setdiffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/setdiff().(want, got); len(missing) > 0 {
61 errDiff(t, want, got, missing)
62 }
63
64 // Repeat the test with explicit CHA initial call graph.
65 g = CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/CallGraph().(ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(prog), chascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/.CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/CallGraph().(prog))
66 got = callGraphStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/callGraphStr().(g)
67 if missing := setdiffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/setdiff().(want, got); len(missing) > 0 {
68 errDiff(t, want, got, missing)
69 }
70 })
71 }
72 }
73
74 // TestVTAProgVsFuncSet exemplifies and tests different possibilities
75 // enabled by having an arbitrary function set as input to CallGraph
76 // instead of the whole program (i.e., ssautil.AllFunctions(prog)).
77 func TestVTAProgVsFuncSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/TestVTAProgVsFuncSet().(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#) {
78 prog, want, err := testProgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/testProg().(t, "testdata/src/callgraph_nested_ptr.go", ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0))
79 if err != nil {
80 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't load test `testdata/src/callgraph_nested_ptr.go`: %s", err)
81 }
82 if len(want) == 0 {
83 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("couldn't find want in `testdata/src/callgraph_nested_ptr.go`")
84 }
85
86 allFuncs := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(prog)
87 g := CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/CallGraph().(allFuncs, chascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/.CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/CallGraph().(prog))
88 // VTA over the whole program will produce a call graph that
89 // includes Baz:(**i).Foo -> A.Foo, B.Foo.
90 got := callGraphStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/callGraphStr().(g)
91 if diff := setdiffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/setdiff().(want, got); len(diff) > 0 {
92 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("computed callgraph %v should contain %v (diff: %v)", got, want, diff)
93 }
94
95 // Prune the set of program functions to exclude Bar(). This should
96 // yield a call graph that includes different set of callees for Baz
97 // Baz:(**i).Foo -> A.Foo
98 //
99 // Note that the exclusion of Bar can happen, for instance, if Baz is
100 // considered an entry point of some data flow analysis and Bar is
101 // provably (e.g., using CHA forward reachability) unreachable from Baz.
102 noBarFuncs := make(map[*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#]bool)
103 for f, in := range allFuncs {
104 noBarFuncs[f] = in && (funcNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/funcName().(f) != "Bar")
105 }
106 want = []string{"Baz: Do(i) -> Do; invoke t2.Foo() -> A.Foo"}
107 g = CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/CallGraph().(noBarFuncs, chascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/.CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/CallGraph().(prog))
108 got = callGraphStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/callGraphStr().(g)
109 if diff := setdiffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/setdiff().(want, got); len(diff) > 0 {
110 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("pruned callgraph %v should contain %v (diff: %v)", got, want, diff)
111 }
112 }
113
114 // TestVTAPanicMissingDefinitions tests if VTA gracefully handles the case
115 // where VTA panics when a definition of a function or method is not
116 // available, which can happen when using analysis package. A successful
117 // test simply does not panic.
118 func TestVTAPanicMissingDefinitionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/TestVTAPanicMissingDefinitions().(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#) {
119 run := func(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) {
120 s := pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[buildssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/buildssa`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/buildssa`/Analyzer.].(*buildssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/buildssa`/.SSAscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/buildssa`/SSA#)
121 CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/CallGraph().(ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(s.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/buildssa`/SSA#Pkg..Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog.), chascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/.CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/CallGraph().(s.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/buildssa`/SSA#Pkg..Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog.))
122 return nil, nil
123 }
124
125 analyzer := &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{
126 Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "test",
127 Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: "test",
128 Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: run,
129 Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires.: []*analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{
130 buildssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/buildssa`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/buildssa`/Analyzer.,
131 },
132 }
133
134 testdata := analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.TestDatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/TestData.()
135 res := analysistestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Run().(t, testdata, analyzer, "t", "d")
136 if len(res) != 2 {
137 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("want analysis results for 2 packages; got %v", len(res))
138 }
139 for _, r := range res {
140 if r.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Err. != nil {
141 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("want no error for package %v; got %v", r.Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Action..Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types..Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().(), r.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/analysistest`/Result#Err.)
142 }
143 }
144 }
145
146 func TestVTACallGraphGenericsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/TestVTACallGraphGenerics().(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#) {
147 // TODO(zpavlinovic): add more tests
148 files := []string{
149 "testdata/src/arrays_generics.go",
150 "testdata/src/callgraph_generics.go",
151 "testdata/src/issue63146.go",
152 }
153 for _, file := range files {
154 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(file, 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#) {
155 prog, want, err := testProgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/testProg().(t, file, 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.)
156 if err != nil {
157 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't load test file '%s': %s", file, err)
158 }
159 if len(want) == 0 {
160 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't find want in `%s`", file)
161 }
162
163 g := CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/CallGraph().(ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(prog), chascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/.CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/CallGraph().(prog))
164 got := callGraphStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/callGraphStr().(g)
165 if diff := setdiffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/setdiff().(want, got); len(diff) != 0 {
166 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("computed callgraph %v should contain %v (diff: %v)", got, want, diff)
167 logFnsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/logFns().(t, prog)
168 }
169 })
170 }
171 }
172
173 func TestVTACallGraphGo117scip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/TestVTACallGraphGo117().(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#) {
174 file := "testdata/src/go117.go"
175 prog, want, err := testProgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/testProg().(t, file, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0))
176 if err != nil {
177 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't load test file '%s': %s", file, err)
178 }
179 if len(want) == 0 {
180 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't find want in `%s`", file)
181 }
182
183 g, _ := typePropGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/typePropGraph().(ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(prog), makeCalleesFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/makeCalleesFunc().(nil, chascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/.CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/CallGraph().(prog)))
184 got := vtaGraphStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraphStr().(g)
185 if diff := setdiffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/setdiff().(want, got); len(diff) != 0 {
186 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("`%s`: want superset of %v;\n got %v", file, want, got)
187 }
188 }