@@ -13,9 +13,11 @@ import (
13
13
"sort"
14
14
"strings"
15
15
"testing"
16
- "unsafe"
17
16
18
17
"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"
19
21
20
22
"golang.org/x/tools/go/types/typeutil"
21
23
)
@@ -58,11 +60,6 @@ func newLocal(name string, t types.Type) local {
58
60
return local {val : val {name : name , typ : t }}
59
61
}
60
62
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
-
66
63
// sccString is a utility for stringifying `nodeToScc`. Every
67
64
// scc is represented as a string where string representation
68
65
// of scc nodes are sorted and concatenated using `;`.
@@ -99,7 +96,9 @@ func nodeToTypeString(pMap propTypeMap) map[string]string {
99
96
for node := range pMap {
100
97
var propStrings []string
101
98
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 )
103
102
}
104
103
sort .Strings (propStrings )
105
104
nodeToTypeStr [node .String ()] = strings .Join (propStrings , ";" )
@@ -149,15 +148,6 @@ func sccMapsConsistent(sccs [][]idx, idxToSccID []int) bool {
149
148
return true
150
149
}
151
150
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
-
161
151
// testSuite produces a named set of graphs as follows, where
162
152
// parentheses contain node types and F nodes stand for function
163
153
// nodes whose content is function named F:
@@ -195,20 +185,35 @@ func setName(f *ssa.Function, name string) {
195
185
// t1 (A) -> t2 (B) -> F1 -> F2 -> F3 -> F4
196
186
// | | | |
197
187
// <------- <------------
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" )
212
217
213
218
graphs := make (map [string ]* vtaGraph )
214
219
v := & vtaGraph {}
@@ -260,7 +265,7 @@ func testSuite() map[string]*vtaGraph {
260
265
}
261
266
262
267
func TestSCC (t * testing.T ) {
263
- suite := testSuite ()
268
+ suite := testSuite (t )
264
269
for _ , test := range []struct {
265
270
name string
266
271
graph * vtaGraph
@@ -294,7 +299,7 @@ func TestSCC(t *testing.T) {
294
299
}
295
300
296
301
func TestPropagation (t * testing.T ) {
297
- suite := testSuite ()
302
+ suite := testSuite (t )
298
303
var canon typeutil.Map
299
304
for _ , test := range []struct {
300
305
name string
0 commit comments