github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/oracle/testdata/src/main/calls.go (about) 1 package main 2 3 // Tests of call-graph queries. 4 // See go.tools/oracle/oracle_test.go for explanation. 5 // See calls.golden for expected query results. 6 7 func A(x *int) { // @pointsto pointsto-A-x "x" 8 // @callers callers-A "^" 9 // @callstack callstack-A "^" 10 } 11 12 func B(x *int) { // @pointsto pointsto-B-x "x" 13 // @callers callers-B "^" 14 } 15 16 // apply is not (yet) treated context-sensitively. 17 func apply(f func(x *int), x *int) { 18 f(x) // @callees callees-apply "f" 19 // @callers callers-apply "^" 20 } 21 22 // store *is* treated context-sensitively, 23 // so the points-to sets for pc, pd are precise. 24 func store(ptr **int, value *int) { 25 *ptr = value 26 // @callers callers-store "^" 27 } 28 29 func call(f func() *int) { 30 // Result points to anon function. 31 f() // @pointsto pointsto-result-f "f" 32 33 // Target of call is anon function. 34 f() // @callees callees-main.call-f "f" 35 36 // @callers callers-main.call "^" 37 } 38 39 func main() { 40 var a, b int 41 apply(A, &a) // @callees callees-main-apply1 "app" 42 apply(B, &b) 43 44 var c, d int 45 var pc, pd *int // @pointsto pointsto-pc "pc" 46 store(&pc, &c) 47 store(&pd, &d) 48 _ = pd // @pointsto pointsto-pd "pd" 49 50 call(func() *int { 51 // We are called twice from main.call 52 // @callers callers-main.anon "^" 53 return &a 54 }) 55 56 // Errors 57 _ = "no function call here" // @callees callees-err-no-call "no" 58 print("builtin") // @callees callees-err-builtin "builtin" 59 _ = string("type conversion") // @callees callees-err-conversion "str" 60 call(nil) // @callees callees-err-bad-selection "call\\(nil" 61 if false { 62 main() // @callees callees-err-deadcode1 "main" 63 } 64 var nilFunc func() 65 nilFunc() // @callees callees-err-nil-func "nilFunc" 66 var i interface { 67 f() 68 } 69 i.f() // @callees callees-err-nil-interface "i.f" 70 71 i = new(myint) 72 i.f() // @callees callees-not-a-wrapper "f" 73 } 74 75 type myint int 76 77 func (myint) f() { 78 // @callers callers-not-a-wrapper "^" 79 } 80 81 var dynamic = func() {} 82 83 func deadcode() { 84 main() // @callees callees-err-deadcode2 "main" 85 // @callers callers-err-deadcode "^" 86 // @callstack callstack-err-deadcode "^" 87 88 // Within dead code, dynamic calls have no callees. 89 dynamic() // @callees callees-err-deadcode3 "dynamic" 90 } 91 92 // This code belongs to init. 93 var global = 123 // @callers callers-global "global" 94 95 // The package initializer may be called by other packages' inits, or 96 // in this case, the root of the callgraph. The source-level init functions 97 // are in turn called by it. 98 func init() { 99 // @callstack callstack-init "^" 100 }