1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:54:40 -07:00

go/ssa: convert tests to new annotation system

Change-Id: I4f44da4fa6a4976b790b6707300cd4facf99c9a5
Reviewed-on: https://go-review.googlesource.com/c/144738
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Ian Cottrell 2018-10-25 16:11:44 -04:00
parent 6dfe7efaa9
commit 89e258047f
2 changed files with 29 additions and 23 deletions

View File

@ -20,6 +20,7 @@ import (
"testing"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/expect"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
@ -232,37 +233,41 @@ func testValueForExpr(t *testing.T, testfile string) {
}
}
// Find the actual AST node for each canonical position.
parenExprByPos := make(map[token.Pos]*ast.ParenExpr)
var parenExprs []*ast.ParenExpr
ast.Inspect(f, func(n ast.Node) bool {
if n != nil {
if e, ok := n.(*ast.ParenExpr); ok {
parenExprByPos[e.Pos()] = e
parenExprs = append(parenExprs, e)
}
}
return true
})
// Find all annotations of form /*@kind*/.
for _, c := range f.Comments {
text := strings.TrimSpace(c.Text())
if text == "" || text[0] != '@' {
continue
notes, err := expect.Extract(prog.Fset, f)
if err != nil {
t.Fatal(err)
}
for _, n := range notes {
want := n.Name
if want == "nil" {
want = "<nil>"
}
text = text[1:]
pos := c.End() + 1
position := prog.Fset.Position(pos)
position := prog.Fset.Position(n.Pos)
var e ast.Expr
if target := parenExprByPos[pos]; target == nil {
t.Errorf("%s: annotation doesn't precede ParenExpr: %q", position, text)
for _, paren := range parenExprs {
if paren.Pos() > n.Pos {
e = paren.X
break
}
}
if e == nil {
t.Errorf("%s: note doesn't precede ParenExpr: %q", position, want)
continue
} else {
e = target.X
}
path, _ := astutil.PathEnclosingInterval(f, pos, pos)
path, _ := astutil.PathEnclosingInterval(f, n.Pos, n.Pos)
if path == nil {
t.Errorf("%s: can't find AST path from root to comment: %s", position, text)
t.Errorf("%s: can't find AST path from root to comment: %s", position, want)
continue
}
@ -274,7 +279,7 @@ func testValueForExpr(t *testing.T, testfile string) {
v, gotAddr := fn.ValueForExpr(e) // (may be nil)
got := strings.TrimPrefix(fmt.Sprintf("%T", v), "*ssa.")
if want := text; got != want {
if got != want {
t.Errorf("%s: got value %q, want %q", position, got, want)
}
if v != nil {

View File

@ -10,12 +10,13 @@ package main
func f(spilled, unspilled int) {
_ = /*@UnOp*/ (spilled)
_ = /*@Parameter*/ (unspilled)
_ = /*@<nil>*/ (1 + 2) // (constant)
_ = /*@nil*/ (1 + 2) // (constant)
i := 0
f := func() (int, int) { return 0, 0 }
/*@Call*/ (print( /*@BinOp*/ (i + 1)))
/*@Call*/
(print( /*@BinOp*/ (i + 1)))
_, _ = /*@Call*/ (f())
ch := /*@MakeChan*/ (make(chan int))
/*@UnOp*/ (<-ch)
@ -43,7 +44,7 @@ func f(spilled, unspilled int) {
sl := []int{}
_ = /*@Slice*/ (sl[:0])
_ = /*@<nil>*/ (new(int)) // optimized away
_ = /*@nil*/ (new(int)) // optimized away
tmp := /*@Alloc*/ (new(int))
_ = tmp
var iface interface{}
@ -88,7 +89,7 @@ func complit() {
_, _, _ = sl1, sl2, sl3
_ = /*@Slice*/ ([]int{})
_ = /*@<nil>*/ (& /*@Slice*/ ([]int{})) // & optimized away
_ = /*@nil*/ (& /*@Slice*/ ([]int{})) // & optimized away
_ = & /*@Slice*/ ([]int{})
// 2. Arrays
@ -117,7 +118,7 @@ func complit() {
_, _, _ = m1, m2, m3
_ = /*@MakeMap*/ (M{})
_ = /*@<nil>*/ (& /*@MakeMap*/ (M{})) // & optimized away
_ = /*@nil*/ (& /*@MakeMap*/ (M{})) // & optimized away
_ = & /*@MakeMap*/ (M{})
// 4. Structs