| 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/testfiles" |
| 24 | "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 TestRTA(t *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.Run(archive, func(t *testing.T) { |
| 42 | ar, err := txtar.ParseFile(archive) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | |
| 47 | pkgs := testfiles.LoadPackages(t, ar, "./...") |
| 48 | |
| 49 | // find the file which contains the expected result |
| 50 | var f *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.Name == "main" { |
| 55 | f = p.Syntax[0] |
| 56 | } |
| 57 | } |
| 58 | if f == nil { |
| 59 | t.Fatalf("failed to find the file with expected result within main package %s", archive) |
| 60 | } |
| 61 | |
| 62 | prog, spkgs := ssautil.Packages(pkgs, ssa.SanityCheckFunctions|ssa.InstantiateGenerics) |
| 63 | |
| 64 | // find the main package to get functions for rta analysis |
| 65 | var mainPkg *ssa.Package |
| 66 | for _, sp := range spkgs { |
| 67 | if sp.Pkg.Name() == "main" { |
| 68 | mainPkg = sp |
| 69 | break |
| 70 | } |
| 71 | } |
| 72 | if mainPkg == nil { |
| 73 | t.Fatalf("failed to find main ssa package %s", archive) |
| 74 | } |
| 75 | |
| 76 | prog.Build() |
| 77 | |
| 78 | res := rta.Analyze([]*ssa.Function{ |
| 79 | mainPkg.Func("main"), |
| 80 | mainPkg.Func("init"), |
| 81 | }, true) |
| 82 | |
| 83 | 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 check(t *testing.T, f *ast.File, pkg *ssa.Package, res *rta.Result) { |
| 102 | tokFile := pkg.Prog.Fset.File(f.FileStart) |
| 103 | |
| 104 | // Find the WANT comment. |
| 105 | expectation := func(f *ast.File) (string, int) { |
| 106 | for _, c := range f.Comments { |
| 107 | text := strings.TrimSpace(c.Text()) |
| 108 | if t, ok := strings.CutPrefix(text, "WANT:\n"); ok { |
| 109 | return t, tokFile.Line(c.Pos()) |
| 110 | } |
| 111 | } |
| 112 | t.Fatalf("No WANT: comment in %s", tokFile.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 strings.SplitSeq(want, "\n") { |
| 124 | linenum++ |
| 125 | orig := line |
| 126 | bad := func() { |
| 127 | t.Fatalf("%s:%d: invalid assertion: %q", tokFile.Name(), linenum, orig) |
| 128 | } |
| 129 | |
| 130 | line := 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 := strings.CutPrefix(line, "!"); ok { |
| 138 | sense = false |
| 139 | line = strings.TrimSpace(rest) |
| 140 | if line == "" { |
| 141 | bad() |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Select the map. |
| 146 | var want map[string]bool |
| 147 | kind := 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 := 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.Errorf("missing %s %q", kind, str) |
| 176 | } else { |
| 177 | t.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 | sort.Strings(strs) |
| 189 | var buf strings.Builder |
| 190 | for _, str := range strs { |
| 191 | fmt.Fprintf(&buf, "%s %s\n", kind, str) |
| 192 | } |
| 193 | t.Errorf("got:\n%s", &buf) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Check call graph edges. |
| 198 | { |
| 199 | got := make(stringset) |
| 200 | callgraph.GraphVisitEdges(res.CallGraph, func(e *callgraph.Edge) error { |
| 201 | edge := fmt.Sprintf("%s --%s--> %s", |
| 202 | e.Caller.Func.RelString(pkg.Pkg), |
| 203 | e.Description(), |
| 204 | e.Callee.Func.RelString(pkg.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.Reachable { |
| 215 | got[f.RelString(pkg.Pkg)] = true |
| 216 | } |
| 217 | compare("reachable", got, wantReachable) |
| 218 | } |
| 219 | |
| 220 | // Check runtime types. |
| 221 | { |
| 222 | got := make(stringset) |
| 223 | res.RuntimeTypes.Iterate(func(key types.Type, value any) { |
| 224 | if !value.(bool) { // accessible to reflection |
| 225 | typ := types.TypeString(types.Unalias(key), types.RelativeTo(pkg.Pkg)) |
| 226 | got[typ] = true |
| 227 | } |
| 228 | }) |
| 229 | compare("rtype", got, wantRtype) |
| 230 | } |
| 231 | } |