printer: constraints formatting (#209)
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
This commit is contained in:
committed by
GitHub
parent
55d98ccf77
commit
79bee1a316
@@ -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"
|
||||
@@ -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 {
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
@@ -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.",
|
||||
"",
|
||||
}
|
||||
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) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package printer
|
||||
|
||||
import (
|
||||
"github.com/mmcloughlin/avo/buildtags"
|
||||
"github.com/mmcloughlin/avo/internal/prnt"
|
||||
"github.com/mmcloughlin/avo/ir"
|
||||
)
|
||||
@@ -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()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/mmcloughlin/avo/build"
|
||||
"github.com/mmcloughlin/avo/buildtags"
|
||||
"github.com/mmcloughlin/avo/printer"
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user