File: static_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.5.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 16,592 kB
  • sloc: javascript: 2,011; asm: 1,635; sh: 192; yacc: 155; makefile: 52; ansic: 8
file content (142 lines) | stat: -rw-r--r-- 2,390 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package static_test

import (
	"fmt"
	"go/parser"
	"reflect"
	"sort"
	"testing"

	"golang.org/x/tools/go/callgraph"
	"golang.org/x/tools/go/callgraph/static"
	"golang.org/x/tools/go/loader"
	"golang.org/x/tools/go/ssa"
	"golang.org/x/tools/go/ssa/ssautil"
	"golang.org/x/tools/internal/typeparams"
)

const input = `package P

type C int
func (C) f()

type I interface{f()}

func f() {
	p := func() {}
	g()
	p() // SSA constant propagation => static

	if unknown {
		p = h
	}
	p() // dynamic

	C(0).f()
}

func g() {
	var i I = C(0)
	i.f()
}

func h()

var unknown bool
`

const genericsInput = `package P

type I interface {
	F()
}

type A struct{}

func (a A) F() {}

type B struct{}

func (b B) F() {}

func instantiated[X I](x X) {
	x.F()
}

func Bar() {}

func f(h func(), a A, b B) {
	h()

	instantiated[A](a)
	instantiated[B](b)
}
`

func TestStatic(t *testing.T) {
	for _, e := range []struct {
		input string
		want  []string
		// typeparams must be true if input uses type parameters
		typeparams bool
	}{
		{input, []string{
			"(*C).f -> (C).f",
			"f -> (C).f",
			"f -> f$1",
			"f -> g",
		}, false},
		{genericsInput, []string{
			"(*A).F -> (A).F",
			"(*B).F -> (B).F",
			"f -> instantiated[P.A]",
			"f -> instantiated[P.B]",
			"instantiated[P.A] -> (A).F",
			"instantiated[P.B] -> (B).F",
		}, true},
	} {
		if e.typeparams && !typeparams.Enabled {
			// Skip tests with type parameters when the build
			// environment is not supporting any.
			continue
		}

		conf := loader.Config{ParserMode: parser.ParseComments}
		f, err := conf.ParseFile("P.go", e.input)
		if err != nil {
			t.Error(err)
			continue
		}

		conf.CreateFromFiles("P", f)
		iprog, err := conf.Load()
		if err != nil {
			t.Error(err)
			continue
		}

		P := iprog.Created[0].Pkg

		prog := ssautil.CreateProgram(iprog, ssa.InstantiateGenerics)
		prog.Build()

		cg := static.CallGraph(prog)

		var edges []string
		callgraph.GraphVisitEdges(cg, func(e *callgraph.Edge) error {
			edges = append(edges, fmt.Sprintf("%s -> %s",
				e.Caller.Func.RelString(P),
				e.Callee.Func.RelString(P)))
			return nil
		})
		sort.Strings(edges)

		if !reflect.DeepEqual(edges, e.want) {
			t.Errorf("Got edges %v, want %v", edges, e.want)
		}
	}
}