-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/callgraph/vta: add basic tests for range-over-func
The tests check the callees at the call to yield and type flow out of the range-over-func iterator. If needed, a test for defer will be added in a follow-up CL. Change-Id: Ic9208ac0824a36fb50879730e8ec9398b9b6e284 Reviewed-on: https://go-review.googlesource.com/c/tools/+/611395 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Tim King <taking@google.com>
- Loading branch information
1 parent
2c7aaab
commit 075ae7d
Showing
2 changed files
with
107 additions
and
9 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
go/callgraph/vta/testdata/src/callgraph_range_over_func.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// go:build ignore | ||
|
||
package testdata | ||
|
||
type I interface { | ||
Foo() | ||
} | ||
|
||
type A struct{} | ||
|
||
func (a A) Foo() {} | ||
|
||
type B struct{} | ||
|
||
func (b B) Foo() {} | ||
|
||
type C struct{} | ||
|
||
func (c C) Foo() {} // Test that this is not called. | ||
|
||
type iset []I | ||
|
||
func (i iset) All() func(func(I) bool) { | ||
return func(yield func(I) bool) { | ||
for _, v := range i { | ||
if !yield(v) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
var x = iset([]I{A{}, B{}}) | ||
|
||
func X() { | ||
for i := range x.All() { | ||
i.Foo() | ||
} | ||
} | ||
|
||
func Y() I { | ||
for i := range x.All() { | ||
return i | ||
} | ||
return nil | ||
} | ||
|
||
func Bar() { | ||
X() | ||
y := Y() | ||
y.Foo() | ||
} | ||
|
||
// Relevant SSA: | ||
//func X$1(I) bool: | ||
// t0 = *jump$1 | ||
// t1 = t0 == 0:int | ||
// if t1 goto 1 else 2 | ||
//1: | ||
// *jump$1 = -1:int | ||
// t2 = invoke arg0.Foo() | ||
// *jump$1 = 0:int | ||
// return true:bool | ||
//2: | ||
// t3 = make interface{} <- string ("yield function ca...":string) interface{} | ||
// panic t3 | ||
// | ||
//func All$1(yield func(I) bool): | ||
// t0 = *i | ||
// t1 = len(t0) | ||
// jump 1 | ||
//1: | ||
// t2 = phi [0: -1:int, 2: t3] #rangeindex | ||
// t3 = t2 + 1:int | ||
// t4 = t3 < t1 | ||
// if t4 goto 2 else 3 | ||
//2: | ||
// t5 = &t0[t3] | ||
// t6 = *t5 | ||
// t7 = yield(t6) | ||
// if t7 goto 1 else 4 | ||
// | ||
//func Bar(): | ||
// t0 = X() | ||
// t1 = Y() | ||
// t2 = invoke t1.Foo() | ||
// return | ||
|
||
// WANT: | ||
// Bar: X() -> X; Y() -> Y; invoke t1.Foo() -> A.Foo, B.Foo | ||
// X$1: invoke arg0.Foo() -> A.Foo, B.Foo | ||
// All$1: yield(t6) -> X$1, Y$1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters