Skip to content

Commit

Permalink
printer: constraints formatting (#209)
Browse files Browse the repository at this point in the history
Uses `buildtag.Format` to format constraints in the assembly and stub file
printers. This will ensure `// + build` and `//go:build` syntax are used
consistent with the current Go version.

Updates #183
  • Loading branch information
mmcloughlin authored Oct 29, 2021
1 parent 55d98cc commit 79bee1a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
7 changes: 6 additions & 1 deletion printer/goasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"
"strings"

"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/internal/prnt"
"github.com/mmcloughlin/avo/ir"
"github.com/mmcloughlin/avo/operand"
Expand Down Expand Up @@ -44,8 +45,12 @@ func (p *goasm) header(f *ir.File) {
p.Comment(p.cfg.GeneratedWarning())

if len(f.Constraints) > 0 {
constraints, err := buildtags.Format(f.Constraints)
if err != nil {
p.AddError(err)
}
p.NL()
p.Printf(f.Constraints.GoString())
p.Printf(constraints)
}

if len(f.Includes) > 0 {
Expand Down
22 changes: 17 additions & 5 deletions printer/goasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/mmcloughlin/avo/attr"
"github.com/mmcloughlin/avo/build"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/printer"
"github.com/mmcloughlin/avo/reg"
)
Expand Down Expand Up @@ -73,13 +74,24 @@ func TestConstraints(t *testing.T) {
ctx.ConstraintExpr("linux,386 darwin,!cgo")
ctx.ConstraintExpr("!noasm")

AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
expect := []string{
"// Code generated by avo. DO NOT EDIT.",
"",
"// +build linux,386 darwin,!cgo",
"// +build !noasm",
"",
})
}
if buildtags.GoBuildSyntaxSupported() {
expect = append(expect,
"//go:build ((linux && 386) || (darwin && !cgo)) && !noasm",
)
}
if buildtags.PlusBuildSyntaxSupported() {
expect = append(expect,
"// +build linux,386 darwin,!cgo",
"// +build !noasm",
)
}
expect = append(expect, "")

AssertPrintsLines(t, ctx, printer.NewGoAsm, expect)
}

func TestAlignmentNoOperands(t *testing.T) {
Expand Down
7 changes: 6 additions & 1 deletion printer/stubs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package printer

import (
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/internal/prnt"
"github.com/mmcloughlin/avo/ir"
)
Expand All @@ -19,8 +20,12 @@ func (s *stubs) Print(f *ir.File) ([]byte, error) {
s.Comment(s.cfg.GeneratedWarning())

if len(f.Constraints) > 0 {
constraints, err := buildtags.Format(f.Constraints)
if err != nil {
s.AddError(err)
}
s.NL()
s.Printf(f.Constraints.GoString())
s.Printf(constraints)
}

s.NL()
Expand Down
30 changes: 30 additions & 0 deletions printer/stubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/mmcloughlin/avo/build"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/printer"
)

Expand All @@ -26,3 +27,32 @@ func TestStubsPragmas(t *testing.T) {
"",
})
}

func TestStubsConstraints(t *testing.T) {
ctx := build.NewContext()
ctx.ConstraintExpr("linux darwin")
ctx.ConstraintExpr("amd64 arm64 mips64x ppc64x")

expect := []string{
"// Code generated by avo. DO NOT EDIT.",
"",
}
if buildtags.GoBuildSyntaxSupported() {
expect = append(expect,
"//go:build (linux || darwin) && (amd64 || arm64 || mips64x || ppc64x)",
)
}
if buildtags.PlusBuildSyntaxSupported() {
expect = append(expect,
"// +build linux darwin",
"// +build amd64 arm64 mips64x ppc64x",
)
}
expect = append(expect,
"",
"package printer",
"",
)

AssertPrintsLines(t, ctx, printer.NewStubs, expect)
}

0 comments on commit 79bee1a

Please sign in to comment.