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