From 89e258047f9bd4255fae0d48139e1d5ff0a7b8dd Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Thu, 25 Oct 2018 16:11:44 -0400 Subject: [PATCH] go/ssa: convert tests to new annotation system Change-Id: I4f44da4fa6a4976b790b6707300cd4facf99c9a5 Reviewed-on: https://go-review.googlesource.com/c/144738 Reviewed-by: Alan Donovan --- go/ssa/source_test.go | 41 ++++++++++++++++++--------------- go/ssa/testdata/valueforexpr.go | 11 +++++---- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/go/ssa/source_test.go b/go/ssa/source_test.go index d543c1b1..e3e70233 100644 --- a/go/ssa/source_test.go +++ b/go/ssa/source_test.go @@ -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 = "" } - 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 { diff --git a/go/ssa/testdata/valueforexpr.go b/go/ssa/testdata/valueforexpr.go index 4a2cb85a..fe65b94f 100644 --- a/go/ssa/testdata/valueforexpr.go +++ b/go/ssa/testdata/valueforexpr.go @@ -10,12 +10,13 @@ package main func f(spilled, unspilled int) { _ = /*@UnOp*/ (spilled) _ = /*@Parameter*/ (unspilled) - _ = /*@*/ (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]) - _ = /*@*/ (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{}) - _ = /*@*/ (& /*@Slice*/ ([]int{})) // & optimized away + _ = /*@nil*/ (& /*@Slice*/ ([]int{})) // & optimized away _ = & /*@Slice*/ ([]int{}) // 2. Arrays @@ -117,7 +118,7 @@ func complit() { _, _, _ = m1, m2, m3 _ = /*@MakeMap*/ (M{}) - _ = /*@*/ (& /*@MakeMap*/ (M{})) // & optimized away + _ = /*@nil*/ (& /*@MakeMap*/ (M{})) // & optimized away _ = & /*@MakeMap*/ (M{}) // 4. Structs