GoPLS Viewer

Home|gopls/cmd/guru/testdata/src/pointsto/main.go
1package main
2
3// Tests of 'pointsto' query.
4// See go.tools/guru/guru_test.go for explanation.
5// See pointsto.golden for expected query results.
6
7const pi = 3.141 // @pointsto const "pi"
8
9var global = new(string// NB: ssa.Global is indirect, i.e. **string
10
11func main() {
12    livecode()
13
14    // func objects
15    _ = main   // @pointsto func-ref-main "main"
16    _ = (*C).f // @pointsto func-ref-*C.f "..C..f"
17    _ = D.f    // @pointsto func-ref-D.f "D.f"
18    _ = I.f    // @pointsto func-ref-I.f "I.f"
19    var d D
20    var i I
21    _ = d.f // @pointsto func-ref-d.f "d.f"
22    _ = i.f // @pointsto func-ref-i.f "i.f"
23
24    // var objects
25    anon := func() {
26        _ = d.f // @pointsto ref-lexical-d.f "d.f"
27    }
28    _ = anon   // @pointsto ref-anon "anon"
29    _ = global // @pointsto ref-global "global"
30
31    // SSA affords some local flow sensitivity.
32    var ab int
33    var x = &a // @pointsto var-def-x-1 "x"
34    _ = x      // @pointsto var-ref-x-1 "x"
35    x = &b     // @pointsto var-def-x-2 "x"
36    _ = x      // @pointsto var-ref-x-2 "x"
37
38    i = new(C// @pointsto var-ref-i-C "i"
39    if i != nil {
40        i = D{} // @pointsto var-ref-i-D "i"
41    }
42    print(i// @pointsto var-ref-i "\\bi\\b"
43
44    m := map[string]*int{"a": &a}
45    mapval_ := m["a"// @pointsto map-lookup,ok "m..a.."
46    _ = mapval          // @pointsto mapval "mapval"
47    _ = m               // @pointsto m "m"
48
49    if false {
50        panic(3// @pointsto builtin-panic "panic"
51    }
52
53    // NB: s.f is addressable per (*ssa.Program).VarValue,
54    // but our query concerns the object, not its address.
55    s := struct{ f interface{} }{fmake(chan bool)}
56    print(s.f// @pointsto var-ref-s-f "s.f"
57}
58
59func livecode() {} // @pointsto func-live "livecode"
60
61func deadcode() { // @pointsto func-dead "deadcode"
62    // Pointer analysis can't run on dead code.
63    var b = new(int// @pointsto b "b"
64    _ = b
65}
66
67type I interface {
68    f()
69}
70
71type C int
72type D struct{}
73
74func (c *Cf() {}
75func (d Df()  {}
76
MembersX
main
main.i
main.a
main.m
D.f
pi
main.d
main.s
deadcode
D.f.d
main.b
livecode
I
C.f.c
C
D
C.f
Members
X