Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: go:build formatting #207

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.15.x, 1.16.x]
go-version: [1.16.x, 1.17.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
lint:
strategy:
matrix:
go-version: [1.16.x]
go-version: [1.17.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ $ go get -u github.com/mmcloughlin/avo

[embedmd]:# (examples/add/asm.go)
```go
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions attr/make_textflag.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
40 changes: 40 additions & 0 deletions buildtags/syntax.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package buildtags

import (
"bufio"
"bytes"
"fmt"
"go/format"
"strings"
)

func PlusBuildSyntaxSupported() bool { return plusbuild }

func GoBuildSyntaxSupported() bool { return gobuild }

func Format(t ConstraintsConvertable) (string, error) {
// Print build tags to minimal Go source that can be passed to go/format.
src := t.ToConstraints().GoString() + "\npackage stub"

// Format them.
formatted, err := format.Source([]byte(src))
if err != nil {
return "", fmt.Errorf("format build constraints: %w", err)
}

// Extract the comment lines.
buf := bytes.NewReader(formatted)
scanner := bufio.NewScanner(buf)
output := ""
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "//") {
output += line + "\n"
}
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("parse formatted build constraints: %w", err)
}

return output, nil
}
9 changes: 9 additions & 0 deletions buildtags/syntax_go116.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !go1.17
// +build !go1.17

package buildtags

const (
plusbuild = true
gobuild = false
)
9 changes: 9 additions & 0 deletions buildtags/syntax_go117.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build go1.17 && !go1.18
// +build go1.17,!go1.18

package buildtags

const (
plusbuild = true
gobuild = true
)
9 changes: 9 additions & 0 deletions buildtags/syntax_go118.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build go1.18
// +build go1.18

package buildtags

const (
plusbuild = false
gobuild = true
)
1 change: 1 addition & 0 deletions examples/add/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The [code generator](asm.go) is as follows:

[embedmd]:# (asm.go)
```go
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/add/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/args/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/complex/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/data/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/dot/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/ext/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/fnv1a/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/geohash/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/pragma/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/returns/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/sha1/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions examples/sum/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions operand/make_const.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
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 {
src, err := buildtags.Format(f.Constraints)
if err != nil {
p.AddError(err)
}
p.NL()
p.Printf(f.Constraints.GoString())
p.Printf(src)
}

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 {
src, err := buildtags.Format(f.Constraints)
if err != nil {
s.AddError(err)
}
s.NL()
s.Printf(f.Constraints.GoString())
s.Printf(src)
}

s.NL()
Expand Down
2 changes: 1 addition & 1 deletion script/tools.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/campoy/embedmd v1.0.0 // indirect
github.com/dlespiau/covertool v0.0.0-20180314162135-b0c4c6d0583a // indirect
github.com/klauspost/asmfmt v1.2.1 // indirect
github.com/klauspost/asmfmt v1.3.1 // indirect
github.com/urfave/cli v1.22.5 // indirect
mvdan.cc/gofumpt v0.0.0-20200412215918-a91da47f375c // indirect
)
2 changes: 2 additions & 0 deletions script/tools.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/dlespiau/covertool v0.0.0-20180314162135-b0c4c6d0583a h1:+cYgqwB++gEE
github.com/dlespiau/covertool v0.0.0-20180314162135-b0c4c6d0583a/go.mod h1:/eQMcW3eA1bzKx23ZYI2H3tXPdJB5JWYTHzoUPBvQY4=
github.com/klauspost/asmfmt v1.2.1 h1:LgH5hc6QnY2sDT2K+ilscIzcZpfQ1xlayuTyLxo4pOA=
github.com/klauspost/asmfmt v1.2.1/go.mod h1:RAoUvqkWr2rUa2I19qKMEVZQe4BVtcHGTMCUOcCU2Lg=
github.com/klauspost/asmfmt v1.3.1 h1:7xZi1N7s9gTLbqiM8KUv8TLyysavbTRGBT5/ly0bRtw=
github.com/klauspost/asmfmt v1.3.1/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down
1 change: 1 addition & 0 deletions tests/alloc/gp8/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/alloc/masks/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/alloc/upper32/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/cast/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
5 changes: 2 additions & 3 deletions tests/fixedbugs/issue100/allocfail/allocfail.s
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Code generated by command: go run asm.go -out allocfail.s -stubs stubs.go. DO NOT EDIT.

// +build !appengine
// +build !noasm
// +build gc
//go:build !appengine && !noasm && gc
// +build !appengine,!noasm,gc

#include "textflag.h"

Expand Down
1 change: 1 addition & 0 deletions tests/fixedbugs/issue100/allocfail/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
5 changes: 2 additions & 3 deletions tests/fixedbugs/issue100/allocfail/stubs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/fixedbugs/issue100/minrepro/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/fixedbugs/issue122/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/fixedbugs/issue50/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/fixedbugs/issue62/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/fixedbugs/issue65/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/fixedbugs/issue68/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/fixedbugs/issue76/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/fixedbugs/issue89/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/fmt/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/labels/asm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down
1 change: 1 addition & 0 deletions tests/textflag/make_attrtest.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package main
Expand Down