Skip to content
Open
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
3 changes: 2 additions & 1 deletion cmd/bpf2go/gen/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cilium/ebpf/btf"
b2gInt "github.com/cilium/ebpf/cmd/bpf2go/internal"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/codegen"
)

//go:embed output.tpl
Expand Down Expand Up @@ -203,7 +204,7 @@ func Generate(args GenerateArgs) error {
return fmt.Errorf("can't generate types: %s", err)
}

return internal.WriteFormatted(buf.Bytes(), args.Output)
return codegen.WriteFormatted(buf.Bytes(), args.Output)
}

// sortTypes returns a list of types sorted by their (generated) Go type name.
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/gentypes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/codegen"
"github.com/cilium/ebpf/internal/sys"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func run(args []string) error {
}
defer w.Close()

return internal.WriteFormatted(output, w)
return codegen.WriteFormatted(output, w)
}

func generateTypes(spec *btf.Spec) ([]byte, error) {
Expand Down
50 changes: 50 additions & 0 deletions internal/codegen/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Package codegen contains helpers for tools generating Go code.
//
// It is kept separate from package internal since go/format and its
// dependencies would otherwise be linked and initialized in every binary
// importing the library.
package codegen

import (
"bytes"
"errors"
"go/format"
"go/scanner"
"io"
)

// WriteFormatted outputs a formatted src into out.
//
// If formatting fails it returns an informative error message.
func WriteFormatted(src []byte, out io.Writer) error {
formatted, err := format.Source(src)
if err == nil {
_, err = out.Write(formatted)
return err
}

var el scanner.ErrorList
if !errors.As(err, &el) {
return err
}

var nel scanner.ErrorList
for _, err := range el {
if !err.Pos.IsValid() {
nel = append(nel, err)
continue
}

buf := src[err.Pos.Offset:]
nl := bytes.IndexRune(buf, '\n')
if nl == -1 {
nel = append(nel, err)
continue
}

err.Msg += ": " + string(buf[:nl])
nel = append(nel, err)
}

return nel
}
41 changes: 0 additions & 41 deletions internal/output.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package internal

import (
"bytes"
"errors"
"go/format"
"go/scanner"
"io"
"reflect"
"strings"
"unicode"
Expand Down Expand Up @@ -48,42 +43,6 @@ func Identifier(str string) string {
}, str)
}

// WriteFormatted outputs a formatted src into out.
//
// If formatting fails it returns an informative error message.
func WriteFormatted(src []byte, out io.Writer) error {
formatted, err := format.Source(src)
if err == nil {
_, err = out.Write(formatted)
return err
}

var el scanner.ErrorList
if !errors.As(err, &el) {
return err
}

var nel scanner.ErrorList
for _, err := range el {
if !err.Pos.IsValid() {
nel = append(nel, err)
continue
}

buf := src[err.Pos.Offset:]
nl := bytes.IndexRune(buf, '\n')
if nl == -1 {
nel = append(nel, err)
continue
}

err.Msg += ": " + string(buf[:nl])
nel = append(nel, err)
}

return nel
}

// GoTypeName is like %T, but elides the package name.
//
// Pointers to a type are peeled off.
Expand Down
Loading