Debian Codegraph @

printer/stubs.go

raw symbols SCIP
1 package printer
2
3 import (
4 "go/format"
5
6 "github.com/mmcloughlin/avo/buildtags"
7 "github.com/mmcloughlin/avo/internal/prnt"
8 "github.com/mmcloughlin/avo/ir"
9 )
10
11 type stubs struct {
12 cfg Config
13 prnt.Generator
14 }
15
16 // NewStubs constructs a printer for writing stub function declarations.
17 func NewStubs(cfg Config) Printer {
18 return &stubs{cfg: cfg}
19 }
20
21 func (s *stubs) Print(f *ir.File) ([]byte, error) {
22 s.Comment(s.cfg.GeneratedWarning())
23
24 if len(f.Constraints) > 0 {
25 constraints, err := buildtags.Format(f.Constraints)
26 if err != nil {
27 s.AddError(err)
28 }
29 s.NL()
30 s.Printf(constraints)
31 }
32
33 s.NL()
34 s.Printf("package %s\n", s.cfg.Pkg)
35 for _, fn := range f.Functions() {
36 s.NL()
37 s.Comment(fn.Doc...)
38 for _, pragma := range fn.Pragmas {
39 s.pragma(pragma)
40 }
41 s.Printf("%s\n", fn.Stub())
42 }
43
44 // Apply formatting to the result. This is the simplest way to ensure
45 // comment formatting rules introduced in Go 1.19 are applied. See
46 // https://go.dev/doc/comment.
47 src, err := s.Result()
48 if err != nil {
49 return nil, err
50 }
51
52 return format.Source(src)
53 }
54
55 func (s *stubs) pragma(p ir.Pragma) {
56 s.Printf("//go:%s", p.Directive)
57 for _, arg := range p.Arguments {
58 s.Printf(" %s", arg)
59 }
60 s.NL()
61 }