| 1 |
// Copyright 2013 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// No testdata on Android. |
| 6 |
|
| 7 |
//go:build !android |
| 8 |
|
| 9 |
package irutil |
| 10 |
|
| 11 |
import ( |
| 12 |
"bytes" |
| 13 |
"fmt" |
| 14 |
"go/parser" |
| 15 |
"path/filepath" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"honnef.co/go/tools/go/ir" |
| 20 |
|
| 21 |
"golang.org/x/tools/go/analysis/analysistest" |
| 22 |
//lint:ignore SA1019 go/loader is deprecated, but works fine for our tests |
| 23 |
"golang.org/x/tools/go/loader" |
| 24 |
) |
| 25 |
|
| 26 |
func TestSwitches(t *testing.T) { |
| 27 |
conf := loader.Config{ParserMode: parser.ParseComments} |
| 28 |
f, err := conf.ParseFile(filepath.Join(analysistest.TestData(), "switches.go"), nil) |
| 29 |
if err != nil { |
| 30 |
t.Error(err) |
| 31 |
return |
| 32 |
} |
| 33 |
|
| 34 |
conf.CreateFromFiles("main", f) |
| 35 |
iprog, err := conf.Load() |
| 36 |
if err != nil { |
| 37 |
t.Error(err) |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
prog := CreateProgram(iprog, 0) |
| 42 |
mainPkg := prog.Package(iprog.Created[0].Pkg) |
| 43 |
mainPkg.Build() |
| 44 |
|
| 45 |
for _, mem := range mainPkg.Members { |
| 46 |
if fn, ok := mem.(*ir.Function); ok { |
| 47 |
if fn.Synthetic != 0 { |
| 48 |
continue // e.g. init() |
| 49 |
} |
| 50 |
// Each (multi-line) "switch" comment within |
| 51 |
// this function must match the printed form |
| 52 |
// of a ConstSwitch. |
| 53 |
var wantSwitches []string |
| 54 |
for _, c := range f.Comments { |
| 55 |
if fn.Source().Pos() <= c.Pos() && c.Pos() < fn.Source().End() { |
| 56 |
text := strings.TrimSpace(c.Text()) |
| 57 |
if strings.HasPrefix(text, "switch ") { |
| 58 |
wantSwitches = append(wantSwitches, text) |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
switches := Switches(fn) |
| 64 |
if len(switches) != len(wantSwitches) { |
| 65 |
t.Errorf("in %s, found %d switches, want %d", fn, len(switches), len(wantSwitches)) |
| 66 |
} |
| 67 |
for i, sw := range switches { |
| 68 |
got := sw.testString() |
| 69 |
if i >= len(wantSwitches) { |
| 70 |
continue |
| 71 |
} |
| 72 |
want := wantSwitches[i] |
| 73 |
if got != want { |
| 74 |
t.Errorf("in %s, found switch %d: got <<%s>>, want <<%s>>", fn, i, got, want) |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func (sw *Switch) testString() string { |
| 82 |
// same as the actual String method, but use the second to last |
| 83 |
// instruction instead, to skip over all the phi and sigma nodes |
| 84 |
// that SSI produces. |
| 85 |
var buf bytes.Buffer |
| 86 |
if sw.ConstCases != nil { |
| 87 |
fmt.Fprintf(&buf, "switch %s {\n", sw.X.Name()) |
| 88 |
for _, c := range sw.ConstCases { |
| 89 |
n := max(len(c.Body.Instrs)-2, 0) |
| 90 |
fmt.Fprintf(&buf, "case %s: %s\n", c.Value.Name(), c.Body.Instrs[n]) |
| 91 |
} |
| 92 |
} else { |
| 93 |
fmt.Fprintf(&buf, "switch %s.(type) {\n", sw.X.Name()) |
| 94 |
for _, c := range sw.TypeCases { |
| 95 |
n := max(len(c.Body.Instrs)-2, 0) |
| 96 |
fmt.Fprintf(&buf, "case %s %s: %s\n", |
| 97 |
c.Binding.Name(), c.Type, c.Body.Instrs[n]) |
| 98 |
} |
| 99 |
} |
| 100 |
if sw.Default != nil { |
| 101 |
n := max(len(sw.Default.Instrs)-2, 0) |
| 102 |
fmt.Fprintf(&buf, "default: %s\n", sw.Default.Instrs[n]) |
| 103 |
} |
| 104 |
fmt.Fprintf(&buf, "}") |
| 105 |
return buf.String() |
| 106 |
} |