mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 00:47:56 +01:00
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
// Use of this source code is governed by a BSD-style license that can be found
|
|
// in the LICENSE file.
|
|
|
|
//go:generate go run gen.go
|
|
|
|
package cgen
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
type Generator struct {
|
|
// Extension should be either 'c' or 'h'.
|
|
Extension byte
|
|
}
|
|
|
|
func (g *Generator) WritePreamble(w *bytes.Buffer) error {
|
|
fmt.Fprintf(w, "// Code generated by puffs-gen-%c. DO NOT EDIT.\n\n", g.Extension)
|
|
w.WriteString(preamble)
|
|
if g.Extension == 'h' {
|
|
w.WriteString("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g *Generator) WritePostamble(w *bytes.Buffer) error {
|
|
if g.Extension == 'h' {
|
|
w.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g *Generator) Format(rawSource []byte) ([]byte, error) {
|
|
stdout := &bytes.Buffer{}
|
|
cmd := exec.Command("clang-format", "-style=Chromium")
|
|
cmd.Stdin = bytes.NewReader(rawSource)
|
|
cmd.Stdout = stdout
|
|
cmd.Stderr = os.Stderr
|
|
if err := cmd.Run(); err != nil {
|
|
return nil, err
|
|
}
|
|
return stdout.Bytes(), nil
|
|
}
|