printer: add generated code warnings

This commit is contained in:
Michael McLoughlin
2018-12-18 22:57:26 -08:00
parent 213d65e481
commit ca5c7e7454
17 changed files with 94 additions and 9 deletions

View File

@@ -58,7 +58,7 @@ func NewFlags(fs *flag.FlagSet) *Flags {
}
func (f *Flags) Config() *Config {
pc := printer.NewDefaultConfig()
pc := printer.NewGoRunConfig()
passes := []pass.Interface{pass.Compile}
for _, pv := range f.printers {
p := pv.Build(pc)

View File

@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT.
#include "textflag.h"

View File

@@ -1,3 +1,5 @@
// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT.
package add
func Add(x uint64, y uint64) uint64

View File

@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out complex.s -stubs stub.go. DO NOT EDIT.
#include "textflag.h"

View File

@@ -1,5 +1,9 @@
// Code generated by command: go run asm.go -out complex.s -stubs stub.go. DO NOT EDIT.
package complex
func Real(x complex128) float64
func Imag(x complex128) float64
func Norm(x complex128) float64

View File

@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out components.s -stubs stub.go. DO NOT EDIT.
#include "textflag.h"

View File

@@ -1,15 +1,29 @@
// Code generated by command: go run asm.go -out components.s -stubs stub.go. DO NOT EDIT.
package components
func FieldByte(s Struct) byte
func FieldInt8(s Struct) int8
func FieldUint16(s Struct) uint16
func FieldInt32(s Struct) int32
func FieldUint64(s Struct) uint64
func FieldFloat32(s Struct) float32
func FieldFloat64(s Struct) float64
func FieldStringLen(s Struct) int
func FieldSliceCap(s Struct) int
func FieldArrayTwoBTwo(s Struct) byte
func FieldArrayOneC(s Struct) uint16
func FieldComplex64Imag(s Struct) float32
func FieldComplex128Real(s Struct) float64

View File

@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out fnv1a.s -stubs stub.go. DO NOT EDIT.
#include "textflag.h"

View File

@@ -1,3 +1,5 @@
// Code generated by command: go run asm.go -out fnv1a.s -stubs stub.go. DO NOT EDIT.
package fnv1a
func Hash64(data []byte) uint64

View File

@@ -1,3 +1,5 @@
// Code generated by command: go run asm.go -out sum.s -stubs stub.go. DO NOT EDIT.
package sum
func Sum(xs []uint64) uint64

View File

@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out sum.s -stubs stub.go. DO NOT EDIT.
#include "textflag.h"

View File

@@ -46,7 +46,7 @@ func main() {
log.Fatalf("unknown generator type '%s'", t)
}
g := builder(printer.NewDefaultConfig())
g := builder(printer.NewArgvConfig())
// Determine output writer.
w := os.Stdout

View File

@@ -71,7 +71,7 @@ func TestInstructionProperties(t *testing.T) {
}
func TestAssembles(t *testing.T) {
g := gen.NewAsmTest(printer.NewDefaultConfig())
g := gen.NewAsmTest(printer.NewArgvConfig())
b, err := g.Generate(inst.Instructions)
if err != nil {
t.Fatal(err)

View File

@@ -22,7 +22,7 @@ func Load(t *testing.T) []inst.Instruction {
func TestAssembles(t *testing.T) {
is := Load(t)
g := gen.NewAsmTest(printer.NewDefaultConfig())
g := gen.NewAsmTest(printer.NewArgvConfig())
b, err := g.Generate(is)
if err != nil {
t.Fatal(err)

View File

@@ -29,6 +29,7 @@ func (p *goasm) Print(f *avo.File) ([]byte, error) {
}
func (p *goasm) header() {
p.Comment(p.cfg.GeneratedWarning())
p.NL()
p.include("textflag.h")
}

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/mmcloughlin/avo"
@@ -22,13 +23,34 @@ type Config struct {
}
func NewDefaultConfig() Config {
cfg := Config{
return Config{
Name: "avo",
Pkg: pkg(),
}
}
func NewArgvConfig() Config {
return Config{
Argv: os.Args,
Pkg: pkg(),
}
if cwd, err := os.Getwd(); err == nil {
cfg.Pkg = filepath.Base(cwd)
}
return cfg
// NewGoRunConfig produces a Config for a generator that's expected to be
// executed via "go run ...".
func NewGoRunConfig() Config {
path := mainfile()
if path == "" {
return NewDefaultConfig()
}
argv := []string{"go", "run", filepath.Base(path)}
if len(os.Args) > 1 {
argv = append(argv, os.Args[1:]...)
}
return Config{
Argv: argv,
Pkg: pkg(),
}
}
func (c Config) GeneratedBy() string {
@@ -41,3 +63,33 @@ func (c Config) GeneratedBy() string {
func (c Config) GeneratedWarning() string {
return fmt.Sprintf("Code generated by %s. DO NOT EDIT.", c.GeneratedBy())
}
// mainfile attempts to determine the file path of the main function by
// inspecting the stack. Returns empty string on failure.
func mainfile() string {
pc := make([]uintptr, 10)
n := runtime.Callers(0, pc)
if n == 0 {
return ""
}
pc = pc[:n]
frames := runtime.CallersFrames(pc)
for {
frame, more := frames.Next()
if frame.Function == "main.main" {
return frame.File
}
if !more {
break
}
}
return ""
}
// pkg guesses the name of the package from the working directory.
func pkg() string {
if cwd, err := os.Getwd(); err == nil {
return filepath.Base(cwd)
}
return ""
}

View File

@@ -15,8 +15,11 @@ func NewStubs(cfg Config) Printer {
}
func (s *stubs) Print(f *avo.File) ([]byte, error) {
s.Printf("package %s\n\n", s.cfg.Pkg)
s.Comment(s.cfg.GeneratedWarning())
s.NL()
s.Printf("package %s\n", s.cfg.Pkg)
for _, fn := range f.Functions {
s.NL()
s.Printf("%s\n", fn.Stub())
}
return s.Result()