Skip to content
Snippets Groups Projects
Select Git revision
  • 3c507195b19128753366bcf352a33865fb85b71a
  • master default protected
  • 1.31
  • 4.38.8
  • 4.38.7
  • 4.38.6
  • 4.38.5
  • 4.38.4
  • 4.38.3
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
23 results

237.html

Blame
  • stubs.go 1.19 KiB
    package printer
    
    import (
    	"go/format"
    
    	"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())
    	}
    
    	// Apply formatting to the result. This is the simplest way to ensure
    	// comment formatting rules introduced in Go 1.19 are applied.  See
    	// https://go.dev/doc/comment.
    	src, err := s.Result()
    	if err != nil {
    		return nil, err
    	}
    
    	return format.Source(src)
    }
    
    func (s *stubs) pragma(p ir.Pragma) {
    	s.Printf("//go:%s", p.Directive)
    	for _, arg := range p.Arguments {
    		s.Printf(" %s", arg)
    	}
    	s.NL()
    }