| 1 | // Copyright 2024 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 | // This file defines helper functions for SSA tests. |
| 6 | |
| 7 | package ssa_test |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| 11 | "go/parser" |
| 12 | "go/token" |
| 13 | "io/fs" |
| 14 | "os" |
| 15 | "testing" |
| 16 | "testing/fstest" |
| 17 | |
| 18 | "golang.org/x/tools/go/packages" |
| 19 | "golang.org/x/tools/go/ssa" |
| 20 | "golang.org/x/tools/go/ssa/ssautil" |
| 21 | "golang.org/x/tools/internal/testenv" |
| 22 | "golang.org/x/tools/internal/testfiles" |
| 23 | "golang.org/x/tools/txtar" |
| 24 | ) |
| 25 | |
| 26 | // goMod returns a go.mod file containing a name and a go directive |
| 27 | // for the major version. If major < 0, use the current go toolchain |
| 28 | // version. |
| 29 | func goMod(name string, major int) []byte { |
| 30 | if major < 0 { |
| 31 | major = testenv.Go1Point() |
| 32 | } |
| 33 | return fmt.Appendf(nil, "module %s\ngo 1.%d", name, major) |
| 34 | } |
| 35 | |
| 36 | // overlayFS returns a simple in-memory filesystem. |
| 37 | func overlayFS(overlay map[string][]byte) fstest.MapFS { |
| 38 | // taking: Maybe loadPackages should take an overlay instead? |
| 39 | fs := make(fstest.MapFS) |
| 40 | for name, data := range overlay { |
| 41 | fs[name] = &fstest.MapFile{Data: data} |
| 42 | } |
| 43 | return fs |
| 44 | } |
| 45 | |
| 46 | // openTxtar opens a txtar file as a filesystem. |
| 47 | func openTxtar(t testing.TB, file string) fs.FS { |
| 48 | // TODO(taking): Move to testfiles? |
| 49 | t.Helper() |
| 50 | |
| 51 | ar, err := txtar.ParseFile(file) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | |
| 56 | fs, err := txtar.FS(ar) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | |
| 61 | return fs |
| 62 | } |
| 63 | |
| 64 | // loadPackages copies the files in a source file system to a unique temporary |
| 65 | // directory and loads packages matching the given patterns from the temporary directory. |
| 66 | // |
| 67 | // TODO(69556): Migrate loader tests to loadPackages. |
| 68 | func loadPackages(t testing.TB, src fs.FS, patterns ...string) []*packages.Package { |
| 69 | t.Helper() |
| 70 | testenv.NeedsGoBuild(t) // for go/packages |
| 71 | |
| 72 | // TODO(taking): src and overlays are very similar. Overlays could have nicer paths. |
| 73 | // Look into migrating src to overlays. |
| 74 | dir := testfiles.CopyToTmp(t, src) |
| 75 | |
| 76 | cfg := &packages.Config{ |
| 77 | Dir: dir, |
| 78 | Mode: packages.NeedSyntax | |
| 79 | packages.NeedTypesInfo | |
| 80 | packages.NeedDeps | |
| 81 | packages.NeedName | |
| 82 | packages.NeedFiles | |
| 83 | packages.NeedImports | |
| 84 | packages.NeedCompiledGoFiles | |
| 85 | packages.NeedTypes, |
| 86 | Env: append(os.Environ(), |
| 87 | "GO111MODULES=on", |
| 88 | "GOPATH=", |
| 89 | "GOWORK=off", |
| 90 | "GOPROXY=off"), |
| 91 | } |
| 92 | pkgs, err := packages.Load(cfg, patterns...) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if packages.PrintErrors(pkgs) > 0 { |
| 97 | t.Fatal("there were errors") |
| 98 | } |
| 99 | return pkgs |
| 100 | } |
| 101 | |
| 102 | // buildPackage builds the content of a go file into: |
| 103 | // * a module with the same name as the package at the current go version, |
| 104 | // * loads the *package.Package, |
| 105 | // * checks that (*packages.Packages).Syntax contains one file, |
| 106 | // * builds the *ssa.Package (and not its dependencies), and |
| 107 | // * returns the built *ssa.Package and the loaded packages.Package. |
| 108 | // |
| 109 | // TODO(adonovan): factor with similar loadFile (2x) in cha/cha_test.go and vta/helpers_test.go. |
| 110 | func buildPackage(t testing.TB, content string, mode ssa.BuilderMode) (*ssa.Package, *packages.Package) { |
| 111 | name := parsePackageClause(t, content) |
| 112 | |
| 113 | fs := overlayFS(map[string][]byte{ |
| 114 | "go.mod": goMod(name, -1), |
| 115 | "input.go": []byte(content), |
| 116 | }) |
| 117 | ppkgs := loadPackages(t, fs, name) |
| 118 | if len(ppkgs) != 1 { |
| 119 | t.Fatalf("Expected to load 1 package from pattern %q. got %d", name, len(ppkgs)) |
| 120 | } |
| 121 | ppkg := ppkgs[0] |
| 122 | |
| 123 | if len(ppkg.Syntax) != 1 { |
| 124 | t.Fatalf("Expected 1 file in package %q. got %d", ppkg, len(ppkg.Syntax)) |
| 125 | } |
| 126 | |
| 127 | prog, _ := ssautil.Packages(ppkgs, mode) |
| 128 | |
| 129 | ssapkg := prog.Package(ppkg.Types) |
| 130 | if ssapkg == nil { |
| 131 | t.Fatalf("Failed to find ssa package for %q", ppkg.Types) |
| 132 | } |
| 133 | ssapkg.Build() |
| 134 | |
| 135 | return ssapkg, ppkg |
| 136 | } |
| 137 | |
| 138 | // parsePackageClause is a test helper to extract the package name from a string |
| 139 | // containing the content of a go file. |
| 140 | func parsePackageClause(t testing.TB, content string) string { |
| 141 | f, err := parser.ParseFile(token.NewFileSet(), "", content, parser.PackageClauseOnly) |
| 142 | if err != nil { |
| 143 | t.Fatalf("parsing the file %q failed with error: %s", content, err) |
| 144 | } |
| 145 | return f.Name.Name |
| 146 | } |