Skip to content

Commit

Permalink
printer: add generated code warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcloughlin committed Dec 19, 2018
1 parent 213d65e commit ca5c7e7
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions examples/add/add.s
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT.

#include "textflag.h"

Expand Down
2 changes: 2 additions & 0 deletions examples/add/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/complex/complex.s
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out complex.s -stubs stub.go. DO NOT EDIT.

#include "textflag.h"

Expand Down
4 changes: 4 additions & 0 deletions examples/complex/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/components/components.s
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out components.s -stubs stub.go. DO NOT EDIT.

#include "textflag.h"

Expand Down
14 changes: 14 additions & 0 deletions examples/components/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/fnv1a/fnv1a.s
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out fnv1a.s -stubs stub.go. DO NOT EDIT.

#include "textflag.h"

Expand Down
2 changes: 2 additions & 0 deletions examples/fnv1a/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/sum/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/sum/sum.s
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Code generated by command: go run asm.go -out sum.s -stubs stub.go. DO NOT EDIT.

#include "textflag.h"

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/avogen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/inst/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/load/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions printer/goasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
60 changes: 56 additions & 4 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/mmcloughlin/avo"
Expand All @@ -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)
}

// 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(),
}
return cfg
}

func (c Config) GeneratedBy() string {
Expand All @@ -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 ""
}
5 changes: 4 additions & 1 deletion printer/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ca5c7e7

Please sign in to comment.