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