Skip to content

Commit ee66134

Browse files
timothy-kinggopherbot
authored andcommitted
go/ssa/ssautil: isolate deprecated CreateProgram
Isolate CreateProgram and its test into their own files to further emphasize that these are deprecated. Change-Id: I441a456752cc3d1728ab86c98e785c59696980f2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/616976 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Tim King <taking@google.com>
1 parent 6ded0c4 commit ee66134

File tree

4 files changed

+86
-56
lines changed

4 files changed

+86
-56
lines changed

go/ssa/ssautil/deprecated.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2015 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 ssautil
6+
7+
// This file contains deprecated public APIs.
8+
// We discourage their use.
9+
10+
import (
11+
"golang.org/x/tools/go/loader"
12+
"golang.org/x/tools/go/ssa"
13+
)
14+
15+
// CreateProgram returns a new program in SSA form, given a program
16+
// loaded from source. An SSA package is created for each transitively
17+
// error-free package of lprog.
18+
//
19+
// Code for bodies of functions is not built until Build is called
20+
// on the result.
21+
//
22+
// The mode parameter controls diagnostics and checking during SSA construction.
23+
//
24+
// Deprecated: Use [golang.org/x/tools/go/packages] and the [Packages]
25+
// function instead; see ssa.Example_loadPackages.
26+
func CreateProgram(lprog *loader.Program, mode ssa.BuilderMode) *ssa.Program {
27+
prog := ssa.NewProgram(lprog.Fset, mode)
28+
29+
for _, info := range lprog.AllPackages {
30+
if info.TransitivelyErrorFree {
31+
prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable)
32+
}
33+
}
34+
35+
return prog
36+
}

go/ssa/ssautil/deprecated_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
package ssautil_test
6+
7+
// Tests of deprecated public APIs.
8+
// We are keeping some tests around to have some test of the public API.
9+
10+
import (
11+
"go/parser"
12+
"os"
13+
"testing"
14+
15+
"golang.org/x/tools/go/loader"
16+
"golang.org/x/tools/go/ssa"
17+
"golang.org/x/tools/go/ssa/ssautil"
18+
)
19+
20+
// TestCreateProgram tests CreateProgram which has an x/tools/go/loader.Program.
21+
func TestCreateProgram(t *testing.T) {
22+
conf := loader.Config{ParserMode: parser.ParseComments}
23+
f, err := conf.ParseFile("hello.go", hello)
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
conf.CreateFromFiles("main", f)
29+
iprog, err := conf.Load()
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
if len(iprog.Created) != 1 {
34+
t.Fatalf("Expected 1 Created package. got %d", len(iprog.Created))
35+
}
36+
pkg := iprog.Created[0].Pkg
37+
38+
prog := ssautil.CreateProgram(iprog, ssa.BuilderMode(0))
39+
ssapkg := prog.Package(pkg)
40+
ssapkg.Build()
41+
42+
if pkg.Name() != "main" {
43+
t.Errorf("pkg.Name() = %s, want main", pkg.Name())
44+
}
45+
if ssapkg.Func("main") == nil {
46+
ssapkg.WriteTo(os.Stderr)
47+
t.Errorf("ssapkg has no main function")
48+
}
49+
}

go/ssa/ssautil/load.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"go/token"
1212
"go/types"
1313

14-
"golang.org/x/tools/go/loader"
1514
"golang.org/x/tools/go/packages"
1615
"golang.org/x/tools/go/ssa"
1716
"golang.org/x/tools/internal/versions"
@@ -111,29 +110,6 @@ func doPackages(initial []*packages.Package, mode ssa.BuilderMode, deps bool) (*
111110
return prog, ssapkgs
112111
}
113112

114-
// CreateProgram returns a new program in SSA form, given a program
115-
// loaded from source. An SSA package is created for each transitively
116-
// error-free package of lprog.
117-
//
118-
// Code for bodies of functions is not built until Build is called
119-
// on the result.
120-
//
121-
// The mode parameter controls diagnostics and checking during SSA construction.
122-
//
123-
// Deprecated: Use [golang.org/x/tools/go/packages] and the [Packages]
124-
// function instead; see ssa.Example_loadPackages.
125-
func CreateProgram(lprog *loader.Program, mode ssa.BuilderMode) *ssa.Program {
126-
prog := ssa.NewProgram(lprog.Fset, mode)
127-
128-
for _, info := range lprog.AllPackages {
129-
if info.TransitivelyErrorFree {
130-
prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable)
131-
}
132-
}
133-
134-
return prog
135-
}
136-
137113
// BuildPackage builds an SSA program with SSA intermediate
138114
// representation (IR) for all functions of a single package.
139115
//

go/ssa/ssautil/switch_test.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010
package ssautil_test
1111

1212
import (
13-
"go/ast"
14-
"go/parser"
15-
"path/filepath"
1613
"strings"
1714
"testing"
1815

19-
"golang.org/x/tools/go/loader"
2016
"golang.org/x/tools/go/ssa"
2117
"golang.org/x/tools/go/ssa/ssautil"
2218
"golang.org/x/tools/internal/testfiles"
@@ -32,39 +28,12 @@ func TestSwitches(t *testing.T) {
3228
if len(ppkgs) != 1 {
3329
t.Fatalf("Expected to load one package but got %d", len(ppkgs))
3430
}
31+
f := ppkgs[0].Syntax[0]
3532

3633
prog, _ := ssautil.Packages(ppkgs, ssa.BuilderMode(0))
3734
mainPkg := prog.Package(ppkgs[0].Types)
3835
mainPkg.Build()
39-
testSwitches(t, ppkgs[0].Syntax[0], mainPkg)
40-
}
41-
42-
// TestCreateProgram uses loader and ssautil.CreateProgram to create an *ssa.Program.
43-
// It has the same testing logic with TestSwitches.
44-
// CreateProgram is deprecated, but it is a part of the public API.
45-
// For now keep a test that exercises it.
46-
func TestCreateProgram(t *testing.T) {
47-
dir := testfiles.ExtractTxtarFileToTmp(t, "testdata/switches.txtar")
48-
conf := loader.Config{ParserMode: parser.ParseComments}
49-
f, err := conf.ParseFile(filepath.Join(dir, "switches.go"), nil)
50-
if err != nil {
51-
t.Error(err)
52-
return
53-
}
54-
55-
conf.CreateFromFiles("main", f)
56-
iprog, err := conf.Load()
57-
if err != nil {
58-
t.Error(err)
59-
return
60-
}
61-
prog := ssautil.CreateProgram(iprog, ssa.BuilderMode(0))
62-
mainPkg := prog.Package(iprog.Created[0].Pkg)
63-
mainPkg.Build()
64-
testSwitches(t, f, mainPkg)
65-
}
6636

67-
func testSwitches(t *testing.T, f *ast.File, mainPkg *ssa.Package) {
6837
for _, mem := range mainPkg.Members {
6938
if fn, ok := mem.(*ssa.Function); ok {
7039
if fn.Synthetic != "" {

0 commit comments

Comments
 (0)