| 1 |
// Copyright 2024 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 |
package ir_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"path/filepath" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"honnef.co/go/tools/go/ir/irutil" |
| 13 |
"honnef.co/go/tools/internal/xtools-internal/testenv" |
| 14 |
|
| 15 |
"golang.org/x/tools/go/packages" |
| 16 |
) |
| 17 |
|
| 18 |
func TestDominatorOrder(t *testing.T) { |
| 19 |
testenv.NeedsGoBuild(t) // for go/packages |
| 20 |
|
| 21 |
const src = `package p |
| 22 |
|
| 23 |
func f(cond bool) { |
| 24 |
// (Print operands match BasicBlock IDs.) |
| 25 |
print(0) |
| 26 |
if cond { |
| 27 |
print(1) |
| 28 |
} else { |
| 29 |
print(2) |
| 30 |
} |
| 31 |
print(3) |
| 32 |
} |
| 33 |
` |
| 34 |
dir := t.TempDir() |
| 35 |
cfg := &packages.Config{ |
| 36 |
Dir: dir, |
| 37 |
Mode: packages.LoadSyntax, |
| 38 |
Overlay: map[string][]byte{ |
| 39 |
filepath.Join(dir, "p.go"): []byte(src), |
| 40 |
}, |
| 41 |
} |
| 42 |
initial, err := packages.Load(cfg, "./p.go") |
| 43 |
if err != nil { |
| 44 |
t.Fatal(err) |
| 45 |
} |
| 46 |
if packages.PrintErrors(initial) > 0 { |
| 47 |
t.Fatal("packages contain errors") |
| 48 |
} |
| 49 |
_, pkgs := irutil.Packages(initial, 0) |
| 50 |
p := pkgs[0] |
| 51 |
p.Build() |
| 52 |
f := p.Func("f") |
| 53 |
|
| 54 |
if got, want := fmt.Sprint(f.DomPreorder()), "[0 1 2 3]"; got != want { |
| 55 |
t.Errorf("DomPreorder: got %v, want %s", got, want) |
| 56 |
} |
| 57 |
if got, want := fmt.Sprint(f.DomPostorder()), "[1 2 3 0]"; got != want { |
| 58 |
t.Errorf("DomPostorder: got %v, want %s", got, want) |
| 59 |
} |
| 60 |
} |