| 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 vta |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "go/ast" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "sort" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "golang.org/x/tools/go/callgraph" |
| 18 | "golang.org/x/tools/go/packages" |
| 19 | "golang.org/x/tools/go/ssa/ssautil" |
| 20 | "golang.org/x/tools/internal/testenv" |
| 21 | |
| 22 | "golang.org/x/tools/go/ssa" |
| 23 | ) |
| 24 | |
| 25 | // want extracts the contents of the first comment |
| 26 | // section starting with "WANT:\n". The returned |
| 27 | // content is split into lines without // prefix. |
| 28 | func want(f *ast.File) []string { |
| 29 | for _, c := range f.Comments { |
| 30 | text := strings.TrimSpace(c.Text()) |
| 31 | if t, ok := strings.CutPrefix(text, "WANT:\n"); ok { |
| 32 | return strings.Split(t, "\n") |
| 33 | } |
| 34 | } |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | // testProg returns an ssa representation of a program at |
| 39 | // `path`, assumed to define package "testdata," and the |
| 40 | // test want result as list of strings. |
| 41 | func testProg(t testing.TB, path string, mode ssa.BuilderMode) (*ssa.Program, []string, error) { |
| 42 | // Set debug mode to exercise DebugRef instructions. |
| 43 | pkg, ssapkg := loadFile(t, path, mode|ssa.GlobalDebug) |
| 44 | return ssapkg.Prog, want(pkg.Syntax[0]), nil |
| 45 | } |
| 46 | |
| 47 | // loadFile loads a built SSA package for a single-file package "x.io/testdata". |
| 48 | // (Ideally all uses would be converted over to txtar files with explicit go.mod files.) |
| 49 | // |
| 50 | // TODO(adonovan): factor with similar loadFile in cha/cha_test.go. |
| 51 | func loadFile(t testing.TB, filename string, mode ssa.BuilderMode) (*packages.Package, *ssa.Package) { |
| 52 | testenv.NeedsGoPackages(t) |
| 53 | |
| 54 | data, err := os.ReadFile(filename) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | dir := t.TempDir() |
| 59 | cfg := &packages.Config{ |
| 60 | Mode: packages.LoadAllSyntax, |
| 61 | Dir: dir, |
| 62 | Overlay: map[string][]byte{ |
| 63 | filepath.Join(dir, "go.mod"): fmt.Appendf(nil, "module x.io\ngo 1.%d", testenv.Go1Point()), |
| 64 | |
| 65 | filepath.Join(dir, "testdata", filepath.Base(filename)): data, |
| 66 | }, |
| 67 | } |
| 68 | pkgs, err := packages.Load(cfg, "./testdata") |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if len(pkgs) != 1 { |
| 73 | t.Fatalf("got %d packages, want 1", len(pkgs)) |
| 74 | } |
| 75 | if len(pkgs[0].Syntax) != 1 { |
| 76 | t.Fatalf("got %d files, want 1", len(pkgs[0].Syntax)) |
| 77 | } |
| 78 | if num := packages.PrintErrors(pkgs); num > 0 { |
| 79 | t.Fatalf("packages contained %d errors", num) |
| 80 | } |
| 81 | prog, ssapkgs := ssautil.Packages(pkgs, mode) |
| 82 | prog.Build() |
| 83 | return pkgs[0], ssapkgs[0] |
| 84 | } |
| 85 | |
| 86 | func firstRegInstr(f *ssa.Function) ssa.Value { |
| 87 | for _, b := range f.Blocks { |
| 88 | for _, i := range b.Instrs { |
| 89 | if v, ok := i.(ssa.Value); ok { |
| 90 | return v |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | // funcName returns a name of the function `f` |
| 98 | // prefixed with the name of the receiver type. |
| 99 | func funcName(f *ssa.Function) string { |
| 100 | recv := f.Signature.Recv() |
| 101 | if recv == nil { |
| 102 | return f.Name() |
| 103 | } |
| 104 | tp := recv.Type().String() |
| 105 | return tp[strings.LastIndex(tp, ".")+1:] + "." + f.Name() |
| 106 | } |
| 107 | |
| 108 | // callGraphStr stringifes `g` into a list of strings where |
| 109 | // each entry is of the form |
| 110 | // |
| 111 | // f: cs1 -> f1, f2, ...; ...; csw -> fx, fy, ... |
| 112 | // |
| 113 | // f is a function, cs1, ..., csw are call sites in f, and |
| 114 | // f1, f2, ..., fx, fy, ... are the resolved callees. |
| 115 | func callGraphStr(g *callgraph.Graph) []string { |
| 116 | var gs []string |
| 117 | for f, n := range g.Nodes { |
| 118 | c := make(map[string][]string) |
| 119 | for _, edge := range n.Out { |
| 120 | cs := edge.Site.String() // TODO(adonovan): handle Site=nil gracefully |
| 121 | c[cs] = append(c[cs], funcName(edge.Callee.Func)) |
| 122 | } |
| 123 | |
| 124 | var cs []string |
| 125 | for site, fs := range c { |
| 126 | sort.Strings(fs) |
| 127 | entry := fmt.Sprintf("%v -> %v", site, strings.Join(fs, ", ")) |
| 128 | cs = append(cs, entry) |
| 129 | } |
| 130 | |
| 131 | sort.Strings(cs) |
| 132 | entry := fmt.Sprintf("%v: %v", funcName(f), strings.Join(cs, "; ")) |
| 133 | gs = append(gs, removeModulePrefix(entry)) |
| 134 | } |
| 135 | return gs |
| 136 | } |
| 137 | |
| 138 | // Logs the functions of prog to t. |
| 139 | func logFns(t testing.TB, prog *ssa.Program) { |
| 140 | for fn := range ssautil.AllFunctions(prog) { |
| 141 | var buf bytes.Buffer |
| 142 | fn.WriteTo(&buf) |
| 143 | t.Log(buf.String()) |
| 144 | } |
| 145 | } |