| 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 vtascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 10 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 11 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 14 | |
| 15 | "golang.org/x/tools/go/callgraph/chascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/" |
| 16 | "golang.org/x/tools/go/ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/" |
| 17 | "golang.org/x/tools/go/ssa/ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/" |
| 18 | ) |
| 19 | |
| 20 | func TestNodeInterfacescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/TestNodeInterface().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 21 | // Since ssa package does not allow explicit creation of ssa |
| 22 | // values, we use the values from the program testdata/src/simple.go: |
| 23 | // - basic type int |
| 24 | // - struct X with two int fields a and b |
| 25 | // - global variable "gl" |
| 26 | // - "foo" function |
| 27 | // - "main" function and its |
| 28 | // - first register instruction t0 := *gl |
| 29 | prog, _, err := testProgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/testProg().(t, "testdata/src/simple.go", ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 30 | if err != nil { |
| 31 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't load testdata/src/simple.go program: %v", err) |
| 32 | } |
| 33 | |
| 34 | pkg := prog.AllPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#AllPackages().()[0] |
| 35 | main := pkg.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("main") |
| 36 | foo := pkg.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Func().("foo") |
| 37 | reg := firstRegInstrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/firstRegInstr().(main) // t0 := *gl |
| 38 | X := pkg.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Type().("X").Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Type#Type().() |
| 39 | gl := pkg.Varscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Var().("gl") |
| 40 | glPtrType, ok := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unaliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unalias().(gl.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Global#Type().()).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#) |
| 41 | if !ok { |
| 42 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("could not cast gl variable to pointer type") |
| 43 | } |
| 44 | bint := glPtrType.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#Elem().() |
| 45 | |
| 46 | pint := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPointer().(bint) |
| 47 | i := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewInterfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewInterface().(nil, nil) |
| 48 | |
| 49 | voidFunc := main.Signaturescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Signature..Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Underlying().() |
| 50 | |
| 51 | for _, test := range []struct { |
| 52 | n nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node# |
| 53 | s string |
| 54 | t typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type# |
| 55 | }{ |
| 56 | {constantscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constant#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constant#typ.: bint}, "Constant(int)", bint}, |
| 57 | {pointerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/pointer#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/pointer#typ.: pint}, "Pointer(*int)", pint}, |
| 58 | {mapKeyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/mapKey#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/mapKey#typ.: bint}, "MapKey(int)", bint}, |
| 59 | {mapValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/mapValue#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/mapValue#typ.: pint}, "MapValue(*int)", pint}, |
| 60 | {sliceElemscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/sliceElem#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/sliceElem#typ.: bint}, "Slice([]int)", bint}, |
| 61 | {channelElemscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/channelElem#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/channelElem#typ.: pint}, "Channel(chan *int)", pint}, |
| 62 | {fieldscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/field#{StructTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/field#StructType.: X, indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/field#index.: 0}, "Field(testdata.X:a)", bint}, |
| 63 | {fieldscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/field#{StructTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/field#StructType.: X, indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/field#index.: 1}, "Field(testdata.X:b)", bint}, |
| 64 | {globalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/global#{valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/global#val.: gl}, "Global(gl)", gl.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Global#Type().()}, |
| 65 | {localscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/local#{valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/local#val.: reg}, "Local(t0)", bint}, |
| 66 | {indexedLocalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/indexedLocal#{valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/indexedLocal#val.: reg, typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/indexedLocal#typ.: X, indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/indexedLocal#index.: 0}, "Local(t0[0])", X}, |
| 67 | {functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/function#{fscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/function#f.: main}, "Function(main)", voidFunc}, |
| 68 | {resultVarscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/resultVar#{fscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/resultVar#f.: foo, indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/resultVar#index.: 0}, "Return(foo[r])", bint}, |
| 69 | {nestedPtrInterfacescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/nestedPtrInterface#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/nestedPtrInterface#typ.: i}, "PtrInterface(interface{})", i}, |
| 70 | {nestedPtrFunctionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/nestedPtrFunction#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/nestedPtrFunction#typ.: voidFunc}, "PtrFunction(func())", voidFunc}, |
| 71 | {panicArgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/panicArg#{}, "Panic", nil}, |
| 72 | {recoverReturnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/recoverReturn#{}, "Recover", nil}, |
| 73 | } { |
| 74 | if removeModulePrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/removeModulePrefix().(test.s) != removeModulePrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/removeModulePrefix().(test.n.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node#String.()) { |
| 75 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("want %s; got %s", removeModulePrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/removeModulePrefix().(test.s), removeModulePrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/removeModulePrefix().(test.n.Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node#String.())) |
| 76 | } |
| 77 | if test.t != test.n.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node#Type.() { |
| 78 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("want %s; got %s", test.t, test.n.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node#Type.()) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // removeModulePrefix removes the "x.io/" module name prefix throughout s. |
| 84 | // (It is added by testProg.) |
| 85 | func removeModulePrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/removeModulePrefix().(s string) string { |
| 86 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(s, "x.io/", "") |
| 87 | } |
| 88 | |
| 89 | func TestVtaGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/TestVtaGraph().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 90 | // Get the basic type int from a real program. |
| 91 | prog, _, err := testProgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/testProg().(t, "testdata/src/simple.go", ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 92 | if err != nil { |
| 93 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't load testdata/src/simple.go program: %v", err) |
| 94 | } |
| 95 | |
| 96 | glPtrType, ok := prog.AllPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Program#AllPackages().()[0].Varscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Package#Var().("gl").Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Global#Type().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#) |
| 97 | if !ok { |
| 98 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("could not cast gl variable to pointer type") |
| 99 | } |
| 100 | bint := glPtrType.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#Elem().() |
| 101 | |
| 102 | n1 := constantscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constant#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/constant#typ.: bint} |
| 103 | n2 := pointerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/pointer#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/pointer#typ.: typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPointer().(bint)} |
| 104 | n3 := mapKeyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/mapKey#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/mapKey#typ.: typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewMapscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewMap().(bint, bint)} |
| 105 | n4 := mapValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/mapValue#{typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/mapValue#typ.: typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewMapscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewMap().(bint, bint)} |
| 106 | |
| 107 | // Create graph |
| 108 | // n1 n2 |
| 109 | // \ / / |
| 110 | // n3 / |
| 111 | // | / |
| 112 | // n4 |
| 113 | var g vtaGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph# |
| 114 | g.addEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#addEdge().(n1, n3) |
| 115 | g.addEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#addEdge().(n2, n3) |
| 116 | g.addEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#addEdge().(n3, n4) |
| 117 | g.addEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#addEdge().(n2, n4) |
| 118 | // for checking duplicates |
| 119 | g.addEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#addEdge().(n1, n3) |
| 120 | |
| 121 | want := vtaGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#{ |
| 122 | mscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#m.: []map[idxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/idx#]emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#{ |
| 123 | map[idxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/idx#]emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#{1: emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#{}}, |
| 124 | map[idxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/idx#]emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#{3: emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#{}}, |
| 125 | map[idxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/idx#]emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#{1: emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#{}, 3: emptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/empty#{}}, |
| 126 | nil, |
| 127 | }, |
| 128 | idxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#idx.: map[nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node#]idxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/idx#{ |
| 129 | n1: 0, |
| 130 | n3: 1, |
| 131 | n2: 2, |
| 132 | n4: 3, |
| 133 | }, |
| 134 | nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#node.: []nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node#{n1, n3, n2, n4}, |
| 135 | } |
| 136 | |
| 137 | if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(want, g) { |
| 138 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("want %v; got %v", want, g) |
| 139 | } |
| 140 | |
| 141 | for _, test := range []struct { |
| 142 | n nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node# |
| 143 | l int |
| 144 | }{ |
| 145 | {n1, 1}, |
| 146 | {n2, 2}, |
| 147 | {n3, 1}, |
| 148 | {n4, 0}, |
| 149 | } { |
| 150 | sl := 0 |
| 151 | for range g.successorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#successors().(g.idxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#idx.[test.n]) { |
| 152 | sl++ |
| 153 | } |
| 154 | if sl != test.l { |
| 155 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("want %d successors; got %d", test.l, sl) |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // vtaGraphStr stringifies vtaGraph into a list of strings |
| 161 | // where each string represents an edge set of the format |
| 162 | // node -> succ_1, ..., succ_n. succ_1, ..., succ_n are |
| 163 | // sorted in alphabetical order. |
| 164 | func vtaGraphStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraphStr().(g *vtaGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#) []string { |
| 165 | var vgs []string |
| 166 | for n := 0; n < g.numNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#numNodes().(); n++ { |
| 167 | var succStr []string |
| 168 | for s := range g.successorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#successors().(idxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/idx#(n)) { |
| 169 | succStr = append(succStr, g.nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#node.[s].Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node#String.()) |
| 170 | } |
| 171 | |
| 172 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(succStr) |
| 173 | entry := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%v -> %v", g.nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraph#node.[n].Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/node#String.(), stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(succStr, ", ")) |
| 174 | vgs = append(vgs, removeModulePrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/removeModulePrefix().(entry)) |
| 175 | } |
| 176 | return vgs |
| 177 | } |
| 178 | |
| 179 | // setdiff returns the set difference of `X-Y` or {s | s ∈ X, s ∉ Y }. |
| 180 | func setdiffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/setdiff().(X, Y []string) []string { |
| 181 | y := make(map[string]bool) |
| 182 | var delta []string |
| 183 | for _, s := range Y { |
| 184 | y[s] = true |
| 185 | } |
| 186 | |
| 187 | for _, s := range X { |
| 188 | if _, ok := y[s]; !ok { |
| 189 | delta = append(delta, s) |
| 190 | } |
| 191 | } |
| 192 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(delta) |
| 193 | return delta |
| 194 | } |
| 195 | |
| 196 | func TestVTAGraphConstructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/TestVTAGraphConstruction().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 197 | for _, file := range []string{ |
| 198 | "testdata/src/store.go", |
| 199 | "testdata/src/phi.go", |
| 200 | "testdata/src/type_conversions.go", |
| 201 | "testdata/src/type_assertions.go", |
| 202 | "testdata/src/fields.go", |
| 203 | "testdata/src/node_uniqueness.go", |
| 204 | "testdata/src/store_load_alias.go", |
| 205 | "testdata/src/phi_alias.go", |
| 206 | "testdata/src/channels.go", |
| 207 | "testdata/src/generic_channels.go", |
| 208 | "testdata/src/select.go", |
| 209 | "testdata/src/stores_arrays.go", |
| 210 | "testdata/src/maps.go", |
| 211 | "testdata/src/ranges.go", |
| 212 | "testdata/src/closures.go", |
| 213 | "testdata/src/function_alias.go", |
| 214 | "testdata/src/static_calls.go", |
| 215 | "testdata/src/dynamic_calls.go", |
| 216 | "testdata/src/returns.go", |
| 217 | "testdata/src/panic.go", |
| 218 | } { |
| 219 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(file, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 220 | prog, want, err := testProgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/testProg().(t, file, ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/.BuilderModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BuilderMode#(0)) |
| 221 | if err != nil { |
| 222 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't load test file '%s': %s", file, err) |
| 223 | } |
| 224 | if len(want) == 0 { |
| 225 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("couldn't find want in `%s`", file) |
| 226 | } |
| 227 | |
| 228 | fs := ssautilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/.AllFunctionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa/ssautil`/AllFunctions().(prog) |
| 229 | |
| 230 | // First test propagation with lazy-CHA initial call graph. |
| 231 | g, _ := typePropGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/typePropGraph().(fs, makeCalleesFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/makeCalleesFunc().(fs, nil)) |
| 232 | got := vtaGraphStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraphStr().(g) |
| 233 | if diff := setdiffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/setdiff().(want, got); len(diff) > 0 { |
| 234 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("`%s`: want superset of %v;\n got %v\ndiff: %v", file, want, got, diff) |
| 235 | } |
| 236 | |
| 237 | // Repeat the test with explicit CHA initial call graph. |
| 238 | g, _ = typePropGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/typePropGraph().(fs, makeCalleesFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/makeCalleesFunc().(fs, chascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/.CallGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/cha`/CallGraph().(prog))) |
| 239 | got = vtaGraphStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/vtaGraphStr().(g) |
| 240 | if diff := setdiffscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta`/setdiff().(want, got); len(diff) > 0 { |
| 241 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("`%s`: want superset of %v;\n got %v\ndiff: %v", file, want, got, diff) |
| 242 | } |
| 243 | }) |
| 244 | } |
| 245 | } |