Debian Codegraph @

go/callgraph/rta/rta_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 // No testdata on Android.
6
7 //go:build !android
8
9 package rta_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta_test`/
10
11 import (
12 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
13 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
14 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
15 "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/"
16 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
17 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
18
19 "golang.org/x/tools/go/callgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/"
20 "golang.org/x/tools/go/callgraph/rtascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta`/"
21 "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/"
22 "golang.org/x/tools/go/ssa/ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/"
23 "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/"
24 "golang.org/x/tools/internal/testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/"
25 "golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/"
26 )
27
28 // TestRTA runs RTA on each testdata/*.txtar file containing a single
29 // go file in a single package or multiple files in different packages,
30 // and compares the results with the expectations expressed in the WANT
31 // comment.
32 func TestRTAscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta_test`/TestRTA().(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#) {
33 archivePaths := []string{
34 "testdata/func.txtar",
35 "testdata/generics.txtar",
36 "testdata/iface.txtar",
37 "testdata/reflectcall.txtar",
38 "testdata/rtype.txtar",
39 "testdata/multipkgs.txtar",
40 }
41 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 {
42 archivePaths = append(archivePaths, "testdata/genericmethod.txtar")
43 }
44
45 for _, archive := range archivePaths {
46 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(archive, 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#) {
47 ar, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.ParseFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/ParseFile().(archive)
48 if err != nil {
49 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
50 }
51
52 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, ar, "./...")
53
54 // find the file which contains the expected result
55 var f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#
56 for _, p := range pkgs {
57 // We assume the packages have a single file or
58 // the wanted result is in the first file of the main package.
59 if p.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Name. == "main" {
60 f = p.Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Syntax.[0]
61 }
62 }
63 if f == nil {
64 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to find the file with expected result within main package %s", archive)
65 }
66
67 prog, spkgs := 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`/.SanityCheckFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/SanityCheckFunctions.|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.)
68
69 // find the main package to get functions for rta analysis
70 var mainPkg *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#
71 for _, sp := range spkgs {
72 if sp.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Pkg..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().() == "main" {
73 mainPkg = sp
74 break
75 }
76 }
77 if mainPkg == nil {
78 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to find main ssa package %s", archive)
79 }
80
81 prog.Buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#Build().()
82
83 res := rtascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta`/.Analyzescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta`/Analyze().([]*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#{
84 mainPkg.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("main"),
85 mainPkg.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("init"),
86 }, true)
87
88 checkscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta_test`/check().(t, f, mainPkg, res)
89 })
90 }
91 }
92
93 // check tests the RTA analysis results against the test expectations
94 // defined by a comment starting with a line "WANT:".
95 //
96 // The rest of the comment consists of lines of the following forms:
97 //
98 // edge <func> --kind--> <func> # call graph edge
99 // reachable <func> # reachable function
100 // rtype <type> # run-time type descriptor needed
101 //
102 // Each line asserts that an element is found in the given set, or, if
103 // the line is preceded by "!", that it is not in the set.
104 //
105 // Functions are notated as if by ssa.Function.String.
106 func checkscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta_test`/check().(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#, f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, pkg *ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#, res *rtascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta`/.Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta`/Result#) {
107 tokFile := pkg.Progscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Prog..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#Fset..Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(f.FileStartscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#FileStart.)
108
109 // Find the WANT comment.
110 expectation := func(f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#) (string, int) {
111 for _, c := range f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. {
112 text := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(c.Textscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Text().())
113 if t, ok := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.CutPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/CutPrefix().(text, "WANT:\n"); ok {
114 return t, tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(c.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().())
115 }
116 }
117 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("No WANT: comment in %s", tokFile.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Name().())
118 return "", 0
119 }
120 want, linenum := expectation(f)
121
122 // Parse the comment into three string-to-sense maps.
123 var (
124 wantEdge = make(map[string]bool)
125 wantReachable = make(map[string]bool)
126 wantRtype = make(map[string]bool)
127 )
128 for line := range stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.SplitSeqscip-go gomod github.com/golang/go/src go1.25.0 strings/SplitSeq().(want, "\n") {
129 linenum++
130 orig := line
131 bad := func() {
132 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("%s:%d: invalid assertion: %q", tokFile.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Name().(), linenum, orig)
133 }
134
135 line := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(line)
136 if line == "" {
137 continue // skip blanks
138 }
139
140 // A leading "!" negates the assertion.
141 sense := true
142 if rest, ok := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.CutPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/CutPrefix().(line, "!"); ok {
143 sense = false
144 line = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(rest)
145 if line == "" {
146 bad()
147 }
148 }
149
150 // Select the map.
151 var want map[string]bool
152 kind := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.25.0 strings/Fields().(line)[0]
153 switch kind {
154 case "edge":
155 want = wantEdge
156 case "reachable":
157 want = wantReachable
158 case "rtype":
159 want = wantRtype
160 default:
161 bad()
162 }
163
164 // Add expectation.
165 str := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(line[len(kind):])
166 want[str] = sense
167 }
168
169 type stringset = map[string]bool // (sets: values are true)
170
171 // compare checks that got matches each assertion of the form
172 // (str, sense) in want. The sense determines whether the test
173 // is positive or negative.
174 compare := func(kind string, got stringset, want map[string]bool) {
175 ok := true
176 for str, sense := range want {
177 if got[str] != sense {
178 ok = false
179 if sense {
180 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("missing %s %q", kind, str)
181 } else {
182 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unwanted %s %q", kind, str)
183 }
184 }
185 }
186
187 // Print the actual output in expectation form.
188 if !ok {
189 var strs []string
190 for s := range got {
191 strs = append(strs, s)
192 }
193 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().(strs)
194 var buf stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#
195 for _, str := range strs {
196 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&buf, "%s %s\n", kind, str)
197 }
198 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got:\n%s", &buf)
199 }
200 }
201
202 // Check call graph edges.
203 {
204 got := make(stringset)
205 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().(res.CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta`/Result#CallGraph., 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 {
206 edge := 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--> %s",
207 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().(pkg.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Pkg.),
208 e.Descriptionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph`/Edge#Description().(),
209 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().(pkg.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Pkg.))
210 got[edge] = true
211 return nil
212 })
213 compare("edge", got, wantEdge)
214 }
215
216 // Check reachable functions.
217 {
218 got := make(stringset)
219 for f := range res.Reachablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta`/Result#Reachable. {
220 got[f.RelStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#RelString().(pkg.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Pkg.)] = true
221 }
222 compare("reachable", got, wantReachable)
223 }
224
225 // Check runtime types.
226 {
227 got := make(stringset)
228 res.RuntimeTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/rta`/Result#RuntimeTypes..Iteratescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#Iterate().(func(key 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#, value any) {
229 if !value.(bool) { // accessible to reflection
230 typ := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeStringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeString().(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unaliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unalias().(key), typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.RelativeToscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/RelativeTo().(pkg.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Pkg.))
231 got[typ] = true
232 }
233 })
234 compare("rtype", got, wantRtype)
235 }
236 }