| 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_test |
| 10 | |
| 11 | import ( |
| 12 | "fmt" |
| 13 | "go/ast" |
| 14 | "go/types" |
| 15 | "sort" |
| 16 | "strings" |
| 17 | "testing" |
| 18 | |
| 19 | "golang.org/x/tools/go/callgraph" |
| 20 | "golang.org/x/tools/go/callgraph/rta" |
| 21 | "golang.org/x/tools/go/ssa" |
| 22 | "golang.org/x/tools/go/ssa/ssautil" |
| 23 | "golang.org/x/tools/internal/testenv" |
| 24 | "golang.org/x/tools/internal/testfiles" |
| 25 | "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 TestRTA(t *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 testenv.Go1Point() >= 27 { |
| 42 | archivePaths = append(archivePaths, "testdata/genericmethod.txtar") |
| 43 | } |
| 44 | |
| 45 | for _, archive := range archivePaths { |
| 46 | t.Run(archive, func(t *testing.T) { |
| 47 | ar, err := txtar.ParseFile(archive) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | |
| 52 | pkgs := testfiles.LoadPackages(t, ar, "./...") |
| 53 | |
| 54 | // find the file which contains the expected result |
| 55 | var f *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.Name == "main" { |
| 60 | f = p.Syntax[0] |
| 61 | } |
| 62 | } |
| 63 | if f == nil { |
| 64 | t.Fatalf("failed to find the file with expected result within main package %s", archive) |
| 65 | } |
| 66 | |
| 67 | prog, spkgs := ssautil.Packages(pkgs, ssa.SanityCheckFunctions|ssa.InstantiateGenerics) |
| 68 | |
| 69 | // find the main package to get functions for rta analysis |
| 70 | var mainPkg *ssa.Package |
| 71 | for _, sp := range spkgs { |
| 72 | if sp.Pkg.Name() == "main" { |
| 73 | mainPkg = sp |
| 74 | break |
| 75 | } |
| 76 | } |
| 77 | if mainPkg == nil { |
| 78 | t.Fatalf("failed to find main ssa package %s", archive) |
| 79 | } |
| 80 | |
| 81 | prog.Build() |
| 82 | |
| 83 | res := rta.Analyze([]*ssa.Function{ |
| 84 | mainPkg.Func("main"), |
| 85 | mainPkg.Func("init"), |
| 86 | }, true) |
| 87 | |
| 88 | 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 check(t *testing.T, f *ast.File, pkg *ssa.Package, res *rta.Result) { |
| 107 | tokFile := pkg.Prog.Fset.File(f.FileStart) |
| 108 | |
| 109 | // Find the WANT comment. |
| 110 | expectation := func(f *ast.File) (string, int) { |
| 111 | for _, c := range f.Comments { |
| 112 | text := strings.TrimSpace(c.Text()) |
| 113 | if t, ok := strings.CutPrefix(text, "WANT:\n"); ok { |
| 114 | return t, tokFile.Line(c.Pos()) |
| 115 | } |
| 116 | } |
| 117 | t.Fatalf("No WANT: comment in %s", tokFile.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 strings.SplitSeq(want, "\n") { |
| 129 | linenum++ |
| 130 | orig := line |
| 131 | bad := func() { |
| 132 | t.Fatalf("%s:%d: invalid assertion: %q", tokFile.Name(), linenum, orig) |
| 133 | } |
| 134 | |
| 135 | line := 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 := strings.CutPrefix(line, "!"); ok { |
| 143 | sense = false |
| 144 | line = strings.TrimSpace(rest) |
| 145 | if line == "" { |
| 146 | bad() |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Select the map. |
| 151 | var want map[string]bool |
| 152 | kind := 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 := 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.Errorf("missing %s %q", kind, str) |
| 181 | } else { |
| 182 | t.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 | sort.Strings(strs) |
| 194 | var buf strings.Builder |
| 195 | for _, str := range strs { |
| 196 | fmt.Fprintf(&buf, "%s %s\n", kind, str) |
| 197 | } |
| 198 | t.Errorf("got:\n%s", &buf) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Check call graph edges. |
| 203 | { |
| 204 | got := make(stringset) |
| 205 | callgraph.GraphVisitEdges(res.CallGraph, func(e *callgraph.Edge) error { |
| 206 | edge := fmt.Sprintf("%s --%s--> %s", |
| 207 | e.Caller.Func.RelString(pkg.Pkg), |
| 208 | e.Description(), |
| 209 | e.Callee.Func.RelString(pkg.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.Reachable { |
| 220 | got[f.RelString(pkg.Pkg)] = true |
| 221 | } |
| 222 | compare("reachable", got, wantReachable) |
| 223 | } |
| 224 | |
| 225 | // Check runtime types. |
| 226 | { |
| 227 | got := make(stringset) |
| 228 | res.RuntimeTypes.Iterate(func(key types.Type, value any) { |
| 229 | if !value.(bool) { // accessible to reflection |
| 230 | typ := types.TypeString(types.Unalias(key), types.RelativeTo(pkg.Pkg)) |
| 231 | got[typ] = true |
| 232 | } |
| 233 | }) |
| 234 | compare("rtype", got, wantRtype) |
| 235 | } |
| 236 | } |