github.com/april1989/origin-go-tools@v0.0.32/cmd/guru/testdata/src/pointsto/main.go (about)

     1  package 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  
     7  const pi = 3.141 // @pointsto const "pi"
     8  
     9  var global = new(string) // NB: ssa.Global is indirect, i.e. **string
    10  
    11  func 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 a, b 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{} }{f: make(chan bool)}
    56  	print(s.f) // @pointsto var-ref-s-f "s.f"
    57  }
    58  
    59  func livecode() {} // @pointsto func-live "livecode"
    60  
    61  func 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  
    67  type I interface {
    68  	f()
    69  }
    70  
    71  type C int
    72  type D struct{}
    73  
    74  func (c *C) f() {}
    75  func (d D) f()  {}