GoPLS Viewer

Home|gopls/go/pointer/testdata/context.go
1//go:build ignore
2// +build ignore
3
4package main
5
6// Test of context-sensitive treatment of certain function calls,
7// e.g. static calls to simple accessor methods.
8
9var ab int
10
11type T struct{ x *int }
12
13func (t *TSetX(x *int) { t.x = x }
14func (t *TGetX() *int  { return t.x }
15
16func context1() {
17    var t1t2 T
18    t1.SetX(&a)
19    t2.SetX(&b)
20    print(t1.GetX()) // @pointsto command-line-arguments.a
21    print(t2.GetX()) // @pointsto command-line-arguments.b
22}
23
24func context2() {
25    id := func(x *int) *int {
26        print(x// @pointsto command-line-arguments.a | command-line-arguments.b
27        return x
28    }
29    print(id(&a)) // @pointsto command-line-arguments.a
30    print(id(&b)) // @pointsto command-line-arguments.b
31
32    // Same again, but anon func has free vars.
33    var c int // @line context2c
34    id2 := func(x *int) (*int, *int) {
35        print(x// @pointsto command-line-arguments.a | command-line-arguments.b
36        return x, &c
37    }
38    pq := id2(&a)
39    print(p// @pointsto command-line-arguments.a
40    print(q// @pointsto c@context2c:6
41    rs := id2(&b)
42    print(r// @pointsto command-line-arguments.b
43    print(s// @pointsto c@context2c:6
44}
45
46func main() {
47    context1()
48    context2()
49}
50
MembersX
T.SetX.x
context1.t1
context2.q
context2.c
context2.s
T.SetX.t
T.SetX
T.GetX
context1
context2
context2.p
T.GetX.t
context1.t2
context2.r
Members
X