Skip to content

Commit bc9374d

Browse files
committed
go/callgraph/vta: remove unnecessary use of unsafe
Change-Id: I25e577210e0627a9e9598273dfb43408786bf3c1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/701895 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent 12d7e15 commit bc9374d

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

go/callgraph/vta/propagation_test.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
"sort"
1414
"strings"
1515
"testing"
16-
"unsafe"
1716

1817
"golang.org/x/tools/go/ssa"
18+
"golang.org/x/tools/go/ssa/ssautil"
19+
"golang.org/x/tools/internal/testfiles"
20+
"golang.org/x/tools/txtar"
1921

2022
"golang.org/x/tools/go/types/typeutil"
2123
)
@@ -58,11 +60,6 @@ func newLocal(name string, t types.Type) local {
5860
return local{val: val{name: name, typ: t}}
5961
}
6062

61-
// newNamedType creates a bogus type named `name`.
62-
func newNamedType(name string) *types.Named {
63-
return types.NewNamed(types.NewTypeName(token.NoPos, nil, name, nil), types.Universe.Lookup("int").Type(), nil)
64-
}
65-
6663
// sccString is a utility for stringifying `nodeToScc`. Every
6764
// scc is represented as a string where string representation
6865
// of scc nodes are sorted and concatenated using `;`.
@@ -99,7 +96,9 @@ func nodeToTypeString(pMap propTypeMap) map[string]string {
9996
for node := range pMap {
10097
var propStrings []string
10198
for prop := range pMap.propTypes(node) {
102-
propStrings = append(propStrings, propTypeString(prop))
99+
s := propTypeString(prop)
100+
s = strings.ReplaceAll(s, "example.com.", "")
101+
propStrings = append(propStrings, s)
103102
}
104103
sort.Strings(propStrings)
105104
nodeToTypeStr[node.String()] = strings.Join(propStrings, ";")
@@ -149,15 +148,6 @@ func sccMapsConsistent(sccs [][]idx, idxToSccID []int) bool {
149148
return true
150149
}
151150

152-
// setName sets name of the function `f` to `name`
153-
// using reflection since setting the name otherwise
154-
// is only possible within the ssa package.
155-
func setName(f *ssa.Function, name string) {
156-
fi := reflect.ValueOf(f).Elem().FieldByName("name")
157-
fi = reflect.NewAt(fi.Type(), unsafe.Pointer(fi.UnsafeAddr())).Elem()
158-
fi.SetString(name)
159-
}
160-
161151
// testSuite produces a named set of graphs as follows, where
162152
// parentheses contain node types and F nodes stand for function
163153
// nodes whose content is function named F:
@@ -195,20 +185,35 @@ func setName(f *ssa.Function, name string) {
195185
// t1 (A) -> t2 (B) -> F1 -> F2 -> F3 -> F4
196186
// | | | |
197187
// <------- <------------
198-
func testSuite() map[string]*vtaGraph {
199-
a := newNamedType("A")
200-
b := newNamedType("B")
201-
c := newNamedType("C")
202-
sig := types.NewSignatureType(nil, nil, nil, types.NewTuple(), types.NewTuple(), false)
203-
204-
f1 := &ssa.Function{Signature: sig}
205-
setName(f1, "F1")
206-
f2 := &ssa.Function{Signature: sig}
207-
setName(f2, "F2")
208-
f3 := &ssa.Function{Signature: sig}
209-
setName(f3, "F3")
210-
f4 := &ssa.Function{Signature: sig}
211-
setName(f4, "F4")
188+
func testSuite(t *testing.T) map[string]*vtaGraph {
189+
ar := txtar.Parse([]byte(`-- go.mod --
190+
module example.com
191+
go 1.24
192+
193+
-- p.go --
194+
package p
195+
type A struct{}
196+
type B struct{}
197+
type C struct{}
198+
func F1()
199+
func F2()
200+
func F3()
201+
func F4()
202+
`))
203+
ppkgs := testfiles.LoadPackages(t, ar, ".")
204+
if len(ppkgs) != 1 {
205+
t.Fatalf("LoadPackages returned %d packages, want 1", len(ppkgs))
206+
}
207+
_, ssapkgs := ssautil.Packages(ppkgs, ssa.BuilderMode(0))
208+
pkg := ssapkgs[0]
209+
210+
a := pkg.Type("A").Type().(*types.Named)
211+
b := pkg.Type("B").Type().(*types.Named)
212+
c := pkg.Type("C").Type().(*types.Named)
213+
f1 := pkg.Func("F1")
214+
f2 := pkg.Func("F2")
215+
f3 := pkg.Func("F3")
216+
f4 := pkg.Func("F4")
212217

213218
graphs := make(map[string]*vtaGraph)
214219
v := &vtaGraph{}
@@ -260,7 +265,7 @@ func testSuite() map[string]*vtaGraph {
260265
}
261266

262267
func TestSCC(t *testing.T) {
263-
suite := testSuite()
268+
suite := testSuite(t)
264269
for _, test := range []struct {
265270
name string
266271
graph *vtaGraph
@@ -294,7 +299,7 @@ func TestSCC(t *testing.T) {
294299
}
295300

296301
func TestPropagation(t *testing.T) {
297-
suite := testSuite()
302+
suite := testSuite(t)
298303
var canon typeutil.Map
299304
for _, test := range []struct {
300305
name string

0 commit comments

Comments
 (0)