Files
avo/printer/stubs.go
Michael McLoughlin 79bee1a316 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
2021-10-29 01:08:02 -07:00

51 lines
958 B
Go

package printer
import (
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/internal/prnt"
"github.com/mmcloughlin/avo/ir"
)
type stubs struct {
cfg Config
prnt.Generator
}
// NewStubs constructs a printer for writing stub function declarations.
func NewStubs(cfg Config) Printer {
return &stubs{cfg: cfg}
}
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(constraints)
}
s.NL()
s.Printf("package %s\n", s.cfg.Pkg)
for _, fn := range f.Functions() {
s.NL()
s.Comment(fn.Doc...)
for _, pragma := range fn.Pragmas {
s.pragma(pragma)
}
s.Printf("%s\n", fn.Stub())
}
return s.Result()
}
func (s *stubs) pragma(p ir.Pragma) {
s.Printf("//go:%s", p.Directive)
for _, arg := range p.Arguments {
s.Printf(" %s", arg)
}
s.NL()
}