github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/pointer/testdata/context.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  // Test of context-sensitive treatment of certain function calls,
     7  // e.g. static calls to simple accessor methods.
     8  
     9  var a, b int
    10  
    11  type T struct{ x *int }
    12  
    13  func (t *T) SetX(x *int) { t.x = x }
    14  func (t *T) GetX() *int  { return t.x }
    15  
    16  func context1() {
    17  	var t1, t2 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  
    24  func 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  	p, q := id2(&a)
    39  	print(p) // @pointsto command-line-arguments.a
    40  	print(q) // @pointsto c@context2c:6
    41  	r, s := id2(&b)
    42  	print(r) // @pointsto command-line-arguments.b
    43  	print(s) // @pointsto c@context2c:6
    44  }
    45  
    46  func main() {
    47  	context1()
    48  	context2()
    49  }